]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ixl/if_iavf.c
Merge compiler-rt trunk r351319, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / dev / ixl / if_iavf.c
1 /******************************************************************************
2
3   Copyright (c) 2013-2018, Intel Corporation
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, are permitted provided that the following conditions are met:
8   
9    1. Redistributions of source code must retain the above copyright notice, 
10       this list of conditions and the following disclaimer.
11   
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.
15   
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.
19   
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.
31
32 ******************************************************************************/
33 /*$FreeBSD$*/
34
35 #include "iavf.h"
36
37 /*********************************************************************
38  *  Driver version
39  *********************************************************************/
40 #define IAVF_DRIVER_VERSION_MAJOR       2
41 #define IAVF_DRIVER_VERSION_MINOR       0
42 #define IAVF_DRIVER_VERSION_BUILD       0
43
44 #define IAVF_DRIVER_VERSION_STRING                      \
45     __XSTRING(IAVF_DRIVER_VERSION_MAJOR) "."            \
46     __XSTRING(IAVF_DRIVER_VERSION_MINOR) "."            \
47     __XSTRING(IAVF_DRIVER_VERSION_BUILD) "-k"
48
49 /*********************************************************************
50  *  PCI Device ID Table
51  *
52  *  Used by probe to select devices to load on
53  *
54  *  ( Vendor ID, Device ID, Branding String )
55  *********************************************************************/
56
57 static pci_vendor_info_t iavf_vendor_info_array[] =
58 {
59         PVID(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_VF, "Intel(R) Ethernet Virtual Function 700 Series"),
60         PVID(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_VF, "Intel(R) Ethernet Virtual Function 700 Series (X722)"),
61         PVID(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_ADAPTIVE_VF, "Intel(R) Ethernet Adaptive Virtual Function"),
62         /* required last entry */
63         PVID_END
64 };
65
66 /*********************************************************************
67  *  Function prototypes
68  *********************************************************************/
69 static void      *iavf_register(device_t dev);
70 static int       iavf_if_attach_pre(if_ctx_t ctx);
71 static int       iavf_if_attach_post(if_ctx_t ctx);
72 static int       iavf_if_detach(if_ctx_t ctx);
73 static int       iavf_if_shutdown(if_ctx_t ctx);
74 static int       iavf_if_suspend(if_ctx_t ctx);
75 static int       iavf_if_resume(if_ctx_t ctx);
76 static int       iavf_if_msix_intr_assign(if_ctx_t ctx, int msix);
77 static void      iavf_if_enable_intr(if_ctx_t ctx);
78 static void      iavf_if_disable_intr(if_ctx_t ctx);
79 static int       iavf_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
80 static int       iavf_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
81 static int       iavf_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
82 static int       iavf_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
83 static void      iavf_if_queues_free(if_ctx_t ctx);
84 static void      iavf_if_update_admin_status(if_ctx_t ctx);
85 static void      iavf_if_multi_set(if_ctx_t ctx);
86 static int       iavf_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
87 static void      iavf_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
88 static int       iavf_if_media_change(if_ctx_t ctx);
89 static int       iavf_if_promisc_set(if_ctx_t ctx, int flags);
90 static void      iavf_if_timer(if_ctx_t ctx, uint16_t qid);
91 static void      iavf_if_vlan_register(if_ctx_t ctx, u16 vtag);
92 static void      iavf_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
93 static uint64_t  iavf_if_get_counter(if_ctx_t ctx, ift_counter cnt);
94 static void      iavf_if_stop(if_ctx_t ctx);
95
96 static int      iavf_allocate_pci_resources(struct iavf_sc *);
97 static int      iavf_reset_complete(struct i40e_hw *);
98 static int      iavf_setup_vc(struct iavf_sc *);
99 static int      iavf_reset(struct iavf_sc *);
100 static int      iavf_vf_config(struct iavf_sc *);
101 static void     iavf_init_filters(struct iavf_sc *);
102 static void     iavf_free_pci_resources(struct iavf_sc *);
103 static void     iavf_free_filters(struct iavf_sc *);
104 static void     iavf_setup_interface(device_t, struct iavf_sc *);
105 static void     iavf_add_device_sysctls(struct iavf_sc *);
106 static void     iavf_enable_adminq_irq(struct i40e_hw *);
107 static void     iavf_disable_adminq_irq(struct i40e_hw *);
108 static void     iavf_enable_queue_irq(struct i40e_hw *, int);
109 static void     iavf_disable_queue_irq(struct i40e_hw *, int);
110 static void     iavf_config_rss(struct iavf_sc *);
111 static void     iavf_stop(struct iavf_sc *);
112
113 static int      iavf_add_mac_filter(struct iavf_sc *, u8 *, u16);
114 static int      iavf_del_mac_filter(struct iavf_sc *sc, u8 *macaddr);
115 static int      iavf_msix_que(void *);
116 static int      iavf_msix_adminq(void *);
117 //static void   iavf_del_multi(struct iavf_sc *sc);
118 static void     iavf_init_multi(struct iavf_sc *sc);
119 static void     iavf_configure_itr(struct iavf_sc *sc);
120
121 static int      iavf_sysctl_rx_itr(SYSCTL_HANDLER_ARGS);
122 static int      iavf_sysctl_tx_itr(SYSCTL_HANDLER_ARGS);
123 static int      iavf_sysctl_current_speed(SYSCTL_HANDLER_ARGS);
124 static int      iavf_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS);
125 static int      iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS);
126 static int      iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS);
127 static int      iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS);
128
129 static void     iavf_save_tunables(struct iavf_sc *);
130 static enum i40e_status_code
131     iavf_process_adminq(struct iavf_sc *, u16 *);
132 static int      iavf_send_vc_msg(struct iavf_sc *sc, u32 op);
133 static int      iavf_send_vc_msg_sleep(struct iavf_sc *sc, u32 op);
134
135 /*********************************************************************
136  *  FreeBSD Device Interface Entry Points
137  *********************************************************************/
138
139 static device_method_t iavf_methods[] = {
140         /* Device interface */
141         DEVMETHOD(device_register, iavf_register),
142         DEVMETHOD(device_probe, iflib_device_probe),
143         DEVMETHOD(device_attach, iflib_device_attach),
144         DEVMETHOD(device_detach, iflib_device_detach),
145         DEVMETHOD(device_shutdown, iflib_device_shutdown),
146         DEVMETHOD_END
147 };
148
149 static driver_t iavf_driver = {
150         "iavf", iavf_methods, sizeof(struct iavf_sc),
151 };
152
153 devclass_t iavf_devclass;
154 DRIVER_MODULE(iavf, pci, iavf_driver, iavf_devclass, 0, 0);
155 MODULE_PNP_INFO("U32:vendor;U32:device;U32:subvendor;U32:subdevice;U32:revision",
156     pci, iavf, iavf_vendor_info_array,
157         nitems(iavf_vendor_info_array) - 1);
158 MODULE_VERSION(iavf, 1);
159
160 MODULE_DEPEND(iavf, pci, 1, 1, 1);
161 MODULE_DEPEND(iavf, ether, 1, 1, 1);
162 MODULE_DEPEND(iavf, iflib, 1, 1, 1);
163
164 MALLOC_DEFINE(M_IAVF, "iavf", "iavf driver allocations");
165
166 static device_method_t iavf_if_methods[] = {
167         DEVMETHOD(ifdi_attach_pre, iavf_if_attach_pre),
168         DEVMETHOD(ifdi_attach_post, iavf_if_attach_post),
169         DEVMETHOD(ifdi_detach, iavf_if_detach),
170         DEVMETHOD(ifdi_shutdown, iavf_if_shutdown),
171         DEVMETHOD(ifdi_suspend, iavf_if_suspend),
172         DEVMETHOD(ifdi_resume, iavf_if_resume),
173         DEVMETHOD(ifdi_init, iavf_if_init),
174         DEVMETHOD(ifdi_stop, iavf_if_stop),
175         DEVMETHOD(ifdi_msix_intr_assign, iavf_if_msix_intr_assign),
176         DEVMETHOD(ifdi_intr_enable, iavf_if_enable_intr),
177         DEVMETHOD(ifdi_intr_disable, iavf_if_disable_intr),
178         DEVMETHOD(ifdi_rx_queue_intr_enable, iavf_if_rx_queue_intr_enable),
179         DEVMETHOD(ifdi_tx_queue_intr_enable, iavf_if_tx_queue_intr_enable),
180         DEVMETHOD(ifdi_tx_queues_alloc, iavf_if_tx_queues_alloc),
181         DEVMETHOD(ifdi_rx_queues_alloc, iavf_if_rx_queues_alloc),
182         DEVMETHOD(ifdi_queues_free, iavf_if_queues_free),
183         DEVMETHOD(ifdi_update_admin_status, iavf_if_update_admin_status),
184         DEVMETHOD(ifdi_multi_set, iavf_if_multi_set),
185         DEVMETHOD(ifdi_mtu_set, iavf_if_mtu_set),
186         DEVMETHOD(ifdi_media_status, iavf_if_media_status),
187         DEVMETHOD(ifdi_media_change, iavf_if_media_change),
188         DEVMETHOD(ifdi_promisc_set, iavf_if_promisc_set),
189         DEVMETHOD(ifdi_timer, iavf_if_timer),
190         DEVMETHOD(ifdi_vlan_register, iavf_if_vlan_register),
191         DEVMETHOD(ifdi_vlan_unregister, iavf_if_vlan_unregister),
192         DEVMETHOD(ifdi_get_counter, iavf_if_get_counter),
193         DEVMETHOD_END
194 };
195
196 static driver_t iavf_if_driver = {
197         "iavf_if", iavf_if_methods, sizeof(struct iavf_sc)
198 };
199
200 /*
201 ** TUNEABLE PARAMETERS:
202 */
203
204 static SYSCTL_NODE(_hw, OID_AUTO, iavf, CTLFLAG_RD, 0,
205     "iavf driver parameters");
206
207 /*
208  * Different method for processing TX descriptor
209  * completion.
210  */
211 static int iavf_enable_head_writeback = 0;
212 TUNABLE_INT("hw.iavf.enable_head_writeback",
213     &iavf_enable_head_writeback);
214 SYSCTL_INT(_hw_iavf, OID_AUTO, enable_head_writeback, CTLFLAG_RDTUN,
215     &iavf_enable_head_writeback, 0,
216     "For detecting last completed TX descriptor by hardware, use value written by HW instead of checking descriptors");
217
218 static int iavf_core_debug_mask = 0;
219 TUNABLE_INT("hw.iavf.core_debug_mask",
220     &iavf_core_debug_mask);
221 SYSCTL_INT(_hw_iavf, OID_AUTO, core_debug_mask, CTLFLAG_RDTUN,
222     &iavf_core_debug_mask, 0,
223     "Display debug statements that are printed in non-shared code");
224
225 static int iavf_shared_debug_mask = 0;
226 TUNABLE_INT("hw.iavf.shared_debug_mask",
227     &iavf_shared_debug_mask);
228 SYSCTL_INT(_hw_iavf, OID_AUTO, shared_debug_mask, CTLFLAG_RDTUN,
229     &iavf_shared_debug_mask, 0,
230     "Display debug statements that are printed in shared code");
231
232 int iavf_rx_itr = IXL_ITR_8K;
233 TUNABLE_INT("hw.iavf.rx_itr", &iavf_rx_itr);
234 SYSCTL_INT(_hw_iavf, OID_AUTO, rx_itr, CTLFLAG_RDTUN,
235     &iavf_rx_itr, 0, "RX Interrupt Rate");
236
237 int iavf_tx_itr = IXL_ITR_4K;
238 TUNABLE_INT("hw.iavf.tx_itr", &iavf_tx_itr);
239 SYSCTL_INT(_hw_iavf, OID_AUTO, tx_itr, CTLFLAG_RDTUN,
240     &iavf_tx_itr, 0, "TX Interrupt Rate");
241
242 extern struct if_txrx ixl_txrx_hwb;
243 extern struct if_txrx ixl_txrx_dwb;
244
245 static struct if_shared_ctx iavf_sctx_init = {
246         .isc_magic = IFLIB_MAGIC,
247         .isc_q_align = PAGE_SIZE,/* max(DBA_ALIGN, PAGE_SIZE) */
248         .isc_tx_maxsize = IXL_TSO_SIZE + sizeof(struct ether_vlan_header),
249         .isc_tx_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
250         .isc_tso_maxsize = IXL_TSO_SIZE + sizeof(struct ether_vlan_header),
251         .isc_tso_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
252         .isc_rx_maxsize = 16384,
253         .isc_rx_nsegments = IXL_MAX_RX_SEGS,
254         .isc_rx_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
255         .isc_nfl = 1,
256         .isc_ntxqs = 1,
257         .isc_nrxqs = 1,
258
259         .isc_admin_intrcnt = 1,
260         .isc_vendor_info = iavf_vendor_info_array,
261         .isc_driver_version = IAVF_DRIVER_VERSION_STRING,
262         .isc_driver = &iavf_if_driver,
263         .isc_flags = IFLIB_NEED_SCRATCH | IFLIB_NEED_ZERO_CSUM | IFLIB_TSO_INIT_IP | IFLIB_IS_VF,
264
265         .isc_nrxd_min = {IXL_MIN_RING},
266         .isc_ntxd_min = {IXL_MIN_RING},
267         .isc_nrxd_max = {IXL_MAX_RING},
268         .isc_ntxd_max = {IXL_MAX_RING},
269         .isc_nrxd_default = {IXL_DEFAULT_RING},
270         .isc_ntxd_default = {IXL_DEFAULT_RING},
271 };
272
273 if_shared_ctx_t iavf_sctx = &iavf_sctx_init;
274
275 /*** Functions ***/
276 static void *
277 iavf_register(device_t dev)
278 {
279         return (iavf_sctx);
280 }
281
282 static int
283 iavf_allocate_pci_resources(struct iavf_sc *sc)
284 {
285         struct i40e_hw *hw = &sc->hw;
286         device_t dev = iflib_get_dev(sc->vsi.ctx);
287         int             rid;
288
289         /* Map BAR0 */
290         rid = PCIR_BAR(0);
291         sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
292             &rid, RF_ACTIVE);
293
294         if (!(sc->pci_mem)) {
295                 device_printf(dev, "Unable to allocate bus resource: PCI memory\n");
296                 return (ENXIO);
297         }
298  
299         /* Save off the PCI information */
300         hw->vendor_id = pci_get_vendor(dev);
301         hw->device_id = pci_get_device(dev);
302         hw->revision_id = pci_read_config(dev, PCIR_REVID, 1);
303         hw->subsystem_vendor_id =
304             pci_read_config(dev, PCIR_SUBVEND_0, 2);
305         hw->subsystem_device_id =
306             pci_read_config(dev, PCIR_SUBDEV_0, 2);
307
308         hw->bus.device = pci_get_slot(dev);
309         hw->bus.func = pci_get_function(dev);
310
311         /* Save off register access information */
312         sc->osdep.mem_bus_space_tag =
313                 rman_get_bustag(sc->pci_mem);
314         sc->osdep.mem_bus_space_handle =
315                 rman_get_bushandle(sc->pci_mem);
316         sc->osdep.mem_bus_space_size = rman_get_size(sc->pci_mem);
317         sc->osdep.flush_reg = I40E_VFGEN_RSTAT;
318         sc->osdep.dev = dev;
319
320         sc->hw.hw_addr = (u8 *) &sc->osdep.mem_bus_space_handle;
321         sc->hw.back = &sc->osdep;
322
323         return (0);
324 }
325
326 static int
327 iavf_if_attach_pre(if_ctx_t ctx)
328 {
329         device_t dev;
330         struct iavf_sc *sc;
331         struct i40e_hw *hw;
332         struct ixl_vsi *vsi;
333         if_softc_ctx_t scctx;
334         int error = 0;
335
336         dev = iflib_get_dev(ctx);
337         sc = iflib_get_softc(ctx);
338
339         vsi = &sc->vsi;
340         vsi->back = sc;
341         sc->dev = dev;
342         hw = &sc->hw;
343
344         vsi->dev = dev;
345         vsi->hw = &sc->hw;
346         vsi->num_vlans = 0;
347         vsi->ctx = ctx;
348         vsi->media = iflib_get_media(ctx);
349         vsi->shared = scctx = iflib_get_softc_ctx(ctx);
350
351         iavf_save_tunables(sc);
352
353         /* Do PCI setup - map BAR0, etc */
354         if (iavf_allocate_pci_resources(sc)) {
355                 device_printf(dev, "%s: Allocation of PCI resources failed\n",
356                     __func__);
357                 error = ENXIO;
358                 goto err_early;
359         }
360
361         iavf_dbg_init(sc, "Allocated PCI resources and MSIX vectors\n");
362
363         /*
364          * XXX: This is called by init_shared_code in the PF driver,
365          * but the rest of that function does not support VFs.
366          */
367         error = i40e_set_mac_type(hw);
368         if (error) {
369                 device_printf(dev, "%s: set_mac_type failed: %d\n",
370                     __func__, error);
371                 goto err_pci_res;
372         }
373
374         error = iavf_reset_complete(hw);
375         if (error) {
376                 device_printf(dev, "%s: Device is still being reset\n",
377                     __func__);
378                 goto err_pci_res;
379         }
380
381         iavf_dbg_init(sc, "VF Device is ready for configuration\n");
382
383         /* Sets up Admin Queue */
384         error = iavf_setup_vc(sc);
385         if (error) {
386                 device_printf(dev, "%s: Error setting up PF comms, %d\n",
387                     __func__, error);
388                 goto err_pci_res;
389         }
390
391         iavf_dbg_init(sc, "PF API version verified\n");
392
393         /* Need API version before sending reset message */
394         error = iavf_reset(sc);
395         if (error) {
396                 device_printf(dev, "VF reset failed; reload the driver\n");
397                 goto err_aq;
398         }
399
400         iavf_dbg_init(sc, "VF reset complete\n");
401
402         /* Ask for VF config from PF */
403         error = iavf_vf_config(sc);
404         if (error) {
405                 device_printf(dev, "Error getting configuration from PF: %d\n",
406                     error);
407                 goto err_aq;
408         }
409
410         device_printf(dev, "VSIs %d, QPs %d, MSIX %d, RSS sizes: key %d lut %d\n",
411             sc->vf_res->num_vsis,
412             sc->vf_res->num_queue_pairs,
413             sc->vf_res->max_vectors,
414             sc->vf_res->rss_key_size,
415             sc->vf_res->rss_lut_size);
416         iavf_dbg_info(sc, "Capabilities=%b\n",
417             sc->vf_res->vf_cap_flags, IAVF_PRINTF_VF_OFFLOAD_FLAGS);
418
419         /* got VF config message back from PF, now we can parse it */
420         for (int i = 0; i < sc->vf_res->num_vsis; i++) {
421                 /* XXX: We only use the first VSI we find */
422                 if (sc->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
423                         sc->vsi_res = &sc->vf_res->vsi_res[i];
424         }
425         if (!sc->vsi_res) {
426                 device_printf(dev, "%s: no LAN VSI found\n", __func__);
427                 error = EIO;
428                 goto err_res_buf;
429         }
430         vsi->id = sc->vsi_res->vsi_id;
431
432         iavf_dbg_init(sc, "Resource Acquisition complete\n");
433
434         /* If no mac address was assigned just make a random one */
435         if (!iavf_check_ether_addr(hw->mac.addr)) {
436                 u8 addr[ETHER_ADDR_LEN];
437                 arc4rand(&addr, sizeof(addr), 0);
438                 addr[0] &= 0xFE;
439                 addr[0] |= 0x02;
440                 bcopy(addr, hw->mac.addr, sizeof(addr));
441         }
442         bcopy(hw->mac.addr, hw->mac.perm_addr, ETHER_ADDR_LEN);
443         iflib_set_mac(ctx, hw->mac.addr);
444
445         /* Allocate filter lists */
446         iavf_init_filters(sc);
447
448         /* Fill out more iflib parameters */
449         scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max =
450             sc->vsi_res->num_queue_pairs;
451         if (vsi->enable_head_writeback) {
452                 scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
453                     * sizeof(struct i40e_tx_desc) + sizeof(u32), DBA_ALIGN);
454                 scctx->isc_txrx = &ixl_txrx_hwb;
455         } else {
456                 scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
457                     * sizeof(struct i40e_tx_desc), DBA_ALIGN);
458                 scctx->isc_txrx = &ixl_txrx_dwb;
459         }
460         scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
461             * sizeof(union i40e_32byte_rx_desc), DBA_ALIGN);
462         scctx->isc_msix_bar = PCIR_BAR(IXL_MSIX_BAR);
463         scctx->isc_tx_nsegments = IXL_MAX_TX_SEGS;
464         scctx->isc_tx_tso_segments_max = IXL_MAX_TSO_SEGS;
465         scctx->isc_tx_tso_size_max = IXL_TSO_SIZE;
466         scctx->isc_tx_tso_segsize_max = IXL_MAX_DMA_SEG_SIZE;
467         scctx->isc_rss_table_size = IXL_RSS_VSI_LUT_SIZE;
468         scctx->isc_tx_csum_flags = CSUM_OFFLOAD;
469         scctx->isc_capabilities = scctx->isc_capenable = IXL_CAPS;
470
471         return (0);
472
473 err_res_buf:
474         free(sc->vf_res, M_IAVF);
475 err_aq:
476         i40e_shutdown_adminq(hw);
477 err_pci_res:
478         iavf_free_pci_resources(sc);
479 err_early:
480         return (error);
481 }
482
483 static int
484 iavf_if_attach_post(if_ctx_t ctx)
485 {
486         device_t dev;
487         struct iavf_sc  *sc;
488         struct i40e_hw  *hw;
489         struct ixl_vsi *vsi;
490         int error = 0;
491
492         INIT_DBG_DEV(dev, "begin");
493
494         dev = iflib_get_dev(ctx);
495         sc = iflib_get_softc(ctx);
496         vsi = &sc->vsi;
497         vsi->ifp = iflib_get_ifp(ctx);
498         hw = &sc->hw;
499
500         /* Save off determined number of queues for interface */
501         vsi->num_rx_queues = vsi->shared->isc_nrxqsets;
502         vsi->num_tx_queues = vsi->shared->isc_ntxqsets;
503
504         /* Setup the stack interface */
505         iavf_setup_interface(dev, sc);
506
507         INIT_DBG_DEV(dev, "Interface setup complete");
508
509         /* Initialize statistics & add sysctls */
510         bzero(&sc->vsi.eth_stats, sizeof(struct i40e_eth_stats));
511         iavf_add_device_sysctls(sc);
512
513         sc->init_state = IAVF_INIT_READY;
514         atomic_store_rel_32(&sc->queues_enabled, 0);
515
516         /* We want AQ enabled early for init */
517         iavf_enable_adminq_irq(hw);
518
519         INIT_DBG_DEV(dev, "end");
520
521         return (error);
522 }
523
524 /**
525  * XXX: iflib always ignores the return value of detach()
526  * -> This means that this isn't allowed to fail
527  */
528 static int
529 iavf_if_detach(if_ctx_t ctx)
530 {
531         struct iavf_sc *sc = iflib_get_softc(ctx);
532         struct ixl_vsi *vsi = &sc->vsi;
533         struct i40e_hw *hw = &sc->hw;
534         device_t dev = sc->dev;
535         enum i40e_status_code status;
536
537         INIT_DBG_DEV(dev, "begin");
538
539         /* Remove all the media and link information */
540         ifmedia_removeall(vsi->media);
541
542         iavf_disable_adminq_irq(hw);
543         status = i40e_shutdown_adminq(&sc->hw);
544         if (status != I40E_SUCCESS) {
545                 device_printf(dev,
546                     "i40e_shutdown_adminq() failed with status %s\n",
547                     i40e_stat_str(hw, status));
548         }
549
550         free(sc->vf_res, M_IAVF);
551         iavf_free_pci_resources(sc);
552         iavf_free_filters(sc);
553
554         INIT_DBG_DEV(dev, "end");
555         return (0);
556 }
557
558 static int
559 iavf_if_shutdown(if_ctx_t ctx)
560 {
561         return (0);
562 }
563
564 static int
565 iavf_if_suspend(if_ctx_t ctx)
566 {
567         return (0);
568 }
569
570 static int
571 iavf_if_resume(if_ctx_t ctx)
572 {
573         return (0);
574 }
575
576 static int
577 iavf_send_vc_msg_sleep(struct iavf_sc *sc, u32 op)
578 {
579         int error = 0;
580         if_ctx_t ctx = sc->vsi.ctx;
581
582         error = ixl_vc_send_cmd(sc, op);
583         if (error != 0) {
584                 iavf_dbg_vc(sc, "Error sending %b: %d\n", op, IAVF_FLAGS, error);
585                 return (error);
586         }
587
588         /* Don't wait for a response if the device is being detached. */
589         if (!iflib_in_detach(ctx)) {
590                 iavf_dbg_vc(sc, "Sleeping for op %b\n", op, IAVF_FLAGS);
591                 error = sx_sleep(ixl_vc_get_op_chan(sc, op),
592                     iflib_ctx_lock_get(ctx), PRI_MAX, "iavf_vc", IAVF_AQ_TIMEOUT);
593
594                 if (error == EWOULDBLOCK)
595                         device_printf(sc->dev, "%b timed out\n", op, IAVF_FLAGS);
596         }
597
598         return (error);
599 }
600
601 static int
602 iavf_send_vc_msg(struct iavf_sc *sc, u32 op)
603 {
604         int error = 0;
605
606         error = ixl_vc_send_cmd(sc, op);
607         if (error != 0)
608                 iavf_dbg_vc(sc, "Error sending %b: %d\n", op, IAVF_FLAGS, error);
609
610         return (error);
611 }
612
613 static void
614 iavf_init_queues(struct ixl_vsi *vsi)
615 {
616         if_softc_ctx_t scctx = vsi->shared;
617         struct ixl_tx_queue *tx_que = vsi->tx_queues;
618         struct ixl_rx_queue *rx_que = vsi->rx_queues;
619         struct rx_ring *rxr;
620
621         for (int i = 0; i < vsi->num_tx_queues; i++, tx_que++)
622                 ixl_init_tx_ring(vsi, tx_que);
623
624         for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++) {
625                 rxr = &rx_que->rxr;
626
627                 if (scctx->isc_max_frame_size <= MCLBYTES)
628                         rxr->mbuf_sz = MCLBYTES;
629                 else
630                         rxr->mbuf_sz = MJUMPAGESIZE;
631
632                 wr32(vsi->hw, rxr->tail, 0);
633         }
634 }
635
636 void
637 iavf_if_init(if_ctx_t ctx)
638 {
639         struct iavf_sc *sc = iflib_get_softc(ctx);
640         struct ixl_vsi *vsi = &sc->vsi;
641         struct i40e_hw *hw = &sc->hw;
642         struct ifnet *ifp = iflib_get_ifp(ctx);
643         u8 tmpaddr[ETHER_ADDR_LEN];
644         int error = 0;
645
646         INIT_DBG_IF(ifp, "begin");
647
648         MPASS(sx_xlocked(iflib_ctx_lock_get(ctx)));
649
650         error = iavf_reset_complete(hw);
651         if (error) {
652                 device_printf(sc->dev, "%s: VF reset failed\n",
653                     __func__);
654         }
655
656         if (!i40e_check_asq_alive(hw)) {
657                 iavf_dbg_info(sc, "ASQ is not alive, re-initializing AQ\n");
658                 pci_enable_busmaster(sc->dev);
659                 i40e_shutdown_adminq(hw);
660                 i40e_init_adminq(hw);
661         }
662
663         /* Make sure queues are disabled */
664         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DISABLE_QUEUES);
665
666         bcopy(IF_LLADDR(ifp), tmpaddr, ETHER_ADDR_LEN);
667         if (!cmp_etheraddr(hw->mac.addr, tmpaddr) &&
668             (i40e_validate_mac_addr(tmpaddr) == I40E_SUCCESS)) {
669                 error = iavf_del_mac_filter(sc, hw->mac.addr);
670                 if (error == 0)
671                         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_MAC_FILTER);
672
673                 bcopy(tmpaddr, hw->mac.addr, ETH_ALEN);
674         }
675
676         error = iavf_add_mac_filter(sc, hw->mac.addr, 0);
677         if (!error || error == EEXIST)
678                 iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_MAC_FILTER);
679         iflib_set_mac(ctx, hw->mac.addr);
680
681         /* Prepare the queues for operation */
682         iavf_init_queues(vsi);
683
684         /* Set initial ITR values */
685         iavf_configure_itr(sc);
686
687         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_QUEUES);
688
689         /* Set up RSS */
690         iavf_config_rss(sc);
691
692         /* Map vectors */
693         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_MAP_VECTORS);
694
695         /* Init SW TX ring indices */
696         if (vsi->enable_head_writeback)
697                 ixl_init_tx_cidx(vsi);
698         else
699                 ixl_init_tx_rsqs(vsi);
700
701         /* Configure promiscuous mode */
702         iavf_if_promisc_set(ctx, if_getflags(ifp));
703
704         /* Enable queues */
705         iavf_send_vc_msg_sleep(sc, IAVF_FLAG_AQ_ENABLE_QUEUES);
706
707         sc->init_state = IAVF_RUNNING;
708 }
709
710 /*
711  * iavf_attach() helper function; initalizes the admin queue
712  * and attempts to establish contact with the PF by
713  * retrying the initial "API version" message several times
714  * or until the PF responds.
715  */
716 static int
717 iavf_setup_vc(struct iavf_sc *sc)
718 {
719         struct i40e_hw *hw = &sc->hw;
720         device_t dev = sc->dev;
721         int error = 0, ret_error = 0, asq_retries = 0;
722         bool send_api_ver_retried = 0;
723
724         /* Need to set these AQ paramters before initializing AQ */
725         hw->aq.num_arq_entries = IXL_AQ_LEN;
726         hw->aq.num_asq_entries = IXL_AQ_LEN;
727         hw->aq.arq_buf_size = IXL_AQ_BUF_SZ;
728         hw->aq.asq_buf_size = IXL_AQ_BUF_SZ;
729
730         for (int i = 0; i < IAVF_AQ_MAX_ERR; i++) {
731                 /* Initialize admin queue */
732                 error = i40e_init_adminq(hw);
733                 if (error) {
734                         device_printf(dev, "%s: init_adminq failed: %d\n",
735                             __func__, error);
736                         ret_error = 1;
737                         continue;
738                 }
739
740                 iavf_dbg_init(sc, "Initialized Admin Queue; starting"
741                     " send_api_ver attempt %d", i+1);
742
743 retry_send:
744                 /* Send VF's API version */
745                 error = iavf_send_api_ver(sc);
746                 if (error) {
747                         i40e_shutdown_adminq(hw);
748                         ret_error = 2;
749                         device_printf(dev, "%s: unable to send api"
750                             " version to PF on attempt %d, error %d\n",
751                             __func__, i+1, error);
752                 }
753
754                 asq_retries = 0;
755                 while (!i40e_asq_done(hw)) {
756                         if (++asq_retries > IAVF_AQ_MAX_ERR) {
757                                 i40e_shutdown_adminq(hw);
758                                 device_printf(dev, "Admin Queue timeout "
759                                     "(waiting for send_api_ver), %d more tries...\n",
760                                     IAVF_AQ_MAX_ERR - (i + 1));
761                                 ret_error = 3;
762                                 break;
763                         } 
764                         i40e_msec_pause(10);
765                 }
766                 if (asq_retries > IAVF_AQ_MAX_ERR)
767                         continue;
768
769                 iavf_dbg_init(sc, "Sent API version message to PF");
770
771                 /* Verify that the VF accepts the PF's API version */
772                 error = iavf_verify_api_ver(sc);
773                 if (error == ETIMEDOUT) {
774                         if (!send_api_ver_retried) {
775                                 /* Resend message, one more time */
776                                 send_api_ver_retried = true;
777                                 device_printf(dev,
778                                     "%s: Timeout while verifying API version on first"
779                                     " try!\n", __func__);
780                                 goto retry_send;
781                         } else {
782                                 device_printf(dev,
783                                     "%s: Timeout while verifying API version on second"
784                                     " try!\n", __func__);
785                                 ret_error = 4;
786                                 break;
787                         }
788                 }
789                 if (error) {
790                         device_printf(dev,
791                             "%s: Unable to verify API version,"
792                             " error %s\n", __func__, i40e_stat_str(hw, error));
793                         ret_error = 5;
794                 }
795                 break;
796         }
797
798         if (ret_error >= 4)
799                 i40e_shutdown_adminq(hw);
800         return (ret_error);
801 }
802
803 /*
804  * iavf_attach() helper function; asks the PF for this VF's
805  * configuration, and saves the information if it receives it.
806  */
807 static int
808 iavf_vf_config(struct iavf_sc *sc)
809 {
810         struct i40e_hw *hw = &sc->hw;
811         device_t dev = sc->dev;
812         int bufsz, error = 0, ret_error = 0;
813         int asq_retries, retried = 0;
814
815 retry_config:
816         error = iavf_send_vf_config_msg(sc);
817         if (error) {
818                 device_printf(dev,
819                     "%s: Unable to send VF config request, attempt %d,"
820                     " error %d\n", __func__, retried + 1, error);
821                 ret_error = 2;
822         }
823
824         asq_retries = 0;
825         while (!i40e_asq_done(hw)) {
826                 if (++asq_retries > IAVF_AQ_MAX_ERR) {
827                         device_printf(dev, "%s: Admin Queue timeout "
828                             "(waiting for send_vf_config_msg), attempt %d\n",
829                             __func__, retried + 1);
830                         ret_error = 3;
831                         goto fail;
832                 }
833                 i40e_msec_pause(10);
834         }
835
836         iavf_dbg_init(sc, "Sent VF config message to PF, attempt %d\n",
837             retried + 1);
838
839         if (!sc->vf_res) {
840                 bufsz = sizeof(struct virtchnl_vf_resource) +
841                     (I40E_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
842                 sc->vf_res = malloc(bufsz, M_IAVF, M_NOWAIT);
843                 if (!sc->vf_res) {
844                         device_printf(dev,
845                             "%s: Unable to allocate memory for VF configuration"
846                             " message from PF on attempt %d\n", __func__, retried + 1);
847                         ret_error = 1;
848                         goto fail;
849                 }
850         }
851
852         /* Check for VF config response */
853         error = iavf_get_vf_config(sc);
854         if (error == ETIMEDOUT) {
855                 /* The 1st time we timeout, send the configuration message again */
856                 if (!retried) {
857                         retried++;
858                         goto retry_config;
859                 }
860                 device_printf(dev,
861                     "%s: iavf_get_vf_config() timed out waiting for a response\n",
862                     __func__);
863         }
864         if (error) {
865                 device_printf(dev,
866                     "%s: Unable to get VF configuration from PF after %d tries!\n",
867                     __func__, retried + 1);
868                 ret_error = 4;
869         }
870         goto done;
871
872 fail:
873         free(sc->vf_res, M_IAVF);
874 done:
875         return (ret_error);
876 }
877
878 static int
879 iavf_if_msix_intr_assign(if_ctx_t ctx, int msix)
880 {
881         struct iavf_sc *sc = iflib_get_softc(ctx);
882         struct ixl_vsi *vsi = &sc->vsi;
883         struct ixl_rx_queue *rx_que = vsi->rx_queues;
884         struct ixl_tx_queue *tx_que = vsi->tx_queues;
885         int err, i, rid, vector = 0;
886         char buf[16];
887
888         MPASS(vsi->shared->isc_nrxqsets > 0);
889         MPASS(vsi->shared->isc_ntxqsets > 0);
890
891         /* Admin Que is vector 0*/
892         rid = vector + 1;
893         err = iflib_irq_alloc_generic(ctx, &vsi->irq, rid, IFLIB_INTR_ADMIN,
894             iavf_msix_adminq, sc, 0, "aq");
895         if (err) {
896                 iflib_irq_free(ctx, &vsi->irq);
897                 device_printf(iflib_get_dev(ctx),
898                     "Failed to register Admin Que handler");
899                 return (err);
900         }
901
902         /* Now set up the stations */
903         for (i = 0, vector = 1; i < vsi->shared->isc_nrxqsets; i++, vector++, rx_que++) {
904                 rid = vector + 1;
905
906                 snprintf(buf, sizeof(buf), "rxq%d", i);
907                 err = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
908                     IFLIB_INTR_RX, iavf_msix_que, rx_que, rx_que->rxr.me, buf);
909                 /* XXX: Does the driver work as expected if there are fewer num_rx_queues than
910                  * what's expected in the iflib context? */
911                 if (err) {
912                         device_printf(iflib_get_dev(ctx),
913                             "Failed to allocate queue RX int vector %d, err: %d\n", i, err);
914                         vsi->num_rx_queues = i + 1;
915                         goto fail;
916                 }
917                 rx_que->msix = vector;
918         }
919
920         bzero(buf, sizeof(buf));
921
922         for (i = 0; i < vsi->shared->isc_ntxqsets; i++, tx_que++) {
923                 snprintf(buf, sizeof(buf), "txq%d", i);
924                 iflib_softirq_alloc_generic(ctx,
925                     &vsi->rx_queues[i % vsi->shared->isc_nrxqsets].que_irq,
926                     IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
927
928                 /* TODO: Maybe call a strategy function for this to figure out which
929                 * interrupts to map Tx queues to. I don't know if there's an immediately
930                 * better way than this other than a user-supplied map, though. */
931                 tx_que->msix = (i % vsi->shared->isc_nrxqsets) + 1;
932         }
933
934         return (0);
935 fail:
936         iflib_irq_free(ctx, &vsi->irq);
937         rx_que = vsi->rx_queues;
938         for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
939                 iflib_irq_free(ctx, &rx_que->que_irq);
940         return (err);
941 }
942
943 /* Enable all interrupts */
944 static void
945 iavf_if_enable_intr(if_ctx_t ctx)
946 {
947         struct iavf_sc *sc = iflib_get_softc(ctx);
948         struct ixl_vsi *vsi = &sc->vsi;
949
950         iavf_enable_intr(vsi);
951 }
952
953 /* Disable all interrupts */
954 static void
955 iavf_if_disable_intr(if_ctx_t ctx)
956 {
957         struct iavf_sc *sc = iflib_get_softc(ctx);
958         struct ixl_vsi *vsi = &sc->vsi;
959
960         iavf_disable_intr(vsi);
961 }
962
963 static int
964 iavf_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
965 {
966         struct iavf_sc *sc = iflib_get_softc(ctx);
967         struct ixl_vsi *vsi = &sc->vsi;
968         struct i40e_hw *hw = vsi->hw;
969         struct ixl_rx_queue *rx_que = &vsi->rx_queues[rxqid];
970
971         iavf_enable_queue_irq(hw, rx_que->msix - 1);
972         return (0);
973 }
974
975 static int
976 iavf_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
977 {
978         struct iavf_sc *sc = iflib_get_softc(ctx);
979         struct ixl_vsi *vsi = &sc->vsi;
980         struct i40e_hw *hw = vsi->hw;
981         struct ixl_tx_queue *tx_que = &vsi->tx_queues[txqid];
982
983         iavf_enable_queue_irq(hw, tx_que->msix - 1);
984         return (0);
985 }
986
987 static int
988 iavf_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets)
989 {
990         struct iavf_sc *sc = iflib_get_softc(ctx);
991         struct ixl_vsi *vsi = &sc->vsi;
992         if_softc_ctx_t scctx = vsi->shared;
993         struct ixl_tx_queue *que;
994         int i, j, error = 0;
995
996         MPASS(scctx->isc_ntxqsets > 0);
997         MPASS(ntxqs == 1);
998         MPASS(scctx->isc_ntxqsets == ntxqsets);
999
1000         /* Allocate queue structure memory */
1001         if (!(vsi->tx_queues =
1002             (struct ixl_tx_queue *) malloc(sizeof(struct ixl_tx_queue) *ntxqsets, M_IAVF, M_NOWAIT | M_ZERO))) {
1003                 device_printf(iflib_get_dev(ctx), "Unable to allocate TX ring memory\n");
1004                 return (ENOMEM);
1005         }
1006
1007         for (i = 0, que = vsi->tx_queues; i < ntxqsets; i++, que++) {
1008                 struct tx_ring *txr = &que->txr;
1009
1010                 txr->me = i;
1011                 que->vsi = vsi;
1012
1013                 if (!vsi->enable_head_writeback) {
1014                         /* Allocate report status array */
1015                         if (!(txr->tx_rsq = malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IAVF, M_NOWAIT))) {
1016                                 device_printf(iflib_get_dev(ctx), "failed to allocate tx_rsq memory\n");
1017                                 error = ENOMEM;
1018                                 goto fail;
1019                         }
1020                         /* Init report status array */
1021                         for (j = 0; j < scctx->isc_ntxd[0]; j++)
1022                                 txr->tx_rsq[j] = QIDX_INVALID;
1023                 }
1024                 /* get the virtual and physical address of the hardware queues */
1025                 txr->tail = I40E_QTX_TAIL1(txr->me);
1026                 txr->tx_base = (struct i40e_tx_desc *)vaddrs[i * ntxqs];
1027                 txr->tx_paddr = paddrs[i * ntxqs];
1028                 txr->que = que;
1029         }
1030
1031         return (0);
1032 fail:
1033         iavf_if_queues_free(ctx);
1034         return (error);
1035 }
1036
1037 static int
1038 iavf_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nrxqs, int nrxqsets)
1039 {
1040         struct iavf_sc *sc = iflib_get_softc(ctx);
1041         struct ixl_vsi *vsi = &sc->vsi;
1042         struct ixl_rx_queue *que;
1043         int i, error = 0;
1044
1045 #ifdef INVARIANTS
1046         if_softc_ctx_t scctx = vsi->shared;
1047         MPASS(scctx->isc_nrxqsets > 0);
1048         MPASS(nrxqs == 1);
1049         MPASS(scctx->isc_nrxqsets == nrxqsets);
1050 #endif
1051
1052         /* Allocate queue structure memory */
1053         if (!(vsi->rx_queues =
1054             (struct ixl_rx_queue *) malloc(sizeof(struct ixl_rx_queue) *
1055             nrxqsets, M_IAVF, M_NOWAIT | M_ZERO))) {
1056                 device_printf(iflib_get_dev(ctx), "Unable to allocate RX ring memory\n");
1057                 error = ENOMEM;
1058                 goto fail;
1059         }
1060
1061         for (i = 0, que = vsi->rx_queues; i < nrxqsets; i++, que++) {
1062                 struct rx_ring *rxr = &que->rxr;
1063
1064                 rxr->me = i;
1065                 que->vsi = vsi;
1066
1067                 /* get the virtual and physical address of the hardware queues */
1068                 rxr->tail = I40E_QRX_TAIL1(rxr->me);
1069                 rxr->rx_base = (union i40e_rx_desc *)vaddrs[i * nrxqs];
1070                 rxr->rx_paddr = paddrs[i * nrxqs];
1071                 rxr->que = que;
1072         }
1073
1074         return (0);
1075 fail:
1076         iavf_if_queues_free(ctx);
1077         return (error);
1078 }
1079
1080 static void
1081 iavf_if_queues_free(if_ctx_t ctx)
1082 {
1083         struct iavf_sc *sc = iflib_get_softc(ctx);
1084         struct ixl_vsi *vsi = &sc->vsi;
1085
1086         if (!vsi->enable_head_writeback) {
1087                 struct ixl_tx_queue *que;
1088                 int i = 0;
1089
1090                 for (i = 0, que = vsi->tx_queues; i < vsi->shared->isc_ntxqsets; i++, que++) {
1091                         struct tx_ring *txr = &que->txr;
1092                         if (txr->tx_rsq != NULL) {
1093                                 free(txr->tx_rsq, M_IAVF);
1094                                 txr->tx_rsq = NULL;
1095                         }
1096                 }
1097         }
1098
1099         if (vsi->tx_queues != NULL) {
1100                 free(vsi->tx_queues, M_IAVF);
1101                 vsi->tx_queues = NULL;
1102         }
1103         if (vsi->rx_queues != NULL) {
1104                 free(vsi->rx_queues, M_IAVF);
1105                 vsi->rx_queues = NULL;
1106         }
1107 }
1108
1109 static int
1110 iavf_check_aq_errors(struct iavf_sc *sc)
1111 {
1112         struct i40e_hw *hw = &sc->hw;
1113         device_t dev = sc->dev;
1114         u32 reg, oldreg;
1115         u8 aq_error = false;
1116
1117         /* check for Admin queue errors */
1118         oldreg = reg = rd32(hw, hw->aq.arq.len);
1119         if (reg & I40E_VF_ARQLEN1_ARQVFE_MASK) {
1120                 device_printf(dev, "ARQ VF Error detected\n");
1121                 reg &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
1122                 aq_error = true;
1123         }
1124         if (reg & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
1125                 device_printf(dev, "ARQ Overflow Error detected\n");
1126                 reg &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
1127                 aq_error = true;
1128         }
1129         if (reg & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
1130                 device_printf(dev, "ARQ Critical Error detected\n");
1131                 reg &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
1132                 aq_error = true;
1133         }
1134         if (oldreg != reg)
1135                 wr32(hw, hw->aq.arq.len, reg);
1136
1137         oldreg = reg = rd32(hw, hw->aq.asq.len);
1138         if (reg & I40E_VF_ATQLEN1_ATQVFE_MASK) {
1139                 device_printf(dev, "ASQ VF Error detected\n");
1140                 reg &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
1141                 aq_error = true;
1142         }
1143         if (reg & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
1144                 device_printf(dev, "ASQ Overflow Error detected\n");
1145                 reg &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
1146                 aq_error = true;
1147         }
1148         if (reg & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
1149                 device_printf(dev, "ASQ Critical Error detected\n");
1150                 reg &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
1151                 aq_error = true;
1152         }
1153         if (oldreg != reg)
1154                 wr32(hw, hw->aq.asq.len, reg);
1155
1156         if (aq_error) {
1157                 device_printf(dev, "WARNING: Stopping VF!\n");
1158                 /*
1159                  * A VF reset might not be enough to fix a problem here;
1160                  * a PF reset could be required.
1161                  */
1162                 sc->init_state = IAVF_RESET_REQUIRED;
1163                 iavf_stop(sc);
1164                 iavf_request_reset(sc);
1165         }
1166
1167         return (aq_error ? EIO : 0);
1168 }
1169
1170 static enum i40e_status_code
1171 iavf_process_adminq(struct iavf_sc *sc, u16 *pending)
1172 {
1173         enum i40e_status_code status = I40E_SUCCESS;
1174         struct i40e_arq_event_info event;
1175         struct i40e_hw *hw = &sc->hw;
1176         struct virtchnl_msg *v_msg;
1177         int error = 0, loop = 0;
1178         u32 reg;
1179
1180         error = iavf_check_aq_errors(sc);
1181         if (error)
1182                 return (I40E_ERR_ADMIN_QUEUE_CRITICAL_ERROR);
1183
1184         event.buf_len = IXL_AQ_BUF_SZ;
1185         event.msg_buf = sc->aq_buffer;
1186         bzero(event.msg_buf, IXL_AQ_BUF_SZ);
1187         v_msg = (struct virtchnl_msg *)&event.desc;
1188
1189         /* clean and process any events */
1190         do {
1191                 status = i40e_clean_arq_element(hw, &event, pending);
1192                 /*
1193                  * Also covers normal case when i40e_clean_arq_element()
1194                  * returns "I40E_ERR_ADMIN_QUEUE_NO_WORK"
1195                  */
1196                 if (status)
1197                         break;
1198                 iavf_vc_completion(sc, v_msg->v_opcode,
1199                     v_msg->v_retval, event.msg_buf, event.msg_len);
1200                 bzero(event.msg_buf, IXL_AQ_BUF_SZ);
1201         } while (*pending && (loop++ < IXL_ADM_LIMIT));
1202
1203         /* Re-enable admin queue interrupt cause */
1204         reg = rd32(hw, I40E_VFINT_ICR0_ENA1);
1205         reg |= I40E_VFINT_ICR0_ENA1_ADMINQ_MASK;
1206         wr32(hw, I40E_VFINT_ICR0_ENA1, reg);
1207
1208         return (status);
1209 }
1210
1211 static void
1212 iavf_if_update_admin_status(if_ctx_t ctx)
1213 {
1214         struct iavf_sc *sc = iflib_get_softc(ctx);
1215         struct i40e_hw *hw = &sc->hw;
1216         u16 pending;
1217
1218         iavf_process_adminq(sc, &pending);
1219         iavf_update_link_status(sc);
1220         
1221         /*
1222          * If there are still messages to process, reschedule.
1223          * Otherwise, re-enable the Admin Queue interrupt.
1224          */
1225         if (pending > 0)
1226                 iflib_admin_intr_deferred(ctx);
1227         else
1228                 iavf_enable_adminq_irq(hw);
1229 }
1230
1231 static int
1232 iavf_mc_filter_apply(void *arg, struct ifmultiaddr *ifma, int count __unused)
1233 {
1234         struct iavf_sc *sc = arg;
1235         int error = 0;
1236
1237         if (ifma->ifma_addr->sa_family != AF_LINK)
1238                 return (0);
1239         error = iavf_add_mac_filter(sc,
1240             (u8*)LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1241             IXL_FILTER_MC);
1242
1243         return (!error);
1244 }
1245
1246 static void
1247 iavf_if_multi_set(if_ctx_t ctx)
1248 {
1249         struct iavf_sc *sc = iflib_get_softc(ctx);
1250         int mcnt = 0;
1251
1252         IOCTL_DEBUGOUT("iavf_if_multi_set: begin");
1253
1254         mcnt = if_multiaddr_count(iflib_get_ifp(ctx), MAX_MULTICAST_ADDR);
1255         if (__predict_false(mcnt == MAX_MULTICAST_ADDR)) {
1256                 /* Delete MC filters and enable mulitcast promisc instead */
1257                 iavf_init_multi(sc);
1258                 sc->promisc_flags |= FLAG_VF_MULTICAST_PROMISC;
1259                 iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_PROMISC);
1260                 return;
1261         }
1262
1263         /* If there aren't too many filters, delete existing MC filters */
1264         iavf_init_multi(sc);
1265
1266         /* And (re-)install filters for all mcast addresses */
1267         mcnt = if_multi_apply(iflib_get_ifp(ctx), iavf_mc_filter_apply, sc);
1268
1269         if (mcnt > 0)
1270                 iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_MAC_FILTER);
1271 }
1272
1273 static int
1274 iavf_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1275 {
1276         struct iavf_sc *sc = iflib_get_softc(ctx);
1277         struct ixl_vsi *vsi = &sc->vsi;
1278
1279         IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
1280         if (mtu > IXL_MAX_FRAME - ETHER_HDR_LEN - ETHER_CRC_LEN -
1281                 ETHER_VLAN_ENCAP_LEN)
1282                 return (EINVAL);
1283
1284         vsi->shared->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1285                 ETHER_VLAN_ENCAP_LEN;
1286
1287         return (0);
1288 }
1289
1290 static void
1291 iavf_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1292 {
1293 #ifdef IXL_DEBUG
1294         struct ifnet *ifp = iflib_get_ifp(ctx);
1295 #endif
1296         struct iavf_sc *sc = iflib_get_softc(ctx);
1297
1298         INIT_DBG_IF(ifp, "begin");
1299
1300         iavf_update_link_status(sc);
1301
1302         ifmr->ifm_status = IFM_AVALID;
1303         ifmr->ifm_active = IFM_ETHER;
1304
1305         if (!sc->link_up)
1306                 return;
1307
1308         ifmr->ifm_status |= IFM_ACTIVE;
1309         /* Hardware is always full-duplex */
1310         ifmr->ifm_active |= IFM_FDX;
1311
1312         /* Based on the link speed reported by the PF over the AdminQ, choose a
1313          * PHY type to report. This isn't 100% correct since we don't really
1314          * know the underlying PHY type of the PF, but at least we can report
1315          * a valid link speed...
1316          */
1317         switch (sc->link_speed) {
1318         case VIRTCHNL_LINK_SPEED_100MB:
1319                 ifmr->ifm_active |= IFM_100_TX;
1320                 break;
1321         case VIRTCHNL_LINK_SPEED_1GB:
1322                 ifmr->ifm_active |= IFM_1000_T;
1323                 break;
1324         case VIRTCHNL_LINK_SPEED_10GB:
1325                 ifmr->ifm_active |= IFM_10G_SR;
1326                 break;
1327         case VIRTCHNL_LINK_SPEED_20GB:
1328         case VIRTCHNL_LINK_SPEED_25GB:
1329                 ifmr->ifm_active |= IFM_25G_SR;
1330                 break;
1331         case VIRTCHNL_LINK_SPEED_40GB:
1332                 ifmr->ifm_active |= IFM_40G_SR4;
1333                 break;
1334         default:
1335                 ifmr->ifm_active |= IFM_UNKNOWN;
1336                 break;
1337         }
1338
1339         INIT_DBG_IF(ifp, "end");
1340 }
1341
1342 static int
1343 iavf_if_media_change(if_ctx_t ctx)
1344 {
1345         struct ifmedia *ifm = iflib_get_media(ctx);
1346
1347         INIT_DEBUGOUT("ixl_media_change: begin");
1348
1349         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1350                 return (EINVAL);
1351
1352         if_printf(iflib_get_ifp(ctx), "Media change is not supported.\n");
1353         return (ENODEV);
1354 }
1355
1356 static int
1357 iavf_if_promisc_set(if_ctx_t ctx, int flags)
1358 {
1359         struct iavf_sc *sc = iflib_get_softc(ctx);
1360         struct ifnet    *ifp = iflib_get_ifp(ctx);
1361
1362         sc->promisc_flags = 0;
1363
1364         if (flags & IFF_ALLMULTI ||
1365                 if_multiaddr_count(ifp, MAX_MULTICAST_ADDR) == MAX_MULTICAST_ADDR)
1366                 sc->promisc_flags |= FLAG_VF_MULTICAST_PROMISC;
1367         if (flags & IFF_PROMISC)
1368                 sc->promisc_flags |= FLAG_VF_UNICAST_PROMISC;
1369
1370         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_PROMISC);
1371
1372         return (0);
1373 }
1374
1375 static void
1376 iavf_if_timer(if_ctx_t ctx, uint16_t qid)
1377 {
1378         struct iavf_sc *sc = iflib_get_softc(ctx);
1379         struct i40e_hw *hw = &sc->hw;
1380         u32 val;
1381
1382         if (qid != 0)
1383                 return;
1384
1385         /* Check for when PF triggers a VF reset */
1386         val = rd32(hw, I40E_VFGEN_RSTAT) &
1387             I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1388         if (val != VIRTCHNL_VFR_VFACTIVE
1389             && val != VIRTCHNL_VFR_COMPLETED) {
1390                 iavf_dbg_info(sc, "reset in progress! (%d)\n", val);
1391                 return;
1392         }
1393
1394         /* Fire off the adminq task */
1395         iflib_admin_intr_deferred(ctx);
1396
1397         /* Update stats */
1398         iavf_request_stats(sc);
1399 }
1400
1401 static void
1402 iavf_if_vlan_register(if_ctx_t ctx, u16 vtag)
1403 {
1404         struct iavf_sc *sc = iflib_get_softc(ctx);
1405         struct ixl_vsi *vsi = &sc->vsi;
1406         struct iavf_vlan_filter *v;
1407
1408         if ((vtag == 0) || (vtag > 4095))       /* Invalid */
1409                 return;
1410
1411         ++vsi->num_vlans;
1412         v = malloc(sizeof(struct iavf_vlan_filter), M_IAVF, M_WAITOK | M_ZERO);
1413         SLIST_INSERT_HEAD(sc->vlan_filters, v, next);
1414         v->vlan = vtag;
1415         v->flags = IXL_FILTER_ADD;
1416
1417         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_VLAN_FILTER);
1418 }
1419
1420 static void
1421 iavf_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
1422 {
1423         struct iavf_sc *sc = iflib_get_softc(ctx);
1424         struct ixl_vsi *vsi = &sc->vsi;
1425         struct iavf_vlan_filter *v;
1426         int                     i = 0;
1427
1428         if ((vtag == 0) || (vtag > 4095))       /* Invalid */
1429                 return;
1430
1431         SLIST_FOREACH(v, sc->vlan_filters, next) {
1432                 if (v->vlan == vtag) {
1433                         v->flags = IXL_FILTER_DEL;
1434                         ++i;
1435                         --vsi->num_vlans;
1436                 }
1437         }
1438         if (i)
1439                 iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_VLAN_FILTER);
1440 }
1441
1442 static uint64_t
1443 iavf_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1444 {
1445         struct iavf_sc *sc = iflib_get_softc(ctx);
1446         struct ixl_vsi *vsi = &sc->vsi;
1447         if_t ifp = iflib_get_ifp(ctx);
1448
1449         switch (cnt) {
1450         case IFCOUNTER_IPACKETS:
1451                 return (vsi->ipackets);
1452         case IFCOUNTER_IERRORS:
1453                 return (vsi->ierrors);
1454         case IFCOUNTER_OPACKETS:
1455                 return (vsi->opackets);
1456         case IFCOUNTER_OERRORS:
1457                 return (vsi->oerrors);
1458         case IFCOUNTER_COLLISIONS:
1459                 /* Collisions are by standard impossible in 40G/10G Ethernet */
1460                 return (0);
1461         case IFCOUNTER_IBYTES:
1462                 return (vsi->ibytes);
1463         case IFCOUNTER_OBYTES:
1464                 return (vsi->obytes);
1465         case IFCOUNTER_IMCASTS:
1466                 return (vsi->imcasts);
1467         case IFCOUNTER_OMCASTS:
1468                 return (vsi->omcasts);
1469         case IFCOUNTER_IQDROPS:
1470                 return (vsi->iqdrops);
1471         case IFCOUNTER_OQDROPS:
1472                 return (vsi->oqdrops);
1473         case IFCOUNTER_NOPROTO:
1474                 return (vsi->noproto);
1475         default:
1476                 return (if_get_counter_default(ifp, cnt));
1477         }
1478 }
1479
1480  
1481 static void
1482 iavf_free_pci_resources(struct iavf_sc *sc)
1483 {
1484         struct ixl_vsi          *vsi = &sc->vsi;
1485         struct ixl_rx_queue     *rx_que = vsi->rx_queues;
1486         device_t                dev = sc->dev;
1487
1488         /* We may get here before stations are setup */
1489         if (rx_que == NULL)
1490                 goto early;
1491
1492         /* Release all interrupts */
1493         iflib_irq_free(vsi->ctx, &vsi->irq);
1494
1495         for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1496                 iflib_irq_free(vsi->ctx, &rx_que->que_irq);
1497
1498 early:
1499         if (sc->pci_mem != NULL)
1500                 bus_release_resource(dev, SYS_RES_MEMORY,
1501                     PCIR_BAR(0), sc->pci_mem);
1502 }
1503
1504
1505 /*
1506 ** Requests a VF reset from the PF.
1507 **
1508 ** Requires the VF's Admin Queue to be initialized.
1509 */
1510 static int
1511 iavf_reset(struct iavf_sc *sc)
1512 {
1513         struct i40e_hw  *hw = &sc->hw;
1514         device_t        dev = sc->dev;
1515         int             error = 0;
1516
1517         /* Ask the PF to reset us if we are initiating */
1518         if (sc->init_state != IAVF_RESET_PENDING)
1519                 iavf_request_reset(sc);
1520
1521         i40e_msec_pause(100);
1522         error = iavf_reset_complete(hw);
1523         if (error) {
1524                 device_printf(dev, "%s: VF reset failed\n",
1525                     __func__);
1526                 return (error);
1527         }
1528         pci_enable_busmaster(dev);
1529
1530         error = i40e_shutdown_adminq(hw);
1531         if (error) {
1532                 device_printf(dev, "%s: shutdown_adminq failed: %d\n",
1533                     __func__, error);
1534                 return (error);
1535         }
1536
1537         error = i40e_init_adminq(hw);
1538         if (error) {
1539                 device_printf(dev, "%s: init_adminq failed: %d\n",
1540                     __func__, error);
1541                 return (error);
1542         }
1543
1544         iavf_enable_adminq_irq(hw);
1545         return (0);
1546 }
1547
1548 static int
1549 iavf_reset_complete(struct i40e_hw *hw)
1550 {
1551         u32 reg;
1552
1553         /* Wait up to ~10 seconds */
1554         for (int i = 0; i < 100; i++) {
1555                 reg = rd32(hw, I40E_VFGEN_RSTAT) &
1556                     I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1557
1558                 if ((reg == VIRTCHNL_VFR_VFACTIVE) ||
1559                     (reg == VIRTCHNL_VFR_COMPLETED))
1560                         return (0);
1561                 i40e_msec_pause(100);
1562         }
1563
1564         return (EBUSY);
1565 }
1566
1567 static void
1568 iavf_setup_interface(device_t dev, struct iavf_sc *sc)
1569 {
1570         struct ixl_vsi *vsi = &sc->vsi;
1571         if_ctx_t ctx = vsi->ctx;
1572         struct ifnet *ifp = iflib_get_ifp(ctx);
1573
1574         INIT_DBG_DEV(dev, "begin");
1575
1576         vsi->shared->isc_max_frame_size =
1577             ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN
1578             + ETHER_VLAN_ENCAP_LEN;
1579 #if __FreeBSD_version >= 1100000
1580         if_setbaudrate(ifp, IF_Gbps(40));
1581 #else
1582         if_initbaudrate(ifp, IF_Gbps(40));
1583 #endif
1584
1585         ifmedia_add(vsi->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1586         ifmedia_set(vsi->media, IFM_ETHER | IFM_AUTO);
1587 }
1588
1589 /*
1590 ** Get a new filter and add it to the mac filter list.
1591 */
1592 static struct iavf_mac_filter *
1593 iavf_get_mac_filter(struct iavf_sc *sc)
1594 {
1595         struct iavf_mac_filter  *f;
1596
1597         f = malloc(sizeof(struct iavf_mac_filter),
1598             M_IAVF, M_NOWAIT | M_ZERO);
1599         if (f)
1600                 SLIST_INSERT_HEAD(sc->mac_filters, f, next);
1601
1602         return (f);
1603 }
1604
1605 /*
1606 ** Find the filter with matching MAC address
1607 */
1608 static struct iavf_mac_filter *
1609 iavf_find_mac_filter(struct iavf_sc *sc, u8 *macaddr)
1610 {
1611         struct iavf_mac_filter  *f;
1612         bool match = FALSE;
1613
1614         SLIST_FOREACH(f, sc->mac_filters, next) {
1615                 if (cmp_etheraddr(f->macaddr, macaddr)) {
1616                         match = TRUE;
1617                         break;
1618                 }
1619         }       
1620
1621         if (!match)
1622                 f = NULL;
1623         return (f);
1624 }
1625
1626 /*
1627 ** Admin Queue interrupt handler
1628 */
1629 static int
1630 iavf_msix_adminq(void *arg)
1631 {
1632         struct iavf_sc  *sc = arg;
1633         struct i40e_hw  *hw = &sc->hw;
1634         u32             reg, mask;
1635         bool            do_task = FALSE;
1636
1637         ++sc->admin_irq;
1638
1639         reg = rd32(hw, I40E_VFINT_ICR01);
1640         /*
1641          * For masking off interrupt causes that need to be handled before
1642          * they can be re-enabled
1643          */
1644         mask = rd32(hw, I40E_VFINT_ICR0_ENA1);
1645
1646         /* Check on the cause */
1647         if (reg & I40E_VFINT_ICR0_ADMINQ_MASK) {
1648                 mask &= ~I40E_VFINT_ICR0_ENA_ADMINQ_MASK;
1649                 do_task = TRUE;
1650         }
1651
1652         wr32(hw, I40E_VFINT_ICR0_ENA1, mask);
1653         iavf_enable_adminq_irq(hw);
1654
1655         if (do_task)
1656                 return (FILTER_SCHEDULE_THREAD);
1657         else
1658                 return (FILTER_HANDLED);
1659 }
1660
1661 void
1662 iavf_enable_intr(struct ixl_vsi *vsi)
1663 {
1664         struct i40e_hw *hw = vsi->hw;
1665         struct ixl_rx_queue *que = vsi->rx_queues;
1666
1667         iavf_enable_adminq_irq(hw);
1668         for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1669                 iavf_enable_queue_irq(hw, que->rxr.me);
1670 }
1671
1672 void
1673 iavf_disable_intr(struct ixl_vsi *vsi)
1674 {
1675         struct i40e_hw *hw = vsi->hw;
1676         struct ixl_rx_queue *que = vsi->rx_queues;
1677
1678         for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1679                 iavf_disable_queue_irq(hw, que->rxr.me);
1680 }
1681
1682 static void
1683 iavf_disable_adminq_irq(struct i40e_hw *hw)
1684 {
1685         wr32(hw, I40E_VFINT_DYN_CTL01, 0);
1686         wr32(hw, I40E_VFINT_ICR0_ENA1, 0);
1687         /* flush */
1688         rd32(hw, I40E_VFGEN_RSTAT);
1689 }
1690
1691 static void
1692 iavf_enable_adminq_irq(struct i40e_hw *hw)
1693 {
1694         wr32(hw, I40E_VFINT_DYN_CTL01,
1695             I40E_VFINT_DYN_CTL01_INTENA_MASK |
1696             I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
1697         wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
1698         /* flush */
1699         rd32(hw, I40E_VFGEN_RSTAT);
1700 }
1701
1702 static void
1703 iavf_enable_queue_irq(struct i40e_hw *hw, int id)
1704 {
1705         u32             reg;
1706
1707         reg = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
1708             I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK |
1709             I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK;
1710         wr32(hw, I40E_VFINT_DYN_CTLN1(id), reg);
1711 }
1712
1713 static void
1714 iavf_disable_queue_irq(struct i40e_hw *hw, int id)
1715 {
1716         wr32(hw, I40E_VFINT_DYN_CTLN1(id),
1717             I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1718         rd32(hw, I40E_VFGEN_RSTAT);
1719 }
1720
1721 static void
1722 iavf_configure_tx_itr(struct iavf_sc *sc)
1723 {
1724         struct i40e_hw          *hw = &sc->hw;
1725         struct ixl_vsi          *vsi = &sc->vsi;
1726         struct ixl_tx_queue     *que = vsi->tx_queues;
1727
1728         vsi->tx_itr_setting = sc->tx_itr;
1729
1730         for (int i = 0; i < vsi->num_tx_queues; i++, que++) {
1731                 struct tx_ring  *txr = &que->txr;
1732
1733                 wr32(hw, I40E_VFINT_ITRN1(IXL_TX_ITR, i),
1734                     vsi->tx_itr_setting);
1735                 txr->itr = vsi->tx_itr_setting;
1736                 txr->latency = IXL_AVE_LATENCY;
1737         }
1738 }
1739
1740 static void
1741 iavf_configure_rx_itr(struct iavf_sc *sc)
1742 {
1743         struct i40e_hw          *hw = &sc->hw;
1744         struct ixl_vsi          *vsi = &sc->vsi;
1745         struct ixl_rx_queue     *que = vsi->rx_queues;
1746
1747         vsi->rx_itr_setting = sc->rx_itr;
1748
1749         for (int i = 0; i < vsi->num_rx_queues; i++, que++) {
1750                 struct rx_ring  *rxr = &que->rxr;
1751
1752                 wr32(hw, I40E_VFINT_ITRN1(IXL_RX_ITR, i),
1753                     vsi->rx_itr_setting);
1754                 rxr->itr = vsi->rx_itr_setting;
1755                 rxr->latency = IXL_AVE_LATENCY;
1756         }
1757 }
1758
1759 /*
1760  * Get initial ITR values from tunable values.
1761  */
1762 static void
1763 iavf_configure_itr(struct iavf_sc *sc)
1764 {
1765         iavf_configure_tx_itr(sc);
1766         iavf_configure_rx_itr(sc);
1767 }
1768
1769 /*
1770 ** Provide a update to the queue RX
1771 ** interrupt moderation value.
1772 */
1773 static void
1774 iavf_set_queue_rx_itr(struct ixl_rx_queue *que)
1775 {
1776         struct ixl_vsi  *vsi = que->vsi;
1777         struct i40e_hw  *hw = vsi->hw;
1778         struct rx_ring  *rxr = &que->rxr;
1779
1780         /* Idle, do nothing */
1781         if (rxr->bytes == 0)
1782                 return;
1783
1784         /* Update the hardware if needed */
1785         if (rxr->itr != vsi->rx_itr_setting) {
1786                 rxr->itr = vsi->rx_itr_setting;
1787                 wr32(hw, I40E_VFINT_ITRN1(IXL_RX_ITR,
1788                     que->rxr.me), rxr->itr);
1789         }
1790 }
1791
1792 static int
1793 iavf_msix_que(void *arg)
1794 {
1795         struct ixl_rx_queue *rx_que = arg;
1796
1797         ++rx_que->irqs;
1798
1799         iavf_set_queue_rx_itr(rx_que);
1800         // iavf_set_queue_tx_itr(que);
1801
1802         return (FILTER_SCHEDULE_THREAD);
1803 }
1804
1805 /*********************************************************************
1806  *  Multicast Initialization
1807  *
1808  *  This routine is called by init to reset a fresh state.
1809  *
1810  **********************************************************************/
1811 static void
1812 iavf_init_multi(struct iavf_sc *sc)
1813 {
1814         struct iavf_mac_filter *f;
1815         int mcnt = 0;
1816
1817         /* First clear any multicast filters */
1818         SLIST_FOREACH(f, sc->mac_filters, next) {
1819                 if ((f->flags & IXL_FILTER_USED)
1820                     && (f->flags & IXL_FILTER_MC)) {
1821                         f->flags |= IXL_FILTER_DEL;
1822                         mcnt++;
1823                 }
1824         }
1825         if (mcnt > 0)
1826                 iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_MAC_FILTER);
1827 }
1828
1829 /*
1830 ** Note: this routine updates the OS on the link state
1831 **      the real check of the hardware only happens with
1832 **      a link interrupt.
1833 */
1834 void
1835 iavf_update_link_status(struct iavf_sc *sc)
1836 {
1837         struct ixl_vsi *vsi = &sc->vsi;
1838         u64 baudrate;
1839
1840         if (sc->link_up){ 
1841                 if (vsi->link_active == FALSE) {
1842                         vsi->link_active = TRUE;
1843                         baudrate = ixl_max_vc_speed_to_value(sc->link_speed);
1844                         iavf_dbg_info(sc, "baudrate: %lu\n", baudrate);
1845                         iflib_link_state_change(vsi->ctx, LINK_STATE_UP, baudrate);
1846                 }
1847         } else { /* Link down */
1848                 if (vsi->link_active == TRUE) {
1849                         vsi->link_active = FALSE;
1850                         iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
1851                 }
1852         }
1853 }
1854
1855 /*********************************************************************
1856  *
1857  *  This routine disables all traffic on the adapter by issuing a
1858  *  global reset on the MAC and deallocates TX/RX buffers.
1859  *
1860  **********************************************************************/
1861
1862 static void
1863 iavf_stop(struct iavf_sc *sc)
1864 {
1865         struct ifnet *ifp;
1866
1867         ifp = sc->vsi.ifp;
1868
1869         iavf_disable_intr(&sc->vsi);
1870
1871         if (atomic_load_acq_32(&sc->queues_enabled))
1872                 iavf_send_vc_msg_sleep(sc, IAVF_FLAG_AQ_DISABLE_QUEUES);
1873 }
1874
1875 static void
1876 iavf_if_stop(if_ctx_t ctx)
1877 {
1878         struct iavf_sc *sc = iflib_get_softc(ctx);
1879
1880         iavf_stop(sc);
1881 }
1882
1883 static void
1884 iavf_config_rss_reg(struct iavf_sc *sc)
1885 {
1886         struct i40e_hw  *hw = &sc->hw;
1887         struct ixl_vsi  *vsi = &sc->vsi;
1888         u32             lut = 0;
1889         u64             set_hena = 0, hena;
1890         int             i, j, que_id;
1891         u32             rss_seed[IXL_RSS_KEY_SIZE_REG];
1892 #ifdef RSS
1893         u32             rss_hash_config;
1894 #endif
1895         
1896         /* Don't set up RSS if using a single queue */
1897         if (vsi->num_rx_queues == 1) {
1898                 wr32(hw, I40E_VFQF_HENA(0), 0);
1899                 wr32(hw, I40E_VFQF_HENA(1), 0);
1900                 ixl_flush(hw);
1901                 return;
1902         }
1903
1904 #ifdef RSS
1905         /* Fetch the configured RSS key */
1906         rss_getkey((uint8_t *) &rss_seed);
1907 #else
1908         ixl_get_default_rss_key(rss_seed);
1909 #endif
1910
1911         /* Fill out hash function seed */
1912         for (i = 0; i < IXL_RSS_KEY_SIZE_REG; i++)
1913                 wr32(hw, I40E_VFQF_HKEY(i), rss_seed[i]);
1914
1915         /* Enable PCTYPES for RSS: */
1916 #ifdef RSS
1917         rss_hash_config = rss_gethashconfig();
1918         if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
1919                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
1920         if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
1921                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1922         if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
1923                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP);
1924         if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
1925                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
1926         if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
1927                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
1928         if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
1929                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1930         if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
1931                 set_hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP);
1932 #else
1933         set_hena = IXL_DEFAULT_RSS_HENA_XL710;
1934 #endif
1935         hena = (u64)rd32(hw, I40E_VFQF_HENA(0)) |
1936             ((u64)rd32(hw, I40E_VFQF_HENA(1)) << 32);
1937         hena |= set_hena;
1938         wr32(hw, I40E_VFQF_HENA(0), (u32)hena);
1939         wr32(hw, I40E_VFQF_HENA(1), (u32)(hena >> 32));
1940
1941         /* Populate the LUT with max no. of queues in round robin fashion */
1942         for (i = 0, j = 0; i < IXL_RSS_VSI_LUT_SIZE; i++, j++) {
1943                 if (j == vsi->num_rx_queues)
1944                         j = 0;
1945 #ifdef RSS
1946                 /*
1947                  * Fetch the RSS bucket id for the given indirection entry.
1948                  * Cap it at the number of configured buckets (which is
1949                  * num_rx_queues.)
1950                  */
1951                 que_id = rss_get_indirection_to_bucket(i);
1952                 que_id = que_id % vsi->num_rx_queues;
1953 #else
1954                 que_id = j;
1955 #endif
1956                 /* lut = 4-byte sliding window of 4 lut entries */
1957                 lut = (lut << 8) | (que_id & IXL_RSS_VF_LUT_ENTRY_MASK);
1958                 /* On i = 3, we have 4 entries in lut; write to the register */
1959                 if ((i & 3) == 3) {
1960                         wr32(hw, I40E_VFQF_HLUT(i >> 2), lut);
1961                         DDPRINTF(sc->dev, "HLUT(%2d): %#010x", i, lut);
1962                 }
1963         }
1964         ixl_flush(hw);
1965 }
1966
1967 static void
1968 iavf_config_rss_pf(struct iavf_sc *sc)
1969 {
1970         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIG_RSS_KEY);
1971
1972         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_SET_RSS_HENA);
1973
1974         iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIG_RSS_LUT);
1975 }
1976
1977 /*
1978 ** iavf_config_rss - setup RSS 
1979 **
1980 ** RSS keys and table are cleared on VF reset.
1981 */
1982 static void
1983 iavf_config_rss(struct iavf_sc *sc)
1984 {
1985         if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_REG) {
1986                 iavf_dbg_info(sc, "Setting up RSS using VF registers...");
1987                 iavf_config_rss_reg(sc);
1988         } else if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1989                 iavf_dbg_info(sc, "Setting up RSS using messages to PF...");
1990                 iavf_config_rss_pf(sc);
1991         } else
1992                 device_printf(sc->dev, "VF does not support RSS capability sent by PF.\n");
1993 }
1994
1995 /*
1996 ** This routine adds new MAC filters to the sc's list;
1997 ** these are later added in hardware by sending a virtual
1998 ** channel message.
1999 */
2000 static int
2001 iavf_add_mac_filter(struct iavf_sc *sc, u8 *macaddr, u16 flags)
2002 {
2003         struct iavf_mac_filter  *f;
2004
2005         /* Does one already exist? */
2006         f = iavf_find_mac_filter(sc, macaddr);
2007         if (f != NULL) {
2008                 iavf_dbg_filter(sc, "exists: " MAC_FORMAT "\n",
2009                     MAC_FORMAT_ARGS(macaddr));
2010                 return (EEXIST);
2011         }
2012
2013         /* If not, get a new empty filter */
2014         f = iavf_get_mac_filter(sc);
2015         if (f == NULL) {
2016                 device_printf(sc->dev, "%s: no filters available!!\n",
2017                     __func__);
2018                 return (ENOMEM);
2019         }
2020
2021         iavf_dbg_filter(sc, "marked: " MAC_FORMAT "\n",
2022             MAC_FORMAT_ARGS(macaddr));
2023
2024         bcopy(macaddr, f->macaddr, ETHER_ADDR_LEN);
2025         f->flags |= (IXL_FILTER_ADD | IXL_FILTER_USED);
2026         f->flags |= flags;
2027         return (0);
2028 }
2029
2030 /*
2031 ** Marks a MAC filter for deletion.
2032 */
2033 static int
2034 iavf_del_mac_filter(struct iavf_sc *sc, u8 *macaddr)
2035 {
2036         struct iavf_mac_filter  *f;
2037
2038         f = iavf_find_mac_filter(sc, macaddr);
2039         if (f == NULL)
2040                 return (ENOENT);
2041
2042         f->flags |= IXL_FILTER_DEL;
2043         return (0);
2044 }
2045
2046 /*
2047  * Re-uses the name from the PF driver.
2048  */
2049 static void
2050 iavf_add_device_sysctls(struct iavf_sc *sc)
2051 {
2052         struct ixl_vsi *vsi = &sc->vsi;
2053         device_t dev = sc->dev;
2054
2055         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
2056         struct sysctl_oid_list *ctx_list =
2057             SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2058         struct sysctl_oid *debug_node;
2059         struct sysctl_oid_list *debug_list;
2060
2061         SYSCTL_ADD_PROC(ctx, ctx_list,
2062             OID_AUTO, "current_speed", CTLTYPE_STRING | CTLFLAG_RD,
2063             sc, 0, iavf_sysctl_current_speed, "A", "Current Port Speed");
2064
2065         SYSCTL_ADD_PROC(ctx, ctx_list,
2066             OID_AUTO, "tx_itr", CTLTYPE_INT | CTLFLAG_RW,
2067             sc, 0, iavf_sysctl_tx_itr, "I",
2068             "Immediately set TX ITR value for all queues");
2069
2070         SYSCTL_ADD_PROC(ctx, ctx_list,
2071             OID_AUTO, "rx_itr", CTLTYPE_INT | CTLFLAG_RW,
2072             sc, 0, iavf_sysctl_rx_itr, "I",
2073             "Immediately set RX ITR value for all queues");
2074
2075         /* Add sysctls meant to print debug information, but don't list them
2076          * in "sysctl -a" output. */
2077         debug_node = SYSCTL_ADD_NODE(ctx, ctx_list,
2078             OID_AUTO, "debug", CTLFLAG_RD | CTLFLAG_SKIP, NULL, "Debug Sysctls");
2079         debug_list = SYSCTL_CHILDREN(debug_node);
2080
2081         SYSCTL_ADD_UINT(ctx, debug_list,
2082             OID_AUTO, "shared_debug_mask", CTLFLAG_RW,
2083             &sc->hw.debug_mask, 0, "Shared code debug message level");
2084
2085         SYSCTL_ADD_UINT(ctx, debug_list,
2086             OID_AUTO, "core_debug_mask", CTLFLAG_RW,
2087             &sc->dbg_mask, 0, "Non-shared code debug message level");
2088
2089         SYSCTL_ADD_PROC(ctx, debug_list,
2090             OID_AUTO, "filter_list", CTLTYPE_STRING | CTLFLAG_RD,
2091             sc, 0, iavf_sysctl_sw_filter_list, "A", "SW Filter List");
2092
2093         SYSCTL_ADD_PROC(ctx, debug_list,
2094             OID_AUTO, "queue_interrupt_table", CTLTYPE_STRING | CTLFLAG_RD,
2095             sc, 0, iavf_sysctl_queue_interrupt_table, "A", "View MSI-X indices for TX/RX queues");
2096
2097         SYSCTL_ADD_PROC(ctx, debug_list,
2098             OID_AUTO, "do_vf_reset", CTLTYPE_INT | CTLFLAG_WR,
2099             sc, 0, iavf_sysctl_vf_reset, "A", "Request a VF reset from PF");
2100
2101         SYSCTL_ADD_PROC(ctx, debug_list,
2102             OID_AUTO, "do_vflr_reset", CTLTYPE_INT | CTLFLAG_WR,
2103             sc, 0, iavf_sysctl_vflr_reset, "A", "Request a VFLR reset from HW");
2104
2105         /* Add stats sysctls */
2106         ixl_add_vsi_sysctls(dev, vsi, ctx, "vsi");
2107         ixl_add_queues_sysctls(dev, vsi);
2108
2109 }
2110
2111 static void
2112 iavf_init_filters(struct iavf_sc *sc)
2113 {
2114         sc->mac_filters = malloc(sizeof(struct mac_list),
2115             M_IAVF, M_WAITOK | M_ZERO);
2116         SLIST_INIT(sc->mac_filters);
2117         sc->vlan_filters = malloc(sizeof(struct vlan_list),
2118             M_IAVF, M_WAITOK | M_ZERO);
2119         SLIST_INIT(sc->vlan_filters);
2120 }
2121
2122 static void
2123 iavf_free_filters(struct iavf_sc *sc)
2124 {
2125         struct iavf_mac_filter *f;
2126         struct iavf_vlan_filter *v;
2127
2128         while (!SLIST_EMPTY(sc->mac_filters)) {
2129                 f = SLIST_FIRST(sc->mac_filters);
2130                 SLIST_REMOVE_HEAD(sc->mac_filters, next);
2131                 free(f, M_IAVF);
2132         }
2133         free(sc->mac_filters, M_IAVF);
2134         while (!SLIST_EMPTY(sc->vlan_filters)) {
2135                 v = SLIST_FIRST(sc->vlan_filters);
2136                 SLIST_REMOVE_HEAD(sc->vlan_filters, next);
2137                 free(v, M_IAVF);
2138         }
2139         free(sc->vlan_filters, M_IAVF);
2140 }
2141
2142 char *
2143 iavf_vc_speed_to_string(enum virtchnl_link_speed link_speed)
2144 {
2145         int index;
2146
2147         char *speeds[] = {
2148                 "Unknown",
2149                 "100 Mbps",
2150                 "1 Gbps",
2151                 "10 Gbps",
2152                 "40 Gbps",
2153                 "20 Gbps",
2154                 "25 Gbps",
2155         };
2156
2157         switch (link_speed) {
2158         case VIRTCHNL_LINK_SPEED_100MB:
2159                 index = 1;
2160                 break;
2161         case VIRTCHNL_LINK_SPEED_1GB:
2162                 index = 2;
2163                 break;
2164         case VIRTCHNL_LINK_SPEED_10GB:
2165                 index = 3;
2166                 break;
2167         case VIRTCHNL_LINK_SPEED_40GB:
2168                 index = 4;
2169                 break;
2170         case VIRTCHNL_LINK_SPEED_20GB:
2171                 index = 5;
2172                 break;
2173         case VIRTCHNL_LINK_SPEED_25GB:
2174                 index = 6;
2175                 break;
2176         case VIRTCHNL_LINK_SPEED_UNKNOWN:
2177         default:
2178                 index = 0;
2179                 break;
2180         }
2181
2182         return speeds[index];
2183 }
2184
2185 static int
2186 iavf_sysctl_current_speed(SYSCTL_HANDLER_ARGS)
2187 {
2188         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2189         int error = 0;
2190
2191         error = sysctl_handle_string(oidp,
2192           iavf_vc_speed_to_string(sc->link_speed),
2193           8, req);
2194         return (error);
2195 }
2196
2197 /*
2198  * Sanity check and save off tunable values.
2199  */
2200 static void
2201 iavf_save_tunables(struct iavf_sc *sc)
2202 {
2203         device_t dev = sc->dev;
2204
2205         /* Save tunable information */
2206         sc->dbg_mask = iavf_core_debug_mask;
2207         sc->hw.debug_mask = iavf_shared_debug_mask;
2208         sc->vsi.enable_head_writeback = !!(iavf_enable_head_writeback);
2209
2210         if (iavf_tx_itr < 0 || iavf_tx_itr > IXL_MAX_ITR) {
2211                 device_printf(dev, "Invalid tx_itr value of %d set!\n",
2212                     iavf_tx_itr);
2213                 device_printf(dev, "tx_itr must be between %d and %d, "
2214                     "inclusive\n",
2215                     0, IXL_MAX_ITR);
2216                 device_printf(dev, "Using default value of %d instead\n",
2217                     IXL_ITR_4K);
2218                 sc->tx_itr = IXL_ITR_4K;
2219         } else
2220                 sc->tx_itr = iavf_tx_itr;
2221
2222         if (iavf_rx_itr < 0 || iavf_rx_itr > IXL_MAX_ITR) {
2223                 device_printf(dev, "Invalid rx_itr value of %d set!\n",
2224                     iavf_rx_itr);
2225                 device_printf(dev, "rx_itr must be between %d and %d, "
2226                     "inclusive\n",
2227                     0, IXL_MAX_ITR);
2228                 device_printf(dev, "Using default value of %d instead\n",
2229                     IXL_ITR_8K);
2230                 sc->rx_itr = IXL_ITR_8K;
2231         } else
2232                 sc->rx_itr = iavf_rx_itr;
2233 }
2234
2235 /*
2236  * Used to set the Tx ITR value for all of the VF's queues.
2237  * Writes to the ITR registers immediately.
2238  */
2239 static int
2240 iavf_sysctl_tx_itr(SYSCTL_HANDLER_ARGS)
2241 {
2242         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2243         device_t dev = sc->dev;
2244         int requested_tx_itr;
2245         int error = 0;
2246
2247         requested_tx_itr = sc->tx_itr;
2248         error = sysctl_handle_int(oidp, &requested_tx_itr, 0, req);
2249         if ((error) || (req->newptr == NULL))
2250                 return (error);
2251         if (requested_tx_itr < 0 || requested_tx_itr > IXL_MAX_ITR) {
2252                 device_printf(dev,
2253                     "Invalid TX itr value; value must be between 0 and %d\n",
2254                         IXL_MAX_ITR);
2255                 return (EINVAL);
2256         }
2257
2258         sc->tx_itr = requested_tx_itr;
2259         iavf_configure_tx_itr(sc);
2260
2261         return (error);
2262 }
2263
2264 /*
2265  * Used to set the Rx ITR value for all of the VF's queues.
2266  * Writes to the ITR registers immediately.
2267  */
2268 static int
2269 iavf_sysctl_rx_itr(SYSCTL_HANDLER_ARGS)
2270 {
2271         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2272         device_t dev = sc->dev;
2273         int requested_rx_itr;
2274         int error = 0;
2275
2276         requested_rx_itr = sc->rx_itr;
2277         error = sysctl_handle_int(oidp, &requested_rx_itr, 0, req);
2278         if ((error) || (req->newptr == NULL))
2279                 return (error);
2280         if (requested_rx_itr < 0 || requested_rx_itr > IXL_MAX_ITR) {
2281                 device_printf(dev,
2282                     "Invalid RX itr value; value must be between 0 and %d\n",
2283                         IXL_MAX_ITR);
2284                 return (EINVAL);
2285         }
2286
2287         sc->rx_itr = requested_rx_itr;
2288         iavf_configure_rx_itr(sc);
2289
2290         return (error);
2291 }
2292
2293 static int
2294 iavf_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS)
2295 {
2296         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2297         struct iavf_mac_filter *f;
2298         struct iavf_vlan_filter *v;
2299         device_t dev = sc->dev;
2300         int ftl_len, ftl_counter = 0, error = 0;
2301         struct sbuf *buf;
2302
2303         buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2304         if (!buf) {
2305                 device_printf(dev, "Could not allocate sbuf for output.\n");
2306                 return (ENOMEM);
2307         }
2308
2309         sbuf_printf(buf, "\n");
2310
2311         /* Print MAC filters */
2312         sbuf_printf(buf, "MAC Filters:\n");
2313         ftl_len = 0;
2314         SLIST_FOREACH(f, sc->mac_filters, next)
2315                 ftl_len++;
2316         if (ftl_len < 1)
2317                 sbuf_printf(buf, "(none)\n");
2318         else {
2319                 SLIST_FOREACH(f, sc->mac_filters, next) {
2320                         sbuf_printf(buf,
2321                             MAC_FORMAT ", flags %#06x\n",
2322                             MAC_FORMAT_ARGS(f->macaddr), f->flags);
2323                 }
2324         }
2325
2326         /* Print VLAN filters */
2327         sbuf_printf(buf, "VLAN Filters:\n");
2328         ftl_len = 0;
2329         SLIST_FOREACH(v, sc->vlan_filters, next)
2330                 ftl_len++;
2331         if (ftl_len < 1)
2332                 sbuf_printf(buf, "(none)");
2333         else {
2334                 SLIST_FOREACH(v, sc->vlan_filters, next) {
2335                         sbuf_printf(buf,
2336                             "%d, flags %#06x",
2337                             v->vlan, v->flags);
2338                         /* don't print '\n' for last entry */
2339                         if (++ftl_counter != ftl_len)
2340                                 sbuf_printf(buf, "\n");
2341                 }
2342         }
2343
2344         error = sbuf_finish(buf);
2345         if (error)
2346                 device_printf(dev, "Error finishing sbuf: %d\n", error);
2347
2348         sbuf_delete(buf);
2349         return (error);
2350 }
2351
2352 /*
2353  * Print out mapping of TX queue indexes and Rx queue indexes
2354  * to MSI-X vectors.
2355  */
2356 static int
2357 iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS)
2358 {
2359         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2360         struct ixl_vsi *vsi = &sc->vsi;
2361         device_t dev = sc->dev;
2362         struct sbuf *buf;
2363         int error = 0;
2364
2365         struct ixl_rx_queue *rx_que = vsi->rx_queues;
2366         struct ixl_tx_queue *tx_que = vsi->tx_queues;
2367
2368         buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2369         if (!buf) {
2370                 device_printf(dev, "Could not allocate sbuf for output.\n");
2371                 return (ENOMEM);
2372         }
2373
2374         sbuf_cat(buf, "\n");
2375         for (int i = 0; i < vsi->num_rx_queues; i++) {
2376                 rx_que = &vsi->rx_queues[i];
2377                 sbuf_printf(buf, "(rxq %3d): %d\n", i, rx_que->msix);
2378         }
2379         for (int i = 0; i < vsi->num_tx_queues; i++) {
2380                 tx_que = &vsi->tx_queues[i];
2381                 sbuf_printf(buf, "(txq %3d): %d\n", i, tx_que->msix);
2382         }
2383
2384         error = sbuf_finish(buf);
2385         if (error)
2386                 device_printf(dev, "Error finishing sbuf: %d\n", error);
2387         sbuf_delete(buf);
2388
2389         return (error);
2390 }
2391
2392 #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
2393 static int
2394 iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS)
2395 {
2396         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2397         int do_reset = 0, error = 0;
2398
2399         error = sysctl_handle_int(oidp, &do_reset, 0, req);
2400         if ((error) || (req->newptr == NULL))
2401                 return (error);
2402
2403         if (do_reset == 1) {
2404                 iavf_reset(sc);
2405                 if (CTX_ACTIVE(sc->vsi.ctx))
2406                         iflib_request_reset(sc->vsi.ctx);
2407         }
2408
2409         return (error);
2410 }
2411
2412 static int
2413 iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS)
2414 {
2415         struct iavf_sc *sc = (struct iavf_sc *)arg1;
2416         device_t dev = sc->dev;
2417         int do_reset = 0, error = 0;
2418
2419         error = sysctl_handle_int(oidp, &do_reset, 0, req);
2420         if ((error) || (req->newptr == NULL))
2421                 return (error);
2422
2423         if (do_reset == 1) {
2424                 if (!pcie_flr(dev, max(pcie_get_max_completion_timeout(dev) / 1000, 10), true)) {
2425                         device_printf(dev, "PCIE FLR failed\n");
2426                         error = EIO;
2427                 }
2428                 else if (CTX_ACTIVE(sc->vsi.ctx))
2429                         iflib_request_reset(sc->vsi.ctx);
2430         }
2431
2432         return (error);
2433 }
2434 #undef CTX_ACTIVE