4cc5dfc3b3c397d1d330ee7b05b13d27e3e0fa5b
[dragonfly.git] / sys / dev / netif / ixgbe / ixgbe.h
1 /******************************************************************************
2
3   Copyright (c) 2001-2012, Intel Corporation 
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, are permitted provided that the following conditions are met:
8   
9    1. Redistributions of source code must retain the above copyright notice, 
10       this list of conditions and the following disclaimer.
11   
12    2. Redistributions in binary form must reproduce the above copyright 
13       notice, this list of conditions and the following disclaimer in the 
14       documentation and/or other materials provided with the distribution.
15   
16    3. Neither the name of the Intel Corporation nor the names of its 
17       contributors may be used to endorse or promote products derived from 
18       this software without specific prior written permission.
19   
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31
32 ******************************************************************************/
33 /*$FreeBSD: src/sys/dev/ixgbe/ixgbe.h,v 1.26 2012/04/23 22:05:09 bz Exp $*/
34
35
36 #ifndef _IXGBE_H_
37 #define _IXGBE_H_
38
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #if __FreeBSD_version >= 800000
43 #include <sys/buf_ring.h>
44 #endif
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/sockio.h>
52
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/ifq_var.h>
56 #include <net/bpf.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60
61 #include <net/bpf.h>
62 #include <net/if_types.h>
63 #include <net/vlan/if_vlan_var.h>
64 #include <net/vlan/if_vlan_ether.h>
65
66 #include <netinet/in_systm.h>
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
71 #include <netinet/tcp.h>
72 #ifdef NET_LRO
73 #include <netinet/tcp_lro.h>    /* XXX: IPv4 only */
74 #endif
75 #include <netinet/udp.h>
76
77 #include <sys/in_cksum.h>
78
79 #include <sys/bus.h>
80 #include <sys/rman.h>
81 #include <sys/resource.h>
82 #include <vm/vm.h>
83 #include <vm/pmap.h>
84 #include <machine/clock.h>
85 #include <bus/pci/pcivar.h>
86 #include <bus/pci/pcireg.h>
87 #include <sys/proc.h>
88 #include <sys/sysctl.h>
89 #include <sys/endian.h>
90 #include <sys/taskqueue.h>
91 #include <machine/smp.h>
92
93 #include <sys/spinlock.h>
94 #include <sys/spinlock2.h>
95
96 #ifdef IXGBE_IEEE1588
97 #include <sys/ieee1588.h>
98 #endif
99
100 #include "ixgbe_api.h"
101 #include "ixgbe_defines.h"
102
103 /* Tunables */
104
105 /*
106  * TxDescriptors Valid Range: 64-4096 Default Value: 256 This value is the
107  * number of transmit descriptors allocated by the driver. Increasing this
108  * value allows the driver to queue more transmits. Each descriptor is 16
109  * bytes. Performance tests have show the 2K value to be optimal for top
110  * performance.
111  */
112 #define DEFAULT_TXD     1024
113 #define PERFORM_TXD     2048
114 #define MAX_TXD         4096
115 #define MIN_TXD         64
116
117 /*
118  * RxDescriptors Valid Range: 64-4096 Default Value: 256 This value is the
119  * number of receive descriptors allocated for each RX queue. Increasing this
120  * value allows the driver to buffer more incoming packets. Each descriptor
121  * is 16 bytes.  A receive buffer is also allocated for each descriptor. 
122  * 
123  * Note: with 8 rings and a dual port card, it is possible to bump up 
124  *      against the system mbuf pool limit, you can tune nmbclusters
125  *      to adjust for this.
126  */
127 #define DEFAULT_RXD     1024
128 #define PERFORM_RXD     2048
129 #define MAX_RXD         4096
130 #define MIN_RXD         64
131
132 /* Alignment for rings */
133 #define DBA_ALIGN       128
134
135 /*
136  * This parameter controls the maximum no of times the driver will loop in
137  * the isr. Minimum Value = 1
138  */
139 #define MAX_LOOP        10
140
141 /*
142  * This is the max watchdog interval, ie. the time that can
143  * pass between any two TX clean operations, such only happening
144  * when the TX hardware is functioning.
145  */
146 #define IXGBE_WATCHDOG                   (10 * hz)
147
148 /*
149  * This parameters control when the driver calls the routine to reclaim
150  * transmit descriptors.
151  */
152 #define IXGBE_TX_CLEANUP_THRESHOLD      (adapter->num_tx_desc / 8)
153 #define IXGBE_TX_OP_THRESHOLD           (adapter->num_tx_desc / 32)
154
155 #define IXGBE_MAX_FRAME_SIZE    0x3F00
156
157 /* Flow control constants */
158 #define IXGBE_FC_PAUSE          0xFFFF
159 #define IXGBE_FC_HI             0x20000
160 #define IXGBE_FC_LO             0x10000
161
162 /* Keep older OS drivers building... */
163 #if !defined(SYSCTL_ADD_UQUAD)
164 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
165 #endif
166
167 /* Defines for printing debug information */
168 #define DEBUG_INIT  0
169 #define DEBUG_IOCTL 0
170 #define DEBUG_HW    0
171
172 #define INIT_DEBUGOUT(S)            if (DEBUG_INIT)  kprintf(S "\n")
173 #define INIT_DEBUGOUT1(S, A)        if (DEBUG_INIT)  kprintf(S "\n", A)
174 #define INIT_DEBUGOUT2(S, A, B)     if (DEBUG_INIT)  kprintf(S "\n", A, B)
175 #define IOCTL_DEBUGOUT(S)           if (DEBUG_IOCTL) kprintf(S "\n")
176 #define IOCTL_DEBUGOUT1(S, A)       if (DEBUG_IOCTL) kprintf(S "\n", A)
177 #define IOCTL_DEBUGOUT2(S, A, B)    if (DEBUG_IOCTL) kprintf(S "\n", A, B)
178 #define HW_DEBUGOUT(S)              if (DEBUG_HW) kprintf(S "\n")
179 #define HW_DEBUGOUT1(S, A)          if (DEBUG_HW) kprintf(S "\n", A)
180 #define HW_DEBUGOUT2(S, A, B)       if (DEBUG_HW) kprintf(S "\n", A, B)
181
182 #define MAX_NUM_MULTICAST_ADDRESSES     128
183 #define IXGBE_82598_SCATTER             100
184 #define IXGBE_82599_SCATTER             32
185 #define MSIX_82598_BAR                  3
186 #define MSIX_82599_BAR                  4
187 #define IXGBE_TSO_SIZE                  262140
188 #define IXGBE_TX_BUFFER_SIZE            ((u32) 1514)
189 #define IXGBE_RX_HDR                    128
190 #define IXGBE_VFTA_SIZE                 128
191 #define IXGBE_BR_SIZE                   4096
192 #define IXGBE_QUEUE_MIN_FREE            32
193 #define IXGBE_QUEUE_IDLE                1
194 #define IXGBE_QUEUE_WORKING             2
195 #define IXGBE_QUEUE_HUNG                4
196 #define IXGBE_QUEUE_DEPLETED            8
197
198 /* Offload bits in mbuf flag */
199 #if __FreeBSD_version >= 800000
200 #define CSUM_OFFLOAD            (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
201 #else
202 #define CSUM_OFFLOAD            (CSUM_IP|CSUM_TCP|CSUM_UDP)
203 #endif
204
205 /* For 6.X code compatibility */
206 #if !defined(ETHER_BPF_MTAP)
207 #define ETHER_BPF_MTAP          BPF_MTAP
208 #endif
209
210 #if __FreeBSD_version < 700000
211 #define CSUM_TSO                0
212 #define IFCAP_TSO4              0
213 #endif
214
215 /*
216  * Interrupt Moderation parameters 
217  */
218 #define IXGBE_LOW_LATENCY       128
219 #define IXGBE_AVE_LATENCY       400
220 #define IXGBE_BULK_LATENCY      1200
221 #define IXGBE_LINK_ITR          2000
222
223 /*
224  *****************************************************************************
225  * vendor_info_array
226  * 
227  * This array contains the list of Subvendor/Subdevice IDs on which the driver
228  * should load.
229  * 
230  *****************************************************************************
231  */
232 typedef struct _ixgbe_vendor_info_t {
233         unsigned int    vendor_id;
234         unsigned int    device_id;
235         unsigned int    subvendor_id;
236         unsigned int    subdevice_id;
237         unsigned int    index;
238 } ixgbe_vendor_info_t;
239
240
241 struct ixgbe_tx_buf {
242         u32             eop_index;
243         struct mbuf     *m_head;
244         bus_dmamap_t    map;
245 };
246
247 struct ixgbe_rx_buf {
248         struct mbuf     *m_head;
249         struct mbuf     *m_pack;
250         struct mbuf     *fmp;
251         bus_dmamap_t    hmap;
252         bus_dmamap_t    pmap;
253 };
254
255 /*
256  * Bus dma allocation structure used by ixgbe_dma_malloc and ixgbe_dma_free.
257  */
258 struct ixgbe_dma_alloc {
259         bus_addr_t              dma_paddr;
260         caddr_t                 dma_vaddr;
261         bus_dma_tag_t           dma_tag;
262         bus_dmamap_t            dma_map;
263         bus_dma_segment_t       dma_seg;
264         bus_size_t              dma_size;
265         int                     dma_nseg;
266 };
267
268 /*
269 ** Driver queue struct: this is the interrupt container
270 **  for the associated tx and rx ring.
271 */
272 struct ix_queue {
273         struct adapter          *adapter;
274         u32                     msix;           /* This queue's MSIX vector */
275         u32                     eims;           /* This queue's EIMS bit */
276         u32                     eitr_setting;
277         struct resource         *res;
278         void                    *tag;
279         struct tx_ring          *txr;
280         struct rx_ring          *rxr;
281         struct task             que_task;
282         struct taskqueue        *tq;
283         u64                     irqs;
284         struct lwkt_serialize   serializer;
285 };
286
287 /*
288  * The transmit ring, one per queue
289  */
290 struct tx_ring {
291         struct adapter          *adapter;
292         struct lock             tx_lock;
293         u32                     me;
294         int                     queue_status;
295         int                     watchdog_time;
296         union ixgbe_adv_tx_desc *tx_base;
297         struct ixgbe_dma_alloc  txdma;
298         u32                     next_avail_desc;
299         u32                     next_to_clean;
300         struct ixgbe_tx_buf     *tx_buffers;
301         volatile u16            tx_avail;
302         u32                     txd_cmd;
303         bus_dma_tag_t           txtag;
304         char                    lock_name[16];
305 #if __FreeBSD_version >= 800000
306         struct buf_ring         *br;
307 #endif
308 #ifdef IXGBE_FDIR
309         u16                     atr_sample;
310         u16                     atr_count;
311 #endif
312         u32                     bytes;  /* used for AIM */
313         u32                     packets;
314         /* Soft Stats */
315         u64                     no_desc_avail;
316         u64                     total_packets;
317 };
318
319
320 /*
321  * The Receive ring, one per rx queue
322  */
323 struct rx_ring {
324         struct adapter          *adapter;
325         struct lock             rx_lock;
326         u32                     me;
327         union ixgbe_adv_rx_desc *rx_base;
328         struct ixgbe_dma_alloc  rxdma;
329 #ifdef NET_LRO
330         struct lro_ctrl         lro;
331 #endif
332         bool                    lro_enabled;
333         bool                    hdr_split;
334         bool                    hw_rsc;
335         bool                    discard;
336         bool                    vtag_strip;
337         u32                     next_to_refresh;
338         u32                     next_to_check;
339         char                    lock_name[16];
340         struct ixgbe_rx_buf     *rx_buffers;
341         bus_dma_tag_t           htag;
342         bus_dma_tag_t           ptag;
343
344         u32                     bytes; /* Used for AIM calc */
345         u32                     packets;
346
347         /* Soft stats */
348         u64                     rx_irq;
349         u64                     rx_split_packets;
350         u64                     rx_packets;
351         u64                     rx_bytes;
352         u64                     rx_discarded;
353         u64                     rsc_num;
354 #ifdef IXGBE_FDIR
355         u64                     flm;
356 #endif
357 };
358
359 /* Our adapter structure */
360 struct adapter {
361         struct ifnet            *ifp;
362         struct ixgbe_hw         hw;
363
364         struct ixgbe_osdep      osdep;
365         struct device           *dev;
366
367         struct resource         *pci_mem;
368         struct resource         *msix_mem;
369
370         /*
371          * Interrupt resources: this set is
372          * either used for legacy, or for Link
373          * when doing MSIX
374          */
375         void                    *tag;
376         struct resource         *res;
377         int                     intr_type;
378
379         struct ifmedia          media;
380         struct callout          timer;
381         int                     msix;
382         int                     if_flags;
383
384         struct lock             core_lock;
385         struct spinlock         mcast_spin;
386
387         eventhandler_tag        vlan_attach;
388         eventhandler_tag        vlan_detach;
389
390         u16                     num_vlans;
391         u16                     num_queues;
392
393         /*
394         ** Shadow VFTA table, this is needed because
395         ** the real vlan filter table gets cleared during
396         ** a soft reset and the driver needs to be able
397         ** to repopulate it.
398         */
399         u32                     shadow_vfta[IXGBE_VFTA_SIZE];
400
401         /* Info about the interface */
402         u32                     optics;
403         u32                     fc; /* local flow ctrl setting */
404         int                     advertise;  /* link speeds */
405         bool                    link_active;
406         u16                     max_frame_size;
407         u16                     num_segs;
408         u32                     link_speed;
409         bool                    link_up;
410         u32                     linkvec;
411
412         /* Mbuf cluster size */
413         u32                     rx_mbuf_sz;
414
415         /* Support for pluggable optics */
416         bool                    sfp_probe;
417         struct task             link_task;  /* Link tasklet */
418         struct task             mod_task;   /* SFP tasklet */
419         struct task             msf_task;   /* Multispeed Fiber */
420 #ifdef IXGBE_FDIR
421         int                     fdir_reinit;
422         struct task             fdir_task;
423 #endif
424         struct taskqueue        *tq;
425
426         /*
427         ** Queues: 
428         **   This is the irq holder, it has
429         **   and RX/TX pair or rings associated
430         **   with it.
431         */
432         struct ix_queue         *queues;
433
434         /*
435          * Transmit rings:
436          *      Allocated at run time, an array of rings.
437          */
438         struct tx_ring          *tx_rings;
439         int                     num_tx_desc;
440
441         /*
442          * Receive rings:
443          *      Allocated at run time, an array of rings.
444          */
445         struct rx_ring          *rx_rings;
446         int                     num_rx_desc;
447         u64                     que_mask;
448         u32                     rx_process_limit;
449
450         /* Multicast array memory */
451         u8                      *mta;
452
453         /* Misc stats maintained by the driver */
454         unsigned long           dropped_pkts;
455         unsigned long           mbuf_defrag_failed;
456         unsigned long           mbuf_header_failed;
457         unsigned long           mbuf_packet_failed;
458         unsigned long           no_tx_map_avail;
459         unsigned long           no_tx_dma_setup;
460         unsigned long           watchdog_events;
461         unsigned long           tso_tx;
462         unsigned long           link_irq;
463
464         struct ixgbe_hw_stats   stats;
465
466         struct lwkt_serialize   serializer;
467         struct sysctl_ctx_list  sysctl_ctx;
468         struct sysctl_oid       *sysctl_tree;
469 };
470
471 /* Precision Time Sync (IEEE 1588) defines */
472 #define ETHERTYPE_IEEE1588      0x88F7
473 #define PICOSECS_PER_TICK       20833
474 #define TSYNC_UDP_PORT          319 /* UDP port for the protocol */
475 #define IXGBE_ADVTXD_TSTAMP     0x00080000
476
477
478 #define IXGBE_CORE_LOCK_INIT(_sc, _name) \
479         lockinit(&(_sc)->core_lock, _name, 0, LK_CANRECURSE)
480 #define IXGBE_CORE_LOCK_DESTROY(_sc)      lockuninit(&(_sc)->core_lock)
481 #define IXGBE_TX_LOCK_DESTROY(_sc)        lockuninit(&(_sc)->tx_lock)
482 #define IXGBE_RX_LOCK_DESTROY(_sc)        lockuninit(&(_sc)->rx_lock)
483 #define IXGBE_CORE_LOCK(_sc)              lockmgr(&(_sc)->core_lock, LK_EXCLUSIVE)
484 #define IXGBE_TX_LOCK(_sc)                lockmgr(&(_sc)->tx_lock, LK_EXCLUSIVE)
485 #define IXGBE_TX_TRYLOCK(_sc)             lockmgr(&(_sc)->tx_mtx, LK_EXCLUSIVE|LK_NOWAIT)
486 #define IXGBE_RX_LOCK(_sc)                lockmgr(&(_sc)->rx_lock, LK_EXCLUSIVE)
487 #define IXGBE_CORE_UNLOCK(_sc)            lockmgr(&(_sc)->core_lock, LK_RELEASE)
488 #define IXGBE_TX_UNLOCK(_sc)              lockmgr(&(_sc)->tx_lock, LK_RELEASE)
489 #define IXGBE_RX_UNLOCK(_sc)              lockmgr(&(_sc)->rx_lock, LK_RELEASE)
490 #define IXGBE_CORE_LOCK_ASSERT(_sc)       KKASSERT(lockstatus(&(_sc)->core_lock, curthread) !=0)
491 #define IXGBE_TX_LOCK_ASSERT(_sc)         KKASSERT(lockstatus(&(_sc)->tx_lock, curthread) != 0)
492
493
494 static inline bool
495 ixgbe_is_sfp(struct ixgbe_hw *hw)
496 {
497         switch (hw->phy.type) {
498         case ixgbe_phy_sfp_avago:
499         case ixgbe_phy_sfp_ftl:
500         case ixgbe_phy_sfp_intel:
501         case ixgbe_phy_sfp_unknown:
502         case ixgbe_phy_sfp_passive_tyco:
503         case ixgbe_phy_sfp_passive_unknown:
504                 return TRUE;
505         default:
506                 return FALSE;
507         }
508 }
509
510 /* Workaround to make 8.0 buildable */
511 #if __FreeBSD_version >= 800000 && __FreeBSD_version < 800504
512 static __inline int
513 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
514 {
515 #ifdef ALTQ
516         if (ALTQ_IS_ENABLED(&ifp->if_snd))
517                 return (1);
518 #endif
519         return (!buf_ring_empty(br));
520 }
521 #endif
522
523 /*
524 ** Find the number of unrefreshed RX descriptors
525 */
526 static inline u16
527 ixgbe_rx_unrefreshed(struct rx_ring *rxr)
528 {       
529         struct adapter  *adapter = rxr->adapter;
530         
531         if (rxr->next_to_check > rxr->next_to_refresh)
532                 return (rxr->next_to_check - rxr->next_to_refresh - 1);
533         else
534                 return ((adapter->num_rx_desc + rxr->next_to_check) -
535                     rxr->next_to_refresh - 1);
536 }       
537
538 #endif /* _IXGBE_H_ */