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>
98 #include "i40e_type.h"
99 #include "i40e_prototype.h"
101 #if defined(IXL_DEBUG) || defined(IXL_DEBUG_SYSCTL)
102 #include <sys/sbuf.h>
104 #define MAC_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x"
105 #define MAC_FORMAT_ARGS(mac_addr) \
106 (mac_addr)[0], (mac_addr)[1], (mac_addr)[2], (mac_addr)[3], \
107 (mac_addr)[4], (mac_addr)[5]
108 #define ON_OFF_STR(is_set) ((is_set) ? "On" : "Off")
109 #endif /* IXL_DEBUG || IXL_DEBUG_SYSCTL */
112 /* Enable debug sysctls */
113 #ifndef IXL_DEBUG_SYSCTL
114 #define IXL_DEBUG_SYSCTL 1
117 #define _DBG_PRINTF(S, ...) printf("%s: " S "\n", __func__, ##__VA_ARGS__)
118 #define _DEV_DBG_PRINTF(dev, S, ...) device_printf(dev, "%s: " S "\n", __func__, ##__VA_ARGS__)
119 #define _IF_DBG_PRINTF(ifp, S, ...) if_printf(ifp, "%s: " S "\n", __func__, ##__VA_ARGS__)
121 /* Defines for printing generic debug information */
122 #define DPRINTF(...) _DBG_PRINTF(__VA_ARGS__)
123 #define DDPRINTF(...) _DEV_DBG_PRINTF(__VA_ARGS__)
124 #define IDPRINTF(...) _IF_DBG_PRINTF(__VA_ARGS__)
126 /* Defines for printing specific debug information */
128 #define DEBUG_IOCTL 1
131 #define INIT_DEBUGOUT(...) if (DEBUG_INIT) _DBG_PRINTF(__VA_ARGS__)
132 #define INIT_DBG_DEV(...) if (DEBUG_INIT) _DEV_DBG_PRINTF(__VA_ARGS__)
133 #define INIT_DBG_IF(...) if (DEBUG_INIT) _IF_DBG_PRINTF(__VA_ARGS__)
135 #define IOCTL_DEBUGOUT(...) if (DEBUG_IOCTL) _DBG_PRINTF(__VA_ARGS__)
136 #define IOCTL_DBG_IF2(ifp, S, ...) if (DEBUG_IOCTL) \
137 if_printf(ifp, S "\n", ##__VA_ARGS__)
138 #define IOCTL_DBG_IF(...) if (DEBUG_IOCTL) _IF_DBG_PRINTF(__VA_ARGS__)
140 #define HW_DEBUGOUT(...) if (DEBUG_HW) _DBG_PRINTF(__VA_ARGS__)
142 #else /* no IXL_DEBUG */
144 #define DEBUG_IOCTL 0
148 #define DDPRINTF(...)
149 #define IDPRINTF(...)
151 #define INIT_DEBUGOUT(...)
152 #define INIT_DBG_DEV(...)
153 #define INIT_DBG_IF(...)
154 #define IOCTL_DEBUGOUT(...)
155 #define IOCTL_DBG_IF2(...)
156 #define IOCTL_DBG_IF(...)
157 #define HW_DEBUGOUT(...)
158 #endif /* IXL_DEBUG */
163 * Ring Descriptors Valid Range: 32-4096 Default Value: 1024 This value is the
164 * number of tx/rx descriptors allocated by the driver. Increasing this
165 * value allows the driver to queue more operations. Each descriptor is 16
166 * or 32 bytes (configurable in FVL)
168 #define DEFAULT_RING 1024
169 #define PERFORM_RING 2048
170 #define MAX_RING 4096
174 ** Default number of entries in Tx queue buf_ring.
176 #define SMALL_TXBRSZ 4096
177 /* This may require mbuf cluster tuning */
178 #define DEFAULT_TXBRSZ (SMALL_TXBRSZ * SMALL_TXBRSZ)
180 /* Alignment for rings */
181 #define DBA_ALIGN 128
184 * This parameter controls the maximum no of times the driver will loop in
185 * the isr. Minimum Value = 1
190 * This is the max watchdog interval, ie. the time that can
191 * pass between any two TX clean operations, such only happening
192 * when the TX hardware is functioning.
194 #define IXL_WATCHDOG (10 * hz)
197 * This parameters control when the driver calls the routine to reclaim
198 * transmit descriptors.
200 #define IXL_TX_CLEANUP_THRESHOLD (que->num_desc / 8)
201 #define IXL_TX_OP_THRESHOLD (que->num_desc / 32)
203 /* Flow control constants */
204 #define IXL_FC_PAUSE 0xFFFF
205 #define IXL_FC_HI 0x20000
206 #define IXL_FC_LO 0x10000
208 #define MAX_MULTICAST_ADDR 128
211 #define IXL_ADM_LIMIT 2
212 #define IXL_TSO_SIZE 65535
213 #define IXL_TX_BUF_SZ ((u32) 1514)
214 #define IXL_AQ_BUF_SZ ((u32) 4096)
215 #define IXL_RX_HDR 128
216 /* Controls the length of the Admin Queue */
217 #define IXL_AQ_LEN 256
218 #define IXL_AQ_LEN_MAX 1024
219 #define IXL_AQ_BUFSZ 4096
220 #define IXL_RX_LIMIT 512
223 #define IXL_ITR_NONE 3
224 #define IXL_QUEUE_EOL 0x7FF
225 #define IXL_MAX_FRAME 0x2600
226 #define IXL_MAX_TX_SEGS 8
227 #define IXL_MAX_TSO_SEGS 66
228 #define IXL_SPARSE_CHAIN 6
229 #define IXL_QUEUE_HUNG 0x80000000
232 #define IXL_VF_MAX_BUFFER 0x3F80
233 #define IXL_VF_MAX_HDR_BUFFER 0x840
234 #define IXL_VF_MAX_FRAME 0x3FFF
236 /* ERJ: hardware can support ~1.5k filters between all functions */
237 #define IXL_MAX_FILTERS 256
238 #define IXL_MAX_TX_BUSY 10
240 #define IXL_NVM_VERSION_LO_SHIFT 0
241 #define IXL_NVM_VERSION_LO_MASK (0xff << IXL_NVM_VERSION_LO_SHIFT)
242 #define IXL_NVM_VERSION_HI_SHIFT 12
243 #define IXL_NVM_VERSION_HI_MASK (0xf << IXL_NVM_VERSION_HI_SHIFT)
247 * Interrupt Moderation parameters
249 #define IXL_MAX_ITR 0x07FF
250 #define IXL_ITR_100K 0x0005
251 #define IXL_ITR_20K 0x0019
252 #define IXL_ITR_8K 0x003E
253 #define IXL_ITR_4K 0x007A
254 #define IXL_ITR_DYNAMIC 0x8000
255 #define IXL_LOW_LATENCY 0
256 #define IXL_AVE_LATENCY 1
257 #define IXL_BULK_LATENCY 2
260 #define IXL_FILTER_USED (u16)(1 << 0)
261 #define IXL_FILTER_VLAN (u16)(1 << 1)
262 #define IXL_FILTER_ADD (u16)(1 << 2)
263 #define IXL_FILTER_DEL (u16)(1 << 3)
264 #define IXL_FILTER_MC (u16)(1 << 4)
266 /* used in the vlan field of the filter when not a vlan */
267 #define IXL_VLAN_ANY -1
269 #define CSUM_OFFLOAD_IPV4 (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
270 #define CSUM_OFFLOAD_IPV6 (CSUM_TCP_IPV6|CSUM_UDP_IPV6|CSUM_SCTP_IPV6)
271 #define CSUM_OFFLOAD (CSUM_OFFLOAD_IPV4|CSUM_OFFLOAD_IPV6|CSUM_TSO)
273 /* Misc flags for ixl_vsi.flags */
274 #define IXL_FLAGS_KEEP_TSO4 (1 << 0)
275 #define IXL_FLAGS_KEEP_TSO6 (1 << 1)
277 #define IXL_VF_RESET_TIMEOUT 100
279 #define IXL_VSI_DATA_PORT 0x01
281 #define IXLV_MAX_QUEUES 16
282 #define IXL_MAX_VSI_QUEUES (2 * (I40E_VSILAN_QTABLE_MAX_INDEX + 1))
284 #define IXL_RX_CTX_BASE_UNITS 128
285 #define IXL_TX_CTX_BASE_UNITS 128
287 #define IXL_VPINT_LNKLSTN_REG(hw, vector, vf_num) \
288 I40E_VPINT_LNKLSTN(((vector) - 1) + \
289 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
291 #define IXL_VFINT_DYN_CTLN_REG(hw, vector, vf_num) \
292 I40E_VFINT_DYN_CTLN(((vector) - 1) + \
293 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
295 #define IXL_PF_PCI_CIAA_VF_DEVICE_STATUS 0xAA
297 #define IXL_PF_PCI_CIAD_VF_TRANS_PENDING_MASK 0x20
299 #define IXL_GLGEN_VFLRSTAT_INDEX(glb_vf) ((glb_vf) / 32)
300 #define IXL_GLGEN_VFLRSTAT_MASK(glb_vf) (1 << ((glb_vf) % 32))
302 #define IXL_MAX_ITR_IDX 3
304 #define IXL_END_OF_INTR_LNKLST 0x7FF
306 #define IXL_TX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
307 #define IXL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
308 #define IXL_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
309 #define IXL_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->mtx)
310 #define IXL_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED)
312 #define IXL_RX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
313 #define IXL_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
314 #define IXL_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
316 #if __FreeBSD_version >= 1100036
317 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ipackets = (count)
318 #define IXL_SET_IERRORS(vsi, count) (vsi)->ierrors = (count)
319 #define IXL_SET_OPACKETS(vsi, count) (vsi)->opackets = (count)
320 #define IXL_SET_OERRORS(vsi, count) (vsi)->oerrors = (count)
321 #define IXL_SET_COLLISIONS(vsi, count) /* Do nothing; collisions is always 0. */
322 #define IXL_SET_IBYTES(vsi, count) (vsi)->ibytes = (count)
323 #define IXL_SET_OBYTES(vsi, count) (vsi)->obytes = (count)
324 #define IXL_SET_IMCASTS(vsi, count) (vsi)->imcasts = (count)
325 #define IXL_SET_OMCASTS(vsi, count) (vsi)->omcasts = (count)
326 #define IXL_SET_IQDROPS(vsi, count) (vsi)->iqdrops = (count)
327 #define IXL_SET_OQDROPS(vsi, count) (vsi)->iqdrops = (count)
328 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
330 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ifp->if_ipackets = (count)
331 #define IXL_SET_IERRORS(vsi, count) (vsi)->ifp->if_ierrors = (count)
332 #define IXL_SET_OPACKETS(vsi, count) (vsi)->ifp->if_opackets = (count)
333 #define IXL_SET_OERRORS(vsi, count) (vsi)->ifp->if_oerrors = (count)
334 #define IXL_SET_COLLISIONS(vsi, count) (vsi)->ifp->if_collisions = (count)
335 #define IXL_SET_IBYTES(vsi, count) (vsi)->ifp->if_ibytes = (count)
336 #define IXL_SET_OBYTES(vsi, count) (vsi)->ifp->if_obytes = (count)
337 #define IXL_SET_IMCASTS(vsi, count) (vsi)->ifp->if_imcasts = (count)
338 #define IXL_SET_OMCASTS(vsi, count) (vsi)->ifp->if_omcasts = (count)
339 #define IXL_SET_IQDROPS(vsi, count) (vsi)->ifp->if_iqdrops = (count)
340 #define IXL_SET_OQDROPS(vsi, odrops) (vsi)->ifp->if_snd.ifq_drops = (odrops)
341 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
345 *****************************************************************************
348 * This array contains the list of Subvendor/Subdevice IDs on which the driver
351 *****************************************************************************
353 typedef struct _ixl_vendor_info_t {
354 unsigned int vendor_id;
355 unsigned int device_id;
356 unsigned int subvendor_id;
357 unsigned int subdevice_id;
378 ** This struct has multiple uses, multicast
379 ** addresses, vlans, and mac filters all use it.
381 struct ixl_mac_filter {
382 SLIST_ENTRY(ixl_mac_filter) next;
383 u8 macaddr[ETHER_ADDR_LEN];
390 * The Transmit ring control struct
393 struct ixl_queue *que;
396 struct i40e_tx_desc *base;
397 struct i40e_dma_mem dma;
404 struct ixl_tx_buf *buffers;
407 bus_dma_tag_t tx_tag;
408 bus_dma_tag_t tso_tag;
412 /* Used for Dynamic ITR calculation */
424 * The Receive ring control struct
427 struct ixl_queue *que;
429 union i40e_rx_desc *base;
430 struct i40e_dma_mem dma;
440 struct ixl_rx_buf *buffers;
446 /* Used for Dynamic ITR calculation */
459 ** Driver queue struct: this is the interrupt container
460 ** for the associated tx and rx ring pair.
465 u32 msix; /* This queue's MSIX vector */
466 u32 eims; /* This queue's EIMS bit */
467 struct resource *res;
469 int num_desc; /* both tx and rx */
475 struct taskqueue *tq;
480 u64 mbuf_defrag_failed;
489 ** Virtual Station interface:
490 ** there would be one of these per traffic class/type
491 ** for now just one, and its embedded in the pf
493 SLIST_HEAD(ixl_ftl_head, ixl_mac_filter);
499 struct ifmedia media;
503 u16 msix_base; /* station base MSIX vector */
508 struct ixl_queue *queues; /* head of queues */
515 /* MAC/VLAN Filter list */
516 struct ixl_ftl_head ftl;
519 struct i40e_aqc_vsi_properties_data info;
521 eventhandler_tag vlan_attach;
522 eventhandler_tag vlan_detach;
525 /* Per-VSI stats from hardware */
526 struct i40e_eth_stats eth_stats;
527 struct i40e_eth_stats eth_stats_offsets;
528 bool stat_offsets_loaded;
529 /* VSI stat counters */
542 /* Driver statistics */
549 struct sysctl_oid *vsi_node;
553 ** Find the number of unrefreshed RX descriptors
556 ixl_rx_unrefreshed(struct ixl_queue *que)
558 struct rx_ring *rxr = &que->rxr;
560 if (rxr->next_check > rxr->next_refresh)
561 return (rxr->next_check - rxr->next_refresh - 1);
563 return ((que->num_desc + rxr->next_check) -
564 rxr->next_refresh - 1);
568 ** Find the next available unused filter
570 static inline struct ixl_mac_filter *
571 ixl_get_filter(struct ixl_vsi *vsi)
573 struct ixl_mac_filter *f;
575 /* create a new empty filter */
576 f = malloc(sizeof(struct ixl_mac_filter),
577 M_DEVBUF, M_NOWAIT | M_ZERO);
579 SLIST_INSERT_HEAD(&vsi->ftl, f, next);
585 ** Compare two ethernet addresses
588 cmp_etheraddr(const u8 *ea1, const u8 *ea2)
592 if ((ea1[0] == ea2[0]) && (ea1[1] == ea2[1]) &&
593 (ea1[2] == ea2[2]) && (ea1[3] == ea2[3]) &&
594 (ea1[4] == ea2[4]) && (ea1[5] == ea2[5]))
601 * Info for stats sysctls
603 struct ixl_sysctl_info {
609 extern int ixl_atr_rate;
612 ** ixl_fw_version_str - format the FW and NVM version strings
615 ixl_fw_version_str(struct i40e_hw *hw)
619 snprintf(buf, sizeof(buf),
620 "f%d.%d a%d.%d n%02x.%02x e%08x",
621 hw->aq.fw_maj_ver, hw->aq.fw_min_ver,
622 hw->aq.api_maj_ver, hw->aq.api_min_ver,
623 (hw->nvm.version & IXL_NVM_VERSION_HI_MASK) >>
624 IXL_NVM_VERSION_HI_SHIFT,
625 (hw->nvm.version & IXL_NVM_VERSION_LO_MASK) >>
626 IXL_NVM_VERSION_LO_SHIFT,
631 /*********************************************************************
632 * TXRX Function prototypes
633 *********************************************************************/
634 int ixl_allocate_tx_data(struct ixl_queue *);
635 int ixl_allocate_rx_data(struct ixl_queue *);
636 void ixl_init_tx_ring(struct ixl_queue *);
637 int ixl_init_rx_ring(struct ixl_queue *);
638 bool ixl_rxeof(struct ixl_queue *, int);
639 bool ixl_txeof(struct ixl_queue *);
640 int ixl_mq_start(struct ifnet *, struct mbuf *);
641 int ixl_mq_start_locked(struct ifnet *, struct tx_ring *);
642 void ixl_deferred_mq_start(void *, int);
643 void ixl_qflush(struct ifnet *);
644 void ixl_free_vsi(struct ixl_vsi *);
645 void ixl_free_que_tx(struct ixl_queue *);
646 void ixl_free_que_rx(struct ixl_queue *);
648 void ixl_atr(struct ixl_queue *, struct tcphdr *, int);
650 #if __FreeBSD_version >= 1100000
651 uint64_t ixl_get_counter(if_t ifp, ift_counter cnt);