1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright (c) 2020, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
34 * @file if_ice_iflib.c
35 * @brief iflib driver implementation
37 * Contains the main entry point for the iflib driver implementation. It
38 * implements the various ifdi driver methods, and sets up the module and
39 * driver values to load an iflib driver.
42 #include "ice_iflib.h"
43 #include "ice_drv_info.h"
44 #include "ice_switch.h"
45 #include "ice_sched.h"
47 #include <sys/module.h>
48 #include <sys/sockio.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
54 * Device method prototypes
57 static void *ice_register(device_t);
58 static int ice_if_attach_pre(if_ctx_t);
59 static int ice_attach_pre_recovery_mode(struct ice_softc *sc);
60 static int ice_if_attach_post(if_ctx_t);
61 static void ice_attach_post_recovery_mode(struct ice_softc *sc);
62 static int ice_if_detach(if_ctx_t);
63 static int ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
64 static int ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
65 static int ice_if_msix_intr_assign(if_ctx_t ctx, int msix);
66 static void ice_if_queues_free(if_ctx_t ctx);
67 static int ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
68 static void ice_if_intr_enable(if_ctx_t ctx);
69 static void ice_if_intr_disable(if_ctx_t ctx);
70 static int ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
71 static int ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
72 static int ice_if_promisc_set(if_ctx_t ctx, int flags);
73 static void ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
74 static int ice_if_media_change(if_ctx_t ctx);
75 static void ice_if_init(if_ctx_t ctx);
76 static void ice_if_timer(if_ctx_t ctx, uint16_t qid);
77 static void ice_if_update_admin_status(if_ctx_t ctx);
78 static void ice_if_multi_set(if_ctx_t ctx);
79 static void ice_if_vlan_register(if_ctx_t ctx, u16 vtag);
80 static void ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
81 static void ice_if_stop(if_ctx_t ctx);
82 static uint64_t ice_if_get_counter(if_ctx_t ctx, ift_counter counter);
83 static int ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
84 static int ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
86 static int ice_msix_que(void *arg);
87 static int ice_msix_admin(void *arg);
90 * Helper function prototypes
92 static int ice_pci_mapping(struct ice_softc *sc);
93 static void ice_free_pci_mapping(struct ice_softc *sc);
94 static void ice_update_link_status(struct ice_softc *sc, bool update_media);
95 static void ice_init_device_features(struct ice_softc *sc);
96 static void ice_init_tx_tracking(struct ice_vsi *vsi);
97 static void ice_handle_reset_event(struct ice_softc *sc);
98 static void ice_handle_pf_reset_request(struct ice_softc *sc);
99 static void ice_prepare_for_reset(struct ice_softc *sc);
100 static int ice_rebuild_pf_vsi_qmap(struct ice_softc *sc);
101 static void ice_rebuild(struct ice_softc *sc);
102 static void ice_rebuild_recovery_mode(struct ice_softc *sc);
103 static void ice_free_irqvs(struct ice_softc *sc);
104 static void ice_update_rx_mbuf_sz(struct ice_softc *sc);
105 static void ice_poll_for_media_avail(struct ice_softc *sc);
106 static void ice_setup_scctx(struct ice_softc *sc);
107 static int ice_allocate_msix(struct ice_softc *sc);
108 static void ice_admin_timer(void *arg);
109 static void ice_transition_recovery_mode(struct ice_softc *sc);
110 static void ice_transition_safe_mode(struct ice_softc *sc);
113 * Device Interface Declaration
118 * @brief ice driver method entry points
120 * List of device methods implementing the generic device interface used by
121 * the device stack to interact with the ice driver. Since this is an iflib
122 * driver, most of the methods point to the generic iflib implementation.
124 static device_method_t ice_methods[] = {
125 /* Device interface */
126 DEVMETHOD(device_register, ice_register),
127 DEVMETHOD(device_probe, iflib_device_probe_vendor),
128 DEVMETHOD(device_attach, iflib_device_attach),
129 DEVMETHOD(device_detach, iflib_device_detach),
130 DEVMETHOD(device_shutdown, iflib_device_shutdown),
131 DEVMETHOD(device_suspend, iflib_device_suspend),
132 DEVMETHOD(device_resume, iflib_device_resume),
137 * @var ice_iflib_methods
138 * @brief iflib method entry points
140 * List of device methods used by the iflib stack to interact with this
141 * driver. These are the real main entry points used to interact with this
144 static device_method_t ice_iflib_methods[] = {
145 DEVMETHOD(ifdi_attach_pre, ice_if_attach_pre),
146 DEVMETHOD(ifdi_attach_post, ice_if_attach_post),
147 DEVMETHOD(ifdi_detach, ice_if_detach),
148 DEVMETHOD(ifdi_tx_queues_alloc, ice_if_tx_queues_alloc),
149 DEVMETHOD(ifdi_rx_queues_alloc, ice_if_rx_queues_alloc),
150 DEVMETHOD(ifdi_msix_intr_assign, ice_if_msix_intr_assign),
151 DEVMETHOD(ifdi_queues_free, ice_if_queues_free),
152 DEVMETHOD(ifdi_mtu_set, ice_if_mtu_set),
153 DEVMETHOD(ifdi_intr_enable, ice_if_intr_enable),
154 DEVMETHOD(ifdi_intr_disable, ice_if_intr_disable),
155 DEVMETHOD(ifdi_rx_queue_intr_enable, ice_if_rx_queue_intr_enable),
156 DEVMETHOD(ifdi_tx_queue_intr_enable, ice_if_tx_queue_intr_enable),
157 DEVMETHOD(ifdi_promisc_set, ice_if_promisc_set),
158 DEVMETHOD(ifdi_media_status, ice_if_media_status),
159 DEVMETHOD(ifdi_media_change, ice_if_media_change),
160 DEVMETHOD(ifdi_init, ice_if_init),
161 DEVMETHOD(ifdi_stop, ice_if_stop),
162 DEVMETHOD(ifdi_timer, ice_if_timer),
163 DEVMETHOD(ifdi_update_admin_status, ice_if_update_admin_status),
164 DEVMETHOD(ifdi_multi_set, ice_if_multi_set),
165 DEVMETHOD(ifdi_vlan_register, ice_if_vlan_register),
166 DEVMETHOD(ifdi_vlan_unregister, ice_if_vlan_unregister),
167 DEVMETHOD(ifdi_get_counter, ice_if_get_counter),
168 DEVMETHOD(ifdi_priv_ioctl, ice_if_priv_ioctl),
169 DEVMETHOD(ifdi_i2c_req, ice_if_i2c_req),
175 * @brief driver structure for the generic device stack
177 * driver_t definition used to setup the generic device methods.
179 static driver_t ice_driver = {
181 .methods = ice_methods,
182 .size = sizeof(struct ice_softc),
186 * @var ice_iflib_driver
187 * @brief driver structure for the iflib stack
189 * driver_t definition used to setup the iflib device methods.
191 static driver_t ice_iflib_driver = {
193 .methods = ice_iflib_methods,
194 .size = sizeof(struct ice_softc),
197 extern struct if_txrx ice_txrx;
198 extern struct if_txrx ice_recovery_txrx;
202 * @brief ice driver shared context
204 * Structure defining shared values (context) that is used by all instances of
205 * the device. Primarily used to setup details about how the iflib stack
206 * should treat this driver. Also defines the default, minimum, and maximum
207 * number of descriptors in each ring.
209 static struct if_shared_ctx ice_sctx = {
210 .isc_magic = IFLIB_MAGIC,
211 .isc_q_align = PAGE_SIZE,
213 .isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
214 /* We could technically set this as high as ICE_MAX_DMA_SEG_SIZE, but
215 * that doesn't make sense since that would be larger than the maximum
216 * size of a single packet.
218 .isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
220 /* XXX: This is only used by iflib to ensure that
221 * scctx->isc_tx_tso_size_max + the VLAN header is a valid size.
223 .isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
224 /* XXX: This is used by iflib to set the number of segments in the TSO
225 * DMA tag. However, scctx->isc_tx_tso_segsize_max is used to set the
226 * related ifnet parameter.
228 .isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
230 .isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
231 .isc_rx_nsegments = ICE_MAX_RX_SEGS,
232 .isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
238 .isc_admin_intrcnt = 1,
239 .isc_vendor_info = ice_vendor_info_array,
240 .isc_driver_version = __DECONST(char *, ice_driver_version),
241 .isc_driver = &ice_iflib_driver,
244 * IFLIB_NEED_SCRATCH ensures that mbufs have scratch space available
245 * for hardware checksum offload
247 * IFLIB_TSO_INIT_IP ensures that the TSO packets have zeroed out the
248 * IP sum field, required by our hardware to calculate valid TSO
251 * IFLIB_ADMIN_ALWAYS_RUN ensures that the administrative task runs
252 * even when the interface is down.
254 * IFLIB_SKIP_MSIX allows the driver to handle allocating MSI-X
255 * vectors manually instead of relying on iflib code to do this.
257 .isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
258 IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX,
260 .isc_nrxd_min = {ICE_MIN_DESC_COUNT},
261 .isc_ntxd_min = {ICE_MIN_DESC_COUNT},
262 .isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
263 .isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
264 .isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
265 .isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
270 * @brief ice driver device class
272 * device class used to setup the ice driver module kobject class.
274 devclass_t ice_devclass;
275 DRIVER_MODULE(ice, pci, ice_driver, ice_devclass, ice_module_event_handler, 0);
277 MODULE_VERSION(ice, 1);
278 MODULE_DEPEND(ice, pci, 1, 1, 1);
279 MODULE_DEPEND(ice, ether, 1, 1, 1);
280 MODULE_DEPEND(ice, iflib, 1, 1, 1);
281 MODULE_DEPEND(ice, firmware, 1, 1, 1);
283 IFLIB_PNP_INFO(pci, ice, ice_vendor_info_array);
285 /* Static driver-wide sysctls */
286 #include "ice_iflib_sysctls.h"
289 * ice_pci_mapping - Map PCI BAR memory
290 * @sc: device private softc
292 * Map PCI BAR 0 for device operation.
295 ice_pci_mapping(struct ice_softc *sc)
300 rc = ice_map_bar(sc->dev, &sc->bar0, 0);
308 * ice_free_pci_mapping - Release PCI BAR memory
309 * @sc: device private softc
311 * Release PCI BARs which were previously mapped by ice_pci_mapping().
314 ice_free_pci_mapping(struct ice_softc *sc)
317 ice_free_bar(sc->dev, &sc->bar0);
325 * ice_register - register device method callback
326 * @dev: the device being registered
328 * Returns a pointer to the shared context structure, which is used by iflib.
331 ice_register(device_t dev __unused)
337 * ice_setup_scctx - Setup the iflib softc context structure
338 * @sc: the device private structure
340 * Setup the parameters in if_softc_ctx_t structure used by the iflib stack
344 ice_setup_scctx(struct ice_softc *sc)
346 if_softc_ctx_t scctx = sc->scctx;
347 struct ice_hw *hw = &sc->hw;
348 bool safe_mode, recovery_mode;
350 safe_mode = ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE);
351 recovery_mode = ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE);
354 * If the driver loads in Safe mode or Recovery mode, limit iflib to
355 * a single queue pair.
357 if (safe_mode || recovery_mode) {
358 scctx->isc_ntxqsets = scctx->isc_nrxqsets = 1;
359 scctx->isc_ntxqsets_max = 1;
360 scctx->isc_nrxqsets_max = 1;
363 * iflib initially sets the isc_ntxqsets and isc_nrxqsets to
364 * the values of the override sysctls. Cache these initial
365 * values so that the driver can be aware of what the iflib
366 * sysctl value is when setting up MSI-X vectors.
368 sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets;
369 sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets;
371 if (scctx->isc_ntxqsets == 0)
372 scctx->isc_ntxqsets = hw->func_caps.common_cap.rss_table_size;
373 if (scctx->isc_nrxqsets == 0)
374 scctx->isc_nrxqsets = hw->func_caps.common_cap.rss_table_size;
376 scctx->isc_ntxqsets_max = hw->func_caps.common_cap.num_txq;
377 scctx->isc_nrxqsets_max = hw->func_caps.common_cap.num_rxq;
380 * Sanity check that the iflib sysctl values are within the
381 * maximum supported range.
383 if (sc->ifc_sysctl_ntxqs > scctx->isc_ntxqsets_max)
384 sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets_max;
385 if (sc->ifc_sysctl_nrxqs > scctx->isc_nrxqsets_max)
386 sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets_max;
389 scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
390 * sizeof(struct ice_tx_desc), DBA_ALIGN);
391 scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
392 * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
394 scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
395 scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
396 scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
397 scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
399 scctx->isc_msix_bar = PCIR_BAR(ICE_MSIX_BAR);
400 scctx->isc_rss_table_size = hw->func_caps.common_cap.rss_table_size;
403 * If the driver loads in recovery mode, disable Tx/Rx functionality
406 scctx->isc_txrx = &ice_recovery_txrx;
408 scctx->isc_txrx = &ice_txrx;
411 * If the driver loads in Safe mode or Recovery mode, disable
412 * advanced features including hardware offloads.
414 if (safe_mode || recovery_mode) {
415 scctx->isc_capenable = ICE_SAFE_CAPS;
416 scctx->isc_tx_csum_flags = 0;
418 scctx->isc_capenable = ICE_FULL_CAPS;
419 scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
422 scctx->isc_capabilities = scctx->isc_capenable;
423 } /* ice_setup_scctx */
426 * ice_if_attach_pre - Early device attach logic
427 * @ctx: the iflib context structure
429 * Called by iflib during the attach process. Earliest main driver entry
430 * point which performs necessary hardware and driver initialization. Called
431 * before the Tx and Rx queues are allocated.
434 ice_if_attach_pre(if_ctx_t ctx)
436 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
437 enum ice_fw_modes fw_mode;
438 enum ice_status status;
439 if_softc_ctx_t scctx;
444 device_printf(iflib_get_dev(ctx), "Loading the iflib ice driver\n");
447 sc->media = iflib_get_media(ctx);
448 sc->sctx = iflib_get_sctx(ctx);
449 sc->iflib_ctx_lock = iflib_ctx_lock_get(ctx);
451 dev = sc->dev = iflib_get_dev(ctx);
452 scctx = sc->scctx = iflib_get_softc_ctx(ctx);
457 snprintf(sc->admin_mtx_name, sizeof(sc->admin_mtx_name),
458 "%s:admin", device_get_nameunit(dev));
459 mtx_init(&sc->admin_mtx, sc->admin_mtx_name, NULL, MTX_DEF);
460 callout_init_mtx(&sc->admin_timer, &sc->admin_mtx, 0);
462 ASSERT_CTX_LOCKED(sc);
464 if (ice_pci_mapping(sc)) {
466 goto destroy_admin_timer;
469 /* Save off the PCI information */
470 ice_save_pci_info(hw, dev);
472 /* create tunables as early as possible */
473 ice_add_device_tunables(sc);
475 /* Setup ControlQ lengths */
476 ice_set_ctrlq_len(hw);
478 fw_mode = ice_get_fw_mode(hw);
479 if (fw_mode == ICE_FW_MODE_REC) {
480 device_printf(dev, "Firmware recovery mode detected. Limiting functionality. Refer to Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
482 err = ice_attach_pre_recovery_mode(sc);
484 goto free_pci_mapping;
489 /* Initialize the hw data structure */
490 status = ice_init_hw(hw);
492 if (status == ICE_ERR_FW_API_VER) {
493 /* Enter recovery mode, so that the driver remains
494 * loaded. This way, if the system administrator
495 * cannot update the driver, they may still attempt to
498 err = ice_attach_pre_recovery_mode(sc);
500 goto free_pci_mapping;
505 device_printf(dev, "Unable to initialize hw, err %s aq_err %s\n",
506 ice_status_str(status),
507 ice_aq_str(hw->adminq.sq_last_status));
509 goto free_pci_mapping;
512 /* Notify firmware of the device driver version */
513 err = ice_send_version(sc);
517 ice_load_pkg_file(sc);
519 err = ice_init_link_events(sc);
521 device_printf(dev, "ice_init_link_events failed: %s\n",
526 ice_print_nvm_version(sc);
528 ice_init_device_features(sc);
530 /* Setup the MAC address */
531 iflib_set_mac(ctx, hw->port_info->mac.lan_addr);
533 /* Setup the iflib softc context structure */
536 /* Initialize the Tx queue manager */
537 err = ice_resmgr_init(&sc->tx_qmgr, hw->func_caps.common_cap.num_txq);
539 device_printf(dev, "Unable to initialize Tx queue manager: %s\n",
544 /* Initialize the Rx queue manager */
545 err = ice_resmgr_init(&sc->rx_qmgr, hw->func_caps.common_cap.num_rxq);
547 device_printf(dev, "Unable to initialize Rx queue manager: %s\n",
552 /* Initialize the interrupt resource manager */
553 err = ice_alloc_intr_tracking(sc);
555 /* Errors are already printed */
558 /* Determine maximum number of VSIs we'll prepare for */
559 sc->num_available_vsi = min(ICE_MAX_VSI_AVAILABLE,
560 hw->func_caps.guar_num_vsi);
562 if (!sc->num_available_vsi) {
564 device_printf(dev, "No VSIs allocated to host\n");
565 goto free_intr_tracking;
568 /* Allocate storage for the VSI pointers */
569 sc->all_vsi = (struct ice_vsi **)
570 malloc(sizeof(struct ice_vsi *) * sc->num_available_vsi,
571 M_ICE, M_WAITOK | M_ZERO);
574 device_printf(dev, "Unable to allocate VSI array\n");
575 goto free_intr_tracking;
579 * Prepare the statically allocated primary PF VSI in the softc
580 * structure. Other VSIs will be dynamically allocated as needed.
582 ice_setup_pf_vsi(sc);
584 err = ice_alloc_vsi_qmap(&sc->pf_vsi, scctx->isc_ntxqsets_max,
585 scctx->isc_nrxqsets_max);
587 device_printf(dev, "Unable to allocate VSI Queue maps\n");
591 /* Allocate MSI-X vectors (due to isc_flags IFLIB_SKIP_MSIX) */
592 err = ice_allocate_msix(sc);
599 /* ice_release_vsi will free the queue maps if they were allocated */
600 ice_release_vsi(&sc->pf_vsi);
601 free(sc->all_vsi, M_ICE);
604 ice_free_intr_tracking(sc);
606 ice_resmgr_destroy(&sc->rx_qmgr);
608 ice_resmgr_destroy(&sc->tx_qmgr);
612 ice_free_pci_mapping(sc);
614 mtx_lock(&sc->admin_mtx);
615 callout_stop(&sc->admin_timer);
616 mtx_unlock(&sc->admin_mtx);
617 mtx_destroy(&sc->admin_mtx);
619 } /* ice_if_attach_pre */
622 * ice_attach_pre_recovery_mode - Limited driver attach_pre for FW recovery
623 * @sc: the device private softc
625 * Loads the device driver in limited Firmware Recovery mode, intended to
626 * allow users to update the firmware to attempt to recover the device.
628 * @remark We may enter recovery mode in case either (a) the firmware is
629 * detected to be in an invalid state and must be re-programmed, or (b) the
630 * driver detects that the loaded firmware has a non-compatible API version
631 * that the driver cannot operate with.
634 ice_attach_pre_recovery_mode(struct ice_softc *sc)
636 ice_set_state(&sc->state, ICE_STATE_RECOVERY_MODE);
638 /* Setup the iflib softc context */
641 /* Setup the PF VSI back pointer */
645 * We still need to allocate MSI-X vectors since we need one vector to
646 * run the administrative admin interrupt
648 return ice_allocate_msix(sc);
652 * ice_update_link_status - notify OS of link state change
653 * @sc: device private softc structure
654 * @update_media: true if we should update media even if link didn't change
656 * Called to notify iflib core of link status changes. Should be called once
657 * during attach_post, and whenever link status changes during runtime.
659 * This call only updates the currently supported media types if the link
660 * status changed, or if update_media is set to true.
663 ice_update_link_status(struct ice_softc *sc, bool update_media)
665 struct ice_hw *hw = &sc->hw;
667 /* Never report link up when in recovery mode */
668 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
671 /* Report link status to iflib only once each time it changes */
672 if (!ice_testandset_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED)) {
673 if (sc->link_up) { /* link is up */
674 uint64_t baudrate = ice_aq_speed_to_rate(sc->hw.port_info);
676 iflib_link_state_change(sc->ctx, LINK_STATE_UP, baudrate);
681 } else { /* link is down */
682 iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
688 /* Update the supported media types */
690 enum ice_status status = ice_add_media_types(sc, sc->media);
692 device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
693 ice_status_str(status),
694 ice_aq_str(hw->adminq.sq_last_status));
697 /* TODO: notify VFs of link state change */
701 * ice_if_attach_post - Late device attach logic
702 * @ctx: the iflib context structure
704 * Called by iflib to finish up attaching the device. Performs any attach
705 * logic which must wait until after the Tx and Rx queues have been
709 ice_if_attach_post(if_ctx_t ctx)
711 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
712 if_t ifp = iflib_get_ifp(ctx);
715 ASSERT_CTX_LOCKED(sc);
717 /* We don't yet support loading if MSI-X is not supported */
718 if (sc->scctx->isc_intr != IFLIB_INTR_MSIX) {
719 device_printf(sc->dev, "The ice driver does not support loading without MSI-X\n");
723 /* The ifnet structure hasn't yet been initialized when the attach_pre
724 * handler is called, so wait until attach_post to setup the
725 * isc_max_frame_size.
729 sc->scctx->isc_max_frame_size = ifp->if_mtu +
730 ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
733 * If we are in recovery mode, only perform a limited subset of
734 * initialization to support NVM recovery.
736 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
737 ice_attach_post_recovery_mode(sc);
741 sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
743 err = ice_initialize_vsi(&sc->pf_vsi);
745 device_printf(sc->dev, "Unable to initialize Main VSI: %s\n",
750 /* Configure the main PF VSI for RSS */
751 err = ice_config_rss(&sc->pf_vsi);
753 device_printf(sc->dev,
754 "Unable to configure RSS for the main VSI, err %s\n",
759 /* Configure switch to drop transmitted LLDP and PAUSE frames */
760 err = ice_cfg_pf_ethertype_filters(sc);
764 ice_get_and_print_bus_info(sc);
766 ice_set_link_management_mode(sc);
768 ice_init_saved_phy_cfg(sc);
770 ice_add_device_sysctls(sc);
772 /* Get DCBX/LLDP state and start DCBX agent */
773 ice_init_dcb_setup(sc);
775 /* Setup link configuration parameters */
776 ice_init_link_configuration(sc);
777 ice_update_link_status(sc, true);
779 /* Configure interrupt causes for the administrative interrupt */
780 ice_configure_misc_interrupts(sc);
782 /* Enable ITR 0 right away, so that we can handle admin interrupts */
783 ice_enable_intr(&sc->hw, sc->irqvs[0].me);
785 /* Start the admin timer */
786 mtx_lock(&sc->admin_mtx);
787 callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
788 mtx_unlock(&sc->admin_mtx);
791 } /* ice_if_attach_post */
794 * ice_attach_post_recovery_mode - Limited driver attach_post for FW recovery
795 * @sc: the device private softc
797 * Performs minimal work to prepare the driver to recover an NVM in case the
798 * firmware is in recovery mode.
801 ice_attach_post_recovery_mode(struct ice_softc *sc)
803 /* Configure interrupt causes for the administrative interrupt */
804 ice_configure_misc_interrupts(sc);
806 /* Enable ITR 0 right away, so that we can handle admin interrupts */
807 ice_enable_intr(&sc->hw, sc->irqvs[0].me);
809 /* Start the admin timer */
810 mtx_lock(&sc->admin_mtx);
811 callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
812 mtx_unlock(&sc->admin_mtx);
816 * ice_free_irqvs - Free IRQ vector memory
817 * @sc: the device private softc structure
819 * Free IRQ vector memory allocated during ice_if_msix_intr_assign.
822 ice_free_irqvs(struct ice_softc *sc)
824 struct ice_vsi *vsi = &sc->pf_vsi;
825 if_ctx_t ctx = sc->ctx;
828 /* If the irqvs array is NULL, then there are no vectors to free */
829 if (sc->irqvs == NULL)
832 /* Free the IRQ vectors */
833 for (i = 0; i < sc->num_irq_vectors; i++)
834 iflib_irq_free(ctx, &sc->irqvs[i].irq);
836 /* Clear the irqv pointers */
837 for (i = 0; i < vsi->num_rx_queues; i++)
838 vsi->rx_queues[i].irqv = NULL;
840 for (i = 0; i < vsi->num_tx_queues; i++)
841 vsi->tx_queues[i].irqv = NULL;
843 /* Release the vector array memory */
844 free(sc->irqvs, M_ICE);
846 sc->num_irq_vectors = 0;
850 * ice_if_detach - Device driver detach logic
851 * @ctx: iflib context structure
853 * Perform device shutdown logic to detach the device driver.
855 * Note that there is no guarantee of the ordering of ice_if_queues_free() and
856 * ice_if_detach(). It is possible for the functions to be called in either
857 * order, and they must not assume to have a strict ordering.
860 ice_if_detach(if_ctx_t ctx)
862 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
863 struct ice_vsi *vsi = &sc->pf_vsi;
866 ASSERT_CTX_LOCKED(sc);
868 /* Indicate that we're detaching */
869 ice_set_state(&sc->state, ICE_STATE_DETACHING);
871 /* Stop the admin timer */
872 mtx_lock(&sc->admin_mtx);
873 callout_stop(&sc->admin_timer);
874 mtx_unlock(&sc->admin_mtx);
875 mtx_destroy(&sc->admin_mtx);
877 /* Free allocated media types */
878 ifmedia_removeall(sc->media);
880 /* Free the Tx and Rx sysctl contexts, and assign NULL to the node
881 * pointers. Note, the calls here and those in ice_if_queues_free()
882 * are *BOTH* necessary, as we cannot guarantee which path will be
885 ice_vsi_del_txqs_ctx(vsi);
886 ice_vsi_del_rxqs_ctx(vsi);
888 /* Release MSI-X resources */
891 for (i = 0; i < sc->num_available_vsi; i++) {
893 ice_release_vsi(sc->all_vsi[i]);
897 free(sc->all_vsi, M_ICE);
901 /* Release MSI-X memory */
902 pci_release_msi(sc->dev);
904 if (sc->msix_table != NULL) {
905 bus_release_resource(sc->dev, SYS_RES_MEMORY,
906 rman_get_rid(sc->msix_table),
908 sc->msix_table = NULL;
911 ice_free_intr_tracking(sc);
913 /* Destroy the queue managers */
914 ice_resmgr_destroy(&sc->tx_qmgr);
915 ice_resmgr_destroy(&sc->rx_qmgr);
917 if (!ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
918 ice_deinit_hw(&sc->hw);
920 ice_free_pci_mapping(sc);
923 } /* ice_if_detach */
926 * ice_if_tx_queues_alloc - Allocate Tx queue memory
927 * @ctx: iflib context structure
928 * @vaddrs: virtual addresses for the queue memory
929 * @paddrs: physical addresses for the queue memory
930 * @ntxqs: the number of Tx queues per set (should always be 1)
931 * @ntxqsets: the number of Tx queue sets to allocate
933 * Called by iflib to allocate Tx queues for the device. Allocates driver
934 * memory to track each queue, the status arrays used for descriptor
935 * status reporting, and Tx queue sysctls.
938 ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
939 int __invariant_only ntxqs, int ntxqsets)
941 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
942 struct ice_vsi *vsi = &sc->pf_vsi;
943 struct ice_tx_queue *txq;
947 MPASS(sc->scctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
948 ASSERT_CTX_LOCKED(sc);
950 /* Do not bother allocating queues if we're in recovery mode */
951 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
954 /* Allocate queue structure memory */
955 if (!(vsi->tx_queues =
956 (struct ice_tx_queue *) malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_WAITOK | M_ZERO))) {
957 device_printf(sc->dev, "Unable to allocate Tx queue memory\n");
961 /* Allocate report status arrays */
962 for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
964 (uint16_t *) malloc(sizeof(uint16_t) * sc->scctx->isc_ntxd[0], M_ICE, M_WAITOK))) {
965 device_printf(sc->dev, "Unable to allocate tx_rsq memory\n");
969 /* Initialize report status array */
970 for (j = 0; j < sc->scctx->isc_ntxd[0]; j++)
971 txq->tx_rsq[j] = QIDX_INVALID;
974 /* Assign queues from PF space to the main VSI */
975 err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap, ntxqsets);
977 device_printf(sc->dev, "Unable to assign PF queues: %s\n",
981 vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
983 /* Add Tx queue sysctls context */
984 ice_vsi_add_txqs_ctx(vsi);
986 for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
990 /* store the queue size for easier access */
991 txq->desc_count = sc->scctx->isc_ntxd[0];
993 /* get the virtual and physical address of the hardware queues */
994 txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
995 txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
996 txq->tx_paddr = paddrs[i];
998 ice_add_txq_sysctls(txq);
1001 vsi->num_tx_queues = ntxqsets;
1006 for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
1007 if (txq->tx_rsq != NULL) {
1008 free(txq->tx_rsq, M_ICE);
1012 free(vsi->tx_queues, M_ICE);
1013 vsi->tx_queues = NULL;
1018 * ice_if_rx_queues_alloc - Allocate Rx queue memory
1019 * @ctx: iflib context structure
1020 * @vaddrs: virtual addresses for the queue memory
1021 * @paddrs: physical addresses for the queue memory
1022 * @nrxqs: number of Rx queues per set (should always be 1)
1023 * @nrxqsets: number of Rx queue sets to allocate
1025 * Called by iflib to allocate Rx queues for the device. Allocates driver
1026 * memory to track each queue, as well as sets up the Rx queue sysctls.
1029 ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
1030 int __invariant_only nrxqs, int nrxqsets)
1032 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1033 struct ice_vsi *vsi = &sc->pf_vsi;
1034 struct ice_rx_queue *rxq;
1038 MPASS(sc->scctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
1039 ASSERT_CTX_LOCKED(sc);
1041 /* Do not bother allocating queues if we're in recovery mode */
1042 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1045 /* Allocate queue structure memory */
1046 if (!(vsi->rx_queues =
1047 (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_WAITOK | M_ZERO))) {
1048 device_printf(sc->dev, "Unable to allocate Rx queue memory\n");
1052 /* Assign queues from PF space to the main VSI */
1053 err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap, nrxqsets);
1055 device_printf(sc->dev, "Unable to assign PF queues: %s\n",
1057 goto free_rx_queues;
1059 vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
1061 /* Add Rx queue sysctls context */
1062 ice_vsi_add_rxqs_ctx(vsi);
1064 for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
1068 /* store the queue size for easier access */
1069 rxq->desc_count = sc->scctx->isc_nrxd[0];
1071 /* get the virtual and physical address of the hardware queues */
1072 rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
1073 rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
1074 rxq->rx_paddr = paddrs[i];
1076 ice_add_rxq_sysctls(rxq);
1079 vsi->num_rx_queues = nrxqsets;
1084 free(vsi->rx_queues, M_ICE);
1085 vsi->rx_queues = NULL;
1090 * ice_if_queues_free - Free queue memory
1091 * @ctx: the iflib context structure
1093 * Free queue memory allocated by ice_if_tx_queues_alloc() and
1094 * ice_if_rx_queues_alloc().
1096 * There is no guarantee that ice_if_queues_free() and ice_if_detach() will be
1097 * called in the same order. It's possible for ice_if_queues_free() to be
1098 * called prior to ice_if_detach(), and vice versa.
1100 * For this reason, the main VSI is a static member of the ice_softc, which is
1101 * not free'd until after iflib finishes calling both of these functions.
1103 * Thus, care must be taken in how we manage the memory being freed by this
1104 * function, and in what tasks it can and must perform.
1107 ice_if_queues_free(if_ctx_t ctx)
1109 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1110 struct ice_vsi *vsi = &sc->pf_vsi;
1111 struct ice_tx_queue *txq;
1114 /* Free the Tx and Rx sysctl contexts, and assign NULL to the node
1115 * pointers. Note, the calls here and those in ice_if_detach()
1116 * are *BOTH* necessary, as we cannot guarantee which path will be
1119 ice_vsi_del_txqs_ctx(vsi);
1120 ice_vsi_del_rxqs_ctx(vsi);
1122 /* Release MSI-X IRQ vectors, if not yet released in ice_if_detach */
1125 if (vsi->tx_queues != NULL) {
1126 /* free the tx_rsq arrays */
1127 for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
1128 if (txq->tx_rsq != NULL) {
1129 free(txq->tx_rsq, M_ICE);
1133 free(vsi->tx_queues, M_ICE);
1134 vsi->tx_queues = NULL;
1135 vsi->num_tx_queues = 0;
1137 if (vsi->rx_queues != NULL) {
1138 free(vsi->rx_queues, M_ICE);
1139 vsi->rx_queues = NULL;
1140 vsi->num_rx_queues = 0;
1145 * ice_msix_que - Fast interrupt handler for MSI-X receive queues
1146 * @arg: The Rx queue memory
1148 * Interrupt filter function for iflib MSI-X interrupts. Called by iflib when
1149 * an MSI-X interrupt for a given queue is triggered. Currently this just asks
1150 * iflib to schedule the main Rx thread.
1153 ice_msix_que(void *arg)
1155 struct ice_rx_queue __unused *rxq = (struct ice_rx_queue *)arg;
1157 /* TODO: dynamic ITR algorithm?? */
1159 return (FILTER_SCHEDULE_THREAD);
1163 * ice_msix_admin - Fast interrupt handler for MSI-X admin interrupt
1164 * @arg: pointer to device softc memory
1166 * Called by iflib when an administrative interrupt occurs. Should perform any
1167 * fast logic for handling the interrupt cause, and then indicate whether the
1168 * admin task needs to be queued.
1171 ice_msix_admin(void *arg)
1173 struct ice_softc *sc = (struct ice_softc *)arg;
1174 struct ice_hw *hw = &sc->hw;
1175 device_t dev = sc->dev;
1178 /* There is no safe way to modify the enabled miscellaneous causes of
1179 * the OICR vector at runtime, as doing so would be prone to race
1180 * conditions. Reading PFINT_OICR will unmask the associated interrupt
1181 * causes and allow future interrupts to occur. The admin interrupt
1182 * vector will not be re-enabled until after we exit this function,
1183 * but any delayed tasks must be resilient against possible "late
1184 * arrival" interrupts that occur while we're already handling the
1185 * task. This is done by using state bits and serializing these
1186 * delayed tasks via the admin status task function.
1188 oicr = rd32(hw, PFINT_OICR);
1190 /* Processing multiple controlq interrupts on a single vector does not
1191 * provide an indication of which controlq triggered the interrupt.
1192 * We might try reading the INTEVENT bit of the respective PFINT_*_CTL
1193 * registers. However, the INTEVENT bit is not guaranteed to be set as
1194 * it gets automatically cleared when the hardware acknowledges the
1197 * This means we don't really have a good indication of whether or
1198 * which controlq triggered this interrupt. We'll just notify the
1199 * admin task that it should check all the controlqs.
1201 ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
1203 if (oicr & PFINT_OICR_VFLR_M) {
1204 ice_set_state(&sc->state, ICE_STATE_VFLR_PENDING);
1207 if (oicr & PFINT_OICR_MAL_DETECT_M) {
1208 ice_set_state(&sc->state, ICE_STATE_MDD_PENDING);
1211 if (oicr & PFINT_OICR_GRST_M) {
1214 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1215 GLGEN_RSTAT_RESET_TYPE_S;
1217 if (reset == ICE_RESET_CORER)
1218 sc->soft_stats.corer_count++;
1219 else if (reset == ICE_RESET_GLOBR)
1220 sc->soft_stats.globr_count++;
1222 sc->soft_stats.empr_count++;
1224 /* There are a couple of bits at play for handling resets.
1225 * First, the ICE_STATE_RESET_OICR_RECV bit is used to
1226 * indicate that the driver has received an OICR with a reset
1227 * bit active, indicating that a CORER/GLOBR/EMPR is about to
1228 * happen. Second, we set hw->reset_ongoing to indicate that
1229 * the hardware is in reset. We will set this back to false as
1230 * soon as the driver has determined that the hardware is out
1233 * If the driver wishes to trigger a reqest, it can set one of
1234 * the ICE_STATE_RESET_*_REQ bits, which will trigger the
1235 * correct type of reset.
1237 if (!ice_testandset_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
1238 hw->reset_ongoing = true;
1241 if (oicr & PFINT_OICR_ECC_ERR_M) {
1242 device_printf(dev, "ECC Error detected!\n");
1243 ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
1246 if (oicr & PFINT_OICR_PE_CRITERR_M) {
1247 device_printf(dev, "Critical Protocol Engine Error detected!\n");
1248 ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
1251 if (oicr & PFINT_OICR_PCI_EXCEPTION_M) {
1252 device_printf(dev, "PCI Exception detected!\n");
1253 ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
1256 if (oicr & PFINT_OICR_HMC_ERR_M) {
1257 /* Log the HMC errors, but don't disable the interrupt cause */
1258 ice_log_hmc_error(hw, dev);
1261 return (FILTER_SCHEDULE_THREAD);
1265 * ice_allocate_msix - Allocate MSI-X vectors for the interface
1266 * @sc: the device private softc
1268 * Map the MSI-X bar, and then request MSI-X vectors in a two-stage process.
1270 * First, determine a suitable total number of vectors based on the number
1271 * of CPUs, RSS buckets, the administrative vector, and other demands such as
1274 * Request the desired amount of vectors, and see how many we obtain. If we
1275 * don't obtain as many as desired, reduce the demands by lowering the number
1276 * of requested queues or reducing the demand from other features such as
1279 * @remark This function is required because the driver sets the
1280 * IFLIB_SKIP_MSIX flag indicating that the driver will manage MSI-X vectors
1283 * @remark This driver will only use MSI-X vectors. If this is not possible,
1284 * neither MSI or legacy interrupts will be tried.
1286 * @post on success this function must set the following scctx parameters:
1287 * isc_vectors, isc_nrxqsets, isc_ntxqsets, and isc_intr.
1289 * @returns zero on success or an error code on failure.
1292 ice_allocate_msix(struct ice_softc *sc)
1294 bool iflib_override_queue_count = false;
1295 if_softc_ctx_t scctx = sc->scctx;
1296 device_t dev = sc->dev;
1298 int bar, queues, vectors, requested;
1301 /* Allocate the MSI-X bar */
1302 bar = scctx->isc_msix_bar;
1303 sc->msix_table = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar, RF_ACTIVE);
1304 if (!sc->msix_table) {
1305 device_printf(dev, "Unable to map MSI-X table\n");
1309 /* Check if the iflib queue count sysctls have been set */
1310 if (sc->ifc_sysctl_ntxqs || sc->ifc_sysctl_nrxqs)
1311 iflib_override_queue_count = true;
1313 err = bus_get_cpus(dev, INTR_CPUS, sizeof(cpus), &cpus);
1315 device_printf(dev, "%s: Unable to fetch the CPU list: %s\n",
1316 __func__, ice_err_str(err));
1317 CPU_COPY(&all_cpus, &cpus);
1320 /* Attempt to mimic behavior of iflib_msix_init */
1321 if (iflib_override_queue_count) {
1323 * If the override sysctls have been set, limit the queues to
1324 * the number of logical CPUs.
1329 * Otherwise, limit the queue count to the CPUs associated
1330 * with the NUMA node the device is associated with.
1332 queues = CPU_COUNT(&cpus);
1335 /* Clamp to the number of RSS buckets */
1336 queues = imin(queues, rss_getnumbuckets());
1339 * Clamp the number of queue pairs to the minimum of the requested Tx
1342 queues = imin(queues, sc->ifc_sysctl_ntxqs ?: scctx->isc_ntxqsets);
1343 queues = imin(queues, sc->ifc_sysctl_nrxqs ?: scctx->isc_nrxqsets);
1346 * Determine the number of vectors to request. Note that we also need
1347 * to allocate one vector for administrative tasks.
1349 requested = queues + 1;
1351 vectors = requested;
1353 err = pci_alloc_msix(dev, &vectors);
1355 device_printf(dev, "Failed to allocate %d MSI-X vectors, err %s\n",
1356 vectors, ice_err_str(err));
1357 goto err_free_msix_table;
1360 /* If we don't receive enough vectors, reduce demands */
1361 if (vectors < requested) {
1362 int diff = requested - vectors;
1364 device_printf(dev, "Requested %d MSI-X vectors, but got only %d\n",
1365 requested, vectors);
1368 * If we still have a difference, we need to reduce the number
1371 * However, we still need at least one vector for the admin
1372 * interrupt and one queue pair.
1374 if (queues <= diff) {
1375 device_printf(dev, "Unable to allocate sufficient MSI-X vectors\n");
1377 goto err_pci_release_msi;
1383 device_printf(dev, "Using %d Tx and Rx queues\n", queues);
1384 device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
1387 scctx->isc_vectors = vectors;
1388 scctx->isc_nrxqsets = queues;
1389 scctx->isc_ntxqsets = queues;
1390 scctx->isc_intr = IFLIB_INTR_MSIX;
1392 /* Interrupt allocation tracking isn't required in recovery mode,
1393 * since neither RDMA nor VFs are enabled.
1395 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1398 /* Keep track of which interrupt indices are being used for what */
1399 sc->lan_vectors = vectors;
1400 err = ice_resmgr_assign_contiguous(&sc->imgr, sc->pf_imap, sc->lan_vectors);
1402 device_printf(dev, "Unable to assign PF interrupt mapping: %s\n",
1404 goto err_pci_release_msi;
1409 err_pci_release_msi:
1410 pci_release_msi(dev);
1411 err_free_msix_table:
1412 if (sc->msix_table != NULL) {
1413 bus_release_resource(sc->dev, SYS_RES_MEMORY,
1414 rman_get_rid(sc->msix_table),
1416 sc->msix_table = NULL;
1423 * ice_if_msix_intr_assign - Assign MSI-X interrupt vectors to queues
1424 * @ctx: the iflib context structure
1425 * @msix: the number of vectors we were assigned
1427 * Called by iflib to assign MSI-X vectors to queues. Currently requires that
1428 * we get at least the same number of vectors as we have queues, and that we
1429 * always have the same number of Tx and Rx queues.
1431 * Tx queues use a softirq instead of using their own hardware interrupt.
1434 ice_if_msix_intr_assign(if_ctx_t ctx, int msix)
1436 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1437 struct ice_vsi *vsi = &sc->pf_vsi;
1440 ASSERT_CTX_LOCKED(sc);
1442 if (vsi->num_rx_queues != vsi->num_tx_queues) {
1443 device_printf(sc->dev,
1444 "iflib requested %d Tx queues, and %d Rx queues, but the driver isn't able to support a differing number of Tx and Rx queues\n",
1445 vsi->num_tx_queues, vsi->num_rx_queues);
1446 return (EOPNOTSUPP);
1449 if (msix < (vsi->num_rx_queues + 1)) {
1450 device_printf(sc->dev,
1451 "Not enough MSI-X vectors to assign one vector to each queue pair\n");
1452 return (EOPNOTSUPP);
1455 /* Save the number of vectors for future use */
1456 sc->num_irq_vectors = vsi->num_rx_queues + 1;
1458 /* Allocate space to store the IRQ vector data */
1460 (struct ice_irq_vector *) malloc(sizeof(struct ice_irq_vector) * (sc->num_irq_vectors),
1461 M_ICE, M_NOWAIT))) {
1462 device_printf(sc->dev,
1463 "Unable to allocate irqv memory\n");
1467 /* Administrative interrupt events will use vector 0 */
1468 err = iflib_irq_alloc_generic(ctx, &sc->irqvs[0].irq, 1, IFLIB_INTR_ADMIN,
1469 ice_msix_admin, sc, 0, "admin");
1471 device_printf(sc->dev,
1472 "Failed to register Admin queue handler: %s\n",
1476 sc->irqvs[0].me = 0;
1478 /* Do not allocate queue interrupts when in recovery mode */
1479 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1482 for (i = 0, vector = 1; i < vsi->num_rx_queues; i++, vector++) {
1483 struct ice_rx_queue *rxq = &vsi->rx_queues[i];
1484 struct ice_tx_queue *txq = &vsi->tx_queues[i];
1485 int rid = vector + 1;
1488 snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
1489 err = iflib_irq_alloc_generic(ctx, &sc->irqvs[vector].irq, rid,
1490 IFLIB_INTR_RX, ice_msix_que,
1491 rxq, rxq->me, irq_name);
1493 device_printf(sc->dev,
1494 "Failed to allocate q int %d err: %s\n",
1495 i, ice_err_str(err));
1500 sc->irqvs[vector].me = vector;
1501 rxq->irqv = &sc->irqvs[vector];
1503 bzero(irq_name, sizeof(irq_name));
1505 snprintf(irq_name, sizeof(irq_name), "txq%d", i);
1506 iflib_softirq_alloc_generic(ctx, &sc->irqvs[vector].irq,
1509 txq->irqv = &sc->irqvs[vector];
1514 for (; i >= 0; i--, vector--)
1515 iflib_irq_free(ctx, &sc->irqvs[vector].irq);
1516 iflib_irq_free(ctx, &sc->irqvs[0].irq);
1518 free(sc->irqvs, M_ICE);
1524 * ice_if_mtu_set - Set the device MTU
1525 * @ctx: iflib context structure
1526 * @mtu: the MTU requested
1528 * Called by iflib to configure the device's Maximum Transmission Unit (MTU).
1530 * @pre assumes the caller holds the iflib CTX lock
1533 ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1535 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1537 ASSERT_CTX_LOCKED(sc);
1539 /* Do not support configuration when in recovery mode */
1540 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1543 if (mtu < ICE_MIN_MTU || mtu > ICE_MAX_MTU)
1546 sc->scctx->isc_max_frame_size = mtu +
1547 ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1549 sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
1555 * ice_if_intr_enable - Enable device interrupts
1556 * @ctx: iflib context structure
1558 * Called by iflib to request enabling device interrupts.
1561 ice_if_intr_enable(if_ctx_t ctx)
1563 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1564 struct ice_vsi *vsi = &sc->pf_vsi;
1565 struct ice_hw *hw = &sc->hw;
1567 ASSERT_CTX_LOCKED(sc);
1570 ice_enable_intr(hw, sc->irqvs[0].me);
1572 /* Do not enable queue interrupts in recovery mode */
1573 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1576 /* Enable all queue interrupts */
1577 for (int i = 0; i < vsi->num_rx_queues; i++)
1578 ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
1582 * ice_if_intr_disable - Disable device interrupts
1583 * @ctx: iflib context structure
1585 * Called by iflib to request disabling device interrupts.
1588 ice_if_intr_disable(if_ctx_t ctx)
1590 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1591 struct ice_hw *hw = &sc->hw;
1594 ASSERT_CTX_LOCKED(sc);
1596 /* IFDI_INTR_DISABLE may be called prior to interrupts actually being
1597 * assigned to queues. Instead of assuming that the interrupt
1598 * assignment in the rx_queues structure is valid, just disable all
1599 * possible interrupts
1601 * Note that we choose not to disable ITR 0 because this handles the
1602 * AdminQ interrupts, and we want to keep processing these even when
1603 * the interface is offline.
1605 for (i = 1; i < hw->func_caps.common_cap.num_msix_vectors; i++)
1606 ice_disable_intr(hw, i);
1610 * ice_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
1611 * @ctx: iflib context structure
1612 * @rxqid: the Rx queue to enable
1614 * Enable a specific Rx queue interrupt.
1616 * This function is not protected by the iflib CTX lock.
1619 ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1621 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1622 struct ice_vsi *vsi = &sc->pf_vsi;
1623 struct ice_hw *hw = &sc->hw;
1625 /* Do not enable queue interrupts in recovery mode */
1626 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1629 ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
1634 * ice_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
1635 * @ctx: iflib context structure
1636 * @txqid: the Tx queue to enable
1638 * Enable a specific Tx queue interrupt.
1640 * This function is not protected by the iflib CTX lock.
1643 ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1645 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1646 struct ice_vsi *vsi = &sc->pf_vsi;
1647 struct ice_hw *hw = &sc->hw;
1649 /* Do not enable queue interrupts in recovery mode */
1650 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1653 ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
1658 * ice_if_promisc_set - Set device promiscuous mode
1659 * @ctx: iflib context structure
1660 * @flags: promiscuous flags to configure
1662 * Called by iflib to configure device promiscuous mode.
1664 * @remark Calls to this function will always overwrite the previous setting
1667 ice_if_promisc_set(if_ctx_t ctx, int flags)
1669 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1670 struct ice_hw *hw = &sc->hw;
1671 device_t dev = sc->dev;
1672 enum ice_status status;
1673 bool promisc_enable = flags & IFF_PROMISC;
1674 bool multi_enable = flags & IFF_ALLMULTI;
1676 /* Do not support configuration when in recovery mode */
1677 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1681 return (EOPNOTSUPP);
1683 if (promisc_enable) {
1684 status = ice_set_vsi_promisc(hw, sc->pf_vsi.idx,
1685 ICE_VSI_PROMISC_MASK, 0);
1686 if (status && status != ICE_ERR_ALREADY_EXISTS) {
1688 "Failed to enable promiscuous mode for PF VSI, err %s aq_err %s\n",
1689 ice_status_str(status),
1690 ice_aq_str(hw->adminq.sq_last_status));
1694 status = ice_clear_vsi_promisc(hw, sc->pf_vsi.idx,
1695 ICE_VSI_PROMISC_MASK, 0);
1698 "Failed to disable promiscuous mode for PF VSI, err %s aq_err %s\n",
1699 ice_status_str(status),
1700 ice_aq_str(hw->adminq.sq_last_status));
1709 * ice_if_media_change - Change device media
1710 * @ctx: device ctx structure
1712 * Called by iflib when a media change is requested. This operation is not
1713 * supported by the hardware, so we just return an error code.
1716 ice_if_media_change(if_ctx_t ctx)
1718 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1720 device_printf(sc->dev, "Media change is not supported.\n");
1725 * ice_if_media_status - Report current device media
1726 * @ctx: iflib context structure
1727 * @ifmr: ifmedia request structure to update
1729 * Updates the provided ifmr with current device media status, including link
1730 * status and media type.
1733 ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1735 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1736 struct ice_link_status *li = &sc->hw.port_info->phy.link_info;
1738 ifmr->ifm_status = IFM_AVALID;
1739 ifmr->ifm_active = IFM_ETHER;
1741 /* Never report link up or media types when in recovery mode */
1742 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1748 ifmr->ifm_status |= IFM_ACTIVE;
1749 ifmr->ifm_active |= IFM_FDX;
1751 if (li->phy_type_low)
1752 ifmr->ifm_active |= ice_get_phy_type_low(li->phy_type_low);
1753 else if (li->phy_type_high)
1754 ifmr->ifm_active |= ice_get_phy_type_high(li->phy_type_high);
1756 ifmr->ifm_active |= IFM_UNKNOWN;
1758 /* Report flow control status as well */
1759 if (li->an_info & ICE_AQ_LINK_PAUSE_TX)
1760 ifmr->ifm_active |= IFM_ETH_TXPAUSE;
1761 if (li->an_info & ICE_AQ_LINK_PAUSE_RX)
1762 ifmr->ifm_active |= IFM_ETH_RXPAUSE;
1766 * ice_init_tx_tracking - Initialize Tx queue software tracking values
1767 * @vsi: the VSI to initialize
1769 * Initialize Tx queue software tracking values, including the Report Status
1770 * queue, and related software tracking values.
1773 ice_init_tx_tracking(struct ice_vsi *vsi)
1775 struct ice_tx_queue *txq;
1779 for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
1781 txq->tx_rs_cidx = txq->tx_rs_pidx = 0;
1783 /* Initialize the last processed descriptor to be the end of
1784 * the ring, rather than the start, so that we avoid an
1785 * off-by-one error in ice_ift_txd_credits_update for the
1788 txq->tx_cidx_processed = txq->desc_count - 1;
1790 for (j = 0; j < txq->desc_count; j++)
1791 txq->tx_rsq[j] = QIDX_INVALID;
1796 * ice_update_rx_mbuf_sz - Update the Rx buffer size for all queues
1797 * @sc: the device softc
1799 * Called to update the Rx queue mbuf_sz parameter for configuring the receive
1800 * buffer sizes when programming hardware.
1803 ice_update_rx_mbuf_sz(struct ice_softc *sc)
1805 uint32_t mbuf_sz = iflib_get_rx_mbuf_sz(sc->ctx);
1806 struct ice_vsi *vsi = &sc->pf_vsi;
1808 MPASS(mbuf_sz <= UINT16_MAX);
1809 vsi->mbuf_sz = mbuf_sz;
1813 * ice_if_init - Initialize the device
1814 * @ctx: iflib ctx structure
1816 * Called by iflib to bring the device up, i.e. ifconfig ice0 up. Initializes
1817 * device filters and prepares the Tx and Rx engines.
1819 * @pre assumes the caller holds the iflib CTX lock
1822 ice_if_init(if_ctx_t ctx)
1824 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1825 device_t dev = sc->dev;
1828 ASSERT_CTX_LOCKED(sc);
1830 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1833 if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
1834 device_printf(sc->dev, "request to start interface cannot be completed as the device failed to reset\n");
1838 if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
1839 device_printf(sc->dev, "request to start interface while device is prepared for impending reset\n");
1843 ice_update_rx_mbuf_sz(sc);
1845 /* Update the MAC address... User might use a LAA */
1846 err = ice_update_laa_mac(sc);
1849 "LAA address change failed, err %s\n",
1854 /* Initialize software Tx tracking values */
1855 ice_init_tx_tracking(&sc->pf_vsi);
1857 err = ice_cfg_vsi_for_tx(&sc->pf_vsi);
1860 "Unable to configure the main VSI for Tx: %s\n",
1865 err = ice_cfg_vsi_for_rx(&sc->pf_vsi);
1868 "Unable to configure the main VSI for Rx: %s\n",
1870 goto err_cleanup_tx;
1873 err = ice_control_rx_queues(&sc->pf_vsi, true);
1876 "Unable to enable Rx rings for transmit: %s\n",
1878 goto err_cleanup_tx;
1881 err = ice_cfg_pf_default_mac_filters(sc);
1884 "Unable to configure default MAC filters: %s\n",
1889 /* We use software interrupts for Tx, so we only program the hardware
1890 * interrupts for Rx.
1892 ice_configure_rxq_interrupts(&sc->pf_vsi);
1893 ice_configure_rx_itr(&sc->pf_vsi);
1895 /* Configure promiscuous mode */
1896 ice_if_promisc_set(ctx, if_getflags(sc->ifp));
1898 ice_set_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED);
1902 ice_control_rx_queues(&sc->pf_vsi, false);
1904 ice_vsi_disable_tx(&sc->pf_vsi);
1908 * ice_poll_for_media_avail - Re-enable link if media is detected
1909 * @sc: device private structure
1911 * Intended to be called from the driver's timer function, this function
1912 * sends the Get Link Status AQ command and re-enables HW link if the
1913 * command says that media is available.
1915 * If the driver doesn't have the "NO_MEDIA" state set, then this does nothing,
1916 * since media removal events are supposed to be sent to the driver through
1917 * a link status event.
1920 ice_poll_for_media_avail(struct ice_softc *sc)
1922 struct ice_hw *hw = &sc->hw;
1923 struct ice_port_info *pi = hw->port_info;
1925 if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA)) {
1926 pi->phy.get_link_info = true;
1927 ice_get_link_status(pi, &sc->link_up);
1929 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
1930 enum ice_status status;
1932 /* Re-enable link and re-apply user link settings */
1933 ice_apply_saved_phy_cfg(sc);
1935 /* Update the OS about changes in media capability */
1936 status = ice_add_media_types(sc, sc->media);
1938 device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
1939 ice_status_str(status),
1940 ice_aq_str(hw->adminq.sq_last_status));
1942 ice_clear_state(&sc->state, ICE_STATE_NO_MEDIA);
1948 * ice_if_timer - called by iflib periodically
1949 * @ctx: iflib ctx structure
1950 * @qid: the queue this timer was called for
1952 * This callback is triggered by iflib periodically. We use it to update the
1955 * @remark this function is not protected by the iflib CTX lock.
1958 ice_if_timer(if_ctx_t ctx, uint16_t qid)
1960 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1961 uint64_t prev_link_xoff_rx = sc->stats.cur.link_xoff_rx;
1966 /* Do not attempt to update stats when in recovery mode */
1967 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1970 /* Update device statistics */
1971 ice_update_pf_stats(sc);
1974 * For proper watchdog management, the iflib stack needs to know if
1975 * we've been paused during the last interval. Check if the
1976 * link_xoff_rx stat changed, and set the isc_pause_frames, if so.
1978 if (sc->stats.cur.link_xoff_rx != prev_link_xoff_rx)
1979 sc->scctx->isc_pause_frames = 1;
1981 /* Update the primary VSI stats */
1982 ice_update_vsi_hw_stats(&sc->pf_vsi);
1986 * ice_admin_timer - called periodically to trigger the admin task
1987 * @arg: callout(9) argument pointing to the device private softc structure
1989 * Timer function used as part of a callout(9) timer that will periodically
1990 * trigger the admin task, even when the interface is down.
1992 * @remark this function is not called by iflib and is not protected by the
1995 * @remark because this is a callout function, it cannot sleep and should not
1996 * attempt taking the iflib CTX lock.
1999 ice_admin_timer(void *arg)
2001 struct ice_softc *sc = (struct ice_softc *)arg;
2003 /* Fire off the admin task */
2004 iflib_admin_intr_deferred(sc->ctx);
2006 /* Reschedule the admin timer */
2007 callout_schedule(&sc->admin_timer, hz/2);
2011 * ice_transition_recovery_mode - Transition to recovery mode
2012 * @sc: the device private softc
2014 * Called when the driver detects that the firmware has entered recovery mode
2018 ice_transition_recovery_mode(struct ice_softc *sc)
2020 struct ice_vsi *vsi = &sc->pf_vsi;
2023 device_printf(sc->dev, "Firmware recovery mode detected. Limiting functionality. Refer to Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
2025 /* Tell the stack that the link has gone down */
2026 iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
2028 /* Request that the device be re-initialized */
2029 ice_request_stack_reinit(sc);
2031 ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
2032 ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2034 ice_vsi_del_txqs_ctx(vsi);
2035 ice_vsi_del_rxqs_ctx(vsi);
2037 for (i = 0; i < sc->num_available_vsi; i++) {
2039 ice_release_vsi(sc->all_vsi[i]);
2041 sc->num_available_vsi = 0;
2044 free(sc->all_vsi, M_ICE);
2048 /* Destroy the interrupt manager */
2049 ice_resmgr_destroy(&sc->imgr);
2050 /* Destroy the queue managers */
2051 ice_resmgr_destroy(&sc->tx_qmgr);
2052 ice_resmgr_destroy(&sc->rx_qmgr);
2054 ice_deinit_hw(&sc->hw);
2058 * ice_transition_safe_mode - Transition to safe mode
2059 * @sc: the device private softc
2061 * Called when the driver attempts to reload the DDP package during a device
2062 * reset, and the new download fails. If so, we must transition to safe mode
2065 * @remark although safe mode normally allocates only a single queue, we can't
2066 * change the number of queues dynamically when using iflib. Due to this, we
2067 * do not attempt to reduce the number of queues.
2070 ice_transition_safe_mode(struct ice_softc *sc)
2072 /* Indicate that we are in Safe mode */
2073 ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_cap);
2074 ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_en);
2076 ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
2077 ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2079 ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
2080 ice_clear_bit(ICE_FEATURE_RSS, sc->feat_en);
2084 * ice_if_update_admin_status - update admin status
2085 * @ctx: iflib ctx structure
2087 * Called by iflib to update the admin status. For our purposes, this means
2088 * check the adminq, and update the link status. It's ultimately triggered by
2089 * our admin interrupt, or by the ice_if_timer periodically.
2091 * @pre assumes the caller holds the iflib CTX lock
2094 ice_if_update_admin_status(if_ctx_t ctx)
2096 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2097 enum ice_fw_modes fw_mode;
2098 bool reschedule = false;
2101 ASSERT_CTX_LOCKED(sc);
2103 /* Check if the firmware entered recovery mode at run time */
2104 fw_mode = ice_get_fw_mode(&sc->hw);
2105 if (fw_mode == ICE_FW_MODE_REC) {
2106 if (!ice_testandset_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2107 /* If we just entered recovery mode, log a warning to
2108 * the system administrator and deinit driver state
2109 * that is no longer functional.
2111 ice_transition_recovery_mode(sc);
2113 } else if (fw_mode == ICE_FW_MODE_ROLLBACK) {
2114 if (!ice_testandset_state(&sc->state, ICE_STATE_ROLLBACK_MODE)) {
2115 /* Rollback mode isn't fatal, but we don't want to
2116 * repeatedly post a message about it.
2118 ice_print_rollback_msg(&sc->hw);
2122 /* Handle global reset events */
2123 ice_handle_reset_event(sc);
2125 /* Handle PF reset requests */
2126 ice_handle_pf_reset_request(sc);
2128 /* Handle MDD events */
2129 ice_handle_mdd_event(sc);
2131 if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED) ||
2132 ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET) ||
2133 ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2135 * If we know the control queues are disabled, skip processing
2136 * the control queues entirely.
2139 } else if (ice_testandclear_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING)) {
2140 ice_process_ctrlq(sc, ICE_CTL_Q_ADMIN, &pending);
2144 ice_process_ctrlq(sc, ICE_CTL_Q_MAILBOX, &pending);
2149 /* Poll for link up */
2150 ice_poll_for_media_avail(sc);
2152 /* Check and update link status */
2153 ice_update_link_status(sc, false);
2156 * If there are still messages to process, we need to reschedule
2157 * ourselves. Otherwise, we can just re-enable the interrupt. We'll be
2158 * woken up at the next interrupt or timer event.
2161 ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
2162 iflib_admin_intr_deferred(ctx);
2164 ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2169 * ice_prepare_for_reset - Prepare device for an impending reset
2170 * @sc: The device private softc
2172 * Prepare the driver for an impending reset, shutting down VSIs, clearing the
2173 * scheduler setup, and shutting down controlqs. Uses the
2174 * ICE_STATE_PREPARED_FOR_RESET to indicate whether we've already prepared the
2175 * driver for reset or not.
2178 ice_prepare_for_reset(struct ice_softc *sc)
2180 struct ice_hw *hw = &sc->hw;
2182 /* If we're already prepared, there's nothing to do */
2183 if (ice_testandset_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET))
2186 log(LOG_INFO, "%s: preparing to reset device logic\n", sc->ifp->if_xname);
2188 /* In recovery mode, hardware is not initialized */
2189 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2192 /* Release the main PF VSI queue mappings */
2193 ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2194 sc->pf_vsi.num_tx_queues);
2195 ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
2196 sc->pf_vsi.num_rx_queues);
2198 ice_clear_hw_tbls(hw);
2201 ice_sched_clear_port(hw->port_info);
2203 ice_shutdown_all_ctrlq(hw);
2207 * ice_rebuild_pf_vsi_qmap - Rebuild the main PF VSI queue mapping
2208 * @sc: the device softc pointer
2210 * Loops over the Tx and Rx queues for the main PF VSI and reassigns the queue
2211 * mapping after a reset occurred.
2214 ice_rebuild_pf_vsi_qmap(struct ice_softc *sc)
2216 struct ice_vsi *vsi = &sc->pf_vsi;
2217 struct ice_tx_queue *txq;
2218 struct ice_rx_queue *rxq;
2221 /* Re-assign Tx queues from PF space to the main VSI */
2222 err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap,
2223 vsi->num_tx_queues);
2225 device_printf(sc->dev, "Unable to re-assign PF Tx queues: %s\n",
2230 /* Re-assign Rx queues from PF space to this VSI */
2231 err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap,
2232 vsi->num_rx_queues);
2234 device_printf(sc->dev, "Unable to re-assign PF Rx queues: %s\n",
2236 goto err_release_tx_queues;
2239 vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
2241 /* Re-assign Tx queue tail pointers */
2242 for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
2243 txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
2245 /* Re-assign Rx queue tail pointers */
2246 for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
2247 rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
2251 err_release_tx_queues:
2252 ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2253 sc->pf_vsi.num_tx_queues);
2258 /* determine if the iflib context is active */
2259 #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
2262 * ice_rebuild_recovery_mode - Rebuild driver state while in recovery mode
2263 * @sc: The device private softc
2265 * Handle a driver rebuild while in recovery mode. This will only rebuild the
2266 * limited functionality supported while in recovery mode.
2269 ice_rebuild_recovery_mode(struct ice_softc *sc)
2271 device_t dev = sc->dev;
2273 /* enable PCIe bus master */
2274 pci_enable_busmaster(dev);
2276 /* Configure interrupt causes for the administrative interrupt */
2277 ice_configure_misc_interrupts(sc);
2279 /* Enable ITR 0 right away, so that we can handle admin interrupts */
2280 ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2282 /* Now that the rebuild is finished, we're no longer prepared to reset */
2283 ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2285 log(LOG_INFO, "%s: device rebuild successful\n", sc->ifp->if_xname);
2287 /* In order to completely restore device functionality, the iflib core
2288 * needs to be reset. We need to request an iflib reset. Additionally,
2289 * because the state of IFC_DO_RESET is cached within task_fn_admin in
2290 * the iflib core, we also want re-run the admin task so that iflib
2291 * resets immediately instead of waiting for the next interrupt.
2293 ice_request_stack_reinit(sc);
2299 * ice_rebuild - Rebuild driver state post reset
2300 * @sc: The device private softc
2302 * Restore driver state after a reset occurred. Restart the controlqs, setup
2303 * the hardware port, and re-enable the VSIs.
2306 ice_rebuild(struct ice_softc *sc)
2308 struct ice_hw *hw = &sc->hw;
2309 device_t dev = sc->dev;
2310 enum ice_status status;
2313 sc->rebuild_ticks = ticks;
2315 /* If we're rebuilding, then a reset has succeeded. */
2316 ice_clear_state(&sc->state, ICE_STATE_RESET_FAILED);
2319 * If the firmware is in recovery mode, only restore the limited
2320 * functionality supported by recovery mode.
2322 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2323 ice_rebuild_recovery_mode(sc);
2327 /* enable PCIe bus master */
2328 pci_enable_busmaster(dev);
2330 status = ice_init_all_ctrlq(hw);
2332 device_printf(dev, "failed to re-init controlqs, err %s\n",
2333 ice_status_str(status));
2334 goto err_shutdown_ctrlq;
2337 /* Query the allocated resources for Tx scheduler */
2338 status = ice_sched_query_res_alloc(hw);
2341 "Failed to query scheduler resources, err %s aq_err %s\n",
2342 ice_status_str(status),
2343 ice_aq_str(hw->adminq.sq_last_status));
2344 goto err_shutdown_ctrlq;
2347 err = ice_send_version(sc);
2349 goto err_shutdown_ctrlq;
2351 err = ice_init_link_events(sc);
2353 device_printf(dev, "ice_init_link_events failed: %s\n",
2355 goto err_shutdown_ctrlq;
2358 status = ice_clear_pf_cfg(hw);
2360 device_printf(dev, "failed to clear PF configuration, err %s\n",
2361 ice_status_str(status));
2362 goto err_shutdown_ctrlq;
2365 ice_clear_pxe_mode(hw);
2367 status = ice_get_caps(hw);
2369 device_printf(dev, "failed to get capabilities, err %s\n",
2370 ice_status_str(status));
2371 goto err_shutdown_ctrlq;
2374 status = ice_sched_init_port(hw->port_info);
2376 device_printf(dev, "failed to initialize port, err %s\n",
2377 ice_status_str(status));
2378 goto err_sched_cleanup;
2381 /* If we previously loaded the package, it needs to be reloaded now */
2382 if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE)) {
2383 status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
2385 ice_log_pkg_init(sc, &status);
2387 ice_transition_safe_mode(sc);
2391 ice_reset_pf_stats(sc);
2393 err = ice_rebuild_pf_vsi_qmap(sc);
2395 device_printf(sc->dev, "Unable to re-assign main VSI queues, err %s\n",
2397 goto err_sched_cleanup;
2399 err = ice_initialize_vsi(&sc->pf_vsi);
2401 device_printf(sc->dev, "Unable to re-initialize Main VSI, err %s\n",
2403 goto err_release_queue_allocations;
2406 /* Replay all VSI configuration */
2407 err = ice_replay_all_vsi_cfg(sc);
2409 goto err_deinit_pf_vsi;
2411 /* Reconfigure the main PF VSI for RSS */
2412 err = ice_config_rss(&sc->pf_vsi);
2414 device_printf(sc->dev,
2415 "Unable to reconfigure RSS for the main VSI, err %s\n",
2417 goto err_deinit_pf_vsi;
2420 /* Refresh link status */
2421 ice_clear_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
2422 sc->hw.port_info->phy.get_link_info = true;
2423 ice_get_link_status(sc->hw.port_info, &sc->link_up);
2424 ice_update_link_status(sc, true);
2426 /* Configure interrupt causes for the administrative interrupt */
2427 ice_configure_misc_interrupts(sc);
2429 /* Enable ITR 0 right away, so that we can handle admin interrupts */
2430 ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2432 /* Now that the rebuild is finished, we're no longer prepared to reset */
2433 ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2435 log(LOG_INFO, "%s: device rebuild successful\n", sc->ifp->if_xname);
2437 /* In order to completely restore device functionality, the iflib core
2438 * needs to be reset. We need to request an iflib reset. Additionally,
2439 * because the state of IFC_DO_RESET is cached within task_fn_admin in
2440 * the iflib core, we also want re-run the admin task so that iflib
2441 * resets immediately instead of waiting for the next interrupt.
2443 ice_request_stack_reinit(sc);
2448 ice_deinit_vsi(&sc->pf_vsi);
2449 err_release_queue_allocations:
2450 ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2451 sc->pf_vsi.num_tx_queues);
2452 ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
2453 sc->pf_vsi.num_rx_queues);
2455 ice_sched_cleanup_all(hw);
2457 ice_shutdown_all_ctrlq(hw);
2458 ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2459 device_printf(dev, "Driver rebuild failed, please reload the device driver\n");
2463 * ice_handle_reset_event - Handle reset events triggered by OICR
2464 * @sc: The device private softc
2466 * Handle reset events triggered by an OICR notification. This includes CORER,
2467 * GLOBR, and EMPR resets triggered by software on this or any other PF or by
2470 * @pre assumes the iflib context lock is held, and will unlock it while
2471 * waiting for the hardware to finish reset.
2474 ice_handle_reset_event(struct ice_softc *sc)
2476 struct ice_hw *hw = &sc->hw;
2477 enum ice_status status;
2478 device_t dev = sc->dev;
2480 /* When a CORER, GLOBR, or EMPR is about to happen, the hardware will
2481 * trigger an OICR interrupt. Our OICR handler will determine when
2482 * this occurs and set the ICE_STATE_RESET_OICR_RECV bit as
2485 if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
2488 ice_prepare_for_reset(sc);
2491 * Release the iflib context lock and wait for the device to finish
2494 IFLIB_CTX_UNLOCK(sc);
2495 status = ice_check_reset(hw);
2498 device_printf(dev, "Device never came out of reset, err %s\n",
2499 ice_status_str(status));
2500 ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2504 /* We're done with the reset, so we can rebuild driver state */
2505 sc->hw.reset_ongoing = false;
2508 /* In the unlikely event that a PF reset request occurs at the same
2509 * time as a global reset, clear the request now. This avoids
2510 * resetting a second time right after we reset due to a global event.
2512 if (ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
2513 device_printf(dev, "Ignoring PFR request that occurred while a reset was ongoing\n");
2517 * ice_handle_pf_reset_request - Initiate PF reset requested by software
2518 * @sc: The device private softc
2520 * Initiate a PF reset requested by software. We handle this in the admin task
2521 * so that only one thread actually handles driver preparation and cleanup,
2522 * rather than having multiple threads possibly attempt to run this code
2525 * @pre assumes the iflib context lock is held and will unlock it while
2526 * waiting for the PF reset to complete.
2529 ice_handle_pf_reset_request(struct ice_softc *sc)
2531 struct ice_hw *hw = &sc->hw;
2532 enum ice_status status;
2534 /* Check for PF reset requests */
2535 if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
2538 /* Make sure we're prepared for reset */
2539 ice_prepare_for_reset(sc);
2542 * Release the iflib context lock and wait for the device to finish
2545 IFLIB_CTX_UNLOCK(sc);
2546 status = ice_reset(hw, ICE_RESET_PFR);
2549 device_printf(sc->dev, "device PF reset failed, err %s\n",
2550 ice_status_str(status));
2551 ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2555 sc->soft_stats.pfr_count++;
2560 * ice_init_device_features - Init device driver features
2561 * @sc: driver softc structure
2563 * @pre assumes that the function capabilities bits have been set up by
2567 ice_init_device_features(struct ice_softc *sc)
2570 * A failed pkg file download triggers safe mode, disabling advanced
2571 * device feature support
2573 if (ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE))
2576 /* Set capabilities that the driver supports */
2577 ice_set_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2578 ice_set_bit(ICE_FEATURE_RSS, sc->feat_cap);
2579 ice_set_bit(ICE_FEATURE_LENIENT_LINK_MODE, sc->feat_cap);
2580 ice_set_bit(ICE_FEATURE_DEFAULT_OVERRIDE, sc->feat_cap);
2582 /* Disable features due to hardware limitations... */
2583 if (!sc->hw.func_caps.common_cap.rss_table_size)
2584 ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
2586 /* Disable capabilities not supported by the OS */
2587 ice_disable_unsupported_features(sc->feat_cap);
2589 /* RSS is always enabled for iflib */
2590 if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RSS))
2591 ice_set_bit(ICE_FEATURE_RSS, sc->feat_en);
2595 * ice_if_multi_set - Callback to update Multicast filters in HW
2596 * @ctx: iflib ctx structure
2598 * Called by iflib in response to SIOCDELMULTI and SIOCADDMULTI. Must search
2599 * the if_multiaddrs list and determine which filters have been added or
2600 * removed from the list, and update HW programming to reflect the new list.
2602 * @pre assumes the caller holds the iflib CTX lock
2605 ice_if_multi_set(if_ctx_t ctx)
2607 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2610 ASSERT_CTX_LOCKED(sc);
2612 /* Do not handle multicast configuration in recovery mode */
2613 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2616 err = ice_sync_multicast_filters(sc);
2618 device_printf(sc->dev,
2619 "Failed to synchronize multicast filter list: %s\n",
2626 * ice_if_vlan_register - Register a VLAN with the hardware
2627 * @ctx: iflib ctx pointer
2628 * @vtag: VLAN to add
2630 * Programs the main PF VSI with a hardware filter for the given VLAN.
2632 * @pre assumes the caller holds the iflib CTX lock
2635 ice_if_vlan_register(if_ctx_t ctx, u16 vtag)
2637 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2638 enum ice_status status;
2640 ASSERT_CTX_LOCKED(sc);
2642 /* Do not handle VLAN configuration in recovery mode */
2643 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2646 status = ice_add_vlan_hw_filter(&sc->pf_vsi, vtag);
2648 device_printf(sc->dev,
2649 "Failure adding VLAN %d to main VSI, err %s aq_err %s\n",
2650 vtag, ice_status_str(status),
2651 ice_aq_str(sc->hw.adminq.sq_last_status));
2656 * ice_if_vlan_unregister - Remove a VLAN filter from the hardware
2657 * @ctx: iflib ctx pointer
2658 * @vtag: VLAN to add
2660 * Removes the previously programmed VLAN filter from the main PF VSI.
2662 * @pre assumes the caller holds the iflib CTX lock
2665 ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
2667 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2668 enum ice_status status;
2670 ASSERT_CTX_LOCKED(sc);
2672 /* Do not handle VLAN configuration in recovery mode */
2673 if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2676 status = ice_remove_vlan_hw_filter(&sc->pf_vsi, vtag);
2678 device_printf(sc->dev,
2679 "Failure removing VLAN %d from main VSI, err %s aq_err %s\n",
2680 vtag, ice_status_str(status),
2681 ice_aq_str(sc->hw.adminq.sq_last_status));
2686 * ice_if_stop - Stop the device
2687 * @ctx: iflib context structure
2689 * Called by iflib to stop the device and bring it down. (i.e. ifconfig ice0
2692 * @pre assumes the caller holds the iflib CTX lock
2695 ice_if_stop(if_ctx_t ctx)
2697 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2699 ASSERT_CTX_LOCKED(sc);
2702 * The iflib core may call IFDI_STOP prior to the first call to
2703 * IFDI_INIT. This will cause us to attempt to remove MAC filters we
2704 * don't have, and disable Tx queues which aren't yet configured.
2705 * Although it is likely these extra operations are harmless, they do
2706 * cause spurious warning messages to be displayed, which may confuse
2709 * To avoid these messages, we use a state bit indicating if we've
2710 * been initialized. It will be set when ice_if_init is called, and
2711 * cleared here in ice_if_stop.
2713 if (!ice_testandclear_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
2716 if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
2717 device_printf(sc->dev, "request to stop interface cannot be completed as the device failed to reset\n");
2721 if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
2722 device_printf(sc->dev, "request to stop interface while device is prepared for impending reset\n");
2726 /* Remove the MAC filters, stop Tx, and stop Rx. We don't check the
2727 * return of these functions because there's nothing we can really do
2728 * if they fail, and the functions already print error messages.
2729 * Just try to shut down as much as we can.
2731 ice_rm_pf_default_mac_filters(sc);
2733 /* Dissociate the Tx and Rx queues from the interrupts */
2734 ice_flush_txq_interrupts(&sc->pf_vsi);
2735 ice_flush_rxq_interrupts(&sc->pf_vsi);
2737 /* Disable the Tx and Rx queues */
2738 ice_vsi_disable_tx(&sc->pf_vsi);
2739 ice_control_rx_queues(&sc->pf_vsi, false);
2743 * ice_if_get_counter - Get current value of an ifnet statistic
2744 * @ctx: iflib context pointer
2745 * @counter: ifnet counter to read
2747 * Reads the current value of an ifnet counter for the device.
2749 * This function is not protected by the iflib CTX lock.
2752 ice_if_get_counter(if_ctx_t ctx, ift_counter counter)
2754 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2756 /* Return the counter for the main PF VSI */
2757 return ice_get_ifnet_counter(&sc->pf_vsi, counter);
2761 * ice_request_stack_reinit - Request that iflib re-initialize
2762 * @sc: the device private softc
2764 * Request that the device be brought down and up, to re-initialize. For
2765 * example, this may be called when a device reset occurs, or when Tx and Rx
2766 * queues need to be re-initialized.
2768 * This is required because the iflib state is outside the driver, and must be
2769 * re-initialized if we need to resart Tx and Rx queues.
2772 ice_request_stack_reinit(struct ice_softc *sc)
2774 if (CTX_ACTIVE(sc->ctx)) {
2775 iflib_request_reset(sc->ctx);
2776 iflib_admin_intr_deferred(sc->ctx);
2781 * ice_driver_is_detaching - Check if the driver is detaching/unloading
2782 * @sc: device private softc
2784 * Returns true if the driver is detaching, false otherwise.
2786 * @remark on newer kernels, take advantage of iflib_in_detach in order to
2787 * report detachment correctly as early as possible.
2789 * @remark this function is used by various code paths that want to avoid
2790 * running if the driver is about to be removed. This includes sysctls and
2791 * other driver access points. Note that it does not fully resolve
2792 * detach-based race conditions as it is possible for a thread to race with
2796 ice_driver_is_detaching(struct ice_softc *sc)
2798 return (ice_test_state(&sc->state, ICE_STATE_DETACHING) ||
2799 iflib_in_detach(sc->ctx));
2803 * ice_if_priv_ioctl - Device private ioctl handler
2804 * @ctx: iflib context pointer
2805 * @command: The ioctl command issued
2806 * @data: ioctl specific data
2808 * iflib callback for handling custom driver specific ioctls.
2810 * @pre Assumes that the iflib context lock is held.
2813 ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
2815 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2817 device_t dev = sc->dev;
2822 ASSERT_CTX_LOCKED(sc);
2824 /* Make sure the command type is valid */
2828 /* Accepted commands */
2830 case SIOCGPRIVATE_0:
2832 * Although we do not support this ioctl command, it's
2833 * expected that iflib will forward it to the IFDI_PRIV_IOCTL
2834 * handler. Do not print a message in this case
2839 * If we get a different command for this function, it's
2840 * definitely unexpected, so log a message indicating what
2841 * command we got for debugging purposes.
2843 device_printf(dev, "%s: unexpected ioctl command %08lx\n",
2848 ifd = (struct ifdrv *)data;
2850 switch (ifd->ifd_cmd) {
2851 case ICE_NVM_ACCESS:
2852 return ice_handle_nvm_access_ioctl(sc, ifd);
2859 * ice_if_i2c_req - I2C request handler for iflib
2860 * @ctx: iflib context pointer
2861 * @req: The I2C parameters to use
2863 * Read from the port's I2C eeprom using the parameters from the ioctl.
2865 * @remark The iflib-only part is pretty simple.
2868 ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
2870 struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2872 return ice_handle_i2c_req(sc, req);