1 /******************************************************************************
2 SPDX-License-Identifier: BSD-3-Clause
4 Copyright (c) 2001-2017, Intel Corporation
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
17 3. Neither the name of the Intel Corporation nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
33 ******************************************************************************/
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf_ring.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
50 #include <sys/sockio.h>
51 #include <sys/eventhandler.h>
54 #include <net/if_var.h>
55 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
62 #include <net/if_types.h>
63 #include <net/if_vlan_var.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip6.h>
70 #include <netinet/tcp.h>
71 #include <netinet/tcp_lro.h>
72 #include <netinet/udp.h>
74 #include <machine/in_cksum.h>
77 #include <machine/bus.h>
79 #include <machine/resource.h>
82 #include <machine/clock.h>
83 #include <dev/pci/pcivar.h>
84 #include <dev/pci/pcireg.h>
86 #include <sys/sysctl.h>
87 #include <sys/endian.h>
88 #include <sys/taskqueue.h>
91 #include <machine/smp.h>
94 #include "ixgbe_api.h"
95 #include "ixgbe_common.h"
96 #include "ixgbe_phy.h"
98 #include "ixgbe_features.h"
103 * TxDescriptors Valid Range: 64-4096 Default Value: 256 This value is the
104 * number of transmit descriptors allocated by the driver. Increasing this
105 * value allows the driver to queue more transmits. Each descriptor is 16
106 * bytes. Performance tests have show the 2K value to be optimal for top
109 #define DEFAULT_TXD 1024
110 #define PERFORM_TXD 2048
115 * RxDescriptors Valid Range: 64-4096 Default Value: 256 This value is the
116 * number of receive descriptors allocated for each RX queue. Increasing this
117 * value allows the driver to buffer more incoming packets. Each descriptor
118 * is 16 bytes. A receive buffer is also allocated for each descriptor.
120 * Note: with 8 rings and a dual port card, it is possible to bump up
121 * against the system mbuf pool limit, you can tune nmbclusters
122 * to adjust for this.
124 #define DEFAULT_RXD 1024
125 #define PERFORM_RXD 2048
129 /* Alignment for rings */
130 #define DBA_ALIGN 128
133 * This is the max watchdog interval, ie. the time that can
134 * pass between any two TX clean operations, such only happening
135 * when the TX hardware is functioning.
137 #define IXGBE_WATCHDOG (10 * hz)
140 * This parameters control when the driver calls the routine to reclaim
141 * transmit descriptors.
143 #define IXGBE_TX_CLEANUP_THRESHOLD(_a) ((_a)->num_tx_desc / 8)
144 #define IXGBE_TX_OP_THRESHOLD(_a) ((_a)->num_tx_desc / 32)
146 /* These defines are used in MTU calculations */
147 #define IXGBE_MAX_FRAME_SIZE 9728
148 #define IXGBE_MTU_HDR (ETHER_HDR_LEN + ETHER_CRC_LEN)
149 #define IXGBE_MTU_HDR_VLAN (ETHER_HDR_LEN + ETHER_CRC_LEN + \
150 ETHER_VLAN_ENCAP_LEN)
151 #define IXGBE_MAX_MTU (IXGBE_MAX_FRAME_SIZE - IXGBE_MTU_HDR)
152 #define IXGBE_MAX_MTU_VLAN (IXGBE_MAX_FRAME_SIZE - IXGBE_MTU_HDR_VLAN)
154 /* Flow control constants */
155 #define IXGBE_FC_PAUSE 0xFFFF
156 #define IXGBE_FC_HI 0x20000
157 #define IXGBE_FC_LO 0x10000
160 * Used for optimizing small rx mbufs. Effort is made to keep the copy
161 * small and aligned for the CPU L1 cache.
163 * MHLEN is typically 168 bytes, giving us 8-byte alignment. Getting
164 * 32 byte alignment needed for the fast bcopy results in 8 bytes being
165 * wasted. Getting 64 byte alignment, which _should_ be ideal for
166 * modern Intel CPUs, results in 40 bytes wasted and a significant drop
167 * in observed efficiency of the optimization, 97.9% -> 81.8%.
169 #if __FreeBSD_version < 1002000
170 #define MPKTHSIZE (sizeof(struct m_hdr) + sizeof(struct pkthdr))
172 #define IXGBE_RX_COPY_HDR_PADDED ((((MPKTHSIZE - 1) / 32) + 1) * 32)
173 #define IXGBE_RX_COPY_LEN (MSIZE - IXGBE_RX_COPY_HDR_PADDED)
174 #define IXGBE_RX_COPY_ALIGN (IXGBE_RX_COPY_HDR_PADDED - MPKTHSIZE)
176 /* Keep older OS drivers building... */
177 #if !defined(SYSCTL_ADD_UQUAD)
178 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
181 /* Defines for printing debug information */
183 #define DEBUG_IOCTL 0
186 #define INIT_DEBUGOUT(S) if (DEBUG_INIT) printf(S "\n")
187 #define INIT_DEBUGOUT1(S, A) if (DEBUG_INIT) printf(S "\n", A)
188 #define INIT_DEBUGOUT2(S, A, B) if (DEBUG_INIT) printf(S "\n", A, B)
189 #define IOCTL_DEBUGOUT(S) if (DEBUG_IOCTL) printf(S "\n")
190 #define IOCTL_DEBUGOUT1(S, A) if (DEBUG_IOCTL) printf(S "\n", A)
191 #define IOCTL_DEBUGOUT2(S, A, B) if (DEBUG_IOCTL) printf(S "\n", A, B)
192 #define HW_DEBUGOUT(S) if (DEBUG_HW) printf(S "\n")
193 #define HW_DEBUGOUT1(S, A) if (DEBUG_HW) printf(S "\n", A)
194 #define HW_DEBUGOUT2(S, A, B) if (DEBUG_HW) printf(S "\n", A, B)
196 #define MAX_NUM_MULTICAST_ADDRESSES 128
197 #define IXGBE_82598_SCATTER 100
198 #define IXGBE_82599_SCATTER 32
199 #define MSIX_82598_BAR 3
200 #define MSIX_82599_BAR 4
201 #define IXGBE_TSO_SIZE 262140
202 #define IXGBE_RX_HDR 128
203 #define IXGBE_VFTA_SIZE 128
204 #define IXGBE_BR_SIZE 4096
205 #define IXGBE_QUEUE_MIN_FREE 32
206 #define IXGBE_MAX_TX_BUSY 10
207 #define IXGBE_QUEUE_HUNG 0x80000000
209 #define IXGBE_EITR_DEFAULT 128
211 /* Supported offload bits in mbuf flag */
212 #if __FreeBSD_version >= 1000000
213 #define CSUM_OFFLOAD (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
214 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
215 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
216 #elif __FreeBSD_version >= 800000
217 #define CSUM_OFFLOAD (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
219 #define CSUM_OFFLOAD (CSUM_IP|CSUM_TCP|CSUM_UDP)
222 /* Backward compatibility items for very old versions */
224 #define pci_find_cap pci_find_extcap
227 #ifndef DEVMETHOD_END
228 #define DEVMETHOD_END { NULL, NULL }
232 * Interrupt Moderation parameters
234 #define IXGBE_LOW_LATENCY 128
235 #define IXGBE_AVE_LATENCY 400
236 #define IXGBE_BULK_LATENCY 1200
238 /* Using 1FF (the max value), the interval is ~1.05ms */
239 #define IXGBE_LINK_ITR_QUANTA 0x1FF
240 #define IXGBE_LINK_ITR ((IXGBE_LINK_ITR_QUANTA << 3) & \
241 IXGBE_EITR_ITR_INT_MASK)
245 /************************************************************************
248 * Contains the list of Subvendor/Subdevice IDs on
249 * which the driver should load.
250 ************************************************************************/
251 typedef struct _ixgbe_vendor_info_t {
252 unsigned int vendor_id;
253 unsigned int device_id;
254 unsigned int subvendor_id;
255 unsigned int subdevice_id;
257 } ixgbe_vendor_info_t;
259 struct ixgbe_bp_data {
265 struct ixgbe_tx_buf {
266 union ixgbe_adv_tx_desc *eop;
271 struct ixgbe_rx_buf {
276 #define IXGBE_RX_COPY 0x01
281 * Bus dma allocation structure used by ixgbe_dma_malloc and ixgbe_dma_free
283 struct ixgbe_dma_alloc {
284 bus_addr_t dma_paddr;
286 bus_dma_tag_t dma_tag;
287 bus_dmamap_t dma_map;
288 bus_dma_segment_t dma_seg;
293 struct ixgbe_mc_addr {
294 u8 addr[IXGBE_ETH_LENGTH_OF_ADDRESS];
299 * Driver queue struct: this is the interrupt container
300 * for the associated tx and rx ring.
303 struct adapter *adapter;
304 u32 msix; /* This queue's MSI-X vector */
305 u32 eims; /* This queue's EIMS bit */
308 struct resource *res;
313 struct task que_task;
314 struct taskqueue *tq;
319 * The transmit ring, one per queue
322 struct adapter *adapter;
327 union ixgbe_adv_tx_desc *tx_base;
328 struct ixgbe_tx_buf *tx_buffers;
329 struct ixgbe_dma_alloc txdma;
330 volatile u16 tx_avail;
338 struct task txq_task;
344 u32 bytes; /* used for AIM */
356 * The Receive ring, one per rx queue
359 struct adapter *adapter;
363 union ixgbe_adv_rx_desc *rx_base;
364 struct ixgbe_dma_alloc rxdma;
374 struct ixgbe_rx_buf *rx_buffers;
377 u32 bytes; /* Used for AIM calc */
392 #define IXGBE_MAX_VF_MC 30 /* Max number of multicast entries */
397 u_int max_frame_size;
399 uint8_t ether_addr[ETHER_ADDR_LEN];
400 uint16_t mc_hash[IXGBE_MAX_VF_MC];
401 uint16_t num_mc_hashes;
402 uint16_t default_vlan;
407 /* Our adapter structure */
410 struct ixgbe_osdep osdep;
415 struct resource *pci_mem;
416 struct resource *msix_mem;
419 * Interrupt resources: this set is
420 * either used for legacy, or for Link
424 struct resource *res;
426 struct ifmedia media;
427 struct callout timer;
433 eventhandler_tag vlan_attach;
434 eventhandler_tag vlan_detach;
440 * Shadow VFTA table, this is needed because
441 * the real vlan filter table gets cleared during
442 * a soft reset and the driver needs to be able
445 u32 shadow_vfta[IXGBE_VFTA_SIZE];
447 /* Info about the interface */
448 int advertise; /* link speeds */
449 int enable_aim; /* adaptive interrupt moderation */
459 /* Power management-related */
463 /* Mbuf cluster size */
466 /* Support for pluggable optics */
468 struct task link_task; /* Link tasklet */
469 struct task mod_task; /* SFP tasklet */
470 struct task msf_task; /* Multispeed Fiber */
471 struct task mbx_task; /* VF -> PF mailbox interrupt */
475 struct task fdir_task;
477 struct task phy_task; /* PHY intr tasklet */
478 struct taskqueue *tq;
482 * This is the irq holder, it has
483 * and RX/TX pair or rings associated
486 struct ix_queue *queues;
490 * Allocated at run time, an array of rings
492 struct tx_ring *tx_rings;
494 u32 tx_process_limit;
498 * Allocated at run time, an array of rings
500 struct rx_ring *rx_rings;
503 u32 rx_process_limit;
505 /* Multicast array memory */
506 struct ixgbe_mc_addr *mta;
512 struct ixgbe_vf *vfs;
515 struct ixgbe_bp_data bypass;
518 void (*init_locked)(struct adapter *);
519 void (*stop_locked)(void *);
521 /* Misc stats maintained by the driver */
522 unsigned long dropped_pkts;
523 unsigned long mbuf_defrag_failed;
524 unsigned long mbuf_header_failed;
525 unsigned long mbuf_packet_failed;
526 unsigned long watchdog_events;
527 unsigned long link_irq;
529 struct ixgbe_hw_stats pf;
530 struct ixgbevf_hw_stats vf;
532 #if __FreeBSD_version >= 1100036
533 /* counter(9) stats */
545 /* Feature capable/enabled flags. See ixgbe_features.h */
551 /* Precision Time Sync (IEEE 1588) defines */
552 #define ETHERTYPE_IEEE1588 0x88F7
553 #define PICOSECS_PER_TICK 20833
554 #define TSYNC_UDP_PORT 319 /* UDP port for the protocol */
555 #define IXGBE_ADVTXD_TSTAMP 0x00080000
558 #define IXGBE_CORE_LOCK_INIT(_sc, _name) \
559 mtx_init(&(_sc)->core_mtx, _name, "IXGBE Core Lock", MTX_DEF)
560 #define IXGBE_CORE_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->core_mtx)
561 #define IXGBE_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->tx_mtx)
562 #define IXGBE_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->rx_mtx)
563 #define IXGBE_CORE_LOCK(_sc) mtx_lock(&(_sc)->core_mtx)
564 #define IXGBE_TX_LOCK(_sc) mtx_lock(&(_sc)->tx_mtx)
565 #define IXGBE_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->tx_mtx)
566 #define IXGBE_RX_LOCK(_sc) mtx_lock(&(_sc)->rx_mtx)
567 #define IXGBE_CORE_UNLOCK(_sc) mtx_unlock(&(_sc)->core_mtx)
568 #define IXGBE_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->tx_mtx)
569 #define IXGBE_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->rx_mtx)
570 #define IXGBE_CORE_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->core_mtx, MA_OWNED)
571 #define IXGBE_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->tx_mtx, MA_OWNED)
573 /* For backward compatibility */
574 #if !defined(PCIER_LINK_STA)
575 #define PCIER_LINK_STA PCIR_EXPRESS_LINK_STA
579 #if __FreeBSD_version >= 1100036
580 #define IXGBE_SET_IPACKETS(sc, count) (sc)->ipackets = (count)
581 #define IXGBE_SET_IERRORS(sc, count) (sc)->ierrors = (count)
582 #define IXGBE_SET_OPACKETS(sc, count) (sc)->opackets = (count)
583 #define IXGBE_SET_OERRORS(sc, count) (sc)->oerrors = (count)
584 #define IXGBE_SET_COLLISIONS(sc, count)
585 #define IXGBE_SET_IBYTES(sc, count) (sc)->ibytes = (count)
586 #define IXGBE_SET_OBYTES(sc, count) (sc)->obytes = (count)
587 #define IXGBE_SET_IMCASTS(sc, count) (sc)->imcasts = (count)
588 #define IXGBE_SET_OMCASTS(sc, count) (sc)->omcasts = (count)
589 #define IXGBE_SET_IQDROPS(sc, count) (sc)->iqdrops = (count)
591 #define IXGBE_SET_IPACKETS(sc, count) (sc)->ifp->if_ipackets = (count)
592 #define IXGBE_SET_IERRORS(sc, count) (sc)->ifp->if_ierrors = (count)
593 #define IXGBE_SET_OPACKETS(sc, count) (sc)->ifp->if_opackets = (count)
594 #define IXGBE_SET_OERRORS(sc, count) (sc)->ifp->if_oerrors = (count)
595 #define IXGBE_SET_COLLISIONS(sc, count) (sc)->ifp->if_collisions = (count)
596 #define IXGBE_SET_IBYTES(sc, count) (sc)->ifp->if_ibytes = (count)
597 #define IXGBE_SET_OBYTES(sc, count) (sc)->ifp->if_obytes = (count)
598 #define IXGBE_SET_IMCASTS(sc, count) (sc)->ifp->if_imcasts = (count)
599 #define IXGBE_SET_OMCASTS(sc, count) (sc)->ifp->if_omcasts = (count)
600 #define IXGBE_SET_IQDROPS(sc, count) (sc)->ifp->if_iqdrops = (count)
603 /* External PHY register addresses */
604 #define IXGBE_PHY_CURRENT_TEMP 0xC820
605 #define IXGBE_PHY_OVERTEMP_STATUS 0xC830
607 /* Sysctl help messages; displayed with sysctl -d */
608 #define IXGBE_SYSCTL_DESC_ADV_SPEED \
609 "\nControl advertised link speed using these flags:\n" \
610 "\t0x1 - advertise 100M\n" \
611 "\t0x2 - advertise 1G\n" \
612 "\t0x4 - advertise 10G\n" \
613 "\t0x8 - advertise 10M\n\n" \
614 "\t100M and 10M are only supported on certain adapters.\n"
616 #define IXGBE_SYSCTL_DESC_SET_FC \
617 "\nSet flow control mode using these values:\n" \
621 "\t3 - tx and rx pause"
623 /* Workaround to make 8.0 buildable */
624 #if __FreeBSD_version >= 800000 && __FreeBSD_version < 800504
626 drbr_needs_enqueue(struct ifnet *ifp, struct buf_ring *br)
629 if (ALTQ_IS_ENABLED(&ifp->if_snd))
632 return (!buf_ring_empty(br));
637 * Find the number of unrefreshed RX descriptors
640 ixgbe_rx_unrefreshed(struct rx_ring *rxr)
642 if (rxr->next_to_check > rxr->next_to_refresh)
643 return (rxr->next_to_check - rxr->next_to_refresh - 1);
645 return ((rxr->num_desc + rxr->next_to_check) -
646 rxr->next_to_refresh - 1);
650 ixgbe_legacy_ring_empty(struct ifnet *ifp, struct buf_ring *dummy)
652 UNREFERENCED_1PARAMETER(dummy);
654 return IFQ_DRV_IS_EMPTY(&ifp->if_snd);
658 * This checks for a zero mac addr, something that will be likely
659 * unless the Admin on the Host has created one.
662 ixv_check_ether_addr(u8 *addr)
666 if ((addr[0] == 0 && addr[1]== 0 && addr[2] == 0 &&
667 addr[3] == 0 && addr[4]== 0 && addr[5] == 0))
673 /* Shared Prototypes */
674 void ixgbe_legacy_start(struct ifnet *);
675 int ixgbe_legacy_start_locked(struct ifnet *, struct tx_ring *);
676 int ixgbe_mq_start(struct ifnet *, struct mbuf *);
677 int ixgbe_mq_start_locked(struct ifnet *, struct tx_ring *);
678 void ixgbe_qflush(struct ifnet *);
679 void ixgbe_deferred_mq_start(void *, int);
681 int ixgbe_allocate_queues(struct adapter *);
682 int ixgbe_setup_transmit_structures(struct adapter *);
683 void ixgbe_free_transmit_structures(struct adapter *);
684 int ixgbe_setup_receive_structures(struct adapter *);
685 void ixgbe_free_receive_structures(struct adapter *);
686 void ixgbe_txeof(struct tx_ring *);
687 bool ixgbe_rxeof(struct ix_queue *);
689 #include "ixgbe_bypass.h"
690 #include "ixgbe_sriov.h"
691 #include "ixgbe_fdir.h"
692 #include "ixgbe_rss.h"
693 #include "ixgbe_netmap.h"
695 #endif /* _IXGBE_H_ */