xref: /freebsd/sys/dev/ice/if_ice_iflib.c (revision 8923de59054358980102ea5acda6c6dd58273957)
171d10453SEric Joyner /* SPDX-License-Identifier: BSD-3-Clause */
2*8923de59SPiotr Kubaj /*  Copyright (c) 2022, Intel Corporation
371d10453SEric Joyner  *  All rights reserved.
471d10453SEric Joyner  *
571d10453SEric Joyner  *  Redistribution and use in source and binary forms, with or without
671d10453SEric Joyner  *  modification, are permitted provided that the following conditions are met:
771d10453SEric Joyner  *
871d10453SEric Joyner  *   1. Redistributions of source code must retain the above copyright notice,
971d10453SEric Joyner  *      this list of conditions and the following disclaimer.
1071d10453SEric Joyner  *
1171d10453SEric Joyner  *   2. Redistributions in binary form must reproduce the above copyright
1271d10453SEric Joyner  *      notice, this list of conditions and the following disclaimer in the
1371d10453SEric Joyner  *      documentation and/or other materials provided with the distribution.
1471d10453SEric Joyner  *
1571d10453SEric Joyner  *   3. Neither the name of the Intel Corporation nor the names of its
1671d10453SEric Joyner  *      contributors may be used to endorse or promote products derived from
1771d10453SEric Joyner  *      this software without specific prior written permission.
1871d10453SEric Joyner  *
1971d10453SEric Joyner  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2071d10453SEric Joyner  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2171d10453SEric Joyner  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2271d10453SEric Joyner  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2371d10453SEric Joyner  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2471d10453SEric Joyner  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2571d10453SEric Joyner  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2671d10453SEric Joyner  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2771d10453SEric Joyner  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2871d10453SEric Joyner  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2971d10453SEric Joyner  *  POSSIBILITY OF SUCH DAMAGE.
3071d10453SEric Joyner  */
3171d10453SEric Joyner /*$FreeBSD$*/
3271d10453SEric Joyner 
3371d10453SEric Joyner /**
3471d10453SEric Joyner  * @file if_ice_iflib.c
3571d10453SEric Joyner  * @brief iflib driver implementation
3671d10453SEric Joyner  *
3771d10453SEric Joyner  * Contains the main entry point for the iflib driver implementation. It
3871d10453SEric Joyner  * implements the various ifdi driver methods, and sets up the module and
3971d10453SEric Joyner  * driver values to load an iflib driver.
4071d10453SEric Joyner  */
4171d10453SEric Joyner 
4271d10453SEric Joyner #include "ice_iflib.h"
4371d10453SEric Joyner #include "ice_drv_info.h"
4471d10453SEric Joyner #include "ice_switch.h"
4571d10453SEric Joyner #include "ice_sched.h"
4671d10453SEric Joyner 
4771d10453SEric Joyner #include <sys/module.h>
4871d10453SEric Joyner #include <sys/sockio.h>
4971d10453SEric Joyner #include <sys/smp.h>
5071d10453SEric Joyner #include <dev/pci/pcivar.h>
5171d10453SEric Joyner #include <dev/pci/pcireg.h>
5271d10453SEric Joyner 
5371d10453SEric Joyner /*
5471d10453SEric Joyner  * Device method prototypes
5571d10453SEric Joyner  */
5671d10453SEric Joyner 
5771d10453SEric Joyner static void *ice_register(device_t);
5871d10453SEric Joyner static int  ice_if_attach_pre(if_ctx_t);
5971d10453SEric Joyner static int  ice_attach_pre_recovery_mode(struct ice_softc *sc);
6071d10453SEric Joyner static int  ice_if_attach_post(if_ctx_t);
6171d10453SEric Joyner static void ice_attach_post_recovery_mode(struct ice_softc *sc);
6271d10453SEric Joyner static int  ice_if_detach(if_ctx_t);
6371d10453SEric Joyner static int  ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
6471d10453SEric Joyner static int  ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
6571d10453SEric Joyner static int ice_if_msix_intr_assign(if_ctx_t ctx, int msix);
6671d10453SEric Joyner static void ice_if_queues_free(if_ctx_t ctx);
6771d10453SEric Joyner static int ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
6871d10453SEric Joyner static void ice_if_intr_enable(if_ctx_t ctx);
6971d10453SEric Joyner static void ice_if_intr_disable(if_ctx_t ctx);
7071d10453SEric Joyner static int ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
7171d10453SEric Joyner static int ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
7271d10453SEric Joyner static int ice_if_promisc_set(if_ctx_t ctx, int flags);
7371d10453SEric Joyner static void ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
7471d10453SEric Joyner static int ice_if_media_change(if_ctx_t ctx);
7571d10453SEric Joyner static void ice_if_init(if_ctx_t ctx);
7671d10453SEric Joyner static void ice_if_timer(if_ctx_t ctx, uint16_t qid);
7771d10453SEric Joyner static void ice_if_update_admin_status(if_ctx_t ctx);
7871d10453SEric Joyner static void ice_if_multi_set(if_ctx_t ctx);
7971d10453SEric Joyner static void ice_if_vlan_register(if_ctx_t ctx, u16 vtag);
8071d10453SEric Joyner static void ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
8171d10453SEric Joyner static void ice_if_stop(if_ctx_t ctx);
8271d10453SEric Joyner static uint64_t ice_if_get_counter(if_ctx_t ctx, ift_counter counter);
8371d10453SEric Joyner static int ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
8471d10453SEric Joyner static int ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
857d7af7f8SEric Joyner static int ice_if_suspend(if_ctx_t ctx);
867d7af7f8SEric Joyner static int ice_if_resume(if_ctx_t ctx);
8771d10453SEric Joyner 
8871d10453SEric Joyner static int ice_msix_que(void *arg);
8971d10453SEric Joyner static int ice_msix_admin(void *arg);
9071d10453SEric Joyner 
9171d10453SEric Joyner /*
9271d10453SEric Joyner  * Helper function prototypes
9371d10453SEric Joyner  */
9471d10453SEric Joyner static int ice_pci_mapping(struct ice_softc *sc);
9571d10453SEric Joyner static void ice_free_pci_mapping(struct ice_softc *sc);
9671d10453SEric Joyner static void ice_update_link_status(struct ice_softc *sc, bool update_media);
9771d10453SEric Joyner static void ice_init_device_features(struct ice_softc *sc);
9871d10453SEric Joyner static void ice_init_tx_tracking(struct ice_vsi *vsi);
9971d10453SEric Joyner static void ice_handle_reset_event(struct ice_softc *sc);
10071d10453SEric Joyner static void ice_handle_pf_reset_request(struct ice_softc *sc);
10171d10453SEric Joyner static void ice_prepare_for_reset(struct ice_softc *sc);
10271d10453SEric Joyner static int ice_rebuild_pf_vsi_qmap(struct ice_softc *sc);
10371d10453SEric Joyner static void ice_rebuild(struct ice_softc *sc);
10471d10453SEric Joyner static void ice_rebuild_recovery_mode(struct ice_softc *sc);
10571d10453SEric Joyner static void ice_free_irqvs(struct ice_softc *sc);
10671d10453SEric Joyner static void ice_update_rx_mbuf_sz(struct ice_softc *sc);
10771d10453SEric Joyner static void ice_poll_for_media_avail(struct ice_softc *sc);
10871d10453SEric Joyner static void ice_setup_scctx(struct ice_softc *sc);
10971d10453SEric Joyner static int ice_allocate_msix(struct ice_softc *sc);
11071d10453SEric Joyner static void ice_admin_timer(void *arg);
11171d10453SEric Joyner static void ice_transition_recovery_mode(struct ice_softc *sc);
11271d10453SEric Joyner static void ice_transition_safe_mode(struct ice_softc *sc);
11371d10453SEric Joyner 
11471d10453SEric Joyner /*
11571d10453SEric Joyner  * Device Interface Declaration
11671d10453SEric Joyner  */
11771d10453SEric Joyner 
11871d10453SEric Joyner /**
11971d10453SEric Joyner  * @var ice_methods
12071d10453SEric Joyner  * @brief ice driver method entry points
12171d10453SEric Joyner  *
12271d10453SEric Joyner  * List of device methods implementing the generic device interface used by
12371d10453SEric Joyner  * the device stack to interact with the ice driver. Since this is an iflib
12471d10453SEric Joyner  * driver, most of the methods point to the generic iflib implementation.
12571d10453SEric Joyner  */
12671d10453SEric Joyner static device_method_t ice_methods[] = {
12771d10453SEric Joyner 	/* Device interface */
12871d10453SEric Joyner 	DEVMETHOD(device_register, ice_register),
12971d10453SEric Joyner 	DEVMETHOD(device_probe,    iflib_device_probe_vendor),
13071d10453SEric Joyner 	DEVMETHOD(device_attach,   iflib_device_attach),
13171d10453SEric Joyner 	DEVMETHOD(device_detach,   iflib_device_detach),
13271d10453SEric Joyner 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
13371d10453SEric Joyner 	DEVMETHOD(device_suspend,  iflib_device_suspend),
13471d10453SEric Joyner 	DEVMETHOD(device_resume,   iflib_device_resume),
13571d10453SEric Joyner 	DEVMETHOD_END
13671d10453SEric Joyner };
13771d10453SEric Joyner 
13871d10453SEric Joyner /**
13971d10453SEric Joyner  * @var ice_iflib_methods
14071d10453SEric Joyner  * @brief iflib method entry points
14171d10453SEric Joyner  *
14271d10453SEric Joyner  * List of device methods used by the iflib stack to interact with this
14371d10453SEric Joyner  * driver. These are the real main entry points used to interact with this
14471d10453SEric Joyner  * driver.
14571d10453SEric Joyner  */
14671d10453SEric Joyner static device_method_t ice_iflib_methods[] = {
14771d10453SEric Joyner 	DEVMETHOD(ifdi_attach_pre, ice_if_attach_pre),
14871d10453SEric Joyner 	DEVMETHOD(ifdi_attach_post, ice_if_attach_post),
14971d10453SEric Joyner 	DEVMETHOD(ifdi_detach, ice_if_detach),
15071d10453SEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ice_if_tx_queues_alloc),
15171d10453SEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ice_if_rx_queues_alloc),
15271d10453SEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ice_if_msix_intr_assign),
15371d10453SEric Joyner 	DEVMETHOD(ifdi_queues_free, ice_if_queues_free),
15471d10453SEric Joyner 	DEVMETHOD(ifdi_mtu_set, ice_if_mtu_set),
15571d10453SEric Joyner 	DEVMETHOD(ifdi_intr_enable, ice_if_intr_enable),
15671d10453SEric Joyner 	DEVMETHOD(ifdi_intr_disable, ice_if_intr_disable),
15771d10453SEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ice_if_rx_queue_intr_enable),
15871d10453SEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ice_if_tx_queue_intr_enable),
15971d10453SEric Joyner 	DEVMETHOD(ifdi_promisc_set, ice_if_promisc_set),
16071d10453SEric Joyner 	DEVMETHOD(ifdi_media_status, ice_if_media_status),
16171d10453SEric Joyner 	DEVMETHOD(ifdi_media_change, ice_if_media_change),
16271d10453SEric Joyner 	DEVMETHOD(ifdi_init, ice_if_init),
16371d10453SEric Joyner 	DEVMETHOD(ifdi_stop, ice_if_stop),
16471d10453SEric Joyner 	DEVMETHOD(ifdi_timer, ice_if_timer),
16571d10453SEric Joyner 	DEVMETHOD(ifdi_update_admin_status, ice_if_update_admin_status),
16671d10453SEric Joyner 	DEVMETHOD(ifdi_multi_set, ice_if_multi_set),
16771d10453SEric Joyner 	DEVMETHOD(ifdi_vlan_register, ice_if_vlan_register),
16871d10453SEric Joyner 	DEVMETHOD(ifdi_vlan_unregister, ice_if_vlan_unregister),
16971d10453SEric Joyner 	DEVMETHOD(ifdi_get_counter, ice_if_get_counter),
17071d10453SEric Joyner 	DEVMETHOD(ifdi_priv_ioctl, ice_if_priv_ioctl),
17171d10453SEric Joyner 	DEVMETHOD(ifdi_i2c_req, ice_if_i2c_req),
1727d7af7f8SEric Joyner 	DEVMETHOD(ifdi_suspend, ice_if_suspend),
1737d7af7f8SEric Joyner 	DEVMETHOD(ifdi_resume, ice_if_resume),
17471d10453SEric Joyner 	DEVMETHOD_END
17571d10453SEric Joyner };
17671d10453SEric Joyner 
17771d10453SEric Joyner /**
17871d10453SEric Joyner  * @var ice_driver
17971d10453SEric Joyner  * @brief driver structure for the generic device stack
18071d10453SEric Joyner  *
18171d10453SEric Joyner  * driver_t definition used to setup the generic device methods.
18271d10453SEric Joyner  */
18371d10453SEric Joyner static driver_t ice_driver = {
18471d10453SEric Joyner 	.name = "ice",
18571d10453SEric Joyner 	.methods = ice_methods,
18671d10453SEric Joyner 	.size = sizeof(struct ice_softc),
18771d10453SEric Joyner };
18871d10453SEric Joyner 
18971d10453SEric Joyner /**
19071d10453SEric Joyner  * @var ice_iflib_driver
19171d10453SEric Joyner  * @brief driver structure for the iflib stack
19271d10453SEric Joyner  *
19371d10453SEric Joyner  * driver_t definition used to setup the iflib device methods.
19471d10453SEric Joyner  */
19571d10453SEric Joyner static driver_t ice_iflib_driver = {
19671d10453SEric Joyner 	.name = "ice",
19771d10453SEric Joyner 	.methods = ice_iflib_methods,
19871d10453SEric Joyner 	.size = sizeof(struct ice_softc),
19971d10453SEric Joyner };
20071d10453SEric Joyner 
20171d10453SEric Joyner extern struct if_txrx ice_txrx;
20271d10453SEric Joyner extern struct if_txrx ice_recovery_txrx;
20371d10453SEric Joyner 
20471d10453SEric Joyner /**
20571d10453SEric Joyner  * @var ice_sctx
20671d10453SEric Joyner  * @brief ice driver shared context
20771d10453SEric Joyner  *
20871d10453SEric Joyner  * Structure defining shared values (context) that is used by all instances of
20971d10453SEric Joyner  * the device. Primarily used to setup details about how the iflib stack
21071d10453SEric Joyner  * should treat this driver. Also defines the default, minimum, and maximum
21171d10453SEric Joyner  * number of descriptors in each ring.
21271d10453SEric Joyner  */
21371d10453SEric Joyner static struct if_shared_ctx ice_sctx = {
21471d10453SEric Joyner 	.isc_magic = IFLIB_MAGIC,
21571d10453SEric Joyner 	.isc_q_align = PAGE_SIZE,
21671d10453SEric Joyner 
21771d10453SEric Joyner 	.isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
21871d10453SEric Joyner 	/* We could technically set this as high as ICE_MAX_DMA_SEG_SIZE, but
21971d10453SEric Joyner 	 * that doesn't make sense since that would be larger than the maximum
22071d10453SEric Joyner 	 * size of a single packet.
22171d10453SEric Joyner 	 */
22271d10453SEric Joyner 	.isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
22371d10453SEric Joyner 
22471d10453SEric Joyner 	/* XXX: This is only used by iflib to ensure that
22571d10453SEric Joyner 	 * scctx->isc_tx_tso_size_max + the VLAN header is a valid size.
22671d10453SEric Joyner 	 */
22771d10453SEric Joyner 	.isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
22871d10453SEric Joyner 	/* XXX: This is used by iflib to set the number of segments in the TSO
22971d10453SEric Joyner 	 * DMA tag. However, scctx->isc_tx_tso_segsize_max is used to set the
23071d10453SEric Joyner 	 * related ifnet parameter.
23171d10453SEric Joyner 	 */
23271d10453SEric Joyner 	.isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
23371d10453SEric Joyner 
23471d10453SEric Joyner 	.isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
23571d10453SEric Joyner 	.isc_rx_nsegments = ICE_MAX_RX_SEGS,
23671d10453SEric Joyner 	.isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
23771d10453SEric Joyner 
23871d10453SEric Joyner 	.isc_nfl = 1,
23971d10453SEric Joyner 	.isc_ntxqs = 1,
24071d10453SEric Joyner 	.isc_nrxqs = 1,
24171d10453SEric Joyner 
24271d10453SEric Joyner 	.isc_admin_intrcnt = 1,
24371d10453SEric Joyner 	.isc_vendor_info = ice_vendor_info_array,
24471d10453SEric Joyner 	.isc_driver_version = __DECONST(char *, ice_driver_version),
24571d10453SEric Joyner 	.isc_driver = &ice_iflib_driver,
24671d10453SEric Joyner 
24771d10453SEric Joyner 	/*
24871d10453SEric Joyner 	 * IFLIB_NEED_SCRATCH ensures that mbufs have scratch space available
24971d10453SEric Joyner 	 * for hardware checksum offload
25071d10453SEric Joyner 	 *
25171d10453SEric Joyner 	 * IFLIB_TSO_INIT_IP ensures that the TSO packets have zeroed out the
25271d10453SEric Joyner 	 * IP sum field, required by our hardware to calculate valid TSO
25371d10453SEric Joyner 	 * checksums.
25471d10453SEric Joyner 	 *
25571d10453SEric Joyner 	 * IFLIB_ADMIN_ALWAYS_RUN ensures that the administrative task runs
25671d10453SEric Joyner 	 * even when the interface is down.
25771d10453SEric Joyner 	 *
25871d10453SEric Joyner 	 * IFLIB_SKIP_MSIX allows the driver to handle allocating MSI-X
25971d10453SEric Joyner 	 * vectors manually instead of relying on iflib code to do this.
26071d10453SEric Joyner 	 */
26171d10453SEric Joyner 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
26271d10453SEric Joyner 		IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX,
26371d10453SEric Joyner 
26471d10453SEric Joyner 	.isc_nrxd_min = {ICE_MIN_DESC_COUNT},
26571d10453SEric Joyner 	.isc_ntxd_min = {ICE_MIN_DESC_COUNT},
26671d10453SEric Joyner 	.isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
26771d10453SEric Joyner 	.isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
26871d10453SEric Joyner 	.isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
26971d10453SEric Joyner 	.isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
27071d10453SEric Joyner };
27171d10453SEric Joyner 
27283c0a9e8SJohn Baldwin DRIVER_MODULE(ice, pci, ice_driver, ice_module_event_handler, NULL);
27371d10453SEric Joyner 
27471d10453SEric Joyner MODULE_VERSION(ice, 1);
27571d10453SEric Joyner MODULE_DEPEND(ice, pci, 1, 1, 1);
27671d10453SEric Joyner MODULE_DEPEND(ice, ether, 1, 1, 1);
27771d10453SEric Joyner MODULE_DEPEND(ice, iflib, 1, 1, 1);
27871d10453SEric Joyner 
27971d10453SEric Joyner IFLIB_PNP_INFO(pci, ice, ice_vendor_info_array);
28071d10453SEric Joyner 
28171d10453SEric Joyner /* Static driver-wide sysctls */
28271d10453SEric Joyner #include "ice_iflib_sysctls.h"
28371d10453SEric Joyner 
28471d10453SEric Joyner /**
28571d10453SEric Joyner  * ice_pci_mapping - Map PCI BAR memory
28671d10453SEric Joyner  * @sc: device private softc
28771d10453SEric Joyner  *
28871d10453SEric Joyner  * Map PCI BAR 0 for device operation.
28971d10453SEric Joyner  */
29071d10453SEric Joyner static int
29171d10453SEric Joyner ice_pci_mapping(struct ice_softc *sc)
29271d10453SEric Joyner {
29371d10453SEric Joyner 	int rc;
29471d10453SEric Joyner 
29571d10453SEric Joyner 	/* Map BAR0 */
29671d10453SEric Joyner 	rc = ice_map_bar(sc->dev, &sc->bar0, 0);
29771d10453SEric Joyner 	if (rc)
29871d10453SEric Joyner 		return rc;
29971d10453SEric Joyner 
30071d10453SEric Joyner 	return 0;
30171d10453SEric Joyner }
30271d10453SEric Joyner 
30371d10453SEric Joyner /**
30471d10453SEric Joyner  * ice_free_pci_mapping - Release PCI BAR memory
30571d10453SEric Joyner  * @sc: device private softc
30671d10453SEric Joyner  *
30771d10453SEric Joyner  * Release PCI BARs which were previously mapped by ice_pci_mapping().
30871d10453SEric Joyner  */
30971d10453SEric Joyner static void
31071d10453SEric Joyner ice_free_pci_mapping(struct ice_softc *sc)
31171d10453SEric Joyner {
31271d10453SEric Joyner 	/* Free BAR0 */
31371d10453SEric Joyner 	ice_free_bar(sc->dev, &sc->bar0);
31471d10453SEric Joyner }
31571d10453SEric Joyner 
31671d10453SEric Joyner /*
31771d10453SEric Joyner  * Device methods
31871d10453SEric Joyner  */
31971d10453SEric Joyner 
32071d10453SEric Joyner /**
32171d10453SEric Joyner  * ice_register - register device method callback
32271d10453SEric Joyner  * @dev: the device being registered
32371d10453SEric Joyner  *
32471d10453SEric Joyner  * Returns a pointer to the shared context structure, which is used by iflib.
32571d10453SEric Joyner  */
32671d10453SEric Joyner static void *
32771d10453SEric Joyner ice_register(device_t dev __unused)
32871d10453SEric Joyner {
32971d10453SEric Joyner 	return &ice_sctx;
33071d10453SEric Joyner } /* ice_register */
33171d10453SEric Joyner 
33271d10453SEric Joyner /**
33371d10453SEric Joyner  * ice_setup_scctx - Setup the iflib softc context structure
33471d10453SEric Joyner  * @sc: the device private structure
33571d10453SEric Joyner  *
33671d10453SEric Joyner  * Setup the parameters in if_softc_ctx_t structure used by the iflib stack
33771d10453SEric Joyner  * when loading.
33871d10453SEric Joyner  */
33971d10453SEric Joyner static void
34071d10453SEric Joyner ice_setup_scctx(struct ice_softc *sc)
34171d10453SEric Joyner {
34271d10453SEric Joyner 	if_softc_ctx_t scctx = sc->scctx;
34371d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
34471d10453SEric Joyner 	bool safe_mode, recovery_mode;
34571d10453SEric Joyner 
34671d10453SEric Joyner 	safe_mode = ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE);
34771d10453SEric Joyner 	recovery_mode = ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE);
34871d10453SEric Joyner 
34971d10453SEric Joyner 	/*
35071d10453SEric Joyner 	 * If the driver loads in Safe mode or Recovery mode, limit iflib to
35171d10453SEric Joyner 	 * a single queue pair.
35271d10453SEric Joyner 	 */
35371d10453SEric Joyner 	if (safe_mode || recovery_mode) {
35471d10453SEric Joyner 		scctx->isc_ntxqsets = scctx->isc_nrxqsets = 1;
35571d10453SEric Joyner 		scctx->isc_ntxqsets_max = 1;
35671d10453SEric Joyner 		scctx->isc_nrxqsets_max = 1;
35771d10453SEric Joyner 	} else {
35871d10453SEric Joyner 		/*
35971d10453SEric Joyner 		 * iflib initially sets the isc_ntxqsets and isc_nrxqsets to
36071d10453SEric Joyner 		 * the values of the override sysctls. Cache these initial
36171d10453SEric Joyner 		 * values so that the driver can be aware of what the iflib
36271d10453SEric Joyner 		 * sysctl value is when setting up MSI-X vectors.
36371d10453SEric Joyner 		 */
36471d10453SEric Joyner 		sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets;
36571d10453SEric Joyner 		sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets;
36671d10453SEric Joyner 
36771d10453SEric Joyner 		if (scctx->isc_ntxqsets == 0)
36871d10453SEric Joyner 			scctx->isc_ntxqsets = hw->func_caps.common_cap.rss_table_size;
36971d10453SEric Joyner 		if (scctx->isc_nrxqsets == 0)
37071d10453SEric Joyner 			scctx->isc_nrxqsets = hw->func_caps.common_cap.rss_table_size;
37171d10453SEric Joyner 
37271d10453SEric Joyner 		scctx->isc_ntxqsets_max = hw->func_caps.common_cap.num_txq;
37371d10453SEric Joyner 		scctx->isc_nrxqsets_max = hw->func_caps.common_cap.num_rxq;
37471d10453SEric Joyner 
37571d10453SEric Joyner 		/*
37671d10453SEric Joyner 		 * Sanity check that the iflib sysctl values are within the
37771d10453SEric Joyner 		 * maximum supported range.
37871d10453SEric Joyner 		 */
37971d10453SEric Joyner 		if (sc->ifc_sysctl_ntxqs > scctx->isc_ntxqsets_max)
38071d10453SEric Joyner 			sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets_max;
38171d10453SEric Joyner 		if (sc->ifc_sysctl_nrxqs > scctx->isc_nrxqsets_max)
38271d10453SEric Joyner 			sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets_max;
38371d10453SEric Joyner 	}
38471d10453SEric Joyner 
38571d10453SEric Joyner 	scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
38671d10453SEric Joyner 	    * sizeof(struct ice_tx_desc), DBA_ALIGN);
38771d10453SEric Joyner 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
38871d10453SEric Joyner 	    * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
38971d10453SEric Joyner 
39071d10453SEric Joyner 	scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
39171d10453SEric Joyner 	scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
39271d10453SEric Joyner 	scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
39371d10453SEric Joyner 	scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
39471d10453SEric Joyner 
39571d10453SEric Joyner 	scctx->isc_msix_bar = PCIR_BAR(ICE_MSIX_BAR);
39671d10453SEric Joyner 	scctx->isc_rss_table_size = hw->func_caps.common_cap.rss_table_size;
39771d10453SEric Joyner 
39871d10453SEric Joyner 	/*
39971d10453SEric Joyner 	 * If the driver loads in recovery mode, disable Tx/Rx functionality
40071d10453SEric Joyner 	 */
40171d10453SEric Joyner 	if (recovery_mode)
40271d10453SEric Joyner 		scctx->isc_txrx = &ice_recovery_txrx;
40371d10453SEric Joyner 	else
40471d10453SEric Joyner 		scctx->isc_txrx = &ice_txrx;
40571d10453SEric Joyner 
40671d10453SEric Joyner 	/*
40771d10453SEric Joyner 	 * If the driver loads in Safe mode or Recovery mode, disable
40871d10453SEric Joyner 	 * advanced features including hardware offloads.
40971d10453SEric Joyner 	 */
41071d10453SEric Joyner 	if (safe_mode || recovery_mode) {
41171d10453SEric Joyner 		scctx->isc_capenable = ICE_SAFE_CAPS;
41271d10453SEric Joyner 		scctx->isc_tx_csum_flags = 0;
41371d10453SEric Joyner 	} else {
41471d10453SEric Joyner 		scctx->isc_capenable = ICE_FULL_CAPS;
41571d10453SEric Joyner 		scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
41671d10453SEric Joyner 	}
41771d10453SEric Joyner 
41871d10453SEric Joyner 	scctx->isc_capabilities = scctx->isc_capenable;
41971d10453SEric Joyner } /* ice_setup_scctx */
42071d10453SEric Joyner 
42171d10453SEric Joyner /**
42271d10453SEric Joyner  * ice_if_attach_pre - Early device attach logic
42371d10453SEric Joyner  * @ctx: the iflib context structure
42471d10453SEric Joyner  *
42571d10453SEric Joyner  * Called by iflib during the attach process. Earliest main driver entry
42671d10453SEric Joyner  * point which performs necessary hardware and driver initialization. Called
42771d10453SEric Joyner  * before the Tx and Rx queues are allocated.
42871d10453SEric Joyner  */
42971d10453SEric Joyner static int
43071d10453SEric Joyner ice_if_attach_pre(if_ctx_t ctx)
43171d10453SEric Joyner {
43271d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
43371d10453SEric Joyner 	enum ice_fw_modes fw_mode;
43471d10453SEric Joyner 	enum ice_status status;
43571d10453SEric Joyner 	if_softc_ctx_t scctx;
43671d10453SEric Joyner 	struct ice_hw *hw;
43771d10453SEric Joyner 	device_t dev;
43871d10453SEric Joyner 	int err;
43971d10453SEric Joyner 
44071d10453SEric Joyner 	device_printf(iflib_get_dev(ctx), "Loading the iflib ice driver\n");
44171d10453SEric Joyner 
44256429daeSEric Joyner 	ice_set_state(&sc->state, ICE_STATE_ATTACHING);
44356429daeSEric Joyner 
44471d10453SEric Joyner 	sc->ctx = ctx;
44571d10453SEric Joyner 	sc->media = iflib_get_media(ctx);
44671d10453SEric Joyner 	sc->sctx = iflib_get_sctx(ctx);
44771d10453SEric Joyner 	sc->iflib_ctx_lock = iflib_ctx_lock_get(ctx);
44871d10453SEric Joyner 
44971d10453SEric Joyner 	dev = sc->dev = iflib_get_dev(ctx);
45071d10453SEric Joyner 	scctx = sc->scctx = iflib_get_softc_ctx(ctx);
45171d10453SEric Joyner 
45271d10453SEric Joyner 	hw = &sc->hw;
45371d10453SEric Joyner 	hw->back = sc;
45471d10453SEric Joyner 
45571d10453SEric Joyner 	snprintf(sc->admin_mtx_name, sizeof(sc->admin_mtx_name),
45671d10453SEric Joyner 		 "%s:admin", device_get_nameunit(dev));
45771d10453SEric Joyner 	mtx_init(&sc->admin_mtx, sc->admin_mtx_name, NULL, MTX_DEF);
45871d10453SEric Joyner 	callout_init_mtx(&sc->admin_timer, &sc->admin_mtx, 0);
45971d10453SEric Joyner 
46071d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
46171d10453SEric Joyner 
46271d10453SEric Joyner 	if (ice_pci_mapping(sc)) {
46371d10453SEric Joyner 		err = (ENXIO);
46471d10453SEric Joyner 		goto destroy_admin_timer;
46571d10453SEric Joyner 	}
46671d10453SEric Joyner 
46771d10453SEric Joyner 	/* Save off the PCI information */
46871d10453SEric Joyner 	ice_save_pci_info(hw, dev);
46971d10453SEric Joyner 
47071d10453SEric Joyner 	/* create tunables as early as possible */
47171d10453SEric Joyner 	ice_add_device_tunables(sc);
47271d10453SEric Joyner 
47371d10453SEric Joyner 	/* Setup ControlQ lengths */
47471d10453SEric Joyner 	ice_set_ctrlq_len(hw);
47571d10453SEric Joyner 
476*8923de59SPiotr Kubaj reinit_hw:
477*8923de59SPiotr Kubaj 
47871d10453SEric Joyner 	fw_mode = ice_get_fw_mode(hw);
47971d10453SEric Joyner 	if (fw_mode == ICE_FW_MODE_REC) {
48071d10453SEric Joyner 		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");
48171d10453SEric Joyner 
48271d10453SEric Joyner 		err = ice_attach_pre_recovery_mode(sc);
48371d10453SEric Joyner 		if (err)
48471d10453SEric Joyner 			goto free_pci_mapping;
48571d10453SEric Joyner 
48671d10453SEric Joyner 		return (0);
48771d10453SEric Joyner 	}
48871d10453SEric Joyner 
48971d10453SEric Joyner 	/* Initialize the hw data structure */
49071d10453SEric Joyner 	status = ice_init_hw(hw);
49171d10453SEric Joyner 	if (status) {
49271d10453SEric Joyner 		if (status == ICE_ERR_FW_API_VER) {
49371d10453SEric Joyner 			/* Enter recovery mode, so that the driver remains
49471d10453SEric Joyner 			 * loaded. This way, if the system administrator
49571d10453SEric Joyner 			 * cannot update the driver, they may still attempt to
49671d10453SEric Joyner 			 * downgrade the NVM.
49771d10453SEric Joyner 			 */
49871d10453SEric Joyner 			err = ice_attach_pre_recovery_mode(sc);
49971d10453SEric Joyner 			if (err)
50071d10453SEric Joyner 				goto free_pci_mapping;
50171d10453SEric Joyner 
50271d10453SEric Joyner 			return (0);
50371d10453SEric Joyner 		} else {
50471d10453SEric Joyner 			err = EIO;
50571d10453SEric Joyner 			device_printf(dev, "Unable to initialize hw, err %s aq_err %s\n",
50671d10453SEric Joyner 				      ice_status_str(status),
50771d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
50871d10453SEric Joyner 		}
50971d10453SEric Joyner 		goto free_pci_mapping;
51071d10453SEric Joyner 	}
51171d10453SEric Joyner 
512*8923de59SPiotr Kubaj 	ice_init_device_features(sc);
513*8923de59SPiotr Kubaj 
51471d10453SEric Joyner 	/* Notify firmware of the device driver version */
51571d10453SEric Joyner 	err = ice_send_version(sc);
51671d10453SEric Joyner 	if (err)
51771d10453SEric Joyner 		goto deinit_hw;
51871d10453SEric Joyner 
519*8923de59SPiotr Kubaj 	/*
520*8923de59SPiotr Kubaj 	 * Success indicates a change was made that requires a reinitialization
521*8923de59SPiotr Kubaj 	 * of the hardware
522*8923de59SPiotr Kubaj 	 */
523*8923de59SPiotr Kubaj 	err = ice_load_pkg_file(sc);
524*8923de59SPiotr Kubaj 	if (err == ICE_SUCCESS) {
525*8923de59SPiotr Kubaj 		ice_deinit_hw(hw);
526*8923de59SPiotr Kubaj 		goto reinit_hw;
527*8923de59SPiotr Kubaj 	}
52871d10453SEric Joyner 
52971d10453SEric Joyner 	err = ice_init_link_events(sc);
53071d10453SEric Joyner 	if (err) {
53171d10453SEric Joyner 		device_printf(dev, "ice_init_link_events failed: %s\n",
53271d10453SEric Joyner 			      ice_err_str(err));
53371d10453SEric Joyner 		goto deinit_hw;
53471d10453SEric Joyner 	}
53571d10453SEric Joyner 
536*8923de59SPiotr Kubaj 	/* Initialize VLAN mode in FW; if dual VLAN mode is supported by the package
537*8923de59SPiotr Kubaj 	 * and firmware, this will force them to use single VLAN mode.
538*8923de59SPiotr Kubaj 	 */
539*8923de59SPiotr Kubaj 	status = ice_set_vlan_mode(hw);
540*8923de59SPiotr Kubaj 	if (status) {
541*8923de59SPiotr Kubaj 		err = EIO;
542*8923de59SPiotr Kubaj 		device_printf(dev, "Unable to initialize VLAN mode, err %s aq_err %s\n",
543*8923de59SPiotr Kubaj 			      ice_status_str(status),
544*8923de59SPiotr Kubaj 			      ice_aq_str(hw->adminq.sq_last_status));
545*8923de59SPiotr Kubaj 		goto deinit_hw;
546*8923de59SPiotr Kubaj 	}
54771d10453SEric Joyner 
548*8923de59SPiotr Kubaj 	ice_print_nvm_version(sc);
54971d10453SEric Joyner 
55071d10453SEric Joyner 	/* Setup the MAC address */
55171d10453SEric Joyner 	iflib_set_mac(ctx, hw->port_info->mac.lan_addr);
55271d10453SEric Joyner 
55371d10453SEric Joyner 	/* Setup the iflib softc context structure */
55471d10453SEric Joyner 	ice_setup_scctx(sc);
55571d10453SEric Joyner 
55671d10453SEric Joyner 	/* Initialize the Tx queue manager */
55771d10453SEric Joyner 	err = ice_resmgr_init(&sc->tx_qmgr, hw->func_caps.common_cap.num_txq);
55871d10453SEric Joyner 	if (err) {
55971d10453SEric Joyner 		device_printf(dev, "Unable to initialize Tx queue manager: %s\n",
56071d10453SEric Joyner 			      ice_err_str(err));
56171d10453SEric Joyner 		goto deinit_hw;
56271d10453SEric Joyner 	}
56371d10453SEric Joyner 
56471d10453SEric Joyner 	/* Initialize the Rx queue manager */
56571d10453SEric Joyner 	err = ice_resmgr_init(&sc->rx_qmgr, hw->func_caps.common_cap.num_rxq);
56671d10453SEric Joyner 	if (err) {
56771d10453SEric Joyner 		device_printf(dev, "Unable to initialize Rx queue manager: %s\n",
56871d10453SEric Joyner 			      ice_err_str(err));
56971d10453SEric Joyner 		goto free_tx_qmgr;
57071d10453SEric Joyner 	}
57171d10453SEric Joyner 
57271d10453SEric Joyner 	/* Initialize the interrupt resource manager */
57371d10453SEric Joyner 	err = ice_alloc_intr_tracking(sc);
57471d10453SEric Joyner 	if (err)
57571d10453SEric Joyner 		/* Errors are already printed */
57671d10453SEric Joyner 		goto free_rx_qmgr;
57771d10453SEric Joyner 
57871d10453SEric Joyner 	/* Determine maximum number of VSIs we'll prepare for */
57971d10453SEric Joyner 	sc->num_available_vsi = min(ICE_MAX_VSI_AVAILABLE,
58071d10453SEric Joyner 				    hw->func_caps.guar_num_vsi);
58171d10453SEric Joyner 
58271d10453SEric Joyner 	if (!sc->num_available_vsi) {
58371d10453SEric Joyner 		err = EIO;
58471d10453SEric Joyner 		device_printf(dev, "No VSIs allocated to host\n");
58571d10453SEric Joyner 		goto free_intr_tracking;
58671d10453SEric Joyner 	}
58771d10453SEric Joyner 
58871d10453SEric Joyner 	/* Allocate storage for the VSI pointers */
58971d10453SEric Joyner 	sc->all_vsi = (struct ice_vsi **)
59071d10453SEric Joyner 		malloc(sizeof(struct ice_vsi *) * sc->num_available_vsi,
59171d10453SEric Joyner 		       M_ICE, M_WAITOK | M_ZERO);
59271d10453SEric Joyner 	if (!sc->all_vsi) {
59371d10453SEric Joyner 		err = ENOMEM;
59471d10453SEric Joyner 		device_printf(dev, "Unable to allocate VSI array\n");
59571d10453SEric Joyner 		goto free_intr_tracking;
59671d10453SEric Joyner 	}
59771d10453SEric Joyner 
59871d10453SEric Joyner 	/*
59971d10453SEric Joyner 	 * Prepare the statically allocated primary PF VSI in the softc
60071d10453SEric Joyner 	 * structure. Other VSIs will be dynamically allocated as needed.
60171d10453SEric Joyner 	 */
60271d10453SEric Joyner 	ice_setup_pf_vsi(sc);
60371d10453SEric Joyner 
60471d10453SEric Joyner 	err = ice_alloc_vsi_qmap(&sc->pf_vsi, scctx->isc_ntxqsets_max,
60571d10453SEric Joyner 	    scctx->isc_nrxqsets_max);
60671d10453SEric Joyner 	if (err) {
60771d10453SEric Joyner 		device_printf(dev, "Unable to allocate VSI Queue maps\n");
60871d10453SEric Joyner 		goto free_main_vsi;
60971d10453SEric Joyner 	}
61071d10453SEric Joyner 
61171d10453SEric Joyner 	/* Allocate MSI-X vectors (due to isc_flags IFLIB_SKIP_MSIX) */
61271d10453SEric Joyner 	err = ice_allocate_msix(sc);
61371d10453SEric Joyner 	if (err)
61471d10453SEric Joyner 		goto free_main_vsi;
61571d10453SEric Joyner 
61671d10453SEric Joyner 	return 0;
61771d10453SEric Joyner 
61871d10453SEric Joyner free_main_vsi:
61971d10453SEric Joyner 	/* ice_release_vsi will free the queue maps if they were allocated */
62071d10453SEric Joyner 	ice_release_vsi(&sc->pf_vsi);
62171d10453SEric Joyner 	free(sc->all_vsi, M_ICE);
62271d10453SEric Joyner 	sc->all_vsi = NULL;
62371d10453SEric Joyner free_intr_tracking:
62471d10453SEric Joyner 	ice_free_intr_tracking(sc);
62571d10453SEric Joyner free_rx_qmgr:
62671d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
62771d10453SEric Joyner free_tx_qmgr:
62871d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
62971d10453SEric Joyner deinit_hw:
63071d10453SEric Joyner 	ice_deinit_hw(hw);
63171d10453SEric Joyner free_pci_mapping:
63271d10453SEric Joyner 	ice_free_pci_mapping(sc);
63371d10453SEric Joyner destroy_admin_timer:
63471d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
63571d10453SEric Joyner 	callout_stop(&sc->admin_timer);
63671d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
63771d10453SEric Joyner 	mtx_destroy(&sc->admin_mtx);
63871d10453SEric Joyner 	return err;
63971d10453SEric Joyner } /* ice_if_attach_pre */
64071d10453SEric Joyner 
64171d10453SEric Joyner /**
64271d10453SEric Joyner  * ice_attach_pre_recovery_mode - Limited driver attach_pre for FW recovery
64371d10453SEric Joyner  * @sc: the device private softc
64471d10453SEric Joyner  *
64571d10453SEric Joyner  * Loads the device driver in limited Firmware Recovery mode, intended to
64671d10453SEric Joyner  * allow users to update the firmware to attempt to recover the device.
64771d10453SEric Joyner  *
64871d10453SEric Joyner  * @remark We may enter recovery mode in case either (a) the firmware is
64971d10453SEric Joyner  * detected to be in an invalid state and must be re-programmed, or (b) the
65071d10453SEric Joyner  * driver detects that the loaded firmware has a non-compatible API version
65171d10453SEric Joyner  * that the driver cannot operate with.
65271d10453SEric Joyner  */
65371d10453SEric Joyner static int
65471d10453SEric Joyner ice_attach_pre_recovery_mode(struct ice_softc *sc)
65571d10453SEric Joyner {
65671d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_RECOVERY_MODE);
65771d10453SEric Joyner 
65871d10453SEric Joyner 	/* Setup the iflib softc context */
65971d10453SEric Joyner 	ice_setup_scctx(sc);
66071d10453SEric Joyner 
66171d10453SEric Joyner 	/* Setup the PF VSI back pointer */
66271d10453SEric Joyner 	sc->pf_vsi.sc = sc;
66371d10453SEric Joyner 
66471d10453SEric Joyner 	/*
66571d10453SEric Joyner 	 * We still need to allocate MSI-X vectors since we need one vector to
66671d10453SEric Joyner 	 * run the administrative admin interrupt
66771d10453SEric Joyner 	 */
66871d10453SEric Joyner 	return ice_allocate_msix(sc);
66971d10453SEric Joyner }
67071d10453SEric Joyner 
67171d10453SEric Joyner /**
67271d10453SEric Joyner  * ice_update_link_status - notify OS of link state change
67371d10453SEric Joyner  * @sc: device private softc structure
67471d10453SEric Joyner  * @update_media: true if we should update media even if link didn't change
67571d10453SEric Joyner  *
67671d10453SEric Joyner  * Called to notify iflib core of link status changes. Should be called once
67771d10453SEric Joyner  * during attach_post, and whenever link status changes during runtime.
67871d10453SEric Joyner  *
67971d10453SEric Joyner  * This call only updates the currently supported media types if the link
68071d10453SEric Joyner  * status changed, or if update_media is set to true.
68171d10453SEric Joyner  */
68271d10453SEric Joyner static void
68371d10453SEric Joyner ice_update_link_status(struct ice_softc *sc, bool update_media)
68471d10453SEric Joyner {
68571d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
6867d7af7f8SEric Joyner 	enum ice_status status;
68771d10453SEric Joyner 
68871d10453SEric Joyner 	/* Never report link up when in recovery mode */
68971d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
69071d10453SEric Joyner 		return;
69171d10453SEric Joyner 
69271d10453SEric Joyner 	/* Report link status to iflib only once each time it changes */
69371d10453SEric Joyner 	if (!ice_testandset_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED)) {
69471d10453SEric Joyner 		if (sc->link_up) { /* link is up */
69571d10453SEric Joyner 			uint64_t baudrate = ice_aq_speed_to_rate(sc->hw.port_info);
69671d10453SEric Joyner 
6977d7af7f8SEric Joyner 			ice_set_default_local_lldp_mib(sc);
6987d7af7f8SEric Joyner 
69971d10453SEric Joyner 			iflib_link_state_change(sc->ctx, LINK_STATE_UP, baudrate);
7008a13362dSEric Joyner 			ice_rdma_link_change(sc, LINK_STATE_UP, baudrate);
70171d10453SEric Joyner 
70271d10453SEric Joyner 			ice_link_up_msg(sc);
70371d10453SEric Joyner 
70471d10453SEric Joyner 			update_media = true;
70571d10453SEric Joyner 		} else { /* link is down */
70671d10453SEric Joyner 			iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
7078a13362dSEric Joyner 			ice_rdma_link_change(sc, LINK_STATE_DOWN, 0);
70871d10453SEric Joyner 
70971d10453SEric Joyner 			update_media = true;
71071d10453SEric Joyner 		}
71171d10453SEric Joyner 	}
71271d10453SEric Joyner 
71371d10453SEric Joyner 	/* Update the supported media types */
71471d10453SEric Joyner 	if (update_media) {
7157d7af7f8SEric Joyner 		status = ice_add_media_types(sc, sc->media);
71671d10453SEric Joyner 		if (status)
71771d10453SEric Joyner 			device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
71871d10453SEric Joyner 				      ice_status_str(status),
71971d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
72071d10453SEric Joyner 	}
72171d10453SEric Joyner 
72271d10453SEric Joyner 	/* TODO: notify VFs of link state change */
72371d10453SEric Joyner }
72471d10453SEric Joyner 
72571d10453SEric Joyner /**
72671d10453SEric Joyner  * ice_if_attach_post - Late device attach logic
72771d10453SEric Joyner  * @ctx: the iflib context structure
72871d10453SEric Joyner  *
72971d10453SEric Joyner  * Called by iflib to finish up attaching the device. Performs any attach
73071d10453SEric Joyner  * logic which must wait until after the Tx and Rx queues have been
73171d10453SEric Joyner  * allocated.
73271d10453SEric Joyner  */
73371d10453SEric Joyner static int
73471d10453SEric Joyner ice_if_attach_post(if_ctx_t ctx)
73571d10453SEric Joyner {
73671d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
73771d10453SEric Joyner 	if_t ifp = iflib_get_ifp(ctx);
73871d10453SEric Joyner 	int err;
73971d10453SEric Joyner 
74071d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
74171d10453SEric Joyner 
74271d10453SEric Joyner 	/* We don't yet support loading if MSI-X is not supported */
74371d10453SEric Joyner 	if (sc->scctx->isc_intr != IFLIB_INTR_MSIX) {
74471d10453SEric Joyner 		device_printf(sc->dev, "The ice driver does not support loading without MSI-X\n");
74571d10453SEric Joyner 		return (ENOTSUP);
74671d10453SEric Joyner 	}
74771d10453SEric Joyner 
74871d10453SEric Joyner 	/* The ifnet structure hasn't yet been initialized when the attach_pre
74971d10453SEric Joyner 	 * handler is called, so wait until attach_post to setup the
75071d10453SEric Joyner 	 * isc_max_frame_size.
75171d10453SEric Joyner 	 */
75271d10453SEric Joyner 
75371d10453SEric Joyner 	sc->ifp = ifp;
754402810d3SJustin Hibbits 	sc->scctx->isc_max_frame_size = if_getmtu(ifp) +
75571d10453SEric Joyner 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
75671d10453SEric Joyner 
75771d10453SEric Joyner 	/*
75871d10453SEric Joyner 	 * If we are in recovery mode, only perform a limited subset of
75971d10453SEric Joyner 	 * initialization to support NVM recovery.
76071d10453SEric Joyner 	 */
76171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
76271d10453SEric Joyner 		ice_attach_post_recovery_mode(sc);
76371d10453SEric Joyner 		return (0);
76471d10453SEric Joyner 	}
76571d10453SEric Joyner 
76671d10453SEric Joyner 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
76771d10453SEric Joyner 
76871d10453SEric Joyner 	err = ice_initialize_vsi(&sc->pf_vsi);
76971d10453SEric Joyner 	if (err) {
77071d10453SEric Joyner 		device_printf(sc->dev, "Unable to initialize Main VSI: %s\n",
77171d10453SEric Joyner 			      ice_err_str(err));
77271d10453SEric Joyner 		return err;
77371d10453SEric Joyner 	}
77471d10453SEric Joyner 
7759cf1841cSEric Joyner 	/* Enable FW health event reporting */
7769cf1841cSEric Joyner 	ice_init_health_events(sc);
7779cf1841cSEric Joyner 
77871d10453SEric Joyner 	/* Configure the main PF VSI for RSS */
77971d10453SEric Joyner 	err = ice_config_rss(&sc->pf_vsi);
78071d10453SEric Joyner 	if (err) {
78171d10453SEric Joyner 		device_printf(sc->dev,
78271d10453SEric Joyner 			      "Unable to configure RSS for the main VSI, err %s\n",
78371d10453SEric Joyner 			      ice_err_str(err));
78471d10453SEric Joyner 		return err;
78571d10453SEric Joyner 	}
78671d10453SEric Joyner 
78771d10453SEric Joyner 	/* Configure switch to drop transmitted LLDP and PAUSE frames */
78871d10453SEric Joyner 	err = ice_cfg_pf_ethertype_filters(sc);
78971d10453SEric Joyner 	if (err)
79071d10453SEric Joyner 		return err;
79171d10453SEric Joyner 
79271d10453SEric Joyner 	ice_get_and_print_bus_info(sc);
79371d10453SEric Joyner 
79471d10453SEric Joyner 	ice_set_link_management_mode(sc);
79571d10453SEric Joyner 
79671d10453SEric Joyner 	ice_init_saved_phy_cfg(sc);
79771d10453SEric Joyner 
79856429daeSEric Joyner 	ice_cfg_pba_num(sc);
79956429daeSEric Joyner 
80071d10453SEric Joyner 	ice_add_device_sysctls(sc);
80171d10453SEric Joyner 
80271d10453SEric Joyner 	/* Get DCBX/LLDP state and start DCBX agent */
80371d10453SEric Joyner 	ice_init_dcb_setup(sc);
80471d10453SEric Joyner 
80571d10453SEric Joyner 	/* Setup link configuration parameters */
80671d10453SEric Joyner 	ice_init_link_configuration(sc);
80771d10453SEric Joyner 	ice_update_link_status(sc, true);
80871d10453SEric Joyner 
80971d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
81071d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
81171d10453SEric Joyner 
81271d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
81371d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
81471d10453SEric Joyner 
8158a13362dSEric Joyner 	err = ice_rdma_pf_attach(sc);
8168a13362dSEric Joyner 	if (err)
8178a13362dSEric Joyner 		return (err);
8188a13362dSEric Joyner 
81971d10453SEric Joyner 	/* Start the admin timer */
82071d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
82171d10453SEric Joyner 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
82271d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
82371d10453SEric Joyner 
82456429daeSEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
82556429daeSEric Joyner 
82671d10453SEric Joyner 	return 0;
82771d10453SEric Joyner } /* ice_if_attach_post */
82871d10453SEric Joyner 
82971d10453SEric Joyner /**
83071d10453SEric Joyner  * ice_attach_post_recovery_mode - Limited driver attach_post for FW recovery
83171d10453SEric Joyner  * @sc: the device private softc
83271d10453SEric Joyner  *
83371d10453SEric Joyner  * Performs minimal work to prepare the driver to recover an NVM in case the
83471d10453SEric Joyner  * firmware is in recovery mode.
83571d10453SEric Joyner  */
83671d10453SEric Joyner static void
83771d10453SEric Joyner ice_attach_post_recovery_mode(struct ice_softc *sc)
83871d10453SEric Joyner {
83971d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
84071d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
84171d10453SEric Joyner 
84271d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
84371d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
84471d10453SEric Joyner 
84571d10453SEric Joyner 	/* Start the admin timer */
84671d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
84771d10453SEric Joyner 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
84871d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
84956429daeSEric Joyner 
85056429daeSEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
85171d10453SEric Joyner }
85271d10453SEric Joyner 
85371d10453SEric Joyner /**
85471d10453SEric Joyner  * ice_free_irqvs - Free IRQ vector memory
85571d10453SEric Joyner  * @sc: the device private softc structure
85671d10453SEric Joyner  *
85771d10453SEric Joyner  * Free IRQ vector memory allocated during ice_if_msix_intr_assign.
85871d10453SEric Joyner  */
85971d10453SEric Joyner static void
86071d10453SEric Joyner ice_free_irqvs(struct ice_softc *sc)
86171d10453SEric Joyner {
86271d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
86371d10453SEric Joyner 	if_ctx_t ctx = sc->ctx;
86471d10453SEric Joyner 	int i;
86571d10453SEric Joyner 
86671d10453SEric Joyner 	/* If the irqvs array is NULL, then there are no vectors to free */
86771d10453SEric Joyner 	if (sc->irqvs == NULL)
86871d10453SEric Joyner 		return;
86971d10453SEric Joyner 
87071d10453SEric Joyner 	/* Free the IRQ vectors */
87171d10453SEric Joyner 	for (i = 0; i < sc->num_irq_vectors; i++)
87271d10453SEric Joyner 		iflib_irq_free(ctx, &sc->irqvs[i].irq);
87371d10453SEric Joyner 
87471d10453SEric Joyner 	/* Clear the irqv pointers */
87571d10453SEric Joyner 	for (i = 0; i < vsi->num_rx_queues; i++)
87671d10453SEric Joyner 		vsi->rx_queues[i].irqv = NULL;
87771d10453SEric Joyner 
87871d10453SEric Joyner 	for (i = 0; i < vsi->num_tx_queues; i++)
87971d10453SEric Joyner 		vsi->tx_queues[i].irqv = NULL;
88071d10453SEric Joyner 
88171d10453SEric Joyner 	/* Release the vector array memory */
88271d10453SEric Joyner 	free(sc->irqvs, M_ICE);
88371d10453SEric Joyner 	sc->irqvs = NULL;
88471d10453SEric Joyner 	sc->num_irq_vectors = 0;
88571d10453SEric Joyner }
88671d10453SEric Joyner 
88771d10453SEric Joyner /**
88871d10453SEric Joyner  * ice_if_detach - Device driver detach logic
88971d10453SEric Joyner  * @ctx: iflib context structure
89071d10453SEric Joyner  *
89171d10453SEric Joyner  * Perform device shutdown logic to detach the device driver.
89271d10453SEric Joyner  *
89371d10453SEric Joyner  * Note that there is no guarantee of the ordering of ice_if_queues_free() and
89471d10453SEric Joyner  * ice_if_detach(). It is possible for the functions to be called in either
89571d10453SEric Joyner  * order, and they must not assume to have a strict ordering.
89671d10453SEric Joyner  */
89771d10453SEric Joyner static int
89871d10453SEric Joyner ice_if_detach(if_ctx_t ctx)
89971d10453SEric Joyner {
90071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
90171d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
90271d10453SEric Joyner 	int i;
90371d10453SEric Joyner 
90471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
90571d10453SEric Joyner 
90671d10453SEric Joyner 	/* Indicate that we're detaching */
90771d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_DETACHING);
90871d10453SEric Joyner 
90971d10453SEric Joyner 	/* Stop the admin timer */
91071d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
91171d10453SEric Joyner 	callout_stop(&sc->admin_timer);
91271d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
91371d10453SEric Joyner 	mtx_destroy(&sc->admin_mtx);
91471d10453SEric Joyner 
9158a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
9168a13362dSEric Joyner 
91771d10453SEric Joyner 	/* Free allocated media types */
91871d10453SEric Joyner 	ifmedia_removeall(sc->media);
91971d10453SEric Joyner 
92071d10453SEric Joyner 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
92171d10453SEric Joyner 	 * pointers. Note, the calls here and those in ice_if_queues_free()
92271d10453SEric Joyner 	 * are *BOTH* necessary, as we cannot guarantee which path will be
92371d10453SEric Joyner 	 * run first
92471d10453SEric Joyner 	 */
92571d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
92671d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
92771d10453SEric Joyner 
92871d10453SEric Joyner 	/* Release MSI-X resources */
92971d10453SEric Joyner 	ice_free_irqvs(sc);
93071d10453SEric Joyner 
93171d10453SEric Joyner 	for (i = 0; i < sc->num_available_vsi; i++) {
93271d10453SEric Joyner 		if (sc->all_vsi[i])
93371d10453SEric Joyner 			ice_release_vsi(sc->all_vsi[i]);
93471d10453SEric Joyner 	}
93571d10453SEric Joyner 
93671d10453SEric Joyner 	if (sc->all_vsi) {
93771d10453SEric Joyner 		free(sc->all_vsi, M_ICE);
93871d10453SEric Joyner 		sc->all_vsi = NULL;
93971d10453SEric Joyner 	}
94071d10453SEric Joyner 
94171d10453SEric Joyner 	/* Release MSI-X memory */
94271d10453SEric Joyner 	pci_release_msi(sc->dev);
94371d10453SEric Joyner 
94471d10453SEric Joyner 	if (sc->msix_table != NULL) {
94571d10453SEric Joyner 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
94671d10453SEric Joyner 				     rman_get_rid(sc->msix_table),
94771d10453SEric Joyner 				     sc->msix_table);
94871d10453SEric Joyner 		sc->msix_table = NULL;
94971d10453SEric Joyner 	}
95071d10453SEric Joyner 
95171d10453SEric Joyner 	ice_free_intr_tracking(sc);
95271d10453SEric Joyner 
95371d10453SEric Joyner 	/* Destroy the queue managers */
95471d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
95571d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
95671d10453SEric Joyner 
95771d10453SEric Joyner 	if (!ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
95871d10453SEric Joyner 		ice_deinit_hw(&sc->hw);
95971d10453SEric Joyner 
96071d10453SEric Joyner 	ice_free_pci_mapping(sc);
96171d10453SEric Joyner 
96271d10453SEric Joyner 	return 0;
96371d10453SEric Joyner } /* ice_if_detach */
96471d10453SEric Joyner 
96571d10453SEric Joyner /**
96671d10453SEric Joyner  * ice_if_tx_queues_alloc - Allocate Tx queue memory
96771d10453SEric Joyner  * @ctx: iflib context structure
96871d10453SEric Joyner  * @vaddrs: virtual addresses for the queue memory
96971d10453SEric Joyner  * @paddrs: physical addresses for the queue memory
97071d10453SEric Joyner  * @ntxqs: the number of Tx queues per set (should always be 1)
97171d10453SEric Joyner  * @ntxqsets: the number of Tx queue sets to allocate
97271d10453SEric Joyner  *
97371d10453SEric Joyner  * Called by iflib to allocate Tx queues for the device. Allocates driver
97471d10453SEric Joyner  * memory to track each queue, the status arrays used for descriptor
97571d10453SEric Joyner  * status reporting, and Tx queue sysctls.
97671d10453SEric Joyner  */
97771d10453SEric Joyner static int
97871d10453SEric Joyner ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
97971d10453SEric Joyner 		       int __invariant_only ntxqs, int ntxqsets)
98071d10453SEric Joyner {
98171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
98271d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
98371d10453SEric Joyner 	struct ice_tx_queue *txq;
98471d10453SEric Joyner 	int err, i, j;
98571d10453SEric Joyner 
98671d10453SEric Joyner 	MPASS(ntxqs == 1);
98771d10453SEric Joyner 	MPASS(sc->scctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
98871d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
98971d10453SEric Joyner 
99071d10453SEric Joyner 	/* Do not bother allocating queues if we're in recovery mode */
99171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
99271d10453SEric Joyner 		return (0);
99371d10453SEric Joyner 
99471d10453SEric Joyner 	/* Allocate queue structure memory */
99571d10453SEric Joyner 	if (!(vsi->tx_queues =
996*8923de59SPiotr Kubaj 	      (struct ice_tx_queue *) malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
99771d10453SEric Joyner 		device_printf(sc->dev, "Unable to allocate Tx queue memory\n");
99871d10453SEric Joyner 		return (ENOMEM);
99971d10453SEric Joyner 	}
100071d10453SEric Joyner 
100171d10453SEric Joyner 	/* Allocate report status arrays */
100271d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
100371d10453SEric Joyner 		if (!(txq->tx_rsq =
1004*8923de59SPiotr Kubaj 		      (uint16_t *) malloc(sizeof(uint16_t) * sc->scctx->isc_ntxd[0], M_ICE, M_NOWAIT))) {
100571d10453SEric Joyner 			device_printf(sc->dev, "Unable to allocate tx_rsq memory\n");
100671d10453SEric Joyner 			err = ENOMEM;
100771d10453SEric Joyner 			goto free_tx_queues;
100871d10453SEric Joyner 		}
100971d10453SEric Joyner 		/* Initialize report status array */
101071d10453SEric Joyner 		for (j = 0; j < sc->scctx->isc_ntxd[0]; j++)
101171d10453SEric Joyner 			txq->tx_rsq[j] = QIDX_INVALID;
101271d10453SEric Joyner 	}
101371d10453SEric Joyner 
101471d10453SEric Joyner 	/* Assign queues from PF space to the main VSI */
101571d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap, ntxqsets);
101671d10453SEric Joyner 	if (err) {
101771d10453SEric Joyner 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
101871d10453SEric Joyner 			      ice_err_str(err));
101971d10453SEric Joyner 		goto free_tx_queues;
102071d10453SEric Joyner 	}
102171d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
102271d10453SEric Joyner 
102371d10453SEric Joyner 	/* Add Tx queue sysctls context */
102471d10453SEric Joyner 	ice_vsi_add_txqs_ctx(vsi);
102571d10453SEric Joyner 
102671d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
102756429daeSEric Joyner 		/* q_handle == me when only one TC */
102856429daeSEric Joyner 		txq->me = txq->q_handle = i;
102971d10453SEric Joyner 		txq->vsi = vsi;
103071d10453SEric Joyner 
103171d10453SEric Joyner 		/* store the queue size for easier access */
103271d10453SEric Joyner 		txq->desc_count = sc->scctx->isc_ntxd[0];
103371d10453SEric Joyner 
103471d10453SEric Joyner 		/* get the virtual and physical address of the hardware queues */
103571d10453SEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
103671d10453SEric Joyner 		txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
103771d10453SEric Joyner 		txq->tx_paddr = paddrs[i];
103871d10453SEric Joyner 
103971d10453SEric Joyner 		ice_add_txq_sysctls(txq);
104071d10453SEric Joyner 	}
104171d10453SEric Joyner 
104271d10453SEric Joyner 	vsi->num_tx_queues = ntxqsets;
104371d10453SEric Joyner 
104471d10453SEric Joyner 	return (0);
104571d10453SEric Joyner 
104671d10453SEric Joyner free_tx_queues:
104771d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
104871d10453SEric Joyner 		if (txq->tx_rsq != NULL) {
104971d10453SEric Joyner 			free(txq->tx_rsq, M_ICE);
105071d10453SEric Joyner 			txq->tx_rsq = NULL;
105171d10453SEric Joyner 		}
105271d10453SEric Joyner 	}
105371d10453SEric Joyner 	free(vsi->tx_queues, M_ICE);
105471d10453SEric Joyner 	vsi->tx_queues = NULL;
105571d10453SEric Joyner 	return err;
105671d10453SEric Joyner }
105771d10453SEric Joyner 
105871d10453SEric Joyner /**
105971d10453SEric Joyner  * ice_if_rx_queues_alloc - Allocate Rx queue memory
106071d10453SEric Joyner  * @ctx: iflib context structure
106171d10453SEric Joyner  * @vaddrs: virtual addresses for the queue memory
106271d10453SEric Joyner  * @paddrs: physical addresses for the queue memory
106371d10453SEric Joyner  * @nrxqs: number of Rx queues per set (should always be 1)
106471d10453SEric Joyner  * @nrxqsets: number of Rx queue sets to allocate
106571d10453SEric Joyner  *
106671d10453SEric Joyner  * Called by iflib to allocate Rx queues for the device. Allocates driver
106771d10453SEric Joyner  * memory to track each queue, as well as sets up the Rx queue sysctls.
106871d10453SEric Joyner  */
106971d10453SEric Joyner static int
107071d10453SEric Joyner ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
107171d10453SEric Joyner 		       int __invariant_only nrxqs, int nrxqsets)
107271d10453SEric Joyner {
107371d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
107471d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
107571d10453SEric Joyner 	struct ice_rx_queue *rxq;
107671d10453SEric Joyner 	int err, i;
107771d10453SEric Joyner 
107871d10453SEric Joyner 	MPASS(nrxqs == 1);
107971d10453SEric Joyner 	MPASS(sc->scctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
108071d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
108171d10453SEric Joyner 
108271d10453SEric Joyner 	/* Do not bother allocating queues if we're in recovery mode */
108371d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
108471d10453SEric Joyner 		return (0);
108571d10453SEric Joyner 
108671d10453SEric Joyner 	/* Allocate queue structure memory */
108771d10453SEric Joyner 	if (!(vsi->rx_queues =
1088*8923de59SPiotr Kubaj 	      (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
108971d10453SEric Joyner 		device_printf(sc->dev, "Unable to allocate Rx queue memory\n");
109071d10453SEric Joyner 		return (ENOMEM);
109171d10453SEric Joyner 	}
109271d10453SEric Joyner 
109371d10453SEric Joyner 	/* Assign queues from PF space to the main VSI */
109471d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap, nrxqsets);
109571d10453SEric Joyner 	if (err) {
109671d10453SEric Joyner 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
109771d10453SEric Joyner 			      ice_err_str(err));
109871d10453SEric Joyner 		goto free_rx_queues;
109971d10453SEric Joyner 	}
110071d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
110171d10453SEric Joyner 
110271d10453SEric Joyner 	/* Add Rx queue sysctls context */
110371d10453SEric Joyner 	ice_vsi_add_rxqs_ctx(vsi);
110471d10453SEric Joyner 
110571d10453SEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
110671d10453SEric Joyner 		rxq->me = i;
110771d10453SEric Joyner 		rxq->vsi = vsi;
110871d10453SEric Joyner 
110971d10453SEric Joyner 		/* store the queue size for easier access */
111071d10453SEric Joyner 		rxq->desc_count = sc->scctx->isc_nrxd[0];
111171d10453SEric Joyner 
111271d10453SEric Joyner 		/* get the virtual and physical address of the hardware queues */
111371d10453SEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
111471d10453SEric Joyner 		rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
111571d10453SEric Joyner 		rxq->rx_paddr = paddrs[i];
111671d10453SEric Joyner 
111771d10453SEric Joyner 		ice_add_rxq_sysctls(rxq);
111871d10453SEric Joyner 	}
111971d10453SEric Joyner 
112071d10453SEric Joyner 	vsi->num_rx_queues = nrxqsets;
112171d10453SEric Joyner 
112271d10453SEric Joyner 	return (0);
112371d10453SEric Joyner 
112471d10453SEric Joyner free_rx_queues:
112571d10453SEric Joyner 	free(vsi->rx_queues, M_ICE);
112671d10453SEric Joyner 	vsi->rx_queues = NULL;
112771d10453SEric Joyner 	return err;
112871d10453SEric Joyner }
112971d10453SEric Joyner 
113071d10453SEric Joyner /**
113171d10453SEric Joyner  * ice_if_queues_free - Free queue memory
113271d10453SEric Joyner  * @ctx: the iflib context structure
113371d10453SEric Joyner  *
113471d10453SEric Joyner  * Free queue memory allocated by ice_if_tx_queues_alloc() and
113571d10453SEric Joyner  * ice_if_rx_queues_alloc().
113671d10453SEric Joyner  *
113771d10453SEric Joyner  * There is no guarantee that ice_if_queues_free() and ice_if_detach() will be
113871d10453SEric Joyner  * called in the same order. It's possible for ice_if_queues_free() to be
113971d10453SEric Joyner  * called prior to ice_if_detach(), and vice versa.
114071d10453SEric Joyner  *
114171d10453SEric Joyner  * For this reason, the main VSI is a static member of the ice_softc, which is
114271d10453SEric Joyner  * not free'd until after iflib finishes calling both of these functions.
114371d10453SEric Joyner  *
114471d10453SEric Joyner  * Thus, care must be taken in how we manage the memory being freed by this
114571d10453SEric Joyner  * function, and in what tasks it can and must perform.
114671d10453SEric Joyner  */
114771d10453SEric Joyner static void
114871d10453SEric Joyner ice_if_queues_free(if_ctx_t ctx)
114971d10453SEric Joyner {
115071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
115171d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
115271d10453SEric Joyner 	struct ice_tx_queue *txq;
115371d10453SEric Joyner 	int i;
115471d10453SEric Joyner 
115571d10453SEric Joyner 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
115671d10453SEric Joyner 	 * pointers. Note, the calls here and those in ice_if_detach()
115771d10453SEric Joyner 	 * are *BOTH* necessary, as we cannot guarantee which path will be
115871d10453SEric Joyner 	 * run first
115971d10453SEric Joyner 	 */
116071d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
116171d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
116271d10453SEric Joyner 
116371d10453SEric Joyner 	/* Release MSI-X IRQ vectors, if not yet released in ice_if_detach */
116471d10453SEric Joyner 	ice_free_irqvs(sc);
116571d10453SEric Joyner 
116671d10453SEric Joyner 	if (vsi->tx_queues != NULL) {
116771d10453SEric Joyner 		/* free the tx_rsq arrays */
116871d10453SEric Joyner 		for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
116971d10453SEric Joyner 			if (txq->tx_rsq != NULL) {
117071d10453SEric Joyner 				free(txq->tx_rsq, M_ICE);
117171d10453SEric Joyner 				txq->tx_rsq = NULL;
117271d10453SEric Joyner 			}
117371d10453SEric Joyner 		}
117471d10453SEric Joyner 		free(vsi->tx_queues, M_ICE);
117571d10453SEric Joyner 		vsi->tx_queues = NULL;
117671d10453SEric Joyner 		vsi->num_tx_queues = 0;
117771d10453SEric Joyner 	}
117871d10453SEric Joyner 	if (vsi->rx_queues != NULL) {
117971d10453SEric Joyner 		free(vsi->rx_queues, M_ICE);
118071d10453SEric Joyner 		vsi->rx_queues = NULL;
118171d10453SEric Joyner 		vsi->num_rx_queues = 0;
118271d10453SEric Joyner 	}
118371d10453SEric Joyner }
118471d10453SEric Joyner 
118571d10453SEric Joyner /**
118671d10453SEric Joyner  * ice_msix_que - Fast interrupt handler for MSI-X receive queues
118771d10453SEric Joyner  * @arg: The Rx queue memory
118871d10453SEric Joyner  *
118971d10453SEric Joyner  * Interrupt filter function for iflib MSI-X interrupts. Called by iflib when
119071d10453SEric Joyner  * an MSI-X interrupt for a given queue is triggered. Currently this just asks
119171d10453SEric Joyner  * iflib to schedule the main Rx thread.
119271d10453SEric Joyner  */
119371d10453SEric Joyner static int
119471d10453SEric Joyner ice_msix_que(void *arg)
119571d10453SEric Joyner {
119671d10453SEric Joyner 	struct ice_rx_queue __unused *rxq = (struct ice_rx_queue *)arg;
119771d10453SEric Joyner 
119871d10453SEric Joyner 	/* TODO: dynamic ITR algorithm?? */
119971d10453SEric Joyner 
120071d10453SEric Joyner 	return (FILTER_SCHEDULE_THREAD);
120171d10453SEric Joyner }
120271d10453SEric Joyner 
120371d10453SEric Joyner /**
120471d10453SEric Joyner  * ice_msix_admin - Fast interrupt handler for MSI-X admin interrupt
120571d10453SEric Joyner  * @arg: pointer to device softc memory
120671d10453SEric Joyner  *
120771d10453SEric Joyner  * Called by iflib when an administrative interrupt occurs. Should perform any
120871d10453SEric Joyner  * fast logic for handling the interrupt cause, and then indicate whether the
120971d10453SEric Joyner  * admin task needs to be queued.
121071d10453SEric Joyner  */
121171d10453SEric Joyner static int
121271d10453SEric Joyner ice_msix_admin(void *arg)
121371d10453SEric Joyner {
121471d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)arg;
121571d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
121671d10453SEric Joyner 	device_t dev = sc->dev;
121771d10453SEric Joyner 	u32 oicr;
121871d10453SEric Joyner 
121971d10453SEric Joyner 	/* There is no safe way to modify the enabled miscellaneous causes of
122071d10453SEric Joyner 	 * the OICR vector at runtime, as doing so would be prone to race
122171d10453SEric Joyner 	 * conditions. Reading PFINT_OICR will unmask the associated interrupt
122271d10453SEric Joyner 	 * causes and allow future interrupts to occur. The admin interrupt
122371d10453SEric Joyner 	 * vector will not be re-enabled until after we exit this function,
122471d10453SEric Joyner 	 * but any delayed tasks must be resilient against possible "late
122571d10453SEric Joyner 	 * arrival" interrupts that occur while we're already handling the
122671d10453SEric Joyner 	 * task. This is done by using state bits and serializing these
122771d10453SEric Joyner 	 * delayed tasks via the admin status task function.
122871d10453SEric Joyner 	 */
122971d10453SEric Joyner 	oicr = rd32(hw, PFINT_OICR);
123071d10453SEric Joyner 
123171d10453SEric Joyner 	/* Processing multiple controlq interrupts on a single vector does not
123271d10453SEric Joyner 	 * provide an indication of which controlq triggered the interrupt.
123371d10453SEric Joyner 	 * We might try reading the INTEVENT bit of the respective PFINT_*_CTL
123471d10453SEric Joyner 	 * registers. However, the INTEVENT bit is not guaranteed to be set as
123571d10453SEric Joyner 	 * it gets automatically cleared when the hardware acknowledges the
123671d10453SEric Joyner 	 * interrupt.
123771d10453SEric Joyner 	 *
123871d10453SEric Joyner 	 * This means we don't really have a good indication of whether or
123971d10453SEric Joyner 	 * which controlq triggered this interrupt. We'll just notify the
124071d10453SEric Joyner 	 * admin task that it should check all the controlqs.
124171d10453SEric Joyner 	 */
124271d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
124371d10453SEric Joyner 
124471d10453SEric Joyner 	if (oicr & PFINT_OICR_VFLR_M) {
124571d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_VFLR_PENDING);
124671d10453SEric Joyner 	}
124771d10453SEric Joyner 
124871d10453SEric Joyner 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
124971d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_MDD_PENDING);
125071d10453SEric Joyner 	}
125171d10453SEric Joyner 
125271d10453SEric Joyner 	if (oicr & PFINT_OICR_GRST_M) {
125371d10453SEric Joyner 		u32 reset;
125471d10453SEric Joyner 
125571d10453SEric Joyner 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
125671d10453SEric Joyner 			GLGEN_RSTAT_RESET_TYPE_S;
125771d10453SEric Joyner 
125871d10453SEric Joyner 		if (reset == ICE_RESET_CORER)
125971d10453SEric Joyner 			sc->soft_stats.corer_count++;
126071d10453SEric Joyner 		else if (reset == ICE_RESET_GLOBR)
126171d10453SEric Joyner 			sc->soft_stats.globr_count++;
126271d10453SEric Joyner 		else
126371d10453SEric Joyner 			sc->soft_stats.empr_count++;
126471d10453SEric Joyner 
126571d10453SEric Joyner 		/* There are a couple of bits at play for handling resets.
126671d10453SEric Joyner 		 * First, the ICE_STATE_RESET_OICR_RECV bit is used to
126771d10453SEric Joyner 		 * indicate that the driver has received an OICR with a reset
126871d10453SEric Joyner 		 * bit active, indicating that a CORER/GLOBR/EMPR is about to
126971d10453SEric Joyner 		 * happen. Second, we set hw->reset_ongoing to indicate that
127071d10453SEric Joyner 		 * the hardware is in reset. We will set this back to false as
127171d10453SEric Joyner 		 * soon as the driver has determined that the hardware is out
127271d10453SEric Joyner 		 * of reset.
127371d10453SEric Joyner 		 *
1274b6f9c6aaSGordon Bergling 		 * If the driver wishes to trigger a request, it can set one of
127571d10453SEric Joyner 		 * the ICE_STATE_RESET_*_REQ bits, which will trigger the
127671d10453SEric Joyner 		 * correct type of reset.
127771d10453SEric Joyner 		 */
127871d10453SEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
127971d10453SEric Joyner 			hw->reset_ongoing = true;
128071d10453SEric Joyner 	}
128171d10453SEric Joyner 
128271d10453SEric Joyner 	if (oicr & PFINT_OICR_ECC_ERR_M) {
128371d10453SEric Joyner 		device_printf(dev, "ECC Error detected!\n");
128471d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
128571d10453SEric Joyner 	}
128671d10453SEric Joyner 
128771d10453SEric Joyner 	if (oicr & PFINT_OICR_PE_CRITERR_M) {
128871d10453SEric Joyner 		device_printf(dev, "Critical Protocol Engine Error detected!\n");
128971d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
129071d10453SEric Joyner 	}
129171d10453SEric Joyner 
129271d10453SEric Joyner 	if (oicr & PFINT_OICR_PCI_EXCEPTION_M) {
129371d10453SEric Joyner 		device_printf(dev, "PCI Exception detected!\n");
129471d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
129571d10453SEric Joyner 	}
129671d10453SEric Joyner 
129771d10453SEric Joyner 	if (oicr & PFINT_OICR_HMC_ERR_M) {
129871d10453SEric Joyner 		/* Log the HMC errors, but don't disable the interrupt cause */
129971d10453SEric Joyner 		ice_log_hmc_error(hw, dev);
130071d10453SEric Joyner 	}
130171d10453SEric Joyner 
130271d10453SEric Joyner 	return (FILTER_SCHEDULE_THREAD);
130371d10453SEric Joyner }
130471d10453SEric Joyner 
130571d10453SEric Joyner /**
130671d10453SEric Joyner  * ice_allocate_msix - Allocate MSI-X vectors for the interface
130771d10453SEric Joyner  * @sc: the device private softc
130871d10453SEric Joyner  *
130971d10453SEric Joyner  * Map the MSI-X bar, and then request MSI-X vectors in a two-stage process.
131071d10453SEric Joyner  *
131171d10453SEric Joyner  * First, determine a suitable total number of vectors based on the number
131271d10453SEric Joyner  * of CPUs, RSS buckets, the administrative vector, and other demands such as
131371d10453SEric Joyner  * RDMA.
131471d10453SEric Joyner  *
131571d10453SEric Joyner  * Request the desired amount of vectors, and see how many we obtain. If we
131671d10453SEric Joyner  * don't obtain as many as desired, reduce the demands by lowering the number
131771d10453SEric Joyner  * of requested queues or reducing the demand from other features such as
131871d10453SEric Joyner  * RDMA.
131971d10453SEric Joyner  *
132071d10453SEric Joyner  * @remark This function is required because the driver sets the
132171d10453SEric Joyner  * IFLIB_SKIP_MSIX flag indicating that the driver will manage MSI-X vectors
132271d10453SEric Joyner  * manually.
132371d10453SEric Joyner  *
132471d10453SEric Joyner  * @remark This driver will only use MSI-X vectors. If this is not possible,
132571d10453SEric Joyner  * neither MSI or legacy interrupts will be tried.
132671d10453SEric Joyner  *
132771d10453SEric Joyner  * @post on success this function must set the following scctx parameters:
132871d10453SEric Joyner  * isc_vectors, isc_nrxqsets, isc_ntxqsets, and isc_intr.
132971d10453SEric Joyner  *
133071d10453SEric Joyner  * @returns zero on success or an error code on failure.
133171d10453SEric Joyner  */
133271d10453SEric Joyner static int
133371d10453SEric Joyner ice_allocate_msix(struct ice_softc *sc)
133471d10453SEric Joyner {
133571d10453SEric Joyner 	bool iflib_override_queue_count = false;
133671d10453SEric Joyner 	if_softc_ctx_t scctx = sc->scctx;
133771d10453SEric Joyner 	device_t dev = sc->dev;
133871d10453SEric Joyner 	cpuset_t cpus;
133971d10453SEric Joyner 	int bar, queues, vectors, requested;
134071d10453SEric Joyner 	int err = 0;
13418a13362dSEric Joyner 	int rdma;
134271d10453SEric Joyner 
134371d10453SEric Joyner 	/* Allocate the MSI-X bar */
134471d10453SEric Joyner 	bar = scctx->isc_msix_bar;
134571d10453SEric Joyner 	sc->msix_table = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar, RF_ACTIVE);
134671d10453SEric Joyner 	if (!sc->msix_table) {
134771d10453SEric Joyner 		device_printf(dev, "Unable to map MSI-X table\n");
134871d10453SEric Joyner 		return (ENOMEM);
134971d10453SEric Joyner 	}
135071d10453SEric Joyner 
135171d10453SEric Joyner 	/* Check if the iflib queue count sysctls have been set */
135271d10453SEric Joyner 	if (sc->ifc_sysctl_ntxqs || sc->ifc_sysctl_nrxqs)
135371d10453SEric Joyner 		iflib_override_queue_count = true;
135471d10453SEric Joyner 
135571d10453SEric Joyner 	err = bus_get_cpus(dev, INTR_CPUS, sizeof(cpus), &cpus);
135671d10453SEric Joyner 	if (err) {
135771d10453SEric Joyner 		device_printf(dev, "%s: Unable to fetch the CPU list: %s\n",
135871d10453SEric Joyner 			      __func__, ice_err_str(err));
135971d10453SEric Joyner 		CPU_COPY(&all_cpus, &cpus);
136071d10453SEric Joyner 	}
136171d10453SEric Joyner 
136271d10453SEric Joyner 	/* Attempt to mimic behavior of iflib_msix_init */
136371d10453SEric Joyner 	if (iflib_override_queue_count) {
136471d10453SEric Joyner 		/*
136571d10453SEric Joyner 		 * If the override sysctls have been set, limit the queues to
136671d10453SEric Joyner 		 * the number of logical CPUs.
136771d10453SEric Joyner 		 */
136871d10453SEric Joyner 		queues = mp_ncpus;
136971d10453SEric Joyner 	} else {
137071d10453SEric Joyner 		/*
137171d10453SEric Joyner 		 * Otherwise, limit the queue count to the CPUs associated
137271d10453SEric Joyner 		 * with the NUMA node the device is associated with.
137371d10453SEric Joyner 		 */
137471d10453SEric Joyner 		queues = CPU_COUNT(&cpus);
137571d10453SEric Joyner 	}
137671d10453SEric Joyner 
137771d10453SEric Joyner 	/* Clamp to the number of RSS buckets */
137871d10453SEric Joyner 	queues = imin(queues, rss_getnumbuckets());
137971d10453SEric Joyner 
138071d10453SEric Joyner 	/*
138171d10453SEric Joyner 	 * Clamp the number of queue pairs to the minimum of the requested Tx
138271d10453SEric Joyner 	 * and Rx queues.
138371d10453SEric Joyner 	 */
138471d10453SEric Joyner 	queues = imin(queues, sc->ifc_sysctl_ntxqs ?: scctx->isc_ntxqsets);
138571d10453SEric Joyner 	queues = imin(queues, sc->ifc_sysctl_nrxqs ?: scctx->isc_nrxqsets);
138671d10453SEric Joyner 
13878a13362dSEric Joyner 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RDMA)) {
13888a13362dSEric Joyner 		/*
13898a13362dSEric Joyner 		 * Choose a number of RDMA vectors based on the number of CPUs
13908a13362dSEric Joyner 		 * up to a maximum
13918a13362dSEric Joyner 		 */
13928a13362dSEric Joyner 		rdma = min(CPU_COUNT(&cpus), ICE_RDMA_MAX_MSIX);
13938a13362dSEric Joyner 
13948a13362dSEric Joyner 		/* Further limit by the user configurable tunable */
13958a13362dSEric Joyner 		rdma = min(rdma, ice_rdma_max_msix);
13968a13362dSEric Joyner 	} else {
13978a13362dSEric Joyner 		rdma = 0;
13988a13362dSEric Joyner 	}
13998a13362dSEric Joyner 
140071d10453SEric Joyner 	/*
140171d10453SEric Joyner 	 * Determine the number of vectors to request. Note that we also need
140271d10453SEric Joyner 	 * to allocate one vector for administrative tasks.
140371d10453SEric Joyner 	 */
14048a13362dSEric Joyner 	requested = rdma + queues + 1;
140571d10453SEric Joyner 
140671d10453SEric Joyner 	vectors = requested;
140771d10453SEric Joyner 
140871d10453SEric Joyner 	err = pci_alloc_msix(dev, &vectors);
140971d10453SEric Joyner 	if (err) {
141071d10453SEric Joyner 		device_printf(dev, "Failed to allocate %d MSI-X vectors, err %s\n",
141171d10453SEric Joyner 			      vectors, ice_err_str(err));
141271d10453SEric Joyner 		goto err_free_msix_table;
141371d10453SEric Joyner 	}
141471d10453SEric Joyner 
141571d10453SEric Joyner 	/* If we don't receive enough vectors, reduce demands */
141671d10453SEric Joyner 	if (vectors < requested) {
141771d10453SEric Joyner 		int diff = requested - vectors;
141871d10453SEric Joyner 
141971d10453SEric Joyner 		device_printf(dev, "Requested %d MSI-X vectors, but got only %d\n",
142071d10453SEric Joyner 			      requested, vectors);
142171d10453SEric Joyner 
142271d10453SEric Joyner 		/*
14238a13362dSEric Joyner 		 * The OS didn't grant us the requested number of vectors.
14248a13362dSEric Joyner 		 * Check to see if we can reduce demands by limiting the
14258a13362dSEric Joyner 		 * number of vectors allocated to certain features.
14268a13362dSEric Joyner 		 */
14278a13362dSEric Joyner 
14288a13362dSEric Joyner 		if (rdma >= diff) {
14298a13362dSEric Joyner 			/* Reduce the number of RDMA vectors we reserve */
14308a13362dSEric Joyner 			rdma -= diff;
14318a13362dSEric Joyner 			diff = 0;
14328a13362dSEric Joyner 		} else {
14338a13362dSEric Joyner 			/* Disable RDMA and reduce the difference */
14348a13362dSEric Joyner 			ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
14358a13362dSEric Joyner 			diff -= rdma;
14368a13362dSEric Joyner 			rdma = 0;
14378a13362dSEric Joyner 		}
14388a13362dSEric Joyner 
14398a13362dSEric Joyner 		/*
144071d10453SEric Joyner 		 * If we still have a difference, we need to reduce the number
144171d10453SEric Joyner 		 * of queue pairs.
144271d10453SEric Joyner 		 *
144371d10453SEric Joyner 		 * However, we still need at least one vector for the admin
144471d10453SEric Joyner 		 * interrupt and one queue pair.
144571d10453SEric Joyner 		 */
144671d10453SEric Joyner 		if (queues <= diff) {
144771d10453SEric Joyner 			device_printf(dev, "Unable to allocate sufficient MSI-X vectors\n");
144871d10453SEric Joyner 			err = (ERANGE);
144971d10453SEric Joyner 			goto err_pci_release_msi;
145071d10453SEric Joyner 		}
145171d10453SEric Joyner 
145271d10453SEric Joyner 		queues -= diff;
145371d10453SEric Joyner 	}
145471d10453SEric Joyner 
145571d10453SEric Joyner 	device_printf(dev, "Using %d Tx and Rx queues\n", queues);
14568a13362dSEric Joyner 	if (rdma)
14578a13362dSEric Joyner 		device_printf(dev, "Reserving %d MSI-X interrupts for iRDMA\n",
14588a13362dSEric Joyner 			      rdma);
145971d10453SEric Joyner 	device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
146071d10453SEric Joyner 		      vectors);
146171d10453SEric Joyner 
146271d10453SEric Joyner 	scctx->isc_vectors = vectors;
146371d10453SEric Joyner 	scctx->isc_nrxqsets = queues;
146471d10453SEric Joyner 	scctx->isc_ntxqsets = queues;
146571d10453SEric Joyner 	scctx->isc_intr = IFLIB_INTR_MSIX;
146671d10453SEric Joyner 
14678a13362dSEric Joyner 	sc->irdma_vectors = rdma;
14688a13362dSEric Joyner 
146971d10453SEric Joyner 	/* Interrupt allocation tracking isn't required in recovery mode,
147071d10453SEric Joyner 	 * since neither RDMA nor VFs are enabled.
147171d10453SEric Joyner 	 */
147271d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
147371d10453SEric Joyner 		return (0);
147471d10453SEric Joyner 
147571d10453SEric Joyner 	/* Keep track of which interrupt indices are being used for what */
14768a13362dSEric Joyner 	sc->lan_vectors = vectors - rdma;
147771d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->imgr, sc->pf_imap, sc->lan_vectors);
147871d10453SEric Joyner 	if (err) {
147971d10453SEric Joyner 		device_printf(dev, "Unable to assign PF interrupt mapping: %s\n",
148071d10453SEric Joyner 			      ice_err_str(err));
148171d10453SEric Joyner 		goto err_pci_release_msi;
148271d10453SEric Joyner 	}
14838a13362dSEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->imgr, sc->rdma_imap, rdma);
14848a13362dSEric Joyner 	if (err) {
14858a13362dSEric Joyner 		device_printf(dev, "Unable to assign PF RDMA interrupt mapping: %s\n",
14868a13362dSEric Joyner 			      ice_err_str(err));
14878a13362dSEric Joyner 		ice_resmgr_release_map(&sc->imgr, sc->pf_imap,
14888a13362dSEric Joyner 					    sc->lan_vectors);
14898a13362dSEric Joyner 		goto err_pci_release_msi;
14908a13362dSEric Joyner 	}
149171d10453SEric Joyner 
149271d10453SEric Joyner 	return (0);
149371d10453SEric Joyner 
149471d10453SEric Joyner err_pci_release_msi:
149571d10453SEric Joyner 	pci_release_msi(dev);
149671d10453SEric Joyner err_free_msix_table:
149771d10453SEric Joyner 	if (sc->msix_table != NULL) {
149871d10453SEric Joyner 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
149971d10453SEric Joyner 				rman_get_rid(sc->msix_table),
150071d10453SEric Joyner 				sc->msix_table);
150171d10453SEric Joyner 		sc->msix_table = NULL;
150271d10453SEric Joyner 	}
150371d10453SEric Joyner 
150471d10453SEric Joyner 	return (err);
150571d10453SEric Joyner }
150671d10453SEric Joyner 
150771d10453SEric Joyner /**
150871d10453SEric Joyner  * ice_if_msix_intr_assign - Assign MSI-X interrupt vectors to queues
150971d10453SEric Joyner  * @ctx: the iflib context structure
151071d10453SEric Joyner  * @msix: the number of vectors we were assigned
151171d10453SEric Joyner  *
151271d10453SEric Joyner  * Called by iflib to assign MSI-X vectors to queues. Currently requires that
151371d10453SEric Joyner  * we get at least the same number of vectors as we have queues, and that we
151471d10453SEric Joyner  * always have the same number of Tx and Rx queues.
151571d10453SEric Joyner  *
151671d10453SEric Joyner  * Tx queues use a softirq instead of using their own hardware interrupt.
151771d10453SEric Joyner  */
151871d10453SEric Joyner static int
151971d10453SEric Joyner ice_if_msix_intr_assign(if_ctx_t ctx, int msix)
152071d10453SEric Joyner {
152171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
152271d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
152371d10453SEric Joyner 	int err, i, vector;
152471d10453SEric Joyner 
152571d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
152671d10453SEric Joyner 
152771d10453SEric Joyner 	if (vsi->num_rx_queues != vsi->num_tx_queues) {
152871d10453SEric Joyner 		device_printf(sc->dev,
152971d10453SEric Joyner 			      "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",
153071d10453SEric Joyner 			      vsi->num_tx_queues, vsi->num_rx_queues);
153171d10453SEric Joyner 		return (EOPNOTSUPP);
153271d10453SEric Joyner 	}
153371d10453SEric Joyner 
153471d10453SEric Joyner 	if (msix < (vsi->num_rx_queues + 1)) {
153571d10453SEric Joyner 		device_printf(sc->dev,
153671d10453SEric Joyner 			      "Not enough MSI-X vectors to assign one vector to each queue pair\n");
153771d10453SEric Joyner 		return (EOPNOTSUPP);
153871d10453SEric Joyner 	}
153971d10453SEric Joyner 
154071d10453SEric Joyner 	/* Save the number of vectors for future use */
154171d10453SEric Joyner 	sc->num_irq_vectors = vsi->num_rx_queues + 1;
154271d10453SEric Joyner 
154371d10453SEric Joyner 	/* Allocate space to store the IRQ vector data */
154471d10453SEric Joyner 	if (!(sc->irqvs =
154571d10453SEric Joyner 	      (struct ice_irq_vector *) malloc(sizeof(struct ice_irq_vector) * (sc->num_irq_vectors),
154671d10453SEric Joyner 					       M_ICE, M_NOWAIT))) {
154771d10453SEric Joyner 		device_printf(sc->dev,
154871d10453SEric Joyner 			      "Unable to allocate irqv memory\n");
154971d10453SEric Joyner 		return (ENOMEM);
155071d10453SEric Joyner 	}
155171d10453SEric Joyner 
155271d10453SEric Joyner 	/* Administrative interrupt events will use vector 0 */
155371d10453SEric Joyner 	err = iflib_irq_alloc_generic(ctx, &sc->irqvs[0].irq, 1, IFLIB_INTR_ADMIN,
155471d10453SEric Joyner 				      ice_msix_admin, sc, 0, "admin");
155571d10453SEric Joyner 	if (err) {
155671d10453SEric Joyner 		device_printf(sc->dev,
155771d10453SEric Joyner 			      "Failed to register Admin queue handler: %s\n",
155871d10453SEric Joyner 			      ice_err_str(err));
155971d10453SEric Joyner 		goto free_irqvs;
156071d10453SEric Joyner 	}
156171d10453SEric Joyner 	sc->irqvs[0].me = 0;
156271d10453SEric Joyner 
156371d10453SEric Joyner 	/* Do not allocate queue interrupts when in recovery mode */
156471d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
156571d10453SEric Joyner 		return (0);
156671d10453SEric Joyner 
156771d10453SEric Joyner 	for (i = 0, vector = 1; i < vsi->num_rx_queues; i++, vector++) {
156871d10453SEric Joyner 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
156971d10453SEric Joyner 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
157071d10453SEric Joyner 		int rid = vector + 1;
157171d10453SEric Joyner 		char irq_name[16];
157271d10453SEric Joyner 
157371d10453SEric Joyner 		snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
157471d10453SEric Joyner 		err = iflib_irq_alloc_generic(ctx, &sc->irqvs[vector].irq, rid,
157581be6552SMatt Macy 					      IFLIB_INTR_RXTX, ice_msix_que,
157671d10453SEric Joyner 					      rxq, rxq->me, irq_name);
157771d10453SEric Joyner 		if (err) {
157871d10453SEric Joyner 			device_printf(sc->dev,
157971d10453SEric Joyner 				      "Failed to allocate q int %d err: %s\n",
158071d10453SEric Joyner 				      i, ice_err_str(err));
158171d10453SEric Joyner 			vector--;
158271d10453SEric Joyner 			i--;
158371d10453SEric Joyner 			goto fail;
158471d10453SEric Joyner 		}
158571d10453SEric Joyner 		sc->irqvs[vector].me = vector;
158671d10453SEric Joyner 		rxq->irqv = &sc->irqvs[vector];
158771d10453SEric Joyner 
158871d10453SEric Joyner 		bzero(irq_name, sizeof(irq_name));
158971d10453SEric Joyner 
159071d10453SEric Joyner 		snprintf(irq_name, sizeof(irq_name), "txq%d", i);
159171d10453SEric Joyner 		iflib_softirq_alloc_generic(ctx, &sc->irqvs[vector].irq,
159271d10453SEric Joyner 					    IFLIB_INTR_TX, txq,
159371d10453SEric Joyner 					    txq->me, irq_name);
159471d10453SEric Joyner 		txq->irqv = &sc->irqvs[vector];
159571d10453SEric Joyner 	}
159671d10453SEric Joyner 
159771d10453SEric Joyner 	return (0);
159871d10453SEric Joyner fail:
159971d10453SEric Joyner 	for (; i >= 0; i--, vector--)
160071d10453SEric Joyner 		iflib_irq_free(ctx, &sc->irqvs[vector].irq);
160171d10453SEric Joyner 	iflib_irq_free(ctx, &sc->irqvs[0].irq);
160271d10453SEric Joyner free_irqvs:
160371d10453SEric Joyner 	free(sc->irqvs, M_ICE);
160471d10453SEric Joyner 	sc->irqvs = NULL;
160571d10453SEric Joyner 	return err;
160671d10453SEric Joyner }
160771d10453SEric Joyner 
160871d10453SEric Joyner /**
160971d10453SEric Joyner  * ice_if_mtu_set - Set the device MTU
161071d10453SEric Joyner  * @ctx: iflib context structure
161171d10453SEric Joyner  * @mtu: the MTU requested
161271d10453SEric Joyner  *
161371d10453SEric Joyner  * Called by iflib to configure the device's Maximum Transmission Unit (MTU).
161471d10453SEric Joyner  *
161571d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
161671d10453SEric Joyner  */
161771d10453SEric Joyner static int
161871d10453SEric Joyner ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
161971d10453SEric Joyner {
162071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
162171d10453SEric Joyner 
162271d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
162371d10453SEric Joyner 
162471d10453SEric Joyner 	/* Do not support configuration when in recovery mode */
162571d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
162671d10453SEric Joyner 		return (ENOSYS);
162771d10453SEric Joyner 
162871d10453SEric Joyner 	if (mtu < ICE_MIN_MTU || mtu > ICE_MAX_MTU)
162971d10453SEric Joyner 		return (EINVAL);
163071d10453SEric Joyner 
163171d10453SEric Joyner 	sc->scctx->isc_max_frame_size = mtu +
163271d10453SEric Joyner 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
163371d10453SEric Joyner 
163471d10453SEric Joyner 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
163571d10453SEric Joyner 
163671d10453SEric Joyner 	return (0);
163771d10453SEric Joyner }
163871d10453SEric Joyner 
163971d10453SEric Joyner /**
164071d10453SEric Joyner  * ice_if_intr_enable - Enable device interrupts
164171d10453SEric Joyner  * @ctx: iflib context structure
164271d10453SEric Joyner  *
164371d10453SEric Joyner  * Called by iflib to request enabling device interrupts.
164471d10453SEric Joyner  */
164571d10453SEric Joyner static void
164671d10453SEric Joyner ice_if_intr_enable(if_ctx_t ctx)
164771d10453SEric Joyner {
164871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
164971d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
165071d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
165171d10453SEric Joyner 
165271d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
165371d10453SEric Joyner 
165471d10453SEric Joyner 	/* Enable ITR 0 */
165571d10453SEric Joyner 	ice_enable_intr(hw, sc->irqvs[0].me);
165671d10453SEric Joyner 
165771d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
165871d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
165971d10453SEric Joyner 		return;
166071d10453SEric Joyner 
166171d10453SEric Joyner 	/* Enable all queue interrupts */
166271d10453SEric Joyner 	for (int i = 0; i < vsi->num_rx_queues; i++)
166371d10453SEric Joyner 		ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
166471d10453SEric Joyner }
166571d10453SEric Joyner 
166671d10453SEric Joyner /**
166771d10453SEric Joyner  * ice_if_intr_disable - Disable device interrupts
166871d10453SEric Joyner  * @ctx: iflib context structure
166971d10453SEric Joyner  *
167071d10453SEric Joyner  * Called by iflib to request disabling device interrupts.
167171d10453SEric Joyner  */
167271d10453SEric Joyner static void
167371d10453SEric Joyner ice_if_intr_disable(if_ctx_t ctx)
167471d10453SEric Joyner {
167571d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
167671d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
167771d10453SEric Joyner 	unsigned int i;
167871d10453SEric Joyner 
167971d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
168071d10453SEric Joyner 
168171d10453SEric Joyner 	/* IFDI_INTR_DISABLE may be called prior to interrupts actually being
168271d10453SEric Joyner 	 * assigned to queues. Instead of assuming that the interrupt
168371d10453SEric Joyner 	 * assignment in the rx_queues structure is valid, just disable all
168471d10453SEric Joyner 	 * possible interrupts
168571d10453SEric Joyner 	 *
168671d10453SEric Joyner 	 * Note that we choose not to disable ITR 0 because this handles the
168771d10453SEric Joyner 	 * AdminQ interrupts, and we want to keep processing these even when
168871d10453SEric Joyner 	 * the interface is offline.
168971d10453SEric Joyner 	 */
169071d10453SEric Joyner 	for (i = 1; i < hw->func_caps.common_cap.num_msix_vectors; i++)
169171d10453SEric Joyner 		ice_disable_intr(hw, i);
169271d10453SEric Joyner }
169371d10453SEric Joyner 
169471d10453SEric Joyner /**
169571d10453SEric Joyner  * ice_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
169671d10453SEric Joyner  * @ctx: iflib context structure
169771d10453SEric Joyner  * @rxqid: the Rx queue to enable
169871d10453SEric Joyner  *
169971d10453SEric Joyner  * Enable a specific Rx queue interrupt.
170071d10453SEric Joyner  *
170171d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
170271d10453SEric Joyner  */
170371d10453SEric Joyner static int
170471d10453SEric Joyner ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
170571d10453SEric Joyner {
170671d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
170771d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
170871d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
170971d10453SEric Joyner 
171071d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
171171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
171271d10453SEric Joyner 		return (ENOSYS);
171371d10453SEric Joyner 
171471d10453SEric Joyner 	ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
171571d10453SEric Joyner 	return (0);
171671d10453SEric Joyner }
171771d10453SEric Joyner 
171871d10453SEric Joyner /**
171971d10453SEric Joyner  * ice_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
172071d10453SEric Joyner  * @ctx: iflib context structure
172171d10453SEric Joyner  * @txqid: the Tx queue to enable
172271d10453SEric Joyner  *
172371d10453SEric Joyner  * Enable a specific Tx queue interrupt.
172471d10453SEric Joyner  *
172571d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
172671d10453SEric Joyner  */
172771d10453SEric Joyner static int
172871d10453SEric Joyner ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
172971d10453SEric Joyner {
173071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
173171d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
173271d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
173371d10453SEric Joyner 
173471d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
173571d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
173671d10453SEric Joyner 		return (ENOSYS);
173771d10453SEric Joyner 
173871d10453SEric Joyner 	ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
173971d10453SEric Joyner 	return (0);
174071d10453SEric Joyner }
174171d10453SEric Joyner 
174271d10453SEric Joyner /**
174371d10453SEric Joyner  * ice_if_promisc_set - Set device promiscuous mode
174471d10453SEric Joyner  * @ctx: iflib context structure
174571d10453SEric Joyner  * @flags: promiscuous flags to configure
174671d10453SEric Joyner  *
174771d10453SEric Joyner  * Called by iflib to configure device promiscuous mode.
174871d10453SEric Joyner  *
174971d10453SEric Joyner  * @remark Calls to this function will always overwrite the previous setting
175071d10453SEric Joyner  */
175171d10453SEric Joyner static int
175271d10453SEric Joyner ice_if_promisc_set(if_ctx_t ctx, int flags)
175371d10453SEric Joyner {
175471d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
175571d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
175671d10453SEric Joyner 	device_t dev = sc->dev;
175771d10453SEric Joyner 	enum ice_status status;
175871d10453SEric Joyner 	bool promisc_enable = flags & IFF_PROMISC;
175971d10453SEric Joyner 	bool multi_enable = flags & IFF_ALLMULTI;
176071d10453SEric Joyner 
176171d10453SEric Joyner 	/* Do not support configuration when in recovery mode */
176271d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
176371d10453SEric Joyner 		return (ENOSYS);
176471d10453SEric Joyner 
176571d10453SEric Joyner 	if (multi_enable)
176671d10453SEric Joyner 		return (EOPNOTSUPP);
176771d10453SEric Joyner 
176871d10453SEric Joyner 	if (promisc_enable) {
176971d10453SEric Joyner 		status = ice_set_vsi_promisc(hw, sc->pf_vsi.idx,
177071d10453SEric Joyner 					     ICE_VSI_PROMISC_MASK, 0);
177171d10453SEric Joyner 		if (status && status != ICE_ERR_ALREADY_EXISTS) {
177271d10453SEric Joyner 			device_printf(dev,
177371d10453SEric Joyner 				      "Failed to enable promiscuous mode for PF VSI, err %s aq_err %s\n",
177471d10453SEric Joyner 				      ice_status_str(status),
177571d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
177671d10453SEric Joyner 			return (EIO);
177771d10453SEric Joyner 		}
177871d10453SEric Joyner 	} else {
177971d10453SEric Joyner 		status = ice_clear_vsi_promisc(hw, sc->pf_vsi.idx,
178071d10453SEric Joyner 					       ICE_VSI_PROMISC_MASK, 0);
178171d10453SEric Joyner 		if (status) {
178271d10453SEric Joyner 			device_printf(dev,
178371d10453SEric Joyner 				      "Failed to disable promiscuous mode for PF VSI, err %s aq_err %s\n",
178471d10453SEric Joyner 				      ice_status_str(status),
178571d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
178671d10453SEric Joyner 			return (EIO);
178771d10453SEric Joyner 		}
178871d10453SEric Joyner 	}
178971d10453SEric Joyner 
179071d10453SEric Joyner 	return (0);
179171d10453SEric Joyner }
179271d10453SEric Joyner 
179371d10453SEric Joyner /**
179471d10453SEric Joyner  * ice_if_media_change - Change device media
179571d10453SEric Joyner  * @ctx: device ctx structure
179671d10453SEric Joyner  *
179771d10453SEric Joyner  * Called by iflib when a media change is requested. This operation is not
179871d10453SEric Joyner  * supported by the hardware, so we just return an error code.
179971d10453SEric Joyner  */
180071d10453SEric Joyner static int
180171d10453SEric Joyner ice_if_media_change(if_ctx_t ctx)
180271d10453SEric Joyner {
180371d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
180471d10453SEric Joyner 
180571d10453SEric Joyner 	device_printf(sc->dev, "Media change is not supported.\n");
180671d10453SEric Joyner 	return (ENODEV);
180771d10453SEric Joyner }
180871d10453SEric Joyner 
180971d10453SEric Joyner /**
181071d10453SEric Joyner  * ice_if_media_status - Report current device media
181171d10453SEric Joyner  * @ctx: iflib context structure
181271d10453SEric Joyner  * @ifmr: ifmedia request structure to update
181371d10453SEric Joyner  *
181471d10453SEric Joyner  * Updates the provided ifmr with current device media status, including link
181571d10453SEric Joyner  * status and media type.
181671d10453SEric Joyner  */
181771d10453SEric Joyner static void
181871d10453SEric Joyner ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
181971d10453SEric Joyner {
182071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
182171d10453SEric Joyner 	struct ice_link_status *li = &sc->hw.port_info->phy.link_info;
182271d10453SEric Joyner 
182371d10453SEric Joyner 	ifmr->ifm_status = IFM_AVALID;
182471d10453SEric Joyner 	ifmr->ifm_active = IFM_ETHER;
182571d10453SEric Joyner 
182671d10453SEric Joyner 	/* Never report link up or media types when in recovery mode */
182771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
182871d10453SEric Joyner 		return;
182971d10453SEric Joyner 
183071d10453SEric Joyner 	if (!sc->link_up)
183171d10453SEric Joyner 		return;
183271d10453SEric Joyner 
183371d10453SEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
183471d10453SEric Joyner 	ifmr->ifm_active |= IFM_FDX;
183571d10453SEric Joyner 
183671d10453SEric Joyner 	if (li->phy_type_low)
183771d10453SEric Joyner 		ifmr->ifm_active |= ice_get_phy_type_low(li->phy_type_low);
183871d10453SEric Joyner 	else if (li->phy_type_high)
183971d10453SEric Joyner 		ifmr->ifm_active |= ice_get_phy_type_high(li->phy_type_high);
184071d10453SEric Joyner 	else
184171d10453SEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
184271d10453SEric Joyner 
184371d10453SEric Joyner 	/* Report flow control status as well */
184471d10453SEric Joyner 	if (li->an_info & ICE_AQ_LINK_PAUSE_TX)
184571d10453SEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
184671d10453SEric Joyner 	if (li->an_info & ICE_AQ_LINK_PAUSE_RX)
184771d10453SEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
184871d10453SEric Joyner }
184971d10453SEric Joyner 
185071d10453SEric Joyner /**
185171d10453SEric Joyner  * ice_init_tx_tracking - Initialize Tx queue software tracking values
185271d10453SEric Joyner  * @vsi: the VSI to initialize
185371d10453SEric Joyner  *
185471d10453SEric Joyner  * Initialize Tx queue software tracking values, including the Report Status
185571d10453SEric Joyner  * queue, and related software tracking values.
185671d10453SEric Joyner  */
185771d10453SEric Joyner static void
185871d10453SEric Joyner ice_init_tx_tracking(struct ice_vsi *vsi)
185971d10453SEric Joyner {
186071d10453SEric Joyner 	struct ice_tx_queue *txq;
186171d10453SEric Joyner 	size_t j;
186271d10453SEric Joyner 	int i;
186371d10453SEric Joyner 
186471d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
186571d10453SEric Joyner 
186671d10453SEric Joyner 		txq->tx_rs_cidx = txq->tx_rs_pidx = 0;
186771d10453SEric Joyner 
186871d10453SEric Joyner 		/* Initialize the last processed descriptor to be the end of
186971d10453SEric Joyner 		 * the ring, rather than the start, so that we avoid an
187071d10453SEric Joyner 		 * off-by-one error in ice_ift_txd_credits_update for the
187171d10453SEric Joyner 		 * first packet.
187271d10453SEric Joyner 		 */
187371d10453SEric Joyner 		txq->tx_cidx_processed = txq->desc_count - 1;
187471d10453SEric Joyner 
187571d10453SEric Joyner 		for (j = 0; j < txq->desc_count; j++)
187671d10453SEric Joyner 			txq->tx_rsq[j] = QIDX_INVALID;
187771d10453SEric Joyner 	}
187871d10453SEric Joyner }
187971d10453SEric Joyner 
188071d10453SEric Joyner /**
188171d10453SEric Joyner  * ice_update_rx_mbuf_sz - Update the Rx buffer size for all queues
188271d10453SEric Joyner  * @sc: the device softc
188371d10453SEric Joyner  *
188471d10453SEric Joyner  * Called to update the Rx queue mbuf_sz parameter for configuring the receive
188571d10453SEric Joyner  * buffer sizes when programming hardware.
188671d10453SEric Joyner  */
188771d10453SEric Joyner static void
188871d10453SEric Joyner ice_update_rx_mbuf_sz(struct ice_softc *sc)
188971d10453SEric Joyner {
189071d10453SEric Joyner 	uint32_t mbuf_sz = iflib_get_rx_mbuf_sz(sc->ctx);
189171d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
189271d10453SEric Joyner 
189371d10453SEric Joyner 	MPASS(mbuf_sz <= UINT16_MAX);
189471d10453SEric Joyner 	vsi->mbuf_sz = mbuf_sz;
189571d10453SEric Joyner }
189671d10453SEric Joyner 
189771d10453SEric Joyner /**
189871d10453SEric Joyner  * ice_if_init - Initialize the device
189971d10453SEric Joyner  * @ctx: iflib ctx structure
190071d10453SEric Joyner  *
190171d10453SEric Joyner  * Called by iflib to bring the device up, i.e. ifconfig ice0 up. Initializes
190271d10453SEric Joyner  * device filters and prepares the Tx and Rx engines.
190371d10453SEric Joyner  *
190471d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
190571d10453SEric Joyner  */
190671d10453SEric Joyner static void
190771d10453SEric Joyner ice_if_init(if_ctx_t ctx)
190871d10453SEric Joyner {
190971d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
191071d10453SEric Joyner 	device_t dev = sc->dev;
191171d10453SEric Joyner 	int err;
191271d10453SEric Joyner 
191371d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
191471d10453SEric Joyner 
19157d7af7f8SEric Joyner 	/*
19167d7af7f8SEric Joyner 	 * We've seen an issue with 11.3/12.1 where sideband routines are
19177d7af7f8SEric Joyner 	 * called after detach is called.  This would call routines after
19187d7af7f8SEric Joyner 	 * if_stop, causing issues with the teardown process.  This has
19197d7af7f8SEric Joyner 	 * seemingly been fixed in STABLE snapshots, but it seems like a
19207d7af7f8SEric Joyner 	 * good idea to have this guard here regardless.
19217d7af7f8SEric Joyner 	 */
19227d7af7f8SEric Joyner 	if (ice_driver_is_detaching(sc))
19237d7af7f8SEric Joyner 		return;
19247d7af7f8SEric Joyner 
192571d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
192671d10453SEric Joyner 		return;
192771d10453SEric Joyner 
192871d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
192971d10453SEric Joyner 		device_printf(sc->dev, "request to start interface cannot be completed as the device failed to reset\n");
193071d10453SEric Joyner 		return;
193171d10453SEric Joyner 	}
193271d10453SEric Joyner 
193371d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
193471d10453SEric Joyner 		device_printf(sc->dev, "request to start interface while device is prepared for impending reset\n");
193571d10453SEric Joyner 		return;
193671d10453SEric Joyner 	}
193771d10453SEric Joyner 
193871d10453SEric Joyner 	ice_update_rx_mbuf_sz(sc);
193971d10453SEric Joyner 
194071d10453SEric Joyner 	/* Update the MAC address... User might use a LAA */
194171d10453SEric Joyner 	err = ice_update_laa_mac(sc);
194271d10453SEric Joyner 	if (err) {
194371d10453SEric Joyner 		device_printf(dev,
194471d10453SEric Joyner 			      "LAA address change failed, err %s\n",
194571d10453SEric Joyner 			      ice_err_str(err));
194671d10453SEric Joyner 		return;
194771d10453SEric Joyner 	}
194871d10453SEric Joyner 
194971d10453SEric Joyner 	/* Initialize software Tx tracking values */
195071d10453SEric Joyner 	ice_init_tx_tracking(&sc->pf_vsi);
195171d10453SEric Joyner 
195271d10453SEric Joyner 	err = ice_cfg_vsi_for_tx(&sc->pf_vsi);
195371d10453SEric Joyner 	if (err) {
195471d10453SEric Joyner 		device_printf(dev,
195571d10453SEric Joyner 			      "Unable to configure the main VSI for Tx: %s\n",
195671d10453SEric Joyner 			      ice_err_str(err));
195771d10453SEric Joyner 		return;
195871d10453SEric Joyner 	}
195971d10453SEric Joyner 
196071d10453SEric Joyner 	err = ice_cfg_vsi_for_rx(&sc->pf_vsi);
196171d10453SEric Joyner 	if (err) {
196271d10453SEric Joyner 		device_printf(dev,
196371d10453SEric Joyner 			      "Unable to configure the main VSI for Rx: %s\n",
196471d10453SEric Joyner 			      ice_err_str(err));
196571d10453SEric Joyner 		goto err_cleanup_tx;
196671d10453SEric Joyner 	}
196771d10453SEric Joyner 
196871d10453SEric Joyner 	err = ice_control_rx_queues(&sc->pf_vsi, true);
196971d10453SEric Joyner 	if (err) {
197071d10453SEric Joyner 		device_printf(dev,
197171d10453SEric Joyner 			      "Unable to enable Rx rings for transmit: %s\n",
197271d10453SEric Joyner 			      ice_err_str(err));
197371d10453SEric Joyner 		goto err_cleanup_tx;
197471d10453SEric Joyner 	}
197571d10453SEric Joyner 
197671d10453SEric Joyner 	err = ice_cfg_pf_default_mac_filters(sc);
197771d10453SEric Joyner 	if (err) {
197871d10453SEric Joyner 		device_printf(dev,
197971d10453SEric Joyner 			      "Unable to configure default MAC filters: %s\n",
198071d10453SEric Joyner 			      ice_err_str(err));
198171d10453SEric Joyner 		goto err_stop_rx;
198271d10453SEric Joyner 	}
198371d10453SEric Joyner 
198471d10453SEric Joyner 	/* We use software interrupts for Tx, so we only program the hardware
198571d10453SEric Joyner 	 * interrupts for Rx.
198671d10453SEric Joyner 	 */
198771d10453SEric Joyner 	ice_configure_rxq_interrupts(&sc->pf_vsi);
198871d10453SEric Joyner 	ice_configure_rx_itr(&sc->pf_vsi);
198971d10453SEric Joyner 
199071d10453SEric Joyner 	/* Configure promiscuous mode */
199171d10453SEric Joyner 	ice_if_promisc_set(ctx, if_getflags(sc->ifp));
199271d10453SEric Joyner 
19938a13362dSEric Joyner 	ice_rdma_pf_init(sc);
19948a13362dSEric Joyner 
199571d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED);
199671d10453SEric Joyner 	return;
199771d10453SEric Joyner 
199871d10453SEric Joyner err_stop_rx:
199971d10453SEric Joyner 	ice_control_rx_queues(&sc->pf_vsi, false);
200071d10453SEric Joyner err_cleanup_tx:
200171d10453SEric Joyner 	ice_vsi_disable_tx(&sc->pf_vsi);
200271d10453SEric Joyner }
200371d10453SEric Joyner 
200471d10453SEric Joyner /**
200571d10453SEric Joyner  * ice_poll_for_media_avail - Re-enable link if media is detected
200671d10453SEric Joyner  * @sc: device private structure
200771d10453SEric Joyner  *
200871d10453SEric Joyner  * Intended to be called from the driver's timer function, this function
200971d10453SEric Joyner  * sends the Get Link Status AQ command and re-enables HW link if the
201071d10453SEric Joyner  * command says that media is available.
201171d10453SEric Joyner  *
201271d10453SEric Joyner  * If the driver doesn't have the "NO_MEDIA" state set, then this does nothing,
201371d10453SEric Joyner  * since media removal events are supposed to be sent to the driver through
201471d10453SEric Joyner  * a link status event.
201571d10453SEric Joyner  */
201671d10453SEric Joyner static void
201771d10453SEric Joyner ice_poll_for_media_avail(struct ice_softc *sc)
201871d10453SEric Joyner {
201971d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
202071d10453SEric Joyner 	struct ice_port_info *pi = hw->port_info;
202171d10453SEric Joyner 
202271d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA)) {
202371d10453SEric Joyner 		pi->phy.get_link_info = true;
202471d10453SEric Joyner 		ice_get_link_status(pi, &sc->link_up);
202571d10453SEric Joyner 
202671d10453SEric Joyner 		if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
202771d10453SEric Joyner 			enum ice_status status;
202871d10453SEric Joyner 
202971d10453SEric Joyner 			/* Re-enable link and re-apply user link settings */
20309cf1841cSEric Joyner 			ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS_FEC_FC);
203171d10453SEric Joyner 
203271d10453SEric Joyner 			/* Update the OS about changes in media capability */
203371d10453SEric Joyner 			status = ice_add_media_types(sc, sc->media);
203471d10453SEric Joyner 			if (status)
203571d10453SEric Joyner 				device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
203671d10453SEric Joyner 					      ice_status_str(status),
203771d10453SEric Joyner 					      ice_aq_str(hw->adminq.sq_last_status));
203871d10453SEric Joyner 
203971d10453SEric Joyner 			ice_clear_state(&sc->state, ICE_STATE_NO_MEDIA);
204071d10453SEric Joyner 		}
204171d10453SEric Joyner 	}
204271d10453SEric Joyner }
204371d10453SEric Joyner 
204471d10453SEric Joyner /**
204571d10453SEric Joyner  * ice_if_timer - called by iflib periodically
204671d10453SEric Joyner  * @ctx: iflib ctx structure
204771d10453SEric Joyner  * @qid: the queue this timer was called for
204871d10453SEric Joyner  *
204971d10453SEric Joyner  * This callback is triggered by iflib periodically. We use it to update the
205071d10453SEric Joyner  * hw statistics.
205171d10453SEric Joyner  *
205271d10453SEric Joyner  * @remark this function is not protected by the iflib CTX lock.
205371d10453SEric Joyner  */
205471d10453SEric Joyner static void
205571d10453SEric Joyner ice_if_timer(if_ctx_t ctx, uint16_t qid)
205671d10453SEric Joyner {
205771d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
205871d10453SEric Joyner 	uint64_t prev_link_xoff_rx = sc->stats.cur.link_xoff_rx;
205971d10453SEric Joyner 
206071d10453SEric Joyner 	if (qid != 0)
206171d10453SEric Joyner 		return;
206271d10453SEric Joyner 
206371d10453SEric Joyner 	/* Do not attempt to update stats when in recovery mode */
206471d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
206571d10453SEric Joyner 		return;
206671d10453SEric Joyner 
206771d10453SEric Joyner 	/* Update device statistics */
206871d10453SEric Joyner 	ice_update_pf_stats(sc);
206971d10453SEric Joyner 
207071d10453SEric Joyner 	/*
207171d10453SEric Joyner 	 * For proper watchdog management, the iflib stack needs to know if
207271d10453SEric Joyner 	 * we've been paused during the last interval. Check if the
207371d10453SEric Joyner 	 * link_xoff_rx stat changed, and set the isc_pause_frames, if so.
207471d10453SEric Joyner 	 */
207571d10453SEric Joyner 	if (sc->stats.cur.link_xoff_rx != prev_link_xoff_rx)
207671d10453SEric Joyner 		sc->scctx->isc_pause_frames = 1;
207771d10453SEric Joyner 
207871d10453SEric Joyner 	/* Update the primary VSI stats */
207971d10453SEric Joyner 	ice_update_vsi_hw_stats(&sc->pf_vsi);
208071d10453SEric Joyner }
208171d10453SEric Joyner 
208271d10453SEric Joyner /**
208371d10453SEric Joyner  * ice_admin_timer - called periodically to trigger the admin task
208471d10453SEric Joyner  * @arg: callout(9) argument pointing to the device private softc structure
208571d10453SEric Joyner  *
208671d10453SEric Joyner  * Timer function used as part of a callout(9) timer that will periodically
208771d10453SEric Joyner  * trigger the admin task, even when the interface is down.
208871d10453SEric Joyner  *
208971d10453SEric Joyner  * @remark this function is not called by iflib and is not protected by the
209071d10453SEric Joyner  * iflib CTX lock.
209171d10453SEric Joyner  *
209271d10453SEric Joyner  * @remark because this is a callout function, it cannot sleep and should not
209371d10453SEric Joyner  * attempt taking the iflib CTX lock.
209471d10453SEric Joyner  */
209571d10453SEric Joyner static void
209671d10453SEric Joyner ice_admin_timer(void *arg)
209771d10453SEric Joyner {
209871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)arg;
209971d10453SEric Joyner 
21009cf1841cSEric Joyner 	/*
21019cf1841cSEric Joyner 	 * There is a point where callout routines are no longer
21029cf1841cSEric Joyner 	 * cancelable.  So there exists a window of time where the
21039cf1841cSEric Joyner 	 * driver enters detach() and tries to cancel the callout, but the
21049cf1841cSEric Joyner 	 * callout routine has passed the cancellation point.  The detach()
21059cf1841cSEric Joyner 	 * routine is unaware of this and tries to free resources that the
21069cf1841cSEric Joyner 	 * callout routine needs.  So we check for the detach state flag to
21079cf1841cSEric Joyner 	 * at least shrink the window of opportunity.
21089cf1841cSEric Joyner 	 */
21099cf1841cSEric Joyner 	if (ice_driver_is_detaching(sc))
21109cf1841cSEric Joyner 		return;
21119cf1841cSEric Joyner 
211271d10453SEric Joyner 	/* Fire off the admin task */
211371d10453SEric Joyner 	iflib_admin_intr_deferred(sc->ctx);
211471d10453SEric Joyner 
211571d10453SEric Joyner 	/* Reschedule the admin timer */
211671d10453SEric Joyner 	callout_schedule(&sc->admin_timer, hz/2);
211771d10453SEric Joyner }
211871d10453SEric Joyner 
211971d10453SEric Joyner /**
212071d10453SEric Joyner  * ice_transition_recovery_mode - Transition to recovery mode
212171d10453SEric Joyner  * @sc: the device private softc
212271d10453SEric Joyner  *
212371d10453SEric Joyner  * Called when the driver detects that the firmware has entered recovery mode
212471d10453SEric Joyner  * at run time.
212571d10453SEric Joyner  */
212671d10453SEric Joyner static void
212771d10453SEric Joyner ice_transition_recovery_mode(struct ice_softc *sc)
212871d10453SEric Joyner {
212971d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
213071d10453SEric Joyner 	int i;
213171d10453SEric Joyner 
213271d10453SEric Joyner 	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");
213371d10453SEric Joyner 
213471d10453SEric Joyner 	/* Tell the stack that the link has gone down */
213571d10453SEric Joyner 	iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
213671d10453SEric Joyner 
213771d10453SEric Joyner 	/* Request that the device be re-initialized */
213871d10453SEric Joyner 	ice_request_stack_reinit(sc);
213971d10453SEric Joyner 
21408a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
21418a13362dSEric Joyner 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
21428a13362dSEric Joyner 
214371d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
214471d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
214571d10453SEric Joyner 
214671d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
214771d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
214871d10453SEric Joyner 
214971d10453SEric Joyner 	for (i = 0; i < sc->num_available_vsi; i++) {
215071d10453SEric Joyner 		if (sc->all_vsi[i])
215171d10453SEric Joyner 			ice_release_vsi(sc->all_vsi[i]);
215271d10453SEric Joyner 	}
215371d10453SEric Joyner 	sc->num_available_vsi = 0;
215471d10453SEric Joyner 
215571d10453SEric Joyner 	if (sc->all_vsi) {
215671d10453SEric Joyner 		free(sc->all_vsi, M_ICE);
215771d10453SEric Joyner 		sc->all_vsi = NULL;
215871d10453SEric Joyner 	}
215971d10453SEric Joyner 
216071d10453SEric Joyner 	/* Destroy the interrupt manager */
216171d10453SEric Joyner 	ice_resmgr_destroy(&sc->imgr);
216271d10453SEric Joyner 	/* Destroy the queue managers */
216371d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
216471d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
216571d10453SEric Joyner 
216671d10453SEric Joyner 	ice_deinit_hw(&sc->hw);
216771d10453SEric Joyner }
216871d10453SEric Joyner 
216971d10453SEric Joyner /**
217071d10453SEric Joyner  * ice_transition_safe_mode - Transition to safe mode
217171d10453SEric Joyner  * @sc: the device private softc
217271d10453SEric Joyner  *
217371d10453SEric Joyner  * Called when the driver attempts to reload the DDP package during a device
217471d10453SEric Joyner  * reset, and the new download fails. If so, we must transition to safe mode
217571d10453SEric Joyner  * at run time.
217671d10453SEric Joyner  *
217771d10453SEric Joyner  * @remark although safe mode normally allocates only a single queue, we can't
217871d10453SEric Joyner  * change the number of queues dynamically when using iflib. Due to this, we
217971d10453SEric Joyner  * do not attempt to reduce the number of queues.
218071d10453SEric Joyner  */
218171d10453SEric Joyner static void
218271d10453SEric Joyner ice_transition_safe_mode(struct ice_softc *sc)
218371d10453SEric Joyner {
218471d10453SEric Joyner 	/* Indicate that we are in Safe mode */
218571d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_cap);
218671d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_en);
218771d10453SEric Joyner 
21888a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
21898a13362dSEric Joyner 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
21908a13362dSEric Joyner 
219171d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
219271d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
219371d10453SEric Joyner 
219471d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
219571d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_en);
219671d10453SEric Joyner }
219771d10453SEric Joyner 
219871d10453SEric Joyner /**
219971d10453SEric Joyner  * ice_if_update_admin_status - update admin status
220071d10453SEric Joyner  * @ctx: iflib ctx structure
220171d10453SEric Joyner  *
220271d10453SEric Joyner  * Called by iflib to update the admin status. For our purposes, this means
220371d10453SEric Joyner  * check the adminq, and update the link status. It's ultimately triggered by
220471d10453SEric Joyner  * our admin interrupt, or by the ice_if_timer periodically.
220571d10453SEric Joyner  *
220671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
220771d10453SEric Joyner  */
220871d10453SEric Joyner static void
220971d10453SEric Joyner ice_if_update_admin_status(if_ctx_t ctx)
221071d10453SEric Joyner {
221171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
221271d10453SEric Joyner 	enum ice_fw_modes fw_mode;
221371d10453SEric Joyner 	bool reschedule = false;
221471d10453SEric Joyner 	u16 pending = 0;
221571d10453SEric Joyner 
221671d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
221771d10453SEric Joyner 
221871d10453SEric Joyner 	/* Check if the firmware entered recovery mode at run time */
221971d10453SEric Joyner 	fw_mode = ice_get_fw_mode(&sc->hw);
222071d10453SEric Joyner 	if (fw_mode == ICE_FW_MODE_REC) {
222171d10453SEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
222271d10453SEric Joyner 			/* If we just entered recovery mode, log a warning to
222371d10453SEric Joyner 			 * the system administrator and deinit driver state
222471d10453SEric Joyner 			 * that is no longer functional.
222571d10453SEric Joyner 			 */
222671d10453SEric Joyner 			ice_transition_recovery_mode(sc);
222771d10453SEric Joyner 		}
222871d10453SEric Joyner 	} else if (fw_mode == ICE_FW_MODE_ROLLBACK) {
222971d10453SEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_ROLLBACK_MODE)) {
223071d10453SEric Joyner 			/* Rollback mode isn't fatal, but we don't want to
223171d10453SEric Joyner 			 * repeatedly post a message about it.
223271d10453SEric Joyner 			 */
223371d10453SEric Joyner 			ice_print_rollback_msg(&sc->hw);
223471d10453SEric Joyner 		}
223571d10453SEric Joyner 	}
223671d10453SEric Joyner 
223771d10453SEric Joyner 	/* Handle global reset events */
223871d10453SEric Joyner 	ice_handle_reset_event(sc);
223971d10453SEric Joyner 
224071d10453SEric Joyner 	/* Handle PF reset requests */
224171d10453SEric Joyner 	ice_handle_pf_reset_request(sc);
224271d10453SEric Joyner 
224371d10453SEric Joyner 	/* Handle MDD events */
224471d10453SEric Joyner 	ice_handle_mdd_event(sc);
224571d10453SEric Joyner 
224671d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED) ||
224771d10453SEric Joyner 	    ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET) ||
224871d10453SEric Joyner 	    ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
224971d10453SEric Joyner 		/*
225071d10453SEric Joyner 		 * If we know the control queues are disabled, skip processing
225171d10453SEric Joyner 		 * the control queues entirely.
225271d10453SEric Joyner 		 */
225371d10453SEric Joyner 		;
225471d10453SEric Joyner 	} else if (ice_testandclear_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING)) {
225571d10453SEric Joyner 		ice_process_ctrlq(sc, ICE_CTL_Q_ADMIN, &pending);
225671d10453SEric Joyner 		if (pending > 0)
225771d10453SEric Joyner 			reschedule = true;
225871d10453SEric Joyner 
225971d10453SEric Joyner 		ice_process_ctrlq(sc, ICE_CTL_Q_MAILBOX, &pending);
226071d10453SEric Joyner 		if (pending > 0)
226171d10453SEric Joyner 			reschedule = true;
226271d10453SEric Joyner 	}
226371d10453SEric Joyner 
226471d10453SEric Joyner 	/* Poll for link up */
226571d10453SEric Joyner 	ice_poll_for_media_avail(sc);
226671d10453SEric Joyner 
226771d10453SEric Joyner 	/* Check and update link status */
226871d10453SEric Joyner 	ice_update_link_status(sc, false);
226971d10453SEric Joyner 
227071d10453SEric Joyner 	/*
227171d10453SEric Joyner 	 * If there are still messages to process, we need to reschedule
227271d10453SEric Joyner 	 * ourselves. Otherwise, we can just re-enable the interrupt. We'll be
227371d10453SEric Joyner 	 * woken up at the next interrupt or timer event.
227471d10453SEric Joyner 	 */
227571d10453SEric Joyner 	if (reschedule) {
227671d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
227771d10453SEric Joyner 		iflib_admin_intr_deferred(ctx);
227871d10453SEric Joyner 	} else {
227971d10453SEric Joyner 		ice_enable_intr(&sc->hw, sc->irqvs[0].me);
228071d10453SEric Joyner 	}
228171d10453SEric Joyner }
228271d10453SEric Joyner 
228371d10453SEric Joyner /**
228471d10453SEric Joyner  * ice_prepare_for_reset - Prepare device for an impending reset
228571d10453SEric Joyner  * @sc: The device private softc
228671d10453SEric Joyner  *
228771d10453SEric Joyner  * Prepare the driver for an impending reset, shutting down VSIs, clearing the
228871d10453SEric Joyner  * scheduler setup, and shutting down controlqs. Uses the
228971d10453SEric Joyner  * ICE_STATE_PREPARED_FOR_RESET to indicate whether we've already prepared the
229071d10453SEric Joyner  * driver for reset or not.
229171d10453SEric Joyner  */
229271d10453SEric Joyner static void
229371d10453SEric Joyner ice_prepare_for_reset(struct ice_softc *sc)
229471d10453SEric Joyner {
229571d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
229671d10453SEric Joyner 
229771d10453SEric Joyner 	/* If we're already prepared, there's nothing to do */
229871d10453SEric Joyner 	if (ice_testandset_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET))
229971d10453SEric Joyner 		return;
230071d10453SEric Joyner 
2301402810d3SJustin Hibbits 	log(LOG_INFO, "%s: preparing to reset device logic\n", if_name(sc->ifp));
230271d10453SEric Joyner 
230371d10453SEric Joyner 	/* In recovery mode, hardware is not initialized */
230471d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
230571d10453SEric Joyner 		return;
230671d10453SEric Joyner 
23078a13362dSEric Joyner 	/* stop the RDMA client */
23088a13362dSEric Joyner 	ice_rdma_pf_stop(sc);
23098a13362dSEric Joyner 
231071d10453SEric Joyner 	/* Release the main PF VSI queue mappings */
231171d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
231271d10453SEric Joyner 				    sc->pf_vsi.num_tx_queues);
231371d10453SEric Joyner 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
231471d10453SEric Joyner 				    sc->pf_vsi.num_rx_queues);
231571d10453SEric Joyner 
231671d10453SEric Joyner 	ice_clear_hw_tbls(hw);
231771d10453SEric Joyner 
231871d10453SEric Joyner 	if (hw->port_info)
231971d10453SEric Joyner 		ice_sched_clear_port(hw->port_info);
232071d10453SEric Joyner 
2321*8923de59SPiotr Kubaj 	ice_shutdown_all_ctrlq(hw, false);
232271d10453SEric Joyner }
232371d10453SEric Joyner 
232471d10453SEric Joyner /**
232571d10453SEric Joyner  * ice_rebuild_pf_vsi_qmap - Rebuild the main PF VSI queue mapping
232671d10453SEric Joyner  * @sc: the device softc pointer
232771d10453SEric Joyner  *
232871d10453SEric Joyner  * Loops over the Tx and Rx queues for the main PF VSI and reassigns the queue
232971d10453SEric Joyner  * mapping after a reset occurred.
233071d10453SEric Joyner  */
233171d10453SEric Joyner static int
233271d10453SEric Joyner ice_rebuild_pf_vsi_qmap(struct ice_softc *sc)
233371d10453SEric Joyner {
233471d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
233571d10453SEric Joyner 	struct ice_tx_queue *txq;
233671d10453SEric Joyner 	struct ice_rx_queue *rxq;
233771d10453SEric Joyner 	int err, i;
233871d10453SEric Joyner 
233971d10453SEric Joyner 	/* Re-assign Tx queues from PF space to the main VSI */
234071d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap,
234171d10453SEric Joyner 					    vsi->num_tx_queues);
234271d10453SEric Joyner 	if (err) {
234371d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign PF Tx queues: %s\n",
234471d10453SEric Joyner 			      ice_err_str(err));
234571d10453SEric Joyner 		return (err);
234671d10453SEric Joyner 	}
234771d10453SEric Joyner 
234871d10453SEric Joyner 	/* Re-assign Rx queues from PF space to this VSI */
234971d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap,
235071d10453SEric Joyner 					    vsi->num_rx_queues);
235171d10453SEric Joyner 	if (err) {
235271d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign PF Rx queues: %s\n",
235371d10453SEric Joyner 			      ice_err_str(err));
235471d10453SEric Joyner 		goto err_release_tx_queues;
235571d10453SEric Joyner 	}
235671d10453SEric Joyner 
235771d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
235871d10453SEric Joyner 
235971d10453SEric Joyner 	/* Re-assign Tx queue tail pointers */
236071d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
236171d10453SEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
236271d10453SEric Joyner 
236371d10453SEric Joyner 	/* Re-assign Rx queue tail pointers */
236471d10453SEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
236571d10453SEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
236671d10453SEric Joyner 
236771d10453SEric Joyner 	return (0);
236871d10453SEric Joyner 
236971d10453SEric Joyner err_release_tx_queues:
237071d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
237171d10453SEric Joyner 				   sc->pf_vsi.num_tx_queues);
237271d10453SEric Joyner 
237371d10453SEric Joyner 	return (err);
237471d10453SEric Joyner }
237571d10453SEric Joyner 
237671d10453SEric Joyner /* determine if the iflib context is active */
237771d10453SEric Joyner #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
237871d10453SEric Joyner 
237971d10453SEric Joyner /**
238071d10453SEric Joyner  * ice_rebuild_recovery_mode - Rebuild driver state while in recovery mode
238171d10453SEric Joyner  * @sc: The device private softc
238271d10453SEric Joyner  *
238371d10453SEric Joyner  * Handle a driver rebuild while in recovery mode. This will only rebuild the
238471d10453SEric Joyner  * limited functionality supported while in recovery mode.
238571d10453SEric Joyner  */
238671d10453SEric Joyner static void
238771d10453SEric Joyner ice_rebuild_recovery_mode(struct ice_softc *sc)
238871d10453SEric Joyner {
238971d10453SEric Joyner 	device_t dev = sc->dev;
239071d10453SEric Joyner 
239171d10453SEric Joyner 	/* enable PCIe bus master */
239271d10453SEric Joyner 	pci_enable_busmaster(dev);
239371d10453SEric Joyner 
239471d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
239571d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
239671d10453SEric Joyner 
239771d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
239871d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
239971d10453SEric Joyner 
240071d10453SEric Joyner 	/* Now that the rebuild is finished, we're no longer prepared to reset */
240171d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
240271d10453SEric Joyner 
2403402810d3SJustin Hibbits 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
240471d10453SEric Joyner 
240571d10453SEric Joyner 	/* In order to completely restore device functionality, the iflib core
240671d10453SEric Joyner 	 * needs to be reset. We need to request an iflib reset. Additionally,
240771d10453SEric Joyner 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
240871d10453SEric Joyner 	 * the iflib core, we also want re-run the admin task so that iflib
240971d10453SEric Joyner 	 * resets immediately instead of waiting for the next interrupt.
241071d10453SEric Joyner 	 */
241171d10453SEric Joyner 	ice_request_stack_reinit(sc);
241271d10453SEric Joyner 
241371d10453SEric Joyner 	return;
241471d10453SEric Joyner }
241571d10453SEric Joyner 
241671d10453SEric Joyner /**
241771d10453SEric Joyner  * ice_rebuild - Rebuild driver state post reset
241871d10453SEric Joyner  * @sc: The device private softc
241971d10453SEric Joyner  *
242071d10453SEric Joyner  * Restore driver state after a reset occurred. Restart the controlqs, setup
242171d10453SEric Joyner  * the hardware port, and re-enable the VSIs.
242271d10453SEric Joyner  */
242371d10453SEric Joyner static void
242471d10453SEric Joyner ice_rebuild(struct ice_softc *sc)
242571d10453SEric Joyner {
242671d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
242771d10453SEric Joyner 	device_t dev = sc->dev;
2428*8923de59SPiotr Kubaj 	enum ice_ddp_state pkg_state;
242971d10453SEric Joyner 	enum ice_status status;
243071d10453SEric Joyner 	int err;
243171d10453SEric Joyner 
243271d10453SEric Joyner 	sc->rebuild_ticks = ticks;
243371d10453SEric Joyner 
243471d10453SEric Joyner 	/* If we're rebuilding, then a reset has succeeded. */
243571d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_RESET_FAILED);
243671d10453SEric Joyner 
243771d10453SEric Joyner 	/*
243871d10453SEric Joyner 	 * If the firmware is in recovery mode, only restore the limited
243971d10453SEric Joyner 	 * functionality supported by recovery mode.
244071d10453SEric Joyner 	 */
244171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
244271d10453SEric Joyner 		ice_rebuild_recovery_mode(sc);
244371d10453SEric Joyner 		return;
244471d10453SEric Joyner 	}
244571d10453SEric Joyner 
244671d10453SEric Joyner 	/* enable PCIe bus master */
244771d10453SEric Joyner 	pci_enable_busmaster(dev);
244871d10453SEric Joyner 
244971d10453SEric Joyner 	status = ice_init_all_ctrlq(hw);
245071d10453SEric Joyner 	if (status) {
245171d10453SEric Joyner 		device_printf(dev, "failed to re-init controlqs, err %s\n",
245271d10453SEric Joyner 			      ice_status_str(status));
245371d10453SEric Joyner 		goto err_shutdown_ctrlq;
245471d10453SEric Joyner 	}
245571d10453SEric Joyner 
245671d10453SEric Joyner 	/* Query the allocated resources for Tx scheduler */
245771d10453SEric Joyner 	status = ice_sched_query_res_alloc(hw);
245871d10453SEric Joyner 	if (status) {
245971d10453SEric Joyner 		device_printf(dev,
246071d10453SEric Joyner 			      "Failed to query scheduler resources, err %s aq_err %s\n",
246171d10453SEric Joyner 			      ice_status_str(status),
246271d10453SEric Joyner 			      ice_aq_str(hw->adminq.sq_last_status));
246371d10453SEric Joyner 		goto err_shutdown_ctrlq;
246471d10453SEric Joyner 	}
246571d10453SEric Joyner 
246656429daeSEric Joyner 	/* Re-enable FW logging. Keep going even if this fails */
246756429daeSEric Joyner 	status = ice_fwlog_set(hw, &hw->fwlog_cfg);
246856429daeSEric Joyner 	if (!status) {
246956429daeSEric Joyner 		/*
247056429daeSEric Joyner 		 * We should have the most updated cached copy of the
247156429daeSEric Joyner 		 * configuration, regardless of whether we're rebuilding
247256429daeSEric Joyner 		 * or not.  So we'll simply check to see if logging was
247356429daeSEric Joyner 		 * enabled pre-rebuild.
247456429daeSEric Joyner 		 */
247556429daeSEric Joyner 		if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
247656429daeSEric Joyner 			status = ice_fwlog_register(hw);
247756429daeSEric Joyner 			if (status)
247856429daeSEric Joyner 				device_printf(dev, "failed to re-register fw logging, err %s aq_err %s\n",
247956429daeSEric Joyner 				   ice_status_str(status),
248056429daeSEric Joyner 				   ice_aq_str(hw->adminq.sq_last_status));
248156429daeSEric Joyner 		}
248256429daeSEric Joyner 	} else
248356429daeSEric Joyner 		device_printf(dev, "failed to rebuild fw logging configuration, err %s aq_err %s\n",
248456429daeSEric Joyner 		   ice_status_str(status),
248556429daeSEric Joyner 		   ice_aq_str(hw->adminq.sq_last_status));
248656429daeSEric Joyner 
248771d10453SEric Joyner 	err = ice_send_version(sc);
248871d10453SEric Joyner 	if (err)
248971d10453SEric Joyner 		goto err_shutdown_ctrlq;
249071d10453SEric Joyner 
249171d10453SEric Joyner 	err = ice_init_link_events(sc);
249271d10453SEric Joyner 	if (err) {
249371d10453SEric Joyner 		device_printf(dev, "ice_init_link_events failed: %s\n",
249471d10453SEric Joyner 			      ice_err_str(err));
249571d10453SEric Joyner 		goto err_shutdown_ctrlq;
249671d10453SEric Joyner 	}
249771d10453SEric Joyner 
249871d10453SEric Joyner 	status = ice_clear_pf_cfg(hw);
249971d10453SEric Joyner 	if (status) {
250071d10453SEric Joyner 		device_printf(dev, "failed to clear PF configuration, err %s\n",
250171d10453SEric Joyner 			      ice_status_str(status));
250271d10453SEric Joyner 		goto err_shutdown_ctrlq;
250371d10453SEric Joyner 	}
250471d10453SEric Joyner 
250571d10453SEric Joyner 	ice_clear_pxe_mode(hw);
250671d10453SEric Joyner 
250771d10453SEric Joyner 	status = ice_get_caps(hw);
250871d10453SEric Joyner 	if (status) {
250971d10453SEric Joyner 		device_printf(dev, "failed to get capabilities, err %s\n",
251071d10453SEric Joyner 			      ice_status_str(status));
251171d10453SEric Joyner 		goto err_shutdown_ctrlq;
251271d10453SEric Joyner 	}
251371d10453SEric Joyner 
251471d10453SEric Joyner 	status = ice_sched_init_port(hw->port_info);
251571d10453SEric Joyner 	if (status) {
251671d10453SEric Joyner 		device_printf(dev, "failed to initialize port, err %s\n",
251771d10453SEric Joyner 			      ice_status_str(status));
251871d10453SEric Joyner 		goto err_sched_cleanup;
251971d10453SEric Joyner 	}
252071d10453SEric Joyner 
252171d10453SEric Joyner 	/* If we previously loaded the package, it needs to be reloaded now */
252271d10453SEric Joyner 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE)) {
2523*8923de59SPiotr Kubaj 		pkg_state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
2524*8923de59SPiotr Kubaj 		if (!ice_is_init_pkg_successful(pkg_state)) {
2525*8923de59SPiotr Kubaj 			ice_log_pkg_init(sc, pkg_state);
252671d10453SEric Joyner 			ice_transition_safe_mode(sc);
252771d10453SEric Joyner 		}
252871d10453SEric Joyner 	}
252971d10453SEric Joyner 
253071d10453SEric Joyner 	ice_reset_pf_stats(sc);
253171d10453SEric Joyner 
253271d10453SEric Joyner 	err = ice_rebuild_pf_vsi_qmap(sc);
253371d10453SEric Joyner 	if (err) {
253471d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign main VSI queues, err %s\n",
253571d10453SEric Joyner 			      ice_err_str(err));
253671d10453SEric Joyner 		goto err_sched_cleanup;
253771d10453SEric Joyner 	}
253871d10453SEric Joyner 	err = ice_initialize_vsi(&sc->pf_vsi);
253971d10453SEric Joyner 	if (err) {
254071d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-initialize Main VSI, err %s\n",
254171d10453SEric Joyner 			      ice_err_str(err));
254271d10453SEric Joyner 		goto err_release_queue_allocations;
254371d10453SEric Joyner 	}
254471d10453SEric Joyner 
254571d10453SEric Joyner 	/* Replay all VSI configuration */
254671d10453SEric Joyner 	err = ice_replay_all_vsi_cfg(sc);
254771d10453SEric Joyner 	if (err)
254871d10453SEric Joyner 		goto err_deinit_pf_vsi;
254971d10453SEric Joyner 
25509cf1841cSEric Joyner 	/* Re-enable FW health event reporting */
25519cf1841cSEric Joyner 	ice_init_health_events(sc);
25529cf1841cSEric Joyner 
255371d10453SEric Joyner 	/* Reconfigure the main PF VSI for RSS */
255471d10453SEric Joyner 	err = ice_config_rss(&sc->pf_vsi);
255571d10453SEric Joyner 	if (err) {
255671d10453SEric Joyner 		device_printf(sc->dev,
255771d10453SEric Joyner 			      "Unable to reconfigure RSS for the main VSI, err %s\n",
255871d10453SEric Joyner 			      ice_err_str(err));
255971d10453SEric Joyner 		goto err_deinit_pf_vsi;
256071d10453SEric Joyner 	}
256171d10453SEric Joyner 
256271d10453SEric Joyner 	/* Refresh link status */
256371d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
256471d10453SEric Joyner 	sc->hw.port_info->phy.get_link_info = true;
256571d10453SEric Joyner 	ice_get_link_status(sc->hw.port_info, &sc->link_up);
256671d10453SEric Joyner 	ice_update_link_status(sc, true);
256771d10453SEric Joyner 
25688a13362dSEric Joyner 	/* RDMA interface will be restarted by the stack re-init */
25698a13362dSEric Joyner 
257071d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
257171d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
257271d10453SEric Joyner 
257371d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
257471d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
257571d10453SEric Joyner 
257671d10453SEric Joyner 	/* Now that the rebuild is finished, we're no longer prepared to reset */
257771d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
257871d10453SEric Joyner 
2579402810d3SJustin Hibbits 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
258071d10453SEric Joyner 
258171d10453SEric Joyner 	/* In order to completely restore device functionality, the iflib core
258271d10453SEric Joyner 	 * needs to be reset. We need to request an iflib reset. Additionally,
258371d10453SEric Joyner 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
258471d10453SEric Joyner 	 * the iflib core, we also want re-run the admin task so that iflib
258571d10453SEric Joyner 	 * resets immediately instead of waiting for the next interrupt.
258671d10453SEric Joyner 	 */
258771d10453SEric Joyner 	ice_request_stack_reinit(sc);
258871d10453SEric Joyner 
258971d10453SEric Joyner 	return;
259071d10453SEric Joyner 
259171d10453SEric Joyner err_deinit_pf_vsi:
259271d10453SEric Joyner 	ice_deinit_vsi(&sc->pf_vsi);
259371d10453SEric Joyner err_release_queue_allocations:
259471d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
259571d10453SEric Joyner 				    sc->pf_vsi.num_tx_queues);
259671d10453SEric Joyner 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
259771d10453SEric Joyner 				    sc->pf_vsi.num_rx_queues);
259871d10453SEric Joyner err_sched_cleanup:
259971d10453SEric Joyner 	ice_sched_cleanup_all(hw);
260071d10453SEric Joyner err_shutdown_ctrlq:
2601*8923de59SPiotr Kubaj 	ice_shutdown_all_ctrlq(hw, false);
2602*8923de59SPiotr Kubaj 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
260371d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
260471d10453SEric Joyner 	device_printf(dev, "Driver rebuild failed, please reload the device driver\n");
260571d10453SEric Joyner }
260671d10453SEric Joyner 
260771d10453SEric Joyner /**
260871d10453SEric Joyner  * ice_handle_reset_event - Handle reset events triggered by OICR
260971d10453SEric Joyner  * @sc: The device private softc
261071d10453SEric Joyner  *
261171d10453SEric Joyner  * Handle reset events triggered by an OICR notification. This includes CORER,
261271d10453SEric Joyner  * GLOBR, and EMPR resets triggered by software on this or any other PF or by
261371d10453SEric Joyner  * firmware.
261471d10453SEric Joyner  *
261571d10453SEric Joyner  * @pre assumes the iflib context lock is held, and will unlock it while
261671d10453SEric Joyner  * waiting for the hardware to finish reset.
261771d10453SEric Joyner  */
261871d10453SEric Joyner static void
261971d10453SEric Joyner ice_handle_reset_event(struct ice_softc *sc)
262071d10453SEric Joyner {
262171d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
262271d10453SEric Joyner 	enum ice_status status;
262371d10453SEric Joyner 	device_t dev = sc->dev;
262471d10453SEric Joyner 
262571d10453SEric Joyner 	/* When a CORER, GLOBR, or EMPR is about to happen, the hardware will
262671d10453SEric Joyner 	 * trigger an OICR interrupt. Our OICR handler will determine when
262771d10453SEric Joyner 	 * this occurs and set the ICE_STATE_RESET_OICR_RECV bit as
262871d10453SEric Joyner 	 * appropriate.
262971d10453SEric Joyner 	 */
263071d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
263171d10453SEric Joyner 		return;
263271d10453SEric Joyner 
263371d10453SEric Joyner 	ice_prepare_for_reset(sc);
263471d10453SEric Joyner 
263571d10453SEric Joyner 	/*
263671d10453SEric Joyner 	 * Release the iflib context lock and wait for the device to finish
263771d10453SEric Joyner 	 * resetting.
263871d10453SEric Joyner 	 */
263971d10453SEric Joyner 	IFLIB_CTX_UNLOCK(sc);
264071d10453SEric Joyner 	status = ice_check_reset(hw);
264171d10453SEric Joyner 	IFLIB_CTX_LOCK(sc);
264271d10453SEric Joyner 	if (status) {
264371d10453SEric Joyner 		device_printf(dev, "Device never came out of reset, err %s\n",
264471d10453SEric Joyner 			      ice_status_str(status));
264571d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
264671d10453SEric Joyner 		return;
264771d10453SEric Joyner 	}
264871d10453SEric Joyner 
264971d10453SEric Joyner 	/* We're done with the reset, so we can rebuild driver state */
265071d10453SEric Joyner 	sc->hw.reset_ongoing = false;
265171d10453SEric Joyner 	ice_rebuild(sc);
265271d10453SEric Joyner 
265371d10453SEric Joyner 	/* In the unlikely event that a PF reset request occurs at the same
265471d10453SEric Joyner 	 * time as a global reset, clear the request now. This avoids
265571d10453SEric Joyner 	 * resetting a second time right after we reset due to a global event.
265671d10453SEric Joyner 	 */
265771d10453SEric Joyner 	if (ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
265871d10453SEric Joyner 		device_printf(dev, "Ignoring PFR request that occurred while a reset was ongoing\n");
265971d10453SEric Joyner }
266071d10453SEric Joyner 
266171d10453SEric Joyner /**
266271d10453SEric Joyner  * ice_handle_pf_reset_request - Initiate PF reset requested by software
266371d10453SEric Joyner  * @sc: The device private softc
266471d10453SEric Joyner  *
266571d10453SEric Joyner  * Initiate a PF reset requested by software. We handle this in the admin task
266671d10453SEric Joyner  * so that only one thread actually handles driver preparation and cleanup,
266771d10453SEric Joyner  * rather than having multiple threads possibly attempt to run this code
266871d10453SEric Joyner  * simultaneously.
266971d10453SEric Joyner  *
267071d10453SEric Joyner  * @pre assumes the iflib context lock is held and will unlock it while
267171d10453SEric Joyner  * waiting for the PF reset to complete.
267271d10453SEric Joyner  */
267371d10453SEric Joyner static void
267471d10453SEric Joyner ice_handle_pf_reset_request(struct ice_softc *sc)
267571d10453SEric Joyner {
267671d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
267771d10453SEric Joyner 	enum ice_status status;
267871d10453SEric Joyner 
267971d10453SEric Joyner 	/* Check for PF reset requests */
268071d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
268171d10453SEric Joyner 		return;
268271d10453SEric Joyner 
268371d10453SEric Joyner 	/* Make sure we're prepared for reset */
268471d10453SEric Joyner 	ice_prepare_for_reset(sc);
268571d10453SEric Joyner 
268671d10453SEric Joyner 	/*
268771d10453SEric Joyner 	 * Release the iflib context lock and wait for the device to finish
268871d10453SEric Joyner 	 * resetting.
268971d10453SEric Joyner 	 */
269071d10453SEric Joyner 	IFLIB_CTX_UNLOCK(sc);
269171d10453SEric Joyner 	status = ice_reset(hw, ICE_RESET_PFR);
269271d10453SEric Joyner 	IFLIB_CTX_LOCK(sc);
269371d10453SEric Joyner 	if (status) {
269471d10453SEric Joyner 		device_printf(sc->dev, "device PF reset failed, err %s\n",
269571d10453SEric Joyner 			      ice_status_str(status));
269671d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
269771d10453SEric Joyner 		return;
269871d10453SEric Joyner 	}
269971d10453SEric Joyner 
270071d10453SEric Joyner 	sc->soft_stats.pfr_count++;
270171d10453SEric Joyner 	ice_rebuild(sc);
270271d10453SEric Joyner }
270371d10453SEric Joyner 
270471d10453SEric Joyner /**
270571d10453SEric Joyner  * ice_init_device_features - Init device driver features
270671d10453SEric Joyner  * @sc: driver softc structure
270771d10453SEric Joyner  *
270871d10453SEric Joyner  * @pre assumes that the function capabilities bits have been set up by
270971d10453SEric Joyner  * ice_init_hw().
271071d10453SEric Joyner  */
271171d10453SEric Joyner static void
271271d10453SEric Joyner ice_init_device_features(struct ice_softc *sc)
271371d10453SEric Joyner {
27147d7af7f8SEric Joyner 	/* Set capabilities that all devices support */
271571d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
271671d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_RSS, sc->feat_cap);
27178a13362dSEric Joyner 	ice_set_bit(ICE_FEATURE_RDMA, sc->feat_cap);
271871d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_LENIENT_LINK_MODE, sc->feat_cap);
27199cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_1, sc->feat_cap);
27209cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_2, sc->feat_cap);
27219cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
272256429daeSEric Joyner 	ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
272356429daeSEric Joyner 	ice_set_bit(ICE_FEATURE_HAS_PBA, sc->feat_cap);
2724*8923de59SPiotr Kubaj 	ice_set_bit(ICE_FEATURE_DCB, sc->feat_cap);
2725*8923de59SPiotr Kubaj 	ice_set_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
272671d10453SEric Joyner 
272771d10453SEric Joyner 	/* Disable features due to hardware limitations... */
272871d10453SEric Joyner 	if (!sc->hw.func_caps.common_cap.rss_table_size)
272971d10453SEric Joyner 		ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
27308a13362dSEric Joyner 	if (!sc->hw.func_caps.common_cap.iwarp || !ice_enable_irdma)
27318a13362dSEric Joyner 		ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
2732*8923de59SPiotr Kubaj 	if (!sc->hw.func_caps.common_cap.dcb)
2733*8923de59SPiotr Kubaj 		ice_clear_bit(ICE_FEATURE_DCB, sc->feat_cap);
27349cf1841cSEric Joyner 	/* Disable features due to firmware limitations... */
27359cf1841cSEric Joyner 	if (!ice_is_fw_health_report_supported(&sc->hw))
27369cf1841cSEric Joyner 		ice_clear_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
273756429daeSEric Joyner 	if (!ice_fwlog_supported(&sc->hw))
273856429daeSEric Joyner 		ice_clear_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
273956429daeSEric Joyner 	if (sc->hw.fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
274056429daeSEric Joyner 		if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_FW_LOGGING))
274156429daeSEric Joyner 			ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_en);
274256429daeSEric Joyner 		else
274356429daeSEric Joyner 			ice_fwlog_unregister(&sc->hw);
274456429daeSEric Joyner 	}
274571d10453SEric Joyner 
274671d10453SEric Joyner 	/* Disable capabilities not supported by the OS */
274771d10453SEric Joyner 	ice_disable_unsupported_features(sc->feat_cap);
274871d10453SEric Joyner 
274971d10453SEric Joyner 	/* RSS is always enabled for iflib */
275071d10453SEric Joyner 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RSS))
275171d10453SEric Joyner 		ice_set_bit(ICE_FEATURE_RSS, sc->feat_en);
2752*8923de59SPiotr Kubaj 
2753*8923de59SPiotr Kubaj 	/* Disable features based on sysctl settings */
2754*8923de59SPiotr Kubaj 	if (!ice_tx_balance_en)
2755*8923de59SPiotr Kubaj 		ice_clear_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
275671d10453SEric Joyner }
275771d10453SEric Joyner 
275871d10453SEric Joyner /**
275971d10453SEric Joyner  * ice_if_multi_set - Callback to update Multicast filters in HW
276071d10453SEric Joyner  * @ctx: iflib ctx structure
276171d10453SEric Joyner  *
276271d10453SEric Joyner  * Called by iflib in response to SIOCDELMULTI and SIOCADDMULTI. Must search
276371d10453SEric Joyner  * the if_multiaddrs list and determine which filters have been added or
276471d10453SEric Joyner  * removed from the list, and update HW programming to reflect the new list.
276571d10453SEric Joyner  *
276671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
276771d10453SEric Joyner  */
276871d10453SEric Joyner static void
276971d10453SEric Joyner ice_if_multi_set(if_ctx_t ctx)
277071d10453SEric Joyner {
277171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
277271d10453SEric Joyner 	int err;
277371d10453SEric Joyner 
277471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
277571d10453SEric Joyner 
277671d10453SEric Joyner 	/* Do not handle multicast configuration in recovery mode */
277771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
277871d10453SEric Joyner 		return;
277971d10453SEric Joyner 
278071d10453SEric Joyner 	err = ice_sync_multicast_filters(sc);
278171d10453SEric Joyner 	if (err) {
278271d10453SEric Joyner 		device_printf(sc->dev,
278371d10453SEric Joyner 			      "Failed to synchronize multicast filter list: %s\n",
278471d10453SEric Joyner 			      ice_err_str(err));
278571d10453SEric Joyner 		return;
278671d10453SEric Joyner 	}
278771d10453SEric Joyner }
278871d10453SEric Joyner 
278971d10453SEric Joyner /**
279071d10453SEric Joyner  * ice_if_vlan_register - Register a VLAN with the hardware
279171d10453SEric Joyner  * @ctx: iflib ctx pointer
279271d10453SEric Joyner  * @vtag: VLAN to add
279371d10453SEric Joyner  *
279471d10453SEric Joyner  * Programs the main PF VSI with a hardware filter for the given VLAN.
279571d10453SEric Joyner  *
279671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
279771d10453SEric Joyner  */
279871d10453SEric Joyner static void
279971d10453SEric Joyner ice_if_vlan_register(if_ctx_t ctx, u16 vtag)
280071d10453SEric Joyner {
280171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
280271d10453SEric Joyner 	enum ice_status status;
280371d10453SEric Joyner 
280471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
280571d10453SEric Joyner 
280671d10453SEric Joyner 	/* Do not handle VLAN configuration in recovery mode */
280771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
280871d10453SEric Joyner 		return;
280971d10453SEric Joyner 
281071d10453SEric Joyner 	status = ice_add_vlan_hw_filter(&sc->pf_vsi, vtag);
281171d10453SEric Joyner 	if (status) {
281271d10453SEric Joyner 		device_printf(sc->dev,
281371d10453SEric Joyner 			      "Failure adding VLAN %d to main VSI, err %s aq_err %s\n",
281471d10453SEric Joyner 			      vtag, ice_status_str(status),
281571d10453SEric Joyner 			      ice_aq_str(sc->hw.adminq.sq_last_status));
281671d10453SEric Joyner 	}
281771d10453SEric Joyner }
281871d10453SEric Joyner 
281971d10453SEric Joyner /**
282071d10453SEric Joyner  * ice_if_vlan_unregister - Remove a VLAN filter from the hardware
282171d10453SEric Joyner  * @ctx: iflib ctx pointer
282271d10453SEric Joyner  * @vtag: VLAN to add
282371d10453SEric Joyner  *
282471d10453SEric Joyner  * Removes the previously programmed VLAN filter from the main PF VSI.
282571d10453SEric Joyner  *
282671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
282771d10453SEric Joyner  */
282871d10453SEric Joyner static void
282971d10453SEric Joyner ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
283071d10453SEric Joyner {
283171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
283271d10453SEric Joyner 	enum ice_status status;
283371d10453SEric Joyner 
283471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
283571d10453SEric Joyner 
283671d10453SEric Joyner 	/* Do not handle VLAN configuration in recovery mode */
283771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
283871d10453SEric Joyner 		return;
283971d10453SEric Joyner 
284071d10453SEric Joyner 	status = ice_remove_vlan_hw_filter(&sc->pf_vsi, vtag);
284171d10453SEric Joyner 	if (status) {
284271d10453SEric Joyner 		device_printf(sc->dev,
284371d10453SEric Joyner 			      "Failure removing VLAN %d from main VSI, err %s aq_err %s\n",
284471d10453SEric Joyner 			      vtag, ice_status_str(status),
284571d10453SEric Joyner 			      ice_aq_str(sc->hw.adminq.sq_last_status));
284671d10453SEric Joyner 	}
284771d10453SEric Joyner }
284871d10453SEric Joyner 
284971d10453SEric Joyner /**
285071d10453SEric Joyner  * ice_if_stop - Stop the device
285171d10453SEric Joyner  * @ctx: iflib context structure
285271d10453SEric Joyner  *
285371d10453SEric Joyner  * Called by iflib to stop the device and bring it down. (i.e. ifconfig ice0
285471d10453SEric Joyner  * down)
285571d10453SEric Joyner  *
285671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
285771d10453SEric Joyner  */
285871d10453SEric Joyner static void
285971d10453SEric Joyner ice_if_stop(if_ctx_t ctx)
286071d10453SEric Joyner {
286171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
286271d10453SEric Joyner 
286371d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
286471d10453SEric Joyner 
286571d10453SEric Joyner 	/*
286671d10453SEric Joyner 	 * The iflib core may call IFDI_STOP prior to the first call to
286771d10453SEric Joyner 	 * IFDI_INIT. This will cause us to attempt to remove MAC filters we
286871d10453SEric Joyner 	 * don't have, and disable Tx queues which aren't yet configured.
286971d10453SEric Joyner 	 * Although it is likely these extra operations are harmless, they do
287071d10453SEric Joyner 	 * cause spurious warning messages to be displayed, which may confuse
287171d10453SEric Joyner 	 * users.
287271d10453SEric Joyner 	 *
287371d10453SEric Joyner 	 * To avoid these messages, we use a state bit indicating if we've
287471d10453SEric Joyner 	 * been initialized. It will be set when ice_if_init is called, and
287571d10453SEric Joyner 	 * cleared here in ice_if_stop.
287671d10453SEric Joyner 	 */
287771d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
287871d10453SEric Joyner 		return;
287971d10453SEric Joyner 
288071d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
288171d10453SEric Joyner 		device_printf(sc->dev, "request to stop interface cannot be completed as the device failed to reset\n");
288271d10453SEric Joyner 		return;
288371d10453SEric Joyner 	}
288471d10453SEric Joyner 
288571d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
288671d10453SEric Joyner 		device_printf(sc->dev, "request to stop interface while device is prepared for impending reset\n");
288771d10453SEric Joyner 		return;
288871d10453SEric Joyner 	}
288971d10453SEric Joyner 
28908a13362dSEric Joyner 	ice_rdma_pf_stop(sc);
28918a13362dSEric Joyner 
289271d10453SEric Joyner 	/* Remove the MAC filters, stop Tx, and stop Rx. We don't check the
289371d10453SEric Joyner 	 * return of these functions because there's nothing we can really do
289471d10453SEric Joyner 	 * if they fail, and the functions already print error messages.
289571d10453SEric Joyner 	 * Just try to shut down as much as we can.
289671d10453SEric Joyner 	 */
289771d10453SEric Joyner 	ice_rm_pf_default_mac_filters(sc);
289871d10453SEric Joyner 
289971d10453SEric Joyner 	/* Dissociate the Tx and Rx queues from the interrupts */
290071d10453SEric Joyner 	ice_flush_txq_interrupts(&sc->pf_vsi);
290171d10453SEric Joyner 	ice_flush_rxq_interrupts(&sc->pf_vsi);
290271d10453SEric Joyner 
290371d10453SEric Joyner 	/* Disable the Tx and Rx queues */
290471d10453SEric Joyner 	ice_vsi_disable_tx(&sc->pf_vsi);
290571d10453SEric Joyner 	ice_control_rx_queues(&sc->pf_vsi, false);
290671d10453SEric Joyner }
290771d10453SEric Joyner 
290871d10453SEric Joyner /**
290971d10453SEric Joyner  * ice_if_get_counter - Get current value of an ifnet statistic
291071d10453SEric Joyner  * @ctx: iflib context pointer
291171d10453SEric Joyner  * @counter: ifnet counter to read
291271d10453SEric Joyner  *
291371d10453SEric Joyner  * Reads the current value of an ifnet counter for the device.
291471d10453SEric Joyner  *
291571d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
291671d10453SEric Joyner  */
291771d10453SEric Joyner static uint64_t
291871d10453SEric Joyner ice_if_get_counter(if_ctx_t ctx, ift_counter counter)
291971d10453SEric Joyner {
292071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
292171d10453SEric Joyner 
292271d10453SEric Joyner 	/* Return the counter for the main PF VSI */
292371d10453SEric Joyner 	return ice_get_ifnet_counter(&sc->pf_vsi, counter);
292471d10453SEric Joyner }
292571d10453SEric Joyner 
292671d10453SEric Joyner /**
292771d10453SEric Joyner  * ice_request_stack_reinit - Request that iflib re-initialize
292871d10453SEric Joyner  * @sc: the device private softc
292971d10453SEric Joyner  *
293071d10453SEric Joyner  * Request that the device be brought down and up, to re-initialize. For
293171d10453SEric Joyner  * example, this may be called when a device reset occurs, or when Tx and Rx
293271d10453SEric Joyner  * queues need to be re-initialized.
293371d10453SEric Joyner  *
293471d10453SEric Joyner  * This is required because the iflib state is outside the driver, and must be
293571d10453SEric Joyner  * re-initialized if we need to resart Tx and Rx queues.
293671d10453SEric Joyner  */
293771d10453SEric Joyner void
293871d10453SEric Joyner ice_request_stack_reinit(struct ice_softc *sc)
293971d10453SEric Joyner {
294071d10453SEric Joyner 	if (CTX_ACTIVE(sc->ctx)) {
294171d10453SEric Joyner 		iflib_request_reset(sc->ctx);
294271d10453SEric Joyner 		iflib_admin_intr_deferred(sc->ctx);
294371d10453SEric Joyner 	}
294471d10453SEric Joyner }
294571d10453SEric Joyner 
294671d10453SEric Joyner /**
294771d10453SEric Joyner  * ice_driver_is_detaching - Check if the driver is detaching/unloading
294871d10453SEric Joyner  * @sc: device private softc
294971d10453SEric Joyner  *
295071d10453SEric Joyner  * Returns true if the driver is detaching, false otherwise.
295171d10453SEric Joyner  *
295271d10453SEric Joyner  * @remark on newer kernels, take advantage of iflib_in_detach in order to
295371d10453SEric Joyner  * report detachment correctly as early as possible.
295471d10453SEric Joyner  *
295571d10453SEric Joyner  * @remark this function is used by various code paths that want to avoid
295671d10453SEric Joyner  * running if the driver is about to be removed. This includes sysctls and
295771d10453SEric Joyner  * other driver access points. Note that it does not fully resolve
295871d10453SEric Joyner  * detach-based race conditions as it is possible for a thread to race with
295971d10453SEric Joyner  * iflib_in_detach.
296071d10453SEric Joyner  */
296171d10453SEric Joyner bool
296271d10453SEric Joyner ice_driver_is_detaching(struct ice_softc *sc)
296371d10453SEric Joyner {
296471d10453SEric Joyner 	return (ice_test_state(&sc->state, ICE_STATE_DETACHING) ||
296571d10453SEric Joyner 		iflib_in_detach(sc->ctx));
296671d10453SEric Joyner }
296771d10453SEric Joyner 
296871d10453SEric Joyner /**
296971d10453SEric Joyner  * ice_if_priv_ioctl - Device private ioctl handler
297071d10453SEric Joyner  * @ctx: iflib context pointer
297171d10453SEric Joyner  * @command: The ioctl command issued
297271d10453SEric Joyner  * @data: ioctl specific data
297371d10453SEric Joyner  *
297471d10453SEric Joyner  * iflib callback for handling custom driver specific ioctls.
297571d10453SEric Joyner  *
297671d10453SEric Joyner  * @pre Assumes that the iflib context lock is held.
297771d10453SEric Joyner  */
297871d10453SEric Joyner static int
297971d10453SEric Joyner ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
298071d10453SEric Joyner {
298171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
298271d10453SEric Joyner 	struct ifdrv *ifd;
298371d10453SEric Joyner 	device_t dev = sc->dev;
298471d10453SEric Joyner 
298571d10453SEric Joyner 	if (data == NULL)
298671d10453SEric Joyner 		return (EINVAL);
298771d10453SEric Joyner 
298871d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
298971d10453SEric Joyner 
299071d10453SEric Joyner 	/* Make sure the command type is valid */
299171d10453SEric Joyner 	switch (command) {
299271d10453SEric Joyner 	case SIOCSDRVSPEC:
299371d10453SEric Joyner 	case SIOCGDRVSPEC:
299471d10453SEric Joyner 		/* Accepted commands */
299571d10453SEric Joyner 		break;
299671d10453SEric Joyner 	case SIOCGPRIVATE_0:
299771d10453SEric Joyner 		/*
299871d10453SEric Joyner 		 * Although we do not support this ioctl command, it's
299971d10453SEric Joyner 		 * expected that iflib will forward it to the IFDI_PRIV_IOCTL
300071d10453SEric Joyner 		 * handler. Do not print a message in this case
300171d10453SEric Joyner 		 */
300271d10453SEric Joyner 		return (ENOTSUP);
300371d10453SEric Joyner 	default:
300471d10453SEric Joyner 		/*
300571d10453SEric Joyner 		 * If we get a different command for this function, it's
300671d10453SEric Joyner 		 * definitely unexpected, so log a message indicating what
300771d10453SEric Joyner 		 * command we got for debugging purposes.
300871d10453SEric Joyner 		 */
300971d10453SEric Joyner 		device_printf(dev, "%s: unexpected ioctl command %08lx\n",
301071d10453SEric Joyner 			      __func__, command);
301171d10453SEric Joyner 		return (EINVAL);
301271d10453SEric Joyner 	}
301371d10453SEric Joyner 
301471d10453SEric Joyner 	ifd = (struct ifdrv *)data;
301571d10453SEric Joyner 
301671d10453SEric Joyner 	switch (ifd->ifd_cmd) {
301771d10453SEric Joyner 	case ICE_NVM_ACCESS:
301871d10453SEric Joyner 		return ice_handle_nvm_access_ioctl(sc, ifd);
3019*8923de59SPiotr Kubaj 	case ICE_DEBUG_DUMP:
3020*8923de59SPiotr Kubaj 		return ice_handle_debug_dump_ioctl(sc, ifd);
302171d10453SEric Joyner 	default:
302271d10453SEric Joyner 		return EINVAL;
302371d10453SEric Joyner 	}
302471d10453SEric Joyner }
302571d10453SEric Joyner 
302671d10453SEric Joyner /**
302771d10453SEric Joyner  * ice_if_i2c_req - I2C request handler for iflib
302871d10453SEric Joyner  * @ctx: iflib context pointer
302971d10453SEric Joyner  * @req: The I2C parameters to use
303071d10453SEric Joyner  *
303171d10453SEric Joyner  * Read from the port's I2C eeprom using the parameters from the ioctl.
303271d10453SEric Joyner  *
303371d10453SEric Joyner  * @remark The iflib-only part is pretty simple.
303471d10453SEric Joyner  */
303571d10453SEric Joyner static int
303671d10453SEric Joyner ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
303771d10453SEric Joyner {
303871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
303971d10453SEric Joyner 
304071d10453SEric Joyner 	return ice_handle_i2c_req(sc, req);
304171d10453SEric Joyner }
304271d10453SEric Joyner 
30437d7af7f8SEric Joyner /**
30447d7af7f8SEric Joyner  * ice_if_suspend - PCI device suspend handler for iflib
30457d7af7f8SEric Joyner  * @ctx: iflib context pointer
30467d7af7f8SEric Joyner  *
30477d7af7f8SEric Joyner  * Deinitializes the driver and clears HW resources in preparation for
30487d7af7f8SEric Joyner  * suspend or an FLR.
30497d7af7f8SEric Joyner  *
30507d7af7f8SEric Joyner  * @returns 0; this return value is ignored
30517d7af7f8SEric Joyner  */
30527d7af7f8SEric Joyner static int
30537d7af7f8SEric Joyner ice_if_suspend(if_ctx_t ctx)
30547d7af7f8SEric Joyner {
30557d7af7f8SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
30567d7af7f8SEric Joyner 
30577d7af7f8SEric Joyner 	/* At least a PFR is always going to happen after this;
30587d7af7f8SEric Joyner 	 * either via FLR or during the D3->D0 transition.
30597d7af7f8SEric Joyner 	 */
30607d7af7f8SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
30617d7af7f8SEric Joyner 
30627d7af7f8SEric Joyner 	ice_prepare_for_reset(sc);
30637d7af7f8SEric Joyner 
30647d7af7f8SEric Joyner 	return (0);
30657d7af7f8SEric Joyner }
30667d7af7f8SEric Joyner 
30677d7af7f8SEric Joyner /**
30687d7af7f8SEric Joyner  * ice_if_resume - PCI device resume handler for iflib
30697d7af7f8SEric Joyner  * @ctx: iflib context pointer
30707d7af7f8SEric Joyner  *
30717d7af7f8SEric Joyner  * Reinitializes the driver and the HW after PCI resume or after
30727d7af7f8SEric Joyner  * an FLR. An init is performed by iflib after this function is finished.
30737d7af7f8SEric Joyner  *
30747d7af7f8SEric Joyner  * @returns 0; this return value is ignored
30757d7af7f8SEric Joyner  */
30767d7af7f8SEric Joyner static int
30777d7af7f8SEric Joyner ice_if_resume(if_ctx_t ctx)
30787d7af7f8SEric Joyner {
30797d7af7f8SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
30807d7af7f8SEric Joyner 
30817d7af7f8SEric Joyner 	ice_rebuild(sc);
30827d7af7f8SEric Joyner 
30837d7af7f8SEric Joyner 	return (0);
30847d7af7f8SEric Joyner }
30857d7af7f8SEric Joyner 
3086