xref: /freebsd/sys/dev/ice/if_ice_iflib.c (revision 03e1f25dcb200edbd6553ffc2748e7410b9ff264)
171d10453SEric Joyner /* SPDX-License-Identifier: BSD-3-Clause */
2015f8cc5SEric Joyner /*  Copyright (c) 2024, 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 
3271d10453SEric Joyner /**
3371d10453SEric Joyner  * @file if_ice_iflib.c
3471d10453SEric Joyner  * @brief iflib driver implementation
3571d10453SEric Joyner  *
3671d10453SEric Joyner  * Contains the main entry point for the iflib driver implementation. It
3771d10453SEric Joyner  * implements the various ifdi driver methods, and sets up the module and
3871d10453SEric Joyner  * driver values to load an iflib driver.
3971d10453SEric Joyner  */
4071d10453SEric Joyner 
4171d10453SEric Joyner #include "ice_iflib.h"
4271d10453SEric Joyner #include "ice_drv_info.h"
4371d10453SEric Joyner #include "ice_switch.h"
4471d10453SEric Joyner #include "ice_sched.h"
4571d10453SEric Joyner 
4671d10453SEric Joyner #include <sys/module.h>
4771d10453SEric Joyner #include <sys/sockio.h>
4871d10453SEric Joyner #include <sys/smp.h>
4971d10453SEric Joyner #include <dev/pci/pcivar.h>
5071d10453SEric Joyner #include <dev/pci/pcireg.h>
5171d10453SEric Joyner 
5271d10453SEric Joyner /*
5371d10453SEric Joyner  * Device method prototypes
5471d10453SEric Joyner  */
5571d10453SEric Joyner 
5671d10453SEric Joyner static void *ice_register(device_t);
5771d10453SEric Joyner static int  ice_if_attach_pre(if_ctx_t);
5871d10453SEric Joyner static int  ice_attach_pre_recovery_mode(struct ice_softc *sc);
5971d10453SEric Joyner static int  ice_if_attach_post(if_ctx_t);
6071d10453SEric Joyner static void ice_attach_post_recovery_mode(struct ice_softc *sc);
6171d10453SEric Joyner static int  ice_if_detach(if_ctx_t);
6271d10453SEric Joyner static int  ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
6371d10453SEric Joyner static int  ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
6471d10453SEric Joyner static int ice_if_msix_intr_assign(if_ctx_t ctx, int msix);
6571d10453SEric Joyner static void ice_if_queues_free(if_ctx_t ctx);
6671d10453SEric Joyner static int ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
6771d10453SEric Joyner static void ice_if_intr_enable(if_ctx_t ctx);
6871d10453SEric Joyner static void ice_if_intr_disable(if_ctx_t ctx);
6971d10453SEric Joyner static int ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
7071d10453SEric Joyner static int ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
7171d10453SEric Joyner static int ice_if_promisc_set(if_ctx_t ctx, int flags);
7271d10453SEric Joyner static void ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
7371d10453SEric Joyner static int ice_if_media_change(if_ctx_t ctx);
7471d10453SEric Joyner static void ice_if_init(if_ctx_t ctx);
7571d10453SEric Joyner static void ice_if_timer(if_ctx_t ctx, uint16_t qid);
7671d10453SEric Joyner static void ice_if_update_admin_status(if_ctx_t ctx);
7771d10453SEric Joyner static void ice_if_multi_set(if_ctx_t ctx);
7871d10453SEric Joyner static void ice_if_vlan_register(if_ctx_t ctx, u16 vtag);
7971d10453SEric Joyner static void ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
8071d10453SEric Joyner static void ice_if_stop(if_ctx_t ctx);
8171d10453SEric Joyner static uint64_t ice_if_get_counter(if_ctx_t ctx, ift_counter counter);
8271d10453SEric Joyner static int ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
8371d10453SEric Joyner static int ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
847d7af7f8SEric Joyner static int ice_if_suspend(if_ctx_t ctx);
857d7af7f8SEric Joyner static int ice_if_resume(if_ctx_t ctx);
869c30461dSEric Joyner static bool ice_if_needs_restart(if_ctx_t ctx, enum iflib_restart_event event);
87f377a0c7SEric Joyner static void ice_init_link(struct ice_softc *sc);
889e54973fSEric Joyner static int ice_setup_mirror_vsi(struct ice_mirr_if *mif);
899e54973fSEric Joyner static int ice_wire_mirror_intrs(struct ice_mirr_if *mif);
909e54973fSEric Joyner static void ice_free_irqvs_subif(struct ice_mirr_if *mif);
919e54973fSEric Joyner static void *ice_subif_register(device_t);
929e54973fSEric Joyner static void ice_subif_setup_scctx(struct ice_mirr_if *mif);
939e54973fSEric Joyner static int ice_subif_rebuild(struct ice_softc *sc);
949e54973fSEric Joyner static int ice_subif_rebuild_vsi_qmap(struct ice_softc *sc);
959e54973fSEric Joyner 
969e54973fSEric Joyner /* Iflib API */
979e54973fSEric Joyner static int ice_subif_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
989e54973fSEric Joyner     uint64_t *paddrs, int ntxqs, int ntxqsets);
999e54973fSEric Joyner static int ice_subif_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
1009e54973fSEric Joyner     uint64_t *paddrs, int nrxqs, int nrxqsets);
1019e54973fSEric Joyner static int ice_subif_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
1029e54973fSEric Joyner static int ice_subif_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
1039e54973fSEric Joyner static void ice_subif_if_intr_enable(if_ctx_t ctx);
1049e54973fSEric Joyner static int ice_subif_if_msix_intr_assign(if_ctx_t ctx, int msix);
1059e54973fSEric Joyner static void ice_subif_if_init(if_ctx_t ctx);
1069e54973fSEric Joyner static void ice_subif_if_stop(if_ctx_t ctx);
1079e54973fSEric Joyner static void ice_subif_if_queues_free(if_ctx_t ctx);
1089e54973fSEric Joyner static int ice_subif_if_attach_pre(if_ctx_t);
1099e54973fSEric Joyner static int ice_subif_if_attach_post(if_ctx_t);
1109e54973fSEric Joyner static void ice_subif_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
1119e54973fSEric Joyner static int ice_subif_if_promisc_set(if_ctx_t ctx, int flags);
11271d10453SEric Joyner 
11371d10453SEric Joyner static int ice_msix_que(void *arg);
11471d10453SEric Joyner static int ice_msix_admin(void *arg);
11571d10453SEric Joyner 
11671d10453SEric Joyner /*
11771d10453SEric Joyner  * Helper function prototypes
11871d10453SEric Joyner  */
11971d10453SEric Joyner static int ice_pci_mapping(struct ice_softc *sc);
12071d10453SEric Joyner static void ice_free_pci_mapping(struct ice_softc *sc);
12171d10453SEric Joyner static void ice_update_link_status(struct ice_softc *sc, bool update_media);
12271d10453SEric Joyner static void ice_init_device_features(struct ice_softc *sc);
12371d10453SEric Joyner static void ice_init_tx_tracking(struct ice_vsi *vsi);
12471d10453SEric Joyner static void ice_handle_reset_event(struct ice_softc *sc);
12571d10453SEric Joyner static void ice_handle_pf_reset_request(struct ice_softc *sc);
12671d10453SEric Joyner static void ice_prepare_for_reset(struct ice_softc *sc);
12771d10453SEric Joyner static int ice_rebuild_pf_vsi_qmap(struct ice_softc *sc);
12871d10453SEric Joyner static void ice_rebuild(struct ice_softc *sc);
12971d10453SEric Joyner static void ice_rebuild_recovery_mode(struct ice_softc *sc);
13071d10453SEric Joyner static void ice_free_irqvs(struct ice_softc *sc);
13171d10453SEric Joyner static void ice_update_rx_mbuf_sz(struct ice_softc *sc);
13271d10453SEric Joyner static void ice_poll_for_media_avail(struct ice_softc *sc);
13371d10453SEric Joyner static void ice_setup_scctx(struct ice_softc *sc);
13471d10453SEric Joyner static int ice_allocate_msix(struct ice_softc *sc);
13571d10453SEric Joyner static void ice_admin_timer(void *arg);
13671d10453SEric Joyner static void ice_transition_recovery_mode(struct ice_softc *sc);
13771d10453SEric Joyner static void ice_transition_safe_mode(struct ice_softc *sc);
1389c30461dSEric Joyner static void ice_set_default_promisc_mask(ice_bitmap_t *promisc_mask);
13971d10453SEric Joyner 
14071d10453SEric Joyner /*
14171d10453SEric Joyner  * Device Interface Declaration
14271d10453SEric Joyner  */
14371d10453SEric Joyner 
14471d10453SEric Joyner /**
14571d10453SEric Joyner  * @var ice_methods
14671d10453SEric Joyner  * @brief ice driver method entry points
14771d10453SEric Joyner  *
14871d10453SEric Joyner  * List of device methods implementing the generic device interface used by
14971d10453SEric Joyner  * the device stack to interact with the ice driver. Since this is an iflib
15071d10453SEric Joyner  * driver, most of the methods point to the generic iflib implementation.
15171d10453SEric Joyner  */
15271d10453SEric Joyner static device_method_t ice_methods[] = {
15371d10453SEric Joyner 	/* Device interface */
15471d10453SEric Joyner 	DEVMETHOD(device_register, ice_register),
15571d10453SEric Joyner 	DEVMETHOD(device_probe,    iflib_device_probe_vendor),
15671d10453SEric Joyner 	DEVMETHOD(device_attach,   iflib_device_attach),
15771d10453SEric Joyner 	DEVMETHOD(device_detach,   iflib_device_detach),
15871d10453SEric Joyner 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
15971d10453SEric Joyner 	DEVMETHOD(device_suspend,  iflib_device_suspend),
16071d10453SEric Joyner 	DEVMETHOD(device_resume,   iflib_device_resume),
16171d10453SEric Joyner 	DEVMETHOD_END
16271d10453SEric Joyner };
16371d10453SEric Joyner 
16471d10453SEric Joyner /**
16571d10453SEric Joyner  * @var ice_iflib_methods
16671d10453SEric Joyner  * @brief iflib method entry points
16771d10453SEric Joyner  *
16871d10453SEric Joyner  * List of device methods used by the iflib stack to interact with this
16971d10453SEric Joyner  * driver. These are the real main entry points used to interact with this
17071d10453SEric Joyner  * driver.
17171d10453SEric Joyner  */
17271d10453SEric Joyner static device_method_t ice_iflib_methods[] = {
17371d10453SEric Joyner 	DEVMETHOD(ifdi_attach_pre, ice_if_attach_pre),
17471d10453SEric Joyner 	DEVMETHOD(ifdi_attach_post, ice_if_attach_post),
17571d10453SEric Joyner 	DEVMETHOD(ifdi_detach, ice_if_detach),
17671d10453SEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ice_if_tx_queues_alloc),
17771d10453SEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ice_if_rx_queues_alloc),
17871d10453SEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ice_if_msix_intr_assign),
17971d10453SEric Joyner 	DEVMETHOD(ifdi_queues_free, ice_if_queues_free),
18071d10453SEric Joyner 	DEVMETHOD(ifdi_mtu_set, ice_if_mtu_set),
18171d10453SEric Joyner 	DEVMETHOD(ifdi_intr_enable, ice_if_intr_enable),
18271d10453SEric Joyner 	DEVMETHOD(ifdi_intr_disable, ice_if_intr_disable),
18371d10453SEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ice_if_rx_queue_intr_enable),
18471d10453SEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ice_if_tx_queue_intr_enable),
18571d10453SEric Joyner 	DEVMETHOD(ifdi_promisc_set, ice_if_promisc_set),
18671d10453SEric Joyner 	DEVMETHOD(ifdi_media_status, ice_if_media_status),
18771d10453SEric Joyner 	DEVMETHOD(ifdi_media_change, ice_if_media_change),
18871d10453SEric Joyner 	DEVMETHOD(ifdi_init, ice_if_init),
18971d10453SEric Joyner 	DEVMETHOD(ifdi_stop, ice_if_stop),
19071d10453SEric Joyner 	DEVMETHOD(ifdi_timer, ice_if_timer),
19171d10453SEric Joyner 	DEVMETHOD(ifdi_update_admin_status, ice_if_update_admin_status),
19271d10453SEric Joyner 	DEVMETHOD(ifdi_multi_set, ice_if_multi_set),
19371d10453SEric Joyner 	DEVMETHOD(ifdi_vlan_register, ice_if_vlan_register),
19471d10453SEric Joyner 	DEVMETHOD(ifdi_vlan_unregister, ice_if_vlan_unregister),
19571d10453SEric Joyner 	DEVMETHOD(ifdi_get_counter, ice_if_get_counter),
19671d10453SEric Joyner 	DEVMETHOD(ifdi_priv_ioctl, ice_if_priv_ioctl),
19771d10453SEric Joyner 	DEVMETHOD(ifdi_i2c_req, ice_if_i2c_req),
1987d7af7f8SEric Joyner 	DEVMETHOD(ifdi_suspend, ice_if_suspend),
1997d7af7f8SEric Joyner 	DEVMETHOD(ifdi_resume, ice_if_resume),
20014a14e36SKevin Bowling 	DEVMETHOD(ifdi_needs_restart, ice_if_needs_restart),
20171d10453SEric Joyner 	DEVMETHOD_END
20271d10453SEric Joyner };
20371d10453SEric Joyner 
20471d10453SEric Joyner /**
20571d10453SEric Joyner  * @var ice_driver
20671d10453SEric Joyner  * @brief driver structure for the generic device stack
20771d10453SEric Joyner  *
20871d10453SEric Joyner  * driver_t definition used to setup the generic device methods.
20971d10453SEric Joyner  */
21071d10453SEric Joyner static driver_t ice_driver = {
21171d10453SEric Joyner 	.name = "ice",
21271d10453SEric Joyner 	.methods = ice_methods,
21371d10453SEric Joyner 	.size = sizeof(struct ice_softc),
21471d10453SEric Joyner };
21571d10453SEric Joyner 
21671d10453SEric Joyner /**
21771d10453SEric Joyner  * @var ice_iflib_driver
21871d10453SEric Joyner  * @brief driver structure for the iflib stack
21971d10453SEric Joyner  *
22071d10453SEric Joyner  * driver_t definition used to setup the iflib device methods.
22171d10453SEric Joyner  */
22271d10453SEric Joyner static driver_t ice_iflib_driver = {
22371d10453SEric Joyner 	.name = "ice",
22471d10453SEric Joyner 	.methods = ice_iflib_methods,
22571d10453SEric Joyner 	.size = sizeof(struct ice_softc),
22671d10453SEric Joyner };
22771d10453SEric Joyner 
22871d10453SEric Joyner extern struct if_txrx ice_txrx;
22971d10453SEric Joyner extern struct if_txrx ice_recovery_txrx;
23071d10453SEric Joyner 
23171d10453SEric Joyner /**
23271d10453SEric Joyner  * @var ice_sctx
23371d10453SEric Joyner  * @brief ice driver shared context
23471d10453SEric Joyner  *
23571d10453SEric Joyner  * Structure defining shared values (context) that is used by all instances of
23671d10453SEric Joyner  * the device. Primarily used to setup details about how the iflib stack
23771d10453SEric Joyner  * should treat this driver. Also defines the default, minimum, and maximum
23871d10453SEric Joyner  * number of descriptors in each ring.
23971d10453SEric Joyner  */
24071d10453SEric Joyner static struct if_shared_ctx ice_sctx = {
24171d10453SEric Joyner 	.isc_magic = IFLIB_MAGIC,
24271d10453SEric Joyner 	.isc_q_align = PAGE_SIZE,
24371d10453SEric Joyner 
24471d10453SEric Joyner 	.isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
24571d10453SEric Joyner 	/* We could technically set this as high as ICE_MAX_DMA_SEG_SIZE, but
24671d10453SEric Joyner 	 * that doesn't make sense since that would be larger than the maximum
24771d10453SEric Joyner 	 * size of a single packet.
24871d10453SEric Joyner 	 */
24971d10453SEric Joyner 	.isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
25071d10453SEric Joyner 
25171d10453SEric Joyner 	/* XXX: This is only used by iflib to ensure that
25271d10453SEric Joyner 	 * scctx->isc_tx_tso_size_max + the VLAN header is a valid size.
25371d10453SEric Joyner 	 */
25471d10453SEric Joyner 	.isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
25571d10453SEric Joyner 	/* XXX: This is used by iflib to set the number of segments in the TSO
25671d10453SEric Joyner 	 * DMA tag. However, scctx->isc_tx_tso_segsize_max is used to set the
25771d10453SEric Joyner 	 * related ifnet parameter.
25871d10453SEric Joyner 	 */
25971d10453SEric Joyner 	.isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
26071d10453SEric Joyner 
26171d10453SEric Joyner 	.isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
26271d10453SEric Joyner 	.isc_rx_nsegments = ICE_MAX_RX_SEGS,
26371d10453SEric Joyner 	.isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
26471d10453SEric Joyner 
26571d10453SEric Joyner 	.isc_nfl = 1,
26671d10453SEric Joyner 	.isc_ntxqs = 1,
26771d10453SEric Joyner 	.isc_nrxqs = 1,
26871d10453SEric Joyner 
26971d10453SEric Joyner 	.isc_admin_intrcnt = 1,
27071d10453SEric Joyner 	.isc_vendor_info = ice_vendor_info_array,
27171d10453SEric Joyner 	.isc_driver_version = __DECONST(char *, ice_driver_version),
27271d10453SEric Joyner 	.isc_driver = &ice_iflib_driver,
27371d10453SEric Joyner 
27471d10453SEric Joyner 	/*
27571d10453SEric Joyner 	 * IFLIB_NEED_SCRATCH ensures that mbufs have scratch space available
27671d10453SEric Joyner 	 * for hardware checksum offload
27771d10453SEric Joyner 	 *
27871d10453SEric Joyner 	 * IFLIB_TSO_INIT_IP ensures that the TSO packets have zeroed out the
27971d10453SEric Joyner 	 * IP sum field, required by our hardware to calculate valid TSO
28071d10453SEric Joyner 	 * checksums.
28171d10453SEric Joyner 	 *
28271d10453SEric Joyner 	 * IFLIB_ADMIN_ALWAYS_RUN ensures that the administrative task runs
28371d10453SEric Joyner 	 * even when the interface is down.
28471d10453SEric Joyner 	 *
28571d10453SEric Joyner 	 * IFLIB_SKIP_MSIX allows the driver to handle allocating MSI-X
28671d10453SEric Joyner 	 * vectors manually instead of relying on iflib code to do this.
28771d10453SEric Joyner 	 */
28871d10453SEric Joyner 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
28971d10453SEric Joyner 		IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX,
29071d10453SEric Joyner 
29171d10453SEric Joyner 	.isc_nrxd_min = {ICE_MIN_DESC_COUNT},
29271d10453SEric Joyner 	.isc_ntxd_min = {ICE_MIN_DESC_COUNT},
29371d10453SEric Joyner 	.isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
29471d10453SEric Joyner 	.isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
29571d10453SEric Joyner 	.isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
29671d10453SEric Joyner 	.isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
29771d10453SEric Joyner };
29871d10453SEric Joyner 
29983c0a9e8SJohn Baldwin DRIVER_MODULE(ice, pci, ice_driver, ice_module_event_handler, NULL);
30071d10453SEric Joyner 
30171d10453SEric Joyner MODULE_VERSION(ice, 1);
30271d10453SEric Joyner MODULE_DEPEND(ice, pci, 1, 1, 1);
30371d10453SEric Joyner MODULE_DEPEND(ice, ether, 1, 1, 1);
30471d10453SEric Joyner MODULE_DEPEND(ice, iflib, 1, 1, 1);
30571d10453SEric Joyner 
30671d10453SEric Joyner IFLIB_PNP_INFO(pci, ice, ice_vendor_info_array);
30771d10453SEric Joyner 
30871d10453SEric Joyner /* Static driver-wide sysctls */
30971d10453SEric Joyner #include "ice_iflib_sysctls.h"
31071d10453SEric Joyner 
31171d10453SEric Joyner /**
31271d10453SEric Joyner  * ice_pci_mapping - Map PCI BAR memory
31371d10453SEric Joyner  * @sc: device private softc
31471d10453SEric Joyner  *
31571d10453SEric Joyner  * Map PCI BAR 0 for device operation.
31671d10453SEric Joyner  */
31771d10453SEric Joyner static int
ice_pci_mapping(struct ice_softc * sc)31871d10453SEric Joyner ice_pci_mapping(struct ice_softc *sc)
31971d10453SEric Joyner {
32071d10453SEric Joyner 	int rc;
32171d10453SEric Joyner 
32271d10453SEric Joyner 	/* Map BAR0 */
32371d10453SEric Joyner 	rc = ice_map_bar(sc->dev, &sc->bar0, 0);
32471d10453SEric Joyner 	if (rc)
32571d10453SEric Joyner 		return rc;
32671d10453SEric Joyner 
32771d10453SEric Joyner 	return 0;
32871d10453SEric Joyner }
32971d10453SEric Joyner 
33071d10453SEric Joyner /**
33171d10453SEric Joyner  * ice_free_pci_mapping - Release PCI BAR memory
33271d10453SEric Joyner  * @sc: device private softc
33371d10453SEric Joyner  *
33471d10453SEric Joyner  * Release PCI BARs which were previously mapped by ice_pci_mapping().
33571d10453SEric Joyner  */
33671d10453SEric Joyner static void
ice_free_pci_mapping(struct ice_softc * sc)33771d10453SEric Joyner ice_free_pci_mapping(struct ice_softc *sc)
33871d10453SEric Joyner {
33971d10453SEric Joyner 	/* Free BAR0 */
34071d10453SEric Joyner 	ice_free_bar(sc->dev, &sc->bar0);
34171d10453SEric Joyner }
34271d10453SEric Joyner 
34371d10453SEric Joyner /*
34471d10453SEric Joyner  * Device methods
34571d10453SEric Joyner  */
34671d10453SEric Joyner 
34771d10453SEric Joyner /**
34871d10453SEric Joyner  * ice_register - register device method callback
34971d10453SEric Joyner  * @dev: the device being registered
35071d10453SEric Joyner  *
35171d10453SEric Joyner  * Returns a pointer to the shared context structure, which is used by iflib.
35271d10453SEric Joyner  */
35371d10453SEric Joyner static void *
ice_register(device_t dev __unused)35471d10453SEric Joyner ice_register(device_t dev __unused)
35571d10453SEric Joyner {
35671d10453SEric Joyner 	return &ice_sctx;
35771d10453SEric Joyner } /* ice_register */
35871d10453SEric Joyner 
35971d10453SEric Joyner /**
36071d10453SEric Joyner  * ice_setup_scctx - Setup the iflib softc context structure
36171d10453SEric Joyner  * @sc: the device private structure
36271d10453SEric Joyner  *
36371d10453SEric Joyner  * Setup the parameters in if_softc_ctx_t structure used by the iflib stack
36471d10453SEric Joyner  * when loading.
36571d10453SEric Joyner  */
36671d10453SEric Joyner static void
ice_setup_scctx(struct ice_softc * sc)36771d10453SEric Joyner ice_setup_scctx(struct ice_softc *sc)
36871d10453SEric Joyner {
36971d10453SEric Joyner 	if_softc_ctx_t scctx = sc->scctx;
37071d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
3719c30461dSEric Joyner 	device_t dev = sc->dev;
37271d10453SEric Joyner 	bool safe_mode, recovery_mode;
37371d10453SEric Joyner 
37471d10453SEric Joyner 	safe_mode = ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE);
37571d10453SEric Joyner 	recovery_mode = ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE);
37671d10453SEric Joyner 
37771d10453SEric Joyner 	/*
37871d10453SEric Joyner 	 * If the driver loads in Safe mode or Recovery mode, limit iflib to
37971d10453SEric Joyner 	 * a single queue pair.
38071d10453SEric Joyner 	 */
38171d10453SEric Joyner 	if (safe_mode || recovery_mode) {
38271d10453SEric Joyner 		scctx->isc_ntxqsets = scctx->isc_nrxqsets = 1;
38371d10453SEric Joyner 		scctx->isc_ntxqsets_max = 1;
38471d10453SEric Joyner 		scctx->isc_nrxqsets_max = 1;
38571d10453SEric Joyner 	} else {
38671d10453SEric Joyner 		/*
38771d10453SEric Joyner 		 * iflib initially sets the isc_ntxqsets and isc_nrxqsets to
38871d10453SEric Joyner 		 * the values of the override sysctls. Cache these initial
38971d10453SEric Joyner 		 * values so that the driver can be aware of what the iflib
39071d10453SEric Joyner 		 * sysctl value is when setting up MSI-X vectors.
39171d10453SEric Joyner 		 */
39271d10453SEric Joyner 		sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets;
39371d10453SEric Joyner 		sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets;
39471d10453SEric Joyner 
39571d10453SEric Joyner 		if (scctx->isc_ntxqsets == 0)
39671d10453SEric Joyner 			scctx->isc_ntxqsets = hw->func_caps.common_cap.rss_table_size;
39771d10453SEric Joyner 		if (scctx->isc_nrxqsets == 0)
39871d10453SEric Joyner 			scctx->isc_nrxqsets = hw->func_caps.common_cap.rss_table_size;
39971d10453SEric Joyner 
40071d10453SEric Joyner 		scctx->isc_ntxqsets_max = hw->func_caps.common_cap.num_txq;
40171d10453SEric Joyner 		scctx->isc_nrxqsets_max = hw->func_caps.common_cap.num_rxq;
40271d10453SEric Joyner 
40371d10453SEric Joyner 		/*
40471d10453SEric Joyner 		 * Sanity check that the iflib sysctl values are within the
40571d10453SEric Joyner 		 * maximum supported range.
40671d10453SEric Joyner 		 */
40771d10453SEric Joyner 		if (sc->ifc_sysctl_ntxqs > scctx->isc_ntxqsets_max)
40871d10453SEric Joyner 			sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets_max;
40971d10453SEric Joyner 		if (sc->ifc_sysctl_nrxqs > scctx->isc_nrxqsets_max)
41071d10453SEric Joyner 			sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets_max;
41171d10453SEric Joyner 	}
41271d10453SEric Joyner 
41371d10453SEric Joyner 	scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
41471d10453SEric Joyner 	    * sizeof(struct ice_tx_desc), DBA_ALIGN);
41571d10453SEric Joyner 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
41671d10453SEric Joyner 	    * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
41771d10453SEric Joyner 
41871d10453SEric Joyner 	scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
41971d10453SEric Joyner 	scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
42071d10453SEric Joyner 	scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
42171d10453SEric Joyner 	scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
42271d10453SEric Joyner 
4239c30461dSEric Joyner 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
42471d10453SEric Joyner 	scctx->isc_rss_table_size = hw->func_caps.common_cap.rss_table_size;
42571d10453SEric Joyner 
42671d10453SEric Joyner 	/*
42771d10453SEric Joyner 	 * If the driver loads in recovery mode, disable Tx/Rx functionality
42871d10453SEric Joyner 	 */
42971d10453SEric Joyner 	if (recovery_mode)
43071d10453SEric Joyner 		scctx->isc_txrx = &ice_recovery_txrx;
43171d10453SEric Joyner 	else
43271d10453SEric Joyner 		scctx->isc_txrx = &ice_txrx;
43371d10453SEric Joyner 
43471d10453SEric Joyner 	/*
43571d10453SEric Joyner 	 * If the driver loads in Safe mode or Recovery mode, disable
43671d10453SEric Joyner 	 * advanced features including hardware offloads.
43771d10453SEric Joyner 	 */
43871d10453SEric Joyner 	if (safe_mode || recovery_mode) {
43971d10453SEric Joyner 		scctx->isc_capenable = ICE_SAFE_CAPS;
44071d10453SEric Joyner 		scctx->isc_tx_csum_flags = 0;
44171d10453SEric Joyner 	} else {
44271d10453SEric Joyner 		scctx->isc_capenable = ICE_FULL_CAPS;
44371d10453SEric Joyner 		scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
44471d10453SEric Joyner 	}
44571d10453SEric Joyner 
44671d10453SEric Joyner 	scctx->isc_capabilities = scctx->isc_capenable;
44771d10453SEric Joyner } /* ice_setup_scctx */
44871d10453SEric Joyner 
44971d10453SEric Joyner /**
45071d10453SEric Joyner  * ice_if_attach_pre - Early device attach logic
45171d10453SEric Joyner  * @ctx: the iflib context structure
45271d10453SEric Joyner  *
45371d10453SEric Joyner  * Called by iflib during the attach process. Earliest main driver entry
45471d10453SEric Joyner  * point which performs necessary hardware and driver initialization. Called
45571d10453SEric Joyner  * before the Tx and Rx queues are allocated.
45671d10453SEric Joyner  */
45771d10453SEric Joyner static int
ice_if_attach_pre(if_ctx_t ctx)45871d10453SEric Joyner ice_if_attach_pre(if_ctx_t ctx)
45971d10453SEric Joyner {
46071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
46171d10453SEric Joyner 	enum ice_fw_modes fw_mode;
462f2635e84SEric Joyner 	int status;
46371d10453SEric Joyner 	if_softc_ctx_t scctx;
46471d10453SEric Joyner 	struct ice_hw *hw;
46571d10453SEric Joyner 	device_t dev;
46671d10453SEric Joyner 	int err;
46771d10453SEric Joyner 
46871d10453SEric Joyner 	device_printf(iflib_get_dev(ctx), "Loading the iflib ice driver\n");
46971d10453SEric Joyner 
47056429daeSEric Joyner 	ice_set_state(&sc->state, ICE_STATE_ATTACHING);
47156429daeSEric Joyner 
47271d10453SEric Joyner 	sc->ctx = ctx;
47371d10453SEric Joyner 	sc->media = iflib_get_media(ctx);
47471d10453SEric Joyner 	sc->sctx = iflib_get_sctx(ctx);
47571d10453SEric Joyner 	sc->iflib_ctx_lock = iflib_ctx_lock_get(ctx);
476f2635e84SEric Joyner 	sc->ifp = iflib_get_ifp(ctx);
47771d10453SEric Joyner 
47871d10453SEric Joyner 	dev = sc->dev = iflib_get_dev(ctx);
47971d10453SEric Joyner 	scctx = sc->scctx = iflib_get_softc_ctx(ctx);
48071d10453SEric Joyner 
48171d10453SEric Joyner 	hw = &sc->hw;
48271d10453SEric Joyner 	hw->back = sc;
48371d10453SEric Joyner 
48471d10453SEric Joyner 	snprintf(sc->admin_mtx_name, sizeof(sc->admin_mtx_name),
48571d10453SEric Joyner 		 "%s:admin", device_get_nameunit(dev));
48671d10453SEric Joyner 	mtx_init(&sc->admin_mtx, sc->admin_mtx_name, NULL, MTX_DEF);
48771d10453SEric Joyner 	callout_init_mtx(&sc->admin_timer, &sc->admin_mtx, 0);
48871d10453SEric Joyner 
48971d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
49071d10453SEric Joyner 
49171d10453SEric Joyner 	if (ice_pci_mapping(sc)) {
49271d10453SEric Joyner 		err = (ENXIO);
49371d10453SEric Joyner 		goto destroy_admin_timer;
49471d10453SEric Joyner 	}
49571d10453SEric Joyner 
49671d10453SEric Joyner 	/* Save off the PCI information */
49771d10453SEric Joyner 	ice_save_pci_info(hw, dev);
49871d10453SEric Joyner 
49971d10453SEric Joyner 	/* create tunables as early as possible */
50071d10453SEric Joyner 	ice_add_device_tunables(sc);
50171d10453SEric Joyner 
50271d10453SEric Joyner 	/* Setup ControlQ lengths */
50371d10453SEric Joyner 	ice_set_ctrlq_len(hw);
50471d10453SEric Joyner 
5058923de59SPiotr Kubaj reinit_hw:
5068923de59SPiotr Kubaj 
50771d10453SEric Joyner 	fw_mode = ice_get_fw_mode(hw);
50871d10453SEric Joyner 	if (fw_mode == ICE_FW_MODE_REC) {
50971d10453SEric 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");
51071d10453SEric Joyner 
51171d10453SEric Joyner 		err = ice_attach_pre_recovery_mode(sc);
51271d10453SEric Joyner 		if (err)
51371d10453SEric Joyner 			goto free_pci_mapping;
51471d10453SEric Joyner 
51571d10453SEric Joyner 		return (0);
51671d10453SEric Joyner 	}
51771d10453SEric Joyner 
51871d10453SEric Joyner 	/* Initialize the hw data structure */
51971d10453SEric Joyner 	status = ice_init_hw(hw);
52071d10453SEric Joyner 	if (status) {
52171d10453SEric Joyner 		if (status == ICE_ERR_FW_API_VER) {
52271d10453SEric Joyner 			/* Enter recovery mode, so that the driver remains
52371d10453SEric Joyner 			 * loaded. This way, if the system administrator
52471d10453SEric Joyner 			 * cannot update the driver, they may still attempt to
52571d10453SEric Joyner 			 * downgrade the NVM.
52671d10453SEric Joyner 			 */
52771d10453SEric Joyner 			err = ice_attach_pre_recovery_mode(sc);
52871d10453SEric Joyner 			if (err)
52971d10453SEric Joyner 				goto free_pci_mapping;
53071d10453SEric Joyner 
53171d10453SEric Joyner 			return (0);
53271d10453SEric Joyner 		} else {
53371d10453SEric Joyner 			err = EIO;
53471d10453SEric Joyner 			device_printf(dev, "Unable to initialize hw, err %s aq_err %s\n",
53571d10453SEric Joyner 				      ice_status_str(status),
53671d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
53771d10453SEric Joyner 		}
53871d10453SEric Joyner 		goto free_pci_mapping;
53971d10453SEric Joyner 	}
54071d10453SEric Joyner 
5418923de59SPiotr Kubaj 	ice_init_device_features(sc);
5428923de59SPiotr Kubaj 
5439c30461dSEric Joyner 	/* Keep flag set by default */
5449c30461dSEric Joyner 	ice_set_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN);
5459c30461dSEric Joyner 
54671d10453SEric Joyner 	/* Notify firmware of the device driver version */
54771d10453SEric Joyner 	err = ice_send_version(sc);
54871d10453SEric Joyner 	if (err)
54971d10453SEric Joyner 		goto deinit_hw;
55071d10453SEric Joyner 
5518923de59SPiotr Kubaj 	/*
5528923de59SPiotr Kubaj 	 * Success indicates a change was made that requires a reinitialization
5538923de59SPiotr Kubaj 	 * of the hardware
5548923de59SPiotr Kubaj 	 */
5558923de59SPiotr Kubaj 	err = ice_load_pkg_file(sc);
556f2635e84SEric Joyner 	if (!err) {
5578923de59SPiotr Kubaj 		ice_deinit_hw(hw);
5588923de59SPiotr Kubaj 		goto reinit_hw;
5598923de59SPiotr Kubaj 	}
56071d10453SEric Joyner 
56171d10453SEric Joyner 	err = ice_init_link_events(sc);
56271d10453SEric Joyner 	if (err) {
56371d10453SEric Joyner 		device_printf(dev, "ice_init_link_events failed: %s\n",
56471d10453SEric Joyner 			      ice_err_str(err));
56571d10453SEric Joyner 		goto deinit_hw;
56671d10453SEric Joyner 	}
56771d10453SEric Joyner 
5688923de59SPiotr Kubaj 	/* Initialize VLAN mode in FW; if dual VLAN mode is supported by the package
5698923de59SPiotr Kubaj 	 * and firmware, this will force them to use single VLAN mode.
5708923de59SPiotr Kubaj 	 */
5718923de59SPiotr Kubaj 	status = ice_set_vlan_mode(hw);
5728923de59SPiotr Kubaj 	if (status) {
5738923de59SPiotr Kubaj 		err = EIO;
5748923de59SPiotr Kubaj 		device_printf(dev, "Unable to initialize VLAN mode, err %s aq_err %s\n",
5758923de59SPiotr Kubaj 			      ice_status_str(status),
5768923de59SPiotr Kubaj 			      ice_aq_str(hw->adminq.sq_last_status));
5778923de59SPiotr Kubaj 		goto deinit_hw;
5788923de59SPiotr Kubaj 	}
57971d10453SEric Joyner 
5808923de59SPiotr Kubaj 	ice_print_nvm_version(sc);
58171d10453SEric Joyner 
58271d10453SEric Joyner 	/* Setup the MAC address */
58371d10453SEric Joyner 	iflib_set_mac(ctx, hw->port_info->mac.lan_addr);
58471d10453SEric Joyner 
58571d10453SEric Joyner 	/* Setup the iflib softc context structure */
58671d10453SEric Joyner 	ice_setup_scctx(sc);
58771d10453SEric Joyner 
58871d10453SEric Joyner 	/* Initialize the Tx queue manager */
58971d10453SEric Joyner 	err = ice_resmgr_init(&sc->tx_qmgr, hw->func_caps.common_cap.num_txq);
59071d10453SEric Joyner 	if (err) {
59171d10453SEric Joyner 		device_printf(dev, "Unable to initialize Tx queue manager: %s\n",
59271d10453SEric Joyner 			      ice_err_str(err));
59371d10453SEric Joyner 		goto deinit_hw;
59471d10453SEric Joyner 	}
59571d10453SEric Joyner 
59671d10453SEric Joyner 	/* Initialize the Rx queue manager */
59771d10453SEric Joyner 	err = ice_resmgr_init(&sc->rx_qmgr, hw->func_caps.common_cap.num_rxq);
59871d10453SEric Joyner 	if (err) {
59971d10453SEric Joyner 		device_printf(dev, "Unable to initialize Rx queue manager: %s\n",
60071d10453SEric Joyner 			      ice_err_str(err));
60171d10453SEric Joyner 		goto free_tx_qmgr;
60271d10453SEric Joyner 	}
60371d10453SEric Joyner 
6049e54973fSEric Joyner 	/* Initialize the PF device interrupt resource manager */
60571d10453SEric Joyner 	err = ice_alloc_intr_tracking(sc);
60671d10453SEric Joyner 	if (err)
60771d10453SEric Joyner 		/* Errors are already printed */
60871d10453SEric Joyner 		goto free_rx_qmgr;
60971d10453SEric Joyner 
61071d10453SEric Joyner 	/* Determine maximum number of VSIs we'll prepare for */
61171d10453SEric Joyner 	sc->num_available_vsi = min(ICE_MAX_VSI_AVAILABLE,
61271d10453SEric Joyner 				    hw->func_caps.guar_num_vsi);
61371d10453SEric Joyner 
61471d10453SEric Joyner 	if (!sc->num_available_vsi) {
61571d10453SEric Joyner 		err = EIO;
61671d10453SEric Joyner 		device_printf(dev, "No VSIs allocated to host\n");
61771d10453SEric Joyner 		goto free_intr_tracking;
61871d10453SEric Joyner 	}
61971d10453SEric Joyner 
62071d10453SEric Joyner 	/* Allocate storage for the VSI pointers */
62171d10453SEric Joyner 	sc->all_vsi = (struct ice_vsi **)
62271d10453SEric Joyner 		malloc(sizeof(struct ice_vsi *) * sc->num_available_vsi,
62371d10453SEric Joyner 		       M_ICE, M_WAITOK | M_ZERO);
62471d10453SEric Joyner 	if (!sc->all_vsi) {
62571d10453SEric Joyner 		err = ENOMEM;
62671d10453SEric Joyner 		device_printf(dev, "Unable to allocate VSI array\n");
62771d10453SEric Joyner 		goto free_intr_tracking;
62871d10453SEric Joyner 	}
62971d10453SEric Joyner 
63071d10453SEric Joyner 	/*
63171d10453SEric Joyner 	 * Prepare the statically allocated primary PF VSI in the softc
63271d10453SEric Joyner 	 * structure. Other VSIs will be dynamically allocated as needed.
63371d10453SEric Joyner 	 */
63471d10453SEric Joyner 	ice_setup_pf_vsi(sc);
63571d10453SEric Joyner 
6365f97656fSZhenlei Huang 	ice_alloc_vsi_qmap(&sc->pf_vsi, scctx->isc_ntxqsets_max,
63771d10453SEric Joyner 	    scctx->isc_nrxqsets_max);
63871d10453SEric Joyner 
63971d10453SEric Joyner 	/* Allocate MSI-X vectors (due to isc_flags IFLIB_SKIP_MSIX) */
64071d10453SEric Joyner 	err = ice_allocate_msix(sc);
64171d10453SEric Joyner 	if (err)
64271d10453SEric Joyner 		goto free_main_vsi;
64371d10453SEric Joyner 
64471d10453SEric Joyner 	return 0;
64571d10453SEric Joyner 
64671d10453SEric Joyner free_main_vsi:
64771d10453SEric Joyner 	/* ice_release_vsi will free the queue maps if they were allocated */
64871d10453SEric Joyner 	ice_release_vsi(&sc->pf_vsi);
64971d10453SEric Joyner 	free(sc->all_vsi, M_ICE);
65071d10453SEric Joyner 	sc->all_vsi = NULL;
65171d10453SEric Joyner free_intr_tracking:
65271d10453SEric Joyner 	ice_free_intr_tracking(sc);
65371d10453SEric Joyner free_rx_qmgr:
65471d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
65571d10453SEric Joyner free_tx_qmgr:
65671d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
65771d10453SEric Joyner deinit_hw:
65871d10453SEric Joyner 	ice_deinit_hw(hw);
65971d10453SEric Joyner free_pci_mapping:
66071d10453SEric Joyner 	ice_free_pci_mapping(sc);
66171d10453SEric Joyner destroy_admin_timer:
66271d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
66371d10453SEric Joyner 	callout_stop(&sc->admin_timer);
66471d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
66571d10453SEric Joyner 	mtx_destroy(&sc->admin_mtx);
66671d10453SEric Joyner 	return err;
66771d10453SEric Joyner } /* ice_if_attach_pre */
66871d10453SEric Joyner 
66971d10453SEric Joyner /**
67071d10453SEric Joyner  * ice_attach_pre_recovery_mode - Limited driver attach_pre for FW recovery
67171d10453SEric Joyner  * @sc: the device private softc
67271d10453SEric Joyner  *
67371d10453SEric Joyner  * Loads the device driver in limited Firmware Recovery mode, intended to
67471d10453SEric Joyner  * allow users to update the firmware to attempt to recover the device.
67571d10453SEric Joyner  *
67671d10453SEric Joyner  * @remark We may enter recovery mode in case either (a) the firmware is
67771d10453SEric Joyner  * detected to be in an invalid state and must be re-programmed, or (b) the
67871d10453SEric Joyner  * driver detects that the loaded firmware has a non-compatible API version
67971d10453SEric Joyner  * that the driver cannot operate with.
68071d10453SEric Joyner  */
68171d10453SEric Joyner static int
ice_attach_pre_recovery_mode(struct ice_softc * sc)68271d10453SEric Joyner ice_attach_pre_recovery_mode(struct ice_softc *sc)
68371d10453SEric Joyner {
68471d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_RECOVERY_MODE);
68571d10453SEric Joyner 
68671d10453SEric Joyner 	/* Setup the iflib softc context */
68771d10453SEric Joyner 	ice_setup_scctx(sc);
68871d10453SEric Joyner 
68971d10453SEric Joyner 	/* Setup the PF VSI back pointer */
69071d10453SEric Joyner 	sc->pf_vsi.sc = sc;
69171d10453SEric Joyner 
69271d10453SEric Joyner 	/*
69371d10453SEric Joyner 	 * We still need to allocate MSI-X vectors since we need one vector to
69471d10453SEric Joyner 	 * run the administrative admin interrupt
69571d10453SEric Joyner 	 */
69671d10453SEric Joyner 	return ice_allocate_msix(sc);
69771d10453SEric Joyner }
69871d10453SEric Joyner 
69971d10453SEric Joyner /**
70071d10453SEric Joyner  * ice_update_link_status - notify OS of link state change
70171d10453SEric Joyner  * @sc: device private softc structure
70271d10453SEric Joyner  * @update_media: true if we should update media even if link didn't change
70371d10453SEric Joyner  *
70471d10453SEric Joyner  * Called to notify iflib core of link status changes. Should be called once
70571d10453SEric Joyner  * during attach_post, and whenever link status changes during runtime.
70671d10453SEric Joyner  *
70771d10453SEric Joyner  * This call only updates the currently supported media types if the link
70871d10453SEric Joyner  * status changed, or if update_media is set to true.
70971d10453SEric Joyner  */
71071d10453SEric Joyner static void
ice_update_link_status(struct ice_softc * sc,bool update_media)71171d10453SEric Joyner ice_update_link_status(struct ice_softc *sc, bool update_media)
71271d10453SEric Joyner {
71371d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
714f2635e84SEric Joyner 	int status;
71571d10453SEric Joyner 
71671d10453SEric Joyner 	/* Never report link up when in recovery mode */
71771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
71871d10453SEric Joyner 		return;
71971d10453SEric Joyner 
72071d10453SEric Joyner 	/* Report link status to iflib only once each time it changes */
72171d10453SEric Joyner 	if (!ice_testandset_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED)) {
72271d10453SEric Joyner 		if (sc->link_up) { /* link is up */
72371d10453SEric Joyner 			uint64_t baudrate = ice_aq_speed_to_rate(sc->hw.port_info);
72471d10453SEric Joyner 
7259c30461dSEric Joyner 			if (!(hw->port_info->phy.link_info_old.link_info & ICE_AQ_LINK_UP))
7267d7af7f8SEric Joyner 				ice_set_default_local_lldp_mib(sc);
7277d7af7f8SEric Joyner 
72871d10453SEric Joyner 			iflib_link_state_change(sc->ctx, LINK_STATE_UP, baudrate);
7298a13362dSEric Joyner 			ice_rdma_link_change(sc, LINK_STATE_UP, baudrate);
73071d10453SEric Joyner 
73171d10453SEric Joyner 			ice_link_up_msg(sc);
73271d10453SEric Joyner 		} else { /* link is down */
73371d10453SEric Joyner 			iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
7348a13362dSEric Joyner 			ice_rdma_link_change(sc, LINK_STATE_DOWN, 0);
73571d10453SEric Joyner 		}
7369dc2f6e2SEric Joyner 		update_media = true;
73771d10453SEric Joyner 	}
73871d10453SEric Joyner 
73971d10453SEric Joyner 	/* Update the supported media types */
7409c30461dSEric Joyner 	if (update_media && !ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
7417d7af7f8SEric Joyner 		status = ice_add_media_types(sc, sc->media);
74271d10453SEric Joyner 		if (status)
74371d10453SEric Joyner 			device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
74471d10453SEric Joyner 				      ice_status_str(status),
74571d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
74671d10453SEric Joyner 	}
74771d10453SEric Joyner }
74871d10453SEric Joyner 
74971d10453SEric Joyner /**
75071d10453SEric Joyner  * ice_if_attach_post - Late device attach logic
75171d10453SEric Joyner  * @ctx: the iflib context structure
75271d10453SEric Joyner  *
75371d10453SEric Joyner  * Called by iflib to finish up attaching the device. Performs any attach
75471d10453SEric Joyner  * logic which must wait until after the Tx and Rx queues have been
75571d10453SEric Joyner  * allocated.
75671d10453SEric Joyner  */
75771d10453SEric Joyner static int
ice_if_attach_post(if_ctx_t ctx)75871d10453SEric Joyner ice_if_attach_post(if_ctx_t ctx)
75971d10453SEric Joyner {
76071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
76171d10453SEric Joyner 	if_t ifp = iflib_get_ifp(ctx);
762f2635e84SEric Joyner 	int status;
76371d10453SEric Joyner 	int err;
76471d10453SEric Joyner 
76571d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
76671d10453SEric Joyner 
76771d10453SEric Joyner 	/* We don't yet support loading if MSI-X is not supported */
76871d10453SEric Joyner 	if (sc->scctx->isc_intr != IFLIB_INTR_MSIX) {
76971d10453SEric Joyner 		device_printf(sc->dev, "The ice driver does not support loading without MSI-X\n");
77071d10453SEric Joyner 		return (ENOTSUP);
77171d10453SEric Joyner 	}
77271d10453SEric Joyner 
77371d10453SEric Joyner 	/* The ifnet structure hasn't yet been initialized when the attach_pre
77471d10453SEric Joyner 	 * handler is called, so wait until attach_post to setup the
77571d10453SEric Joyner 	 * isc_max_frame_size.
77671d10453SEric Joyner 	 */
777402810d3SJustin Hibbits 	sc->scctx->isc_max_frame_size = if_getmtu(ifp) +
77871d10453SEric Joyner 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
77971d10453SEric Joyner 
78071d10453SEric Joyner 	/*
78171d10453SEric Joyner 	 * If we are in recovery mode, only perform a limited subset of
78271d10453SEric Joyner 	 * initialization to support NVM recovery.
78371d10453SEric Joyner 	 */
78471d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
78571d10453SEric Joyner 		ice_attach_post_recovery_mode(sc);
78671d10453SEric Joyner 		return (0);
78771d10453SEric Joyner 	}
78871d10453SEric Joyner 
78971d10453SEric Joyner 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
79071d10453SEric Joyner 
79171d10453SEric Joyner 	err = ice_initialize_vsi(&sc->pf_vsi);
79271d10453SEric Joyner 	if (err) {
79371d10453SEric Joyner 		device_printf(sc->dev, "Unable to initialize Main VSI: %s\n",
79471d10453SEric Joyner 			      ice_err_str(err));
79571d10453SEric Joyner 		return err;
79671d10453SEric Joyner 	}
79771d10453SEric Joyner 
7989cf1841cSEric Joyner 	/* Enable FW health event reporting */
7999cf1841cSEric Joyner 	ice_init_health_events(sc);
8009cf1841cSEric Joyner 
80171d10453SEric Joyner 	/* Configure the main PF VSI for RSS */
80271d10453SEric Joyner 	err = ice_config_rss(&sc->pf_vsi);
80371d10453SEric Joyner 	if (err) {
80471d10453SEric Joyner 		device_printf(sc->dev,
80571d10453SEric Joyner 			      "Unable to configure RSS for the main VSI, err %s\n",
80671d10453SEric Joyner 			      ice_err_str(err));
80771d10453SEric Joyner 		return err;
80871d10453SEric Joyner 	}
80971d10453SEric Joyner 
81071d10453SEric Joyner 	/* Configure switch to drop transmitted LLDP and PAUSE frames */
81171d10453SEric Joyner 	err = ice_cfg_pf_ethertype_filters(sc);
81271d10453SEric Joyner 	if (err)
81371d10453SEric Joyner 		return err;
81471d10453SEric Joyner 
81571d10453SEric Joyner 	ice_get_and_print_bus_info(sc);
81671d10453SEric Joyner 
81771d10453SEric Joyner 	ice_set_link_management_mode(sc);
81871d10453SEric Joyner 
81971d10453SEric Joyner 	ice_init_saved_phy_cfg(sc);
82071d10453SEric Joyner 
82156429daeSEric Joyner 	ice_cfg_pba_num(sc);
82256429daeSEric Joyner 
8239c30461dSEric Joyner 	/* Set a default value for PFC mode on attach since the FW state is unknown
8249c30461dSEric Joyner 	 * before sysctl tunables are executed and it can't be queried. This fixes an
8259c30461dSEric Joyner 	 * issue when loading the driver with the FW LLDP agent enabled but the FW
8269c30461dSEric Joyner 	 * was previously in DSCP PFC mode.
8279c30461dSEric Joyner 	 */
8289c30461dSEric Joyner 	status = ice_aq_set_pfc_mode(&sc->hw, ICE_AQC_PFC_VLAN_BASED_PFC, NULL);
829f2635e84SEric Joyner 	if (status)
8309c30461dSEric Joyner 		device_printf(sc->dev, "Setting pfc mode failed, status %s\n", ice_status_str(status));
8319c30461dSEric Joyner 
83271d10453SEric Joyner 	ice_add_device_sysctls(sc);
83371d10453SEric Joyner 
83471d10453SEric Joyner 	/* Get DCBX/LLDP state and start DCBX agent */
83571d10453SEric Joyner 	ice_init_dcb_setup(sc);
83671d10453SEric Joyner 
837f377a0c7SEric Joyner 	/* Setup link, if PHY FW is ready */
838f377a0c7SEric Joyner 	ice_init_link(sc);
83971d10453SEric Joyner 
84071d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
84171d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
84271d10453SEric Joyner 
84371d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
84471d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
84571d10453SEric Joyner 
8468a13362dSEric Joyner 	err = ice_rdma_pf_attach(sc);
8478a13362dSEric Joyner 	if (err)
8488a13362dSEric Joyner 		return (err);
8498a13362dSEric Joyner 
85071d10453SEric Joyner 	/* Start the admin timer */
85171d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
85271d10453SEric Joyner 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
85371d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
85471d10453SEric Joyner 
8559c30461dSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
8569c30461dSEric Joyner 		 !ice_test_state(&sc->state, ICE_STATE_NO_MEDIA))
8579c30461dSEric Joyner 		ice_set_state(&sc->state, ICE_STATE_FIRST_INIT_LINK);
8589c30461dSEric Joyner 
85956429daeSEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
86056429daeSEric Joyner 
86171d10453SEric Joyner 	return 0;
86271d10453SEric Joyner } /* ice_if_attach_post */
86371d10453SEric Joyner 
86471d10453SEric Joyner /**
86571d10453SEric Joyner  * ice_attach_post_recovery_mode - Limited driver attach_post for FW recovery
86671d10453SEric Joyner  * @sc: the device private softc
86771d10453SEric Joyner  *
86871d10453SEric Joyner  * Performs minimal work to prepare the driver to recover an NVM in case the
86971d10453SEric Joyner  * firmware is in recovery mode.
87071d10453SEric Joyner  */
87171d10453SEric Joyner static void
ice_attach_post_recovery_mode(struct ice_softc * sc)87271d10453SEric Joyner ice_attach_post_recovery_mode(struct ice_softc *sc)
87371d10453SEric Joyner {
87471d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
87571d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
87671d10453SEric Joyner 
87771d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
87871d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
87971d10453SEric Joyner 
88071d10453SEric Joyner 	/* Start the admin timer */
88171d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
88271d10453SEric Joyner 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
88371d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
88456429daeSEric Joyner 
88556429daeSEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
88671d10453SEric Joyner }
88771d10453SEric Joyner 
88871d10453SEric Joyner /**
88971d10453SEric Joyner  * ice_free_irqvs - Free IRQ vector memory
89071d10453SEric Joyner  * @sc: the device private softc structure
89171d10453SEric Joyner  *
89271d10453SEric Joyner  * Free IRQ vector memory allocated during ice_if_msix_intr_assign.
89371d10453SEric Joyner  */
89471d10453SEric Joyner static void
ice_free_irqvs(struct ice_softc * sc)89571d10453SEric Joyner ice_free_irqvs(struct ice_softc *sc)
89671d10453SEric Joyner {
89771d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
89871d10453SEric Joyner 	if_ctx_t ctx = sc->ctx;
89971d10453SEric Joyner 	int i;
90071d10453SEric Joyner 
90171d10453SEric Joyner 	/* If the irqvs array is NULL, then there are no vectors to free */
90271d10453SEric Joyner 	if (sc->irqvs == NULL)
90371d10453SEric Joyner 		return;
90471d10453SEric Joyner 
90571d10453SEric Joyner 	/* Free the IRQ vectors */
90671d10453SEric Joyner 	for (i = 0; i < sc->num_irq_vectors; i++)
90771d10453SEric Joyner 		iflib_irq_free(ctx, &sc->irqvs[i].irq);
90871d10453SEric Joyner 
90971d10453SEric Joyner 	/* Clear the irqv pointers */
91071d10453SEric Joyner 	for (i = 0; i < vsi->num_rx_queues; i++)
91171d10453SEric Joyner 		vsi->rx_queues[i].irqv = NULL;
91271d10453SEric Joyner 
91371d10453SEric Joyner 	for (i = 0; i < vsi->num_tx_queues; i++)
91471d10453SEric Joyner 		vsi->tx_queues[i].irqv = NULL;
91571d10453SEric Joyner 
91671d10453SEric Joyner 	/* Release the vector array memory */
91771d10453SEric Joyner 	free(sc->irqvs, M_ICE);
91871d10453SEric Joyner 	sc->irqvs = NULL;
91971d10453SEric Joyner 	sc->num_irq_vectors = 0;
92071d10453SEric Joyner }
92171d10453SEric Joyner 
92271d10453SEric Joyner /**
92371d10453SEric Joyner  * ice_if_detach - Device driver detach logic
92471d10453SEric Joyner  * @ctx: iflib context structure
92571d10453SEric Joyner  *
92671d10453SEric Joyner  * Perform device shutdown logic to detach the device driver.
92771d10453SEric Joyner  *
92871d10453SEric Joyner  * Note that there is no guarantee of the ordering of ice_if_queues_free() and
92971d10453SEric Joyner  * ice_if_detach(). It is possible for the functions to be called in either
93071d10453SEric Joyner  * order, and they must not assume to have a strict ordering.
93171d10453SEric Joyner  */
93271d10453SEric Joyner static int
ice_if_detach(if_ctx_t ctx)93371d10453SEric Joyner ice_if_detach(if_ctx_t ctx)
93471d10453SEric Joyner {
93571d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
93671d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
937f2635e84SEric Joyner 	int status;
93871d10453SEric Joyner 	int i;
93971d10453SEric Joyner 
94071d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
94171d10453SEric Joyner 
94271d10453SEric Joyner 	/* Indicate that we're detaching */
94371d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_DETACHING);
94471d10453SEric Joyner 
94571d10453SEric Joyner 	/* Stop the admin timer */
94671d10453SEric Joyner 	mtx_lock(&sc->admin_mtx);
94771d10453SEric Joyner 	callout_stop(&sc->admin_timer);
94871d10453SEric Joyner 	mtx_unlock(&sc->admin_mtx);
94971d10453SEric Joyner 	mtx_destroy(&sc->admin_mtx);
95071d10453SEric Joyner 
9519e54973fSEric Joyner 	/* Remove additional interfaces if they exist */
9529e54973fSEric Joyner 	if (sc->mirr_if)
9539e54973fSEric Joyner 		ice_destroy_mirror_interface(sc);
9548a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
9558a13362dSEric Joyner 
95671d10453SEric Joyner 	/* Free allocated media types */
95771d10453SEric Joyner 	ifmedia_removeall(sc->media);
95871d10453SEric Joyner 
95971d10453SEric Joyner 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
96071d10453SEric Joyner 	 * pointers. Note, the calls here and those in ice_if_queues_free()
96171d10453SEric Joyner 	 * are *BOTH* necessary, as we cannot guarantee which path will be
96271d10453SEric Joyner 	 * run first
96371d10453SEric Joyner 	 */
96471d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
96571d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
96671d10453SEric Joyner 
96771d10453SEric Joyner 	/* Release MSI-X resources */
96871d10453SEric Joyner 	ice_free_irqvs(sc);
96971d10453SEric Joyner 
97071d10453SEric Joyner 	for (i = 0; i < sc->num_available_vsi; i++) {
97171d10453SEric Joyner 		if (sc->all_vsi[i])
97271d10453SEric Joyner 			ice_release_vsi(sc->all_vsi[i]);
97371d10453SEric Joyner 	}
97471d10453SEric Joyner 
97571d10453SEric Joyner 	if (sc->all_vsi) {
97671d10453SEric Joyner 		free(sc->all_vsi, M_ICE);
97771d10453SEric Joyner 		sc->all_vsi = NULL;
97871d10453SEric Joyner 	}
97971d10453SEric Joyner 
98071d10453SEric Joyner 	/* Release MSI-X memory */
98171d10453SEric Joyner 	pci_release_msi(sc->dev);
98271d10453SEric Joyner 
98371d10453SEric Joyner 	if (sc->msix_table != NULL) {
98471d10453SEric Joyner 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
98571d10453SEric Joyner 				     rman_get_rid(sc->msix_table),
98671d10453SEric Joyner 				     sc->msix_table);
98771d10453SEric Joyner 		sc->msix_table = NULL;
98871d10453SEric Joyner 	}
98971d10453SEric Joyner 
99071d10453SEric Joyner 	ice_free_intr_tracking(sc);
99171d10453SEric Joyner 
99271d10453SEric Joyner 	/* Destroy the queue managers */
99371d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
99471d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
99571d10453SEric Joyner 
99671d10453SEric Joyner 	if (!ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
99771d10453SEric Joyner 		ice_deinit_hw(&sc->hw);
99871d10453SEric Joyner 
9999c30461dSEric Joyner 	IFLIB_CTX_UNLOCK(sc);
10009c30461dSEric Joyner 	status = ice_reset(&sc->hw, ICE_RESET_PFR);
10019c30461dSEric Joyner 	IFLIB_CTX_LOCK(sc);
10029c30461dSEric Joyner 	if (status) {
10039c30461dSEric Joyner 		device_printf(sc->dev, "device PF reset failed, err %s\n",
10049c30461dSEric Joyner 			      ice_status_str(status));
10059c30461dSEric Joyner 	}
10069c30461dSEric Joyner 
100771d10453SEric Joyner 	ice_free_pci_mapping(sc);
100871d10453SEric Joyner 
100971d10453SEric Joyner 	return 0;
101071d10453SEric Joyner } /* ice_if_detach */
101171d10453SEric Joyner 
101271d10453SEric Joyner /**
101371d10453SEric Joyner  * ice_if_tx_queues_alloc - Allocate Tx queue memory
101471d10453SEric Joyner  * @ctx: iflib context structure
101571d10453SEric Joyner  * @vaddrs: virtual addresses for the queue memory
101671d10453SEric Joyner  * @paddrs: physical addresses for the queue memory
101771d10453SEric Joyner  * @ntxqs: the number of Tx queues per set (should always be 1)
101871d10453SEric Joyner  * @ntxqsets: the number of Tx queue sets to allocate
101971d10453SEric Joyner  *
102071d10453SEric Joyner  * Called by iflib to allocate Tx queues for the device. Allocates driver
102171d10453SEric Joyner  * memory to track each queue, the status arrays used for descriptor
102271d10453SEric Joyner  * status reporting, and Tx queue sysctls.
102371d10453SEric Joyner  */
102471d10453SEric Joyner static int
ice_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int __invariant_only ntxqs,int ntxqsets)102571d10453SEric Joyner ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
102671d10453SEric Joyner 		       int __invariant_only ntxqs, int ntxqsets)
102771d10453SEric Joyner {
102871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
102971d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
103071d10453SEric Joyner 	struct ice_tx_queue *txq;
103171d10453SEric Joyner 	int err, i, j;
103271d10453SEric Joyner 
103371d10453SEric Joyner 	MPASS(ntxqs == 1);
103471d10453SEric Joyner 	MPASS(sc->scctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
103571d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
103671d10453SEric Joyner 
103771d10453SEric Joyner 	/* Do not bother allocating queues if we're in recovery mode */
103871d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
103971d10453SEric Joyner 		return (0);
104071d10453SEric Joyner 
104171d10453SEric Joyner 	/* Allocate queue structure memory */
104271d10453SEric Joyner 	if (!(vsi->tx_queues =
10438923de59SPiotr Kubaj 	      (struct ice_tx_queue *) malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
104471d10453SEric Joyner 		device_printf(sc->dev, "Unable to allocate Tx queue memory\n");
104571d10453SEric Joyner 		return (ENOMEM);
104671d10453SEric Joyner 	}
104771d10453SEric Joyner 
104871d10453SEric Joyner 	/* Allocate report status arrays */
104971d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
105071d10453SEric Joyner 		if (!(txq->tx_rsq =
10518923de59SPiotr Kubaj 		      (uint16_t *) malloc(sizeof(uint16_t) * sc->scctx->isc_ntxd[0], M_ICE, M_NOWAIT))) {
105271d10453SEric Joyner 			device_printf(sc->dev, "Unable to allocate tx_rsq memory\n");
105371d10453SEric Joyner 			err = ENOMEM;
105471d10453SEric Joyner 			goto free_tx_queues;
105571d10453SEric Joyner 		}
105671d10453SEric Joyner 		/* Initialize report status array */
105771d10453SEric Joyner 		for (j = 0; j < sc->scctx->isc_ntxd[0]; j++)
105871d10453SEric Joyner 			txq->tx_rsq[j] = QIDX_INVALID;
105971d10453SEric Joyner 	}
106071d10453SEric Joyner 
106171d10453SEric Joyner 	/* Assign queues from PF space to the main VSI */
106271d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap, ntxqsets);
106371d10453SEric Joyner 	if (err) {
106471d10453SEric Joyner 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
106571d10453SEric Joyner 			      ice_err_str(err));
106671d10453SEric Joyner 		goto free_tx_queues;
106771d10453SEric Joyner 	}
106871d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
106971d10453SEric Joyner 
107071d10453SEric Joyner 	/* Add Tx queue sysctls context */
107171d10453SEric Joyner 	ice_vsi_add_txqs_ctx(vsi);
107271d10453SEric Joyner 
107371d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
107456429daeSEric Joyner 		/* q_handle == me when only one TC */
107556429daeSEric Joyner 		txq->me = txq->q_handle = i;
107671d10453SEric Joyner 		txq->vsi = vsi;
107771d10453SEric Joyner 
107871d10453SEric Joyner 		/* store the queue size for easier access */
107971d10453SEric Joyner 		txq->desc_count = sc->scctx->isc_ntxd[0];
108071d10453SEric Joyner 
108171d10453SEric Joyner 		/* get the virtual and physical address of the hardware queues */
108271d10453SEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
108371d10453SEric Joyner 		txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
108471d10453SEric Joyner 		txq->tx_paddr = paddrs[i];
108571d10453SEric Joyner 
108671d10453SEric Joyner 		ice_add_txq_sysctls(txq);
108771d10453SEric Joyner 	}
108871d10453SEric Joyner 
108971d10453SEric Joyner 	vsi->num_tx_queues = ntxqsets;
109071d10453SEric Joyner 
109171d10453SEric Joyner 	return (0);
109271d10453SEric Joyner 
109371d10453SEric Joyner free_tx_queues:
109471d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
109571d10453SEric Joyner 		if (txq->tx_rsq != NULL) {
109671d10453SEric Joyner 			free(txq->tx_rsq, M_ICE);
109771d10453SEric Joyner 			txq->tx_rsq = NULL;
109871d10453SEric Joyner 		}
109971d10453SEric Joyner 	}
110071d10453SEric Joyner 	free(vsi->tx_queues, M_ICE);
110171d10453SEric Joyner 	vsi->tx_queues = NULL;
110271d10453SEric Joyner 	return err;
110371d10453SEric Joyner }
110471d10453SEric Joyner 
110571d10453SEric Joyner /**
110671d10453SEric Joyner  * ice_if_rx_queues_alloc - Allocate Rx queue memory
110771d10453SEric Joyner  * @ctx: iflib context structure
110871d10453SEric Joyner  * @vaddrs: virtual addresses for the queue memory
110971d10453SEric Joyner  * @paddrs: physical addresses for the queue memory
111071d10453SEric Joyner  * @nrxqs: number of Rx queues per set (should always be 1)
111171d10453SEric Joyner  * @nrxqsets: number of Rx queue sets to allocate
111271d10453SEric Joyner  *
111371d10453SEric Joyner  * Called by iflib to allocate Rx queues for the device. Allocates driver
111471d10453SEric Joyner  * memory to track each queue, as well as sets up the Rx queue sysctls.
111571d10453SEric Joyner  */
111671d10453SEric Joyner static int
ice_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int __invariant_only nrxqs,int nrxqsets)111771d10453SEric Joyner ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
111871d10453SEric Joyner 		       int __invariant_only nrxqs, int nrxqsets)
111971d10453SEric Joyner {
112071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
112171d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
112271d10453SEric Joyner 	struct ice_rx_queue *rxq;
112371d10453SEric Joyner 	int err, i;
112471d10453SEric Joyner 
112571d10453SEric Joyner 	MPASS(nrxqs == 1);
112671d10453SEric Joyner 	MPASS(sc->scctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
112771d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
112871d10453SEric Joyner 
112971d10453SEric Joyner 	/* Do not bother allocating queues if we're in recovery mode */
113071d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
113171d10453SEric Joyner 		return (0);
113271d10453SEric Joyner 
113371d10453SEric Joyner 	/* Allocate queue structure memory */
113471d10453SEric Joyner 	if (!(vsi->rx_queues =
11358923de59SPiotr Kubaj 	      (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
113671d10453SEric Joyner 		device_printf(sc->dev, "Unable to allocate Rx queue memory\n");
113771d10453SEric Joyner 		return (ENOMEM);
113871d10453SEric Joyner 	}
113971d10453SEric Joyner 
114071d10453SEric Joyner 	/* Assign queues from PF space to the main VSI */
114171d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap, nrxqsets);
114271d10453SEric Joyner 	if (err) {
114371d10453SEric Joyner 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
114471d10453SEric Joyner 			      ice_err_str(err));
114571d10453SEric Joyner 		goto free_rx_queues;
114671d10453SEric Joyner 	}
114771d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
114871d10453SEric Joyner 
114971d10453SEric Joyner 	/* Add Rx queue sysctls context */
115071d10453SEric Joyner 	ice_vsi_add_rxqs_ctx(vsi);
115171d10453SEric Joyner 
115271d10453SEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
115371d10453SEric Joyner 		rxq->me = i;
115471d10453SEric Joyner 		rxq->vsi = vsi;
115571d10453SEric Joyner 
115671d10453SEric Joyner 		/* store the queue size for easier access */
115771d10453SEric Joyner 		rxq->desc_count = sc->scctx->isc_nrxd[0];
115871d10453SEric Joyner 
115971d10453SEric Joyner 		/* get the virtual and physical address of the hardware queues */
116071d10453SEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
116171d10453SEric Joyner 		rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
116271d10453SEric Joyner 		rxq->rx_paddr = paddrs[i];
116371d10453SEric Joyner 
116471d10453SEric Joyner 		ice_add_rxq_sysctls(rxq);
116571d10453SEric Joyner 	}
116671d10453SEric Joyner 
116771d10453SEric Joyner 	vsi->num_rx_queues = nrxqsets;
116871d10453SEric Joyner 
116971d10453SEric Joyner 	return (0);
117071d10453SEric Joyner 
117171d10453SEric Joyner free_rx_queues:
117271d10453SEric Joyner 	free(vsi->rx_queues, M_ICE);
117371d10453SEric Joyner 	vsi->rx_queues = NULL;
117471d10453SEric Joyner 	return err;
117571d10453SEric Joyner }
117671d10453SEric Joyner 
117771d10453SEric Joyner /**
117871d10453SEric Joyner  * ice_if_queues_free - Free queue memory
117971d10453SEric Joyner  * @ctx: the iflib context structure
118071d10453SEric Joyner  *
118171d10453SEric Joyner  * Free queue memory allocated by ice_if_tx_queues_alloc() and
118271d10453SEric Joyner  * ice_if_rx_queues_alloc().
118371d10453SEric Joyner  *
118471d10453SEric Joyner  * There is no guarantee that ice_if_queues_free() and ice_if_detach() will be
118571d10453SEric Joyner  * called in the same order. It's possible for ice_if_queues_free() to be
118671d10453SEric Joyner  * called prior to ice_if_detach(), and vice versa.
118771d10453SEric Joyner  *
118871d10453SEric Joyner  * For this reason, the main VSI is a static member of the ice_softc, which is
118971d10453SEric Joyner  * not free'd until after iflib finishes calling both of these functions.
119071d10453SEric Joyner  *
119171d10453SEric Joyner  * Thus, care must be taken in how we manage the memory being freed by this
119271d10453SEric Joyner  * function, and in what tasks it can and must perform.
119371d10453SEric Joyner  */
119471d10453SEric Joyner static void
ice_if_queues_free(if_ctx_t ctx)119571d10453SEric Joyner ice_if_queues_free(if_ctx_t ctx)
119671d10453SEric Joyner {
119771d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
119871d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
119971d10453SEric Joyner 	struct ice_tx_queue *txq;
120071d10453SEric Joyner 	int i;
120171d10453SEric Joyner 
120271d10453SEric Joyner 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
120371d10453SEric Joyner 	 * pointers. Note, the calls here and those in ice_if_detach()
120471d10453SEric Joyner 	 * are *BOTH* necessary, as we cannot guarantee which path will be
120571d10453SEric Joyner 	 * run first
120671d10453SEric Joyner 	 */
120771d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
120871d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
120971d10453SEric Joyner 
121071d10453SEric Joyner 	/* Release MSI-X IRQ vectors, if not yet released in ice_if_detach */
121171d10453SEric Joyner 	ice_free_irqvs(sc);
121271d10453SEric Joyner 
121371d10453SEric Joyner 	if (vsi->tx_queues != NULL) {
121471d10453SEric Joyner 		/* free the tx_rsq arrays */
121571d10453SEric Joyner 		for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
121671d10453SEric Joyner 			if (txq->tx_rsq != NULL) {
121771d10453SEric Joyner 				free(txq->tx_rsq, M_ICE);
121871d10453SEric Joyner 				txq->tx_rsq = NULL;
121971d10453SEric Joyner 			}
122071d10453SEric Joyner 		}
122171d10453SEric Joyner 		free(vsi->tx_queues, M_ICE);
122271d10453SEric Joyner 		vsi->tx_queues = NULL;
122371d10453SEric Joyner 		vsi->num_tx_queues = 0;
122471d10453SEric Joyner 	}
122571d10453SEric Joyner 	if (vsi->rx_queues != NULL) {
122671d10453SEric Joyner 		free(vsi->rx_queues, M_ICE);
122771d10453SEric Joyner 		vsi->rx_queues = NULL;
122871d10453SEric Joyner 		vsi->num_rx_queues = 0;
122971d10453SEric Joyner 	}
123071d10453SEric Joyner }
123171d10453SEric Joyner 
123271d10453SEric Joyner /**
123371d10453SEric Joyner  * ice_msix_que - Fast interrupt handler for MSI-X receive queues
123471d10453SEric Joyner  * @arg: The Rx queue memory
123571d10453SEric Joyner  *
123671d10453SEric Joyner  * Interrupt filter function for iflib MSI-X interrupts. Called by iflib when
123771d10453SEric Joyner  * an MSI-X interrupt for a given queue is triggered. Currently this just asks
123871d10453SEric Joyner  * iflib to schedule the main Rx thread.
123971d10453SEric Joyner  */
124071d10453SEric Joyner static int
ice_msix_que(void * arg)124171d10453SEric Joyner ice_msix_que(void *arg)
124271d10453SEric Joyner {
124371d10453SEric Joyner 	struct ice_rx_queue __unused *rxq = (struct ice_rx_queue *)arg;
124471d10453SEric Joyner 
124571d10453SEric Joyner 	/* TODO: dynamic ITR algorithm?? */
124671d10453SEric Joyner 
124771d10453SEric Joyner 	return (FILTER_SCHEDULE_THREAD);
124871d10453SEric Joyner }
124971d10453SEric Joyner 
125071d10453SEric Joyner /**
125171d10453SEric Joyner  * ice_msix_admin - Fast interrupt handler for MSI-X admin interrupt
125271d10453SEric Joyner  * @arg: pointer to device softc memory
125371d10453SEric Joyner  *
125471d10453SEric Joyner  * Called by iflib when an administrative interrupt occurs. Should perform any
125571d10453SEric Joyner  * fast logic for handling the interrupt cause, and then indicate whether the
125671d10453SEric Joyner  * admin task needs to be queued.
125771d10453SEric Joyner  */
125871d10453SEric Joyner static int
ice_msix_admin(void * arg)125971d10453SEric Joyner ice_msix_admin(void *arg)
126071d10453SEric Joyner {
126171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)arg;
126271d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
126371d10453SEric Joyner 	device_t dev = sc->dev;
126471d10453SEric Joyner 	u32 oicr;
126571d10453SEric Joyner 
126671d10453SEric Joyner 	/* There is no safe way to modify the enabled miscellaneous causes of
126771d10453SEric Joyner 	 * the OICR vector at runtime, as doing so would be prone to race
126871d10453SEric Joyner 	 * conditions. Reading PFINT_OICR will unmask the associated interrupt
126971d10453SEric Joyner 	 * causes and allow future interrupts to occur. The admin interrupt
127071d10453SEric Joyner 	 * vector will not be re-enabled until after we exit this function,
127171d10453SEric Joyner 	 * but any delayed tasks must be resilient against possible "late
127271d10453SEric Joyner 	 * arrival" interrupts that occur while we're already handling the
127371d10453SEric Joyner 	 * task. This is done by using state bits and serializing these
127471d10453SEric Joyner 	 * delayed tasks via the admin status task function.
127571d10453SEric Joyner 	 */
127671d10453SEric Joyner 	oicr = rd32(hw, PFINT_OICR);
127771d10453SEric Joyner 
127871d10453SEric Joyner 	/* Processing multiple controlq interrupts on a single vector does not
127971d10453SEric Joyner 	 * provide an indication of which controlq triggered the interrupt.
128071d10453SEric Joyner 	 * We might try reading the INTEVENT bit of the respective PFINT_*_CTL
128171d10453SEric Joyner 	 * registers. However, the INTEVENT bit is not guaranteed to be set as
128271d10453SEric Joyner 	 * it gets automatically cleared when the hardware acknowledges the
128371d10453SEric Joyner 	 * interrupt.
128471d10453SEric Joyner 	 *
128571d10453SEric Joyner 	 * This means we don't really have a good indication of whether or
128671d10453SEric Joyner 	 * which controlq triggered this interrupt. We'll just notify the
128771d10453SEric Joyner 	 * admin task that it should check all the controlqs.
128871d10453SEric Joyner 	 */
128971d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
129071d10453SEric Joyner 
129171d10453SEric Joyner 	if (oicr & PFINT_OICR_VFLR_M) {
129271d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_VFLR_PENDING);
129371d10453SEric Joyner 	}
129471d10453SEric Joyner 
129571d10453SEric Joyner 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
129671d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_MDD_PENDING);
129771d10453SEric Joyner 	}
129871d10453SEric Joyner 
129971d10453SEric Joyner 	if (oicr & PFINT_OICR_GRST_M) {
130071d10453SEric Joyner 		u32 reset;
130171d10453SEric Joyner 
130271d10453SEric Joyner 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
130371d10453SEric Joyner 			GLGEN_RSTAT_RESET_TYPE_S;
130471d10453SEric Joyner 
130571d10453SEric Joyner 		if (reset == ICE_RESET_CORER)
130671d10453SEric Joyner 			sc->soft_stats.corer_count++;
130771d10453SEric Joyner 		else if (reset == ICE_RESET_GLOBR)
130871d10453SEric Joyner 			sc->soft_stats.globr_count++;
130971d10453SEric Joyner 		else
131071d10453SEric Joyner 			sc->soft_stats.empr_count++;
131171d10453SEric Joyner 
131271d10453SEric Joyner 		/* There are a couple of bits at play for handling resets.
131371d10453SEric Joyner 		 * First, the ICE_STATE_RESET_OICR_RECV bit is used to
131471d10453SEric Joyner 		 * indicate that the driver has received an OICR with a reset
131571d10453SEric Joyner 		 * bit active, indicating that a CORER/GLOBR/EMPR is about to
131671d10453SEric Joyner 		 * happen. Second, we set hw->reset_ongoing to indicate that
131771d10453SEric Joyner 		 * the hardware is in reset. We will set this back to false as
131871d10453SEric Joyner 		 * soon as the driver has determined that the hardware is out
131971d10453SEric Joyner 		 * of reset.
132071d10453SEric Joyner 		 *
1321b6f9c6aaSGordon Bergling 		 * If the driver wishes to trigger a request, it can set one of
132271d10453SEric Joyner 		 * the ICE_STATE_RESET_*_REQ bits, which will trigger the
132371d10453SEric Joyner 		 * correct type of reset.
132471d10453SEric Joyner 		 */
13259e54973fSEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_RESET_OICR_RECV)) {
132671d10453SEric Joyner 			hw->reset_ongoing = true;
13279e54973fSEric Joyner 			/*
13289e54973fSEric Joyner 			 * During the NVM update process, there is a driver reset and link
13299e54973fSEric Joyner 			 * goes down and then up. The below if-statement prevents a second
13309e54973fSEric Joyner 			 * link flap from occurring in ice_if_init().
13319e54973fSEric Joyner 			 */
13329e54973fSEric Joyner 			if (if_getflags(sc->ifp) & IFF_UP)
13339e54973fSEric Joyner 				ice_set_state(&sc->state, ICE_STATE_FIRST_INIT_LINK);
13349e54973fSEric Joyner 		}
133571d10453SEric Joyner 	}
133671d10453SEric Joyner 
133771d10453SEric Joyner 	if (oicr & PFINT_OICR_ECC_ERR_M) {
133871d10453SEric Joyner 		device_printf(dev, "ECC Error detected!\n");
133971d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
134071d10453SEric Joyner 	}
134171d10453SEric Joyner 
134201fbb869SBartosz Sobczak 	if (oicr & (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M)) {
134301fbb869SBartosz Sobczak 		if (oicr & PFINT_OICR_HMC_ERR_M)
134401fbb869SBartosz Sobczak 			/* Log the HMC errors */
134501fbb869SBartosz Sobczak 			ice_log_hmc_error(hw, dev);
134601fbb869SBartosz Sobczak 		ice_rdma_notify_pe_intr(sc, oicr);
134771d10453SEric Joyner 	}
134871d10453SEric Joyner 
134971d10453SEric Joyner 	if (oicr & PFINT_OICR_PCI_EXCEPTION_M) {
135071d10453SEric Joyner 		device_printf(dev, "PCI Exception detected!\n");
135171d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
135271d10453SEric Joyner 	}
135371d10453SEric Joyner 
135471d10453SEric Joyner 	return (FILTER_SCHEDULE_THREAD);
135571d10453SEric Joyner }
135671d10453SEric Joyner 
135771d10453SEric Joyner /**
135871d10453SEric Joyner  * ice_allocate_msix - Allocate MSI-X vectors for the interface
135971d10453SEric Joyner  * @sc: the device private softc
136071d10453SEric Joyner  *
136171d10453SEric Joyner  * Map the MSI-X bar, and then request MSI-X vectors in a two-stage process.
136271d10453SEric Joyner  *
136371d10453SEric Joyner  * First, determine a suitable total number of vectors based on the number
136471d10453SEric Joyner  * of CPUs, RSS buckets, the administrative vector, and other demands such as
136571d10453SEric Joyner  * RDMA.
136671d10453SEric Joyner  *
136771d10453SEric Joyner  * Request the desired amount of vectors, and see how many we obtain. If we
136871d10453SEric Joyner  * don't obtain as many as desired, reduce the demands by lowering the number
136971d10453SEric Joyner  * of requested queues or reducing the demand from other features such as
137071d10453SEric Joyner  * RDMA.
137171d10453SEric Joyner  *
137271d10453SEric Joyner  * @remark This function is required because the driver sets the
137371d10453SEric Joyner  * IFLIB_SKIP_MSIX flag indicating that the driver will manage MSI-X vectors
137471d10453SEric Joyner  * manually.
137571d10453SEric Joyner  *
137671d10453SEric Joyner  * @remark This driver will only use MSI-X vectors. If this is not possible,
137771d10453SEric Joyner  * neither MSI or legacy interrupts will be tried.
137871d10453SEric Joyner  *
13799e54973fSEric Joyner  * @remark if it exists, os_imgr is initialized here for keeping track of
13809e54973fSEric Joyner  * the assignments of extra MSIX vectors.
13819e54973fSEric Joyner  *
138271d10453SEric Joyner  * @post on success this function must set the following scctx parameters:
138371d10453SEric Joyner  * isc_vectors, isc_nrxqsets, isc_ntxqsets, and isc_intr.
138471d10453SEric Joyner  *
138571d10453SEric Joyner  * @returns zero on success or an error code on failure.
138671d10453SEric Joyner  */
138771d10453SEric Joyner static int
ice_allocate_msix(struct ice_softc * sc)138871d10453SEric Joyner ice_allocate_msix(struct ice_softc *sc)
138971d10453SEric Joyner {
139071d10453SEric Joyner 	bool iflib_override_queue_count = false;
139171d10453SEric Joyner 	if_softc_ctx_t scctx = sc->scctx;
139271d10453SEric Joyner 	device_t dev = sc->dev;
139371d10453SEric Joyner 	cpuset_t cpus;
139471d10453SEric Joyner 	int bar, queues, vectors, requested;
139571d10453SEric Joyner 	int err = 0;
13968a13362dSEric Joyner 	int rdma;
139771d10453SEric Joyner 
139871d10453SEric Joyner 	/* Allocate the MSI-X bar */
139971d10453SEric Joyner 	bar = scctx->isc_msix_bar;
140071d10453SEric Joyner 	sc->msix_table = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar, RF_ACTIVE);
140171d10453SEric Joyner 	if (!sc->msix_table) {
140271d10453SEric Joyner 		device_printf(dev, "Unable to map MSI-X table\n");
140371d10453SEric Joyner 		return (ENOMEM);
140471d10453SEric Joyner 	}
140571d10453SEric Joyner 
140671d10453SEric Joyner 	/* Check if the iflib queue count sysctls have been set */
140771d10453SEric Joyner 	if (sc->ifc_sysctl_ntxqs || sc->ifc_sysctl_nrxqs)
140871d10453SEric Joyner 		iflib_override_queue_count = true;
140971d10453SEric Joyner 
141071d10453SEric Joyner 	err = bus_get_cpus(dev, INTR_CPUS, sizeof(cpus), &cpus);
141171d10453SEric Joyner 	if (err) {
141271d10453SEric Joyner 		device_printf(dev, "%s: Unable to fetch the CPU list: %s\n",
141371d10453SEric Joyner 			      __func__, ice_err_str(err));
141471d10453SEric Joyner 		CPU_COPY(&all_cpus, &cpus);
141571d10453SEric Joyner 	}
141671d10453SEric Joyner 
141771d10453SEric Joyner 	/* Attempt to mimic behavior of iflib_msix_init */
141871d10453SEric Joyner 	if (iflib_override_queue_count) {
141971d10453SEric Joyner 		/*
142071d10453SEric Joyner 		 * If the override sysctls have been set, limit the queues to
142171d10453SEric Joyner 		 * the number of logical CPUs.
142271d10453SEric Joyner 		 */
142371d10453SEric Joyner 		queues = mp_ncpus;
142471d10453SEric Joyner 	} else {
142571d10453SEric Joyner 		/*
142671d10453SEric Joyner 		 * Otherwise, limit the queue count to the CPUs associated
142771d10453SEric Joyner 		 * with the NUMA node the device is associated with.
142871d10453SEric Joyner 		 */
142971d10453SEric Joyner 		queues = CPU_COUNT(&cpus);
143071d10453SEric Joyner 	}
143171d10453SEric Joyner 
143271d10453SEric Joyner 	/* Clamp to the number of RSS buckets */
143371d10453SEric Joyner 	queues = imin(queues, rss_getnumbuckets());
143471d10453SEric Joyner 
143571d10453SEric Joyner 	/*
143671d10453SEric Joyner 	 * Clamp the number of queue pairs to the minimum of the requested Tx
143771d10453SEric Joyner 	 * and Rx queues.
143871d10453SEric Joyner 	 */
143971d10453SEric Joyner 	queues = imin(queues, sc->ifc_sysctl_ntxqs ?: scctx->isc_ntxqsets);
144071d10453SEric Joyner 	queues = imin(queues, sc->ifc_sysctl_nrxqs ?: scctx->isc_nrxqsets);
144171d10453SEric Joyner 
14428a13362dSEric Joyner 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RDMA)) {
14438a13362dSEric Joyner 		/*
14448a13362dSEric Joyner 		 * Choose a number of RDMA vectors based on the number of CPUs
14458a13362dSEric Joyner 		 * up to a maximum
14468a13362dSEric Joyner 		 */
14478a13362dSEric Joyner 		rdma = min(CPU_COUNT(&cpus), ICE_RDMA_MAX_MSIX);
14488a13362dSEric Joyner 
14498a13362dSEric Joyner 		/* Further limit by the user configurable tunable */
14508a13362dSEric Joyner 		rdma = min(rdma, ice_rdma_max_msix);
14518a13362dSEric Joyner 	} else {
14528a13362dSEric Joyner 		rdma = 0;
14538a13362dSEric Joyner 	}
14548a13362dSEric Joyner 
145571d10453SEric Joyner 	/*
145671d10453SEric Joyner 	 * Determine the number of vectors to request. Note that we also need
145771d10453SEric Joyner 	 * to allocate one vector for administrative tasks.
145871d10453SEric Joyner 	 */
14598a13362dSEric Joyner 	requested = rdma + queues + 1;
14609e54973fSEric Joyner 	/* Add extra vectors requested by the user for later subinterface
14619e54973fSEric Joyner 	 * creation.
14629e54973fSEric Joyner 	 */
14639e54973fSEric Joyner 	if_ctx_t ctx = sc->ctx;
14649e54973fSEric Joyner 	u32 extra_vectors = iflib_get_extra_msix_vectors_sysctl(ctx);
14659e54973fSEric Joyner 	requested += extra_vectors;
146671d10453SEric Joyner 
146771d10453SEric Joyner 	vectors = requested;
146871d10453SEric Joyner 	err = pci_alloc_msix(dev, &vectors);
146971d10453SEric Joyner 	if (err) {
147071d10453SEric Joyner 		device_printf(dev, "Failed to allocate %d MSI-X vectors, err %s\n",
147171d10453SEric Joyner 			      vectors, ice_err_str(err));
147271d10453SEric Joyner 		goto err_free_msix_table;
147371d10453SEric Joyner 	}
147471d10453SEric Joyner 
147571d10453SEric Joyner 	/* If we don't receive enough vectors, reduce demands */
147671d10453SEric Joyner 	if (vectors < requested) {
147771d10453SEric Joyner 		int diff = requested - vectors;
147871d10453SEric Joyner 
147971d10453SEric Joyner 		device_printf(dev, "Requested %d MSI-X vectors, but got only %d\n",
148071d10453SEric Joyner 			      requested, vectors);
148171d10453SEric Joyner 
14829e54973fSEric Joyner 		diff += extra_vectors;
14839e54973fSEric Joyner 		extra_vectors = 0;
148471d10453SEric Joyner 		/*
14858a13362dSEric Joyner 		 * The OS didn't grant us the requested number of vectors.
14868a13362dSEric Joyner 		 * Check to see if we can reduce demands by limiting the
14878a13362dSEric Joyner 		 * number of vectors allocated to certain features.
14888a13362dSEric Joyner 		 */
14898a13362dSEric Joyner 
14908a13362dSEric Joyner 		if (rdma >= diff) {
14918a13362dSEric Joyner 			/* Reduce the number of RDMA vectors we reserve */
14928a13362dSEric Joyner 			rdma -= diff;
14938a13362dSEric Joyner 			diff = 0;
14948a13362dSEric Joyner 		} else {
14958a13362dSEric Joyner 			/* Disable RDMA and reduce the difference */
14968a13362dSEric Joyner 			ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
14978a13362dSEric Joyner 			diff -= rdma;
14988a13362dSEric Joyner 			rdma = 0;
14998a13362dSEric Joyner 		}
15008a13362dSEric Joyner 
15018a13362dSEric Joyner 		/*
150271d10453SEric Joyner 		 * If we still have a difference, we need to reduce the number
150371d10453SEric Joyner 		 * of queue pairs.
150471d10453SEric Joyner 		 *
150571d10453SEric Joyner 		 * However, we still need at least one vector for the admin
150671d10453SEric Joyner 		 * interrupt and one queue pair.
150771d10453SEric Joyner 		 */
150871d10453SEric Joyner 		if (queues <= diff) {
150971d10453SEric Joyner 			device_printf(dev, "Unable to allocate sufficient MSI-X vectors\n");
151071d10453SEric Joyner 			err = (ERANGE);
151171d10453SEric Joyner 			goto err_pci_release_msi;
151271d10453SEric Joyner 		}
151371d10453SEric Joyner 
151471d10453SEric Joyner 		queues -= diff;
151571d10453SEric Joyner 	}
151671d10453SEric Joyner 
151771d10453SEric Joyner 	device_printf(dev, "Using %d Tx and Rx queues\n", queues);
15188a13362dSEric Joyner 	if (rdma)
15198a13362dSEric Joyner 		device_printf(dev, "Reserving %d MSI-X interrupts for iRDMA\n",
15208a13362dSEric Joyner 			      rdma);
152171d10453SEric Joyner 	device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
152271d10453SEric Joyner 		      vectors);
152371d10453SEric Joyner 
15249e54973fSEric Joyner 	/* Split resulting vectors back into requested splits */
152571d10453SEric Joyner 	scctx->isc_vectors = vectors;
152671d10453SEric Joyner 	scctx->isc_nrxqsets = queues;
152771d10453SEric Joyner 	scctx->isc_ntxqsets = queues;
152871d10453SEric Joyner 	scctx->isc_intr = IFLIB_INTR_MSIX;
152971d10453SEric Joyner 
15308a13362dSEric Joyner 	sc->irdma_vectors = rdma;
15318a13362dSEric Joyner 
153271d10453SEric Joyner 	/* Interrupt allocation tracking isn't required in recovery mode,
153371d10453SEric Joyner 	 * since neither RDMA nor VFs are enabled.
153471d10453SEric Joyner 	 */
153571d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
153671d10453SEric Joyner 		return (0);
153771d10453SEric Joyner 
153871d10453SEric Joyner 	/* Keep track of which interrupt indices are being used for what */
15398a13362dSEric Joyner 	sc->lan_vectors = vectors - rdma;
15409e54973fSEric Joyner 	sc->lan_vectors -= extra_vectors;
15419e54973fSEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->dev_imgr, sc->pf_imap, sc->lan_vectors);
154271d10453SEric Joyner 	if (err) {
154371d10453SEric Joyner 		device_printf(dev, "Unable to assign PF interrupt mapping: %s\n",
154471d10453SEric Joyner 			      ice_err_str(err));
154571d10453SEric Joyner 		goto err_pci_release_msi;
154671d10453SEric Joyner 	}
15479e54973fSEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->dev_imgr, sc->rdma_imap, rdma);
15488a13362dSEric Joyner 	if (err) {
15498a13362dSEric Joyner 		device_printf(dev, "Unable to assign PF RDMA interrupt mapping: %s\n",
15508a13362dSEric Joyner 			      ice_err_str(err));
15519e54973fSEric Joyner 		goto err_release_pf_imap;
15528a13362dSEric Joyner 	}
15539e54973fSEric Joyner 	sc->extra_vectors = extra_vectors;
15549e54973fSEric Joyner 	/* Setup another resource manager to track the assignments of extra OS
15559e54973fSEric Joyner 	 * vectors. These OS interrupt allocations don't need to be contiguous,
15569e54973fSEric Joyner 	 * unlike the ones that come from the device.
15579e54973fSEric Joyner 	 */
15589e54973fSEric Joyner 	err = ice_resmgr_init(&sc->os_imgr, sc->extra_vectors);
15599e54973fSEric Joyner 	if (err) {
15609e54973fSEric Joyner 		device_printf(dev, "Unable to initialize OS extra interrupt manager: %s\n",
15619e54973fSEric Joyner 			      ice_err_str(err));
15629e54973fSEric Joyner 		ice_resmgr_release_map(&sc->dev_imgr, sc->rdma_imap,
15639e54973fSEric Joyner 					    rdma);
15649e54973fSEric Joyner 		goto err_release_pf_imap;
15659e54973fSEric Joyner 	}
156671d10453SEric Joyner 	return (0);
156771d10453SEric Joyner 
15689e54973fSEric Joyner err_release_pf_imap:
15699e54973fSEric Joyner 	ice_resmgr_release_map(&sc->dev_imgr, sc->pf_imap,
15709e54973fSEric Joyner 				    sc->lan_vectors);
157171d10453SEric Joyner err_pci_release_msi:
157271d10453SEric Joyner 	pci_release_msi(dev);
157371d10453SEric Joyner err_free_msix_table:
157471d10453SEric Joyner 	if (sc->msix_table != NULL) {
157571d10453SEric Joyner 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
157671d10453SEric Joyner 				rman_get_rid(sc->msix_table),
157771d10453SEric Joyner 				sc->msix_table);
157871d10453SEric Joyner 		sc->msix_table = NULL;
157971d10453SEric Joyner 	}
158071d10453SEric Joyner 
158171d10453SEric Joyner 	return (err);
158271d10453SEric Joyner }
158371d10453SEric Joyner 
158471d10453SEric Joyner /**
158571d10453SEric Joyner  * ice_if_msix_intr_assign - Assign MSI-X interrupt vectors to queues
158671d10453SEric Joyner  * @ctx: the iflib context structure
158771d10453SEric Joyner  * @msix: the number of vectors we were assigned
158871d10453SEric Joyner  *
158971d10453SEric Joyner  * Called by iflib to assign MSI-X vectors to queues. Currently requires that
159071d10453SEric Joyner  * we get at least the same number of vectors as we have queues, and that we
159171d10453SEric Joyner  * always have the same number of Tx and Rx queues.
159271d10453SEric Joyner  *
159371d10453SEric Joyner  * Tx queues use a softirq instead of using their own hardware interrupt.
159471d10453SEric Joyner  */
159571d10453SEric Joyner static int
ice_if_msix_intr_assign(if_ctx_t ctx,int msix)159671d10453SEric Joyner ice_if_msix_intr_assign(if_ctx_t ctx, int msix)
159771d10453SEric Joyner {
159871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
159971d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
160071d10453SEric Joyner 	int err, i, vector;
160171d10453SEric Joyner 
160271d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
160371d10453SEric Joyner 
160471d10453SEric Joyner 	if (vsi->num_rx_queues != vsi->num_tx_queues) {
160571d10453SEric Joyner 		device_printf(sc->dev,
160671d10453SEric 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",
160771d10453SEric Joyner 			      vsi->num_tx_queues, vsi->num_rx_queues);
160871d10453SEric Joyner 		return (EOPNOTSUPP);
160971d10453SEric Joyner 	}
161071d10453SEric Joyner 
161171d10453SEric Joyner 	if (msix < (vsi->num_rx_queues + 1)) {
161271d10453SEric Joyner 		device_printf(sc->dev,
161371d10453SEric Joyner 			      "Not enough MSI-X vectors to assign one vector to each queue pair\n");
161471d10453SEric Joyner 		return (EOPNOTSUPP);
161571d10453SEric Joyner 	}
161671d10453SEric Joyner 
161771d10453SEric Joyner 	/* Save the number of vectors for future use */
161871d10453SEric Joyner 	sc->num_irq_vectors = vsi->num_rx_queues + 1;
161971d10453SEric Joyner 
162071d10453SEric Joyner 	/* Allocate space to store the IRQ vector data */
162171d10453SEric Joyner 	if (!(sc->irqvs =
162271d10453SEric Joyner 	      (struct ice_irq_vector *) malloc(sizeof(struct ice_irq_vector) * (sc->num_irq_vectors),
162371d10453SEric Joyner 					       M_ICE, M_NOWAIT))) {
162471d10453SEric Joyner 		device_printf(sc->dev,
162571d10453SEric Joyner 			      "Unable to allocate irqv memory\n");
162671d10453SEric Joyner 		return (ENOMEM);
162771d10453SEric Joyner 	}
162871d10453SEric Joyner 
162971d10453SEric Joyner 	/* Administrative interrupt events will use vector 0 */
163071d10453SEric Joyner 	err = iflib_irq_alloc_generic(ctx, &sc->irqvs[0].irq, 1, IFLIB_INTR_ADMIN,
163171d10453SEric Joyner 				      ice_msix_admin, sc, 0, "admin");
163271d10453SEric Joyner 	if (err) {
163371d10453SEric Joyner 		device_printf(sc->dev,
163471d10453SEric Joyner 			      "Failed to register Admin queue handler: %s\n",
163571d10453SEric Joyner 			      ice_err_str(err));
163671d10453SEric Joyner 		goto free_irqvs;
163771d10453SEric Joyner 	}
163871d10453SEric Joyner 	sc->irqvs[0].me = 0;
163971d10453SEric Joyner 
164071d10453SEric Joyner 	/* Do not allocate queue interrupts when in recovery mode */
164171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
164271d10453SEric Joyner 		return (0);
164371d10453SEric Joyner 
16449e54973fSEric Joyner 	int rid;
164571d10453SEric Joyner 	for (i = 0, vector = 1; i < vsi->num_rx_queues; i++, vector++) {
164671d10453SEric Joyner 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
164771d10453SEric Joyner 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
164871d10453SEric Joyner 		char irq_name[16];
164971d10453SEric Joyner 
16509e54973fSEric Joyner 		rid = vector + 1;
16519e54973fSEric Joyner 
165271d10453SEric Joyner 		snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
165371d10453SEric Joyner 		err = iflib_irq_alloc_generic(ctx, &sc->irqvs[vector].irq, rid,
165481be6552SMatt Macy 					      IFLIB_INTR_RXTX, ice_msix_que,
165571d10453SEric Joyner 					      rxq, rxq->me, irq_name);
165671d10453SEric Joyner 		if (err) {
165771d10453SEric Joyner 			device_printf(sc->dev,
165871d10453SEric Joyner 				      "Failed to allocate q int %d err: %s\n",
165971d10453SEric Joyner 				      i, ice_err_str(err));
166071d10453SEric Joyner 			vector--;
166171d10453SEric Joyner 			i--;
166271d10453SEric Joyner 			goto fail;
166371d10453SEric Joyner 		}
166471d10453SEric Joyner 		sc->irqvs[vector].me = vector;
166571d10453SEric Joyner 		rxq->irqv = &sc->irqvs[vector];
166671d10453SEric Joyner 
166771d10453SEric Joyner 		bzero(irq_name, sizeof(irq_name));
166871d10453SEric Joyner 
166971d10453SEric Joyner 		snprintf(irq_name, sizeof(irq_name), "txq%d", i);
167071d10453SEric Joyner 		iflib_softirq_alloc_generic(ctx, &sc->irqvs[vector].irq,
167171d10453SEric Joyner 					    IFLIB_INTR_TX, txq,
167271d10453SEric Joyner 					    txq->me, irq_name);
167371d10453SEric Joyner 		txq->irqv = &sc->irqvs[vector];
167471d10453SEric Joyner 	}
167571d10453SEric Joyner 
16769e54973fSEric Joyner 	/* For future interrupt assignments */
16779e54973fSEric Joyner 	sc->last_rid = rid + sc->irdma_vectors;
16789e54973fSEric Joyner 
167971d10453SEric Joyner 	return (0);
168071d10453SEric Joyner fail:
168171d10453SEric Joyner 	for (; i >= 0; i--, vector--)
168271d10453SEric Joyner 		iflib_irq_free(ctx, &sc->irqvs[vector].irq);
168371d10453SEric Joyner 	iflib_irq_free(ctx, &sc->irqvs[0].irq);
168471d10453SEric Joyner free_irqvs:
168571d10453SEric Joyner 	free(sc->irqvs, M_ICE);
168671d10453SEric Joyner 	sc->irqvs = NULL;
168771d10453SEric Joyner 	return err;
168871d10453SEric Joyner }
168971d10453SEric Joyner 
169071d10453SEric Joyner /**
169171d10453SEric Joyner  * ice_if_mtu_set - Set the device MTU
169271d10453SEric Joyner  * @ctx: iflib context structure
169371d10453SEric Joyner  * @mtu: the MTU requested
169471d10453SEric Joyner  *
169571d10453SEric Joyner  * Called by iflib to configure the device's Maximum Transmission Unit (MTU).
169671d10453SEric Joyner  *
169771d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
169871d10453SEric Joyner  */
169971d10453SEric Joyner static int
ice_if_mtu_set(if_ctx_t ctx,uint32_t mtu)170071d10453SEric Joyner ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
170171d10453SEric Joyner {
170271d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
170371d10453SEric Joyner 
170471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
170571d10453SEric Joyner 
170671d10453SEric Joyner 	/* Do not support configuration when in recovery mode */
170771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
170871d10453SEric Joyner 		return (ENOSYS);
170971d10453SEric Joyner 
171071d10453SEric Joyner 	if (mtu < ICE_MIN_MTU || mtu > ICE_MAX_MTU)
171171d10453SEric Joyner 		return (EINVAL);
171271d10453SEric Joyner 
171371d10453SEric Joyner 	sc->scctx->isc_max_frame_size = mtu +
171471d10453SEric Joyner 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
171571d10453SEric Joyner 
171671d10453SEric Joyner 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
171771d10453SEric Joyner 
171871d10453SEric Joyner 	return (0);
171971d10453SEric Joyner }
172071d10453SEric Joyner 
172171d10453SEric Joyner /**
172271d10453SEric Joyner  * ice_if_intr_enable - Enable device interrupts
172371d10453SEric Joyner  * @ctx: iflib context structure
172471d10453SEric Joyner  *
172571d10453SEric Joyner  * Called by iflib to request enabling device interrupts.
172671d10453SEric Joyner  */
172771d10453SEric Joyner static void
ice_if_intr_enable(if_ctx_t ctx)172871d10453SEric Joyner ice_if_intr_enable(if_ctx_t ctx)
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 	ASSERT_CTX_LOCKED(sc);
173571d10453SEric Joyner 
173671d10453SEric Joyner 	/* Enable ITR 0 */
173771d10453SEric Joyner 	ice_enable_intr(hw, sc->irqvs[0].me);
173871d10453SEric Joyner 
173971d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
174071d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
174171d10453SEric Joyner 		return;
174271d10453SEric Joyner 
174371d10453SEric Joyner 	/* Enable all queue interrupts */
174471d10453SEric Joyner 	for (int i = 0; i < vsi->num_rx_queues; i++)
174571d10453SEric Joyner 		ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
174671d10453SEric Joyner }
174771d10453SEric Joyner 
174871d10453SEric Joyner /**
174971d10453SEric Joyner  * ice_if_intr_disable - Disable device interrupts
175071d10453SEric Joyner  * @ctx: iflib context structure
175171d10453SEric Joyner  *
175271d10453SEric Joyner  * Called by iflib to request disabling device interrupts.
175371d10453SEric Joyner  */
175471d10453SEric Joyner static void
ice_if_intr_disable(if_ctx_t ctx)175571d10453SEric Joyner ice_if_intr_disable(if_ctx_t ctx)
175671d10453SEric Joyner {
175771d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
175871d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
175971d10453SEric Joyner 	unsigned int i;
176071d10453SEric Joyner 
176171d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
176271d10453SEric Joyner 
176371d10453SEric Joyner 	/* IFDI_INTR_DISABLE may be called prior to interrupts actually being
176471d10453SEric Joyner 	 * assigned to queues. Instead of assuming that the interrupt
176571d10453SEric Joyner 	 * assignment in the rx_queues structure is valid, just disable all
176671d10453SEric Joyner 	 * possible interrupts
176771d10453SEric Joyner 	 *
176871d10453SEric Joyner 	 * Note that we choose not to disable ITR 0 because this handles the
176971d10453SEric Joyner 	 * AdminQ interrupts, and we want to keep processing these even when
177071d10453SEric Joyner 	 * the interface is offline.
177171d10453SEric Joyner 	 */
177271d10453SEric Joyner 	for (i = 1; i < hw->func_caps.common_cap.num_msix_vectors; i++)
177371d10453SEric Joyner 		ice_disable_intr(hw, i);
177471d10453SEric Joyner }
177571d10453SEric Joyner 
177671d10453SEric Joyner /**
177771d10453SEric Joyner  * ice_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
177871d10453SEric Joyner  * @ctx: iflib context structure
177971d10453SEric Joyner  * @rxqid: the Rx queue to enable
178071d10453SEric Joyner  *
178171d10453SEric Joyner  * Enable a specific Rx queue interrupt.
178271d10453SEric Joyner  *
178371d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
178471d10453SEric Joyner  */
178571d10453SEric Joyner static int
ice_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)178671d10453SEric Joyner ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
178771d10453SEric Joyner {
178871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
178971d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
179071d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
179171d10453SEric Joyner 
179271d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
179371d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
179471d10453SEric Joyner 		return (ENOSYS);
179571d10453SEric Joyner 
179671d10453SEric Joyner 	ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
179771d10453SEric Joyner 	return (0);
179871d10453SEric Joyner }
179971d10453SEric Joyner 
180071d10453SEric Joyner /**
180171d10453SEric Joyner  * ice_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
180271d10453SEric Joyner  * @ctx: iflib context structure
180371d10453SEric Joyner  * @txqid: the Tx queue to enable
180471d10453SEric Joyner  *
180571d10453SEric Joyner  * Enable a specific Tx queue interrupt.
180671d10453SEric Joyner  *
180771d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
180871d10453SEric Joyner  */
180971d10453SEric Joyner static int
ice_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)181071d10453SEric Joyner ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
181171d10453SEric Joyner {
181271d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
181371d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
181471d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
181571d10453SEric Joyner 
181671d10453SEric Joyner 	/* Do not enable queue interrupts in recovery mode */
181771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
181871d10453SEric Joyner 		return (ENOSYS);
181971d10453SEric Joyner 
182071d10453SEric Joyner 	ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
182171d10453SEric Joyner 	return (0);
182271d10453SEric Joyner }
182371d10453SEric Joyner 
182471d10453SEric Joyner /**
18259c30461dSEric Joyner  * ice_set_default_promisc_mask - Set default config for promisc settings
18269c30461dSEric Joyner  * @promisc_mask: bitmask to setup
18279c30461dSEric Joyner  *
18289c30461dSEric Joyner  * The ice_(set|clear)_vsi_promisc() function expects a mask of promiscuous
18299c30461dSEric Joyner  * modes to operate on. The mask used in here is the default one for the
18309c30461dSEric Joyner  * driver, where promiscuous is enabled/disabled for all types of
18319c30461dSEric Joyner  * non-VLAN-tagged/VLAN 0 traffic.
18329c30461dSEric Joyner  */
18339c30461dSEric Joyner static void
ice_set_default_promisc_mask(ice_bitmap_t * promisc_mask)18349c30461dSEric Joyner ice_set_default_promisc_mask(ice_bitmap_t *promisc_mask)
18359c30461dSEric Joyner {
18369c30461dSEric Joyner 	ice_zero_bitmap(promisc_mask, ICE_PROMISC_MAX);
18379c30461dSEric Joyner 	ice_set_bit(ICE_PROMISC_UCAST_TX, promisc_mask);
18389c30461dSEric Joyner 	ice_set_bit(ICE_PROMISC_UCAST_RX, promisc_mask);
18399c30461dSEric Joyner 	ice_set_bit(ICE_PROMISC_MCAST_TX, promisc_mask);
18409c30461dSEric Joyner 	ice_set_bit(ICE_PROMISC_MCAST_RX, promisc_mask);
18419c30461dSEric Joyner }
18429c30461dSEric Joyner 
18439c30461dSEric Joyner /**
184471d10453SEric Joyner  * ice_if_promisc_set - Set device promiscuous mode
184571d10453SEric Joyner  * @ctx: iflib context structure
184671d10453SEric Joyner  * @flags: promiscuous flags to configure
184771d10453SEric Joyner  *
184871d10453SEric Joyner  * Called by iflib to configure device promiscuous mode.
184971d10453SEric Joyner  *
185071d10453SEric Joyner  * @remark Calls to this function will always overwrite the previous setting
185171d10453SEric Joyner  */
185271d10453SEric Joyner static int
ice_if_promisc_set(if_ctx_t ctx,int flags)185371d10453SEric Joyner ice_if_promisc_set(if_ctx_t ctx, int flags)
185471d10453SEric Joyner {
185571d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
185671d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
185771d10453SEric Joyner 	device_t dev = sc->dev;
1858f2635e84SEric Joyner 	int status;
185971d10453SEric Joyner 	bool promisc_enable = flags & IFF_PROMISC;
186071d10453SEric Joyner 	bool multi_enable = flags & IFF_ALLMULTI;
18619c30461dSEric Joyner 	ice_declare_bitmap(promisc_mask, ICE_PROMISC_MAX);
186271d10453SEric Joyner 
186371d10453SEric Joyner 	/* Do not support configuration when in recovery mode */
186471d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
186571d10453SEric Joyner 		return (ENOSYS);
186671d10453SEric Joyner 
18679c30461dSEric Joyner 	ice_set_default_promisc_mask(promisc_mask);
18689c30461dSEric Joyner 
186971d10453SEric Joyner 	if (multi_enable)
187071d10453SEric Joyner 		return (EOPNOTSUPP);
187171d10453SEric Joyner 
187271d10453SEric Joyner 	if (promisc_enable) {
187371d10453SEric Joyner 		status = ice_set_vsi_promisc(hw, sc->pf_vsi.idx,
18749c30461dSEric Joyner 					     promisc_mask, 0);
187571d10453SEric Joyner 		if (status && status != ICE_ERR_ALREADY_EXISTS) {
187671d10453SEric Joyner 			device_printf(dev,
187771d10453SEric Joyner 				      "Failed to enable promiscuous mode for PF VSI, err %s aq_err %s\n",
187871d10453SEric Joyner 				      ice_status_str(status),
187971d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
188071d10453SEric Joyner 			return (EIO);
188171d10453SEric Joyner 		}
188271d10453SEric Joyner 	} else {
188371d10453SEric Joyner 		status = ice_clear_vsi_promisc(hw, sc->pf_vsi.idx,
18849c30461dSEric Joyner 					       promisc_mask, 0);
188571d10453SEric Joyner 		if (status) {
188671d10453SEric Joyner 			device_printf(dev,
188771d10453SEric Joyner 				      "Failed to disable promiscuous mode for PF VSI, err %s aq_err %s\n",
188871d10453SEric Joyner 				      ice_status_str(status),
188971d10453SEric Joyner 				      ice_aq_str(hw->adminq.sq_last_status));
189071d10453SEric Joyner 			return (EIO);
189171d10453SEric Joyner 		}
189271d10453SEric Joyner 	}
189371d10453SEric Joyner 
189471d10453SEric Joyner 	return (0);
189571d10453SEric Joyner }
189671d10453SEric Joyner 
189771d10453SEric Joyner /**
189871d10453SEric Joyner  * ice_if_media_change - Change device media
189971d10453SEric Joyner  * @ctx: device ctx structure
190071d10453SEric Joyner  *
190171d10453SEric Joyner  * Called by iflib when a media change is requested. This operation is not
190271d10453SEric Joyner  * supported by the hardware, so we just return an error code.
190371d10453SEric Joyner  */
190471d10453SEric Joyner static int
ice_if_media_change(if_ctx_t ctx)190571d10453SEric Joyner ice_if_media_change(if_ctx_t ctx)
190671d10453SEric Joyner {
190771d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
190871d10453SEric Joyner 
190971d10453SEric Joyner 	device_printf(sc->dev, "Media change is not supported.\n");
191071d10453SEric Joyner 	return (ENODEV);
191171d10453SEric Joyner }
191271d10453SEric Joyner 
191371d10453SEric Joyner /**
191471d10453SEric Joyner  * ice_if_media_status - Report current device media
191571d10453SEric Joyner  * @ctx: iflib context structure
191671d10453SEric Joyner  * @ifmr: ifmedia request structure to update
191771d10453SEric Joyner  *
191871d10453SEric Joyner  * Updates the provided ifmr with current device media status, including link
191971d10453SEric Joyner  * status and media type.
192071d10453SEric Joyner  */
192171d10453SEric Joyner static void
ice_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)192271d10453SEric Joyner ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
192371d10453SEric Joyner {
192471d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
192571d10453SEric Joyner 	struct ice_link_status *li = &sc->hw.port_info->phy.link_info;
192671d10453SEric Joyner 
192771d10453SEric Joyner 	ifmr->ifm_status = IFM_AVALID;
192871d10453SEric Joyner 	ifmr->ifm_active = IFM_ETHER;
192971d10453SEric Joyner 
193071d10453SEric Joyner 	/* Never report link up or media types when in recovery mode */
193171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
193271d10453SEric Joyner 		return;
193371d10453SEric Joyner 
193471d10453SEric Joyner 	if (!sc->link_up)
193571d10453SEric Joyner 		return;
193671d10453SEric Joyner 
193771d10453SEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
193871d10453SEric Joyner 	ifmr->ifm_active |= IFM_FDX;
193971d10453SEric Joyner 
194071d10453SEric Joyner 	if (li->phy_type_low)
194171d10453SEric Joyner 		ifmr->ifm_active |= ice_get_phy_type_low(li->phy_type_low);
194271d10453SEric Joyner 	else if (li->phy_type_high)
194371d10453SEric Joyner 		ifmr->ifm_active |= ice_get_phy_type_high(li->phy_type_high);
194471d10453SEric Joyner 	else
194571d10453SEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
194671d10453SEric Joyner 
194771d10453SEric Joyner 	/* Report flow control status as well */
194871d10453SEric Joyner 	if (li->an_info & ICE_AQ_LINK_PAUSE_TX)
194971d10453SEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
195071d10453SEric Joyner 	if (li->an_info & ICE_AQ_LINK_PAUSE_RX)
195171d10453SEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
195271d10453SEric Joyner }
195371d10453SEric Joyner 
195471d10453SEric Joyner /**
195571d10453SEric Joyner  * ice_init_tx_tracking - Initialize Tx queue software tracking values
195671d10453SEric Joyner  * @vsi: the VSI to initialize
195771d10453SEric Joyner  *
195871d10453SEric Joyner  * Initialize Tx queue software tracking values, including the Report Status
195971d10453SEric Joyner  * queue, and related software tracking values.
196071d10453SEric Joyner  */
196171d10453SEric Joyner static void
ice_init_tx_tracking(struct ice_vsi * vsi)196271d10453SEric Joyner ice_init_tx_tracking(struct ice_vsi *vsi)
196371d10453SEric Joyner {
196471d10453SEric Joyner 	struct ice_tx_queue *txq;
196571d10453SEric Joyner 	size_t j;
196671d10453SEric Joyner 	int i;
196771d10453SEric Joyner 
196871d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
196971d10453SEric Joyner 
197071d10453SEric Joyner 		txq->tx_rs_cidx = txq->tx_rs_pidx = 0;
197171d10453SEric Joyner 
197271d10453SEric Joyner 		/* Initialize the last processed descriptor to be the end of
197371d10453SEric Joyner 		 * the ring, rather than the start, so that we avoid an
197471d10453SEric Joyner 		 * off-by-one error in ice_ift_txd_credits_update for the
197571d10453SEric Joyner 		 * first packet.
197671d10453SEric Joyner 		 */
197771d10453SEric Joyner 		txq->tx_cidx_processed = txq->desc_count - 1;
197871d10453SEric Joyner 
197971d10453SEric Joyner 		for (j = 0; j < txq->desc_count; j++)
198071d10453SEric Joyner 			txq->tx_rsq[j] = QIDX_INVALID;
198171d10453SEric Joyner 	}
198271d10453SEric Joyner }
198371d10453SEric Joyner 
198471d10453SEric Joyner /**
198571d10453SEric Joyner  * ice_update_rx_mbuf_sz - Update the Rx buffer size for all queues
198671d10453SEric Joyner  * @sc: the device softc
198771d10453SEric Joyner  *
198871d10453SEric Joyner  * Called to update the Rx queue mbuf_sz parameter for configuring the receive
198971d10453SEric Joyner  * buffer sizes when programming hardware.
199071d10453SEric Joyner  */
199171d10453SEric Joyner static void
ice_update_rx_mbuf_sz(struct ice_softc * sc)199271d10453SEric Joyner ice_update_rx_mbuf_sz(struct ice_softc *sc)
199371d10453SEric Joyner {
199471d10453SEric Joyner 	uint32_t mbuf_sz = iflib_get_rx_mbuf_sz(sc->ctx);
199571d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
199671d10453SEric Joyner 
199771d10453SEric Joyner 	MPASS(mbuf_sz <= UINT16_MAX);
199871d10453SEric Joyner 	vsi->mbuf_sz = mbuf_sz;
199971d10453SEric Joyner }
200071d10453SEric Joyner 
200171d10453SEric Joyner /**
200271d10453SEric Joyner  * ice_if_init - Initialize the device
200371d10453SEric Joyner  * @ctx: iflib ctx structure
200471d10453SEric Joyner  *
200571d10453SEric Joyner  * Called by iflib to bring the device up, i.e. ifconfig ice0 up. Initializes
200671d10453SEric Joyner  * device filters and prepares the Tx and Rx engines.
200771d10453SEric Joyner  *
200871d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
200971d10453SEric Joyner  */
201071d10453SEric Joyner static void
ice_if_init(if_ctx_t ctx)201171d10453SEric Joyner ice_if_init(if_ctx_t ctx)
201271d10453SEric Joyner {
20139e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
201471d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
201571d10453SEric Joyner 	device_t dev = sc->dev;
201671d10453SEric Joyner 	int err;
201771d10453SEric Joyner 
201871d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
201971d10453SEric Joyner 
20207d7af7f8SEric Joyner 	/*
20217d7af7f8SEric Joyner 	 * We've seen an issue with 11.3/12.1 where sideband routines are
20227d7af7f8SEric Joyner 	 * called after detach is called.  This would call routines after
20237d7af7f8SEric Joyner 	 * if_stop, causing issues with the teardown process.  This has
20247d7af7f8SEric Joyner 	 * seemingly been fixed in STABLE snapshots, but it seems like a
20257d7af7f8SEric Joyner 	 * good idea to have this guard here regardless.
20267d7af7f8SEric Joyner 	 */
20277d7af7f8SEric Joyner 	if (ice_driver_is_detaching(sc))
20287d7af7f8SEric Joyner 		return;
20297d7af7f8SEric Joyner 
203071d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
203171d10453SEric Joyner 		return;
203271d10453SEric Joyner 
203371d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
203471d10453SEric Joyner 		device_printf(sc->dev, "request to start interface cannot be completed as the device failed to reset\n");
203571d10453SEric Joyner 		return;
203671d10453SEric Joyner 	}
203771d10453SEric Joyner 
203871d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
203971d10453SEric Joyner 		device_printf(sc->dev, "request to start interface while device is prepared for impending reset\n");
204071d10453SEric Joyner 		return;
204171d10453SEric Joyner 	}
204271d10453SEric Joyner 
204371d10453SEric Joyner 	ice_update_rx_mbuf_sz(sc);
204471d10453SEric Joyner 
204571d10453SEric Joyner 	/* Update the MAC address... User might use a LAA */
204671d10453SEric Joyner 	err = ice_update_laa_mac(sc);
204771d10453SEric Joyner 	if (err) {
204871d10453SEric Joyner 		device_printf(dev,
204971d10453SEric Joyner 			      "LAA address change failed, err %s\n",
205071d10453SEric Joyner 			      ice_err_str(err));
205171d10453SEric Joyner 		return;
205271d10453SEric Joyner 	}
205371d10453SEric Joyner 
205471d10453SEric Joyner 	/* Initialize software Tx tracking values */
205571d10453SEric Joyner 	ice_init_tx_tracking(&sc->pf_vsi);
205671d10453SEric Joyner 
205771d10453SEric Joyner 	err = ice_cfg_vsi_for_tx(&sc->pf_vsi);
205871d10453SEric Joyner 	if (err) {
205971d10453SEric Joyner 		device_printf(dev,
206071d10453SEric Joyner 			      "Unable to configure the main VSI for Tx: %s\n",
206171d10453SEric Joyner 			      ice_err_str(err));
206271d10453SEric Joyner 		return;
206371d10453SEric Joyner 	}
206471d10453SEric Joyner 
206571d10453SEric Joyner 	err = ice_cfg_vsi_for_rx(&sc->pf_vsi);
206671d10453SEric Joyner 	if (err) {
206771d10453SEric Joyner 		device_printf(dev,
206871d10453SEric Joyner 			      "Unable to configure the main VSI for Rx: %s\n",
206971d10453SEric Joyner 			      ice_err_str(err));
207071d10453SEric Joyner 		goto err_cleanup_tx;
207171d10453SEric Joyner 	}
207271d10453SEric Joyner 
20739dc2f6e2SEric Joyner 	err = ice_control_all_rx_queues(&sc->pf_vsi, true);
207471d10453SEric Joyner 	if (err) {
207571d10453SEric Joyner 		device_printf(dev,
207671d10453SEric Joyner 			      "Unable to enable Rx rings for transmit: %s\n",
207771d10453SEric Joyner 			      ice_err_str(err));
207871d10453SEric Joyner 		goto err_cleanup_tx;
207971d10453SEric Joyner 	}
208071d10453SEric Joyner 
208171d10453SEric Joyner 	err = ice_cfg_pf_default_mac_filters(sc);
208271d10453SEric Joyner 	if (err) {
208371d10453SEric Joyner 		device_printf(dev,
208471d10453SEric Joyner 			      "Unable to configure default MAC filters: %s\n",
208571d10453SEric Joyner 			      ice_err_str(err));
208671d10453SEric Joyner 		goto err_stop_rx;
208771d10453SEric Joyner 	}
208871d10453SEric Joyner 
208971d10453SEric Joyner 	/* We use software interrupts for Tx, so we only program the hardware
209071d10453SEric Joyner 	 * interrupts for Rx.
209171d10453SEric Joyner 	 */
20929dc2f6e2SEric Joyner 	ice_configure_all_rxq_interrupts(&sc->pf_vsi);
209371d10453SEric Joyner 	ice_configure_rx_itr(&sc->pf_vsi);
209471d10453SEric Joyner 
209571d10453SEric Joyner 	/* Configure promiscuous mode */
209671d10453SEric Joyner 	ice_if_promisc_set(ctx, if_getflags(sc->ifp));
209771d10453SEric Joyner 
20989c30461dSEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_FIRST_INIT_LINK))
20999c30461dSEric Joyner 		if (!sc->link_up && ((if_getflags(sc->ifp) & IFF_UP) ||
21009c30461dSEric Joyner 			 ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN)))
21019c30461dSEric Joyner 			ice_set_link(sc, true);
21029c30461dSEric Joyner 
21038a13362dSEric Joyner 	ice_rdma_pf_init(sc);
21048a13362dSEric Joyner 
210571d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED);
21069e54973fSEric Joyner 
21079e54973fSEric Joyner 	if (sc->mirr_if && ice_testandclear_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT)) {
21089e54973fSEric Joyner 		ice_clear_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED);
21099e54973fSEric Joyner 		iflib_request_reset(sc->mirr_if->subctx);
21109e54973fSEric Joyner 		iflib_admin_intr_deferred(sc->mirr_if->subctx);
21119e54973fSEric Joyner 	}
21129e54973fSEric Joyner 
211371d10453SEric Joyner 	return;
211471d10453SEric Joyner 
211571d10453SEric Joyner err_stop_rx:
21169dc2f6e2SEric Joyner 	ice_control_all_rx_queues(&sc->pf_vsi, false);
211771d10453SEric Joyner err_cleanup_tx:
211871d10453SEric Joyner 	ice_vsi_disable_tx(&sc->pf_vsi);
211971d10453SEric Joyner }
212071d10453SEric Joyner 
212171d10453SEric Joyner /**
212271d10453SEric Joyner  * ice_poll_for_media_avail - Re-enable link if media is detected
212371d10453SEric Joyner  * @sc: device private structure
212471d10453SEric Joyner  *
212571d10453SEric Joyner  * Intended to be called from the driver's timer function, this function
212671d10453SEric Joyner  * sends the Get Link Status AQ command and re-enables HW link if the
212771d10453SEric Joyner  * command says that media is available.
212871d10453SEric Joyner  *
212971d10453SEric Joyner  * If the driver doesn't have the "NO_MEDIA" state set, then this does nothing,
213071d10453SEric Joyner  * since media removal events are supposed to be sent to the driver through
213171d10453SEric Joyner  * a link status event.
213271d10453SEric Joyner  */
213371d10453SEric Joyner static void
ice_poll_for_media_avail(struct ice_softc * sc)213471d10453SEric Joyner ice_poll_for_media_avail(struct ice_softc *sc)
213571d10453SEric Joyner {
213671d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
213771d10453SEric Joyner 	struct ice_port_info *pi = hw->port_info;
213871d10453SEric Joyner 
2139f377a0c7SEric Joyner 	/* E830 only: There's no interrupt for when the PHY FW has finished loading,
2140f377a0c7SEric Joyner 	 * so poll for the status in the media task here if it's previously
2141f377a0c7SEric Joyner 	 * been detected that it's still loading.
2142f377a0c7SEric Joyner 	 */
2143f377a0c7SEric Joyner 	if (ice_is_e830(hw) &&
2144f377a0c7SEric Joyner 	    ice_test_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING)) {
2145f377a0c7SEric Joyner 		if (rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M)
2146f377a0c7SEric Joyner 			ice_clear_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
2147f377a0c7SEric Joyner 		else
2148f377a0c7SEric Joyner 			return;
2149f377a0c7SEric Joyner 	}
2150f377a0c7SEric Joyner 
215171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA)) {
215271d10453SEric Joyner 		pi->phy.get_link_info = true;
215371d10453SEric Joyner 		ice_get_link_status(pi, &sc->link_up);
215471d10453SEric Joyner 
215571d10453SEric Joyner 		if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2156f2635e84SEric Joyner 			int status;
215771d10453SEric Joyner 
215871d10453SEric Joyner 			/* Re-enable link and re-apply user link settings */
21599c30461dSEric Joyner 			if (ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) ||
21609c30461dSEric Joyner 			    (if_getflags(sc->ifp) & IFF_UP)) {
21619cf1841cSEric Joyner 				ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS_FEC_FC);
216271d10453SEric Joyner 
216371d10453SEric Joyner 				/* Update the OS about changes in media capability */
216471d10453SEric Joyner 				status = ice_add_media_types(sc, sc->media);
216571d10453SEric Joyner 				if (status)
21669c30461dSEric Joyner 					device_printf(sc->dev,
21679c30461dSEric Joyner 					    "Error adding device media types: %s aq_err %s\n",
216871d10453SEric Joyner 					    ice_status_str(status),
216971d10453SEric Joyner 					    ice_aq_str(hw->adminq.sq_last_status));
21709c30461dSEric Joyner 			}
217171d10453SEric Joyner 
217271d10453SEric Joyner 			ice_clear_state(&sc->state, ICE_STATE_NO_MEDIA);
217371d10453SEric Joyner 		}
217471d10453SEric Joyner 	}
217571d10453SEric Joyner }
217671d10453SEric Joyner 
217771d10453SEric Joyner /**
217871d10453SEric Joyner  * ice_if_timer - called by iflib periodically
217971d10453SEric Joyner  * @ctx: iflib ctx structure
218071d10453SEric Joyner  * @qid: the queue this timer was called for
218171d10453SEric Joyner  *
218271d10453SEric Joyner  * This callback is triggered by iflib periodically. We use it to update the
218371d10453SEric Joyner  * hw statistics.
218471d10453SEric Joyner  *
218571d10453SEric Joyner  * @remark this function is not protected by the iflib CTX lock.
218671d10453SEric Joyner  */
218771d10453SEric Joyner static void
ice_if_timer(if_ctx_t ctx,uint16_t qid)218871d10453SEric Joyner ice_if_timer(if_ctx_t ctx, uint16_t qid)
218971d10453SEric Joyner {
219071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
219171d10453SEric Joyner 	uint64_t prev_link_xoff_rx = sc->stats.cur.link_xoff_rx;
219271d10453SEric Joyner 
219371d10453SEric Joyner 	if (qid != 0)
219471d10453SEric Joyner 		return;
219571d10453SEric Joyner 
219671d10453SEric Joyner 	/* Do not attempt to update stats when in recovery mode */
219771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
219871d10453SEric Joyner 		return;
219971d10453SEric Joyner 
220071d10453SEric Joyner 	/* Update device statistics */
220171d10453SEric Joyner 	ice_update_pf_stats(sc);
220271d10453SEric Joyner 
220371d10453SEric Joyner 	/*
220471d10453SEric Joyner 	 * For proper watchdog management, the iflib stack needs to know if
220571d10453SEric Joyner 	 * we've been paused during the last interval. Check if the
220671d10453SEric Joyner 	 * link_xoff_rx stat changed, and set the isc_pause_frames, if so.
220771d10453SEric Joyner 	 */
220871d10453SEric Joyner 	if (sc->stats.cur.link_xoff_rx != prev_link_xoff_rx)
220971d10453SEric Joyner 		sc->scctx->isc_pause_frames = 1;
221071d10453SEric Joyner 
221171d10453SEric Joyner 	/* Update the primary VSI stats */
221271d10453SEric Joyner 	ice_update_vsi_hw_stats(&sc->pf_vsi);
22139e54973fSEric Joyner 
22149e54973fSEric Joyner 	/* Update mirror VSI stats */
22159e54973fSEric Joyner 	if (sc->mirr_if && sc->mirr_if->if_attached)
22169e54973fSEric Joyner 		ice_update_vsi_hw_stats(sc->mirr_if->vsi);
221771d10453SEric Joyner }
221871d10453SEric Joyner 
221971d10453SEric Joyner /**
222071d10453SEric Joyner  * ice_admin_timer - called periodically to trigger the admin task
222171d10453SEric Joyner  * @arg: callout(9) argument pointing to the device private softc structure
222271d10453SEric Joyner  *
222371d10453SEric Joyner  * Timer function used as part of a callout(9) timer that will periodically
222471d10453SEric Joyner  * trigger the admin task, even when the interface is down.
222571d10453SEric Joyner  *
222671d10453SEric Joyner  * @remark this function is not called by iflib and is not protected by the
222771d10453SEric Joyner  * iflib CTX lock.
222871d10453SEric Joyner  *
222971d10453SEric Joyner  * @remark because this is a callout function, it cannot sleep and should not
223071d10453SEric Joyner  * attempt taking the iflib CTX lock.
223171d10453SEric Joyner  */
223271d10453SEric Joyner static void
ice_admin_timer(void * arg)223371d10453SEric Joyner ice_admin_timer(void *arg)
223471d10453SEric Joyner {
223571d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)arg;
223671d10453SEric Joyner 
22379cf1841cSEric Joyner 	/*
22389cf1841cSEric Joyner 	 * There is a point where callout routines are no longer
22399cf1841cSEric Joyner 	 * cancelable.  So there exists a window of time where the
22409cf1841cSEric Joyner 	 * driver enters detach() and tries to cancel the callout, but the
22419cf1841cSEric Joyner 	 * callout routine has passed the cancellation point.  The detach()
22429cf1841cSEric Joyner 	 * routine is unaware of this and tries to free resources that the
22439cf1841cSEric Joyner 	 * callout routine needs.  So we check for the detach state flag to
22449cf1841cSEric Joyner 	 * at least shrink the window of opportunity.
22459cf1841cSEric Joyner 	 */
22469cf1841cSEric Joyner 	if (ice_driver_is_detaching(sc))
22479cf1841cSEric Joyner 		return;
22489cf1841cSEric Joyner 
224971d10453SEric Joyner 	/* Fire off the admin task */
225071d10453SEric Joyner 	iflib_admin_intr_deferred(sc->ctx);
225171d10453SEric Joyner 
225271d10453SEric Joyner 	/* Reschedule the admin timer */
225371d10453SEric Joyner 	callout_schedule(&sc->admin_timer, hz/2);
225471d10453SEric Joyner }
225571d10453SEric Joyner 
225671d10453SEric Joyner /**
225771d10453SEric Joyner  * ice_transition_recovery_mode - Transition to recovery mode
225871d10453SEric Joyner  * @sc: the device private softc
225971d10453SEric Joyner  *
226071d10453SEric Joyner  * Called when the driver detects that the firmware has entered recovery mode
226171d10453SEric Joyner  * at run time.
226271d10453SEric Joyner  */
226371d10453SEric Joyner static void
ice_transition_recovery_mode(struct ice_softc * sc)226471d10453SEric Joyner ice_transition_recovery_mode(struct ice_softc *sc)
226571d10453SEric Joyner {
226671d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
226771d10453SEric Joyner 	int i;
226871d10453SEric Joyner 
226971d10453SEric 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");
227071d10453SEric Joyner 
227171d10453SEric Joyner 	/* Tell the stack that the link has gone down */
227271d10453SEric Joyner 	iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
227371d10453SEric Joyner 
227471d10453SEric Joyner 	/* Request that the device be re-initialized */
227571d10453SEric Joyner 	ice_request_stack_reinit(sc);
227671d10453SEric Joyner 
22778a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
22788a13362dSEric Joyner 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
22798a13362dSEric Joyner 
228071d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
228171d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
228271d10453SEric Joyner 
228371d10453SEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
228471d10453SEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
228571d10453SEric Joyner 
228671d10453SEric Joyner 	for (i = 0; i < sc->num_available_vsi; i++) {
228771d10453SEric Joyner 		if (sc->all_vsi[i])
228871d10453SEric Joyner 			ice_release_vsi(sc->all_vsi[i]);
228971d10453SEric Joyner 	}
229071d10453SEric Joyner 	sc->num_available_vsi = 0;
229171d10453SEric Joyner 
229271d10453SEric Joyner 	if (sc->all_vsi) {
229371d10453SEric Joyner 		free(sc->all_vsi, M_ICE);
229471d10453SEric Joyner 		sc->all_vsi = NULL;
229571d10453SEric Joyner 	}
229671d10453SEric Joyner 
229771d10453SEric Joyner 	/* Destroy the interrupt manager */
22989e54973fSEric Joyner 	ice_resmgr_destroy(&sc->dev_imgr);
229971d10453SEric Joyner 	/* Destroy the queue managers */
230071d10453SEric Joyner 	ice_resmgr_destroy(&sc->tx_qmgr);
230171d10453SEric Joyner 	ice_resmgr_destroy(&sc->rx_qmgr);
230271d10453SEric Joyner 
230371d10453SEric Joyner 	ice_deinit_hw(&sc->hw);
230471d10453SEric Joyner }
230571d10453SEric Joyner 
230671d10453SEric Joyner /**
230771d10453SEric Joyner  * ice_transition_safe_mode - Transition to safe mode
230871d10453SEric Joyner  * @sc: the device private softc
230971d10453SEric Joyner  *
231071d10453SEric Joyner  * Called when the driver attempts to reload the DDP package during a device
231171d10453SEric Joyner  * reset, and the new download fails. If so, we must transition to safe mode
231271d10453SEric Joyner  * at run time.
231371d10453SEric Joyner  *
231471d10453SEric Joyner  * @remark although safe mode normally allocates only a single queue, we can't
231571d10453SEric Joyner  * change the number of queues dynamically when using iflib. Due to this, we
231671d10453SEric Joyner  * do not attempt to reduce the number of queues.
231771d10453SEric Joyner  */
231871d10453SEric Joyner static void
ice_transition_safe_mode(struct ice_softc * sc)231971d10453SEric Joyner ice_transition_safe_mode(struct ice_softc *sc)
232071d10453SEric Joyner {
232171d10453SEric Joyner 	/* Indicate that we are in Safe mode */
232271d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_cap);
232371d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_en);
232471d10453SEric Joyner 
23258a13362dSEric Joyner 	ice_rdma_pf_detach(sc);
23268a13362dSEric Joyner 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
23278a13362dSEric Joyner 
232871d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
232971d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
233071d10453SEric Joyner 
233171d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
233271d10453SEric Joyner 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_en);
233371d10453SEric Joyner }
233471d10453SEric Joyner 
233571d10453SEric Joyner /**
233671d10453SEric Joyner  * ice_if_update_admin_status - update admin status
233771d10453SEric Joyner  * @ctx: iflib ctx structure
233871d10453SEric Joyner  *
233971d10453SEric Joyner  * Called by iflib to update the admin status. For our purposes, this means
234071d10453SEric Joyner  * check the adminq, and update the link status. It's ultimately triggered by
234171d10453SEric Joyner  * our admin interrupt, or by the ice_if_timer periodically.
234271d10453SEric Joyner  *
234371d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
234471d10453SEric Joyner  */
234571d10453SEric Joyner static void
ice_if_update_admin_status(if_ctx_t ctx)234671d10453SEric Joyner ice_if_update_admin_status(if_ctx_t ctx)
234771d10453SEric Joyner {
234871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
234971d10453SEric Joyner 	enum ice_fw_modes fw_mode;
235071d10453SEric Joyner 	bool reschedule = false;
235171d10453SEric Joyner 	u16 pending = 0;
235271d10453SEric Joyner 
235371d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
235471d10453SEric Joyner 
235571d10453SEric Joyner 	/* Check if the firmware entered recovery mode at run time */
235671d10453SEric Joyner 	fw_mode = ice_get_fw_mode(&sc->hw);
235771d10453SEric Joyner 	if (fw_mode == ICE_FW_MODE_REC) {
235871d10453SEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
235971d10453SEric Joyner 			/* If we just entered recovery mode, log a warning to
236071d10453SEric Joyner 			 * the system administrator and deinit driver state
236171d10453SEric Joyner 			 * that is no longer functional.
236271d10453SEric Joyner 			 */
236371d10453SEric Joyner 			ice_transition_recovery_mode(sc);
236471d10453SEric Joyner 		}
236571d10453SEric Joyner 	} else if (fw_mode == ICE_FW_MODE_ROLLBACK) {
236671d10453SEric Joyner 		if (!ice_testandset_state(&sc->state, ICE_STATE_ROLLBACK_MODE)) {
236771d10453SEric Joyner 			/* Rollback mode isn't fatal, but we don't want to
236871d10453SEric Joyner 			 * repeatedly post a message about it.
236971d10453SEric Joyner 			 */
237071d10453SEric Joyner 			ice_print_rollback_msg(&sc->hw);
237171d10453SEric Joyner 		}
237271d10453SEric Joyner 	}
237371d10453SEric Joyner 
237471d10453SEric Joyner 	/* Handle global reset events */
237571d10453SEric Joyner 	ice_handle_reset_event(sc);
237671d10453SEric Joyner 
237771d10453SEric Joyner 	/* Handle PF reset requests */
237871d10453SEric Joyner 	ice_handle_pf_reset_request(sc);
237971d10453SEric Joyner 
238071d10453SEric Joyner 	/* Handle MDD events */
238171d10453SEric Joyner 	ice_handle_mdd_event(sc);
238271d10453SEric Joyner 
238371d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED) ||
238471d10453SEric Joyner 	    ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET) ||
238571d10453SEric Joyner 	    ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
238671d10453SEric Joyner 		/*
238771d10453SEric Joyner 		 * If we know the control queues are disabled, skip processing
238871d10453SEric Joyner 		 * the control queues entirely.
238971d10453SEric Joyner 		 */
239071d10453SEric Joyner 		;
239171d10453SEric Joyner 	} else if (ice_testandclear_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING)) {
239271d10453SEric Joyner 		ice_process_ctrlq(sc, ICE_CTL_Q_ADMIN, &pending);
239371d10453SEric Joyner 		if (pending > 0)
239471d10453SEric Joyner 			reschedule = true;
239571d10453SEric Joyner 
2396f2635e84SEric Joyner 		if (ice_is_generic_mac(&sc->hw)) {
2397f2635e84SEric Joyner 			ice_process_ctrlq(sc, ICE_CTL_Q_SB, &pending);
2398f2635e84SEric Joyner 			if (pending > 0)
2399f2635e84SEric Joyner 				reschedule = true;
2400f2635e84SEric Joyner 		}
2401f2635e84SEric Joyner 
240271d10453SEric Joyner 		ice_process_ctrlq(sc, ICE_CTL_Q_MAILBOX, &pending);
240371d10453SEric Joyner 		if (pending > 0)
240471d10453SEric Joyner 			reschedule = true;
240571d10453SEric Joyner 	}
240671d10453SEric Joyner 
240771d10453SEric Joyner 	/* Poll for link up */
240871d10453SEric Joyner 	ice_poll_for_media_avail(sc);
240971d10453SEric Joyner 
241071d10453SEric Joyner 	/* Check and update link status */
241171d10453SEric Joyner 	ice_update_link_status(sc, false);
241271d10453SEric Joyner 
241371d10453SEric Joyner 	/*
241471d10453SEric Joyner 	 * If there are still messages to process, we need to reschedule
241571d10453SEric Joyner 	 * ourselves. Otherwise, we can just re-enable the interrupt. We'll be
241671d10453SEric Joyner 	 * woken up at the next interrupt or timer event.
241771d10453SEric Joyner 	 */
241871d10453SEric Joyner 	if (reschedule) {
241971d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
242071d10453SEric Joyner 		iflib_admin_intr_deferred(ctx);
242171d10453SEric Joyner 	} else {
242271d10453SEric Joyner 		ice_enable_intr(&sc->hw, sc->irqvs[0].me);
242371d10453SEric Joyner 	}
242471d10453SEric Joyner }
242571d10453SEric Joyner 
242671d10453SEric Joyner /**
242771d10453SEric Joyner  * ice_prepare_for_reset - Prepare device for an impending reset
242871d10453SEric Joyner  * @sc: The device private softc
242971d10453SEric Joyner  *
243071d10453SEric Joyner  * Prepare the driver for an impending reset, shutting down VSIs, clearing the
243171d10453SEric Joyner  * scheduler setup, and shutting down controlqs. Uses the
243271d10453SEric Joyner  * ICE_STATE_PREPARED_FOR_RESET to indicate whether we've already prepared the
243371d10453SEric Joyner  * driver for reset or not.
243471d10453SEric Joyner  */
243571d10453SEric Joyner static void
ice_prepare_for_reset(struct ice_softc * sc)243671d10453SEric Joyner ice_prepare_for_reset(struct ice_softc *sc)
243771d10453SEric Joyner {
243871d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
243971d10453SEric Joyner 
244071d10453SEric Joyner 	/* If we're already prepared, there's nothing to do */
244171d10453SEric Joyner 	if (ice_testandset_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET))
244271d10453SEric Joyner 		return;
244371d10453SEric Joyner 
2444402810d3SJustin Hibbits 	log(LOG_INFO, "%s: preparing to reset device logic\n", if_name(sc->ifp));
244571d10453SEric Joyner 
244671d10453SEric Joyner 	/* In recovery mode, hardware is not initialized */
244771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
244871d10453SEric Joyner 		return;
244971d10453SEric Joyner 
245001fbb869SBartosz Sobczak 	/* inform the RDMA client */
245101fbb869SBartosz Sobczak 	ice_rdma_notify_reset(sc);
24528a13362dSEric Joyner 	/* stop the RDMA client */
24538a13362dSEric Joyner 	ice_rdma_pf_stop(sc);
24548a13362dSEric Joyner 
245571d10453SEric Joyner 	/* Release the main PF VSI queue mappings */
245671d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
245771d10453SEric Joyner 				    sc->pf_vsi.num_tx_queues);
245871d10453SEric Joyner 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
245971d10453SEric Joyner 				    sc->pf_vsi.num_rx_queues);
24609e54973fSEric Joyner 	if (sc->mirr_if) {
24619e54973fSEric Joyner 		ice_resmgr_release_map(&sc->tx_qmgr, sc->mirr_if->vsi->tx_qmap,
24629e54973fSEric Joyner 		    sc->mirr_if->num_irq_vectors);
24639e54973fSEric Joyner 		ice_resmgr_release_map(&sc->rx_qmgr, sc->mirr_if->vsi->rx_qmap,
24649e54973fSEric Joyner 		    sc->mirr_if->num_irq_vectors);
24659e54973fSEric Joyner 	}
246671d10453SEric Joyner 
246771d10453SEric Joyner 	ice_clear_hw_tbls(hw);
246871d10453SEric Joyner 
246971d10453SEric Joyner 	if (hw->port_info)
24709c30461dSEric Joyner 		ice_sched_cleanup_all(hw);
247171d10453SEric Joyner 
24728923de59SPiotr Kubaj 	ice_shutdown_all_ctrlq(hw, false);
247371d10453SEric Joyner }
247471d10453SEric Joyner 
247571d10453SEric Joyner /**
247671d10453SEric Joyner  * ice_rebuild_pf_vsi_qmap - Rebuild the main PF VSI queue mapping
247771d10453SEric Joyner  * @sc: the device softc pointer
247871d10453SEric Joyner  *
247971d10453SEric Joyner  * Loops over the Tx and Rx queues for the main PF VSI and reassigns the queue
248071d10453SEric Joyner  * mapping after a reset occurred.
248171d10453SEric Joyner  */
248271d10453SEric Joyner static int
ice_rebuild_pf_vsi_qmap(struct ice_softc * sc)248371d10453SEric Joyner ice_rebuild_pf_vsi_qmap(struct ice_softc *sc)
248471d10453SEric Joyner {
248571d10453SEric Joyner 	struct ice_vsi *vsi = &sc->pf_vsi;
248671d10453SEric Joyner 	struct ice_tx_queue *txq;
248771d10453SEric Joyner 	struct ice_rx_queue *rxq;
248871d10453SEric Joyner 	int err, i;
248971d10453SEric Joyner 
249071d10453SEric Joyner 	/* Re-assign Tx queues from PF space to the main VSI */
249171d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap,
249271d10453SEric Joyner 					    vsi->num_tx_queues);
249371d10453SEric Joyner 	if (err) {
249471d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign PF Tx queues: %s\n",
249571d10453SEric Joyner 			      ice_err_str(err));
249671d10453SEric Joyner 		return (err);
249771d10453SEric Joyner 	}
249871d10453SEric Joyner 
249971d10453SEric Joyner 	/* Re-assign Rx queues from PF space to this VSI */
250071d10453SEric Joyner 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap,
250171d10453SEric Joyner 					    vsi->num_rx_queues);
250271d10453SEric Joyner 	if (err) {
250371d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign PF Rx queues: %s\n",
250471d10453SEric Joyner 			      ice_err_str(err));
250571d10453SEric Joyner 		goto err_release_tx_queues;
250671d10453SEric Joyner 	}
250771d10453SEric Joyner 
250871d10453SEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
250971d10453SEric Joyner 
251071d10453SEric Joyner 	/* Re-assign Tx queue tail pointers */
251171d10453SEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
251271d10453SEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
251371d10453SEric Joyner 
251471d10453SEric Joyner 	/* Re-assign Rx queue tail pointers */
251571d10453SEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
251671d10453SEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
251771d10453SEric Joyner 
251871d10453SEric Joyner 	return (0);
251971d10453SEric Joyner 
252071d10453SEric Joyner err_release_tx_queues:
252171d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
252271d10453SEric Joyner 				   sc->pf_vsi.num_tx_queues);
252371d10453SEric Joyner 
252471d10453SEric Joyner 	return (err);
252571d10453SEric Joyner }
252671d10453SEric Joyner 
252771d10453SEric Joyner /* determine if the iflib context is active */
252871d10453SEric Joyner #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
252971d10453SEric Joyner 
253071d10453SEric Joyner /**
253171d10453SEric Joyner  * ice_rebuild_recovery_mode - Rebuild driver state while in recovery mode
253271d10453SEric Joyner  * @sc: The device private softc
253371d10453SEric Joyner  *
253471d10453SEric Joyner  * Handle a driver rebuild while in recovery mode. This will only rebuild the
253571d10453SEric Joyner  * limited functionality supported while in recovery mode.
253671d10453SEric Joyner  */
253771d10453SEric Joyner static void
ice_rebuild_recovery_mode(struct ice_softc * sc)253871d10453SEric Joyner ice_rebuild_recovery_mode(struct ice_softc *sc)
253971d10453SEric Joyner {
254071d10453SEric Joyner 	device_t dev = sc->dev;
254171d10453SEric Joyner 
254271d10453SEric Joyner 	/* enable PCIe bus master */
254371d10453SEric Joyner 	pci_enable_busmaster(dev);
254471d10453SEric Joyner 
254571d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
254671d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
254771d10453SEric Joyner 
254871d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
254971d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
255071d10453SEric Joyner 
255171d10453SEric Joyner 	/* Now that the rebuild is finished, we're no longer prepared to reset */
255271d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
255371d10453SEric Joyner 
2554402810d3SJustin Hibbits 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
255571d10453SEric Joyner 
255671d10453SEric Joyner 	/* In order to completely restore device functionality, the iflib core
255771d10453SEric Joyner 	 * needs to be reset. We need to request an iflib reset. Additionally,
255871d10453SEric Joyner 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
255971d10453SEric Joyner 	 * the iflib core, we also want re-run the admin task so that iflib
256071d10453SEric Joyner 	 * resets immediately instead of waiting for the next interrupt.
256171d10453SEric Joyner 	 */
256271d10453SEric Joyner 	ice_request_stack_reinit(sc);
256371d10453SEric Joyner 
256471d10453SEric Joyner 	return;
256571d10453SEric Joyner }
256671d10453SEric Joyner 
256771d10453SEric Joyner /**
256871d10453SEric Joyner  * ice_rebuild - Rebuild driver state post reset
256971d10453SEric Joyner  * @sc: The device private softc
257071d10453SEric Joyner  *
257171d10453SEric Joyner  * Restore driver state after a reset occurred. Restart the controlqs, setup
257271d10453SEric Joyner  * the hardware port, and re-enable the VSIs.
257371d10453SEric Joyner  */
257471d10453SEric Joyner static void
ice_rebuild(struct ice_softc * sc)257571d10453SEric Joyner ice_rebuild(struct ice_softc *sc)
257671d10453SEric Joyner {
257771d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
257871d10453SEric Joyner 	device_t dev = sc->dev;
25798923de59SPiotr Kubaj 	enum ice_ddp_state pkg_state;
2580f2635e84SEric Joyner 	int status;
258171d10453SEric Joyner 	int err;
258271d10453SEric Joyner 
258371d10453SEric Joyner 	sc->rebuild_ticks = ticks;
258471d10453SEric Joyner 
258571d10453SEric Joyner 	/* If we're rebuilding, then a reset has succeeded. */
258671d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_RESET_FAILED);
258771d10453SEric Joyner 
258871d10453SEric Joyner 	/*
258971d10453SEric Joyner 	 * If the firmware is in recovery mode, only restore the limited
259071d10453SEric Joyner 	 * functionality supported by recovery mode.
259171d10453SEric Joyner 	 */
259271d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
259371d10453SEric Joyner 		ice_rebuild_recovery_mode(sc);
259471d10453SEric Joyner 		return;
259571d10453SEric Joyner 	}
259671d10453SEric Joyner 
259771d10453SEric Joyner 	/* enable PCIe bus master */
259871d10453SEric Joyner 	pci_enable_busmaster(dev);
259971d10453SEric Joyner 
260071d10453SEric Joyner 	status = ice_init_all_ctrlq(hw);
260171d10453SEric Joyner 	if (status) {
260271d10453SEric Joyner 		device_printf(dev, "failed to re-init controlqs, err %s\n",
260371d10453SEric Joyner 			      ice_status_str(status));
260471d10453SEric Joyner 		goto err_shutdown_ctrlq;
260571d10453SEric Joyner 	}
260671d10453SEric Joyner 
260771d10453SEric Joyner 	/* Query the allocated resources for Tx scheduler */
260871d10453SEric Joyner 	status = ice_sched_query_res_alloc(hw);
260971d10453SEric Joyner 	if (status) {
261071d10453SEric Joyner 		device_printf(dev,
261171d10453SEric Joyner 			      "Failed to query scheduler resources, err %s aq_err %s\n",
261271d10453SEric Joyner 			      ice_status_str(status),
261371d10453SEric Joyner 			      ice_aq_str(hw->adminq.sq_last_status));
261471d10453SEric Joyner 		goto err_shutdown_ctrlq;
261571d10453SEric Joyner 	}
261671d10453SEric Joyner 
261756429daeSEric Joyner 	/* Re-enable FW logging. Keep going even if this fails */
2618440addc6SEric Joyner 	status = ICE_SUCCESS;
2619440addc6SEric Joyner 	if (hw->pf_id == 0)
262056429daeSEric Joyner 		status = ice_fwlog_set(hw, &hw->fwlog_cfg);
262156429daeSEric Joyner 	if (!status) {
262256429daeSEric Joyner 		/*
262356429daeSEric Joyner 		 * We should have the most updated cached copy of the
262456429daeSEric Joyner 		 * configuration, regardless of whether we're rebuilding
262556429daeSEric Joyner 		 * or not.  So we'll simply check to see if logging was
262656429daeSEric Joyner 		 * enabled pre-rebuild.
262756429daeSEric Joyner 		 */
262856429daeSEric Joyner 		if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
262956429daeSEric Joyner 			status = ice_fwlog_register(hw);
263056429daeSEric Joyner 			if (status)
263156429daeSEric Joyner 				device_printf(dev, "failed to re-register fw logging, err %s aq_err %s\n",
263256429daeSEric Joyner 				   ice_status_str(status),
263356429daeSEric Joyner 				   ice_aq_str(hw->adminq.sq_last_status));
263456429daeSEric Joyner 		}
263556429daeSEric Joyner 	} else
263656429daeSEric Joyner 		device_printf(dev, "failed to rebuild fw logging configuration, err %s aq_err %s\n",
263756429daeSEric Joyner 		   ice_status_str(status),
263856429daeSEric Joyner 		   ice_aq_str(hw->adminq.sq_last_status));
263956429daeSEric Joyner 
264071d10453SEric Joyner 	err = ice_send_version(sc);
264171d10453SEric Joyner 	if (err)
264271d10453SEric Joyner 		goto err_shutdown_ctrlq;
264371d10453SEric Joyner 
264471d10453SEric Joyner 	err = ice_init_link_events(sc);
264571d10453SEric Joyner 	if (err) {
264671d10453SEric Joyner 		device_printf(dev, "ice_init_link_events failed: %s\n",
264771d10453SEric Joyner 			      ice_err_str(err));
264871d10453SEric Joyner 		goto err_shutdown_ctrlq;
264971d10453SEric Joyner 	}
265071d10453SEric Joyner 
265171d10453SEric Joyner 	status = ice_clear_pf_cfg(hw);
265271d10453SEric Joyner 	if (status) {
265371d10453SEric Joyner 		device_printf(dev, "failed to clear PF configuration, err %s\n",
265471d10453SEric Joyner 			      ice_status_str(status));
265571d10453SEric Joyner 		goto err_shutdown_ctrlq;
265671d10453SEric Joyner 	}
265771d10453SEric Joyner 
26589e54973fSEric Joyner 	ice_clean_all_vsi_rss_cfg(sc);
26599e54973fSEric Joyner 
266071d10453SEric Joyner 	ice_clear_pxe_mode(hw);
266171d10453SEric Joyner 
266271d10453SEric Joyner 	status = ice_get_caps(hw);
266371d10453SEric Joyner 	if (status) {
266471d10453SEric Joyner 		device_printf(dev, "failed to get capabilities, err %s\n",
266571d10453SEric Joyner 			      ice_status_str(status));
266671d10453SEric Joyner 		goto err_shutdown_ctrlq;
266771d10453SEric Joyner 	}
266871d10453SEric Joyner 
266971d10453SEric Joyner 	status = ice_sched_init_port(hw->port_info);
267071d10453SEric Joyner 	if (status) {
267171d10453SEric Joyner 		device_printf(dev, "failed to initialize port, err %s\n",
267271d10453SEric Joyner 			      ice_status_str(status));
267371d10453SEric Joyner 		goto err_sched_cleanup;
267471d10453SEric Joyner 	}
267571d10453SEric Joyner 
267671d10453SEric Joyner 	/* If we previously loaded the package, it needs to be reloaded now */
267771d10453SEric Joyner 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE)) {
26788923de59SPiotr Kubaj 		pkg_state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
26798923de59SPiotr Kubaj 		if (!ice_is_init_pkg_successful(pkg_state)) {
26808923de59SPiotr Kubaj 			ice_log_pkg_init(sc, pkg_state);
268171d10453SEric Joyner 			ice_transition_safe_mode(sc);
268271d10453SEric Joyner 		}
268371d10453SEric Joyner 	}
268471d10453SEric Joyner 
268571d10453SEric Joyner 	ice_reset_pf_stats(sc);
268671d10453SEric Joyner 
268771d10453SEric Joyner 	err = ice_rebuild_pf_vsi_qmap(sc);
268871d10453SEric Joyner 	if (err) {
268971d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-assign main VSI queues, err %s\n",
269071d10453SEric Joyner 			      ice_err_str(err));
269171d10453SEric Joyner 		goto err_sched_cleanup;
269271d10453SEric Joyner 	}
269371d10453SEric Joyner 	err = ice_initialize_vsi(&sc->pf_vsi);
269471d10453SEric Joyner 	if (err) {
269571d10453SEric Joyner 		device_printf(sc->dev, "Unable to re-initialize Main VSI, err %s\n",
269671d10453SEric Joyner 			      ice_err_str(err));
269771d10453SEric Joyner 		goto err_release_queue_allocations;
269871d10453SEric Joyner 	}
269971d10453SEric Joyner 
270071d10453SEric Joyner 	/* Replay all VSI configuration */
270171d10453SEric Joyner 	err = ice_replay_all_vsi_cfg(sc);
270271d10453SEric Joyner 	if (err)
270371d10453SEric Joyner 		goto err_deinit_pf_vsi;
270471d10453SEric Joyner 
27059cf1841cSEric Joyner 	/* Re-enable FW health event reporting */
27069cf1841cSEric Joyner 	ice_init_health_events(sc);
27079cf1841cSEric Joyner 
270871d10453SEric Joyner 	/* Reconfigure the main PF VSI for RSS */
270971d10453SEric Joyner 	err = ice_config_rss(&sc->pf_vsi);
271071d10453SEric Joyner 	if (err) {
271171d10453SEric Joyner 		device_printf(sc->dev,
271271d10453SEric Joyner 			      "Unable to reconfigure RSS for the main VSI, err %s\n",
271371d10453SEric Joyner 			      ice_err_str(err));
271471d10453SEric Joyner 		goto err_deinit_pf_vsi;
271571d10453SEric Joyner 	}
271671d10453SEric Joyner 
27179c30461dSEric Joyner 	if (hw->port_info->qos_cfg.is_sw_lldp)
27189c30461dSEric Joyner 		ice_add_rx_lldp_filter(sc);
27199c30461dSEric Joyner 
2720f377a0c7SEric Joyner 	/* Apply previous link settings and refresh link status, if PHY
2721f377a0c7SEric Joyner 	 * FW is ready.
2722f377a0c7SEric Joyner 	 */
272371d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
2724f377a0c7SEric Joyner 	ice_init_link(sc);
272571d10453SEric Joyner 
27268a13362dSEric Joyner 	/* RDMA interface will be restarted by the stack re-init */
27278a13362dSEric Joyner 
272871d10453SEric Joyner 	/* Configure interrupt causes for the administrative interrupt */
272971d10453SEric Joyner 	ice_configure_misc_interrupts(sc);
273071d10453SEric Joyner 
273171d10453SEric Joyner 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
273271d10453SEric Joyner 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
273371d10453SEric Joyner 
273471d10453SEric Joyner 	/* Now that the rebuild is finished, we're no longer prepared to reset */
273571d10453SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
273671d10453SEric Joyner 
27379e54973fSEric Joyner 	/* Reconfigure the subinterface */
27389e54973fSEric Joyner 	if (sc->mirr_if) {
27399e54973fSEric Joyner 		err = ice_subif_rebuild(sc);
27409e54973fSEric Joyner 		if (err)
27419e54973fSEric Joyner 			goto err_deinit_pf_vsi;
27429e54973fSEric Joyner 	}
27439e54973fSEric Joyner 
2744f2635e84SEric Joyner 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
274571d10453SEric Joyner 
274671d10453SEric Joyner 	/* In order to completely restore device functionality, the iflib core
274771d10453SEric Joyner 	 * needs to be reset. We need to request an iflib reset. Additionally,
274871d10453SEric Joyner 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
274971d10453SEric Joyner 	 * the iflib core, we also want re-run the admin task so that iflib
275071d10453SEric Joyner 	 * resets immediately instead of waiting for the next interrupt.
27519c30461dSEric Joyner 	 * If LLDP is enabled we need to reconfig DCB to properly reinit all TC
27529c30461dSEric Joyner 	 * queues, not only 0. It contains ice_request_stack_reinit as well.
275371d10453SEric Joyner 	 */
27549c30461dSEric Joyner 	if (hw->port_info->qos_cfg.is_sw_lldp)
275571d10453SEric Joyner 		ice_request_stack_reinit(sc);
27569c30461dSEric Joyner 	else
27579c30461dSEric Joyner 		ice_do_dcb_reconfig(sc, false);
275871d10453SEric Joyner 
275971d10453SEric Joyner 	return;
276071d10453SEric Joyner 
276171d10453SEric Joyner err_deinit_pf_vsi:
276271d10453SEric Joyner 	ice_deinit_vsi(&sc->pf_vsi);
276371d10453SEric Joyner err_release_queue_allocations:
276471d10453SEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
276571d10453SEric Joyner 				    sc->pf_vsi.num_tx_queues);
276671d10453SEric Joyner 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
276771d10453SEric Joyner 				    sc->pf_vsi.num_rx_queues);
276871d10453SEric Joyner err_sched_cleanup:
276971d10453SEric Joyner 	ice_sched_cleanup_all(hw);
277071d10453SEric Joyner err_shutdown_ctrlq:
27718923de59SPiotr Kubaj 	ice_shutdown_all_ctrlq(hw, false);
27728923de59SPiotr Kubaj 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
277371d10453SEric Joyner 	ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
277471d10453SEric Joyner 	device_printf(dev, "Driver rebuild failed, please reload the device driver\n");
277571d10453SEric Joyner }
277671d10453SEric Joyner 
277771d10453SEric Joyner /**
277871d10453SEric Joyner  * ice_handle_reset_event - Handle reset events triggered by OICR
277971d10453SEric Joyner  * @sc: The device private softc
278071d10453SEric Joyner  *
278171d10453SEric Joyner  * Handle reset events triggered by an OICR notification. This includes CORER,
278271d10453SEric Joyner  * GLOBR, and EMPR resets triggered by software on this or any other PF or by
278371d10453SEric Joyner  * firmware.
278471d10453SEric Joyner  *
278571d10453SEric Joyner  * @pre assumes the iflib context lock is held, and will unlock it while
278671d10453SEric Joyner  * waiting for the hardware to finish reset.
278771d10453SEric Joyner  */
278871d10453SEric Joyner static void
ice_handle_reset_event(struct ice_softc * sc)278971d10453SEric Joyner ice_handle_reset_event(struct ice_softc *sc)
279071d10453SEric Joyner {
279171d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
2792f2635e84SEric Joyner 	int status;
279371d10453SEric Joyner 	device_t dev = sc->dev;
279471d10453SEric Joyner 
279571d10453SEric Joyner 	/* When a CORER, GLOBR, or EMPR is about to happen, the hardware will
279671d10453SEric Joyner 	 * trigger an OICR interrupt. Our OICR handler will determine when
279771d10453SEric Joyner 	 * this occurs and set the ICE_STATE_RESET_OICR_RECV bit as
279871d10453SEric Joyner 	 * appropriate.
279971d10453SEric Joyner 	 */
280071d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
280171d10453SEric Joyner 		return;
280271d10453SEric Joyner 
280371d10453SEric Joyner 	ice_prepare_for_reset(sc);
280471d10453SEric Joyner 
280571d10453SEric Joyner 	/*
280671d10453SEric Joyner 	 * Release the iflib context lock and wait for the device to finish
280771d10453SEric Joyner 	 * resetting.
280871d10453SEric Joyner 	 */
280971d10453SEric Joyner 	IFLIB_CTX_UNLOCK(sc);
2810f377a0c7SEric Joyner 
2811f377a0c7SEric Joyner #define ICE_EMPR_ADDL_WAIT_MSEC_SLOW		20000
2812f377a0c7SEric Joyner 	if ((ice_is_e830(hw) || ice_is_e825c(hw)) &&
2813f377a0c7SEric Joyner 	    (((rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2814f377a0c7SEric Joyner 	         GLGEN_RSTAT_RESET_TYPE_S) == ICE_RESET_EMPR))
2815f377a0c7SEric Joyner 			ice_msec_pause(ICE_EMPR_ADDL_WAIT_MSEC_SLOW);
2816f377a0c7SEric Joyner 
281771d10453SEric Joyner 	status = ice_check_reset(hw);
281871d10453SEric Joyner 	IFLIB_CTX_LOCK(sc);
281971d10453SEric Joyner 	if (status) {
282071d10453SEric Joyner 		device_printf(dev, "Device never came out of reset, err %s\n",
282171d10453SEric Joyner 			      ice_status_str(status));
2822f377a0c7SEric Joyner 
282371d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2824f377a0c7SEric Joyner 		ice_clear_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
2825f377a0c7SEric Joyner 		ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2826f377a0c7SEric Joyner 		device_printf(dev, "Reset failed; please reload the device driver\n");
282771d10453SEric Joyner 		return;
282871d10453SEric Joyner 	}
282971d10453SEric Joyner 
283071d10453SEric Joyner 	/* We're done with the reset, so we can rebuild driver state */
283171d10453SEric Joyner 	sc->hw.reset_ongoing = false;
283271d10453SEric Joyner 	ice_rebuild(sc);
283371d10453SEric Joyner 
283471d10453SEric Joyner 	/* In the unlikely event that a PF reset request occurs at the same
283571d10453SEric Joyner 	 * time as a global reset, clear the request now. This avoids
283671d10453SEric Joyner 	 * resetting a second time right after we reset due to a global event.
283771d10453SEric Joyner 	 */
283871d10453SEric Joyner 	if (ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
283971d10453SEric Joyner 		device_printf(dev, "Ignoring PFR request that occurred while a reset was ongoing\n");
284071d10453SEric Joyner }
284171d10453SEric Joyner 
284271d10453SEric Joyner /**
284371d10453SEric Joyner  * ice_handle_pf_reset_request - Initiate PF reset requested by software
284471d10453SEric Joyner  * @sc: The device private softc
284571d10453SEric Joyner  *
284671d10453SEric Joyner  * Initiate a PF reset requested by software. We handle this in the admin task
284771d10453SEric Joyner  * so that only one thread actually handles driver preparation and cleanup,
284871d10453SEric Joyner  * rather than having multiple threads possibly attempt to run this code
284971d10453SEric Joyner  * simultaneously.
285071d10453SEric Joyner  *
285171d10453SEric Joyner  * @pre assumes the iflib context lock is held and will unlock it while
285271d10453SEric Joyner  * waiting for the PF reset to complete.
285371d10453SEric Joyner  */
285471d10453SEric Joyner static void
ice_handle_pf_reset_request(struct ice_softc * sc)285571d10453SEric Joyner ice_handle_pf_reset_request(struct ice_softc *sc)
285671d10453SEric Joyner {
285771d10453SEric Joyner 	struct ice_hw *hw = &sc->hw;
2858f2635e84SEric Joyner 	int status;
285971d10453SEric Joyner 
286071d10453SEric Joyner 	/* Check for PF reset requests */
286171d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
286271d10453SEric Joyner 		return;
286371d10453SEric Joyner 
286471d10453SEric Joyner 	/* Make sure we're prepared for reset */
286571d10453SEric Joyner 	ice_prepare_for_reset(sc);
286671d10453SEric Joyner 
286771d10453SEric Joyner 	/*
286871d10453SEric Joyner 	 * Release the iflib context lock and wait for the device to finish
286971d10453SEric Joyner 	 * resetting.
287071d10453SEric Joyner 	 */
287171d10453SEric Joyner 	IFLIB_CTX_UNLOCK(sc);
287271d10453SEric Joyner 	status = ice_reset(hw, ICE_RESET_PFR);
287371d10453SEric Joyner 	IFLIB_CTX_LOCK(sc);
287471d10453SEric Joyner 	if (status) {
287571d10453SEric Joyner 		device_printf(sc->dev, "device PF reset failed, err %s\n",
287671d10453SEric Joyner 			      ice_status_str(status));
287771d10453SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
287871d10453SEric Joyner 		return;
287971d10453SEric Joyner 	}
288071d10453SEric Joyner 
288171d10453SEric Joyner 	sc->soft_stats.pfr_count++;
288271d10453SEric Joyner 	ice_rebuild(sc);
288371d10453SEric Joyner }
288471d10453SEric Joyner 
288571d10453SEric Joyner /**
288671d10453SEric Joyner  * ice_init_device_features - Init device driver features
288771d10453SEric Joyner  * @sc: driver softc structure
288871d10453SEric Joyner  *
288971d10453SEric Joyner  * @pre assumes that the function capabilities bits have been set up by
289071d10453SEric Joyner  * ice_init_hw().
289171d10453SEric Joyner  */
289271d10453SEric Joyner static void
ice_init_device_features(struct ice_softc * sc)289371d10453SEric Joyner ice_init_device_features(struct ice_softc *sc)
289471d10453SEric Joyner {
28959c30461dSEric Joyner 	struct ice_hw *hw = &sc->hw;
28969c30461dSEric Joyner 
28977d7af7f8SEric Joyner 	/* Set capabilities that all devices support */
289871d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
289971d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_RSS, sc->feat_cap);
29008a13362dSEric Joyner 	ice_set_bit(ICE_FEATURE_RDMA, sc->feat_cap);
290171d10453SEric Joyner 	ice_set_bit(ICE_FEATURE_LENIENT_LINK_MODE, sc->feat_cap);
29029cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_1, sc->feat_cap);
29039cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_2, sc->feat_cap);
29049cf1841cSEric Joyner 	ice_set_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
290556429daeSEric Joyner 	ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
290656429daeSEric Joyner 	ice_set_bit(ICE_FEATURE_HAS_PBA, sc->feat_cap);
29078923de59SPiotr Kubaj 	ice_set_bit(ICE_FEATURE_DCB, sc->feat_cap);
29088923de59SPiotr Kubaj 	ice_set_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
2909f2635e84SEric Joyner 	ice_set_bit(ICE_FEATURE_PHY_STATISTICS, sc->feat_cap);
291071d10453SEric Joyner 
2911f2635e84SEric Joyner 	if (ice_is_e810(hw))
2912f2635e84SEric Joyner 		ice_set_bit(ICE_FEATURE_PHY_STATISTICS, sc->feat_en);
2913f2635e84SEric Joyner 
2914f377a0c7SEric Joyner 	if (ice_is_e825c(hw))
2915f2635e84SEric Joyner 		ice_set_bit(ICE_FEATURE_DUAL_NAC, sc->feat_cap);
291671d10453SEric Joyner 	/* Disable features due to hardware limitations... */
29179c30461dSEric Joyner 	if (!hw->func_caps.common_cap.rss_table_size)
291871d10453SEric Joyner 		ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
2919*03e1f25dSEric Joyner 	if (!hw->func_caps.common_cap.iwarp || !ice_enable_irdma)
29208a13362dSEric Joyner 		ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
29219c30461dSEric Joyner 	if (!hw->func_caps.common_cap.dcb)
29228923de59SPiotr Kubaj 		ice_clear_bit(ICE_FEATURE_DCB, sc->feat_cap);
29239cf1841cSEric Joyner 	/* Disable features due to firmware limitations... */
29249c30461dSEric Joyner 	if (!ice_is_fw_health_report_supported(hw))
29259cf1841cSEric Joyner 		ice_clear_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
29269c30461dSEric Joyner 	if (!ice_fwlog_supported(hw))
292756429daeSEric Joyner 		ice_clear_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
29289c30461dSEric Joyner 	if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
292956429daeSEric Joyner 		if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_FW_LOGGING))
293056429daeSEric Joyner 			ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_en);
293156429daeSEric Joyner 		else
29329c30461dSEric Joyner 			ice_fwlog_unregister(hw);
293356429daeSEric Joyner 	}
293471d10453SEric Joyner 
293571d10453SEric Joyner 	/* Disable capabilities not supported by the OS */
293671d10453SEric Joyner 	ice_disable_unsupported_features(sc->feat_cap);
293771d10453SEric Joyner 
293871d10453SEric Joyner 	/* RSS is always enabled for iflib */
293971d10453SEric Joyner 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RSS))
294071d10453SEric Joyner 		ice_set_bit(ICE_FEATURE_RSS, sc->feat_en);
29418923de59SPiotr Kubaj 
29428923de59SPiotr Kubaj 	/* Disable features based on sysctl settings */
29438923de59SPiotr Kubaj 	if (!ice_tx_balance_en)
29448923de59SPiotr Kubaj 		ice_clear_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
29459c30461dSEric Joyner 
29469c30461dSEric Joyner 	if (hw->dev_caps.supported_sensors & ICE_SENSOR_SUPPORT_E810_INT_TEMP) {
29479c30461dSEric Joyner 		ice_set_bit(ICE_FEATURE_TEMP_SENSOR, sc->feat_cap);
29489c30461dSEric Joyner 		ice_set_bit(ICE_FEATURE_TEMP_SENSOR, sc->feat_en);
29499c30461dSEric Joyner 	}
2950f2635e84SEric Joyner 
2951f2635e84SEric Joyner 	if (hw->func_caps.common_cap.next_cluster_id_support ||
2952f2635e84SEric Joyner 	    hw->dev_caps.common_cap.next_cluster_id_support) {
2953f2635e84SEric Joyner 		ice_set_bit(ICE_FEATURE_NEXT_CLUSTER_ID, sc->feat_cap);
2954f2635e84SEric Joyner 		ice_set_bit(ICE_FEATURE_NEXT_CLUSTER_ID, sc->feat_en);
2955f2635e84SEric Joyner 	}
295671d10453SEric Joyner }
295771d10453SEric Joyner 
295871d10453SEric Joyner /**
295971d10453SEric Joyner  * ice_if_multi_set - Callback to update Multicast filters in HW
296071d10453SEric Joyner  * @ctx: iflib ctx structure
296171d10453SEric Joyner  *
296271d10453SEric Joyner  * Called by iflib in response to SIOCDELMULTI and SIOCADDMULTI. Must search
296371d10453SEric Joyner  * the if_multiaddrs list and determine which filters have been added or
296471d10453SEric Joyner  * removed from the list, and update HW programming to reflect the new list.
296571d10453SEric Joyner  *
296671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
296771d10453SEric Joyner  */
296871d10453SEric Joyner static void
ice_if_multi_set(if_ctx_t ctx)296971d10453SEric Joyner ice_if_multi_set(if_ctx_t ctx)
297071d10453SEric Joyner {
297171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
297271d10453SEric Joyner 	int err;
297371d10453SEric Joyner 
297471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
297571d10453SEric Joyner 
297671d10453SEric Joyner 	/* Do not handle multicast configuration in recovery mode */
297771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
297871d10453SEric Joyner 		return;
297971d10453SEric Joyner 
298071d10453SEric Joyner 	err = ice_sync_multicast_filters(sc);
298171d10453SEric Joyner 	if (err) {
298271d10453SEric Joyner 		device_printf(sc->dev,
298371d10453SEric Joyner 			      "Failed to synchronize multicast filter list: %s\n",
298471d10453SEric Joyner 			      ice_err_str(err));
298571d10453SEric Joyner 		return;
298671d10453SEric Joyner 	}
298771d10453SEric Joyner }
298871d10453SEric Joyner 
298971d10453SEric Joyner /**
299071d10453SEric Joyner  * ice_if_vlan_register - Register a VLAN with the hardware
299171d10453SEric Joyner  * @ctx: iflib ctx pointer
299271d10453SEric Joyner  * @vtag: VLAN to add
299371d10453SEric Joyner  *
299471d10453SEric Joyner  * Programs the main PF VSI with a hardware filter for the given VLAN.
299571d10453SEric Joyner  *
299671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
299771d10453SEric Joyner  */
299871d10453SEric Joyner static void
ice_if_vlan_register(if_ctx_t ctx,u16 vtag)299971d10453SEric Joyner ice_if_vlan_register(if_ctx_t ctx, u16 vtag)
300071d10453SEric Joyner {
300171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3002f2635e84SEric Joyner 	int status;
300371d10453SEric Joyner 
300471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
300571d10453SEric Joyner 
300671d10453SEric Joyner 	/* Do not handle VLAN configuration in recovery mode */
300771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
300871d10453SEric Joyner 		return;
300971d10453SEric Joyner 
301071d10453SEric Joyner 	status = ice_add_vlan_hw_filter(&sc->pf_vsi, vtag);
301171d10453SEric Joyner 	if (status) {
301271d10453SEric Joyner 		device_printf(sc->dev,
301371d10453SEric Joyner 			      "Failure adding VLAN %d to main VSI, err %s aq_err %s\n",
301471d10453SEric Joyner 			      vtag, ice_status_str(status),
301571d10453SEric Joyner 			      ice_aq_str(sc->hw.adminq.sq_last_status));
301671d10453SEric Joyner 	}
301771d10453SEric Joyner }
301871d10453SEric Joyner 
301971d10453SEric Joyner /**
302071d10453SEric Joyner  * ice_if_vlan_unregister - Remove a VLAN filter from the hardware
302171d10453SEric Joyner  * @ctx: iflib ctx pointer
302271d10453SEric Joyner  * @vtag: VLAN to add
302371d10453SEric Joyner  *
302471d10453SEric Joyner  * Removes the previously programmed VLAN filter from the main PF VSI.
302571d10453SEric Joyner  *
302671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
302771d10453SEric Joyner  */
302871d10453SEric Joyner static void
ice_if_vlan_unregister(if_ctx_t ctx,u16 vtag)302971d10453SEric Joyner ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
303071d10453SEric Joyner {
303171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3032f2635e84SEric Joyner 	int status;
303371d10453SEric Joyner 
303471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
303571d10453SEric Joyner 
303671d10453SEric Joyner 	/* Do not handle VLAN configuration in recovery mode */
303771d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
303871d10453SEric Joyner 		return;
303971d10453SEric Joyner 
304071d10453SEric Joyner 	status = ice_remove_vlan_hw_filter(&sc->pf_vsi, vtag);
304171d10453SEric Joyner 	if (status) {
304271d10453SEric Joyner 		device_printf(sc->dev,
304371d10453SEric Joyner 			      "Failure removing VLAN %d from main VSI, err %s aq_err %s\n",
304471d10453SEric Joyner 			      vtag, ice_status_str(status),
304571d10453SEric Joyner 			      ice_aq_str(sc->hw.adminq.sq_last_status));
304671d10453SEric Joyner 	}
304771d10453SEric Joyner }
304871d10453SEric Joyner 
304971d10453SEric Joyner /**
305071d10453SEric Joyner  * ice_if_stop - Stop the device
305171d10453SEric Joyner  * @ctx: iflib context structure
305271d10453SEric Joyner  *
305371d10453SEric Joyner  * Called by iflib to stop the device and bring it down. (i.e. ifconfig ice0
305471d10453SEric Joyner  * down)
305571d10453SEric Joyner  *
305671d10453SEric Joyner  * @pre assumes the caller holds the iflib CTX lock
305771d10453SEric Joyner  */
305871d10453SEric Joyner static void
ice_if_stop(if_ctx_t ctx)305971d10453SEric Joyner ice_if_stop(if_ctx_t ctx)
306071d10453SEric Joyner {
30619e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
306271d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
306371d10453SEric Joyner 
306471d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
306571d10453SEric Joyner 
306671d10453SEric Joyner 	/*
306771d10453SEric Joyner 	 * The iflib core may call IFDI_STOP prior to the first call to
306871d10453SEric Joyner 	 * IFDI_INIT. This will cause us to attempt to remove MAC filters we
306971d10453SEric Joyner 	 * don't have, and disable Tx queues which aren't yet configured.
307071d10453SEric Joyner 	 * Although it is likely these extra operations are harmless, they do
307171d10453SEric Joyner 	 * cause spurious warning messages to be displayed, which may confuse
307271d10453SEric Joyner 	 * users.
307371d10453SEric Joyner 	 *
307471d10453SEric Joyner 	 * To avoid these messages, we use a state bit indicating if we've
307571d10453SEric Joyner 	 * been initialized. It will be set when ice_if_init is called, and
307671d10453SEric Joyner 	 * cleared here in ice_if_stop.
307771d10453SEric Joyner 	 */
307871d10453SEric Joyner 	if (!ice_testandclear_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
307971d10453SEric Joyner 		return;
308071d10453SEric Joyner 
308171d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
308271d10453SEric Joyner 		device_printf(sc->dev, "request to stop interface cannot be completed as the device failed to reset\n");
308371d10453SEric Joyner 		return;
308471d10453SEric Joyner 	}
308571d10453SEric Joyner 
308671d10453SEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
308771d10453SEric Joyner 		device_printf(sc->dev, "request to stop interface while device is prepared for impending reset\n");
308871d10453SEric Joyner 		return;
308971d10453SEric Joyner 	}
309071d10453SEric Joyner 
30918a13362dSEric Joyner 	ice_rdma_pf_stop(sc);
30928a13362dSEric Joyner 
309371d10453SEric Joyner 	/* Remove the MAC filters, stop Tx, and stop Rx. We don't check the
309471d10453SEric Joyner 	 * return of these functions because there's nothing we can really do
309571d10453SEric Joyner 	 * if they fail, and the functions already print error messages.
309671d10453SEric Joyner 	 * Just try to shut down as much as we can.
309771d10453SEric Joyner 	 */
309871d10453SEric Joyner 	ice_rm_pf_default_mac_filters(sc);
309971d10453SEric Joyner 
310071d10453SEric Joyner 	/* Dissociate the Tx and Rx queues from the interrupts */
310171d10453SEric Joyner 	ice_flush_txq_interrupts(&sc->pf_vsi);
310271d10453SEric Joyner 	ice_flush_rxq_interrupts(&sc->pf_vsi);
310371d10453SEric Joyner 
310471d10453SEric Joyner 	/* Disable the Tx and Rx queues */
310571d10453SEric Joyner 	ice_vsi_disable_tx(&sc->pf_vsi);
31069dc2f6e2SEric Joyner 	ice_control_all_rx_queues(&sc->pf_vsi, false);
31079c30461dSEric Joyner 
31089c30461dSEric Joyner 	if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
31099c30461dSEric Joyner 		 !(if_getflags(sc->ifp) & IFF_UP) && sc->link_up)
31109c30461dSEric Joyner 		ice_set_link(sc, false);
31119e54973fSEric Joyner 
31129e54973fSEric Joyner 	if (sc->mirr_if && ice_test_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT)) {
31139e54973fSEric Joyner 		ice_subif_if_stop(sc->mirr_if->subctx);
31149e54973fSEric Joyner 		device_printf(sc->dev, "The subinterface also comes down and up after reset\n");
31159e54973fSEric Joyner 	}
311671d10453SEric Joyner }
311771d10453SEric Joyner 
311871d10453SEric Joyner /**
311971d10453SEric Joyner  * ice_if_get_counter - Get current value of an ifnet statistic
312071d10453SEric Joyner  * @ctx: iflib context pointer
312171d10453SEric Joyner  * @counter: ifnet counter to read
312271d10453SEric Joyner  *
312371d10453SEric Joyner  * Reads the current value of an ifnet counter for the device.
312471d10453SEric Joyner  *
312571d10453SEric Joyner  * This function is not protected by the iflib CTX lock.
312671d10453SEric Joyner  */
312771d10453SEric Joyner static uint64_t
ice_if_get_counter(if_ctx_t ctx,ift_counter counter)312871d10453SEric Joyner ice_if_get_counter(if_ctx_t ctx, ift_counter counter)
312971d10453SEric Joyner {
313071d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
313171d10453SEric Joyner 
313271d10453SEric Joyner 	/* Return the counter for the main PF VSI */
313371d10453SEric Joyner 	return ice_get_ifnet_counter(&sc->pf_vsi, counter);
313471d10453SEric Joyner }
313571d10453SEric Joyner 
313671d10453SEric Joyner /**
313771d10453SEric Joyner  * ice_request_stack_reinit - Request that iflib re-initialize
313871d10453SEric Joyner  * @sc: the device private softc
313971d10453SEric Joyner  *
314071d10453SEric Joyner  * Request that the device be brought down and up, to re-initialize. For
314171d10453SEric Joyner  * example, this may be called when a device reset occurs, or when Tx and Rx
314271d10453SEric Joyner  * queues need to be re-initialized.
314371d10453SEric Joyner  *
314471d10453SEric Joyner  * This is required because the iflib state is outside the driver, and must be
314571d10453SEric Joyner  * re-initialized if we need to resart Tx and Rx queues.
314671d10453SEric Joyner  */
314771d10453SEric Joyner void
ice_request_stack_reinit(struct ice_softc * sc)314871d10453SEric Joyner ice_request_stack_reinit(struct ice_softc *sc)
314971d10453SEric Joyner {
315071d10453SEric Joyner 	if (CTX_ACTIVE(sc->ctx)) {
315171d10453SEric Joyner 		iflib_request_reset(sc->ctx);
315271d10453SEric Joyner 		iflib_admin_intr_deferred(sc->ctx);
315371d10453SEric Joyner 	}
315471d10453SEric Joyner }
315571d10453SEric Joyner 
315671d10453SEric Joyner /**
315771d10453SEric Joyner  * ice_driver_is_detaching - Check if the driver is detaching/unloading
315871d10453SEric Joyner  * @sc: device private softc
315971d10453SEric Joyner  *
316071d10453SEric Joyner  * Returns true if the driver is detaching, false otherwise.
316171d10453SEric Joyner  *
316271d10453SEric Joyner  * @remark on newer kernels, take advantage of iflib_in_detach in order to
316371d10453SEric Joyner  * report detachment correctly as early as possible.
316471d10453SEric Joyner  *
316571d10453SEric Joyner  * @remark this function is used by various code paths that want to avoid
316671d10453SEric Joyner  * running if the driver is about to be removed. This includes sysctls and
316771d10453SEric Joyner  * other driver access points. Note that it does not fully resolve
316871d10453SEric Joyner  * detach-based race conditions as it is possible for a thread to race with
316971d10453SEric Joyner  * iflib_in_detach.
317071d10453SEric Joyner  */
317171d10453SEric Joyner bool
ice_driver_is_detaching(struct ice_softc * sc)317271d10453SEric Joyner ice_driver_is_detaching(struct ice_softc *sc)
317371d10453SEric Joyner {
317471d10453SEric Joyner 	return (ice_test_state(&sc->state, ICE_STATE_DETACHING) ||
317571d10453SEric Joyner 		iflib_in_detach(sc->ctx));
317671d10453SEric Joyner }
317771d10453SEric Joyner 
317871d10453SEric Joyner /**
317971d10453SEric Joyner  * ice_if_priv_ioctl - Device private ioctl handler
318071d10453SEric Joyner  * @ctx: iflib context pointer
318171d10453SEric Joyner  * @command: The ioctl command issued
318271d10453SEric Joyner  * @data: ioctl specific data
318371d10453SEric Joyner  *
318471d10453SEric Joyner  * iflib callback for handling custom driver specific ioctls.
318571d10453SEric Joyner  *
318671d10453SEric Joyner  * @pre Assumes that the iflib context lock is held.
318771d10453SEric Joyner  */
318871d10453SEric Joyner static int
ice_if_priv_ioctl(if_ctx_t ctx,u_long command,caddr_t data)318971d10453SEric Joyner ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
319071d10453SEric Joyner {
319171d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
319271d10453SEric Joyner 	struct ifdrv *ifd;
319371d10453SEric Joyner 	device_t dev = sc->dev;
319471d10453SEric Joyner 
319571d10453SEric Joyner 	if (data == NULL)
319671d10453SEric Joyner 		return (EINVAL);
319771d10453SEric Joyner 
319871d10453SEric Joyner 	ASSERT_CTX_LOCKED(sc);
319971d10453SEric Joyner 
320071d10453SEric Joyner 	/* Make sure the command type is valid */
320171d10453SEric Joyner 	switch (command) {
320271d10453SEric Joyner 	case SIOCSDRVSPEC:
320371d10453SEric Joyner 	case SIOCGDRVSPEC:
320471d10453SEric Joyner 		/* Accepted commands */
320571d10453SEric Joyner 		break;
320671d10453SEric Joyner 	case SIOCGPRIVATE_0:
320771d10453SEric Joyner 		/*
320871d10453SEric Joyner 		 * Although we do not support this ioctl command, it's
320971d10453SEric Joyner 		 * expected that iflib will forward it to the IFDI_PRIV_IOCTL
321071d10453SEric Joyner 		 * handler. Do not print a message in this case
321171d10453SEric Joyner 		 */
321271d10453SEric Joyner 		return (ENOTSUP);
321371d10453SEric Joyner 	default:
321471d10453SEric Joyner 		/*
321571d10453SEric Joyner 		 * If we get a different command for this function, it's
321671d10453SEric Joyner 		 * definitely unexpected, so log a message indicating what
321771d10453SEric Joyner 		 * command we got for debugging purposes.
321871d10453SEric Joyner 		 */
321971d10453SEric Joyner 		device_printf(dev, "%s: unexpected ioctl command %08lx\n",
322071d10453SEric Joyner 			      __func__, command);
322171d10453SEric Joyner 		return (EINVAL);
322271d10453SEric Joyner 	}
322371d10453SEric Joyner 
322471d10453SEric Joyner 	ifd = (struct ifdrv *)data;
322571d10453SEric Joyner 
322671d10453SEric Joyner 	switch (ifd->ifd_cmd) {
322771d10453SEric Joyner 	case ICE_NVM_ACCESS:
322871d10453SEric Joyner 		return ice_handle_nvm_access_ioctl(sc, ifd);
32298923de59SPiotr Kubaj 	case ICE_DEBUG_DUMP:
32308923de59SPiotr Kubaj 		return ice_handle_debug_dump_ioctl(sc, ifd);
323171d10453SEric Joyner 	default:
323271d10453SEric Joyner 		return EINVAL;
323371d10453SEric Joyner 	}
323471d10453SEric Joyner }
323571d10453SEric Joyner 
323671d10453SEric Joyner /**
323771d10453SEric Joyner  * ice_if_i2c_req - I2C request handler for iflib
323871d10453SEric Joyner  * @ctx: iflib context pointer
323971d10453SEric Joyner  * @req: The I2C parameters to use
324071d10453SEric Joyner  *
324171d10453SEric Joyner  * Read from the port's I2C eeprom using the parameters from the ioctl.
324271d10453SEric Joyner  *
324371d10453SEric Joyner  * @remark The iflib-only part is pretty simple.
324471d10453SEric Joyner  */
324571d10453SEric Joyner static int
ice_if_i2c_req(if_ctx_t ctx,struct ifi2creq * req)324671d10453SEric Joyner ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
324771d10453SEric Joyner {
324871d10453SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
324971d10453SEric Joyner 
325071d10453SEric Joyner 	return ice_handle_i2c_req(sc, req);
325171d10453SEric Joyner }
325271d10453SEric Joyner 
32537d7af7f8SEric Joyner /**
32547d7af7f8SEric Joyner  * ice_if_suspend - PCI device suspend handler for iflib
32557d7af7f8SEric Joyner  * @ctx: iflib context pointer
32567d7af7f8SEric Joyner  *
32577d7af7f8SEric Joyner  * Deinitializes the driver and clears HW resources in preparation for
32587d7af7f8SEric Joyner  * suspend or an FLR.
32597d7af7f8SEric Joyner  *
32607d7af7f8SEric Joyner  * @returns 0; this return value is ignored
32617d7af7f8SEric Joyner  */
32627d7af7f8SEric Joyner static int
ice_if_suspend(if_ctx_t ctx)32637d7af7f8SEric Joyner ice_if_suspend(if_ctx_t ctx)
32647d7af7f8SEric Joyner {
32657d7af7f8SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
32667d7af7f8SEric Joyner 
32677d7af7f8SEric Joyner 	/* At least a PFR is always going to happen after this;
32687d7af7f8SEric Joyner 	 * either via FLR or during the D3->D0 transition.
32697d7af7f8SEric Joyner 	 */
32707d7af7f8SEric Joyner 	ice_clear_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
32717d7af7f8SEric Joyner 
32727d7af7f8SEric Joyner 	ice_prepare_for_reset(sc);
32737d7af7f8SEric Joyner 
32747d7af7f8SEric Joyner 	return (0);
32757d7af7f8SEric Joyner }
32767d7af7f8SEric Joyner 
32777d7af7f8SEric Joyner /**
32787d7af7f8SEric Joyner  * ice_if_resume - PCI device resume handler for iflib
32797d7af7f8SEric Joyner  * @ctx: iflib context pointer
32807d7af7f8SEric Joyner  *
32817d7af7f8SEric Joyner  * Reinitializes the driver and the HW after PCI resume or after
32827d7af7f8SEric Joyner  * an FLR. An init is performed by iflib after this function is finished.
32837d7af7f8SEric Joyner  *
32847d7af7f8SEric Joyner  * @returns 0; this return value is ignored
32857d7af7f8SEric Joyner  */
32867d7af7f8SEric Joyner static int
ice_if_resume(if_ctx_t ctx)32877d7af7f8SEric Joyner ice_if_resume(if_ctx_t ctx)
32887d7af7f8SEric Joyner {
32897d7af7f8SEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
32907d7af7f8SEric Joyner 
32917d7af7f8SEric Joyner 	ice_rebuild(sc);
32927d7af7f8SEric Joyner 
32937d7af7f8SEric Joyner 	return (0);
32947d7af7f8SEric Joyner }
32957d7af7f8SEric Joyner 
32969c30461dSEric Joyner /**
32979c30461dSEric Joyner  * ice_if_needs_restart - Tell iflib when the driver needs to be reinitialized
32989c30461dSEric Joyner  * @ctx: iflib context pointer
329914a14e36SKevin Bowling  * @event: event code to check
330014a14e36SKevin Bowling  *
33019e54973fSEric Joyner  * Defaults to returning true for unknown events.
330214a14e36SKevin Bowling  *
330314a14e36SKevin Bowling  * @returns true if iflib needs to reinit the interface
330414a14e36SKevin Bowling  */
330514a14e36SKevin Bowling static bool
ice_if_needs_restart(if_ctx_t ctx,enum iflib_restart_event event)33069e54973fSEric Joyner ice_if_needs_restart(if_ctx_t ctx, enum iflib_restart_event event)
330714a14e36SKevin Bowling {
33089e54973fSEric Joyner 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
33099e54973fSEric Joyner 
331014a14e36SKevin Bowling 	switch (event) {
331114a14e36SKevin Bowling 	case IFLIB_RESTART_VLAN_CONFIG:
33129e54973fSEric Joyner 		if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
33139e54973fSEric Joyner 			 !(if_getflags(sc->ifp) & IFF_UP))
33149e54973fSEric Joyner 			return false;
331514a14e36SKevin Bowling 	default:
33169e54973fSEric Joyner 		return true;
331714a14e36SKevin Bowling 	}
331814a14e36SKevin Bowling }
331914a14e36SKevin Bowling 
3320f377a0c7SEric Joyner /**
3321f377a0c7SEric Joyner  * ice_init_link - Do link configuration and link status reporting
3322f377a0c7SEric Joyner  * @sc: driver private structure
3323f377a0c7SEric Joyner  *
3324f377a0c7SEric Joyner  * Contains an extra check that skips link config when an E830 device
3325f377a0c7SEric Joyner  * does not have the "FW_LOADING"/"PHYBUSY" bit set in GL_MNG_FWSM set.
3326f377a0c7SEric Joyner  */
3327f377a0c7SEric Joyner static void
ice_init_link(struct ice_softc * sc)3328f377a0c7SEric Joyner ice_init_link(struct ice_softc *sc)
3329f377a0c7SEric Joyner {
3330f377a0c7SEric Joyner 	struct ice_hw *hw = &sc->hw;
3331f377a0c7SEric Joyner 	device_t dev = sc->dev;
3332f377a0c7SEric Joyner 
3333f377a0c7SEric Joyner 	/* Check if FW is ready before setting up link; defer setup to the
3334f377a0c7SEric Joyner 	 * admin task if it isn't.
3335f377a0c7SEric Joyner 	 */
3336f377a0c7SEric Joyner 	if (ice_is_e830(hw) &&
3337f377a0c7SEric Joyner 	    (rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M)) {
3338f377a0c7SEric Joyner 		ice_set_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
3339f377a0c7SEric Joyner 		device_printf(dev,
3340f377a0c7SEric Joyner 		    "Link initialization is blocked by PHY FW initialization.\n");
3341f377a0c7SEric Joyner 		device_printf(dev,
3342f377a0c7SEric Joyner 		    "Link initialization will continue after PHY FW initialization completes.\n");
3343f377a0c7SEric Joyner 		/* Do not access PHY config while PHY FW is busy initializing */
3344f377a0c7SEric Joyner 	} else {
3345f377a0c7SEric Joyner 		ice_clear_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
3346f377a0c7SEric Joyner 		ice_init_link_configuration(sc);
3347f377a0c7SEric Joyner 		ice_update_link_status(sc, true);
3348f377a0c7SEric Joyner 	}
3349f377a0c7SEric Joyner 
3350f377a0c7SEric Joyner }
3351f377a0c7SEric Joyner 
33529e54973fSEric Joyner extern struct if_txrx ice_subif_txrx;
33539e54973fSEric Joyner 
33549e54973fSEric Joyner /**
33559e54973fSEric Joyner  * @var ice_subif_methods
33569e54973fSEric Joyner  * @brief ice driver method entry points
33579e54973fSEric Joyner  */
33589e54973fSEric Joyner static device_method_t ice_subif_methods[] = {
33599e54973fSEric Joyner 	/* Device interface */
33609e54973fSEric Joyner 	DEVMETHOD(device_register, ice_subif_register),
33619e54973fSEric Joyner 	DEVMETHOD_END
33629e54973fSEric Joyner };
33639e54973fSEric Joyner 
33649e54973fSEric Joyner /**
33659e54973fSEric Joyner  * @var ice_subif_driver
33669e54973fSEric Joyner  * @brief driver structure for the device API
33679e54973fSEric Joyner  */
33689e54973fSEric Joyner static driver_t ice_subif_driver = {
33699e54973fSEric Joyner 	.name = "ice_subif",
33709e54973fSEric Joyner 	.methods = ice_subif_methods,
33719e54973fSEric Joyner 	.size = sizeof(struct ice_mirr_if),
33729e54973fSEric Joyner };
33739e54973fSEric Joyner 
33749e54973fSEric Joyner static device_method_t ice_iflib_subif_methods[] = {
33759e54973fSEric Joyner 	DEVMETHOD(ifdi_attach_pre, ice_subif_if_attach_pre),
33769e54973fSEric Joyner 	DEVMETHOD(ifdi_attach_post, ice_subif_if_attach_post),
33779e54973fSEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ice_subif_if_tx_queues_alloc),
33789e54973fSEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ice_subif_if_rx_queues_alloc),
33799e54973fSEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ice_subif_if_msix_intr_assign),
33809e54973fSEric Joyner 	DEVMETHOD(ifdi_intr_enable, ice_subif_if_intr_enable),
33819e54973fSEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ice_subif_if_rx_queue_intr_enable),
33829e54973fSEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ice_subif_if_tx_queue_intr_enable),
33839e54973fSEric Joyner 	DEVMETHOD(ifdi_init, ice_subif_if_init),
33849e54973fSEric Joyner 	DEVMETHOD(ifdi_stop, ice_subif_if_stop),
33859e54973fSEric Joyner 	DEVMETHOD(ifdi_queues_free, ice_subif_if_queues_free),
33869e54973fSEric Joyner 	DEVMETHOD(ifdi_media_status, ice_subif_if_media_status),
33879e54973fSEric Joyner 	DEVMETHOD(ifdi_promisc_set, ice_subif_if_promisc_set),
33889e54973fSEric Joyner };
33899e54973fSEric Joyner 
33909e54973fSEric Joyner /**
33919e54973fSEric Joyner  * @var ice_iflib_subif_driver
33929e54973fSEric Joyner  * @brief driver structure for the iflib stack
33939e54973fSEric Joyner  *
33949e54973fSEric Joyner  * driver_t definition used to setup the iflib device methods.
33959e54973fSEric Joyner  */
33969e54973fSEric Joyner static driver_t ice_iflib_subif_driver = {
33979e54973fSEric Joyner 	.name = "ice_subif",
33989e54973fSEric Joyner 	.methods = ice_iflib_subif_methods,
33999e54973fSEric Joyner 	.size = sizeof(struct ice_mirr_if),
34009e54973fSEric Joyner };
34019e54973fSEric Joyner 
34029e54973fSEric Joyner /**
34039e54973fSEric Joyner  * @var ice_subif_sctx
34049e54973fSEric Joyner  * @brief ice driver shared context
34059e54973fSEric Joyner  *
34069e54973fSEric Joyner  * Similar to the existing ice_sctx, this structure has these differences:
34079e54973fSEric Joyner  * - isc_admin_intrcnt is set to 0
34089e54973fSEric Joyner  * - Uses subif iflib driver methods
34099e54973fSEric Joyner  * - Flagged as a VF for iflib
34109e54973fSEric Joyner  */
34119e54973fSEric Joyner static struct if_shared_ctx ice_subif_sctx = {
34129e54973fSEric Joyner 	.isc_magic = IFLIB_MAGIC,
34139e54973fSEric Joyner 	.isc_q_align = PAGE_SIZE,
34149e54973fSEric Joyner 
34159e54973fSEric Joyner 	.isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
34169e54973fSEric Joyner 	.isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
34179e54973fSEric Joyner 	.isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
34189e54973fSEric Joyner 	.isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
34199e54973fSEric Joyner 
34209e54973fSEric Joyner 	.isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
34219e54973fSEric Joyner 	.isc_rx_nsegments = ICE_MAX_RX_SEGS,
34229e54973fSEric Joyner 	.isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
34239e54973fSEric Joyner 
34249e54973fSEric Joyner 	.isc_nfl = 1,
34259e54973fSEric Joyner 	.isc_ntxqs = 1,
34269e54973fSEric Joyner 	.isc_nrxqs = 1,
34279e54973fSEric Joyner 
34289e54973fSEric Joyner 	.isc_admin_intrcnt = 0,
34299e54973fSEric Joyner 	.isc_vendor_info = ice_vendor_info_array,
34309e54973fSEric Joyner 	.isc_driver_version = __DECONST(char *, ice_driver_version),
34319e54973fSEric Joyner 	.isc_driver = &ice_iflib_subif_driver,
34329e54973fSEric Joyner 
34339e54973fSEric Joyner 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
34349e54973fSEric Joyner 		IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX |
34359e54973fSEric Joyner 		IFLIB_IS_VF,
34369e54973fSEric Joyner 
34379e54973fSEric Joyner 	.isc_nrxd_min = {ICE_MIN_DESC_COUNT},
34389e54973fSEric Joyner 	.isc_ntxd_min = {ICE_MIN_DESC_COUNT},
34399e54973fSEric Joyner 	.isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
34409e54973fSEric Joyner 	.isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
34419e54973fSEric Joyner 	.isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
34429e54973fSEric Joyner 	.isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
34439e54973fSEric Joyner };
34449e54973fSEric Joyner 
34459e54973fSEric Joyner static void *
ice_subif_register(device_t dev __unused)34469e54973fSEric Joyner ice_subif_register(device_t dev __unused)
34479e54973fSEric Joyner {
34489e54973fSEric Joyner 	return (&ice_subif_sctx);
34499e54973fSEric Joyner }
34509e54973fSEric Joyner 
34519e54973fSEric Joyner static void
ice_subif_setup_scctx(struct ice_mirr_if * mif)34529e54973fSEric Joyner ice_subif_setup_scctx(struct ice_mirr_if *mif)
34539e54973fSEric Joyner {
34549e54973fSEric Joyner 	if_softc_ctx_t scctx = mif->subscctx;
34559e54973fSEric Joyner 
34569e54973fSEric Joyner 	scctx->isc_txrx = &ice_subif_txrx;
34579e54973fSEric Joyner 
34589e54973fSEric Joyner 	scctx->isc_capenable = ICE_FULL_CAPS;
34599e54973fSEric Joyner 	scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
34609e54973fSEric Joyner 
34619e54973fSEric Joyner 	scctx->isc_ntxqsets = 4;
34629e54973fSEric Joyner 	scctx->isc_nrxqsets = 4;
34639e54973fSEric Joyner 	scctx->isc_vectors = scctx->isc_nrxqsets;
34649e54973fSEric Joyner 
34659e54973fSEric Joyner 	scctx->isc_ntxqsets_max = 256;
34669e54973fSEric Joyner 	scctx->isc_nrxqsets_max = 256;
34679e54973fSEric Joyner 
34689e54973fSEric Joyner 	scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
34699e54973fSEric Joyner 	    * sizeof(struct ice_tx_desc), DBA_ALIGN);
34709e54973fSEric Joyner 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
34719e54973fSEric Joyner 	    * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
34729e54973fSEric Joyner 
34739e54973fSEric Joyner 	scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
34749e54973fSEric Joyner 	scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
34759e54973fSEric Joyner 	scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
34769e54973fSEric Joyner 	scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
34779e54973fSEric Joyner }
34789e54973fSEric Joyner 
34799e54973fSEric Joyner static int
ice_subif_if_attach_pre(if_ctx_t ctx)34809e54973fSEric Joyner ice_subif_if_attach_pre(if_ctx_t ctx)
34819e54973fSEric Joyner {
34829e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
34839e54973fSEric Joyner 	device_t dev = iflib_get_dev(ctx);
34849e54973fSEric Joyner 
34859e54973fSEric Joyner 	mif->subctx = ctx;
34869e54973fSEric Joyner 	mif->subdev = dev;
34879e54973fSEric Joyner 	mif->subscctx = iflib_get_softc_ctx(ctx);
34889e54973fSEric Joyner 
34899e54973fSEric Joyner 	/* Setup the iflib softc context structure */
34909e54973fSEric Joyner 	ice_subif_setup_scctx(mif);
34919e54973fSEric Joyner 
34929e54973fSEric Joyner 	return (0);
34939e54973fSEric Joyner }
34949e54973fSEric Joyner 
34959e54973fSEric Joyner static int
ice_subif_if_attach_post(if_ctx_t ctx __unused)34969e54973fSEric Joyner ice_subif_if_attach_post(if_ctx_t ctx __unused)
34979e54973fSEric Joyner {
34989e54973fSEric Joyner 	return (0);
34999e54973fSEric Joyner }
35009e54973fSEric Joyner 
35019e54973fSEric Joyner /**
35029e54973fSEric Joyner  * ice_destroy_mirror_interface - destroy mirror interface
35039e54973fSEric Joyner  * @sc: driver private data
35049e54973fSEric Joyner  *
35059e54973fSEric Joyner  * Destroys all resources associated with the mirroring interface.
35069e54973fSEric Joyner  * Will not exit early on failure.
35079e54973fSEric Joyner  *
35089e54973fSEric Joyner  * @pre: Mirror interface already exists and is initialized.
35099e54973fSEric Joyner  */
35109e54973fSEric Joyner void
ice_destroy_mirror_interface(struct ice_softc * sc)35119e54973fSEric Joyner ice_destroy_mirror_interface(struct ice_softc *sc)
35129e54973fSEric Joyner {
35139e54973fSEric Joyner 	struct ice_mirr_if *mif = sc->mirr_if;
35149e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
35159e54973fSEric Joyner 	bool is_locked = false;
35169e54973fSEric Joyner 	int ret;
35179e54973fSEric Joyner 
35189e54973fSEric Joyner 	is_locked = sx_xlocked(sc->iflib_ctx_lock);
35199e54973fSEric Joyner 	if (is_locked)
35209e54973fSEric Joyner 		IFLIB_CTX_UNLOCK(sc);
35219e54973fSEric Joyner 
35229e54973fSEric Joyner 	if (mif->ifp) {
35239e54973fSEric Joyner 		ret = iflib_device_deregister(mif->subctx);
35249e54973fSEric Joyner 		if (ret) {
35259e54973fSEric Joyner 			device_printf(sc->dev,
35269e54973fSEric Joyner 			    "iflib_device_deregister for mirror interface failed: %d\n",
35279e54973fSEric Joyner 			    ret);
35289e54973fSEric Joyner 		}
35299e54973fSEric Joyner 	}
35309e54973fSEric Joyner 
35319e54973fSEric Joyner 	bus_topo_lock();
35329e54973fSEric Joyner 	ret = device_delete_child(sc->dev, mif->subdev);
35339e54973fSEric Joyner 	bus_topo_unlock();
35349e54973fSEric Joyner 	if (ret) {
35359e54973fSEric Joyner 		device_printf(sc->dev,
35369e54973fSEric Joyner 		    "device_delete_child for mirror interface failed: %d\n",
35379e54973fSEric Joyner 		    ret);
35389e54973fSEric Joyner 	}
35399e54973fSEric Joyner 
35409e54973fSEric Joyner 	if (is_locked)
35419e54973fSEric Joyner 		IFLIB_CTX_LOCK(sc);
35429e54973fSEric Joyner 
35439e54973fSEric Joyner 	if (mif->if_imap) {
35449e54973fSEric Joyner 		free(mif->if_imap, M_ICE);
35459e54973fSEric Joyner 		mif->if_imap = NULL;
35469e54973fSEric Joyner 	}
35479e54973fSEric Joyner 	if (mif->os_imap) {
35489e54973fSEric Joyner 		free(mif->os_imap, M_ICE);
35499e54973fSEric Joyner 		mif->os_imap = NULL;
35509e54973fSEric Joyner 	}
35519e54973fSEric Joyner 
35529e54973fSEric Joyner 	/* These are freed via ice_subif_queues_free_subif
35539e54973fSEric Joyner 	 * vsi:
35549e54973fSEric Joyner 	 * - rx_irqvs
35559e54973fSEric Joyner 	 * - tx_queues
35569e54973fSEric Joyner 	 * - rx_queues
35579e54973fSEric Joyner 	 */
35589e54973fSEric Joyner 	ice_release_vsi(vsi);
35599e54973fSEric Joyner 
35609e54973fSEric Joyner 	free(mif, M_ICE);
35619e54973fSEric Joyner 	sc->mirr_if = NULL;
35629e54973fSEric Joyner 
35639e54973fSEric Joyner }
35649e54973fSEric Joyner 
35659e54973fSEric Joyner /**
35669e54973fSEric Joyner  * ice_setup_mirror_vsi - Initialize mirror VSI
35679e54973fSEric Joyner  * @mif: driver private data for mirror interface
35689e54973fSEric Joyner  *
35699e54973fSEric Joyner  * Allocates a VSI for a mirror interface, and sets that VSI up for use as a
35709e54973fSEric Joyner  * mirror for the main PF VSI.
35719e54973fSEric Joyner  *
35729e54973fSEric Joyner  * Returns 0 on success, or a standard error code on failure.
35739e54973fSEric Joyner  */
35749e54973fSEric Joyner static int
ice_setup_mirror_vsi(struct ice_mirr_if * mif)35759e54973fSEric Joyner ice_setup_mirror_vsi(struct ice_mirr_if *mif)
35769e54973fSEric Joyner {
35779e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
35789e54973fSEric Joyner 	device_t dev = sc->dev;
35799e54973fSEric Joyner 	struct ice_vsi *vsi;
35809e54973fSEric Joyner 	int ret = 0;
35819e54973fSEric Joyner 
35829e54973fSEric Joyner 	/* vsi is for the new mirror vsi, not the PF's main VSI */
35839e54973fSEric Joyner 	vsi = ice_alloc_vsi(sc, ICE_VSI_VMDQ2);
35849e54973fSEric Joyner 	if (!vsi) {
35859e54973fSEric Joyner 		/* Already prints an error message */
35869e54973fSEric Joyner 		return (ENOMEM);
35879e54973fSEric Joyner 	}
35889e54973fSEric Joyner 	mif->vsi = vsi;
35899e54973fSEric Joyner 
35909e54973fSEric Joyner 	/* Reserve VSI queue allocation from PF queues */
35915f97656fSZhenlei Huang 	ice_alloc_vsi_qmap(vsi, ICE_DEFAULT_VF_QUEUES, ICE_DEFAULT_VF_QUEUES);
35929e54973fSEric Joyner 	vsi->num_tx_queues = vsi->num_rx_queues = ICE_DEFAULT_VF_QUEUES;
35939e54973fSEric Joyner 
35949e54973fSEric Joyner 	/* Assign Tx queues from PF space */
35959e54973fSEric Joyner 	ret = ice_resmgr_assign_scattered(&sc->tx_qmgr, vsi->tx_qmap,
35969e54973fSEric Joyner 	    vsi->num_tx_queues);
35979e54973fSEric Joyner 	if (ret) {
35989e54973fSEric Joyner 		device_printf(dev, "Unable to assign mirror VSI Tx queues: %s\n",
35999e54973fSEric Joyner 		    ice_err_str(ret));
36009e54973fSEric Joyner 		goto release_vsi;
36019e54973fSEric Joyner 	}
36029e54973fSEric Joyner 	/* Assign Rx queues from PF space */
36039e54973fSEric Joyner 	ret = ice_resmgr_assign_scattered(&sc->rx_qmgr, vsi->rx_qmap,
36049e54973fSEric Joyner 	    vsi->num_rx_queues);
36059e54973fSEric Joyner 	if (ret) {
36069e54973fSEric Joyner 		device_printf(dev, "Unable to assign mirror VSI Rx queues: %s\n",
36079e54973fSEric Joyner 		    ice_err_str(ret));
36089e54973fSEric Joyner 		goto release_vsi;
36099e54973fSEric Joyner 	}
36109e54973fSEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_SCATTERED;
36119e54973fSEric Joyner 	vsi->max_frame_size = ICE_MAX_FRAME_SIZE;
36129e54973fSEric Joyner 
36139e54973fSEric Joyner 	ret = ice_initialize_vsi(vsi);
36149e54973fSEric Joyner 	if (ret) {
36159e54973fSEric Joyner 		device_printf(dev, "%s: Error in ice_initialize_vsi for mirror VSI: %s\n",
36169e54973fSEric Joyner 		    __func__, ice_err_str(ret));
36179e54973fSEric Joyner 		goto release_vsi;
36189e54973fSEric Joyner 	}
36199e54973fSEric Joyner 
36209e54973fSEric Joyner 	/* Setup this VSI for receiving traffic */
36219e54973fSEric Joyner 	ret = ice_config_rss(vsi);
36229e54973fSEric Joyner 	if (ret) {
36239e54973fSEric Joyner 		device_printf(dev,
36249e54973fSEric Joyner 		    "Unable to configure RSS for mirror VSI: %s\n",
36259e54973fSEric Joyner 		    ice_err_str(ret));
36269e54973fSEric Joyner 		goto release_vsi;
36279e54973fSEric Joyner 	}
36289e54973fSEric Joyner 
36299e54973fSEric Joyner 	/* Set HW rules for mirroring traffic */
36309e54973fSEric Joyner 	vsi->mirror_src_vsi = sc->pf_vsi.idx;
36319e54973fSEric Joyner 
36329e54973fSEric Joyner 	ice_debug(&sc->hw, ICE_DBG_INIT,
36339e54973fSEric Joyner 	    "Configuring mirroring from VSI %d to %d\n",
36349e54973fSEric Joyner 	    vsi->mirror_src_vsi, vsi->idx);
36359e54973fSEric Joyner 	ice_debug(&sc->hw, ICE_DBG_INIT, "(HW num: VSI %d to %d)\n",
36369e54973fSEric Joyner 	    ice_get_hw_vsi_num(&sc->hw, vsi->mirror_src_vsi),
36379e54973fSEric Joyner 	    ice_get_hw_vsi_num(&sc->hw, vsi->idx));
36389e54973fSEric Joyner 
36399e54973fSEric Joyner 	ret = ice_setup_vsi_mirroring(vsi);
36409e54973fSEric Joyner 	if (ret) {
36419e54973fSEric Joyner 		device_printf(dev,
36429e54973fSEric Joyner 		    "Unable to configure mirroring for VSI: %s\n",
36439e54973fSEric Joyner 		    ice_err_str(ret));
36449e54973fSEric Joyner 		goto release_vsi;
36459e54973fSEric Joyner 	}
36469e54973fSEric Joyner 
36479e54973fSEric Joyner 	return (0);
36489e54973fSEric Joyner 
36499e54973fSEric Joyner release_vsi:
36509e54973fSEric Joyner 	ice_release_vsi(vsi);
36519e54973fSEric Joyner 	mif->vsi = NULL;
36529e54973fSEric Joyner 	return (ret);
36539e54973fSEric Joyner }
36549e54973fSEric Joyner 
36559e54973fSEric Joyner /**
36569e54973fSEric Joyner  * ice_create_mirror_interface - Initialize mirror interface
36579e54973fSEric Joyner  * @sc: driver private data
36589e54973fSEric Joyner  *
36599e54973fSEric Joyner  * Creates and sets up a mirror interface that will mirror traffic from
36609e54973fSEric Joyner  * the main PF interface. Includes a call to iflib_device_register() in order
36619e54973fSEric Joyner  * to setup necessary iflib structures for this new interface as well.
36629e54973fSEric Joyner  *
36639e54973fSEric Joyner  * If it returns successfully, a new interface will be created and will show
36649e54973fSEric Joyner  * up in the ifconfig interface list.
36659e54973fSEric Joyner  *
36669e54973fSEric Joyner  * Returns 0 on success, or a standard error code on failure.
36679e54973fSEric Joyner  */
36689e54973fSEric Joyner int
ice_create_mirror_interface(struct ice_softc * sc)36699e54973fSEric Joyner ice_create_mirror_interface(struct ice_softc *sc)
36709e54973fSEric Joyner {
36719e54973fSEric Joyner 	device_t dev = sc->dev;
36729e54973fSEric Joyner 	struct ice_mirr_if *mif;
36739e54973fSEric Joyner 	struct ifmedia *media;
36749e54973fSEric Joyner 	struct sbuf *sb;
36759e54973fSEric Joyner 	int ret = 0;
36769e54973fSEric Joyner 
36779e54973fSEric Joyner 	mif = (struct ice_mirr_if *)malloc(sizeof(*mif), M_ICE, M_ZERO | M_NOWAIT);
36789e54973fSEric Joyner 	if (!mif) {
36799e54973fSEric Joyner 		device_printf(dev, "malloc() error allocating mirror interface\n");
36809e54973fSEric Joyner 		return (ENOMEM);
36819e54973fSEric Joyner 	}
36829e54973fSEric Joyner 
36839e54973fSEric Joyner 	/* Set pointers */
36849e54973fSEric Joyner 	sc->mirr_if = mif;
36859e54973fSEric Joyner 	mif->back = sc;
36869e54973fSEric Joyner 
36879e54973fSEric Joyner 	/* Do early setup because these will be called during iflib_device_register():
36889e54973fSEric Joyner 	 * - ice_subif_if_tx_queues_alloc
36899e54973fSEric Joyner 	 * - ice_subif_if_rx_queues_alloc
36909e54973fSEric Joyner 	 */
36919e54973fSEric Joyner 	ret = ice_setup_mirror_vsi(mif);
36929e54973fSEric Joyner 	if (ret)
36939e54973fSEric Joyner 		goto out;
36949e54973fSEric Joyner 
36959e54973fSEric Joyner 	/* Determine name for new interface:
36969e54973fSEric Joyner 	 * (base interface name)(modifier name)(modifier unit number)
36979e54973fSEric Joyner 	 * e.g. for ice0 with a new mirror interface (modifier m)
36989e54973fSEric Joyner 	 * of index 0, this equals "ice0m0"
36999e54973fSEric Joyner 	 */
37009e54973fSEric Joyner 	sb = sbuf_new_auto();
37019e54973fSEric Joyner 	MPASS(sb != NULL);
37029e54973fSEric Joyner 	sbuf_printf(sb, "%sm", device_get_nameunit(dev));
37039e54973fSEric Joyner 	sbuf_finish(sb);
37049e54973fSEric Joyner 
37059e54973fSEric Joyner 	bus_topo_lock();
37069e54973fSEric Joyner 	mif->subdev = device_add_child(dev, sbuf_data(sb), 0);
37079e54973fSEric Joyner 	bus_topo_unlock();
37089e54973fSEric Joyner 
37099e54973fSEric Joyner 	if (!mif->subdev) {
37109e54973fSEric Joyner 		device_printf(dev, "device_add_child failed for %s0\n", sbuf_data(sb));
37119e54973fSEric Joyner 		sbuf_delete(sb);
37129e54973fSEric Joyner 		free(mif, M_ICE);
37139e54973fSEric Joyner 		sc->mirr_if = NULL;
37149e54973fSEric Joyner 		return (ENOMEM);
37159e54973fSEric Joyner 	}
37169e54973fSEric Joyner 	sbuf_delete(sb);
37179e54973fSEric Joyner 
37189e54973fSEric Joyner 	device_set_driver(mif->subdev, &ice_subif_driver);
37199e54973fSEric Joyner 
37209e54973fSEric Joyner 	/* Use iflib_device_register() directly because the driver already
37219e54973fSEric Joyner 	 * has an initialized softc to pass to iflib
37229e54973fSEric Joyner 	 */
37239e54973fSEric Joyner 	ret = iflib_device_register(mif->subdev, mif, &ice_subif_sctx, &mif->subctx);
37249e54973fSEric Joyner 	if (ret)
37259e54973fSEric Joyner 		goto out;
37269e54973fSEric Joyner 
37279e54973fSEric Joyner 	/* Indicate that created interface will be just for monitoring */
37289e54973fSEric Joyner 	mif->ifp = iflib_get_ifp(mif->subctx);
37299e54973fSEric Joyner 	if_setflagbits(mif->ifp, IFF_MONITOR, 0);
37309e54973fSEric Joyner 
37319e54973fSEric Joyner 	/* Use autoselect media by default */
37329e54973fSEric Joyner 	media = iflib_get_media(mif->subctx);
37339e54973fSEric Joyner 	ifmedia_add(media, IFM_ETHER | IFM_AUTO, 0, NULL);
37349e54973fSEric Joyner 	ifmedia_set(media, IFM_ETHER | IFM_AUTO);
37359e54973fSEric Joyner 
37369e54973fSEric Joyner 	device_printf(dev, "Created dev %s and ifnet %s for mirroring\n",
37379e54973fSEric Joyner 	    device_get_nameunit(mif->subdev), if_name(mif->ifp));
37389e54973fSEric Joyner 
37399e54973fSEric Joyner 	ice_add_vsi_sysctls(mif->vsi);
37409e54973fSEric Joyner 
37419e54973fSEric Joyner 	ret = ice_wire_mirror_intrs(mif);
37429e54973fSEric Joyner 	if (ret)
37439e54973fSEric Joyner 		goto out;
37449e54973fSEric Joyner 
37459e54973fSEric Joyner 	mif->if_attached = true;
37469e54973fSEric Joyner 	return (0);
37479e54973fSEric Joyner 
37489e54973fSEric Joyner out:
37499e54973fSEric Joyner 	ice_destroy_mirror_interface(sc);
37509e54973fSEric Joyner 	return (ret);
37519e54973fSEric Joyner }
37529e54973fSEric Joyner 
37539e54973fSEric Joyner /**
37549e54973fSEric Joyner  * ice_wire_mirror_intrs
37559e54973fSEric Joyner  * @mif: driver private subinterface structure
37569e54973fSEric Joyner  *
37579e54973fSEric Joyner  * Helper function that sets up driver interrupt data and calls
37589e54973fSEric Joyner  * into iflib in order to setup interrupts in its data structures as well.
37599e54973fSEric Joyner  *
37609e54973fSEric Joyner  * Like ice_if_msix_intr_assign, currently requires that we get at least the same
37619e54973fSEric Joyner  * number of vectors as we have queues, and that we always have the same number
37629e54973fSEric Joyner  * of Tx and Rx queues. Unlike that function, this calls a special
37639e54973fSEric Joyner  * iflib_irq_alloc_generic_subif() function for RX interrupts because the
37649e54973fSEric Joyner  * driver needs to get MSI-X resources from the parent device.
37659e54973fSEric Joyner  *
37669e54973fSEric Joyner  * Tx queues use a softirq instead of using their own hardware interrupt so that
37679e54973fSEric Joyner  * remains unchanged.
37689e54973fSEric Joyner  *
37699e54973fSEric Joyner  * Returns 0 on success or an error code from iflib_irq_alloc_generic_subctx()
37709e54973fSEric Joyner  * on failure.
37719e54973fSEric Joyner  */
37729e54973fSEric Joyner static int
ice_wire_mirror_intrs(struct ice_mirr_if * mif)37739e54973fSEric Joyner ice_wire_mirror_intrs(struct ice_mirr_if *mif)
37749e54973fSEric Joyner {
37759e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
37769e54973fSEric Joyner 	struct ice_hw *hw = &sc->hw;
37779e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
37789e54973fSEric Joyner 	device_t dev = mif->subdev;
37799e54973fSEric Joyner 	int err, i, rid;
37809e54973fSEric Joyner 
37819e54973fSEric Joyner 	if_ctx_t ctx = mif->subctx;
37829e54973fSEric Joyner 
37839e54973fSEric Joyner 	ice_debug(hw, ICE_DBG_INIT, "%s: Last rid: %d\n", __func__, sc->last_rid);
37849e54973fSEric Joyner 
37859e54973fSEric Joyner 	rid = sc->last_rid + 1;
37869e54973fSEric Joyner 	for (i = 0; i < vsi->num_rx_queues; i++, rid++) {
37879e54973fSEric Joyner 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
37889e54973fSEric Joyner 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
37899e54973fSEric Joyner 		char irq_name[16];
37909e54973fSEric Joyner 
37919e54973fSEric Joyner 		// TODO: Change to use dynamic interface number
37929e54973fSEric Joyner 		snprintf(irq_name, sizeof(irq_name), "m0rxq%d", i);
37939e54973fSEric Joyner 		/* First arg is parent device (physical port's) iflib ctx */
37949e54973fSEric Joyner 		err = iflib_irq_alloc_generic_subctx(sc->ctx, ctx,
37959e54973fSEric Joyner 		    &mif->rx_irqvs[i].irq, rid, IFLIB_INTR_RXTX, ice_msix_que,
37969e54973fSEric Joyner 		    rxq, rxq->me, irq_name);
37979e54973fSEric Joyner 		if (err) {
37989e54973fSEric Joyner 			device_printf(dev,
37999e54973fSEric Joyner 			    "Failed to allocate q int %d err: %s\n",
38009e54973fSEric Joyner 			    i, ice_err_str(err));
38019e54973fSEric Joyner 			i--;
38029e54973fSEric Joyner 			goto fail;
38039e54973fSEric Joyner 		}
38049e54973fSEric Joyner 		MPASS(rid - 1 > 0);
38059e54973fSEric Joyner 		/* Set vector number used in interrupt enable/disable functions */
38069e54973fSEric Joyner 		mif->rx_irqvs[i].me = rid - 1;
38079e54973fSEric Joyner 		rxq->irqv = &mif->rx_irqvs[i];
38089e54973fSEric Joyner 
38099e54973fSEric Joyner 		bzero(irq_name, sizeof(irq_name));
38109e54973fSEric Joyner 		snprintf(irq_name, sizeof(irq_name), "m0txq%d", i);
38119e54973fSEric Joyner 		iflib_softirq_alloc_generic(ctx, &mif->rx_irqvs[i].irq,
38129e54973fSEric Joyner 		    IFLIB_INTR_TX, txq, txq->me, irq_name);
38139e54973fSEric Joyner 		txq->irqv = &mif->rx_irqvs[i];
38149e54973fSEric Joyner 	}
38159e54973fSEric Joyner 
38169e54973fSEric Joyner 	sc->last_rid = rid - 1;
38179e54973fSEric Joyner 
38189e54973fSEric Joyner 	ice_debug(hw, ICE_DBG_INIT, "%s: New last rid: %d\n", __func__,
38199e54973fSEric Joyner 	    sc->last_rid);
38209e54973fSEric Joyner 
38219e54973fSEric Joyner 	return (0);
38229e54973fSEric Joyner 
38239e54973fSEric Joyner fail:
38249e54973fSEric Joyner 	for (; i >= 0; i--)
38259e54973fSEric Joyner 		iflib_irq_free(ctx, &mif->rx_irqvs[i].irq);
38269e54973fSEric Joyner 	return (err);
38279e54973fSEric Joyner }
38289e54973fSEric Joyner 
38299e54973fSEric Joyner /**
38309e54973fSEric Joyner  * ice_subif_rebuild - Rebuild subinterface post reset
38319e54973fSEric Joyner  * @sc: The device private softc
38329e54973fSEric Joyner  *
38339e54973fSEric Joyner  * Restore subinterface state after a reset occurred.
38349e54973fSEric Joyner  * Restart the VSI and enable the mirroring.
38359e54973fSEric Joyner  */
38369e54973fSEric Joyner static int
ice_subif_rebuild(struct ice_softc * sc)38379e54973fSEric Joyner ice_subif_rebuild(struct ice_softc *sc)
38389e54973fSEric Joyner {
38399e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(sc->ctx);
38409e54973fSEric Joyner 	struct ice_vsi *vsi = sc->mirr_if->vsi;
38419e54973fSEric Joyner 	int err;
38429e54973fSEric Joyner 
38439e54973fSEric Joyner 	err = ice_subif_rebuild_vsi_qmap(sc);
38449e54973fSEric Joyner 	if (err) {
38459e54973fSEric Joyner 		device_printf(sc->dev, "Unable to re-assign mirror VSI queues, err %s\n",
38469e54973fSEric Joyner 		      ice_err_str(err));
38479e54973fSEric Joyner 		return (err);
38489e54973fSEric Joyner 	}
38499e54973fSEric Joyner 
38509e54973fSEric Joyner 	err = ice_initialize_vsi(vsi);
38519e54973fSEric Joyner 	if (err) {
38529e54973fSEric Joyner 		device_printf(sc->dev, "Unable to re-initialize mirror VSI, err %s\n",
38539e54973fSEric Joyner 		      ice_err_str(err));
38549e54973fSEric Joyner 		goto err_release_queue_allocations_subif;
38559e54973fSEric Joyner 	}
38569e54973fSEric Joyner 
38579e54973fSEric Joyner 	err = ice_config_rss(vsi);
38589e54973fSEric Joyner 	if (err) {
38599e54973fSEric Joyner 		device_printf(sc->dev,
38609e54973fSEric Joyner 		      "Unable to reconfigure RSS for the mirror VSI, err %s\n",
38619e54973fSEric Joyner 		      ice_err_str(err));
38629e54973fSEric Joyner 		goto err_deinit_subif_vsi;
38639e54973fSEric Joyner 	}
38649e54973fSEric Joyner 
38659e54973fSEric Joyner 	vsi->mirror_src_vsi = sc->pf_vsi.idx;
38669e54973fSEric Joyner 
38679e54973fSEric Joyner 	err = ice_setup_vsi_mirroring(vsi);
38689e54973fSEric Joyner 	if (err) {
38699e54973fSEric Joyner 		device_printf(sc->dev,
38709e54973fSEric Joyner 		      "Unable to configure mirroring for VSI: %s\n",
38719e54973fSEric Joyner 		      ice_err_str(err));
38729e54973fSEric Joyner 		goto err_deinit_subif_vsi;
38739e54973fSEric Joyner 	}
38749e54973fSEric Joyner 
38759e54973fSEric Joyner 	ice_set_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT);
38769e54973fSEric Joyner 
38779e54973fSEric Joyner 	return (0);
38789e54973fSEric Joyner 
38799e54973fSEric Joyner err_deinit_subif_vsi:
38809e54973fSEric Joyner 	ice_deinit_vsi(vsi);
38819e54973fSEric Joyner err_release_queue_allocations_subif:
38829e54973fSEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, vsi->tx_qmap,
38839e54973fSEric Joyner 	    sc->mirr_if->num_irq_vectors);
38849e54973fSEric Joyner 	ice_resmgr_release_map(&sc->rx_qmgr, vsi->rx_qmap,
38859e54973fSEric Joyner 	    sc->mirr_if->num_irq_vectors);
38869e54973fSEric Joyner 
38879e54973fSEric Joyner 	return (err);
38889e54973fSEric Joyner }
38899e54973fSEric Joyner 
38909e54973fSEric Joyner /**
38919e54973fSEric Joyner  * ice_subif_rebuild_vsi_qmap - Rebuild the mirror VSI queue mapping
38929e54973fSEric Joyner  * @sc: the device softc pointer
38939e54973fSEric Joyner  *
38949e54973fSEric Joyner  * Loops over the Tx and Rx queues for the mirror VSI and reassigns the queue
38959e54973fSEric Joyner  * mapping after a reset occurred.
38969e54973fSEric Joyner  */
38979e54973fSEric Joyner static int
ice_subif_rebuild_vsi_qmap(struct ice_softc * sc)38989e54973fSEric Joyner ice_subif_rebuild_vsi_qmap(struct ice_softc *sc)
38999e54973fSEric Joyner {
39009e54973fSEric Joyner 	struct ice_vsi *vsi = sc->mirr_if->vsi;
39019e54973fSEric Joyner 	struct ice_tx_queue *txq;
39029e54973fSEric Joyner 	struct ice_rx_queue *rxq;
39039e54973fSEric Joyner 	int err, i;
39049e54973fSEric Joyner 
39059e54973fSEric Joyner 	err = ice_resmgr_assign_scattered(&sc->tx_qmgr, vsi->tx_qmap, sc->mirr_if->num_irq_vectors);
39069e54973fSEric Joyner 	if (err) {
39079e54973fSEric Joyner 		device_printf(sc->dev, "Unable to assign mirror VSI Tx queues: %s\n",
39089e54973fSEric Joyner 		      ice_err_str(err));
39099e54973fSEric Joyner 		return (err);
39109e54973fSEric Joyner 	}
39119e54973fSEric Joyner 
39129e54973fSEric Joyner 	err = ice_resmgr_assign_scattered(&sc->rx_qmgr, vsi->rx_qmap, sc->mirr_if->num_irq_vectors);
39139e54973fSEric Joyner 	if (err) {
39149e54973fSEric Joyner 		device_printf(sc->dev, "Unable to assign mirror VSI Rx queues: %s\n",
39159e54973fSEric Joyner 		      ice_err_str(err));
39169e54973fSEric Joyner 		goto err_release_tx_queues;
39179e54973fSEric Joyner 	}
39189e54973fSEric Joyner 
39199e54973fSEric Joyner 	vsi->qmap_type = ICE_RESMGR_ALLOC_SCATTERED;
39209e54973fSEric Joyner 
39219e54973fSEric Joyner 	/* Re-assign Tx queue tail pointers */
39229e54973fSEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
39239e54973fSEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
39249e54973fSEric Joyner 
39259e54973fSEric Joyner 	/* Re-assign Rx queue tail pointers */
39269e54973fSEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
39279e54973fSEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
39289e54973fSEric Joyner 
39299e54973fSEric Joyner 	return (0);
39309e54973fSEric Joyner 
39319e54973fSEric Joyner err_release_tx_queues:
39329e54973fSEric Joyner 	ice_resmgr_release_map(&sc->tx_qmgr, vsi->tx_qmap, vsi->num_tx_queues);
39339e54973fSEric Joyner 
39349e54973fSEric Joyner 	return (err);
39359e54973fSEric Joyner }
39369e54973fSEric Joyner 
39379e54973fSEric Joyner /**
39389e54973fSEric Joyner  * ice_subif_if_tx_queues_alloc - Allocate Tx queue memory for subinterfaces
39399e54973fSEric Joyner  * @ctx: iflib context structure
39409e54973fSEric Joyner  * @vaddrs: virtual addresses for the queue memory
39419e54973fSEric Joyner  * @paddrs: physical addresses for the queue memory
39429e54973fSEric Joyner  * @ntxqs: the number of Tx queues per set (should always be 1)
39439e54973fSEric Joyner  * @ntxqsets: the number of Tx queue sets to allocate
39449e54973fSEric Joyner  *
39459e54973fSEric Joyner  * See ice_if_tx_queues_alloc() description. Similar to that function, but
39469e54973fSEric Joyner  * for subinterfaces instead.
39479e54973fSEric Joyner  */
39489e54973fSEric Joyner static int
ice_subif_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int __invariant_only ntxqs,int ntxqsets)39499e54973fSEric Joyner ice_subif_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
39509e54973fSEric Joyner 			     int __invariant_only ntxqs, int ntxqsets)
39519e54973fSEric Joyner {
39529e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
39539e54973fSEric Joyner 	struct ice_tx_queue *txq;
39549e54973fSEric Joyner 	device_t dev = mif->subdev;
39559e54973fSEric Joyner 	struct ice_vsi *vsi;
39569e54973fSEric Joyner 	int err, i, j;
39579e54973fSEric Joyner 
39589e54973fSEric Joyner 	MPASS(mif != NULL);
39599e54973fSEric Joyner 	MPASS(ntxqs == 1);
39609e54973fSEric Joyner 	MPASS(mif->subscctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
39619e54973fSEric Joyner 
39629e54973fSEric Joyner 	vsi = mif->vsi;
39639e54973fSEric Joyner 
39649e54973fSEric Joyner 	MPASS(vsi->num_tx_queues == ntxqsets);
39659e54973fSEric Joyner 
39669e54973fSEric Joyner 	/* Allocate queue structure memory */
39679e54973fSEric Joyner 	if (!(vsi->tx_queues =
39689e54973fSEric Joyner 	      (struct ice_tx_queue *)malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
39699e54973fSEric Joyner 		device_printf(dev, "%s: Unable to allocate Tx queue memory for subfunction\n",
39709e54973fSEric Joyner 		    __func__);
39719e54973fSEric Joyner 		return (ENOMEM);
39729e54973fSEric Joyner 	}
39739e54973fSEric Joyner 
39749e54973fSEric Joyner 	/* Allocate report status arrays */
39759e54973fSEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
39769e54973fSEric Joyner 		if (!(txq->tx_rsq =
39779e54973fSEric Joyner 		      (uint16_t *)malloc(sizeof(uint16_t) * mif->subscctx->isc_ntxd[0], M_ICE, M_NOWAIT))) {
39789e54973fSEric Joyner 			device_printf(dev,
39799e54973fSEric Joyner 			    "%s: Unable to allocate tx_rsq memory for subfunction\n", __func__);
39809e54973fSEric Joyner 			err = ENOMEM;
39819e54973fSEric Joyner 			goto free_tx_queues;
39829e54973fSEric Joyner 		}
39839e54973fSEric Joyner 		/* Initialize report status array */
39849e54973fSEric Joyner 		for (j = 0; j < mif->subscctx->isc_ntxd[0]; j++)
39859e54973fSEric Joyner 			txq->tx_rsq[j] = QIDX_INVALID;
39869e54973fSEric Joyner 	}
39879e54973fSEric Joyner 
39889e54973fSEric Joyner 	/* Add Tx queue sysctls context */
39899e54973fSEric Joyner 	ice_vsi_add_txqs_ctx(vsi);
39909e54973fSEric Joyner 
39919e54973fSEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
39929e54973fSEric Joyner 		/* q_handle == me when only one TC */
39939e54973fSEric Joyner 		txq->me = txq->q_handle = i;
39949e54973fSEric Joyner 		txq->vsi = vsi;
39959e54973fSEric Joyner 
39969e54973fSEric Joyner 		/* store the queue size for easier access */
39979e54973fSEric Joyner 		txq->desc_count = mif->subscctx->isc_ntxd[0];
39989e54973fSEric Joyner 
39999e54973fSEric Joyner 		/* get the virtual and physical address of the hardware queues */
40009e54973fSEric Joyner 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
40019e54973fSEric Joyner 		txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
40029e54973fSEric Joyner 		txq->tx_paddr = paddrs[i];
40039e54973fSEric Joyner 
40049e54973fSEric Joyner 		ice_add_txq_sysctls(txq);
40059e54973fSEric Joyner 	}
40069e54973fSEric Joyner 
40079e54973fSEric Joyner 	return (0);
40089e54973fSEric Joyner 
40099e54973fSEric Joyner free_tx_queues:
40109e54973fSEric Joyner 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
40119e54973fSEric Joyner 		if (txq->tx_rsq != NULL) {
40129e54973fSEric Joyner 			free(txq->tx_rsq, M_ICE);
40139e54973fSEric Joyner 			txq->tx_rsq = NULL;
40149e54973fSEric Joyner 		}
40159e54973fSEric Joyner 	}
40169e54973fSEric Joyner 	free(vsi->tx_queues, M_ICE);
40179e54973fSEric Joyner 	vsi->tx_queues = NULL;
40189e54973fSEric Joyner 	return (err);
40199e54973fSEric Joyner }
40209e54973fSEric Joyner 
40219e54973fSEric Joyner /**
40229e54973fSEric Joyner  * ice_subif_if_rx_queues_alloc - Allocate Rx queue memory for subinterfaces
40239e54973fSEric Joyner  * @ctx: iflib context structure
40249e54973fSEric Joyner  * @vaddrs: virtual addresses for the queue memory
40259e54973fSEric Joyner  * @paddrs: physical addresses for the queue memory
40269e54973fSEric Joyner  * @nrxqs: number of Rx queues per set (should always be 1)
40279e54973fSEric Joyner  * @nrxqsets: number of Rx queue sets to allocate
40289e54973fSEric Joyner  *
40299e54973fSEric Joyner  * See ice_if_rx_queues_alloc() for general summary; this is similar to that
40309e54973fSEric Joyner  * but implemented for subinterfaces.
40319e54973fSEric Joyner  */
40329e54973fSEric Joyner static int
ice_subif_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int __invariant_only nrxqs,int nrxqsets)40339e54973fSEric Joyner ice_subif_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
40349e54973fSEric Joyner     int __invariant_only nrxqs, int nrxqsets)
40359e54973fSEric Joyner {
40369e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
40379e54973fSEric Joyner 	struct ice_rx_queue *rxq;
40389e54973fSEric Joyner 	device_t dev = mif->subdev;
40399e54973fSEric Joyner 	struct ice_vsi *vsi;
40409e54973fSEric Joyner 	int i;
40419e54973fSEric Joyner 
40429e54973fSEric Joyner 	MPASS(mif != NULL);
40439e54973fSEric Joyner 	MPASS(nrxqs == 1);
40449e54973fSEric Joyner 	MPASS(mif->subscctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
40459e54973fSEric Joyner 
40469e54973fSEric Joyner 	vsi = mif->vsi;
40479e54973fSEric Joyner 
40489e54973fSEric Joyner 	MPASS(vsi->num_rx_queues == nrxqsets);
40499e54973fSEric Joyner 
40509e54973fSEric Joyner 	/* Allocate queue structure memory */
40519e54973fSEric Joyner 	if (!(vsi->rx_queues =
40529e54973fSEric Joyner 	      (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
40539e54973fSEric Joyner 		device_printf(dev, "%s: Unable to allocate Rx queue memory for subfunction\n",
40549e54973fSEric Joyner 		    __func__);
40559e54973fSEric Joyner 		return (ENOMEM);
40569e54973fSEric Joyner 	}
40579e54973fSEric Joyner 
40589e54973fSEric Joyner 	/* Add Rx queue sysctls context */
40599e54973fSEric Joyner 	ice_vsi_add_rxqs_ctx(vsi);
40609e54973fSEric Joyner 
40619e54973fSEric Joyner 	for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
40629e54973fSEric Joyner 		rxq->me = i;
40639e54973fSEric Joyner 		rxq->vsi = vsi;
40649e54973fSEric Joyner 
40659e54973fSEric Joyner 		/* store the queue size for easier access */
40669e54973fSEric Joyner 		rxq->desc_count = mif->subscctx->isc_nrxd[0];
40679e54973fSEric Joyner 
40689e54973fSEric Joyner 		/* get the virtual and physical address of the hardware queues */
40699e54973fSEric Joyner 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
40709e54973fSEric Joyner 		rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
40719e54973fSEric Joyner 		rxq->rx_paddr = paddrs[i];
40729e54973fSEric Joyner 
40739e54973fSEric Joyner 		ice_add_rxq_sysctls(rxq);
40749e54973fSEric Joyner 	}
40759e54973fSEric Joyner 
40769e54973fSEric Joyner 	return (0);
40779e54973fSEric Joyner }
40789e54973fSEric Joyner 
40799e54973fSEric Joyner /**
40809e54973fSEric Joyner  * ice_subif_if_msix_intr_assign - Assign MSI-X interrupts to new sub interface
40819e54973fSEric Joyner  * @ctx: the iflib context structure
40829e54973fSEric Joyner  * @msix: the number of vectors we were assigned
40839e54973fSEric Joyner  *
40849e54973fSEric Joyner  * Allocates and assigns driver private resources for MSI-X interrupt tracking.
40859e54973fSEric Joyner  *
40869e54973fSEric Joyner  * @pre OS MSI-X resources have been pre-allocated by parent interface.
40879e54973fSEric Joyner  */
40889e54973fSEric Joyner static int
ice_subif_if_msix_intr_assign(if_ctx_t ctx,int msix)40899e54973fSEric Joyner ice_subif_if_msix_intr_assign(if_ctx_t ctx, int msix)
40909e54973fSEric Joyner {
40919e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
40929e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
40939e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
40949e54973fSEric Joyner 
40959e54973fSEric Joyner 	device_t dev = mif->subdev;
40969e54973fSEric Joyner 	int ret;
40979e54973fSEric Joyner 
40989e54973fSEric Joyner 	if (vsi->num_rx_queues != vsi->num_tx_queues) {
40999e54973fSEric Joyner 		device_printf(dev,
41009e54973fSEric 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",
41019e54973fSEric Joyner 			      vsi->num_tx_queues, vsi->num_rx_queues);
41029e54973fSEric Joyner 		return (EOPNOTSUPP);
41039e54973fSEric Joyner 	}
41049e54973fSEric Joyner 
41059e54973fSEric Joyner 	if (msix > sc->extra_vectors) {
41069e54973fSEric Joyner 		device_printf(dev,
41079e54973fSEric Joyner 		     "%s: Not enough spare (%d) msix vectors for new sub-interface requested (%d)\n",
41089e54973fSEric Joyner 		     __func__, sc->extra_vectors, msix);
41099e54973fSEric Joyner 		return (ENOSPC);
41109e54973fSEric Joyner 	}
41119e54973fSEric Joyner 	device_printf(dev, "%s: Using %d vectors for sub-interface\n", __func__,
41129e54973fSEric Joyner 	    msix);
41139e54973fSEric Joyner 
41149e54973fSEric Joyner 	/* Allocate space to store the IRQ vector data */
41159e54973fSEric Joyner 	mif->num_irq_vectors = vsi->num_rx_queues;
41169e54973fSEric Joyner 	mif->rx_irqvs = (struct ice_irq_vector *)
41179e54973fSEric Joyner 	    malloc(sizeof(struct ice_irq_vector) * (mif->num_irq_vectors),
41189e54973fSEric Joyner 		   M_ICE, M_NOWAIT);
41199e54973fSEric Joyner 	if (!mif->rx_irqvs) {
41209e54973fSEric Joyner 		device_printf(dev,
41219e54973fSEric Joyner 			      "Unable to allocate RX irqv memory for mirror's %d vectors\n",
41229e54973fSEric Joyner 			      mif->num_irq_vectors);
41239e54973fSEric Joyner 		return (ENOMEM);
41249e54973fSEric Joyner 	}
41259e54973fSEric Joyner 
41269e54973fSEric Joyner 	/* Assign mirror interface interrupts from PF device space */
41279e54973fSEric Joyner 	if (!(mif->if_imap =
41289e54973fSEric Joyner 	      (u16 *)malloc(sizeof(u16) * mif->num_irq_vectors,
41299e54973fSEric Joyner 	      M_ICE, M_NOWAIT))) {
41309e54973fSEric Joyner 		device_printf(dev, "Unable to allocate mirror intfc if_imap memory\n");
41319e54973fSEric Joyner 		ret = ENOMEM;
41329e54973fSEric Joyner 		goto free_irqvs;
41339e54973fSEric Joyner 	}
41349e54973fSEric Joyner 	ret = ice_resmgr_assign_contiguous(&sc->dev_imgr, mif->if_imap, mif->num_irq_vectors);
41359e54973fSEric Joyner 	if (ret) {
41369e54973fSEric Joyner 		device_printf(dev, "Unable to assign mirror intfc PF device interrupt mapping: %s\n",
41379e54973fSEric Joyner 			      ice_err_str(ret));
41389e54973fSEric Joyner 		goto free_if_imap;
41399e54973fSEric Joyner 	}
41409e54973fSEric Joyner 	/* Assign mirror interface interrupts from OS interrupt allocation space */
41419e54973fSEric Joyner 	if (!(mif->os_imap =
41429e54973fSEric Joyner 	      (u16 *)malloc(sizeof(u16) * mif->num_irq_vectors,
41439e54973fSEric Joyner 	      M_ICE, M_NOWAIT))) {
41449e54973fSEric Joyner 		device_printf(dev, "Unable to allocate mirror intfc os_imap memory\n");
41459e54973fSEric Joyner 		ret = ENOMEM;
41469e54973fSEric Joyner 		goto free_if_imap;
41479e54973fSEric Joyner 	}
41489e54973fSEric Joyner 	ret = ice_resmgr_assign_contiguous(&sc->os_imgr, mif->os_imap, mif->num_irq_vectors);
41499e54973fSEric Joyner 	if (ret) {
41509e54973fSEric Joyner 		device_printf(dev, "Unable to assign mirror intfc OS interrupt mapping: %s\n",
41519e54973fSEric Joyner 			      ice_err_str(ret));
41529e54973fSEric Joyner 		goto free_if_imap;
41539e54973fSEric Joyner 	}
41549e54973fSEric Joyner 
41559e54973fSEric Joyner 	return (0);
41569e54973fSEric Joyner 
41579e54973fSEric Joyner free_if_imap:
41589e54973fSEric Joyner 	free(mif->if_imap, M_ICE);
41599e54973fSEric Joyner 	mif->if_imap = NULL;
41609e54973fSEric Joyner free_irqvs:
41619e54973fSEric Joyner 	free(mif->rx_irqvs, M_ICE);
41629e54973fSEric Joyner 	mif->rx_irqvs = NULL;
41639e54973fSEric Joyner 	return (ret);
41649e54973fSEric Joyner }
41659e54973fSEric Joyner 
41669e54973fSEric Joyner /**
41679e54973fSEric Joyner  * ice_subif_if_intr_enable - Enable device interrupts for a subinterface
41689e54973fSEric Joyner  * @ctx: iflib context structure
41699e54973fSEric Joyner  *
41709e54973fSEric Joyner  * Called by iflib to request enabling all interrupts that belong to a
41719e54973fSEric Joyner  * subinterface.
41729e54973fSEric Joyner  */
41739e54973fSEric Joyner static void
ice_subif_if_intr_enable(if_ctx_t ctx)41749e54973fSEric Joyner ice_subif_if_intr_enable(if_ctx_t ctx)
41759e54973fSEric Joyner {
41769e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
41779e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
41789e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
41799e54973fSEric Joyner 	struct ice_hw *hw = &sc->hw;
41809e54973fSEric Joyner 
41819e54973fSEric Joyner 	/* Do not enable queue interrupts in recovery mode */
41829e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
41839e54973fSEric Joyner 		return;
41849e54973fSEric Joyner 
41859e54973fSEric Joyner 	/* Enable all queue interrupts */
41869e54973fSEric Joyner 	for (int i = 0; i < vsi->num_rx_queues; i++)
41879e54973fSEric Joyner 		ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
41889e54973fSEric Joyner }
41899e54973fSEric Joyner 
41909e54973fSEric Joyner /**
41919e54973fSEric Joyner  * ice_subif_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
41929e54973fSEric Joyner  * @ctx: iflib context structure
41939e54973fSEric Joyner  * @rxqid: the Rx queue to enable
41949e54973fSEric Joyner  *
41959e54973fSEric Joyner  * Enable a specific Rx queue interrupt.
41969e54973fSEric Joyner  *
41979e54973fSEric Joyner  * This function is not protected by the iflib CTX lock.
41989e54973fSEric Joyner  */
41999e54973fSEric Joyner static int
ice_subif_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)42009e54973fSEric Joyner ice_subif_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
42019e54973fSEric Joyner {
42029e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
42039e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
42049e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
42059e54973fSEric Joyner 	struct ice_hw *hw = &sc->hw;
42069e54973fSEric Joyner 
42079e54973fSEric Joyner 	/* Do not enable queue interrupts in recovery mode */
42089e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
42099e54973fSEric Joyner 		return (ENOSYS);
42109e54973fSEric Joyner 
42119e54973fSEric Joyner 	ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
42129e54973fSEric Joyner 	return (0);
42139e54973fSEric Joyner }
42149e54973fSEric Joyner 
42159e54973fSEric Joyner /**
42169e54973fSEric Joyner  * ice_subif_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
42179e54973fSEric Joyner  * @ctx: iflib context structure
42189e54973fSEric Joyner  * @txqid: the Tx queue to enable
42199e54973fSEric Joyner  *
42209e54973fSEric Joyner  * Enable a specific Tx queue interrupt.
42219e54973fSEric Joyner  *
42229e54973fSEric Joyner  * This function is not protected by the iflib CTX lock.
42239e54973fSEric Joyner  */
42249e54973fSEric Joyner static int
ice_subif_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)42259e54973fSEric Joyner ice_subif_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
42269e54973fSEric Joyner {
42279e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
42289e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
42299e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
42309e54973fSEric Joyner 	struct ice_hw *hw = &sc->hw;
42319e54973fSEric Joyner 
42329e54973fSEric Joyner 	/* Do not enable queue interrupts in recovery mode */
42339e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
42349e54973fSEric Joyner 		return (ENOSYS);
42359e54973fSEric Joyner 
42369e54973fSEric Joyner 	ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
42379e54973fSEric Joyner 	return (0);
42389e54973fSEric Joyner }
42399e54973fSEric Joyner 
42409e54973fSEric Joyner /**
42419e54973fSEric Joyner  * ice_subif_if_init - Initialize the subinterface
42429e54973fSEric Joyner  * @ctx: iflib ctx structure
42439e54973fSEric Joyner  *
42449e54973fSEric Joyner  * Called by iflib to bring the device up, i.e. ifconfig ice0m0 up.
42459e54973fSEric Joyner  * Prepares the Tx and Rx engines and enables interrupts.
42469e54973fSEric Joyner  *
42479e54973fSEric Joyner  * @pre assumes the caller holds the iflib CTX lock
42489e54973fSEric Joyner  */
42499e54973fSEric Joyner static void
ice_subif_if_init(if_ctx_t ctx)42509e54973fSEric Joyner ice_subif_if_init(if_ctx_t ctx)
42519e54973fSEric Joyner {
42529e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
42539e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
42549e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
42559e54973fSEric Joyner 	device_t dev = mif->subdev;
42569e54973fSEric Joyner 	int err;
42579e54973fSEric Joyner 
42589e54973fSEric Joyner 	if (ice_driver_is_detaching(sc))
42599e54973fSEric Joyner 		return;
42609e54973fSEric Joyner 
42619e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
42629e54973fSEric Joyner 		return;
42639e54973fSEric Joyner 
42649e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
42659e54973fSEric Joyner 		device_printf(dev,
42669e54973fSEric Joyner 		    "request to start interface cannot be completed as the parent device %s failed to reset\n",
42679e54973fSEric Joyner 		    device_get_nameunit(sc->dev));
42689e54973fSEric Joyner 		return;
42699e54973fSEric Joyner 	}
42709e54973fSEric Joyner 
42719e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
42729e54973fSEric Joyner 		device_printf(dev,
42739e54973fSEric Joyner 		    "request to start interface cannot be completed while parent device %s is prepared for impending reset\n",
42749e54973fSEric Joyner 		    device_get_nameunit(sc->dev));
42759e54973fSEric Joyner 		return;
42769e54973fSEric Joyner 	}
42779e54973fSEric Joyner 
42789e54973fSEric Joyner 	/* XXX: Equiv to ice_update_rx_mbuf_sz */
42799e54973fSEric Joyner 	vsi->mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
42809e54973fSEric Joyner 
42819e54973fSEric Joyner 	/* Initialize software Tx tracking values */
42829e54973fSEric Joyner 	ice_init_tx_tracking(vsi);
42839e54973fSEric Joyner 
42849e54973fSEric Joyner 	err = ice_cfg_vsi_for_tx(vsi);
42859e54973fSEric Joyner 	if (err) {
42869e54973fSEric Joyner 		device_printf(dev,
42879e54973fSEric Joyner 			      "Unable to configure subif VSI for Tx: %s\n",
42889e54973fSEric Joyner 			      ice_err_str(err));
42899e54973fSEric Joyner 		return;
42909e54973fSEric Joyner 	}
42919e54973fSEric Joyner 
42929e54973fSEric Joyner 	err = ice_cfg_vsi_for_rx(vsi);
42939e54973fSEric Joyner 	if (err) {
42949e54973fSEric Joyner 		device_printf(dev,
42959e54973fSEric Joyner 			      "Unable to configure subif VSI for Rx: %s\n",
42969e54973fSEric Joyner 			      ice_err_str(err));
42979e54973fSEric Joyner 		goto err_cleanup_tx;
42989e54973fSEric Joyner 	}
42999e54973fSEric Joyner 
43009e54973fSEric Joyner 	err = ice_control_all_rx_queues(vsi, true);
43019e54973fSEric Joyner 	if (err) {
43029e54973fSEric Joyner 		device_printf(dev,
43039e54973fSEric Joyner 			      "Unable to enable subif Rx rings for receive: %s\n",
43049e54973fSEric Joyner 			      ice_err_str(err));
43059e54973fSEric Joyner 		goto err_cleanup_tx;
43069e54973fSEric Joyner 	}
43079e54973fSEric Joyner 
43089e54973fSEric Joyner 	ice_configure_all_rxq_interrupts(vsi);
43099e54973fSEric Joyner 	ice_configure_rx_itr(vsi);
43109e54973fSEric Joyner 
43119e54973fSEric Joyner 	ice_set_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED);
43129e54973fSEric Joyner 	return;
43139e54973fSEric Joyner 
43149e54973fSEric Joyner err_cleanup_tx:
43159e54973fSEric Joyner 	ice_vsi_disable_tx(vsi);
43169e54973fSEric Joyner }
43179e54973fSEric Joyner 
43189e54973fSEric Joyner /**
43199e54973fSEric Joyner  * ice_if_stop_subif - Stop the subinterface
43209e54973fSEric Joyner  * @ctx: iflib context structure
43219e54973fSEric Joyner  * @ifs: subinterface context structure
43229e54973fSEric Joyner  *
43239e54973fSEric Joyner  * Called by iflib to stop the subinterface and bring it down.
43249e54973fSEric Joyner  * (e.g. ifconfig ice0m0 down)
43259e54973fSEric Joyner  *
43269e54973fSEric Joyner  * @pre assumes the caller holds the iflib CTX lock
43279e54973fSEric Joyner  */
43289e54973fSEric Joyner static void
ice_subif_if_stop(if_ctx_t ctx)43299e54973fSEric Joyner ice_subif_if_stop(if_ctx_t ctx)
43309e54973fSEric Joyner {
43319e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
43329e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
43339e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
43349e54973fSEric Joyner 	device_t dev = mif->subdev;
43359e54973fSEric Joyner 
43369e54973fSEric Joyner 	if (!ice_testandclear_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED))
43379e54973fSEric Joyner 		return;
43389e54973fSEric Joyner 
43399e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
43409e54973fSEric Joyner 		device_printf(dev,
43419e54973fSEric Joyner 		    "request to stop interface cannot be completed as the parent device %s failed to reset\n",
43429e54973fSEric Joyner 		    device_get_nameunit(sc->dev));
43439e54973fSEric Joyner 		return;
43449e54973fSEric Joyner 	}
43459e54973fSEric Joyner 
43469e54973fSEric Joyner 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
43479e54973fSEric Joyner 		device_printf(dev,
43489e54973fSEric Joyner 		    "request to stop interface cannot be completed while parent device %s is prepared for impending reset\n",
43499e54973fSEric Joyner 		    device_get_nameunit(sc->dev));
43509e54973fSEric Joyner 		return;
43519e54973fSEric Joyner 	}
43529e54973fSEric Joyner 
43539e54973fSEric Joyner 	/* Dissociate the Tx and Rx queues from the interrupts */
43549e54973fSEric Joyner 	ice_flush_txq_interrupts(vsi);
43559e54973fSEric Joyner 	ice_flush_rxq_interrupts(vsi);
43569e54973fSEric Joyner 
43579e54973fSEric Joyner 	/* Disable the Tx and Rx queues */
43589e54973fSEric Joyner 	ice_vsi_disable_tx(vsi);
43599e54973fSEric Joyner 	ice_control_all_rx_queues(vsi, false);
43609e54973fSEric Joyner }
43619e54973fSEric Joyner 
43629e54973fSEric Joyner /**
43639e54973fSEric Joyner  * ice_free_irqvs_subif - Free IRQ vector memory for subinterfaces
43649e54973fSEric Joyner  * @mif: Mirror interface private structure
43659e54973fSEric Joyner  *
43669e54973fSEric Joyner  * Free IRQ vector memory allocated during ice_subif_if_msix_intr_assign.
43679e54973fSEric Joyner  */
43689e54973fSEric Joyner static void
ice_free_irqvs_subif(struct ice_mirr_if * mif)43699e54973fSEric Joyner ice_free_irqvs_subif(struct ice_mirr_if *mif)
43709e54973fSEric Joyner {
43719e54973fSEric Joyner 	struct ice_softc *sc = mif->back;
43729e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
43739e54973fSEric Joyner 	if_ctx_t ctx = sc->ctx;
43749e54973fSEric Joyner 	int i;
43759e54973fSEric Joyner 
43769e54973fSEric Joyner 	/* If the irqvs array is NULL, then there are no vectors to free */
43779e54973fSEric Joyner 	if (mif->rx_irqvs == NULL)
43789e54973fSEric Joyner 		return;
43799e54973fSEric Joyner 
43809e54973fSEric Joyner 	/* Free the IRQ vectors -- currently subinterfaces have number
43819e54973fSEric Joyner 	 * of vectors equal to number of RX queues
43829e54973fSEric Joyner 	 *
43839e54973fSEric Joyner 	 * XXX: ctx is parent device's ctx, not the subinterface ctx
43849e54973fSEric Joyner 	 */
43859e54973fSEric Joyner 	for (i = 0; i < vsi->num_rx_queues; i++)
43869e54973fSEric Joyner 		iflib_irq_free(ctx, &mif->rx_irqvs[i].irq);
43879e54973fSEric Joyner 
43889e54973fSEric Joyner 	ice_resmgr_release_map(&sc->os_imgr, mif->os_imap,
43899e54973fSEric Joyner 	    mif->num_irq_vectors);
43909e54973fSEric Joyner 	ice_resmgr_release_map(&sc->dev_imgr, mif->if_imap,
43919e54973fSEric Joyner 	    mif->num_irq_vectors);
43929e54973fSEric Joyner 
43939e54973fSEric Joyner 	sc->last_rid -= vsi->num_rx_queues;
43949e54973fSEric Joyner 
43959e54973fSEric Joyner 	/* Clear the irqv pointers */
43969e54973fSEric Joyner 	for (i = 0; i < vsi->num_rx_queues; i++)
43979e54973fSEric Joyner 		vsi->rx_queues[i].irqv = NULL;
43989e54973fSEric Joyner 
43999e54973fSEric Joyner 	for (i = 0; i < vsi->num_tx_queues; i++)
44009e54973fSEric Joyner 		vsi->tx_queues[i].irqv = NULL;
44019e54973fSEric Joyner 
44029e54973fSEric Joyner 	/* Release the vector array memory */
44039e54973fSEric Joyner 	free(mif->rx_irqvs, M_ICE);
44049e54973fSEric Joyner 	mif->rx_irqvs = NULL;
44059e54973fSEric Joyner }
44069e54973fSEric Joyner 
44079e54973fSEric Joyner /**
44089e54973fSEric Joyner  * ice_subif_if_queues_free - Free queue memory for subinterfaces
44099e54973fSEric Joyner  * @ctx: the iflib context structure
44109e54973fSEric Joyner  *
44119e54973fSEric Joyner  * Free queue memory allocated by ice_subif_tx_queues_alloc() and
44129e54973fSEric Joyner  * ice_subif_if_rx_queues_alloc().
44139e54973fSEric Joyner  */
44149e54973fSEric Joyner static void
ice_subif_if_queues_free(if_ctx_t ctx)44159e54973fSEric Joyner ice_subif_if_queues_free(if_ctx_t ctx)
44169e54973fSEric Joyner {
44179e54973fSEric Joyner 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
44189e54973fSEric Joyner 	struct ice_vsi *vsi = mif->vsi;
44199e54973fSEric Joyner 	struct ice_tx_queue *txq;
44209e54973fSEric Joyner 	int i;
44219e54973fSEric Joyner 
44229e54973fSEric Joyner 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
44239e54973fSEric Joyner 	 * pointers.
44249e54973fSEric Joyner 	 */
44259e54973fSEric Joyner 	ice_vsi_del_txqs_ctx(vsi);
44269e54973fSEric Joyner 	ice_vsi_del_rxqs_ctx(vsi);
44279e54973fSEric Joyner 
44289e54973fSEric Joyner 	/* Release MSI-X IRQ vectors */
44299e54973fSEric Joyner 	ice_free_irqvs_subif(mif);
44309e54973fSEric Joyner 
44319e54973fSEric Joyner 	if (vsi->tx_queues != NULL) {
44329e54973fSEric Joyner 		/* free the tx_rsq arrays */
44339e54973fSEric Joyner 		for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
44349e54973fSEric Joyner 			if (txq->tx_rsq != NULL) {
44359e54973fSEric Joyner 				free(txq->tx_rsq, M_ICE);
44369e54973fSEric Joyner 				txq->tx_rsq = NULL;
44379e54973fSEric Joyner 			}
44389e54973fSEric Joyner 		}
44399e54973fSEric Joyner 		free(vsi->tx_queues, M_ICE);
44409e54973fSEric Joyner 		vsi->tx_queues = NULL;
44419e54973fSEric Joyner 	}
44429e54973fSEric Joyner 	if (vsi->rx_queues != NULL) {
44439e54973fSEric Joyner 		free(vsi->rx_queues, M_ICE);
44449e54973fSEric Joyner 		vsi->rx_queues = NULL;
44459e54973fSEric Joyner 	}
44469e54973fSEric Joyner }
44479e54973fSEric Joyner 
44489e54973fSEric Joyner /**
44499e54973fSEric Joyner  * ice_subif_if_media_status - Report subinterface media
44509e54973fSEric Joyner  * @ctx: iflib context structure
44519e54973fSEric Joyner  * @ifmr: ifmedia request structure to update
44529e54973fSEric Joyner  *
44539e54973fSEric Joyner  * Updates the provided ifmr with something, in order to prevent a
44549e54973fSEric Joyner  * "no media types?" message from ifconfig.
44559e54973fSEric Joyner  *
44569e54973fSEric Joyner  * Mirror interfaces are always up.
44579e54973fSEric Joyner  */
44589e54973fSEric Joyner static void
ice_subif_if_media_status(if_ctx_t ctx __unused,struct ifmediareq * ifmr)44599e54973fSEric Joyner ice_subif_if_media_status(if_ctx_t ctx __unused, struct ifmediareq *ifmr)
44609e54973fSEric Joyner {
44619e54973fSEric Joyner 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
44629e54973fSEric Joyner 	ifmr->ifm_active = IFM_ETHER | IFM_AUTO;
44639e54973fSEric Joyner }
44649e54973fSEric Joyner 
44659e54973fSEric Joyner /**
44669e54973fSEric Joyner  * ice_subif_if_promisc_set - Set subinterface promiscuous mode
44679e54973fSEric Joyner  * @ctx: iflib context structure
44689e54973fSEric Joyner  * @flags: promiscuous flags to configure
44699e54973fSEric Joyner  *
44709e54973fSEric Joyner  * Called by iflib to configure device promiscuous mode.
44719e54973fSEric Joyner  *
44729e54973fSEric Joyner  * @remark This does not need to be implemented for now.
44739e54973fSEric Joyner  */
44749e54973fSEric Joyner static int
ice_subif_if_promisc_set(if_ctx_t ctx __unused,int flags __unused)44759e54973fSEric Joyner ice_subif_if_promisc_set(if_ctx_t ctx __unused, int flags __unused)
44769e54973fSEric Joyner {
44779e54973fSEric Joyner 	return (0);
44789e54973fSEric Joyner }
44799e54973fSEric Joyner 
4480