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>
92 #include <machine/smp.h>
96 #include <sys/iov_schema.h>
97 #include <dev/pci/pci_iov.h>
100 #include "i40e_type.h"
101 #include "i40e_prototype.h"
103 #if defined(IXL_DEBUG) || defined(IXL_DEBUG_SYSCTL)
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.
167 * Tx descriptors are always 16 bytes, but Rx descriptors can be 32 bytes.
168 * The driver currently always uses 32 byte Rx descriptors.
170 #define DEFAULT_RING 1024
171 #define PERFORM_RING 2048
172 #define MAX_RING 4096
176 ** Default number of entries in Tx queue buf_ring.
178 #define SMALL_TXBRSZ 4096
179 /* This may require mbuf cluster tuning */
180 #define DEFAULT_TXBRSZ (SMALL_TXBRSZ * SMALL_TXBRSZ)
182 /* Alignment for rings */
183 #define DBA_ALIGN 128
186 * This is the max watchdog interval, ie. the time that can
187 * pass between any two TX clean operations, such only happening
188 * when the TX hardware is functioning.
190 #define IXL_WATCHDOG (10 * hz)
193 * This parameters control when the driver calls the routine to reclaim
194 * transmit descriptors.
196 #define IXL_TX_CLEANUP_THRESHOLD (que->num_desc / 8)
197 #define IXL_TX_OP_THRESHOLD (que->num_desc / 32)
199 /* Flow control constants */
200 #define IXL_FC_PAUSE 0xFFFF
201 #define IXL_FC_HI 0x20000
202 #define IXL_FC_LO 0x10000
204 #define MAX_MULTICAST_ADDR 128
207 #define IXL_ADM_LIMIT 2
208 #define IXL_TSO_SIZE 65535
209 #define IXL_AQ_BUF_SZ ((u32) 4096)
210 #define IXL_RX_HDR 128
211 /* Controls the length of the Admin Queue */
212 #define IXL_AQ_LEN 256
213 #define IXL_AQ_LEN_MAX 1024
214 #define IXL_AQ_BUFSZ 4096
215 #define IXL_RX_LIMIT 512
218 #define IXL_ITR_NONE 3
219 #define IXL_QUEUE_EOL 0x7FF
220 #define IXL_MAX_FRAME 9728
221 #define IXL_MAX_TX_SEGS 8
222 #define IXL_MAX_TSO_SEGS 66
223 #define IXL_SPARSE_CHAIN 6
224 #define IXL_QUEUE_HUNG 0x80000000
227 #define IXL_VF_MAX_BUFFER 0x3F80
228 #define IXL_VF_MAX_HDR_BUFFER 0x840
229 #define IXL_VF_MAX_FRAME 0x3FFF
231 /* ERJ: hardware can support ~1.5k filters between all functions */
232 #define IXL_MAX_FILTERS 256
233 #define IXL_MAX_TX_BUSY 10
235 #define IXL_NVM_VERSION_LO_SHIFT 0
236 #define IXL_NVM_VERSION_LO_MASK (0xff << IXL_NVM_VERSION_LO_SHIFT)
237 #define IXL_NVM_VERSION_HI_SHIFT 12
238 #define IXL_NVM_VERSION_HI_MASK (0xf << IXL_NVM_VERSION_HI_SHIFT)
242 * Interrupt Moderation parameters
244 #define IXL_MAX_ITR 0x07FF
245 #define IXL_ITR_100K 0x0005
246 #define IXL_ITR_20K 0x0019
247 #define IXL_ITR_8K 0x003E
248 #define IXL_ITR_4K 0x007A
249 #define IXL_ITR_DYNAMIC 0x8000
250 #define IXL_LOW_LATENCY 0
251 #define IXL_AVE_LATENCY 1
252 #define IXL_BULK_LATENCY 2
255 #define IXL_FILTER_USED (u16)(1 << 0)
256 #define IXL_FILTER_VLAN (u16)(1 << 1)
257 #define IXL_FILTER_ADD (u16)(1 << 2)
258 #define IXL_FILTER_DEL (u16)(1 << 3)
259 #define IXL_FILTER_MC (u16)(1 << 4)
261 /* used in the vlan field of the filter when not a vlan */
262 #define IXL_VLAN_ANY -1
264 #define CSUM_OFFLOAD_IPV4 (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
265 #define CSUM_OFFLOAD_IPV6 (CSUM_TCP_IPV6|CSUM_UDP_IPV6|CSUM_SCTP_IPV6)
266 #define CSUM_OFFLOAD (CSUM_OFFLOAD_IPV4|CSUM_OFFLOAD_IPV6|CSUM_TSO)
268 /* Misc flags for ixl_vsi.flags */
269 #define IXL_FLAGS_KEEP_TSO4 (1 << 0)
270 #define IXL_FLAGS_KEEP_TSO6 (1 << 1)
272 #define IXL_VF_RESET_TIMEOUT 100
274 #define IXL_VSI_DATA_PORT 0x01
276 #define IXLV_MAX_QUEUES 16
277 #define IXL_MAX_VSI_QUEUES (2 * (I40E_VSILAN_QTABLE_MAX_INDEX + 1))
279 #define IXL_RX_CTX_BASE_UNITS 128
280 #define IXL_TX_CTX_BASE_UNITS 128
282 #define IXL_VPINT_LNKLSTN_REG(hw, vector, vf_num) \
283 I40E_VPINT_LNKLSTN(((vector) - 1) + \
284 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
286 #define IXL_VFINT_DYN_CTLN_REG(hw, vector, vf_num) \
287 I40E_VFINT_DYN_CTLN(((vector) - 1) + \
288 (((hw)->func_caps.num_msix_vectors_vf - 1) * (vf_num)))
290 #define IXL_PF_PCI_CIAA_VF_DEVICE_STATUS 0xAA
292 #define IXL_PF_PCI_CIAD_VF_TRANS_PENDING_MASK 0x20
294 #define IXL_GLGEN_VFLRSTAT_INDEX(glb_vf) ((glb_vf) / 32)
295 #define IXL_GLGEN_VFLRSTAT_MASK(glb_vf) (1 << ((glb_vf) % 32))
297 #define IXL_MAX_ITR_IDX 3
299 #define IXL_END_OF_INTR_LNKLST 0x7FF
301 #define IXL_TX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
302 #define IXL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
303 #define IXL_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
304 #define IXL_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->mtx)
305 #define IXL_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED)
307 #define IXL_RX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
308 #define IXL_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
309 #define IXL_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
311 /* Pre-11 counter(9) compatibility */
312 #if __FreeBSD_version >= 1100036
313 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ipackets = (count)
314 #define IXL_SET_IERRORS(vsi, count) (vsi)->ierrors = (count)
315 #define IXL_SET_OPACKETS(vsi, count) (vsi)->opackets = (count)
316 #define IXL_SET_OERRORS(vsi, count) (vsi)->oerrors = (count)
317 #define IXL_SET_COLLISIONS(vsi, count) /* Do nothing; collisions is always 0. */
318 #define IXL_SET_IBYTES(vsi, count) (vsi)->ibytes = (count)
319 #define IXL_SET_OBYTES(vsi, count) (vsi)->obytes = (count)
320 #define IXL_SET_IMCASTS(vsi, count) (vsi)->imcasts = (count)
321 #define IXL_SET_OMCASTS(vsi, count) (vsi)->omcasts = (count)
322 #define IXL_SET_IQDROPS(vsi, count) (vsi)->iqdrops = (count)
323 #define IXL_SET_OQDROPS(vsi, count) (vsi)->oqdrops = (count)
324 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
326 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ifp->if_ipackets = (count)
327 #define IXL_SET_IERRORS(vsi, count) (vsi)->ifp->if_ierrors = (count)
328 #define IXL_SET_OPACKETS(vsi, count) (vsi)->ifp->if_opackets = (count)
329 #define IXL_SET_OERRORS(vsi, count) (vsi)->ifp->if_oerrors = (count)
330 #define IXL_SET_COLLISIONS(vsi, count) (vsi)->ifp->if_collisions = (count)
331 #define IXL_SET_IBYTES(vsi, count) (vsi)->ifp->if_ibytes = (count)
332 #define IXL_SET_OBYTES(vsi, count) (vsi)->ifp->if_obytes = (count)
333 #define IXL_SET_IMCASTS(vsi, count) (vsi)->ifp->if_imcasts = (count)
334 #define IXL_SET_OMCASTS(vsi, count) (vsi)->ifp->if_omcasts = (count)
335 #define IXL_SET_IQDROPS(vsi, count) (vsi)->ifp->if_iqdrops = (count)
336 #define IXL_SET_OQDROPS(vsi, odrops) (vsi)->ifp->if_snd.ifq_drops = (odrops)
337 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
340 /* Pre-10.2 media type compatibility */
341 #if __FreeBSD_version < 1002000
342 #define IFM_OTHER IFM_UNKNOWN
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];
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;
500 enum i40e_vsi_type type;
504 u16 msix_base; /* station base MSIX vector */
509 struct ixl_queue *queues; /* head of queues */
518 /* MAC/VLAN Filter list */
519 struct ixl_ftl_head ftl;
522 struct i40e_aqc_vsi_properties_data info;
524 eventhandler_tag vlan_attach;
525 eventhandler_tag vlan_detach;
528 /* Per-VSI stats from hardware */
529 struct i40e_eth_stats eth_stats;
530 struct i40e_eth_stats eth_stats_offsets;
531 bool stat_offsets_loaded;
532 /* VSI stat counters */
545 /* Driver statistics */
552 struct sysctl_oid *vsi_node;
556 ** Find the number of unrefreshed RX descriptors
559 ixl_rx_unrefreshed(struct ixl_queue *que)
561 struct rx_ring *rxr = &que->rxr;
563 if (rxr->next_check > rxr->next_refresh)
564 return (rxr->next_check - rxr->next_refresh - 1);
566 return ((que->num_desc + rxr->next_check) -
567 rxr->next_refresh - 1);
571 ** Find the next available unused filter
573 static inline struct ixl_mac_filter *
574 ixl_get_filter(struct ixl_vsi *vsi)
576 struct ixl_mac_filter *f;
578 /* create a new empty filter */
579 f = malloc(sizeof(struct ixl_mac_filter),
580 M_DEVBUF, M_NOWAIT | M_ZERO);
582 SLIST_INSERT_HEAD(&vsi->ftl, f, next);
588 ** Compare two ethernet addresses
591 cmp_etheraddr(const u8 *ea1, const u8 *ea2)
595 if ((ea1[0] == ea2[0]) && (ea1[1] == ea2[1]) &&
596 (ea1[2] == ea2[2]) && (ea1[3] == ea2[3]) &&
597 (ea1[4] == ea2[4]) && (ea1[5] == ea2[5]))
604 * Info for stats sysctls
606 struct ixl_sysctl_info {
612 extern int ixl_atr_rate;
614 /*********************************************************************
615 * TXRX Function prototypes
616 *********************************************************************/
617 int ixl_allocate_tx_data(struct ixl_queue *);
618 int ixl_allocate_rx_data(struct ixl_queue *);
619 void ixl_init_tx_ring(struct ixl_queue *);
620 int ixl_init_rx_ring(struct ixl_queue *);
621 bool ixl_rxeof(struct ixl_queue *, int);
622 bool ixl_txeof(struct ixl_queue *);
623 int ixl_mq_start(struct ifnet *, struct mbuf *);
624 int ixl_mq_start_locked(struct ifnet *, struct tx_ring *);
625 void ixl_deferred_mq_start(void *, int);
626 void ixl_qflush(struct ifnet *);
627 void ixl_free_vsi(struct ixl_vsi *);
628 void ixl_free_que_tx(struct ixl_queue *);
629 void ixl_free_que_rx(struct ixl_queue *);
631 void ixl_atr(struct ixl_queue *, struct tcphdr *, int);
633 #if __FreeBSD_version >= 1100000
634 uint64_t ixl_get_counter(if_t ifp, ift_counter cnt);