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