1 /******************************************************************************
3 Copyright (c) 2013-2014, 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>
93 #include "i40e_type.h"
94 #include "i40e_prototype.h"
99 #define MAC_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x"
100 #define MAC_FORMAT_ARGS(mac_addr) \
101 (mac_addr)[0], (mac_addr)[1], (mac_addr)[2], (mac_addr)[3], \
102 (mac_addr)[4], (mac_addr)[5]
103 #define ON_OFF_STR(is_set) ((is_set) ? "On" : "Off")
106 #define _DBG_PRINTF(S, ...) printf("%s: " S "\n", __func__, ##__VA_ARGS__)
107 #define _DEV_DBG_PRINTF(dev, S, ...) device_printf(dev, "%s: " S "\n", __func__, ##__VA_ARGS__)
108 #define _IF_DBG_PRINTF(ifp, S, ...) if_printf(ifp, "%s: " S "\n", __func__, ##__VA_ARGS__)
110 /* Defines for printing generic debug information */
111 #define DPRINTF(...) _DBG_PRINTF(__VA_ARGS__)
112 #define DDPRINTF(...) _DEV_DBG_PRINTF(__VA_ARGS__)
113 #define IDPRINTF(...) _IF_DBG_PRINTF(__VA_ARGS__)
115 /* Defines for printing specific debug information */
117 #define DEBUG_IOCTL 1
120 #define INIT_DEBUGOUT(...) if (DEBUG_INIT) _DBG_PRINTF(__VA_ARGS__)
121 #define INIT_DBG_DEV(...) if (DEBUG_INIT) _DEV_DBG_PRINTF(__VA_ARGS__)
122 #define INIT_DBG_IF(...) if (DEBUG_INIT) _IF_DBG_PRINTF(__VA_ARGS__)
124 #define IOCTL_DEBUGOUT(...) if (DEBUG_IOCTL) _DBG_PRINTF(__VA_ARGS__)
125 #define IOCTL_DBG_IF2(ifp, S, ...) if (DEBUG_IOCTL) \
126 if_printf(ifp, S "\n", ##__VA_ARGS__)
127 #define IOCTL_DBG_IF(...) if (DEBUG_IOCTL) _IF_DBG_PRINTF(__VA_ARGS__)
129 #define HW_DEBUGOUT(...) if (DEBUG_HW) _DBG_PRINTF(__VA_ARGS__)
133 #define DEBUG_IOCTL 0
137 #define DDPRINTF(...)
138 #define IDPRINTF(...)
140 #define INIT_DEBUGOUT(...)
141 #define INIT_DBG_DEV(...)
142 #define INIT_DBG_IF(...)
143 #define IOCTL_DEBUGOUT(...)
144 #define IOCTL_DBG_IF2(...)
145 #define IOCTL_DBG_IF(...)
146 #define HW_DEBUGOUT(...)
152 * Ring Descriptors Valid Range: 32-4096 Default Value: 1024 This value is the
153 * number of tx/rx descriptors allocated by the driver. Increasing this
154 * value allows the driver to queue more operations. Each descriptor is 16
155 * or 32 bytes (configurable in FVL)
157 #define DEFAULT_RING 1024
158 #define PERFORM_RING 2048
159 #define MAX_RING 4096
163 ** Default number of entries in Tx queue buf_ring.
165 #define SMALL_TXBRSZ 4096
166 /* This may require mbuf cluster tuning */
167 #define DEFAULT_TXBRSZ (SMALL_TXBRSZ * SMALL_TXBRSZ)
169 /* Alignment for rings */
170 #define DBA_ALIGN 128
173 * This parameter controls the maximum no of times the driver will loop in
174 * the isr. Minimum Value = 1
179 * This is the max watchdog interval, ie. the time that can
180 * pass between any two TX clean operations, such only happening
181 * when the TX hardware is functioning.
183 #define IXL_WATCHDOG (10 * hz)
186 * This parameters control when the driver calls the routine to reclaim
187 * transmit descriptors.
189 #define IXL_TX_CLEANUP_THRESHOLD (que->num_desc / 8)
190 #define IXL_TX_OP_THRESHOLD (que->num_desc / 32)
192 /* Flow control constants */
193 #define IXL_FC_PAUSE 0xFFFF
194 #define IXL_FC_HI 0x20000
195 #define IXL_FC_LO 0x10000
197 #define MAX_MULTICAST_ADDR 128
200 #define IXL_ADM_LIMIT 2
201 #define IXL_TSO_SIZE 65535
202 #define IXL_TX_BUF_SZ ((u32) 1514)
203 #define IXL_AQ_BUF_SZ ((u32) 4096)
204 #define IXL_RX_HDR 128
205 #define IXL_AQ_LEN 256
206 #define IXL_AQ_BUFSZ 4096
207 #define IXL_RX_LIMIT 512
210 #define IXL_ITR_NONE 3
211 #define IXL_QUEUE_EOL 0x7FF
212 #define IXL_MAX_FRAME 0x2600
213 #define IXL_MAX_TX_SEGS 8
214 #define IXL_MAX_TSO_SEGS 66
215 #define IXL_SPARSE_CHAIN 6
216 #define IXL_QUEUE_HUNG 0x80000000
218 /* ERJ: hardware can support ~1.5k filters between all functions */
219 #define IXL_MAX_FILTERS 256
220 #define IXL_MAX_TX_BUSY 10
222 #define IXL_NVM_VERSION_LO_SHIFT 0
223 #define IXL_NVM_VERSION_LO_MASK (0xff << IXL_NVM_VERSION_LO_SHIFT)
224 #define IXL_NVM_VERSION_HI_SHIFT 12
225 #define IXL_NVM_VERSION_HI_MASK (0xf << IXL_NVM_VERSION_HI_SHIFT)
229 * Interrupt Moderation parameters
231 #define IXL_MAX_ITR 0x07FF
232 #define IXL_ITR_100K 0x0005
233 #define IXL_ITR_20K 0x0019
234 #define IXL_ITR_8K 0x003E
235 #define IXL_ITR_4K 0x007A
236 #define IXL_ITR_DYNAMIC 0x8000
237 #define IXL_LOW_LATENCY 0
238 #define IXL_AVE_LATENCY 1
239 #define IXL_BULK_LATENCY 2
242 #define IXL_FILTER_USED (u16)(1 << 0)
243 #define IXL_FILTER_VLAN (u16)(1 << 1)
244 #define IXL_FILTER_ADD (u16)(1 << 2)
245 #define IXL_FILTER_DEL (u16)(1 << 3)
246 #define IXL_FILTER_MC (u16)(1 << 4)
248 /* used in the vlan field of the filter when not a vlan */
249 #define IXL_VLAN_ANY -1
251 #define CSUM_OFFLOAD_IPV4 (CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
252 #define CSUM_OFFLOAD_IPV6 (CSUM_TCP_IPV6|CSUM_UDP_IPV6|CSUM_SCTP_IPV6)
253 #define CSUM_OFFLOAD (CSUM_OFFLOAD_IPV4|CSUM_OFFLOAD_IPV6|CSUM_TSO)
255 /* Misc flags for ixl_vsi.flags */
256 #define IXL_FLAGS_KEEP_TSO4 (1 << 0)
257 #define IXL_FLAGS_KEEP_TSO6 (1 << 1)
259 #define IXL_TX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
260 #define IXL_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
261 #define IXL_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
262 #define IXL_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->mtx)
263 #define IXL_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED)
265 #define IXL_RX_LOCK(_sc) mtx_lock(&(_sc)->mtx)
266 #define IXL_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
267 #define IXL_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->mtx)
269 #if __FreeBSD_version >= 1100000
270 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ipackets = (count)
271 #define IXL_SET_IERRORS(vsi, count) (vsi)->ierrors = (count)
272 #define IXL_SET_OPACKETS(vsi, count) (vsi)->opackets = (count)
273 #define IXL_SET_OERRORS(vsi, count) (vsi)->oerrors = (count)
274 #define IXL_SET_COLLISIONS(vsi, count) /* Do nothing; collisions is always 0. */
275 #define IXL_SET_IBYTES(vsi, count) (vsi)->ibytes = (count)
276 #define IXL_SET_OBYTES(vsi, count) (vsi)->obytes = (count)
277 #define IXL_SET_IMCASTS(vsi, count) (vsi)->imcasts = (count)
278 #define IXL_SET_OMCASTS(vsi, count) (vsi)->omcasts = (count)
279 #define IXL_SET_IQDROPS(vsi, count) (vsi)->iqdrops = (count)
280 #define IXL_SET_OQDROPS(vsi, count) (vsi)->iqdrops = (count)
281 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
283 #define IXL_SET_IPACKETS(vsi, count) (vsi)->ifp->if_ipackets = (count)
284 #define IXL_SET_IERRORS(vsi, count) (vsi)->ifp->if_ierrors = (count)
285 #define IXL_SET_OPACKETS(vsi, count) (vsi)->ifp->if_opackets = (count)
286 #define IXL_SET_OERRORS(vsi, count) (vsi)->ifp->if_oerrors = (count)
287 #define IXL_SET_COLLISIONS(vsi, count) (vsi)->ifp->if_collisions = (count)
288 #define IXL_SET_IBYTES(vsi, count) (vsi)->ifp->if_ibytes = (count)
289 #define IXL_SET_OBYTES(vsi, count) (vsi)->ifp->if_obytes = (count)
290 #define IXL_SET_IMCASTS(vsi, count) (vsi)->ifp->if_imcasts = (count)
291 #define IXL_SET_OMCASTS(vsi, count) (vsi)->ifp->if_omcasts = (count)
292 #define IXL_SET_IQDROPS(vsi, count) (vsi)->ifp->if_iqdrops = (count)
293 #define IXL_SET_OQDROPS(vsi, odrops) (vsi)->ifp->if_snd.ifq_drops = (odrops)
294 #define IXL_SET_NOPROTO(vsi, count) (vsi)->noproto = (count)
298 *****************************************************************************
301 * This array contains the list of Subvendor/Subdevice IDs on which the driver
304 *****************************************************************************
306 typedef struct _ixl_vendor_info_t {
307 unsigned int vendor_id;
308 unsigned int device_id;
309 unsigned int subvendor_id;
310 unsigned int subdevice_id;
331 ** This struct has multiple uses, multicast
332 ** addresses, vlans, and mac filters all use it.
334 struct ixl_mac_filter {
335 SLIST_ENTRY(ixl_mac_filter) next;
336 u8 macaddr[ETHER_ADDR_LEN];
343 * The Transmit ring control struct
346 struct ixl_queue *que;
349 struct i40e_tx_desc *base;
350 struct i40e_dma_mem dma;
357 struct ixl_tx_buf *buffers;
360 bus_dma_tag_t tx_tag;
361 bus_dma_tag_t tso_tag;
365 /* Used for Dynamic ITR calculation */
377 * The Receive ring control struct
380 struct ixl_queue *que;
382 union i40e_rx_desc *base;
383 struct i40e_dma_mem dma;
393 struct ixl_rx_buf *buffers;
399 /* Used for Dynamic ITR calculation */
412 ** Driver queue struct: this is the interrupt container
413 ** for the associated tx and rx ring pair.
418 u32 msix; /* This queue's MSIX vector */
419 u32 eims; /* This queue's EIMS bit */
420 struct resource *res;
422 int num_desc; /* both tx and rx */
428 struct taskqueue *tq;
433 u64 mbuf_defrag_failed;
442 ** Virtual Station interface:
443 ** there would be one of these per traffic class/type
444 ** for now just one, and its embedded in the pf
446 SLIST_HEAD(ixl_ftl_head, ixl_mac_filter);
452 struct ifmedia media;
455 u16 msix_base; /* station base MSIX vector */
459 struct ixl_queue *queues; /* head of queues */
465 u32 fc; /* local flow ctrl setting */
467 /* MAC/VLAN Filter list */
468 struct ixl_ftl_head ftl;
470 struct i40e_aqc_vsi_properties_data info;
472 eventhandler_tag vlan_attach;
473 eventhandler_tag vlan_detach;
476 /* Per-VSI stats from hardware */
477 struct i40e_eth_stats eth_stats;
478 struct i40e_eth_stats eth_stats_offsets;
479 bool stat_offsets_loaded;
480 /* VSI stat counters */
493 /* Driver statistics */
503 ** Find the number of unrefreshed RX descriptors
506 ixl_rx_unrefreshed(struct ixl_queue *que)
508 struct rx_ring *rxr = &que->rxr;
510 if (rxr->next_check > rxr->next_refresh)
511 return (rxr->next_check - rxr->next_refresh - 1);
513 return ((que->num_desc + rxr->next_check) -
514 rxr->next_refresh - 1);
518 ** Find the next available unused filter
520 static inline struct ixl_mac_filter *
521 ixl_get_filter(struct ixl_vsi *vsi)
523 struct ixl_mac_filter *f;
525 /* create a new empty filter */
526 f = malloc(sizeof(struct ixl_mac_filter),
527 M_DEVBUF, M_NOWAIT | M_ZERO);
529 SLIST_INSERT_HEAD(&vsi->ftl, f, next);
535 ** Compare two ethernet addresses
538 cmp_etheraddr(u8 *ea1, u8 *ea2)
542 if ((ea1[0] == ea2[0]) && (ea1[1] == ea2[1]) &&
543 (ea1[2] == ea2[2]) && (ea1[3] == ea2[3]) &&
544 (ea1[4] == ea2[4]) && (ea1[5] == ea2[5]))
551 * Info for stats sysctls
553 struct ixl_sysctl_info {
559 extern int ixl_atr_rate;
562 ** ixl_fw_version_str - format the FW and NVM version strings
565 ixl_fw_version_str(struct i40e_hw *hw)
569 snprintf(buf, sizeof(buf),
570 "f%d.%d a%d.%d n%02x.%02x e%08x",
571 hw->aq.fw_maj_ver, hw->aq.fw_min_ver,
572 hw->aq.api_maj_ver, hw->aq.api_min_ver,
573 (hw->nvm.version & IXL_NVM_VERSION_HI_MASK) >>
574 IXL_NVM_VERSION_HI_SHIFT,
575 (hw->nvm.version & IXL_NVM_VERSION_LO_MASK) >>
576 IXL_NVM_VERSION_LO_SHIFT,
581 /*********************************************************************
582 * TXRX Function prototypes
583 *********************************************************************/
584 int ixl_allocate_tx_data(struct ixl_queue *);
585 int ixl_allocate_rx_data(struct ixl_queue *);
586 void ixl_init_tx_ring(struct ixl_queue *);
587 int ixl_init_rx_ring(struct ixl_queue *);
588 bool ixl_rxeof(struct ixl_queue *, int);
589 bool ixl_txeof(struct ixl_queue *);
590 int ixl_mq_start(struct ifnet *, struct mbuf *);
591 int ixl_mq_start_locked(struct ifnet *, struct tx_ring *);
592 void ixl_deferred_mq_start(void *, int);
593 void ixl_qflush(struct ifnet *);
594 void ixl_free_vsi(struct ixl_vsi *);
595 void ixl_free_que_tx(struct ixl_queue *);
596 void ixl_free_que_rx(struct ixl_queue *);
598 void ixl_atr(struct ixl_queue *, struct tcphdr *, int);
600 #if __FreeBSD_version >= 1100000
601 uint64_t ixl_get_counter(if_t ifp, ift_counter cnt);