c61d7b5c254af81718d2768f24c962728b79d026
[dragonfly.git] / sys / dev / netif / jme / if_jmevar.h
1 /*-
2  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/jme/if_jmevar.h,v 1.1 2008/05/27 01:42:01 yongari Exp $
28  * $DragonFly: src/sys/dev/netif/jme/if_jmevar.h,v 1.8 2008/11/26 11:55:18 sephe Exp $
29  */
30
31 #ifndef _IF_JMEVAR_H
32 #define _IF_JMEVAR_H
33
34 #include <sys/queue.h>
35 #include <sys/callout.h>
36 #include <sys/taskqueue.h>
37
38 /*
39  * JMC250 supports upto 1024 descriptors and the number of
40  * descriptors should be multiple of JME_NDESC_ALIGN.
41  */
42 #define JME_TX_RING_CNT_DEF     384
43 #define JME_RX_RING_CNT_DEF     256
44 #define JME_NDESC_ALIGN         16
45
46 /*
47  * Tx/Rx descriptor queue base should be 16bytes aligned and
48  * should not cross 4G bytes boundary on the 64bits address
49  * mode.
50  */
51 #define JME_TX_RING_ALIGN       16
52 #define JME_RX_RING_ALIGN       16
53 #define JME_TSO_MAXSEGSIZE      4096
54 #define JME_TSO_MAXSIZE         (65535 + sizeof(struct ether_vlan_header))
55 #define JME_MAXTXSEGS           32
56 #define JME_RX_BUF_ALIGN        sizeof(uint64_t)
57 #define JME_SSB_ALIGN           16
58
59 #define JME_ADDR_LO(x)          ((uint64_t) (x) & 0xFFFFFFFF)
60 #define JME_ADDR_HI(x)          ((uint64_t) (x) >> 32)
61
62 #define JME_MSI_MESSAGES        8
63 #define JME_MSIX_MESSAGES       8
64
65 /* Water mark to kick reclaiming Tx buffers. */
66 #define JME_TX_DESC_HIWAT(sc)   \
67         ((sc)->jme_tx_ring_cnt - (((sc)->jme_tx_ring_cnt * 3) / 10))
68
69 /*
70  * JMC250 can send 9K jumbo frame on Tx path and can receive
71  * 65535 bytes.
72  */
73 #define JME_JUMBO_FRAMELEN      9216
74 #define JME_JUMBO_MTU                                                   \
75         (JME_JUMBO_FRAMELEN - sizeof(struct ether_vlan_header) -        \
76          ETHER_HDR_LEN - ETHER_CRC_LEN)
77 #define JME_MAX_MTU                                                     \
78         (ETHER_MAX_LEN + sizeof(struct ether_vlan_header) -             \
79          ETHER_HDR_LEN - ETHER_CRC_LEN)
80 /*
81  * JMC250 can't handle Tx checksum offload/TSO if frame length
82  * is larger than its FIFO size(2K). It's also good idea to not
83  * use jumbo frame if hardware is running at half-duplex media.
84  * Because the jumbo frame may not fit into the Tx FIFO,
85  * collisions make hardware fetch frame from host memory with
86  * DMA again which in turn slows down Tx performance
87  * significantly.
88  */
89 #define JME_TX_FIFO_SIZE        2000
90 /*
91  * JMC250 has just 4K Rx FIFO. To support jumbo frame that is
92  * larger than 4K bytes in length, Rx FIFO threshold should be
93  * adjusted to minimize Rx FIFO overrun.
94  */
95 #define JME_RX_FIFO_SIZE        4000
96
97 #define JME_DESC_INC(x, y)      ((x) = ((x) + 1) % (y))
98
99 struct jme_txdesc {
100         struct mbuf             *tx_m;
101         bus_dmamap_t            tx_dmamap;
102         int                     tx_ndesc;
103         struct jme_desc         *tx_desc;
104 };
105
106 struct jme_rxdesc {
107         struct mbuf             *rx_m;
108         bus_dmamap_t            rx_dmamap;
109         struct jme_desc         *rx_desc;
110 };
111
112 struct jme_chain_data{
113         bus_dma_tag_t           jme_ring_tag;
114         bus_dma_tag_t           jme_buffer_tag;
115         bus_dma_tag_t           jme_ssb_tag;
116         bus_dmamap_t            jme_ssb_map;
117         bus_dma_tag_t           jme_tx_tag;
118         struct jme_txdesc       *jme_txdesc;
119         bus_dma_tag_t           jme_rx_tag;
120         struct jme_rxdesc       *jme_rxdesc;
121         bus_dma_tag_t           jme_tx_ring_tag;
122         bus_dmamap_t            jme_tx_ring_map;
123         bus_dma_tag_t           jme_rx_ring_tag;
124         bus_dmamap_t            jme_rx_ring_map;
125         bus_dmamap_t            jme_rx_sparemap;
126
127         int                     jme_tx_prod;
128         int                     jme_tx_cons;
129         int                     jme_tx_cnt;
130
131         int                     jme_rx_cons;
132         int                     jme_rxlen;
133         struct mbuf             *jme_rxhead;
134         struct mbuf             *jme_rxtail;
135 };
136
137 struct jme_ring_data {
138         struct jme_desc         *jme_tx_ring;
139         bus_addr_t              jme_tx_ring_paddr;
140         struct jme_desc         *jme_rx_ring;
141         bus_addr_t              jme_rx_ring_paddr;
142         struct jme_ssb          *jme_ssb_block;
143         bus_addr_t              jme_ssb_block_paddr;
144 };
145
146 #define JME_TX_RING_ADDR(sc, i) \
147     ((sc)->jme_rdata.jme_tx_ring_paddr + sizeof(struct jme_desc) * (i))
148 #define JME_RX_RING_ADDR(sc, i) \
149     ((sc)->jme_rdata.jme_rx_ring_paddr + sizeof(struct jme_desc) * (i))
150
151 #define JME_TX_RING_SIZE(sc)    \
152     (sizeof(struct jme_desc) * (sc)->jme_tx_ring_cnt)
153 #define JME_RX_RING_SIZE(sc)    \
154     (sizeof(struct jme_desc) * (sc)->jme_rx_ring_cnt)
155 #define JME_SSB_SIZE            sizeof(struct jme_ssb)
156
157 struct jme_dmamap_ctx {
158         int                     nsegs;
159         bus_dma_segment_t       *segs;
160 };
161
162 /*
163  * Software state per device.
164  */
165 struct jme_softc {
166         struct arpcom           arpcom;
167         device_t                jme_dev;
168
169         int                     jme_mem_rid;
170         struct resource         *jme_mem_res;
171         bus_space_tag_t         jme_mem_bt;
172         bus_space_handle_t      jme_mem_bh;
173
174         int                     jme_irq_rid;
175         struct resource         *jme_irq_res;
176         void                    *jme_irq_handle;
177
178         device_t                jme_miibus;
179         int                     jme_phyaddr;
180         bus_addr_t              jme_lowaddr;
181
182         uint32_t                jme_clksrc;
183         uint32_t                jme_clksrc_1000;
184         uint32_t                jme_tx_dma_size;
185         uint32_t                jme_rx_dma_size;
186
187         uint32_t                jme_caps;
188 #define JME_CAP_FPGA            0x0001
189 #define JME_CAP_PCIE            0x0002
190 #define JME_CAP_PMCAP           0x0004
191 #define JME_CAP_FASTETH         0x0008
192 #define JME_CAP_JUMBO           0x0010
193
194         uint32_t                jme_workaround;
195 #define JME_WA_EXTFIFO          0x0001
196 #define JME_WA_HDX              0x0002
197
198         uint32_t                jme_flags;
199 #define JME_FLAG_MSI            0x0001
200 #define JME_FLAG_MSIX           0x0002
201 #define JME_FLAG_DETACH         0x0004
202 #define JME_FLAG_LINK           0x0008
203
204         struct callout          jme_tick_ch;
205         struct jme_chain_data   jme_cdata;
206         struct jme_ring_data    jme_rdata;
207         int                     jme_if_flags;
208         uint32_t                jme_txcsr;
209         uint32_t                jme_rxcsr;
210
211         int                     jme_txd_spare;
212
213         struct sysctl_ctx_list  jme_sysctl_ctx;
214         struct sysctl_oid       *jme_sysctl_tree;
215
216         /*
217          * Sysctl variables
218          */
219         int                     jme_tx_coal_to;
220         int                     jme_tx_coal_pkt;
221         int                     jme_rx_coal_to;
222         int                     jme_rx_coal_pkt;
223         int                     jme_rx_ring_cnt;
224         int                     jme_tx_ring_cnt;
225 };
226
227 /* Register access macros. */
228 #define CSR_WRITE_4(_sc, reg, val)      \
229         bus_space_write_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg), (val))
230 #define CSR_READ_4(_sc, reg)            \
231         bus_space_read_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg))
232
233 #define JME_MAXERR      5
234
235 #define JME_RXCHAIN_RESET(_sc)                                          \
236 do {                                                                    \
237         (_sc)->jme_cdata.jme_rxhead = NULL;                             \
238         (_sc)->jme_cdata.jme_rxtail = NULL;                             \
239         (_sc)->jme_cdata.jme_rxlen = 0;                                 \
240 } while (0)
241
242 #define JME_TX_TIMEOUT          5
243 #define JME_TIMEOUT             1000
244 #define JME_PHY_TIMEOUT         1000
245 #define JME_EEPROM_TIMEOUT      1000
246
247 #define JME_TXD_RSVD            1
248
249 #endif