xref: /freebsd/sys/dev/ixl/if_ixl.c (revision ff21e856ece05246dbeb099c3b1bf632852a7aa5)
161ae650dSJack F Vogel /******************************************************************************
261ae650dSJack F Vogel 
361ae650dSJack F Vogel   Copyright (c) 2013-2014, Intel Corporation
461ae650dSJack F Vogel   All rights reserved.
561ae650dSJack F Vogel 
661ae650dSJack F Vogel   Redistribution and use in source and binary forms, with or without
761ae650dSJack F Vogel   modification, are permitted provided that the following conditions are met:
861ae650dSJack F Vogel 
961ae650dSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
1061ae650dSJack F Vogel       this list of conditions and the following disclaimer.
1161ae650dSJack F Vogel 
1261ae650dSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
1361ae650dSJack F Vogel       notice, this list of conditions and the following disclaimer in the
1461ae650dSJack F Vogel       documentation and/or other materials provided with the distribution.
1561ae650dSJack F Vogel 
1661ae650dSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
1761ae650dSJack F Vogel       contributors may be used to endorse or promote products derived from
1861ae650dSJack F Vogel       this software without specific prior written permission.
1961ae650dSJack F Vogel 
2061ae650dSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2161ae650dSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261ae650dSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361ae650dSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2461ae650dSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2561ae650dSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2661ae650dSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2761ae650dSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2861ae650dSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2961ae650dSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3061ae650dSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
3161ae650dSJack F Vogel 
3261ae650dSJack F Vogel ******************************************************************************/
3361ae650dSJack F Vogel /*$FreeBSD$*/
3461ae650dSJack F Vogel 
3561ae650dSJack F Vogel #include "opt_inet.h"
3661ae650dSJack F Vogel #include "opt_inet6.h"
3761ae650dSJack F Vogel #include "ixl.h"
3861ae650dSJack F Vogel #include "ixl_pf.h"
3961ae650dSJack F Vogel 
4061ae650dSJack F Vogel /*********************************************************************
4161ae650dSJack F Vogel  *  Driver version
4261ae650dSJack F Vogel  *********************************************************************/
4361ae650dSJack F Vogel char ixl_driver_version[] = "1.2.2";
4461ae650dSJack F Vogel 
4561ae650dSJack F Vogel /*********************************************************************
4661ae650dSJack F Vogel  *  PCI Device ID Table
4761ae650dSJack F Vogel  *
4861ae650dSJack F Vogel  *  Used by probe to select devices to load on
4961ae650dSJack F Vogel  *  Last field stores an index into ixl_strings
5061ae650dSJack F Vogel  *  Last entry must be all 0s
5161ae650dSJack F Vogel  *
5261ae650dSJack F Vogel  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
5361ae650dSJack F Vogel  *********************************************************************/
5461ae650dSJack F Vogel 
5561ae650dSJack F Vogel static ixl_vendor_info_t ixl_vendor_info_array[] =
5661ae650dSJack F Vogel {
5761ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_XL710, 0, 0, 0},
5861ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_A, 0, 0, 0},
5961ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_B, 0, 0, 0},
6061ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_C, 0, 0, 0},
6161ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_A, 0, 0, 0},
6261ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_B, 0, 0, 0},
6361ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_C, 0, 0, 0},
6461ae650dSJack F Vogel 	{I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T, 0, 0, 0},
6561ae650dSJack F Vogel 	/* required last entry */
6661ae650dSJack F Vogel 	{0, 0, 0, 0, 0}
6761ae650dSJack F Vogel };
6861ae650dSJack F Vogel 
6961ae650dSJack F Vogel /*********************************************************************
7061ae650dSJack F Vogel  *  Table of branding strings
7161ae650dSJack F Vogel  *********************************************************************/
7261ae650dSJack F Vogel 
7361ae650dSJack F Vogel static char    *ixl_strings[] = {
7461ae650dSJack F Vogel 	"Intel(R) Ethernet Connection XL710 Driver"
7561ae650dSJack F Vogel };
7661ae650dSJack F Vogel 
7761ae650dSJack F Vogel 
7861ae650dSJack F Vogel /*********************************************************************
7961ae650dSJack F Vogel  *  Function prototypes
8061ae650dSJack F Vogel  *********************************************************************/
8161ae650dSJack F Vogel static int      ixl_probe(device_t);
8261ae650dSJack F Vogel static int      ixl_attach(device_t);
8361ae650dSJack F Vogel static int      ixl_detach(device_t);
8461ae650dSJack F Vogel static int      ixl_shutdown(device_t);
8561ae650dSJack F Vogel static int	ixl_get_hw_capabilities(struct ixl_pf *);
8661ae650dSJack F Vogel static void	ixl_cap_txcsum_tso(struct ixl_vsi *, struct ifnet *, int);
8761ae650dSJack F Vogel static int      ixl_ioctl(struct ifnet *, u_long, caddr_t);
8861ae650dSJack F Vogel static void	ixl_init(void *);
8961ae650dSJack F Vogel static void	ixl_init_locked(struct ixl_pf *);
9061ae650dSJack F Vogel static void     ixl_stop(struct ixl_pf *);
9161ae650dSJack F Vogel static void     ixl_media_status(struct ifnet *, struct ifmediareq *);
9261ae650dSJack F Vogel static int      ixl_media_change(struct ifnet *);
9361ae650dSJack F Vogel static void     ixl_update_link_status(struct ixl_pf *);
9461ae650dSJack F Vogel static int      ixl_allocate_pci_resources(struct ixl_pf *);
9561ae650dSJack F Vogel static u16	ixl_get_bus_info(struct i40e_hw *, device_t);
9661ae650dSJack F Vogel static int	ixl_setup_stations(struct ixl_pf *);
9761ae650dSJack F Vogel static int	ixl_setup_vsi(struct ixl_vsi *);
9861ae650dSJack F Vogel static int	ixl_initialize_vsi(struct ixl_vsi *);
9961ae650dSJack F Vogel static int	ixl_assign_vsi_msix(struct ixl_pf *);
10061ae650dSJack F Vogel static int	ixl_assign_vsi_legacy(struct ixl_pf *);
10161ae650dSJack F Vogel static int	ixl_init_msix(struct ixl_pf *);
10261ae650dSJack F Vogel static void	ixl_configure_msix(struct ixl_pf *);
10361ae650dSJack F Vogel static void	ixl_configure_itr(struct ixl_pf *);
10461ae650dSJack F Vogel static void	ixl_configure_legacy(struct ixl_pf *);
10561ae650dSJack F Vogel static void	ixl_free_pci_resources(struct ixl_pf *);
10661ae650dSJack F Vogel static void	ixl_local_timer(void *);
10761ae650dSJack F Vogel static int	ixl_setup_interface(device_t, struct ixl_vsi *);
10861ae650dSJack F Vogel static bool	ixl_config_link(struct i40e_hw *);
10961ae650dSJack F Vogel static void	ixl_config_rss(struct ixl_vsi *);
11061ae650dSJack F Vogel static void	ixl_set_queue_rx_itr(struct ixl_queue *);
11161ae650dSJack F Vogel static void	ixl_set_queue_tx_itr(struct ixl_queue *);
11261ae650dSJack F Vogel 
11361ae650dSJack F Vogel static void	ixl_enable_rings(struct ixl_vsi *);
11461ae650dSJack F Vogel static void	ixl_disable_rings(struct ixl_vsi *);
11561ae650dSJack F Vogel static void     ixl_enable_intr(struct ixl_vsi *);
11661ae650dSJack F Vogel static void     ixl_disable_intr(struct ixl_vsi *);
11761ae650dSJack F Vogel 
11861ae650dSJack F Vogel static void     ixl_enable_adminq(struct i40e_hw *);
11961ae650dSJack F Vogel static void     ixl_disable_adminq(struct i40e_hw *);
12061ae650dSJack F Vogel static void     ixl_enable_queue(struct i40e_hw *, int);
12161ae650dSJack F Vogel static void     ixl_disable_queue(struct i40e_hw *, int);
12261ae650dSJack F Vogel static void     ixl_enable_legacy(struct i40e_hw *);
12361ae650dSJack F Vogel static void     ixl_disable_legacy(struct i40e_hw *);
12461ae650dSJack F Vogel 
12561ae650dSJack F Vogel static void     ixl_set_promisc(struct ixl_vsi *);
12661ae650dSJack F Vogel static void     ixl_add_multi(struct ixl_vsi *);
12761ae650dSJack F Vogel static void     ixl_del_multi(struct ixl_vsi *);
12861ae650dSJack F Vogel static void	ixl_register_vlan(void *, struct ifnet *, u16);
12961ae650dSJack F Vogel static void	ixl_unregister_vlan(void *, struct ifnet *, u16);
13061ae650dSJack F Vogel static void	ixl_setup_vlan_filters(struct ixl_vsi *);
13161ae650dSJack F Vogel 
13261ae650dSJack F Vogel static void	ixl_init_filters(struct ixl_vsi *);
13361ae650dSJack F Vogel static void	ixl_add_filter(struct ixl_vsi *, u8 *, s16 vlan);
13461ae650dSJack F Vogel static void	ixl_del_filter(struct ixl_vsi *, u8 *, s16 vlan);
13561ae650dSJack F Vogel static void	ixl_add_hw_filters(struct ixl_vsi *, int, int);
13661ae650dSJack F Vogel static void	ixl_del_hw_filters(struct ixl_vsi *, int);
13761ae650dSJack F Vogel static struct ixl_mac_filter *
13861ae650dSJack F Vogel 		ixl_find_filter(struct ixl_vsi *, u8 *, s16);
13961ae650dSJack F Vogel static void	ixl_add_mc_filter(struct ixl_vsi *, u8 *);
14061ae650dSJack F Vogel 
14161ae650dSJack F Vogel /* Sysctl debug interface */
14261ae650dSJack F Vogel static int	ixl_debug_info(SYSCTL_HANDLER_ARGS);
14361ae650dSJack F Vogel static void	ixl_print_debug_info(struct ixl_pf *);
14461ae650dSJack F Vogel 
14561ae650dSJack F Vogel /* The MSI/X Interrupt handlers */
14661ae650dSJack F Vogel static void	ixl_intr(void *);
14761ae650dSJack F Vogel static void	ixl_msix_que(void *);
14861ae650dSJack F Vogel static void	ixl_msix_adminq(void *);
14961ae650dSJack F Vogel static void	ixl_handle_mdd_event(struct ixl_pf *);
15061ae650dSJack F Vogel 
15161ae650dSJack F Vogel /* Deferred interrupt tasklets */
15261ae650dSJack F Vogel static void	ixl_do_adminq(void *, int);
15361ae650dSJack F Vogel 
15461ae650dSJack F Vogel /* Sysctl handlers */
15561ae650dSJack F Vogel static int	ixl_set_flowcntl(SYSCTL_HANDLER_ARGS);
15661ae650dSJack F Vogel static int	ixl_set_advertise(SYSCTL_HANDLER_ARGS);
15761ae650dSJack F Vogel static int	ixl_current_speed(SYSCTL_HANDLER_ARGS);
15861ae650dSJack F Vogel 
15961ae650dSJack F Vogel /* Statistics */
16061ae650dSJack F Vogel static void     ixl_add_hw_stats(struct ixl_pf *);
16161ae650dSJack F Vogel static void	ixl_add_sysctls_mac_stats(struct sysctl_ctx_list *,
16261ae650dSJack F Vogel 		    struct sysctl_oid_list *, struct i40e_hw_port_stats *);
16361ae650dSJack F Vogel static void	ixl_add_sysctls_eth_stats(struct sysctl_ctx_list *,
16461ae650dSJack F Vogel 		    struct sysctl_oid_list *,
16561ae650dSJack F Vogel 		    struct i40e_eth_stats *);
16661ae650dSJack F Vogel static void	ixl_update_stats_counters(struct ixl_pf *);
16761ae650dSJack F Vogel static void	ixl_update_eth_stats(struct ixl_vsi *);
16861ae650dSJack F Vogel static void	ixl_pf_reset_stats(struct ixl_pf *);
16961ae650dSJack F Vogel static void	ixl_vsi_reset_stats(struct ixl_vsi *);
17061ae650dSJack F Vogel static void	ixl_stat_update48(struct i40e_hw *, u32, u32, bool,
17161ae650dSJack F Vogel 		    u64 *, u64 *);
17261ae650dSJack F Vogel static void	ixl_stat_update32(struct i40e_hw *, u32, bool,
17361ae650dSJack F Vogel 		    u64 *, u64 *);
17461ae650dSJack F Vogel 
17561ae650dSJack F Vogel #ifdef IXL_DEBUG
17661ae650dSJack F Vogel static int 	ixl_sysctl_link_status(SYSCTL_HANDLER_ARGS);
17761ae650dSJack F Vogel static int	ixl_sysctl_phy_abilities(SYSCTL_HANDLER_ARGS);
17861ae650dSJack F Vogel static int	ixl_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS);
17961ae650dSJack F Vogel static int	ixl_sysctl_hw_res_info(SYSCTL_HANDLER_ARGS);
18061ae650dSJack F Vogel static int	ixl_sysctl_dump_txd(SYSCTL_HANDLER_ARGS);
18161ae650dSJack F Vogel #endif
18261ae650dSJack F Vogel 
18361ae650dSJack F Vogel /*********************************************************************
18461ae650dSJack F Vogel  *  FreeBSD Device Interface Entry Points
18561ae650dSJack F Vogel  *********************************************************************/
18661ae650dSJack F Vogel 
18761ae650dSJack F Vogel static device_method_t ixl_methods[] = {
18861ae650dSJack F Vogel 	/* Device interface */
18961ae650dSJack F Vogel 	DEVMETHOD(device_probe, ixl_probe),
19061ae650dSJack F Vogel 	DEVMETHOD(device_attach, ixl_attach),
19161ae650dSJack F Vogel 	DEVMETHOD(device_detach, ixl_detach),
19261ae650dSJack F Vogel 	DEVMETHOD(device_shutdown, ixl_shutdown),
19361ae650dSJack F Vogel 	{0, 0}
19461ae650dSJack F Vogel };
19561ae650dSJack F Vogel 
19661ae650dSJack F Vogel static driver_t ixl_driver = {
19761ae650dSJack F Vogel 	"ixl", ixl_methods, sizeof(struct ixl_pf),
19861ae650dSJack F Vogel };
19961ae650dSJack F Vogel 
20061ae650dSJack F Vogel devclass_t ixl_devclass;
20161ae650dSJack F Vogel DRIVER_MODULE(ixl, pci, ixl_driver, ixl_devclass, 0, 0);
20261ae650dSJack F Vogel 
20361ae650dSJack F Vogel MODULE_DEPEND(ixl, pci, 1, 1, 1);
20461ae650dSJack F Vogel MODULE_DEPEND(ixl, ether, 1, 1, 1);
20561ae650dSJack F Vogel 
20661ae650dSJack F Vogel /*
20761ae650dSJack F Vogel ** Global reset mutex
20861ae650dSJack F Vogel */
20961ae650dSJack F Vogel static struct mtx ixl_reset_mtx;
21061ae650dSJack F Vogel 
21161ae650dSJack F Vogel /*
21261ae650dSJack F Vogel ** TUNEABLE PARAMETERS:
21361ae650dSJack F Vogel */
21461ae650dSJack F Vogel 
21561ae650dSJack F Vogel static SYSCTL_NODE(_hw, OID_AUTO, ixl, CTLFLAG_RD, 0,
21661ae650dSJack F Vogel                    "IXL driver parameters");
21761ae650dSJack F Vogel 
21861ae650dSJack F Vogel /*
21961ae650dSJack F Vogel  * MSIX should be the default for best performance,
22061ae650dSJack F Vogel  * but this allows it to be forced off for testing.
22161ae650dSJack F Vogel  */
22261ae650dSJack F Vogel static int ixl_enable_msix = 1;
22361ae650dSJack F Vogel TUNABLE_INT("hw.ixl.enable_msix", &ixl_enable_msix);
22461ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixl_enable_msix, 0,
22561ae650dSJack F Vogel     "Enable MSI-X interrupts");
22661ae650dSJack F Vogel 
22761ae650dSJack F Vogel /*
22861ae650dSJack F Vogel ** Number of descriptors per ring:
22961ae650dSJack F Vogel **   - TX and RX are the same size
23061ae650dSJack F Vogel */
23161ae650dSJack F Vogel static int ixl_ringsz = DEFAULT_RING;
23261ae650dSJack F Vogel TUNABLE_INT("hw.ixl.ringsz", &ixl_ringsz);
23361ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, ring_size, CTLFLAG_RDTUN,
23461ae650dSJack F Vogel     &ixl_ringsz, 0, "Descriptor Ring Size");
23561ae650dSJack F Vogel 
23661ae650dSJack F Vogel /*
23761ae650dSJack F Vogel ** This can be set manually, if left as 0 the
23861ae650dSJack F Vogel ** number of queues will be calculated based
23961ae650dSJack F Vogel ** on cpus and msix vectors available.
24061ae650dSJack F Vogel */
24161ae650dSJack F Vogel int ixl_max_queues = 0;
24261ae650dSJack F Vogel TUNABLE_INT("hw.ixl.max_queues", &ixl_max_queues);
24361ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, max_queues, CTLFLAG_RDTUN,
24461ae650dSJack F Vogel     &ixl_max_queues, 0, "Number of Queues");
24561ae650dSJack F Vogel 
24661ae650dSJack F Vogel /*
24761ae650dSJack F Vogel ** Controls for Interrupt Throttling
24861ae650dSJack F Vogel **	- true/false for dynamic adjustment
24961ae650dSJack F Vogel ** 	- default values for static ITR
25061ae650dSJack F Vogel */
25161ae650dSJack F Vogel int ixl_dynamic_rx_itr = 0;
25261ae650dSJack F Vogel TUNABLE_INT("hw.ixl.dynamic_rx_itr", &ixl_dynamic_rx_itr);
25361ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_rx_itr, CTLFLAG_RDTUN,
25461ae650dSJack F Vogel     &ixl_dynamic_rx_itr, 0, "Dynamic RX Interrupt Rate");
25561ae650dSJack F Vogel 
25661ae650dSJack F Vogel int ixl_dynamic_tx_itr = 0;
25761ae650dSJack F Vogel TUNABLE_INT("hw.ixl.dynamic_tx_itr", &ixl_dynamic_tx_itr);
25861ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_tx_itr, CTLFLAG_RDTUN,
25961ae650dSJack F Vogel     &ixl_dynamic_tx_itr, 0, "Dynamic TX Interrupt Rate");
26061ae650dSJack F Vogel 
26161ae650dSJack F Vogel int ixl_rx_itr = IXL_ITR_8K;
26261ae650dSJack F Vogel TUNABLE_INT("hw.ixl.rx_itr", &ixl_rx_itr);
26361ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, rx_itr, CTLFLAG_RDTUN,
26461ae650dSJack F Vogel     &ixl_rx_itr, 0, "RX Interrupt Rate");
26561ae650dSJack F Vogel 
26661ae650dSJack F Vogel int ixl_tx_itr = IXL_ITR_4K;
26761ae650dSJack F Vogel TUNABLE_INT("hw.ixl.tx_itr", &ixl_tx_itr);
26861ae650dSJack F Vogel SYSCTL_INT(_hw_ixl, OID_AUTO, tx_itr, CTLFLAG_RDTUN,
26961ae650dSJack F Vogel     &ixl_tx_itr, 0, "TX Interrupt Rate");
27061ae650dSJack F Vogel 
27161ae650dSJack F Vogel #ifdef IXL_FDIR
27261ae650dSJack F Vogel static int ixl_enable_fdir = 1;
27361ae650dSJack F Vogel TUNABLE_INT("hw.ixl.enable_fdir", &ixl_enable_fdir);
27461ae650dSJack F Vogel /* Rate at which we sample */
27561ae650dSJack F Vogel int ixl_atr_rate = 20;
27661ae650dSJack F Vogel TUNABLE_INT("hw.ixl.atr_rate", &ixl_atr_rate);
27761ae650dSJack F Vogel #endif
27861ae650dSJack F Vogel 
27961ae650dSJack F Vogel static char *ixl_fc_string[6] = {
28061ae650dSJack F Vogel 	"None",
28161ae650dSJack F Vogel 	"Rx",
28261ae650dSJack F Vogel 	"Tx",
28361ae650dSJack F Vogel 	"Full",
28461ae650dSJack F Vogel 	"Priority",
28561ae650dSJack F Vogel 	"Default"
28661ae650dSJack F Vogel };
28761ae650dSJack F Vogel 
28861ae650dSJack F Vogel 
28961ae650dSJack F Vogel /*********************************************************************
29061ae650dSJack F Vogel  *  Device identification routine
29161ae650dSJack F Vogel  *
29261ae650dSJack F Vogel  *  ixl_probe determines if the driver should be loaded on
29361ae650dSJack F Vogel  *  the hardware based on PCI vendor/device id of the device.
29461ae650dSJack F Vogel  *
29561ae650dSJack F Vogel  *  return BUS_PROBE_DEFAULT on success, positive on failure
29661ae650dSJack F Vogel  *********************************************************************/
29761ae650dSJack F Vogel 
29861ae650dSJack F Vogel static int
29961ae650dSJack F Vogel ixl_probe(device_t dev)
30061ae650dSJack F Vogel {
30161ae650dSJack F Vogel 	ixl_vendor_info_t *ent;
30261ae650dSJack F Vogel 
30361ae650dSJack F Vogel 	u16	pci_vendor_id, pci_device_id;
30461ae650dSJack F Vogel 	u16	pci_subvendor_id, pci_subdevice_id;
30561ae650dSJack F Vogel 	char	device_name[256];
30661ae650dSJack F Vogel 	static bool lock_init = FALSE;
30761ae650dSJack F Vogel 
30861ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_probe: begin");
30961ae650dSJack F Vogel 
31061ae650dSJack F Vogel 	pci_vendor_id = pci_get_vendor(dev);
31161ae650dSJack F Vogel 	if (pci_vendor_id != I40E_INTEL_VENDOR_ID)
31261ae650dSJack F Vogel 		return (ENXIO);
31361ae650dSJack F Vogel 
31461ae650dSJack F Vogel 	pci_device_id = pci_get_device(dev);
31561ae650dSJack F Vogel 	pci_subvendor_id = pci_get_subvendor(dev);
31661ae650dSJack F Vogel 	pci_subdevice_id = pci_get_subdevice(dev);
31761ae650dSJack F Vogel 
31861ae650dSJack F Vogel 	ent = ixl_vendor_info_array;
31961ae650dSJack F Vogel 	while (ent->vendor_id != 0) {
32061ae650dSJack F Vogel 		if ((pci_vendor_id == ent->vendor_id) &&
32161ae650dSJack F Vogel 		    (pci_device_id == ent->device_id) &&
32261ae650dSJack F Vogel 
32361ae650dSJack F Vogel 		    ((pci_subvendor_id == ent->subvendor_id) ||
32461ae650dSJack F Vogel 		     (ent->subvendor_id == 0)) &&
32561ae650dSJack F Vogel 
32661ae650dSJack F Vogel 		    ((pci_subdevice_id == ent->subdevice_id) ||
32761ae650dSJack F Vogel 		     (ent->subdevice_id == 0))) {
32861ae650dSJack F Vogel 			sprintf(device_name, "%s, Version - %s",
32961ae650dSJack F Vogel 				ixl_strings[ent->index],
33061ae650dSJack F Vogel 				ixl_driver_version);
33161ae650dSJack F Vogel 			device_set_desc_copy(dev, device_name);
33261ae650dSJack F Vogel 			/* One shot mutex init */
33361ae650dSJack F Vogel 			if (lock_init == FALSE) {
33461ae650dSJack F Vogel 				lock_init = TRUE;
33561ae650dSJack F Vogel 				mtx_init(&ixl_reset_mtx,
33661ae650dSJack F Vogel 				    "ixl_reset",
33761ae650dSJack F Vogel 				    "IXL RESET Lock", MTX_DEF);
33861ae650dSJack F Vogel 			}
33961ae650dSJack F Vogel 			return (BUS_PROBE_DEFAULT);
34061ae650dSJack F Vogel 		}
34161ae650dSJack F Vogel 		ent++;
34261ae650dSJack F Vogel 	}
34361ae650dSJack F Vogel 	return (ENXIO);
34461ae650dSJack F Vogel }
34561ae650dSJack F Vogel 
34661ae650dSJack F Vogel /*********************************************************************
34761ae650dSJack F Vogel  *  Device initialization routine
34861ae650dSJack F Vogel  *
34961ae650dSJack F Vogel  *  The attach entry point is called when the driver is being loaded.
35061ae650dSJack F Vogel  *  This routine identifies the type of hardware, allocates all resources
35161ae650dSJack F Vogel  *  and initializes the hardware.
35261ae650dSJack F Vogel  *
35361ae650dSJack F Vogel  *  return 0 on success, positive on failure
35461ae650dSJack F Vogel  *********************************************************************/
35561ae650dSJack F Vogel 
35661ae650dSJack F Vogel static int
35761ae650dSJack F Vogel ixl_attach(device_t dev)
35861ae650dSJack F Vogel {
35961ae650dSJack F Vogel 	struct ixl_pf	*pf;
36061ae650dSJack F Vogel 	struct i40e_hw	*hw;
36161ae650dSJack F Vogel 	struct ixl_vsi *vsi;
36261ae650dSJack F Vogel 	u16		bus;
36361ae650dSJack F Vogel 	int             error = 0;
36461ae650dSJack F Vogel 
36561ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_attach: begin");
36661ae650dSJack F Vogel 
36761ae650dSJack F Vogel 	/* Allocate, clear, and link in our primary soft structure */
36861ae650dSJack F Vogel 	pf = device_get_softc(dev);
36961ae650dSJack F Vogel 	pf->dev = pf->osdep.dev = dev;
37061ae650dSJack F Vogel 	hw = &pf->hw;
37161ae650dSJack F Vogel 
37261ae650dSJack F Vogel 	/*
37361ae650dSJack F Vogel 	** Note this assumes we have a single embedded VSI,
37461ae650dSJack F Vogel 	** this could be enhanced later to allocate multiple
37561ae650dSJack F Vogel 	*/
37661ae650dSJack F Vogel 	vsi = &pf->vsi;
37761ae650dSJack F Vogel 	vsi->dev = pf->dev;
37861ae650dSJack F Vogel 
37961ae650dSJack F Vogel 	/* Core Lock Init*/
38061ae650dSJack F Vogel 	IXL_PF_LOCK_INIT(pf, device_get_nameunit(dev));
38161ae650dSJack F Vogel 
38261ae650dSJack F Vogel 	/* Set up the timer callout */
38361ae650dSJack F Vogel 	callout_init_mtx(&pf->timer, &pf->pf_mtx, 0);
38461ae650dSJack F Vogel 
38561ae650dSJack F Vogel 	/* Set up sysctls */
38661ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
38761ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
38861ae650dSJack F Vogel 	    OID_AUTO, "fc", CTLTYPE_INT | CTLFLAG_RW,
38961ae650dSJack F Vogel 	    pf, 0, ixl_set_flowcntl, "I", "Flow Control");
39061ae650dSJack F Vogel 
39161ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
39261ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
39361ae650dSJack F Vogel 	    OID_AUTO, "advertise_speed", CTLTYPE_INT | CTLFLAG_RW,
39461ae650dSJack F Vogel 	    pf, 0, ixl_set_advertise, "I", "Advertised Speed");
39561ae650dSJack F Vogel 
39661ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
39761ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
39861ae650dSJack F Vogel 	    OID_AUTO, "current_speed", CTLTYPE_STRING | CTLFLAG_RD,
39961ae650dSJack F Vogel 	    pf, 0, ixl_current_speed, "A", "Current Port Speed");
40061ae650dSJack F Vogel 
40161ae650dSJack F Vogel 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
40261ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
40361ae650dSJack F Vogel 	    OID_AUTO, "rx_itr", CTLTYPE_INT | CTLFLAG_RW,
40461ae650dSJack F Vogel 	    &ixl_rx_itr, IXL_ITR_8K, "RX ITR");
40561ae650dSJack F Vogel 
40661ae650dSJack F Vogel 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
40761ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
40861ae650dSJack F Vogel 	    OID_AUTO, "dynamic_rx_itr", CTLTYPE_INT | CTLFLAG_RW,
40961ae650dSJack F Vogel 	    &ixl_dynamic_rx_itr, 0, "Dynamic RX ITR");
41061ae650dSJack F Vogel 
41161ae650dSJack F Vogel 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
41261ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
41361ae650dSJack F Vogel 	    OID_AUTO, "tx_itr", CTLTYPE_INT | CTLFLAG_RW,
41461ae650dSJack F Vogel 	    &ixl_tx_itr, IXL_ITR_4K, "TX ITR");
41561ae650dSJack F Vogel 
41661ae650dSJack F Vogel 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
41761ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
41861ae650dSJack F Vogel 	    OID_AUTO, "dynamic_tx_itr", CTLTYPE_INT | CTLFLAG_RW,
41961ae650dSJack F Vogel 	    &ixl_dynamic_tx_itr, 0, "Dynamic TX ITR");
42061ae650dSJack F Vogel 
42161ae650dSJack F Vogel #ifdef IXL_DEBUG
42261ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
42361ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
42461ae650dSJack F Vogel 	    OID_AUTO, "link_status", CTLTYPE_STRING | CTLFLAG_RD,
42561ae650dSJack F Vogel 	    pf, 0, ixl_sysctl_link_status, "A", "Current Link Status");
42661ae650dSJack F Vogel 
42761ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
42861ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
42961ae650dSJack F Vogel 	    OID_AUTO, "phy_abilities", CTLTYPE_STRING | CTLFLAG_RD,
43061ae650dSJack F Vogel 	    pf, 0, ixl_sysctl_phy_abilities, "A", "PHY Abilities");
43161ae650dSJack F Vogel 
43261ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
43361ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
43461ae650dSJack F Vogel 	    OID_AUTO, "filter_list", CTLTYPE_STRING | CTLFLAG_RD,
43561ae650dSJack F Vogel 	    pf, 0, ixl_sysctl_sw_filter_list, "A", "SW Filter List");
43661ae650dSJack F Vogel 
43761ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
43861ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
43961ae650dSJack F Vogel 	    OID_AUTO, "hw_res_info", CTLTYPE_STRING | CTLFLAG_RD,
44061ae650dSJack F Vogel 	    pf, 0, ixl_sysctl_hw_res_info, "A", "HW Resource Allocation");
44161ae650dSJack F Vogel 
44261ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
44361ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
44461ae650dSJack F Vogel 	    OID_AUTO, "dump_desc", CTLTYPE_INT | CTLFLAG_WR,
44561ae650dSJack F Vogel 	    pf, 0, ixl_sysctl_dump_txd, "I", "Desc dump");
44661ae650dSJack F Vogel #endif
44761ae650dSJack F Vogel 
44861ae650dSJack F Vogel 	/* Save off the information about this board */
44961ae650dSJack F Vogel 	hw->vendor_id = pci_get_vendor(dev);
45061ae650dSJack F Vogel 	hw->device_id = pci_get_device(dev);
45161ae650dSJack F Vogel 	hw->revision_id = pci_read_config(dev, PCIR_REVID, 1);
45261ae650dSJack F Vogel 	hw->subsystem_vendor_id =
45361ae650dSJack F Vogel 	    pci_read_config(dev, PCIR_SUBVEND_0, 2);
45461ae650dSJack F Vogel 	hw->subsystem_device_id =
45561ae650dSJack F Vogel 	    pci_read_config(dev, PCIR_SUBDEV_0, 2);
45661ae650dSJack F Vogel 
45761ae650dSJack F Vogel 	hw->bus.device = pci_get_slot(dev);
45861ae650dSJack F Vogel 	hw->bus.func = pci_get_function(dev);
45961ae650dSJack F Vogel 
46061ae650dSJack F Vogel 	/* Do PCI setup - map BAR0, etc */
46161ae650dSJack F Vogel 	if (ixl_allocate_pci_resources(pf)) {
46261ae650dSJack F Vogel 		device_printf(dev, "Allocation of PCI resources failed\n");
46361ae650dSJack F Vogel 		error = ENXIO;
46461ae650dSJack F Vogel 		goto err_out;
46561ae650dSJack F Vogel 	}
46661ae650dSJack F Vogel 
46761ae650dSJack F Vogel 	/* Create for initial debugging use */
46861ae650dSJack F Vogel 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
46961ae650dSJack F Vogel 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
47061ae650dSJack F Vogel 	    OID_AUTO, "debug", CTLTYPE_INT|CTLFLAG_RW, pf, 0,
47161ae650dSJack F Vogel 	    ixl_debug_info, "I", "Debug Information");
47261ae650dSJack F Vogel 
47361ae650dSJack F Vogel 
47461ae650dSJack F Vogel 	/* Establish a clean starting point */
47561ae650dSJack F Vogel 	i40e_clear_hw(hw);
47661ae650dSJack F Vogel 	error = i40e_pf_reset(hw);
47761ae650dSJack F Vogel 	if (error) {
47861ae650dSJack F Vogel 		device_printf(dev,"PF reset failure %x\n", error);
47961ae650dSJack F Vogel 		error = EIO;
48061ae650dSJack F Vogel 		goto err_out;
48161ae650dSJack F Vogel 	}
48261ae650dSJack F Vogel 
48361ae650dSJack F Vogel 	/* For now always do an initial CORE reset on first device */
48461ae650dSJack F Vogel 	{
48561ae650dSJack F Vogel 		static int	ixl_dev_count;
48661ae650dSJack F Vogel 		static int	ixl_dev_track[32];
48761ae650dSJack F Vogel 		u32		my_dev;
48861ae650dSJack F Vogel 		int		i, found = FALSE;
48961ae650dSJack F Vogel 		u16		bus = pci_get_bus(dev);
49061ae650dSJack F Vogel 
49161ae650dSJack F Vogel 		mtx_lock(&ixl_reset_mtx);
49261ae650dSJack F Vogel 		my_dev = (bus << 8) | hw->bus.device;
49361ae650dSJack F Vogel 
49461ae650dSJack F Vogel 		for (i = 0; i < ixl_dev_count; i++) {
49561ae650dSJack F Vogel 			if (ixl_dev_track[i] == my_dev)
49661ae650dSJack F Vogel 				found = TRUE;
49761ae650dSJack F Vogel 		}
49861ae650dSJack F Vogel 
49961ae650dSJack F Vogel                 if (!found) {
50061ae650dSJack F Vogel                         u32 reg;
50161ae650dSJack F Vogel 
50261ae650dSJack F Vogel                         ixl_dev_track[ixl_dev_count] = my_dev;
50361ae650dSJack F Vogel                         ixl_dev_count++;
50461ae650dSJack F Vogel 
50561ae650dSJack F Vogel 			INIT_DEBUGOUT("Initial CORE RESET\n");
50661ae650dSJack F Vogel                         wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
50761ae650dSJack F Vogel                         ixl_flush(hw);
50861ae650dSJack F Vogel                         i = 50;
50961ae650dSJack F Vogel                         do {
51061ae650dSJack F Vogel 				i40e_msec_delay(50);
51161ae650dSJack F Vogel                                 reg = rd32(hw, I40E_GLGEN_RSTAT);
51261ae650dSJack F Vogel                                 if (!(reg & I40E_GLGEN_RSTAT_DEVSTATE_MASK))
51361ae650dSJack F Vogel                                         break;
51461ae650dSJack F Vogel                         } while (i--);
51561ae650dSJack F Vogel 
51661ae650dSJack F Vogel                         /* paranoia */
51761ae650dSJack F Vogel                         wr32(hw, I40E_PF_ATQLEN, 0);
51861ae650dSJack F Vogel                         wr32(hw, I40E_PF_ATQBAL, 0);
51961ae650dSJack F Vogel                         wr32(hw, I40E_PF_ATQBAH, 0);
52061ae650dSJack F Vogel                         i40e_clear_pxe_mode(hw);
52161ae650dSJack F Vogel                 }
52261ae650dSJack F Vogel                 mtx_unlock(&ixl_reset_mtx);
52361ae650dSJack F Vogel 	}
52461ae650dSJack F Vogel 
52561ae650dSJack F Vogel 	/* Set admin queue parameters */
52661ae650dSJack F Vogel 	hw->aq.num_arq_entries = IXL_AQ_LEN;
52761ae650dSJack F Vogel 	hw->aq.num_asq_entries = IXL_AQ_LEN;
52861ae650dSJack F Vogel 	hw->aq.arq_buf_size = IXL_AQ_BUFSZ;
52961ae650dSJack F Vogel 	hw->aq.asq_buf_size = IXL_AQ_BUFSZ;
53061ae650dSJack F Vogel 
53161ae650dSJack F Vogel 	/* Initialize the shared code */
53261ae650dSJack F Vogel 	error = i40e_init_shared_code(hw);
53361ae650dSJack F Vogel 	if (error) {
53461ae650dSJack F Vogel 		device_printf(dev,"Unable to initialize the shared code\n");
53561ae650dSJack F Vogel 		error = EIO;
53661ae650dSJack F Vogel 		goto err_out;
53761ae650dSJack F Vogel 	}
53861ae650dSJack F Vogel 
53961ae650dSJack F Vogel 	/* Set up the admin queue */
54061ae650dSJack F Vogel 	error = i40e_init_adminq(hw);
54161ae650dSJack F Vogel 	if (error) {
54261ae650dSJack F Vogel 		device_printf(dev, "The driver for the device stopped "
54361ae650dSJack F Vogel 		    "because the NVM image is newer than expected.\n"
54461ae650dSJack F Vogel 		    "You must install the most recent version of "
54561ae650dSJack F Vogel 		    " the network driver.\n");
54661ae650dSJack F Vogel 		goto err_out;
54761ae650dSJack F Vogel 	}
54861ae650dSJack F Vogel 	device_printf(dev, "%s\n", ixl_fw_version_str(hw));
54961ae650dSJack F Vogel 
55061ae650dSJack F Vogel         if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
55161ae650dSJack F Vogel 	    hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
55261ae650dSJack F Vogel 		device_printf(dev, "The driver for the device detected "
55361ae650dSJack F Vogel 		    "a newer version of the NVM image than expected.\n"
55461ae650dSJack F Vogel 		    "Please install the most recent version of the network driver.\n");
55561ae650dSJack F Vogel 	else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
55661ae650dSJack F Vogel 	    hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
55761ae650dSJack F Vogel 		device_printf(dev, "The driver for the device detected "
55861ae650dSJack F Vogel 		    "an older version of the NVM image than expected.\n"
55961ae650dSJack F Vogel 		    "Please update the NVM image.\n");
56061ae650dSJack F Vogel 
56161ae650dSJack F Vogel 	/* Clear PXE mode */
56261ae650dSJack F Vogel 	i40e_clear_pxe_mode(hw);
56361ae650dSJack F Vogel 
56461ae650dSJack F Vogel 	/* Get capabilities from the device */
56561ae650dSJack F Vogel 	error = ixl_get_hw_capabilities(pf);
56661ae650dSJack F Vogel 	if (error) {
56761ae650dSJack F Vogel 		device_printf(dev, "HW capabilities failure!\n");
56861ae650dSJack F Vogel 		goto err_get_cap;
56961ae650dSJack F Vogel 	}
57061ae650dSJack F Vogel 
57161ae650dSJack F Vogel 	/* Set up host memory cache */
57261ae650dSJack F Vogel 	error = i40e_init_lan_hmc(hw, vsi->num_queues, vsi->num_queues, 0, 0);
57361ae650dSJack F Vogel 	if (error) {
57461ae650dSJack F Vogel 		device_printf(dev, "init_lan_hmc failed: %d\n", error);
57561ae650dSJack F Vogel 		goto err_get_cap;
57661ae650dSJack F Vogel 	}
57761ae650dSJack F Vogel 
57861ae650dSJack F Vogel 	error = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
57961ae650dSJack F Vogel 	if (error) {
58061ae650dSJack F Vogel 		device_printf(dev, "configure_lan_hmc failed: %d\n", error);
58161ae650dSJack F Vogel 		goto err_mac_hmc;
58261ae650dSJack F Vogel 	}
58361ae650dSJack F Vogel 
58461ae650dSJack F Vogel 	/* Disable LLDP from the firmware */
58561ae650dSJack F Vogel 	i40e_aq_stop_lldp(hw, TRUE, NULL);
58661ae650dSJack F Vogel 
58761ae650dSJack F Vogel 	i40e_get_mac_addr(hw, hw->mac.addr);
58861ae650dSJack F Vogel 	error = i40e_validate_mac_addr(hw->mac.addr);
58961ae650dSJack F Vogel 	if (error) {
59061ae650dSJack F Vogel 		device_printf(dev, "validate_mac_addr failed: %d\n", error);
59161ae650dSJack F Vogel 		goto err_mac_hmc;
59261ae650dSJack F Vogel 	}
59361ae650dSJack F Vogel 	bcopy(hw->mac.addr, hw->mac.perm_addr, ETHER_ADDR_LEN);
59461ae650dSJack F Vogel 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
59561ae650dSJack F Vogel 
59661ae650dSJack F Vogel 	if (ixl_setup_stations(pf) != 0) {
59761ae650dSJack F Vogel 		device_printf(dev, "setup stations failed!\n");
59861ae650dSJack F Vogel 		error = ENOMEM;
59961ae650dSJack F Vogel 		goto err_mac_hmc;
60061ae650dSJack F Vogel 	}
60161ae650dSJack F Vogel 
60261ae650dSJack F Vogel 	/* Initialize mac filter list for VSI */
60361ae650dSJack F Vogel 	SLIST_INIT(&vsi->ftl);
60461ae650dSJack F Vogel 
60561ae650dSJack F Vogel 	/* Set up interrupt routing here */
60661ae650dSJack F Vogel 	if (pf->msix > 1)
60761ae650dSJack F Vogel 		error = ixl_assign_vsi_msix(pf);
60861ae650dSJack F Vogel 	else
60961ae650dSJack F Vogel 		error = ixl_assign_vsi_legacy(pf);
61061ae650dSJack F Vogel 	if (error)
61161ae650dSJack F Vogel 		goto err_late;
61261ae650dSJack F Vogel 
61361ae650dSJack F Vogel 	i40e_msec_delay(75);
61461ae650dSJack F Vogel 	error = i40e_aq_set_link_restart_an(hw, TRUE, NULL);
61561ae650dSJack F Vogel 	if (error) {
61661ae650dSJack F Vogel 		device_printf(dev, "link restart failed, aq_err=%d\n",
61761ae650dSJack F Vogel 		    pf->hw.aq.asq_last_status);
61861ae650dSJack F Vogel 	}
61961ae650dSJack F Vogel 
62061ae650dSJack F Vogel 	/* Determine link state */
62161ae650dSJack F Vogel 	vsi->link_up = ixl_config_link(hw);
62261ae650dSJack F Vogel 
62361ae650dSJack F Vogel 	/* Report if Unqualified modules are found */
62461ae650dSJack F Vogel 	if ((vsi->link_up == FALSE) &&
62561ae650dSJack F Vogel 	    (pf->hw.phy.link_info.link_info &
62661ae650dSJack F Vogel 	    I40E_AQ_MEDIA_AVAILABLE) &&
62761ae650dSJack F Vogel 	    (!(pf->hw.phy.link_info.an_info &
62861ae650dSJack F Vogel 	    I40E_AQ_QUALIFIED_MODULE)))
62961ae650dSJack F Vogel 		device_printf(dev, "Link failed because "
63061ae650dSJack F Vogel 		    "an unqualified module was detected\n");
63161ae650dSJack F Vogel 
63261ae650dSJack F Vogel 	/* Setup OS specific network interface */
63361ae650dSJack F Vogel 	if (ixl_setup_interface(dev, vsi) != 0)
63461ae650dSJack F Vogel 		goto err_late;
63561ae650dSJack F Vogel 
63661ae650dSJack F Vogel 	/* Get the bus configuration and set the shared code */
63761ae650dSJack F Vogel 	bus = ixl_get_bus_info(hw, dev);
63861ae650dSJack F Vogel 	i40e_set_pci_config_data(hw, bus);
63961ae650dSJack F Vogel 
64061ae650dSJack F Vogel 	/* Initialize statistics */
64161ae650dSJack F Vogel 	ixl_pf_reset_stats(pf);
64261ae650dSJack F Vogel 	ixl_update_stats_counters(pf);
64361ae650dSJack F Vogel 	ixl_add_hw_stats(pf);
64461ae650dSJack F Vogel 
64561ae650dSJack F Vogel 	/* Register for VLAN events */
64661ae650dSJack F Vogel 	vsi->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
64761ae650dSJack F Vogel 	    ixl_register_vlan, vsi, EVENTHANDLER_PRI_FIRST);
64861ae650dSJack F Vogel 	vsi->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
64961ae650dSJack F Vogel 	    ixl_unregister_vlan, vsi, EVENTHANDLER_PRI_FIRST);
65061ae650dSJack F Vogel 
65161ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_attach: end");
65261ae650dSJack F Vogel 	return (0);
65361ae650dSJack F Vogel 
65461ae650dSJack F Vogel err_late:
65561ae650dSJack F Vogel 	ixl_free_vsi(vsi);
65661ae650dSJack F Vogel err_mac_hmc:
65761ae650dSJack F Vogel 	i40e_shutdown_lan_hmc(hw);
65861ae650dSJack F Vogel err_get_cap:
65961ae650dSJack F Vogel 	i40e_shutdown_adminq(hw);
66061ae650dSJack F Vogel err_out:
66161ae650dSJack F Vogel 	if (vsi->ifp != NULL)
66261ae650dSJack F Vogel 		if_free(vsi->ifp);
66361ae650dSJack F Vogel 	ixl_free_pci_resources(pf);
66461ae650dSJack F Vogel 	IXL_PF_LOCK_DESTROY(pf);
66561ae650dSJack F Vogel 	return (error);
66661ae650dSJack F Vogel }
66761ae650dSJack F Vogel 
66861ae650dSJack F Vogel /*********************************************************************
66961ae650dSJack F Vogel  *  Device removal routine
67061ae650dSJack F Vogel  *
67161ae650dSJack F Vogel  *  The detach entry point is called when the driver is being removed.
67261ae650dSJack F Vogel  *  This routine stops the adapter and deallocates all the resources
67361ae650dSJack F Vogel  *  that were allocated for driver operation.
67461ae650dSJack F Vogel  *
67561ae650dSJack F Vogel  *  return 0 on success, positive on failure
67661ae650dSJack F Vogel  *********************************************************************/
67761ae650dSJack F Vogel 
67861ae650dSJack F Vogel static int
67961ae650dSJack F Vogel ixl_detach(device_t dev)
68061ae650dSJack F Vogel {
68161ae650dSJack F Vogel 	struct ixl_pf		*pf = device_get_softc(dev);
68261ae650dSJack F Vogel 	struct i40e_hw		*hw = &pf->hw;
68361ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
68461ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
68561ae650dSJack F Vogel 	i40e_status		status;
68661ae650dSJack F Vogel 
68761ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_detach: begin");
68861ae650dSJack F Vogel 
68961ae650dSJack F Vogel 	/* Make sure VLANS are not using driver */
69061ae650dSJack F Vogel 	if (vsi->ifp->if_vlantrunk != NULL) {
69161ae650dSJack F Vogel 		device_printf(dev,"Vlan in use, detach first\n");
69261ae650dSJack F Vogel 		return (EBUSY);
69361ae650dSJack F Vogel 	}
69461ae650dSJack F Vogel 
69561ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
69661ae650dSJack F Vogel 	ixl_stop(pf);
69761ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
69861ae650dSJack F Vogel 
69961ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, que++) {
70061ae650dSJack F Vogel 		if (que->tq) {
70161ae650dSJack F Vogel 			taskqueue_drain(que->tq, &que->task);
70261ae650dSJack F Vogel 			taskqueue_drain(que->tq, &que->tx_task);
70361ae650dSJack F Vogel 			taskqueue_free(que->tq);
70461ae650dSJack F Vogel 		}
70561ae650dSJack F Vogel 	}
70661ae650dSJack F Vogel 
70761ae650dSJack F Vogel 	/* Shutdown LAN HMC */
70861ae650dSJack F Vogel 	status = i40e_shutdown_lan_hmc(hw);
70961ae650dSJack F Vogel 	if (status)
71061ae650dSJack F Vogel 		device_printf(dev,
71161ae650dSJack F Vogel 		    "Shutdown LAN HMC failed with code %d\n", status);
71261ae650dSJack F Vogel 
71361ae650dSJack F Vogel 	/* Shutdown admin queue */
71461ae650dSJack F Vogel 	status = i40e_shutdown_adminq(hw);
71561ae650dSJack F Vogel 	if (status)
71661ae650dSJack F Vogel 		device_printf(dev,
71761ae650dSJack F Vogel 		    "Shutdown Admin queue failed with code %d\n", status);
71861ae650dSJack F Vogel 
71961ae650dSJack F Vogel 	/* Unregister VLAN events */
72061ae650dSJack F Vogel 	if (vsi->vlan_attach != NULL)
72161ae650dSJack F Vogel 		EVENTHANDLER_DEREGISTER(vlan_config, vsi->vlan_attach);
72261ae650dSJack F Vogel 	if (vsi->vlan_detach != NULL)
72361ae650dSJack F Vogel 		EVENTHANDLER_DEREGISTER(vlan_unconfig, vsi->vlan_detach);
72461ae650dSJack F Vogel 
72561ae650dSJack F Vogel 	ether_ifdetach(vsi->ifp);
72661ae650dSJack F Vogel 	callout_drain(&pf->timer);
72761ae650dSJack F Vogel 
72861ae650dSJack F Vogel 	ixl_free_pci_resources(pf);
72961ae650dSJack F Vogel 	bus_generic_detach(dev);
73061ae650dSJack F Vogel 	if_free(vsi->ifp);
73161ae650dSJack F Vogel 	ixl_free_vsi(vsi);
73261ae650dSJack F Vogel 	IXL_PF_LOCK_DESTROY(pf);
73361ae650dSJack F Vogel 	return (0);
73461ae650dSJack F Vogel }
73561ae650dSJack F Vogel 
73661ae650dSJack F Vogel /*********************************************************************
73761ae650dSJack F Vogel  *
73861ae650dSJack F Vogel  *  Shutdown entry point
73961ae650dSJack F Vogel  *
74061ae650dSJack F Vogel  **********************************************************************/
74161ae650dSJack F Vogel 
74261ae650dSJack F Vogel static int
74361ae650dSJack F Vogel ixl_shutdown(device_t dev)
74461ae650dSJack F Vogel {
74561ae650dSJack F Vogel 	struct ixl_pf *pf = device_get_softc(dev);
74661ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
74761ae650dSJack F Vogel 	ixl_stop(pf);
74861ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
74961ae650dSJack F Vogel 	return (0);
75061ae650dSJack F Vogel }
75161ae650dSJack F Vogel 
75261ae650dSJack F Vogel 
75361ae650dSJack F Vogel /*********************************************************************
75461ae650dSJack F Vogel  *
75561ae650dSJack F Vogel  *  Get the hardware capabilities
75661ae650dSJack F Vogel  *
75761ae650dSJack F Vogel  **********************************************************************/
75861ae650dSJack F Vogel 
75961ae650dSJack F Vogel static int
76061ae650dSJack F Vogel ixl_get_hw_capabilities(struct ixl_pf *pf)
76161ae650dSJack F Vogel {
76261ae650dSJack F Vogel 	struct i40e_aqc_list_capabilities_element_resp *buf;
76361ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
76461ae650dSJack F Vogel 	device_t 	dev = pf->dev;
76561ae650dSJack F Vogel 	int             error, len;
76661ae650dSJack F Vogel 	u16		needed;
76761ae650dSJack F Vogel 	bool		again = TRUE;
76861ae650dSJack F Vogel 
76961ae650dSJack F Vogel 	len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
77061ae650dSJack F Vogel retry:
77161ae650dSJack F Vogel 	if (!(buf = (struct i40e_aqc_list_capabilities_element_resp *)
77261ae650dSJack F Vogel 	    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO))) {
77361ae650dSJack F Vogel 		device_printf(dev, "Unable to allocate cap memory\n");
77461ae650dSJack F Vogel                 return (ENOMEM);
77561ae650dSJack F Vogel 	}
77661ae650dSJack F Vogel 
77761ae650dSJack F Vogel 	/* This populates the hw struct */
77861ae650dSJack F Vogel         error = i40e_aq_discover_capabilities(hw, buf, len,
77961ae650dSJack F Vogel 	    &needed, i40e_aqc_opc_list_func_capabilities, NULL);
78061ae650dSJack F Vogel 	free(buf, M_DEVBUF);
78161ae650dSJack F Vogel 	if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) &&
78261ae650dSJack F Vogel 	    (again == TRUE)) {
78361ae650dSJack F Vogel 		/* retry once with a larger buffer */
78461ae650dSJack F Vogel 		again = FALSE;
78561ae650dSJack F Vogel 		len = needed;
78661ae650dSJack F Vogel 		goto retry;
78761ae650dSJack F Vogel 	} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
78861ae650dSJack F Vogel 		device_printf(dev, "capability discovery failed: %d\n",
78961ae650dSJack F Vogel 		    pf->hw.aq.asq_last_status);
79061ae650dSJack F Vogel 		return (ENODEV);
79161ae650dSJack F Vogel 	}
79261ae650dSJack F Vogel 
79361ae650dSJack F Vogel 	/* Capture this PF's starting queue pair */
79461ae650dSJack F Vogel 	pf->qbase = hw->func_caps.base_queue;
79561ae650dSJack F Vogel 
79661ae650dSJack F Vogel #ifdef IXL_DEBUG
79761ae650dSJack F Vogel 	device_printf(dev,"pf_id=%d, num_vfs=%d, msix_pf=%d, "
79861ae650dSJack F Vogel 	    "msix_vf=%d, fd_g=%d, fd_b=%d, tx_qp=%d rx_qp=%d qbase=%d\n",
79961ae650dSJack F Vogel 	    hw->pf_id, hw->func_caps.num_vfs,
80061ae650dSJack F Vogel 	    hw->func_caps.num_msix_vectors,
80161ae650dSJack F Vogel 	    hw->func_caps.num_msix_vectors_vf,
80261ae650dSJack F Vogel 	    hw->func_caps.fd_filters_guaranteed,
80361ae650dSJack F Vogel 	    hw->func_caps.fd_filters_best_effort,
80461ae650dSJack F Vogel 	    hw->func_caps.num_tx_qp,
80561ae650dSJack F Vogel 	    hw->func_caps.num_rx_qp,
80661ae650dSJack F Vogel 	    hw->func_caps.base_queue);
80761ae650dSJack F Vogel #endif
80861ae650dSJack F Vogel 	return (error);
80961ae650dSJack F Vogel }
81061ae650dSJack F Vogel 
81161ae650dSJack F Vogel static void
81261ae650dSJack F Vogel ixl_cap_txcsum_tso(struct ixl_vsi *vsi, struct ifnet *ifp, int mask)
81361ae650dSJack F Vogel {
81461ae650dSJack F Vogel 	device_t 	dev = vsi->dev;
81561ae650dSJack F Vogel 
81661ae650dSJack F Vogel 	/* Enable/disable TXCSUM/TSO4 */
81761ae650dSJack F Vogel 	if (!(ifp->if_capenable & IFCAP_TXCSUM)
81861ae650dSJack F Vogel 	    && !(ifp->if_capenable & IFCAP_TSO4)) {
81961ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM) {
82061ae650dSJack F Vogel 			ifp->if_capenable |= IFCAP_TXCSUM;
82161ae650dSJack F Vogel 			/* enable TXCSUM, restore TSO if previously enabled */
82261ae650dSJack F Vogel 			if (vsi->flags & IXL_FLAGS_KEEP_TSO4) {
82361ae650dSJack F Vogel 				vsi->flags &= ~IXL_FLAGS_KEEP_TSO4;
82461ae650dSJack F Vogel 				ifp->if_capenable |= IFCAP_TSO4;
82561ae650dSJack F Vogel 			}
82661ae650dSJack F Vogel 		}
82761ae650dSJack F Vogel 		else if (mask & IFCAP_TSO4) {
82861ae650dSJack F Vogel 			ifp->if_capenable |= (IFCAP_TXCSUM | IFCAP_TSO4);
82961ae650dSJack F Vogel 			vsi->flags &= ~IXL_FLAGS_KEEP_TSO4;
83061ae650dSJack F Vogel 			device_printf(dev,
83161ae650dSJack F Vogel 			    "TSO4 requires txcsum, enabling both...\n");
83261ae650dSJack F Vogel 		}
83361ae650dSJack F Vogel 	} else if((ifp->if_capenable & IFCAP_TXCSUM)
83461ae650dSJack F Vogel 	    && !(ifp->if_capenable & IFCAP_TSO4)) {
83561ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM)
83661ae650dSJack F Vogel 			ifp->if_capenable &= ~IFCAP_TXCSUM;
83761ae650dSJack F Vogel 		else if (mask & IFCAP_TSO4)
83861ae650dSJack F Vogel 			ifp->if_capenable |= IFCAP_TSO4;
83961ae650dSJack F Vogel 	} else if((ifp->if_capenable & IFCAP_TXCSUM)
84061ae650dSJack F Vogel 	    && (ifp->if_capenable & IFCAP_TSO4)) {
84161ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM) {
84261ae650dSJack F Vogel 			vsi->flags |= IXL_FLAGS_KEEP_TSO4;
84361ae650dSJack F Vogel 			ifp->if_capenable &= ~(IFCAP_TXCSUM | IFCAP_TSO4);
84461ae650dSJack F Vogel 			device_printf(dev,
84561ae650dSJack F Vogel 			    "TSO4 requires txcsum, disabling both...\n");
84661ae650dSJack F Vogel 		} else if (mask & IFCAP_TSO4)
84761ae650dSJack F Vogel 			ifp->if_capenable &= ~IFCAP_TSO4;
84861ae650dSJack F Vogel 	}
84961ae650dSJack F Vogel 
85061ae650dSJack F Vogel 	/* Enable/disable TXCSUM_IPV6/TSO6 */
85161ae650dSJack F Vogel 	if (!(ifp->if_capenable & IFCAP_TXCSUM_IPV6)
85261ae650dSJack F Vogel 	    && !(ifp->if_capenable & IFCAP_TSO6)) {
85361ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM_IPV6) {
85461ae650dSJack F Vogel 			ifp->if_capenable |= IFCAP_TXCSUM_IPV6;
85561ae650dSJack F Vogel 			if (vsi->flags & IXL_FLAGS_KEEP_TSO6) {
85661ae650dSJack F Vogel 				vsi->flags &= ~IXL_FLAGS_KEEP_TSO6;
85761ae650dSJack F Vogel 				ifp->if_capenable |= IFCAP_TSO6;
85861ae650dSJack F Vogel 			}
85961ae650dSJack F Vogel 		} else if (mask & IFCAP_TSO6) {
86061ae650dSJack F Vogel 			ifp->if_capenable |= (IFCAP_TXCSUM_IPV6 | IFCAP_TSO6);
86161ae650dSJack F Vogel 			vsi->flags &= ~IXL_FLAGS_KEEP_TSO6;
86261ae650dSJack F Vogel 			device_printf(dev,
86361ae650dSJack F Vogel 			    "TSO6 requires txcsum6, enabling both...\n");
86461ae650dSJack F Vogel 		}
86561ae650dSJack F Vogel 	} else if((ifp->if_capenable & IFCAP_TXCSUM_IPV6)
86661ae650dSJack F Vogel 	    && !(ifp->if_capenable & IFCAP_TSO6)) {
86761ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM_IPV6)
86861ae650dSJack F Vogel 			ifp->if_capenable &= ~IFCAP_TXCSUM_IPV6;
86961ae650dSJack F Vogel 		else if (mask & IFCAP_TSO6)
87061ae650dSJack F Vogel 			ifp->if_capenable |= IFCAP_TSO6;
87161ae650dSJack F Vogel 	} else if ((ifp->if_capenable & IFCAP_TXCSUM_IPV6)
87261ae650dSJack F Vogel 	    && (ifp->if_capenable & IFCAP_TSO6)) {
87361ae650dSJack F Vogel 		if (mask & IFCAP_TXCSUM_IPV6) {
87461ae650dSJack F Vogel 			vsi->flags |= IXL_FLAGS_KEEP_TSO6;
87561ae650dSJack F Vogel 			ifp->if_capenable &= ~(IFCAP_TXCSUM_IPV6 | IFCAP_TSO6);
87661ae650dSJack F Vogel 			device_printf(dev,
87761ae650dSJack F Vogel 			    "TSO6 requires txcsum6, disabling both...\n");
87861ae650dSJack F Vogel 		} else if (mask & IFCAP_TSO6)
87961ae650dSJack F Vogel 			ifp->if_capenable &= ~IFCAP_TSO6;
88061ae650dSJack F Vogel 	}
88161ae650dSJack F Vogel }
88261ae650dSJack F Vogel 
88361ae650dSJack F Vogel /*********************************************************************
88461ae650dSJack F Vogel  *  Ioctl entry point
88561ae650dSJack F Vogel  *
88661ae650dSJack F Vogel  *  ixl_ioctl is called when the user wants to configure the
88761ae650dSJack F Vogel  *  interface.
88861ae650dSJack F Vogel  *
88961ae650dSJack F Vogel  *  return 0 on success, positive on failure
89061ae650dSJack F Vogel  **********************************************************************/
89161ae650dSJack F Vogel 
89261ae650dSJack F Vogel static int
89361ae650dSJack F Vogel ixl_ioctl(struct ifnet * ifp, u_long command, caddr_t data)
89461ae650dSJack F Vogel {
89561ae650dSJack F Vogel 	struct ixl_vsi	*vsi = ifp->if_softc;
89661ae650dSJack F Vogel 	struct ixl_pf	*pf = (struct ixl_pf *)vsi->back;
89761ae650dSJack F Vogel 	struct ifreq	*ifr = (struct ifreq *) data;
89861ae650dSJack F Vogel #if defined(INET) || defined(INET6)
89961ae650dSJack F Vogel 	struct ifaddr *ifa = (struct ifaddr *)data;
90061ae650dSJack F Vogel 	bool		avoid_reset = FALSE;
90161ae650dSJack F Vogel #endif
90261ae650dSJack F Vogel 	int             error = 0;
90361ae650dSJack F Vogel 
90461ae650dSJack F Vogel 	switch (command) {
90561ae650dSJack F Vogel 
90661ae650dSJack F Vogel         case SIOCSIFADDR:
90761ae650dSJack F Vogel #ifdef INET
90861ae650dSJack F Vogel 		if (ifa->ifa_addr->sa_family == AF_INET)
90961ae650dSJack F Vogel 			avoid_reset = TRUE;
91061ae650dSJack F Vogel #endif
91161ae650dSJack F Vogel #ifdef INET6
91261ae650dSJack F Vogel 		if (ifa->ifa_addr->sa_family == AF_INET6)
91361ae650dSJack F Vogel 			avoid_reset = TRUE;
91461ae650dSJack F Vogel #endif
91561ae650dSJack F Vogel #if defined(INET) || defined(INET6)
91661ae650dSJack F Vogel 		/*
91761ae650dSJack F Vogel 		** Calling init results in link renegotiation,
91861ae650dSJack F Vogel 		** so we avoid doing it when possible.
91961ae650dSJack F Vogel 		*/
92061ae650dSJack F Vogel 		if (avoid_reset) {
92161ae650dSJack F Vogel 			ifp->if_flags |= IFF_UP;
92261ae650dSJack F Vogel 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
92361ae650dSJack F Vogel 				ixl_init(pf);
92461ae650dSJack F Vogel 			if (!(ifp->if_flags & IFF_NOARP))
92561ae650dSJack F Vogel 				arp_ifinit(ifp, ifa);
92661ae650dSJack F Vogel 		} else
92761ae650dSJack F Vogel 			error = ether_ioctl(ifp, command, data);
92861ae650dSJack F Vogel 		break;
92961ae650dSJack F Vogel #endif
93061ae650dSJack F Vogel 	case SIOCSIFMTU:
93161ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
93261ae650dSJack F Vogel 		if (ifr->ifr_mtu > IXL_MAX_FRAME -
93361ae650dSJack F Vogel 		   ETHER_HDR_LEN - ETHER_CRC_LEN - ETHER_VLAN_ENCAP_LEN) {
93461ae650dSJack F Vogel 			error = EINVAL;
93561ae650dSJack F Vogel 		} else {
93661ae650dSJack F Vogel 			IXL_PF_LOCK(pf);
93761ae650dSJack F Vogel 			ifp->if_mtu = ifr->ifr_mtu;
93861ae650dSJack F Vogel 			vsi->max_frame_size =
93961ae650dSJack F Vogel 				ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN
94061ae650dSJack F Vogel 			    + ETHER_VLAN_ENCAP_LEN;
94161ae650dSJack F Vogel 			ixl_init_locked(pf);
94261ae650dSJack F Vogel 			IXL_PF_UNLOCK(pf);
94361ae650dSJack F Vogel 		}
94461ae650dSJack F Vogel 		break;
94561ae650dSJack F Vogel 	case SIOCSIFFLAGS:
94661ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
94761ae650dSJack F Vogel 		IXL_PF_LOCK(pf);
94861ae650dSJack F Vogel 		if (ifp->if_flags & IFF_UP) {
94961ae650dSJack F Vogel 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
95061ae650dSJack F Vogel 				if ((ifp->if_flags ^ pf->if_flags) &
95161ae650dSJack F Vogel 				    (IFF_PROMISC | IFF_ALLMULTI)) {
95261ae650dSJack F Vogel 					ixl_set_promisc(vsi);
95361ae650dSJack F Vogel 				}
95461ae650dSJack F Vogel 			} else
95561ae650dSJack F Vogel 				ixl_init_locked(pf);
95661ae650dSJack F Vogel 		} else
95761ae650dSJack F Vogel 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
95861ae650dSJack F Vogel 				ixl_stop(pf);
95961ae650dSJack F Vogel 		pf->if_flags = ifp->if_flags;
96061ae650dSJack F Vogel 		IXL_PF_UNLOCK(pf);
96161ae650dSJack F Vogel 		break;
96261ae650dSJack F Vogel 	case SIOCADDMULTI:
96361ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCADDMULTI");
96461ae650dSJack F Vogel 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
96561ae650dSJack F Vogel 			IXL_PF_LOCK(pf);
96661ae650dSJack F Vogel 			ixl_disable_intr(vsi);
96761ae650dSJack F Vogel 			ixl_add_multi(vsi);
96861ae650dSJack F Vogel 			ixl_enable_intr(vsi);
96961ae650dSJack F Vogel 			IXL_PF_UNLOCK(pf);
97061ae650dSJack F Vogel 		}
97161ae650dSJack F Vogel 		break;
97261ae650dSJack F Vogel 	case SIOCDELMULTI:
97361ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCDELMULTI");
97461ae650dSJack F Vogel 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
97561ae650dSJack F Vogel 			IXL_PF_LOCK(pf);
97661ae650dSJack F Vogel 			ixl_disable_intr(vsi);
97761ae650dSJack F Vogel 			ixl_del_multi(vsi);
97861ae650dSJack F Vogel 			ixl_enable_intr(vsi);
97961ae650dSJack F Vogel 			IXL_PF_UNLOCK(pf);
98061ae650dSJack F Vogel 		}
98161ae650dSJack F Vogel 		break;
98261ae650dSJack F Vogel 	case SIOCSIFMEDIA:
98361ae650dSJack F Vogel 	case SIOCGIFMEDIA:
98461ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
98561ae650dSJack F Vogel 		error = ifmedia_ioctl(ifp, ifr, &vsi->media, command);
98661ae650dSJack F Vogel 		break;
98761ae650dSJack F Vogel 	case SIOCSIFCAP:
98861ae650dSJack F Vogel 	{
98961ae650dSJack F Vogel 		int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
99061ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
99161ae650dSJack F Vogel 
99261ae650dSJack F Vogel 		ixl_cap_txcsum_tso(vsi, ifp, mask);
99361ae650dSJack F Vogel 
99461ae650dSJack F Vogel 		if (mask & IFCAP_RXCSUM)
99561ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_RXCSUM;
99661ae650dSJack F Vogel 		if (mask & IFCAP_RXCSUM_IPV6)
99761ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
99861ae650dSJack F Vogel 		if (mask & IFCAP_LRO)
99961ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_LRO;
100061ae650dSJack F Vogel 		if (mask & IFCAP_VLAN_HWTAGGING)
100161ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
100261ae650dSJack F Vogel 		if (mask & IFCAP_VLAN_HWFILTER)
100361ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
100461ae650dSJack F Vogel 		if (mask & IFCAP_VLAN_HWTSO)
100561ae650dSJack F Vogel 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
100661ae650dSJack F Vogel 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
100761ae650dSJack F Vogel 			IXL_PF_LOCK(pf);
100861ae650dSJack F Vogel 			ixl_init_locked(pf);
100961ae650dSJack F Vogel 			IXL_PF_UNLOCK(pf);
101061ae650dSJack F Vogel 		}
101161ae650dSJack F Vogel 		VLAN_CAPABILITIES(ifp);
101261ae650dSJack F Vogel 
101361ae650dSJack F Vogel 		break;
101461ae650dSJack F Vogel 	}
101561ae650dSJack F Vogel 
101661ae650dSJack F Vogel 	default:
101761ae650dSJack F Vogel 		IOCTL_DEBUGOUT("ioctl: UNKNOWN (0x%X)\n", (int)command);
101861ae650dSJack F Vogel 		error = ether_ioctl(ifp, command, data);
101961ae650dSJack F Vogel 		break;
102061ae650dSJack F Vogel 	}
102161ae650dSJack F Vogel 
102261ae650dSJack F Vogel 	return (error);
102361ae650dSJack F Vogel }
102461ae650dSJack F Vogel 
102561ae650dSJack F Vogel 
102661ae650dSJack F Vogel /*********************************************************************
102761ae650dSJack F Vogel  *  Init entry point
102861ae650dSJack F Vogel  *
102961ae650dSJack F Vogel  *  This routine is used in two ways. It is used by the stack as
103061ae650dSJack F Vogel  *  init entry point in network interface structure. It is also used
103161ae650dSJack F Vogel  *  by the driver as a hw/sw initialization routine to get to a
103261ae650dSJack F Vogel  *  consistent state.
103361ae650dSJack F Vogel  *
103461ae650dSJack F Vogel  *  return 0 on success, positive on failure
103561ae650dSJack F Vogel  **********************************************************************/
103661ae650dSJack F Vogel 
103761ae650dSJack F Vogel static void
103861ae650dSJack F Vogel ixl_init_locked(struct ixl_pf *pf)
103961ae650dSJack F Vogel {
104061ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
104161ae650dSJack F Vogel 	struct ixl_vsi	*vsi = &pf->vsi;
104261ae650dSJack F Vogel 	struct ifnet	*ifp = vsi->ifp;
104361ae650dSJack F Vogel 	device_t 	dev = pf->dev;
104461ae650dSJack F Vogel 	struct i40e_filter_control_settings	filter;
104561ae650dSJack F Vogel 	u8		tmpaddr[ETHER_ADDR_LEN];
104661ae650dSJack F Vogel 	int		ret;
104761ae650dSJack F Vogel 
104861ae650dSJack F Vogel 	mtx_assert(&pf->pf_mtx, MA_OWNED);
104961ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_init: begin");
105061ae650dSJack F Vogel 	ixl_stop(pf);
105161ae650dSJack F Vogel 
105261ae650dSJack F Vogel 	/* Get the latest mac address... User might use a LAA */
105361ae650dSJack F Vogel 	bcopy(IF_LLADDR(vsi->ifp), tmpaddr,
105461ae650dSJack F Vogel 	      I40E_ETH_LENGTH_OF_ADDRESS);
105561ae650dSJack F Vogel 	if (!cmp_etheraddr(hw->mac.addr, tmpaddr) &&
105661ae650dSJack F Vogel 	    i40e_validate_mac_addr(tmpaddr)) {
105761ae650dSJack F Vogel 		bcopy(tmpaddr, hw->mac.addr,
105861ae650dSJack F Vogel 		    I40E_ETH_LENGTH_OF_ADDRESS);
105961ae650dSJack F Vogel 		ret = i40e_aq_mac_address_write(hw,
106061ae650dSJack F Vogel 		    I40E_AQC_WRITE_TYPE_LAA_ONLY,
106161ae650dSJack F Vogel 		    hw->mac.addr, NULL);
106261ae650dSJack F Vogel 		if (ret) {
106361ae650dSJack F Vogel 			device_printf(dev, "LLA address"
106461ae650dSJack F Vogel 			 "change failed!!\n");
106561ae650dSJack F Vogel 			return;
106661ae650dSJack F Vogel 		}
106761ae650dSJack F Vogel 	}
106861ae650dSJack F Vogel 
106961ae650dSJack F Vogel 	/* Set the various hardware offload abilities */
107061ae650dSJack F Vogel 	ifp->if_hwassist = 0;
107161ae650dSJack F Vogel 	if (ifp->if_capenable & IFCAP_TSO)
107261ae650dSJack F Vogel 		ifp->if_hwassist |= CSUM_TSO;
107361ae650dSJack F Vogel 	if (ifp->if_capenable & IFCAP_TXCSUM)
107461ae650dSJack F Vogel 		ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
107561ae650dSJack F Vogel 	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
107661ae650dSJack F Vogel 		ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
107761ae650dSJack F Vogel 
107861ae650dSJack F Vogel 	/* Set up the device filtering */
107961ae650dSJack F Vogel 	bzero(&filter, sizeof(filter));
108061ae650dSJack F Vogel 	filter.enable_ethtype = TRUE;
108161ae650dSJack F Vogel 	filter.enable_macvlan = TRUE;
108261ae650dSJack F Vogel #ifdef IXL_FDIR
108361ae650dSJack F Vogel 	filter.enable_fdir = TRUE;
108461ae650dSJack F Vogel #endif
108561ae650dSJack F Vogel 	if (i40e_set_filter_control(hw, &filter))
108661ae650dSJack F Vogel 		device_printf(dev, "set_filter_control() failed\n");
108761ae650dSJack F Vogel 
108861ae650dSJack F Vogel 	/* Set up RSS */
108961ae650dSJack F Vogel 	ixl_config_rss(vsi);
109061ae650dSJack F Vogel 
109161ae650dSJack F Vogel 	/* Setup the VSI */
109261ae650dSJack F Vogel 	ixl_setup_vsi(vsi);
109361ae650dSJack F Vogel 
109461ae650dSJack F Vogel 	/*
109561ae650dSJack F Vogel 	** Prepare the rings, hmc contexts, etc...
109661ae650dSJack F Vogel 	*/
109761ae650dSJack F Vogel 	if (ixl_initialize_vsi(vsi)) {
109861ae650dSJack F Vogel 		device_printf(dev, "initialize vsi failed!!\n");
109961ae650dSJack F Vogel 		return;
110061ae650dSJack F Vogel 	}
110161ae650dSJack F Vogel 
110261ae650dSJack F Vogel 	/* Add protocol filters to list */
110361ae650dSJack F Vogel 	ixl_init_filters(vsi);
110461ae650dSJack F Vogel 
110561ae650dSJack F Vogel 	/* Setup vlan's if needed */
110661ae650dSJack F Vogel 	ixl_setup_vlan_filters(vsi);
110761ae650dSJack F Vogel 
110861ae650dSJack F Vogel 	/* Start the local timer */
110961ae650dSJack F Vogel 	callout_reset(&pf->timer, hz, ixl_local_timer, pf);
111061ae650dSJack F Vogel 
111161ae650dSJack F Vogel 	/* Set up MSI/X routing and the ITR settings */
111261ae650dSJack F Vogel 	if (ixl_enable_msix) {
111361ae650dSJack F Vogel 		ixl_configure_msix(pf);
111461ae650dSJack F Vogel 		ixl_configure_itr(pf);
111561ae650dSJack F Vogel 	} else
111661ae650dSJack F Vogel 		ixl_configure_legacy(pf);
111761ae650dSJack F Vogel 
111861ae650dSJack F Vogel 	ixl_enable_rings(vsi);
111961ae650dSJack F Vogel 
112061ae650dSJack F Vogel 	i40e_aq_set_default_vsi(hw, vsi->seid, NULL);
112161ae650dSJack F Vogel 
112261ae650dSJack F Vogel 	/* Set MTU in hardware*/
112361ae650dSJack F Vogel 	int aq_error = i40e_aq_set_mac_config(hw, vsi->max_frame_size,
112461ae650dSJack F Vogel 	    TRUE, 0, NULL);
112561ae650dSJack F Vogel 	if (aq_error)
112661ae650dSJack F Vogel 		device_printf(vsi->dev,
112761ae650dSJack F Vogel 			"aq_set_mac_config in init error, code %d\n",
112861ae650dSJack F Vogel 		    aq_error);
112961ae650dSJack F Vogel 
113061ae650dSJack F Vogel 	/* And now turn on interrupts */
113161ae650dSJack F Vogel 	ixl_enable_intr(vsi);
113261ae650dSJack F Vogel 
113361ae650dSJack F Vogel 	/* Now inform the stack we're ready */
113461ae650dSJack F Vogel 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
113561ae650dSJack F Vogel 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
113661ae650dSJack F Vogel 
113761ae650dSJack F Vogel 	return;
113861ae650dSJack F Vogel }
113961ae650dSJack F Vogel 
114061ae650dSJack F Vogel static void
114161ae650dSJack F Vogel ixl_init(void *arg)
114261ae650dSJack F Vogel {
114361ae650dSJack F Vogel 	struct ixl_pf *pf = arg;
114461ae650dSJack F Vogel 
114561ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
114661ae650dSJack F Vogel 	ixl_init_locked(pf);
114761ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
114861ae650dSJack F Vogel 	return;
114961ae650dSJack F Vogel }
115061ae650dSJack F Vogel 
115161ae650dSJack F Vogel /*
115261ae650dSJack F Vogel **
115361ae650dSJack F Vogel ** MSIX Interrupt Handlers and Tasklets
115461ae650dSJack F Vogel **
115561ae650dSJack F Vogel */
115661ae650dSJack F Vogel static void
115761ae650dSJack F Vogel ixl_handle_que(void *context, int pending)
115861ae650dSJack F Vogel {
115961ae650dSJack F Vogel 	struct ixl_queue *que = context;
116061ae650dSJack F Vogel 	struct ixl_vsi *vsi = que->vsi;
116161ae650dSJack F Vogel 	struct i40e_hw  *hw = vsi->hw;
116261ae650dSJack F Vogel 	struct tx_ring  *txr = &que->txr;
116361ae650dSJack F Vogel 	struct ifnet    *ifp = vsi->ifp;
116461ae650dSJack F Vogel 	bool		more;
116561ae650dSJack F Vogel 
116661ae650dSJack F Vogel 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
116761ae650dSJack F Vogel 		more = ixl_rxeof(que, IXL_RX_LIMIT);
116861ae650dSJack F Vogel 		IXL_TX_LOCK(txr);
116961ae650dSJack F Vogel 		ixl_txeof(que);
117061ae650dSJack F Vogel 		if (!drbr_empty(ifp, txr->br))
117161ae650dSJack F Vogel 			ixl_mq_start_locked(ifp, txr);
117261ae650dSJack F Vogel 		IXL_TX_UNLOCK(txr);
117361ae650dSJack F Vogel 		if (more) {
117461ae650dSJack F Vogel 			taskqueue_enqueue(que->tq, &que->task);
117561ae650dSJack F Vogel 			return;
117661ae650dSJack F Vogel 		}
117761ae650dSJack F Vogel 	}
117861ae650dSJack F Vogel 
117961ae650dSJack F Vogel 	/* Reenable this interrupt - hmmm */
118061ae650dSJack F Vogel 	ixl_enable_queue(hw, que->me);
118161ae650dSJack F Vogel 	return;
118261ae650dSJack F Vogel }
118361ae650dSJack F Vogel 
118461ae650dSJack F Vogel 
118561ae650dSJack F Vogel /*********************************************************************
118661ae650dSJack F Vogel  *
118761ae650dSJack F Vogel  *  Legacy Interrupt Service routine
118861ae650dSJack F Vogel  *
118961ae650dSJack F Vogel  **********************************************************************/
119061ae650dSJack F Vogel void
119161ae650dSJack F Vogel ixl_intr(void *arg)
119261ae650dSJack F Vogel {
119361ae650dSJack F Vogel 	struct ixl_pf		*pf = arg;
119461ae650dSJack F Vogel 	struct i40e_hw		*hw =  &pf->hw;
119561ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
119661ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
119761ae650dSJack F Vogel 	struct ifnet		*ifp = vsi->ifp;
119861ae650dSJack F Vogel 	struct tx_ring		*txr = &que->txr;
119961ae650dSJack F Vogel         u32			reg, icr0, mask;
120061ae650dSJack F Vogel 	bool			more_tx, more_rx;
120161ae650dSJack F Vogel 
120261ae650dSJack F Vogel 	++que->irqs;
120361ae650dSJack F Vogel 
120461ae650dSJack F Vogel 	/* Protect against spurious interrupts */
120561ae650dSJack F Vogel 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
120661ae650dSJack F Vogel 		return;
120761ae650dSJack F Vogel 
120861ae650dSJack F Vogel 	icr0 = rd32(hw, I40E_PFINT_ICR0);
120961ae650dSJack F Vogel 
121061ae650dSJack F Vogel 	reg = rd32(hw, I40E_PFINT_DYN_CTL0);
121161ae650dSJack F Vogel 	reg = reg | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
121261ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
121361ae650dSJack F Vogel 
121461ae650dSJack F Vogel         mask = rd32(hw, I40E_PFINT_ICR0_ENA);
121561ae650dSJack F Vogel 
121661ae650dSJack F Vogel 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
121761ae650dSJack F Vogel 		taskqueue_enqueue(pf->tq, &pf->adminq);
121861ae650dSJack F Vogel 		return;
121961ae650dSJack F Vogel 	}
122061ae650dSJack F Vogel 
122161ae650dSJack F Vogel 	more_rx = ixl_rxeof(que, IXL_RX_LIMIT);
122261ae650dSJack F Vogel 
122361ae650dSJack F Vogel 	IXL_TX_LOCK(txr);
122461ae650dSJack F Vogel 	more_tx = ixl_txeof(que);
122561ae650dSJack F Vogel 	if (!drbr_empty(vsi->ifp, txr->br))
122661ae650dSJack F Vogel 		more_tx = 1;
122761ae650dSJack F Vogel 	IXL_TX_UNLOCK(txr);
122861ae650dSJack F Vogel 
122961ae650dSJack F Vogel 	/* re-enable other interrupt causes */
123061ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, mask);
123161ae650dSJack F Vogel 
123261ae650dSJack F Vogel 	/* And now the queues */
123361ae650dSJack F Vogel 	reg = rd32(hw, I40E_QINT_RQCTL(0));
123461ae650dSJack F Vogel 	reg |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
123561ae650dSJack F Vogel 	wr32(hw, I40E_QINT_RQCTL(0), reg);
123661ae650dSJack F Vogel 
123761ae650dSJack F Vogel 	reg = rd32(hw, I40E_QINT_TQCTL(0));
123861ae650dSJack F Vogel 	reg |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
123961ae650dSJack F Vogel 	reg &= ~I40E_PFINT_ICR0_INTEVENT_MASK;
124061ae650dSJack F Vogel 	wr32(hw, I40E_QINT_TQCTL(0), reg);
124161ae650dSJack F Vogel 
124261ae650dSJack F Vogel 	ixl_enable_legacy(hw);
124361ae650dSJack F Vogel 
124461ae650dSJack F Vogel 	return;
124561ae650dSJack F Vogel }
124661ae650dSJack F Vogel 
124761ae650dSJack F Vogel 
124861ae650dSJack F Vogel /*********************************************************************
124961ae650dSJack F Vogel  *
125061ae650dSJack F Vogel  *  MSIX VSI Interrupt Service routine
125161ae650dSJack F Vogel  *
125261ae650dSJack F Vogel  **********************************************************************/
125361ae650dSJack F Vogel void
125461ae650dSJack F Vogel ixl_msix_que(void *arg)
125561ae650dSJack F Vogel {
125661ae650dSJack F Vogel 	struct ixl_queue	*que = arg;
125761ae650dSJack F Vogel 	struct ixl_vsi	*vsi = que->vsi;
125861ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
125961ae650dSJack F Vogel 	struct tx_ring	*txr = &que->txr;
126061ae650dSJack F Vogel 	bool		more_tx, more_rx;
126161ae650dSJack F Vogel 
126261ae650dSJack F Vogel 	/* Protect against spurious interrupts */
126361ae650dSJack F Vogel 	if (!(vsi->ifp->if_drv_flags & IFF_DRV_RUNNING))
126461ae650dSJack F Vogel 		return;
126561ae650dSJack F Vogel 
126661ae650dSJack F Vogel 	++que->irqs;
126761ae650dSJack F Vogel 
126861ae650dSJack F Vogel 	more_rx = ixl_rxeof(que, IXL_RX_LIMIT);
126961ae650dSJack F Vogel 
127061ae650dSJack F Vogel 	IXL_TX_LOCK(txr);
127161ae650dSJack F Vogel 	more_tx = ixl_txeof(que);
127261ae650dSJack F Vogel 	/*
127361ae650dSJack F Vogel 	** Make certain that if the stack
127461ae650dSJack F Vogel 	** has anything queued the task gets
127561ae650dSJack F Vogel 	** scheduled to handle it.
127661ae650dSJack F Vogel 	*/
127761ae650dSJack F Vogel 	if (!drbr_empty(vsi->ifp, txr->br))
127861ae650dSJack F Vogel 		more_tx = 1;
127961ae650dSJack F Vogel 	IXL_TX_UNLOCK(txr);
128061ae650dSJack F Vogel 
128161ae650dSJack F Vogel 	ixl_set_queue_rx_itr(que);
128261ae650dSJack F Vogel 	ixl_set_queue_tx_itr(que);
128361ae650dSJack F Vogel 
128461ae650dSJack F Vogel 	if (more_tx || more_rx)
128561ae650dSJack F Vogel 		taskqueue_enqueue(que->tq, &que->task);
128661ae650dSJack F Vogel 	else
128761ae650dSJack F Vogel 		ixl_enable_queue(hw, que->me);
128861ae650dSJack F Vogel 
128961ae650dSJack F Vogel 	return;
129061ae650dSJack F Vogel }
129161ae650dSJack F Vogel 
129261ae650dSJack F Vogel 
129361ae650dSJack F Vogel /*********************************************************************
129461ae650dSJack F Vogel  *
129561ae650dSJack F Vogel  *  MSIX Admin Queue Interrupt Service routine
129661ae650dSJack F Vogel  *
129761ae650dSJack F Vogel  **********************************************************************/
129861ae650dSJack F Vogel static void
129961ae650dSJack F Vogel ixl_msix_adminq(void *arg)
130061ae650dSJack F Vogel {
130161ae650dSJack F Vogel 	struct ixl_pf	*pf = arg;
130261ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
130361ae650dSJack F Vogel 	u32		reg, mask;
130461ae650dSJack F Vogel 
130561ae650dSJack F Vogel 	++pf->admin_irq;
130661ae650dSJack F Vogel 
130761ae650dSJack F Vogel 	reg = rd32(hw, I40E_PFINT_ICR0);
130861ae650dSJack F Vogel 	mask = rd32(hw, I40E_PFINT_ICR0_ENA);
130961ae650dSJack F Vogel 
131061ae650dSJack F Vogel 	/* Check on the cause */
131161ae650dSJack F Vogel 	if (reg & I40E_PFINT_ICR0_ADMINQ_MASK)
131261ae650dSJack F Vogel 		mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
131361ae650dSJack F Vogel 
131461ae650dSJack F Vogel 	if (reg & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
131561ae650dSJack F Vogel 		ixl_handle_mdd_event(pf);
131661ae650dSJack F Vogel 		mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
131761ae650dSJack F Vogel 	}
131861ae650dSJack F Vogel 
131961ae650dSJack F Vogel 	if (reg & I40E_PFINT_ICR0_VFLR_MASK)
132061ae650dSJack F Vogel 		mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
132161ae650dSJack F Vogel 
132261ae650dSJack F Vogel 	reg = rd32(hw, I40E_PFINT_DYN_CTL0);
132361ae650dSJack F Vogel 	reg = reg | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
132461ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
132561ae650dSJack F Vogel 
132661ae650dSJack F Vogel 	taskqueue_enqueue(pf->tq, &pf->adminq);
132761ae650dSJack F Vogel 	return;
132861ae650dSJack F Vogel }
132961ae650dSJack F Vogel 
133061ae650dSJack F Vogel /*********************************************************************
133161ae650dSJack F Vogel  *
133261ae650dSJack F Vogel  *  Media Ioctl callback
133361ae650dSJack F Vogel  *
133461ae650dSJack F Vogel  *  This routine is called whenever the user queries the status of
133561ae650dSJack F Vogel  *  the interface using ifconfig.
133661ae650dSJack F Vogel  *
133761ae650dSJack F Vogel  **********************************************************************/
133861ae650dSJack F Vogel static void
133961ae650dSJack F Vogel ixl_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
134061ae650dSJack F Vogel {
134161ae650dSJack F Vogel 	struct ixl_vsi	*vsi = ifp->if_softc;
134261ae650dSJack F Vogel 	struct ixl_pf	*pf = (struct ixl_pf *)vsi->back;
134361ae650dSJack F Vogel 	struct i40e_hw  *hw = &pf->hw;
134461ae650dSJack F Vogel 
134561ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_media_status: begin");
134661ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
134761ae650dSJack F Vogel 
134861ae650dSJack F Vogel 	ixl_update_link_status(pf);
134961ae650dSJack F Vogel 
135061ae650dSJack F Vogel 	ifmr->ifm_status = IFM_AVALID;
135161ae650dSJack F Vogel 	ifmr->ifm_active = IFM_ETHER;
135261ae650dSJack F Vogel 
135361ae650dSJack F Vogel 	if (!vsi->link_up) {
135461ae650dSJack F Vogel 		IXL_PF_UNLOCK(pf);
135561ae650dSJack F Vogel 		return;
135661ae650dSJack F Vogel 	}
135761ae650dSJack F Vogel 
135861ae650dSJack F Vogel 	ifmr->ifm_status |= IFM_ACTIVE;
135961ae650dSJack F Vogel 	/* Hardware is always full-duplex */
136061ae650dSJack F Vogel 	ifmr->ifm_active |= IFM_FDX;
136161ae650dSJack F Vogel 
136261ae650dSJack F Vogel 	switch (hw->phy.link_info.phy_type) {
136361ae650dSJack F Vogel 		/* 100 M */
136461ae650dSJack F Vogel 		case I40E_PHY_TYPE_100BASE_TX:
136561ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_100_TX;
136661ae650dSJack F Vogel 			break;
136761ae650dSJack F Vogel 		/* 1 G */
136861ae650dSJack F Vogel 		case I40E_PHY_TYPE_1000BASE_T:
136961ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_1000_T;
137061ae650dSJack F Vogel 			break;
137161ae650dSJack F Vogel 		case I40E_PHY_TYPE_1000BASE_SX:
137261ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_1000_SX;
137361ae650dSJack F Vogel 			break;
137461ae650dSJack F Vogel 		case I40E_PHY_TYPE_1000BASE_LX:
137561ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_1000_LX;
137661ae650dSJack F Vogel 			break;
137761ae650dSJack F Vogel 		/* 10 G */
137861ae650dSJack F Vogel 		case I40E_PHY_TYPE_10GBASE_CR1_CU:
137961ae650dSJack F Vogel 		case I40E_PHY_TYPE_10GBASE_SFPP_CU:
138061ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_10G_TWINAX;
138161ae650dSJack F Vogel 			break;
138261ae650dSJack F Vogel 		case I40E_PHY_TYPE_10GBASE_SR:
138361ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_10G_SR;
138461ae650dSJack F Vogel 			break;
138561ae650dSJack F Vogel 		case I40E_PHY_TYPE_10GBASE_LR:
138661ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_10G_LR;
138761ae650dSJack F Vogel 			break;
138861ae650dSJack F Vogel 		case I40E_PHY_TYPE_10GBASE_T:
138961ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_10G_T;
139061ae650dSJack F Vogel 			break;
139161ae650dSJack F Vogel 		/* 40 G */
139261ae650dSJack F Vogel 		case I40E_PHY_TYPE_40GBASE_CR4:
139361ae650dSJack F Vogel 		case I40E_PHY_TYPE_40GBASE_CR4_CU:
139461ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_40G_CR4;
139561ae650dSJack F Vogel 			break;
139661ae650dSJack F Vogel 		case I40E_PHY_TYPE_40GBASE_SR4:
139761ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_40G_SR4;
139861ae650dSJack F Vogel 			break;
139961ae650dSJack F Vogel 		case I40E_PHY_TYPE_40GBASE_LR4:
140061ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_40G_LR4;
140161ae650dSJack F Vogel 			break;
140261ae650dSJack F Vogel 		default:
140361ae650dSJack F Vogel 			ifmr->ifm_active |= IFM_UNKNOWN;
140461ae650dSJack F Vogel 			break;
140561ae650dSJack F Vogel 	}
140661ae650dSJack F Vogel 	/* Report flow control status as well */
140761ae650dSJack F Vogel 	if (hw->phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
140861ae650dSJack F Vogel 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
140961ae650dSJack F Vogel 	if (hw->phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
141061ae650dSJack F Vogel 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
141161ae650dSJack F Vogel 
141261ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
141361ae650dSJack F Vogel 
141461ae650dSJack F Vogel 	return;
141561ae650dSJack F Vogel }
141661ae650dSJack F Vogel 
141761ae650dSJack F Vogel /*********************************************************************
141861ae650dSJack F Vogel  *
141961ae650dSJack F Vogel  *  Media Ioctl callback
142061ae650dSJack F Vogel  *
142161ae650dSJack F Vogel  *  This routine is called when the user changes speed/duplex using
142261ae650dSJack F Vogel  *  media/mediopt option with ifconfig.
142361ae650dSJack F Vogel  *
142461ae650dSJack F Vogel  **********************************************************************/
142561ae650dSJack F Vogel static int
142661ae650dSJack F Vogel ixl_media_change(struct ifnet * ifp)
142761ae650dSJack F Vogel {
142861ae650dSJack F Vogel 	struct ixl_vsi *vsi = ifp->if_softc;
142961ae650dSJack F Vogel 	struct ifmedia *ifm = &vsi->media;
143061ae650dSJack F Vogel 
143161ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_media_change: begin");
143261ae650dSJack F Vogel 
143361ae650dSJack F Vogel 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
143461ae650dSJack F Vogel 		return (EINVAL);
143561ae650dSJack F Vogel 
143661ae650dSJack F Vogel 	if_printf(ifp, "Media change is currently not supported.\n");
143761ae650dSJack F Vogel 
143861ae650dSJack F Vogel 	return (ENODEV);
143961ae650dSJack F Vogel }
144061ae650dSJack F Vogel 
144161ae650dSJack F Vogel 
144261ae650dSJack F Vogel #ifdef IXL_FDIR
144361ae650dSJack F Vogel /*
144461ae650dSJack F Vogel ** ATR: Application Targetted Receive - creates a filter
144561ae650dSJack F Vogel **	based on TX flow info that will keep the receive
144661ae650dSJack F Vogel **	portion of the flow on the same queue. Based on the
144761ae650dSJack F Vogel **	implementation this is only available for TCP connections
144861ae650dSJack F Vogel */
144961ae650dSJack F Vogel void
145061ae650dSJack F Vogel ixl_atr(struct ixl_queue *que, struct tcphdr *th, int etype)
145161ae650dSJack F Vogel {
145261ae650dSJack F Vogel 	struct ixl_vsi			*vsi = que->vsi;
145361ae650dSJack F Vogel 	struct tx_ring			*txr = &que->txr;
145461ae650dSJack F Vogel 	struct i40e_filter_program_desc	*FDIR;
145561ae650dSJack F Vogel 	u32				ptype, dtype;
145661ae650dSJack F Vogel 	int				idx;
145761ae650dSJack F Vogel 
145861ae650dSJack F Vogel 	/* check if ATR is enabled and sample rate */
145961ae650dSJack F Vogel 	if ((!ixl_enable_fdir) || (!txr->atr_rate))
146061ae650dSJack F Vogel 		return;
146161ae650dSJack F Vogel 	/*
146261ae650dSJack F Vogel 	** We sample all TCP SYN/FIN packets,
146361ae650dSJack F Vogel 	** or at the selected sample rate
146461ae650dSJack F Vogel 	*/
146561ae650dSJack F Vogel 	txr->atr_count++;
146661ae650dSJack F Vogel 	if (((th->th_flags & (TH_FIN | TH_SYN)) == 0) &&
146761ae650dSJack F Vogel 	    (txr->atr_count < txr->atr_rate))
146861ae650dSJack F Vogel                 return;
146961ae650dSJack F Vogel 	txr->atr_count = 0;
147061ae650dSJack F Vogel 
147161ae650dSJack F Vogel 	/* Get a descriptor to use */
147261ae650dSJack F Vogel 	idx = txr->next_avail;
147361ae650dSJack F Vogel 	FDIR = (struct i40e_filter_program_desc *) &txr->base[idx];
147461ae650dSJack F Vogel 	if (++idx == que->num_desc)
147561ae650dSJack F Vogel 		idx = 0;
147661ae650dSJack F Vogel 	txr->avail--;
147761ae650dSJack F Vogel 	txr->next_avail = idx;
147861ae650dSJack F Vogel 
147961ae650dSJack F Vogel 	ptype = (que->me << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
148061ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW0_QINDEX_MASK;
148161ae650dSJack F Vogel 
148261ae650dSJack F Vogel 	ptype |= (etype == ETHERTYPE_IP) ?
148361ae650dSJack F Vogel 	    (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
148461ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
148561ae650dSJack F Vogel 	    (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
148661ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
148761ae650dSJack F Vogel 
148861ae650dSJack F Vogel 	ptype |= vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
148961ae650dSJack F Vogel 
149061ae650dSJack F Vogel 	dtype = I40E_TX_DESC_DTYPE_FILTER_PROG;
149161ae650dSJack F Vogel 
149261ae650dSJack F Vogel 	/*
149361ae650dSJack F Vogel 	** We use the TCP TH_FIN as a trigger to remove
149461ae650dSJack F Vogel 	** the filter, otherwise its an update.
149561ae650dSJack F Vogel 	*/
149661ae650dSJack F Vogel 	dtype |= (th->th_flags & TH_FIN) ?
149761ae650dSJack F Vogel 	    (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
149861ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
149961ae650dSJack F Vogel 	    (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
150061ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW1_PCMD_SHIFT);
150161ae650dSJack F Vogel 
150261ae650dSJack F Vogel 	dtype |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
150361ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW1_DEST_SHIFT;
150461ae650dSJack F Vogel 
150561ae650dSJack F Vogel 	dtype |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
150661ae650dSJack F Vogel 	    I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
150761ae650dSJack F Vogel 
150861ae650dSJack F Vogel 	FDIR->qindex_flex_ptype_vsi = htole32(ptype);
150961ae650dSJack F Vogel 	FDIR->dtype_cmd_cntindex = htole32(dtype);
151061ae650dSJack F Vogel 	return;
151161ae650dSJack F Vogel }
151261ae650dSJack F Vogel #endif
151361ae650dSJack F Vogel 
151461ae650dSJack F Vogel 
151561ae650dSJack F Vogel static void
151661ae650dSJack F Vogel ixl_set_promisc(struct ixl_vsi *vsi)
151761ae650dSJack F Vogel {
151861ae650dSJack F Vogel 	struct ifnet	*ifp = vsi->ifp;
151961ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
152061ae650dSJack F Vogel 	int		err, mcnt = 0;
152161ae650dSJack F Vogel 	bool		uni = FALSE, multi = FALSE;
152261ae650dSJack F Vogel 
152361ae650dSJack F Vogel 	if (ifp->if_flags & IFF_ALLMULTI)
152461ae650dSJack F Vogel                 multi = TRUE;
152561ae650dSJack F Vogel 	else { /* Need to count the multicast addresses */
152661ae650dSJack F Vogel 		struct  ifmultiaddr *ifma;
152761ae650dSJack F Vogel 		if_maddr_rlock(ifp);
152861ae650dSJack F Vogel 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
152961ae650dSJack F Vogel                         if (ifma->ifma_addr->sa_family != AF_LINK)
153061ae650dSJack F Vogel                                 continue;
153161ae650dSJack F Vogel                         if (mcnt == MAX_MULTICAST_ADDR)
153261ae650dSJack F Vogel                                 break;
153361ae650dSJack F Vogel                         mcnt++;
153461ae650dSJack F Vogel 		}
153561ae650dSJack F Vogel 		if_maddr_runlock(ifp);
153661ae650dSJack F Vogel 	}
153761ae650dSJack F Vogel 
153861ae650dSJack F Vogel 	if (mcnt >= MAX_MULTICAST_ADDR)
153961ae650dSJack F Vogel                 multi = TRUE;
154061ae650dSJack F Vogel         if (ifp->if_flags & IFF_PROMISC)
154161ae650dSJack F Vogel 		uni = TRUE;
154261ae650dSJack F Vogel 
154361ae650dSJack F Vogel 	err = i40e_aq_set_vsi_unicast_promiscuous(hw,
154461ae650dSJack F Vogel 	    vsi->seid, uni, NULL);
154561ae650dSJack F Vogel 	err = i40e_aq_set_vsi_multicast_promiscuous(hw,
154661ae650dSJack F Vogel 	    vsi->seid, multi, NULL);
154761ae650dSJack F Vogel 	return;
154861ae650dSJack F Vogel }
154961ae650dSJack F Vogel 
155061ae650dSJack F Vogel /*********************************************************************
155161ae650dSJack F Vogel  * 	Filter Routines
155261ae650dSJack F Vogel  *
155361ae650dSJack F Vogel  *	Routines for multicast and vlan filter management.
155461ae650dSJack F Vogel  *
155561ae650dSJack F Vogel  *********************************************************************/
155661ae650dSJack F Vogel static void
155761ae650dSJack F Vogel ixl_add_multi(struct ixl_vsi *vsi)
155861ae650dSJack F Vogel {
155961ae650dSJack F Vogel 	struct	ifmultiaddr	*ifma;
156061ae650dSJack F Vogel 	struct ifnet		*ifp = vsi->ifp;
156161ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
156261ae650dSJack F Vogel 	int			mcnt = 0, flags;
156361ae650dSJack F Vogel 
156461ae650dSJack F Vogel 	IOCTL_DEBUGOUT("ixl_add_multi: begin");
156561ae650dSJack F Vogel 
156661ae650dSJack F Vogel 	if_maddr_rlock(ifp);
156761ae650dSJack F Vogel 	/*
156861ae650dSJack F Vogel 	** First just get a count, to decide if we
156961ae650dSJack F Vogel 	** we simply use multicast promiscuous.
157061ae650dSJack F Vogel 	*/
157161ae650dSJack F Vogel 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
157261ae650dSJack F Vogel 		if (ifma->ifma_addr->sa_family != AF_LINK)
157361ae650dSJack F Vogel 			continue;
157461ae650dSJack F Vogel 		mcnt++;
157561ae650dSJack F Vogel 	}
157661ae650dSJack F Vogel 	if_maddr_runlock(ifp);
157761ae650dSJack F Vogel 
157861ae650dSJack F Vogel 	if (__predict_false(mcnt >= MAX_MULTICAST_ADDR)) {
157961ae650dSJack F Vogel 		/* delete existing MC filters */
158061ae650dSJack F Vogel 		ixl_del_hw_filters(vsi, mcnt);
158161ae650dSJack F Vogel 		i40e_aq_set_vsi_multicast_promiscuous(hw,
158261ae650dSJack F Vogel 		    vsi->seid, TRUE, NULL);
158361ae650dSJack F Vogel 		return;
158461ae650dSJack F Vogel 	}
158561ae650dSJack F Vogel 
158661ae650dSJack F Vogel 	mcnt = 0;
158761ae650dSJack F Vogel 	if_maddr_rlock(ifp);
158861ae650dSJack F Vogel 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
158961ae650dSJack F Vogel 		if (ifma->ifma_addr->sa_family != AF_LINK)
159061ae650dSJack F Vogel 			continue;
159161ae650dSJack F Vogel 		ixl_add_mc_filter(vsi,
159261ae650dSJack F Vogel 		    (u8*)LLADDR((struct sockaddr_dl *) ifma->ifma_addr));
159361ae650dSJack F Vogel 		mcnt++;
159461ae650dSJack F Vogel 	}
159561ae650dSJack F Vogel 	if_maddr_runlock(ifp);
159661ae650dSJack F Vogel 	if (mcnt > 0) {
159761ae650dSJack F Vogel 		flags = (IXL_FILTER_ADD | IXL_FILTER_USED | IXL_FILTER_MC);
159861ae650dSJack F Vogel 		ixl_add_hw_filters(vsi, flags, mcnt);
159961ae650dSJack F Vogel 	}
160061ae650dSJack F Vogel 
160161ae650dSJack F Vogel 	IOCTL_DEBUGOUT("ixl_add_multi: end");
160261ae650dSJack F Vogel 	return;
160361ae650dSJack F Vogel }
160461ae650dSJack F Vogel 
160561ae650dSJack F Vogel static void
160661ae650dSJack F Vogel ixl_del_multi(struct ixl_vsi *vsi)
160761ae650dSJack F Vogel {
160861ae650dSJack F Vogel 	struct ifnet		*ifp = vsi->ifp;
160961ae650dSJack F Vogel 	struct ifmultiaddr	*ifma;
161061ae650dSJack F Vogel 	struct ixl_mac_filter	*f;
161161ae650dSJack F Vogel 	int			mcnt = 0;
161261ae650dSJack F Vogel 	bool		match = FALSE;
161361ae650dSJack F Vogel 
161461ae650dSJack F Vogel 	IOCTL_DEBUGOUT("ixl_del_multi: begin");
161561ae650dSJack F Vogel 
161661ae650dSJack F Vogel 	/* Search for removed multicast addresses */
161761ae650dSJack F Vogel 	if_maddr_rlock(ifp);
161861ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
161961ae650dSJack F Vogel 		if ((f->flags & IXL_FILTER_USED) && (f->flags & IXL_FILTER_MC)) {
162061ae650dSJack F Vogel 			match = FALSE;
162161ae650dSJack F Vogel 			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
162261ae650dSJack F Vogel 				if (ifma->ifma_addr->sa_family != AF_LINK)
162361ae650dSJack F Vogel 					continue;
162461ae650dSJack F Vogel 				u8 *mc_addr = (u8 *)LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
162561ae650dSJack F Vogel 				if (cmp_etheraddr(f->macaddr, mc_addr)) {
162661ae650dSJack F Vogel 					match = TRUE;
162761ae650dSJack F Vogel 					break;
162861ae650dSJack F Vogel 				}
162961ae650dSJack F Vogel 			}
163061ae650dSJack F Vogel 			if (match == FALSE) {
163161ae650dSJack F Vogel 				f->flags |= IXL_FILTER_DEL;
163261ae650dSJack F Vogel 				mcnt++;
163361ae650dSJack F Vogel 			}
163461ae650dSJack F Vogel 		}
163561ae650dSJack F Vogel 	}
163661ae650dSJack F Vogel 	if_maddr_runlock(ifp);
163761ae650dSJack F Vogel 
163861ae650dSJack F Vogel 	if (mcnt > 0)
163961ae650dSJack F Vogel 		ixl_del_hw_filters(vsi, mcnt);
164061ae650dSJack F Vogel }
164161ae650dSJack F Vogel 
164261ae650dSJack F Vogel 
164361ae650dSJack F Vogel /*********************************************************************
164461ae650dSJack F Vogel  *  Timer routine
164561ae650dSJack F Vogel  *
164661ae650dSJack F Vogel  *  This routine checks for link status,updates statistics,
164761ae650dSJack F Vogel  *  and runs the watchdog check.
164861ae650dSJack F Vogel  *
164961ae650dSJack F Vogel  **********************************************************************/
165061ae650dSJack F Vogel 
165161ae650dSJack F Vogel static void
165261ae650dSJack F Vogel ixl_local_timer(void *arg)
165361ae650dSJack F Vogel {
165461ae650dSJack F Vogel 	struct ixl_pf		*pf = arg;
165561ae650dSJack F Vogel 	struct i40e_hw		*hw = &pf->hw;
165661ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
165761ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
165861ae650dSJack F Vogel 	device_t		dev = pf->dev;
165961ae650dSJack F Vogel 	int			hung = 0;
166061ae650dSJack F Vogel 	u32			mask;
166161ae650dSJack F Vogel 
166261ae650dSJack F Vogel 	mtx_assert(&pf->pf_mtx, MA_OWNED);
166361ae650dSJack F Vogel 
166461ae650dSJack F Vogel 	/* Fire off the adminq task */
166561ae650dSJack F Vogel 	taskqueue_enqueue(pf->tq, &pf->adminq);
166661ae650dSJack F Vogel 
166761ae650dSJack F Vogel 	/* Update stats */
166861ae650dSJack F Vogel 	ixl_update_stats_counters(pf);
166961ae650dSJack F Vogel 
167061ae650dSJack F Vogel 	/*
167161ae650dSJack F Vogel 	** Check status of the queues
167261ae650dSJack F Vogel 	*/
167361ae650dSJack F Vogel 	mask = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
167461ae650dSJack F Vogel 		I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
167561ae650dSJack F Vogel 
167661ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++,que++) {
167761ae650dSJack F Vogel 		/* Any queues with outstanding work get a sw irq */
167861ae650dSJack F Vogel 		if (que->busy)
167961ae650dSJack F Vogel 			wr32(hw, I40E_PFINT_DYN_CTLN(que->me), mask);
168061ae650dSJack F Vogel 		/*
168161ae650dSJack F Vogel 		** Each time txeof runs without cleaning, but there
168261ae650dSJack F Vogel 		** are uncleaned descriptors it increments busy. If
168361ae650dSJack F Vogel 		** we get to 5 we declare it hung.
168461ae650dSJack F Vogel 		*/
168561ae650dSJack F Vogel 		if (que->busy == IXL_QUEUE_HUNG) {
168661ae650dSJack F Vogel 			++hung;
168761ae650dSJack F Vogel 			/* Mark the queue as inactive */
168861ae650dSJack F Vogel 			vsi->active_queues &= ~((u64)1 << que->me);
168961ae650dSJack F Vogel 			continue;
169061ae650dSJack F Vogel 		} else {
169161ae650dSJack F Vogel 			/* Check if we've come back from hung */
169261ae650dSJack F Vogel 			if ((vsi->active_queues & ((u64)1 << que->me)) == 0)
169361ae650dSJack F Vogel 				vsi->active_queues |= ((u64)1 << que->me);
169461ae650dSJack F Vogel 		}
169561ae650dSJack F Vogel 		if (que->busy >= IXL_MAX_TX_BUSY) {
169661ae650dSJack F Vogel 			device_printf(dev,"Warning queue %d "
169761ae650dSJack F Vogel 			    "appears to be hung!\n", i);
169861ae650dSJack F Vogel 			que->busy = IXL_QUEUE_HUNG;
169961ae650dSJack F Vogel 			++hung;
170061ae650dSJack F Vogel 		}
170161ae650dSJack F Vogel 	}
170261ae650dSJack F Vogel 	/* Only reinit if all queues show hung */
170361ae650dSJack F Vogel 	if (hung == vsi->num_queues)
170461ae650dSJack F Vogel 		goto hung;
170561ae650dSJack F Vogel 
170661ae650dSJack F Vogel 	callout_reset(&pf->timer, hz, ixl_local_timer, pf);
170761ae650dSJack F Vogel 	return;
170861ae650dSJack F Vogel 
170961ae650dSJack F Vogel hung:
171061ae650dSJack F Vogel 	device_printf(dev, "Local Timer: HANG DETECT - Resetting!!\n");
171161ae650dSJack F Vogel 	ixl_init_locked(pf);
171261ae650dSJack F Vogel }
171361ae650dSJack F Vogel 
171461ae650dSJack F Vogel /*
171561ae650dSJack F Vogel ** Note: this routine updates the OS on the link state
171661ae650dSJack F Vogel **	the real check of the hardware only happens with
171761ae650dSJack F Vogel **	a link interrupt.
171861ae650dSJack F Vogel */
171961ae650dSJack F Vogel static void
172061ae650dSJack F Vogel ixl_update_link_status(struct ixl_pf *pf)
172161ae650dSJack F Vogel {
172261ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
172361ae650dSJack F Vogel 	struct i40e_hw		*hw = &pf->hw;
172461ae650dSJack F Vogel 	struct ifnet		*ifp = vsi->ifp;
172561ae650dSJack F Vogel 	device_t		dev = pf->dev;
172661ae650dSJack F Vogel 	enum i40e_fc_mode 	fc;
172761ae650dSJack F Vogel 
172861ae650dSJack F Vogel 
172961ae650dSJack F Vogel 	if (vsi->link_up){
173061ae650dSJack F Vogel 		if (vsi->link_active == FALSE) {
173161ae650dSJack F Vogel 			i40e_aq_get_link_info(hw, TRUE, NULL, NULL);
173261ae650dSJack F Vogel 			if (bootverbose) {
173361ae650dSJack F Vogel 				fc = hw->fc.current_mode;
173461ae650dSJack F Vogel 				device_printf(dev,"Link is up %d Gbps %s,"
173561ae650dSJack F Vogel 				    " Flow Control: %s\n",
173661ae650dSJack F Vogel 				    ((vsi->link_speed == I40E_LINK_SPEED_40GB)? 40:10),
173761ae650dSJack F Vogel 				    "Full Duplex", ixl_fc_string[fc]);
173861ae650dSJack F Vogel 			}
173961ae650dSJack F Vogel 			vsi->link_active = TRUE;
174061ae650dSJack F Vogel 			if_link_state_change(ifp, LINK_STATE_UP);
174161ae650dSJack F Vogel 		}
174261ae650dSJack F Vogel 	} else { /* Link down */
174361ae650dSJack F Vogel 		if (vsi->link_active == TRUE) {
174461ae650dSJack F Vogel 			if (bootverbose)
174561ae650dSJack F Vogel 				device_printf(dev,"Link is Down\n");
174661ae650dSJack F Vogel 			if_link_state_change(ifp, LINK_STATE_DOWN);
174761ae650dSJack F Vogel 			vsi->link_active = FALSE;
174861ae650dSJack F Vogel 		}
174961ae650dSJack F Vogel 	}
175061ae650dSJack F Vogel 
175161ae650dSJack F Vogel 	return;
175261ae650dSJack F Vogel }
175361ae650dSJack F Vogel 
175461ae650dSJack F Vogel /*********************************************************************
175561ae650dSJack F Vogel  *
175661ae650dSJack F Vogel  *  This routine disables all traffic on the adapter by issuing a
175761ae650dSJack F Vogel  *  global reset on the MAC and deallocates TX/RX buffers.
175861ae650dSJack F Vogel  *
175961ae650dSJack F Vogel  **********************************************************************/
176061ae650dSJack F Vogel 
176161ae650dSJack F Vogel static void
176261ae650dSJack F Vogel ixl_stop(struct ixl_pf *pf)
176361ae650dSJack F Vogel {
176461ae650dSJack F Vogel 	struct ixl_vsi	*vsi = &pf->vsi;
176561ae650dSJack F Vogel 	struct ifnet	*ifp = vsi->ifp;
176661ae650dSJack F Vogel 
176761ae650dSJack F Vogel 	mtx_assert(&pf->pf_mtx, MA_OWNED);
176861ae650dSJack F Vogel 
176961ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_stop: begin\n");
177061ae650dSJack F Vogel 	ixl_disable_intr(vsi);
177161ae650dSJack F Vogel 	ixl_disable_rings(vsi);
177261ae650dSJack F Vogel 
177361ae650dSJack F Vogel 	/* Tell the stack that the interface is no longer active */
177461ae650dSJack F Vogel 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
177561ae650dSJack F Vogel 
177661ae650dSJack F Vogel 	/* Stop the local timer */
177761ae650dSJack F Vogel 	callout_stop(&pf->timer);
177861ae650dSJack F Vogel 
177961ae650dSJack F Vogel 	return;
178061ae650dSJack F Vogel }
178161ae650dSJack F Vogel 
178261ae650dSJack F Vogel 
178361ae650dSJack F Vogel /*********************************************************************
178461ae650dSJack F Vogel  *
178561ae650dSJack F Vogel  *  Setup MSIX Interrupt resources and handlers for the VSI
178661ae650dSJack F Vogel  *
178761ae650dSJack F Vogel  **********************************************************************/
178861ae650dSJack F Vogel static int
178961ae650dSJack F Vogel ixl_assign_vsi_legacy(struct ixl_pf *pf)
179061ae650dSJack F Vogel {
179161ae650dSJack F Vogel 	device_t        dev = pf->dev;
179261ae650dSJack F Vogel 	struct 		ixl_vsi *vsi = &pf->vsi;
179361ae650dSJack F Vogel 	struct		ixl_queue *que = vsi->queues;
179461ae650dSJack F Vogel 	int 		error, rid = 0;
179561ae650dSJack F Vogel 
179661ae650dSJack F Vogel 	if (pf->msix == 1)
179761ae650dSJack F Vogel 		rid = 1;
179861ae650dSJack F Vogel 	pf->res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
179961ae650dSJack F Vogel 	    &rid, RF_SHAREABLE | RF_ACTIVE);
180061ae650dSJack F Vogel 	if (pf->res == NULL) {
180161ae650dSJack F Vogel 		device_printf(dev,"Unable to allocate"
180261ae650dSJack F Vogel 		    " bus resource: vsi legacy/msi interrupt\n");
180361ae650dSJack F Vogel 		return (ENXIO);
180461ae650dSJack F Vogel 	}
180561ae650dSJack F Vogel 
180661ae650dSJack F Vogel 	/* Set the handler function */
180761ae650dSJack F Vogel 	error = bus_setup_intr(dev, pf->res,
180861ae650dSJack F Vogel 	    INTR_TYPE_NET | INTR_MPSAFE, NULL,
180961ae650dSJack F Vogel 	    ixl_intr, pf, &pf->tag);
181061ae650dSJack F Vogel 	if (error) {
181161ae650dSJack F Vogel 		pf->res = NULL;
181261ae650dSJack F Vogel 		device_printf(dev, "Failed to register legacy/msi handler");
181361ae650dSJack F Vogel 		return (error);
181461ae650dSJack F Vogel 	}
181561ae650dSJack F Vogel 	bus_describe_intr(dev, pf->res, pf->tag, "irq0");
181661ae650dSJack F Vogel 	TASK_INIT(&que->tx_task, 0, ixl_deferred_mq_start, que);
181761ae650dSJack F Vogel 	TASK_INIT(&que->task, 0, ixl_handle_que, que);
181861ae650dSJack F Vogel 	que->tq = taskqueue_create_fast("ixl_que", M_NOWAIT,
181961ae650dSJack F Vogel 	    taskqueue_thread_enqueue, &que->tq);
182061ae650dSJack F Vogel 	taskqueue_start_threads(&que->tq, 1, PI_NET, "%s que",
182161ae650dSJack F Vogel 	    device_get_nameunit(dev));
182261ae650dSJack F Vogel 	TASK_INIT(&pf->adminq, 0, ixl_do_adminq, pf);
182361ae650dSJack F Vogel 	pf->tq = taskqueue_create_fast("ixl_adm", M_NOWAIT,
182461ae650dSJack F Vogel 	    taskqueue_thread_enqueue, &pf->tq);
182561ae650dSJack F Vogel 	taskqueue_start_threads(&pf->tq, 1, PI_NET, "%s adminq",
182661ae650dSJack F Vogel 	    device_get_nameunit(dev));
182761ae650dSJack F Vogel 
182861ae650dSJack F Vogel 	return (0);
182961ae650dSJack F Vogel }
183061ae650dSJack F Vogel 
183161ae650dSJack F Vogel 
183261ae650dSJack F Vogel /*********************************************************************
183361ae650dSJack F Vogel  *
183461ae650dSJack F Vogel  *  Setup MSIX Interrupt resources and handlers for the VSI
183561ae650dSJack F Vogel  *
183661ae650dSJack F Vogel  **********************************************************************/
183761ae650dSJack F Vogel static int
183861ae650dSJack F Vogel ixl_assign_vsi_msix(struct ixl_pf *pf)
183961ae650dSJack F Vogel {
184061ae650dSJack F Vogel 	device_t	dev = pf->dev;
184161ae650dSJack F Vogel 	struct 		ixl_vsi *vsi = &pf->vsi;
184261ae650dSJack F Vogel 	struct 		ixl_queue *que = vsi->queues;
184361ae650dSJack F Vogel 	struct		tx_ring	 *txr;
184461ae650dSJack F Vogel 	int 		error, rid, vector = 0;
184561ae650dSJack F Vogel 
184661ae650dSJack F Vogel 	/* Admin Que is vector 0*/
184761ae650dSJack F Vogel 	rid = vector + 1;
184861ae650dSJack F Vogel 	pf->res = bus_alloc_resource_any(dev,
184961ae650dSJack F Vogel     	    SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE);
185061ae650dSJack F Vogel 	if (!pf->res) {
185161ae650dSJack F Vogel 		device_printf(dev,"Unable to allocate"
185261ae650dSJack F Vogel     	    " bus resource: Adminq interrupt [%d]\n", rid);
185361ae650dSJack F Vogel 		return (ENXIO);
185461ae650dSJack F Vogel 	}
185561ae650dSJack F Vogel 	/* Set the adminq vector and handler */
185661ae650dSJack F Vogel 	error = bus_setup_intr(dev, pf->res,
185761ae650dSJack F Vogel 	    INTR_TYPE_NET | INTR_MPSAFE, NULL,
185861ae650dSJack F Vogel 	    ixl_msix_adminq, pf, &pf->tag);
185961ae650dSJack F Vogel 	if (error) {
186061ae650dSJack F Vogel 		pf->res = NULL;
186161ae650dSJack F Vogel 		device_printf(dev, "Failed to register Admin que handler");
186261ae650dSJack F Vogel 		return (error);
186361ae650dSJack F Vogel 	}
186461ae650dSJack F Vogel 	bus_describe_intr(dev, pf->res, pf->tag, "aq");
186561ae650dSJack F Vogel 	pf->admvec = vector;
186661ae650dSJack F Vogel 	/* Tasklet for Admin Queue */
186761ae650dSJack F Vogel 	TASK_INIT(&pf->adminq, 0, ixl_do_adminq, pf);
186861ae650dSJack F Vogel 	pf->tq = taskqueue_create_fast("ixl_adm", M_NOWAIT,
186961ae650dSJack F Vogel 	    taskqueue_thread_enqueue, &pf->tq);
187061ae650dSJack F Vogel 	taskqueue_start_threads(&pf->tq, 1, PI_NET, "%s adminq",
187161ae650dSJack F Vogel 	    device_get_nameunit(pf->dev));
187261ae650dSJack F Vogel 	++vector;
187361ae650dSJack F Vogel 
187461ae650dSJack F Vogel 	/* Now set up the stations */
187561ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, vector++, que++) {
187661ae650dSJack F Vogel 		rid = vector + 1;
187761ae650dSJack F Vogel 		txr = &que->txr;
187861ae650dSJack F Vogel 		que->res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
187961ae650dSJack F Vogel 		    RF_SHAREABLE | RF_ACTIVE);
188061ae650dSJack F Vogel 		if (que->res == NULL) {
188161ae650dSJack F Vogel 			device_printf(dev,"Unable to allocate"
188261ae650dSJack F Vogel 		    	    " bus resource: que interrupt [%d]\n", vector);
188361ae650dSJack F Vogel 			return (ENXIO);
188461ae650dSJack F Vogel 		}
188561ae650dSJack F Vogel 		/* Set the handler function */
188661ae650dSJack F Vogel 		error = bus_setup_intr(dev, que->res,
188761ae650dSJack F Vogel 		    INTR_TYPE_NET | INTR_MPSAFE, NULL,
188861ae650dSJack F Vogel 		    ixl_msix_que, que, &que->tag);
188961ae650dSJack F Vogel 		if (error) {
189061ae650dSJack F Vogel 			que->res = NULL;
189161ae650dSJack F Vogel 			device_printf(dev, "Failed to register que handler");
189261ae650dSJack F Vogel 			return (error);
189361ae650dSJack F Vogel 		}
189461ae650dSJack F Vogel 		bus_describe_intr(dev, que->res, que->tag, "q%d", i);
189561ae650dSJack F Vogel 		/* Bind the vector to a CPU */
189661ae650dSJack F Vogel 		bus_bind_intr(dev, que->res, i);
189761ae650dSJack F Vogel 		que->msix = vector;
189861ae650dSJack F Vogel 		TASK_INIT(&que->tx_task, 0, ixl_deferred_mq_start, que);
189961ae650dSJack F Vogel 		TASK_INIT(&que->task, 0, ixl_handle_que, que);
190061ae650dSJack F Vogel 		que->tq = taskqueue_create_fast("ixl_que", M_NOWAIT,
190161ae650dSJack F Vogel 		    taskqueue_thread_enqueue, &que->tq);
190261ae650dSJack F Vogel 		taskqueue_start_threads(&que->tq, 1, PI_NET, "%s que",
190361ae650dSJack F Vogel 		    device_get_nameunit(pf->dev));
190461ae650dSJack F Vogel 	}
190561ae650dSJack F Vogel 
190661ae650dSJack F Vogel 	return (0);
190761ae650dSJack F Vogel }
190861ae650dSJack F Vogel 
190961ae650dSJack F Vogel 
191061ae650dSJack F Vogel /*
191161ae650dSJack F Vogel  * Allocate MSI/X vectors
191261ae650dSJack F Vogel  */
191361ae650dSJack F Vogel static int
191461ae650dSJack F Vogel ixl_init_msix(struct ixl_pf *pf)
191561ae650dSJack F Vogel {
191661ae650dSJack F Vogel 	device_t dev = pf->dev;
191761ae650dSJack F Vogel 	int rid, want, vectors, queues, available;
191861ae650dSJack F Vogel 
191961ae650dSJack F Vogel 	/* Override by tuneable */
192061ae650dSJack F Vogel 	if (ixl_enable_msix == 0)
192161ae650dSJack F Vogel 		goto msi;
192261ae650dSJack F Vogel 
192361ae650dSJack F Vogel 	/*
192461ae650dSJack F Vogel 	** When used in a virtualized environment
192561ae650dSJack F Vogel 	** PCI BUSMASTER capability may not be set
192661ae650dSJack F Vogel 	** so explicity set it here and rewrite
192761ae650dSJack F Vogel 	** the ENABLE in the MSIX control register
192861ae650dSJack F Vogel 	** at this point to cause the host to
192961ae650dSJack F Vogel 	** successfully initialize us.
193061ae650dSJack F Vogel 	*/
193161ae650dSJack F Vogel 	{
193261ae650dSJack F Vogel 		u16 pci_cmd_word;
193361ae650dSJack F Vogel 		int msix_ctrl;
193461ae650dSJack F Vogel 		pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2);
193561ae650dSJack F Vogel 		pci_cmd_word |= PCIM_CMD_BUSMASTEREN;
193661ae650dSJack F Vogel 		pci_write_config(dev, PCIR_COMMAND, pci_cmd_word, 2);
193761ae650dSJack F Vogel 		pci_find_cap(dev, PCIY_MSIX, &rid);
193861ae650dSJack F Vogel 		rid += PCIR_MSIX_CTRL;
193961ae650dSJack F Vogel 		msix_ctrl = pci_read_config(dev, rid, 2);
194061ae650dSJack F Vogel 		msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
194161ae650dSJack F Vogel 		pci_write_config(dev, rid, msix_ctrl, 2);
194261ae650dSJack F Vogel 	}
194361ae650dSJack F Vogel 
194461ae650dSJack F Vogel 	/* First try MSI/X */
194561ae650dSJack F Vogel 	rid = PCIR_BAR(IXL_BAR);
194661ae650dSJack F Vogel 	pf->msix_mem = bus_alloc_resource_any(dev,
194761ae650dSJack F Vogel 	    SYS_RES_MEMORY, &rid, RF_ACTIVE);
194861ae650dSJack F Vogel        	if (!pf->msix_mem) {
194961ae650dSJack F Vogel 		/* May not be enabled */
195061ae650dSJack F Vogel 		device_printf(pf->dev,
195161ae650dSJack F Vogel 		    "Unable to map MSIX table \n");
195261ae650dSJack F Vogel 		goto msi;
195361ae650dSJack F Vogel 	}
195461ae650dSJack F Vogel 
195561ae650dSJack F Vogel 	available = pci_msix_count(dev);
195661ae650dSJack F Vogel 	if (available == 0) { /* system has msix disabled */
195761ae650dSJack F Vogel 		bus_release_resource(dev, SYS_RES_MEMORY,
195861ae650dSJack F Vogel 		    rid, pf->msix_mem);
195961ae650dSJack F Vogel 		pf->msix_mem = NULL;
196061ae650dSJack F Vogel 		goto msi;
196161ae650dSJack F Vogel 	}
196261ae650dSJack F Vogel 
196361ae650dSJack F Vogel 	/* Figure out a reasonable auto config value */
196461ae650dSJack F Vogel 	queues = (mp_ncpus > (available - 1)) ? (available - 1) : mp_ncpus;
196561ae650dSJack F Vogel 
196661ae650dSJack F Vogel 	/* Override with hardcoded value if sane */
196761ae650dSJack F Vogel 	if ((ixl_max_queues != 0) && (ixl_max_queues <= queues))
196861ae650dSJack F Vogel 		queues = ixl_max_queues;
196961ae650dSJack F Vogel 
197061ae650dSJack F Vogel 	/*
197161ae650dSJack F Vogel 	** Want one vector (RX/TX pair) per queue
197261ae650dSJack F Vogel 	** plus an additional for the admin queue.
197361ae650dSJack F Vogel 	*/
197461ae650dSJack F Vogel 	want = queues + 1;
197561ae650dSJack F Vogel 	if (want <= available)	/* Have enough */
197661ae650dSJack F Vogel 		vectors = want;
197761ae650dSJack F Vogel 	else {
197861ae650dSJack F Vogel                	device_printf(pf->dev,
197961ae650dSJack F Vogel 		    "MSIX Configuration Problem, "
198061ae650dSJack F Vogel 		    "%d vectors available but %d wanted!\n",
198161ae650dSJack F Vogel 		    available, want);
198261ae650dSJack F Vogel 		return (0); /* Will go to Legacy setup */
198361ae650dSJack F Vogel 	}
198461ae650dSJack F Vogel 
198561ae650dSJack F Vogel 	if (pci_alloc_msix(dev, &vectors) == 0) {
198661ae650dSJack F Vogel                	device_printf(pf->dev,
198761ae650dSJack F Vogel 		    "Using MSIX interrupts with %d vectors\n", vectors);
198861ae650dSJack F Vogel 		pf->msix = vectors;
198961ae650dSJack F Vogel 		pf->vsi.num_queues = queues;
199061ae650dSJack F Vogel 		return (vectors);
199161ae650dSJack F Vogel 	}
199261ae650dSJack F Vogel msi:
199361ae650dSJack F Vogel        	vectors = pci_msi_count(dev);
199461ae650dSJack F Vogel 	pf->vsi.num_queues = 1;
199561ae650dSJack F Vogel 	pf->msix = 1;
199661ae650dSJack F Vogel 	ixl_max_queues = 1;
199761ae650dSJack F Vogel 	ixl_enable_msix = 0;
199861ae650dSJack F Vogel        	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0)
199961ae650dSJack F Vogel                	device_printf(pf->dev,"Using an MSI interrupt\n");
200061ae650dSJack F Vogel 	else {
200161ae650dSJack F Vogel 		pf->msix = 0;
200261ae650dSJack F Vogel                	device_printf(pf->dev,"Using a Legacy interrupt\n");
200361ae650dSJack F Vogel 	}
200461ae650dSJack F Vogel 	return (vectors);
200561ae650dSJack F Vogel }
200661ae650dSJack F Vogel 
200761ae650dSJack F Vogel 
200861ae650dSJack F Vogel /*
200961ae650dSJack F Vogel  * Plumb MSI/X vectors
201061ae650dSJack F Vogel  */
201161ae650dSJack F Vogel static void
201261ae650dSJack F Vogel ixl_configure_msix(struct ixl_pf *pf)
201361ae650dSJack F Vogel {
201461ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
201561ae650dSJack F Vogel 	struct ixl_vsi *vsi = &pf->vsi;
201661ae650dSJack F Vogel 	u32		reg;
201761ae650dSJack F Vogel 	u16		vector = 1;
201861ae650dSJack F Vogel 
201961ae650dSJack F Vogel 	/* First set up the adminq - vector 0 */
202061ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
202161ae650dSJack F Vogel 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
202261ae650dSJack F Vogel 
202361ae650dSJack F Vogel 	reg = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
202461ae650dSJack F Vogel 	    I40E_PFINT_ICR0_ENA_GRST_MASK |
202561ae650dSJack F Vogel 	    I40E_PFINT_ICR0_HMC_ERR_MASK |
202661ae650dSJack F Vogel 	    I40E_PFINT_ICR0_ENA_ADMINQ_MASK |
202761ae650dSJack F Vogel 	    I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
202861ae650dSJack F Vogel 	    I40E_PFINT_ICR0_ENA_VFLR_MASK |
202961ae650dSJack F Vogel 	    I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK;
203061ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
203161ae650dSJack F Vogel 
203261ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_LNKLST0, 0x7FF);
203361ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ITR0(IXL_RX_ITR), 0x003E);
203461ae650dSJack F Vogel 
203561ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0,
203661ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
203761ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
203861ae650dSJack F Vogel 
203961ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
204061ae650dSJack F Vogel 
204161ae650dSJack F Vogel 	/* Next configure the queues */
204261ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, vector++) {
204361ae650dSJack F Vogel 		wr32(hw, I40E_PFINT_DYN_CTLN(i), i);
204461ae650dSJack F Vogel 		wr32(hw, I40E_PFINT_LNKLSTN(i), i);
204561ae650dSJack F Vogel 
204661ae650dSJack F Vogel 		reg = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
204761ae650dSJack F Vogel 		(IXL_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
204861ae650dSJack F Vogel 		(vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
204961ae650dSJack F Vogel 		(i << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
205061ae650dSJack F Vogel 		(I40E_QUEUE_TYPE_TX << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
205161ae650dSJack F Vogel 		wr32(hw, I40E_QINT_RQCTL(i), reg);
205261ae650dSJack F Vogel 
205361ae650dSJack F Vogel 		reg = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
205461ae650dSJack F Vogel 		(IXL_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
205561ae650dSJack F Vogel 		(vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
205661ae650dSJack F Vogel 		((i+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
205761ae650dSJack F Vogel 		(I40E_QUEUE_TYPE_RX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
205861ae650dSJack F Vogel 		if (i == (vsi->num_queues - 1))
205961ae650dSJack F Vogel 			reg |= (IXL_QUEUE_EOL
206061ae650dSJack F Vogel 			    << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
206161ae650dSJack F Vogel 		wr32(hw, I40E_QINT_TQCTL(i), reg);
206261ae650dSJack F Vogel 	}
206361ae650dSJack F Vogel }
206461ae650dSJack F Vogel 
206561ae650dSJack F Vogel /*
206661ae650dSJack F Vogel  * Configure for MSI single vector operation
206761ae650dSJack F Vogel  */
206861ae650dSJack F Vogel static void
206961ae650dSJack F Vogel ixl_configure_legacy(struct ixl_pf *pf)
207061ae650dSJack F Vogel {
207161ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
207261ae650dSJack F Vogel 	u32		reg;
207361ae650dSJack F Vogel 
207461ae650dSJack F Vogel 
207561ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ITR0(0), 0);
207661ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ITR0(1), 0);
207761ae650dSJack F Vogel 
207861ae650dSJack F Vogel 
207961ae650dSJack F Vogel 	/* Setup "other" causes */
208061ae650dSJack F Vogel 	reg = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK
208161ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK
208261ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_GRST_MASK
208361ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK
208461ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_GPIO_MASK
208561ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_LINK_STAT_CHANGE_MASK
208661ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_HMC_ERR_MASK
208761ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK
208861ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_VFLR_MASK
208961ae650dSJack F Vogel 	    | I40E_PFINT_ICR0_ENA_ADMINQ_MASK
209061ae650dSJack F Vogel 	    ;
209161ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
209261ae650dSJack F Vogel 
209361ae650dSJack F Vogel 	/* SW_ITR_IDX = 0, but don't change INTENA */
209461ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0,
209561ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTLN_SW_ITR_INDX_MASK |
209661ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTLN_INTENA_MSK_MASK);
209761ae650dSJack F Vogel 	/* SW_ITR_IDX = 0, OTHER_ITR_IDX = 0 */
209861ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
209961ae650dSJack F Vogel 
210061ae650dSJack F Vogel 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
210161ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_LNKLST0, 0);
210261ae650dSJack F Vogel 
210361ae650dSJack F Vogel 	/* Associate the queue pair to the vector and enable the q int */
210461ae650dSJack F Vogel 	reg = I40E_QINT_RQCTL_CAUSE_ENA_MASK
210561ae650dSJack F Vogel 	    | (IXL_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)
210661ae650dSJack F Vogel 	    | (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
210761ae650dSJack F Vogel 	wr32(hw, I40E_QINT_RQCTL(0), reg);
210861ae650dSJack F Vogel 
210961ae650dSJack F Vogel 	reg = I40E_QINT_TQCTL_CAUSE_ENA_MASK
211061ae650dSJack F Vogel 	    | (IXL_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)
211161ae650dSJack F Vogel 	    | (IXL_QUEUE_EOL << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
211261ae650dSJack F Vogel 	wr32(hw, I40E_QINT_TQCTL(0), reg);
211361ae650dSJack F Vogel 
211461ae650dSJack F Vogel 	/* Next enable the queue pair */
211561ae650dSJack F Vogel 	reg = rd32(hw, I40E_QTX_ENA(0));
211661ae650dSJack F Vogel 	reg |= I40E_QTX_ENA_QENA_REQ_MASK;
211761ae650dSJack F Vogel 	wr32(hw, I40E_QTX_ENA(0), reg);
211861ae650dSJack F Vogel 
211961ae650dSJack F Vogel 	reg = rd32(hw, I40E_QRX_ENA(0));
212061ae650dSJack F Vogel 	reg |= I40E_QRX_ENA_QENA_REQ_MASK;
212161ae650dSJack F Vogel 	wr32(hw, I40E_QRX_ENA(0), reg);
212261ae650dSJack F Vogel }
212361ae650dSJack F Vogel 
212461ae650dSJack F Vogel 
212561ae650dSJack F Vogel /*
212661ae650dSJack F Vogel  * Set the Initial ITR state
212761ae650dSJack F Vogel  */
212861ae650dSJack F Vogel static void
212961ae650dSJack F Vogel ixl_configure_itr(struct ixl_pf *pf)
213061ae650dSJack F Vogel {
213161ae650dSJack F Vogel 	struct i40e_hw		*hw = &pf->hw;
213261ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
213361ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
213461ae650dSJack F Vogel 
213561ae650dSJack F Vogel 	vsi->rx_itr_setting = ixl_rx_itr;
213661ae650dSJack F Vogel 	if (ixl_dynamic_rx_itr)
213761ae650dSJack F Vogel 		vsi->rx_itr_setting |= IXL_ITR_DYNAMIC;
213861ae650dSJack F Vogel 	vsi->tx_itr_setting = ixl_tx_itr;
213961ae650dSJack F Vogel 	if (ixl_dynamic_tx_itr)
214061ae650dSJack F Vogel 		vsi->tx_itr_setting |= IXL_ITR_DYNAMIC;
214161ae650dSJack F Vogel 
214261ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, que++) {
214361ae650dSJack F Vogel 		struct tx_ring	*txr = &que->txr;
214461ae650dSJack F Vogel 		struct rx_ring 	*rxr = &que->rxr;
214561ae650dSJack F Vogel 
214661ae650dSJack F Vogel 		wr32(hw, I40E_PFINT_ITRN(IXL_RX_ITR, i),
214761ae650dSJack F Vogel 		    vsi->rx_itr_setting);
214861ae650dSJack F Vogel 		rxr->itr = vsi->rx_itr_setting;
214961ae650dSJack F Vogel 		rxr->latency = IXL_AVE_LATENCY;
215061ae650dSJack F Vogel 		wr32(hw, I40E_PFINT_ITRN(IXL_TX_ITR, i),
215161ae650dSJack F Vogel 		    vsi->tx_itr_setting);
215261ae650dSJack F Vogel 		txr->itr = vsi->tx_itr_setting;
215361ae650dSJack F Vogel 		txr->latency = IXL_AVE_LATENCY;
215461ae650dSJack F Vogel 	}
215561ae650dSJack F Vogel }
215661ae650dSJack F Vogel 
215761ae650dSJack F Vogel 
215861ae650dSJack F Vogel static int
215961ae650dSJack F Vogel ixl_allocate_pci_resources(struct ixl_pf *pf)
216061ae650dSJack F Vogel {
216161ae650dSJack F Vogel 	int             rid;
216261ae650dSJack F Vogel 	device_t        dev = pf->dev;
216361ae650dSJack F Vogel 
216461ae650dSJack F Vogel 	rid = PCIR_BAR(0);
216561ae650dSJack F Vogel 	pf->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
216661ae650dSJack F Vogel 	    &rid, RF_ACTIVE);
216761ae650dSJack F Vogel 
216861ae650dSJack F Vogel 	if (!(pf->pci_mem)) {
216961ae650dSJack F Vogel 		device_printf(dev,"Unable to allocate bus resource: memory\n");
217061ae650dSJack F Vogel 		return (ENXIO);
217161ae650dSJack F Vogel 	}
217261ae650dSJack F Vogel 
217361ae650dSJack F Vogel 	pf->osdep.mem_bus_space_tag =
217461ae650dSJack F Vogel 		rman_get_bustag(pf->pci_mem);
217561ae650dSJack F Vogel 	pf->osdep.mem_bus_space_handle =
217661ae650dSJack F Vogel 		rman_get_bushandle(pf->pci_mem);
217761ae650dSJack F Vogel 	pf->osdep.mem_bus_space_size = rman_get_size(pf->pci_mem);
217861ae650dSJack F Vogel 	pf->hw.hw_addr = (u8 *) &pf->osdep.mem_bus_space_handle;
217961ae650dSJack F Vogel 
218061ae650dSJack F Vogel 	pf->hw.back = &pf->osdep;
218161ae650dSJack F Vogel 
218261ae650dSJack F Vogel 	/*
218361ae650dSJack F Vogel 	** Now setup MSI or MSI/X, should
218461ae650dSJack F Vogel 	** return us the number of supported
218561ae650dSJack F Vogel 	** vectors. (Will be 1 for MSI)
218661ae650dSJack F Vogel 	*/
218761ae650dSJack F Vogel 	pf->msix = ixl_init_msix(pf);
218861ae650dSJack F Vogel 	return (0);
218961ae650dSJack F Vogel }
219061ae650dSJack F Vogel 
219161ae650dSJack F Vogel static void
219261ae650dSJack F Vogel ixl_free_pci_resources(struct ixl_pf * pf)
219361ae650dSJack F Vogel {
219461ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
219561ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
219661ae650dSJack F Vogel 	device_t		dev = pf->dev;
219761ae650dSJack F Vogel 	int			rid, memrid;
219861ae650dSJack F Vogel 
219961ae650dSJack F Vogel 	memrid = PCIR_BAR(IXL_BAR);
220061ae650dSJack F Vogel 
220161ae650dSJack F Vogel 	/* We may get here before stations are setup */
220261ae650dSJack F Vogel 	if ((!ixl_enable_msix) || (que == NULL))
220361ae650dSJack F Vogel 		goto early;
220461ae650dSJack F Vogel 
220561ae650dSJack F Vogel 	/*
220661ae650dSJack F Vogel 	**  Release all msix VSI resources:
220761ae650dSJack F Vogel 	*/
220861ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, que++) {
220961ae650dSJack F Vogel 		rid = que->msix + 1;
221061ae650dSJack F Vogel 		if (que->tag != NULL) {
221161ae650dSJack F Vogel 			bus_teardown_intr(dev, que->res, que->tag);
221261ae650dSJack F Vogel 			que->tag = NULL;
221361ae650dSJack F Vogel 		}
221461ae650dSJack F Vogel 		if (que->res != NULL)
221561ae650dSJack F Vogel 			bus_release_resource(dev, SYS_RES_IRQ, rid, que->res);
221661ae650dSJack F Vogel 	}
221761ae650dSJack F Vogel 
221861ae650dSJack F Vogel early:
221961ae650dSJack F Vogel 	/* Clean the AdminQ interrupt last */
222061ae650dSJack F Vogel 	if (pf->admvec) /* we are doing MSIX */
222161ae650dSJack F Vogel 		rid = pf->admvec + 1;
222261ae650dSJack F Vogel 	else
222361ae650dSJack F Vogel 		(pf->msix != 0) ? (rid = 1):(rid = 0);
222461ae650dSJack F Vogel 
222561ae650dSJack F Vogel 	if (pf->tag != NULL) {
222661ae650dSJack F Vogel 		bus_teardown_intr(dev, pf->res, pf->tag);
222761ae650dSJack F Vogel 		pf->tag = NULL;
222861ae650dSJack F Vogel 	}
222961ae650dSJack F Vogel 	if (pf->res != NULL)
223061ae650dSJack F Vogel 		bus_release_resource(dev, SYS_RES_IRQ, rid, pf->res);
223161ae650dSJack F Vogel 
223261ae650dSJack F Vogel 	if (pf->msix)
223361ae650dSJack F Vogel 		pci_release_msi(dev);
223461ae650dSJack F Vogel 
223561ae650dSJack F Vogel 	if (pf->msix_mem != NULL)
223661ae650dSJack F Vogel 		bus_release_resource(dev, SYS_RES_MEMORY,
223761ae650dSJack F Vogel 		    memrid, pf->msix_mem);
223861ae650dSJack F Vogel 
223961ae650dSJack F Vogel 	if (pf->pci_mem != NULL)
224061ae650dSJack F Vogel 		bus_release_resource(dev, SYS_RES_MEMORY,
224161ae650dSJack F Vogel 		    PCIR_BAR(0), pf->pci_mem);
224261ae650dSJack F Vogel 
224361ae650dSJack F Vogel 	return;
224461ae650dSJack F Vogel }
224561ae650dSJack F Vogel 
224661ae650dSJack F Vogel 
224761ae650dSJack F Vogel /*********************************************************************
224861ae650dSJack F Vogel  *
224961ae650dSJack F Vogel  *  Setup networking device structure and register an interface.
225061ae650dSJack F Vogel  *
225161ae650dSJack F Vogel  **********************************************************************/
225261ae650dSJack F Vogel static int
225361ae650dSJack F Vogel ixl_setup_interface(device_t dev, struct ixl_vsi *vsi)
225461ae650dSJack F Vogel {
225561ae650dSJack F Vogel 	struct ifnet		*ifp;
225661ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
225761ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
225861ae650dSJack F Vogel 	struct i40e_aq_get_phy_abilities_resp abilities_resp;
225961ae650dSJack F Vogel 	enum i40e_status_code aq_error = 0;
226061ae650dSJack F Vogel 
226161ae650dSJack F Vogel 	INIT_DEBUGOUT("ixl_setup_interface: begin");
226261ae650dSJack F Vogel 
226361ae650dSJack F Vogel 	ifp = vsi->ifp = if_alloc(IFT_ETHER);
226461ae650dSJack F Vogel 	if (ifp == NULL) {
226561ae650dSJack F Vogel 		device_printf(dev, "can not allocate ifnet structure\n");
226661ae650dSJack F Vogel 		return (-1);
226761ae650dSJack F Vogel 	}
226861ae650dSJack F Vogel 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
226961ae650dSJack F Vogel 	ifp->if_mtu = ETHERMTU;
227061ae650dSJack F Vogel 	ifp->if_baudrate = 4000000000;  // ??
227161ae650dSJack F Vogel 	ifp->if_init = ixl_init;
227261ae650dSJack F Vogel 	ifp->if_softc = vsi;
227361ae650dSJack F Vogel 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
227461ae650dSJack F Vogel 	ifp->if_ioctl = ixl_ioctl;
227561ae650dSJack F Vogel 
227661ae650dSJack F Vogel 	ifp->if_transmit = ixl_mq_start;
227761ae650dSJack F Vogel 
227861ae650dSJack F Vogel 	ifp->if_qflush = ixl_qflush;
227961ae650dSJack F Vogel 
228061ae650dSJack F Vogel 	ifp->if_snd.ifq_maxlen = que->num_desc - 2;
228161ae650dSJack F Vogel 
228261ae650dSJack F Vogel 	ether_ifattach(ifp, hw->mac.addr);
228361ae650dSJack F Vogel 
228461ae650dSJack F Vogel 	vsi->max_frame_size =
228561ae650dSJack F Vogel 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN
228661ae650dSJack F Vogel 	    + ETHER_VLAN_ENCAP_LEN;
228761ae650dSJack F Vogel 
228861ae650dSJack F Vogel 	/*
228961ae650dSJack F Vogel 	 * Tell the upper layer(s) we support long frames.
229061ae650dSJack F Vogel 	 */
229161ae650dSJack F Vogel 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
229261ae650dSJack F Vogel 
229361ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_HWCSUM;
229461ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_HWCSUM_IPV6;
229561ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_TSO;
229661ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_JUMBO_MTU;
229761ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_LRO;
229861ae650dSJack F Vogel 
229961ae650dSJack F Vogel 	/* VLAN capabilties */
230061ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING
230161ae650dSJack F Vogel 			     |  IFCAP_VLAN_HWTSO
230261ae650dSJack F Vogel 			     |  IFCAP_VLAN_MTU
230361ae650dSJack F Vogel 			     |  IFCAP_VLAN_HWCSUM;
230461ae650dSJack F Vogel 	ifp->if_capenable = ifp->if_capabilities;
230561ae650dSJack F Vogel 
230661ae650dSJack F Vogel 	/*
230761ae650dSJack F Vogel 	** Don't turn this on by default, if vlans are
230861ae650dSJack F Vogel 	** created on another pseudo device (eg. lagg)
230961ae650dSJack F Vogel 	** then vlan events are not passed thru, breaking
231061ae650dSJack F Vogel 	** operation, but with HW FILTER off it works. If
231161ae650dSJack F Vogel 	** using vlans directly on the ixl driver you can
231261ae650dSJack F Vogel 	** enable this and get full hardware tag filtering.
231361ae650dSJack F Vogel 	*/
231461ae650dSJack F Vogel 	ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
231561ae650dSJack F Vogel 
231661ae650dSJack F Vogel 	/*
231761ae650dSJack F Vogel 	 * Specify the media types supported by this adapter and register
231861ae650dSJack F Vogel 	 * callbacks to update media and link information
231961ae650dSJack F Vogel 	 */
232061ae650dSJack F Vogel 	ifmedia_init(&vsi->media, IFM_IMASK, ixl_media_change,
232161ae650dSJack F Vogel 		     ixl_media_status);
232261ae650dSJack F Vogel 
232361ae650dSJack F Vogel 	aq_error = i40e_aq_get_phy_capabilities(hw, FALSE, TRUE, &abilities_resp, NULL);
232461ae650dSJack F Vogel 	if (aq_error) {
232561ae650dSJack F Vogel 		printf("Error getting supported media types, AQ error %d\n", aq_error);
232661ae650dSJack F Vogel 		return (EPERM);
232761ae650dSJack F Vogel 	}
232861ae650dSJack F Vogel 
232961ae650dSJack F Vogel 	/* Display supported media types */
233061ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_100BASE_TX))
233161ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_100_TX, 0, NULL);
233261ae650dSJack F Vogel 
233361ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_1000BASE_T))
233461ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_1000_T, 0, NULL);
233561ae650dSJack F Vogel 
233661ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_10GBASE_CR1_CU) ||
233761ae650dSJack F Vogel 	    abilities_resp.phy_type & (1 << I40E_PHY_TYPE_10GBASE_SFPP_CU))
233861ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_10G_TWINAX, 0, NULL);
233961ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_10GBASE_SR))
234061ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
234161ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_10GBASE_LR))
234261ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
234361ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_10GBASE_T))
234461ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_10G_T, 0, NULL);
234561ae650dSJack F Vogel 
234661ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_40GBASE_CR4_CU) ||
234761ae650dSJack F Vogel 	    abilities_resp.phy_type & (1 << I40E_PHY_TYPE_40GBASE_CR4))
234861ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_40G_CR4, 0, NULL);
234961ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_40GBASE_SR4))
235061ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_40G_SR4, 0, NULL);
235161ae650dSJack F Vogel 	if (abilities_resp.phy_type & (1 << I40E_PHY_TYPE_40GBASE_LR4))
235261ae650dSJack F Vogel 		ifmedia_add(&vsi->media, IFM_ETHER | IFM_40G_LR4, 0, NULL);
235361ae650dSJack F Vogel 
235461ae650dSJack F Vogel 	/* Use autoselect media by default */
235561ae650dSJack F Vogel 	ifmedia_add(&vsi->media, IFM_ETHER | IFM_AUTO, 0, NULL);
235661ae650dSJack F Vogel 	ifmedia_set(&vsi->media, IFM_ETHER | IFM_AUTO);
235761ae650dSJack F Vogel 
235861ae650dSJack F Vogel 	return (0);
235961ae650dSJack F Vogel }
236061ae650dSJack F Vogel 
236161ae650dSJack F Vogel static bool
236261ae650dSJack F Vogel ixl_config_link(struct i40e_hw *hw)
236361ae650dSJack F Vogel {
236461ae650dSJack F Vogel 	bool check;
236561ae650dSJack F Vogel 
236661ae650dSJack F Vogel 	i40e_aq_get_link_info(hw, TRUE, NULL, NULL);
236761ae650dSJack F Vogel 	check = i40e_get_link_status(hw);
236861ae650dSJack F Vogel #ifdef IXL_DEBUG
236961ae650dSJack F Vogel 	printf("Link is %s\n", check ? "up":"down");
237061ae650dSJack F Vogel #endif
237161ae650dSJack F Vogel 	return (check);
237261ae650dSJack F Vogel }
237361ae650dSJack F Vogel 
237461ae650dSJack F Vogel /*********************************************************************
237561ae650dSJack F Vogel  *
237661ae650dSJack F Vogel  *  Initialize this VSI
237761ae650dSJack F Vogel  *
237861ae650dSJack F Vogel  **********************************************************************/
237961ae650dSJack F Vogel static int
238061ae650dSJack F Vogel ixl_setup_vsi(struct ixl_vsi *vsi)
238161ae650dSJack F Vogel {
238261ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
238361ae650dSJack F Vogel 	device_t 	dev = vsi->dev;
238461ae650dSJack F Vogel 	struct i40e_aqc_get_switch_config_resp *sw_config;
238561ae650dSJack F Vogel 	struct i40e_vsi_context	ctxt;
238661ae650dSJack F Vogel 	u8	aq_buf[I40E_AQ_LARGE_BUF];
238761ae650dSJack F Vogel 	int	ret = I40E_SUCCESS;
238861ae650dSJack F Vogel 	u16	next = 0;
238961ae650dSJack F Vogel 
239061ae650dSJack F Vogel 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
239161ae650dSJack F Vogel 	ret = i40e_aq_get_switch_config(hw, sw_config,
239261ae650dSJack F Vogel 	    sizeof(aq_buf), &next, NULL);
239361ae650dSJack F Vogel 	if (ret) {
239461ae650dSJack F Vogel 		device_printf(dev,"aq_get_switch_config failed!!\n");
239561ae650dSJack F Vogel 		return (ret);
239661ae650dSJack F Vogel 	}
239761ae650dSJack F Vogel #ifdef IXL_DEBUG
239861ae650dSJack F Vogel 	printf("Switch config: header reported: %d in structure, %d total\n",
239961ae650dSJack F Vogel     	    sw_config->header.num_reported, sw_config->header.num_total);
240061ae650dSJack F Vogel 	printf("type=%d seid=%d uplink=%d downlink=%d\n",
240161ae650dSJack F Vogel 	    sw_config->element[0].element_type,
240261ae650dSJack F Vogel 	    sw_config->element[0].seid,
240361ae650dSJack F Vogel 	    sw_config->element[0].uplink_seid,
240461ae650dSJack F Vogel 	    sw_config->element[0].downlink_seid);
240561ae650dSJack F Vogel #endif
240661ae650dSJack F Vogel 	/* Save off this important value */
240761ae650dSJack F Vogel 	vsi->seid = sw_config->element[0].seid;
240861ae650dSJack F Vogel 
240961ae650dSJack F Vogel 	memset(&ctxt, 0, sizeof(ctxt));
241061ae650dSJack F Vogel 	ctxt.seid = vsi->seid;
241161ae650dSJack F Vogel 	ctxt.pf_num = hw->pf_id;
241261ae650dSJack F Vogel 	ret = i40e_aq_get_vsi_params(hw, &ctxt, NULL);
241361ae650dSJack F Vogel 	if (ret) {
241461ae650dSJack F Vogel 		device_printf(dev,"get vsi params failed %x!!\n", ret);
241561ae650dSJack F Vogel 		return (ret);
241661ae650dSJack F Vogel 	}
241761ae650dSJack F Vogel #ifdef IXL_DEBUG
241861ae650dSJack F Vogel 	printf("get_vsi_params: seid: %d, uplinkseid: %d, vsi_number: %d, "
241961ae650dSJack F Vogel 	    "vsis_allocated: %d, vsis_unallocated: %d, flags: 0x%x, "
242061ae650dSJack F Vogel 	    "pfnum: %d, vfnum: %d, stat idx: %d, enabled: %d\n", ctxt.seid,
242161ae650dSJack F Vogel 	    ctxt.uplink_seid, ctxt.vsi_number,
242261ae650dSJack F Vogel 	    ctxt.vsis_allocated, ctxt.vsis_unallocated,
242361ae650dSJack F Vogel 	    ctxt.flags, ctxt.pf_num, ctxt.vf_num,
242461ae650dSJack F Vogel 	    ctxt.info.stat_counter_idx, ctxt.info.up_enable_bits);
242561ae650dSJack F Vogel #endif
242661ae650dSJack F Vogel 	/*
242761ae650dSJack F Vogel 	** Set the queue and traffic class bits
242861ae650dSJack F Vogel 	**  - when multiple traffic classes are supported
242961ae650dSJack F Vogel 	**    this will need to be more robust.
243061ae650dSJack F Vogel 	*/
243161ae650dSJack F Vogel 	ctxt.info.valid_sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
243261ae650dSJack F Vogel 	ctxt.info.mapping_flags |= I40E_AQ_VSI_QUE_MAP_CONTIG;
243361ae650dSJack F Vogel 	ctxt.info.queue_mapping[0] = 0;
243461ae650dSJack F Vogel 	ctxt.info.tc_mapping[0] = 0x0800;
243561ae650dSJack F Vogel 
243661ae650dSJack F Vogel 	/* Set VLAN receive stripping mode */
243761ae650dSJack F Vogel 	ctxt.info.valid_sections |= I40E_AQ_VSI_PROP_VLAN_VALID;
243861ae650dSJack F Vogel 	ctxt.info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL;
243961ae650dSJack F Vogel 	if (vsi->ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
244061ae650dSJack F Vogel 	    ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
244161ae650dSJack F Vogel 	else
244261ae650dSJack F Vogel 	    ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
244361ae650dSJack F Vogel 
244461ae650dSJack F Vogel 	/* Keep copy of VSI info in VSI for statistic counters */
244561ae650dSJack F Vogel 	memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
244661ae650dSJack F Vogel 
244761ae650dSJack F Vogel 	/* Reset VSI statistics */
244861ae650dSJack F Vogel 	ixl_vsi_reset_stats(vsi);
244961ae650dSJack F Vogel 	vsi->hw_filters_add = 0;
245061ae650dSJack F Vogel 	vsi->hw_filters_del = 0;
245161ae650dSJack F Vogel 
245261ae650dSJack F Vogel 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
245361ae650dSJack F Vogel 	if (ret)
245461ae650dSJack F Vogel 		device_printf(dev,"update vsi params failed %x!!\n",
245561ae650dSJack F Vogel 		   hw->aq.asq_last_status);
245661ae650dSJack F Vogel 	return (ret);
245761ae650dSJack F Vogel }
245861ae650dSJack F Vogel 
245961ae650dSJack F Vogel 
246061ae650dSJack F Vogel /*********************************************************************
246161ae650dSJack F Vogel  *
246261ae650dSJack F Vogel  *  Initialize the VSI:  this handles contexts, which means things
246361ae650dSJack F Vogel  *  			 like the number of descriptors, buffer size,
246461ae650dSJack F Vogel  *			 plus we init the rings thru this function.
246561ae650dSJack F Vogel  *
246661ae650dSJack F Vogel  **********************************************************************/
246761ae650dSJack F Vogel static int
246861ae650dSJack F Vogel ixl_initialize_vsi(struct ixl_vsi *vsi)
246961ae650dSJack F Vogel {
247061ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
247161ae650dSJack F Vogel 	device_t		dev = vsi->dev;
247261ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
247361ae650dSJack F Vogel 	int			err = 0;
247461ae650dSJack F Vogel 
247561ae650dSJack F Vogel 
247661ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, que++) {
247761ae650dSJack F Vogel 		struct tx_ring		*txr = &que->txr;
247861ae650dSJack F Vogel 		struct rx_ring 		*rxr = &que->rxr;
247961ae650dSJack F Vogel 		struct i40e_hmc_obj_txq tctx;
248061ae650dSJack F Vogel 		struct i40e_hmc_obj_rxq rctx;
248161ae650dSJack F Vogel 		u32			txctl;
248261ae650dSJack F Vogel 		u16			size;
248361ae650dSJack F Vogel 
248461ae650dSJack F Vogel 
248561ae650dSJack F Vogel 		/* Setup the HMC TX Context  */
248661ae650dSJack F Vogel 		size = que->num_desc * sizeof(struct i40e_tx_desc);
248761ae650dSJack F Vogel 		memset(&tctx, 0, sizeof(struct i40e_hmc_obj_txq));
248861ae650dSJack F Vogel 		tctx.new_context = 1;
248961ae650dSJack F Vogel 		tctx.base = (txr->dma.pa/128);
249061ae650dSJack F Vogel 		tctx.qlen = que->num_desc;
249161ae650dSJack F Vogel 		tctx.fc_ena = 0;
249261ae650dSJack F Vogel 		tctx.rdylist = vsi->info.qs_handle[0]; /* index is TC */
249361ae650dSJack F Vogel 		/* Enable HEAD writeback */
249461ae650dSJack F Vogel 		tctx.head_wb_ena = 1;
249561ae650dSJack F Vogel 		tctx.head_wb_addr = txr->dma.pa +
249661ae650dSJack F Vogel 		    (que->num_desc * sizeof(struct i40e_tx_desc));
249761ae650dSJack F Vogel 		tctx.rdylist_act = 0;
249861ae650dSJack F Vogel 		err = i40e_clear_lan_tx_queue_context(hw, i);
249961ae650dSJack F Vogel 		if (err) {
250061ae650dSJack F Vogel 			device_printf(dev, "Unable to clear TX context\n");
250161ae650dSJack F Vogel 			break;
250261ae650dSJack F Vogel 		}
250361ae650dSJack F Vogel 		err = i40e_set_lan_tx_queue_context(hw, i, &tctx);
250461ae650dSJack F Vogel 		if (err) {
250561ae650dSJack F Vogel 			device_printf(dev, "Unable to set TX context\n");
250661ae650dSJack F Vogel 			break;
250761ae650dSJack F Vogel 		}
250861ae650dSJack F Vogel 		/* Associate the ring with this PF */
250961ae650dSJack F Vogel 		txctl = I40E_QTX_CTL_PF_QUEUE;
251061ae650dSJack F Vogel 		txctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
251161ae650dSJack F Vogel 		    I40E_QTX_CTL_PF_INDX_MASK);
251261ae650dSJack F Vogel 		wr32(hw, I40E_QTX_CTL(i), txctl);
251361ae650dSJack F Vogel 		ixl_flush(hw);
251461ae650dSJack F Vogel 
251561ae650dSJack F Vogel 		/* Do ring (re)init */
251661ae650dSJack F Vogel 		ixl_init_tx_ring(que);
251761ae650dSJack F Vogel 
251861ae650dSJack F Vogel 		/* Next setup the HMC RX Context  */
251961ae650dSJack F Vogel 		if (vsi->max_frame_size <= 2048)
252061ae650dSJack F Vogel 			rxr->mbuf_sz = MCLBYTES;
252161ae650dSJack F Vogel 		else
252261ae650dSJack F Vogel 			rxr->mbuf_sz = MJUMPAGESIZE;
252361ae650dSJack F Vogel 
252461ae650dSJack F Vogel 		u16 max_rxmax = rxr->mbuf_sz * hw->func_caps.rx_buf_chain_len;
252561ae650dSJack F Vogel 
252661ae650dSJack F Vogel 		/* Set up an RX context for the HMC */
252761ae650dSJack F Vogel 		memset(&rctx, 0, sizeof(struct i40e_hmc_obj_rxq));
252861ae650dSJack F Vogel 		rctx.dbuff = rxr->mbuf_sz >> I40E_RXQ_CTX_DBUFF_SHIFT;
252961ae650dSJack F Vogel 		/* ignore header split for now */
253061ae650dSJack F Vogel 		rctx.hbuff = 0 >> I40E_RXQ_CTX_HBUFF_SHIFT;
253161ae650dSJack F Vogel 		rctx.rxmax = (vsi->max_frame_size < max_rxmax) ?
253261ae650dSJack F Vogel 		    vsi->max_frame_size : max_rxmax;
253361ae650dSJack F Vogel 		rctx.dtype = 0;
253461ae650dSJack F Vogel 		rctx.dsize = 1;	/* do 32byte descriptors */
253561ae650dSJack F Vogel 		rctx.hsplit_0 = 0;  /* no HDR split initially */
253661ae650dSJack F Vogel 		rctx.base = (rxr->dma.pa/128);
253761ae650dSJack F Vogel 		rctx.qlen = que->num_desc;
253861ae650dSJack F Vogel 		rctx.tphrdesc_ena = 1;
253961ae650dSJack F Vogel 		rctx.tphwdesc_ena = 1;
254061ae650dSJack F Vogel 		rctx.tphdata_ena = 0;
254161ae650dSJack F Vogel 		rctx.tphhead_ena = 0;
254261ae650dSJack F Vogel 		rctx.lrxqthresh = 2;
254361ae650dSJack F Vogel 		rctx.crcstrip = 1;
254461ae650dSJack F Vogel 		rctx.l2tsel = 1;
254561ae650dSJack F Vogel 		rctx.showiv = 1;
254661ae650dSJack F Vogel 		rctx.fc_ena = 0;
254761ae650dSJack F Vogel 		rctx.prefena = 1;
254861ae650dSJack F Vogel 
254961ae650dSJack F Vogel 		err = i40e_clear_lan_rx_queue_context(hw, i);
255061ae650dSJack F Vogel 		if (err) {
255161ae650dSJack F Vogel 			device_printf(dev,
255261ae650dSJack F Vogel 			    "Unable to clear RX context %d\n", i);
255361ae650dSJack F Vogel 			break;
255461ae650dSJack F Vogel 		}
255561ae650dSJack F Vogel 		err = i40e_set_lan_rx_queue_context(hw, i, &rctx);
255661ae650dSJack F Vogel 		if (err) {
255761ae650dSJack F Vogel 			device_printf(dev, "Unable to set RX context %d\n", i);
255861ae650dSJack F Vogel 			break;
255961ae650dSJack F Vogel 		}
256061ae650dSJack F Vogel 		err = ixl_init_rx_ring(que);
256161ae650dSJack F Vogel 		if (err) {
256261ae650dSJack F Vogel 			device_printf(dev, "Fail in init_rx_ring %d\n", i);
256361ae650dSJack F Vogel 			break;
256461ae650dSJack F Vogel 		}
256561ae650dSJack F Vogel 		wr32(vsi->hw, I40E_QRX_TAIL(que->me), 0);
256661ae650dSJack F Vogel 		wr32(vsi->hw, I40E_QRX_TAIL(que->me), que->num_desc - 1);
256761ae650dSJack F Vogel 	}
256861ae650dSJack F Vogel 	return (err);
256961ae650dSJack F Vogel }
257061ae650dSJack F Vogel 
257161ae650dSJack F Vogel 
257261ae650dSJack F Vogel /*********************************************************************
257361ae650dSJack F Vogel  *
257461ae650dSJack F Vogel  *  Free all VSI structs.
257561ae650dSJack F Vogel  *
257661ae650dSJack F Vogel  **********************************************************************/
257761ae650dSJack F Vogel void
257861ae650dSJack F Vogel ixl_free_vsi(struct ixl_vsi *vsi)
257961ae650dSJack F Vogel {
258061ae650dSJack F Vogel 	struct ixl_pf		*pf = (struct ixl_pf *)vsi->back;
258161ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
258261ae650dSJack F Vogel 	struct ixl_mac_filter *f;
258361ae650dSJack F Vogel 
258461ae650dSJack F Vogel 	/* Free station queues */
258561ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++, que++) {
258661ae650dSJack F Vogel 		struct tx_ring *txr = &que->txr;
258761ae650dSJack F Vogel 		struct rx_ring *rxr = &que->rxr;
258861ae650dSJack F Vogel 
258961ae650dSJack F Vogel 		if (!mtx_initialized(&txr->mtx)) /* uninitialized */
259061ae650dSJack F Vogel 			continue;
259161ae650dSJack F Vogel 		IXL_TX_LOCK(txr);
259261ae650dSJack F Vogel 		ixl_free_que_tx(que);
259361ae650dSJack F Vogel 		if (txr->base)
259461ae650dSJack F Vogel 			i40e_free_dma(&pf->hw, &txr->dma);
259561ae650dSJack F Vogel 		IXL_TX_UNLOCK(txr);
259661ae650dSJack F Vogel 		IXL_TX_LOCK_DESTROY(txr);
259761ae650dSJack F Vogel 
259861ae650dSJack F Vogel 		if (!mtx_initialized(&rxr->mtx)) /* uninitialized */
259961ae650dSJack F Vogel 			continue;
260061ae650dSJack F Vogel 		IXL_RX_LOCK(rxr);
260161ae650dSJack F Vogel 		ixl_free_que_rx(que);
260261ae650dSJack F Vogel 		if (rxr->base)
260361ae650dSJack F Vogel 			i40e_free_dma(&pf->hw, &rxr->dma);
260461ae650dSJack F Vogel 		IXL_RX_UNLOCK(rxr);
260561ae650dSJack F Vogel 		IXL_RX_LOCK_DESTROY(rxr);
260661ae650dSJack F Vogel 
260761ae650dSJack F Vogel 	}
260861ae650dSJack F Vogel 	free(vsi->queues, M_DEVBUF);
260961ae650dSJack F Vogel 
261061ae650dSJack F Vogel 	/* Free VSI filter list */
261161ae650dSJack F Vogel 	while (!SLIST_EMPTY(&vsi->ftl)) {
261261ae650dSJack F Vogel 		f = SLIST_FIRST(&vsi->ftl);
261361ae650dSJack F Vogel 		SLIST_REMOVE_HEAD(&vsi->ftl, next);
261461ae650dSJack F Vogel 		free(f, M_DEVBUF);
261561ae650dSJack F Vogel 	}
261661ae650dSJack F Vogel }
261761ae650dSJack F Vogel 
261861ae650dSJack F Vogel 
261961ae650dSJack F Vogel /*********************************************************************
262061ae650dSJack F Vogel  *
262161ae650dSJack F Vogel  *  Allocate memory for the VSI (virtual station interface) and their
262261ae650dSJack F Vogel  *  associated queues, rings and the descriptors associated with each,
262361ae650dSJack F Vogel  *  called only once at attach.
262461ae650dSJack F Vogel  *
262561ae650dSJack F Vogel  **********************************************************************/
262661ae650dSJack F Vogel static int
262761ae650dSJack F Vogel ixl_setup_stations(struct ixl_pf *pf)
262861ae650dSJack F Vogel {
262961ae650dSJack F Vogel 	device_t		dev = pf->dev;
263061ae650dSJack F Vogel 	struct ixl_vsi		*vsi;
263161ae650dSJack F Vogel 	struct ixl_queue	*que;
263261ae650dSJack F Vogel 	struct tx_ring		*txr;
263361ae650dSJack F Vogel 	struct rx_ring		*rxr;
263461ae650dSJack F Vogel 	int 			rsize, tsize;
263561ae650dSJack F Vogel 	int			error = I40E_SUCCESS;
263661ae650dSJack F Vogel 
263761ae650dSJack F Vogel 	vsi = &pf->vsi;
263861ae650dSJack F Vogel 	vsi->back = (void *)pf;
263961ae650dSJack F Vogel 	vsi->hw = &pf->hw;
264061ae650dSJack F Vogel 	vsi->id = 0;
264161ae650dSJack F Vogel 	vsi->num_vlans = 0;
264261ae650dSJack F Vogel 
264361ae650dSJack F Vogel 	/* Get memory for the station queues */
264461ae650dSJack F Vogel         if (!(vsi->queues =
264561ae650dSJack F Vogel             (struct ixl_queue *) malloc(sizeof(struct ixl_queue) *
264661ae650dSJack F Vogel             vsi->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
264761ae650dSJack F Vogel                 device_printf(dev, "Unable to allocate queue memory\n");
264861ae650dSJack F Vogel                 error = ENOMEM;
264961ae650dSJack F Vogel                 goto early;
265061ae650dSJack F Vogel         }
265161ae650dSJack F Vogel 
265261ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++) {
265361ae650dSJack F Vogel 		que = &vsi->queues[i];
265461ae650dSJack F Vogel 		que->num_desc = ixl_ringsz;
265561ae650dSJack F Vogel 		que->me = i;
265661ae650dSJack F Vogel 		que->vsi = vsi;
265761ae650dSJack F Vogel 		/* mark the queue as active */
265861ae650dSJack F Vogel 		vsi->active_queues |= (u64)1 << que->me;
265961ae650dSJack F Vogel 		txr = &que->txr;
266061ae650dSJack F Vogel 		txr->que = que;
266161ae650dSJack F Vogel 		txr->tail = I40E_QTX_TAIL(que->me);
266261ae650dSJack F Vogel 
266361ae650dSJack F Vogel 		/* Initialize the TX lock */
266461ae650dSJack F Vogel 		snprintf(txr->mtx_name, sizeof(txr->mtx_name), "%s:tx(%d)",
266561ae650dSJack F Vogel 		    device_get_nameunit(dev), que->me);
266661ae650dSJack F Vogel 		mtx_init(&txr->mtx, txr->mtx_name, NULL, MTX_DEF);
266761ae650dSJack F Vogel 		/* Create the TX descriptor ring */
266861ae650dSJack F Vogel 		tsize = roundup2((que->num_desc *
266961ae650dSJack F Vogel 		    sizeof(struct i40e_tx_desc)) +
267061ae650dSJack F Vogel 		    sizeof(u32), DBA_ALIGN);
267161ae650dSJack F Vogel 		if (i40e_allocate_dma(&pf->hw,
267261ae650dSJack F Vogel 		    &txr->dma, tsize, DBA_ALIGN)) {
267361ae650dSJack F Vogel 			device_printf(dev,
267461ae650dSJack F Vogel 			    "Unable to allocate TX Descriptor memory\n");
267561ae650dSJack F Vogel 			error = ENOMEM;
267661ae650dSJack F Vogel 			goto fail;
267761ae650dSJack F Vogel 		}
267861ae650dSJack F Vogel 		txr->base = (struct i40e_tx_desc *)txr->dma.va;
267961ae650dSJack F Vogel 		bzero((void *)txr->base, tsize);
268061ae650dSJack F Vogel        		/* Now allocate transmit soft structs for the ring */
268161ae650dSJack F Vogel        		if (ixl_allocate_tx_data(que)) {
268261ae650dSJack F Vogel 			device_printf(dev,
268361ae650dSJack F Vogel 			    "Critical Failure setting up TX structures\n");
268461ae650dSJack F Vogel 			error = ENOMEM;
268561ae650dSJack F Vogel 			goto fail;
268661ae650dSJack F Vogel        		}
268761ae650dSJack F Vogel 		/* Allocate a buf ring */
268861ae650dSJack F Vogel 		txr->br = buf_ring_alloc(4096, M_DEVBUF,
268961ae650dSJack F Vogel 		    M_WAITOK, &txr->mtx);
269061ae650dSJack F Vogel 		if (txr->br == NULL) {
269161ae650dSJack F Vogel 			device_printf(dev,
269261ae650dSJack F Vogel 			    "Critical Failure setting up TX buf ring\n");
269361ae650dSJack F Vogel 			error = ENOMEM;
269461ae650dSJack F Vogel 			goto fail;
269561ae650dSJack F Vogel        		}
269661ae650dSJack F Vogel 
269761ae650dSJack F Vogel 		/*
269861ae650dSJack F Vogel 		 * Next the RX queues...
269961ae650dSJack F Vogel 		 */
270061ae650dSJack F Vogel 		rsize = roundup2(que->num_desc *
270161ae650dSJack F Vogel 		    sizeof(union i40e_rx_desc), DBA_ALIGN);
270261ae650dSJack F Vogel 		rxr = &que->rxr;
270361ae650dSJack F Vogel 		rxr->que = que;
270461ae650dSJack F Vogel 		rxr->tail = I40E_QRX_TAIL(que->me);
270561ae650dSJack F Vogel 
270661ae650dSJack F Vogel 		/* Initialize the RX side lock */
270761ae650dSJack F Vogel 		snprintf(rxr->mtx_name, sizeof(rxr->mtx_name), "%s:rx(%d)",
270861ae650dSJack F Vogel 		    device_get_nameunit(dev), que->me);
270961ae650dSJack F Vogel 		mtx_init(&rxr->mtx, rxr->mtx_name, NULL, MTX_DEF);
271061ae650dSJack F Vogel 
271161ae650dSJack F Vogel 		if (i40e_allocate_dma(&pf->hw,
271261ae650dSJack F Vogel 		    &rxr->dma, rsize, 4096)) {
271361ae650dSJack F Vogel 			device_printf(dev,
271461ae650dSJack F Vogel 			    "Unable to allocate RX Descriptor memory\n");
271561ae650dSJack F Vogel 			error = ENOMEM;
271661ae650dSJack F Vogel 			goto fail;
271761ae650dSJack F Vogel 		}
271861ae650dSJack F Vogel 		rxr->base = (union i40e_rx_desc *)rxr->dma.va;
271961ae650dSJack F Vogel 		bzero((void *)rxr->base, rsize);
272061ae650dSJack F Vogel 
272161ae650dSJack F Vogel         	/* Allocate receive soft structs for the ring*/
272261ae650dSJack F Vogel 		if (ixl_allocate_rx_data(que)) {
272361ae650dSJack F Vogel 			device_printf(dev,
272461ae650dSJack F Vogel 			    "Critical Failure setting up receive structs\n");
272561ae650dSJack F Vogel 			error = ENOMEM;
272661ae650dSJack F Vogel 			goto fail;
272761ae650dSJack F Vogel 		}
272861ae650dSJack F Vogel 	}
272961ae650dSJack F Vogel 
273061ae650dSJack F Vogel 	return (0);
273161ae650dSJack F Vogel 
273261ae650dSJack F Vogel fail:
273361ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++) {
273461ae650dSJack F Vogel 		que = &vsi->queues[i];
273561ae650dSJack F Vogel 		rxr = &que->rxr;
273661ae650dSJack F Vogel 		txr = &que->txr;
273761ae650dSJack F Vogel 		if (rxr->base)
273861ae650dSJack F Vogel 			i40e_free_dma(&pf->hw, &rxr->dma);
273961ae650dSJack F Vogel 		if (txr->base)
274061ae650dSJack F Vogel 			i40e_free_dma(&pf->hw, &txr->dma);
274161ae650dSJack F Vogel 	}
274261ae650dSJack F Vogel 
274361ae650dSJack F Vogel early:
274461ae650dSJack F Vogel 	return (error);
274561ae650dSJack F Vogel }
274661ae650dSJack F Vogel 
274761ae650dSJack F Vogel /*
274861ae650dSJack F Vogel ** Provide a update to the queue RX
274961ae650dSJack F Vogel ** interrupt moderation value.
275061ae650dSJack F Vogel */
275161ae650dSJack F Vogel static void
275261ae650dSJack F Vogel ixl_set_queue_rx_itr(struct ixl_queue *que)
275361ae650dSJack F Vogel {
275461ae650dSJack F Vogel 	struct ixl_vsi	*vsi = que->vsi;
275561ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
275661ae650dSJack F Vogel 	struct rx_ring	*rxr = &que->rxr;
275761ae650dSJack F Vogel 	u16		rx_itr;
275861ae650dSJack F Vogel 	u16		rx_latency = 0;
275961ae650dSJack F Vogel 	int		rx_bytes;
276061ae650dSJack F Vogel 
276161ae650dSJack F Vogel 
276261ae650dSJack F Vogel 	/* Idle, do nothing */
276361ae650dSJack F Vogel 	if (rxr->bytes == 0)
276461ae650dSJack F Vogel 		return;
276561ae650dSJack F Vogel 
276661ae650dSJack F Vogel 	if (ixl_dynamic_rx_itr) {
276761ae650dSJack F Vogel 		rx_bytes = rxr->bytes/rxr->itr;
276861ae650dSJack F Vogel 		rx_itr = rxr->itr;
276961ae650dSJack F Vogel 
277061ae650dSJack F Vogel 		/* Adjust latency range */
277161ae650dSJack F Vogel 		switch (rxr->latency) {
277261ae650dSJack F Vogel 		case IXL_LOW_LATENCY:
277361ae650dSJack F Vogel 			if (rx_bytes > 10) {
277461ae650dSJack F Vogel 				rx_latency = IXL_AVE_LATENCY;
277561ae650dSJack F Vogel 				rx_itr = IXL_ITR_20K;
277661ae650dSJack F Vogel 			}
277761ae650dSJack F Vogel 			break;
277861ae650dSJack F Vogel 		case IXL_AVE_LATENCY:
277961ae650dSJack F Vogel 			if (rx_bytes > 20) {
278061ae650dSJack F Vogel 				rx_latency = IXL_BULK_LATENCY;
278161ae650dSJack F Vogel 				rx_itr = IXL_ITR_8K;
278261ae650dSJack F Vogel 			} else if (rx_bytes <= 10) {
278361ae650dSJack F Vogel 				rx_latency = IXL_LOW_LATENCY;
278461ae650dSJack F Vogel 				rx_itr = IXL_ITR_100K;
278561ae650dSJack F Vogel 			}
278661ae650dSJack F Vogel 			break;
278761ae650dSJack F Vogel 		case IXL_BULK_LATENCY:
278861ae650dSJack F Vogel 			if (rx_bytes <= 20) {
278961ae650dSJack F Vogel 				rx_latency = IXL_AVE_LATENCY;
279061ae650dSJack F Vogel 				rx_itr = IXL_ITR_20K;
279161ae650dSJack F Vogel 			}
279261ae650dSJack F Vogel 			break;
279361ae650dSJack F Vogel        		 }
279461ae650dSJack F Vogel 
279561ae650dSJack F Vogel 		rxr->latency = rx_latency;
279661ae650dSJack F Vogel 
279761ae650dSJack F Vogel 		if (rx_itr != rxr->itr) {
279861ae650dSJack F Vogel 			/* do an exponential smoothing */
279961ae650dSJack F Vogel 			rx_itr = (10 * rx_itr * rxr->itr) /
280061ae650dSJack F Vogel 			    ((9 * rx_itr) + rxr->itr);
280161ae650dSJack F Vogel 			rxr->itr = rx_itr & IXL_MAX_ITR;
280261ae650dSJack F Vogel 			wr32(hw, I40E_PFINT_ITRN(IXL_RX_ITR,
280361ae650dSJack F Vogel 			    que->me), rxr->itr);
280461ae650dSJack F Vogel 		}
280561ae650dSJack F Vogel 	} else { /* We may have have toggled to non-dynamic */
280661ae650dSJack F Vogel 		if (vsi->rx_itr_setting & IXL_ITR_DYNAMIC)
280761ae650dSJack F Vogel 			vsi->rx_itr_setting = ixl_rx_itr;
280861ae650dSJack F Vogel 		/* Update the hardware if needed */
280961ae650dSJack F Vogel 		if (rxr->itr != vsi->rx_itr_setting) {
281061ae650dSJack F Vogel 			rxr->itr = vsi->rx_itr_setting;
281161ae650dSJack F Vogel 			wr32(hw, I40E_PFINT_ITRN(IXL_RX_ITR,
281261ae650dSJack F Vogel 			    que->me), rxr->itr);
281361ae650dSJack F Vogel 		}
281461ae650dSJack F Vogel 	}
281561ae650dSJack F Vogel 	rxr->bytes = 0;
281661ae650dSJack F Vogel 	rxr->packets = 0;
281761ae650dSJack F Vogel 	return;
281861ae650dSJack F Vogel }
281961ae650dSJack F Vogel 
282061ae650dSJack F Vogel 
282161ae650dSJack F Vogel /*
282261ae650dSJack F Vogel ** Provide a update to the queue TX
282361ae650dSJack F Vogel ** interrupt moderation value.
282461ae650dSJack F Vogel */
282561ae650dSJack F Vogel static void
282661ae650dSJack F Vogel ixl_set_queue_tx_itr(struct ixl_queue *que)
282761ae650dSJack F Vogel {
282861ae650dSJack F Vogel 	struct ixl_vsi	*vsi = que->vsi;
282961ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
283061ae650dSJack F Vogel 	struct tx_ring	*txr = &que->txr;
283161ae650dSJack F Vogel 	u16		tx_itr;
283261ae650dSJack F Vogel 	u16		tx_latency = 0;
283361ae650dSJack F Vogel 	int		tx_bytes;
283461ae650dSJack F Vogel 
283561ae650dSJack F Vogel 
283661ae650dSJack F Vogel 	/* Idle, do nothing */
283761ae650dSJack F Vogel 	if (txr->bytes == 0)
283861ae650dSJack F Vogel 		return;
283961ae650dSJack F Vogel 
284061ae650dSJack F Vogel 	if (ixl_dynamic_tx_itr) {
284161ae650dSJack F Vogel 		tx_bytes = txr->bytes/txr->itr;
284261ae650dSJack F Vogel 		tx_itr = txr->itr;
284361ae650dSJack F Vogel 
284461ae650dSJack F Vogel 		switch (txr->latency) {
284561ae650dSJack F Vogel 		case IXL_LOW_LATENCY:
284661ae650dSJack F Vogel 			if (tx_bytes > 10) {
284761ae650dSJack F Vogel 				tx_latency = IXL_AVE_LATENCY;
284861ae650dSJack F Vogel 				tx_itr = IXL_ITR_20K;
284961ae650dSJack F Vogel 			}
285061ae650dSJack F Vogel 			break;
285161ae650dSJack F Vogel 		case IXL_AVE_LATENCY:
285261ae650dSJack F Vogel 			if (tx_bytes > 20) {
285361ae650dSJack F Vogel 				tx_latency = IXL_BULK_LATENCY;
285461ae650dSJack F Vogel 				tx_itr = IXL_ITR_8K;
285561ae650dSJack F Vogel 			} else if (tx_bytes <= 10) {
285661ae650dSJack F Vogel 				tx_latency = IXL_LOW_LATENCY;
285761ae650dSJack F Vogel 				tx_itr = IXL_ITR_100K;
285861ae650dSJack F Vogel 			}
285961ae650dSJack F Vogel 			break;
286061ae650dSJack F Vogel 		case IXL_BULK_LATENCY:
286161ae650dSJack F Vogel 			if (tx_bytes <= 20) {
286261ae650dSJack F Vogel 				tx_latency = IXL_AVE_LATENCY;
286361ae650dSJack F Vogel 				tx_itr = IXL_ITR_20K;
286461ae650dSJack F Vogel 			}
286561ae650dSJack F Vogel 			break;
286661ae650dSJack F Vogel 		}
286761ae650dSJack F Vogel 
286861ae650dSJack F Vogel 		txr->latency = tx_latency;
286961ae650dSJack F Vogel 
287061ae650dSJack F Vogel 		if (tx_itr != txr->itr) {
287161ae650dSJack F Vogel        	         /* do an exponential smoothing */
287261ae650dSJack F Vogel 			tx_itr = (10 * tx_itr * txr->itr) /
287361ae650dSJack F Vogel 			    ((9 * tx_itr) + txr->itr);
287461ae650dSJack F Vogel 			txr->itr = tx_itr & IXL_MAX_ITR;
287561ae650dSJack F Vogel 			wr32(hw, I40E_PFINT_ITRN(IXL_TX_ITR,
287661ae650dSJack F Vogel 			    que->me), txr->itr);
287761ae650dSJack F Vogel 		}
287861ae650dSJack F Vogel 
287961ae650dSJack F Vogel 	} else { /* We may have have toggled to non-dynamic */
288061ae650dSJack F Vogel 		if (vsi->tx_itr_setting & IXL_ITR_DYNAMIC)
288161ae650dSJack F Vogel 			vsi->tx_itr_setting = ixl_tx_itr;
288261ae650dSJack F Vogel 		/* Update the hardware if needed */
288361ae650dSJack F Vogel 		if (txr->itr != vsi->tx_itr_setting) {
288461ae650dSJack F Vogel 			txr->itr = vsi->tx_itr_setting;
288561ae650dSJack F Vogel 			wr32(hw, I40E_PFINT_ITRN(IXL_TX_ITR,
288661ae650dSJack F Vogel 			    que->me), txr->itr);
288761ae650dSJack F Vogel 		}
288861ae650dSJack F Vogel 	}
288961ae650dSJack F Vogel 	txr->bytes = 0;
289061ae650dSJack F Vogel 	txr->packets = 0;
289161ae650dSJack F Vogel 	return;
289261ae650dSJack F Vogel }
289361ae650dSJack F Vogel 
289461ae650dSJack F Vogel 
289561ae650dSJack F Vogel static void
289661ae650dSJack F Vogel ixl_add_hw_stats(struct ixl_pf *pf)
289761ae650dSJack F Vogel {
289861ae650dSJack F Vogel 	device_t dev = pf->dev;
289961ae650dSJack F Vogel 	struct ixl_vsi *vsi = &pf->vsi;
290061ae650dSJack F Vogel 	struct ixl_queue *queues = vsi->queues;
290161ae650dSJack F Vogel 	struct i40e_eth_stats *vsi_stats = &vsi->eth_stats;
290261ae650dSJack F Vogel 	struct i40e_hw_port_stats *pf_stats = &pf->stats;
290361ae650dSJack F Vogel 
290461ae650dSJack F Vogel 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
290561ae650dSJack F Vogel 	struct sysctl_oid *tree = device_get_sysctl_tree(dev);
290661ae650dSJack F Vogel 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
290761ae650dSJack F Vogel 
290861ae650dSJack F Vogel 	struct sysctl_oid *vsi_node, *queue_node;
290961ae650dSJack F Vogel 	struct sysctl_oid_list *vsi_list, *queue_list;
291061ae650dSJack F Vogel 
291161ae650dSJack F Vogel 	struct tx_ring *txr;
291261ae650dSJack F Vogel 	struct rx_ring *rxr;
291361ae650dSJack F Vogel 
291461ae650dSJack F Vogel 	/* Driver statistics */
291561ae650dSJack F Vogel 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_events",
291661ae650dSJack F Vogel 			CTLFLAG_RD, &pf->watchdog_events,
291761ae650dSJack F Vogel 			"Watchdog timeouts");
291861ae650dSJack F Vogel 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "admin_irq",
291961ae650dSJack F Vogel 			CTLFLAG_RD, &pf->admin_irq,
292061ae650dSJack F Vogel 			"Admin Queue IRQ Handled");
292161ae650dSJack F Vogel 
292261ae650dSJack F Vogel 	/* VSI statistics */
292361ae650dSJack F Vogel #define QUEUE_NAME_LEN 32
292461ae650dSJack F Vogel 	char queue_namebuf[QUEUE_NAME_LEN];
292561ae650dSJack F Vogel 
292661ae650dSJack F Vogel 	// ERJ: Only one vsi now, re-do when >1 VSI enabled
292761ae650dSJack F Vogel 	// snprintf(vsi_namebuf, QUEUE_NAME_LEN, "vsi%d", vsi->info.stat_counter_idx);
292861ae650dSJack F Vogel 	vsi_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "vsi",
292961ae650dSJack F Vogel 				   CTLFLAG_RD, NULL, "VSI-specific stats");
293061ae650dSJack F Vogel 	vsi_list = SYSCTL_CHILDREN(vsi_node);
293161ae650dSJack F Vogel 
293261ae650dSJack F Vogel 	ixl_add_sysctls_eth_stats(ctx, vsi_list, vsi_stats);
293361ae650dSJack F Vogel 
293461ae650dSJack F Vogel 	/* Queue statistics */
293561ae650dSJack F Vogel 	for (int q = 0; q < vsi->num_queues; q++) {
293661ae650dSJack F Vogel 		snprintf(queue_namebuf, QUEUE_NAME_LEN, "que%d", q);
293761ae650dSJack F Vogel 		queue_node = SYSCTL_ADD_NODE(ctx, vsi_list, OID_AUTO, queue_namebuf,
293861ae650dSJack F Vogel 					     CTLFLAG_RD, NULL, "Queue #");
293961ae650dSJack F Vogel 		queue_list = SYSCTL_CHILDREN(queue_node);
294061ae650dSJack F Vogel 
294161ae650dSJack F Vogel 		txr = &(queues[q].txr);
294261ae650dSJack F Vogel 		rxr = &(queues[q].rxr);
294361ae650dSJack F Vogel 
294461ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "mbuf_defrag_failed",
294561ae650dSJack F Vogel 				CTLFLAG_RD, &(queues[q].mbuf_defrag_failed),
294661ae650dSJack F Vogel 				"m_defrag() failed");
294761ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "dropped",
294861ae650dSJack F Vogel 				CTLFLAG_RD, &(queues[q].dropped_pkts),
294961ae650dSJack F Vogel 				"Driver dropped packets");
295061ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
295161ae650dSJack F Vogel 				CTLFLAG_RD, &(queues[q].irqs),
295261ae650dSJack F Vogel 				"irqs on this queue");
295361ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso_tx",
295461ae650dSJack F Vogel 				CTLFLAG_RD, &(queues[q].tso),
295561ae650dSJack F Vogel 				"TSO");
295661ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_dma_setup",
295761ae650dSJack F Vogel 				CTLFLAG_RD, &(queues[q].tx_dma_setup),
295861ae650dSJack F Vogel 				"Driver tx dma failure in xmit");
295961ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "no_desc_avail",
296061ae650dSJack F Vogel 				CTLFLAG_RD, &(txr->no_desc),
296161ae650dSJack F Vogel 				"Queue No Descriptor Available");
296261ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_packets",
296361ae650dSJack F Vogel 				CTLFLAG_RD, &(txr->total_packets),
296461ae650dSJack F Vogel 				"Queue Packets Transmitted");
296561ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_bytes",
296661ae650dSJack F Vogel 				CTLFLAG_RD, &(txr->tx_bytes),
296761ae650dSJack F Vogel 				"Queue Bytes Transmitted");
296861ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_packets",
296961ae650dSJack F Vogel 				CTLFLAG_RD, &(rxr->rx_packets),
297061ae650dSJack F Vogel 				"Queue Packets Received");
297161ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_bytes",
297261ae650dSJack F Vogel 				CTLFLAG_RD, &(rxr->rx_bytes),
297361ae650dSJack F Vogel 				"Queue Bytes Received");
297461ae650dSJack F Vogel 	}
297561ae650dSJack F Vogel 
297661ae650dSJack F Vogel 	/* MAC stats */
297761ae650dSJack F Vogel 	ixl_add_sysctls_mac_stats(ctx, child, pf_stats);
297861ae650dSJack F Vogel }
297961ae650dSJack F Vogel 
298061ae650dSJack F Vogel static void
298161ae650dSJack F Vogel ixl_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
298261ae650dSJack F Vogel 	struct sysctl_oid_list *child,
298361ae650dSJack F Vogel 	struct i40e_eth_stats *eth_stats)
298461ae650dSJack F Vogel {
298561ae650dSJack F Vogel 	struct ixl_sysctl_info ctls[] =
298661ae650dSJack F Vogel 	{
298761ae650dSJack F Vogel 		{&eth_stats->rx_bytes, "good_octets_rcvd", "Good Octets Received"},
298861ae650dSJack F Vogel 		{&eth_stats->rx_unicast, "ucast_pkts_rcvd",
298961ae650dSJack F Vogel 			"Unicast Packets Received"},
299061ae650dSJack F Vogel 		{&eth_stats->rx_multicast, "mcast_pkts_rcvd",
299161ae650dSJack F Vogel 			"Multicast Packets Received"},
299261ae650dSJack F Vogel 		{&eth_stats->rx_broadcast, "bcast_pkts_rcvd",
299361ae650dSJack F Vogel 			"Broadcast Packets Received"},
299461ae650dSJack F Vogel 		{&eth_stats->rx_discards, "rx_discards", "Discarded RX packets"},
299561ae650dSJack F Vogel 		{&eth_stats->tx_bytes, "good_octets_txd", "Good Octets Transmitted"},
299661ae650dSJack F Vogel 		{&eth_stats->tx_unicast, "ucast_pkts_txd", "Unicast Packets Transmitted"},
299761ae650dSJack F Vogel 		{&eth_stats->tx_multicast, "mcast_pkts_txd",
299861ae650dSJack F Vogel 			"Multicast Packets Transmitted"},
299961ae650dSJack F Vogel 		{&eth_stats->tx_broadcast, "bcast_pkts_txd",
300061ae650dSJack F Vogel 			"Broadcast Packets Transmitted"},
300161ae650dSJack F Vogel 		{&eth_stats->tx_discards, "tx_discards", "Discarded TX packets"},
300261ae650dSJack F Vogel 		// end
300361ae650dSJack F Vogel 		{0,0,0}
300461ae650dSJack F Vogel 	};
300561ae650dSJack F Vogel 
300661ae650dSJack F Vogel 	struct ixl_sysctl_info *entry = ctls;
300761ae650dSJack F Vogel 	while (entry->stat != 0)
300861ae650dSJack F Vogel 	{
300961ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, entry->name,
301061ae650dSJack F Vogel 				CTLFLAG_RD, entry->stat,
301161ae650dSJack F Vogel 				entry->description);
301261ae650dSJack F Vogel 		entry++;
301361ae650dSJack F Vogel 	}
301461ae650dSJack F Vogel }
301561ae650dSJack F Vogel 
301661ae650dSJack F Vogel static void
301761ae650dSJack F Vogel ixl_add_sysctls_mac_stats(struct sysctl_ctx_list *ctx,
301861ae650dSJack F Vogel 	struct sysctl_oid_list *child,
301961ae650dSJack F Vogel 	struct i40e_hw_port_stats *stats)
302061ae650dSJack F Vogel {
302161ae650dSJack F Vogel 	struct sysctl_oid *stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac",
302261ae650dSJack F Vogel 				    CTLFLAG_RD, NULL, "Mac Statistics");
302361ae650dSJack F Vogel 	struct sysctl_oid_list *stat_list = SYSCTL_CHILDREN(stat_node);
302461ae650dSJack F Vogel 
302561ae650dSJack F Vogel 	struct i40e_eth_stats *eth_stats = &stats->eth;
302661ae650dSJack F Vogel 	ixl_add_sysctls_eth_stats(ctx, stat_list, eth_stats);
302761ae650dSJack F Vogel 
302861ae650dSJack F Vogel 	struct ixl_sysctl_info ctls[] =
302961ae650dSJack F Vogel 	{
303061ae650dSJack F Vogel 		{&stats->crc_errors, "crc_errors", "CRC Errors"},
303161ae650dSJack F Vogel 		{&stats->illegal_bytes, "illegal_bytes", "Illegal Byte Errors"},
303261ae650dSJack F Vogel 		{&stats->mac_local_faults, "local_faults", "MAC Local Faults"},
303361ae650dSJack F Vogel 		{&stats->mac_remote_faults, "remote_faults", "MAC Remote Faults"},
303461ae650dSJack F Vogel 		{&stats->rx_length_errors, "rx_length_errors", "Receive Length Errors"},
303561ae650dSJack F Vogel 		/* Packet Reception Stats */
303661ae650dSJack F Vogel 		{&stats->rx_size_64, "rx_frames_64", "64 byte frames received"},
303761ae650dSJack F Vogel 		{&stats->rx_size_127, "rx_frames_65_127", "65-127 byte frames received"},
303861ae650dSJack F Vogel 		{&stats->rx_size_255, "rx_frames_128_255", "128-255 byte frames received"},
303961ae650dSJack F Vogel 		{&stats->rx_size_511, "rx_frames_256_511", "256-511 byte frames received"},
304061ae650dSJack F Vogel 		{&stats->rx_size_1023, "rx_frames_512_1023", "512-1023 byte frames received"},
304161ae650dSJack F Vogel 		{&stats->rx_size_1522, "rx_frames_1024_1522", "1024-1522 byte frames received"},
304261ae650dSJack F Vogel 		{&stats->rx_size_big, "rx_frames_big", "1523-9522 byte frames received"},
304361ae650dSJack F Vogel 		{&stats->rx_undersize, "rx_undersize", "Undersized packets received"},
304461ae650dSJack F Vogel 		{&stats->rx_fragments, "rx_fragmented", "Fragmented packets received"},
304561ae650dSJack F Vogel 		{&stats->rx_oversize, "rx_oversized", "Oversized packets received"},
304661ae650dSJack F Vogel 		{&stats->rx_jabber, "rx_jabber", "Received Jabber"},
304761ae650dSJack F Vogel 		{&stats->checksum_error, "checksum_errors", "Checksum Errors"},
304861ae650dSJack F Vogel 		/* Packet Transmission Stats */
304961ae650dSJack F Vogel 		{&stats->tx_size_64, "tx_frames_64", "64 byte frames transmitted"},
305061ae650dSJack F Vogel 		{&stats->tx_size_127, "tx_frames_65_127", "65-127 byte frames transmitted"},
305161ae650dSJack F Vogel 		{&stats->tx_size_255, "tx_frames_128_255", "128-255 byte frames transmitted"},
305261ae650dSJack F Vogel 		{&stats->tx_size_511, "tx_frames_256_511", "256-511 byte frames transmitted"},
305361ae650dSJack F Vogel 		{&stats->tx_size_1023, "tx_frames_512_1023", "512-1023 byte frames transmitted"},
305461ae650dSJack F Vogel 		{&stats->tx_size_1522, "tx_frames_1024_1522", "1024-1522 byte frames transmitted"},
305561ae650dSJack F Vogel 		{&stats->tx_size_big, "tx_frames_big", "1523-9522 byte frames transmitted"},
305661ae650dSJack F Vogel 		/* Flow control */
305761ae650dSJack F Vogel 		{&stats->link_xon_tx, "xon_txd", "Link XON transmitted"},
305861ae650dSJack F Vogel 		{&stats->link_xon_rx, "xon_recvd", "Link XON received"},
305961ae650dSJack F Vogel 		{&stats->link_xoff_tx, "xoff_txd", "Link XOFF transmitted"},
306061ae650dSJack F Vogel 		{&stats->link_xoff_rx, "xoff_recvd", "Link XOFF received"},
306161ae650dSJack F Vogel 		/* End */
306261ae650dSJack F Vogel 		{0,0,0}
306361ae650dSJack F Vogel 	};
306461ae650dSJack F Vogel 
306561ae650dSJack F Vogel 	struct ixl_sysctl_info *entry = ctls;
306661ae650dSJack F Vogel 	while (entry->stat != 0)
306761ae650dSJack F Vogel 	{
306861ae650dSJack F Vogel 		SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, entry->name,
306961ae650dSJack F Vogel 				CTLFLAG_RD, entry->stat,
307061ae650dSJack F Vogel 				entry->description);
307161ae650dSJack F Vogel 		entry++;
307261ae650dSJack F Vogel 	}
307361ae650dSJack F Vogel }
307461ae650dSJack F Vogel 
307561ae650dSJack F Vogel /*
307661ae650dSJack F Vogel ** ixl_config_rss - setup RSS
307761ae650dSJack F Vogel **  - note this is done for the single vsi
307861ae650dSJack F Vogel */
307961ae650dSJack F Vogel static void ixl_config_rss(struct ixl_vsi *vsi)
308061ae650dSJack F Vogel {
308161ae650dSJack F Vogel 	struct ixl_pf	*pf = (struct ixl_pf *)vsi->back;
308261ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
308361ae650dSJack F Vogel 	u32		lut = 0;
308461ae650dSJack F Vogel 	u64		set_hena, hena;
308561ae650dSJack F Vogel 	int		i, j;
308661ae650dSJack F Vogel 
308761ae650dSJack F Vogel 	static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
308861ae650dSJack F Vogel 	    0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
308961ae650dSJack F Vogel 	    0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
309061ae650dSJack F Vogel 	    0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
309161ae650dSJack F Vogel 
309261ae650dSJack F Vogel 	/* Fill out hash function seed */
309361ae650dSJack F Vogel 	for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
309461ae650dSJack F Vogel                 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
309561ae650dSJack F Vogel 
309661ae650dSJack F Vogel 	/* Enable PCTYPES for RSS: */
309761ae650dSJack F Vogel 	set_hena =
309861ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
309961ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
310061ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |
310161ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
310261ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4) |
310361ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
310461ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
310561ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |
310661ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
310761ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6) |
310861ae650dSJack F Vogel 		((u64)1 << I40E_FILTER_PCTYPE_L2_PAYLOAD);
310961ae650dSJack F Vogel 
311061ae650dSJack F Vogel 	hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
311161ae650dSJack F Vogel 	    ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
311261ae650dSJack F Vogel 	hena |= set_hena;
311361ae650dSJack F Vogel 	wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
311461ae650dSJack F Vogel 	wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
311561ae650dSJack F Vogel 
311661ae650dSJack F Vogel 	/* Populate the LUT with max no. of queues in round robin fashion */
311761ae650dSJack F Vogel 	for (i = j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
311861ae650dSJack F Vogel 		if (j == vsi->num_queues)
311961ae650dSJack F Vogel 			j = 0;
312061ae650dSJack F Vogel 		/* lut = 4-byte sliding window of 4 lut entries */
312161ae650dSJack F Vogel 		lut = (lut << 8) | (j &
312261ae650dSJack F Vogel 		    ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
312361ae650dSJack F Vogel 		/* On i = 3, we have 4 entries in lut; write to the register */
312461ae650dSJack F Vogel 		if ((i & 3) == 3)
312561ae650dSJack F Vogel 			wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
312661ae650dSJack F Vogel 	}
312761ae650dSJack F Vogel 	ixl_flush(hw);
312861ae650dSJack F Vogel }
312961ae650dSJack F Vogel 
313061ae650dSJack F Vogel 
313161ae650dSJack F Vogel /*
313261ae650dSJack F Vogel ** This routine is run via an vlan config EVENT,
313361ae650dSJack F Vogel ** it enables us to use the HW Filter table since
313461ae650dSJack F Vogel ** we can get the vlan id. This just creates the
313561ae650dSJack F Vogel ** entry in the soft version of the VFTA, init will
313661ae650dSJack F Vogel ** repopulate the real table.
313761ae650dSJack F Vogel */
313861ae650dSJack F Vogel static void
313961ae650dSJack F Vogel ixl_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
314061ae650dSJack F Vogel {
314161ae650dSJack F Vogel 	struct ixl_vsi	*vsi = ifp->if_softc;
314261ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
314361ae650dSJack F Vogel 	struct ixl_pf	*pf = (struct ixl_pf *)vsi->back;
314461ae650dSJack F Vogel 
314561ae650dSJack F Vogel 	if (ifp->if_softc !=  arg)   /* Not our event */
314661ae650dSJack F Vogel 		return;
314761ae650dSJack F Vogel 
314861ae650dSJack F Vogel 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
314961ae650dSJack F Vogel 		return;
315061ae650dSJack F Vogel 
315161ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
315261ae650dSJack F Vogel 	++vsi->num_vlans;
315361ae650dSJack F Vogel 	ixl_add_filter(vsi, hw->mac.addr, vtag);
315461ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
315561ae650dSJack F Vogel }
315661ae650dSJack F Vogel 
315761ae650dSJack F Vogel /*
315861ae650dSJack F Vogel ** This routine is run via an vlan
315961ae650dSJack F Vogel ** unconfig EVENT, remove our entry
316061ae650dSJack F Vogel ** in the soft vfta.
316161ae650dSJack F Vogel */
316261ae650dSJack F Vogel static void
316361ae650dSJack F Vogel ixl_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
316461ae650dSJack F Vogel {
316561ae650dSJack F Vogel 	struct ixl_vsi	*vsi = ifp->if_softc;
316661ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
316761ae650dSJack F Vogel 	struct ixl_pf	*pf = (struct ixl_pf *)vsi->back;
316861ae650dSJack F Vogel 
316961ae650dSJack F Vogel 	if (ifp->if_softc !=  arg)
317061ae650dSJack F Vogel 		return;
317161ae650dSJack F Vogel 
317261ae650dSJack F Vogel 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
317361ae650dSJack F Vogel 		return;
317461ae650dSJack F Vogel 
317561ae650dSJack F Vogel 	IXL_PF_LOCK(pf);
317661ae650dSJack F Vogel 	--vsi->num_vlans;
317761ae650dSJack F Vogel 	ixl_del_filter(vsi, hw->mac.addr, vtag);
317861ae650dSJack F Vogel 	IXL_PF_UNLOCK(pf);
317961ae650dSJack F Vogel }
318061ae650dSJack F Vogel 
318161ae650dSJack F Vogel /*
318261ae650dSJack F Vogel ** This routine updates vlan filters, called by init
318361ae650dSJack F Vogel ** it scans the filter table and then updates the hw
318461ae650dSJack F Vogel ** after a soft reset.
318561ae650dSJack F Vogel */
318661ae650dSJack F Vogel static void
318761ae650dSJack F Vogel ixl_setup_vlan_filters(struct ixl_vsi *vsi)
318861ae650dSJack F Vogel {
318961ae650dSJack F Vogel 	struct ixl_mac_filter	*f;
319061ae650dSJack F Vogel 	int			cnt = 0, flags;
319161ae650dSJack F Vogel 
319261ae650dSJack F Vogel 	if (vsi->num_vlans == 0)
319361ae650dSJack F Vogel 		return;
319461ae650dSJack F Vogel 	/*
319561ae650dSJack F Vogel 	** Scan the filter list for vlan entries,
319661ae650dSJack F Vogel 	** mark them for addition and then call
319761ae650dSJack F Vogel 	** for the AQ update.
319861ae650dSJack F Vogel 	*/
319961ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
320061ae650dSJack F Vogel 		if (f->flags & IXL_FILTER_VLAN) {
320161ae650dSJack F Vogel 			f->flags |=
320261ae650dSJack F Vogel 			    (IXL_FILTER_ADD |
320361ae650dSJack F Vogel 			    IXL_FILTER_USED);
320461ae650dSJack F Vogel 			cnt++;
320561ae650dSJack F Vogel 		}
320661ae650dSJack F Vogel 	}
320761ae650dSJack F Vogel 	if (cnt == 0) {
320861ae650dSJack F Vogel 		printf("setup vlan: no filters found!\n");
320961ae650dSJack F Vogel 		return;
321061ae650dSJack F Vogel 	}
321161ae650dSJack F Vogel 	flags = IXL_FILTER_VLAN;
321261ae650dSJack F Vogel 	flags |= (IXL_FILTER_ADD | IXL_FILTER_USED);
321361ae650dSJack F Vogel 	ixl_add_hw_filters(vsi, flags, cnt);
321461ae650dSJack F Vogel 	return;
321561ae650dSJack F Vogel }
321661ae650dSJack F Vogel 
321761ae650dSJack F Vogel /*
321861ae650dSJack F Vogel ** Initialize filter list and add filters that the hardware
321961ae650dSJack F Vogel ** needs to know about.
322061ae650dSJack F Vogel */
322161ae650dSJack F Vogel static void
322261ae650dSJack F Vogel ixl_init_filters(struct ixl_vsi *vsi)
322361ae650dSJack F Vogel {
322461ae650dSJack F Vogel 	/* Add broadcast address */
322561ae650dSJack F Vogel 	u8 bc[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
322661ae650dSJack F Vogel 	ixl_add_filter(vsi, bc, IXL_VLAN_ANY);
322761ae650dSJack F Vogel }
322861ae650dSJack F Vogel 
322961ae650dSJack F Vogel /*
323061ae650dSJack F Vogel ** This routine adds mulicast filters
323161ae650dSJack F Vogel */
323261ae650dSJack F Vogel static void
323361ae650dSJack F Vogel ixl_add_mc_filter(struct ixl_vsi *vsi, u8 *macaddr)
323461ae650dSJack F Vogel {
323561ae650dSJack F Vogel 	struct ixl_mac_filter *f;
323661ae650dSJack F Vogel 
323761ae650dSJack F Vogel 	/* Does one already exist */
323861ae650dSJack F Vogel 	f = ixl_find_filter(vsi, macaddr, IXL_VLAN_ANY);
323961ae650dSJack F Vogel 	if (f != NULL)
324061ae650dSJack F Vogel 		return;
324161ae650dSJack F Vogel 
324261ae650dSJack F Vogel 	f = ixl_get_filter(vsi);
324361ae650dSJack F Vogel 	if (f == NULL) {
324461ae650dSJack F Vogel 		printf("WARNING: no filter available!!\n");
324561ae650dSJack F Vogel 		return;
324661ae650dSJack F Vogel 	}
324761ae650dSJack F Vogel 	bcopy(macaddr, f->macaddr, ETHER_ADDR_LEN);
324861ae650dSJack F Vogel 	f->vlan = IXL_VLAN_ANY;
324961ae650dSJack F Vogel 	f->flags |= (IXL_FILTER_ADD | IXL_FILTER_USED
325061ae650dSJack F Vogel 	    | IXL_FILTER_MC);
325161ae650dSJack F Vogel 
325261ae650dSJack F Vogel 	return;
325361ae650dSJack F Vogel }
325461ae650dSJack F Vogel 
325561ae650dSJack F Vogel /*
325661ae650dSJack F Vogel ** This routine adds macvlan filters
325761ae650dSJack F Vogel */
325861ae650dSJack F Vogel static void
325961ae650dSJack F Vogel ixl_add_filter(struct ixl_vsi *vsi, u8 *macaddr, s16 vlan)
326061ae650dSJack F Vogel {
326161ae650dSJack F Vogel 	struct ixl_mac_filter	*f, *tmp;
326261ae650dSJack F Vogel 	device_t		dev = vsi->dev;
326361ae650dSJack F Vogel 
326461ae650dSJack F Vogel 	DEBUGOUT("ixl_add_filter: begin");
326561ae650dSJack F Vogel 
326661ae650dSJack F Vogel 	/* Does one already exist */
326761ae650dSJack F Vogel 	f = ixl_find_filter(vsi, macaddr, vlan);
326861ae650dSJack F Vogel 	if (f != NULL)
326961ae650dSJack F Vogel 		return;
327061ae650dSJack F Vogel 	/*
327161ae650dSJack F Vogel 	** Is this the first vlan being registered, if so we
327261ae650dSJack F Vogel 	** need to remove the ANY filter that indicates we are
327361ae650dSJack F Vogel 	** not in a vlan, and replace that with a 0 filter.
327461ae650dSJack F Vogel 	*/
327561ae650dSJack F Vogel 	if ((vlan != IXL_VLAN_ANY) && (vsi->num_vlans == 1)) {
327661ae650dSJack F Vogel 		tmp = ixl_find_filter(vsi, macaddr, IXL_VLAN_ANY);
327761ae650dSJack F Vogel 		if (tmp != NULL) {
327861ae650dSJack F Vogel 			ixl_del_filter(vsi, macaddr, IXL_VLAN_ANY);
327961ae650dSJack F Vogel 			ixl_add_filter(vsi, macaddr, 0);
328061ae650dSJack F Vogel 		}
328161ae650dSJack F Vogel 	}
328261ae650dSJack F Vogel 
328361ae650dSJack F Vogel 	f = ixl_get_filter(vsi);
328461ae650dSJack F Vogel 	if (f == NULL) {
328561ae650dSJack F Vogel 		device_printf(dev, "WARNING: no filter available!!\n");
328661ae650dSJack F Vogel 		return;
328761ae650dSJack F Vogel 	}
328861ae650dSJack F Vogel 	bcopy(macaddr, f->macaddr, ETHER_ADDR_LEN);
328961ae650dSJack F Vogel 	f->vlan = vlan;
329061ae650dSJack F Vogel 	f->flags |= (IXL_FILTER_ADD | IXL_FILTER_USED);
329161ae650dSJack F Vogel 	if (f->vlan != IXL_VLAN_ANY)
329261ae650dSJack F Vogel 		f->flags |= IXL_FILTER_VLAN;
329361ae650dSJack F Vogel 
329461ae650dSJack F Vogel 	ixl_add_hw_filters(vsi, f->flags, 1);
329561ae650dSJack F Vogel 	return;
329661ae650dSJack F Vogel }
329761ae650dSJack F Vogel 
329861ae650dSJack F Vogel static void
329961ae650dSJack F Vogel ixl_del_filter(struct ixl_vsi *vsi, u8 *macaddr, s16 vlan)
330061ae650dSJack F Vogel {
330161ae650dSJack F Vogel 	struct ixl_mac_filter *f;
330261ae650dSJack F Vogel 
330361ae650dSJack F Vogel 	f = ixl_find_filter(vsi, macaddr, vlan);
330461ae650dSJack F Vogel 	if (f == NULL)
330561ae650dSJack F Vogel 		return;
330661ae650dSJack F Vogel 
330761ae650dSJack F Vogel 	f->flags |= IXL_FILTER_DEL;
330861ae650dSJack F Vogel 	ixl_del_hw_filters(vsi, 1);
330961ae650dSJack F Vogel 
331061ae650dSJack F Vogel 	/* Check if this is the last vlan removal */
331161ae650dSJack F Vogel 	if (vlan != IXL_VLAN_ANY && vsi->num_vlans == 0) {
331261ae650dSJack F Vogel 		/* Switch back to a non-vlan filter */
331361ae650dSJack F Vogel 		ixl_del_filter(vsi, macaddr, 0);
331461ae650dSJack F Vogel 		ixl_add_filter(vsi, macaddr, IXL_VLAN_ANY);
331561ae650dSJack F Vogel 	}
331661ae650dSJack F Vogel 	return;
331761ae650dSJack F Vogel }
331861ae650dSJack F Vogel 
331961ae650dSJack F Vogel /*
332061ae650dSJack F Vogel ** Find the filter with both matching mac addr and vlan id
332161ae650dSJack F Vogel */
332261ae650dSJack F Vogel static struct ixl_mac_filter *
332361ae650dSJack F Vogel ixl_find_filter(struct ixl_vsi *vsi, u8 *macaddr, s16 vlan)
332461ae650dSJack F Vogel {
332561ae650dSJack F Vogel 	struct ixl_mac_filter	*f;
332661ae650dSJack F Vogel 	bool			match = FALSE;
332761ae650dSJack F Vogel 
332861ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
332961ae650dSJack F Vogel 		if (!cmp_etheraddr(f->macaddr, macaddr))
333061ae650dSJack F Vogel 			continue;
333161ae650dSJack F Vogel 		if (f->vlan == vlan) {
333261ae650dSJack F Vogel 			match = TRUE;
333361ae650dSJack F Vogel 			break;
333461ae650dSJack F Vogel 		}
333561ae650dSJack F Vogel 	}
333661ae650dSJack F Vogel 
333761ae650dSJack F Vogel 	if (!match)
333861ae650dSJack F Vogel 		f = NULL;
333961ae650dSJack F Vogel 	return (f);
334061ae650dSJack F Vogel }
334161ae650dSJack F Vogel 
334261ae650dSJack F Vogel /*
334361ae650dSJack F Vogel ** This routine takes additions to the vsi filter
334461ae650dSJack F Vogel ** table and creates an Admin Queue call to create
334561ae650dSJack F Vogel ** the filters in the hardware.
334661ae650dSJack F Vogel */
334761ae650dSJack F Vogel static void
334861ae650dSJack F Vogel ixl_add_hw_filters(struct ixl_vsi *vsi, int flags, int cnt)
334961ae650dSJack F Vogel {
335061ae650dSJack F Vogel 	struct i40e_aqc_add_macvlan_element_data *a, *b;
335161ae650dSJack F Vogel 	struct ixl_mac_filter	*f;
335261ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
335361ae650dSJack F Vogel 	device_t	dev = vsi->dev;
335461ae650dSJack F Vogel 	int		err, j = 0;
335561ae650dSJack F Vogel 
335661ae650dSJack F Vogel 	a = malloc(sizeof(struct i40e_aqc_add_macvlan_element_data) * cnt,
335761ae650dSJack F Vogel 	    M_DEVBUF, M_NOWAIT | M_ZERO);
335861ae650dSJack F Vogel 	if (a == NULL) {
335961ae650dSJack F Vogel 		device_printf(dev, "add hw filter failed to get memory\n");
336061ae650dSJack F Vogel 		return;
336161ae650dSJack F Vogel 	}
336261ae650dSJack F Vogel 
336361ae650dSJack F Vogel 	/*
336461ae650dSJack F Vogel 	** Scan the filter list, each time we find one
336561ae650dSJack F Vogel 	** we add it to the admin queue array and turn off
336661ae650dSJack F Vogel 	** the add bit.
336761ae650dSJack F Vogel 	*/
336861ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
336961ae650dSJack F Vogel 		if (f->flags == flags) {
337061ae650dSJack F Vogel 			b = &a[j]; // a pox on fvl long names :)
337161ae650dSJack F Vogel 			bcopy(f->macaddr, b->mac_addr, ETHER_ADDR_LEN);
337261ae650dSJack F Vogel 			b->vlan_tag =
337361ae650dSJack F Vogel 			    (f->vlan == IXL_VLAN_ANY ? 0 : f->vlan);
337461ae650dSJack F Vogel 			b->flags = I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
337561ae650dSJack F Vogel 			f->flags &= ~IXL_FILTER_ADD;
337661ae650dSJack F Vogel 			j++;
337761ae650dSJack F Vogel 		}
337861ae650dSJack F Vogel 		if (j == cnt)
337961ae650dSJack F Vogel 			break;
338061ae650dSJack F Vogel 	}
338161ae650dSJack F Vogel 	if (j > 0) {
338261ae650dSJack F Vogel 		err = i40e_aq_add_macvlan(hw, vsi->seid, a, j, NULL);
338361ae650dSJack F Vogel 		if (err)
338461ae650dSJack F Vogel 			device_printf(dev, "aq_add_macvlan failure %d\n",
338561ae650dSJack F Vogel 			    hw->aq.asq_last_status);
338661ae650dSJack F Vogel 		else
338761ae650dSJack F Vogel 			vsi->hw_filters_add += j;
338861ae650dSJack F Vogel 	}
338961ae650dSJack F Vogel 	free(a, M_DEVBUF);
339061ae650dSJack F Vogel 	return;
339161ae650dSJack F Vogel }
339261ae650dSJack F Vogel 
339361ae650dSJack F Vogel /*
339461ae650dSJack F Vogel ** This routine takes removals in the vsi filter
339561ae650dSJack F Vogel ** table and creates an Admin Queue call to delete
339661ae650dSJack F Vogel ** the filters in the hardware.
339761ae650dSJack F Vogel */
339861ae650dSJack F Vogel static void
339961ae650dSJack F Vogel ixl_del_hw_filters(struct ixl_vsi *vsi, int cnt)
340061ae650dSJack F Vogel {
340161ae650dSJack F Vogel 	struct i40e_aqc_remove_macvlan_element_data *d, *e;
340261ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
340361ae650dSJack F Vogel 	device_t		dev = vsi->dev;
340461ae650dSJack F Vogel 	struct ixl_mac_filter	*f, *f_temp;
340561ae650dSJack F Vogel 	int			err, j = 0;
340661ae650dSJack F Vogel 
340761ae650dSJack F Vogel 	DEBUGOUT("ixl_del_hw_filters: begin\n");
340861ae650dSJack F Vogel 
340961ae650dSJack F Vogel 	d = malloc(sizeof(struct i40e_aqc_remove_macvlan_element_data) * cnt,
341061ae650dSJack F Vogel 	    M_DEVBUF, M_NOWAIT | M_ZERO);
341161ae650dSJack F Vogel 	if (d == NULL) {
341261ae650dSJack F Vogel 		printf("del hw filter failed to get memory\n");
341361ae650dSJack F Vogel 		return;
341461ae650dSJack F Vogel 	}
341561ae650dSJack F Vogel 
341661ae650dSJack F Vogel 	SLIST_FOREACH_SAFE(f, &vsi->ftl, next, f_temp) {
341761ae650dSJack F Vogel 		if (f->flags & IXL_FILTER_DEL) {
341861ae650dSJack F Vogel 			e = &d[j]; // a pox on fvl long names :)
341961ae650dSJack F Vogel 			bcopy(f->macaddr, e->mac_addr, ETHER_ADDR_LEN);
342061ae650dSJack F Vogel 			e->vlan_tag = (f->vlan == IXL_VLAN_ANY ? 0 : f->vlan);
342161ae650dSJack F Vogel 			e->flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
342261ae650dSJack F Vogel 			/* delete entry from vsi list */
342361ae650dSJack F Vogel 			SLIST_REMOVE(&vsi->ftl, f, ixl_mac_filter, next);
342461ae650dSJack F Vogel 			free(f, M_DEVBUF);
342561ae650dSJack F Vogel 			j++;
342661ae650dSJack F Vogel 		}
342761ae650dSJack F Vogel 		if (j == cnt)
342861ae650dSJack F Vogel 			break;
342961ae650dSJack F Vogel 	}
343061ae650dSJack F Vogel 	if (j > 0) {
343161ae650dSJack F Vogel 		err = i40e_aq_remove_macvlan(hw, vsi->seid, d, j, NULL);
343261ae650dSJack F Vogel 		/* NOTE: returns ENOENT every time but seems to work fine,
343361ae650dSJack F Vogel 		   so we'll ignore that specific error. */
343461ae650dSJack F Vogel 		if (err && hw->aq.asq_last_status != I40E_AQ_RC_ENOENT) {
343561ae650dSJack F Vogel 			int sc = 0;
343661ae650dSJack F Vogel 			for (int i = 0; i < j; i++)
343761ae650dSJack F Vogel 				sc += (!d[i].error_code);
343861ae650dSJack F Vogel 			vsi->hw_filters_del += sc;
343961ae650dSJack F Vogel 			device_printf(dev,
344061ae650dSJack F Vogel 			    "Failed to remove %d/%d filters, aq error %d\n",
344161ae650dSJack F Vogel 			    j - sc, j, hw->aq.asq_last_status);
344261ae650dSJack F Vogel 		} else
344361ae650dSJack F Vogel 			vsi->hw_filters_del += j;
344461ae650dSJack F Vogel 	}
344561ae650dSJack F Vogel 	free(d, M_DEVBUF);
344661ae650dSJack F Vogel 
344761ae650dSJack F Vogel 	DEBUGOUT("ixl_del_hw_filters: end\n");
344861ae650dSJack F Vogel 	return;
344961ae650dSJack F Vogel }
345061ae650dSJack F Vogel 
345161ae650dSJack F Vogel 
345261ae650dSJack F Vogel static void
345361ae650dSJack F Vogel ixl_enable_rings(struct ixl_vsi *vsi)
345461ae650dSJack F Vogel {
345561ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
345661ae650dSJack F Vogel 	u32		reg;
345761ae650dSJack F Vogel 
345861ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++) {
345961ae650dSJack F Vogel 		i40e_pre_tx_queue_cfg(hw, i, TRUE);
346061ae650dSJack F Vogel 
346161ae650dSJack F Vogel 		reg = rd32(hw, I40E_QTX_ENA(i));
346261ae650dSJack F Vogel 		reg |= I40E_QTX_ENA_QENA_REQ_MASK |
346361ae650dSJack F Vogel 		    I40E_QTX_ENA_QENA_STAT_MASK;
346461ae650dSJack F Vogel 		wr32(hw, I40E_QTX_ENA(i), reg);
346561ae650dSJack F Vogel 		/* Verify the enable took */
346661ae650dSJack F Vogel 		for (int j = 0; j < 10; j++) {
346761ae650dSJack F Vogel 			reg = rd32(hw, I40E_QTX_ENA(i));
346861ae650dSJack F Vogel 			if (reg & I40E_QTX_ENA_QENA_STAT_MASK)
346961ae650dSJack F Vogel 				break;
347061ae650dSJack F Vogel 			i40e_msec_delay(10);
347161ae650dSJack F Vogel 		}
347261ae650dSJack F Vogel 		if ((reg & I40E_QTX_ENA_QENA_STAT_MASK) == 0)
347361ae650dSJack F Vogel 			printf("TX queue %d disabled!\n", i);
347461ae650dSJack F Vogel 
347561ae650dSJack F Vogel 		reg = rd32(hw, I40E_QRX_ENA(i));
347661ae650dSJack F Vogel 		reg |= I40E_QRX_ENA_QENA_REQ_MASK |
347761ae650dSJack F Vogel 		    I40E_QRX_ENA_QENA_STAT_MASK;
347861ae650dSJack F Vogel 		wr32(hw, I40E_QRX_ENA(i), reg);
347961ae650dSJack F Vogel 		/* Verify the enable took */
348061ae650dSJack F Vogel 		for (int j = 0; j < 10; j++) {
348161ae650dSJack F Vogel 			reg = rd32(hw, I40E_QRX_ENA(i));
348261ae650dSJack F Vogel 			if (reg & I40E_QRX_ENA_QENA_STAT_MASK)
348361ae650dSJack F Vogel 				break;
348461ae650dSJack F Vogel 			i40e_msec_delay(10);
348561ae650dSJack F Vogel 		}
348661ae650dSJack F Vogel 		if ((reg & I40E_QRX_ENA_QENA_STAT_MASK) == 0)
348761ae650dSJack F Vogel 			printf("RX queue %d disabled!\n", i);
348861ae650dSJack F Vogel 	}
348961ae650dSJack F Vogel }
349061ae650dSJack F Vogel 
349161ae650dSJack F Vogel static void
349261ae650dSJack F Vogel ixl_disable_rings(struct ixl_vsi *vsi)
349361ae650dSJack F Vogel {
349461ae650dSJack F Vogel 	struct i40e_hw	*hw = vsi->hw;
349561ae650dSJack F Vogel 	u32		reg;
349661ae650dSJack F Vogel 
349761ae650dSJack F Vogel 	for (int i = 0; i < vsi->num_queues; i++) {
349861ae650dSJack F Vogel 		i40e_pre_tx_queue_cfg(hw, i, FALSE);
349961ae650dSJack F Vogel 		i40e_usec_delay(500);
350061ae650dSJack F Vogel 
350161ae650dSJack F Vogel 		reg = rd32(hw, I40E_QTX_ENA(i));
350261ae650dSJack F Vogel 		reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
350361ae650dSJack F Vogel 		wr32(hw, I40E_QTX_ENA(i), reg);
350461ae650dSJack F Vogel 		/* Verify the disable took */
350561ae650dSJack F Vogel 		for (int j = 0; j < 10; j++) {
350661ae650dSJack F Vogel 			reg = rd32(hw, I40E_QTX_ENA(i));
350761ae650dSJack F Vogel 			if (!(reg & I40E_QTX_ENA_QENA_STAT_MASK))
350861ae650dSJack F Vogel 				break;
350961ae650dSJack F Vogel 			i40e_msec_delay(10);
351061ae650dSJack F Vogel 		}
351161ae650dSJack F Vogel 		if (reg & I40E_QTX_ENA_QENA_STAT_MASK)
351261ae650dSJack F Vogel 			printf("TX queue %d still enabled!\n", i);
351361ae650dSJack F Vogel 
351461ae650dSJack F Vogel 		reg = rd32(hw, I40E_QRX_ENA(i));
351561ae650dSJack F Vogel 		reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
351661ae650dSJack F Vogel 		wr32(hw, I40E_QRX_ENA(i), reg);
351761ae650dSJack F Vogel 		/* Verify the disable took */
351861ae650dSJack F Vogel 		for (int j = 0; j < 10; j++) {
351961ae650dSJack F Vogel 			reg = rd32(hw, I40E_QRX_ENA(i));
352061ae650dSJack F Vogel 			if (!(reg & I40E_QRX_ENA_QENA_STAT_MASK))
352161ae650dSJack F Vogel 				break;
352261ae650dSJack F Vogel 			i40e_msec_delay(10);
352361ae650dSJack F Vogel 		}
352461ae650dSJack F Vogel 		if (reg & I40E_QRX_ENA_QENA_STAT_MASK)
352561ae650dSJack F Vogel 			printf("RX queue %d still enabled!\n", i);
352661ae650dSJack F Vogel 	}
352761ae650dSJack F Vogel }
352861ae650dSJack F Vogel 
352961ae650dSJack F Vogel /**
353061ae650dSJack F Vogel  * ixl_handle_mdd_event
353161ae650dSJack F Vogel  *
353261ae650dSJack F Vogel  * Called from interrupt handler to identify possibly malicious vfs
353361ae650dSJack F Vogel  * (But also detects events from the PF, as well)
353461ae650dSJack F Vogel  **/
353561ae650dSJack F Vogel static void ixl_handle_mdd_event(struct ixl_pf *pf)
353661ae650dSJack F Vogel {
353761ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
353861ae650dSJack F Vogel 	device_t dev = pf->dev;
353961ae650dSJack F Vogel 	bool mdd_detected = false;
354061ae650dSJack F Vogel 	bool pf_mdd_detected = false;
354161ae650dSJack F Vogel 	u32 reg;
354261ae650dSJack F Vogel 
354361ae650dSJack F Vogel 	/* find what triggered the MDD event */
354461ae650dSJack F Vogel 	reg = rd32(hw, I40E_GL_MDET_TX);
354561ae650dSJack F Vogel 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
354661ae650dSJack F Vogel 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
354761ae650dSJack F Vogel 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
354861ae650dSJack F Vogel 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
354961ae650dSJack F Vogel 				I40E_GL_MDET_TX_EVENT_SHIFT;
355061ae650dSJack F Vogel 		u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
355161ae650dSJack F Vogel 				I40E_GL_MDET_TX_QUEUE_SHIFT;
355261ae650dSJack F Vogel 		device_printf(dev,
355361ae650dSJack F Vogel 			 "Malicious Driver Detection event 0x%02x"
355461ae650dSJack F Vogel 			 " on TX queue %d pf number 0x%02x\n",
355561ae650dSJack F Vogel 			 event, queue, pf_num);
355661ae650dSJack F Vogel 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
355761ae650dSJack F Vogel 		mdd_detected = true;
355861ae650dSJack F Vogel 	}
355961ae650dSJack F Vogel 	reg = rd32(hw, I40E_GL_MDET_RX);
356061ae650dSJack F Vogel 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
356161ae650dSJack F Vogel 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
356261ae650dSJack F Vogel 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
356361ae650dSJack F Vogel 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
356461ae650dSJack F Vogel 				I40E_GL_MDET_RX_EVENT_SHIFT;
356561ae650dSJack F Vogel 		u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
356661ae650dSJack F Vogel 				I40E_GL_MDET_RX_QUEUE_SHIFT;
356761ae650dSJack F Vogel 		device_printf(dev,
356861ae650dSJack F Vogel 			 "Malicious Driver Detection event 0x%02x"
356961ae650dSJack F Vogel 			 " on RX queue %d of function 0x%02x\n",
357061ae650dSJack F Vogel 			 event, queue, func);
357161ae650dSJack F Vogel 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
357261ae650dSJack F Vogel 		mdd_detected = true;
357361ae650dSJack F Vogel 	}
357461ae650dSJack F Vogel 
357561ae650dSJack F Vogel 	if (mdd_detected) {
357661ae650dSJack F Vogel 		reg = rd32(hw, I40E_PF_MDET_TX);
357761ae650dSJack F Vogel 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
357861ae650dSJack F Vogel 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
357961ae650dSJack F Vogel 			device_printf(dev,
358061ae650dSJack F Vogel 				 "MDD TX event is for this function 0x%08x",
358161ae650dSJack F Vogel 				 reg);
358261ae650dSJack F Vogel 			pf_mdd_detected = true;
358361ae650dSJack F Vogel 		}
358461ae650dSJack F Vogel 		reg = rd32(hw, I40E_PF_MDET_RX);
358561ae650dSJack F Vogel 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
358661ae650dSJack F Vogel 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
358761ae650dSJack F Vogel 			device_printf(dev,
358861ae650dSJack F Vogel 				 "MDD RX event is for this function 0x%08x",
358961ae650dSJack F Vogel 				 reg);
359061ae650dSJack F Vogel 			pf_mdd_detected = true;
359161ae650dSJack F Vogel 		}
359261ae650dSJack F Vogel 	}
359361ae650dSJack F Vogel 
359461ae650dSJack F Vogel 	/* re-enable mdd interrupt cause */
359561ae650dSJack F Vogel 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
359661ae650dSJack F Vogel 	reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
359761ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
359861ae650dSJack F Vogel 	ixl_flush(hw);
359961ae650dSJack F Vogel }
360061ae650dSJack F Vogel 
360161ae650dSJack F Vogel static void
360261ae650dSJack F Vogel ixl_enable_intr(struct ixl_vsi *vsi)
360361ae650dSJack F Vogel {
360461ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
360561ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
360661ae650dSJack F Vogel 
360761ae650dSJack F Vogel 	if (ixl_enable_msix) {
360861ae650dSJack F Vogel 		ixl_enable_adminq(hw);
360961ae650dSJack F Vogel 		for (int i = 0; i < vsi->num_queues; i++, que++)
361061ae650dSJack F Vogel 			ixl_enable_queue(hw, que->me);
361161ae650dSJack F Vogel 	} else
361261ae650dSJack F Vogel 		ixl_enable_legacy(hw);
361361ae650dSJack F Vogel }
361461ae650dSJack F Vogel 
361561ae650dSJack F Vogel static void
361661ae650dSJack F Vogel ixl_disable_intr(struct ixl_vsi *vsi)
361761ae650dSJack F Vogel {
361861ae650dSJack F Vogel 	struct i40e_hw		*hw = vsi->hw;
361961ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
362061ae650dSJack F Vogel 
362161ae650dSJack F Vogel 	if (ixl_enable_msix) {
362261ae650dSJack F Vogel 		ixl_disable_adminq(hw);
362361ae650dSJack F Vogel 		for (int i = 0; i < vsi->num_queues; i++, que++)
362461ae650dSJack F Vogel 			ixl_disable_queue(hw, que->me);
362561ae650dSJack F Vogel 	} else
362661ae650dSJack F Vogel 		ixl_disable_legacy(hw);
362761ae650dSJack F Vogel }
362861ae650dSJack F Vogel 
362961ae650dSJack F Vogel static void
363061ae650dSJack F Vogel ixl_enable_adminq(struct i40e_hw *hw)
363161ae650dSJack F Vogel {
363261ae650dSJack F Vogel 	u32		reg;
363361ae650dSJack F Vogel 
363461ae650dSJack F Vogel 	reg = I40E_PFINT_DYN_CTL0_INTENA_MASK |
363561ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
363661ae650dSJack F Vogel 	    (IXL_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
363761ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
363861ae650dSJack F Vogel 	ixl_flush(hw);
363961ae650dSJack F Vogel 	return;
364061ae650dSJack F Vogel }
364161ae650dSJack F Vogel 
364261ae650dSJack F Vogel static void
364361ae650dSJack F Vogel ixl_disable_adminq(struct i40e_hw *hw)
364461ae650dSJack F Vogel {
364561ae650dSJack F Vogel 	u32		reg;
364661ae650dSJack F Vogel 
364761ae650dSJack F Vogel 	reg = IXL_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT;
364861ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
364961ae650dSJack F Vogel 
365061ae650dSJack F Vogel 	return;
365161ae650dSJack F Vogel }
365261ae650dSJack F Vogel 
365361ae650dSJack F Vogel static void
365461ae650dSJack F Vogel ixl_enable_queue(struct i40e_hw *hw, int id)
365561ae650dSJack F Vogel {
365661ae650dSJack F Vogel 	u32		reg;
365761ae650dSJack F Vogel 
365861ae650dSJack F Vogel 	reg = I40E_PFINT_DYN_CTLN_INTENA_MASK |
365961ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
366061ae650dSJack F Vogel 	    (IXL_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
366161ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTLN(id), reg);
366261ae650dSJack F Vogel }
366361ae650dSJack F Vogel 
366461ae650dSJack F Vogel static void
366561ae650dSJack F Vogel ixl_disable_queue(struct i40e_hw *hw, int id)
366661ae650dSJack F Vogel {
366761ae650dSJack F Vogel 	u32		reg;
366861ae650dSJack F Vogel 
366961ae650dSJack F Vogel 	reg = IXL_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
367061ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTLN(id), reg);
367161ae650dSJack F Vogel 
367261ae650dSJack F Vogel 	return;
367361ae650dSJack F Vogel }
367461ae650dSJack F Vogel 
367561ae650dSJack F Vogel static void
367661ae650dSJack F Vogel ixl_enable_legacy(struct i40e_hw *hw)
367761ae650dSJack F Vogel {
367861ae650dSJack F Vogel 	u32		reg;
367961ae650dSJack F Vogel 	reg = I40E_PFINT_DYN_CTL0_INTENA_MASK |
368061ae650dSJack F Vogel 	    I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
368161ae650dSJack F Vogel 	    (IXL_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
368261ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
368361ae650dSJack F Vogel }
368461ae650dSJack F Vogel 
368561ae650dSJack F Vogel static void
368661ae650dSJack F Vogel ixl_disable_legacy(struct i40e_hw *hw)
368761ae650dSJack F Vogel {
368861ae650dSJack F Vogel 	u32		reg;
368961ae650dSJack F Vogel 
369061ae650dSJack F Vogel 	reg = IXL_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT;
369161ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_DYN_CTL0, reg);
369261ae650dSJack F Vogel 
369361ae650dSJack F Vogel 	return;
369461ae650dSJack F Vogel }
369561ae650dSJack F Vogel 
369661ae650dSJack F Vogel static void
369761ae650dSJack F Vogel ixl_update_stats_counters(struct ixl_pf *pf)
369861ae650dSJack F Vogel {
369961ae650dSJack F Vogel 	struct i40e_hw	*hw = &pf->hw;
370061ae650dSJack F Vogel 	struct ixl_vsi *vsi = &pf->vsi;
370161ae650dSJack F Vogel 	struct ifnet	*ifp = vsi->ifp;
370261ae650dSJack F Vogel 
370361ae650dSJack F Vogel 	struct i40e_hw_port_stats *nsd = &pf->stats;
370461ae650dSJack F Vogel 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
370561ae650dSJack F Vogel 
370661ae650dSJack F Vogel 	/* Update hw stats */
370761ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
370861ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
370961ae650dSJack F Vogel 			   &osd->crc_errors, &nsd->crc_errors);
371061ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
371161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
371261ae650dSJack F Vogel 			   &osd->illegal_bytes, &nsd->illegal_bytes);
371361ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
371461ae650dSJack F Vogel 			   I40E_GLPRT_GORCL(hw->port),
371561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
371661ae650dSJack F Vogel 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
371761ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
371861ae650dSJack F Vogel 			   I40E_GLPRT_GOTCL(hw->port),
371961ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
372061ae650dSJack F Vogel 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
372161ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
372261ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
372361ae650dSJack F Vogel 			   &osd->eth.rx_discards,
372461ae650dSJack F Vogel 			   &nsd->eth.rx_discards);
372561ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
372661ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
372761ae650dSJack F Vogel 			   &osd->eth.tx_discards,
372861ae650dSJack F Vogel 			   &nsd->eth.tx_discards);
372961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
373061ae650dSJack F Vogel 			   I40E_GLPRT_UPRCL(hw->port),
373161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
373261ae650dSJack F Vogel 			   &osd->eth.rx_unicast,
373361ae650dSJack F Vogel 			   &nsd->eth.rx_unicast);
373461ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
373561ae650dSJack F Vogel 			   I40E_GLPRT_UPTCL(hw->port),
373661ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
373761ae650dSJack F Vogel 			   &osd->eth.tx_unicast,
373861ae650dSJack F Vogel 			   &nsd->eth.tx_unicast);
373961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
374061ae650dSJack F Vogel 			   I40E_GLPRT_MPRCL(hw->port),
374161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
374261ae650dSJack F Vogel 			   &osd->eth.rx_multicast,
374361ae650dSJack F Vogel 			   &nsd->eth.rx_multicast);
374461ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
374561ae650dSJack F Vogel 			   I40E_GLPRT_MPTCL(hw->port),
374661ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
374761ae650dSJack F Vogel 			   &osd->eth.tx_multicast,
374861ae650dSJack F Vogel 			   &nsd->eth.tx_multicast);
374961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
375061ae650dSJack F Vogel 			   I40E_GLPRT_BPRCL(hw->port),
375161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
375261ae650dSJack F Vogel 			   &osd->eth.rx_broadcast,
375361ae650dSJack F Vogel 			   &nsd->eth.rx_broadcast);
375461ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
375561ae650dSJack F Vogel 			   I40E_GLPRT_BPTCL(hw->port),
375661ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
375761ae650dSJack F Vogel 			   &osd->eth.tx_broadcast,
375861ae650dSJack F Vogel 			   &nsd->eth.tx_broadcast);
375961ae650dSJack F Vogel 
376061ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
376161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
376261ae650dSJack F Vogel 			   &osd->tx_dropped_link_down,
376361ae650dSJack F Vogel 			   &nsd->tx_dropped_link_down);
376461ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
376561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
376661ae650dSJack F Vogel 			   &osd->mac_local_faults,
376761ae650dSJack F Vogel 			   &nsd->mac_local_faults);
376861ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
376961ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
377061ae650dSJack F Vogel 			   &osd->mac_remote_faults,
377161ae650dSJack F Vogel 			   &nsd->mac_remote_faults);
377261ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
377361ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
377461ae650dSJack F Vogel 			   &osd->rx_length_errors,
377561ae650dSJack F Vogel 			   &nsd->rx_length_errors);
377661ae650dSJack F Vogel 
377761ae650dSJack F Vogel 	/* Flow control (LFC) stats */
377861ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
377961ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
378061ae650dSJack F Vogel 			   &osd->link_xon_rx, &nsd->link_xon_rx);
378161ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
378261ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
378361ae650dSJack F Vogel 			   &osd->link_xon_tx, &nsd->link_xon_tx);
378461ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
378561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
378661ae650dSJack F Vogel 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
378761ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
378861ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
378961ae650dSJack F Vogel 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
379061ae650dSJack F Vogel 
379161ae650dSJack F Vogel 	/* Priority flow control stats */
379261ae650dSJack F Vogel #if 0
379361ae650dSJack F Vogel 	for (int i = 0; i < 8; i++) {
379461ae650dSJack F Vogel 		ixl_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
379561ae650dSJack F Vogel 				   pf->stat_offsets_loaded,
379661ae650dSJack F Vogel 				   &osd->priority_xon_rx[i],
379761ae650dSJack F Vogel 				   &nsd->priority_xon_rx[i]);
379861ae650dSJack F Vogel 		ixl_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
379961ae650dSJack F Vogel 				   pf->stat_offsets_loaded,
380061ae650dSJack F Vogel 				   &osd->priority_xon_tx[i],
380161ae650dSJack F Vogel 				   &nsd->priority_xon_tx[i]);
380261ae650dSJack F Vogel 		ixl_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
380361ae650dSJack F Vogel 				   pf->stat_offsets_loaded,
380461ae650dSJack F Vogel 				   &osd->priority_xoff_tx[i],
380561ae650dSJack F Vogel 				   &nsd->priority_xoff_tx[i]);
380661ae650dSJack F Vogel 		ixl_stat_update32(hw,
380761ae650dSJack F Vogel 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
380861ae650dSJack F Vogel 				   pf->stat_offsets_loaded,
380961ae650dSJack F Vogel 				   &osd->priority_xon_2_xoff[i],
381061ae650dSJack F Vogel 				   &nsd->priority_xon_2_xoff[i]);
381161ae650dSJack F Vogel 	}
381261ae650dSJack F Vogel #endif
381361ae650dSJack F Vogel 
381461ae650dSJack F Vogel 	/* Packet size stats rx */
381561ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
381661ae650dSJack F Vogel 			   I40E_GLPRT_PRC64L(hw->port),
381761ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
381861ae650dSJack F Vogel 			   &osd->rx_size_64, &nsd->rx_size_64);
381961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
382061ae650dSJack F Vogel 			   I40E_GLPRT_PRC127L(hw->port),
382161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
382261ae650dSJack F Vogel 			   &osd->rx_size_127, &nsd->rx_size_127);
382361ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
382461ae650dSJack F Vogel 			   I40E_GLPRT_PRC255L(hw->port),
382561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
382661ae650dSJack F Vogel 			   &osd->rx_size_255, &nsd->rx_size_255);
382761ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
382861ae650dSJack F Vogel 			   I40E_GLPRT_PRC511L(hw->port),
382961ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
383061ae650dSJack F Vogel 			   &osd->rx_size_511, &nsd->rx_size_511);
383161ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
383261ae650dSJack F Vogel 			   I40E_GLPRT_PRC1023L(hw->port),
383361ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
383461ae650dSJack F Vogel 			   &osd->rx_size_1023, &nsd->rx_size_1023);
383561ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
383661ae650dSJack F Vogel 			   I40E_GLPRT_PRC1522L(hw->port),
383761ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
383861ae650dSJack F Vogel 			   &osd->rx_size_1522, &nsd->rx_size_1522);
383961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
384061ae650dSJack F Vogel 			   I40E_GLPRT_PRC9522L(hw->port),
384161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
384261ae650dSJack F Vogel 			   &osd->rx_size_big, &nsd->rx_size_big);
384361ae650dSJack F Vogel 
384461ae650dSJack F Vogel 	/* Packet size stats tx */
384561ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
384661ae650dSJack F Vogel 			   I40E_GLPRT_PTC64L(hw->port),
384761ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
384861ae650dSJack F Vogel 			   &osd->tx_size_64, &nsd->tx_size_64);
384961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
385061ae650dSJack F Vogel 			   I40E_GLPRT_PTC127L(hw->port),
385161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
385261ae650dSJack F Vogel 			   &osd->tx_size_127, &nsd->tx_size_127);
385361ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
385461ae650dSJack F Vogel 			   I40E_GLPRT_PTC255L(hw->port),
385561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
385661ae650dSJack F Vogel 			   &osd->tx_size_255, &nsd->tx_size_255);
385761ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
385861ae650dSJack F Vogel 			   I40E_GLPRT_PTC511L(hw->port),
385961ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
386061ae650dSJack F Vogel 			   &osd->tx_size_511, &nsd->tx_size_511);
386161ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
386261ae650dSJack F Vogel 			   I40E_GLPRT_PTC1023L(hw->port),
386361ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
386461ae650dSJack F Vogel 			   &osd->tx_size_1023, &nsd->tx_size_1023);
386561ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
386661ae650dSJack F Vogel 			   I40E_GLPRT_PTC1522L(hw->port),
386761ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
386861ae650dSJack F Vogel 			   &osd->tx_size_1522, &nsd->tx_size_1522);
386961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
387061ae650dSJack F Vogel 			   I40E_GLPRT_PTC9522L(hw->port),
387161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
387261ae650dSJack F Vogel 			   &osd->tx_size_big, &nsd->tx_size_big);
387361ae650dSJack F Vogel 
387461ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
387561ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
387661ae650dSJack F Vogel 			   &osd->rx_undersize, &nsd->rx_undersize);
387761ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
387861ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
387961ae650dSJack F Vogel 			   &osd->rx_fragments, &nsd->rx_fragments);
388061ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
388161ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
388261ae650dSJack F Vogel 			   &osd->rx_oversize, &nsd->rx_oversize);
388361ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
388461ae650dSJack F Vogel 			   pf->stat_offsets_loaded,
388561ae650dSJack F Vogel 			   &osd->rx_jabber, &nsd->rx_jabber);
388661ae650dSJack F Vogel 	pf->stat_offsets_loaded = true;
388761ae650dSJack F Vogel 	/* End hw stats */
388861ae650dSJack F Vogel 
388961ae650dSJack F Vogel 	/* Update vsi stats */
389061ae650dSJack F Vogel 	ixl_update_eth_stats(vsi);
389161ae650dSJack F Vogel 
389261ae650dSJack F Vogel 	/* OS statistics */
389361ae650dSJack F Vogel 	// ERJ - these are per-port, update all vsis?
389461ae650dSJack F Vogel 	ifp->if_ierrors = nsd->crc_errors + nsd->illegal_bytes;
389561ae650dSJack F Vogel }
389661ae650dSJack F Vogel 
389761ae650dSJack F Vogel /*
389861ae650dSJack F Vogel ** Tasklet handler for MSIX Adminq interrupts
389961ae650dSJack F Vogel **  - do outside interrupt since it might sleep
390061ae650dSJack F Vogel */
390161ae650dSJack F Vogel static void
390261ae650dSJack F Vogel ixl_do_adminq(void *context, int pending)
390361ae650dSJack F Vogel {
390461ae650dSJack F Vogel 	struct ixl_pf			*pf = context;
390561ae650dSJack F Vogel 	struct i40e_hw			*hw = &pf->hw;
390661ae650dSJack F Vogel 	struct ixl_vsi			*vsi = &pf->vsi;
390761ae650dSJack F Vogel 	struct i40e_arq_event_info	event;
390861ae650dSJack F Vogel 	i40e_status			ret;
390961ae650dSJack F Vogel 	u32				reg, loop = 0;
391061ae650dSJack F Vogel 	u16				opcode, result;
391161ae650dSJack F Vogel 
391261ae650dSJack F Vogel 	event.msg_len = IXL_AQ_BUF_SZ;
391361ae650dSJack F Vogel 	event.msg_buf = malloc(event.msg_len,
391461ae650dSJack F Vogel 	    M_DEVBUF, M_NOWAIT | M_ZERO);
391561ae650dSJack F Vogel 	if (!event.msg_buf) {
391661ae650dSJack F Vogel 		printf("Unable to allocate adminq memory\n");
391761ae650dSJack F Vogel 		return;
391861ae650dSJack F Vogel 	}
391961ae650dSJack F Vogel 
392061ae650dSJack F Vogel 	/* clean and process any events */
392161ae650dSJack F Vogel 	do {
392261ae650dSJack F Vogel 		ret = i40e_clean_arq_element(hw, &event, &result);
392361ae650dSJack F Vogel 		if (ret)
392461ae650dSJack F Vogel 			break;
392561ae650dSJack F Vogel 		opcode = LE16_TO_CPU(event.desc.opcode);
392661ae650dSJack F Vogel 		switch (opcode) {
392761ae650dSJack F Vogel 		case i40e_aqc_opc_get_link_status:
392861ae650dSJack F Vogel 			vsi->link_up = ixl_config_link(hw);
392961ae650dSJack F Vogel 			ixl_update_link_status(pf);
393061ae650dSJack F Vogel 			break;
393161ae650dSJack F Vogel 		case i40e_aqc_opc_send_msg_to_pf:
393261ae650dSJack F Vogel 			/* process pf/vf communication here */
393361ae650dSJack F Vogel 			break;
393461ae650dSJack F Vogel 		case i40e_aqc_opc_event_lan_overflow:
393561ae650dSJack F Vogel 			break;
393661ae650dSJack F Vogel 		default:
393761ae650dSJack F Vogel #ifdef IXL_DEBUG
393861ae650dSJack F Vogel 			printf("AdminQ unknown event %x\n", opcode);
393961ae650dSJack F Vogel #endif
394061ae650dSJack F Vogel 			break;
394161ae650dSJack F Vogel 		}
394261ae650dSJack F Vogel 
394361ae650dSJack F Vogel 	} while (result && (loop++ < IXL_ADM_LIMIT));
394461ae650dSJack F Vogel 
394561ae650dSJack F Vogel 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
394661ae650dSJack F Vogel 	reg |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
394761ae650dSJack F Vogel 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
394861ae650dSJack F Vogel 	free(event.msg_buf, M_DEVBUF);
394961ae650dSJack F Vogel 
395061ae650dSJack F Vogel 	if (pf->msix > 1)
395161ae650dSJack F Vogel 		ixl_enable_adminq(&pf->hw);
395261ae650dSJack F Vogel 	else
395361ae650dSJack F Vogel 		ixl_enable_intr(vsi);
395461ae650dSJack F Vogel }
395561ae650dSJack F Vogel 
395661ae650dSJack F Vogel static int
395761ae650dSJack F Vogel ixl_debug_info(SYSCTL_HANDLER_ARGS)
395861ae650dSJack F Vogel {
395961ae650dSJack F Vogel 	struct ixl_pf	*pf;
396061ae650dSJack F Vogel 	int		error, input = 0;
396161ae650dSJack F Vogel 
396261ae650dSJack F Vogel 	error = sysctl_handle_int(oidp, &input, 0, req);
396361ae650dSJack F Vogel 
396461ae650dSJack F Vogel 	if (error || !req->newptr)
396561ae650dSJack F Vogel 		return (error);
396661ae650dSJack F Vogel 
396761ae650dSJack F Vogel 	if (input == 1) {
396861ae650dSJack F Vogel 		pf = (struct ixl_pf *)arg1;
396961ae650dSJack F Vogel 		ixl_print_debug_info(pf);
397061ae650dSJack F Vogel 	}
397161ae650dSJack F Vogel 
397261ae650dSJack F Vogel 	return (error);
397361ae650dSJack F Vogel }
397461ae650dSJack F Vogel 
397561ae650dSJack F Vogel static void
397661ae650dSJack F Vogel ixl_print_debug_info(struct ixl_pf *pf)
397761ae650dSJack F Vogel {
397861ae650dSJack F Vogel 	struct i40e_hw		*hw = &pf->hw;
397961ae650dSJack F Vogel 	struct ixl_vsi		*vsi = &pf->vsi;
398061ae650dSJack F Vogel 	struct ixl_queue	*que = vsi->queues;
398161ae650dSJack F Vogel 	struct rx_ring		*rxr = &que->rxr;
398261ae650dSJack F Vogel 	struct tx_ring		*txr = &que->txr;
398361ae650dSJack F Vogel 	u32			reg;
398461ae650dSJack F Vogel 
398561ae650dSJack F Vogel 
3986*ff21e856SBjoern A. Zeeb 	printf("Queue irqs = %jx\n", (uintmax_t)que->irqs);
3987*ff21e856SBjoern A. Zeeb 	printf("AdminQ irqs = %jx\n", (uintmax_t)pf->admin_irq);
398861ae650dSJack F Vogel 	printf("RX next check = %x\n", rxr->next_check);
3989*ff21e856SBjoern A. Zeeb 	printf("RX not ready = %jx\n", (uintmax_t)rxr->not_done);
3990*ff21e856SBjoern A. Zeeb 	printf("RX packets = %jx\n", (uintmax_t)rxr->rx_packets);
399161ae650dSJack F Vogel 	printf("TX desc avail = %x\n", txr->avail);
399261ae650dSJack F Vogel 
399361ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLV_GORCL(0xc));
399461ae650dSJack F Vogel 	 printf("RX Bytes = %x\n", reg);
399561ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_GORCL(hw->port));
399661ae650dSJack F Vogel 	 printf("Port RX Bytes = %x\n", reg);
399761ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLV_RDPC(0xc));
399861ae650dSJack F Vogel 	 printf("RX discard = %x\n", reg);
399961ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_RDPC(hw->port));
400061ae650dSJack F Vogel 	 printf("Port RX discard = %x\n", reg);
400161ae650dSJack F Vogel 
400261ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLV_TEPC(0xc));
400361ae650dSJack F Vogel 	 printf("TX errors = %x\n", reg);
400461ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLV_GOTCL(0xc));
400561ae650dSJack F Vogel 	 printf("TX Bytes = %x\n", reg);
400661ae650dSJack F Vogel 
400761ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_RUC(hw->port));
400861ae650dSJack F Vogel 	 printf("RX undersize = %x\n", reg);
400961ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_RFC(hw->port));
401061ae650dSJack F Vogel 	 printf("RX fragments = %x\n", reg);
401161ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_ROC(hw->port));
401261ae650dSJack F Vogel 	 printf("RX oversize = %x\n", reg);
401361ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_RLEC(hw->port));
401461ae650dSJack F Vogel 	 printf("RX length error = %x\n", reg);
401561ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_MRFC(hw->port));
401661ae650dSJack F Vogel 	 printf("mac remote fault = %x\n", reg);
401761ae650dSJack F Vogel 	reg = rd32(hw, I40E_GLPRT_MLFC(hw->port));
401861ae650dSJack F Vogel 	 printf("mac local fault = %x\n", reg);
401961ae650dSJack F Vogel }
402061ae650dSJack F Vogel 
402161ae650dSJack F Vogel /**
402261ae650dSJack F Vogel  * Update VSI-specific ethernet statistics counters.
402361ae650dSJack F Vogel  **/
402461ae650dSJack F Vogel void ixl_update_eth_stats(struct ixl_vsi *vsi)
402561ae650dSJack F Vogel {
402661ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)vsi->back;
402761ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
402861ae650dSJack F Vogel 	struct ifnet *ifp = vsi->ifp;
402961ae650dSJack F Vogel 	struct i40e_eth_stats *es;
403061ae650dSJack F Vogel 	struct i40e_eth_stats *oes;
403161ae650dSJack F Vogel 	u16 stat_idx = vsi->info.stat_counter_idx;
403261ae650dSJack F Vogel 
403361ae650dSJack F Vogel 	es = &vsi->eth_stats;
403461ae650dSJack F Vogel 	oes = &vsi->eth_stats_offsets;
403561ae650dSJack F Vogel 
403661ae650dSJack F Vogel 	/* Gather up the stats that the hw collects */
403761ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
403861ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
403961ae650dSJack F Vogel 			   &oes->tx_errors, &es->tx_errors);
404061ae650dSJack F Vogel 	ixl_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
404161ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
404261ae650dSJack F Vogel 			   &oes->rx_discards, &es->rx_discards);
404361ae650dSJack F Vogel 
404461ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
404561ae650dSJack F Vogel 			   I40E_GLV_GORCL(stat_idx),
404661ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
404761ae650dSJack F Vogel 			   &oes->rx_bytes, &es->rx_bytes);
404861ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
404961ae650dSJack F Vogel 			   I40E_GLV_UPRCL(stat_idx),
405061ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
405161ae650dSJack F Vogel 			   &oes->rx_unicast, &es->rx_unicast);
405261ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
405361ae650dSJack F Vogel 			   I40E_GLV_MPRCL(stat_idx),
405461ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
405561ae650dSJack F Vogel 			   &oes->rx_multicast, &es->rx_multicast);
405661ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
405761ae650dSJack F Vogel 			   I40E_GLV_BPRCL(stat_idx),
405861ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
405961ae650dSJack F Vogel 			   &oes->rx_broadcast, &es->rx_broadcast);
406061ae650dSJack F Vogel 
406161ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
406261ae650dSJack F Vogel 			   I40E_GLV_GOTCL(stat_idx),
406361ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
406461ae650dSJack F Vogel 			   &oes->tx_bytes, &es->tx_bytes);
406561ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
406661ae650dSJack F Vogel 			   I40E_GLV_UPTCL(stat_idx),
406761ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
406861ae650dSJack F Vogel 			   &oes->tx_unicast, &es->tx_unicast);
406961ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
407061ae650dSJack F Vogel 			   I40E_GLV_MPTCL(stat_idx),
407161ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
407261ae650dSJack F Vogel 			   &oes->tx_multicast, &es->tx_multicast);
407361ae650dSJack F Vogel 	ixl_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
407461ae650dSJack F Vogel 			   I40E_GLV_BPTCL(stat_idx),
407561ae650dSJack F Vogel 			   vsi->stat_offsets_loaded,
407661ae650dSJack F Vogel 			   &oes->tx_broadcast, &es->tx_broadcast);
407761ae650dSJack F Vogel 	vsi->stat_offsets_loaded = true;
407861ae650dSJack F Vogel 
407961ae650dSJack F Vogel 	/* Update ifnet stats */
408061ae650dSJack F Vogel 	ifp->if_ipackets = es->rx_unicast +
408161ae650dSJack F Vogel 	                   es->rx_multicast +
408261ae650dSJack F Vogel 			   es->rx_broadcast;
408361ae650dSJack F Vogel 	ifp->if_opackets = es->tx_unicast +
408461ae650dSJack F Vogel 	                   es->tx_multicast +
408561ae650dSJack F Vogel 			   es->tx_broadcast;
408661ae650dSJack F Vogel 	ifp->if_ibytes = es->rx_bytes;
408761ae650dSJack F Vogel 	ifp->if_obytes = es->tx_bytes;
408861ae650dSJack F Vogel 	ifp->if_imcasts = es->rx_multicast;
408961ae650dSJack F Vogel 	ifp->if_omcasts = es->tx_multicast;
409061ae650dSJack F Vogel 
409161ae650dSJack F Vogel 	ifp->if_oerrors = es->tx_errors;
409261ae650dSJack F Vogel 	ifp->if_iqdrops = es->rx_discards;
409361ae650dSJack F Vogel 	ifp->if_noproto = es->rx_unknown_protocol;
409461ae650dSJack F Vogel 	ifp->if_collisions = 0;
409561ae650dSJack F Vogel }
409661ae650dSJack F Vogel 
409761ae650dSJack F Vogel /**
409861ae650dSJack F Vogel  * Reset all of the stats for the given pf
409961ae650dSJack F Vogel  **/
410061ae650dSJack F Vogel void ixl_pf_reset_stats(struct ixl_pf *pf)
410161ae650dSJack F Vogel {
410261ae650dSJack F Vogel 	bzero(&pf->stats, sizeof(struct i40e_hw_port_stats));
410361ae650dSJack F Vogel 	bzero(&pf->stats_offsets, sizeof(struct i40e_hw_port_stats));
410461ae650dSJack F Vogel 	pf->stat_offsets_loaded = false;
410561ae650dSJack F Vogel }
410661ae650dSJack F Vogel 
410761ae650dSJack F Vogel /**
410861ae650dSJack F Vogel  * Resets all stats of the given vsi
410961ae650dSJack F Vogel  **/
411061ae650dSJack F Vogel void ixl_vsi_reset_stats(struct ixl_vsi *vsi)
411161ae650dSJack F Vogel {
411261ae650dSJack F Vogel 	bzero(&vsi->eth_stats, sizeof(struct i40e_eth_stats));
411361ae650dSJack F Vogel 	bzero(&vsi->eth_stats_offsets, sizeof(struct i40e_eth_stats));
411461ae650dSJack F Vogel 	vsi->stat_offsets_loaded = false;
411561ae650dSJack F Vogel }
411661ae650dSJack F Vogel 
411761ae650dSJack F Vogel /**
411861ae650dSJack F Vogel  * Read and update a 48 bit stat from the hw
411961ae650dSJack F Vogel  *
412061ae650dSJack F Vogel  * Since the device stats are not reset at PFReset, they likely will not
412161ae650dSJack F Vogel  * be zeroed when the driver starts.  We'll save the first values read
412261ae650dSJack F Vogel  * and use them as offsets to be subtracted from the raw values in order
412361ae650dSJack F Vogel  * to report stats that count from zero.
412461ae650dSJack F Vogel  **/
412561ae650dSJack F Vogel static void
412661ae650dSJack F Vogel ixl_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
412761ae650dSJack F Vogel 	bool offset_loaded, u64 *offset, u64 *stat)
412861ae650dSJack F Vogel {
412961ae650dSJack F Vogel 	u64 new_data;
413061ae650dSJack F Vogel 
4131*ff21e856SBjoern A. Zeeb #if defined(__FreeBSD__) && (__FreeBSD_version >= 1000000) && defined(__amd64__)
413261ae650dSJack F Vogel 	new_data = rd64(hw, loreg);
413361ae650dSJack F Vogel #else
413461ae650dSJack F Vogel 	/*
413561ae650dSJack F Vogel 	 * Use two rd32's instead of one rd64; FreeBSD versions before
413661ae650dSJack F Vogel 	 * 10 don't support 8 byte bus reads/writes.
413761ae650dSJack F Vogel 	 */
413861ae650dSJack F Vogel 	new_data = rd32(hw, loreg);
413961ae650dSJack F Vogel 	new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
414061ae650dSJack F Vogel #endif
414161ae650dSJack F Vogel 
414261ae650dSJack F Vogel 	if (!offset_loaded)
414361ae650dSJack F Vogel 		*offset = new_data;
414461ae650dSJack F Vogel 	if (new_data >= *offset)
414561ae650dSJack F Vogel 		*stat = new_data - *offset;
414661ae650dSJack F Vogel 	else
414761ae650dSJack F Vogel 		*stat = (new_data + ((u64)1 << 48)) - *offset;
414861ae650dSJack F Vogel 	*stat &= 0xFFFFFFFFFFFFULL;
414961ae650dSJack F Vogel }
415061ae650dSJack F Vogel 
415161ae650dSJack F Vogel /**
415261ae650dSJack F Vogel  * Read and update a 32 bit stat from the hw
415361ae650dSJack F Vogel  **/
415461ae650dSJack F Vogel static void
415561ae650dSJack F Vogel ixl_stat_update32(struct i40e_hw *hw, u32 reg,
415661ae650dSJack F Vogel 	bool offset_loaded, u64 *offset, u64 *stat)
415761ae650dSJack F Vogel {
415861ae650dSJack F Vogel 	u32 new_data;
415961ae650dSJack F Vogel 
416061ae650dSJack F Vogel 	new_data = rd32(hw, reg);
416161ae650dSJack F Vogel 	if (!offset_loaded)
416261ae650dSJack F Vogel 		*offset = new_data;
416361ae650dSJack F Vogel 	if (new_data >= *offset)
416461ae650dSJack F Vogel 		*stat = (u32)(new_data - *offset);
416561ae650dSJack F Vogel 	else
416661ae650dSJack F Vogel 		*stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
416761ae650dSJack F Vogel }
416861ae650dSJack F Vogel 
416961ae650dSJack F Vogel /*
417061ae650dSJack F Vogel ** Set flow control using sysctl:
417161ae650dSJack F Vogel ** 	0 - off
417261ae650dSJack F Vogel **	1 - rx pause
417361ae650dSJack F Vogel **	2 - tx pause
417461ae650dSJack F Vogel **	3 - full
417561ae650dSJack F Vogel */
417661ae650dSJack F Vogel static int
417761ae650dSJack F Vogel ixl_set_flowcntl(SYSCTL_HANDLER_ARGS)
417861ae650dSJack F Vogel {
417961ae650dSJack F Vogel 	/*
418061ae650dSJack F Vogel 	 * TODO: ensure flow control is disabled if
418161ae650dSJack F Vogel 	 * priority flow control is enabled
418261ae650dSJack F Vogel 	 *
418361ae650dSJack F Vogel 	 * TODO: ensure tx CRC by hardware should be enabled
418461ae650dSJack F Vogel 	 * if tx flow control is enabled.
418561ae650dSJack F Vogel 	 */
418661ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
418761ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
418861ae650dSJack F Vogel 	device_t dev = pf->dev;
418961ae650dSJack F Vogel 	int requested_fc = 0, error = 0;
419061ae650dSJack F Vogel 	enum i40e_status_code aq_error = 0;
419161ae650dSJack F Vogel 	u8 fc_aq_err = 0;
419261ae650dSJack F Vogel 
419361ae650dSJack F Vogel 	aq_error = i40e_aq_get_link_info(hw, TRUE, NULL, NULL);
419461ae650dSJack F Vogel 	if (aq_error) {
419561ae650dSJack F Vogel 		device_printf(dev,
419661ae650dSJack F Vogel 		    "%s: Error retrieving link info from aq, %d\n",
419761ae650dSJack F Vogel 		    __func__, aq_error);
419861ae650dSJack F Vogel 		return (EAGAIN);
419961ae650dSJack F Vogel 	}
420061ae650dSJack F Vogel 
420161ae650dSJack F Vogel 	/* Read in new mode */
420261ae650dSJack F Vogel 	requested_fc = hw->fc.current_mode;
420361ae650dSJack F Vogel 	error = sysctl_handle_int(oidp, &requested_fc, 0, req);
420461ae650dSJack F Vogel 	if ((error) || (req->newptr == NULL))
420561ae650dSJack F Vogel 		return (error);
420661ae650dSJack F Vogel 	if (requested_fc < 0 || requested_fc > 3) {
420761ae650dSJack F Vogel 		device_printf(dev,
420861ae650dSJack F Vogel 		    "Invalid fc mode; valid modes are 0 through 3\n");
420961ae650dSJack F Vogel 		return (EINVAL);
421061ae650dSJack F Vogel 	}
421161ae650dSJack F Vogel 
421261ae650dSJack F Vogel 	/*
421361ae650dSJack F Vogel 	** Changing flow control mode currently does not work on
421461ae650dSJack F Vogel 	** 40GBASE-CR4 PHYs
421561ae650dSJack F Vogel 	*/
421661ae650dSJack F Vogel 	if (hw->phy.link_info.phy_type == I40E_PHY_TYPE_40GBASE_CR4
421761ae650dSJack F Vogel 	    || hw->phy.link_info.phy_type == I40E_PHY_TYPE_40GBASE_CR4_CU) {
421861ae650dSJack F Vogel 		device_printf(dev, "Changing flow control mode unsupported"
421961ae650dSJack F Vogel 		    " on 40GBase-CR4 media.\n");
422061ae650dSJack F Vogel 		return (ENODEV);
422161ae650dSJack F Vogel 	}
422261ae650dSJack F Vogel 
422361ae650dSJack F Vogel 	/* Set fc ability for port */
422461ae650dSJack F Vogel 	hw->fc.requested_mode = requested_fc;
422561ae650dSJack F Vogel 	aq_error = i40e_set_fc(hw, &fc_aq_err, TRUE);
422661ae650dSJack F Vogel 	if (aq_error) {
422761ae650dSJack F Vogel 		device_printf(dev,
422861ae650dSJack F Vogel 		    "%s: Error setting new fc mode %d; fc_err %#x\n",
422961ae650dSJack F Vogel 		    __func__, aq_error, fc_aq_err);
423061ae650dSJack F Vogel 		return (EAGAIN);
423161ae650dSJack F Vogel 	}
423261ae650dSJack F Vogel 
423361ae650dSJack F Vogel 	if (hw->fc.current_mode != hw->fc.requested_mode) {
423461ae650dSJack F Vogel 		device_printf(dev, "%s: FC set failure:\n", __func__);
423561ae650dSJack F Vogel 		device_printf(dev, "%s: Current: %s / Requested: %s\n",
423661ae650dSJack F Vogel 		    __func__,
423761ae650dSJack F Vogel 		    ixl_fc_string[hw->fc.current_mode],
423861ae650dSJack F Vogel 		    ixl_fc_string[hw->fc.requested_mode]);
423961ae650dSJack F Vogel 	}
424061ae650dSJack F Vogel 
424161ae650dSJack F Vogel 	return (0);
424261ae650dSJack F Vogel }
424361ae650dSJack F Vogel 
424461ae650dSJack F Vogel static int
424561ae650dSJack F Vogel ixl_current_speed(SYSCTL_HANDLER_ARGS)
424661ae650dSJack F Vogel {
424761ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
424861ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
424961ae650dSJack F Vogel 	int error = 0, index = 0;
425061ae650dSJack F Vogel 
425161ae650dSJack F Vogel 	char *speeds[] = {
425261ae650dSJack F Vogel 		"Unknown",
425361ae650dSJack F Vogel 		"100M",
425461ae650dSJack F Vogel 		"1G",
425561ae650dSJack F Vogel 		"10G",
425661ae650dSJack F Vogel 		"40G",
425761ae650dSJack F Vogel 		"20G"
425861ae650dSJack F Vogel 	};
425961ae650dSJack F Vogel 
426061ae650dSJack F Vogel 	ixl_update_link_status(pf);
426161ae650dSJack F Vogel 
426261ae650dSJack F Vogel 	switch (hw->phy.link_info.link_speed) {
426361ae650dSJack F Vogel 	case I40E_LINK_SPEED_100MB:
426461ae650dSJack F Vogel 		index = 1;
426561ae650dSJack F Vogel 		break;
426661ae650dSJack F Vogel 	case I40E_LINK_SPEED_1GB:
426761ae650dSJack F Vogel 		index = 2;
426861ae650dSJack F Vogel 		break;
426961ae650dSJack F Vogel 	case I40E_LINK_SPEED_10GB:
427061ae650dSJack F Vogel 		index = 3;
427161ae650dSJack F Vogel 		break;
427261ae650dSJack F Vogel 	case I40E_LINK_SPEED_40GB:
427361ae650dSJack F Vogel 		index = 4;
427461ae650dSJack F Vogel 		break;
427561ae650dSJack F Vogel 	case I40E_LINK_SPEED_20GB:
427661ae650dSJack F Vogel 		index = 5;
427761ae650dSJack F Vogel 		break;
427861ae650dSJack F Vogel 	case I40E_LINK_SPEED_UNKNOWN:
427961ae650dSJack F Vogel 	default:
428061ae650dSJack F Vogel 		index = 0;
428161ae650dSJack F Vogel 		break;
428261ae650dSJack F Vogel 	}
428361ae650dSJack F Vogel 
428461ae650dSJack F Vogel 	error = sysctl_handle_string(oidp, speeds[index],
428561ae650dSJack F Vogel 	    strlen(speeds[index]), req);
428661ae650dSJack F Vogel 	return (error);
428761ae650dSJack F Vogel }
428861ae650dSJack F Vogel 
428961ae650dSJack F Vogel /*
429061ae650dSJack F Vogel ** Control link advertise speed:
429161ae650dSJack F Vogel **	Flags:
429261ae650dSJack F Vogel **	0x1 - advertise 100 Mb
429361ae650dSJack F Vogel **	0x2 - advertise 1G
429461ae650dSJack F Vogel **	0x4 - advertise 10G
429561ae650dSJack F Vogel **
429661ae650dSJack F Vogel ** Does not work on 40G devices.
429761ae650dSJack F Vogel */
429861ae650dSJack F Vogel static int
429961ae650dSJack F Vogel ixl_set_advertise(SYSCTL_HANDLER_ARGS)
430061ae650dSJack F Vogel {
430161ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
430261ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
430361ae650dSJack F Vogel 	device_t dev = pf->dev;
430461ae650dSJack F Vogel 	struct i40e_aq_get_phy_abilities_resp abilities;
430561ae650dSJack F Vogel 	struct i40e_aq_set_phy_config config;
430661ae650dSJack F Vogel 	int requested_ls = 0;
430761ae650dSJack F Vogel 	enum i40e_status_code aq_error = 0;
430861ae650dSJack F Vogel 	int error = 0;
430961ae650dSJack F Vogel 
431061ae650dSJack F Vogel 	/*
431161ae650dSJack F Vogel 	** FW doesn't support changing advertised speed
431261ae650dSJack F Vogel 	** for 40G devices; speed is always 40G.
431361ae650dSJack F Vogel 	*/
431461ae650dSJack F Vogel 	if (i40e_is_40G_device(hw->device_id))
431561ae650dSJack F Vogel 		return (ENODEV);
431661ae650dSJack F Vogel 
431761ae650dSJack F Vogel 	/* Read in new mode */
431861ae650dSJack F Vogel 	requested_ls = pf->advertised_speed;
431961ae650dSJack F Vogel 	error = sysctl_handle_int(oidp, &requested_ls, 0, req);
432061ae650dSJack F Vogel 	if ((error) || (req->newptr == NULL))
432161ae650dSJack F Vogel 		return (error);
432261ae650dSJack F Vogel 	if (requested_ls < 1 || requested_ls > 7) {
432361ae650dSJack F Vogel 		device_printf(dev,
432461ae650dSJack F Vogel 		    "Invalid advertised speed; valid modes are 0x1 through 0x7\n");
432561ae650dSJack F Vogel 		return (EINVAL);
432661ae650dSJack F Vogel 	}
432761ae650dSJack F Vogel 
432861ae650dSJack F Vogel 	/* Exit if no change */
432961ae650dSJack F Vogel 	if (pf->advertised_speed == requested_ls)
433061ae650dSJack F Vogel 		return (0);
433161ae650dSJack F Vogel 
433261ae650dSJack F Vogel 	/* Get current capability information */
433361ae650dSJack F Vogel 	aq_error = i40e_aq_get_phy_capabilities(hw, FALSE, FALSE, &abilities, NULL);
433461ae650dSJack F Vogel 	if (aq_error) {
433561ae650dSJack F Vogel 		device_printf(dev, "%s: Error getting phy capabilities %d,"
433661ae650dSJack F Vogel 		    " aq error: %d\n", __func__, aq_error,
433761ae650dSJack F Vogel 		    hw->aq.asq_last_status);
433861ae650dSJack F Vogel 		return (EAGAIN);
433961ae650dSJack F Vogel 	}
434061ae650dSJack F Vogel 
434161ae650dSJack F Vogel 	/* Prepare new config */
434261ae650dSJack F Vogel 	bzero(&config, sizeof(config));
434361ae650dSJack F Vogel 	config.phy_type = abilities.phy_type;
434461ae650dSJack F Vogel 	config.abilities = abilities.abilities
434561ae650dSJack F Vogel 	    | I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
434661ae650dSJack F Vogel 	config.eee_capability = abilities.eee_capability;
434761ae650dSJack F Vogel 	config.eeer = abilities.eeer_val;
434861ae650dSJack F Vogel 	config.low_power_ctrl = abilities.d3_lpan;
434961ae650dSJack F Vogel 	/* Translate into aq cmd link_speed */
435061ae650dSJack F Vogel 	if (requested_ls & 0x4)
435161ae650dSJack F Vogel 		config.link_speed |= I40E_LINK_SPEED_10GB;
435261ae650dSJack F Vogel 	if (requested_ls & 0x2)
435361ae650dSJack F Vogel 		config.link_speed |= I40E_LINK_SPEED_1GB;
435461ae650dSJack F Vogel 	if (requested_ls & 0x1)
435561ae650dSJack F Vogel 		config.link_speed |= I40E_LINK_SPEED_100MB;
435661ae650dSJack F Vogel 
435761ae650dSJack F Vogel 	/* Do aq command & restart link */
435861ae650dSJack F Vogel 	aq_error = i40e_aq_set_phy_config(hw, &config, NULL);
435961ae650dSJack F Vogel 	if (aq_error) {
436061ae650dSJack F Vogel 		device_printf(dev, "%s: Error setting new phy config %d,"
436161ae650dSJack F Vogel 		    " aq error: %d\n", __func__, aq_error,
436261ae650dSJack F Vogel 		    hw->aq.asq_last_status);
436361ae650dSJack F Vogel 		return (EAGAIN);
436461ae650dSJack F Vogel 	}
436561ae650dSJack F Vogel 
436661ae650dSJack F Vogel 	pf->advertised_speed = requested_ls;
436761ae650dSJack F Vogel 	ixl_update_link_status(pf);
436861ae650dSJack F Vogel 	return (0);
436961ae650dSJack F Vogel }
437061ae650dSJack F Vogel 
437161ae650dSJack F Vogel /*
437261ae650dSJack F Vogel ** Get the width and transaction speed of
437361ae650dSJack F Vogel ** the bus this adapter is plugged into.
437461ae650dSJack F Vogel */
437561ae650dSJack F Vogel static u16
437661ae650dSJack F Vogel ixl_get_bus_info(struct i40e_hw *hw, device_t dev)
437761ae650dSJack F Vogel {
437861ae650dSJack F Vogel         u16                     link;
437961ae650dSJack F Vogel         u32                     offset;
438061ae650dSJack F Vogel 
438161ae650dSJack F Vogel 
438261ae650dSJack F Vogel         /* Get the PCI Express Capabilities offset */
438361ae650dSJack F Vogel         pci_find_cap(dev, PCIY_EXPRESS, &offset);
438461ae650dSJack F Vogel 
438561ae650dSJack F Vogel         /* ...and read the Link Status Register */
438661ae650dSJack F Vogel         link = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
438761ae650dSJack F Vogel 
438861ae650dSJack F Vogel         switch (link & I40E_PCI_LINK_WIDTH) {
438961ae650dSJack F Vogel         case I40E_PCI_LINK_WIDTH_1:
439061ae650dSJack F Vogel                 hw->bus.width = i40e_bus_width_pcie_x1;
439161ae650dSJack F Vogel                 break;
439261ae650dSJack F Vogel         case I40E_PCI_LINK_WIDTH_2:
439361ae650dSJack F Vogel                 hw->bus.width = i40e_bus_width_pcie_x2;
439461ae650dSJack F Vogel                 break;
439561ae650dSJack F Vogel         case I40E_PCI_LINK_WIDTH_4:
439661ae650dSJack F Vogel                 hw->bus.width = i40e_bus_width_pcie_x4;
439761ae650dSJack F Vogel                 break;
439861ae650dSJack F Vogel         case I40E_PCI_LINK_WIDTH_8:
439961ae650dSJack F Vogel                 hw->bus.width = i40e_bus_width_pcie_x8;
440061ae650dSJack F Vogel                 break;
440161ae650dSJack F Vogel         default:
440261ae650dSJack F Vogel                 hw->bus.width = i40e_bus_width_unknown;
440361ae650dSJack F Vogel                 break;
440461ae650dSJack F Vogel         }
440561ae650dSJack F Vogel 
440661ae650dSJack F Vogel         switch (link & I40E_PCI_LINK_SPEED) {
440761ae650dSJack F Vogel         case I40E_PCI_LINK_SPEED_2500:
440861ae650dSJack F Vogel                 hw->bus.speed = i40e_bus_speed_2500;
440961ae650dSJack F Vogel                 break;
441061ae650dSJack F Vogel         case I40E_PCI_LINK_SPEED_5000:
441161ae650dSJack F Vogel                 hw->bus.speed = i40e_bus_speed_5000;
441261ae650dSJack F Vogel                 break;
441361ae650dSJack F Vogel         case I40E_PCI_LINK_SPEED_8000:
441461ae650dSJack F Vogel                 hw->bus.speed = i40e_bus_speed_8000;
441561ae650dSJack F Vogel                 break;
441661ae650dSJack F Vogel         default:
441761ae650dSJack F Vogel                 hw->bus.speed = i40e_bus_speed_unknown;
441861ae650dSJack F Vogel                 break;
441961ae650dSJack F Vogel         }
442061ae650dSJack F Vogel 
442161ae650dSJack F Vogel 
442261ae650dSJack F Vogel         device_printf(dev,"PCI Express Bus: Speed %s %s\n",
442361ae650dSJack F Vogel             ((hw->bus.speed == i40e_bus_speed_8000) ? "8.0GT/s":
442461ae650dSJack F Vogel             (hw->bus.speed == i40e_bus_speed_5000) ? "5.0GT/s":
442561ae650dSJack F Vogel             (hw->bus.speed == i40e_bus_speed_2500) ? "2.5GT/s":"Unknown"),
442661ae650dSJack F Vogel             (hw->bus.width == i40e_bus_width_pcie_x8) ? "Width x8" :
442761ae650dSJack F Vogel             (hw->bus.width == i40e_bus_width_pcie_x4) ? "Width x4" :
442861ae650dSJack F Vogel             (hw->bus.width == i40e_bus_width_pcie_x1) ? "Width x1" :
442961ae650dSJack F Vogel             ("Unknown"));
443061ae650dSJack F Vogel 
443161ae650dSJack F Vogel         if ((hw->bus.width <= i40e_bus_width_pcie_x8) &&
443261ae650dSJack F Vogel             (hw->bus.speed < i40e_bus_speed_8000)) {
443361ae650dSJack F Vogel                 device_printf(dev, "PCI-Express bandwidth available"
443461ae650dSJack F Vogel                     " for this device\n     is not sufficient for"
443561ae650dSJack F Vogel                     " normal operation.\n");
443661ae650dSJack F Vogel                 device_printf(dev, "For expected performance a x8 "
443761ae650dSJack F Vogel                     "PCIE Gen3 slot is required.\n");
443861ae650dSJack F Vogel         }
443961ae650dSJack F Vogel 
444061ae650dSJack F Vogel         return (link);
444161ae650dSJack F Vogel }
444261ae650dSJack F Vogel 
444361ae650dSJack F Vogel #ifdef IXL_DEBUG
444461ae650dSJack F Vogel static int
444561ae650dSJack F Vogel ixl_sysctl_link_status(SYSCTL_HANDLER_ARGS)
444661ae650dSJack F Vogel {
444761ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
444861ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
444961ae650dSJack F Vogel 	struct i40e_link_status link_status;
445061ae650dSJack F Vogel 	char buf[512];
445161ae650dSJack F Vogel 
445261ae650dSJack F Vogel 	enum i40e_status_code aq_error = 0;
445361ae650dSJack F Vogel 
445461ae650dSJack F Vogel 	aq_error = i40e_aq_get_link_info(hw, TRUE, &link_status, NULL);
445561ae650dSJack F Vogel 	if (aq_error) {
445661ae650dSJack F Vogel 		printf("i40e_aq_get_link_info() error %d\n", aq_error);
445761ae650dSJack F Vogel 		return (EPERM);
445861ae650dSJack F Vogel 	}
445961ae650dSJack F Vogel 
446061ae650dSJack F Vogel 	sprintf(buf, "\n"
446161ae650dSJack F Vogel 	    "PHY Type : %#04x\n"
446261ae650dSJack F Vogel 	    "Speed    : %#04x\n"
446361ae650dSJack F Vogel 	    "Link info: %#04x\n"
446461ae650dSJack F Vogel 	    "AN info  : %#04x\n"
446561ae650dSJack F Vogel 	    "Ext info : %#04x",
446661ae650dSJack F Vogel 	    link_status.phy_type, link_status.link_speed,
446761ae650dSJack F Vogel 	    link_status.link_info, link_status.an_info,
446861ae650dSJack F Vogel 	    link_status.ext_info);
446961ae650dSJack F Vogel 
447061ae650dSJack F Vogel 	return (sysctl_handle_string(oidp, buf, strlen(buf), req));
447161ae650dSJack F Vogel }
447261ae650dSJack F Vogel 
447361ae650dSJack F Vogel static int
447461ae650dSJack F Vogel ixl_sysctl_phy_abilities(SYSCTL_HANDLER_ARGS)
447561ae650dSJack F Vogel {
447661ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
447761ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
447861ae650dSJack F Vogel 	struct i40e_aq_get_phy_abilities_resp abilities_resp;
447961ae650dSJack F Vogel 	char buf[512];
448061ae650dSJack F Vogel 
448161ae650dSJack F Vogel 	enum i40e_status_code aq_error = 0;
448261ae650dSJack F Vogel 
448361ae650dSJack F Vogel 	// TODO: Print out list of qualified modules as well?
448461ae650dSJack F Vogel 	aq_error = i40e_aq_get_phy_capabilities(hw, TRUE, FALSE, &abilities_resp, NULL);
448561ae650dSJack F Vogel 	if (aq_error) {
448661ae650dSJack F Vogel 		printf("i40e_aq_get_phy_capabilities() error %d\n", aq_error);
448761ae650dSJack F Vogel 		return (EPERM);
448861ae650dSJack F Vogel 	}
448961ae650dSJack F Vogel 
449061ae650dSJack F Vogel 	sprintf(buf, "\n"
449161ae650dSJack F Vogel 	    "PHY Type : %#010x\n"
449261ae650dSJack F Vogel 	    "Speed    : %#04x\n"
449361ae650dSJack F Vogel 	    "Abilities: %#04x\n"
449461ae650dSJack F Vogel 	    "EEE cap  : %#06x\n"
449561ae650dSJack F Vogel 	    "EEER reg : %#010x\n"
449661ae650dSJack F Vogel 	    "D3 Lpan  : %#04x",
449761ae650dSJack F Vogel 	    abilities_resp.phy_type, abilities_resp.link_speed,
449861ae650dSJack F Vogel 	    abilities_resp.abilities, abilities_resp.eee_capability,
449961ae650dSJack F Vogel 	    abilities_resp.eeer_val, abilities_resp.d3_lpan);
450061ae650dSJack F Vogel 
450161ae650dSJack F Vogel 	return (sysctl_handle_string(oidp, buf, strlen(buf), req));
450261ae650dSJack F Vogel }
450361ae650dSJack F Vogel 
450461ae650dSJack F Vogel static int
450561ae650dSJack F Vogel ixl_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS)
450661ae650dSJack F Vogel {
450761ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
450861ae650dSJack F Vogel 	struct ixl_vsi *vsi = &pf->vsi;
450961ae650dSJack F Vogel 	struct ixl_mac_filter *f;
451061ae650dSJack F Vogel 	char *buf, *buf_i;
451161ae650dSJack F Vogel 
451261ae650dSJack F Vogel 	int error = 0;
451361ae650dSJack F Vogel 	int ftl_len = 0;
451461ae650dSJack F Vogel 	int ftl_counter = 0;
451561ae650dSJack F Vogel 	int buf_len = 0;
451661ae650dSJack F Vogel 	int entry_len = 42;
451761ae650dSJack F Vogel 
451861ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
451961ae650dSJack F Vogel 		ftl_len++;
452061ae650dSJack F Vogel 	}
452161ae650dSJack F Vogel 
452261ae650dSJack F Vogel 	if (ftl_len < 1) {
452361ae650dSJack F Vogel 		sysctl_handle_string(oidp, "(none)", 6, req);
452461ae650dSJack F Vogel 		return (0);
452561ae650dSJack F Vogel 	}
452661ae650dSJack F Vogel 
452761ae650dSJack F Vogel 	buf_len = sizeof(char) * (entry_len + 1) * ftl_len + 2;
452861ae650dSJack F Vogel 	buf = buf_i = malloc(buf_len, M_DEVBUF, M_NOWAIT);
452961ae650dSJack F Vogel 
453061ae650dSJack F Vogel 	sprintf(buf_i++, "\n");
453161ae650dSJack F Vogel 	SLIST_FOREACH(f, &vsi->ftl, next) {
453261ae650dSJack F Vogel 		sprintf(buf_i,
453361ae650dSJack F Vogel 		    MAC_FORMAT ", vlan %4d, flags %#06x",
453461ae650dSJack F Vogel 		    MAC_FORMAT_ARGS(f->macaddr), f->vlan, f->flags);
453561ae650dSJack F Vogel 		buf_i += entry_len;
453661ae650dSJack F Vogel 		/* don't print '\n' for last entry */
453761ae650dSJack F Vogel 		if (++ftl_counter != ftl_len) {
453861ae650dSJack F Vogel 			sprintf(buf_i, "\n");
453961ae650dSJack F Vogel 			buf_i++;
454061ae650dSJack F Vogel 		}
454161ae650dSJack F Vogel 	}
454261ae650dSJack F Vogel 
454361ae650dSJack F Vogel 	error = sysctl_handle_string(oidp, buf, strlen(buf), req);
454461ae650dSJack F Vogel 	if (error)
454561ae650dSJack F Vogel 		printf("sysctl error: %d\n", error);
454661ae650dSJack F Vogel 	free(buf, M_DEVBUF);
454761ae650dSJack F Vogel 	return error;
454861ae650dSJack F Vogel }
454961ae650dSJack F Vogel 
455061ae650dSJack F Vogel #define IXL_SW_RES_SIZE 0x14
455161ae650dSJack F Vogel static int
455261ae650dSJack F Vogel ixl_sysctl_hw_res_info(SYSCTL_HANDLER_ARGS)
455361ae650dSJack F Vogel {
455461ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
455561ae650dSJack F Vogel 	struct i40e_hw *hw = &pf->hw;
455661ae650dSJack F Vogel 	device_t dev = pf->dev;
455761ae650dSJack F Vogel 	struct sbuf *buf;
455861ae650dSJack F Vogel 	int error = 0;
455961ae650dSJack F Vogel 
456061ae650dSJack F Vogel 	u8 num_entries;
456161ae650dSJack F Vogel 	struct i40e_aqc_switch_resource_alloc_element_resp resp[IXL_SW_RES_SIZE];
456261ae650dSJack F Vogel 
456361ae650dSJack F Vogel 	buf = sbuf_new_for_sysctl(NULL, NULL, 0, req);
456461ae650dSJack F Vogel 	if (!buf) {
456561ae650dSJack F Vogel 		device_printf(dev, "Could not allocate sbuf for output.\n");
456661ae650dSJack F Vogel 		return (ENOMEM);
456761ae650dSJack F Vogel 	}
456861ae650dSJack F Vogel 
456961ae650dSJack F Vogel 	error = i40e_aq_get_switch_resource_alloc(hw, &num_entries,
457061ae650dSJack F Vogel 				resp,
457161ae650dSJack F Vogel 				IXL_SW_RES_SIZE,
457261ae650dSJack F Vogel 				NULL);
457361ae650dSJack F Vogel 	if (error) {
457461ae650dSJack F Vogel 		device_printf(dev, "%s: get_switch_resource_alloc() error %d, aq error %d\n",
457561ae650dSJack F Vogel 		    __func__, error, hw->aq.asq_last_status);
457661ae650dSJack F Vogel 		sbuf_delete(buf);
457761ae650dSJack F Vogel 		return error;
457861ae650dSJack F Vogel 	}
457961ae650dSJack F Vogel 	device_printf(dev, "Num_entries: %d\n", num_entries);
458061ae650dSJack F Vogel 
458161ae650dSJack F Vogel 	sbuf_cat(buf, "\n");
458261ae650dSJack F Vogel 	sbuf_printf(buf,
458361ae650dSJack F Vogel 	    "Type | Guaranteed | Total | Used   | Un-allocated\n"
458461ae650dSJack F Vogel 	    "     | (this)     | (all) | (this) | (all)       \n");
458561ae650dSJack F Vogel 	for (int i = 0; i < num_entries; i++) {
458661ae650dSJack F Vogel 		sbuf_printf(buf,
458761ae650dSJack F Vogel 		    "%#4x | %10d   %5d   %6d   %12d",
458861ae650dSJack F Vogel 		    resp[i].resource_type,
458961ae650dSJack F Vogel 		    resp[i].guaranteed,
459061ae650dSJack F Vogel 		    resp[i].total,
459161ae650dSJack F Vogel 		    resp[i].used,
459261ae650dSJack F Vogel 		    resp[i].total_unalloced);
459361ae650dSJack F Vogel 		if (i < num_entries - 1)
459461ae650dSJack F Vogel 			sbuf_cat(buf, "\n");
459561ae650dSJack F Vogel 	}
459661ae650dSJack F Vogel 
459761ae650dSJack F Vogel 	error = sbuf_finish(buf);
459861ae650dSJack F Vogel 	if (error) {
459961ae650dSJack F Vogel 		device_printf(dev, "Error finishing sbuf: %d\n", error);
460061ae650dSJack F Vogel 		sbuf_delete(buf);
460161ae650dSJack F Vogel 		return error;
460261ae650dSJack F Vogel 	}
460361ae650dSJack F Vogel 
460461ae650dSJack F Vogel 	error = sysctl_handle_string(oidp, sbuf_data(buf), sbuf_len(buf), req);
460561ae650dSJack F Vogel 	if (error)
460661ae650dSJack F Vogel 		device_printf(dev, "sysctl error: %d\n", error);
460761ae650dSJack F Vogel 	sbuf_delete(buf);
460861ae650dSJack F Vogel 	return error;
460961ae650dSJack F Vogel 
461061ae650dSJack F Vogel }
461161ae650dSJack F Vogel 
461261ae650dSJack F Vogel /*
461361ae650dSJack F Vogel ** Dump TX desc given index.
461461ae650dSJack F Vogel ** Doesn't work; don't use.
461561ae650dSJack F Vogel ** TODO: Also needs a queue index input!
461661ae650dSJack F Vogel **/
461761ae650dSJack F Vogel static int
461861ae650dSJack F Vogel ixl_sysctl_dump_txd(SYSCTL_HANDLER_ARGS)
461961ae650dSJack F Vogel {
462061ae650dSJack F Vogel 	struct ixl_pf *pf = (struct ixl_pf *)arg1;
462161ae650dSJack F Vogel 	device_t dev = pf->dev;
462261ae650dSJack F Vogel 	struct sbuf *buf;
462361ae650dSJack F Vogel 	int error = 0;
462461ae650dSJack F Vogel 
462561ae650dSJack F Vogel 	u16 desc_idx = 0;
462661ae650dSJack F Vogel 
462761ae650dSJack F Vogel 	buf = sbuf_new_for_sysctl(NULL, NULL, 0, req);
462861ae650dSJack F Vogel 	if (!buf) {
462961ae650dSJack F Vogel 		device_printf(dev, "Could not allocate sbuf for output.\n");
463061ae650dSJack F Vogel 		return (ENOMEM);
463161ae650dSJack F Vogel 	}
463261ae650dSJack F Vogel 
463361ae650dSJack F Vogel 	/* Read in index */
463461ae650dSJack F Vogel 	error = sysctl_handle_int(oidp, &desc_idx, 0, req);
463561ae650dSJack F Vogel 	if (error)
463661ae650dSJack F Vogel 		return (error);
463761ae650dSJack F Vogel 	if (req->newptr == NULL)
463861ae650dSJack F Vogel 		return (EIO); // fix
463961ae650dSJack F Vogel 	if (desc_idx > 1024) { // fix
464061ae650dSJack F Vogel 		device_printf(dev,
464161ae650dSJack F Vogel 		    "Invalid descriptor index, needs to be < 1024\n"); // fix
464261ae650dSJack F Vogel 		return (EINVAL);
464361ae650dSJack F Vogel 	}
464461ae650dSJack F Vogel 
464561ae650dSJack F Vogel 	// Don't use this sysctl yet
464661ae650dSJack F Vogel 	if (TRUE)
464761ae650dSJack F Vogel 		return (ENODEV);
464861ae650dSJack F Vogel 
464961ae650dSJack F Vogel 	sbuf_cat(buf, "\n");
465061ae650dSJack F Vogel 
465161ae650dSJack F Vogel 	// set to queue 1?
465261ae650dSJack F Vogel 	struct ixl_queue *que = pf->vsi.queues;
465361ae650dSJack F Vogel 	struct tx_ring *txr = &(que[1].txr);
465461ae650dSJack F Vogel 	struct i40e_tx_desc *txd = &txr->base[desc_idx];
465561ae650dSJack F Vogel 
465661ae650dSJack F Vogel 	sbuf_printf(buf, "Que: %d, Desc: %d\n", que->me, desc_idx);
465761ae650dSJack F Vogel 	sbuf_printf(buf, "Addr: %#18lx\n", txd->buffer_addr);
465861ae650dSJack F Vogel 	sbuf_printf(buf, "Opts: %#18lx\n", txd->cmd_type_offset_bsz);
465961ae650dSJack F Vogel 
466061ae650dSJack F Vogel 	error = sbuf_finish(buf);
466161ae650dSJack F Vogel 	if (error) {
466261ae650dSJack F Vogel 		device_printf(dev, "Error finishing sbuf: %d\n", error);
466361ae650dSJack F Vogel 		sbuf_delete(buf);
466461ae650dSJack F Vogel 		return error;
466561ae650dSJack F Vogel 	}
466661ae650dSJack F Vogel 
466761ae650dSJack F Vogel 	error = sysctl_handle_string(oidp, sbuf_data(buf), sbuf_len(buf), req);
466861ae650dSJack F Vogel 	if (error)
466961ae650dSJack F Vogel 		device_printf(dev, "sysctl error: %d\n", error);
467061ae650dSJack F Vogel 	sbuf_delete(buf);
467161ae650dSJack F Vogel 	return error;
467261ae650dSJack F Vogel }
467361ae650dSJack F Vogel #endif
467461ae650dSJack F Vogel 
4675