1 /******************************************************************************
3 Copyright (c) 2013-2015, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
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.
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.
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.
32 ******************************************************************************/
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/buf_ring.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/sockio.h>
50 #include <sys/eventhandler.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
56 #include <net/ethernet.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/if_vlan_var.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcp_lro.h>
71 #include <netinet/udp.h>
72 #include <netinet/sctp.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>
95 #include <sys/iov_schema.h>
96 #include <dev/pci/pci_iov.h>
99 #include "i40e_type.h"
100 #include "i40e_prototype.h"
102 #if defined(IXL_DEBUG) || defined(IXL_DEBUG_SYSCTL)
103 #include <sys/sbuf.h>
105 #define MAC_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x"
106 #define MAC_FORMAT_ARGS(mac_addr) \
107 (mac_addr)[0], (mac_addr)[1], (mac_addr)[2], (mac_addr)[3], \
108 (mac_addr)[4], (mac_addr)[5]
109 #define ON_OFF_STR(is_set) ((is_set) ? "On" : "Off")
110 #endif /* IXL_DEBUG || IXL_DEBUG_SYSCTL */
113 /* Enable debug sysctls */
114 #ifndef IXL_DEBUG_SYSCTL
115 #define IXL_DEBUG_SYSCTL 1
118 #define _DBG_PRINTF(S, ...) printf("%s: " S "\n", __func__, ##__VA_ARGS__)
119 #define _DEV_DBG_PRINTF(dev, S, ...) device_printf(dev, "%s: " S "\n", __func__, ##__VA_ARGS__)
120 #define _IF_DBG_PRINTF(ifp, S, ...) if_printf(ifp, "%s: " S "\n", __func__, ##__VA_ARGS__)
122 /* Defines for printing generic debug information */
123 #define DPRINTF(...) _DBG_PRINTF(__VA_ARGS__)
124 #define DDPRINTF(...) _DEV_DBG_PRINTF(__VA_ARGS__)
125 #define IDPRINTF(...) _IF_DBG_PRINTF(__VA_ARGS__)
127 /* Defines for printing specific debug information */
129 #define DEBUG_IOCTL 1
132 #define INIT_DEBUGOUT(...) if (DEBUG_INIT) _DBG_PRINTF(__VA_ARGS__)
133 #define INIT_DBG_DEV(...) if (DEBUG_INIT) _DEV_DBG_PRINTF(__VA_ARGS__)
134 #define INIT_DBG_IF(...) if (DEBUG_INIT) _IF_DBG_PRINTF(__VA_ARGS__)
136 #define IOCTL_DEBUGOUT(...) if (DEBUG_IOCTL) _DBG_PRINTF(__VA_ARGS__)
137 #define IOCTL_DBG_IF2(ifp, S, ...) if (DEBUG_IOCTL) \
138 if_printf(ifp, S "\n", ##__VA_ARGS__)
139 #define IOCTL_DBG_IF(...) if (DEBUG_IOCTL) _IF_DBG_PRINTF(__VA_ARGS__)
141 #define HW_DEBUGOUT(...) if (DEBUG_HW) _DBG_PRINTF(__VA_ARGS__)
143 #else /* no IXL_DEBUG */
145 #define DEBUG_IOCTL 0
149 #define DDPRINTF(...)
150 #define IDPRINTF(...)
152 #define INIT_DEBUGOUT(...)
153 #define INIT_DBG_DEV(...)
154 #define INIT_DBG_IF(...)
155 #define IOCTL_DEBUGOUT(...)
156 #define IOCTL_DBG_IF2(...)
157 #define IOCTL_DBG_IF(...)
158 #define HW_DEBUGOUT(...)
159 #endif /* IXL_DEBUG */
164 * Ring Descriptors Valid Range: 32-4096 Default Value: 1024 This value is the
165 * number of tx/rx descriptors allocated by the driver. Increasing this
166 * value allows the driver to queue more operations. Each descriptor is 16
167 * or 32 bytes (configurable in FVL)
169 #define DEFAULT_RING 1024
170 #define PERFORM_RING 2048
171 #define MAX_RING 4096
175 ** Default number of entries in Tx queue buf_ring.
177 #define SMALL_TXBRSZ 4096
178 /* This may require mbuf cluster tuning */
179 #define DEFAULT_TXBRSZ (SMALL_TXBRSZ * SMALL_TXBRSZ)
181 /* Alignment for rings */
182 #define DBA_ALIGN 128
185 * This parameter controls the maximum no of times the driver will loop in
186 * the isr. Minimum Value = 1
191 * This is the max watchdog interval, ie. the time that can
192 * pass between any two TX clean operations, such only happening
193 * when the TX hardware is functioning.
195 #define IXL_WATCHDOG (10 * hz)
198 * This parameters control when the driver calls the routine to reclaim
199 * transmit descriptors.
201 #define IXL_TX_CLEANUP_THRESHOLD (que->num_desc / 8)
202 #define IXL_TX_OP_THRESHOLD (que->num_desc / 32)
204 /* Flow control constants */
205 #define IXL_FC_PAUSE 0xFFFF
206 #define IXL_FC_HI 0x20000
207 #define IXL_FC_LO 0x10000
209 #define MAX_MULTICAST_ADDR 128
212 #define IXL_ADM_LIMIT 2
213 #define IXL_TSO_SIZE 65535
214 #define IXL_TX_BUF_SZ ((u32) 1514)
215 #define IXL_AQ_BUF_SZ ((u32) 4096)
216 #define IXL_RX_HDR 128
217 /* Controls the length of the Admin Queue */
218 #define IXL_AQ_LEN 256
219 #define IXL_AQ_LEN_MAX 1024
220 #define IXL_AQ_BUFSZ 4096
221 #define IXL_RX_LIMIT 512
224 #define IXL_ITR_NONE 3
225 #define IXL_QUEUE_EOL 0x7FF
226 #define IXL_MAX_FRAME 0x2600
227 #define IXL_MAX_TX_SEGS 8
228 #define IXL_MAX_TSO_SEGS 66
229 #define IXL_SPARSE_CHAIN 6
230 #define IXL_QUEUE_HUNG 0x80000000
233 #define IXL_VF_MAX_BUFFER 0x3F80
234 #define IXL_VF_MAX_HDR_BUFFER 0x840
235 #define IXL_VF_MAX_FRAME 0x3FFF
237 /* ERJ: hardware can support ~1.5k filters between all functions */
238 #define IXL_MAX_FILTERS 256
239 #define IXL_MAX_TX_BUSY 10
241 #define IXL_NVM_VERSION_LO_SHIFT 0
242 #define IXL_NVM_VERSION_LO_MASK (0xff << IXL_NVM_VERSION_LO_SHIFT)
243 #define IXL_NVM_VERSION_HI_SHIFT 12
244 #define IXL_NVM_VERSION_HI_MASK (0xf << IXL_NVM_VERSION_HI_SHIFT)
248 * Interrupt Moderation parameters
250 #define IXL_MAX_ITR 0x07FF
251 #define IXL_ITR_100K 0x0005
252 #define IXL_ITR_20K 0x0019
253 #define IXL_ITR_8K 0x003E
254 #define IXL_ITR_4K 0x007A
255 #define IXL_ITR_DYNAMIC 0x8000
256 #define IXL_LOW_LATENCY 0
257 #define IXL_AVE_LATENCY 1
258 #define IXL_BULK_LATENCY 2
261 #define IXL_FILTER_USED (u16)(1 << 0)
262 #define IXL_FILTER_VLAN (u16)(1 << 1)
263 #define IXL_FILTER_ADD (u16)(1 << 2)
264 #define IXL_FILTER_DEL (u16)(1 << 3)
265 #define IXL_FILTER_MC (u16)(1 << 4)
267 /* used in the vlan field of the filter when not a vlan */
268 #define IXL_VLAN_ANY -1
270 #define CSUM_OFFLOAD_IPV4 (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
271 #define CSUM_OFFLOAD_IPV6 (CSUM_TCP_IPV6|CSUM_UDP_IPV6|CSUM_SCTP_IPV6)
272 #define CSUM_OFFLOAD (CSUM_OFFLOAD_IPV4|CSUM_OFFLOAD_IPV6|CSUM_TSO)
274 /* Misc flags for ixl_vsi.flags */
275 #define IXL_FLAGS_KEEP_TSO4 (1 << 0)
276 #define IXL_FLAGS_KEEP_TSO6 (1 << 1)
278 #define IXL_VF_RESET_TIMEOUT 100
280 #define IXL_VSI_DATA_PORT 0x01
282 #define IXLV_MAX_QUEUES 16
283 #define IXL_MAX_VSI_QUEUES (2 * (I40E_VSILAN_QTABLE_MAX_INDEX + 1))
285 #define IXL_RX_CTX_BASE_UNITS 128
286 #define IXL_TX_CTX_BASE_UNITS 128
288 #define IXL_VPINT_LNKLSTN_REG(hw, vector, vf_num) \
289 I40E_VPINT_LNKLSTN(((vector) - 1) + \
290 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
292 #define IXL_VFINT_DYN_CTLN_REG(hw, vector, vf_num) \
293 I40E_VFINT_DYN_CTLN(((vector) - 1) + \
294 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
296 #define IXL_PF_PCI_CIAA_VF_DEVICE_STATUS 0xAA
298 #define IXL_PF_PCI_CIAD_VF_TRANS_PENDING_MASK 0x20
300 #define IXL_GLGEN_VFLRSTAT_INDEX(glb_vf) ((glb_vf) / 32)
301 #define IXL_GLGEN_VFLRSTAT_MASK(glb_vf) (1 << ((glb_vf) % 32))
303 #define IXL_MAX_ITR_IDX 3
305 #define IXL_END_OF_INTR_LNKLST 0x7FF
307 #define IXL_TX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
308 #define IXL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
309 #define IXL_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
310 #define IXL_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->mtx)
311 #define IXL_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED)
313 #define IXL_RX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
314 #define IXL_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
315 #define IXL_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
317 #if __FreeBSD_version >= 1100036
318 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ipackets = (count)
319 #define IXL_SET_IERRORS(vsi, count) (vsi)->ierrors = (count)
320 #define IXL_SET_OPACKETS(vsi, count) (vsi)->opackets = (count)
321 #define IXL_SET_OERRORS(vsi, count) (vsi)->oerrors = (count)
322 #define IXL_SET_COLLISIONS(vsi, count) /* Do nothing; collisions is always 0. */
323 #define IXL_SET_IBYTES(vsi, count) (vsi)->ibytes = (count)
324 #define IXL_SET_OBYTES(vsi, count) (vsi)->obytes = (count)
325 #define IXL_SET_IMCASTS(vsi, count) (vsi)->imcasts = (count)
326 #define IXL_SET_OMCASTS(vsi, count) (vsi)->omcasts = (count)
327 #define IXL_SET_IQDROPS(vsi, count) (vsi)->iqdrops = (count)
328 #define IXL_SET_OQDROPS(vsi, count) (vsi)->oqdrops = (count)
329 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
331 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ifp->if_ipackets = (count)
332 #define IXL_SET_IERRORS(vsi, count) (vsi)->ifp->if_ierrors = (count)
333 #define IXL_SET_OPACKETS(vsi, count) (vsi)->ifp->if_opackets = (count)
334 #define IXL_SET_OERRORS(vsi, count) (vsi)->ifp->if_oerrors = (count)
335 #define IXL_SET_COLLISIONS(vsi, count) (vsi)->ifp->if_collisions = (count)
336 #define IXL_SET_IBYTES(vsi, count) (vsi)->ifp->if_ibytes = (count)
337 #define IXL_SET_OBYTES(vsi, count) (vsi)->ifp->if_obytes = (count)
338 #define IXL_SET_IMCASTS(vsi, count) (vsi)->ifp->if_imcasts = (count)
339 #define IXL_SET_OMCASTS(vsi, count) (vsi)->ifp->if_omcasts = (count)
340 #define IXL_SET_IQDROPS(vsi, count) (vsi)->ifp->if_iqdrops = (count)
341 #define IXL_SET_OQDROPS(vsi, odrops) (vsi)->ifp->if_snd.ifq_drops = (odrops)
342 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
346 *****************************************************************************
349 * This array contains the list of Subvendor/Subdevice IDs on which the driver
352 *****************************************************************************
354 typedef struct _ixl_vendor_info_t {
355 unsigned int vendor_id;
356 unsigned int device_id;
357 unsigned int subvendor_id;
358 unsigned int subdevice_id;
379 ** This struct has multiple uses, multicast
380 ** addresses, vlans, and mac filters all use it.
382 struct ixl_mac_filter {
383 SLIST_ENTRY(ixl_mac_filter) next;
384 u8 macaddr[ETHER_ADDR_LEN];
391 * The Transmit ring control struct
394 struct ixl_queue *que;
397 struct i40e_tx_desc *base;
398 struct i40e_dma_mem dma;
405 struct ixl_tx_buf *buffers;
408 bus_dma_tag_t tx_tag;
409 bus_dma_tag_t tso_tag;
413 /* Used for Dynamic ITR calculation */
425 * The Receive ring control struct
428 struct ixl_queue *que;
430 union i40e_rx_desc *base;
431 struct i40e_dma_mem dma;
441 struct ixl_rx_buf *buffers;
447 /* Used for Dynamic ITR calculation */
460 ** Driver queue struct: this is the interrupt container
461 ** for the associated tx and rx ring pair.
466 u32 msix; /* This queue's MSIX vector */
467 u32 eims; /* This queue's EIMS bit */
468 struct resource *res;
470 int num_desc; /* both tx and rx */
476 struct taskqueue *tq;
481 u64 mbuf_defrag_failed;
490 ** Virtual Station interface:
491 ** there would be one of these per traffic class/type
492 ** for now just one, and its embedded in the pf
494 SLIST_HEAD(ixl_ftl_head, ixl_mac_filter);
500 struct ifmedia media;
504 u16 msix_base; /* station base MSIX vector */
509 struct ixl_queue *queues; /* head of queues */
516 /* MAC/VLAN Filter list */
517 struct ixl_ftl_head ftl;
520 struct i40e_aqc_vsi_properties_data info;
522 eventhandler_tag vlan_attach;
523 eventhandler_tag vlan_detach;
526 /* Per-VSI stats from hardware */
527 struct i40e_eth_stats eth_stats;
528 struct i40e_eth_stats eth_stats_offsets;
529 bool stat_offsets_loaded;
530 /* VSI stat counters */
543 /* Driver statistics */
550 struct sysctl_oid *vsi_node;
554 ** Find the number of unrefreshed RX descriptors
557 ixl_rx_unrefreshed(struct ixl_queue *que)
559 struct rx_ring *rxr = &que->rxr;
561 if (rxr->next_check > rxr->next_refresh)
562 return (rxr->next_check - rxr->next_refresh - 1);
564 return ((que->num_desc + rxr->next_check) -
565 rxr->next_refresh - 1);
569 ** Find the next available unused filter
571 static inline struct ixl_mac_filter *
572 ixl_get_filter(struct ixl_vsi *vsi)
574 struct ixl_mac_filter *f;
576 /* create a new empty filter */
577 f = malloc(sizeof(struct ixl_mac_filter),
578 M_DEVBUF, M_NOWAIT | M_ZERO);
580 SLIST_INSERT_HEAD(&vsi->ftl, f, next);
586 ** Compare two ethernet addresses
589 cmp_etheraddr(const u8 *ea1, const u8 *ea2)
593 if ((ea1[0] == ea2[0]) && (ea1[1] == ea2[1]) &&
594 (ea1[2] == ea2[2]) && (ea1[3] == ea2[3]) &&
595 (ea1[4] == ea2[4]) && (ea1[5] == ea2[5]))
602 * Info for stats sysctls
604 struct ixl_sysctl_info {
610 extern int ixl_atr_rate;
613 ** ixl_fw_version_str - format the FW and NVM version strings
616 ixl_fw_version_str(struct i40e_hw *hw)
620 snprintf(buf, sizeof(buf),
621 "f%d.%d a%d.%d n%02x.%02x e%08x",
622 hw->aq.fw_maj_ver, hw->aq.fw_min_ver,
623 hw->aq.api_maj_ver, hw->aq.api_min_ver,
624 (hw->nvm.version & IXL_NVM_VERSION_HI_MASK) >>
625 IXL_NVM_VERSION_HI_SHIFT,
626 (hw->nvm.version & IXL_NVM_VERSION_LO_MASK) >>
627 IXL_NVM_VERSION_LO_SHIFT,
632 /*********************************************************************
633 * TXRX Function prototypes
634 *********************************************************************/
635 int ixl_allocate_tx_data(struct ixl_queue *);
636 int ixl_allocate_rx_data(struct ixl_queue *);
637 void ixl_init_tx_ring(struct ixl_queue *);
638 int ixl_init_rx_ring(struct ixl_queue *);
639 bool ixl_rxeof(struct ixl_queue *, int);
640 bool ixl_txeof(struct ixl_queue *);
641 int ixl_mq_start(struct ifnet *, struct mbuf *);
642 int ixl_mq_start_locked(struct ifnet *, struct tx_ring *);
643 void ixl_deferred_mq_start(void *, int);
644 void ixl_qflush(struct ifnet *);
645 void ixl_free_vsi(struct ixl_vsi *);
646 void ixl_free_que_tx(struct ixl_queue *);
647 void ixl_free_que_rx(struct ixl_queue *);
649 void ixl_atr(struct ixl_queue *, struct tcphdr *, int);
651 #if __FreeBSD_version >= 1100000
652 uint64_t ixl_get_counter(if_t ifp, ift_counter cnt);