349ab25ce65d82953b8ee5e48dc64be9888dedff
[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 JME_NDESC_MAX descriptors and the number of
40  * descriptors should be multiple of JME_NDESC_ALIGN.
41  */
42 #define JME_TX_DESC_CNT_DEF     512
43 #define JME_RX_DESC_CNT_DEF     512
44
45 #define JME_NDESC_ALIGN         16
46 #define JME_NDESC_MAX           1024
47
48 #define JME_NRXRING_1           1
49 #define JME_NRXRING_2           2
50 #define JME_NRXRING_4           4
51
52 #define JME_NRXRING_MIN         JME_NRXRING_1
53 #define JME_NRXRING_MAX         JME_NRXRING_4
54
55 /* RX rings + TX ring + status */
56 #define JME_NSERIALIZE          (JME_NRXRING_MAX + 1 + 1)
57
58 /* RX rings + TX ring + status */
59 #define JME_MSIXCNT(nrx)        ((nrx) + 1 + 1)
60 #define JME_NMSIX               JME_MSIXCNT(JME_NRXRING_MAX)
61
62 /*
63  * Tx/Rx descriptor queue base should be 16bytes aligned and
64  * should not cross 4G bytes boundary on the 64bits address
65  * mode.
66  */
67 #define JME_TX_RING_ALIGN       __VM_CACHELINE_SIZE
68 #define JME_RX_RING_ALIGN       __VM_CACHELINE_SIZE
69 #define JME_MAXSEGSIZE          4096
70 #define JME_TSO_MAXSIZE         (IP_MAXPACKET + sizeof(struct ether_vlan_header))
71 #define JME_MAXTXSEGS           40
72 #define JME_RX_BUF_ALIGN        sizeof(uint64_t)
73 #define JME_SSB_ALIGN           __VM_CACHELINE_SIZE
74
75 #if (BUS_SPACE_MAXADDR != BUS_SPACE_MAXADDR_32BIT)
76 #define JME_RING_BOUNDARY       0x100000000ULL
77 #else
78 #define JME_RING_BOUNDARY       0
79 #endif
80
81 #define JME_ADDR_LO(x)          ((uint64_t) (x) & 0xFFFFFFFF)
82 #define JME_ADDR_HI(x)          ((uint64_t) (x) >> 32)
83
84 /* Water mark to kick reclaiming Tx buffers. */
85 #define JME_TX_DESC_HIWAT(sc)   \
86         ((sc)->jme_cdata.jme_tx_desc_cnt - \
87          (((sc)->jme_cdata.jme_tx_desc_cnt * 3) / 10))
88
89 /*
90  * JMC250 can send 9K jumbo frame on Tx path and can receive
91  * 65535 bytes.
92  */
93 #define JME_JUMBO_FRAMELEN      9216
94 #define JME_JUMBO_MTU                                                   \
95         (JME_JUMBO_FRAMELEN - sizeof(struct ether_vlan_header) -        \
96          ETHER_HDR_LEN - ETHER_CRC_LEN)
97 #define JME_MAX_MTU                                                     \
98         (ETHER_MAX_LEN + sizeof(struct ether_vlan_header) -             \
99          ETHER_HDR_LEN - ETHER_CRC_LEN)
100 /*
101  * JMC250 can't handle Tx checksum offload/TSO if frame length
102  * is larger than its FIFO size(2K). It's also good idea to not
103  * use jumbo frame if hardware is running at half-duplex media.
104  * Because the jumbo frame may not fit into the Tx FIFO,
105  * collisions make hardware fetch frame from host memory with
106  * DMA again which in turn slows down Tx performance
107  * significantly.
108  */
109 #define JME_TX_FIFO_SIZE        2000
110 /*
111  * JMC250 has just 4K Rx FIFO. To support jumbo frame that is
112  * larger than 4K bytes in length, Rx FIFO threshold should be
113  * adjusted to minimize Rx FIFO overrun.
114  */
115 #define JME_RX_FIFO_SIZE        4000
116
117 #define JME_DESC_INC(x, y)      ((x) = ((x) + 1) % (y))
118 #define JME_DESC_ADD(x, d, y)   ((x) = ((x) + (d)) % (y))
119
120 struct jme_txdesc {
121         struct mbuf             *tx_m;
122         bus_dmamap_t            tx_dmamap;
123         int                     tx_ndesc;
124         struct jme_desc         *tx_desc;
125 };
126
127 struct jme_rxdesc {
128         struct mbuf             *rx_m;
129         bus_addr_t              rx_paddr;
130         bus_dmamap_t            rx_dmamap;
131         struct jme_desc         *rx_desc;
132 };
133
134 struct jme_softc;
135
136 /*
137  * RX ring/descs
138  */
139 struct jme_rxdata {
140         struct lwkt_serialize   jme_rx_serialize;
141         struct jme_softc        *jme_sc;
142
143         uint32_t                jme_rx_coal;
144         uint32_t                jme_rx_comp;
145         uint32_t                jme_rx_empty;
146         int                     jme_rx_idx;
147
148         bus_dma_tag_t           jme_rx_tag;     /* RX mbuf tag */
149         bus_dmamap_t            jme_rx_sparemap;
150         struct jme_rxdesc       *jme_rxdesc;
151
152         struct jme_desc         *jme_rx_ring;
153         int                     jme_rx_cons;
154         int                     jme_rx_desc_cnt;
155
156         int                     jme_rxlen;
157         struct mbuf             *jme_rxhead;
158         struct mbuf             *jme_rxtail;
159
160         u_long                  jme_rx_pkt;
161         u_long                  jme_rx_emp;
162
163         bus_addr_t              jme_rx_ring_paddr;
164         bus_dma_tag_t           jme_rx_ring_tag;
165         bus_dmamap_t            jme_rx_ring_map;
166 } __cachealign;
167
168 struct jme_chain_data {
169         /*
170          * TX ring/descs
171          */
172         struct lwkt_serialize   jme_tx_serialize;
173         struct jme_softc        *jme_sc;
174         bus_dma_tag_t           jme_tx_tag;     /* TX mbuf tag */
175         struct jme_txdesc       *jme_txdesc;
176
177         struct jme_desc         *jme_tx_ring;
178
179         int                     jme_tx_prod;
180         int                     jme_tx_cons;
181         int                     jme_tx_cnt;
182         int                     jme_tx_desc_cnt;
183
184         bus_addr_t              jme_tx_ring_paddr;
185         bus_dma_tag_t           jme_tx_ring_tag;
186         bus_dmamap_t            jme_tx_ring_map;
187
188         int                     jme_rx_ring_cnt;
189         struct jme_rxdata       jme_rx_data[JME_NRXRING_MAX];
190
191         /*
192          * Top level tags
193          */
194         bus_dma_tag_t           jme_ring_tag;   /* parent ring tag */
195         bus_dma_tag_t           jme_buffer_tag; /* parent mbuf/ssb tag */
196
197         /*
198          * Shadow status block
199          */
200         struct jme_ssb          *jme_ssb_block;
201         bus_addr_t              jme_ssb_block_paddr;
202         bus_dma_tag_t           jme_ssb_tag;
203         bus_dmamap_t            jme_ssb_map;
204 } __cachealign;
205
206 struct jme_msix_data {
207         int                     jme_msix_rid;
208         int                     jme_msix_cpuid;
209         u_int                   jme_msix_vector;
210         uint32_t                jme_msix_intrs;
211         struct resource         *jme_msix_res;
212         void                    *jme_msix_handle;
213         struct lwkt_serialize   *jme_msix_serialize;
214         char                    jme_msix_desc[64];
215
216         driver_intr_t           *jme_msix_func;
217         void                    *jme_msix_arg;
218 };
219
220 #define JME_TX_RING_SIZE(sc)    \
221     (sizeof(struct jme_desc) * (sc)->jme_cdata.jme_tx_desc_cnt)
222 #define JME_RX_RING_SIZE(rdata) \
223     (sizeof(struct jme_desc) * (rdata)->jme_rx_desc_cnt)
224 #define JME_SSB_SIZE            sizeof(struct jme_ssb)
225
226 /*
227  * Software state per device.
228  */
229 struct jme_softc {
230         struct arpcom           arpcom;
231         device_t                jme_dev;
232
233         int                     jme_mem_rid;
234         struct resource         *jme_mem_res;
235         bus_space_tag_t         jme_mem_bt;
236         bus_space_handle_t      jme_mem_bh;
237
238         int                     jme_irq_type;
239         int                     jme_irq_rid;
240         struct resource         *jme_irq_res;
241         void                    *jme_irq_handle;
242         struct jme_msix_data    jme_msix[JME_NMSIX];
243         int                     jme_msix_cnt;
244         uint32_t                jme_msinum[JME_MSINUM_CNT];
245         int                     jme_tx_cpuid;
246
247         int                     jme_npoll_rxoff;
248         int                     jme_npoll_txoff;
249
250         device_t                jme_miibus;
251         int                     jme_phyaddr;
252         bus_addr_t              jme_lowaddr;
253
254         uint32_t                jme_clksrc;
255         uint32_t                jme_clksrc_1000;
256         uint32_t                jme_tx_dma_size;
257         uint32_t                jme_rx_dma_size;
258
259         uint32_t                jme_caps;
260 #define JME_CAP_FPGA            0x0001
261 #define JME_CAP_PCIE            0x0002
262 #define JME_CAP_PMCAP           0x0004
263 #define JME_CAP_FASTETH         0x0008
264 #define JME_CAP_JUMBO           0x0010
265
266         uint32_t                jme_workaround;
267 #define JME_WA_EXTFIFO          0x0001
268 #define JME_WA_HDX              0x0002
269
270         boolean_t               jme_has_link;
271         boolean_t               jme_in_tick;
272
273         struct lwkt_serialize   jme_serialize;
274         struct lwkt_serialize   *jme_serialize_arr[JME_NSERIALIZE];
275         int                     jme_serialize_cnt;
276
277         struct callout          jme_tick_ch;
278         struct jme_chain_data   jme_cdata;
279         int                     jme_if_flags;
280         uint32_t                jme_txcsr;
281         uint32_t                jme_rxcsr;
282
283         struct sysctl_ctx_list  jme_sysctl_ctx;
284         struct sysctl_oid       *jme_sysctl_tree;
285
286         /*
287          * Sysctl variables
288          */
289         int                     jme_tx_coal_to;
290         int                     jme_tx_coal_pkt;
291         int                     jme_rx_coal_to;
292         int                     jme_rx_coal_pkt;
293         int                     jme_rss_debug;
294 };
295
296 /* Register access macros. */
297 #define CSR_WRITE_4(_sc, reg, val)      \
298         bus_space_write_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg), (val))
299 #define CSR_READ_4(_sc, reg)            \
300         bus_space_read_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg))
301
302 #define JME_MAXERR      5
303
304 #define JME_RXCHAIN_RESET(rdata)        \
305 do {                                    \
306         (rdata)->jme_rxhead = NULL;     \
307         (rdata)->jme_rxtail = NULL;     \
308         (rdata)->jme_rxlen = 0;         \
309 } while (0)
310
311 #define JME_TX_TIMEOUT          5
312 #define JME_TIMEOUT             1000
313 #define JME_PHY_TIMEOUT         1000
314 #define JME_EEPROM_TIMEOUT      1000
315
316 #define JME_TXD_RSVD            1
317 /* Large enough to cooperate 64K TSO segment and one spare TX descriptor */
318 #define JME_TXD_SPARE           34
319
320 #define JME_ENABLE_HWRSS(sc)    \
321         ((sc)->jme_cdata.jme_rx_ring_cnt > JME_NRXRING_MIN)
322
323 #endif