xref: /freebsd/sys/dev/iavf/iavf_lib.c (revision 942815c54820783d3d4f7f6faa71ab7919b5f0e5)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD$*/
32 
33 /**
34  * @file iavf_lib.c
35  * @brief library code common to both legacy and iflib
36  *
37  * Contains functions common to the iflib and legacy drivers. Includes
38  * hardware initialization and control functions, as well as sysctl handlers
39  * for the sysctls which are shared between the legacy and iflib drivers.
40  */
41 #include "iavf_iflib.h"
42 #include "iavf_vc_common.h"
43 
44 static void iavf_init_hw(struct iavf_hw *hw, device_t dev);
45 static u_int iavf_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int cnt);
46 
47 /**
48  * iavf_msec_pause - Pause for at least the specified number of milliseconds
49  * @msecs: number of milliseconds to pause for
50  *
51  * Pause execution of the current thread for a specified number of
52  * milliseconds. Used to enforce minimum delay times when waiting for various
53  * hardware events.
54  */
55 void
56 iavf_msec_pause(int msecs)
57 {
58 	pause("iavf_msec_pause", MSEC_2_TICKS(msecs));
59 }
60 
61 /**
62  * iavf_get_default_rss_key - Get the default RSS key for this driver
63  * @key: output parameter to store the key in
64  *
65  * Copies the driver's default RSS key into the provided key variable.
66  *
67  * @pre assumes that key is not NULL and has at least IAVF_RSS_KEY_SIZE
68  * storage space.
69  */
70 void
71 iavf_get_default_rss_key(u32 *key)
72 {
73 	MPASS(key != NULL);
74 
75 	u32 rss_seed[IAVF_RSS_KEY_SIZE_REG] = {0x41b01687,
76 	    0x183cfd8c, 0xce880440, 0x580cbc3c,
77 	    0x35897377, 0x328b25e1, 0x4fa98922,
78 	    0xb7d90c14, 0xd5bad70d, 0xcd15a2c1,
79 	    0x0, 0x0, 0x0};
80 
81 	bcopy(rss_seed, key, IAVF_RSS_KEY_SIZE);
82 }
83 
84 /**
85  * iavf_allocate_pci_resources_common - Allocate PCI resources
86  * @sc: the private device softc pointer
87  *
88  * @pre sc->dev is set
89  *
90  * Allocates the common PCI resources used by the driver.
91  *
92  * @returns zero on success, or an error code on failure.
93  */
94 int
95 iavf_allocate_pci_resources_common(struct iavf_sc *sc)
96 {
97 	struct iavf_hw *hw = &sc->hw;
98 	device_t dev = sc->dev;
99 	int rid;
100 
101 	/* Map PCI BAR0 */
102 	rid = PCIR_BAR(0);
103 	sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
104 	    &rid, RF_ACTIVE);
105 
106 	if (!(sc->pci_mem)) {
107 		device_printf(dev, "Unable to allocate bus resource: PCI memory\n");
108 		return (ENXIO);
109 	}
110 
111 	iavf_init_hw(hw, dev);
112 
113 	/* Save off register access information */
114 	sc->osdep.mem_bus_space_tag =
115 		rman_get_bustag(sc->pci_mem);
116 	sc->osdep.mem_bus_space_handle =
117 		rman_get_bushandle(sc->pci_mem);
118 	sc->osdep.mem_bus_space_size = rman_get_size(sc->pci_mem);
119 	sc->osdep.flush_reg = IAVF_VFGEN_RSTAT;
120 	sc->osdep.dev = dev;
121 
122 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
123 	sc->hw.back = &sc->osdep;
124 
125 	return (0);
126 }
127 
128 /**
129  * iavf_init_hw - Initialize the device HW
130  * @hw: device hardware structure
131  * @dev: the stack device_t pointer
132  *
133  * Attach helper function. Gathers information about the (virtual) hardware
134  * for use elsewhere in the driver.
135  */
136 static void
137 iavf_init_hw(struct iavf_hw *hw, device_t dev)
138 {
139 	/* Save off the information about this board */
140 	hw->vendor_id = pci_get_vendor(dev);
141 	hw->device_id = pci_get_device(dev);
142 	hw->revision_id = pci_read_config(dev, PCIR_REVID, 1);
143 	hw->subsystem_vendor_id =
144 	    pci_read_config(dev, PCIR_SUBVEND_0, 2);
145 	hw->subsystem_device_id =
146 	    pci_read_config(dev, PCIR_SUBDEV_0, 2);
147 
148 	hw->bus.device = pci_get_slot(dev);
149 	hw->bus.func = pci_get_function(dev);
150 }
151 
152 /**
153  * iavf_sysctl_current_speed - Sysctl to display the current device speed
154  * @oidp: syctl oid pointer
155  * @arg1: pointer to the device softc typecasted to void *
156  * @arg2: unused sysctl argument
157  * @req: sysctl request structure
158  *
159  * Reads the current speed reported from the physical device into a string for
160  * display by the current_speed sysctl.
161  *
162  * @returns zero or an error code on failure.
163  */
164 int
165 iavf_sysctl_current_speed(SYSCTL_HANDLER_ARGS)
166 {
167 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
168 	int error = 0;
169 
170 	UNREFERENCED_PARAMETER(arg2);
171 
172 	if (iavf_driver_is_detaching(sc))
173 		return (ESHUTDOWN);
174 
175 	if (IAVF_CAP_ADV_LINK_SPEED(sc))
176 		error = sysctl_handle_string(oidp,
177 		  __DECONST(char *, iavf_ext_speed_to_str(iavf_adv_speed_to_ext_speed(sc->link_speed_adv))),
178 		  8, req);
179 	else
180 		error = sysctl_handle_string(oidp,
181 		  __DECONST(char *, iavf_vc_speed_to_string(sc->link_speed)),
182 		  8, req);
183 
184 	return (error);
185 }
186 
187 /**
188  * iavf_reset_complete - Wait for a device reset to complete
189  * @hw: pointer to the hardware structure
190  *
191  * Reads the reset registers and waits until they indicate that a device reset
192  * is complete.
193  *
194  * @pre this function may call pause() and must not be called from a context
195  * that cannot sleep.
196  *
197  * @returns zero on success, or EBUSY if it times out waiting for reset.
198  */
199 int
200 iavf_reset_complete(struct iavf_hw *hw)
201 {
202 	u32 reg;
203 
204 	/* Wait up to ~10 seconds */
205 	for (int i = 0; i < 100; i++) {
206 		reg = rd32(hw, IAVF_VFGEN_RSTAT) &
207 		    IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
208 
209                 if ((reg == VIRTCHNL_VFR_VFACTIVE) ||
210 		    (reg == VIRTCHNL_VFR_COMPLETED))
211 			return (0);
212 		iavf_msec_pause(100);
213 	}
214 
215 	return (EBUSY);
216 }
217 
218 /**
219  * iavf_setup_vc - Setup virtchnl communication
220  * @sc: device private softc
221  *
222  * iavf_attach() helper function. Initializes the admin queue and attempts to
223  * establish contact with the PF by retrying the initial "API version" message
224  * several times or until the PF responds.
225  *
226  * @returns zero on success, or an error code on failure.
227  */
228 int
229 iavf_setup_vc(struct iavf_sc *sc)
230 {
231 	struct iavf_hw *hw = &sc->hw;
232 	device_t dev = sc->dev;
233 	int error = 0, ret_error = 0, asq_retries = 0;
234 	bool send_api_ver_retried = 0;
235 
236 	/* Need to set these AQ parameters before initializing AQ */
237 	hw->aq.num_arq_entries = IAVF_AQ_LEN;
238 	hw->aq.num_asq_entries = IAVF_AQ_LEN;
239 	hw->aq.arq_buf_size = IAVF_AQ_BUF_SZ;
240 	hw->aq.asq_buf_size = IAVF_AQ_BUF_SZ;
241 
242 	for (int i = 0; i < IAVF_AQ_MAX_ERR; i++) {
243 		/* Initialize admin queue */
244 		error = iavf_init_adminq(hw);
245 		if (error) {
246 			device_printf(dev, "%s: init_adminq failed: %d\n",
247 			    __func__, error);
248 			ret_error = 1;
249 			continue;
250 		}
251 
252 		iavf_dbg_init(sc, "Initialized Admin Queue; starting"
253 		    " send_api_ver attempt %d", i+1);
254 
255 retry_send:
256 		/* Send VF's API version */
257 		error = iavf_send_api_ver(sc);
258 		if (error) {
259 			iavf_shutdown_adminq(hw);
260 			ret_error = 2;
261 			device_printf(dev, "%s: unable to send api"
262 			    " version to PF on attempt %d, error %d\n",
263 			    __func__, i+1, error);
264 		}
265 
266 		asq_retries = 0;
267 		while (!iavf_asq_done(hw)) {
268 			if (++asq_retries > IAVF_AQ_MAX_ERR) {
269 				iavf_shutdown_adminq(hw);
270 				device_printf(dev, "Admin Queue timeout "
271 				    "(waiting for send_api_ver), %d more tries...\n",
272 				    IAVF_AQ_MAX_ERR - (i + 1));
273 				ret_error = 3;
274 				break;
275 			}
276 			iavf_msec_pause(10);
277 		}
278 		if (asq_retries > IAVF_AQ_MAX_ERR)
279 			continue;
280 
281 		iavf_dbg_init(sc, "Sent API version message to PF");
282 
283 		/* Verify that the VF accepts the PF's API version */
284 		error = iavf_verify_api_ver(sc);
285 		if (error == ETIMEDOUT) {
286 			if (!send_api_ver_retried) {
287 				/* Resend message, one more time */
288 				send_api_ver_retried = true;
289 				device_printf(dev,
290 				    "%s: Timeout while verifying API version on first"
291 				    " try!\n", __func__);
292 				goto retry_send;
293 			} else {
294 				device_printf(dev,
295 				    "%s: Timeout while verifying API version on second"
296 				    " try!\n", __func__);
297 				ret_error = 4;
298 				break;
299 			}
300 		}
301 		if (error) {
302 			device_printf(dev,
303 			    "%s: Unable to verify API version,"
304 			    " error %d\n", __func__, error);
305 			ret_error = 5;
306 		}
307 		break;
308 	}
309 
310 	if (ret_error >= 4)
311 		iavf_shutdown_adminq(hw);
312 	return (ret_error);
313 }
314 
315 /**
316  * iavf_reset - Requests a VF reset from the PF.
317  * @sc: device private softc
318  *
319  * @pre Requires the VF's Admin Queue to be initialized.
320  * @returns zero on success, or an error code on failure.
321  */
322 int
323 iavf_reset(struct iavf_sc *sc)
324 {
325 	struct iavf_hw	*hw = &sc->hw;
326 	device_t	dev = sc->dev;
327 	int		error = 0;
328 
329 	/* Ask the PF to reset us if we are initiating */
330 	if (!iavf_test_state(&sc->state, IAVF_STATE_RESET_PENDING))
331 		iavf_request_reset(sc);
332 
333 	iavf_msec_pause(100);
334 	error = iavf_reset_complete(hw);
335 	if (error) {
336 		device_printf(dev, "%s: VF reset failed\n",
337 		    __func__);
338 		return (error);
339 	}
340 	pci_enable_busmaster(dev);
341 
342 	error = iavf_shutdown_adminq(hw);
343 	if (error) {
344 		device_printf(dev, "%s: shutdown_adminq failed: %d\n",
345 		    __func__, error);
346 		return (error);
347 	}
348 
349 	error = iavf_init_adminq(hw);
350 	if (error) {
351 		device_printf(dev, "%s: init_adminq failed: %d\n",
352 		    __func__, error);
353 		return (error);
354 	}
355 
356 	/* IFLIB: This is called only in the iflib driver */
357 	iavf_enable_adminq_irq(hw);
358 	return (0);
359 }
360 
361 /**
362  * iavf_enable_admin_irq - Enable the administrative interrupt
363  * @hw: pointer to the hardware structure
364  *
365  * Writes to registers to enable the administrative interrupt cause, in order
366  * to handle non-queue related interrupt events.
367  */
368 void
369 iavf_enable_adminq_irq(struct iavf_hw *hw)
370 {
371 	wr32(hw, IAVF_VFINT_DYN_CTL01,
372 	    IAVF_VFINT_DYN_CTL01_INTENA_MASK |
373 	    IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
374 	    IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
375 	wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
376 	/* flush */
377 	rd32(hw, IAVF_VFGEN_RSTAT);
378 }
379 
380 /**
381  * iavf_disable_admin_irq - Disable the administrative interrupt cause
382  * @hw: pointer to the hardware structure
383  *
384  * Writes to registers to disable the administrative interrupt cause.
385  */
386 void
387 iavf_disable_adminq_irq(struct iavf_hw *hw)
388 {
389 	wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
390 	wr32(hw, IAVF_VFINT_ICR0_ENA1, 0);
391 	iavf_flush(hw);
392 }
393 
394 /**
395  * iavf_vf_config - Configure this VF over the virtchnl
396  * @sc: device private softc
397  *
398  * iavf_attach() helper function. Asks the PF for this VF's configuration, and
399  * saves the information if it receives it.
400  *
401  * @returns zero on success, or an error code on failure.
402  */
403 int
404 iavf_vf_config(struct iavf_sc *sc)
405 {
406 	struct iavf_hw *hw = &sc->hw;
407 	device_t dev = sc->dev;
408 	int bufsz, error = 0, ret_error = 0;
409 	int asq_retries, retried = 0;
410 
411 retry_config:
412 	error = iavf_send_vf_config_msg(sc);
413 	if (error) {
414 		device_printf(dev,
415 		    "%s: Unable to send VF config request, attempt %d,"
416 		    " error %d\n", __func__, retried + 1, error);
417 		ret_error = 2;
418 	}
419 
420 	asq_retries = 0;
421 	while (!iavf_asq_done(hw)) {
422 		if (++asq_retries > IAVF_AQ_MAX_ERR) {
423 			device_printf(dev, "%s: Admin Queue timeout "
424 			    "(waiting for send_vf_config_msg), attempt %d\n",
425 			    __func__, retried + 1);
426 			ret_error = 3;
427 			goto fail;
428 		}
429 		iavf_msec_pause(10);
430 	}
431 
432 	iavf_dbg_init(sc, "Sent VF config message to PF, attempt %d\n",
433 	    retried + 1);
434 
435 	if (!sc->vf_res) {
436 		bufsz = sizeof(struct virtchnl_vf_resource) +
437 		    (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
438 		sc->vf_res = (struct virtchnl_vf_resource *)malloc(bufsz, M_IAVF, M_NOWAIT);
439 		if (!sc->vf_res) {
440 			device_printf(dev,
441 			    "%s: Unable to allocate memory for VF configuration"
442 			    " message from PF on attempt %d\n", __func__, retried + 1);
443 			ret_error = 1;
444 			goto fail;
445 		}
446 	}
447 
448 	/* Check for VF config response */
449 	error = iavf_get_vf_config(sc);
450 	if (error == ETIMEDOUT) {
451 		/* The 1st time we timeout, send the configuration message again */
452 		if (!retried) {
453 			retried++;
454 			goto retry_config;
455 		}
456 		device_printf(dev,
457 		    "%s: iavf_get_vf_config() timed out waiting for a response\n",
458 		    __func__);
459 	}
460 	if (error) {
461 		device_printf(dev,
462 		    "%s: Unable to get VF configuration from PF after %d tries!\n",
463 		    __func__, retried + 1);
464 		ret_error = 4;
465 	}
466 	goto done;
467 
468 fail:
469 	free(sc->vf_res, M_IAVF);
470 done:
471 	return (ret_error);
472 }
473 
474 /**
475  * iavf_print_device_info - Print some device parameters at attach
476  * @sc: device private softc
477  *
478  * Log a message about this virtual device's capabilities at attach time.
479  */
480 void
481 iavf_print_device_info(struct iavf_sc *sc)
482 {
483 	device_t dev = sc->dev;
484 
485 	device_printf(dev,
486 	    "VSIs %d, QPs %d, MSI-X %d, RSS sizes: key %d lut %d\n",
487 	    sc->vf_res->num_vsis,
488 	    sc->vf_res->num_queue_pairs,
489 	    sc->vf_res->max_vectors,
490 	    sc->vf_res->rss_key_size,
491 	    sc->vf_res->rss_lut_size);
492 	iavf_dbg_info(sc, "Capabilities=%b\n",
493 	    sc->vf_res->vf_cap_flags, IAVF_PRINTF_VF_OFFLOAD_FLAGS);
494 }
495 
496 /**
497  * iavf_get_vsi_res_from_vf_res - Get VSI parameters and info for this VF
498  * @sc: device private softc
499  *
500  * Get the VSI parameters and information from the general VF resource info
501  * received by the physical device.
502  *
503  * @returns zero on success, or an error code on failure.
504  */
505 int
506 iavf_get_vsi_res_from_vf_res(struct iavf_sc *sc)
507 {
508 	struct iavf_vsi *vsi = &sc->vsi;
509 	device_t dev = sc->dev;
510 
511 	sc->vsi_res = NULL;
512 
513 	for (int i = 0; i < sc->vf_res->num_vsis; i++) {
514 		/* XXX: We only use the first VSI we find */
515 		if (sc->vf_res->vsi_res[i].vsi_type == IAVF_VSI_SRIOV)
516 			sc->vsi_res = &sc->vf_res->vsi_res[i];
517 	}
518 	if (!sc->vsi_res) {
519 		device_printf(dev, "%s: no LAN VSI found\n", __func__);
520 		return (EIO);
521 	}
522 
523 	vsi->id = sc->vsi_res->vsi_id;
524 	return (0);
525 }
526 
527 /**
528  * iavf_set_mac_addresses - Set the MAC address for this interface
529  * @sc: device private softc
530  *
531  * Set the permanent MAC address field in the HW structure. If a MAC address
532  * has not yet been set for this device by the physical function, generate one
533  * randomly.
534  */
535 void
536 iavf_set_mac_addresses(struct iavf_sc *sc)
537 {
538 	struct iavf_hw *hw = &sc->hw;
539 	device_t dev = sc->dev;
540 	u8 addr[ETHER_ADDR_LEN];
541 
542 	/* If no mac address was assigned just make a random one */
543 	if (ETHER_IS_ZERO(hw->mac.addr)) {
544 		arc4rand(&addr, sizeof(addr), 0);
545 		addr[0] &= 0xFE;
546 		addr[0] |= 0x02;
547 		memcpy(hw->mac.addr, addr, sizeof(addr));
548 		device_printf(dev, "Generated random MAC address\n");
549 	}
550 	memcpy(hw->mac.perm_addr, hw->mac.addr, ETHER_ADDR_LEN);
551 }
552 
553 /**
554  * iavf_init_filters - Initialize filter structures
555  * @sc: device private softc
556  *
557  * Initialize the MAC and VLAN filter list heads.
558  *
559  * @remark this is intended to be called only once during the device attach
560  * process.
561  *
562  * @pre Because it uses M_WAITOK, this function should only be called in
563  * a context that is safe to sleep.
564  */
565 void
566 iavf_init_filters(struct iavf_sc *sc)
567 {
568 	sc->mac_filters = (struct mac_list *)malloc(sizeof(struct iavf_mac_filter),
569 	    M_IAVF, M_WAITOK | M_ZERO);
570 	SLIST_INIT(sc->mac_filters);
571 	sc->vlan_filters = (struct vlan_list *)malloc(sizeof(struct iavf_vlan_filter),
572 	    M_IAVF, M_WAITOK | M_ZERO);
573 	SLIST_INIT(sc->vlan_filters);
574 }
575 
576 /**
577  * iavf_free_filters - Release filter lists
578  * @sc: device private softc
579  *
580  * Free the MAC and VLAN filter lists.
581  *
582  * @remark this is intended to be called only once during the device detach
583  * process.
584  */
585 void
586 iavf_free_filters(struct iavf_sc *sc)
587 {
588 	struct iavf_mac_filter *f;
589 	struct iavf_vlan_filter *v;
590 
591 	while (!SLIST_EMPTY(sc->mac_filters)) {
592 		f = SLIST_FIRST(sc->mac_filters);
593 		SLIST_REMOVE_HEAD(sc->mac_filters, next);
594 		free(f, M_IAVF);
595 	}
596 	free(sc->mac_filters, M_IAVF);
597 	while (!SLIST_EMPTY(sc->vlan_filters)) {
598 		v = SLIST_FIRST(sc->vlan_filters);
599 		SLIST_REMOVE_HEAD(sc->vlan_filters, next);
600 		free(v, M_IAVF);
601 	}
602 	free(sc->vlan_filters, M_IAVF);
603 }
604 
605 /**
606  * iavf_add_device_sysctls_common - Initialize common device sysctls
607  * @sc: device private softc
608  *
609  * Setup sysctls common to both the iflib and legacy drivers.
610  */
611 void
612 iavf_add_device_sysctls_common(struct iavf_sc *sc)
613 {
614 	device_t dev = sc->dev;
615 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
616 	struct sysctl_oid_list *ctx_list =
617 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
618 
619 	SYSCTL_ADD_PROC(ctx, ctx_list,
620 	    OID_AUTO, "current_speed", CTLTYPE_STRING | CTLFLAG_RD,
621 	    sc, 0, iavf_sysctl_current_speed, "A", "Current Port Speed");
622 
623 	SYSCTL_ADD_PROC(ctx, ctx_list,
624 	    OID_AUTO, "tx_itr", CTLTYPE_INT | CTLFLAG_RW,
625 	    sc, 0, iavf_sysctl_tx_itr, "I",
626 	    "Immediately set TX ITR value for all queues");
627 
628 	SYSCTL_ADD_PROC(ctx, ctx_list,
629 	    OID_AUTO, "rx_itr", CTLTYPE_INT | CTLFLAG_RW,
630 	    sc, 0, iavf_sysctl_rx_itr, "I",
631 	    "Immediately set RX ITR value for all queues");
632 
633 	SYSCTL_ADD_UQUAD(ctx, ctx_list,
634 	    OID_AUTO, "admin_irq", CTLFLAG_RD,
635 	    &sc->admin_irq, "Admin Queue IRQ Handled");
636 }
637 
638 /**
639  * iavf_add_debug_sysctls_common - Initialize common debug sysctls
640  * @sc: device private softc
641  * @debug_list: pionter to debug sysctl node
642  *
643  * Setup sysctls used for debugging the device driver into the debug sysctl
644  * node.
645  */
646 void
647 iavf_add_debug_sysctls_common(struct iavf_sc *sc, struct sysctl_oid_list *debug_list)
648 {
649 	device_t dev = sc->dev;
650 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
651 
652 	SYSCTL_ADD_UINT(ctx, debug_list,
653 	    OID_AUTO, "shared_debug_mask", CTLFLAG_RW,
654 	    &sc->hw.debug_mask, 0, "Shared code debug message level");
655 
656 	SYSCTL_ADD_UINT(ctx, debug_list,
657 	    OID_AUTO, "core_debug_mask", CTLFLAG_RW,
658 	    (unsigned int *)&sc->dbg_mask, 0, "Non-shared code debug message level");
659 
660 	SYSCTL_ADD_PROC(ctx, debug_list,
661 	    OID_AUTO, "filter_list", CTLTYPE_STRING | CTLFLAG_RD,
662 	    sc, 0, iavf_sysctl_sw_filter_list, "A", "SW Filter List");
663 }
664 
665 /**
666  * iavf_sysctl_tx_itr - Sysctl to set the Tx ITR value
667  * @oidp: sysctl oid pointer
668  * @arg1: pointer to the device softc
669  * @arg2: unused sysctl argument
670  * @req: sysctl req pointer
671  *
672  * On read, returns the Tx ITR value for all of the VF queues. On write,
673  * update the Tx ITR registers with the new Tx ITR value.
674  *
675  * @returns zero on success, or an error code on failure.
676  */
677 int
678 iavf_sysctl_tx_itr(SYSCTL_HANDLER_ARGS)
679 {
680 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
681 	device_t dev = sc->dev;
682 	int requested_tx_itr;
683 	int error = 0;
684 
685 	UNREFERENCED_PARAMETER(arg2);
686 
687 	if (iavf_driver_is_detaching(sc))
688 		return (ESHUTDOWN);
689 
690 	requested_tx_itr = sc->tx_itr;
691 	error = sysctl_handle_int(oidp, &requested_tx_itr, 0, req);
692 	if ((error) || (req->newptr == NULL))
693 		return (error);
694 	if (requested_tx_itr < 0 || requested_tx_itr > IAVF_MAX_ITR) {
695 		device_printf(dev,
696 		    "Invalid TX itr value; value must be between 0 and %d\n",
697 		        IAVF_MAX_ITR);
698 		return (EINVAL);
699 	}
700 
701 	sc->tx_itr = requested_tx_itr;
702 	iavf_configure_tx_itr(sc);
703 
704 	return (error);
705 }
706 
707 /**
708  * iavf_sysctl_rx_itr - Sysctl to set the Rx ITR value
709  * @oidp: sysctl oid pointer
710  * @arg1: pointer to the device softc
711  * @arg2: unused sysctl argument
712  * @req: sysctl req pointer
713  *
714  * On read, returns the Rx ITR value for all of the VF queues. On write,
715  * update the ITR registers with the new Rx ITR value.
716  *
717  * @returns zero on success, or an error code on failure.
718  */
719 int
720 iavf_sysctl_rx_itr(SYSCTL_HANDLER_ARGS)
721 {
722 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
723 	device_t dev = sc->dev;
724 	int requested_rx_itr;
725 	int error = 0;
726 
727 	UNREFERENCED_PARAMETER(arg2);
728 
729 	if (iavf_driver_is_detaching(sc))
730 		return (ESHUTDOWN);
731 
732 	requested_rx_itr = sc->rx_itr;
733 	error = sysctl_handle_int(oidp, &requested_rx_itr, 0, req);
734 	if ((error) || (req->newptr == NULL))
735 		return (error);
736 	if (requested_rx_itr < 0 || requested_rx_itr > IAVF_MAX_ITR) {
737 		device_printf(dev,
738 		    "Invalid RX itr value; value must be between 0 and %d\n",
739 		        IAVF_MAX_ITR);
740 		return (EINVAL);
741 	}
742 
743 	sc->rx_itr = requested_rx_itr;
744 	iavf_configure_rx_itr(sc);
745 
746 	return (error);
747 }
748 
749 /**
750  * iavf_configure_tx_itr - Configure the Tx ITR
751  * @sc: device private softc
752  *
753  * Updates the ITR registers with a new Tx ITR setting.
754  */
755 void
756 iavf_configure_tx_itr(struct iavf_sc *sc)
757 {
758 	struct iavf_hw		*hw = &sc->hw;
759 	struct iavf_vsi		*vsi = &sc->vsi;
760 	struct iavf_tx_queue	*que = vsi->tx_queues;
761 
762 	vsi->tx_itr_setting = sc->tx_itr;
763 
764 	for (int i = 0; i < IAVF_NTXQS(vsi); i++, que++) {
765 		struct tx_ring	*txr = &que->txr;
766 
767 		wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, i),
768 		    vsi->tx_itr_setting);
769 		txr->itr = vsi->tx_itr_setting;
770 		txr->latency = IAVF_AVE_LATENCY;
771 	}
772 }
773 
774 /**
775  * iavf_configure_rx_itr - Configure the Rx ITR
776  * @sc: device private softc
777  *
778  * Updates the ITR registers with a new Rx ITR setting.
779  */
780 void
781 iavf_configure_rx_itr(struct iavf_sc *sc)
782 {
783 	struct iavf_hw		*hw = &sc->hw;
784 	struct iavf_vsi		*vsi = &sc->vsi;
785 	struct iavf_rx_queue	*que = vsi->rx_queues;
786 
787 	vsi->rx_itr_setting = sc->rx_itr;
788 
789 	for (int i = 0; i < IAVF_NRXQS(vsi); i++, que++) {
790 		struct rx_ring	*rxr = &que->rxr;
791 
792 		wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, i),
793 		    vsi->rx_itr_setting);
794 		rxr->itr = vsi->rx_itr_setting;
795 		rxr->latency = IAVF_AVE_LATENCY;
796 	}
797 }
798 
799 /**
800  * iavf_create_debug_sysctl_tree - Create a debug sysctl node
801  * @sc: device private softc
802  *
803  * Create a sysctl node meant to hold sysctls used to print debug information.
804  * Mark it as CTLFLAG_SKIP so that these sysctls do not show up in the
805  * "sysctl -a" output.
806  *
807  * @returns a pointer to the created sysctl node.
808  */
809 struct sysctl_oid_list *
810 iavf_create_debug_sysctl_tree(struct iavf_sc *sc)
811 {
812 	device_t dev = sc->dev;
813 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
814 	struct sysctl_oid_list *ctx_list =
815 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
816 	struct sysctl_oid *debug_node;
817 
818 	debug_node = SYSCTL_ADD_NODE(ctx, ctx_list,
819 	    OID_AUTO, "debug", CTLFLAG_RD | CTLFLAG_SKIP, NULL, "Debug Sysctls");
820 
821 	return (SYSCTL_CHILDREN(debug_node));
822 }
823 
824 /**
825  * iavf_add_vsi_sysctls - Add sysctls for a given VSI
826  * @dev: device pointer
827  * @vsi: pointer to the VSI
828  * @ctx: sysctl context to add to
829  * @sysctl_name: name of the sysctl node (containing the VSI number)
830  *
831  * Adds a new sysctl node for holding specific sysctls for the given VSI.
832  */
833 void
834 iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi,
835     struct sysctl_ctx_list *ctx, const char *sysctl_name)
836 {
837 	struct sysctl_oid *tree;
838 	struct sysctl_oid_list *child;
839 	struct sysctl_oid_list *vsi_list;
840 
841 	tree = device_get_sysctl_tree(dev);
842 	child = SYSCTL_CHILDREN(tree);
843 	vsi->vsi_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, sysctl_name,
844 				   CTLFLAG_RD, NULL, "VSI Number");
845 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
846 
847 	iavf_add_sysctls_eth_stats(ctx, vsi_list, &vsi->eth_stats);
848 }
849 
850 /**
851  * iavf_sysctl_sw_filter_list - Dump software filters
852  * @oidp: sysctl oid pointer
853  * @arg1: pointer to the device softc
854  * @arg2: unused sysctl argument
855  * @req: sysctl req pointer
856  *
857  * On read, generates a string which lists the MAC and VLAN filters added to
858  * this virtual device. Useful for debugging to see whether or not the
859  * expected filters have been configured by software.
860  *
861  * @returns zero on success, or an error code on failure.
862  */
863 int
864 iavf_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS)
865 {
866 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
867 	struct iavf_mac_filter *f;
868 	struct iavf_vlan_filter *v;
869 	device_t dev = sc->dev;
870 	int ftl_len, ftl_counter = 0, error = 0;
871 	struct sbuf *buf;
872 
873 	UNREFERENCED_2PARAMETER(arg2, oidp);
874 
875 	if (iavf_driver_is_detaching(sc))
876 		return (ESHUTDOWN);
877 
878 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
879 	if (!buf) {
880 		device_printf(dev, "Could not allocate sbuf for output.\n");
881 		return (ENOMEM);
882 	}
883 
884 	sbuf_printf(buf, "\n");
885 
886 	/* Print MAC filters */
887 	sbuf_printf(buf, "MAC Filters:\n");
888 	ftl_len = 0;
889 	SLIST_FOREACH(f, sc->mac_filters, next)
890 		ftl_len++;
891 	if (ftl_len < 1)
892 		sbuf_printf(buf, "(none)\n");
893 	else {
894 		SLIST_FOREACH(f, sc->mac_filters, next) {
895 			sbuf_printf(buf,
896 			    MAC_FORMAT ", flags %#06x\n",
897 			    MAC_FORMAT_ARGS(f->macaddr), f->flags);
898 		}
899 	}
900 
901 	/* Print VLAN filters */
902 	sbuf_printf(buf, "VLAN Filters:\n");
903 	ftl_len = 0;
904 	SLIST_FOREACH(v, sc->vlan_filters, next)
905 		ftl_len++;
906 	if (ftl_len < 1)
907 		sbuf_printf(buf, "(none)");
908 	else {
909 		SLIST_FOREACH(v, sc->vlan_filters, next) {
910 			sbuf_printf(buf,
911 			    "%d, flags %#06x",
912 			    v->vlan, v->flags);
913 			/* don't print '\n' for last entry */
914 			if (++ftl_counter != ftl_len)
915 				sbuf_printf(buf, "\n");
916 		}
917 	}
918 
919 	error = sbuf_finish(buf);
920 	if (error)
921 		device_printf(dev, "Error finishing sbuf: %d\n", error);
922 
923 	sbuf_delete(buf);
924 	return (error);
925 }
926 
927 /**
928  * iavf_media_status_common - Get media status for this device
929  * @sc: device softc pointer
930  * @ifmr: ifmedia request structure
931  *
932  * Report the media status for this device into the given ifmr structure.
933  */
934 void
935 iavf_media_status_common(struct iavf_sc *sc, struct ifmediareq *ifmr)
936 {
937 	enum iavf_ext_link_speed ext_speed;
938 
939 	iavf_update_link_status(sc);
940 
941 	ifmr->ifm_status = IFM_AVALID;
942 	ifmr->ifm_active = IFM_ETHER;
943 
944 	if (!sc->link_up)
945 		return;
946 
947 	ifmr->ifm_status |= IFM_ACTIVE;
948 	/* Hardware is always full-duplex */
949 	ifmr->ifm_active |= IFM_FDX;
950 
951 	/* Based on the link speed reported by the PF over the AdminQ, choose a
952 	 * PHY type to report. This isn't 100% correct since we don't really
953 	 * know the underlying PHY type of the PF, but at least we can report
954 	 * a valid link speed...
955 	 */
956 	if (IAVF_CAP_ADV_LINK_SPEED(sc))
957 		ext_speed = iavf_adv_speed_to_ext_speed(sc->link_speed_adv);
958 	else
959 		ext_speed = iavf_vc_speed_to_ext_speed(sc->link_speed);
960 
961 	ifmr->ifm_active |= iavf_ext_speed_to_ifmedia(ext_speed);
962 }
963 
964 /**
965  * iavf_media_change_common - Change the media type for this device
966  * @ifp: ifnet structure
967  *
968  * @returns ENODEV because changing the media and speed is not supported.
969  */
970 int
971 iavf_media_change_common(if_t ifp)
972 {
973 	if_printf(ifp, "Changing speed is not supported\n");
974 
975 	return (ENODEV);
976 }
977 
978 /**
979  * iavf_set_initial_baudrate - Set the initial device baudrate
980  * @ifp: ifnet structure
981  *
982  * Set the baudrate for this ifnet structure to the expected initial value of
983  * 40Gbps. This maybe updated to a lower baudrate after the physical function
984  * reports speed to us over the virtchnl interface.
985  */
986 void
987 iavf_set_initial_baudrate(if_t ifp)
988 {
989 	if_setbaudrate(ifp, IF_Gbps(40));
990 }
991 
992 /**
993  * iavf_add_sysctls_eth_stats - Add ethernet statistics sysctls
994  * @ctx: the sysctl ctx to add to
995  * @child: the node to add the sysctls to
996  * @eth_stats: ethernet stats structure
997  *
998  * Creates sysctls that report the values of the provided ethernet stats
999  * structure.
1000  */
1001 void
1002 iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
1003 	struct sysctl_oid_list *child,
1004 	struct iavf_eth_stats *eth_stats)
1005 {
1006 	struct iavf_sysctl_info ctls[] =
1007 	{
1008 		{&eth_stats->rx_bytes, "good_octets_rcvd", "Good Octets Received"},
1009 		{&eth_stats->rx_unicast, "ucast_pkts_rcvd",
1010 			"Unicast Packets Received"},
1011 		{&eth_stats->rx_multicast, "mcast_pkts_rcvd",
1012 			"Multicast Packets Received"},
1013 		{&eth_stats->rx_broadcast, "bcast_pkts_rcvd",
1014 			"Broadcast Packets Received"},
1015 		{&eth_stats->rx_discards, "rx_discards", "Discarded RX packets"},
1016 		{&eth_stats->rx_unknown_protocol, "rx_unknown_proto",
1017 			"RX unknown protocol packets"},
1018 		{&eth_stats->tx_bytes, "good_octets_txd", "Good Octets Transmitted"},
1019 		{&eth_stats->tx_unicast, "ucast_pkts_txd", "Unicast Packets Transmitted"},
1020 		{&eth_stats->tx_multicast, "mcast_pkts_txd",
1021 			"Multicast Packets Transmitted"},
1022 		{&eth_stats->tx_broadcast, "bcast_pkts_txd",
1023 			"Broadcast Packets Transmitted"},
1024 		{&eth_stats->tx_errors, "tx_errors", "TX packet errors"},
1025 		// end
1026 		{0,0,0}
1027 	};
1028 
1029 	struct iavf_sysctl_info *entry = ctls;
1030 
1031 	while (entry->stat != 0)
1032 	{
1033 		SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, entry->name,
1034 				CTLFLAG_RD, entry->stat,
1035 				entry->description);
1036 		entry++;
1037 	}
1038 }
1039 
1040 /**
1041  * iavf_max_vc_speed_to_value - Convert link speed to IF speed value
1042  * @link_speeds: bitmap of supported link speeds
1043  *
1044  * @returns the link speed value for the highest speed reported in the
1045  * link_speeds bitmap.
1046  */
1047 u64
1048 iavf_max_vc_speed_to_value(u8 link_speeds)
1049 {
1050 	if (link_speeds & VIRTCHNL_LINK_SPEED_40GB)
1051 		return IF_Gbps(40);
1052 	if (link_speeds & VIRTCHNL_LINK_SPEED_25GB)
1053 		return IF_Gbps(25);
1054 	if (link_speeds & VIRTCHNL_LINK_SPEED_20GB)
1055 		return IF_Gbps(20);
1056 	if (link_speeds & VIRTCHNL_LINK_SPEED_10GB)
1057 		return IF_Gbps(10);
1058 	if (link_speeds & VIRTCHNL_LINK_SPEED_1GB)
1059 		return IF_Gbps(1);
1060 	if (link_speeds & VIRTCHNL_LINK_SPEED_100MB)
1061 		return IF_Mbps(100);
1062 	else
1063 		/* Minimum supported link speed */
1064 		return IF_Mbps(100);
1065 }
1066 
1067 /**
1068  * iavf_config_rss_reg - Configure RSS using registers
1069  * @sc: device private softc
1070  *
1071  * Configures RSS for this function using the device registers. Called if the
1072  * PF does not support configuring RSS over the virtchnl interface.
1073  */
1074 void
1075 iavf_config_rss_reg(struct iavf_sc *sc)
1076 {
1077 	struct iavf_hw	*hw = &sc->hw;
1078 	struct iavf_vsi	*vsi = &sc->vsi;
1079 	u32		lut = 0;
1080 	u64		set_hena = 0, hena;
1081 	int		i, j, que_id;
1082 	u32		rss_seed[IAVF_RSS_KEY_SIZE_REG];
1083 #ifdef RSS
1084 	u32		rss_hash_config;
1085 #endif
1086 
1087 	/* Don't set up RSS if using a single queue */
1088 	if (IAVF_NRXQS(vsi) == 1) {
1089 		wr32(hw, IAVF_VFQF_HENA(0), 0);
1090 		wr32(hw, IAVF_VFQF_HENA(1), 0);
1091 		iavf_flush(hw);
1092 		return;
1093 	}
1094 
1095 #ifdef RSS
1096 	/* Fetch the configured RSS key */
1097 	rss_getkey((uint8_t *) &rss_seed);
1098 #else
1099 	iavf_get_default_rss_key(rss_seed);
1100 #endif
1101 
1102 	/* Fill out hash function seed */
1103 	for (i = 0; i < IAVF_RSS_KEY_SIZE_REG; i++)
1104                 wr32(hw, IAVF_VFQF_HKEY(i), rss_seed[i]);
1105 
1106 	/* Enable PCTYPES for RSS: */
1107 #ifdef RSS
1108 	rss_hash_config = rss_gethashconfig();
1109 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
1110                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV4_OTHER);
1111 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
1112                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV4_TCP);
1113 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
1114                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV4_UDP);
1115 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
1116                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV6_OTHER);
1117 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
1118 		set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_FRAG_IPV6);
1119 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
1120                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV6_TCP);
1121         if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
1122                 set_hena |= ((u64)1 << IAVF_FILTER_PCTYPE_NONF_IPV6_UDP);
1123 #else
1124 	set_hena = IAVF_DEFAULT_RSS_HENA_XL710;
1125 #endif
1126 	hena = (u64)rd32(hw, IAVF_VFQF_HENA(0)) |
1127 	    ((u64)rd32(hw, IAVF_VFQF_HENA(1)) << 32);
1128 	hena |= set_hena;
1129 	wr32(hw, IAVF_VFQF_HENA(0), (u32)hena);
1130 	wr32(hw, IAVF_VFQF_HENA(1), (u32)(hena >> 32));
1131 
1132 	/* Populate the LUT with max no. of queues in round robin fashion */
1133 	for (i = 0, j = 0; i < IAVF_RSS_VSI_LUT_SIZE; i++, j++) {
1134                 if (j == IAVF_NRXQS(vsi))
1135                         j = 0;
1136 #ifdef RSS
1137 		/*
1138 		 * Fetch the RSS bucket id for the given indirection entry.
1139 		 * Cap it at the number of configured buckets (which is
1140 		 * num_rx_queues.)
1141 		 */
1142 		que_id = rss_get_indirection_to_bucket(i);
1143 		que_id = que_id % IAVF_NRXQS(vsi);
1144 #else
1145 		que_id = j;
1146 #endif
1147                 /* lut = 4-byte sliding window of 4 lut entries */
1148                 lut = (lut << 8) | (que_id & IAVF_RSS_VF_LUT_ENTRY_MASK);
1149                 /* On i = 3, we have 4 entries in lut; write to the register */
1150                 if ((i & 3) == 3) {
1151                         wr32(hw, IAVF_VFQF_HLUT(i >> 2), lut);
1152 			iavf_dbg_rss(sc, "%s: HLUT(%2d): %#010x", __func__,
1153 			    i, lut);
1154 		}
1155         }
1156 	iavf_flush(hw);
1157 }
1158 
1159 /**
1160  * iavf_config_rss_pf - Configure RSS using PF virtchnl messages
1161  * @sc: device private softc
1162  *
1163  * Configure RSS by sending virtchnl messages to the PF.
1164  */
1165 void
1166 iavf_config_rss_pf(struct iavf_sc *sc)
1167 {
1168 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIG_RSS_KEY);
1169 
1170 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_SET_RSS_HENA);
1171 
1172 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIG_RSS_LUT);
1173 }
1174 
1175 /**
1176  * iavf_config_rss - setup RSS
1177  * @sc: device private softc
1178  *
1179  * Configures RSS using the method determined by capability flags in the VF
1180  * resources structure sent from the PF over the virtchnl interface.
1181  *
1182  * @remark RSS keys and table are cleared on VF reset.
1183  */
1184 void
1185 iavf_config_rss(struct iavf_sc *sc)
1186 {
1187 	if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_REG) {
1188 		iavf_dbg_info(sc, "Setting up RSS using VF registers...\n");
1189 		iavf_config_rss_reg(sc);
1190 	} else if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1191 		iavf_dbg_info(sc, "Setting up RSS using messages to PF...\n");
1192 		iavf_config_rss_pf(sc);
1193 	} else
1194 		device_printf(sc->dev, "VF does not support RSS capability sent by PF.\n");
1195 }
1196 
1197 /**
1198  * iavf_config_promisc - setup promiscuous mode
1199  * @sc: device private softc
1200  * @flags: promiscuous flags to configure
1201  *
1202  * Request that promiscuous modes be enabled from the PF
1203  *
1204  * @returns zero on success, or an error code on failure.
1205  */
1206 int
1207 iavf_config_promisc(struct iavf_sc *sc, int flags)
1208 {
1209 	if_t ifp = sc->vsi.ifp;
1210 
1211 	sc->promisc_flags = 0;
1212 
1213 	if (flags & IFF_ALLMULTI ||
1214 		if_llmaddr_count(ifp) == MAX_MULTICAST_ADDR)
1215 		sc->promisc_flags |= FLAG_VF_MULTICAST_PROMISC;
1216 	if (flags & IFF_PROMISC)
1217 		sc->promisc_flags |= FLAG_VF_UNICAST_PROMISC;
1218 
1219 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_PROMISC);
1220 
1221 	return (0);
1222 }
1223 
1224 /**
1225  * iavf_mc_filter_apply - Program a MAC filter for this VF
1226  * @arg: pointer to the device softc
1227  * @sdl: MAC multicast address
1228  * @cnt: unused parameter
1229  *
1230  * Program a MAC address multicast filter for this device. Intended
1231  * to be used with the map-like function if_foreach_llmaddr().
1232  *
1233  * @returns 1 on success, or 0 on failure
1234  */
1235 static u_int
1236 iavf_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int cnt __unused)
1237 {
1238 	struct iavf_sc *sc = (struct iavf_sc *)arg;
1239 	int error;
1240 
1241 	error = iavf_add_mac_filter(sc, (u8*)LLADDR(sdl), IAVF_FILTER_MC);
1242 
1243 	return (!error);
1244 }
1245 
1246 /**
1247  * iavf_init_multi - Initialize multicast address filters
1248  * @sc: device private softc
1249  *
1250  * Called during initialization to reset multicast address filters to a known
1251  * fresh state by deleting all currently active filters.
1252  */
1253 void
1254 iavf_init_multi(struct iavf_sc *sc)
1255 {
1256 	struct iavf_mac_filter *f;
1257 	int mcnt = 0;
1258 
1259 	/* First clear any multicast filters */
1260 	SLIST_FOREACH(f, sc->mac_filters, next) {
1261 		if ((f->flags & IAVF_FILTER_USED)
1262 		    && (f->flags & IAVF_FILTER_MC)) {
1263 			f->flags |= IAVF_FILTER_DEL;
1264 			mcnt++;
1265 		}
1266 	}
1267 	if (mcnt > 0)
1268 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_MAC_FILTER);
1269 }
1270 
1271 /**
1272  * iavf_multi_set - Set multicast filters
1273  * @sc: device private softc
1274  *
1275  * Set multicast MAC filters for this device. If there are too many filters,
1276  * this will request the device to go into multicast promiscuous mode instead.
1277  */
1278 void
1279 iavf_multi_set(struct iavf_sc *sc)
1280 {
1281 	if_t ifp = sc->vsi.ifp;
1282 	int mcnt = 0;
1283 
1284 	IOCTL_DEBUGOUT("iavf_multi_set: begin");
1285 
1286 	mcnt = if_llmaddr_count(ifp);
1287 	if (__predict_false(mcnt == MAX_MULTICAST_ADDR)) {
1288 		/* Delete MC filters and enable mulitcast promisc instead */
1289 		iavf_init_multi(sc);
1290 		sc->promisc_flags |= FLAG_VF_MULTICAST_PROMISC;
1291 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_PROMISC);
1292 		return;
1293 	}
1294 
1295 	/* If there aren't too many filters, delete existing MC filters */
1296 	iavf_init_multi(sc);
1297 
1298 	/* And (re-)install filters for all mcast addresses */
1299 	mcnt = if_foreach_llmaddr(ifp, iavf_mc_filter_apply, sc);
1300 
1301 	if (mcnt > 0)
1302 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_MAC_FILTER);
1303 }
1304 
1305 /**
1306  * iavf_add_mac_filter - Add a MAC filter to the sc MAC list
1307  * @sc: device private softc
1308  * @macaddr: MAC address to add
1309  * @flags: filter flags
1310  *
1311  * Add a new MAC filter to the softc MAC filter list. These will later be sent
1312  * to the physical function (and ultimately hardware) via the virtchnl
1313  * interface.
1314  *
1315  * @returns zero on success, EEXIST if the filter already exists, and ENOMEM
1316  * if we ran out of memory allocating the filter structure.
1317  */
1318 int
1319 iavf_add_mac_filter(struct iavf_sc *sc, u8 *macaddr, u16 flags)
1320 {
1321 	struct iavf_mac_filter	*f;
1322 
1323 	/* Does one already exist? */
1324 	f = iavf_find_mac_filter(sc, macaddr);
1325 	if (f != NULL) {
1326 		iavf_dbg_filter(sc, "exists: " MAC_FORMAT "\n",
1327 		    MAC_FORMAT_ARGS(macaddr));
1328 		return (EEXIST);
1329 	}
1330 
1331 	/* If not, get a new empty filter */
1332 	f = iavf_get_mac_filter(sc);
1333 	if (f == NULL) {
1334 		device_printf(sc->dev, "%s: no filters available!!\n",
1335 		    __func__);
1336 		return (ENOMEM);
1337 	}
1338 
1339 	iavf_dbg_filter(sc, "marked: " MAC_FORMAT "\n",
1340 	    MAC_FORMAT_ARGS(macaddr));
1341 
1342 	bcopy(macaddr, f->macaddr, ETHER_ADDR_LEN);
1343 	f->flags |= (IAVF_FILTER_ADD | IAVF_FILTER_USED);
1344 	f->flags |= flags;
1345 	return (0);
1346 }
1347 
1348 /**
1349  * iavf_find_mac_filter - Find a MAC filter with the given address
1350  * @sc: device private softc
1351  * @macaddr: the MAC address to find
1352  *
1353  * Finds the filter structure in the MAC filter list with the corresponding
1354  * MAC address.
1355  *
1356  * @returns a pointer to the filter structure, or NULL if no such filter
1357  * exists in the list yet.
1358  */
1359 struct iavf_mac_filter *
1360 iavf_find_mac_filter(struct iavf_sc *sc, u8 *macaddr)
1361 {
1362 	struct iavf_mac_filter	*f;
1363 	bool match = FALSE;
1364 
1365 	SLIST_FOREACH(f, sc->mac_filters, next) {
1366 		if (cmp_etheraddr(f->macaddr, macaddr)) {
1367 			match = TRUE;
1368 			break;
1369 		}
1370 	}
1371 
1372 	if (!match)
1373 		f = NULL;
1374 	return (f);
1375 }
1376 
1377 /**
1378  * iavf_get_mac_filter - Get a new MAC address filter
1379  * @sc: device private softc
1380  *
1381  * Allocates a new filter structure and inserts it into the MAC filter list.
1382  *
1383  * @post the caller must fill in the structure details after calling this
1384  * function, but does not need to insert it into the linked list.
1385  *
1386  * @returns a pointer to the new filter structure, or NULL of we failed to
1387  * allocate it.
1388  */
1389 struct iavf_mac_filter *
1390 iavf_get_mac_filter(struct iavf_sc *sc)
1391 {
1392 	struct iavf_mac_filter *f;
1393 
1394 	f = (struct iavf_mac_filter *)malloc(sizeof(struct iavf_mac_filter),
1395 	    M_IAVF, M_NOWAIT | M_ZERO);
1396 	if (f)
1397 		SLIST_INSERT_HEAD(sc->mac_filters, f, next);
1398 
1399 	return (f);
1400 }
1401 
1402 /**
1403  * iavf_baudrate_from_link_speed - Convert link speed to baudrate
1404  * @sc: device private softc
1405  *
1406  * @post The link_speed_adv field is in Mbps, so it is multipled by
1407  * 1,000,000 before it's returned.
1408  *
1409  * @returns the adapter link speed in bits/sec
1410  */
1411 u64
1412 iavf_baudrate_from_link_speed(struct iavf_sc *sc)
1413 {
1414 	if (sc->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1415 		return (sc->link_speed_adv * IAVF_ADV_LINK_SPEED_SCALE);
1416 	else
1417 		return iavf_max_vc_speed_to_value(sc->link_speed);
1418 }
1419 
1420 /**
1421  * iavf_add_vlan_filter - Add a VLAN filter to the softc VLAN list
1422  * @sc: device private softc
1423  * @vtag: the VLAN id to filter
1424  *
1425  * Allocate a new VLAN filter structure and insert it into the VLAN list.
1426  */
1427 void
1428 iavf_add_vlan_filter(struct iavf_sc *sc, u16 vtag)
1429 {
1430 	struct iavf_vlan_filter	*v;
1431 
1432 	v = (struct iavf_vlan_filter *)malloc(sizeof(struct iavf_vlan_filter),
1433 	    M_IAVF, M_WAITOK | M_ZERO);
1434 	SLIST_INSERT_HEAD(sc->vlan_filters, v, next);
1435 	v->vlan = vtag;
1436 	v->flags = IAVF_FILTER_ADD;
1437 }
1438 
1439 /**
1440  * iavf_mark_del_vlan_filter - Mark a given VLAN id for deletion
1441  * @sc: device private softc
1442  * @vtag: the VLAN id to delete
1443  *
1444  * Marks all VLAN filters matching the given vtag for deletion.
1445  *
1446  * @returns the number of filters marked for deletion.
1447  *
1448  * @remark the filters are not removed immediately, but will be removed from
1449  * the list by another function that synchronizes over the virtchnl interface.
1450  */
1451 int
1452 iavf_mark_del_vlan_filter(struct iavf_sc *sc, u16 vtag)
1453 {
1454 	struct iavf_vlan_filter	*v;
1455 	int i = 0;
1456 
1457 	SLIST_FOREACH(v, sc->vlan_filters, next) {
1458 		if (v->vlan == vtag) {
1459 			v->flags = IAVF_FILTER_DEL;
1460 			++i;
1461 		}
1462 	}
1463 
1464 	return (i);
1465 }
1466 
1467 /**
1468  * iavf_update_msix_devinfo - Fix MSIX values for pci_msix_count()
1469  * @dev: pointer to kernel device
1470  *
1471  * Fix cached MSI-X control register information. This is a workaround
1472  * for an issue where VFs spawned in non-passthrough mode on FreeBSD
1473  * will have their PCI information cached before the PF driver
1474  * finishes updating their PCI information.
1475  *
1476  * @pre Must be called before pci_msix_count()
1477  */
1478 void
1479 iavf_update_msix_devinfo(device_t dev)
1480 {
1481 	struct pci_devinfo *dinfo;
1482 	u32 msix_ctrl;
1483 
1484 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
1485 	/* We can hardcode this offset since we know the device */
1486 	msix_ctrl = pci_read_config(dev, 0x70 + PCIR_MSIX_CTRL, 2);
1487 	dinfo->cfg.msix.msix_ctrl = msix_ctrl;
1488 	dinfo->cfg.msix.msix_msgnum = (msix_ctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1;
1489 }
1490 
1491 /**
1492  * iavf_disable_queues_with_retries - Send PF multiple DISABLE_QUEUES messages
1493  * @sc: device softc
1494  *
1495  * Send a virtual channel message to the PF to DISABLE_QUEUES, but resend it up
1496  * to IAVF_MAX_DIS_Q_RETRY times if the response says that it wasn't
1497  * successful. This is intended to workaround a bug that can appear on the PF.
1498  */
1499 void
1500 iavf_disable_queues_with_retries(struct iavf_sc *sc)
1501 {
1502 	bool in_detach = iavf_driver_is_detaching(sc);
1503 	int max_attempts = IAVF_MAX_DIS_Q_RETRY;
1504 	int msg_count = 0;
1505 
1506 	/* While the driver is detaching, it doesn't care if the queue
1507 	 * disable finishes successfully or not. Just send one message
1508 	 * to just notify the PF driver.
1509 	 */
1510 	if (in_detach)
1511 		max_attempts = 1;
1512 
1513 	while ((msg_count < max_attempts) &&
1514 	    atomic_load_acq_32(&sc->queues_enabled)) {
1515 		msg_count++;
1516 		iavf_send_vc_msg_sleep(sc, IAVF_FLAG_AQ_DISABLE_QUEUES);
1517 	}
1518 
1519 	/* Possibly print messages about retry attempts and issues */
1520 	if (msg_count > 1)
1521 		iavf_dbg_vc(sc, "DISABLE_QUEUES messages sent: %d\n",
1522 		    msg_count);
1523 
1524 	if (!in_detach && msg_count >= max_attempts)
1525 		device_printf(sc->dev, "%s: DISABLE_QUEUES may have failed\n",
1526 		    __func__);
1527 }
1528