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