Home | History | Annotate | Line # | Download | only in pci
if_ixl.c revision 1.73
      1  1.73  yamaguch /*	$NetBSD: if_ixl.c,v 1.73 2020/08/19 09:07:57 yamaguchi Exp $	*/
      2   1.1  yamaguch 
      3   1.1  yamaguch /*
      4   1.1  yamaguch  * Copyright (c) 2013-2015, Intel Corporation
      5   1.1  yamaguch  * All rights reserved.
      6   1.1  yamaguch 
      7   1.1  yamaguch  * Redistribution and use in source and binary forms, with or without
      8   1.1  yamaguch  * modification, are permitted provided that the following conditions are met:
      9   1.1  yamaguch  *
     10   1.1  yamaguch  *  1. Redistributions of source code must retain the above copyright notice,
     11   1.1  yamaguch  *     this list of conditions and the following disclaimer.
     12   1.1  yamaguch  *
     13   1.1  yamaguch  *  2. Redistributions in binary form must reproduce the above copyright
     14   1.1  yamaguch  *     notice, this list of conditions and the following disclaimer in the
     15   1.1  yamaguch  *     documentation and/or other materials provided with the distribution.
     16   1.1  yamaguch  *
     17   1.1  yamaguch  *  3. Neither the name of the Intel Corporation nor the names of its
     18   1.1  yamaguch  *     contributors may be used to endorse or promote products derived from
     19   1.1  yamaguch  *     this software without specific prior written permission.
     20   1.1  yamaguch  *
     21   1.1  yamaguch  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     22   1.1  yamaguch  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1  yamaguch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1  yamaguch  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     25   1.1  yamaguch  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1  yamaguch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1  yamaguch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1  yamaguch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1  yamaguch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1  yamaguch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1  yamaguch  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1  yamaguch  */
     33   1.1  yamaguch 
     34   1.1  yamaguch /*
     35   1.1  yamaguch  * Copyright (c) 2016,2017 David Gwynne <dlg (at) openbsd.org>
     36   1.1  yamaguch  *
     37   1.1  yamaguch  * Permission to use, copy, modify, and distribute this software for any
     38   1.1  yamaguch  * purpose with or without fee is hereby granted, provided that the above
     39   1.1  yamaguch  * copyright notice and this permission notice appear in all copies.
     40   1.1  yamaguch  *
     41   1.1  yamaguch  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     42   1.1  yamaguch  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     43   1.1  yamaguch  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     44   1.1  yamaguch  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     45   1.1  yamaguch  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     46   1.1  yamaguch  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     47   1.1  yamaguch  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     48   1.1  yamaguch  */
     49   1.1  yamaguch 
     50   1.1  yamaguch /*
     51   1.1  yamaguch  * Copyright (c) 2019 Internet Initiative Japan, Inc.
     52   1.1  yamaguch  * All rights reserved.
     53   1.1  yamaguch  *
     54   1.1  yamaguch  * Redistribution and use in source and binary forms, with or without
     55   1.1  yamaguch  * modification, are permitted provided that the following conditions
     56   1.1  yamaguch  * are met:
     57   1.1  yamaguch  * 1. Redistributions of source code must retain the above copyright
     58   1.1  yamaguch  *    notice, this list of conditions and the following disclaimer.
     59   1.1  yamaguch  * 2. Redistributions in binary form must reproduce the above copyright
     60   1.1  yamaguch  *    notice, this list of conditions and the following disclaimer in the
     61   1.1  yamaguch  *    documentation and/or other materials provided with the distribution.
     62   1.1  yamaguch  *
     63   1.1  yamaguch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     64   1.1  yamaguch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     65   1.1  yamaguch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     66   1.1  yamaguch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     67   1.1  yamaguch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     68   1.1  yamaguch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     69   1.1  yamaguch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     70   1.1  yamaguch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     71   1.1  yamaguch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     72   1.1  yamaguch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     73   1.1  yamaguch  * POSSIBILITY OF SUCH DAMAGE.
     74   1.1  yamaguch  */
     75   1.1  yamaguch 
     76   1.1  yamaguch #include <sys/cdefs.h>
     77  1.73  yamaguch __KERNEL_RCSID(0, "$NetBSD: if_ixl.c,v 1.73 2020/08/19 09:07:57 yamaguchi Exp $");
     78   1.1  yamaguch 
     79   1.1  yamaguch #ifdef _KERNEL_OPT
     80   1.1  yamaguch #include "opt_net_mpsafe.h"
     81  1.21  yamaguch #include "opt_if_ixl.h"
     82   1.1  yamaguch #endif
     83   1.1  yamaguch 
     84   1.1  yamaguch #include <sys/param.h>
     85   1.1  yamaguch #include <sys/types.h>
     86   1.1  yamaguch 
     87  1.70  yamaguch #include <sys/bitops.h>
     88   1.1  yamaguch #include <sys/cpu.h>
     89   1.1  yamaguch #include <sys/device.h>
     90   1.1  yamaguch #include <sys/evcnt.h>
     91   1.1  yamaguch #include <sys/interrupt.h>
     92   1.1  yamaguch #include <sys/kmem.h>
     93   1.1  yamaguch #include <sys/module.h>
     94   1.1  yamaguch #include <sys/mutex.h>
     95   1.1  yamaguch #include <sys/pcq.h>
     96   1.1  yamaguch #include <sys/syslog.h>
     97   1.1  yamaguch #include <sys/workqueue.h>
     98   1.1  yamaguch 
     99   1.1  yamaguch #include <sys/bus.h>
    100   1.1  yamaguch 
    101   1.1  yamaguch #include <net/bpf.h>
    102   1.1  yamaguch #include <net/if.h>
    103   1.1  yamaguch #include <net/if_dl.h>
    104   1.1  yamaguch #include <net/if_media.h>
    105   1.1  yamaguch #include <net/if_ether.h>
    106   1.1  yamaguch #include <net/rss_config.h>
    107   1.1  yamaguch 
    108  1.28  yamaguch #include <netinet/tcp.h>	/* for struct tcphdr */
    109  1.28  yamaguch #include <netinet/udp.h>	/* for struct udphdr */
    110  1.28  yamaguch 
    111   1.1  yamaguch #include <dev/pci/pcivar.h>
    112   1.1  yamaguch #include <dev/pci/pcidevs.h>
    113   1.1  yamaguch 
    114   1.1  yamaguch #include <dev/pci/if_ixlreg.h>
    115   1.1  yamaguch #include <dev/pci/if_ixlvar.h>
    116   1.1  yamaguch 
    117  1.19  yamaguch #include <prop/proplib.h>
    118  1.19  yamaguch 
    119   1.1  yamaguch struct ixl_softc; /* defined */
    120   1.1  yamaguch 
    121   1.1  yamaguch #define I40E_PF_RESET_WAIT_COUNT	200
    122   1.1  yamaguch #define I40E_AQ_LARGE_BUF		512
    123   1.1  yamaguch 
    124   1.1  yamaguch /* bitfields for Tx queue mapping in QTX_CTL */
    125   1.1  yamaguch #define I40E_QTX_CTL_VF_QUEUE		0x0
    126   1.1  yamaguch #define I40E_QTX_CTL_VM_QUEUE		0x1
    127   1.1  yamaguch #define I40E_QTX_CTL_PF_QUEUE		0x2
    128   1.1  yamaguch 
    129   1.1  yamaguch #define I40E_QUEUE_TYPE_EOL		0x7ff
    130   1.1  yamaguch #define I40E_INTR_NOTX_QUEUE		0
    131   1.1  yamaguch 
    132   1.1  yamaguch #define I40E_QUEUE_TYPE_RX		0x0
    133   1.1  yamaguch #define I40E_QUEUE_TYPE_TX		0x1
    134   1.1  yamaguch #define I40E_QUEUE_TYPE_PE_CEQ		0x2
    135   1.1  yamaguch #define I40E_QUEUE_TYPE_UNKNOWN		0x3
    136   1.1  yamaguch 
    137   1.1  yamaguch #define I40E_ITR_INDEX_RX		0x0
    138   1.1  yamaguch #define I40E_ITR_INDEX_TX		0x1
    139   1.1  yamaguch #define I40E_ITR_INDEX_OTHER		0x2
    140   1.1  yamaguch #define I40E_ITR_INDEX_NONE		0x3
    141  1.64  yamaguch #define IXL_ITR_RX			0x7a /* 4K intrs/sec */
    142  1.64  yamaguch #define IXL_ITR_TX			0x7a /* 4K intrs/sec */
    143   1.1  yamaguch 
    144   1.1  yamaguch #define I40E_INTR_NOTX_QUEUE		0
    145   1.1  yamaguch #define I40E_INTR_NOTX_INTR		0
    146   1.1  yamaguch #define I40E_INTR_NOTX_RX_QUEUE		0
    147   1.1  yamaguch #define I40E_INTR_NOTX_TX_QUEUE		1
    148   1.1  yamaguch #define I40E_INTR_NOTX_RX_MASK		I40E_PFINT_ICR0_QUEUE_0_MASK
    149   1.1  yamaguch #define I40E_INTR_NOTX_TX_MASK		I40E_PFINT_ICR0_QUEUE_1_MASK
    150   1.1  yamaguch 
    151   1.1  yamaguch #define BIT_ULL(a)	(1ULL << (a))
    152   1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_BASE			\
    153   1.1  yamaguch 	(BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |	\
    154   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |	\
    155   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |	\
    156   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |	\
    157   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4) |	\
    158   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |	\
    159   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |	\
    160   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |	\
    161   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |	\
    162   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6) |	\
    163   1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_L2_PAYLOAD))
    164   1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_XL710	IXL_RSS_HENA_DEFAULT_BASE
    165   1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_X722	(IXL_RSS_HENA_DEFAULT_XL710 |	\
    166   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |		\
    167   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |		\
    168   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |		\
    169   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |		\
    170   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) |	\
    171   1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK))
    172   1.1  yamaguch #define I40E_HASH_LUT_SIZE_128		0
    173   1.1  yamaguch #define IXL_RSS_KEY_SIZE_REG		13
    174   1.1  yamaguch 
    175   1.1  yamaguch #define IXL_ICR0_CRIT_ERR_MASK			\
    176   1.1  yamaguch 	(I40E_PFINT_ICR0_PCI_EXCEPTION_MASK |	\
    177   1.1  yamaguch 	I40E_PFINT_ICR0_ECC_ERR_MASK |		\
    178   1.1  yamaguch 	I40E_PFINT_ICR0_PE_CRITERR_MASK)
    179   1.1  yamaguch 
    180  1.47  yamaguch #define IXL_QUEUE_MAX_XL710		64
    181  1.47  yamaguch #define IXL_QUEUE_MAX_X722		128
    182  1.47  yamaguch 
    183   1.1  yamaguch #define IXL_TX_PKT_DESCS		8
    184  1.29  yamaguch #define IXL_TX_PKT_MAXSIZE		(MCLBYTES * IXL_TX_PKT_DESCS)
    185   1.1  yamaguch #define IXL_TX_QUEUE_ALIGN		128
    186   1.1  yamaguch #define IXL_RX_QUEUE_ALIGN		128
    187   1.1  yamaguch 
    188  1.29  yamaguch #define IXL_MCLBYTES			(MCLBYTES - ETHER_ALIGN)
    189  1.29  yamaguch #define IXL_MTU_ETHERLEN		ETHER_HDR_LEN		\
    190  1.29  yamaguch 					+ ETHER_CRC_LEN
    191  1.29  yamaguch #if 0
    192  1.29  yamaguch #define IXL_MAX_MTU			(9728 - IXL_MTU_ETHERLEN)
    193  1.29  yamaguch #else
    194  1.29  yamaguch /* (dbuff * 5) - ETHER_HDR_LEN - ETHER_CRC_LEN */
    195  1.29  yamaguch #define IXL_MAX_MTU			(9600 - IXL_MTU_ETHERLEN)
    196  1.29  yamaguch #endif
    197  1.29  yamaguch #define IXL_MIN_MTU			(ETHER_MIN_LEN - ETHER_CRC_LEN)
    198   1.1  yamaguch 
    199   1.1  yamaguch #define IXL_PCIREG			PCI_MAPREG_START
    200   1.1  yamaguch 
    201   1.1  yamaguch #define IXL_ITR0			0x0
    202   1.1  yamaguch #define IXL_ITR1			0x1
    203   1.1  yamaguch #define IXL_ITR2			0x2
    204   1.1  yamaguch #define IXL_NOITR			0x3
    205   1.1  yamaguch 
    206   1.1  yamaguch #define IXL_AQ_NUM			256
    207   1.1  yamaguch #define IXL_AQ_MASK			(IXL_AQ_NUM - 1)
    208   1.1  yamaguch #define IXL_AQ_ALIGN			64 /* lol */
    209   1.1  yamaguch #define IXL_AQ_BUFLEN			4096
    210   1.1  yamaguch 
    211   1.1  yamaguch #define IXL_HMC_ROUNDUP			512
    212   1.1  yamaguch #define IXL_HMC_PGSIZE			4096
    213   1.1  yamaguch #define IXL_HMC_DVASZ			sizeof(uint64_t)
    214   1.1  yamaguch #define IXL_HMC_PGS			(IXL_HMC_PGSIZE / IXL_HMC_DVASZ)
    215   1.1  yamaguch #define IXL_HMC_L2SZ			(IXL_HMC_PGSIZE * IXL_HMC_PGS)
    216   1.1  yamaguch #define IXL_HMC_PDVALID			1ULL
    217   1.1  yamaguch 
    218   1.1  yamaguch #define IXL_ATQ_EXEC_TIMEOUT		(10 * hz)
    219   1.1  yamaguch 
    220  1.26  yamaguch #define IXL_SRRD_SRCTL_ATTEMPTS		100000
    221  1.26  yamaguch 
    222   1.1  yamaguch struct ixl_aq_regs {
    223   1.1  yamaguch 	bus_size_t		atq_tail;
    224   1.1  yamaguch 	bus_size_t		atq_head;
    225   1.1  yamaguch 	bus_size_t		atq_len;
    226   1.1  yamaguch 	bus_size_t		atq_bal;
    227   1.1  yamaguch 	bus_size_t		atq_bah;
    228   1.1  yamaguch 
    229   1.1  yamaguch 	bus_size_t		arq_tail;
    230   1.1  yamaguch 	bus_size_t		arq_head;
    231   1.1  yamaguch 	bus_size_t		arq_len;
    232   1.1  yamaguch 	bus_size_t		arq_bal;
    233   1.1  yamaguch 	bus_size_t		arq_bah;
    234   1.1  yamaguch 
    235   1.1  yamaguch 	uint32_t		atq_len_enable;
    236   1.1  yamaguch 	uint32_t		atq_tail_mask;
    237   1.1  yamaguch 	uint32_t		atq_head_mask;
    238   1.1  yamaguch 
    239   1.1  yamaguch 	uint32_t		arq_len_enable;
    240   1.1  yamaguch 	uint32_t		arq_tail_mask;
    241   1.1  yamaguch 	uint32_t		arq_head_mask;
    242   1.1  yamaguch };
    243   1.1  yamaguch 
    244   1.1  yamaguch struct ixl_phy_type {
    245   1.1  yamaguch 	uint64_t	phy_type;
    246   1.1  yamaguch 	uint64_t	ifm_type;
    247   1.1  yamaguch };
    248   1.1  yamaguch 
    249   1.1  yamaguch struct ixl_speed_type {
    250   1.1  yamaguch 	uint8_t		dev_speed;
    251   1.1  yamaguch 	uint64_t	net_speed;
    252   1.1  yamaguch };
    253   1.1  yamaguch 
    254   1.1  yamaguch struct ixl_aq_buf {
    255   1.1  yamaguch 	SIMPLEQ_ENTRY(ixl_aq_buf)
    256   1.1  yamaguch 				 aqb_entry;
    257   1.1  yamaguch 	void			*aqb_data;
    258   1.1  yamaguch 	bus_dmamap_t		 aqb_map;
    259   1.1  yamaguch 	bus_dma_segment_t	 aqb_seg;
    260   1.1  yamaguch 	size_t			 aqb_size;
    261   1.1  yamaguch 	int			 aqb_nsegs;
    262   1.1  yamaguch };
    263   1.1  yamaguch SIMPLEQ_HEAD(ixl_aq_bufs, ixl_aq_buf);
    264   1.1  yamaguch 
    265   1.1  yamaguch struct ixl_dmamem {
    266   1.1  yamaguch 	bus_dmamap_t		 ixm_map;
    267   1.1  yamaguch 	bus_dma_segment_t	 ixm_seg;
    268   1.1  yamaguch 	int			 ixm_nsegs;
    269   1.1  yamaguch 	size_t			 ixm_size;
    270   1.1  yamaguch 	void			*ixm_kva;
    271   1.1  yamaguch };
    272   1.1  yamaguch 
    273   1.1  yamaguch #define IXL_DMA_MAP(_ixm)	((_ixm)->ixm_map)
    274   1.1  yamaguch #define IXL_DMA_DVA(_ixm)	((_ixm)->ixm_map->dm_segs[0].ds_addr)
    275   1.1  yamaguch #define IXL_DMA_KVA(_ixm)	((void *)(_ixm)->ixm_kva)
    276   1.1  yamaguch #define IXL_DMA_LEN(_ixm)	((_ixm)->ixm_size)
    277   1.1  yamaguch 
    278   1.1  yamaguch struct ixl_hmc_entry {
    279   1.1  yamaguch 	uint64_t		 hmc_base;
    280   1.1  yamaguch 	uint32_t		 hmc_count;
    281   1.1  yamaguch 	uint64_t		 hmc_size;
    282   1.1  yamaguch };
    283   1.1  yamaguch 
    284   1.1  yamaguch enum  ixl_hmc_types {
    285   1.1  yamaguch 	IXL_HMC_LAN_TX = 0,
    286   1.1  yamaguch 	IXL_HMC_LAN_RX,
    287   1.1  yamaguch 	IXL_HMC_FCOE_CTX,
    288   1.1  yamaguch 	IXL_HMC_FCOE_FILTER,
    289   1.1  yamaguch 	IXL_HMC_COUNT
    290   1.1  yamaguch };
    291   1.1  yamaguch 
    292   1.1  yamaguch struct ixl_hmc_pack {
    293   1.1  yamaguch 	uint16_t		offset;
    294   1.1  yamaguch 	uint16_t		width;
    295   1.1  yamaguch 	uint16_t		lsb;
    296   1.1  yamaguch };
    297   1.1  yamaguch 
    298   1.1  yamaguch /*
    299   1.1  yamaguch  * these hmc objects have weird sizes and alignments, so these are abstract
    300   1.1  yamaguch  * representations of them that are nice for c to populate.
    301   1.1  yamaguch  *
    302   1.1  yamaguch  * the packing code relies on little-endian values being stored in the fields,
    303   1.1  yamaguch  * no high bits in the fields being set, and the fields must be packed in the
    304   1.1  yamaguch  * same order as they are in the ctx structure.
    305   1.1  yamaguch  */
    306   1.1  yamaguch 
    307   1.1  yamaguch struct ixl_hmc_rxq {
    308   1.1  yamaguch 	uint16_t		 head;
    309   1.1  yamaguch 	uint8_t			 cpuid;
    310   1.1  yamaguch 	uint64_t		 base;
    311   1.1  yamaguch #define IXL_HMC_RXQ_BASE_UNIT		128
    312   1.1  yamaguch 	uint16_t		 qlen;
    313   1.1  yamaguch 	uint16_t		 dbuff;
    314   1.1  yamaguch #define IXL_HMC_RXQ_DBUFF_UNIT		128
    315   1.1  yamaguch 	uint8_t			 hbuff;
    316   1.1  yamaguch #define IXL_HMC_RXQ_HBUFF_UNIT		64
    317   1.1  yamaguch 	uint8_t			 dtype;
    318   1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_NOSPLIT	0x0
    319   1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_HSPLIT	0x1
    320   1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_SPLIT_ALWAYS	0x2
    321   1.1  yamaguch 	uint8_t			 dsize;
    322   1.1  yamaguch #define IXL_HMC_RXQ_DSIZE_16		0
    323   1.1  yamaguch #define IXL_HMC_RXQ_DSIZE_32		1
    324   1.1  yamaguch 	uint8_t			 crcstrip;
    325   1.1  yamaguch 	uint8_t			 fc_ena;
    326   1.1  yamaguch 	uint8_t			 l2sel;
    327   1.1  yamaguch 	uint8_t			 hsplit_0;
    328   1.1  yamaguch 	uint8_t			 hsplit_1;
    329   1.1  yamaguch 	uint8_t			 showiv;
    330   1.1  yamaguch 	uint16_t		 rxmax;
    331   1.1  yamaguch 	uint8_t			 tphrdesc_ena;
    332   1.1  yamaguch 	uint8_t			 tphwdesc_ena;
    333   1.1  yamaguch 	uint8_t			 tphdata_ena;
    334   1.1  yamaguch 	uint8_t			 tphhead_ena;
    335   1.1  yamaguch 	uint8_t			 lrxqthresh;
    336   1.1  yamaguch 	uint8_t			 prefena;
    337   1.1  yamaguch };
    338   1.1  yamaguch 
    339   1.1  yamaguch static const struct ixl_hmc_pack ixl_hmc_pack_rxq[] = {
    340   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, head),		13,	0 },
    341   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, cpuid),		8,	13 },
    342   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, base),		57,	32 },
    343   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, qlen),		13,	89 },
    344   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dbuff),		7,	102 },
    345   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hbuff),		5,	109 },
    346   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dtype),		2,	114 },
    347   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dsize),		1,	116 },
    348   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, crcstrip),	1,	117 },
    349   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, fc_ena),		1,	118 },
    350   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, l2sel),		1,	119 },
    351   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hsplit_0),	4,	120 },
    352   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hsplit_1),	2,	124 },
    353   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, showiv),		1,	127 },
    354   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, rxmax),		14,	174 },
    355   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphrdesc_ena),	1,	193 },
    356   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphwdesc_ena),	1,	194 },
    357   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphdata_ena),	1,	195 },
    358   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphhead_ena),	1,	196 },
    359   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, lrxqthresh),	3,	198 },
    360   1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, prefena),	1,	201 },
    361   1.1  yamaguch };
    362   1.1  yamaguch 
    363   1.1  yamaguch #define IXL_HMC_RXQ_MINSIZE (201 + 1)
    364   1.1  yamaguch 
    365   1.1  yamaguch struct ixl_hmc_txq {
    366   1.1  yamaguch 	uint16_t		head;
    367   1.1  yamaguch 	uint8_t			new_context;
    368   1.1  yamaguch 	uint64_t		base;
    369   1.1  yamaguch #define IXL_HMC_TXQ_BASE_UNIT		128
    370   1.1  yamaguch 	uint8_t			fc_ena;
    371   1.1  yamaguch 	uint8_t			timesync_ena;
    372   1.1  yamaguch 	uint8_t			fd_ena;
    373   1.1  yamaguch 	uint8_t			alt_vlan_ena;
    374  1.49  yamaguch 	uint8_t			cpuid;
    375   1.1  yamaguch 	uint16_t		thead_wb;
    376   1.1  yamaguch 	uint8_t			head_wb_ena;
    377   1.1  yamaguch #define IXL_HMC_TXQ_DESC_WB		0
    378   1.1  yamaguch #define IXL_HMC_TXQ_HEAD_WB		1
    379   1.1  yamaguch 	uint16_t		qlen;
    380   1.1  yamaguch 	uint8_t			tphrdesc_ena;
    381   1.1  yamaguch 	uint8_t			tphrpacket_ena;
    382   1.1  yamaguch 	uint8_t			tphwdesc_ena;
    383   1.1  yamaguch 	uint64_t		head_wb_addr;
    384   1.1  yamaguch 	uint32_t		crc;
    385   1.1  yamaguch 	uint16_t		rdylist;
    386   1.1  yamaguch 	uint8_t			rdylist_act;
    387   1.1  yamaguch };
    388   1.1  yamaguch 
    389   1.1  yamaguch static const struct ixl_hmc_pack ixl_hmc_pack_txq[] = {
    390   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head),		13,	0 },
    391   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, new_context),	1,	30 },
    392   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, base),		57,	32 },
    393   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, fc_ena),		1,	89 },
    394   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, timesync_ena),	1,	90 },
    395   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, fd_ena),		1,	91 },
    396   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, alt_vlan_ena),	1,	92 },
    397   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, cpuid),		8,	96 },
    398   1.1  yamaguch /* line 1 */
    399   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, thead_wb),	13,	0 + 128 },
    400   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head_wb_ena),	1,	32 + 128 },
    401   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, qlen),		13,	33 + 128 },
    402   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphrdesc_ena),	1,	46 + 128 },
    403   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphrpacket_ena),	1,	47 + 128 },
    404   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphwdesc_ena),	1,	48 + 128 },
    405   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head_wb_addr),	64,	64 + 128 },
    406   1.1  yamaguch /* line 7 */
    407   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, crc),		32,	0 + (7*128) },
    408   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, rdylist),	10,	84 + (7*128) },
    409   1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, rdylist_act),	1,	94 + (7*128) },
    410   1.1  yamaguch };
    411   1.1  yamaguch 
    412   1.1  yamaguch #define IXL_HMC_TXQ_MINSIZE (94 + (7*128) + 1)
    413   1.1  yamaguch 
    414   1.1  yamaguch struct ixl_work {
    415   1.1  yamaguch 	struct work	 ixw_cookie;
    416   1.1  yamaguch 	void		(*ixw_func)(void *);
    417   1.1  yamaguch 	void		*ixw_arg;
    418   1.1  yamaguch 	unsigned int	 ixw_added;
    419   1.1  yamaguch };
    420   1.1  yamaguch #define IXL_WORKQUEUE_PRI	PRI_SOFTNET
    421   1.1  yamaguch 
    422   1.1  yamaguch struct ixl_tx_map {
    423   1.1  yamaguch 	struct mbuf		*txm_m;
    424   1.1  yamaguch 	bus_dmamap_t		 txm_map;
    425   1.1  yamaguch 	unsigned int		 txm_eop;
    426   1.1  yamaguch };
    427   1.1  yamaguch 
    428   1.1  yamaguch struct ixl_tx_ring {
    429   1.1  yamaguch 	kmutex_t		 txr_lock;
    430   1.1  yamaguch 	struct ixl_softc	*txr_sc;
    431   1.1  yamaguch 
    432   1.1  yamaguch 	unsigned int		 txr_prod;
    433   1.1  yamaguch 	unsigned int		 txr_cons;
    434   1.1  yamaguch 
    435   1.1  yamaguch 	struct ixl_tx_map	*txr_maps;
    436   1.1  yamaguch 	struct ixl_dmamem	 txr_mem;
    437   1.1  yamaguch 
    438   1.1  yamaguch 	bus_size_t		 txr_tail;
    439   1.1  yamaguch 	unsigned int		 txr_qid;
    440   1.1  yamaguch 	pcq_t			*txr_intrq;
    441   1.1  yamaguch 	void			*txr_si;
    442   1.1  yamaguch 
    443   1.1  yamaguch 	struct evcnt		 txr_defragged;
    444   1.1  yamaguch 	struct evcnt		 txr_defrag_failed;
    445   1.1  yamaguch 	struct evcnt		 txr_pcqdrop;
    446   1.1  yamaguch 	struct evcnt		 txr_transmitdef;
    447   1.1  yamaguch 	struct evcnt		 txr_intr;
    448   1.1  yamaguch 	struct evcnt		 txr_defer;
    449   1.1  yamaguch };
    450   1.1  yamaguch 
    451   1.1  yamaguch struct ixl_rx_map {
    452   1.1  yamaguch 	struct mbuf		*rxm_m;
    453   1.1  yamaguch 	bus_dmamap_t		 rxm_map;
    454   1.1  yamaguch };
    455   1.1  yamaguch 
    456   1.1  yamaguch struct ixl_rx_ring {
    457   1.1  yamaguch 	kmutex_t		 rxr_lock;
    458   1.1  yamaguch 
    459   1.1  yamaguch 	unsigned int		 rxr_prod;
    460   1.1  yamaguch 	unsigned int		 rxr_cons;
    461   1.1  yamaguch 
    462   1.1  yamaguch 	struct ixl_rx_map	*rxr_maps;
    463   1.1  yamaguch 	struct ixl_dmamem	 rxr_mem;
    464   1.1  yamaguch 
    465   1.1  yamaguch 	struct mbuf		*rxr_m_head;
    466   1.1  yamaguch 	struct mbuf		**rxr_m_tail;
    467   1.1  yamaguch 
    468   1.1  yamaguch 	bus_size_t		 rxr_tail;
    469   1.1  yamaguch 	unsigned int		 rxr_qid;
    470   1.1  yamaguch 
    471   1.1  yamaguch 	struct evcnt		 rxr_mgethdr_failed;
    472   1.1  yamaguch 	struct evcnt		 rxr_mgetcl_failed;
    473   1.1  yamaguch 	struct evcnt		 rxr_mbuf_load_failed;
    474   1.1  yamaguch 	struct evcnt		 rxr_intr;
    475   1.1  yamaguch 	struct evcnt		 rxr_defer;
    476   1.1  yamaguch };
    477   1.1  yamaguch 
    478   1.1  yamaguch struct ixl_queue_pair {
    479   1.1  yamaguch 	struct ixl_softc	*qp_sc;
    480   1.1  yamaguch 	struct ixl_tx_ring	*qp_txr;
    481   1.1  yamaguch 	struct ixl_rx_ring	*qp_rxr;
    482   1.1  yamaguch 
    483   1.1  yamaguch 	char			 qp_name[16];
    484   1.1  yamaguch 
    485   1.1  yamaguch 	void			*qp_si;
    486  1.45  yamaguch 	struct work		 qp_work;
    487   1.1  yamaguch 	bool			 qp_workqueue;
    488   1.1  yamaguch };
    489   1.1  yamaguch 
    490   1.1  yamaguch struct ixl_atq {
    491   1.1  yamaguch 	struct ixl_aq_desc	 iatq_desc;
    492   1.4  yamaguch 	void			(*iatq_fn)(struct ixl_softc *,
    493   1.4  yamaguch 				    const struct ixl_aq_desc *);
    494   1.1  yamaguch };
    495   1.1  yamaguch SIMPLEQ_HEAD(ixl_atq_list, ixl_atq);
    496   1.1  yamaguch 
    497   1.1  yamaguch struct ixl_product {
    498   1.1  yamaguch 	unsigned int	 vendor_id;
    499   1.1  yamaguch 	unsigned int	 product_id;
    500   1.1  yamaguch };
    501   1.1  yamaguch 
    502  1.17  yamaguch struct ixl_stats_counters {
    503  1.17  yamaguch 	bool		 isc_has_offset;
    504  1.17  yamaguch 	struct evcnt	 isc_crc_errors;
    505  1.17  yamaguch 	uint64_t	 isc_crc_errors_offset;
    506  1.17  yamaguch 	struct evcnt	 isc_illegal_bytes;
    507  1.17  yamaguch 	uint64_t	 isc_illegal_bytes_offset;
    508  1.17  yamaguch 	struct evcnt	 isc_rx_bytes;
    509  1.17  yamaguch 	uint64_t	 isc_rx_bytes_offset;
    510  1.17  yamaguch 	struct evcnt	 isc_rx_discards;
    511  1.17  yamaguch 	uint64_t	 isc_rx_discards_offset;
    512  1.17  yamaguch 	struct evcnt	 isc_rx_unicast;
    513  1.17  yamaguch 	uint64_t	 isc_rx_unicast_offset;
    514  1.17  yamaguch 	struct evcnt	 isc_rx_multicast;
    515  1.17  yamaguch 	uint64_t	 isc_rx_multicast_offset;
    516  1.17  yamaguch 	struct evcnt	 isc_rx_broadcast;
    517  1.17  yamaguch 	uint64_t	 isc_rx_broadcast_offset;
    518  1.17  yamaguch 	struct evcnt	 isc_rx_size_64;
    519  1.17  yamaguch 	uint64_t	 isc_rx_size_64_offset;
    520  1.17  yamaguch 	struct evcnt	 isc_rx_size_127;
    521  1.17  yamaguch 	uint64_t	 isc_rx_size_127_offset;
    522  1.17  yamaguch 	struct evcnt	 isc_rx_size_255;
    523  1.17  yamaguch 	uint64_t	 isc_rx_size_255_offset;
    524  1.17  yamaguch 	struct evcnt	 isc_rx_size_511;
    525  1.17  yamaguch 	uint64_t	 isc_rx_size_511_offset;
    526  1.17  yamaguch 	struct evcnt	 isc_rx_size_1023;
    527  1.17  yamaguch 	uint64_t	 isc_rx_size_1023_offset;
    528  1.17  yamaguch 	struct evcnt	 isc_rx_size_1522;
    529  1.17  yamaguch 	uint64_t	 isc_rx_size_1522_offset;
    530  1.17  yamaguch 	struct evcnt	 isc_rx_size_big;
    531  1.17  yamaguch 	uint64_t	 isc_rx_size_big_offset;
    532  1.17  yamaguch 	struct evcnt	 isc_rx_undersize;
    533  1.17  yamaguch 	uint64_t	 isc_rx_undersize_offset;
    534  1.17  yamaguch 	struct evcnt	 isc_rx_oversize;
    535  1.17  yamaguch 	uint64_t	 isc_rx_oversize_offset;
    536  1.17  yamaguch 	struct evcnt	 isc_rx_fragments;
    537  1.17  yamaguch 	uint64_t	 isc_rx_fragments_offset;
    538  1.17  yamaguch 	struct evcnt	 isc_rx_jabber;
    539  1.17  yamaguch 	uint64_t	 isc_rx_jabber_offset;
    540  1.17  yamaguch 	struct evcnt	 isc_tx_bytes;
    541  1.17  yamaguch 	uint64_t	 isc_tx_bytes_offset;
    542  1.17  yamaguch 	struct evcnt	 isc_tx_dropped_link_down;
    543  1.17  yamaguch 	uint64_t	 isc_tx_dropped_link_down_offset;
    544  1.17  yamaguch 	struct evcnt	 isc_tx_unicast;
    545  1.17  yamaguch 	uint64_t	 isc_tx_unicast_offset;
    546  1.17  yamaguch 	struct evcnt	 isc_tx_multicast;
    547  1.17  yamaguch 	uint64_t	 isc_tx_multicast_offset;
    548  1.17  yamaguch 	struct evcnt	 isc_tx_broadcast;
    549  1.17  yamaguch 	uint64_t	 isc_tx_broadcast_offset;
    550  1.17  yamaguch 	struct evcnt	 isc_tx_size_64;
    551  1.17  yamaguch 	uint64_t	 isc_tx_size_64_offset;
    552  1.17  yamaguch 	struct evcnt	 isc_tx_size_127;
    553  1.17  yamaguch 	uint64_t	 isc_tx_size_127_offset;
    554  1.17  yamaguch 	struct evcnt	 isc_tx_size_255;
    555  1.17  yamaguch 	uint64_t	 isc_tx_size_255_offset;
    556  1.17  yamaguch 	struct evcnt	 isc_tx_size_511;
    557  1.17  yamaguch 	uint64_t	 isc_tx_size_511_offset;
    558  1.17  yamaguch 	struct evcnt	 isc_tx_size_1023;
    559  1.17  yamaguch 	uint64_t	 isc_tx_size_1023_offset;
    560  1.17  yamaguch 	struct evcnt	 isc_tx_size_1522;
    561  1.17  yamaguch 	uint64_t	 isc_tx_size_1522_offset;
    562  1.17  yamaguch 	struct evcnt	 isc_tx_size_big;
    563  1.17  yamaguch 	uint64_t	 isc_tx_size_big_offset;
    564  1.17  yamaguch 	struct evcnt	 isc_mac_local_faults;
    565  1.17  yamaguch 	uint64_t	 isc_mac_local_faults_offset;
    566  1.17  yamaguch 	struct evcnt	 isc_mac_remote_faults;
    567  1.17  yamaguch 	uint64_t	 isc_mac_remote_faults_offset;
    568  1.17  yamaguch 	struct evcnt	 isc_link_xon_rx;
    569  1.17  yamaguch 	uint64_t	 isc_link_xon_rx_offset;
    570  1.17  yamaguch 	struct evcnt	 isc_link_xon_tx;
    571  1.17  yamaguch 	uint64_t	 isc_link_xon_tx_offset;
    572  1.17  yamaguch 	struct evcnt	 isc_link_xoff_rx;
    573  1.17  yamaguch 	uint64_t	 isc_link_xoff_rx_offset;
    574  1.17  yamaguch 	struct evcnt	 isc_link_xoff_tx;
    575  1.17  yamaguch 	uint64_t	 isc_link_xoff_tx_offset;
    576  1.18  yamaguch 	struct evcnt	 isc_vsi_rx_discards;
    577  1.18  yamaguch 	uint64_t	 isc_vsi_rx_discards_offset;
    578  1.18  yamaguch 	struct evcnt	 isc_vsi_rx_bytes;
    579  1.18  yamaguch 	uint64_t	 isc_vsi_rx_bytes_offset;
    580  1.18  yamaguch 	struct evcnt	 isc_vsi_rx_unicast;
    581  1.18  yamaguch 	uint64_t	 isc_vsi_rx_unicast_offset;
    582  1.18  yamaguch 	struct evcnt	 isc_vsi_rx_multicast;
    583  1.18  yamaguch 	uint64_t	 isc_vsi_rx_multicast_offset;
    584  1.18  yamaguch 	struct evcnt	 isc_vsi_rx_broadcast;
    585  1.18  yamaguch 	uint64_t	 isc_vsi_rx_broadcast_offset;
    586  1.18  yamaguch 	struct evcnt	 isc_vsi_tx_errors;
    587  1.18  yamaguch 	uint64_t	 isc_vsi_tx_errors_offset;
    588  1.18  yamaguch 	struct evcnt	 isc_vsi_tx_bytes;
    589  1.18  yamaguch 	uint64_t	 isc_vsi_tx_bytes_offset;
    590  1.18  yamaguch 	struct evcnt	 isc_vsi_tx_unicast;
    591  1.18  yamaguch 	uint64_t	 isc_vsi_tx_unicast_offset;
    592  1.18  yamaguch 	struct evcnt	 isc_vsi_tx_multicast;
    593  1.18  yamaguch 	uint64_t	 isc_vsi_tx_multicast_offset;
    594  1.18  yamaguch 	struct evcnt	 isc_vsi_tx_broadcast;
    595  1.18  yamaguch 	uint64_t	 isc_vsi_tx_broadcast_offset;
    596  1.17  yamaguch };
    597  1.17  yamaguch 
    598   1.1  yamaguch /*
    599   1.1  yamaguch  * Locking notes:
    600   1.1  yamaguch  * + a field in ixl_tx_ring is protected by txr_lock (a spin mutex), and
    601   1.1  yamaguch  *   a field in ixl_rx_ring is protected by rxr_lock (a spin mutex).
    602   1.1  yamaguch  *    - more than one lock of them cannot be held at once.
    603   1.1  yamaguch  * + a field named sc_atq_* in ixl_softc is protected by sc_atq_lock
    604   1.1  yamaguch  *   (a spin mutex).
    605   1.1  yamaguch  *    - the lock cannot held with txr_lock or rxr_lock.
    606   1.1  yamaguch  * + a field named sc_arq_* is not protected by any lock.
    607   1.1  yamaguch  *    - operations for sc_arq_* is done in one context related to
    608   1.1  yamaguch  *      sc_arq_task.
    609   1.1  yamaguch  * + other fields in ixl_softc is protected by sc_cfg_lock
    610   1.1  yamaguch  *   (an adaptive mutex)
    611   1.1  yamaguch  *    - It must be held before another lock is held, and It can be
    612   1.1  yamaguch  *      released after the other lock is released.
    613   1.1  yamaguch  * */
    614   1.1  yamaguch 
    615   1.1  yamaguch struct ixl_softc {
    616   1.1  yamaguch 	device_t		 sc_dev;
    617   1.1  yamaguch 	struct ethercom		 sc_ec;
    618   1.1  yamaguch 	bool			 sc_attached;
    619   1.1  yamaguch 	bool			 sc_dead;
    620  1.17  yamaguch 	uint32_t		 sc_port;
    621   1.1  yamaguch 	struct sysctllog	*sc_sysctllog;
    622   1.1  yamaguch 	struct workqueue	*sc_workq;
    623   1.1  yamaguch 	struct workqueue	*sc_workq_txrx;
    624  1.17  yamaguch 	int			 sc_stats_intval;
    625  1.17  yamaguch 	callout_t		 sc_stats_callout;
    626  1.17  yamaguch 	struct ixl_work		 sc_stats_task;
    627  1.17  yamaguch 	struct ixl_stats_counters
    628  1.17  yamaguch 				 sc_stats_counters;
    629   1.1  yamaguch 	uint8_t			 sc_enaddr[ETHER_ADDR_LEN];
    630   1.1  yamaguch 	struct ifmedia		 sc_media;
    631   1.1  yamaguch 	uint64_t		 sc_media_status;
    632   1.1  yamaguch 	uint64_t		 sc_media_active;
    633  1.31  yamaguch 	uint64_t		 sc_phy_types;
    634  1.31  yamaguch 	uint8_t			 sc_phy_abilities;
    635  1.31  yamaguch 	uint8_t			 sc_phy_linkspeed;
    636  1.31  yamaguch 	uint8_t			 sc_phy_fec_cfg;
    637  1.31  yamaguch 	uint16_t		 sc_eee_cap;
    638  1.31  yamaguch 	uint32_t		 sc_eeer_val;
    639  1.31  yamaguch 	uint8_t			 sc_d3_lpan;
    640   1.1  yamaguch 	kmutex_t		 sc_cfg_lock;
    641   1.1  yamaguch 	enum i40e_mac_type	 sc_mac_type;
    642   1.1  yamaguch 	uint32_t		 sc_rss_table_size;
    643   1.1  yamaguch 	uint32_t		 sc_rss_table_entry_width;
    644   1.1  yamaguch 	bool			 sc_txrx_workqueue;
    645   1.1  yamaguch 	u_int			 sc_tx_process_limit;
    646   1.1  yamaguch 	u_int			 sc_rx_process_limit;
    647   1.1  yamaguch 	u_int			 sc_tx_intr_process_limit;
    648   1.1  yamaguch 	u_int			 sc_rx_intr_process_limit;
    649   1.1  yamaguch 
    650  1.11  yamaguch 	int			 sc_cur_ec_capenable;
    651  1.11  yamaguch 
    652   1.1  yamaguch 	struct pci_attach_args	 sc_pa;
    653   1.1  yamaguch 	pci_intr_handle_t	*sc_ihp;
    654   1.1  yamaguch 	void			**sc_ihs;
    655   1.1  yamaguch 	unsigned int		 sc_nintrs;
    656   1.1  yamaguch 
    657   1.1  yamaguch 	bus_dma_tag_t		 sc_dmat;
    658   1.1  yamaguch 	bus_space_tag_t		 sc_memt;
    659   1.1  yamaguch 	bus_space_handle_t	 sc_memh;
    660   1.1  yamaguch 	bus_size_t		 sc_mems;
    661   1.1  yamaguch 
    662   1.1  yamaguch 	uint8_t			 sc_pf_id;
    663   1.1  yamaguch 	uint16_t		 sc_uplink_seid;	/* le */
    664   1.1  yamaguch 	uint16_t		 sc_downlink_seid;	/* le */
    665  1.41  yamaguch 	uint16_t		 sc_vsi_number;
    666  1.18  yamaguch 	uint16_t		 sc_vsi_stat_counter_idx;
    667   1.1  yamaguch 	uint16_t		 sc_seid;
    668   1.1  yamaguch 	unsigned int		 sc_base_queue;
    669   1.1  yamaguch 
    670   1.1  yamaguch 	pci_intr_type_t		 sc_intrtype;
    671   1.1  yamaguch 	unsigned int		 sc_msix_vector_queue;
    672   1.1  yamaguch 
    673   1.1  yamaguch 	struct ixl_dmamem	 sc_scratch;
    674  1.26  yamaguch 	struct ixl_dmamem	 sc_aqbuf;
    675   1.1  yamaguch 
    676   1.1  yamaguch 	const struct ixl_aq_regs *
    677   1.1  yamaguch 				 sc_aq_regs;
    678  1.25  yamaguch 	uint32_t		 sc_aq_flags;
    679  1.25  yamaguch #define IXL_SC_AQ_FLAG_RXCTL	__BIT(0)
    680  1.26  yamaguch #define IXL_SC_AQ_FLAG_NVMLOCK	__BIT(1)
    681  1.26  yamaguch #define IXL_SC_AQ_FLAG_NVMREAD	__BIT(2)
    682  1.41  yamaguch #define IXL_SC_AQ_FLAG_RSS	__BIT(3)
    683   1.1  yamaguch 
    684   1.1  yamaguch 	kmutex_t		 sc_atq_lock;
    685   1.1  yamaguch 	kcondvar_t		 sc_atq_cv;
    686   1.1  yamaguch 	struct ixl_dmamem	 sc_atq;
    687   1.1  yamaguch 	unsigned int		 sc_atq_prod;
    688   1.1  yamaguch 	unsigned int		 sc_atq_cons;
    689   1.1  yamaguch 
    690   1.1  yamaguch 	struct ixl_dmamem	 sc_arq;
    691   1.1  yamaguch 	struct ixl_work		 sc_arq_task;
    692   1.1  yamaguch 	struct ixl_aq_bufs	 sc_arq_idle;
    693   1.1  yamaguch 	struct ixl_aq_buf	*sc_arq_live[IXL_AQ_NUM];
    694   1.1  yamaguch 	unsigned int		 sc_arq_prod;
    695   1.1  yamaguch 	unsigned int		 sc_arq_cons;
    696   1.1  yamaguch 
    697   1.1  yamaguch 	struct ixl_work		 sc_link_state_task;
    698   1.1  yamaguch 	struct ixl_atq		 sc_link_state_atq;
    699   1.1  yamaguch 
    700   1.1  yamaguch 	struct ixl_dmamem	 sc_hmc_sd;
    701   1.1  yamaguch 	struct ixl_dmamem	 sc_hmc_pd;
    702   1.1  yamaguch 	struct ixl_hmc_entry	 sc_hmc_entries[IXL_HMC_COUNT];
    703   1.1  yamaguch 
    704  1.71  yamaguch 	struct if_percpuq	*sc_ipq;
    705   1.1  yamaguch 	unsigned int		 sc_tx_ring_ndescs;
    706   1.1  yamaguch 	unsigned int		 sc_rx_ring_ndescs;
    707   1.1  yamaguch 	unsigned int		 sc_nqueue_pairs;
    708   1.1  yamaguch 	unsigned int		 sc_nqueue_pairs_max;
    709   1.1  yamaguch 	unsigned int		 sc_nqueue_pairs_device;
    710   1.1  yamaguch 	struct ixl_queue_pair	*sc_qps;
    711  1.64  yamaguch 	uint32_t		 sc_itr_rx;
    712  1.64  yamaguch 	uint32_t		 sc_itr_tx;
    713   1.1  yamaguch 
    714   1.1  yamaguch 	struct evcnt		 sc_event_atq;
    715   1.1  yamaguch 	struct evcnt		 sc_event_link;
    716   1.1  yamaguch 	struct evcnt		 sc_event_ecc_err;
    717   1.1  yamaguch 	struct evcnt		 sc_event_pci_exception;
    718   1.1  yamaguch 	struct evcnt		 sc_event_crit_err;
    719   1.1  yamaguch };
    720   1.1  yamaguch 
    721   1.1  yamaguch #define IXL_TXRX_PROCESS_UNLIMIT	UINT_MAX
    722   1.1  yamaguch #define IXL_TX_PROCESS_LIMIT		256
    723   1.1  yamaguch #define IXL_RX_PROCESS_LIMIT		256
    724   1.1  yamaguch #define IXL_TX_INTR_PROCESS_LIMIT	256
    725   1.1  yamaguch #define IXL_RX_INTR_PROCESS_LIMIT	0U
    726   1.1  yamaguch 
    727  1.28  yamaguch #define IXL_IFCAP_RXCSUM	(IFCAP_CSUM_IPv4_Rx |	\
    728  1.28  yamaguch 				 IFCAP_CSUM_TCPv4_Rx |	\
    729  1.28  yamaguch 				 IFCAP_CSUM_UDPv4_Rx |	\
    730  1.28  yamaguch 				 IFCAP_CSUM_TCPv6_Rx |	\
    731  1.14  yamaguch 				 IFCAP_CSUM_UDPv6_Rx)
    732  1.28  yamaguch #define IXL_IFCAP_TXCSUM	(IFCAP_CSUM_IPv4_Tx |	\
    733  1.28  yamaguch 				 IFCAP_CSUM_TCPv4_Tx |	\
    734  1.28  yamaguch 				 IFCAP_CSUM_UDPv4_Tx |	\
    735  1.28  yamaguch 				 IFCAP_CSUM_TCPv6_Tx |	\
    736  1.28  yamaguch 				 IFCAP_CSUM_UDPv6_Tx)
    737  1.28  yamaguch #define IXL_CSUM_ALL_OFFLOAD	(M_CSUM_IPv4 |			\
    738  1.28  yamaguch 				 M_CSUM_TCPv4 | M_CSUM_TCPv6 |	\
    739  1.28  yamaguch 				 M_CSUM_UDPv4 | M_CSUM_UDPv6)
    740  1.14  yamaguch 
    741   1.1  yamaguch #define delaymsec(_x)	DELAY(1000 * (_x))
    742   1.1  yamaguch #ifdef IXL_DEBUG
    743  1.13  yamaguch #define DDPRINTF(sc, fmt, args...)			\
    744  1.13  yamaguch do {							\
    745  1.13  yamaguch 	if ((sc) != NULL) {				\
    746  1.13  yamaguch 		device_printf(				\
    747  1.13  yamaguch 		    ((struct ixl_softc *)(sc))->sc_dev,	\
    748  1.13  yamaguch 		    "");				\
    749  1.13  yamaguch 	}						\
    750  1.13  yamaguch 	printf("%s:\t" fmt, __func__, ##args);		\
    751   1.1  yamaguch } while (0)
    752   1.1  yamaguch #else
    753   1.1  yamaguch #define DDPRINTF(sc, fmt, args...)	__nothing
    754   1.1  yamaguch #endif
    755  1.17  yamaguch #ifndef IXL_STATS_INTERVAL_MSEC
    756  1.17  yamaguch #define IXL_STATS_INTERVAL_MSEC	10000
    757  1.17  yamaguch #endif
    758  1.19  yamaguch #ifndef IXL_QUEUE_NUM
    759  1.19  yamaguch #define IXL_QUEUE_NUM		0
    760  1.19  yamaguch #endif
    761  1.19  yamaguch 
    762  1.19  yamaguch static bool		 ixl_param_nomsix = false;
    763  1.19  yamaguch static int		 ixl_param_stats_interval = IXL_STATS_INTERVAL_MSEC;
    764  1.19  yamaguch static int		 ixl_param_nqps_limit = IXL_QUEUE_NUM;
    765  1.73  yamaguch static unsigned int	 ixl_param_tx_ndescs = 512;
    766  1.73  yamaguch static unsigned int	 ixl_param_rx_ndescs = 256;
    767   1.1  yamaguch 
    768   1.1  yamaguch static enum i40e_mac_type
    769  1.68  yamaguch 	    ixl_mactype(pci_product_id_t);
    770  1.68  yamaguch static void	ixl_pci_csr_setup(pci_chipset_tag_t, pcitag_t);
    771   1.1  yamaguch static void	ixl_clear_hw(struct ixl_softc *);
    772   1.1  yamaguch static int	ixl_pf_reset(struct ixl_softc *);
    773   1.1  yamaguch 
    774   1.1  yamaguch static int	ixl_dmamem_alloc(struct ixl_softc *, struct ixl_dmamem *,
    775   1.1  yamaguch 		    bus_size_t, bus_size_t);
    776   1.1  yamaguch static void	ixl_dmamem_free(struct ixl_softc *, struct ixl_dmamem *);
    777   1.1  yamaguch 
    778   1.1  yamaguch static int	ixl_arq_fill(struct ixl_softc *);
    779   1.1  yamaguch static void	ixl_arq_unfill(struct ixl_softc *);
    780   1.1  yamaguch 
    781   1.1  yamaguch static int	ixl_atq_poll(struct ixl_softc *, struct ixl_aq_desc *,
    782   1.1  yamaguch 		    unsigned int);
    783   1.4  yamaguch static void	ixl_atq_set(struct ixl_atq *,
    784   1.4  yamaguch 		    void (*)(struct ixl_softc *, const struct ixl_aq_desc *));
    785   1.1  yamaguch static int	ixl_atq_post_locked(struct ixl_softc *, struct ixl_atq *);
    786   1.1  yamaguch static void	ixl_atq_done(struct ixl_softc *);
    787   1.1  yamaguch static int	ixl_atq_exec(struct ixl_softc *, struct ixl_atq *);
    788  1.59  yamaguch static int	ixl_atq_exec_locked(struct ixl_softc *, struct ixl_atq *);
    789   1.1  yamaguch static int	ixl_get_version(struct ixl_softc *);
    790  1.26  yamaguch static int	ixl_get_nvm_version(struct ixl_softc *);
    791   1.1  yamaguch static int	ixl_get_hw_capabilities(struct ixl_softc *);
    792   1.1  yamaguch static int	ixl_pxe_clear(struct ixl_softc *);
    793   1.1  yamaguch static int	ixl_lldp_shut(struct ixl_softc *);
    794   1.1  yamaguch static int	ixl_get_mac(struct ixl_softc *);
    795   1.1  yamaguch static int	ixl_get_switch_config(struct ixl_softc *);
    796   1.1  yamaguch static int	ixl_phy_mask_ints(struct ixl_softc *);
    797  1.31  yamaguch static int	ixl_get_phy_info(struct ixl_softc *);
    798  1.31  yamaguch static int	ixl_set_phy_config(struct ixl_softc *, uint8_t, uint8_t, bool);
    799  1.31  yamaguch static int	ixl_set_phy_autoselect(struct ixl_softc *);
    800   1.1  yamaguch static int	ixl_restart_an(struct ixl_softc *);
    801   1.1  yamaguch static int	ixl_hmc(struct ixl_softc *);
    802   1.1  yamaguch static void	ixl_hmc_free(struct ixl_softc *);
    803   1.1  yamaguch static int	ixl_get_vsi(struct ixl_softc *);
    804   1.1  yamaguch static int	ixl_set_vsi(struct ixl_softc *);
    805   1.1  yamaguch static void	ixl_set_filter_control(struct ixl_softc *);
    806   1.4  yamaguch static void	ixl_get_link_status(void *);
    807  1.37  yamaguch static int	ixl_get_link_status_poll(struct ixl_softc *, int *);
    808  1.57  yamaguch static void	ixl_get_link_status_done(struct ixl_softc *,
    809  1.57  yamaguch 		    const struct ixl_aq_desc *);
    810  1.62  yamaguch static int	ixl_set_link_status_locked(struct ixl_softc *,
    811   1.1  yamaguch 		    const struct ixl_aq_desc *);
    812  1.31  yamaguch static uint64_t	ixl_search_link_speed(uint8_t);
    813  1.31  yamaguch static uint8_t	ixl_search_baudrate(uint64_t);
    814   1.1  yamaguch static void	ixl_config_rss(struct ixl_softc *);
    815   1.1  yamaguch static int	ixl_add_macvlan(struct ixl_softc *, const uint8_t *,
    816   1.1  yamaguch 		    uint16_t, uint16_t);
    817  1.12  yamaguch static int	ixl_remove_macvlan(struct ixl_softc *, const uint8_t *,
    818  1.12  yamaguch 		    uint16_t, uint16_t);
    819   1.1  yamaguch static void	ixl_arq(void *);
    820   1.1  yamaguch static void	ixl_hmc_pack(void *, const void *,
    821   1.1  yamaguch 		    const struct ixl_hmc_pack *, unsigned int);
    822   1.1  yamaguch static uint32_t	ixl_rd_rx_csr(struct ixl_softc *, uint32_t);
    823   1.1  yamaguch static void	ixl_wr_rx_csr(struct ixl_softc *, uint32_t, uint32_t);
    824  1.26  yamaguch static int	ixl_rd16_nvm(struct ixl_softc *, uint16_t, uint16_t *);
    825   1.1  yamaguch 
    826   1.1  yamaguch static int	ixl_match(device_t, cfdata_t, void *);
    827   1.1  yamaguch static void	ixl_attach(device_t, device_t, void *);
    828   1.1  yamaguch static int	ixl_detach(device_t, int);
    829   1.1  yamaguch 
    830  1.31  yamaguch static void	ixl_media_add(struct ixl_softc *);
    831   1.1  yamaguch static int	ixl_media_change(struct ifnet *);
    832   1.1  yamaguch static void	ixl_media_status(struct ifnet *, struct ifmediareq *);
    833   1.1  yamaguch static void	ixl_watchdog(struct ifnet *);
    834   1.1  yamaguch static int	ixl_ioctl(struct ifnet *, u_long, void *);
    835   1.1  yamaguch static void	ixl_start(struct ifnet *);
    836   1.1  yamaguch static int	ixl_transmit(struct ifnet *, struct mbuf *);
    837   1.1  yamaguch static void	ixl_deferred_transmit(void *);
    838   1.1  yamaguch static int	ixl_intr(void *);
    839   1.1  yamaguch static int	ixl_queue_intr(void *);
    840   1.1  yamaguch static int	ixl_other_intr(void *);
    841   1.1  yamaguch static void	ixl_handle_queue(void *);
    842  1.45  yamaguch static void	ixl_handle_queue_wk(struct work *, void *);
    843   1.1  yamaguch static void	ixl_sched_handle_queue(struct ixl_softc *,
    844   1.1  yamaguch 		    struct ixl_queue_pair *);
    845   1.1  yamaguch static int	ixl_init(struct ifnet *);
    846   1.1  yamaguch static int	ixl_init_locked(struct ixl_softc *);
    847   1.1  yamaguch static void	ixl_stop(struct ifnet *, int);
    848   1.1  yamaguch static void	ixl_stop_locked(struct ixl_softc *);
    849   1.1  yamaguch static int	ixl_iff(struct ixl_softc *);
    850   1.1  yamaguch static int	ixl_ifflags_cb(struct ethercom *);
    851   1.1  yamaguch static int	ixl_setup_interrupts(struct ixl_softc *);
    852   1.1  yamaguch static int	ixl_establish_intx(struct ixl_softc *);
    853   1.1  yamaguch static int	ixl_establish_msix(struct ixl_softc *);
    854   1.1  yamaguch static void	ixl_enable_queue_intr(struct ixl_softc *,
    855   1.1  yamaguch 		    struct ixl_queue_pair *);
    856   1.1  yamaguch static void	ixl_disable_queue_intr(struct ixl_softc *,
    857   1.1  yamaguch 		    struct ixl_queue_pair *);
    858   1.1  yamaguch static void	ixl_enable_other_intr(struct ixl_softc *);
    859   1.1  yamaguch static void	ixl_disable_other_intr(struct ixl_softc *);
    860   1.1  yamaguch static void	ixl_config_queue_intr(struct ixl_softc *);
    861   1.1  yamaguch static void	ixl_config_other_intr(struct ixl_softc *);
    862   1.1  yamaguch 
    863   1.1  yamaguch static struct ixl_tx_ring *
    864   1.1  yamaguch 		ixl_txr_alloc(struct ixl_softc *, unsigned int);
    865   1.1  yamaguch static void	ixl_txr_qdis(struct ixl_softc *, struct ixl_tx_ring *, int);
    866   1.1  yamaguch static void	ixl_txr_config(struct ixl_softc *, struct ixl_tx_ring *);
    867   1.1  yamaguch static int	ixl_txr_enabled(struct ixl_softc *, struct ixl_tx_ring *);
    868   1.1  yamaguch static int	ixl_txr_disabled(struct ixl_softc *, struct ixl_tx_ring *);
    869   1.1  yamaguch static void	ixl_txr_unconfig(struct ixl_softc *, struct ixl_tx_ring *);
    870   1.1  yamaguch static void	ixl_txr_clean(struct ixl_softc *, struct ixl_tx_ring *);
    871   1.1  yamaguch static void	ixl_txr_free(struct ixl_softc *, struct ixl_tx_ring *);
    872   1.1  yamaguch static int	ixl_txeof(struct ixl_softc *, struct ixl_tx_ring *, u_int);
    873   1.1  yamaguch 
    874   1.1  yamaguch static struct ixl_rx_ring *
    875   1.1  yamaguch 		ixl_rxr_alloc(struct ixl_softc *, unsigned int);
    876   1.1  yamaguch static void	ixl_rxr_config(struct ixl_softc *, struct ixl_rx_ring *);
    877   1.1  yamaguch static int	ixl_rxr_enabled(struct ixl_softc *, struct ixl_rx_ring *);
    878   1.1  yamaguch static int	ixl_rxr_disabled(struct ixl_softc *, struct ixl_rx_ring *);
    879   1.1  yamaguch static void	ixl_rxr_unconfig(struct ixl_softc *, struct ixl_rx_ring *);
    880   1.1  yamaguch static void	ixl_rxr_clean(struct ixl_softc *, struct ixl_rx_ring *);
    881   1.1  yamaguch static void	ixl_rxr_free(struct ixl_softc *, struct ixl_rx_ring *);
    882   1.1  yamaguch static int	ixl_rxeof(struct ixl_softc *, struct ixl_rx_ring *, u_int);
    883   1.1  yamaguch static int	ixl_rxfill(struct ixl_softc *, struct ixl_rx_ring *);
    884   1.1  yamaguch 
    885   1.1  yamaguch static struct workqueue *
    886   1.1  yamaguch     ixl_workq_create(const char *, pri_t, int, int);
    887   1.1  yamaguch static void	ixl_workq_destroy(struct workqueue *);
    888   1.1  yamaguch static int	ixl_workqs_teardown(device_t);
    889   1.1  yamaguch static void	ixl_work_set(struct ixl_work *, void (*)(void *), void *);
    890   1.1  yamaguch static void	ixl_work_add(struct workqueue *, struct ixl_work *);
    891   1.1  yamaguch static void	ixl_work_wait(struct workqueue *, struct ixl_work *);
    892   1.1  yamaguch static void	ixl_workq_work(struct work *, void *);
    893   1.1  yamaguch static const struct ixl_product *
    894   1.1  yamaguch 		ixl_lookup(const struct pci_attach_args *pa);
    895   1.4  yamaguch static void	ixl_link_state_update(struct ixl_softc *,
    896   1.4  yamaguch 		    const struct ixl_aq_desc *);
    897  1.12  yamaguch static int	ixl_vlan_cb(struct ethercom *, uint16_t, bool);
    898  1.12  yamaguch static int	ixl_setup_vlan_hwfilter(struct ixl_softc *);
    899  1.12  yamaguch static void	ixl_teardown_vlan_hwfilter(struct ixl_softc *);
    900  1.12  yamaguch static int	ixl_update_macvlan(struct ixl_softc *);
    901  1.66   msaitoh static int	ixl_setup_interrupts(struct ixl_softc *);
    902   1.1  yamaguch static void	ixl_teardown_interrupts(struct ixl_softc *);
    903   1.1  yamaguch static int	ixl_setup_stats(struct ixl_softc *);
    904   1.1  yamaguch static void	ixl_teardown_stats(struct ixl_softc *);
    905  1.17  yamaguch static void	ixl_stats_callout(void *);
    906  1.17  yamaguch static void	ixl_stats_update(void *);
    907   1.1  yamaguch static int	ixl_setup_sysctls(struct ixl_softc *);
    908   1.1  yamaguch static void	ixl_teardown_sysctls(struct ixl_softc *);
    909  1.69  yamaguch static int	ixl_sysctl_itr_handler(SYSCTLFN_PROTO);
    910  1.70  yamaguch static int	ixl_sysctl_ndescs_handler(SYSCTLFN_PROTO);
    911   1.1  yamaguch static int	ixl_queue_pairs_alloc(struct ixl_softc *);
    912   1.1  yamaguch static void	ixl_queue_pairs_free(struct ixl_softc *);
    913   1.1  yamaguch 
    914   1.1  yamaguch static const struct ixl_phy_type ixl_phy_type_map[] = {
    915   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_SGMII,		IFM_1000_SGMII },
    916   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_KX,	IFM_1000_KX },
    917   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_KX4,	IFM_10G_KX4 },
    918   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_KR,	IFM_10G_KR },
    919   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_KR4,	IFM_40G_KR4 },
    920   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_XAUI |
    921   1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_XFI,		IFM_10G_CX4 },
    922   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_SFI,		IFM_10G_SFI },
    923   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_XLAUI |
    924   1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_XLPPI,		IFM_40G_XLPPI },
    925   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_CR4_CU |
    926   1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_40GBASE_CR4,	IFM_40G_CR4 },
    927   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_CR1_CU |
    928   1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_10GBASE_CR1,	IFM_10G_CR1 },
    929   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_AOC,	IFM_10G_AOC },
    930   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_AOC,	IFM_40G_AOC },
    931   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_100BASE_TX,	IFM_100_TX },
    932   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_T_OPTICAL |
    933   1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_1000BASE_T,	IFM_1000_T },
    934   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_T,	IFM_10G_T },
    935   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_SR,	IFM_10G_SR },
    936   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_LR,	IFM_10G_LR },
    937   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_SFPP_CU,	IFM_10G_TWINAX },
    938   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_SR4,	IFM_40G_SR4 },
    939   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_LR4,	IFM_40G_LR4 },
    940  1.34  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_SX,	IFM_1000_SX },
    941  1.34  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_LX,	IFM_1000_LX },
    942   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_20GBASE_KR2,	IFM_20G_KR2 },
    943   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_KR,	IFM_25G_KR },
    944   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_CR,	IFM_25G_CR },
    945   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_SR,	IFM_25G_SR },
    946   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_LR,	IFM_25G_LR },
    947   1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_AOC,	IFM_25G_AOC },
    948  1.55  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_ACC,	IFM_25G_ACC },
    949   1.1  yamaguch };
    950   1.1  yamaguch 
    951   1.1  yamaguch static const struct ixl_speed_type ixl_speed_type_map[] = {
    952   1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_40GB,		IF_Gbps(40) },
    953   1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_25GB,		IF_Gbps(25) },
    954   1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_10GB,		IF_Gbps(10) },
    955   1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_1000MB,		IF_Mbps(1000) },
    956   1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_100MB,		IF_Mbps(100)},
    957   1.1  yamaguch };
    958   1.1  yamaguch 
    959   1.1  yamaguch static const struct ixl_aq_regs ixl_pf_aq_regs = {
    960   1.1  yamaguch 	.atq_tail	= I40E_PF_ATQT,
    961   1.1  yamaguch 	.atq_tail_mask	= I40E_PF_ATQT_ATQT_MASK,
    962   1.1  yamaguch 	.atq_head	= I40E_PF_ATQH,
    963   1.1  yamaguch 	.atq_head_mask	= I40E_PF_ATQH_ATQH_MASK,
    964   1.1  yamaguch 	.atq_len	= I40E_PF_ATQLEN,
    965   1.1  yamaguch 	.atq_bal	= I40E_PF_ATQBAL,
    966   1.1  yamaguch 	.atq_bah	= I40E_PF_ATQBAH,
    967   1.1  yamaguch 	.atq_len_enable	= I40E_PF_ATQLEN_ATQENABLE_MASK,
    968   1.1  yamaguch 
    969   1.1  yamaguch 	.arq_tail	= I40E_PF_ARQT,
    970   1.1  yamaguch 	.arq_tail_mask	= I40E_PF_ARQT_ARQT_MASK,
    971   1.1  yamaguch 	.arq_head	= I40E_PF_ARQH,
    972   1.1  yamaguch 	.arq_head_mask	= I40E_PF_ARQH_ARQH_MASK,
    973   1.1  yamaguch 	.arq_len	= I40E_PF_ARQLEN,
    974   1.1  yamaguch 	.arq_bal	= I40E_PF_ARQBAL,
    975   1.1  yamaguch 	.arq_bah	= I40E_PF_ARQBAH,
    976   1.1  yamaguch 	.arq_len_enable	= I40E_PF_ARQLEN_ARQENABLE_MASK,
    977   1.1  yamaguch };
    978   1.1  yamaguch 
    979   1.1  yamaguch #define ixl_rd(_s, _r)			\
    980   1.1  yamaguch 	bus_space_read_4((_s)->sc_memt, (_s)->sc_memh, (_r))
    981   1.1  yamaguch #define ixl_wr(_s, _r, _v)		\
    982   1.1  yamaguch 	bus_space_write_4((_s)->sc_memt, (_s)->sc_memh, (_r), (_v))
    983   1.1  yamaguch #define ixl_barrier(_s, _r, _l, _o) \
    984   1.1  yamaguch     bus_space_barrier((_s)->sc_memt, (_s)->sc_memh, (_r), (_l), (_o))
    985   1.1  yamaguch #define ixl_flush(_s)	(void)ixl_rd((_s), I40E_GLGEN_STAT)
    986   1.1  yamaguch #define ixl_nqueues(_sc)	(1 << ((_sc)->sc_nqueue_pairs - 1))
    987   1.2  yamaguch 
    988   1.2  yamaguch static inline uint32_t
    989   1.2  yamaguch ixl_dmamem_hi(struct ixl_dmamem *ixm)
    990   1.2  yamaguch {
    991   1.3  yamaguch 	uint32_t retval;
    992   1.3  yamaguch 	uint64_t val;
    993   1.2  yamaguch 
    994   1.5  yamaguch 	if (sizeof(IXL_DMA_DVA(ixm)) > 4) {
    995   1.3  yamaguch 		val = (intptr_t)IXL_DMA_DVA(ixm);
    996   1.3  yamaguch 		retval = (uint32_t)(val >> 32);
    997   1.3  yamaguch 	} else {
    998   1.3  yamaguch 		retval = 0;
    999   1.3  yamaguch 	}
   1000   1.2  yamaguch 
   1001   1.3  yamaguch 	return retval;
   1002   1.2  yamaguch }
   1003   1.2  yamaguch 
   1004   1.2  yamaguch static inline uint32_t
   1005   1.2  yamaguch ixl_dmamem_lo(struct ixl_dmamem *ixm)
   1006   1.2  yamaguch {
   1007   1.2  yamaguch 
   1008   1.2  yamaguch 	return (uint32_t)IXL_DMA_DVA(ixm);
   1009   1.2  yamaguch }
   1010   1.1  yamaguch 
   1011   1.1  yamaguch static inline void
   1012   1.1  yamaguch ixl_aq_dva(struct ixl_aq_desc *iaq, bus_addr_t addr)
   1013   1.1  yamaguch {
   1014   1.3  yamaguch 	uint64_t val;
   1015   1.2  yamaguch 
   1016   1.5  yamaguch 	if (sizeof(addr) > 4) {
   1017   1.3  yamaguch 		val = (intptr_t)addr;
   1018   1.3  yamaguch 		iaq->iaq_param[2] = htole32(val >> 32);
   1019   1.3  yamaguch 	} else {
   1020   1.2  yamaguch 		iaq->iaq_param[2] = htole32(0);
   1021   1.3  yamaguch 	}
   1022   1.2  yamaguch 
   1023   1.1  yamaguch 	iaq->iaq_param[3] = htole32(addr);
   1024   1.1  yamaguch }
   1025   1.1  yamaguch 
   1026   1.1  yamaguch static inline unsigned int
   1027   1.1  yamaguch ixl_rxr_unrefreshed(unsigned int prod, unsigned int cons, unsigned int ndescs)
   1028   1.1  yamaguch {
   1029   1.1  yamaguch 	unsigned int num;
   1030   1.1  yamaguch 
   1031   1.1  yamaguch 	if (prod  < cons)
   1032   1.1  yamaguch 		num = cons - prod;
   1033   1.1  yamaguch 	else
   1034   1.1  yamaguch 		num  = (ndescs - prod) + cons;
   1035   1.1  yamaguch 
   1036   1.1  yamaguch 	if (__predict_true(num > 0)) {
   1037   1.1  yamaguch 		/* device cannot receive packets if all descripter is filled */
   1038   1.1  yamaguch 		num -= 1;
   1039   1.1  yamaguch 	}
   1040   1.1  yamaguch 
   1041   1.1  yamaguch 	return num;
   1042   1.1  yamaguch }
   1043   1.1  yamaguch 
   1044   1.1  yamaguch CFATTACH_DECL3_NEW(ixl, sizeof(struct ixl_softc),
   1045   1.1  yamaguch     ixl_match, ixl_attach, ixl_detach, NULL, NULL, NULL,
   1046   1.1  yamaguch     DVF_DETACH_SHUTDOWN);
   1047   1.1  yamaguch 
   1048   1.1  yamaguch static const struct ixl_product ixl_products[] = {
   1049   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_SFP },
   1050   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_KX_B },
   1051   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_KX_C },
   1052   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_A },
   1053   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_B },
   1054   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_C },
   1055   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X710_10G_T },
   1056   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_20G_BP_1 },
   1057   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_20G_BP_2 },
   1058   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X710_T4_10G },
   1059   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XXV710_25G_BP },
   1060   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XXV710_25G_SFP28 },
   1061   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_KX },
   1062   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_QSFP },
   1063   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_SFP },
   1064   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_1G_BASET },
   1065   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_10G_BASET },
   1066   1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_I_SFP },
   1067   1.1  yamaguch 	/* required last entry */
   1068   1.1  yamaguch 	{0, 0}
   1069   1.1  yamaguch };
   1070   1.1  yamaguch 
   1071   1.1  yamaguch static const struct ixl_product *
   1072   1.1  yamaguch ixl_lookup(const struct pci_attach_args *pa)
   1073   1.1  yamaguch {
   1074   1.1  yamaguch 	const struct ixl_product *ixlp;
   1075   1.1  yamaguch 
   1076   1.1  yamaguch 	for (ixlp = ixl_products; ixlp->vendor_id != 0; ixlp++) {
   1077   1.1  yamaguch 		if (PCI_VENDOR(pa->pa_id) == ixlp->vendor_id &&
   1078   1.1  yamaguch 		    PCI_PRODUCT(pa->pa_id) == ixlp->product_id)
   1079   1.1  yamaguch 			return ixlp;
   1080   1.1  yamaguch 	}
   1081   1.1  yamaguch 
   1082   1.1  yamaguch 	return NULL;
   1083   1.1  yamaguch }
   1084   1.1  yamaguch 
   1085   1.1  yamaguch static int
   1086   1.1  yamaguch ixl_match(device_t parent, cfdata_t match, void *aux)
   1087   1.1  yamaguch {
   1088   1.1  yamaguch 	const struct pci_attach_args *pa = aux;
   1089   1.1  yamaguch 
   1090   1.1  yamaguch 	return (ixl_lookup(pa) != NULL) ? 1 : 0;
   1091   1.1  yamaguch }
   1092   1.1  yamaguch 
   1093   1.1  yamaguch static void
   1094   1.1  yamaguch ixl_attach(device_t parent, device_t self, void *aux)
   1095   1.1  yamaguch {
   1096   1.1  yamaguch 	struct ixl_softc *sc;
   1097   1.1  yamaguch 	struct pci_attach_args *pa = aux;
   1098   1.1  yamaguch 	struct ifnet *ifp;
   1099  1.23  yamaguch 	pcireg_t memtype;
   1100   1.1  yamaguch 	uint32_t firstq, port, ari, func;
   1101   1.1  yamaguch 	char xnamebuf[32];
   1102  1.37  yamaguch 	int tries, rv, link;
   1103   1.1  yamaguch 
   1104   1.1  yamaguch 	sc = device_private(self);
   1105   1.1  yamaguch 	sc->sc_dev = self;
   1106   1.1  yamaguch 	ifp = &sc->sc_ec.ec_if;
   1107   1.1  yamaguch 
   1108   1.1  yamaguch 	sc->sc_pa = *pa;
   1109   1.1  yamaguch 	sc->sc_dmat = (pci_dma64_available(pa)) ?
   1110   1.1  yamaguch 	    pa->pa_dmat64 : pa->pa_dmat;
   1111   1.1  yamaguch 	sc->sc_aq_regs = &ixl_pf_aq_regs;
   1112   1.1  yamaguch 
   1113  1.23  yamaguch 	sc->sc_mac_type = ixl_mactype(PCI_PRODUCT(pa->pa_id));
   1114   1.1  yamaguch 
   1115  1.68  yamaguch 	ixl_pci_csr_setup(pa->pa_pc, pa->pa_tag);
   1116  1.68  yamaguch 
   1117   1.1  yamaguch 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IXL_PCIREG);
   1118   1.1  yamaguch 	if (pci_mapreg_map(pa, IXL_PCIREG, memtype, 0,
   1119   1.1  yamaguch 	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems)) {
   1120   1.1  yamaguch 		aprint_error(": unable to map registers\n");
   1121   1.1  yamaguch 		return;
   1122   1.1  yamaguch 	}
   1123   1.1  yamaguch 
   1124   1.1  yamaguch 	mutex_init(&sc->sc_cfg_lock, MUTEX_DEFAULT, IPL_SOFTNET);
   1125   1.1  yamaguch 
   1126   1.1  yamaguch 	firstq = ixl_rd(sc, I40E_PFLAN_QALLOC);
   1127   1.1  yamaguch 	firstq &= I40E_PFLAN_QALLOC_FIRSTQ_MASK;
   1128   1.1  yamaguch 	firstq >>= I40E_PFLAN_QALLOC_FIRSTQ_SHIFT;
   1129   1.1  yamaguch 	sc->sc_base_queue = firstq;
   1130   1.1  yamaguch 
   1131   1.1  yamaguch 	ixl_clear_hw(sc);
   1132   1.1  yamaguch 	if (ixl_pf_reset(sc) == -1) {
   1133   1.1  yamaguch 		/* error printed by ixl pf_reset */
   1134   1.1  yamaguch 		goto unmap;
   1135   1.1  yamaguch 	}
   1136   1.1  yamaguch 
   1137   1.1  yamaguch 	port = ixl_rd(sc, I40E_PFGEN_PORTNUM);
   1138   1.1  yamaguch 	port &= I40E_PFGEN_PORTNUM_PORT_NUM_MASK;
   1139   1.1  yamaguch 	port >>= I40E_PFGEN_PORTNUM_PORT_NUM_SHIFT;
   1140  1.17  yamaguch 	sc->sc_port = port;
   1141  1.17  yamaguch 	aprint_normal(": port %u", sc->sc_port);
   1142   1.1  yamaguch 
   1143   1.1  yamaguch 	ari = ixl_rd(sc, I40E_GLPCI_CAPSUP);
   1144   1.1  yamaguch 	ari &= I40E_GLPCI_CAPSUP_ARI_EN_MASK;
   1145   1.1  yamaguch 	ari >>= I40E_GLPCI_CAPSUP_ARI_EN_SHIFT;
   1146   1.1  yamaguch 
   1147   1.1  yamaguch 	func = ixl_rd(sc, I40E_PF_FUNC_RID);
   1148   1.1  yamaguch 	sc->sc_pf_id = func & (ari ? 0xff : 0x7);
   1149   1.1  yamaguch 
   1150   1.1  yamaguch 	/* initialise the adminq */
   1151   1.1  yamaguch 
   1152   1.1  yamaguch 	mutex_init(&sc->sc_atq_lock, MUTEX_DEFAULT, IPL_NET);
   1153   1.1  yamaguch 
   1154   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_atq,
   1155   1.1  yamaguch 	    sizeof(struct ixl_aq_desc) * IXL_AQ_NUM, IXL_AQ_ALIGN) != 0) {
   1156   1.1  yamaguch 		aprint_error("\n" "%s: unable to allocate atq\n",
   1157   1.1  yamaguch 		    device_xname(self));
   1158   1.1  yamaguch 		goto unmap;
   1159   1.1  yamaguch 	}
   1160   1.1  yamaguch 
   1161   1.1  yamaguch 	SIMPLEQ_INIT(&sc->sc_arq_idle);
   1162   1.1  yamaguch 	ixl_work_set(&sc->sc_arq_task, ixl_arq, sc);
   1163   1.1  yamaguch 	sc->sc_arq_cons = 0;
   1164   1.1  yamaguch 	sc->sc_arq_prod = 0;
   1165   1.1  yamaguch 
   1166   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_arq,
   1167   1.1  yamaguch 	    sizeof(struct ixl_aq_desc) * IXL_AQ_NUM, IXL_AQ_ALIGN) != 0) {
   1168   1.1  yamaguch 		aprint_error("\n" "%s: unable to allocate arq\n",
   1169   1.1  yamaguch 		    device_xname(self));
   1170   1.1  yamaguch 		goto free_atq;
   1171   1.1  yamaguch 	}
   1172   1.1  yamaguch 
   1173   1.1  yamaguch 	if (!ixl_arq_fill(sc)) {
   1174   1.1  yamaguch 		aprint_error("\n" "%s: unable to fill arq descriptors\n",
   1175   1.1  yamaguch 		    device_xname(self));
   1176   1.1  yamaguch 		goto free_arq;
   1177   1.1  yamaguch 	}
   1178   1.1  yamaguch 
   1179   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   1180   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   1181   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1182   1.1  yamaguch 
   1183   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   1184   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   1185   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1186   1.1  yamaguch 
   1187   1.1  yamaguch 	for (tries = 0; tries < 10; tries++) {
   1188   1.1  yamaguch 		sc->sc_atq_cons = 0;
   1189   1.1  yamaguch 		sc->sc_atq_prod = 0;
   1190   1.1  yamaguch 
   1191   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
   1192   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
   1193   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
   1194   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
   1195   1.1  yamaguch 
   1196   1.1  yamaguch 		ixl_barrier(sc, 0, sc->sc_mems, BUS_SPACE_BARRIER_WRITE);
   1197   1.1  yamaguch 
   1198   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_bal,
   1199   1.1  yamaguch 		    ixl_dmamem_lo(&sc->sc_atq));
   1200   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_bah,
   1201   1.1  yamaguch 		    ixl_dmamem_hi(&sc->sc_atq));
   1202   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_len,
   1203   1.1  yamaguch 		    sc->sc_aq_regs->atq_len_enable | IXL_AQ_NUM);
   1204   1.1  yamaguch 
   1205   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_bal,
   1206   1.1  yamaguch 		    ixl_dmamem_lo(&sc->sc_arq));
   1207   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_bah,
   1208   1.1  yamaguch 		    ixl_dmamem_hi(&sc->sc_arq));
   1209   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_len,
   1210   1.1  yamaguch 		    sc->sc_aq_regs->arq_len_enable | IXL_AQ_NUM);
   1211   1.1  yamaguch 
   1212   1.1  yamaguch 		rv = ixl_get_version(sc);
   1213   1.1  yamaguch 		if (rv == 0)
   1214   1.1  yamaguch 			break;
   1215   1.1  yamaguch 		if (rv != ETIMEDOUT) {
   1216   1.1  yamaguch 			aprint_error(", unable to get firmware version\n");
   1217   1.1  yamaguch 			goto shutdown;
   1218   1.1  yamaguch 		}
   1219   1.1  yamaguch 
   1220   1.1  yamaguch 		delaymsec(100);
   1221   1.1  yamaguch 	}
   1222   1.1  yamaguch 
   1223   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
   1224   1.1  yamaguch 
   1225  1.26  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_aqbuf, IXL_AQ_BUFLEN, 0) != 0) {
   1226  1.26  yamaguch 		aprint_error_dev(self, ", unable to allocate nvm buffer\n");
   1227  1.26  yamaguch 		goto shutdown;
   1228  1.26  yamaguch 	}
   1229  1.26  yamaguch 
   1230  1.26  yamaguch 	ixl_get_nvm_version(sc);
   1231  1.26  yamaguch 
   1232   1.1  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722)
   1233  1.47  yamaguch 		sc->sc_nqueue_pairs_device = IXL_QUEUE_MAX_X722;
   1234   1.1  yamaguch 	else
   1235  1.47  yamaguch 		sc->sc_nqueue_pairs_device = IXL_QUEUE_MAX_XL710;
   1236   1.1  yamaguch 
   1237   1.1  yamaguch 	rv = ixl_get_hw_capabilities(sc);
   1238   1.1  yamaguch 	if (rv != 0) {
   1239   1.1  yamaguch 		aprint_error(", GET HW CAPABILITIES %s\n",
   1240   1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1241  1.26  yamaguch 		goto free_aqbuf;
   1242   1.1  yamaguch 	}
   1243   1.1  yamaguch 
   1244  1.19  yamaguch 	sc->sc_nqueue_pairs_max = MIN((int)sc->sc_nqueue_pairs_device, ncpu);
   1245  1.19  yamaguch 	if (ixl_param_nqps_limit > 0) {
   1246  1.19  yamaguch 		sc->sc_nqueue_pairs_max = MIN((int)sc->sc_nqueue_pairs_max,
   1247  1.19  yamaguch 		    ixl_param_nqps_limit);
   1248  1.19  yamaguch 	}
   1249  1.19  yamaguch 
   1250  1.19  yamaguch 	sc->sc_nqueue_pairs = sc->sc_nqueue_pairs_max;
   1251  1.19  yamaguch 	sc->sc_tx_ring_ndescs = ixl_param_tx_ndescs;
   1252  1.19  yamaguch 	sc->sc_rx_ring_ndescs = ixl_param_rx_ndescs;
   1253   1.1  yamaguch 
   1254   1.1  yamaguch 	KASSERT(IXL_TXRX_PROCESS_UNLIMIT > sc->sc_rx_ring_ndescs);
   1255   1.1  yamaguch 	KASSERT(IXL_TXRX_PROCESS_UNLIMIT > sc->sc_tx_ring_ndescs);
   1256  1.70  yamaguch 	KASSERT(sc->sc_rx_ring_ndescs ==
   1257  1.70  yamaguch 	    (1U << (fls32(sc->sc_rx_ring_ndescs) - 1)));
   1258  1.70  yamaguch 	KASSERT(sc->sc_tx_ring_ndescs ==
   1259  1.70  yamaguch 	    (1U << (fls32(sc->sc_tx_ring_ndescs) - 1)));
   1260   1.1  yamaguch 
   1261   1.1  yamaguch 	if (ixl_get_mac(sc) != 0) {
   1262   1.1  yamaguch 		/* error printed by ixl_get_mac */
   1263  1.26  yamaguch 		goto free_aqbuf;
   1264   1.1  yamaguch 	}
   1265   1.1  yamaguch 
   1266   1.1  yamaguch 	aprint_normal("\n");
   1267   1.1  yamaguch 	aprint_naive("\n");
   1268   1.1  yamaguch 
   1269   1.1  yamaguch 	aprint_normal_dev(self, "Ethernet address %s\n",
   1270   1.1  yamaguch 	    ether_sprintf(sc->sc_enaddr));
   1271   1.1  yamaguch 
   1272   1.1  yamaguch 	rv = ixl_pxe_clear(sc);
   1273   1.1  yamaguch 	if (rv != 0) {
   1274   1.1  yamaguch 		aprint_debug_dev(self, "CLEAR PXE MODE %s\n",
   1275   1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1276   1.1  yamaguch 	}
   1277   1.1  yamaguch 
   1278   1.1  yamaguch 	ixl_set_filter_control(sc);
   1279   1.1  yamaguch 
   1280   1.1  yamaguch 	if (ixl_hmc(sc) != 0) {
   1281   1.1  yamaguch 		/* error printed by ixl_hmc */
   1282  1.26  yamaguch 		goto free_aqbuf;
   1283   1.1  yamaguch 	}
   1284   1.1  yamaguch 
   1285   1.1  yamaguch 	if (ixl_lldp_shut(sc) != 0) {
   1286   1.1  yamaguch 		/* error printed by ixl_lldp_shut */
   1287   1.1  yamaguch 		goto free_hmc;
   1288   1.1  yamaguch 	}
   1289   1.1  yamaguch 
   1290   1.1  yamaguch 	if (ixl_phy_mask_ints(sc) != 0) {
   1291   1.1  yamaguch 		/* error printed by ixl_phy_mask_ints */
   1292   1.1  yamaguch 		goto free_hmc;
   1293   1.1  yamaguch 	}
   1294   1.1  yamaguch 
   1295   1.1  yamaguch 	if (ixl_restart_an(sc) != 0) {
   1296   1.1  yamaguch 		/* error printed by ixl_restart_an */
   1297   1.1  yamaguch 		goto free_hmc;
   1298   1.1  yamaguch 	}
   1299   1.1  yamaguch 
   1300   1.1  yamaguch 	if (ixl_get_switch_config(sc) != 0) {
   1301   1.1  yamaguch 		/* error printed by ixl_get_switch_config */
   1302   1.1  yamaguch 		goto free_hmc;
   1303   1.1  yamaguch 	}
   1304   1.1  yamaguch 
   1305  1.37  yamaguch 	rv = ixl_get_link_status_poll(sc, NULL);
   1306   1.1  yamaguch 	if (rv != 0) {
   1307   1.1  yamaguch 		aprint_error_dev(self, "GET LINK STATUS %s\n",
   1308   1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1309   1.1  yamaguch 		goto free_hmc;
   1310   1.1  yamaguch 	}
   1311   1.1  yamaguch 
   1312  1.38  yamaguch 	/*
   1313  1.38  yamaguch 	 * The FW often returns EIO in "Get PHY Abilities" command
   1314  1.38  yamaguch 	 * if there is no delay
   1315  1.38  yamaguch 	 */
   1316  1.38  yamaguch 	DELAY(500);
   1317  1.38  yamaguch 	if (ixl_get_phy_info(sc) != 0) {
   1318  1.38  yamaguch 		/* error printed by ixl_get_phy_info */
   1319  1.38  yamaguch 		goto free_hmc;
   1320  1.38  yamaguch 	}
   1321  1.38  yamaguch 
   1322   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_scratch,
   1323   1.1  yamaguch 	    sizeof(struct ixl_aq_vsi_data), 8) != 0) {
   1324   1.1  yamaguch 		aprint_error_dev(self, "unable to allocate scratch buffer\n");
   1325   1.1  yamaguch 		goto free_hmc;
   1326   1.1  yamaguch 	}
   1327   1.1  yamaguch 
   1328  1.11  yamaguch 	rv = ixl_get_vsi(sc);
   1329  1.11  yamaguch 	if (rv != 0) {
   1330  1.11  yamaguch 		aprint_error_dev(self, "GET VSI %s %d\n",
   1331  1.11  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error", rv);
   1332   1.1  yamaguch 		goto free_scratch;
   1333   1.1  yamaguch 	}
   1334   1.1  yamaguch 
   1335  1.11  yamaguch 	rv = ixl_set_vsi(sc);
   1336  1.11  yamaguch 	if (rv != 0) {
   1337  1.11  yamaguch 		aprint_error_dev(self, "UPDATE VSI error %s %d\n",
   1338  1.11  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error", rv);
   1339   1.1  yamaguch 		goto free_scratch;
   1340   1.1  yamaguch 	}
   1341   1.1  yamaguch 
   1342   1.1  yamaguch 	if (ixl_queue_pairs_alloc(sc) != 0) {
   1343   1.1  yamaguch 		/* error printed by ixl_queue_pairs_alloc */
   1344   1.1  yamaguch 		goto free_scratch;
   1345   1.1  yamaguch 	}
   1346   1.1  yamaguch 
   1347   1.1  yamaguch 	if (ixl_setup_interrupts(sc) != 0) {
   1348   1.1  yamaguch 		/* error printed by ixl_setup_interrupts */
   1349   1.1  yamaguch 		goto free_queue_pairs;
   1350   1.1  yamaguch 	}
   1351   1.1  yamaguch 
   1352   1.1  yamaguch 	if (ixl_setup_stats(sc) != 0) {
   1353   1.1  yamaguch 		aprint_error_dev(self, "failed to setup event counters\n");
   1354   1.1  yamaguch 		goto teardown_intrs;
   1355   1.1  yamaguch 	}
   1356   1.1  yamaguch 
   1357   1.1  yamaguch 	if (ixl_setup_sysctls(sc) != 0) {
   1358   1.1  yamaguch 		/* error printed by ixl_setup_sysctls */
   1359   1.1  yamaguch 		goto teardown_stats;
   1360   1.1  yamaguch 	}
   1361   1.1  yamaguch 
   1362   1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_cfg", device_xname(self));
   1363   1.1  yamaguch 	sc->sc_workq = ixl_workq_create(xnamebuf, IXL_WORKQUEUE_PRI,
   1364  1.44  yamaguch 	    IPL_NET, WQ_MPSAFE);
   1365   1.1  yamaguch 	if (sc->sc_workq == NULL)
   1366   1.1  yamaguch 		goto teardown_sysctls;
   1367   1.1  yamaguch 
   1368   1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_txrx", device_xname(self));
   1369  1.45  yamaguch 	rv = workqueue_create(&sc->sc_workq_txrx, xnamebuf, ixl_handle_queue_wk,
   1370  1.45  yamaguch 	    sc, IXL_WORKQUEUE_PRI, IPL_NET, WQ_PERCPU | WQ_MPSAFE);
   1371  1.45  yamaguch 	if (rv != 0) {
   1372  1.45  yamaguch 		sc->sc_workq_txrx = NULL;
   1373   1.1  yamaguch 		goto teardown_wqs;
   1374  1.45  yamaguch 	}
   1375   1.1  yamaguch 
   1376   1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_atq_cv", device_xname(self));
   1377   1.1  yamaguch 	cv_init(&sc->sc_atq_cv, xnamebuf);
   1378   1.1  yamaguch 
   1379   1.1  yamaguch 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
   1380   1.1  yamaguch 
   1381   1.1  yamaguch 	ifp->if_softc = sc;
   1382   1.1  yamaguch 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1383   1.1  yamaguch 	ifp->if_extflags = IFEF_MPSAFE;
   1384   1.1  yamaguch 	ifp->if_ioctl = ixl_ioctl;
   1385   1.1  yamaguch 	ifp->if_start = ixl_start;
   1386   1.1  yamaguch 	ifp->if_transmit = ixl_transmit;
   1387   1.1  yamaguch 	ifp->if_watchdog = ixl_watchdog;
   1388   1.1  yamaguch 	ifp->if_init = ixl_init;
   1389   1.1  yamaguch 	ifp->if_stop = ixl_stop;
   1390   1.1  yamaguch 	IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_tx_ring_ndescs);
   1391   1.1  yamaguch 	IFQ_SET_READY(&ifp->if_snd);
   1392  1.14  yamaguch 	ifp->if_capabilities |= IXL_IFCAP_RXCSUM;
   1393  1.28  yamaguch 	ifp->if_capabilities |= IXL_IFCAP_TXCSUM;
   1394   1.1  yamaguch #if 0
   1395  1.28  yamaguch 	ifp->if_capabilities |= IFCAP_TSOv4 | IFCAP_TSOv6;
   1396   1.1  yamaguch #endif
   1397  1.12  yamaguch 	ether_set_vlan_cb(&sc->sc_ec, ixl_vlan_cb);
   1398  1.29  yamaguch 	sc->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1399   1.1  yamaguch 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
   1400  1.12  yamaguch 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   1401  1.11  yamaguch 
   1402  1.11  yamaguch 	sc->sc_ec.ec_capenable = sc->sc_ec.ec_capabilities;
   1403  1.12  yamaguch 	/* Disable VLAN_HWFILTER by default */
   1404  1.12  yamaguch 	CLR(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWFILTER);
   1405  1.12  yamaguch 
   1406  1.11  yamaguch 	sc->sc_cur_ec_capenable = sc->sc_ec.ec_capenable;
   1407   1.1  yamaguch 
   1408   1.1  yamaguch 	sc->sc_ec.ec_ifmedia = &sc->sc_media;
   1409  1.65  yamaguch 	ifmedia_init_with_lock(&sc->sc_media, IFM_IMASK, ixl_media_change,
   1410  1.65  yamaguch 	    ixl_media_status, &sc->sc_cfg_lock);
   1411   1.1  yamaguch 
   1412  1.31  yamaguch 	ixl_media_add(sc);
   1413   1.1  yamaguch 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
   1414  1.31  yamaguch 	if (ISSET(sc->sc_phy_abilities,
   1415  1.31  yamaguch 	    (IXL_PHY_ABILITY_PAUSE_TX | IXL_PHY_ABILITY_PAUSE_RX))) {
   1416  1.31  yamaguch 		ifmedia_add(&sc->sc_media,
   1417  1.31  yamaguch 		    IFM_ETHER | IFM_AUTO | IFM_FLOW, 0, NULL);
   1418  1.31  yamaguch 	}
   1419  1.31  yamaguch 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_NONE, 0, NULL);
   1420   1.1  yamaguch 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
   1421   1.1  yamaguch 
   1422  1.71  yamaguch 	rv = if_initialize(ifp);
   1423  1.71  yamaguch 	if (rv != 0) {
   1424  1.71  yamaguch 		aprint_error_dev(self, "if_initialize failed=%d\n", rv);
   1425  1.71  yamaguch 		goto teardown_wqs;
   1426  1.71  yamaguch 	}
   1427  1.71  yamaguch 
   1428  1.71  yamaguch 	sc->sc_ipq = if_percpuq_create(ifp);
   1429   1.1  yamaguch 	if_deferred_start_init(ifp, NULL);
   1430   1.1  yamaguch 	ether_ifattach(ifp, sc->sc_enaddr);
   1431   1.1  yamaguch 	ether_set_ifflags_cb(&sc->sc_ec, ixl_ifflags_cb);
   1432   1.1  yamaguch 
   1433  1.37  yamaguch 	rv = ixl_get_link_status_poll(sc, &link);
   1434  1.37  yamaguch 	if (rv != 0)
   1435  1.37  yamaguch 		link = LINK_STATE_UNKNOWN;
   1436  1.37  yamaguch 	if_link_state_change(ifp, link);
   1437  1.37  yamaguch 
   1438  1.57  yamaguch 	ixl_atq_set(&sc->sc_link_state_atq, ixl_get_link_status_done);
   1439   1.4  yamaguch 	ixl_work_set(&sc->sc_link_state_task, ixl_get_link_status, sc);
   1440   1.1  yamaguch 
   1441   1.1  yamaguch 	ixl_config_other_intr(sc);
   1442  1.10  yamaguch 	ixl_enable_other_intr(sc);
   1443   1.1  yamaguch 
   1444  1.31  yamaguch 	ixl_set_phy_autoselect(sc);
   1445  1.31  yamaguch 
   1446  1.12  yamaguch 	/* remove default mac filter and replace it so we can see vlans */
   1447  1.12  yamaguch 	rv = ixl_remove_macvlan(sc, sc->sc_enaddr, 0, 0);
   1448  1.12  yamaguch 	if (rv != ENOENT) {
   1449  1.12  yamaguch 		aprint_debug_dev(self,
   1450  1.12  yamaguch 		    "unable to remove macvlan %u\n", rv);
   1451  1.12  yamaguch 	}
   1452  1.12  yamaguch 	rv = ixl_remove_macvlan(sc, sc->sc_enaddr, 0,
   1453  1.12  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1454  1.12  yamaguch 	if (rv != ENOENT) {
   1455  1.12  yamaguch 		aprint_debug_dev(self,
   1456  1.12  yamaguch 		    "unable to remove macvlan, ignore vlan %u\n", rv);
   1457  1.12  yamaguch 	}
   1458  1.12  yamaguch 
   1459  1.12  yamaguch 	if (ixl_update_macvlan(sc) != 0) {
   1460  1.12  yamaguch 		aprint_debug_dev(self,
   1461  1.12  yamaguch 		    "couldn't enable vlan hardware filter\n");
   1462  1.12  yamaguch 		CLR(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWFILTER);
   1463  1.12  yamaguch 		CLR(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER);
   1464  1.12  yamaguch 	}
   1465   1.1  yamaguch 
   1466   1.1  yamaguch 	sc->sc_txrx_workqueue = true;
   1467   1.1  yamaguch 	sc->sc_tx_process_limit = IXL_TX_PROCESS_LIMIT;
   1468   1.1  yamaguch 	sc->sc_rx_process_limit = IXL_RX_PROCESS_LIMIT;
   1469   1.1  yamaguch 	sc->sc_tx_intr_process_limit = IXL_TX_INTR_PROCESS_LIMIT;
   1470   1.1  yamaguch 	sc->sc_rx_intr_process_limit = IXL_RX_INTR_PROCESS_LIMIT;
   1471   1.1  yamaguch 
   1472  1.17  yamaguch 	ixl_stats_update(sc);
   1473  1.17  yamaguch 	sc->sc_stats_counters.isc_has_offset = true;
   1474  1.17  yamaguch 
   1475   1.1  yamaguch 	if (pmf_device_register(self, NULL, NULL) != true)
   1476   1.1  yamaguch 		aprint_debug_dev(self, "couldn't establish power handler\n");
   1477  1.64  yamaguch 	sc->sc_itr_rx = IXL_ITR_RX;
   1478  1.64  yamaguch 	sc->sc_itr_tx = IXL_ITR_TX;
   1479   1.1  yamaguch 	sc->sc_attached = true;
   1480  1.71  yamaguch 	if_register(ifp);
   1481  1.71  yamaguch 
   1482   1.1  yamaguch 	return;
   1483   1.1  yamaguch 
   1484   1.1  yamaguch teardown_wqs:
   1485   1.1  yamaguch 	config_finalize_register(self, ixl_workqs_teardown);
   1486   1.1  yamaguch teardown_sysctls:
   1487   1.1  yamaguch 	ixl_teardown_sysctls(sc);
   1488   1.1  yamaguch teardown_stats:
   1489   1.1  yamaguch 	ixl_teardown_stats(sc);
   1490   1.1  yamaguch teardown_intrs:
   1491   1.1  yamaguch 	ixl_teardown_interrupts(sc);
   1492   1.1  yamaguch free_queue_pairs:
   1493   1.1  yamaguch 	ixl_queue_pairs_free(sc);
   1494   1.1  yamaguch free_scratch:
   1495   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_scratch);
   1496   1.1  yamaguch free_hmc:
   1497   1.1  yamaguch 	ixl_hmc_free(sc);
   1498  1.26  yamaguch free_aqbuf:
   1499  1.26  yamaguch 	ixl_dmamem_free(sc, &sc->sc_aqbuf);
   1500   1.1  yamaguch shutdown:
   1501   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
   1502   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
   1503   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
   1504   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
   1505   1.1  yamaguch 
   1506   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bal, 0);
   1507   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bah, 0);
   1508   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_len, 0);
   1509   1.1  yamaguch 
   1510   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bal, 0);
   1511   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bah, 0);
   1512   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_len, 0);
   1513   1.1  yamaguch 
   1514   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   1515   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   1516   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   1517   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   1518   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   1519   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1520   1.1  yamaguch 
   1521   1.1  yamaguch 	ixl_arq_unfill(sc);
   1522   1.1  yamaguch free_arq:
   1523   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_arq);
   1524   1.1  yamaguch free_atq:
   1525   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_atq);
   1526   1.1  yamaguch unmap:
   1527   1.1  yamaguch 	mutex_destroy(&sc->sc_atq_lock);
   1528   1.1  yamaguch 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
   1529   1.1  yamaguch 	mutex_destroy(&sc->sc_cfg_lock);
   1530   1.1  yamaguch 	sc->sc_mems = 0;
   1531   1.1  yamaguch 
   1532   1.1  yamaguch 	sc->sc_attached = false;
   1533   1.1  yamaguch }
   1534   1.1  yamaguch 
   1535   1.1  yamaguch static int
   1536   1.1  yamaguch ixl_detach(device_t self, int flags)
   1537   1.1  yamaguch {
   1538   1.1  yamaguch 	struct ixl_softc *sc = device_private(self);
   1539   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1540   1.1  yamaguch 
   1541   1.1  yamaguch 	if (!sc->sc_attached)
   1542   1.1  yamaguch 		return 0;
   1543   1.1  yamaguch 
   1544   1.1  yamaguch 	ixl_stop(ifp, 1);
   1545   1.1  yamaguch 
   1546  1.10  yamaguch 	ixl_disable_other_intr(sc);
   1547  1.10  yamaguch 
   1548  1.51  yamaguch 	callout_halt(&sc->sc_stats_callout, NULL);
   1549  1.17  yamaguch 	ixl_work_wait(sc->sc_workq, &sc->sc_stats_task);
   1550  1.17  yamaguch 
   1551  1.10  yamaguch 	/* wait for ATQ handler */
   1552  1.10  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   1553  1.10  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   1554  1.10  yamaguch 
   1555  1.10  yamaguch 	ixl_work_wait(sc->sc_workq, &sc->sc_arq_task);
   1556  1.10  yamaguch 	ixl_work_wait(sc->sc_workq, &sc->sc_link_state_task);
   1557  1.10  yamaguch 
   1558   1.1  yamaguch 	if (sc->sc_workq != NULL) {
   1559   1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq);
   1560   1.1  yamaguch 		sc->sc_workq = NULL;
   1561   1.1  yamaguch 	}
   1562   1.1  yamaguch 
   1563   1.1  yamaguch 	if (sc->sc_workq_txrx != NULL) {
   1564  1.45  yamaguch 		workqueue_destroy(sc->sc_workq_txrx);
   1565   1.1  yamaguch 		sc->sc_workq_txrx = NULL;
   1566   1.1  yamaguch 	}
   1567   1.1  yamaguch 
   1568  1.71  yamaguch 	if_percpuq_destroy(sc->sc_ipq);
   1569   1.1  yamaguch 	ether_ifdetach(ifp);
   1570   1.1  yamaguch 	if_detach(ifp);
   1571  1.36   thorpej 	ifmedia_fini(&sc->sc_media);
   1572   1.1  yamaguch 
   1573   1.1  yamaguch 	ixl_teardown_interrupts(sc);
   1574   1.1  yamaguch 	ixl_teardown_stats(sc);
   1575  1.15  yamaguch 	ixl_teardown_sysctls(sc);
   1576   1.1  yamaguch 
   1577   1.1  yamaguch 	ixl_queue_pairs_free(sc);
   1578   1.1  yamaguch 
   1579   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_scratch);
   1580   1.1  yamaguch 	ixl_hmc_free(sc);
   1581   1.1  yamaguch 
   1582   1.1  yamaguch 	/* shutdown */
   1583   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
   1584   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
   1585   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
   1586   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
   1587   1.1  yamaguch 
   1588   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bal, 0);
   1589   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bah, 0);
   1590   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_len, 0);
   1591   1.1  yamaguch 
   1592   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bal, 0);
   1593   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bah, 0);
   1594   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_len, 0);
   1595   1.1  yamaguch 
   1596   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   1597   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   1598   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   1599   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   1600   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   1601   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1602   1.1  yamaguch 
   1603   1.1  yamaguch 	ixl_arq_unfill(sc);
   1604   1.1  yamaguch 
   1605   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_arq);
   1606   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_atq);
   1607  1.26  yamaguch 	ixl_dmamem_free(sc, &sc->sc_aqbuf);
   1608   1.1  yamaguch 
   1609   1.1  yamaguch 	cv_destroy(&sc->sc_atq_cv);
   1610   1.1  yamaguch 	mutex_destroy(&sc->sc_atq_lock);
   1611   1.1  yamaguch 
   1612   1.1  yamaguch 	if (sc->sc_mems != 0) {
   1613   1.1  yamaguch 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
   1614   1.1  yamaguch 		sc->sc_mems = 0;
   1615   1.1  yamaguch 	}
   1616   1.1  yamaguch 
   1617   1.1  yamaguch 	mutex_destroy(&sc->sc_cfg_lock);
   1618   1.1  yamaguch 
   1619   1.1  yamaguch 	return 0;
   1620   1.1  yamaguch }
   1621   1.1  yamaguch 
   1622   1.1  yamaguch static int
   1623   1.1  yamaguch ixl_workqs_teardown(device_t self)
   1624   1.1  yamaguch {
   1625   1.1  yamaguch 	struct ixl_softc *sc = device_private(self);
   1626   1.1  yamaguch 
   1627   1.1  yamaguch 	if (sc->sc_workq != NULL) {
   1628   1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq);
   1629   1.1  yamaguch 		sc->sc_workq = NULL;
   1630   1.1  yamaguch 	}
   1631   1.1  yamaguch 
   1632   1.1  yamaguch 	if (sc->sc_workq_txrx != NULL) {
   1633  1.45  yamaguch 		workqueue_destroy(sc->sc_workq_txrx);
   1634   1.1  yamaguch 		sc->sc_workq_txrx = NULL;
   1635   1.1  yamaguch 	}
   1636   1.1  yamaguch 
   1637   1.1  yamaguch 	return 0;
   1638   1.1  yamaguch }
   1639   1.1  yamaguch 
   1640  1.12  yamaguch static int
   1641  1.12  yamaguch ixl_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
   1642  1.12  yamaguch {
   1643  1.12  yamaguch 	struct ifnet *ifp = &ec->ec_if;
   1644  1.12  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1645  1.12  yamaguch 	int rv;
   1646  1.12  yamaguch 
   1647  1.12  yamaguch 	if (!ISSET(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER)) {
   1648  1.12  yamaguch 		return 0;
   1649  1.12  yamaguch 	}
   1650  1.12  yamaguch 
   1651  1.12  yamaguch 	if (set) {
   1652  1.12  yamaguch 		rv = ixl_add_macvlan(sc, sc->sc_enaddr, vid,
   1653  1.12  yamaguch 		    IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   1654  1.12  yamaguch 		if (rv == 0) {
   1655  1.12  yamaguch 			rv = ixl_add_macvlan(sc, etherbroadcastaddr,
   1656  1.12  yamaguch 			    vid, IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   1657  1.12  yamaguch 		}
   1658  1.12  yamaguch 	} else {
   1659  1.12  yamaguch 		rv = ixl_remove_macvlan(sc, sc->sc_enaddr, vid,
   1660  1.12  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   1661  1.12  yamaguch 		(void)ixl_remove_macvlan(sc, etherbroadcastaddr, vid,
   1662  1.12  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   1663  1.12  yamaguch 	}
   1664  1.12  yamaguch 
   1665  1.12  yamaguch 	return rv;
   1666  1.12  yamaguch }
   1667  1.12  yamaguch 
   1668   1.1  yamaguch static void
   1669  1.31  yamaguch ixl_media_add(struct ixl_softc *sc)
   1670   1.1  yamaguch {
   1671   1.1  yamaguch 	struct ifmedia *ifm = &sc->sc_media;
   1672   1.1  yamaguch 	const struct ixl_phy_type *itype;
   1673   1.1  yamaguch 	unsigned int i;
   1674  1.31  yamaguch 	bool flow;
   1675  1.31  yamaguch 
   1676  1.31  yamaguch 	if (ISSET(sc->sc_phy_abilities,
   1677  1.31  yamaguch 	    (IXL_PHY_ABILITY_PAUSE_TX | IXL_PHY_ABILITY_PAUSE_RX))) {
   1678  1.31  yamaguch 		flow = true;
   1679  1.31  yamaguch 	} else {
   1680  1.31  yamaguch 		flow = false;
   1681  1.31  yamaguch 	}
   1682   1.1  yamaguch 
   1683   1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_phy_type_map); i++) {
   1684   1.1  yamaguch 		itype = &ixl_phy_type_map[i];
   1685   1.1  yamaguch 
   1686  1.31  yamaguch 		if (ISSET(sc->sc_phy_types, itype->phy_type)) {
   1687   1.1  yamaguch 			ifmedia_add(ifm,
   1688   1.1  yamaguch 			    IFM_ETHER | IFM_FDX | itype->ifm_type, 0, NULL);
   1689   1.1  yamaguch 
   1690  1.31  yamaguch 			if (flow) {
   1691  1.31  yamaguch 				ifmedia_add(ifm,
   1692  1.31  yamaguch 				    IFM_ETHER | IFM_FDX | IFM_FLOW |
   1693  1.31  yamaguch 				    itype->ifm_type, 0, NULL);
   1694  1.31  yamaguch 			}
   1695  1.31  yamaguch 
   1696  1.31  yamaguch 			if (itype->ifm_type != IFM_100_TX)
   1697  1.31  yamaguch 				continue;
   1698  1.31  yamaguch 
   1699  1.31  yamaguch 			ifmedia_add(ifm, IFM_ETHER | itype->ifm_type,
   1700  1.31  yamaguch 			    0, NULL);
   1701  1.31  yamaguch 			if (flow) {
   1702  1.31  yamaguch 				ifmedia_add(ifm,
   1703  1.31  yamaguch 				    IFM_ETHER | IFM_FLOW | itype->ifm_type,
   1704   1.1  yamaguch 				    0, NULL);
   1705   1.1  yamaguch 			}
   1706   1.1  yamaguch 		}
   1707   1.1  yamaguch 	}
   1708   1.1  yamaguch }
   1709   1.1  yamaguch 
   1710   1.1  yamaguch static void
   1711   1.1  yamaguch ixl_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1712   1.1  yamaguch {
   1713   1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1714   1.1  yamaguch 
   1715  1.65  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   1716  1.65  yamaguch 
   1717   1.1  yamaguch 	ifmr->ifm_status = sc->sc_media_status;
   1718   1.1  yamaguch 	ifmr->ifm_active = sc->sc_media_active;
   1719   1.1  yamaguch }
   1720   1.1  yamaguch 
   1721   1.1  yamaguch static int
   1722   1.1  yamaguch ixl_media_change(struct ifnet *ifp)
   1723   1.1  yamaguch {
   1724  1.31  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1725  1.31  yamaguch 	struct ifmedia *ifm = &sc->sc_media;
   1726  1.31  yamaguch 	uint64_t ifm_active = sc->sc_media_active;
   1727  1.31  yamaguch 	uint8_t link_speed, abilities;
   1728  1.31  yamaguch 
   1729  1.31  yamaguch 	switch (IFM_SUBTYPE(ifm_active)) {
   1730  1.31  yamaguch 	case IFM_1000_SGMII:
   1731  1.31  yamaguch 	case IFM_1000_KX:
   1732  1.31  yamaguch 	case IFM_10G_KX4:
   1733  1.31  yamaguch 	case IFM_10G_KR:
   1734  1.31  yamaguch 	case IFM_40G_KR4:
   1735  1.31  yamaguch 	case IFM_20G_KR2:
   1736  1.31  yamaguch 	case IFM_25G_KR:
   1737  1.31  yamaguch 		/* backplanes */
   1738  1.31  yamaguch 		return EINVAL;
   1739  1.31  yamaguch 	}
   1740  1.31  yamaguch 
   1741  1.31  yamaguch 	abilities = IXL_PHY_ABILITY_AUTONEGO | IXL_PHY_ABILITY_LINKUP;
   1742   1.1  yamaguch 
   1743  1.31  yamaguch 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   1744  1.31  yamaguch 	case IFM_AUTO:
   1745  1.31  yamaguch 		link_speed = sc->sc_phy_linkspeed;
   1746  1.31  yamaguch 		break;
   1747  1.31  yamaguch 	case IFM_NONE:
   1748  1.31  yamaguch 		link_speed = 0;
   1749  1.31  yamaguch 		CLR(abilities, IXL_PHY_ABILITY_LINKUP);
   1750  1.31  yamaguch 		break;
   1751  1.31  yamaguch 	default:
   1752  1.31  yamaguch 		link_speed = ixl_search_baudrate(
   1753  1.31  yamaguch 		    ifmedia_baudrate(ifm->ifm_media));
   1754  1.31  yamaguch 	}
   1755  1.31  yamaguch 
   1756  1.31  yamaguch 	if (ISSET(abilities, IXL_PHY_ABILITY_LINKUP)) {
   1757  1.31  yamaguch 		if (ISSET(link_speed, sc->sc_phy_linkspeed) == 0)
   1758  1.31  yamaguch 			return EINVAL;
   1759  1.31  yamaguch 	}
   1760  1.31  yamaguch 
   1761  1.31  yamaguch 	if (ifm->ifm_media & IFM_FLOW) {
   1762  1.31  yamaguch 		abilities |= sc->sc_phy_abilities &
   1763  1.31  yamaguch 		    (IXL_PHY_ABILITY_PAUSE_TX | IXL_PHY_ABILITY_PAUSE_RX);
   1764  1.31  yamaguch 	}
   1765  1.31  yamaguch 
   1766  1.31  yamaguch 	return ixl_set_phy_config(sc, link_speed, abilities, false);
   1767   1.1  yamaguch }
   1768   1.1  yamaguch 
   1769   1.1  yamaguch static void
   1770   1.1  yamaguch ixl_watchdog(struct ifnet *ifp)
   1771   1.1  yamaguch {
   1772   1.1  yamaguch 
   1773   1.1  yamaguch }
   1774   1.1  yamaguch 
   1775   1.1  yamaguch static void
   1776   1.1  yamaguch ixl_del_all_multiaddr(struct ixl_softc *sc)
   1777   1.1  yamaguch {
   1778   1.1  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   1779   1.1  yamaguch 	struct ether_multi *enm;
   1780   1.1  yamaguch 	struct ether_multistep step;
   1781   1.1  yamaguch 
   1782   1.1  yamaguch 	ETHER_LOCK(ec);
   1783   1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1784   1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1785   1.1  yamaguch 		ixl_remove_macvlan(sc, enm->enm_addrlo, 0,
   1786   1.1  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1787   1.1  yamaguch 	}
   1788   1.1  yamaguch 	ETHER_UNLOCK(ec);
   1789   1.1  yamaguch }
   1790   1.1  yamaguch 
   1791   1.1  yamaguch static int
   1792   1.1  yamaguch ixl_add_multi(struct ixl_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
   1793   1.1  yamaguch {
   1794   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1795   1.1  yamaguch 	int rv;
   1796   1.1  yamaguch 
   1797   1.1  yamaguch 	if (ISSET(ifp->if_flags, IFF_ALLMULTI))
   1798   1.1  yamaguch 		return 0;
   1799   1.1  yamaguch 
   1800   1.1  yamaguch 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) {
   1801   1.1  yamaguch 		ixl_del_all_multiaddr(sc);
   1802   1.1  yamaguch 		SET(ifp->if_flags, IFF_ALLMULTI);
   1803   1.8  yamaguch 		return ENETRESET;
   1804   1.1  yamaguch 	}
   1805   1.1  yamaguch 
   1806  1.12  yamaguch 	/* multicast address can not use VLAN HWFILTER */
   1807   1.1  yamaguch 	rv = ixl_add_macvlan(sc, addrlo, 0,
   1808   1.1  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   1809   1.1  yamaguch 
   1810   1.7  yamaguch 	if (rv == ENOSPC) {
   1811   1.1  yamaguch 		ixl_del_all_multiaddr(sc);
   1812   1.1  yamaguch 		SET(ifp->if_flags, IFF_ALLMULTI);
   1813   1.8  yamaguch 		return ENETRESET;
   1814   1.1  yamaguch 	}
   1815   1.1  yamaguch 
   1816   1.7  yamaguch 	return rv;
   1817   1.1  yamaguch }
   1818   1.1  yamaguch 
   1819   1.8  yamaguch static int
   1820   1.1  yamaguch ixl_del_multi(struct ixl_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
   1821   1.1  yamaguch {
   1822   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1823   1.1  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   1824   1.1  yamaguch 	struct ether_multi *enm, *enm_last;
   1825   1.1  yamaguch 	struct ether_multistep step;
   1826   1.8  yamaguch 	int error, rv = 0;
   1827   1.1  yamaguch 
   1828   1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_ALLMULTI)) {
   1829   1.1  yamaguch 		ixl_remove_macvlan(sc, addrlo, 0,
   1830   1.1  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1831   1.8  yamaguch 		return 0;
   1832   1.1  yamaguch 	}
   1833   1.1  yamaguch 
   1834   1.1  yamaguch 	ETHER_LOCK(ec);
   1835   1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1836   1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1837   1.1  yamaguch 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1838   1.1  yamaguch 		    ETHER_ADDR_LEN) != 0) {
   1839   1.8  yamaguch 			goto out;
   1840   1.1  yamaguch 		}
   1841   1.1  yamaguch 	}
   1842   1.1  yamaguch 
   1843   1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1844   1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1845   1.8  yamaguch 		error = ixl_add_macvlan(sc, enm->enm_addrlo, 0,
   1846   1.1  yamaguch 		    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   1847   1.8  yamaguch 		if (error != 0)
   1848   1.1  yamaguch 			break;
   1849   1.1  yamaguch 	}
   1850   1.1  yamaguch 
   1851   1.1  yamaguch 	if (enm != NULL) {
   1852   1.1  yamaguch 		enm_last = enm;
   1853   1.1  yamaguch 		for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1854   1.1  yamaguch 		    ETHER_NEXT_MULTI(step, enm)) {
   1855   1.1  yamaguch 			if (enm == enm_last)
   1856   1.1  yamaguch 				break;
   1857   1.1  yamaguch 
   1858   1.1  yamaguch 			ixl_remove_macvlan(sc, enm->enm_addrlo, 0,
   1859   1.1  yamaguch 			    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1860   1.1  yamaguch 		}
   1861   1.1  yamaguch 	} else {
   1862   1.1  yamaguch 		CLR(ifp->if_flags, IFF_ALLMULTI);
   1863   1.8  yamaguch 		rv = ENETRESET;
   1864   1.1  yamaguch 	}
   1865   1.1  yamaguch 
   1866   1.8  yamaguch out:
   1867   1.1  yamaguch 	ETHER_UNLOCK(ec);
   1868   1.8  yamaguch 	return rv;
   1869   1.1  yamaguch }
   1870   1.1  yamaguch 
   1871   1.1  yamaguch static int
   1872   1.1  yamaguch ixl_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1873   1.1  yamaguch {
   1874   1.1  yamaguch 	struct ifreq *ifr = (struct ifreq *)data;
   1875   1.1  yamaguch 	struct ixl_softc *sc = (struct ixl_softc *)ifp->if_softc;
   1876   1.1  yamaguch 	const struct sockaddr *sa;
   1877   1.1  yamaguch 	uint8_t addrhi[ETHER_ADDR_LEN], addrlo[ETHER_ADDR_LEN];
   1878   1.1  yamaguch 	int s, error = 0;
   1879  1.35   thorpej 	unsigned int nmtu;
   1880   1.1  yamaguch 
   1881   1.1  yamaguch 	switch (cmd) {
   1882  1.29  yamaguch 	case SIOCSIFMTU:
   1883  1.29  yamaguch 		nmtu = ifr->ifr_mtu;
   1884  1.29  yamaguch 
   1885  1.29  yamaguch 		if (nmtu < IXL_MIN_MTU || nmtu > IXL_MAX_MTU) {
   1886  1.29  yamaguch 			error = EINVAL;
   1887  1.29  yamaguch 			break;
   1888  1.29  yamaguch 		}
   1889  1.29  yamaguch 		if (ifp->if_mtu != nmtu) {
   1890  1.29  yamaguch 			s = splnet();
   1891  1.29  yamaguch 			error = ether_ioctl(ifp, cmd, data);
   1892  1.29  yamaguch 			splx(s);
   1893  1.29  yamaguch 			if (error == ENETRESET)
   1894  1.29  yamaguch 				error = ixl_init(ifp);
   1895  1.29  yamaguch 		}
   1896  1.29  yamaguch 		break;
   1897   1.1  yamaguch 	case SIOCADDMULTI:
   1898   1.1  yamaguch 		sa = ifreq_getaddr(SIOCADDMULTI, ifr);
   1899   1.1  yamaguch 		if (ether_addmulti(sa, &sc->sc_ec) == ENETRESET) {
   1900   1.1  yamaguch 			error = ether_multiaddr(sa, addrlo, addrhi);
   1901   1.1  yamaguch 			if (error != 0)
   1902   1.1  yamaguch 				return error;
   1903   1.1  yamaguch 
   1904   1.8  yamaguch 			error = ixl_add_multi(sc, addrlo, addrhi);
   1905   1.8  yamaguch 			if (error != 0 && error != ENETRESET) {
   1906   1.1  yamaguch 				ether_delmulti(sa, &sc->sc_ec);
   1907   1.1  yamaguch 				error = EIO;
   1908   1.1  yamaguch 			}
   1909   1.1  yamaguch 		}
   1910   1.1  yamaguch 		break;
   1911   1.1  yamaguch 
   1912   1.1  yamaguch 	case SIOCDELMULTI:
   1913   1.1  yamaguch 		sa = ifreq_getaddr(SIOCDELMULTI, ifr);
   1914   1.1  yamaguch 		if (ether_delmulti(sa, &sc->sc_ec) == ENETRESET) {
   1915   1.1  yamaguch 			error = ether_multiaddr(sa, addrlo, addrhi);
   1916   1.1  yamaguch 			if (error != 0)
   1917   1.1  yamaguch 				return error;
   1918   1.1  yamaguch 
   1919   1.8  yamaguch 			error = ixl_del_multi(sc, addrlo, addrhi);
   1920   1.1  yamaguch 		}
   1921   1.1  yamaguch 		break;
   1922   1.1  yamaguch 
   1923   1.1  yamaguch 	default:
   1924   1.1  yamaguch 		s = splnet();
   1925   1.1  yamaguch 		error = ether_ioctl(ifp, cmd, data);
   1926   1.1  yamaguch 		splx(s);
   1927   1.1  yamaguch 	}
   1928   1.1  yamaguch 
   1929   1.1  yamaguch 	if (error == ENETRESET)
   1930   1.1  yamaguch 		error = ixl_iff(sc);
   1931   1.1  yamaguch 
   1932   1.1  yamaguch 	return error;
   1933   1.1  yamaguch }
   1934   1.1  yamaguch 
   1935   1.1  yamaguch static enum i40e_mac_type
   1936   1.1  yamaguch ixl_mactype(pci_product_id_t id)
   1937   1.1  yamaguch {
   1938   1.1  yamaguch 
   1939   1.1  yamaguch 	switch (id) {
   1940   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_SFP:
   1941   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_KX_B:
   1942   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_KX_C:
   1943   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_A:
   1944   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_B:
   1945   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_C:
   1946   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X710_10G_T:
   1947   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_20G_BP_1:
   1948   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_20G_BP_2:
   1949   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X710_T4_10G:
   1950   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XXV710_25G_BP:
   1951   1.1  yamaguch 	case PCI_PRODUCT_INTEL_XXV710_25G_SFP28:
   1952   1.1  yamaguch 		return I40E_MAC_XL710;
   1953   1.1  yamaguch 
   1954   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_KX:
   1955   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_QSFP:
   1956   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_SFP:
   1957   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_1G_BASET:
   1958   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_10G_BASET:
   1959   1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_I_SFP:
   1960   1.1  yamaguch 		return I40E_MAC_X722;
   1961   1.1  yamaguch 	}
   1962   1.1  yamaguch 
   1963   1.1  yamaguch 	return I40E_MAC_GENERIC;
   1964   1.1  yamaguch }
   1965   1.1  yamaguch 
   1966  1.68  yamaguch static void
   1967  1.68  yamaguch ixl_pci_csr_setup(pci_chipset_tag_t pc, pcitag_t tag)
   1968  1.68  yamaguch {
   1969  1.68  yamaguch 	pcireg_t csr;
   1970  1.68  yamaguch 
   1971  1.68  yamaguch 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
   1972  1.68  yamaguch 	csr |= (PCI_COMMAND_MASTER_ENABLE |
   1973  1.68  yamaguch 	    PCI_COMMAND_MEM_ENABLE);
   1974  1.68  yamaguch 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
   1975  1.68  yamaguch }
   1976  1.68  yamaguch 
   1977   1.1  yamaguch static inline void *
   1978   1.1  yamaguch ixl_hmc_kva(struct ixl_softc *sc, enum ixl_hmc_types type, unsigned int i)
   1979   1.1  yamaguch {
   1980   1.1  yamaguch 	uint8_t *kva = IXL_DMA_KVA(&sc->sc_hmc_pd);
   1981   1.1  yamaguch 	struct ixl_hmc_entry *e = &sc->sc_hmc_entries[type];
   1982   1.1  yamaguch 
   1983   1.1  yamaguch 	if (i >= e->hmc_count)
   1984   1.1  yamaguch 		return NULL;
   1985   1.1  yamaguch 
   1986   1.1  yamaguch 	kva += e->hmc_base;
   1987   1.1  yamaguch 	kva += i * e->hmc_size;
   1988   1.1  yamaguch 
   1989   1.1  yamaguch 	return kva;
   1990   1.1  yamaguch }
   1991   1.1  yamaguch 
   1992   1.1  yamaguch static inline size_t
   1993   1.1  yamaguch ixl_hmc_len(struct ixl_softc *sc, enum ixl_hmc_types type)
   1994   1.1  yamaguch {
   1995   1.1  yamaguch 	struct ixl_hmc_entry *e = &sc->sc_hmc_entries[type];
   1996   1.1  yamaguch 
   1997   1.1  yamaguch 	return e->hmc_size;
   1998   1.1  yamaguch }
   1999   1.1  yamaguch 
   2000   1.1  yamaguch static void
   2001   1.1  yamaguch ixl_enable_queue_intr(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   2002   1.1  yamaguch {
   2003   1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   2004   1.1  yamaguch 
   2005   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTLN(rxr->rxr_qid),
   2006   1.1  yamaguch 	    I40E_PFINT_DYN_CTLN_INTENA_MASK |
   2007   1.1  yamaguch 	    I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
   2008   1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT));
   2009   1.1  yamaguch 	ixl_flush(sc);
   2010   1.1  yamaguch }
   2011   1.1  yamaguch 
   2012   1.1  yamaguch static void
   2013   1.1  yamaguch ixl_disable_queue_intr(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   2014   1.1  yamaguch {
   2015   1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   2016   1.1  yamaguch 
   2017   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTLN(rxr->rxr_qid),
   2018   1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT));
   2019   1.1  yamaguch 	ixl_flush(sc);
   2020   1.1  yamaguch }
   2021   1.1  yamaguch 
   2022   1.1  yamaguch static void
   2023   1.1  yamaguch ixl_enable_other_intr(struct ixl_softc *sc)
   2024   1.1  yamaguch {
   2025   1.1  yamaguch 
   2026   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTL0,
   2027   1.1  yamaguch 	    I40E_PFINT_DYN_CTL0_INTENA_MASK |
   2028   1.1  yamaguch 	    I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
   2029   1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT));
   2030   1.1  yamaguch 	ixl_flush(sc);
   2031   1.1  yamaguch }
   2032   1.1  yamaguch 
   2033   1.1  yamaguch static void
   2034   1.1  yamaguch ixl_disable_other_intr(struct ixl_softc *sc)
   2035   1.1  yamaguch {
   2036   1.1  yamaguch 
   2037   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTL0,
   2038   1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT));
   2039   1.1  yamaguch 	ixl_flush(sc);
   2040   1.1  yamaguch }
   2041   1.1  yamaguch 
   2042   1.1  yamaguch static int
   2043   1.1  yamaguch ixl_reinit(struct ixl_softc *sc)
   2044   1.1  yamaguch {
   2045   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   2046   1.1  yamaguch 	struct ixl_tx_ring *txr;
   2047   1.1  yamaguch 	unsigned int i;
   2048   1.1  yamaguch 	uint32_t reg;
   2049   1.1  yamaguch 
   2050   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   2051   1.1  yamaguch 
   2052  1.11  yamaguch 	if (ixl_get_vsi(sc) != 0)
   2053  1.11  yamaguch 		return EIO;
   2054  1.11  yamaguch 
   2055  1.11  yamaguch 	if (ixl_set_vsi(sc) != 0)
   2056  1.11  yamaguch 		return EIO;
   2057  1.11  yamaguch 
   2058   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2059   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2060   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2061   1.1  yamaguch 
   2062   1.1  yamaguch 		ixl_txr_config(sc, txr);
   2063   1.1  yamaguch 		ixl_rxr_config(sc, rxr);
   2064   1.1  yamaguch 	}
   2065   1.1  yamaguch 
   2066   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   2067   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_PREWRITE);
   2068   1.1  yamaguch 
   2069   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2070   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2071   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2072   1.1  yamaguch 
   2073   1.1  yamaguch 		ixl_wr(sc, I40E_QTX_CTL(i), I40E_QTX_CTL_PF_QUEUE |
   2074   1.1  yamaguch 		    (sc->sc_pf_id << I40E_QTX_CTL_PF_INDX_SHIFT));
   2075   1.1  yamaguch 		ixl_flush(sc);
   2076   1.1  yamaguch 
   2077   1.1  yamaguch 		ixl_wr(sc, txr->txr_tail, txr->txr_prod);
   2078   1.1  yamaguch 		ixl_wr(sc, rxr->rxr_tail, rxr->rxr_prod);
   2079   1.1  yamaguch 
   2080   1.1  yamaguch 		/* ixl_rxfill() needs lock held */
   2081   1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   2082   1.1  yamaguch 		ixl_rxfill(sc, rxr);
   2083   1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   2084   1.1  yamaguch 
   2085   1.1  yamaguch 		reg = ixl_rd(sc, I40E_QRX_ENA(i));
   2086   1.1  yamaguch 		SET(reg, I40E_QRX_ENA_QENA_REQ_MASK);
   2087   1.1  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), reg);
   2088   1.1  yamaguch 		if (ixl_rxr_enabled(sc, rxr) != 0)
   2089   1.1  yamaguch 			goto stop;
   2090   1.1  yamaguch 
   2091   1.1  yamaguch 		ixl_txr_qdis(sc, txr, 1);
   2092   1.1  yamaguch 
   2093   1.1  yamaguch 		reg = ixl_rd(sc, I40E_QTX_ENA(i));
   2094   1.1  yamaguch 		SET(reg, I40E_QTX_ENA_QENA_REQ_MASK);
   2095   1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), reg);
   2096   1.1  yamaguch 
   2097   1.1  yamaguch 		if (ixl_txr_enabled(sc, txr) != 0)
   2098   1.1  yamaguch 			goto stop;
   2099   1.1  yamaguch 	}
   2100   1.1  yamaguch 
   2101   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   2102   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_POSTWRITE);
   2103   1.1  yamaguch 
   2104   1.1  yamaguch 	return 0;
   2105   1.1  yamaguch 
   2106   1.1  yamaguch stop:
   2107   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   2108   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_POSTWRITE);
   2109   1.1  yamaguch 
   2110   1.1  yamaguch 	return ETIMEDOUT;
   2111   1.1  yamaguch }
   2112   1.1  yamaguch 
   2113   1.1  yamaguch static int
   2114   1.1  yamaguch ixl_init_locked(struct ixl_softc *sc)
   2115   1.1  yamaguch {
   2116   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2117   1.1  yamaguch 	unsigned int i;
   2118  1.12  yamaguch 	int error, eccap_change;
   2119   1.1  yamaguch 
   2120   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   2121   1.1  yamaguch 
   2122  1.11  yamaguch 	if (ISSET(ifp->if_flags, IFF_RUNNING))
   2123  1.11  yamaguch 		ixl_stop_locked(sc);
   2124  1.11  yamaguch 
   2125   1.1  yamaguch 	if (sc->sc_dead) {
   2126   1.1  yamaguch 		return ENXIO;
   2127   1.1  yamaguch 	}
   2128   1.1  yamaguch 
   2129  1.12  yamaguch 	eccap_change = sc->sc_ec.ec_capenable ^ sc->sc_cur_ec_capenable;
   2130  1.12  yamaguch 	if (ISSET(eccap_change, ETHERCAP_VLAN_HWTAGGING))
   2131  1.12  yamaguch 		sc->sc_cur_ec_capenable ^= ETHERCAP_VLAN_HWTAGGING;
   2132  1.12  yamaguch 
   2133  1.12  yamaguch 	if (ISSET(eccap_change, ETHERCAP_VLAN_HWFILTER)) {
   2134  1.12  yamaguch 		if (ixl_update_macvlan(sc) == 0) {
   2135  1.12  yamaguch 			sc->sc_cur_ec_capenable ^= ETHERCAP_VLAN_HWFILTER;
   2136  1.12  yamaguch 		} else {
   2137  1.12  yamaguch 			CLR(sc->sc_ec.ec_capenable, ETHERCAP_VLAN_HWFILTER);
   2138  1.12  yamaguch 			CLR(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER);
   2139  1.12  yamaguch 		}
   2140  1.12  yamaguch 	}
   2141  1.11  yamaguch 
   2142   1.1  yamaguch 	if (sc->sc_intrtype != PCI_INTR_TYPE_MSIX)
   2143   1.1  yamaguch 		sc->sc_nqueue_pairs = 1;
   2144   1.1  yamaguch 	else
   2145   1.1  yamaguch 		sc->sc_nqueue_pairs = sc->sc_nqueue_pairs_max;
   2146   1.1  yamaguch 
   2147   1.1  yamaguch 	error = ixl_reinit(sc);
   2148   1.1  yamaguch 	if (error) {
   2149   1.1  yamaguch 		ixl_stop_locked(sc);
   2150   1.1  yamaguch 		return error;
   2151   1.1  yamaguch 	}
   2152   1.1  yamaguch 
   2153   1.1  yamaguch 	SET(ifp->if_flags, IFF_RUNNING);
   2154   1.1  yamaguch 	CLR(ifp->if_flags, IFF_OACTIVE);
   2155   1.4  yamaguch 
   2156   1.1  yamaguch 	ixl_config_rss(sc);
   2157   1.1  yamaguch 	ixl_config_queue_intr(sc);
   2158   1.1  yamaguch 
   2159   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2160   1.1  yamaguch 		ixl_enable_queue_intr(sc, &sc->sc_qps[i]);
   2161   1.1  yamaguch 	}
   2162   1.1  yamaguch 
   2163   1.1  yamaguch 	error = ixl_iff(sc);
   2164   1.1  yamaguch 	if (error) {
   2165   1.1  yamaguch 		ixl_stop_locked(sc);
   2166   1.1  yamaguch 		return error;
   2167   1.1  yamaguch 	}
   2168   1.1  yamaguch 
   2169  1.50  yamaguch 	callout_schedule(&sc->sc_stats_callout, mstohz(sc->sc_stats_intval));
   2170  1.50  yamaguch 
   2171   1.1  yamaguch 	return 0;
   2172   1.1  yamaguch }
   2173   1.1  yamaguch 
   2174   1.1  yamaguch static int
   2175   1.1  yamaguch ixl_init(struct ifnet *ifp)
   2176   1.1  yamaguch {
   2177   1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   2178   1.1  yamaguch 	int error;
   2179   1.1  yamaguch 
   2180   1.1  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   2181   1.1  yamaguch 	error = ixl_init_locked(sc);
   2182   1.1  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   2183   1.1  yamaguch 
   2184  1.63  yamaguch 	if (error == 0)
   2185  1.63  yamaguch 		(void)ixl_get_link_status(sc);
   2186  1.63  yamaguch 
   2187   1.1  yamaguch 	return error;
   2188   1.1  yamaguch }
   2189   1.1  yamaguch 
   2190   1.1  yamaguch static int
   2191   1.1  yamaguch ixl_iff(struct ixl_softc *sc)
   2192   1.1  yamaguch {
   2193   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2194   1.1  yamaguch 	struct ixl_atq iatq;
   2195   1.1  yamaguch 	struct ixl_aq_desc *iaq;
   2196   1.1  yamaguch 	struct ixl_aq_vsi_promisc_param *param;
   2197  1.12  yamaguch 	uint16_t flag_add, flag_del;
   2198   1.1  yamaguch 	int error;
   2199   1.1  yamaguch 
   2200   1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   2201   1.1  yamaguch 		return 0;
   2202   1.1  yamaguch 
   2203   1.1  yamaguch 	memset(&iatq, 0, sizeof(iatq));
   2204   1.1  yamaguch 
   2205   1.1  yamaguch 	iaq = &iatq.iatq_desc;
   2206   1.1  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_SET_VSI_PROMISC);
   2207   1.1  yamaguch 
   2208   1.1  yamaguch 	param = (struct ixl_aq_vsi_promisc_param *)&iaq->iaq_param;
   2209  1.12  yamaguch 	param->flags = htole16(0);
   2210  1.12  yamaguch 
   2211  1.12  yamaguch 	if (!ISSET(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER)
   2212  1.12  yamaguch 	    || ISSET(ifp->if_flags, IFF_PROMISC)) {
   2213  1.12  yamaguch 		param->flags |= htole16(IXL_AQ_VSI_PROMISC_FLAG_BCAST |
   2214  1.12  yamaguch 		    IXL_AQ_VSI_PROMISC_FLAG_VLAN);
   2215  1.12  yamaguch 	}
   2216  1.12  yamaguch 
   2217   1.1  yamaguch 	if (ISSET(ifp->if_flags, IFF_PROMISC)) {
   2218   1.1  yamaguch 		param->flags |= htole16(IXL_AQ_VSI_PROMISC_FLAG_UCAST |
   2219   1.1  yamaguch 		    IXL_AQ_VSI_PROMISC_FLAG_MCAST);
   2220   1.1  yamaguch 	} else if (ISSET(ifp->if_flags, IFF_ALLMULTI)) {
   2221   1.1  yamaguch 		param->flags |= htole16(IXL_AQ_VSI_PROMISC_FLAG_MCAST);
   2222   1.1  yamaguch 	}
   2223   1.1  yamaguch 	param->valid_flags = htole16(IXL_AQ_VSI_PROMISC_FLAG_UCAST |
   2224   1.1  yamaguch 	    IXL_AQ_VSI_PROMISC_FLAG_MCAST | IXL_AQ_VSI_PROMISC_FLAG_BCAST |
   2225   1.1  yamaguch 	    IXL_AQ_VSI_PROMISC_FLAG_VLAN);
   2226   1.1  yamaguch 	param->seid = sc->sc_seid;
   2227   1.1  yamaguch 
   2228   1.1  yamaguch 	error = ixl_atq_exec(sc, &iatq);
   2229   1.1  yamaguch 	if (error)
   2230   1.1  yamaguch 		return error;
   2231   1.1  yamaguch 
   2232   1.1  yamaguch 	if (iaq->iaq_retval != htole16(IXL_AQ_RC_OK))
   2233   1.1  yamaguch 		return EIO;
   2234   1.1  yamaguch 
   2235   1.1  yamaguch 	if (memcmp(sc->sc_enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN) != 0) {
   2236  1.12  yamaguch 		if (ISSET(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER)) {
   2237  1.12  yamaguch 			flag_add = IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH;
   2238  1.12  yamaguch 			flag_del = IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH;
   2239  1.12  yamaguch 		} else {
   2240  1.12  yamaguch 			flag_add = IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN;
   2241  1.12  yamaguch 			flag_del = IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN;
   2242  1.12  yamaguch 		}
   2243  1.12  yamaguch 
   2244  1.12  yamaguch 		ixl_remove_macvlan(sc, sc->sc_enaddr, 0, flag_del);
   2245   1.1  yamaguch 
   2246   1.1  yamaguch 		memcpy(sc->sc_enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2247  1.12  yamaguch 		ixl_add_macvlan(sc, sc->sc_enaddr, 0, flag_add);
   2248   1.1  yamaguch 	}
   2249   1.1  yamaguch 	return 0;
   2250   1.1  yamaguch }
   2251   1.1  yamaguch 
   2252   1.1  yamaguch static void
   2253   1.1  yamaguch ixl_stop_rendezvous(struct ixl_softc *sc)
   2254   1.1  yamaguch {
   2255   1.1  yamaguch 	struct ixl_tx_ring *txr;
   2256   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   2257   1.1  yamaguch 	unsigned int i;
   2258   1.1  yamaguch 
   2259   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2260   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2261   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2262   1.1  yamaguch 
   2263   1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   2264   1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   2265   1.1  yamaguch 
   2266   1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   2267   1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   2268   1.1  yamaguch 
   2269  1.45  yamaguch 		sc->sc_qps[i].qp_workqueue = false;
   2270  1.45  yamaguch 		workqueue_wait(sc->sc_workq_txrx,
   2271  1.45  yamaguch 		    &sc->sc_qps[i].qp_work);
   2272   1.1  yamaguch 	}
   2273   1.1  yamaguch }
   2274   1.1  yamaguch 
   2275   1.1  yamaguch static void
   2276   1.1  yamaguch ixl_stop_locked(struct ixl_softc *sc)
   2277   1.1  yamaguch {
   2278   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2279   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   2280   1.1  yamaguch 	struct ixl_tx_ring *txr;
   2281   1.1  yamaguch 	unsigned int i;
   2282   1.1  yamaguch 	uint32_t reg;
   2283   1.1  yamaguch 
   2284   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   2285   1.1  yamaguch 
   2286   1.1  yamaguch 	CLR(ifp->if_flags, IFF_RUNNING | IFF_OACTIVE);
   2287  1.50  yamaguch 	callout_stop(&sc->sc_stats_callout);
   2288   1.1  yamaguch 
   2289   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2290   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2291   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2292   1.1  yamaguch 
   2293   1.1  yamaguch 		ixl_disable_queue_intr(sc, &sc->sc_qps[i]);
   2294   1.1  yamaguch 
   2295   1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   2296   1.1  yamaguch 		ixl_txr_qdis(sc, txr, 0);
   2297  1.52  yamaguch 		mutex_exit(&txr->txr_lock);
   2298  1.52  yamaguch 	}
   2299  1.52  yamaguch 
   2300  1.52  yamaguch 	/* XXX wait at least 400 usec for all tx queues in one go */
   2301  1.52  yamaguch 	ixl_flush(sc);
   2302  1.52  yamaguch 	DELAY(500);
   2303  1.52  yamaguch 
   2304  1.52  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2305  1.52  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2306  1.52  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2307   1.1  yamaguch 
   2308  1.52  yamaguch 		mutex_enter(&txr->txr_lock);
   2309   1.1  yamaguch 		reg = ixl_rd(sc, I40E_QTX_ENA(i));
   2310   1.1  yamaguch 		CLR(reg, I40E_QTX_ENA_QENA_REQ_MASK);
   2311   1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), reg);
   2312  1.52  yamaguch 		mutex_exit(&txr->txr_lock);
   2313  1.52  yamaguch 
   2314  1.52  yamaguch 		mutex_enter(&rxr->rxr_lock);
   2315  1.52  yamaguch 		reg = ixl_rd(sc, I40E_QRX_ENA(i));
   2316  1.52  yamaguch 		CLR(reg, I40E_QRX_ENA_QENA_REQ_MASK);
   2317  1.52  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), reg);
   2318  1.52  yamaguch 		mutex_exit(&rxr->rxr_lock);
   2319  1.52  yamaguch 	}
   2320  1.52  yamaguch 
   2321  1.52  yamaguch 	/* XXX short wait for all queue disables to settle */
   2322  1.52  yamaguch 	ixl_flush(sc);
   2323  1.52  yamaguch 	DELAY(50);
   2324  1.52  yamaguch 
   2325  1.52  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2326  1.52  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2327  1.52  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2328   1.1  yamaguch 
   2329  1.52  yamaguch 		mutex_enter(&txr->txr_lock);
   2330   1.1  yamaguch 		if (ixl_txr_disabled(sc, txr) != 0) {
   2331   1.1  yamaguch 			mutex_exit(&txr->txr_lock);
   2332   1.1  yamaguch 			goto die;
   2333   1.1  yamaguch 		}
   2334   1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   2335   1.1  yamaguch 
   2336   1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   2337   1.1  yamaguch 		if (ixl_rxr_disabled(sc, rxr) != 0) {
   2338   1.1  yamaguch 			mutex_exit(&rxr->rxr_lock);
   2339   1.1  yamaguch 			goto die;
   2340   1.1  yamaguch 		}
   2341   1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   2342   1.1  yamaguch 	}
   2343   1.1  yamaguch 
   2344   1.1  yamaguch 	ixl_stop_rendezvous(sc);
   2345   1.1  yamaguch 
   2346   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2347   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   2348   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   2349   1.1  yamaguch 
   2350  1.48  yamaguch 		mutex_enter(&txr->txr_lock);
   2351   1.1  yamaguch 		ixl_txr_unconfig(sc, txr);
   2352  1.48  yamaguch 		mutex_exit(&txr->txr_lock);
   2353  1.48  yamaguch 
   2354  1.48  yamaguch 		mutex_enter(&rxr->rxr_lock);
   2355   1.1  yamaguch 		ixl_rxr_unconfig(sc, rxr);
   2356  1.48  yamaguch 		mutex_exit(&rxr->rxr_lock);
   2357   1.1  yamaguch 
   2358   1.1  yamaguch 		ixl_txr_clean(sc, txr);
   2359   1.1  yamaguch 		ixl_rxr_clean(sc, rxr);
   2360   1.1  yamaguch 	}
   2361   1.1  yamaguch 
   2362   1.1  yamaguch 	return;
   2363   1.1  yamaguch die:
   2364   1.1  yamaguch 	sc->sc_dead = true;
   2365   1.1  yamaguch 	log(LOG_CRIT, "%s: failed to shut down rings",
   2366   1.1  yamaguch 	    device_xname(sc->sc_dev));
   2367   1.1  yamaguch 	return;
   2368   1.1  yamaguch }
   2369   1.1  yamaguch 
   2370   1.1  yamaguch static void
   2371   1.1  yamaguch ixl_stop(struct ifnet *ifp, int disable)
   2372   1.1  yamaguch {
   2373   1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   2374   1.1  yamaguch 
   2375   1.1  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   2376   1.1  yamaguch 	ixl_stop_locked(sc);
   2377   1.1  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   2378   1.1  yamaguch }
   2379   1.1  yamaguch 
   2380   1.1  yamaguch static int
   2381   1.1  yamaguch ixl_queue_pairs_alloc(struct ixl_softc *sc)
   2382   1.1  yamaguch {
   2383   1.1  yamaguch 	struct ixl_queue_pair *qp;
   2384   1.1  yamaguch 	unsigned int i;
   2385   1.1  yamaguch 	size_t sz;
   2386   1.1  yamaguch 
   2387   1.1  yamaguch 	sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   2388   1.1  yamaguch 	sc->sc_qps = kmem_zalloc(sz, KM_SLEEP);
   2389   1.1  yamaguch 
   2390   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   2391   1.1  yamaguch 		qp = &sc->sc_qps[i];
   2392   1.1  yamaguch 
   2393   1.1  yamaguch 		qp->qp_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   2394   1.1  yamaguch 		    ixl_handle_queue, qp);
   2395   1.6  yamaguch 		if (qp->qp_si == NULL)
   2396   1.6  yamaguch 			goto free;
   2397   1.1  yamaguch 
   2398   1.1  yamaguch 		qp->qp_txr = ixl_txr_alloc(sc, i);
   2399   1.1  yamaguch 		if (qp->qp_txr == NULL)
   2400   1.1  yamaguch 			goto free;
   2401   1.1  yamaguch 
   2402   1.1  yamaguch 		qp->qp_rxr = ixl_rxr_alloc(sc, i);
   2403   1.1  yamaguch 		if (qp->qp_rxr == NULL)
   2404   1.1  yamaguch 			goto free;
   2405   1.1  yamaguch 
   2406   1.1  yamaguch 		qp->qp_sc = sc;
   2407   1.1  yamaguch 		snprintf(qp->qp_name, sizeof(qp->qp_name),
   2408   1.1  yamaguch 		    "%s-TXRX%d", device_xname(sc->sc_dev), i);
   2409   1.1  yamaguch 	}
   2410   1.1  yamaguch 
   2411   1.1  yamaguch 	return 0;
   2412   1.1  yamaguch free:
   2413   1.1  yamaguch 	if (sc->sc_qps != NULL) {
   2414   1.1  yamaguch 		for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   2415   1.1  yamaguch 			qp = &sc->sc_qps[i];
   2416   1.1  yamaguch 
   2417   1.1  yamaguch 			if (qp->qp_txr != NULL)
   2418   1.1  yamaguch 				ixl_txr_free(sc, qp->qp_txr);
   2419   1.1  yamaguch 			if (qp->qp_rxr != NULL)
   2420   1.1  yamaguch 				ixl_rxr_free(sc, qp->qp_rxr);
   2421   1.6  yamaguch 			if (qp->qp_si != NULL)
   2422   1.6  yamaguch 				softint_disestablish(qp->qp_si);
   2423   1.1  yamaguch 		}
   2424   1.1  yamaguch 
   2425   1.1  yamaguch 		sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   2426   1.1  yamaguch 		kmem_free(sc->sc_qps, sz);
   2427   1.1  yamaguch 		sc->sc_qps = NULL;
   2428   1.1  yamaguch 	}
   2429   1.1  yamaguch 
   2430   1.1  yamaguch 	return -1;
   2431   1.1  yamaguch }
   2432   1.1  yamaguch 
   2433   1.1  yamaguch static void
   2434   1.1  yamaguch ixl_queue_pairs_free(struct ixl_softc *sc)
   2435   1.1  yamaguch {
   2436   1.1  yamaguch 	struct ixl_queue_pair *qp;
   2437   1.1  yamaguch 	unsigned int i;
   2438   1.1  yamaguch 	size_t sz;
   2439   1.1  yamaguch 
   2440   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   2441   1.1  yamaguch 		qp = &sc->sc_qps[i];
   2442   1.1  yamaguch 		ixl_txr_free(sc, qp->qp_txr);
   2443   1.1  yamaguch 		ixl_rxr_free(sc, qp->qp_rxr);
   2444   1.6  yamaguch 		softint_disestablish(qp->qp_si);
   2445   1.1  yamaguch 	}
   2446   1.1  yamaguch 
   2447   1.1  yamaguch 	sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   2448   1.1  yamaguch 	kmem_free(sc->sc_qps, sz);
   2449   1.1  yamaguch 	sc->sc_qps = NULL;
   2450   1.1  yamaguch }
   2451   1.1  yamaguch 
   2452   1.1  yamaguch static struct ixl_tx_ring *
   2453   1.1  yamaguch ixl_txr_alloc(struct ixl_softc *sc, unsigned int qid)
   2454   1.1  yamaguch {
   2455   1.1  yamaguch 	struct ixl_tx_ring *txr = NULL;
   2456   1.1  yamaguch 	struct ixl_tx_map *maps = NULL, *txm;
   2457   1.1  yamaguch 	unsigned int i;
   2458   1.1  yamaguch 
   2459   1.1  yamaguch 	txr = kmem_zalloc(sizeof(*txr), KM_SLEEP);
   2460   1.1  yamaguch 	maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_tx_ring_ndescs,
   2461   1.1  yamaguch 	    KM_SLEEP);
   2462   1.1  yamaguch 
   2463   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &txr->txr_mem,
   2464   1.1  yamaguch 	    sizeof(struct ixl_tx_desc) * sc->sc_tx_ring_ndescs,
   2465   1.1  yamaguch 	    IXL_TX_QUEUE_ALIGN) != 0)
   2466   1.1  yamaguch 	    goto free;
   2467   1.1  yamaguch 
   2468   1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2469   1.1  yamaguch 		txm = &maps[i];
   2470   1.1  yamaguch 
   2471  1.29  yamaguch 		if (bus_dmamap_create(sc->sc_dmat, IXL_TX_PKT_MAXSIZE,
   2472  1.29  yamaguch 		    IXL_TX_PKT_DESCS, IXL_TX_PKT_MAXSIZE, 0,
   2473   1.1  yamaguch 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &txm->txm_map) != 0)
   2474   1.1  yamaguch 			goto uncreate;
   2475   1.1  yamaguch 
   2476   1.1  yamaguch 		txm->txm_eop = -1;
   2477   1.1  yamaguch 		txm->txm_m = NULL;
   2478   1.1  yamaguch 	}
   2479   1.1  yamaguch 
   2480   1.1  yamaguch 	txr->txr_cons = txr->txr_prod = 0;
   2481   1.1  yamaguch 	txr->txr_maps = maps;
   2482   1.1  yamaguch 
   2483   1.1  yamaguch 	txr->txr_intrq = pcq_create(sc->sc_tx_ring_ndescs, KM_NOSLEEP);
   2484   1.1  yamaguch 	if (txr->txr_intrq == NULL)
   2485   1.1  yamaguch 		goto uncreate;
   2486   1.1  yamaguch 
   2487   1.1  yamaguch 	txr->txr_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   2488   1.1  yamaguch 	    ixl_deferred_transmit, txr);
   2489   1.1  yamaguch 	if (txr->txr_si == NULL)
   2490   1.1  yamaguch 		goto destroy_pcq;
   2491   1.1  yamaguch 
   2492   1.1  yamaguch 	txr->txr_tail = I40E_QTX_TAIL(qid);
   2493   1.1  yamaguch 	txr->txr_qid = qid;
   2494   1.1  yamaguch 	txr->txr_sc = sc;
   2495   1.1  yamaguch 	mutex_init(&txr->txr_lock, MUTEX_DEFAULT, IPL_NET);
   2496   1.1  yamaguch 
   2497   1.1  yamaguch 	return txr;
   2498   1.1  yamaguch 
   2499   1.1  yamaguch destroy_pcq:
   2500   1.1  yamaguch 	pcq_destroy(txr->txr_intrq);
   2501   1.1  yamaguch uncreate:
   2502   1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2503   1.1  yamaguch 		txm = &maps[i];
   2504   1.1  yamaguch 
   2505   1.1  yamaguch 		if (txm->txm_map == NULL)
   2506   1.1  yamaguch 			continue;
   2507   1.1  yamaguch 
   2508   1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, txm->txm_map);
   2509   1.1  yamaguch 	}
   2510   1.1  yamaguch 
   2511   1.1  yamaguch 	ixl_dmamem_free(sc, &txr->txr_mem);
   2512   1.1  yamaguch free:
   2513   1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
   2514   1.1  yamaguch 	kmem_free(txr, sizeof(*txr));
   2515   1.1  yamaguch 
   2516   1.1  yamaguch 	return NULL;
   2517   1.1  yamaguch }
   2518   1.1  yamaguch 
   2519   1.1  yamaguch static void
   2520   1.1  yamaguch ixl_txr_qdis(struct ixl_softc *sc, struct ixl_tx_ring *txr, int enable)
   2521   1.1  yamaguch {
   2522   1.1  yamaguch 	unsigned int qid;
   2523   1.1  yamaguch 	bus_size_t reg;
   2524   1.1  yamaguch 	uint32_t r;
   2525   1.1  yamaguch 
   2526   1.1  yamaguch 	qid = txr->txr_qid + sc->sc_base_queue;
   2527   1.1  yamaguch 	reg = I40E_GLLAN_TXPRE_QDIS(qid / 128);
   2528   1.1  yamaguch 	qid %= 128;
   2529   1.1  yamaguch 
   2530   1.1  yamaguch 	r = ixl_rd(sc, reg);
   2531   1.1  yamaguch 	CLR(r, I40E_GLLAN_TXPRE_QDIS_QINDX_MASK);
   2532   1.1  yamaguch 	SET(r, qid << I40E_GLLAN_TXPRE_QDIS_QINDX_SHIFT);
   2533   1.1  yamaguch 	SET(r, enable ? I40E_GLLAN_TXPRE_QDIS_CLEAR_QDIS_MASK :
   2534   1.1  yamaguch 	    I40E_GLLAN_TXPRE_QDIS_SET_QDIS_MASK);
   2535   1.1  yamaguch 	ixl_wr(sc, reg, r);
   2536   1.1  yamaguch }
   2537   1.1  yamaguch 
   2538   1.1  yamaguch static void
   2539   1.1  yamaguch ixl_txr_config(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2540   1.1  yamaguch {
   2541   1.1  yamaguch 	struct ixl_hmc_txq txq;
   2542   1.1  yamaguch 	struct ixl_aq_vsi_data *data = IXL_DMA_KVA(&sc->sc_scratch);
   2543   1.1  yamaguch 	void *hmc;
   2544   1.1  yamaguch 
   2545   1.1  yamaguch 	memset(&txq, 0, sizeof(txq));
   2546   1.1  yamaguch 	txq.head = htole16(txr->txr_cons);
   2547   1.1  yamaguch 	txq.new_context = 1;
   2548   1.1  yamaguch 	txq.base = htole64(IXL_DMA_DVA(&txr->txr_mem) / IXL_HMC_TXQ_BASE_UNIT);
   2549   1.1  yamaguch 	txq.head_wb_ena = IXL_HMC_TXQ_DESC_WB;
   2550   1.1  yamaguch 	txq.qlen = htole16(sc->sc_tx_ring_ndescs);
   2551   1.1  yamaguch 	txq.tphrdesc_ena = 0;
   2552   1.1  yamaguch 	txq.tphrpacket_ena = 0;
   2553   1.1  yamaguch 	txq.tphwdesc_ena = 0;
   2554   1.1  yamaguch 	txq.rdylist = data->qs_handle[0];
   2555   1.1  yamaguch 
   2556   1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_TX, txr->txr_qid);
   2557   1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_TX));
   2558   1.1  yamaguch 	ixl_hmc_pack(hmc, &txq, ixl_hmc_pack_txq,
   2559   1.1  yamaguch 	    __arraycount(ixl_hmc_pack_txq));
   2560   1.1  yamaguch }
   2561   1.1  yamaguch 
   2562   1.1  yamaguch static void
   2563   1.1  yamaguch ixl_txr_unconfig(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2564   1.1  yamaguch {
   2565   1.1  yamaguch 	void *hmc;
   2566   1.1  yamaguch 
   2567   1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_TX, txr->txr_qid);
   2568   1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_TX));
   2569  1.46  yamaguch 	txr->txr_cons = txr->txr_prod = 0;
   2570   1.1  yamaguch }
   2571   1.1  yamaguch 
   2572   1.1  yamaguch static void
   2573   1.1  yamaguch ixl_txr_clean(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2574   1.1  yamaguch {
   2575   1.1  yamaguch 	struct ixl_tx_map *maps, *txm;
   2576   1.1  yamaguch 	bus_dmamap_t map;
   2577   1.1  yamaguch 	unsigned int i;
   2578   1.1  yamaguch 
   2579   1.1  yamaguch 	maps = txr->txr_maps;
   2580   1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2581   1.1  yamaguch 		txm = &maps[i];
   2582   1.1  yamaguch 
   2583   1.1  yamaguch 		if (txm->txm_m == NULL)
   2584   1.1  yamaguch 			continue;
   2585   1.1  yamaguch 
   2586   1.1  yamaguch 		map = txm->txm_map;
   2587   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2588   1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   2589   1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2590   1.1  yamaguch 
   2591   1.1  yamaguch 		m_freem(txm->txm_m);
   2592   1.1  yamaguch 		txm->txm_m = NULL;
   2593   1.1  yamaguch 	}
   2594   1.1  yamaguch }
   2595   1.1  yamaguch 
   2596   1.1  yamaguch static int
   2597   1.1  yamaguch ixl_txr_enabled(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2598   1.1  yamaguch {
   2599   1.1  yamaguch 	bus_size_t ena = I40E_QTX_ENA(txr->txr_qid);
   2600   1.1  yamaguch 	uint32_t reg;
   2601   1.1  yamaguch 	int i;
   2602   1.1  yamaguch 
   2603   1.1  yamaguch 	for (i = 0; i < 10; i++) {
   2604   1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2605   1.1  yamaguch 		if (ISSET(reg, I40E_QTX_ENA_QENA_STAT_MASK))
   2606   1.1  yamaguch 			return 0;
   2607   1.1  yamaguch 
   2608   1.1  yamaguch 		delaymsec(10);
   2609   1.1  yamaguch 	}
   2610   1.1  yamaguch 
   2611   1.1  yamaguch 	return ETIMEDOUT;
   2612   1.1  yamaguch }
   2613   1.1  yamaguch 
   2614   1.1  yamaguch static int
   2615   1.1  yamaguch ixl_txr_disabled(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2616   1.1  yamaguch {
   2617   1.1  yamaguch 	bus_size_t ena = I40E_QTX_ENA(txr->txr_qid);
   2618   1.1  yamaguch 	uint32_t reg;
   2619   1.1  yamaguch 	int i;
   2620   1.1  yamaguch 
   2621   1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2622   1.1  yamaguch 
   2623  1.53  yamaguch 	for (i = 0; i < 10; i++) {
   2624   1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2625   1.1  yamaguch 		if (ISSET(reg, I40E_QTX_ENA_QENA_STAT_MASK) == 0)
   2626   1.1  yamaguch 			return 0;
   2627   1.1  yamaguch 
   2628   1.1  yamaguch 		delaymsec(10);
   2629   1.1  yamaguch 	}
   2630   1.1  yamaguch 
   2631   1.1  yamaguch 	return ETIMEDOUT;
   2632   1.1  yamaguch }
   2633   1.1  yamaguch 
   2634   1.1  yamaguch static void
   2635   1.1  yamaguch ixl_txr_free(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2636   1.1  yamaguch {
   2637   1.1  yamaguch 	struct ixl_tx_map *maps, *txm;
   2638   1.1  yamaguch 	struct mbuf *m;
   2639   1.1  yamaguch 	unsigned int i;
   2640   1.1  yamaguch 
   2641   1.1  yamaguch 	softint_disestablish(txr->txr_si);
   2642   1.1  yamaguch 	while ((m = pcq_get(txr->txr_intrq)) != NULL)
   2643   1.1  yamaguch 		m_freem(m);
   2644   1.1  yamaguch 	pcq_destroy(txr->txr_intrq);
   2645   1.1  yamaguch 
   2646   1.1  yamaguch 	maps = txr->txr_maps;
   2647   1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2648   1.1  yamaguch 		txm = &maps[i];
   2649   1.1  yamaguch 
   2650   1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, txm->txm_map);
   2651   1.1  yamaguch 	}
   2652   1.1  yamaguch 
   2653   1.1  yamaguch 	ixl_dmamem_free(sc, &txr->txr_mem);
   2654   1.1  yamaguch 	mutex_destroy(&txr->txr_lock);
   2655   1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
   2656   1.1  yamaguch 	kmem_free(txr, sizeof(*txr));
   2657   1.1  yamaguch }
   2658   1.1  yamaguch 
   2659   1.1  yamaguch static inline int
   2660   1.1  yamaguch ixl_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf **m0,
   2661   1.1  yamaguch     struct ixl_tx_ring *txr)
   2662   1.1  yamaguch {
   2663   1.1  yamaguch 	struct mbuf *m;
   2664   1.1  yamaguch 	int error;
   2665   1.1  yamaguch 
   2666   1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2667   1.1  yamaguch 
   2668   1.1  yamaguch 	m = *m0;
   2669   1.1  yamaguch 
   2670   1.1  yamaguch 	error = bus_dmamap_load_mbuf(dmat, map, m,
   2671   1.1  yamaguch 	    BUS_DMA_STREAMING | BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2672   1.1  yamaguch 	if (error != EFBIG)
   2673   1.1  yamaguch 		return error;
   2674   1.1  yamaguch 
   2675   1.1  yamaguch 	m = m_defrag(m, M_DONTWAIT);
   2676   1.1  yamaguch 	if (m != NULL) {
   2677   1.1  yamaguch 		*m0 = m;
   2678   1.1  yamaguch 		txr->txr_defragged.ev_count++;
   2679   1.1  yamaguch 
   2680   1.1  yamaguch 		error = bus_dmamap_load_mbuf(dmat, map, m,
   2681   1.1  yamaguch 		    BUS_DMA_STREAMING | BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2682   1.1  yamaguch 	} else {
   2683   1.1  yamaguch 		txr->txr_defrag_failed.ev_count++;
   2684   1.1  yamaguch 		error = ENOBUFS;
   2685   1.1  yamaguch 	}
   2686   1.1  yamaguch 
   2687   1.1  yamaguch 	return error;
   2688   1.1  yamaguch }
   2689   1.1  yamaguch 
   2690  1.28  yamaguch static inline int
   2691  1.28  yamaguch ixl_tx_setup_offloads(struct mbuf *m, uint64_t *cmd_txd)
   2692  1.28  yamaguch {
   2693  1.28  yamaguch 	struct ether_header *eh;
   2694  1.28  yamaguch 	size_t len;
   2695  1.28  yamaguch 	uint64_t cmd;
   2696  1.28  yamaguch 
   2697  1.28  yamaguch 	cmd = 0;
   2698  1.28  yamaguch 
   2699  1.28  yamaguch 	eh = mtod(m, struct ether_header *);
   2700  1.28  yamaguch 	switch (htons(eh->ether_type)) {
   2701  1.28  yamaguch 	case ETHERTYPE_IP:
   2702  1.28  yamaguch 	case ETHERTYPE_IPV6:
   2703  1.28  yamaguch 		len = ETHER_HDR_LEN;
   2704  1.28  yamaguch 		break;
   2705  1.28  yamaguch 	case ETHERTYPE_VLAN:
   2706  1.28  yamaguch 		len = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   2707  1.28  yamaguch 		break;
   2708  1.28  yamaguch 	default:
   2709  1.28  yamaguch 		len = 0;
   2710  1.28  yamaguch 	}
   2711  1.28  yamaguch 	cmd |= ((len >> 1) << IXL_TX_DESC_MACLEN_SHIFT);
   2712  1.28  yamaguch 
   2713  1.28  yamaguch 	if (m->m_pkthdr.csum_flags &
   2714  1.28  yamaguch 	    (M_CSUM_TSOv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
   2715  1.28  yamaguch 		cmd |= IXL_TX_DESC_CMD_IIPT_IPV4;
   2716  1.28  yamaguch 	}
   2717  1.28  yamaguch 	if (m->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   2718  1.28  yamaguch 		cmd |= IXL_TX_DESC_CMD_IIPT_IPV4_CSUM;
   2719  1.28  yamaguch 	}
   2720  1.28  yamaguch 
   2721  1.28  yamaguch 	if (m->m_pkthdr.csum_flags &
   2722  1.28  yamaguch 	    (M_CSUM_TSOv6 | M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
   2723  1.28  yamaguch 		cmd |= IXL_TX_DESC_CMD_IIPT_IPV6;
   2724  1.28  yamaguch 	}
   2725  1.28  yamaguch 
   2726  1.28  yamaguch 	switch (cmd & IXL_TX_DESC_CMD_IIPT_MASK) {
   2727  1.28  yamaguch 	case IXL_TX_DESC_CMD_IIPT_IPV4:
   2728  1.28  yamaguch 	case IXL_TX_DESC_CMD_IIPT_IPV4_CSUM:
   2729  1.28  yamaguch 		len = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
   2730  1.28  yamaguch 		break;
   2731  1.28  yamaguch 	case IXL_TX_DESC_CMD_IIPT_IPV6:
   2732  1.28  yamaguch 		len = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
   2733  1.28  yamaguch 		break;
   2734  1.28  yamaguch 	default:
   2735  1.28  yamaguch 		len = 0;
   2736  1.28  yamaguch 	}
   2737  1.28  yamaguch 	cmd |= ((len >> 2) << IXL_TX_DESC_IPLEN_SHIFT);
   2738  1.28  yamaguch 
   2739  1.28  yamaguch 	if (m->m_pkthdr.csum_flags &
   2740  1.28  yamaguch 	    (M_CSUM_TSOv4 | M_CSUM_TSOv6 | M_CSUM_TCPv4 | M_CSUM_TCPv6)) {
   2741  1.28  yamaguch 		len = sizeof(struct tcphdr);
   2742  1.28  yamaguch 		cmd |= IXL_TX_DESC_CMD_L4T_EOFT_TCP;
   2743  1.28  yamaguch 	} else if (m->m_pkthdr.csum_flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) {
   2744  1.28  yamaguch 		len = sizeof(struct udphdr);
   2745  1.28  yamaguch 		cmd |= IXL_TX_DESC_CMD_L4T_EOFT_UDP;
   2746  1.28  yamaguch 	} else {
   2747  1.28  yamaguch 		len = 0;
   2748  1.28  yamaguch 	}
   2749  1.28  yamaguch 	cmd |= ((len >> 2) << IXL_TX_DESC_L4LEN_SHIFT);
   2750  1.28  yamaguch 
   2751  1.28  yamaguch 	*cmd_txd |= cmd;
   2752  1.28  yamaguch 	return 0;
   2753  1.28  yamaguch }
   2754  1.28  yamaguch 
   2755   1.1  yamaguch static void
   2756   1.1  yamaguch ixl_tx_common_locked(struct ifnet *ifp, struct ixl_tx_ring *txr,
   2757   1.1  yamaguch     bool is_transmit)
   2758   1.1  yamaguch {
   2759   1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   2760   1.1  yamaguch 	struct ixl_tx_desc *ring, *txd;
   2761   1.1  yamaguch 	struct ixl_tx_map *txm;
   2762   1.1  yamaguch 	bus_dmamap_t map;
   2763   1.1  yamaguch 	struct mbuf *m;
   2764  1.28  yamaguch 	uint64_t cmd, cmd_txd;
   2765   1.1  yamaguch 	unsigned int prod, free, last, i;
   2766   1.1  yamaguch 	unsigned int mask;
   2767   1.1  yamaguch 	int post = 0;
   2768   1.1  yamaguch 
   2769   1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2770   1.1  yamaguch 
   2771  1.60  yamaguch 	if (!ISSET(ifp->if_flags, IFF_RUNNING)
   2772   1.1  yamaguch 	    || (!is_transmit && ISSET(ifp->if_flags, IFF_OACTIVE))) {
   2773   1.1  yamaguch 		if (!is_transmit)
   2774   1.1  yamaguch 			IFQ_PURGE(&ifp->if_snd);
   2775   1.1  yamaguch 		return;
   2776   1.1  yamaguch 	}
   2777   1.1  yamaguch 
   2778   1.1  yamaguch 	prod = txr->txr_prod;
   2779   1.1  yamaguch 	free = txr->txr_cons;
   2780   1.1  yamaguch 	if (free <= prod)
   2781   1.1  yamaguch 		free += sc->sc_tx_ring_ndescs;
   2782   1.1  yamaguch 	free -= prod;
   2783   1.1  yamaguch 
   2784   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2785   1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTWRITE);
   2786   1.1  yamaguch 
   2787   1.1  yamaguch 	ring = IXL_DMA_KVA(&txr->txr_mem);
   2788   1.1  yamaguch 	mask = sc->sc_tx_ring_ndescs - 1;
   2789   1.1  yamaguch 	last = prod;
   2790   1.1  yamaguch 	cmd = 0;
   2791   1.1  yamaguch 	txd = NULL;
   2792   1.1  yamaguch 
   2793   1.1  yamaguch 	for (;;) {
   2794   1.1  yamaguch 		if (free <= IXL_TX_PKT_DESCS) {
   2795   1.1  yamaguch 			if (!is_transmit)
   2796   1.1  yamaguch 				SET(ifp->if_flags, IFF_OACTIVE);
   2797   1.1  yamaguch 			break;
   2798   1.1  yamaguch 		}
   2799   1.1  yamaguch 
   2800   1.1  yamaguch 		if (is_transmit)
   2801   1.1  yamaguch 			m = pcq_get(txr->txr_intrq);
   2802   1.1  yamaguch 		else
   2803   1.1  yamaguch 			IFQ_DEQUEUE(&ifp->if_snd, m);
   2804   1.1  yamaguch 
   2805   1.1  yamaguch 		if (m == NULL)
   2806   1.1  yamaguch 			break;
   2807   1.1  yamaguch 
   2808   1.1  yamaguch 		txm = &txr->txr_maps[prod];
   2809   1.1  yamaguch 		map = txm->txm_map;
   2810   1.1  yamaguch 
   2811   1.1  yamaguch 		if (ixl_load_mbuf(sc->sc_dmat, map, &m, txr) != 0) {
   2812  1.35   thorpej 			if_statinc(ifp, if_oerrors);
   2813   1.1  yamaguch 			m_freem(m);
   2814   1.1  yamaguch 			continue;
   2815   1.1  yamaguch 		}
   2816   1.1  yamaguch 
   2817  1.28  yamaguch 		cmd_txd = 0;
   2818  1.28  yamaguch 		if (m->m_pkthdr.csum_flags & IXL_CSUM_ALL_OFFLOAD) {
   2819  1.28  yamaguch 			ixl_tx_setup_offloads(m, &cmd_txd);
   2820  1.28  yamaguch 		}
   2821  1.28  yamaguch 
   2822  1.11  yamaguch 		if (vlan_has_tag(m)) {
   2823  1.28  yamaguch 			cmd_txd |= (uint64_t)vlan_get_tag(m) <<
   2824  1.11  yamaguch 			    IXL_TX_DESC_L2TAG1_SHIFT;
   2825  1.28  yamaguch 			cmd_txd |= IXL_TX_DESC_CMD_IL2TAG1;
   2826  1.11  yamaguch 		}
   2827  1.11  yamaguch 
   2828   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0,
   2829   1.1  yamaguch 		    map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2830   1.1  yamaguch 
   2831   1.1  yamaguch 		for (i = 0; i < (unsigned int)map->dm_nsegs; i++) {
   2832   1.1  yamaguch 			txd = &ring[prod];
   2833   1.1  yamaguch 
   2834   1.1  yamaguch 			cmd = (uint64_t)map->dm_segs[i].ds_len <<
   2835   1.1  yamaguch 			    IXL_TX_DESC_BSIZE_SHIFT;
   2836   1.1  yamaguch 			cmd |= IXL_TX_DESC_DTYPE_DATA | IXL_TX_DESC_CMD_ICRC;
   2837  1.28  yamaguch 			cmd |= cmd_txd;
   2838   1.1  yamaguch 
   2839   1.1  yamaguch 			txd->addr = htole64(map->dm_segs[i].ds_addr);
   2840   1.1  yamaguch 			txd->cmd = htole64(cmd);
   2841   1.1  yamaguch 
   2842   1.1  yamaguch 			last = prod;
   2843   1.1  yamaguch 
   2844   1.1  yamaguch 			prod++;
   2845   1.1  yamaguch 			prod &= mask;
   2846   1.1  yamaguch 		}
   2847   1.1  yamaguch 		cmd |= IXL_TX_DESC_CMD_EOP | IXL_TX_DESC_CMD_RS;
   2848   1.1  yamaguch 		txd->cmd = htole64(cmd);
   2849   1.1  yamaguch 
   2850   1.1  yamaguch 		txm->txm_m = m;
   2851   1.1  yamaguch 		txm->txm_eop = last;
   2852   1.1  yamaguch 
   2853   1.1  yamaguch 		bpf_mtap(ifp, m, BPF_D_OUT);
   2854   1.1  yamaguch 
   2855   1.1  yamaguch 		free -= i;
   2856   1.1  yamaguch 		post = 1;
   2857   1.1  yamaguch 	}
   2858   1.1  yamaguch 
   2859   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2860   1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREWRITE);
   2861   1.1  yamaguch 
   2862   1.1  yamaguch 	if (post) {
   2863   1.1  yamaguch 		txr->txr_prod = prod;
   2864   1.1  yamaguch 		ixl_wr(sc, txr->txr_tail, prod);
   2865   1.1  yamaguch 	}
   2866   1.1  yamaguch }
   2867   1.1  yamaguch 
   2868   1.1  yamaguch static int
   2869   1.1  yamaguch ixl_txeof(struct ixl_softc *sc, struct ixl_tx_ring *txr, u_int txlimit)
   2870   1.1  yamaguch {
   2871   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2872   1.1  yamaguch 	struct ixl_tx_desc *ring, *txd;
   2873   1.1  yamaguch 	struct ixl_tx_map *txm;
   2874   1.1  yamaguch 	struct mbuf *m;
   2875   1.1  yamaguch 	bus_dmamap_t map;
   2876   1.1  yamaguch 	unsigned int cons, prod, last;
   2877   1.1  yamaguch 	unsigned int mask;
   2878   1.1  yamaguch 	uint64_t dtype;
   2879   1.1  yamaguch 	int done = 0, more = 0;
   2880   1.1  yamaguch 
   2881   1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2882   1.1  yamaguch 
   2883   1.1  yamaguch 	prod = txr->txr_prod;
   2884   1.1  yamaguch 	cons = txr->txr_cons;
   2885   1.1  yamaguch 
   2886   1.1  yamaguch 	if (cons == prod)
   2887   1.1  yamaguch 		return 0;
   2888   1.1  yamaguch 
   2889   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2890   1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTREAD);
   2891   1.1  yamaguch 
   2892   1.1  yamaguch 	ring = IXL_DMA_KVA(&txr->txr_mem);
   2893   1.1  yamaguch 	mask = sc->sc_tx_ring_ndescs - 1;
   2894   1.1  yamaguch 
   2895  1.35   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   2896  1.35   thorpej 
   2897   1.1  yamaguch 	do {
   2898   1.1  yamaguch 		if (txlimit-- <= 0) {
   2899   1.1  yamaguch 			more = 1;
   2900   1.1  yamaguch 			break;
   2901   1.1  yamaguch 		}
   2902   1.1  yamaguch 
   2903   1.1  yamaguch 		txm = &txr->txr_maps[cons];
   2904   1.1  yamaguch 		last = txm->txm_eop;
   2905   1.1  yamaguch 		txd = &ring[last];
   2906   1.1  yamaguch 
   2907   1.1  yamaguch 		dtype = txd->cmd & htole64(IXL_TX_DESC_DTYPE_MASK);
   2908   1.1  yamaguch 		if (dtype != htole64(IXL_TX_DESC_DTYPE_DONE))
   2909   1.1  yamaguch 			break;
   2910   1.1  yamaguch 
   2911   1.1  yamaguch 		map = txm->txm_map;
   2912   1.1  yamaguch 
   2913   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2914   1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   2915   1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2916   1.1  yamaguch 
   2917   1.1  yamaguch 		m = txm->txm_m;
   2918   1.1  yamaguch 		if (m != NULL) {
   2919  1.35   thorpej 			if_statinc_ref(nsr, if_opackets);
   2920  1.35   thorpej 			if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   2921   1.1  yamaguch 			if (ISSET(m->m_flags, M_MCAST))
   2922  1.35   thorpej 				if_statinc_ref(nsr, if_omcasts);
   2923   1.1  yamaguch 			m_freem(m);
   2924   1.1  yamaguch 		}
   2925   1.1  yamaguch 
   2926   1.1  yamaguch 		txm->txm_m = NULL;
   2927   1.1  yamaguch 		txm->txm_eop = -1;
   2928   1.1  yamaguch 
   2929   1.1  yamaguch 		cons = last + 1;
   2930   1.1  yamaguch 		cons &= mask;
   2931   1.1  yamaguch 		done = 1;
   2932   1.1  yamaguch 	} while (cons != prod);
   2933   1.1  yamaguch 
   2934  1.35   thorpej 	IF_STAT_PUTREF(ifp);
   2935  1.35   thorpej 
   2936   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2937   1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREREAD);
   2938   1.1  yamaguch 
   2939   1.1  yamaguch 	txr->txr_cons = cons;
   2940   1.1  yamaguch 
   2941   1.1  yamaguch 	if (done) {
   2942   1.1  yamaguch 		softint_schedule(txr->txr_si);
   2943   1.1  yamaguch 		if (txr->txr_qid == 0) {
   2944   1.1  yamaguch 			CLR(ifp->if_flags, IFF_OACTIVE);
   2945   1.1  yamaguch 			if_schedule_deferred_start(ifp);
   2946   1.1  yamaguch 		}
   2947   1.1  yamaguch 	}
   2948   1.1  yamaguch 
   2949   1.1  yamaguch 	return more;
   2950   1.1  yamaguch }
   2951   1.1  yamaguch 
   2952   1.1  yamaguch static void
   2953   1.1  yamaguch ixl_start(struct ifnet *ifp)
   2954   1.1  yamaguch {
   2955   1.1  yamaguch 	struct ixl_softc	*sc;
   2956   1.1  yamaguch 	struct ixl_tx_ring	*txr;
   2957   1.1  yamaguch 
   2958   1.1  yamaguch 	sc = ifp->if_softc;
   2959   1.1  yamaguch 	txr = sc->sc_qps[0].qp_txr;
   2960   1.1  yamaguch 
   2961   1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   2962   1.1  yamaguch 	ixl_tx_common_locked(ifp, txr, false);
   2963   1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   2964   1.1  yamaguch }
   2965   1.1  yamaguch 
   2966   1.1  yamaguch static inline unsigned int
   2967   1.1  yamaguch ixl_select_txqueue(struct ixl_softc *sc, struct mbuf *m)
   2968   1.1  yamaguch {
   2969   1.1  yamaguch 	u_int cpuid;
   2970   1.1  yamaguch 
   2971   1.1  yamaguch 	cpuid = cpu_index(curcpu());
   2972   1.1  yamaguch 
   2973   1.1  yamaguch 	return (unsigned int)(cpuid % sc->sc_nqueue_pairs);
   2974   1.1  yamaguch }
   2975   1.1  yamaguch 
   2976   1.1  yamaguch static int
   2977   1.1  yamaguch ixl_transmit(struct ifnet *ifp, struct mbuf *m)
   2978   1.1  yamaguch {
   2979   1.1  yamaguch 	struct ixl_softc *sc;
   2980   1.1  yamaguch 	struct ixl_tx_ring *txr;
   2981   1.1  yamaguch 	unsigned int qid;
   2982   1.1  yamaguch 
   2983   1.1  yamaguch 	sc = ifp->if_softc;
   2984   1.1  yamaguch 	qid = ixl_select_txqueue(sc, m);
   2985   1.1  yamaguch 
   2986   1.1  yamaguch 	txr = sc->sc_qps[qid].qp_txr;
   2987   1.1  yamaguch 
   2988   1.1  yamaguch 	if (__predict_false(!pcq_put(txr->txr_intrq, m))) {
   2989   1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   2990   1.1  yamaguch 		txr->txr_pcqdrop.ev_count++;
   2991   1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   2992   1.1  yamaguch 
   2993   1.1  yamaguch 		m_freem(m);
   2994   1.1  yamaguch 		return ENOBUFS;
   2995   1.1  yamaguch 	}
   2996   1.1  yamaguch 
   2997   1.1  yamaguch 	if (mutex_tryenter(&txr->txr_lock)) {
   2998   1.1  yamaguch 		ixl_tx_common_locked(ifp, txr, true);
   2999   1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   3000   1.1  yamaguch 	} else {
   3001  1.33  yamaguch 		kpreempt_disable();
   3002   1.1  yamaguch 		softint_schedule(txr->txr_si);
   3003  1.33  yamaguch 		kpreempt_enable();
   3004   1.1  yamaguch 	}
   3005   1.1  yamaguch 
   3006   1.1  yamaguch 	return 0;
   3007   1.1  yamaguch }
   3008   1.1  yamaguch 
   3009   1.1  yamaguch static void
   3010   1.1  yamaguch ixl_deferred_transmit(void *xtxr)
   3011   1.1  yamaguch {
   3012   1.1  yamaguch 	struct ixl_tx_ring *txr = xtxr;
   3013   1.1  yamaguch 	struct ixl_softc *sc = txr->txr_sc;
   3014   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3015   1.1  yamaguch 
   3016   1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   3017   1.1  yamaguch 	txr->txr_transmitdef.ev_count++;
   3018   1.1  yamaguch 	if (pcq_peek(txr->txr_intrq) != NULL)
   3019   1.1  yamaguch 		ixl_tx_common_locked(ifp, txr, true);
   3020   1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   3021   1.1  yamaguch }
   3022   1.1  yamaguch 
   3023   1.1  yamaguch static struct ixl_rx_ring *
   3024   1.1  yamaguch ixl_rxr_alloc(struct ixl_softc *sc, unsigned int qid)
   3025   1.1  yamaguch {
   3026   1.1  yamaguch 	struct ixl_rx_ring *rxr = NULL;
   3027   1.1  yamaguch 	struct ixl_rx_map *maps = NULL, *rxm;
   3028   1.1  yamaguch 	unsigned int i;
   3029   1.1  yamaguch 
   3030   1.1  yamaguch 	rxr = kmem_zalloc(sizeof(*rxr), KM_SLEEP);
   3031   1.1  yamaguch 	maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_rx_ring_ndescs,
   3032   1.1  yamaguch 	    KM_SLEEP);
   3033   1.1  yamaguch 
   3034   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &rxr->rxr_mem,
   3035  1.11  yamaguch 	    sizeof(struct ixl_rx_rd_desc_32) * sc->sc_rx_ring_ndescs,
   3036   1.1  yamaguch 	    IXL_RX_QUEUE_ALIGN) != 0)
   3037   1.1  yamaguch 		goto free;
   3038   1.1  yamaguch 
   3039   1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   3040   1.1  yamaguch 		rxm = &maps[i];
   3041   1.1  yamaguch 
   3042   1.1  yamaguch 		if (bus_dmamap_create(sc->sc_dmat,
   3043  1.29  yamaguch 		    IXL_MCLBYTES, 1, IXL_MCLBYTES, 0,
   3044   1.1  yamaguch 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &rxm->rxm_map) != 0)
   3045   1.1  yamaguch 			goto uncreate;
   3046   1.1  yamaguch 
   3047   1.1  yamaguch 		rxm->rxm_m = NULL;
   3048   1.1  yamaguch 	}
   3049   1.1  yamaguch 
   3050   1.1  yamaguch 	rxr->rxr_cons = rxr->rxr_prod = 0;
   3051   1.1  yamaguch 	rxr->rxr_m_head = NULL;
   3052   1.1  yamaguch 	rxr->rxr_m_tail = &rxr->rxr_m_head;
   3053   1.1  yamaguch 	rxr->rxr_maps = maps;
   3054   1.1  yamaguch 
   3055   1.1  yamaguch 	rxr->rxr_tail = I40E_QRX_TAIL(qid);
   3056   1.1  yamaguch 	rxr->rxr_qid = qid;
   3057   1.1  yamaguch 	mutex_init(&rxr->rxr_lock, MUTEX_DEFAULT, IPL_NET);
   3058   1.1  yamaguch 
   3059   1.1  yamaguch 	return rxr;
   3060   1.1  yamaguch 
   3061   1.1  yamaguch uncreate:
   3062   1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   3063   1.1  yamaguch 		rxm = &maps[i];
   3064   1.1  yamaguch 
   3065   1.1  yamaguch 		if (rxm->rxm_map == NULL)
   3066   1.1  yamaguch 			continue;
   3067   1.1  yamaguch 
   3068   1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, rxm->rxm_map);
   3069   1.1  yamaguch 	}
   3070   1.1  yamaguch 
   3071   1.1  yamaguch 	ixl_dmamem_free(sc, &rxr->rxr_mem);
   3072   1.1  yamaguch free:
   3073   1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
   3074   1.1  yamaguch 	kmem_free(rxr, sizeof(*rxr));
   3075   1.1  yamaguch 
   3076   1.1  yamaguch 	return NULL;
   3077   1.1  yamaguch }
   3078   1.1  yamaguch 
   3079   1.1  yamaguch static void
   3080   1.1  yamaguch ixl_rxr_clean(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3081   1.1  yamaguch {
   3082   1.1  yamaguch 	struct ixl_rx_map *maps, *rxm;
   3083   1.1  yamaguch 	bus_dmamap_t map;
   3084   1.1  yamaguch 	unsigned int i;
   3085   1.1  yamaguch 
   3086   1.1  yamaguch 	maps = rxr->rxr_maps;
   3087   1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   3088   1.1  yamaguch 		rxm = &maps[i];
   3089   1.1  yamaguch 
   3090   1.1  yamaguch 		if (rxm->rxm_m == NULL)
   3091   1.1  yamaguch 			continue;
   3092   1.1  yamaguch 
   3093   1.1  yamaguch 		map = rxm->rxm_map;
   3094   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   3095   1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   3096   1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   3097   1.1  yamaguch 
   3098   1.1  yamaguch 		m_freem(rxm->rxm_m);
   3099   1.1  yamaguch 		rxm->rxm_m = NULL;
   3100   1.1  yamaguch 	}
   3101   1.1  yamaguch 
   3102   1.1  yamaguch 	m_freem(rxr->rxr_m_head);
   3103   1.1  yamaguch 	rxr->rxr_m_head = NULL;
   3104   1.1  yamaguch 	rxr->rxr_m_tail = &rxr->rxr_m_head;
   3105   1.1  yamaguch 
   3106   1.1  yamaguch 	rxr->rxr_prod = rxr->rxr_cons = 0;
   3107   1.1  yamaguch }
   3108   1.1  yamaguch 
   3109   1.1  yamaguch static int
   3110   1.1  yamaguch ixl_rxr_enabled(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3111   1.1  yamaguch {
   3112   1.1  yamaguch 	bus_size_t ena = I40E_QRX_ENA(rxr->rxr_qid);
   3113   1.1  yamaguch 	uint32_t reg;
   3114   1.1  yamaguch 	int i;
   3115   1.1  yamaguch 
   3116   1.1  yamaguch 	for (i = 0; i < 10; i++) {
   3117   1.1  yamaguch 		reg = ixl_rd(sc, ena);
   3118   1.1  yamaguch 		if (ISSET(reg, I40E_QRX_ENA_QENA_STAT_MASK))
   3119   1.1  yamaguch 			return 0;
   3120   1.1  yamaguch 
   3121   1.1  yamaguch 		delaymsec(10);
   3122   1.1  yamaguch 	}
   3123   1.1  yamaguch 
   3124   1.1  yamaguch 	return ETIMEDOUT;
   3125   1.1  yamaguch }
   3126   1.1  yamaguch 
   3127   1.1  yamaguch static int
   3128   1.1  yamaguch ixl_rxr_disabled(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3129   1.1  yamaguch {
   3130   1.1  yamaguch 	bus_size_t ena = I40E_QRX_ENA(rxr->rxr_qid);
   3131   1.1  yamaguch 	uint32_t reg;
   3132   1.1  yamaguch 	int i;
   3133   1.1  yamaguch 
   3134   1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   3135   1.1  yamaguch 
   3136  1.53  yamaguch 	for (i = 0; i < 10; i++) {
   3137   1.1  yamaguch 		reg = ixl_rd(sc, ena);
   3138   1.1  yamaguch 		if (ISSET(reg, I40E_QRX_ENA_QENA_STAT_MASK) == 0)
   3139   1.1  yamaguch 			return 0;
   3140   1.1  yamaguch 
   3141   1.1  yamaguch 		delaymsec(10);
   3142   1.1  yamaguch 	}
   3143   1.1  yamaguch 
   3144   1.1  yamaguch 	return ETIMEDOUT;
   3145   1.1  yamaguch }
   3146   1.1  yamaguch 
   3147   1.1  yamaguch static void
   3148   1.1  yamaguch ixl_rxr_config(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3149   1.1  yamaguch {
   3150   1.1  yamaguch 	struct ixl_hmc_rxq rxq;
   3151  1.29  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3152  1.29  yamaguch 	uint16_t rxmax;
   3153   1.1  yamaguch 	void *hmc;
   3154   1.1  yamaguch 
   3155   1.1  yamaguch 	memset(&rxq, 0, sizeof(rxq));
   3156  1.29  yamaguch 	rxmax = ifp->if_mtu + IXL_MTU_ETHERLEN;
   3157   1.1  yamaguch 
   3158   1.1  yamaguch 	rxq.head = htole16(rxr->rxr_cons);
   3159   1.1  yamaguch 	rxq.base = htole64(IXL_DMA_DVA(&rxr->rxr_mem) / IXL_HMC_RXQ_BASE_UNIT);
   3160   1.1  yamaguch 	rxq.qlen = htole16(sc->sc_rx_ring_ndescs);
   3161  1.29  yamaguch 	rxq.dbuff = htole16(IXL_MCLBYTES / IXL_HMC_RXQ_DBUFF_UNIT);
   3162   1.1  yamaguch 	rxq.hbuff = 0;
   3163   1.1  yamaguch 	rxq.dtype = IXL_HMC_RXQ_DTYPE_NOSPLIT;
   3164  1.11  yamaguch 	rxq.dsize = IXL_HMC_RXQ_DSIZE_32;
   3165   1.1  yamaguch 	rxq.crcstrip = 1;
   3166  1.11  yamaguch 	rxq.l2sel = 1;
   3167  1.11  yamaguch 	rxq.showiv = 1;
   3168  1.29  yamaguch 	rxq.rxmax = htole16(rxmax);
   3169   1.1  yamaguch 	rxq.tphrdesc_ena = 0;
   3170   1.1  yamaguch 	rxq.tphwdesc_ena = 0;
   3171   1.1  yamaguch 	rxq.tphdata_ena = 0;
   3172   1.1  yamaguch 	rxq.tphhead_ena = 0;
   3173   1.1  yamaguch 	rxq.lrxqthresh = 0;
   3174   1.1  yamaguch 	rxq.prefena = 1;
   3175   1.1  yamaguch 
   3176   1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_RX, rxr->rxr_qid);
   3177   1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_RX));
   3178   1.1  yamaguch 	ixl_hmc_pack(hmc, &rxq, ixl_hmc_pack_rxq,
   3179   1.1  yamaguch 	    __arraycount(ixl_hmc_pack_rxq));
   3180   1.1  yamaguch }
   3181   1.1  yamaguch 
   3182   1.1  yamaguch static void
   3183   1.1  yamaguch ixl_rxr_unconfig(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3184   1.1  yamaguch {
   3185   1.1  yamaguch 	void *hmc;
   3186   1.1  yamaguch 
   3187   1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_RX, rxr->rxr_qid);
   3188   1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_RX));
   3189  1.46  yamaguch 	rxr->rxr_cons = rxr->rxr_prod = 0;
   3190   1.1  yamaguch }
   3191   1.1  yamaguch 
   3192   1.1  yamaguch static void
   3193   1.1  yamaguch ixl_rxr_free(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3194   1.1  yamaguch {
   3195   1.1  yamaguch 	struct ixl_rx_map *maps, *rxm;
   3196   1.1  yamaguch 	unsigned int i;
   3197   1.1  yamaguch 
   3198   1.1  yamaguch 	maps = rxr->rxr_maps;
   3199   1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   3200   1.1  yamaguch 		rxm = &maps[i];
   3201   1.1  yamaguch 
   3202   1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, rxm->rxm_map);
   3203   1.1  yamaguch 	}
   3204   1.1  yamaguch 
   3205   1.1  yamaguch 	ixl_dmamem_free(sc, &rxr->rxr_mem);
   3206   1.1  yamaguch 	mutex_destroy(&rxr->rxr_lock);
   3207   1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
   3208   1.1  yamaguch 	kmem_free(rxr, sizeof(*rxr));
   3209   1.1  yamaguch }
   3210   1.1  yamaguch 
   3211  1.14  yamaguch static inline void
   3212  1.14  yamaguch ixl_rx_csum(struct mbuf *m, uint64_t qword)
   3213  1.14  yamaguch {
   3214  1.14  yamaguch 	int flags_mask;
   3215  1.14  yamaguch 
   3216  1.14  yamaguch 	if (!ISSET(qword, IXL_RX_DESC_L3L4P)) {
   3217  1.14  yamaguch 		/* No L3 or L4 checksum was calculated */
   3218  1.14  yamaguch 		return;
   3219  1.14  yamaguch 	}
   3220  1.14  yamaguch 
   3221  1.14  yamaguch 	switch (__SHIFTOUT(qword, IXL_RX_DESC_PTYPE_MASK)) {
   3222  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_IPV4FRAG:
   3223  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_IPV4:
   3224  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_SCTPV4:
   3225  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_ICMPV4:
   3226  1.14  yamaguch 		flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
   3227  1.14  yamaguch 		break;
   3228  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_TCPV4:
   3229  1.14  yamaguch 		flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
   3230  1.14  yamaguch 		flags_mask |= M_CSUM_TCPv4 | M_CSUM_TCP_UDP_BAD;
   3231  1.14  yamaguch 		break;
   3232  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_UDPV4:
   3233  1.14  yamaguch 		flags_mask = M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
   3234  1.14  yamaguch 		flags_mask |= M_CSUM_UDPv4 | M_CSUM_TCP_UDP_BAD;
   3235  1.14  yamaguch 		break;
   3236  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_TCPV6:
   3237  1.14  yamaguch 		flags_mask = M_CSUM_TCPv6 | M_CSUM_TCP_UDP_BAD;
   3238  1.14  yamaguch 		break;
   3239  1.14  yamaguch 	case IXL_RX_DESC_PTYPE_UDPV6:
   3240  1.14  yamaguch 		flags_mask = M_CSUM_UDPv6 | M_CSUM_TCP_UDP_BAD;
   3241  1.14  yamaguch 		break;
   3242  1.14  yamaguch 	default:
   3243  1.14  yamaguch 		flags_mask = 0;
   3244  1.14  yamaguch 	}
   3245  1.14  yamaguch 
   3246  1.14  yamaguch 	m->m_pkthdr.csum_flags |= (flags_mask & (M_CSUM_IPv4 |
   3247  1.14  yamaguch 	    M_CSUM_TCPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv4 | M_CSUM_UDPv6));
   3248  1.14  yamaguch 
   3249  1.14  yamaguch 	if (ISSET(qword, IXL_RX_DESC_IPE)) {
   3250  1.14  yamaguch 		m->m_pkthdr.csum_flags |= (flags_mask & M_CSUM_IPv4_BAD);
   3251  1.14  yamaguch 	}
   3252  1.14  yamaguch 
   3253  1.14  yamaguch 	if (ISSET(qword, IXL_RX_DESC_L4E)) {
   3254  1.14  yamaguch 		m->m_pkthdr.csum_flags |= (flags_mask & M_CSUM_TCP_UDP_BAD);
   3255  1.14  yamaguch 	}
   3256  1.14  yamaguch }
   3257  1.14  yamaguch 
   3258   1.1  yamaguch static int
   3259   1.1  yamaguch ixl_rxeof(struct ixl_softc *sc, struct ixl_rx_ring *rxr, u_int rxlimit)
   3260   1.1  yamaguch {
   3261   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3262  1.11  yamaguch 	struct ixl_rx_wb_desc_32 *ring, *rxd;
   3263   1.1  yamaguch 	struct ixl_rx_map *rxm;
   3264   1.1  yamaguch 	bus_dmamap_t map;
   3265   1.1  yamaguch 	unsigned int cons, prod;
   3266   1.1  yamaguch 	struct mbuf *m;
   3267  1.11  yamaguch 	uint64_t word, word0;
   3268   1.1  yamaguch 	unsigned int len;
   3269   1.1  yamaguch 	unsigned int mask;
   3270   1.1  yamaguch 	int done = 0, more = 0;
   3271   1.1  yamaguch 
   3272   1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   3273   1.1  yamaguch 
   3274   1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   3275   1.1  yamaguch 		return 0;
   3276   1.1  yamaguch 
   3277   1.1  yamaguch 	prod = rxr->rxr_prod;
   3278   1.1  yamaguch 	cons = rxr->rxr_cons;
   3279   1.1  yamaguch 
   3280   1.1  yamaguch 	if (cons == prod)
   3281   1.1  yamaguch 		return 0;
   3282   1.1  yamaguch 
   3283   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
   3284   1.1  yamaguch 	    0, IXL_DMA_LEN(&rxr->rxr_mem),
   3285   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3286   1.1  yamaguch 
   3287   1.1  yamaguch 	ring = IXL_DMA_KVA(&rxr->rxr_mem);
   3288   1.1  yamaguch 	mask = sc->sc_rx_ring_ndescs - 1;
   3289   1.1  yamaguch 
   3290  1.35   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   3291  1.35   thorpej 
   3292   1.1  yamaguch 	do {
   3293   1.1  yamaguch 		if (rxlimit-- <= 0) {
   3294   1.1  yamaguch 			more = 1;
   3295   1.1  yamaguch 			break;
   3296   1.1  yamaguch 		}
   3297   1.1  yamaguch 
   3298   1.1  yamaguch 		rxd = &ring[cons];
   3299   1.1  yamaguch 
   3300   1.1  yamaguch 		word = le64toh(rxd->qword1);
   3301   1.1  yamaguch 
   3302   1.1  yamaguch 		if (!ISSET(word, IXL_RX_DESC_DD))
   3303   1.1  yamaguch 			break;
   3304   1.1  yamaguch 
   3305   1.1  yamaguch 		rxm = &rxr->rxr_maps[cons];
   3306   1.1  yamaguch 
   3307   1.1  yamaguch 		map = rxm->rxm_map;
   3308   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   3309   1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   3310   1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   3311   1.1  yamaguch 
   3312   1.1  yamaguch 		m = rxm->rxm_m;
   3313   1.1  yamaguch 		rxm->rxm_m = NULL;
   3314   1.1  yamaguch 
   3315   1.1  yamaguch 		KASSERT(m != NULL);
   3316   1.1  yamaguch 
   3317   1.1  yamaguch 		len = (word & IXL_RX_DESC_PLEN_MASK) >> IXL_RX_DESC_PLEN_SHIFT;
   3318   1.1  yamaguch 		m->m_len = len;
   3319   1.1  yamaguch 		m->m_pkthdr.len = 0;
   3320   1.1  yamaguch 
   3321   1.1  yamaguch 		m->m_next = NULL;
   3322   1.1  yamaguch 		*rxr->rxr_m_tail = m;
   3323   1.1  yamaguch 		rxr->rxr_m_tail = &m->m_next;
   3324   1.1  yamaguch 
   3325   1.1  yamaguch 		m = rxr->rxr_m_head;
   3326   1.1  yamaguch 		m->m_pkthdr.len += len;
   3327   1.1  yamaguch 
   3328   1.1  yamaguch 		if (ISSET(word, IXL_RX_DESC_EOP)) {
   3329  1.11  yamaguch 			word0 = le64toh(rxd->qword0);
   3330  1.11  yamaguch 
   3331  1.11  yamaguch 			if (ISSET(word, IXL_RX_DESC_L2TAG1P)) {
   3332  1.11  yamaguch 				vlan_set_tag(m,
   3333  1.11  yamaguch 				    __SHIFTOUT(word0, IXL_RX_DESC_L2TAG1_MASK));
   3334  1.11  yamaguch 			}
   3335  1.11  yamaguch 
   3336  1.14  yamaguch 			if ((ifp->if_capenable & IXL_IFCAP_RXCSUM) != 0)
   3337  1.14  yamaguch 				ixl_rx_csum(m, word);
   3338  1.14  yamaguch 
   3339   1.1  yamaguch 			if (!ISSET(word,
   3340   1.1  yamaguch 			    IXL_RX_DESC_RXE | IXL_RX_DESC_OVERSIZE)) {
   3341   1.1  yamaguch 				m_set_rcvif(m, ifp);
   3342  1.35   thorpej 				if_statinc_ref(nsr, if_ipackets);
   3343  1.35   thorpej 				if_statadd_ref(nsr, if_ibytes,
   3344  1.35   thorpej 				    m->m_pkthdr.len);
   3345  1.71  yamaguch 				if_percpuq_enqueue(sc->sc_ipq, m);
   3346   1.1  yamaguch 			} else {
   3347  1.35   thorpej 				if_statinc_ref(nsr, if_ierrors);
   3348   1.1  yamaguch 				m_freem(m);
   3349   1.1  yamaguch 			}
   3350   1.1  yamaguch 
   3351   1.1  yamaguch 			rxr->rxr_m_head = NULL;
   3352   1.1  yamaguch 			rxr->rxr_m_tail = &rxr->rxr_m_head;
   3353   1.1  yamaguch 		}
   3354   1.1  yamaguch 
   3355   1.1  yamaguch 		cons++;
   3356   1.1  yamaguch 		cons &= mask;
   3357   1.1  yamaguch 
   3358   1.1  yamaguch 		done = 1;
   3359   1.1  yamaguch 	} while (cons != prod);
   3360   1.1  yamaguch 
   3361   1.1  yamaguch 	if (done) {
   3362   1.1  yamaguch 		rxr->rxr_cons = cons;
   3363   1.1  yamaguch 		if (ixl_rxfill(sc, rxr) == -1)
   3364  1.35   thorpej 			if_statinc_ref(nsr, if_iqdrops);
   3365   1.1  yamaguch 	}
   3366   1.1  yamaguch 
   3367  1.35   thorpej 	IF_STAT_PUTREF(ifp);
   3368  1.35   thorpej 
   3369   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
   3370   1.1  yamaguch 	    0, IXL_DMA_LEN(&rxr->rxr_mem),
   3371   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3372   1.1  yamaguch 
   3373   1.1  yamaguch 	return more;
   3374   1.1  yamaguch }
   3375   1.1  yamaguch 
   3376   1.1  yamaguch static int
   3377   1.1  yamaguch ixl_rxfill(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   3378   1.1  yamaguch {
   3379  1.11  yamaguch 	struct ixl_rx_rd_desc_32 *ring, *rxd;
   3380   1.1  yamaguch 	struct ixl_rx_map *rxm;
   3381   1.1  yamaguch 	bus_dmamap_t map;
   3382   1.1  yamaguch 	struct mbuf *m;
   3383   1.1  yamaguch 	unsigned int prod;
   3384   1.1  yamaguch 	unsigned int slots;
   3385   1.1  yamaguch 	unsigned int mask;
   3386   1.1  yamaguch 	int post = 0, error = 0;
   3387   1.1  yamaguch 
   3388   1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   3389   1.1  yamaguch 
   3390   1.1  yamaguch 	prod = rxr->rxr_prod;
   3391   1.1  yamaguch 	slots = ixl_rxr_unrefreshed(rxr->rxr_prod, rxr->rxr_cons,
   3392   1.1  yamaguch 	    sc->sc_rx_ring_ndescs);
   3393   1.1  yamaguch 
   3394   1.1  yamaguch 	ring = IXL_DMA_KVA(&rxr->rxr_mem);
   3395   1.1  yamaguch 	mask = sc->sc_rx_ring_ndescs - 1;
   3396   1.1  yamaguch 
   3397   1.1  yamaguch 	if (__predict_false(slots <= 0))
   3398   1.1  yamaguch 		return -1;
   3399   1.1  yamaguch 
   3400   1.1  yamaguch 	do {
   3401   1.1  yamaguch 		rxm = &rxr->rxr_maps[prod];
   3402   1.1  yamaguch 
   3403   1.1  yamaguch 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   3404   1.1  yamaguch 		if (m == NULL) {
   3405   1.1  yamaguch 			rxr->rxr_mgethdr_failed.ev_count++;
   3406   1.1  yamaguch 			error = -1;
   3407   1.1  yamaguch 			break;
   3408   1.1  yamaguch 		}
   3409   1.1  yamaguch 
   3410   1.1  yamaguch 		MCLGET(m, M_DONTWAIT);
   3411   1.1  yamaguch 		if (!ISSET(m->m_flags, M_EXT)) {
   3412   1.1  yamaguch 			rxr->rxr_mgetcl_failed.ev_count++;
   3413   1.1  yamaguch 			error = -1;
   3414   1.1  yamaguch 			m_freem(m);
   3415   1.1  yamaguch 			break;
   3416   1.1  yamaguch 		}
   3417   1.1  yamaguch 
   3418  1.29  yamaguch 		m->m_len = m->m_pkthdr.len = MCLBYTES;
   3419   1.1  yamaguch 		m_adj(m, ETHER_ALIGN);
   3420   1.1  yamaguch 
   3421   1.1  yamaguch 		map = rxm->rxm_map;
   3422   1.1  yamaguch 
   3423   1.1  yamaguch 		if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   3424   1.1  yamaguch 		    BUS_DMA_READ | BUS_DMA_NOWAIT) != 0) {
   3425   1.1  yamaguch 			rxr->rxr_mbuf_load_failed.ev_count++;
   3426   1.1  yamaguch 			error = -1;
   3427   1.1  yamaguch 			m_freem(m);
   3428   1.1  yamaguch 			break;
   3429   1.1  yamaguch 		}
   3430   1.1  yamaguch 
   3431   1.1  yamaguch 		rxm->rxm_m = m;
   3432   1.1  yamaguch 
   3433   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   3434   1.1  yamaguch 		    BUS_DMASYNC_PREREAD);
   3435   1.1  yamaguch 
   3436   1.1  yamaguch 		rxd = &ring[prod];
   3437   1.1  yamaguch 
   3438   1.1  yamaguch 		rxd->paddr = htole64(map->dm_segs[0].ds_addr);
   3439   1.1  yamaguch 		rxd->haddr = htole64(0);
   3440   1.1  yamaguch 
   3441   1.1  yamaguch 		prod++;
   3442   1.1  yamaguch 		prod &= mask;
   3443   1.1  yamaguch 
   3444   1.1  yamaguch 		post = 1;
   3445   1.1  yamaguch 
   3446   1.1  yamaguch 	} while (--slots);
   3447   1.1  yamaguch 
   3448   1.1  yamaguch 	if (post) {
   3449   1.1  yamaguch 		rxr->rxr_prod = prod;
   3450   1.1  yamaguch 		ixl_wr(sc, rxr->rxr_tail, prod);
   3451   1.1  yamaguch 	}
   3452   1.1  yamaguch 
   3453   1.1  yamaguch 	return error;
   3454   1.1  yamaguch }
   3455   1.1  yamaguch 
   3456   1.1  yamaguch static inline int
   3457   1.1  yamaguch ixl_handle_queue_common(struct ixl_softc *sc, struct ixl_queue_pair *qp,
   3458   1.1  yamaguch     u_int txlimit, struct evcnt *txevcnt,
   3459   1.1  yamaguch     u_int rxlimit, struct evcnt *rxevcnt)
   3460   1.1  yamaguch {
   3461   1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   3462   1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   3463   1.1  yamaguch 	int txmore, rxmore;
   3464   1.1  yamaguch 	int rv;
   3465   1.1  yamaguch 
   3466   1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   3467   1.1  yamaguch 	txevcnt->ev_count++;
   3468   1.1  yamaguch 	txmore = ixl_txeof(sc, txr, txlimit);
   3469   1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   3470   1.1  yamaguch 
   3471   1.1  yamaguch 	mutex_enter(&rxr->rxr_lock);
   3472   1.1  yamaguch 	rxevcnt->ev_count++;
   3473   1.1  yamaguch 	rxmore = ixl_rxeof(sc, rxr, rxlimit);
   3474   1.1  yamaguch 	mutex_exit(&rxr->rxr_lock);
   3475   1.1  yamaguch 
   3476   1.1  yamaguch 	rv = txmore | (rxmore << 1);
   3477   1.1  yamaguch 
   3478   1.1  yamaguch 	return rv;
   3479   1.1  yamaguch }
   3480   1.1  yamaguch 
   3481   1.1  yamaguch static void
   3482   1.1  yamaguch ixl_sched_handle_queue(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   3483   1.1  yamaguch {
   3484   1.1  yamaguch 
   3485   1.1  yamaguch 	if (qp->qp_workqueue)
   3486  1.45  yamaguch 		workqueue_enqueue(sc->sc_workq_txrx, &qp->qp_work, NULL);
   3487   1.1  yamaguch 	else
   3488   1.1  yamaguch 		softint_schedule(qp->qp_si);
   3489   1.1  yamaguch }
   3490   1.1  yamaguch 
   3491   1.1  yamaguch static int
   3492   1.1  yamaguch ixl_intr(void *xsc)
   3493   1.1  yamaguch {
   3494   1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3495   1.1  yamaguch 	struct ixl_tx_ring *txr;
   3496   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   3497   1.1  yamaguch 	uint32_t icr, rxintr, txintr;
   3498   1.1  yamaguch 	int rv = 0;
   3499   1.1  yamaguch 	unsigned int i;
   3500   1.1  yamaguch 
   3501   1.1  yamaguch 	KASSERT(sc != NULL);
   3502   1.1  yamaguch 
   3503   1.1  yamaguch 	ixl_enable_other_intr(sc);
   3504   1.1  yamaguch 	icr = ixl_rd(sc, I40E_PFINT_ICR0);
   3505   1.1  yamaguch 
   3506   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ADMINQ_MASK)) {
   3507   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_atq.ev_count);
   3508   1.1  yamaguch 		ixl_atq_done(sc);
   3509   1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_arq_task);
   3510   1.1  yamaguch 		rv = 1;
   3511   1.1  yamaguch 	}
   3512   1.1  yamaguch 
   3513   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_LINK_STAT_CHANGE_MASK)) {
   3514   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_link.ev_count);
   3515   1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_link_state_task);
   3516   1.1  yamaguch 		rv = 1;
   3517   1.1  yamaguch 	}
   3518   1.1  yamaguch 
   3519   1.1  yamaguch 	rxintr = icr & I40E_INTR_NOTX_RX_MASK;
   3520   1.1  yamaguch 	txintr = icr & I40E_INTR_NOTX_TX_MASK;
   3521   1.1  yamaguch 
   3522   1.1  yamaguch 	if (txintr || rxintr) {
   3523   1.1  yamaguch 		for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   3524   1.1  yamaguch 			txr = sc->sc_qps[i].qp_txr;
   3525   1.1  yamaguch 			rxr = sc->sc_qps[i].qp_rxr;
   3526   1.1  yamaguch 
   3527   1.1  yamaguch 			ixl_handle_queue_common(sc, &sc->sc_qps[i],
   3528   1.1  yamaguch 			    IXL_TXRX_PROCESS_UNLIMIT, &txr->txr_intr,
   3529   1.1  yamaguch 			    IXL_TXRX_PROCESS_UNLIMIT, &rxr->rxr_intr);
   3530   1.1  yamaguch 		}
   3531   1.1  yamaguch 		rv = 1;
   3532   1.1  yamaguch 	}
   3533   1.1  yamaguch 
   3534   1.1  yamaguch 	return rv;
   3535   1.1  yamaguch }
   3536   1.1  yamaguch 
   3537   1.1  yamaguch static int
   3538   1.1  yamaguch ixl_queue_intr(void *xqp)
   3539   1.1  yamaguch {
   3540   1.1  yamaguch 	struct ixl_queue_pair *qp = xqp;
   3541   1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   3542   1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   3543   1.1  yamaguch 	struct ixl_softc *sc = qp->qp_sc;
   3544   1.1  yamaguch 	u_int txlimit, rxlimit;
   3545   1.1  yamaguch 	int more;
   3546   1.1  yamaguch 
   3547   1.1  yamaguch 	txlimit = sc->sc_tx_intr_process_limit;
   3548   1.1  yamaguch 	rxlimit = sc->sc_rx_intr_process_limit;
   3549   1.1  yamaguch 	qp->qp_workqueue = sc->sc_txrx_workqueue;
   3550   1.1  yamaguch 
   3551   1.1  yamaguch 	more = ixl_handle_queue_common(sc, qp,
   3552   1.1  yamaguch 	    txlimit, &txr->txr_intr, rxlimit, &rxr->rxr_intr);
   3553   1.1  yamaguch 
   3554   1.1  yamaguch 	if (more != 0) {
   3555   1.1  yamaguch 		ixl_sched_handle_queue(sc, qp);
   3556   1.1  yamaguch 	} else {
   3557   1.1  yamaguch 		/* for ALTQ */
   3558   1.1  yamaguch 		if (txr->txr_qid == 0)
   3559   1.1  yamaguch 			if_schedule_deferred_start(&sc->sc_ec.ec_if);
   3560   1.1  yamaguch 		softint_schedule(txr->txr_si);
   3561   1.1  yamaguch 
   3562   1.1  yamaguch 		ixl_enable_queue_intr(sc, qp);
   3563   1.1  yamaguch 	}
   3564   1.1  yamaguch 
   3565   1.1  yamaguch 	return 1;
   3566   1.1  yamaguch }
   3567   1.1  yamaguch 
   3568   1.1  yamaguch static void
   3569  1.45  yamaguch ixl_handle_queue_wk(struct work *wk, void *xsc)
   3570  1.45  yamaguch {
   3571  1.45  yamaguch 	struct ixl_queue_pair *qp;
   3572  1.45  yamaguch 
   3573  1.45  yamaguch 	qp = container_of(wk, struct ixl_queue_pair, qp_work);
   3574  1.45  yamaguch 	ixl_handle_queue(qp);
   3575  1.45  yamaguch }
   3576  1.45  yamaguch 
   3577  1.45  yamaguch static void
   3578   1.1  yamaguch ixl_handle_queue(void *xqp)
   3579   1.1  yamaguch {
   3580   1.1  yamaguch 	struct ixl_queue_pair *qp = xqp;
   3581   1.1  yamaguch 	struct ixl_softc *sc = qp->qp_sc;
   3582   1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   3583   1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   3584   1.1  yamaguch 	u_int txlimit, rxlimit;
   3585   1.1  yamaguch 	int more;
   3586   1.1  yamaguch 
   3587   1.1  yamaguch 	txlimit = sc->sc_tx_process_limit;
   3588   1.1  yamaguch 	rxlimit = sc->sc_rx_process_limit;
   3589   1.1  yamaguch 
   3590   1.1  yamaguch 	more = ixl_handle_queue_common(sc, qp,
   3591   1.1  yamaguch 	    txlimit, &txr->txr_defer, rxlimit, &rxr->rxr_defer);
   3592   1.1  yamaguch 
   3593   1.1  yamaguch 	if (more != 0)
   3594   1.1  yamaguch 		ixl_sched_handle_queue(sc, qp);
   3595   1.1  yamaguch 	else
   3596   1.1  yamaguch 		ixl_enable_queue_intr(sc, qp);
   3597   1.1  yamaguch }
   3598   1.1  yamaguch 
   3599   1.1  yamaguch static inline void
   3600   1.1  yamaguch ixl_print_hmc_error(struct ixl_softc *sc, uint32_t reg)
   3601   1.1  yamaguch {
   3602   1.1  yamaguch 	uint32_t hmc_idx, hmc_isvf;
   3603   1.1  yamaguch 	uint32_t hmc_errtype, hmc_objtype, hmc_data;
   3604   1.1  yamaguch 
   3605   1.1  yamaguch 	hmc_idx = reg & I40E_PFHMC_ERRORINFO_PMF_INDEX_MASK;
   3606   1.1  yamaguch 	hmc_idx = hmc_idx >> I40E_PFHMC_ERRORINFO_PMF_INDEX_SHIFT;
   3607   1.1  yamaguch 	hmc_isvf = reg & I40E_PFHMC_ERRORINFO_PMF_ISVF_MASK;
   3608   1.1  yamaguch 	hmc_isvf = hmc_isvf >> I40E_PFHMC_ERRORINFO_PMF_ISVF_SHIFT;
   3609   1.1  yamaguch 	hmc_errtype = reg & I40E_PFHMC_ERRORINFO_HMC_ERROR_TYPE_MASK;
   3610   1.1  yamaguch 	hmc_errtype = hmc_errtype >> I40E_PFHMC_ERRORINFO_HMC_ERROR_TYPE_SHIFT;
   3611   1.1  yamaguch 	hmc_objtype = reg & I40E_PFHMC_ERRORINFO_HMC_OBJECT_TYPE_MASK;
   3612   1.1  yamaguch 	hmc_objtype = hmc_objtype >> I40E_PFHMC_ERRORINFO_HMC_OBJECT_TYPE_SHIFT;
   3613   1.1  yamaguch 	hmc_data = ixl_rd(sc, I40E_PFHMC_ERRORDATA);
   3614   1.1  yamaguch 
   3615   1.1  yamaguch 	device_printf(sc->sc_dev,
   3616   1.1  yamaguch 	    "HMC Error (idx=0x%x, isvf=0x%x, err=0x%x, obj=0x%x, data=0x%x)\n",
   3617   1.1  yamaguch 	    hmc_idx, hmc_isvf, hmc_errtype, hmc_objtype, hmc_data);
   3618   1.1  yamaguch }
   3619   1.1  yamaguch 
   3620   1.1  yamaguch static int
   3621   1.1  yamaguch ixl_other_intr(void *xsc)
   3622   1.1  yamaguch {
   3623   1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3624   1.1  yamaguch 	uint32_t icr, mask, reg;
   3625   1.1  yamaguch 	int rv;
   3626   1.1  yamaguch 
   3627   1.1  yamaguch 	icr = ixl_rd(sc, I40E_PFINT_ICR0);
   3628   1.1  yamaguch 	mask = ixl_rd(sc, I40E_PFINT_ICR0_ENA);
   3629   1.1  yamaguch 
   3630   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ADMINQ_MASK)) {
   3631   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_atq.ev_count);
   3632   1.1  yamaguch 		ixl_atq_done(sc);
   3633   1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_arq_task);
   3634   1.1  yamaguch 		rv = 1;
   3635   1.1  yamaguch 	}
   3636   1.1  yamaguch 
   3637   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_LINK_STAT_CHANGE_MASK)) {
   3638   1.1  yamaguch 		if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3639   1.1  yamaguch 			device_printf(sc->sc_dev, "link stat changed\n");
   3640   1.1  yamaguch 
   3641   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_link.ev_count);
   3642   1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_link_state_task);
   3643   1.1  yamaguch 		rv = 1;
   3644   1.1  yamaguch 	}
   3645   1.1  yamaguch 
   3646   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_GRST_MASK)) {
   3647   1.1  yamaguch 		CLR(mask, I40E_PFINT_ICR0_ENA_GRST_MASK);
   3648   1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLGEN_RSTAT);
   3649   1.1  yamaguch 		reg = reg & I40E_GLGEN_RSTAT_RESET_TYPE_MASK;
   3650   1.1  yamaguch 		reg = reg >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
   3651   1.1  yamaguch 
   3652   1.1  yamaguch 		device_printf(sc->sc_dev, "GRST: %s\n",
   3653   1.1  yamaguch 		    reg == I40E_RESET_CORER ? "CORER" :
   3654   1.1  yamaguch 		    reg == I40E_RESET_GLOBR ? "GLOBR" :
   3655   1.1  yamaguch 		    reg == I40E_RESET_EMPR ? "EMPR" :
   3656   1.1  yamaguch 		    "POR");
   3657   1.1  yamaguch 	}
   3658   1.1  yamaguch 
   3659   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ECC_ERR_MASK))
   3660   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_ecc_err.ev_count);
   3661   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_PCI_EXCEPTION_MASK))
   3662   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_pci_exception.ev_count);
   3663   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_PE_CRITERR_MASK))
   3664   1.1  yamaguch 		atomic_inc_64(&sc->sc_event_crit_err.ev_count);
   3665   1.1  yamaguch 
   3666   1.1  yamaguch 	if (ISSET(icr, IXL_ICR0_CRIT_ERR_MASK)) {
   3667   1.1  yamaguch 		CLR(mask, IXL_ICR0_CRIT_ERR_MASK);
   3668   1.1  yamaguch 		device_printf(sc->sc_dev, "critical error\n");
   3669   1.1  yamaguch 	}
   3670   1.1  yamaguch 
   3671   1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_HMC_ERR_MASK)) {
   3672   1.1  yamaguch 		reg = ixl_rd(sc, I40E_PFHMC_ERRORINFO);
   3673   1.1  yamaguch 		if (ISSET(reg, I40E_PFHMC_ERRORINFO_ERROR_DETECTED_MASK))
   3674   1.1  yamaguch 			ixl_print_hmc_error(sc, reg);
   3675   1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_ERRORINFO, 0);
   3676   1.1  yamaguch 	}
   3677   1.1  yamaguch 
   3678   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, mask);
   3679   1.1  yamaguch 	ixl_flush(sc);
   3680   1.1  yamaguch 	ixl_enable_other_intr(sc);
   3681   1.1  yamaguch 	return rv;
   3682   1.1  yamaguch }
   3683   1.1  yamaguch 
   3684   1.1  yamaguch static void
   3685   1.4  yamaguch ixl_get_link_status_done(struct ixl_softc *sc,
   3686   1.4  yamaguch     const struct ixl_aq_desc *iaq)
   3687   1.1  yamaguch {
   3688  1.62  yamaguch 	struct ixl_aq_desc iaq_buf;
   3689   1.1  yamaguch 
   3690  1.62  yamaguch 	memcpy(&iaq_buf, iaq, sizeof(iaq_buf));
   3691  1.62  yamaguch 
   3692  1.62  yamaguch 	/*
   3693  1.62  yamaguch 	 * The lock can be released here
   3694  1.62  yamaguch 	 * because there is no post processing about ATQ
   3695  1.62  yamaguch 	 */
   3696  1.62  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3697  1.62  yamaguch 	ixl_link_state_update(sc, &iaq_buf);
   3698  1.62  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3699   1.1  yamaguch }
   3700   1.1  yamaguch 
   3701   1.1  yamaguch static void
   3702   1.4  yamaguch ixl_get_link_status(void *xsc)
   3703   1.1  yamaguch {
   3704   1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3705   1.1  yamaguch 	struct ixl_aq_desc *iaq;
   3706   1.1  yamaguch 	struct ixl_aq_link_param *param;
   3707  1.59  yamaguch 	int error;
   3708   1.1  yamaguch 
   3709  1.57  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3710  1.57  yamaguch 
   3711   1.1  yamaguch 	iaq = &sc->sc_link_state_atq.iatq_desc;
   3712  1.57  yamaguch 	memset(iaq, 0, sizeof(*iaq));
   3713   1.1  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_PHY_LINK_STATUS);
   3714   1.1  yamaguch 	param = (struct ixl_aq_link_param *)iaq->iaq_param;
   3715   1.1  yamaguch 	param->notify = IXL_AQ_LINK_NOTIFY;
   3716   1.1  yamaguch 
   3717  1.59  yamaguch 	error = ixl_atq_exec_locked(sc, &sc->sc_link_state_atq);
   3718  1.62  yamaguch 	ixl_atq_set(&sc->sc_link_state_atq, ixl_get_link_status_done);
   3719  1.62  yamaguch 
   3720  1.59  yamaguch 	if (error == 0) {
   3721  1.59  yamaguch 		ixl_get_link_status_done(sc, iaq);
   3722  1.59  yamaguch 	}
   3723  1.59  yamaguch 
   3724  1.57  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3725   1.1  yamaguch }
   3726   1.1  yamaguch 
   3727   1.1  yamaguch static void
   3728   1.4  yamaguch ixl_link_state_update(struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   3729   1.1  yamaguch {
   3730   1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3731   1.1  yamaguch 	int link_state;
   3732   1.1  yamaguch 
   3733  1.62  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   3734  1.62  yamaguch 	link_state = ixl_set_link_status_locked(sc, iaq);
   3735  1.62  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   3736   1.1  yamaguch 
   3737   1.1  yamaguch 	if (ifp->if_link_state != link_state)
   3738   1.1  yamaguch 		if_link_state_change(ifp, link_state);
   3739   1.1  yamaguch 
   3740   1.1  yamaguch 	if (link_state != LINK_STATE_DOWN) {
   3741  1.62  yamaguch 		kpreempt_disable();
   3742   1.1  yamaguch 		if_schedule_deferred_start(ifp);
   3743  1.62  yamaguch 		kpreempt_enable();
   3744   1.1  yamaguch 	}
   3745   1.1  yamaguch }
   3746   1.1  yamaguch 
   3747   1.1  yamaguch static void
   3748   1.4  yamaguch ixl_aq_dump(const struct ixl_softc *sc, const struct ixl_aq_desc *iaq,
   3749   1.4  yamaguch     const char *msg)
   3750   1.1  yamaguch {
   3751   1.1  yamaguch 	char	 buf[512];
   3752   1.1  yamaguch 	size_t	 len;
   3753   1.1  yamaguch 
   3754   1.1  yamaguch 	len = sizeof(buf);
   3755   1.1  yamaguch 	buf[--len] = '\0';
   3756   1.1  yamaguch 
   3757   1.4  yamaguch 	device_printf(sc->sc_dev, "%s\n", msg);
   3758   1.1  yamaguch 	snprintb(buf, len, IXL_AQ_FLAGS_FMT, le16toh(iaq->iaq_flags));
   3759   1.1  yamaguch 	device_printf(sc->sc_dev, "flags %s opcode %04x\n",
   3760   1.1  yamaguch 	    buf, le16toh(iaq->iaq_opcode));
   3761   1.1  yamaguch 	device_printf(sc->sc_dev, "datalen %u retval %u\n",
   3762   1.1  yamaguch 	    le16toh(iaq->iaq_datalen), le16toh(iaq->iaq_retval));
   3763   1.1  yamaguch 	device_printf(sc->sc_dev, "cookie %016" PRIx64 "\n", iaq->iaq_cookie);
   3764   1.1  yamaguch 	device_printf(sc->sc_dev, "%08x %08x %08x %08x\n",
   3765   1.1  yamaguch 	    le32toh(iaq->iaq_param[0]), le32toh(iaq->iaq_param[1]),
   3766   1.1  yamaguch 	    le32toh(iaq->iaq_param[2]), le32toh(iaq->iaq_param[3]));
   3767   1.1  yamaguch }
   3768   1.1  yamaguch 
   3769   1.1  yamaguch static void
   3770   1.1  yamaguch ixl_arq(void *xsc)
   3771   1.1  yamaguch {
   3772   1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3773   1.1  yamaguch 	struct ixl_aq_desc *arq, *iaq;
   3774   1.1  yamaguch 	struct ixl_aq_buf *aqb;
   3775   1.1  yamaguch 	unsigned int cons = sc->sc_arq_cons;
   3776   1.1  yamaguch 	unsigned int prod;
   3777   1.1  yamaguch 	int done = 0;
   3778   1.1  yamaguch 
   3779   1.1  yamaguch 	prod = ixl_rd(sc, sc->sc_aq_regs->arq_head) &
   3780   1.1  yamaguch 	    sc->sc_aq_regs->arq_head_mask;
   3781   1.1  yamaguch 
   3782   1.1  yamaguch 	if (cons == prod)
   3783   1.1  yamaguch 		goto done;
   3784   1.1  yamaguch 
   3785   1.1  yamaguch 	arq = IXL_DMA_KVA(&sc->sc_arq);
   3786   1.1  yamaguch 
   3787   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   3788   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   3789   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3790   1.1  yamaguch 
   3791   1.1  yamaguch 	do {
   3792   1.1  yamaguch 		iaq = &arq[cons];
   3793   1.1  yamaguch 		aqb = sc->sc_arq_live[cons];
   3794   1.1  yamaguch 
   3795   1.1  yamaguch 		KASSERT(aqb != NULL);
   3796   1.1  yamaguch 
   3797   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, IXL_AQ_BUFLEN,
   3798   1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   3799   1.1  yamaguch 
   3800   1.1  yamaguch 		if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3801   1.4  yamaguch 			ixl_aq_dump(sc, iaq, "arq event");
   3802   1.1  yamaguch 
   3803   1.1  yamaguch 		switch (iaq->iaq_opcode) {
   3804   1.1  yamaguch 		case htole16(IXL_AQ_OP_PHY_LINK_STATUS):
   3805   1.4  yamaguch 			ixl_link_state_update(sc, iaq);
   3806   1.1  yamaguch 			break;
   3807   1.1  yamaguch 		}
   3808   1.1  yamaguch 
   3809   1.1  yamaguch 		memset(iaq, 0, sizeof(*iaq));
   3810   1.1  yamaguch 		sc->sc_arq_live[cons] = NULL;
   3811   1.1  yamaguch 		SIMPLEQ_INSERT_TAIL(&sc->sc_arq_idle, aqb, aqb_entry);
   3812   1.1  yamaguch 
   3813   1.1  yamaguch 		cons++;
   3814   1.1  yamaguch 		cons &= IXL_AQ_MASK;
   3815   1.1  yamaguch 
   3816   1.1  yamaguch 		done = 1;
   3817   1.1  yamaguch 	} while (cons != prod);
   3818   1.1  yamaguch 
   3819   1.1  yamaguch 	if (done) {
   3820   1.1  yamaguch 		sc->sc_arq_cons = cons;
   3821   1.1  yamaguch 		ixl_arq_fill(sc);
   3822   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   3823   1.1  yamaguch 		    0, IXL_DMA_LEN(&sc->sc_arq),
   3824   1.1  yamaguch 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3825   1.1  yamaguch 	}
   3826   1.1  yamaguch 
   3827   1.1  yamaguch done:
   3828   1.1  yamaguch 	ixl_enable_other_intr(sc);
   3829   1.1  yamaguch }
   3830   1.1  yamaguch 
   3831   1.1  yamaguch static void
   3832   1.4  yamaguch ixl_atq_set(struct ixl_atq *iatq,
   3833   1.4  yamaguch     void (*fn)(struct ixl_softc *, const struct ixl_aq_desc *))
   3834   1.1  yamaguch {
   3835   1.4  yamaguch 
   3836   1.1  yamaguch 	iatq->iatq_fn = fn;
   3837   1.1  yamaguch }
   3838   1.1  yamaguch 
   3839   1.1  yamaguch static int
   3840   1.1  yamaguch ixl_atq_post_locked(struct ixl_softc *sc, struct ixl_atq *iatq)
   3841   1.1  yamaguch {
   3842   1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3843   1.1  yamaguch 	unsigned int prod, cons, prod_next;
   3844   1.1  yamaguch 
   3845   1.1  yamaguch 	/* assert locked */
   3846   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3847   1.1  yamaguch 
   3848   1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3849   1.1  yamaguch 	prod = sc->sc_atq_prod;
   3850   1.1  yamaguch 	cons = sc->sc_atq_cons;
   3851   1.1  yamaguch 	prod_next = (prod +1) & IXL_AQ_MASK;
   3852   1.1  yamaguch 
   3853   1.1  yamaguch 	if (cons == prod_next)
   3854   1.1  yamaguch 		return ENOMEM;
   3855   1.1  yamaguch 
   3856   1.1  yamaguch 	slot = &atq[prod];
   3857   1.1  yamaguch 
   3858   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3859   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTWRITE);
   3860   1.1  yamaguch 
   3861  1.58  yamaguch 	KASSERT(iatq->iatq_fn != NULL);
   3862   1.1  yamaguch 	*slot = iatq->iatq_desc;
   3863   1.1  yamaguch 	slot->iaq_cookie = (uint64_t)((intptr_t)iatq);
   3864   1.1  yamaguch 
   3865   1.1  yamaguch 	if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3866   1.4  yamaguch 		ixl_aq_dump(sc, slot, "atq command");
   3867   1.1  yamaguch 
   3868   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3869   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREWRITE);
   3870   1.1  yamaguch 
   3871   1.1  yamaguch 	sc->sc_atq_prod = prod_next;
   3872   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, sc->sc_atq_prod);
   3873   1.1  yamaguch 
   3874   1.1  yamaguch 	return 0;
   3875   1.1  yamaguch }
   3876   1.1  yamaguch 
   3877   1.1  yamaguch static void
   3878   1.1  yamaguch ixl_atq_done_locked(struct ixl_softc *sc)
   3879   1.1  yamaguch {
   3880   1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3881   1.1  yamaguch 	struct ixl_atq *iatq;
   3882   1.1  yamaguch 	unsigned int cons;
   3883   1.1  yamaguch 	unsigned int prod;
   3884   1.1  yamaguch 
   3885   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3886   1.1  yamaguch 
   3887   1.1  yamaguch 	prod = sc->sc_atq_prod;
   3888   1.1  yamaguch 	cons = sc->sc_atq_cons;
   3889   1.1  yamaguch 
   3890   1.1  yamaguch 	if (prod == cons)
   3891   1.1  yamaguch 		return;
   3892   1.1  yamaguch 
   3893   1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3894   1.1  yamaguch 
   3895   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3896   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   3897   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3898   1.1  yamaguch 
   3899   1.1  yamaguch 	do {
   3900   1.1  yamaguch 		slot = &atq[cons];
   3901   1.1  yamaguch 		if (!ISSET(slot->iaq_flags, htole16(IXL_AQ_DD)))
   3902   1.1  yamaguch 			break;
   3903   1.1  yamaguch 
   3904   1.1  yamaguch 		iatq = (struct ixl_atq *)((intptr_t)slot->iaq_cookie);
   3905   1.1  yamaguch 		iatq->iatq_desc = *slot;
   3906   1.1  yamaguch 
   3907   1.1  yamaguch 		memset(slot, 0, sizeof(*slot));
   3908   1.1  yamaguch 
   3909   1.4  yamaguch 		if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3910   1.4  yamaguch 			ixl_aq_dump(sc, &iatq->iatq_desc, "atq response");
   3911   1.4  yamaguch 
   3912   1.4  yamaguch 		(*iatq->iatq_fn)(sc, &iatq->iatq_desc);
   3913   1.1  yamaguch 
   3914   1.1  yamaguch 		cons++;
   3915   1.1  yamaguch 		cons &= IXL_AQ_MASK;
   3916   1.1  yamaguch 	} while (cons != prod);
   3917   1.1  yamaguch 
   3918   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3919   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   3920   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3921   1.1  yamaguch 
   3922   1.1  yamaguch 	sc->sc_atq_cons = cons;
   3923   1.1  yamaguch }
   3924   1.1  yamaguch 
   3925   1.1  yamaguch static void
   3926   1.1  yamaguch ixl_atq_done(struct ixl_softc *sc)
   3927   1.1  yamaguch {
   3928   1.1  yamaguch 
   3929   1.1  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3930   1.1  yamaguch 	ixl_atq_done_locked(sc);
   3931   1.1  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3932   1.1  yamaguch }
   3933   1.1  yamaguch 
   3934   1.1  yamaguch static void
   3935   1.4  yamaguch ixl_wakeup(struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   3936   1.1  yamaguch {
   3937   1.1  yamaguch 
   3938   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3939   1.1  yamaguch 
   3940   1.1  yamaguch 	cv_signal(&sc->sc_atq_cv);
   3941   1.1  yamaguch }
   3942   1.1  yamaguch 
   3943   1.1  yamaguch static int
   3944   1.1  yamaguch ixl_atq_exec(struct ixl_softc *sc, struct ixl_atq *iatq)
   3945   1.1  yamaguch {
   3946   1.1  yamaguch 	int error;
   3947   1.1  yamaguch 
   3948  1.59  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3949  1.59  yamaguch 	error = ixl_atq_exec_locked(sc, iatq);
   3950  1.59  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3951  1.59  yamaguch 
   3952  1.59  yamaguch 	return error;
   3953  1.59  yamaguch }
   3954  1.59  yamaguch 
   3955  1.59  yamaguch static int
   3956  1.59  yamaguch ixl_atq_exec_locked(struct ixl_softc *sc, struct ixl_atq *iatq)
   3957  1.59  yamaguch {
   3958  1.59  yamaguch 	int error;
   3959  1.59  yamaguch 
   3960  1.59  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3961   1.1  yamaguch 	KASSERT(iatq->iatq_desc.iaq_cookie == 0);
   3962   1.1  yamaguch 
   3963   1.1  yamaguch 	ixl_atq_set(iatq, ixl_wakeup);
   3964   1.1  yamaguch 
   3965   1.1  yamaguch 	error = ixl_atq_post_locked(sc, iatq);
   3966  1.59  yamaguch 	if (error)
   3967   1.1  yamaguch 		return error;
   3968   1.1  yamaguch 
   3969   1.1  yamaguch 	error = cv_timedwait(&sc->sc_atq_cv, &sc->sc_atq_lock,
   3970   1.1  yamaguch 	    IXL_ATQ_EXEC_TIMEOUT);
   3971   1.1  yamaguch 
   3972   1.1  yamaguch 	return error;
   3973   1.1  yamaguch }
   3974   1.1  yamaguch 
   3975   1.1  yamaguch static int
   3976   1.1  yamaguch ixl_atq_poll(struct ixl_softc *sc, struct ixl_aq_desc *iaq, unsigned int tm)
   3977   1.1  yamaguch {
   3978   1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3979   1.1  yamaguch 	unsigned int prod;
   3980   1.1  yamaguch 	unsigned int t = 0;
   3981   1.1  yamaguch 
   3982   1.9  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3983   1.9  yamaguch 
   3984   1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3985   1.1  yamaguch 	prod = sc->sc_atq_prod;
   3986   1.1  yamaguch 	slot = atq + prod;
   3987   1.1  yamaguch 
   3988   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3989   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTWRITE);
   3990   1.1  yamaguch 
   3991   1.1  yamaguch 	*slot = *iaq;
   3992   1.1  yamaguch 	slot->iaq_flags |= htole16(IXL_AQ_SI);
   3993   1.1  yamaguch 
   3994   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3995   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREWRITE);
   3996   1.1  yamaguch 
   3997   1.1  yamaguch 	prod++;
   3998   1.1  yamaguch 	prod &= IXL_AQ_MASK;
   3999   1.1  yamaguch 	sc->sc_atq_prod = prod;
   4000   1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, prod);
   4001   1.1  yamaguch 
   4002   1.1  yamaguch 	while (ixl_rd(sc, sc->sc_aq_regs->atq_head) != prod) {
   4003   1.1  yamaguch 		delaymsec(1);
   4004   1.1  yamaguch 
   4005   1.9  yamaguch 		if (t++ > tm) {
   4006   1.9  yamaguch 			mutex_exit(&sc->sc_atq_lock);
   4007   1.1  yamaguch 			return ETIMEDOUT;
   4008   1.9  yamaguch 		}
   4009   1.1  yamaguch 	}
   4010   1.1  yamaguch 
   4011   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   4012   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTREAD);
   4013   1.1  yamaguch 	*iaq = *slot;
   4014   1.1  yamaguch 	memset(slot, 0, sizeof(*slot));
   4015   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   4016   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREREAD);
   4017   1.1  yamaguch 
   4018   1.1  yamaguch 	sc->sc_atq_cons = prod;
   4019   1.1  yamaguch 
   4020   1.9  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   4021   1.9  yamaguch 
   4022   1.1  yamaguch 	return 0;
   4023   1.1  yamaguch }
   4024   1.1  yamaguch 
   4025   1.1  yamaguch static int
   4026   1.1  yamaguch ixl_get_version(struct ixl_softc *sc)
   4027   1.1  yamaguch {
   4028   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4029   1.1  yamaguch 	uint32_t fwbuild, fwver, apiver;
   4030   1.1  yamaguch 	uint16_t api_maj_ver, api_min_ver;
   4031   1.1  yamaguch 
   4032   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4033   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_GET_VERSION);
   4034   1.1  yamaguch 
   4035   1.1  yamaguch 	iaq.iaq_retval = le16toh(23);
   4036   1.1  yamaguch 
   4037   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 2000) != 0)
   4038   1.1  yamaguch 		return ETIMEDOUT;
   4039   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK))
   4040   1.1  yamaguch 		return EIO;
   4041   1.1  yamaguch 
   4042   1.1  yamaguch 	fwbuild = le32toh(iaq.iaq_param[1]);
   4043   1.1  yamaguch 	fwver = le32toh(iaq.iaq_param[2]);
   4044   1.1  yamaguch 	apiver = le32toh(iaq.iaq_param[3]);
   4045   1.1  yamaguch 
   4046   1.1  yamaguch 	api_maj_ver = (uint16_t)apiver;
   4047   1.1  yamaguch 	api_min_ver = (uint16_t)(apiver >> 16);
   4048   1.1  yamaguch 
   4049   1.1  yamaguch 	aprint_normal(", FW %hu.%hu.%05u API %hu.%hu", (uint16_t)fwver,
   4050   1.1  yamaguch 	    (uint16_t)(fwver >> 16), fwbuild, api_maj_ver, api_min_ver);
   4051   1.1  yamaguch 
   4052  1.26  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722) {
   4053  1.26  yamaguch 		SET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_NVMLOCK |
   4054  1.26  yamaguch 		    IXL_SC_AQ_FLAG_NVMREAD);
   4055  1.40  yamaguch 		SET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RXCTL);
   4056  1.41  yamaguch 		SET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RSS);
   4057  1.26  yamaguch 	}
   4058  1.26  yamaguch 
   4059  1.25  yamaguch #define IXL_API_VER(maj, min)	(((uint32_t)(maj) << 16) | (min))
   4060  1.25  yamaguch 	if (IXL_API_VER(api_maj_ver, api_min_ver) >= IXL_API_VER(1, 5)) {
   4061  1.40  yamaguch 		SET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RXCTL);
   4062  1.26  yamaguch 		SET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_NVMLOCK);
   4063   1.1  yamaguch 	}
   4064  1.25  yamaguch #undef IXL_API_VER
   4065   1.1  yamaguch 
   4066   1.1  yamaguch 	return 0;
   4067   1.1  yamaguch }
   4068   1.1  yamaguch 
   4069   1.1  yamaguch static int
   4070  1.26  yamaguch ixl_get_nvm_version(struct ixl_softc *sc)
   4071  1.26  yamaguch {
   4072  1.26  yamaguch 	uint16_t nvmver, cfg_ptr, eetrack_hi, eetrack_lo, oem_hi, oem_lo;
   4073  1.26  yamaguch 	uint32_t eetrack, oem;
   4074  1.26  yamaguch 	uint16_t nvm_maj_ver, nvm_min_ver, oem_build;
   4075  1.26  yamaguch 	uint8_t oem_ver, oem_patch;
   4076  1.26  yamaguch 
   4077  1.26  yamaguch 	nvmver = cfg_ptr = eetrack_hi = eetrack_lo = oem_hi = oem_lo = 0;
   4078  1.26  yamaguch 	ixl_rd16_nvm(sc, I40E_SR_NVM_DEV_STARTER_VERSION, &nvmver);
   4079  1.26  yamaguch 	ixl_rd16_nvm(sc, I40E_SR_NVM_EETRACK_HI, &eetrack_hi);
   4080  1.26  yamaguch 	ixl_rd16_nvm(sc, I40E_SR_NVM_EETRACK_LO, &eetrack_lo);
   4081  1.26  yamaguch 	ixl_rd16_nvm(sc, I40E_SR_BOOT_CONFIG_PTR, &cfg_ptr);
   4082  1.26  yamaguch 	ixl_rd16_nvm(sc, cfg_ptr + I40E_NVM_OEM_VER_OFF, &oem_hi);
   4083  1.26  yamaguch 	ixl_rd16_nvm(sc, cfg_ptr + I40E_NVM_OEM_VER_OFF + 1, &oem_lo);
   4084  1.26  yamaguch 
   4085  1.26  yamaguch 	nvm_maj_ver = (uint16_t)__SHIFTOUT(nvmver, IXL_NVM_VERSION_HI_MASK);
   4086  1.26  yamaguch 	nvm_min_ver = (uint16_t)__SHIFTOUT(nvmver, IXL_NVM_VERSION_LO_MASK);
   4087  1.26  yamaguch 	eetrack = ((uint32_t)eetrack_hi << 16) | eetrack_lo;
   4088  1.26  yamaguch 	oem = ((uint32_t)oem_hi << 16) | oem_lo;
   4089  1.26  yamaguch 	oem_ver = __SHIFTOUT(oem, IXL_NVM_OEMVERSION_MASK);
   4090  1.26  yamaguch 	oem_build = __SHIFTOUT(oem, IXL_NVM_OEMBUILD_MASK);
   4091  1.26  yamaguch 	oem_patch = __SHIFTOUT(oem, IXL_NVM_OEMPATCH_MASK);
   4092  1.26  yamaguch 
   4093  1.26  yamaguch 	aprint_normal(" nvm %x.%02x etid %08x oem %d.%d.%d",
   4094  1.26  yamaguch 	    nvm_maj_ver, nvm_min_ver, eetrack,
   4095  1.26  yamaguch 	    oem_ver, oem_build, oem_patch);
   4096  1.26  yamaguch 
   4097  1.26  yamaguch 	return 0;
   4098  1.26  yamaguch }
   4099  1.26  yamaguch 
   4100  1.26  yamaguch static int
   4101   1.1  yamaguch ixl_pxe_clear(struct ixl_softc *sc)
   4102   1.1  yamaguch {
   4103   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4104   1.1  yamaguch 	int rv;
   4105   1.1  yamaguch 
   4106   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4107   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_CLEAR_PXE_MODE);
   4108   1.1  yamaguch 	iaq.iaq_param[0] = htole32(0x2);
   4109   1.1  yamaguch 
   4110   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4111   1.1  yamaguch 
   4112   1.1  yamaguch 	ixl_wr(sc, I40E_GLLAN_RCTL_0, 0x1);
   4113   1.1  yamaguch 
   4114   1.1  yamaguch 	if (rv != 0)
   4115   1.1  yamaguch 		return ETIMEDOUT;
   4116   1.1  yamaguch 
   4117   1.1  yamaguch 	switch (iaq.iaq_retval) {
   4118   1.1  yamaguch 	case htole16(IXL_AQ_RC_OK):
   4119   1.1  yamaguch 	case htole16(IXL_AQ_RC_EEXIST):
   4120   1.1  yamaguch 		break;
   4121   1.1  yamaguch 	default:
   4122   1.1  yamaguch 		return EIO;
   4123   1.1  yamaguch 	}
   4124   1.1  yamaguch 
   4125   1.1  yamaguch 	return 0;
   4126   1.1  yamaguch }
   4127   1.1  yamaguch 
   4128   1.1  yamaguch static int
   4129   1.1  yamaguch ixl_lldp_shut(struct ixl_softc *sc)
   4130   1.1  yamaguch {
   4131   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4132   1.1  yamaguch 
   4133   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4134   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_LLDP_STOP_AGENT);
   4135   1.1  yamaguch 	iaq.iaq_param[0] = htole32(IXL_LLDP_SHUTDOWN);
   4136   1.1  yamaguch 
   4137   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4138   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "STOP LLDP AGENT timeout\n");
   4139   1.1  yamaguch 		return -1;
   4140   1.1  yamaguch 	}
   4141   1.1  yamaguch 
   4142   1.1  yamaguch 	switch (iaq.iaq_retval) {
   4143   1.1  yamaguch 	case htole16(IXL_AQ_RC_EMODE):
   4144   1.1  yamaguch 	case htole16(IXL_AQ_RC_EPERM):
   4145   1.1  yamaguch 		/* ignore silently */
   4146   1.1  yamaguch 	default:
   4147   1.1  yamaguch 		break;
   4148   1.1  yamaguch 	}
   4149   1.1  yamaguch 
   4150   1.1  yamaguch 	return 0;
   4151   1.1  yamaguch }
   4152   1.1  yamaguch 
   4153   1.1  yamaguch static void
   4154   1.1  yamaguch ixl_parse_hw_capability(struct ixl_softc *sc, struct ixl_aq_capability *cap)
   4155   1.1  yamaguch {
   4156   1.1  yamaguch 	uint16_t id;
   4157   1.1  yamaguch 	uint32_t number, logical_id;
   4158   1.1  yamaguch 
   4159   1.1  yamaguch 	id = le16toh(cap->cap_id);
   4160   1.1  yamaguch 	number = le32toh(cap->number);
   4161   1.1  yamaguch 	logical_id = le32toh(cap->logical_id);
   4162   1.1  yamaguch 
   4163   1.1  yamaguch 	switch (id) {
   4164   1.1  yamaguch 	case IXL_AQ_CAP_RSS:
   4165   1.1  yamaguch 		sc->sc_rss_table_size = number;
   4166   1.1  yamaguch 		sc->sc_rss_table_entry_width = logical_id;
   4167   1.1  yamaguch 		break;
   4168   1.1  yamaguch 	case IXL_AQ_CAP_RXQ:
   4169   1.1  yamaguch 	case IXL_AQ_CAP_TXQ:
   4170   1.1  yamaguch 		sc->sc_nqueue_pairs_device = MIN(number,
   4171   1.1  yamaguch 		    sc->sc_nqueue_pairs_device);
   4172   1.1  yamaguch 		break;
   4173   1.1  yamaguch 	}
   4174   1.1  yamaguch }
   4175   1.1  yamaguch 
   4176   1.1  yamaguch static int
   4177   1.1  yamaguch ixl_get_hw_capabilities(struct ixl_softc *sc)
   4178   1.1  yamaguch {
   4179   1.1  yamaguch 	struct ixl_dmamem idm;
   4180   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4181   1.1  yamaguch 	struct ixl_aq_capability *caps;
   4182   1.1  yamaguch 	size_t i, ncaps;
   4183   1.1  yamaguch 	bus_size_t caps_size;
   4184   1.1  yamaguch 	uint16_t status;
   4185   1.1  yamaguch 	int rv;
   4186   1.1  yamaguch 
   4187   1.1  yamaguch 	caps_size = sizeof(caps[0]) * 40;
   4188   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4189   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_LIST_FUNC_CAP);
   4190   1.1  yamaguch 
   4191   1.1  yamaguch 	do {
   4192   1.1  yamaguch 		if (ixl_dmamem_alloc(sc, &idm, caps_size, 0) != 0) {
   4193   1.1  yamaguch 			return -1;
   4194   1.1  yamaguch 		}
   4195   1.1  yamaguch 
   4196   1.1  yamaguch 		iaq.iaq_flags = htole16(IXL_AQ_BUF |
   4197   1.1  yamaguch 		    (caps_size > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4198   1.1  yamaguch 		iaq.iaq_datalen = htole16(caps_size);
   4199   1.1  yamaguch 		ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   4200   1.1  yamaguch 
   4201   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0,
   4202   1.1  yamaguch 		    IXL_DMA_LEN(&idm), BUS_DMASYNC_PREREAD);
   4203   1.1  yamaguch 
   4204   1.1  yamaguch 		rv = ixl_atq_poll(sc, &iaq, 250);
   4205   1.1  yamaguch 
   4206   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0,
   4207   1.1  yamaguch 		    IXL_DMA_LEN(&idm), BUS_DMASYNC_POSTREAD);
   4208   1.1  yamaguch 
   4209   1.1  yamaguch 		if (rv != 0) {
   4210   1.1  yamaguch 			aprint_error(", HW capabilities timeout\n");
   4211   1.1  yamaguch 			goto done;
   4212   1.1  yamaguch 		}
   4213   1.1  yamaguch 
   4214   1.1  yamaguch 		status = le16toh(iaq.iaq_retval);
   4215   1.1  yamaguch 
   4216   1.1  yamaguch 		if (status == IXL_AQ_RC_ENOMEM) {
   4217   1.1  yamaguch 			caps_size = le16toh(iaq.iaq_datalen);
   4218   1.1  yamaguch 			ixl_dmamem_free(sc, &idm);
   4219   1.1  yamaguch 		}
   4220   1.1  yamaguch 	} while (status == IXL_AQ_RC_ENOMEM);
   4221   1.1  yamaguch 
   4222   1.1  yamaguch 	if (status != IXL_AQ_RC_OK) {
   4223   1.1  yamaguch 		aprint_error(", HW capabilities error\n");
   4224   1.1  yamaguch 		goto done;
   4225   1.1  yamaguch 	}
   4226   1.1  yamaguch 
   4227   1.1  yamaguch 	caps = IXL_DMA_KVA(&idm);
   4228   1.1  yamaguch 	ncaps = le16toh(iaq.iaq_param[1]);
   4229   1.1  yamaguch 
   4230   1.1  yamaguch 	for (i = 0; i < ncaps; i++) {
   4231   1.1  yamaguch 		ixl_parse_hw_capability(sc, &caps[i]);
   4232   1.1  yamaguch 	}
   4233   1.1  yamaguch 
   4234   1.1  yamaguch done:
   4235   1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   4236   1.1  yamaguch 	return rv;
   4237   1.1  yamaguch }
   4238   1.1  yamaguch 
   4239   1.1  yamaguch static int
   4240   1.1  yamaguch ixl_get_mac(struct ixl_softc *sc)
   4241   1.1  yamaguch {
   4242   1.1  yamaguch 	struct ixl_dmamem idm;
   4243   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4244   1.1  yamaguch 	struct ixl_aq_mac_addresses *addrs;
   4245   1.1  yamaguch 	int rv;
   4246   1.1  yamaguch 
   4247   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, sizeof(*addrs), 0) != 0) {
   4248   1.1  yamaguch 		aprint_error(", unable to allocate mac addresses\n");
   4249   1.1  yamaguch 		return -1;
   4250   1.1  yamaguch 	}
   4251   1.1  yamaguch 
   4252   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4253   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF);
   4254   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_MAC_ADDRESS_READ);
   4255   1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*addrs));
   4256   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   4257   1.1  yamaguch 
   4258   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   4259   1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   4260   1.1  yamaguch 
   4261   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4262   1.1  yamaguch 
   4263   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   4264   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   4265   1.1  yamaguch 
   4266   1.1  yamaguch 	if (rv != 0) {
   4267   1.1  yamaguch 		aprint_error(", MAC ADDRESS READ timeout\n");
   4268   1.1  yamaguch 		rv = -1;
   4269   1.1  yamaguch 		goto done;
   4270   1.1  yamaguch 	}
   4271   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4272   1.1  yamaguch 		aprint_error(", MAC ADDRESS READ error\n");
   4273   1.1  yamaguch 		rv = -1;
   4274   1.1  yamaguch 		goto done;
   4275   1.1  yamaguch 	}
   4276   1.1  yamaguch 
   4277   1.1  yamaguch 	addrs = IXL_DMA_KVA(&idm);
   4278   1.1  yamaguch 	if (!ISSET(iaq.iaq_param[0], htole32(IXL_AQ_MAC_PORT_VALID))) {
   4279   1.1  yamaguch 		printf(", port address is not valid\n");
   4280   1.1  yamaguch 		goto done;
   4281   1.1  yamaguch 	}
   4282   1.1  yamaguch 
   4283   1.1  yamaguch 	memcpy(sc->sc_enaddr, addrs->port, ETHER_ADDR_LEN);
   4284   1.1  yamaguch 	rv = 0;
   4285   1.1  yamaguch 
   4286   1.1  yamaguch done:
   4287   1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   4288   1.1  yamaguch 	return rv;
   4289   1.1  yamaguch }
   4290   1.1  yamaguch 
   4291   1.1  yamaguch static int
   4292   1.1  yamaguch ixl_get_switch_config(struct ixl_softc *sc)
   4293   1.1  yamaguch {
   4294   1.1  yamaguch 	struct ixl_dmamem idm;
   4295   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4296   1.1  yamaguch 	struct ixl_aq_switch_config *hdr;
   4297   1.1  yamaguch 	struct ixl_aq_switch_config_element *elms, *elm;
   4298   1.1  yamaguch 	unsigned int nelm, i;
   4299   1.1  yamaguch 	int rv;
   4300   1.1  yamaguch 
   4301   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, IXL_AQ_BUFLEN, 0) != 0) {
   4302   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4303   1.1  yamaguch 		    "unable to allocate switch config buffer\n");
   4304   1.1  yamaguch 		return -1;
   4305   1.1  yamaguch 	}
   4306   1.1  yamaguch 
   4307   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4308   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   4309   1.1  yamaguch 	    (IXL_AQ_BUFLEN > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4310   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_SWITCH_GET_CONFIG);
   4311   1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_AQ_BUFLEN);
   4312   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   4313   1.1  yamaguch 
   4314   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   4315   1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   4316   1.1  yamaguch 
   4317   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4318   1.1  yamaguch 
   4319   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   4320   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   4321   1.1  yamaguch 
   4322   1.1  yamaguch 	if (rv != 0) {
   4323   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET SWITCH CONFIG timeout\n");
   4324   1.1  yamaguch 		rv = -1;
   4325   1.1  yamaguch 		goto done;
   4326   1.1  yamaguch 	}
   4327   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4328   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET SWITCH CONFIG error\n");
   4329   1.1  yamaguch 		rv = -1;
   4330   1.1  yamaguch 		goto done;
   4331   1.1  yamaguch 	}
   4332   1.1  yamaguch 
   4333   1.1  yamaguch 	hdr = IXL_DMA_KVA(&idm);
   4334   1.1  yamaguch 	elms = (struct ixl_aq_switch_config_element *)(hdr + 1);
   4335   1.1  yamaguch 
   4336   1.1  yamaguch 	nelm = le16toh(hdr->num_reported);
   4337   1.1  yamaguch 	if (nelm < 1) {
   4338   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "no switch config available\n");
   4339   1.1  yamaguch 		rv = -1;
   4340   1.1  yamaguch 		goto done;
   4341   1.1  yamaguch 	}
   4342   1.1  yamaguch 
   4343   1.1  yamaguch 	for (i = 0; i < nelm; i++) {
   4344   1.1  yamaguch 		elm = &elms[i];
   4345   1.1  yamaguch 
   4346   1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   4347   1.1  yamaguch 		    "type %x revision %u seid %04x\n",
   4348   1.1  yamaguch 		    elm->type, elm->revision, le16toh(elm->seid));
   4349   1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   4350   1.1  yamaguch 		    "uplink %04x downlink %04x\n",
   4351   1.1  yamaguch 		    le16toh(elm->uplink_seid),
   4352   1.1  yamaguch 		    le16toh(elm->downlink_seid));
   4353   1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   4354   1.1  yamaguch 		    "conntype %x scheduler %04x extra %04x\n",
   4355   1.1  yamaguch 		    elm->connection_type,
   4356   1.1  yamaguch 		    le16toh(elm->scheduler_id),
   4357   1.1  yamaguch 		    le16toh(elm->element_info));
   4358   1.1  yamaguch 	}
   4359   1.1  yamaguch 
   4360   1.1  yamaguch 	elm = &elms[0];
   4361   1.1  yamaguch 
   4362   1.1  yamaguch 	sc->sc_uplink_seid = elm->uplink_seid;
   4363   1.1  yamaguch 	sc->sc_downlink_seid = elm->downlink_seid;
   4364   1.1  yamaguch 	sc->sc_seid = elm->seid;
   4365   1.1  yamaguch 
   4366   1.1  yamaguch 	if ((sc->sc_uplink_seid == htole16(0)) !=
   4367   1.1  yamaguch 	    (sc->sc_downlink_seid == htole16(0))) {
   4368   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SEIDs are misconfigured\n");
   4369   1.1  yamaguch 		rv = -1;
   4370   1.1  yamaguch 		goto done;
   4371   1.1  yamaguch 	}
   4372   1.1  yamaguch 
   4373   1.1  yamaguch done:
   4374   1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   4375   1.1  yamaguch 	return rv;
   4376   1.1  yamaguch }
   4377   1.1  yamaguch 
   4378   1.1  yamaguch static int
   4379   1.1  yamaguch ixl_phy_mask_ints(struct ixl_softc *sc)
   4380   1.1  yamaguch {
   4381   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4382   1.1  yamaguch 
   4383   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4384   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_SET_EVENT_MASK);
   4385   1.1  yamaguch 	iaq.iaq_param[2] = htole32(IXL_AQ_PHY_EV_MASK &
   4386   1.1  yamaguch 	    ~(IXL_AQ_PHY_EV_LINK_UPDOWN | IXL_AQ_PHY_EV_MODULE_QUAL_FAIL |
   4387   1.1  yamaguch 	      IXL_AQ_PHY_EV_MEDIA_NA));
   4388   1.1  yamaguch 
   4389   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4390   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SET PHY EVENT MASK timeout\n");
   4391   1.1  yamaguch 		return -1;
   4392   1.1  yamaguch 	}
   4393   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4394   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SET PHY EVENT MASK error\n");
   4395   1.1  yamaguch 		return -1;
   4396   1.1  yamaguch 	}
   4397   1.1  yamaguch 
   4398   1.1  yamaguch 	return 0;
   4399   1.1  yamaguch }
   4400   1.1  yamaguch 
   4401   1.1  yamaguch static int
   4402  1.72  yamaguch ixl_get_phy_abilities(struct ixl_softc *sc, struct ixl_dmamem *idm)
   4403   1.1  yamaguch {
   4404   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4405   1.1  yamaguch 	int rv;
   4406   1.1  yamaguch 
   4407   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4408   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   4409   1.1  yamaguch 	    (IXL_DMA_LEN(idm) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4410   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_GET_ABILITIES);
   4411   1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(idm));
   4412   1.1  yamaguch 	iaq.iaq_param[0] = htole32(IXL_AQ_PHY_REPORT_INIT);
   4413   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(idm));
   4414   1.1  yamaguch 
   4415   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   4416   1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   4417   1.1  yamaguch 
   4418   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4419   1.1  yamaguch 
   4420   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   4421   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   4422   1.1  yamaguch 
   4423   1.1  yamaguch 	if (rv != 0)
   4424   1.1  yamaguch 		return -1;
   4425   1.1  yamaguch 
   4426   1.1  yamaguch 	return le16toh(iaq.iaq_retval);
   4427   1.1  yamaguch }
   4428   1.1  yamaguch 
   4429   1.1  yamaguch static int
   4430  1.31  yamaguch ixl_get_phy_info(struct ixl_softc *sc)
   4431   1.1  yamaguch {
   4432   1.1  yamaguch 	struct ixl_dmamem idm;
   4433   1.1  yamaguch 	struct ixl_aq_phy_abilities *phy;
   4434   1.1  yamaguch 	int rv;
   4435   1.1  yamaguch 
   4436   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, IXL_AQ_BUFLEN, 0) != 0) {
   4437   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4438  1.39  yamaguch 		    "unable to allocate phy abilities buffer\n");
   4439   1.1  yamaguch 		return -1;
   4440   1.1  yamaguch 	}
   4441   1.1  yamaguch 
   4442   1.1  yamaguch 	rv = ixl_get_phy_abilities(sc, &idm);
   4443   1.1  yamaguch 	switch (rv) {
   4444   1.1  yamaguch 	case -1:
   4445   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET PHY ABILITIES timeout\n");
   4446   1.1  yamaguch 		goto done;
   4447   1.1  yamaguch 	case IXL_AQ_RC_OK:
   4448   1.1  yamaguch 		break;
   4449   1.1  yamaguch 	case IXL_AQ_RC_EIO:
   4450   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,"unable to query phy types\n");
   4451  1.30  yamaguch 		goto done;
   4452   1.1  yamaguch 	default:
   4453   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4454   1.1  yamaguch 		    "GET PHY ABILITIIES error %u\n", rv);
   4455   1.1  yamaguch 		goto done;
   4456   1.1  yamaguch 	}
   4457   1.1  yamaguch 
   4458   1.1  yamaguch 	phy = IXL_DMA_KVA(&idm);
   4459   1.1  yamaguch 
   4460  1.31  yamaguch 	sc->sc_phy_types = le32toh(phy->phy_type);
   4461  1.31  yamaguch 	sc->sc_phy_types |= (uint64_t)le32toh(phy->phy_type_ext) << 32;
   4462   1.1  yamaguch 
   4463  1.31  yamaguch 	sc->sc_phy_abilities = phy->abilities;
   4464  1.31  yamaguch 	sc->sc_phy_linkspeed = phy->link_speed;
   4465  1.31  yamaguch 	sc->sc_phy_fec_cfg = phy->fec_cfg_curr_mod_ext_info &
   4466  1.31  yamaguch 	    (IXL_AQ_ENABLE_FEC_KR | IXL_AQ_ENABLE_FEC_RS |
   4467  1.31  yamaguch 	    IXL_AQ_REQUEST_FEC_KR | IXL_AQ_REQUEST_FEC_RS);
   4468  1.31  yamaguch 	sc->sc_eee_cap = phy->eee_capability;
   4469  1.31  yamaguch 	sc->sc_eeer_val = phy->eeer_val;
   4470  1.31  yamaguch 	sc->sc_d3_lpan = phy->d3_lpan;
   4471   1.1  yamaguch 
   4472   1.1  yamaguch 	rv = 0;
   4473   1.1  yamaguch 
   4474   1.1  yamaguch done:
   4475   1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   4476   1.1  yamaguch 	return rv;
   4477   1.1  yamaguch }
   4478   1.1  yamaguch 
   4479   1.1  yamaguch static int
   4480  1.31  yamaguch ixl_set_phy_config(struct ixl_softc *sc,
   4481  1.31  yamaguch     uint8_t link_speed, uint8_t abilities, bool polling)
   4482  1.31  yamaguch {
   4483  1.31  yamaguch 	struct ixl_aq_phy_param *param;
   4484  1.31  yamaguch 	struct ixl_atq iatq;
   4485  1.31  yamaguch 	struct ixl_aq_desc *iaq;
   4486  1.31  yamaguch 	int error;
   4487  1.31  yamaguch 
   4488  1.31  yamaguch 	memset(&iatq, 0, sizeof(iatq));
   4489  1.31  yamaguch 
   4490  1.31  yamaguch 	iaq = &iatq.iatq_desc;
   4491  1.31  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_PHY_SET_CONFIG);
   4492  1.31  yamaguch 	param = (struct ixl_aq_phy_param *)&iaq->iaq_param;
   4493  1.31  yamaguch 	param->phy_types = htole32((uint32_t)sc->sc_phy_types);
   4494  1.31  yamaguch 	param->phy_type_ext = (uint8_t)(sc->sc_phy_types >> 32);
   4495  1.31  yamaguch 	param->link_speed = link_speed;
   4496  1.31  yamaguch 	param->abilities = abilities | IXL_AQ_PHY_ABILITY_AUTO_LINK;
   4497  1.31  yamaguch 	param->fec_cfg = sc->sc_phy_fec_cfg;
   4498  1.31  yamaguch 	param->eee_capability = sc->sc_eee_cap;
   4499  1.31  yamaguch 	param->eeer_val = sc->sc_eeer_val;
   4500  1.31  yamaguch 	param->d3_lpan = sc->sc_d3_lpan;
   4501  1.31  yamaguch 
   4502  1.31  yamaguch 	if (polling)
   4503  1.31  yamaguch 		error = ixl_atq_poll(sc, iaq, 250);
   4504  1.31  yamaguch 	else
   4505  1.31  yamaguch 		error = ixl_atq_exec(sc, &iatq);
   4506  1.31  yamaguch 
   4507  1.31  yamaguch 	if (error != 0)
   4508  1.31  yamaguch 		return error;
   4509  1.31  yamaguch 
   4510  1.31  yamaguch 	switch (le16toh(iaq->iaq_retval)) {
   4511  1.31  yamaguch 	case IXL_AQ_RC_OK:
   4512  1.31  yamaguch 		break;
   4513  1.31  yamaguch 	case IXL_AQ_RC_EPERM:
   4514  1.31  yamaguch 		return EPERM;
   4515  1.31  yamaguch 	default:
   4516  1.31  yamaguch 		return EIO;
   4517  1.31  yamaguch 	}
   4518  1.31  yamaguch 
   4519  1.31  yamaguch 	return 0;
   4520  1.31  yamaguch }
   4521  1.31  yamaguch 
   4522  1.31  yamaguch static int
   4523  1.31  yamaguch ixl_set_phy_autoselect(struct ixl_softc *sc)
   4524  1.31  yamaguch {
   4525  1.31  yamaguch 	uint8_t link_speed, abilities;
   4526  1.31  yamaguch 
   4527  1.31  yamaguch 	link_speed = sc->sc_phy_linkspeed;
   4528  1.31  yamaguch 	abilities = IXL_PHY_ABILITY_LINKUP | IXL_PHY_ABILITY_AUTONEGO;
   4529  1.31  yamaguch 
   4530  1.31  yamaguch 	return ixl_set_phy_config(sc, link_speed, abilities, true);
   4531  1.31  yamaguch }
   4532  1.31  yamaguch 
   4533  1.31  yamaguch static int
   4534  1.37  yamaguch ixl_get_link_status_poll(struct ixl_softc *sc, int *l)
   4535   1.1  yamaguch {
   4536   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4537   1.1  yamaguch 	struct ixl_aq_link_param *param;
   4538   1.1  yamaguch 	int link;
   4539   1.1  yamaguch 
   4540   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4541   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_LINK_STATUS);
   4542   1.1  yamaguch 	param = (struct ixl_aq_link_param *)iaq.iaq_param;
   4543   1.1  yamaguch 	param->notify = IXL_AQ_LINK_NOTIFY;
   4544   1.1  yamaguch 
   4545   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4546   1.1  yamaguch 		return ETIMEDOUT;
   4547   1.1  yamaguch 	}
   4548   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4549   1.1  yamaguch 		return EIO;
   4550   1.1  yamaguch 	}
   4551   1.1  yamaguch 
   4552  1.62  yamaguch 	/* It is unneccessary to hold lock */
   4553  1.62  yamaguch 	link = ixl_set_link_status_locked(sc, &iaq);
   4554  1.37  yamaguch 
   4555  1.37  yamaguch 	if (l != NULL)
   4556  1.37  yamaguch 		*l = link;
   4557   1.1  yamaguch 
   4558   1.1  yamaguch 	return 0;
   4559   1.1  yamaguch }
   4560   1.1  yamaguch 
   4561   1.1  yamaguch static int
   4562   1.1  yamaguch ixl_get_vsi(struct ixl_softc *sc)
   4563   1.1  yamaguch {
   4564   1.1  yamaguch 	struct ixl_dmamem *vsi = &sc->sc_scratch;
   4565   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4566   1.1  yamaguch 	struct ixl_aq_vsi_param *param;
   4567   1.1  yamaguch 	struct ixl_aq_vsi_reply *reply;
   4568  1.18  yamaguch 	struct ixl_aq_vsi_data *data;
   4569   1.1  yamaguch 	int rv;
   4570   1.1  yamaguch 
   4571   1.1  yamaguch 	/* grumble, vsi info isn't "known" at compile time */
   4572   1.1  yamaguch 
   4573   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4574   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   4575   1.1  yamaguch 	    (IXL_DMA_LEN(vsi) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4576   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_GET_VSI_PARAMS);
   4577   1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(vsi));
   4578   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(vsi));
   4579   1.1  yamaguch 
   4580   1.1  yamaguch 	param = (struct ixl_aq_vsi_param *)iaq.iaq_param;
   4581   1.1  yamaguch 	param->uplink_seid = sc->sc_seid;
   4582   1.1  yamaguch 
   4583   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   4584   1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   4585   1.1  yamaguch 
   4586   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4587   1.1  yamaguch 
   4588   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   4589   1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   4590   1.1  yamaguch 
   4591   1.1  yamaguch 	if (rv != 0) {
   4592  1.11  yamaguch 		return ETIMEDOUT;
   4593   1.1  yamaguch 	}
   4594   1.1  yamaguch 
   4595  1.12  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   4596  1.12  yamaguch 	case IXL_AQ_RC_OK:
   4597  1.12  yamaguch 		break;
   4598  1.12  yamaguch 	case IXL_AQ_RC_ENOENT:
   4599  1.12  yamaguch 		return ENOENT;
   4600  1.12  yamaguch 	case IXL_AQ_RC_EACCES:
   4601  1.12  yamaguch 		return EACCES;
   4602  1.12  yamaguch 	default:
   4603  1.11  yamaguch 		return EIO;
   4604   1.1  yamaguch 	}
   4605   1.1  yamaguch 
   4606   1.1  yamaguch 	reply = (struct ixl_aq_vsi_reply *)iaq.iaq_param;
   4607  1.41  yamaguch 	sc->sc_vsi_number = le16toh(reply->vsi_number);
   4608  1.18  yamaguch 	data = IXL_DMA_KVA(vsi);
   4609  1.18  yamaguch 	sc->sc_vsi_stat_counter_idx = le16toh(data->stat_counter_idx);
   4610   1.1  yamaguch 
   4611   1.1  yamaguch 	return 0;
   4612   1.1  yamaguch }
   4613   1.1  yamaguch 
   4614   1.1  yamaguch static int
   4615   1.1  yamaguch ixl_set_vsi(struct ixl_softc *sc)
   4616   1.1  yamaguch {
   4617   1.1  yamaguch 	struct ixl_dmamem *vsi = &sc->sc_scratch;
   4618   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4619   1.1  yamaguch 	struct ixl_aq_vsi_param *param;
   4620   1.1  yamaguch 	struct ixl_aq_vsi_data *data = IXL_DMA_KVA(vsi);
   4621   1.1  yamaguch 	unsigned int qnum;
   4622  1.11  yamaguch 	uint16_t val;
   4623   1.1  yamaguch 	int rv;
   4624   1.1  yamaguch 
   4625   1.1  yamaguch 	qnum = sc->sc_nqueue_pairs - 1;
   4626   1.1  yamaguch 
   4627   1.1  yamaguch 	data->valid_sections = htole16(IXL_AQ_VSI_VALID_QUEUE_MAP |
   4628   1.1  yamaguch 	    IXL_AQ_VSI_VALID_VLAN);
   4629   1.1  yamaguch 
   4630   1.1  yamaguch 	CLR(data->mapping_flags, htole16(IXL_AQ_VSI_QUE_MAP_MASK));
   4631   1.1  yamaguch 	SET(data->mapping_flags, htole16(IXL_AQ_VSI_QUE_MAP_CONTIG));
   4632   1.1  yamaguch 	data->queue_mapping[0] = htole16(0);
   4633   1.1  yamaguch 	data->tc_mapping[0] = htole16((0 << IXL_AQ_VSI_TC_Q_OFFSET_SHIFT) |
   4634   1.1  yamaguch 	    (qnum << IXL_AQ_VSI_TC_Q_NUMBER_SHIFT));
   4635   1.1  yamaguch 
   4636  1.11  yamaguch 	val = le16toh(data->port_vlan_flags);
   4637  1.11  yamaguch 	CLR(val, IXL_AQ_VSI_PVLAN_MODE_MASK | IXL_AQ_VSI_PVLAN_EMOD_MASK);
   4638  1.11  yamaguch 	SET(val, IXL_AQ_VSI_PVLAN_MODE_ALL);
   4639  1.11  yamaguch 
   4640  1.11  yamaguch 	if (ISSET(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWTAGGING)) {
   4641  1.11  yamaguch 		SET(val, IXL_AQ_VSI_PVLAN_EMOD_STR_BOTH);
   4642  1.11  yamaguch 	} else {
   4643  1.11  yamaguch 		SET(val, IXL_AQ_VSI_PVLAN_EMOD_NOTHING);
   4644  1.11  yamaguch 	}
   4645  1.11  yamaguch 
   4646  1.11  yamaguch 	data->port_vlan_flags = htole16(val);
   4647   1.1  yamaguch 
   4648   1.1  yamaguch 	/* grumble, vsi info isn't "known" at compile time */
   4649   1.1  yamaguch 
   4650   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4651   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD |
   4652   1.1  yamaguch 	    (IXL_DMA_LEN(vsi) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4653   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_UPD_VSI_PARAMS);
   4654   1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(vsi));
   4655   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(vsi));
   4656   1.1  yamaguch 
   4657   1.1  yamaguch 	param = (struct ixl_aq_vsi_param *)iaq.iaq_param;
   4658   1.1  yamaguch 	param->uplink_seid = sc->sc_seid;
   4659   1.1  yamaguch 
   4660   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   4661   1.1  yamaguch 	    BUS_DMASYNC_PREWRITE);
   4662   1.1  yamaguch 
   4663   1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   4664   1.1  yamaguch 
   4665   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   4666   1.1  yamaguch 	    BUS_DMASYNC_POSTWRITE);
   4667   1.1  yamaguch 
   4668   1.1  yamaguch 	if (rv != 0) {
   4669  1.11  yamaguch 		return ETIMEDOUT;
   4670   1.1  yamaguch 	}
   4671   1.1  yamaguch 
   4672  1.12  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   4673  1.12  yamaguch 	case IXL_AQ_RC_OK:
   4674  1.12  yamaguch 		break;
   4675  1.12  yamaguch 	case IXL_AQ_RC_ENOENT:
   4676  1.12  yamaguch 		return ENOENT;
   4677  1.12  yamaguch 	case IXL_AQ_RC_EACCES:
   4678  1.12  yamaguch 		return EACCES;
   4679  1.12  yamaguch 	default:
   4680  1.11  yamaguch 		return EIO;
   4681   1.1  yamaguch 	}
   4682   1.1  yamaguch 
   4683   1.1  yamaguch 	return 0;
   4684   1.1  yamaguch }
   4685   1.1  yamaguch 
   4686   1.1  yamaguch static void
   4687   1.1  yamaguch ixl_set_filter_control(struct ixl_softc *sc)
   4688   1.1  yamaguch {
   4689   1.1  yamaguch 	uint32_t reg;
   4690   1.1  yamaguch 
   4691   1.1  yamaguch 	reg = ixl_rd_rx_csr(sc, I40E_PFQF_CTL_0);
   4692   1.1  yamaguch 
   4693   1.1  yamaguch 	CLR(reg, I40E_PFQF_CTL_0_HASHLUTSIZE_MASK);
   4694   1.1  yamaguch 	SET(reg, I40E_HASH_LUT_SIZE_128 << I40E_PFQF_CTL_0_HASHLUTSIZE_SHIFT);
   4695   1.1  yamaguch 
   4696   1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_FD_ENA_MASK);
   4697   1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_ETYPE_ENA_MASK);
   4698   1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_MACVLAN_ENA_MASK);
   4699   1.1  yamaguch 
   4700   1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_CTL_0, reg);
   4701   1.1  yamaguch }
   4702   1.1  yamaguch 
   4703   1.1  yamaguch static inline void
   4704   1.1  yamaguch ixl_get_default_rss_key(uint32_t *buf, size_t len)
   4705   1.1  yamaguch {
   4706   1.1  yamaguch 	size_t cplen;
   4707   1.1  yamaguch 	uint8_t rss_seed[RSS_KEYSIZE];
   4708   1.1  yamaguch 
   4709   1.1  yamaguch 	rss_getkey(rss_seed);
   4710   1.1  yamaguch 	memset(buf, 0, len);
   4711   1.1  yamaguch 
   4712   1.1  yamaguch 	cplen = MIN(len, sizeof(rss_seed));
   4713   1.1  yamaguch 	memcpy(buf, rss_seed, cplen);
   4714   1.1  yamaguch }
   4715   1.1  yamaguch 
   4716  1.41  yamaguch static int
   4717  1.41  yamaguch ixl_set_rss_key(struct ixl_softc *sc, uint8_t *key, size_t keylen)
   4718  1.41  yamaguch {
   4719  1.41  yamaguch 	struct ixl_dmamem *idm;
   4720  1.41  yamaguch 	struct ixl_atq iatq;
   4721  1.41  yamaguch 	struct ixl_aq_desc *iaq;
   4722  1.41  yamaguch 	struct ixl_aq_rss_key_param *param;
   4723  1.41  yamaguch 	struct ixl_aq_rss_key_data *data;
   4724  1.41  yamaguch 	size_t len, datalen, stdlen, extlen;
   4725  1.41  yamaguch 	uint16_t vsi_id;
   4726  1.41  yamaguch 	int rv;
   4727  1.41  yamaguch 
   4728  1.41  yamaguch 	memset(&iatq, 0, sizeof(iatq));
   4729  1.41  yamaguch 	iaq = &iatq.iatq_desc;
   4730  1.41  yamaguch 	idm = &sc->sc_aqbuf;
   4731  1.41  yamaguch 
   4732  1.41  yamaguch 	datalen = sizeof(*data);
   4733  1.41  yamaguch 
   4734  1.41  yamaguch 	/*XXX The buf size has to be less than the size of the register */
   4735  1.41  yamaguch 	datalen = MIN(IXL_RSS_KEY_SIZE_REG * sizeof(uint32_t), datalen);
   4736  1.41  yamaguch 
   4737  1.41  yamaguch 	iaq->iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD |
   4738  1.41  yamaguch 	    (datalen > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4739  1.41  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_RSS_SET_KEY);
   4740  1.41  yamaguch 	iaq->iaq_datalen = htole16(datalen);
   4741  1.41  yamaguch 
   4742  1.41  yamaguch 	param = (struct ixl_aq_rss_key_param *)iaq->iaq_param;
   4743  1.41  yamaguch 	vsi_id = (sc->sc_vsi_number << IXL_AQ_RSSKEY_VSI_ID_SHIFT) |
   4744  1.41  yamaguch 	    IXL_AQ_RSSKEY_VSI_VALID;
   4745  1.41  yamaguch 	param->vsi_id = htole16(vsi_id);
   4746  1.41  yamaguch 
   4747  1.41  yamaguch 	memset(IXL_DMA_KVA(idm), 0, IXL_DMA_LEN(idm));
   4748  1.41  yamaguch 	data = IXL_DMA_KVA(idm);
   4749  1.41  yamaguch 
   4750  1.41  yamaguch 	len = MIN(keylen, datalen);
   4751  1.41  yamaguch 	stdlen = MIN(sizeof(data->standard_rss_key), len);
   4752  1.41  yamaguch 	memcpy(data->standard_rss_key, key, stdlen);
   4753  1.41  yamaguch 	len = (len > stdlen) ? (len - stdlen) : 0;
   4754  1.41  yamaguch 
   4755  1.41  yamaguch 	extlen = MIN(sizeof(data->extended_hash_key), len);
   4756  1.41  yamaguch 	extlen = (stdlen < keylen) ? 0 : keylen - stdlen;
   4757  1.41  yamaguch 	memcpy(data->extended_hash_key, key + stdlen, extlen);
   4758  1.41  yamaguch 
   4759  1.41  yamaguch 	ixl_aq_dva(iaq, IXL_DMA_DVA(idm));
   4760  1.41  yamaguch 
   4761  1.41  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0,
   4762  1.41  yamaguch 	    IXL_DMA_LEN(idm), BUS_DMASYNC_PREWRITE);
   4763  1.41  yamaguch 
   4764  1.41  yamaguch 	rv = ixl_atq_exec(sc, &iatq);
   4765  1.41  yamaguch 
   4766  1.41  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0,
   4767  1.41  yamaguch 	    IXL_DMA_LEN(idm), BUS_DMASYNC_POSTWRITE);
   4768  1.41  yamaguch 
   4769  1.41  yamaguch 	if (rv != 0) {
   4770  1.41  yamaguch 		return ETIMEDOUT;
   4771  1.41  yamaguch 	}
   4772  1.41  yamaguch 
   4773  1.41  yamaguch 	if (iaq->iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4774  1.41  yamaguch 		return EIO;
   4775  1.41  yamaguch 	}
   4776  1.41  yamaguch 
   4777  1.41  yamaguch 	return 0;
   4778  1.41  yamaguch }
   4779  1.41  yamaguch 
   4780  1.41  yamaguch static int
   4781  1.41  yamaguch ixl_set_rss_lut(struct ixl_softc *sc, uint8_t *lut, size_t lutlen)
   4782  1.41  yamaguch {
   4783  1.41  yamaguch 	struct ixl_dmamem *idm;
   4784  1.41  yamaguch 	struct ixl_atq iatq;
   4785  1.41  yamaguch 	struct ixl_aq_desc *iaq;
   4786  1.41  yamaguch 	struct ixl_aq_rss_lut_param *param;
   4787  1.41  yamaguch 	uint16_t vsi_id;
   4788  1.41  yamaguch 	uint8_t *data;
   4789  1.41  yamaguch 	size_t dmalen;
   4790  1.41  yamaguch 	int rv;
   4791  1.41  yamaguch 
   4792  1.41  yamaguch 	memset(&iatq, 0, sizeof(iatq));
   4793  1.41  yamaguch 	iaq = &iatq.iatq_desc;
   4794  1.41  yamaguch 	idm = &sc->sc_aqbuf;
   4795  1.41  yamaguch 
   4796  1.41  yamaguch 	dmalen = MIN(lutlen, IXL_DMA_LEN(idm));
   4797  1.41  yamaguch 
   4798  1.41  yamaguch 	iaq->iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD |
   4799  1.41  yamaguch 	    (dmalen > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   4800  1.41  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_RSS_SET_LUT);
   4801  1.41  yamaguch 	iaq->iaq_datalen = htole16(dmalen);
   4802  1.41  yamaguch 
   4803  1.41  yamaguch 	memset(IXL_DMA_KVA(idm), 0, IXL_DMA_LEN(idm));
   4804  1.41  yamaguch 	data = IXL_DMA_KVA(idm);
   4805  1.41  yamaguch 	memcpy(data, lut, dmalen);
   4806  1.41  yamaguch 	ixl_aq_dva(iaq, IXL_DMA_DVA(idm));
   4807  1.41  yamaguch 
   4808  1.41  yamaguch 	param = (struct ixl_aq_rss_lut_param *)iaq->iaq_param;
   4809  1.41  yamaguch 	vsi_id = (sc->sc_vsi_number << IXL_AQ_RSSLUT_VSI_ID_SHIFT) |
   4810  1.41  yamaguch 	    IXL_AQ_RSSLUT_VSI_VALID;
   4811  1.41  yamaguch 	param->vsi_id = htole16(vsi_id);
   4812  1.41  yamaguch 	param->flags = htole16(IXL_AQ_RSSLUT_TABLE_TYPE_PF <<
   4813  1.41  yamaguch 	    IXL_AQ_RSSLUT_TABLE_TYPE_SHIFT);
   4814  1.41  yamaguch 
   4815  1.41  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0,
   4816  1.41  yamaguch 	    IXL_DMA_LEN(idm), BUS_DMASYNC_PREWRITE);
   4817  1.41  yamaguch 
   4818  1.41  yamaguch 	rv = ixl_atq_exec(sc, &iatq);
   4819  1.41  yamaguch 
   4820  1.41  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0,
   4821  1.41  yamaguch 	    IXL_DMA_LEN(idm), BUS_DMASYNC_POSTWRITE);
   4822  1.41  yamaguch 
   4823  1.41  yamaguch 	if (rv != 0) {
   4824  1.41  yamaguch 		return ETIMEDOUT;
   4825  1.41  yamaguch 	}
   4826  1.41  yamaguch 
   4827  1.41  yamaguch 	if (iaq->iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4828  1.41  yamaguch 		return EIO;
   4829  1.41  yamaguch 	}
   4830  1.41  yamaguch 
   4831  1.41  yamaguch 	return 0;
   4832  1.41  yamaguch }
   4833  1.41  yamaguch 
   4834  1.41  yamaguch static int
   4835  1.41  yamaguch ixl_register_rss_key(struct ixl_softc *sc)
   4836   1.1  yamaguch {
   4837   1.1  yamaguch 	uint32_t rss_seed[IXL_RSS_KEY_SIZE_REG];
   4838  1.41  yamaguch 	int rv;
   4839   1.1  yamaguch 	size_t i;
   4840   1.1  yamaguch 
   4841   1.1  yamaguch 	ixl_get_default_rss_key(rss_seed, sizeof(rss_seed));
   4842   1.1  yamaguch 
   4843  1.72  yamaguch 	if (ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RSS)) {
   4844  1.41  yamaguch 		rv = ixl_set_rss_key(sc, (uint8_t*)rss_seed,
   4845  1.41  yamaguch 		    sizeof(rss_seed));
   4846  1.41  yamaguch 	} else {
   4847  1.41  yamaguch 		rv = 0;
   4848  1.41  yamaguch 		for (i = 0; i < IXL_RSS_KEY_SIZE_REG; i++) {
   4849  1.41  yamaguch 			ixl_wr_rx_csr(sc, I40E_PFQF_HKEY(i), rss_seed[i]);
   4850  1.41  yamaguch 		}
   4851   1.1  yamaguch 	}
   4852  1.41  yamaguch 
   4853  1.41  yamaguch 	return rv;
   4854   1.1  yamaguch }
   4855   1.1  yamaguch 
   4856   1.1  yamaguch static void
   4857  1.41  yamaguch ixl_register_rss_pctype(struct ixl_softc *sc)
   4858   1.1  yamaguch {
   4859   1.1  yamaguch 	uint64_t set_hena = 0;
   4860   1.1  yamaguch 	uint32_t hena0, hena1;
   4861   1.1  yamaguch 
   4862  1.56  yamaguch 	/*
   4863  1.56  yamaguch 	 * We use TCP/UDP with IPv4/IPv6 by default.
   4864  1.56  yamaguch 	 * Note: the device can not use just IP header in each
   4865  1.56  yamaguch 	 * TCP/UDP packets for the RSS hash calculation.
   4866  1.56  yamaguch 	 */
   4867   1.1  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722)
   4868   1.1  yamaguch 		set_hena = IXL_RSS_HENA_DEFAULT_X722;
   4869   1.1  yamaguch 	else
   4870   1.1  yamaguch 		set_hena = IXL_RSS_HENA_DEFAULT_XL710;
   4871   1.1  yamaguch 
   4872   1.1  yamaguch 	hena0 = ixl_rd_rx_csr(sc, I40E_PFQF_HENA(0));
   4873   1.1  yamaguch 	hena1 = ixl_rd_rx_csr(sc, I40E_PFQF_HENA(1));
   4874   1.1  yamaguch 
   4875   1.1  yamaguch 	SET(hena0, set_hena);
   4876   1.1  yamaguch 	SET(hena1, set_hena >> 32);
   4877   1.1  yamaguch 
   4878   1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_HENA(0), hena0);
   4879   1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_HENA(1), hena1);
   4880   1.1  yamaguch }
   4881   1.1  yamaguch 
   4882  1.41  yamaguch static int
   4883  1.41  yamaguch ixl_register_rss_hlut(struct ixl_softc *sc)
   4884   1.1  yamaguch {
   4885   1.1  yamaguch 	unsigned int qid;
   4886   1.1  yamaguch 	uint8_t hlut_buf[512], lut_mask;
   4887   1.1  yamaguch 	uint32_t *hluts;
   4888   1.1  yamaguch 	size_t i, hluts_num;
   4889  1.41  yamaguch 	int rv;
   4890   1.1  yamaguch 
   4891   1.1  yamaguch 	lut_mask = (0x01 << sc->sc_rss_table_entry_width) - 1;
   4892   1.1  yamaguch 
   4893   1.1  yamaguch 	for (i = 0; i < sc->sc_rss_table_size; i++) {
   4894   1.1  yamaguch 		qid = i % sc->sc_nqueue_pairs;
   4895   1.1  yamaguch 		hlut_buf[i] = qid & lut_mask;
   4896   1.1  yamaguch 	}
   4897   1.1  yamaguch 
   4898  1.41  yamaguch 	if (ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RSS)) {
   4899  1.41  yamaguch 		rv = ixl_set_rss_lut(sc, hlut_buf, sizeof(hlut_buf));
   4900  1.41  yamaguch 	} else {
   4901  1.41  yamaguch 		rv = 0;
   4902  1.41  yamaguch 		hluts = (uint32_t *)hlut_buf;
   4903  1.41  yamaguch 		hluts_num = sc->sc_rss_table_size >> 2;
   4904  1.41  yamaguch 		for (i = 0; i < hluts_num; i++) {
   4905  1.41  yamaguch 			ixl_wr(sc, I40E_PFQF_HLUT(i), hluts[i]);
   4906  1.41  yamaguch 		}
   4907  1.41  yamaguch 		ixl_flush(sc);
   4908   1.1  yamaguch 	}
   4909  1.41  yamaguch 
   4910  1.41  yamaguch 	return rv;
   4911   1.1  yamaguch }
   4912   1.1  yamaguch 
   4913   1.1  yamaguch static void
   4914   1.1  yamaguch ixl_config_rss(struct ixl_softc *sc)
   4915   1.1  yamaguch {
   4916   1.1  yamaguch 
   4917   1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   4918   1.1  yamaguch 
   4919  1.41  yamaguch 	ixl_register_rss_key(sc);
   4920  1.41  yamaguch 	ixl_register_rss_pctype(sc);
   4921  1.41  yamaguch 	ixl_register_rss_hlut(sc);
   4922   1.1  yamaguch }
   4923   1.1  yamaguch 
   4924   1.1  yamaguch static const struct ixl_phy_type *
   4925   1.1  yamaguch ixl_search_phy_type(uint8_t phy_type)
   4926   1.1  yamaguch {
   4927   1.1  yamaguch 	const struct ixl_phy_type *itype;
   4928   1.1  yamaguch 	uint64_t mask;
   4929   1.1  yamaguch 	unsigned int i;
   4930   1.1  yamaguch 
   4931   1.1  yamaguch 	if (phy_type >= 64)
   4932   1.1  yamaguch 		return NULL;
   4933   1.1  yamaguch 
   4934   1.1  yamaguch 	mask = 1ULL << phy_type;
   4935   1.1  yamaguch 
   4936   1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_phy_type_map); i++) {
   4937   1.1  yamaguch 		itype = &ixl_phy_type_map[i];
   4938   1.1  yamaguch 
   4939   1.1  yamaguch 		if (ISSET(itype->phy_type, mask))
   4940   1.1  yamaguch 			return itype;
   4941   1.1  yamaguch 	}
   4942   1.1  yamaguch 
   4943   1.1  yamaguch 	return NULL;
   4944   1.1  yamaguch }
   4945   1.1  yamaguch 
   4946   1.1  yamaguch static uint64_t
   4947   1.1  yamaguch ixl_search_link_speed(uint8_t link_speed)
   4948   1.1  yamaguch {
   4949   1.1  yamaguch 	const struct ixl_speed_type *type;
   4950   1.1  yamaguch 	unsigned int i;
   4951   1.1  yamaguch 
   4952   1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_speed_type_map); i++) {
   4953   1.1  yamaguch 		type = &ixl_speed_type_map[i];
   4954   1.1  yamaguch 
   4955   1.1  yamaguch 		if (ISSET(type->dev_speed, link_speed))
   4956   1.1  yamaguch 			return type->net_speed;
   4957   1.1  yamaguch 	}
   4958   1.1  yamaguch 
   4959   1.1  yamaguch 	return 0;
   4960   1.1  yamaguch }
   4961   1.1  yamaguch 
   4962  1.31  yamaguch static uint8_t
   4963  1.31  yamaguch ixl_search_baudrate(uint64_t baudrate)
   4964  1.31  yamaguch {
   4965  1.31  yamaguch 	const struct ixl_speed_type *type;
   4966  1.31  yamaguch 	unsigned int i;
   4967  1.31  yamaguch 
   4968  1.31  yamaguch 	for (i = 0; i < __arraycount(ixl_speed_type_map); i++) {
   4969  1.31  yamaguch 		type = &ixl_speed_type_map[i];
   4970  1.31  yamaguch 
   4971  1.31  yamaguch 		if (type->net_speed == baudrate) {
   4972  1.31  yamaguch 			return type->dev_speed;
   4973  1.31  yamaguch 		}
   4974  1.31  yamaguch 	}
   4975  1.31  yamaguch 
   4976  1.31  yamaguch 	return 0;
   4977  1.31  yamaguch }
   4978  1.31  yamaguch 
   4979   1.1  yamaguch static int
   4980   1.1  yamaguch ixl_restart_an(struct ixl_softc *sc)
   4981   1.1  yamaguch {
   4982   1.1  yamaguch 	struct ixl_aq_desc iaq;
   4983   1.1  yamaguch 
   4984   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4985   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_RESTART_AN);
   4986   1.1  yamaguch 	iaq.iaq_param[0] =
   4987   1.1  yamaguch 	    htole32(IXL_AQ_PHY_RESTART_AN | IXL_AQ_PHY_LINK_ENABLE);
   4988   1.1  yamaguch 
   4989   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4990   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "RESTART AN timeout\n");
   4991   1.1  yamaguch 		return -1;
   4992   1.1  yamaguch 	}
   4993   1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4994   1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "RESTART AN error\n");
   4995   1.1  yamaguch 		return -1;
   4996   1.1  yamaguch 	}
   4997   1.1  yamaguch 
   4998   1.1  yamaguch 	return 0;
   4999   1.1  yamaguch }
   5000   1.1  yamaguch 
   5001   1.1  yamaguch static int
   5002   1.1  yamaguch ixl_add_macvlan(struct ixl_softc *sc, const uint8_t *macaddr,
   5003   1.1  yamaguch     uint16_t vlan, uint16_t flags)
   5004   1.1  yamaguch {
   5005   1.1  yamaguch 	struct ixl_aq_desc iaq;
   5006   1.1  yamaguch 	struct ixl_aq_add_macvlan *param;
   5007   1.1  yamaguch 	struct ixl_aq_add_macvlan_elem *elem;
   5008   1.1  yamaguch 
   5009   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   5010   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
   5011   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_ADD_MACVLAN);
   5012   1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*elem));
   5013   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&sc->sc_scratch));
   5014   1.1  yamaguch 
   5015   1.1  yamaguch 	param = (struct ixl_aq_add_macvlan *)&iaq.iaq_param;
   5016   1.1  yamaguch 	param->num_addrs = htole16(1);
   5017   1.1  yamaguch 	param->seid0 = htole16(0x8000) | sc->sc_seid;
   5018   1.1  yamaguch 	param->seid1 = 0;
   5019   1.1  yamaguch 	param->seid2 = 0;
   5020   1.1  yamaguch 
   5021   1.1  yamaguch 	elem = IXL_DMA_KVA(&sc->sc_scratch);
   5022   1.1  yamaguch 	memset(elem, 0, sizeof(*elem));
   5023   1.1  yamaguch 	memcpy(elem->macaddr, macaddr, ETHER_ADDR_LEN);
   5024   1.1  yamaguch 	elem->flags = htole16(IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH | flags);
   5025   1.1  yamaguch 	elem->vlan = htole16(vlan);
   5026   1.1  yamaguch 
   5027   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   5028   1.1  yamaguch 		return IXL_AQ_RC_EINVAL;
   5029   1.1  yamaguch 	}
   5030   1.1  yamaguch 
   5031   1.7  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   5032   1.7  yamaguch 	case IXL_AQ_RC_OK:
   5033   1.7  yamaguch 		break;
   5034   1.7  yamaguch 	case IXL_AQ_RC_ENOSPC:
   5035   1.7  yamaguch 		return ENOSPC;
   5036   1.7  yamaguch 	case IXL_AQ_RC_ENOENT:
   5037   1.7  yamaguch 		return ENOENT;
   5038   1.7  yamaguch 	case IXL_AQ_RC_EACCES:
   5039   1.7  yamaguch 		return EACCES;
   5040   1.7  yamaguch 	case IXL_AQ_RC_EEXIST:
   5041   1.7  yamaguch 		return EEXIST;
   5042   1.7  yamaguch 	case IXL_AQ_RC_EINVAL:
   5043   1.7  yamaguch 		return EINVAL;
   5044   1.7  yamaguch 	default:
   5045   1.7  yamaguch 		return EIO;
   5046   1.7  yamaguch 	}
   5047   1.7  yamaguch 
   5048   1.7  yamaguch 	return 0;
   5049   1.1  yamaguch }
   5050   1.1  yamaguch 
   5051   1.1  yamaguch static int
   5052  1.12  yamaguch ixl_remove_macvlan(struct ixl_softc *sc, const uint8_t *macaddr,
   5053   1.1  yamaguch     uint16_t vlan, uint16_t flags)
   5054   1.1  yamaguch {
   5055   1.1  yamaguch 	struct ixl_aq_desc iaq;
   5056   1.1  yamaguch 	struct ixl_aq_remove_macvlan *param;
   5057   1.1  yamaguch 	struct ixl_aq_remove_macvlan_elem *elem;
   5058   1.1  yamaguch 
   5059   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   5060   1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
   5061   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_REMOVE_MACVLAN);
   5062   1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*elem));
   5063   1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&sc->sc_scratch));
   5064   1.1  yamaguch 
   5065   1.1  yamaguch 	param = (struct ixl_aq_remove_macvlan *)&iaq.iaq_param;
   5066   1.1  yamaguch 	param->num_addrs = htole16(1);
   5067   1.1  yamaguch 	param->seid0 = htole16(0x8000) | sc->sc_seid;
   5068   1.1  yamaguch 	param->seid1 = 0;
   5069   1.1  yamaguch 	param->seid2 = 0;
   5070   1.1  yamaguch 
   5071   1.1  yamaguch 	elem = IXL_DMA_KVA(&sc->sc_scratch);
   5072   1.1  yamaguch 	memset(elem, 0, sizeof(*elem));
   5073   1.1  yamaguch 	memcpy(elem->macaddr, macaddr, ETHER_ADDR_LEN);
   5074   1.1  yamaguch 	elem->flags = htole16(IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH | flags);
   5075   1.1  yamaguch 	elem->vlan = htole16(vlan);
   5076   1.1  yamaguch 
   5077   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   5078   1.7  yamaguch 		return EINVAL;
   5079   1.7  yamaguch 	}
   5080   1.7  yamaguch 
   5081   1.7  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   5082   1.7  yamaguch 	case IXL_AQ_RC_OK:
   5083   1.7  yamaguch 		break;
   5084   1.7  yamaguch 	case IXL_AQ_RC_ENOENT:
   5085   1.7  yamaguch 		return ENOENT;
   5086   1.7  yamaguch 	case IXL_AQ_RC_EACCES:
   5087   1.7  yamaguch 		return EACCES;
   5088   1.7  yamaguch 	case IXL_AQ_RC_EINVAL:
   5089   1.7  yamaguch 		return EINVAL;
   5090   1.7  yamaguch 	default:
   5091   1.7  yamaguch 		return EIO;
   5092   1.1  yamaguch 	}
   5093   1.1  yamaguch 
   5094   1.7  yamaguch 	return 0;
   5095   1.1  yamaguch }
   5096   1.1  yamaguch 
   5097   1.1  yamaguch static int
   5098   1.1  yamaguch ixl_hmc(struct ixl_softc *sc)
   5099   1.1  yamaguch {
   5100   1.1  yamaguch 	struct {
   5101   1.1  yamaguch 		uint32_t   count;
   5102   1.1  yamaguch 		uint32_t   minsize;
   5103   1.1  yamaguch 		bus_size_t objsiz;
   5104   1.1  yamaguch 		bus_size_t setoff;
   5105   1.1  yamaguch 		bus_size_t setcnt;
   5106   1.1  yamaguch 	} regs[] = {
   5107   1.1  yamaguch 		{
   5108   1.1  yamaguch 			0,
   5109   1.1  yamaguch 			IXL_HMC_TXQ_MINSIZE,
   5110   1.1  yamaguch 			I40E_GLHMC_LANTXOBJSZ,
   5111   1.1  yamaguch 			I40E_GLHMC_LANTXBASE(sc->sc_pf_id),
   5112   1.1  yamaguch 			I40E_GLHMC_LANTXCNT(sc->sc_pf_id),
   5113   1.1  yamaguch 		},
   5114   1.1  yamaguch 		{
   5115   1.1  yamaguch 			0,
   5116   1.1  yamaguch 			IXL_HMC_RXQ_MINSIZE,
   5117   1.1  yamaguch 			I40E_GLHMC_LANRXOBJSZ,
   5118   1.1  yamaguch 			I40E_GLHMC_LANRXBASE(sc->sc_pf_id),
   5119   1.1  yamaguch 			I40E_GLHMC_LANRXCNT(sc->sc_pf_id),
   5120   1.1  yamaguch 		},
   5121   1.1  yamaguch 		{
   5122   1.1  yamaguch 			0,
   5123   1.1  yamaguch 			0,
   5124   1.1  yamaguch 			I40E_GLHMC_FCOEDDPOBJSZ,
   5125   1.1  yamaguch 			I40E_GLHMC_FCOEDDPBASE(sc->sc_pf_id),
   5126   1.1  yamaguch 			I40E_GLHMC_FCOEDDPCNT(sc->sc_pf_id),
   5127   1.1  yamaguch 		},
   5128   1.1  yamaguch 		{
   5129   1.1  yamaguch 			0,
   5130   1.1  yamaguch 			0,
   5131   1.1  yamaguch 			I40E_GLHMC_FCOEFOBJSZ,
   5132   1.1  yamaguch 			I40E_GLHMC_FCOEFBASE(sc->sc_pf_id),
   5133   1.1  yamaguch 			I40E_GLHMC_FCOEFCNT(sc->sc_pf_id),
   5134   1.1  yamaguch 		},
   5135   1.1  yamaguch 	};
   5136   1.1  yamaguch 	struct ixl_hmc_entry *e;
   5137   1.1  yamaguch 	uint64_t size, dva;
   5138   1.1  yamaguch 	uint8_t *kva;
   5139   1.1  yamaguch 	uint64_t *sdpage;
   5140   1.1  yamaguch 	unsigned int i;
   5141   1.1  yamaguch 	int npages, tables;
   5142   1.1  yamaguch 	uint32_t reg;
   5143   1.1  yamaguch 
   5144   1.1  yamaguch 	CTASSERT(__arraycount(regs) <= __arraycount(sc->sc_hmc_entries));
   5145   1.1  yamaguch 
   5146   1.1  yamaguch 	regs[IXL_HMC_LAN_TX].count = regs[IXL_HMC_LAN_RX].count =
   5147   1.1  yamaguch 	    ixl_rd(sc, I40E_GLHMC_LANQMAX);
   5148   1.1  yamaguch 
   5149   1.1  yamaguch 	size = 0;
   5150   1.1  yamaguch 	for (i = 0; i < __arraycount(regs); i++) {
   5151   1.1  yamaguch 		e = &sc->sc_hmc_entries[i];
   5152   1.1  yamaguch 
   5153   1.1  yamaguch 		e->hmc_count = regs[i].count;
   5154   1.1  yamaguch 		reg = ixl_rd(sc, regs[i].objsiz);
   5155   1.1  yamaguch 		e->hmc_size = BIT_ULL(0x3F & reg);
   5156   1.1  yamaguch 		e->hmc_base = size;
   5157   1.1  yamaguch 
   5158   1.1  yamaguch 		if ((e->hmc_size * 8) < regs[i].minsize) {
   5159   1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   5160   1.1  yamaguch 			    "kernel hmc entry is too big\n");
   5161   1.1  yamaguch 			return -1;
   5162   1.1  yamaguch 		}
   5163   1.1  yamaguch 
   5164   1.1  yamaguch 		size += roundup(e->hmc_size * e->hmc_count, IXL_HMC_ROUNDUP);
   5165   1.1  yamaguch 	}
   5166   1.1  yamaguch 	size = roundup(size, IXL_HMC_PGSIZE);
   5167   1.1  yamaguch 	npages = size / IXL_HMC_PGSIZE;
   5168   1.1  yamaguch 
   5169   1.1  yamaguch 	tables = roundup(size, IXL_HMC_L2SZ) / IXL_HMC_L2SZ;
   5170   1.1  yamaguch 
   5171   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_hmc_pd, size, IXL_HMC_PGSIZE) != 0) {
   5172   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   5173   1.1  yamaguch 		    "unable to allocate hmc pd memory\n");
   5174   1.1  yamaguch 		return -1;
   5175   1.1  yamaguch 	}
   5176   1.1  yamaguch 
   5177   1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_hmc_sd, tables * IXL_HMC_PGSIZE,
   5178   1.1  yamaguch 	    IXL_HMC_PGSIZE) != 0) {
   5179   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   5180   1.1  yamaguch 		    "unable to allocate hmc sd memory\n");
   5181   1.1  yamaguch 		ixl_dmamem_free(sc, &sc->sc_hmc_pd);
   5182   1.1  yamaguch 		return -1;
   5183   1.1  yamaguch 	}
   5184   1.1  yamaguch 
   5185   1.1  yamaguch 	kva = IXL_DMA_KVA(&sc->sc_hmc_pd);
   5186   1.1  yamaguch 	memset(kva, 0, IXL_DMA_LEN(&sc->sc_hmc_pd));
   5187   1.1  yamaguch 
   5188   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   5189   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd),
   5190   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   5191   1.1  yamaguch 
   5192   1.1  yamaguch 	dva = IXL_DMA_DVA(&sc->sc_hmc_pd);
   5193   1.1  yamaguch 	sdpage = IXL_DMA_KVA(&sc->sc_hmc_sd);
   5194   1.1  yamaguch 	memset(sdpage, 0, IXL_DMA_LEN(&sc->sc_hmc_sd));
   5195   1.1  yamaguch 
   5196   1.1  yamaguch 	for (i = 0; (int)i < npages; i++) {
   5197   1.1  yamaguch 		*sdpage = htole64(dva | IXL_HMC_PDVALID);
   5198   1.1  yamaguch 		sdpage++;
   5199   1.1  yamaguch 
   5200   1.1  yamaguch 		dva += IXL_HMC_PGSIZE;
   5201   1.1  yamaguch 	}
   5202   1.1  yamaguch 
   5203   1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_sd),
   5204   1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_sd),
   5205   1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   5206   1.1  yamaguch 
   5207   1.1  yamaguch 	dva = IXL_DMA_DVA(&sc->sc_hmc_sd);
   5208   1.1  yamaguch 	for (i = 0; (int)i < tables; i++) {
   5209   1.1  yamaguch 		uint32_t count;
   5210   1.1  yamaguch 
   5211   1.1  yamaguch 		KASSERT(npages >= 0);
   5212   1.1  yamaguch 
   5213   1.1  yamaguch 		count = ((unsigned int)npages > IXL_HMC_PGS) ?
   5214   1.1  yamaguch 		    IXL_HMC_PGS : (unsigned int)npages;
   5215   1.1  yamaguch 
   5216   1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDDATAHIGH, dva >> 32);
   5217   1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDDATALOW, dva |
   5218   1.1  yamaguch 		    (count << I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |
   5219   1.1  yamaguch 		    (1U << I40E_PFHMC_SDDATALOW_PMSDVALID_SHIFT));
   5220   1.1  yamaguch 		ixl_barrier(sc, 0, sc->sc_mems, BUS_SPACE_BARRIER_WRITE);
   5221   1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDCMD,
   5222   1.1  yamaguch 		    (1U << I40E_PFHMC_SDCMD_PMSDWR_SHIFT) | i);
   5223   1.1  yamaguch 
   5224   1.1  yamaguch 		npages -= IXL_HMC_PGS;
   5225   1.1  yamaguch 		dva += IXL_HMC_PGSIZE;
   5226   1.1  yamaguch 	}
   5227   1.1  yamaguch 
   5228   1.1  yamaguch 	for (i = 0; i < __arraycount(regs); i++) {
   5229   1.1  yamaguch 		e = &sc->sc_hmc_entries[i];
   5230   1.1  yamaguch 
   5231   1.1  yamaguch 		ixl_wr(sc, regs[i].setoff, e->hmc_base / IXL_HMC_ROUNDUP);
   5232   1.1  yamaguch 		ixl_wr(sc, regs[i].setcnt, e->hmc_count);
   5233   1.1  yamaguch 	}
   5234   1.1  yamaguch 
   5235   1.1  yamaguch 	return 0;
   5236   1.1  yamaguch }
   5237   1.1  yamaguch 
   5238   1.1  yamaguch static void
   5239   1.1  yamaguch ixl_hmc_free(struct ixl_softc *sc)
   5240   1.1  yamaguch {
   5241   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_hmc_sd);
   5242   1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_hmc_pd);
   5243   1.1  yamaguch }
   5244   1.1  yamaguch 
   5245   1.1  yamaguch static void
   5246   1.1  yamaguch ixl_hmc_pack(void *d, const void *s, const struct ixl_hmc_pack *packing,
   5247   1.1  yamaguch     unsigned int npacking)
   5248   1.1  yamaguch {
   5249   1.1  yamaguch 	uint8_t *dst = d;
   5250   1.1  yamaguch 	const uint8_t *src = s;
   5251   1.1  yamaguch 	unsigned int i;
   5252   1.1  yamaguch 
   5253   1.1  yamaguch 	for (i = 0; i < npacking; i++) {
   5254   1.1  yamaguch 		const struct ixl_hmc_pack *pack = &packing[i];
   5255   1.1  yamaguch 		unsigned int offset = pack->lsb / 8;
   5256   1.1  yamaguch 		unsigned int align = pack->lsb % 8;
   5257   1.1  yamaguch 		const uint8_t *in = src + pack->offset;
   5258   1.1  yamaguch 		uint8_t *out = dst + offset;
   5259   1.1  yamaguch 		int width = pack->width;
   5260   1.1  yamaguch 		unsigned int inbits = 0;
   5261   1.1  yamaguch 
   5262   1.1  yamaguch 		if (align) {
   5263   1.1  yamaguch 			inbits = (*in++) << align;
   5264   1.1  yamaguch 			*out++ |= (inbits & 0xff);
   5265   1.1  yamaguch 			inbits >>= 8;
   5266   1.1  yamaguch 
   5267   1.1  yamaguch 			width -= 8 - align;
   5268   1.1  yamaguch 		}
   5269   1.1  yamaguch 
   5270   1.1  yamaguch 		while (width >= 8) {
   5271   1.1  yamaguch 			inbits |= (*in++) << align;
   5272   1.1  yamaguch 			*out++ = (inbits & 0xff);
   5273   1.1  yamaguch 			inbits >>= 8;
   5274   1.1  yamaguch 
   5275   1.1  yamaguch 			width -= 8;
   5276   1.1  yamaguch 		}
   5277   1.1  yamaguch 
   5278   1.1  yamaguch 		if (width > 0) {
   5279   1.1  yamaguch 			inbits |= (*in) << align;
   5280   1.1  yamaguch 			*out |= (inbits & ((1 << width) - 1));
   5281   1.1  yamaguch 		}
   5282   1.1  yamaguch 	}
   5283   1.1  yamaguch }
   5284   1.1  yamaguch 
   5285   1.1  yamaguch static struct ixl_aq_buf *
   5286   1.1  yamaguch ixl_aqb_alloc(struct ixl_softc *sc)
   5287   1.1  yamaguch {
   5288   1.1  yamaguch 	struct ixl_aq_buf *aqb;
   5289   1.1  yamaguch 
   5290  1.54  yamaguch 	aqb = kmem_alloc(sizeof(*aqb), KM_SLEEP);
   5291   1.1  yamaguch 
   5292   1.1  yamaguch 	aqb->aqb_size = IXL_AQ_BUFLEN;
   5293   1.1  yamaguch 
   5294   1.1  yamaguch 	if (bus_dmamap_create(sc->sc_dmat, aqb->aqb_size, 1,
   5295   1.1  yamaguch 	    aqb->aqb_size, 0,
   5296   1.1  yamaguch 	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &aqb->aqb_map) != 0)
   5297   1.1  yamaguch 		goto free;
   5298   1.1  yamaguch 	if (bus_dmamem_alloc(sc->sc_dmat, aqb->aqb_size,
   5299   1.1  yamaguch 	    IXL_AQ_ALIGN, 0, &aqb->aqb_seg, 1, &aqb->aqb_nsegs,
   5300   1.1  yamaguch 	    BUS_DMA_WAITOK) != 0)
   5301   1.1  yamaguch 		goto destroy;
   5302   1.1  yamaguch 	if (bus_dmamem_map(sc->sc_dmat, &aqb->aqb_seg, aqb->aqb_nsegs,
   5303   1.1  yamaguch 	    aqb->aqb_size, &aqb->aqb_data, BUS_DMA_WAITOK) != 0)
   5304   1.1  yamaguch 		goto dma_free;
   5305   1.1  yamaguch 	if (bus_dmamap_load(sc->sc_dmat, aqb->aqb_map, aqb->aqb_data,
   5306   1.1  yamaguch 	    aqb->aqb_size, NULL, BUS_DMA_WAITOK) != 0)
   5307   1.1  yamaguch 		goto unmap;
   5308   1.1  yamaguch 
   5309   1.1  yamaguch 	return aqb;
   5310   1.1  yamaguch unmap:
   5311   1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, aqb->aqb_data, aqb->aqb_size);
   5312   1.1  yamaguch dma_free:
   5313   1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &aqb->aqb_seg, 1);
   5314   1.1  yamaguch destroy:
   5315   1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, aqb->aqb_map);
   5316   1.1  yamaguch free:
   5317  1.54  yamaguch 	kmem_free(aqb, sizeof(*aqb));
   5318   1.1  yamaguch 
   5319   1.1  yamaguch 	return NULL;
   5320   1.1  yamaguch }
   5321   1.1  yamaguch 
   5322   1.1  yamaguch static void
   5323   1.1  yamaguch ixl_aqb_free(struct ixl_softc *sc, struct ixl_aq_buf *aqb)
   5324   1.1  yamaguch {
   5325  1.54  yamaguch 
   5326   1.1  yamaguch 	bus_dmamap_unload(sc->sc_dmat, aqb->aqb_map);
   5327   1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, aqb->aqb_data, aqb->aqb_size);
   5328   1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &aqb->aqb_seg, 1);
   5329   1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, aqb->aqb_map);
   5330  1.54  yamaguch 	kmem_free(aqb, sizeof(*aqb));
   5331   1.1  yamaguch }
   5332   1.1  yamaguch 
   5333   1.1  yamaguch static int
   5334   1.1  yamaguch ixl_arq_fill(struct ixl_softc *sc)
   5335   1.1  yamaguch {
   5336   1.1  yamaguch 	struct ixl_aq_buf *aqb;
   5337   1.1  yamaguch 	struct ixl_aq_desc *arq, *iaq;
   5338   1.1  yamaguch 	unsigned int prod = sc->sc_arq_prod;
   5339   1.1  yamaguch 	unsigned int n;
   5340   1.1  yamaguch 	int post = 0;
   5341   1.1  yamaguch 
   5342   1.1  yamaguch 	n = ixl_rxr_unrefreshed(sc->sc_arq_prod, sc->sc_arq_cons,
   5343   1.1  yamaguch 	    IXL_AQ_NUM);
   5344   1.1  yamaguch 	arq = IXL_DMA_KVA(&sc->sc_arq);
   5345   1.1  yamaguch 
   5346   1.1  yamaguch 	if (__predict_false(n <= 0))
   5347   1.1  yamaguch 		return 0;
   5348   1.1  yamaguch 
   5349   1.1  yamaguch 	do {
   5350   1.1  yamaguch 		aqb = sc->sc_arq_live[prod];
   5351   1.1  yamaguch 		iaq = &arq[prod];
   5352   1.1  yamaguch 
   5353   1.1  yamaguch 		if (aqb == NULL) {
   5354   1.1  yamaguch 			aqb = SIMPLEQ_FIRST(&sc->sc_arq_idle);
   5355   1.1  yamaguch 			if (aqb != NULL) {
   5356   1.1  yamaguch 				SIMPLEQ_REMOVE(&sc->sc_arq_idle, aqb,
   5357   1.1  yamaguch 				    ixl_aq_buf, aqb_entry);
   5358   1.1  yamaguch 			} else if ((aqb = ixl_aqb_alloc(sc)) == NULL) {
   5359   1.1  yamaguch 				break;
   5360   1.1  yamaguch 			}
   5361   1.1  yamaguch 
   5362   1.1  yamaguch 			sc->sc_arq_live[prod] = aqb;
   5363   1.1  yamaguch 			memset(aqb->aqb_data, 0, aqb->aqb_size);
   5364   1.1  yamaguch 
   5365   1.1  yamaguch 			bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0,
   5366   1.1  yamaguch 			    aqb->aqb_size, BUS_DMASYNC_PREREAD);
   5367   1.1  yamaguch 
   5368   1.1  yamaguch 			iaq->iaq_flags = htole16(IXL_AQ_BUF |
   5369   1.1  yamaguch 			    (IXL_AQ_BUFLEN > I40E_AQ_LARGE_BUF ?
   5370   1.1  yamaguch 			    IXL_AQ_LB : 0));
   5371   1.1  yamaguch 			iaq->iaq_opcode = 0;
   5372   1.1  yamaguch 			iaq->iaq_datalen = htole16(aqb->aqb_size);
   5373   1.1  yamaguch 			iaq->iaq_retval = 0;
   5374   1.1  yamaguch 			iaq->iaq_cookie = 0;
   5375   1.1  yamaguch 			iaq->iaq_param[0] = 0;
   5376   1.1  yamaguch 			iaq->iaq_param[1] = 0;
   5377   1.1  yamaguch 			ixl_aq_dva(iaq, aqb->aqb_map->dm_segs[0].ds_addr);
   5378   1.1  yamaguch 		}
   5379   1.1  yamaguch 
   5380   1.1  yamaguch 		prod++;
   5381   1.1  yamaguch 		prod &= IXL_AQ_MASK;
   5382   1.1  yamaguch 
   5383   1.1  yamaguch 		post = 1;
   5384   1.1  yamaguch 
   5385   1.1  yamaguch 	} while (--n);
   5386   1.1  yamaguch 
   5387   1.1  yamaguch 	if (post) {
   5388   1.1  yamaguch 		sc->sc_arq_prod = prod;
   5389   1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
   5390   1.1  yamaguch 	}
   5391   1.1  yamaguch 
   5392   1.1  yamaguch 	return post;
   5393   1.1  yamaguch }
   5394   1.1  yamaguch 
   5395   1.1  yamaguch static void
   5396   1.1  yamaguch ixl_arq_unfill(struct ixl_softc *sc)
   5397   1.1  yamaguch {
   5398   1.1  yamaguch 	struct ixl_aq_buf *aqb;
   5399   1.1  yamaguch 	unsigned int i;
   5400   1.1  yamaguch 
   5401   1.1  yamaguch 	for (i = 0; i < __arraycount(sc->sc_arq_live); i++) {
   5402   1.1  yamaguch 		aqb = sc->sc_arq_live[i];
   5403   1.1  yamaguch 		if (aqb == NULL)
   5404   1.1  yamaguch 			continue;
   5405   1.1  yamaguch 
   5406   1.1  yamaguch 		sc->sc_arq_live[i] = NULL;
   5407   1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, aqb->aqb_size,
   5408   1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   5409   1.1  yamaguch 		ixl_aqb_free(sc, aqb);
   5410   1.1  yamaguch 	}
   5411   1.1  yamaguch 
   5412   1.1  yamaguch 	while ((aqb = SIMPLEQ_FIRST(&sc->sc_arq_idle)) != NULL) {
   5413   1.1  yamaguch 		SIMPLEQ_REMOVE(&sc->sc_arq_idle, aqb,
   5414   1.1  yamaguch 		    ixl_aq_buf, aqb_entry);
   5415   1.1  yamaguch 		ixl_aqb_free(sc, aqb);
   5416   1.1  yamaguch 	}
   5417   1.1  yamaguch }
   5418   1.1  yamaguch 
   5419   1.1  yamaguch static void
   5420   1.1  yamaguch ixl_clear_hw(struct ixl_softc *sc)
   5421   1.1  yamaguch {
   5422   1.1  yamaguch 	uint32_t num_queues, base_queue;
   5423   1.1  yamaguch 	uint32_t num_pf_int;
   5424   1.1  yamaguch 	uint32_t num_vf_int;
   5425   1.1  yamaguch 	uint32_t num_vfs;
   5426   1.1  yamaguch 	uint32_t i, j;
   5427   1.1  yamaguch 	uint32_t val;
   5428   1.1  yamaguch 	uint32_t eol = 0x7ff;
   5429   1.1  yamaguch 
   5430   1.1  yamaguch 	/* get number of interrupts, queues, and vfs */
   5431   1.1  yamaguch 	val = ixl_rd(sc, I40E_GLPCI_CNF2);
   5432   1.1  yamaguch 	num_pf_int = (val & I40E_GLPCI_CNF2_MSI_X_PF_N_MASK) >>
   5433   1.1  yamaguch 	    I40E_GLPCI_CNF2_MSI_X_PF_N_SHIFT;
   5434   1.1  yamaguch 	num_vf_int = (val & I40E_GLPCI_CNF2_MSI_X_VF_N_MASK) >>
   5435   1.1  yamaguch 	    I40E_GLPCI_CNF2_MSI_X_VF_N_SHIFT;
   5436   1.1  yamaguch 
   5437   1.1  yamaguch 	val = ixl_rd(sc, I40E_PFLAN_QALLOC);
   5438   1.1  yamaguch 	base_queue = (val & I40E_PFLAN_QALLOC_FIRSTQ_MASK) >>
   5439   1.1  yamaguch 	    I40E_PFLAN_QALLOC_FIRSTQ_SHIFT;
   5440   1.1  yamaguch 	j = (val & I40E_PFLAN_QALLOC_LASTQ_MASK) >>
   5441   1.1  yamaguch 	    I40E_PFLAN_QALLOC_LASTQ_SHIFT;
   5442   1.1  yamaguch 	if (val & I40E_PFLAN_QALLOC_VALID_MASK)
   5443   1.1  yamaguch 		num_queues = (j - base_queue) + 1;
   5444   1.1  yamaguch 	else
   5445   1.1  yamaguch 		num_queues = 0;
   5446   1.1  yamaguch 
   5447   1.1  yamaguch 	val = ixl_rd(sc, I40E_PF_VT_PFALLOC);
   5448   1.1  yamaguch 	i = (val & I40E_PF_VT_PFALLOC_FIRSTVF_MASK) >>
   5449   1.1  yamaguch 	    I40E_PF_VT_PFALLOC_FIRSTVF_SHIFT;
   5450   1.1  yamaguch 	j = (val & I40E_PF_VT_PFALLOC_LASTVF_MASK) >>
   5451   1.1  yamaguch 	    I40E_PF_VT_PFALLOC_LASTVF_SHIFT;
   5452   1.1  yamaguch 	if (val & I40E_PF_VT_PFALLOC_VALID_MASK)
   5453   1.1  yamaguch 		num_vfs = (j - i) + 1;
   5454   1.1  yamaguch 	else
   5455   1.1  yamaguch 		num_vfs = 0;
   5456   1.1  yamaguch 
   5457   1.1  yamaguch 	/* stop all the interrupts */
   5458   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, 0);
   5459   1.1  yamaguch 	ixl_flush(sc);
   5460   1.1  yamaguch 	val = 0x3 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
   5461   1.1  yamaguch 	for (i = 0; i < num_pf_int - 2; i++)
   5462   1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_DYN_CTLN(i), val);
   5463   1.1  yamaguch 	ixl_flush(sc);
   5464   1.1  yamaguch 
   5465   1.1  yamaguch 	/* Set the FIRSTQ_INDX field to 0x7FF in PFINT_LNKLSTx */
   5466   1.1  yamaguch 	val = eol << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
   5467   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_LNKLST0, val);
   5468   1.1  yamaguch 	for (i = 0; i < num_pf_int - 2; i++)
   5469   1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLSTN(i), val);
   5470   1.1  yamaguch 	val = eol << I40E_VPINT_LNKLST0_FIRSTQ_INDX_SHIFT;
   5471   1.1  yamaguch 	for (i = 0; i < num_vfs; i++)
   5472   1.1  yamaguch 		ixl_wr(sc, I40E_VPINT_LNKLST0(i), val);
   5473   1.1  yamaguch 	for (i = 0; i < num_vf_int - 2; i++)
   5474   1.1  yamaguch 		ixl_wr(sc, I40E_VPINT_LNKLSTN(i), val);
   5475   1.1  yamaguch 
   5476   1.1  yamaguch 	/* warn the HW of the coming Tx disables */
   5477   1.1  yamaguch 	for (i = 0; i < num_queues; i++) {
   5478   1.1  yamaguch 		uint32_t abs_queue_idx = base_queue + i;
   5479   1.1  yamaguch 		uint32_t reg_block = 0;
   5480   1.1  yamaguch 
   5481   1.1  yamaguch 		if (abs_queue_idx >= 128) {
   5482   1.1  yamaguch 			reg_block = abs_queue_idx / 128;
   5483   1.1  yamaguch 			abs_queue_idx %= 128;
   5484   1.1  yamaguch 		}
   5485   1.1  yamaguch 
   5486   1.1  yamaguch 		val = ixl_rd(sc, I40E_GLLAN_TXPRE_QDIS(reg_block));
   5487   1.1  yamaguch 		val &= ~I40E_GLLAN_TXPRE_QDIS_QINDX_MASK;
   5488   1.1  yamaguch 		val |= (abs_queue_idx << I40E_GLLAN_TXPRE_QDIS_QINDX_SHIFT);
   5489   1.1  yamaguch 		val |= I40E_GLLAN_TXPRE_QDIS_SET_QDIS_MASK;
   5490   1.1  yamaguch 
   5491   1.1  yamaguch 		ixl_wr(sc, I40E_GLLAN_TXPRE_QDIS(reg_block), val);
   5492   1.1  yamaguch 	}
   5493   1.1  yamaguch 	delaymsec(400);
   5494   1.1  yamaguch 
   5495   1.1  yamaguch 	/* stop all the queues */
   5496   1.1  yamaguch 	for (i = 0; i < num_queues; i++) {
   5497   1.1  yamaguch 		ixl_wr(sc, I40E_QINT_TQCTL(i), 0);
   5498   1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), 0);
   5499   1.1  yamaguch 		ixl_wr(sc, I40E_QINT_RQCTL(i), 0);
   5500   1.1  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), 0);
   5501   1.1  yamaguch 	}
   5502   1.1  yamaguch 
   5503   1.1  yamaguch 	/* short wait for all queue disables to settle */
   5504   1.1  yamaguch 	delaymsec(50);
   5505   1.1  yamaguch }
   5506   1.1  yamaguch 
   5507   1.1  yamaguch static int
   5508   1.1  yamaguch ixl_pf_reset(struct ixl_softc *sc)
   5509   1.1  yamaguch {
   5510   1.1  yamaguch 	uint32_t cnt = 0;
   5511   1.1  yamaguch 	uint32_t cnt1 = 0;
   5512   1.1  yamaguch 	uint32_t reg = 0, reg0 = 0;
   5513   1.1  yamaguch 	uint32_t grst_del;
   5514   1.1  yamaguch 
   5515   1.1  yamaguch 	/*
   5516   1.1  yamaguch 	 * Poll for Global Reset steady state in case of recent GRST.
   5517   1.1  yamaguch 	 * The grst delay value is in 100ms units, and we'll wait a
   5518   1.1  yamaguch 	 * couple counts longer to be sure we don't just miss the end.
   5519   1.1  yamaguch 	 */
   5520   1.1  yamaguch 	grst_del = ixl_rd(sc, I40E_GLGEN_RSTCTL);
   5521   1.1  yamaguch 	grst_del &= I40E_GLGEN_RSTCTL_GRSTDEL_MASK;
   5522   1.1  yamaguch 	grst_del >>= I40E_GLGEN_RSTCTL_GRSTDEL_SHIFT;
   5523   1.1  yamaguch 
   5524   1.1  yamaguch 	grst_del = grst_del * 20;
   5525   1.1  yamaguch 
   5526   1.1  yamaguch 	for (cnt = 0; cnt < grst_del; cnt++) {
   5527   1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLGEN_RSTAT);
   5528   1.1  yamaguch 		if (!(reg & I40E_GLGEN_RSTAT_DEVSTATE_MASK))
   5529   1.1  yamaguch 			break;
   5530   1.1  yamaguch 		delaymsec(100);
   5531   1.1  yamaguch 	}
   5532   1.1  yamaguch 	if (reg & I40E_GLGEN_RSTAT_DEVSTATE_MASK) {
   5533   1.1  yamaguch 		aprint_error(", Global reset polling failed to complete\n");
   5534   1.1  yamaguch 		return -1;
   5535   1.1  yamaguch 	}
   5536   1.1  yamaguch 
   5537   1.1  yamaguch 	/* Now Wait for the FW to be ready */
   5538   1.1  yamaguch 	for (cnt1 = 0; cnt1 < I40E_PF_RESET_WAIT_COUNT; cnt1++) {
   5539   1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLNVM_ULD);
   5540   1.1  yamaguch 		reg &= (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   5541   1.1  yamaguch 		    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK);
   5542   1.1  yamaguch 		if (reg == (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   5543   1.1  yamaguch 		    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK))
   5544   1.1  yamaguch 			break;
   5545   1.1  yamaguch 
   5546   1.1  yamaguch 		delaymsec(10);
   5547   1.1  yamaguch 	}
   5548   1.1  yamaguch 	if (!(reg & (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   5549   1.1  yamaguch 	    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK))) {
   5550   1.1  yamaguch 		aprint_error(", wait for FW Reset complete timed out "
   5551   1.1  yamaguch 		    "(I40E_GLNVM_ULD = 0x%x)\n", reg);
   5552   1.1  yamaguch 		return -1;
   5553   1.1  yamaguch 	}
   5554   1.1  yamaguch 
   5555   1.1  yamaguch 	/*
   5556   1.1  yamaguch 	 * If there was a Global Reset in progress when we got here,
   5557   1.1  yamaguch 	 * we don't need to do the PF Reset
   5558   1.1  yamaguch 	 */
   5559   1.1  yamaguch 	if (cnt == 0) {
   5560   1.1  yamaguch 		reg = ixl_rd(sc, I40E_PFGEN_CTRL);
   5561   1.1  yamaguch 		ixl_wr(sc, I40E_PFGEN_CTRL, reg | I40E_PFGEN_CTRL_PFSWR_MASK);
   5562   1.1  yamaguch 		for (cnt = 0; cnt < I40E_PF_RESET_WAIT_COUNT; cnt++) {
   5563   1.1  yamaguch 			reg = ixl_rd(sc, I40E_PFGEN_CTRL);
   5564   1.1  yamaguch 			if (!(reg & I40E_PFGEN_CTRL_PFSWR_MASK))
   5565   1.1  yamaguch 				break;
   5566   1.1  yamaguch 			delaymsec(1);
   5567   1.1  yamaguch 
   5568   1.1  yamaguch 			reg0 = ixl_rd(sc, I40E_GLGEN_RSTAT);
   5569   1.1  yamaguch 			if (reg0 & I40E_GLGEN_RSTAT_DEVSTATE_MASK) {
   5570   1.1  yamaguch 				aprint_error(", Core reset upcoming."
   5571   1.1  yamaguch 				    " Skipping PF reset reset request\n");
   5572   1.1  yamaguch 				return -1;
   5573   1.1  yamaguch 			}
   5574   1.1  yamaguch 		}
   5575   1.1  yamaguch 		if (reg & I40E_PFGEN_CTRL_PFSWR_MASK) {
   5576   1.1  yamaguch 			aprint_error(", PF reset polling failed to complete"
   5577   1.1  yamaguch 			    "(I40E_PFGEN_CTRL= 0x%x)\n", reg);
   5578   1.1  yamaguch 			return -1;
   5579   1.1  yamaguch 		}
   5580   1.1  yamaguch 	}
   5581   1.1  yamaguch 
   5582   1.1  yamaguch 	return 0;
   5583   1.1  yamaguch }
   5584   1.1  yamaguch 
   5585   1.1  yamaguch static int
   5586   1.1  yamaguch ixl_dmamem_alloc(struct ixl_softc *sc, struct ixl_dmamem *ixm,
   5587   1.1  yamaguch     bus_size_t size, bus_size_t align)
   5588   1.1  yamaguch {
   5589   1.1  yamaguch 	ixm->ixm_size = size;
   5590   1.1  yamaguch 
   5591   1.1  yamaguch 	if (bus_dmamap_create(sc->sc_dmat, ixm->ixm_size, 1,
   5592   1.1  yamaguch 	    ixm->ixm_size, 0,
   5593   1.1  yamaguch 	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
   5594   1.1  yamaguch 	    &ixm->ixm_map) != 0)
   5595   1.1  yamaguch 		return 1;
   5596   1.1  yamaguch 	if (bus_dmamem_alloc(sc->sc_dmat, ixm->ixm_size,
   5597   1.1  yamaguch 	    align, 0, &ixm->ixm_seg, 1, &ixm->ixm_nsegs,
   5598   1.1  yamaguch 	    BUS_DMA_WAITOK) != 0)
   5599   1.1  yamaguch 		goto destroy;
   5600   1.1  yamaguch 	if (bus_dmamem_map(sc->sc_dmat, &ixm->ixm_seg, ixm->ixm_nsegs,
   5601   1.1  yamaguch 	    ixm->ixm_size, &ixm->ixm_kva, BUS_DMA_WAITOK) != 0)
   5602   1.1  yamaguch 		goto free;
   5603   1.1  yamaguch 	if (bus_dmamap_load(sc->sc_dmat, ixm->ixm_map, ixm->ixm_kva,
   5604   1.1  yamaguch 	    ixm->ixm_size, NULL, BUS_DMA_WAITOK) != 0)
   5605   1.1  yamaguch 		goto unmap;
   5606   1.1  yamaguch 
   5607   1.1  yamaguch 	memset(ixm->ixm_kva, 0, ixm->ixm_size);
   5608   1.1  yamaguch 
   5609   1.1  yamaguch 	return 0;
   5610   1.1  yamaguch unmap:
   5611   1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, ixm->ixm_kva, ixm->ixm_size);
   5612   1.1  yamaguch free:
   5613   1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &ixm->ixm_seg, 1);
   5614   1.1  yamaguch destroy:
   5615   1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, ixm->ixm_map);
   5616   1.1  yamaguch 	return 1;
   5617   1.1  yamaguch }
   5618   1.1  yamaguch 
   5619   1.1  yamaguch static void
   5620   1.1  yamaguch ixl_dmamem_free(struct ixl_softc *sc, struct ixl_dmamem *ixm)
   5621   1.1  yamaguch {
   5622   1.1  yamaguch 	bus_dmamap_unload(sc->sc_dmat, ixm->ixm_map);
   5623   1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, ixm->ixm_kva, ixm->ixm_size);
   5624   1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &ixm->ixm_seg, 1);
   5625   1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, ixm->ixm_map);
   5626   1.1  yamaguch }
   5627   1.1  yamaguch 
   5628   1.1  yamaguch static int
   5629  1.12  yamaguch ixl_setup_vlan_hwfilter(struct ixl_softc *sc)
   5630   1.1  yamaguch {
   5631  1.12  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   5632  1.12  yamaguch 	struct vlanid_list *vlanidp;
   5633  1.12  yamaguch 	int rv;
   5634  1.12  yamaguch 
   5635  1.12  yamaguch 	ixl_remove_macvlan(sc, sc->sc_enaddr, 0,
   5636  1.12  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   5637  1.12  yamaguch 	ixl_remove_macvlan(sc, etherbroadcastaddr, 0,
   5638  1.12  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   5639   1.1  yamaguch 
   5640  1.12  yamaguch 	rv = ixl_add_macvlan(sc, sc->sc_enaddr, 0,
   5641  1.12  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   5642  1.12  yamaguch 	if (rv != 0)
   5643  1.12  yamaguch 		return rv;
   5644  1.12  yamaguch 	rv = ixl_add_macvlan(sc, etherbroadcastaddr, 0,
   5645  1.12  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   5646  1.12  yamaguch 	if (rv != 0)
   5647  1.12  yamaguch 		return rv;
   5648   1.1  yamaguch 
   5649  1.12  yamaguch 	ETHER_LOCK(ec);
   5650  1.12  yamaguch 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
   5651  1.12  yamaguch 		rv = ixl_add_macvlan(sc, sc->sc_enaddr,
   5652  1.12  yamaguch 		    vlanidp->vid, IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   5653  1.12  yamaguch 		if (rv != 0)
   5654  1.12  yamaguch 			break;
   5655  1.12  yamaguch 		rv = ixl_add_macvlan(sc, etherbroadcastaddr,
   5656  1.12  yamaguch 		    vlanidp->vid, IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH);
   5657  1.12  yamaguch 		if (rv != 0)
   5658  1.12  yamaguch 			break;
   5659   1.1  yamaguch 	}
   5660  1.12  yamaguch 	ETHER_UNLOCK(ec);
   5661   1.1  yamaguch 
   5662  1.12  yamaguch 	return rv;
   5663  1.12  yamaguch }
   5664  1.12  yamaguch 
   5665  1.12  yamaguch static void
   5666  1.12  yamaguch ixl_teardown_vlan_hwfilter(struct ixl_softc *sc)
   5667  1.12  yamaguch {
   5668  1.12  yamaguch 	struct vlanid_list *vlanidp;
   5669  1.12  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   5670  1.12  yamaguch 
   5671  1.12  yamaguch 	ixl_remove_macvlan(sc, sc->sc_enaddr, 0,
   5672  1.12  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   5673  1.12  yamaguch 	ixl_remove_macvlan(sc, etherbroadcastaddr, 0,
   5674  1.12  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   5675  1.12  yamaguch 
   5676  1.12  yamaguch 	ETHER_LOCK(ec);
   5677  1.12  yamaguch 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
   5678  1.12  yamaguch 		ixl_remove_macvlan(sc, sc->sc_enaddr,
   5679  1.12  yamaguch 		    vlanidp->vid, IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   5680  1.12  yamaguch 		ixl_remove_macvlan(sc, etherbroadcastaddr,
   5681  1.12  yamaguch 		    vlanidp->vid, IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH);
   5682   1.1  yamaguch 	}
   5683  1.12  yamaguch 	ETHER_UNLOCK(ec);
   5684   1.1  yamaguch 
   5685  1.12  yamaguch 	ixl_add_macvlan(sc, sc->sc_enaddr, 0,
   5686   1.1  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   5687  1.12  yamaguch 	ixl_add_macvlan(sc, etherbroadcastaddr, 0,
   5688  1.12  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   5689  1.12  yamaguch }
   5690  1.12  yamaguch 
   5691  1.12  yamaguch static int
   5692  1.12  yamaguch ixl_update_macvlan(struct ixl_softc *sc)
   5693  1.12  yamaguch {
   5694  1.12  yamaguch 	int rv = 0;
   5695  1.12  yamaguch 	int next_ec_capenable = sc->sc_ec.ec_capenable;
   5696   1.1  yamaguch 
   5697  1.12  yamaguch 	if (ISSET(next_ec_capenable, ETHERCAP_VLAN_HWFILTER)) {
   5698  1.12  yamaguch 		rv = ixl_setup_vlan_hwfilter(sc);
   5699  1.12  yamaguch 		if (rv != 0)
   5700  1.12  yamaguch 			ixl_teardown_vlan_hwfilter(sc);
   5701  1.12  yamaguch 	} else {
   5702  1.12  yamaguch 		ixl_teardown_vlan_hwfilter(sc);
   5703   1.1  yamaguch 	}
   5704   1.1  yamaguch 
   5705   1.1  yamaguch 	return rv;
   5706   1.1  yamaguch }
   5707   1.1  yamaguch 
   5708   1.1  yamaguch static int
   5709   1.1  yamaguch ixl_ifflags_cb(struct ethercom *ec)
   5710   1.1  yamaguch {
   5711   1.9  yamaguch 	struct ifnet *ifp = &ec->ec_if;
   5712   1.9  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   5713  1.11  yamaguch 	int rv, change;
   5714   1.9  yamaguch 
   5715   1.9  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   5716  1.11  yamaguch 
   5717  1.11  yamaguch 	change = ec->ec_capenable ^ sc->sc_cur_ec_capenable;
   5718  1.11  yamaguch 
   5719  1.11  yamaguch 	if (ISSET(change, ETHERCAP_VLAN_HWTAGGING)) {
   5720  1.12  yamaguch 		sc->sc_cur_ec_capenable ^= ETHERCAP_VLAN_HWTAGGING;
   5721  1.11  yamaguch 		rv = ENETRESET;
   5722  1.11  yamaguch 		goto out;
   5723  1.11  yamaguch 	}
   5724  1.11  yamaguch 
   5725  1.12  yamaguch 	if (ISSET(change, ETHERCAP_VLAN_HWFILTER)) {
   5726  1.12  yamaguch 		rv = ixl_update_macvlan(sc);
   5727  1.12  yamaguch 		if (rv == 0) {
   5728  1.12  yamaguch 			sc->sc_cur_ec_capenable ^= ETHERCAP_VLAN_HWFILTER;
   5729  1.12  yamaguch 		} else {
   5730  1.12  yamaguch 			CLR(ec->ec_capenable, ETHERCAP_VLAN_HWFILTER);
   5731  1.12  yamaguch 			CLR(sc->sc_cur_ec_capenable, ETHERCAP_VLAN_HWFILTER);
   5732  1.12  yamaguch 		}
   5733  1.12  yamaguch 	}
   5734  1.12  yamaguch 
   5735   1.9  yamaguch 	rv = ixl_iff(sc);
   5736  1.11  yamaguch out:
   5737   1.9  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   5738   1.1  yamaguch 
   5739   1.9  yamaguch 	return rv;
   5740   1.1  yamaguch }
   5741   1.1  yamaguch 
   5742   1.1  yamaguch static int
   5743  1.62  yamaguch ixl_set_link_status_locked(struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   5744   1.1  yamaguch {
   5745   1.1  yamaguch 	const struct ixl_aq_link_status *status;
   5746   1.1  yamaguch 	const struct ixl_phy_type *itype;
   5747   1.1  yamaguch 
   5748   1.1  yamaguch 	uint64_t ifm_active = IFM_ETHER;
   5749   1.1  yamaguch 	uint64_t ifm_status = IFM_AVALID;
   5750   1.1  yamaguch 	int link_state = LINK_STATE_DOWN;
   5751   1.1  yamaguch 	uint64_t baudrate = 0;
   5752   1.1  yamaguch 
   5753   1.1  yamaguch 	status = (const struct ixl_aq_link_status *)iaq->iaq_param;
   5754  1.31  yamaguch 	if (!ISSET(status->link_info, IXL_AQ_LINK_UP_FUNCTION)) {
   5755  1.31  yamaguch 		ifm_active |= IFM_NONE;
   5756   1.1  yamaguch 		goto done;
   5757  1.31  yamaguch 	}
   5758   1.1  yamaguch 
   5759   1.1  yamaguch 	ifm_active |= IFM_FDX;
   5760   1.1  yamaguch 	ifm_status |= IFM_ACTIVE;
   5761   1.1  yamaguch 	link_state = LINK_STATE_UP;
   5762   1.1  yamaguch 
   5763   1.1  yamaguch 	itype = ixl_search_phy_type(status->phy_type);
   5764   1.1  yamaguch 	if (itype != NULL)
   5765   1.1  yamaguch 		ifm_active |= itype->ifm_type;
   5766   1.1  yamaguch 
   5767   1.1  yamaguch 	if (ISSET(status->an_info, IXL_AQ_LINK_PAUSE_TX))
   5768   1.1  yamaguch 		ifm_active |= IFM_ETH_TXPAUSE;
   5769   1.1  yamaguch 	if (ISSET(status->an_info, IXL_AQ_LINK_PAUSE_RX))
   5770   1.1  yamaguch 		ifm_active |= IFM_ETH_RXPAUSE;
   5771   1.1  yamaguch 
   5772   1.1  yamaguch 	baudrate = ixl_search_link_speed(status->link_speed);
   5773   1.1  yamaguch 
   5774   1.1  yamaguch done:
   5775  1.62  yamaguch 	/* sc->sc_cfg_lock held expect during attach */
   5776   1.1  yamaguch 	sc->sc_media_active = ifm_active;
   5777   1.1  yamaguch 	sc->sc_media_status = ifm_status;
   5778   1.1  yamaguch 
   5779   1.1  yamaguch 	sc->sc_ec.ec_if.if_baudrate = baudrate;
   5780   1.1  yamaguch 
   5781   1.1  yamaguch 	return link_state;
   5782   1.1  yamaguch }
   5783   1.1  yamaguch 
   5784   1.1  yamaguch static int
   5785   1.1  yamaguch ixl_establish_intx(struct ixl_softc *sc)
   5786   1.1  yamaguch {
   5787   1.1  yamaguch 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
   5788   1.1  yamaguch 	pci_intr_handle_t *intr;
   5789   1.1  yamaguch 	char xnamebuf[32];
   5790   1.1  yamaguch 	char intrbuf[PCI_INTRSTR_LEN];
   5791   1.1  yamaguch 	char const *intrstr;
   5792   1.1  yamaguch 
   5793   1.1  yamaguch 	KASSERT(sc->sc_nintrs == 1);
   5794   1.1  yamaguch 
   5795   1.1  yamaguch 	intr = &sc->sc_ihp[0];
   5796   1.1  yamaguch 
   5797   1.1  yamaguch 	intrstr = pci_intr_string(pc, *intr, intrbuf, sizeof(intrbuf));
   5798   1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s:legacy",
   5799   1.1  yamaguch 	    device_xname(sc->sc_dev));
   5800   1.1  yamaguch 
   5801   1.1  yamaguch 	sc->sc_ihs[0] = pci_intr_establish_xname(pc, *intr, IPL_NET, ixl_intr,
   5802   1.1  yamaguch 	    sc, xnamebuf);
   5803   1.1  yamaguch 
   5804   1.1  yamaguch 	if (sc->sc_ihs[0] == NULL) {
   5805   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   5806   1.1  yamaguch 		    "unable to establish interrupt at %s\n", intrstr);
   5807   1.1  yamaguch 		return -1;
   5808   1.1  yamaguch 	}
   5809   1.1  yamaguch 
   5810   1.1  yamaguch 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   5811   1.1  yamaguch 	return 0;
   5812   1.1  yamaguch }
   5813   1.1  yamaguch 
   5814   1.1  yamaguch static int
   5815   1.1  yamaguch ixl_establish_msix(struct ixl_softc *sc)
   5816   1.1  yamaguch {
   5817   1.1  yamaguch 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
   5818  1.22  yamaguch 	kcpuset_t *affinity;
   5819   1.1  yamaguch 	unsigned int vector = 0;
   5820   1.1  yamaguch 	unsigned int i;
   5821  1.22  yamaguch 	int affinity_to, r;
   5822   1.1  yamaguch 	char xnamebuf[32];
   5823   1.1  yamaguch 	char intrbuf[PCI_INTRSTR_LEN];
   5824   1.1  yamaguch 	char const *intrstr;
   5825   1.1  yamaguch 
   5826  1.22  yamaguch 	kcpuset_create(&affinity, false);
   5827  1.22  yamaguch 
   5828   1.1  yamaguch 	/* the "other" intr is mapped to vector 0 */
   5829   1.1  yamaguch 	vector = 0;
   5830   1.1  yamaguch 	intrstr = pci_intr_string(pc, sc->sc_ihp[vector],
   5831   1.1  yamaguch 	    intrbuf, sizeof(intrbuf));
   5832   1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s others",
   5833   1.1  yamaguch 	    device_xname(sc->sc_dev));
   5834   1.1  yamaguch 	sc->sc_ihs[vector] = pci_intr_establish_xname(pc,
   5835   1.1  yamaguch 	    sc->sc_ihp[vector], IPL_NET, ixl_other_intr,
   5836   1.1  yamaguch 	    sc, xnamebuf);
   5837   1.1  yamaguch 	if (sc->sc_ihs[vector] == NULL) {
   5838   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   5839   1.1  yamaguch 		    "unable to establish interrupt at %s\n", intrstr);
   5840   1.1  yamaguch 		goto fail;
   5841   1.1  yamaguch 	}
   5842  1.22  yamaguch 
   5843  1.22  yamaguch 	aprint_normal_dev(sc->sc_dev, "other interrupt at %s", intrstr);
   5844  1.22  yamaguch 
   5845  1.22  yamaguch 	affinity_to = ncpu > (int)sc->sc_nqueue_pairs_max ? 1 : 0;
   5846  1.22  yamaguch 	affinity_to = (affinity_to + sc->sc_nqueue_pairs_max) % ncpu;
   5847  1.22  yamaguch 
   5848  1.22  yamaguch 	kcpuset_zero(affinity);
   5849  1.22  yamaguch 	kcpuset_set(affinity, affinity_to);
   5850  1.22  yamaguch 	r = interrupt_distribute(sc->sc_ihs[vector], affinity, NULL);
   5851  1.22  yamaguch 	if (r == 0) {
   5852  1.22  yamaguch 		aprint_normal(", affinity to %u", affinity_to);
   5853  1.22  yamaguch 	}
   5854  1.22  yamaguch 	aprint_normal("\n");
   5855   1.1  yamaguch 	vector++;
   5856   1.1  yamaguch 
   5857   1.1  yamaguch 	sc->sc_msix_vector_queue = vector;
   5858  1.22  yamaguch 	affinity_to = ncpu > (int)sc->sc_nqueue_pairs_max ? 1 : 0;
   5859   1.1  yamaguch 
   5860   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   5861   1.1  yamaguch 		intrstr = pci_intr_string(pc, sc->sc_ihp[vector],
   5862   1.1  yamaguch 		    intrbuf, sizeof(intrbuf));
   5863   1.1  yamaguch 		snprintf(xnamebuf, sizeof(xnamebuf), "%s TXRX%d",
   5864   1.1  yamaguch 		    device_xname(sc->sc_dev), i);
   5865   1.1  yamaguch 
   5866   1.1  yamaguch 		sc->sc_ihs[vector] = pci_intr_establish_xname(pc,
   5867   1.1  yamaguch 		    sc->sc_ihp[vector], IPL_NET, ixl_queue_intr,
   5868   1.1  yamaguch 		    (void *)&sc->sc_qps[i], xnamebuf);
   5869   1.1  yamaguch 
   5870   1.1  yamaguch 		if (sc->sc_ihs[vector] == NULL) {
   5871   1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   5872   1.1  yamaguch 			    "unable to establish interrupt at %s\n", intrstr);
   5873   1.1  yamaguch 			goto fail;
   5874   1.1  yamaguch 		}
   5875  1.22  yamaguch 
   5876  1.22  yamaguch 		aprint_normal_dev(sc->sc_dev,
   5877  1.72  yamaguch 		    "for TXRX%d interrupt at %s", i, intrstr);
   5878  1.22  yamaguch 
   5879  1.22  yamaguch 		kcpuset_zero(affinity);
   5880  1.22  yamaguch 		kcpuset_set(affinity, affinity_to);
   5881  1.22  yamaguch 		r = interrupt_distribute(sc->sc_ihs[vector], affinity, NULL);
   5882  1.22  yamaguch 		if (r == 0) {
   5883  1.22  yamaguch 			aprint_normal(", affinity to %u", affinity_to);
   5884  1.22  yamaguch 			affinity_to = (affinity_to + 1) % ncpu;
   5885  1.22  yamaguch 		}
   5886  1.22  yamaguch 		aprint_normal("\n");
   5887   1.1  yamaguch 		vector++;
   5888   1.1  yamaguch 	}
   5889   1.1  yamaguch 
   5890  1.22  yamaguch 	kcpuset_destroy(affinity);
   5891  1.22  yamaguch 
   5892   1.1  yamaguch 	return 0;
   5893   1.1  yamaguch fail:
   5894   1.1  yamaguch 	for (i = 0; i < vector; i++) {
   5895   1.1  yamaguch 		pci_intr_disestablish(pc, sc->sc_ihs[i]);
   5896   1.1  yamaguch 	}
   5897   1.1  yamaguch 
   5898   1.1  yamaguch 	sc->sc_msix_vector_queue = 0;
   5899   1.1  yamaguch 	sc->sc_msix_vector_queue = 0;
   5900  1.22  yamaguch 	kcpuset_destroy(affinity);
   5901   1.1  yamaguch 
   5902   1.1  yamaguch 	return -1;
   5903   1.1  yamaguch }
   5904   1.1  yamaguch 
   5905   1.1  yamaguch static void
   5906   1.1  yamaguch ixl_config_queue_intr(struct ixl_softc *sc)
   5907   1.1  yamaguch {
   5908   1.1  yamaguch 	unsigned int i, vector;
   5909   1.1  yamaguch 
   5910   1.1  yamaguch 	if (sc->sc_intrtype == PCI_INTR_TYPE_MSIX) {
   5911   1.1  yamaguch 		vector = sc->sc_msix_vector_queue;
   5912   1.1  yamaguch 	} else {
   5913   1.1  yamaguch 		vector = I40E_INTR_NOTX_INTR;
   5914   1.1  yamaguch 
   5915   1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLST0,
   5916   1.1  yamaguch 		    (I40E_INTR_NOTX_QUEUE <<
   5917   1.1  yamaguch 		     I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT) |
   5918   1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   5919   1.1  yamaguch 		     I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
   5920   1.1  yamaguch 	}
   5921   1.1  yamaguch 
   5922   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   5923   1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_DYN_CTLN(i), 0);
   5924   1.1  yamaguch 		ixl_flush(sc);
   5925   1.1  yamaguch 
   5926   1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLSTN(i),
   5927   1.1  yamaguch 		    ((i) << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT) |
   5928   1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   5929   1.1  yamaguch 		     I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
   5930   1.1  yamaguch 
   5931   1.1  yamaguch 		ixl_wr(sc, I40E_QINT_RQCTL(i),
   5932   1.1  yamaguch 		    (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
   5933   1.1  yamaguch 		    (I40E_ITR_INDEX_RX <<
   5934   1.1  yamaguch 		     I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
   5935   1.1  yamaguch 		    (I40E_INTR_NOTX_RX_QUEUE <<
   5936   1.1  yamaguch 		     I40E_QINT_RQCTL_MSIX0_INDX_SHIFT) |
   5937   1.1  yamaguch 		    (i << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
   5938   1.1  yamaguch 		    (I40E_QUEUE_TYPE_TX <<
   5939   1.1  yamaguch 		     I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
   5940   1.1  yamaguch 		    I40E_QINT_RQCTL_CAUSE_ENA_MASK);
   5941   1.1  yamaguch 
   5942   1.1  yamaguch 		ixl_wr(sc, I40E_QINT_TQCTL(i),
   5943   1.1  yamaguch 		    (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
   5944   1.1  yamaguch 		    (I40E_ITR_INDEX_TX <<
   5945   1.1  yamaguch 		     I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
   5946   1.1  yamaguch 		    (I40E_INTR_NOTX_TX_QUEUE <<
   5947   1.1  yamaguch 		     I40E_QINT_TQCTL_MSIX0_INDX_SHIFT) |
   5948   1.1  yamaguch 		    (I40E_QUEUE_TYPE_EOL <<
   5949   1.1  yamaguch 		     I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
   5950   1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   5951   1.1  yamaguch 		     I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT) |
   5952   1.1  yamaguch 		     I40E_QINT_TQCTL_CAUSE_ENA_MASK);
   5953   1.1  yamaguch 
   5954  1.64  yamaguch 		if (sc->sc_intrtype == PCI_INTR_TYPE_MSIX) {
   5955  1.64  yamaguch 			ixl_wr(sc, I40E_PFINT_ITRN(I40E_ITR_INDEX_RX, i),
   5956  1.64  yamaguch 			    sc->sc_itr_rx);
   5957  1.64  yamaguch 			ixl_wr(sc, I40E_PFINT_ITRN(I40E_ITR_INDEX_TX, i),
   5958  1.64  yamaguch 			    sc->sc_itr_tx);
   5959   1.1  yamaguch 			vector++;
   5960  1.64  yamaguch 		}
   5961   1.1  yamaguch 	}
   5962   1.1  yamaguch 	ixl_flush(sc);
   5963   1.1  yamaguch 
   5964  1.64  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_RX), sc->sc_itr_rx);
   5965  1.64  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_TX), sc->sc_itr_tx);
   5966   1.1  yamaguch 	ixl_flush(sc);
   5967   1.1  yamaguch }
   5968   1.1  yamaguch 
   5969   1.1  yamaguch static void
   5970   1.1  yamaguch ixl_config_other_intr(struct ixl_softc *sc)
   5971   1.1  yamaguch {
   5972   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, 0);
   5973   1.1  yamaguch 	(void)ixl_rd(sc, I40E_PFINT_ICR0);
   5974   1.1  yamaguch 
   5975   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA,
   5976   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
   5977   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_GRST_MASK |
   5978   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_ADMINQ_MASK |
   5979   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
   5980   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
   5981   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_VFLR_MASK |
   5982   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK |
   5983   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
   5984   1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_LINK_STAT_CHANGE_MASK);
   5985   1.1  yamaguch 
   5986   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_LNKLST0, 0x7FF);
   5987   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_OTHER), 0);
   5988   1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_STAT_CTL0,
   5989   1.1  yamaguch 	    (I40E_ITR_INDEX_OTHER <<
   5990   1.1  yamaguch 	     I40E_PFINT_STAT_CTL0_OTHER_ITR_INDX_SHIFT));
   5991   1.1  yamaguch 	ixl_flush(sc);
   5992   1.1  yamaguch }
   5993   1.1  yamaguch 
   5994   1.1  yamaguch static int
   5995   1.1  yamaguch ixl_setup_interrupts(struct ixl_softc *sc)
   5996   1.1  yamaguch {
   5997   1.1  yamaguch 	struct pci_attach_args *pa = &sc->sc_pa;
   5998   1.1  yamaguch 	pci_intr_type_t max_type, intr_type;
   5999   1.1  yamaguch 	int counts[PCI_INTR_TYPE_SIZE];
   6000   1.1  yamaguch 	int error;
   6001   1.1  yamaguch 	unsigned int i;
   6002  1.19  yamaguch 	bool retry;
   6003   1.1  yamaguch 
   6004   1.1  yamaguch 	memset(counts, 0, sizeof(counts));
   6005   1.1  yamaguch 	max_type = PCI_INTR_TYPE_MSIX;
   6006   1.1  yamaguch 	/* QPs + other interrupt */
   6007   1.1  yamaguch 	counts[PCI_INTR_TYPE_MSIX] = sc->sc_nqueue_pairs_max + 1;
   6008   1.1  yamaguch 	counts[PCI_INTR_TYPE_INTX] = 1;
   6009   1.1  yamaguch 
   6010  1.19  yamaguch 	if (ixl_param_nomsix)
   6011   1.1  yamaguch 		counts[PCI_INTR_TYPE_MSIX] = 0;
   6012   1.1  yamaguch 
   6013   1.1  yamaguch 	do {
   6014   1.1  yamaguch 		retry = false;
   6015   1.1  yamaguch 		error = pci_intr_alloc(pa, &sc->sc_ihp, counts, max_type);
   6016   1.1  yamaguch 		if (error != 0) {
   6017   1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   6018   1.1  yamaguch 			    "couldn't map interrupt\n");
   6019   1.1  yamaguch 			break;
   6020   1.1  yamaguch 		}
   6021  1.27  yamaguch 
   6022  1.27  yamaguch 		intr_type = pci_intr_type(pa->pa_pc, sc->sc_ihp[0]);
   6023  1.27  yamaguch 		sc->sc_nintrs = counts[intr_type];
   6024  1.27  yamaguch 		KASSERT(sc->sc_nintrs > 0);
   6025  1.27  yamaguch 
   6026   1.1  yamaguch 		for (i = 0; i < sc->sc_nintrs; i++) {
   6027   1.1  yamaguch 			pci_intr_setattr(pa->pa_pc, &sc->sc_ihp[i],
   6028   1.1  yamaguch 			    PCI_INTR_MPSAFE, true);
   6029   1.1  yamaguch 		}
   6030   1.1  yamaguch 
   6031   1.1  yamaguch 		sc->sc_ihs = kmem_alloc(sizeof(sc->sc_ihs[0]) * sc->sc_nintrs,
   6032   1.1  yamaguch 		    KM_SLEEP);
   6033   1.1  yamaguch 
   6034   1.1  yamaguch 		if (intr_type == PCI_INTR_TYPE_MSIX) {
   6035   1.1  yamaguch 			error = ixl_establish_msix(sc);
   6036   1.1  yamaguch 			if (error) {
   6037   1.1  yamaguch 				counts[PCI_INTR_TYPE_MSIX] = 0;
   6038   1.1  yamaguch 				retry = true;
   6039   1.1  yamaguch 			}
   6040   1.1  yamaguch 		} else if (intr_type == PCI_INTR_TYPE_INTX) {
   6041   1.1  yamaguch 			error = ixl_establish_intx(sc);
   6042   1.1  yamaguch 		} else {
   6043   1.1  yamaguch 			error = -1;
   6044   1.1  yamaguch 		}
   6045   1.1  yamaguch 
   6046   1.1  yamaguch 		if (error) {
   6047   1.1  yamaguch 			kmem_free(sc->sc_ihs,
   6048   1.1  yamaguch 			    sizeof(sc->sc_ihs[0]) * sc->sc_nintrs);
   6049   1.1  yamaguch 			pci_intr_release(pa->pa_pc, sc->sc_ihp, sc->sc_nintrs);
   6050   1.1  yamaguch 		} else {
   6051   1.1  yamaguch 			sc->sc_intrtype = intr_type;
   6052   1.1  yamaguch 		}
   6053   1.1  yamaguch 	} while (retry);
   6054   1.1  yamaguch 
   6055   1.1  yamaguch 	return error;
   6056   1.1  yamaguch }
   6057   1.1  yamaguch 
   6058   1.1  yamaguch static void
   6059   1.1  yamaguch ixl_teardown_interrupts(struct ixl_softc *sc)
   6060   1.1  yamaguch {
   6061   1.1  yamaguch 	struct pci_attach_args *pa = &sc->sc_pa;
   6062   1.1  yamaguch 	unsigned int i;
   6063   1.1  yamaguch 
   6064   1.1  yamaguch 	for (i = 0; i < sc->sc_nintrs; i++) {
   6065   1.1  yamaguch 		pci_intr_disestablish(pa->pa_pc, sc->sc_ihs[i]);
   6066   1.1  yamaguch 	}
   6067   1.1  yamaguch 
   6068   1.1  yamaguch 	pci_intr_release(pa->pa_pc, sc->sc_ihp, sc->sc_nintrs);
   6069   1.1  yamaguch 
   6070   1.1  yamaguch 	kmem_free(sc->sc_ihs, sizeof(sc->sc_ihs[0]) * sc->sc_nintrs);
   6071   1.1  yamaguch 	sc->sc_ihs = NULL;
   6072   1.1  yamaguch 	sc->sc_nintrs = 0;
   6073   1.1  yamaguch }
   6074   1.1  yamaguch 
   6075   1.1  yamaguch static int
   6076   1.1  yamaguch ixl_setup_stats(struct ixl_softc *sc)
   6077   1.1  yamaguch {
   6078   1.1  yamaguch 	struct ixl_queue_pair *qp;
   6079   1.1  yamaguch 	struct ixl_tx_ring *txr;
   6080   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   6081  1.17  yamaguch 	struct ixl_stats_counters *isc;
   6082   1.1  yamaguch 	unsigned int i;
   6083   1.1  yamaguch 
   6084   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   6085   1.1  yamaguch 		qp = &sc->sc_qps[i];
   6086   1.1  yamaguch 		txr = qp->qp_txr;
   6087   1.1  yamaguch 		rxr = qp->qp_rxr;
   6088   1.1  yamaguch 
   6089   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defragged, EVCNT_TYPE_MISC,
   6090   1.1  yamaguch 		    NULL, qp->qp_name, "m_defrag successed");
   6091   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defrag_failed, EVCNT_TYPE_MISC,
   6092   1.1  yamaguch 		    NULL, qp->qp_name, "m_defrag_failed");
   6093   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_pcqdrop, EVCNT_TYPE_MISC,
   6094   1.1  yamaguch 		    NULL, qp->qp_name, "Dropped in pcq");
   6095   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_transmitdef, EVCNT_TYPE_MISC,
   6096   1.1  yamaguch 		    NULL, qp->qp_name, "Deferred transmit");
   6097   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_intr, EVCNT_TYPE_INTR,
   6098   1.1  yamaguch 		    NULL, qp->qp_name, "Interrupt on queue");
   6099   1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defer, EVCNT_TYPE_MISC,
   6100   1.1  yamaguch 		    NULL, qp->qp_name, "Handled queue in softint/workqueue");
   6101   1.1  yamaguch 
   6102   1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mgethdr_failed, EVCNT_TYPE_MISC,
   6103   1.1  yamaguch 		    NULL, qp->qp_name, "MGETHDR failed");
   6104   1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mgetcl_failed, EVCNT_TYPE_MISC,
   6105   1.1  yamaguch 		    NULL, qp->qp_name, "MCLGET failed");
   6106   1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mbuf_load_failed,
   6107   1.1  yamaguch 		    EVCNT_TYPE_MISC, NULL, qp->qp_name,
   6108   1.1  yamaguch 		    "bus_dmamap_load_mbuf failed");
   6109   1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_intr, EVCNT_TYPE_INTR,
   6110   1.1  yamaguch 		    NULL, qp->qp_name, "Interrupt on queue");
   6111   1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_defer, EVCNT_TYPE_MISC,
   6112   1.1  yamaguch 		    NULL, qp->qp_name, "Handled queue in softint/workqueue");
   6113   1.1  yamaguch 	}
   6114   1.1  yamaguch 
   6115   1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_atq, EVCNT_TYPE_INTR,
   6116   1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Interrupt for other events");
   6117   1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_link, EVCNT_TYPE_MISC,
   6118   1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Link status event");
   6119   1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_ecc_err, EVCNT_TYPE_MISC,
   6120   1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "ECC error");
   6121   1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_pci_exception, EVCNT_TYPE_MISC,
   6122   1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "PCI exception");
   6123   1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_crit_err, EVCNT_TYPE_MISC,
   6124   1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Critical error");
   6125   1.1  yamaguch 
   6126  1.17  yamaguch 	isc = &sc->sc_stats_counters;
   6127  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_crc_errors, EVCNT_TYPE_MISC,
   6128  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "CRC errors");
   6129  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_illegal_bytes, EVCNT_TYPE_MISC,
   6130  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Illegal bytes");
   6131  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_mac_local_faults, EVCNT_TYPE_MISC,
   6132  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Mac local faults");
   6133  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_mac_remote_faults, EVCNT_TYPE_MISC,
   6134  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Mac remote faults");
   6135  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_link_xon_rx, EVCNT_TYPE_MISC,
   6136  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx xon");
   6137  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_link_xon_tx, EVCNT_TYPE_MISC,
   6138  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx xon");
   6139  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_link_xoff_rx, EVCNT_TYPE_MISC,
   6140  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx xoff");
   6141  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_link_xoff_tx, EVCNT_TYPE_MISC,
   6142  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx xoff");
   6143  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_fragments, EVCNT_TYPE_MISC,
   6144  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx fragments");
   6145  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_jabber, EVCNT_TYPE_MISC,
   6146  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx jabber");
   6147  1.17  yamaguch 
   6148  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_64, EVCNT_TYPE_MISC,
   6149  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 64");
   6150  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_127, EVCNT_TYPE_MISC,
   6151  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 127");
   6152  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_255, EVCNT_TYPE_MISC,
   6153  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 255");
   6154  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_511, EVCNT_TYPE_MISC,
   6155  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 511");
   6156  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_1023, EVCNT_TYPE_MISC,
   6157  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 1023");
   6158  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_1522, EVCNT_TYPE_MISC,
   6159  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx size 1522");
   6160  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_size_big, EVCNT_TYPE_MISC,
   6161  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx jumbo packets");
   6162  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_undersize, EVCNT_TYPE_MISC,
   6163  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx under size");
   6164  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_oversize, EVCNT_TYPE_MISC,
   6165  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx over size");
   6166  1.17  yamaguch 
   6167  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_bytes, EVCNT_TYPE_MISC,
   6168  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx bytes / port");
   6169  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_discards, EVCNT_TYPE_MISC,
   6170  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx discards / port");
   6171  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_unicast, EVCNT_TYPE_MISC,
   6172  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx unicast / port");
   6173  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_multicast, EVCNT_TYPE_MISC,
   6174  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx multicast / port");
   6175  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_rx_broadcast, EVCNT_TYPE_MISC,
   6176  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx broadcast / port");
   6177  1.17  yamaguch 
   6178  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_rx_bytes, EVCNT_TYPE_MISC,
   6179  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx bytes / vsi");
   6180  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_rx_discards, EVCNT_TYPE_MISC,
   6181  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx discard / vsi");
   6182  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_rx_unicast, EVCNT_TYPE_MISC,
   6183  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx unicast / vsi");
   6184  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_rx_multicast, EVCNT_TYPE_MISC,
   6185  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx multicast / vsi");
   6186  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_rx_broadcast, EVCNT_TYPE_MISC,
   6187  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Rx broadcast / vsi");
   6188  1.18  yamaguch 
   6189  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_64, EVCNT_TYPE_MISC,
   6190  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 64");
   6191  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_127, EVCNT_TYPE_MISC,
   6192  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 127");
   6193  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_255, EVCNT_TYPE_MISC,
   6194  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 255");
   6195  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_511, EVCNT_TYPE_MISC,
   6196  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 511");
   6197  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_1023, EVCNT_TYPE_MISC,
   6198  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 1023");
   6199  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_1522, EVCNT_TYPE_MISC,
   6200  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx size 1522");
   6201  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_size_big, EVCNT_TYPE_MISC,
   6202  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx jumbo packets");
   6203  1.17  yamaguch 
   6204  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_bytes, EVCNT_TYPE_MISC,
   6205  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx bytes / port");
   6206  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_dropped_link_down, EVCNT_TYPE_MISC,
   6207  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev),
   6208  1.17  yamaguch 	    "Tx dropped due to link down / port");
   6209  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_unicast, EVCNT_TYPE_MISC,
   6210  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx unicast / port");
   6211  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_multicast, EVCNT_TYPE_MISC,
   6212  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx multicast / port");
   6213  1.17  yamaguch 	evcnt_attach_dynamic(&isc->isc_tx_broadcast, EVCNT_TYPE_MISC,
   6214  1.17  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx broadcast / port");
   6215  1.17  yamaguch 
   6216  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_tx_bytes, EVCNT_TYPE_MISC,
   6217  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx bytes / vsi");
   6218  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_tx_errors, EVCNT_TYPE_MISC,
   6219  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx errors / vsi");
   6220  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_tx_unicast, EVCNT_TYPE_MISC,
   6221  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx unicast / vsi");
   6222  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_tx_multicast, EVCNT_TYPE_MISC,
   6223  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx multicast / vsi");
   6224  1.18  yamaguch 	evcnt_attach_dynamic(&isc->isc_vsi_tx_broadcast, EVCNT_TYPE_MISC,
   6225  1.18  yamaguch 	    NULL, device_xname(sc->sc_dev), "Tx broadcast / vsi");
   6226  1.18  yamaguch 
   6227  1.19  yamaguch 	sc->sc_stats_intval = ixl_param_stats_interval;
   6228  1.17  yamaguch 	callout_init(&sc->sc_stats_callout, CALLOUT_MPSAFE);
   6229  1.17  yamaguch 	callout_setfunc(&sc->sc_stats_callout, ixl_stats_callout, sc);
   6230  1.17  yamaguch 	ixl_work_set(&sc->sc_stats_task, ixl_stats_update, sc);
   6231  1.17  yamaguch 
   6232   1.1  yamaguch 	return 0;
   6233   1.1  yamaguch }
   6234   1.1  yamaguch 
   6235   1.1  yamaguch static void
   6236   1.1  yamaguch ixl_teardown_stats(struct ixl_softc *sc)
   6237   1.1  yamaguch {
   6238   1.1  yamaguch 	struct ixl_tx_ring *txr;
   6239   1.1  yamaguch 	struct ixl_rx_ring *rxr;
   6240  1.17  yamaguch 	struct ixl_stats_counters *isc;
   6241   1.1  yamaguch 	unsigned int i;
   6242   1.1  yamaguch 
   6243   1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   6244   1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   6245   1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   6246   1.1  yamaguch 
   6247   1.1  yamaguch 		evcnt_detach(&txr->txr_defragged);
   6248   1.1  yamaguch 		evcnt_detach(&txr->txr_defrag_failed);
   6249   1.1  yamaguch 		evcnt_detach(&txr->txr_pcqdrop);
   6250   1.1  yamaguch 		evcnt_detach(&txr->txr_transmitdef);
   6251   1.1  yamaguch 		evcnt_detach(&txr->txr_intr);
   6252   1.1  yamaguch 		evcnt_detach(&txr->txr_defer);
   6253   1.1  yamaguch 
   6254   1.1  yamaguch 		evcnt_detach(&rxr->rxr_mgethdr_failed);
   6255   1.1  yamaguch 		evcnt_detach(&rxr->rxr_mgetcl_failed);
   6256   1.1  yamaguch 		evcnt_detach(&rxr->rxr_mbuf_load_failed);
   6257   1.1  yamaguch 		evcnt_detach(&rxr->rxr_intr);
   6258   1.1  yamaguch 		evcnt_detach(&rxr->rxr_defer);
   6259   1.1  yamaguch 	}
   6260   1.1  yamaguch 
   6261  1.17  yamaguch 	isc = &sc->sc_stats_counters;
   6262  1.17  yamaguch 	evcnt_detach(&isc->isc_crc_errors);
   6263  1.17  yamaguch 	evcnt_detach(&isc->isc_illegal_bytes);
   6264  1.17  yamaguch 	evcnt_detach(&isc->isc_mac_local_faults);
   6265  1.17  yamaguch 	evcnt_detach(&isc->isc_mac_remote_faults);
   6266  1.17  yamaguch 	evcnt_detach(&isc->isc_link_xon_rx);
   6267  1.17  yamaguch 	evcnt_detach(&isc->isc_link_xon_tx);
   6268  1.17  yamaguch 	evcnt_detach(&isc->isc_link_xoff_rx);
   6269  1.17  yamaguch 	evcnt_detach(&isc->isc_link_xoff_tx);
   6270  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_fragments);
   6271  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_jabber);
   6272  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_bytes);
   6273  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_discards);
   6274  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_unicast);
   6275  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_multicast);
   6276  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_broadcast);
   6277  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_64);
   6278  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_127);
   6279  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_255);
   6280  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_511);
   6281  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_1023);
   6282  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_1522);
   6283  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_size_big);
   6284  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_undersize);
   6285  1.17  yamaguch 	evcnt_detach(&isc->isc_rx_oversize);
   6286  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_bytes);
   6287  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_dropped_link_down);
   6288  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_unicast);
   6289  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_multicast);
   6290  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_broadcast);
   6291  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_64);
   6292  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_127);
   6293  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_255);
   6294  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_511);
   6295  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_1023);
   6296  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_1522);
   6297  1.17  yamaguch 	evcnt_detach(&isc->isc_tx_size_big);
   6298  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_rx_discards);
   6299  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_rx_bytes);
   6300  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_rx_unicast);
   6301  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_rx_multicast);
   6302  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_rx_broadcast);
   6303  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_tx_errors);
   6304  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_tx_bytes);
   6305  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_tx_unicast);
   6306  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_tx_multicast);
   6307  1.18  yamaguch 	evcnt_detach(&isc->isc_vsi_tx_broadcast);
   6308  1.17  yamaguch 
   6309   1.1  yamaguch 	evcnt_detach(&sc->sc_event_atq);
   6310   1.1  yamaguch 	evcnt_detach(&sc->sc_event_link);
   6311   1.1  yamaguch 	evcnt_detach(&sc->sc_event_ecc_err);
   6312   1.1  yamaguch 	evcnt_detach(&sc->sc_event_pci_exception);
   6313   1.1  yamaguch 	evcnt_detach(&sc->sc_event_crit_err);
   6314  1.17  yamaguch 
   6315  1.17  yamaguch 	callout_destroy(&sc->sc_stats_callout);
   6316  1.17  yamaguch }
   6317  1.17  yamaguch 
   6318  1.17  yamaguch static void
   6319  1.17  yamaguch ixl_stats_callout(void *xsc)
   6320  1.17  yamaguch {
   6321  1.17  yamaguch 	struct ixl_softc *sc = xsc;
   6322  1.17  yamaguch 
   6323  1.17  yamaguch 	ixl_work_add(sc->sc_workq, &sc->sc_stats_task);
   6324  1.17  yamaguch 	callout_schedule(&sc->sc_stats_callout, mstohz(sc->sc_stats_intval));
   6325  1.17  yamaguch }
   6326  1.17  yamaguch 
   6327  1.17  yamaguch static uint64_t
   6328  1.17  yamaguch ixl_stat_delta(struct ixl_softc *sc, uint32_t reg_hi, uint32_t reg_lo,
   6329  1.17  yamaguch     uint64_t *offset, bool has_offset)
   6330  1.17  yamaguch {
   6331  1.17  yamaguch 	uint64_t value, delta;
   6332  1.17  yamaguch 	int bitwidth;
   6333  1.17  yamaguch 
   6334  1.17  yamaguch 	bitwidth = reg_hi == 0 ? 32 : 48;
   6335  1.17  yamaguch 
   6336  1.17  yamaguch 	value = ixl_rd(sc, reg_lo);
   6337  1.17  yamaguch 
   6338  1.17  yamaguch 	if (bitwidth > 32) {
   6339  1.17  yamaguch 		value |= ((uint64_t)ixl_rd(sc, reg_hi) << 32);
   6340  1.17  yamaguch 	}
   6341  1.17  yamaguch 
   6342  1.17  yamaguch 	if (__predict_true(has_offset)) {
   6343  1.17  yamaguch 		delta = value;
   6344  1.17  yamaguch 		if (value < *offset)
   6345  1.17  yamaguch 			delta += ((uint64_t)1 << bitwidth);
   6346  1.17  yamaguch 		delta -= *offset;
   6347  1.17  yamaguch 	} else {
   6348  1.17  yamaguch 		delta = 0;
   6349  1.17  yamaguch 	}
   6350  1.17  yamaguch 	atomic_swap_64(offset, value);
   6351  1.17  yamaguch 
   6352  1.17  yamaguch 	return delta;
   6353  1.17  yamaguch }
   6354  1.17  yamaguch 
   6355  1.17  yamaguch static void
   6356  1.17  yamaguch ixl_stats_update(void *xsc)
   6357  1.17  yamaguch {
   6358  1.17  yamaguch 	struct ixl_softc *sc = xsc;
   6359  1.17  yamaguch 	struct ixl_stats_counters *isc;
   6360  1.17  yamaguch 	uint64_t delta;
   6361  1.17  yamaguch 
   6362  1.17  yamaguch 	isc = &sc->sc_stats_counters;
   6363  1.17  yamaguch 
   6364  1.17  yamaguch 	/* errors */
   6365  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6366  1.17  yamaguch 	    0, I40E_GLPRT_CRCERRS(sc->sc_port),
   6367  1.17  yamaguch 	    &isc->isc_crc_errors_offset, isc->isc_has_offset);
   6368  1.17  yamaguch 	atomic_add_64(&isc->isc_crc_errors.ev_count, delta);
   6369  1.17  yamaguch 
   6370  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6371  1.17  yamaguch 	    0, I40E_GLPRT_ILLERRC(sc->sc_port),
   6372  1.17  yamaguch 	    &isc->isc_illegal_bytes_offset, isc->isc_has_offset);
   6373  1.17  yamaguch 	atomic_add_64(&isc->isc_illegal_bytes.ev_count, delta);
   6374  1.17  yamaguch 
   6375  1.17  yamaguch 	/* rx */
   6376  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6377  1.17  yamaguch 	    I40E_GLPRT_GORCH(sc->sc_port), I40E_GLPRT_GORCL(sc->sc_port),
   6378  1.17  yamaguch 	    &isc->isc_rx_bytes_offset, isc->isc_has_offset);
   6379  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_bytes.ev_count, delta);
   6380  1.17  yamaguch 
   6381  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6382  1.17  yamaguch 	    0, I40E_GLPRT_RDPC(sc->sc_port),
   6383  1.17  yamaguch 	    &isc->isc_rx_discards_offset, isc->isc_has_offset);
   6384  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_discards.ev_count, delta);
   6385  1.17  yamaguch 
   6386  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6387  1.17  yamaguch 	    I40E_GLPRT_UPRCH(sc->sc_port), I40E_GLPRT_UPRCL(sc->sc_port),
   6388  1.17  yamaguch 	    &isc->isc_rx_unicast_offset, isc->isc_has_offset);
   6389  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_unicast.ev_count, delta);
   6390  1.17  yamaguch 
   6391  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6392  1.17  yamaguch 	    I40E_GLPRT_MPRCH(sc->sc_port), I40E_GLPRT_MPRCL(sc->sc_port),
   6393  1.17  yamaguch 	    &isc->isc_rx_multicast_offset, isc->isc_has_offset);
   6394  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_multicast.ev_count, delta);
   6395  1.17  yamaguch 
   6396  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6397  1.17  yamaguch 	    I40E_GLPRT_BPRCH(sc->sc_port), I40E_GLPRT_BPRCL(sc->sc_port),
   6398  1.17  yamaguch 	    &isc->isc_rx_broadcast_offset, isc->isc_has_offset);
   6399  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_broadcast.ev_count, delta);
   6400  1.17  yamaguch 
   6401  1.17  yamaguch 	/* Packet size stats rx */
   6402  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6403  1.17  yamaguch 	    I40E_GLPRT_PRC64H(sc->sc_port), I40E_GLPRT_PRC64L(sc->sc_port),
   6404  1.17  yamaguch 	    &isc->isc_rx_size_64_offset, isc->isc_has_offset);
   6405  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_64.ev_count, delta);
   6406  1.17  yamaguch 
   6407  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6408  1.17  yamaguch 	    I40E_GLPRT_PRC127H(sc->sc_port), I40E_GLPRT_PRC127L(sc->sc_port),
   6409  1.17  yamaguch 	    &isc->isc_rx_size_127_offset, isc->isc_has_offset);
   6410  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_127.ev_count, delta);
   6411  1.17  yamaguch 
   6412  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6413  1.17  yamaguch 	    I40E_GLPRT_PRC255H(sc->sc_port), I40E_GLPRT_PRC255L(sc->sc_port),
   6414  1.17  yamaguch 	    &isc->isc_rx_size_255_offset, isc->isc_has_offset);
   6415  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_255.ev_count, delta);
   6416  1.17  yamaguch 
   6417  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6418  1.17  yamaguch 	    I40E_GLPRT_PRC511H(sc->sc_port), I40E_GLPRT_PRC511L(sc->sc_port),
   6419  1.17  yamaguch 	    &isc->isc_rx_size_511_offset, isc->isc_has_offset);
   6420  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_511.ev_count, delta);
   6421  1.17  yamaguch 
   6422  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6423  1.17  yamaguch 	    I40E_GLPRT_PRC1023H(sc->sc_port), I40E_GLPRT_PRC1023L(sc->sc_port),
   6424  1.17  yamaguch 	    &isc->isc_rx_size_1023_offset, isc->isc_has_offset);
   6425  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_1023.ev_count, delta);
   6426  1.17  yamaguch 
   6427  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6428  1.17  yamaguch 	    I40E_GLPRT_PRC1522H(sc->sc_port), I40E_GLPRT_PRC1522L(sc->sc_port),
   6429  1.17  yamaguch 	    &isc->isc_rx_size_1522_offset, isc->isc_has_offset);
   6430  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_1522.ev_count, delta);
   6431  1.17  yamaguch 
   6432  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6433  1.17  yamaguch 	    I40E_GLPRT_PRC9522H(sc->sc_port), I40E_GLPRT_PRC9522L(sc->sc_port),
   6434  1.17  yamaguch 	    &isc->isc_rx_size_big_offset, isc->isc_has_offset);
   6435  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_size_big.ev_count, delta);
   6436  1.17  yamaguch 
   6437  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6438  1.17  yamaguch 	    0, I40E_GLPRT_RUC(sc->sc_port),
   6439  1.17  yamaguch 	    &isc->isc_rx_undersize_offset, isc->isc_has_offset);
   6440  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_undersize.ev_count, delta);
   6441  1.17  yamaguch 
   6442  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6443  1.17  yamaguch 	    0, I40E_GLPRT_ROC(sc->sc_port),
   6444  1.17  yamaguch 	    &isc->isc_rx_oversize_offset, isc->isc_has_offset);
   6445  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_oversize.ev_count, delta);
   6446  1.17  yamaguch 
   6447  1.17  yamaguch 	/* tx */
   6448  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6449  1.17  yamaguch 	    I40E_GLPRT_GOTCH(sc->sc_port), I40E_GLPRT_GOTCL(sc->sc_port),
   6450  1.17  yamaguch 	    &isc->isc_tx_bytes_offset, isc->isc_has_offset);
   6451  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_bytes.ev_count, delta);
   6452  1.17  yamaguch 
   6453  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6454  1.17  yamaguch 	    0, I40E_GLPRT_TDOLD(sc->sc_port),
   6455  1.17  yamaguch 	    &isc->isc_tx_dropped_link_down_offset, isc->isc_has_offset);
   6456  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_dropped_link_down.ev_count, delta);
   6457  1.17  yamaguch 
   6458  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6459  1.17  yamaguch 	    I40E_GLPRT_UPTCH(sc->sc_port), I40E_GLPRT_UPTCL(sc->sc_port),
   6460  1.17  yamaguch 	    &isc->isc_tx_unicast_offset, isc->isc_has_offset);
   6461  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_unicast.ev_count, delta);
   6462  1.17  yamaguch 
   6463  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6464  1.17  yamaguch 	    I40E_GLPRT_MPTCH(sc->sc_port), I40E_GLPRT_MPTCL(sc->sc_port),
   6465  1.17  yamaguch 	    &isc->isc_tx_multicast_offset, isc->isc_has_offset);
   6466  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_multicast.ev_count, delta);
   6467  1.17  yamaguch 
   6468  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6469  1.17  yamaguch 	    I40E_GLPRT_BPTCH(sc->sc_port), I40E_GLPRT_BPTCL(sc->sc_port),
   6470  1.17  yamaguch 	    &isc->isc_tx_broadcast_offset, isc->isc_has_offset);
   6471  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_broadcast.ev_count, delta);
   6472  1.17  yamaguch 
   6473  1.17  yamaguch 	/* Packet size stats tx */
   6474  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6475  1.17  yamaguch 	    I40E_GLPRT_PTC64L(sc->sc_port), I40E_GLPRT_PTC64L(sc->sc_port),
   6476  1.17  yamaguch 	    &isc->isc_tx_size_64_offset, isc->isc_has_offset);
   6477  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_64.ev_count, delta);
   6478  1.17  yamaguch 
   6479  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6480  1.17  yamaguch 	    I40E_GLPRT_PTC127H(sc->sc_port), I40E_GLPRT_PTC127L(sc->sc_port),
   6481  1.17  yamaguch 	    &isc->isc_tx_size_127_offset, isc->isc_has_offset);
   6482  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_127.ev_count, delta);
   6483  1.17  yamaguch 
   6484  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6485  1.17  yamaguch 	    I40E_GLPRT_PTC255H(sc->sc_port), I40E_GLPRT_PTC255L(sc->sc_port),
   6486  1.17  yamaguch 	    &isc->isc_tx_size_255_offset, isc->isc_has_offset);
   6487  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_255.ev_count, delta);
   6488  1.17  yamaguch 
   6489  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6490  1.17  yamaguch 	    I40E_GLPRT_PTC511H(sc->sc_port), I40E_GLPRT_PTC511L(sc->sc_port),
   6491  1.17  yamaguch 	    &isc->isc_tx_size_511_offset, isc->isc_has_offset);
   6492  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_511.ev_count, delta);
   6493  1.17  yamaguch 
   6494  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6495  1.17  yamaguch 	    I40E_GLPRT_PTC1023H(sc->sc_port), I40E_GLPRT_PTC1023L(sc->sc_port),
   6496  1.17  yamaguch 	    &isc->isc_tx_size_1023_offset, isc->isc_has_offset);
   6497  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_1023.ev_count, delta);
   6498  1.17  yamaguch 
   6499  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6500  1.17  yamaguch 	    I40E_GLPRT_PTC1522H(sc->sc_port), I40E_GLPRT_PTC1522L(sc->sc_port),
   6501  1.17  yamaguch 	    &isc->isc_tx_size_1522_offset, isc->isc_has_offset);
   6502  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_1522.ev_count, delta);
   6503  1.17  yamaguch 
   6504  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6505  1.17  yamaguch 	    I40E_GLPRT_PTC9522H(sc->sc_port), I40E_GLPRT_PTC9522L(sc->sc_port),
   6506  1.17  yamaguch 	    &isc->isc_tx_size_big_offset, isc->isc_has_offset);
   6507  1.17  yamaguch 	atomic_add_64(&isc->isc_tx_size_big.ev_count, delta);
   6508  1.17  yamaguch 
   6509  1.17  yamaguch 	/* mac faults */
   6510  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6511  1.17  yamaguch 	    0, I40E_GLPRT_MLFC(sc->sc_port),
   6512  1.17  yamaguch 	    &isc->isc_mac_local_faults_offset, isc->isc_has_offset);
   6513  1.17  yamaguch 	atomic_add_64(&isc->isc_mac_local_faults.ev_count, delta);
   6514  1.17  yamaguch 
   6515  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6516  1.17  yamaguch 	    0, I40E_GLPRT_MRFC(sc->sc_port),
   6517  1.17  yamaguch 	    &isc->isc_mac_remote_faults_offset, isc->isc_has_offset);
   6518  1.17  yamaguch 	atomic_add_64(&isc->isc_mac_remote_faults.ev_count, delta);
   6519  1.17  yamaguch 
   6520  1.17  yamaguch 	/* Flow control (LFC) stats */
   6521  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6522  1.17  yamaguch 	    0, I40E_GLPRT_LXONRXC(sc->sc_port),
   6523  1.17  yamaguch 	    &isc->isc_link_xon_rx_offset, isc->isc_has_offset);
   6524  1.17  yamaguch 	atomic_add_64(&isc->isc_link_xon_rx.ev_count, delta);
   6525  1.17  yamaguch 
   6526  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6527  1.17  yamaguch 	    0, I40E_GLPRT_LXONTXC(sc->sc_port),
   6528  1.17  yamaguch 	    &isc->isc_link_xon_tx_offset, isc->isc_has_offset);
   6529  1.17  yamaguch 	atomic_add_64(&isc->isc_link_xon_tx.ev_count, delta);
   6530  1.17  yamaguch 
   6531  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6532  1.17  yamaguch 	    0, I40E_GLPRT_LXOFFRXC(sc->sc_port),
   6533  1.17  yamaguch 	    &isc->isc_link_xoff_rx_offset, isc->isc_has_offset);
   6534  1.17  yamaguch 	atomic_add_64(&isc->isc_link_xoff_rx.ev_count, delta);
   6535  1.17  yamaguch 
   6536  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6537  1.17  yamaguch 	    0, I40E_GLPRT_LXOFFTXC(sc->sc_port),
   6538  1.17  yamaguch 	    &isc->isc_link_xoff_tx_offset, isc->isc_has_offset);
   6539  1.17  yamaguch 	atomic_add_64(&isc->isc_link_xoff_tx.ev_count, delta);
   6540  1.17  yamaguch 
   6541  1.17  yamaguch 	/* fragments */
   6542  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6543  1.17  yamaguch 	    0, I40E_GLPRT_RFC(sc->sc_port),
   6544  1.17  yamaguch 	    &isc->isc_rx_fragments_offset, isc->isc_has_offset);
   6545  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_fragments.ev_count, delta);
   6546  1.17  yamaguch 
   6547  1.17  yamaguch 	delta = ixl_stat_delta(sc,
   6548  1.17  yamaguch 	    0, I40E_GLPRT_RJC(sc->sc_port),
   6549  1.17  yamaguch 	    &isc->isc_rx_jabber_offset, isc->isc_has_offset);
   6550  1.17  yamaguch 	atomic_add_64(&isc->isc_rx_jabber.ev_count, delta);
   6551  1.18  yamaguch 
   6552  1.18  yamaguch 	/* VSI rx counters */
   6553  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6554  1.18  yamaguch 	    0, I40E_GLV_RDPC(sc->sc_vsi_stat_counter_idx),
   6555  1.18  yamaguch 	    &isc->isc_vsi_rx_discards_offset, isc->isc_has_offset);
   6556  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_rx_discards.ev_count, delta);
   6557  1.18  yamaguch 
   6558  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6559  1.18  yamaguch 	    I40E_GLV_GORCH(sc->sc_vsi_stat_counter_idx),
   6560  1.18  yamaguch 	    I40E_GLV_GORCL(sc->sc_vsi_stat_counter_idx),
   6561  1.18  yamaguch 	    &isc->isc_vsi_rx_bytes_offset, isc->isc_has_offset);
   6562  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_rx_bytes.ev_count, delta);
   6563  1.18  yamaguch 
   6564  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6565  1.18  yamaguch 	    I40E_GLV_UPRCH(sc->sc_vsi_stat_counter_idx),
   6566  1.18  yamaguch 	    I40E_GLV_UPRCL(sc->sc_vsi_stat_counter_idx),
   6567  1.18  yamaguch 	    &isc->isc_vsi_rx_unicast_offset, isc->isc_has_offset);
   6568  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_rx_unicast.ev_count, delta);
   6569  1.18  yamaguch 
   6570  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6571  1.18  yamaguch 	    I40E_GLV_MPRCH(sc->sc_vsi_stat_counter_idx),
   6572  1.18  yamaguch 	    I40E_GLV_MPRCL(sc->sc_vsi_stat_counter_idx),
   6573  1.18  yamaguch 	    &isc->isc_vsi_rx_multicast_offset, isc->isc_has_offset);
   6574  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_rx_multicast.ev_count, delta);
   6575  1.18  yamaguch 
   6576  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6577  1.18  yamaguch 	    I40E_GLV_BPRCH(sc->sc_vsi_stat_counter_idx),
   6578  1.18  yamaguch 	    I40E_GLV_BPRCL(sc->sc_vsi_stat_counter_idx),
   6579  1.18  yamaguch 	    &isc->isc_vsi_rx_broadcast_offset, isc->isc_has_offset);
   6580  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_rx_broadcast.ev_count, delta);
   6581  1.18  yamaguch 
   6582  1.18  yamaguch 	/* VSI tx counters */
   6583  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6584  1.18  yamaguch 	    0, I40E_GLV_TEPC(sc->sc_vsi_stat_counter_idx),
   6585  1.18  yamaguch 	    &isc->isc_vsi_tx_errors_offset, isc->isc_has_offset);
   6586  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_tx_errors.ev_count, delta);
   6587  1.18  yamaguch 
   6588  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6589  1.18  yamaguch 	    I40E_GLV_GOTCH(sc->sc_vsi_stat_counter_idx),
   6590  1.18  yamaguch 	    I40E_GLV_GOTCL(sc->sc_vsi_stat_counter_idx),
   6591  1.18  yamaguch 	    &isc->isc_vsi_tx_bytes_offset, isc->isc_has_offset);
   6592  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_tx_bytes.ev_count, delta);
   6593  1.18  yamaguch 
   6594  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6595  1.18  yamaguch 	    I40E_GLV_UPTCH(sc->sc_vsi_stat_counter_idx),
   6596  1.18  yamaguch 	    I40E_GLV_UPTCL(sc->sc_vsi_stat_counter_idx),
   6597  1.18  yamaguch 	    &isc->isc_vsi_tx_unicast_offset, isc->isc_has_offset);
   6598  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_tx_unicast.ev_count, delta);
   6599  1.18  yamaguch 
   6600  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6601  1.18  yamaguch 	    I40E_GLV_MPTCH(sc->sc_vsi_stat_counter_idx),
   6602  1.18  yamaguch 	    I40E_GLV_MPTCL(sc->sc_vsi_stat_counter_idx),
   6603  1.18  yamaguch 	    &isc->isc_vsi_tx_multicast_offset, isc->isc_has_offset);
   6604  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_tx_multicast.ev_count, delta);
   6605  1.18  yamaguch 
   6606  1.18  yamaguch 	delta = ixl_stat_delta(sc,
   6607  1.18  yamaguch 	    I40E_GLV_BPTCH(sc->sc_vsi_stat_counter_idx),
   6608  1.18  yamaguch 	    I40E_GLV_BPTCL(sc->sc_vsi_stat_counter_idx),
   6609  1.18  yamaguch 	    &isc->isc_vsi_tx_broadcast_offset, isc->isc_has_offset);
   6610  1.18  yamaguch 	atomic_add_64(&isc->isc_vsi_tx_broadcast.ev_count, delta);
   6611   1.1  yamaguch }
   6612   1.1  yamaguch 
   6613   1.1  yamaguch static int
   6614   1.1  yamaguch ixl_setup_sysctls(struct ixl_softc *sc)
   6615   1.1  yamaguch {
   6616   1.1  yamaguch 	const char *devname;
   6617   1.1  yamaguch 	struct sysctllog **log;
   6618   1.1  yamaguch 	const struct sysctlnode *rnode, *rxnode, *txnode;
   6619   1.1  yamaguch 	int error;
   6620   1.1  yamaguch 
   6621   1.1  yamaguch 	log = &sc->sc_sysctllog;
   6622   1.1  yamaguch 	devname = device_xname(sc->sc_dev);
   6623   1.1  yamaguch 
   6624   1.1  yamaguch 	error = sysctl_createv(log, 0, NULL, &rnode,
   6625   1.1  yamaguch 	    0, CTLTYPE_NODE, devname,
   6626   1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings"),
   6627   1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   6628   1.1  yamaguch 	if (error)
   6629   1.1  yamaguch 		goto out;
   6630   1.1  yamaguch 
   6631   1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, NULL,
   6632   1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
   6633   1.1  yamaguch 	    SYSCTL_DESCR("Use workqueue for packet processing"),
   6634   1.1  yamaguch 	    NULL, 0, &sc->sc_txrx_workqueue, 0, CTL_CREATE, CTL_EOL);
   6635   1.1  yamaguch 	if (error)
   6636   1.1  yamaguch 		goto out;
   6637   1.1  yamaguch 
   6638  1.17  yamaguch 	error = sysctl_createv(log, 0, &rnode, NULL,
   6639  1.17  yamaguch 	    CTLFLAG_READONLY, CTLTYPE_INT, "stats_interval",
   6640  1.17  yamaguch 	    SYSCTL_DESCR("Statistics collection interval in milliseconds"),
   6641  1.17  yamaguch 	    NULL, 0, &sc->sc_stats_intval, 0, CTL_CREATE, CTL_EOL);
   6642  1.17  yamaguch 
   6643   1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, &rxnode,
   6644   1.1  yamaguch 	    0, CTLTYPE_NODE, "rx",
   6645   1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings for Rx"),
   6646   1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   6647   1.1  yamaguch 	if (error)
   6648   1.1  yamaguch 		goto out;
   6649   1.1  yamaguch 
   6650   1.1  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   6651  1.69  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "itr",
   6652  1.69  yamaguch 	    SYSCTL_DESCR("Interrupt Throttling"),
   6653  1.69  yamaguch 	    ixl_sysctl_itr_handler, 0,
   6654  1.69  yamaguch 	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
   6655  1.69  yamaguch 	if (error)
   6656  1.69  yamaguch 		goto out;
   6657  1.69  yamaguch 
   6658  1.69  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   6659  1.70  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "descriptor_num",
   6660  1.70  yamaguch 	    SYSCTL_DESCR("the number of rx descriptors"),
   6661  1.70  yamaguch 	    ixl_sysctl_ndescs_handler, 0,
   6662  1.70  yamaguch 	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
   6663  1.70  yamaguch 	if (error)
   6664  1.70  yamaguch 		goto out;
   6665  1.70  yamaguch 
   6666  1.70  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   6667   1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   6668   1.1  yamaguch 	    SYSCTL_DESCR("max number of Rx packets"
   6669   1.1  yamaguch 	    " to process for interrupt processing"),
   6670   1.1  yamaguch 	    NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   6671   1.1  yamaguch 	if (error)
   6672   1.1  yamaguch 		goto out;
   6673   1.1  yamaguch 
   6674   1.1  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   6675   1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   6676   1.1  yamaguch 	    SYSCTL_DESCR("max number of Rx packets"
   6677   1.1  yamaguch 	    " to process for deferred processing"),
   6678   1.1  yamaguch 	    NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
   6679   1.1  yamaguch 	if (error)
   6680   1.1  yamaguch 		goto out;
   6681   1.1  yamaguch 
   6682   1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, &txnode,
   6683   1.1  yamaguch 	    0, CTLTYPE_NODE, "tx",
   6684   1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings for Tx"),
   6685   1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   6686   1.1  yamaguch 	if (error)
   6687   1.1  yamaguch 		goto out;
   6688   1.1  yamaguch 
   6689   1.1  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   6690  1.69  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "itr",
   6691  1.69  yamaguch 	    SYSCTL_DESCR("Interrupt Throttling"),
   6692  1.69  yamaguch 	    ixl_sysctl_itr_handler, 0,
   6693  1.69  yamaguch 	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
   6694  1.69  yamaguch 	if (error)
   6695  1.69  yamaguch 		goto out;
   6696  1.69  yamaguch 
   6697  1.69  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   6698  1.70  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "descriptor_num",
   6699  1.70  yamaguch 	    SYSCTL_DESCR("the number of tx descriptors"),
   6700  1.70  yamaguch 	    ixl_sysctl_ndescs_handler, 0,
   6701  1.70  yamaguch 	    (void *)sc, 0, CTL_CREATE, CTL_EOL);
   6702  1.70  yamaguch 	if (error)
   6703  1.70  yamaguch 		goto out;
   6704  1.70  yamaguch 
   6705  1.70  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   6706   1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   6707   1.1  yamaguch 	    SYSCTL_DESCR("max number of Tx packets"
   6708   1.1  yamaguch 	    " to process for interrupt processing"),
   6709   1.1  yamaguch 	    NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   6710   1.1  yamaguch 	if (error)
   6711   1.1  yamaguch 		goto out;
   6712   1.1  yamaguch 
   6713   1.1  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   6714   1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   6715   1.1  yamaguch 	    SYSCTL_DESCR("max number of Tx packets"
   6716   1.1  yamaguch 	    " to process for deferred processing"),
   6717   1.1  yamaguch 	    NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
   6718   1.1  yamaguch 	if (error)
   6719   1.1  yamaguch 		goto out;
   6720   1.1  yamaguch 
   6721   1.1  yamaguch out:
   6722   1.1  yamaguch 	if (error) {
   6723   1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   6724   1.1  yamaguch 		    "unable to create sysctl node\n");
   6725   1.1  yamaguch 		sysctl_teardown(log);
   6726   1.1  yamaguch 	}
   6727   1.1  yamaguch 
   6728   1.1  yamaguch 	return error;
   6729   1.1  yamaguch }
   6730   1.1  yamaguch 
   6731   1.1  yamaguch static void
   6732   1.1  yamaguch ixl_teardown_sysctls(struct ixl_softc *sc)
   6733   1.1  yamaguch {
   6734   1.1  yamaguch 
   6735   1.1  yamaguch 	sysctl_teardown(&sc->sc_sysctllog);
   6736   1.1  yamaguch }
   6737   1.1  yamaguch 
   6738  1.69  yamaguch static bool
   6739  1.69  yamaguch ixl_sysctlnode_is_rx(struct sysctlnode *node)
   6740  1.69  yamaguch {
   6741  1.69  yamaguch 
   6742  1.69  yamaguch 	if (strstr(node->sysctl_parent->sysctl_name, "rx") != NULL)
   6743  1.69  yamaguch 		return true;
   6744  1.69  yamaguch 
   6745  1.69  yamaguch 	return false;
   6746  1.69  yamaguch }
   6747  1.69  yamaguch 
   6748  1.69  yamaguch static int
   6749  1.69  yamaguch ixl_sysctl_itr_handler(SYSCTLFN_ARGS)
   6750  1.69  yamaguch {
   6751  1.69  yamaguch 	struct sysctlnode node = *rnode;
   6752  1.69  yamaguch 	struct ixl_softc *sc = (struct ixl_softc *)node.sysctl_data;
   6753  1.69  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   6754  1.69  yamaguch 	uint32_t newitr, *itrptr;
   6755  1.69  yamaguch 	int error;
   6756  1.69  yamaguch 
   6757  1.69  yamaguch 	if (ixl_sysctlnode_is_rx(&node)) {
   6758  1.69  yamaguch 		itrptr = &sc->sc_itr_rx;
   6759  1.69  yamaguch 	} else {
   6760  1.69  yamaguch 		itrptr = &sc->sc_itr_tx;
   6761  1.69  yamaguch 	}
   6762  1.69  yamaguch 
   6763  1.69  yamaguch 	newitr = *itrptr;
   6764  1.69  yamaguch 	node.sysctl_data = &newitr;
   6765  1.69  yamaguch 	node.sysctl_size = sizeof(newitr);
   6766  1.69  yamaguch 
   6767  1.69  yamaguch 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6768  1.69  yamaguch 
   6769  1.69  yamaguch 	if (error || newp == NULL)
   6770  1.69  yamaguch 		return error;
   6771  1.69  yamaguch 
   6772  1.69  yamaguch 	/* ITRs are applied in ixl_init() for simple implementaion */
   6773  1.69  yamaguch 	if (ISSET(ifp->if_flags, IFF_RUNNING))
   6774  1.69  yamaguch 		return EBUSY;
   6775  1.69  yamaguch 
   6776  1.69  yamaguch 	if (newitr > 0x07ff)
   6777  1.69  yamaguch 		return EINVAL;
   6778  1.69  yamaguch 
   6779  1.69  yamaguch 	*itrptr = newitr;
   6780  1.69  yamaguch 
   6781  1.69  yamaguch 	return 0;
   6782  1.69  yamaguch }
   6783  1.69  yamaguch 
   6784  1.70  yamaguch static int
   6785  1.70  yamaguch ixl_sysctl_ndescs_handler(SYSCTLFN_ARGS)
   6786  1.70  yamaguch {
   6787  1.70  yamaguch 	struct sysctlnode node = *rnode;
   6788  1.70  yamaguch 	struct ixl_softc *sc = (struct ixl_softc *)node.sysctl_data;
   6789  1.70  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   6790  1.70  yamaguch 	unsigned int *ndescs_ptr, ndescs, n;
   6791  1.70  yamaguch 	int error;
   6792  1.70  yamaguch 
   6793  1.70  yamaguch 	if (ixl_sysctlnode_is_rx(&node)) {
   6794  1.70  yamaguch 		ndescs_ptr = &sc->sc_rx_ring_ndescs;
   6795  1.70  yamaguch 	} else {
   6796  1.70  yamaguch 		ndescs_ptr = &sc->sc_tx_ring_ndescs;
   6797  1.70  yamaguch 	}
   6798  1.70  yamaguch 
   6799  1.70  yamaguch 	ndescs = *ndescs_ptr;
   6800  1.70  yamaguch 	node.sysctl_data = &ndescs;
   6801  1.70  yamaguch 	node.sysctl_size = sizeof(ndescs);
   6802  1.70  yamaguch 
   6803  1.70  yamaguch 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6804  1.70  yamaguch 
   6805  1.70  yamaguch 	if (error || newp == NULL)
   6806  1.70  yamaguch 		return error;
   6807  1.70  yamaguch 
   6808  1.70  yamaguch 	if (ISSET(ifp->if_flags, IFF_RUNNING))
   6809  1.70  yamaguch 		return EBUSY;
   6810  1.70  yamaguch 
   6811  1.70  yamaguch 	if (ndescs < 8 || 0xffff < ndescs)
   6812  1.70  yamaguch 		return EINVAL;
   6813  1.70  yamaguch 
   6814  1.70  yamaguch 	n = 1U << (fls32(ndescs) - 1);
   6815  1.70  yamaguch 	if (n != ndescs)
   6816  1.70  yamaguch 		return EINVAL;
   6817  1.70  yamaguch 
   6818  1.70  yamaguch 	*ndescs_ptr = ndescs;
   6819  1.70  yamaguch 
   6820  1.70  yamaguch 	return 0;
   6821  1.70  yamaguch }
   6822  1.70  yamaguch 
   6823   1.1  yamaguch static struct workqueue *
   6824   1.1  yamaguch ixl_workq_create(const char *name, pri_t prio, int ipl, int flags)
   6825   1.1  yamaguch {
   6826   1.1  yamaguch 	struct workqueue *wq;
   6827   1.1  yamaguch 	int error;
   6828   1.1  yamaguch 
   6829   1.1  yamaguch 	error = workqueue_create(&wq, name, ixl_workq_work, NULL,
   6830   1.1  yamaguch 	    prio, ipl, flags);
   6831   1.1  yamaguch 
   6832   1.1  yamaguch 	if (error)
   6833   1.1  yamaguch 		return NULL;
   6834   1.1  yamaguch 
   6835   1.1  yamaguch 	return wq;
   6836   1.1  yamaguch }
   6837   1.1  yamaguch 
   6838   1.1  yamaguch static void
   6839   1.1  yamaguch ixl_workq_destroy(struct workqueue *wq)
   6840   1.1  yamaguch {
   6841   1.1  yamaguch 
   6842   1.1  yamaguch 	workqueue_destroy(wq);
   6843   1.1  yamaguch }
   6844   1.1  yamaguch 
   6845   1.1  yamaguch static void
   6846   1.1  yamaguch ixl_work_set(struct ixl_work *work, void (*func)(void *), void *arg)
   6847   1.1  yamaguch {
   6848   1.1  yamaguch 
   6849   1.1  yamaguch 	memset(work, 0, sizeof(*work));
   6850   1.1  yamaguch 	work->ixw_func = func;
   6851   1.1  yamaguch 	work->ixw_arg = arg;
   6852   1.1  yamaguch }
   6853   1.1  yamaguch 
   6854   1.1  yamaguch static void
   6855   1.1  yamaguch ixl_work_add(struct workqueue *wq, struct ixl_work *work)
   6856   1.1  yamaguch {
   6857   1.1  yamaguch 	if (atomic_cas_uint(&work->ixw_added, 0, 1) != 0)
   6858   1.1  yamaguch 		return;
   6859   1.1  yamaguch 
   6860  1.44  yamaguch 	kpreempt_disable();
   6861   1.1  yamaguch 	workqueue_enqueue(wq, &work->ixw_cookie, NULL);
   6862  1.44  yamaguch 	kpreempt_enable();
   6863   1.1  yamaguch }
   6864   1.1  yamaguch 
   6865   1.1  yamaguch static void
   6866   1.1  yamaguch ixl_work_wait(struct workqueue *wq, struct ixl_work *work)
   6867   1.1  yamaguch {
   6868   1.1  yamaguch 
   6869   1.1  yamaguch 	workqueue_wait(wq, &work->ixw_cookie);
   6870   1.1  yamaguch }
   6871   1.1  yamaguch 
   6872   1.1  yamaguch static void
   6873   1.1  yamaguch ixl_workq_work(struct work *wk, void *context)
   6874   1.1  yamaguch {
   6875   1.1  yamaguch 	struct ixl_work *work;
   6876   1.1  yamaguch 
   6877   1.1  yamaguch 	work = container_of(wk, struct ixl_work, ixw_cookie);
   6878   1.1  yamaguch 
   6879   1.1  yamaguch 	atomic_swap_uint(&work->ixw_added, 0);
   6880   1.1  yamaguch 	work->ixw_func(work->ixw_arg);
   6881   1.1  yamaguch }
   6882   1.1  yamaguch 
   6883   1.1  yamaguch static int
   6884   1.1  yamaguch ixl_rx_ctl_read(struct ixl_softc *sc, uint32_t reg, uint32_t *rv)
   6885   1.1  yamaguch {
   6886   1.1  yamaguch 	struct ixl_aq_desc iaq;
   6887   1.1  yamaguch 
   6888   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   6889   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_RX_CTL_REG_READ);
   6890   1.1  yamaguch 	iaq.iaq_param[1] = htole32(reg);
   6891   1.1  yamaguch 
   6892   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0)
   6893   1.1  yamaguch 		return ETIMEDOUT;
   6894   1.1  yamaguch 
   6895   1.1  yamaguch 	switch (htole16(iaq.iaq_retval)) {
   6896   1.1  yamaguch 	case IXL_AQ_RC_OK:
   6897   1.1  yamaguch 		/* success */
   6898   1.1  yamaguch 		break;
   6899   1.1  yamaguch 	case IXL_AQ_RC_EACCES:
   6900   1.1  yamaguch 		return EPERM;
   6901   1.1  yamaguch 	case IXL_AQ_RC_EAGAIN:
   6902   1.1  yamaguch 		return EAGAIN;
   6903   1.1  yamaguch 	default:
   6904   1.1  yamaguch 		return EIO;
   6905   1.1  yamaguch 	}
   6906   1.1  yamaguch 
   6907   1.1  yamaguch 	*rv = htole32(iaq.iaq_param[3]);
   6908   1.1  yamaguch 	return 0;
   6909   1.1  yamaguch }
   6910   1.1  yamaguch 
   6911   1.1  yamaguch static uint32_t
   6912   1.1  yamaguch ixl_rd_rx_csr(struct ixl_softc *sc, uint32_t reg)
   6913   1.1  yamaguch {
   6914   1.1  yamaguch 	uint32_t val;
   6915   1.1  yamaguch 	int rv, retry, retry_limit;
   6916   1.1  yamaguch 
   6917  1.25  yamaguch 	if (ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RXCTL)) {
   6918  1.25  yamaguch 		retry_limit = 5;
   6919  1.25  yamaguch 	} else {
   6920  1.25  yamaguch 		retry_limit = 0;
   6921  1.25  yamaguch 	}
   6922   1.1  yamaguch 
   6923   1.1  yamaguch 	for (retry = 0; retry < retry_limit; retry++) {
   6924   1.1  yamaguch 		rv = ixl_rx_ctl_read(sc, reg, &val);
   6925   1.1  yamaguch 		if (rv == 0)
   6926   1.1  yamaguch 			return val;
   6927   1.1  yamaguch 		else if (rv == EAGAIN)
   6928   1.1  yamaguch 			delaymsec(1);
   6929   1.1  yamaguch 		else
   6930   1.1  yamaguch 			break;
   6931   1.1  yamaguch 	}
   6932   1.1  yamaguch 
   6933   1.1  yamaguch 	val = ixl_rd(sc, reg);
   6934   1.1  yamaguch 
   6935   1.1  yamaguch 	return val;
   6936   1.1  yamaguch }
   6937   1.1  yamaguch 
   6938   1.1  yamaguch static int
   6939   1.1  yamaguch ixl_rx_ctl_write(struct ixl_softc *sc, uint32_t reg, uint32_t value)
   6940   1.1  yamaguch {
   6941   1.1  yamaguch 	struct ixl_aq_desc iaq;
   6942   1.1  yamaguch 
   6943   1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   6944   1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_RX_CTL_REG_WRITE);
   6945   1.1  yamaguch 	iaq.iaq_param[1] = htole32(reg);
   6946   1.1  yamaguch 	iaq.iaq_param[3] = htole32(value);
   6947   1.1  yamaguch 
   6948   1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0)
   6949   1.1  yamaguch 		return ETIMEDOUT;
   6950   1.1  yamaguch 
   6951   1.1  yamaguch 	switch (htole16(iaq.iaq_retval)) {
   6952   1.1  yamaguch 	case IXL_AQ_RC_OK:
   6953   1.1  yamaguch 		/* success */
   6954   1.1  yamaguch 		break;
   6955   1.1  yamaguch 	case IXL_AQ_RC_EACCES:
   6956   1.1  yamaguch 		return EPERM;
   6957   1.1  yamaguch 	case IXL_AQ_RC_EAGAIN:
   6958   1.1  yamaguch 		return EAGAIN;
   6959   1.1  yamaguch 	default:
   6960   1.1  yamaguch 		return EIO;
   6961   1.1  yamaguch 	}
   6962   1.1  yamaguch 
   6963   1.1  yamaguch 	return 0;
   6964   1.1  yamaguch }
   6965   1.1  yamaguch 
   6966   1.1  yamaguch static void
   6967   1.1  yamaguch ixl_wr_rx_csr(struct ixl_softc *sc, uint32_t reg, uint32_t value)
   6968   1.1  yamaguch {
   6969   1.1  yamaguch 	int rv, retry, retry_limit;
   6970   1.1  yamaguch 
   6971  1.25  yamaguch 	if (ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_RXCTL)) {
   6972  1.25  yamaguch 		retry_limit = 5;
   6973  1.25  yamaguch 	} else {
   6974  1.25  yamaguch 		retry_limit = 0;
   6975  1.25  yamaguch 	}
   6976   1.1  yamaguch 
   6977   1.1  yamaguch 	for (retry = 0; retry < retry_limit; retry++) {
   6978   1.1  yamaguch 		rv = ixl_rx_ctl_write(sc, reg, value);
   6979   1.1  yamaguch 		if (rv == 0)
   6980   1.1  yamaguch 			return;
   6981   1.1  yamaguch 		else if (rv == EAGAIN)
   6982   1.1  yamaguch 			delaymsec(1);
   6983   1.1  yamaguch 		else
   6984   1.1  yamaguch 			break;
   6985   1.1  yamaguch 	}
   6986   1.1  yamaguch 
   6987   1.1  yamaguch 	ixl_wr(sc, reg, value);
   6988   1.1  yamaguch }
   6989   1.1  yamaguch 
   6990  1.26  yamaguch static int
   6991  1.26  yamaguch ixl_nvm_lock(struct ixl_softc *sc, char rw)
   6992  1.26  yamaguch {
   6993  1.26  yamaguch 	struct ixl_aq_desc iaq;
   6994  1.26  yamaguch 	struct ixl_aq_req_resource_param *param;
   6995  1.26  yamaguch 	int rv;
   6996  1.26  yamaguch 
   6997  1.26  yamaguch 	if (!ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_NVMLOCK))
   6998  1.26  yamaguch 		return 0;
   6999  1.26  yamaguch 
   7000  1.26  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   7001  1.26  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_REQUEST_RESOURCE);
   7002  1.26  yamaguch 
   7003  1.26  yamaguch 	param = (struct ixl_aq_req_resource_param *)&iaq.iaq_param;
   7004  1.26  yamaguch 	param->resource_id = htole16(IXL_AQ_RESOURCE_ID_NVM);
   7005  1.26  yamaguch 	if (rw == 'R') {
   7006  1.26  yamaguch 		param->access_type = htole16(IXL_AQ_RESOURCE_ACCES_READ);
   7007  1.26  yamaguch 	} else {
   7008  1.26  yamaguch 		param->access_type = htole16(IXL_AQ_RESOURCE_ACCES_WRITE);
   7009  1.26  yamaguch 	}
   7010  1.26  yamaguch 
   7011  1.26  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   7012  1.26  yamaguch 
   7013  1.26  yamaguch 	if (rv != 0)
   7014  1.26  yamaguch 		return ETIMEDOUT;
   7015  1.26  yamaguch 
   7016  1.26  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   7017  1.26  yamaguch 	case IXL_AQ_RC_OK:
   7018  1.26  yamaguch 		break;
   7019  1.26  yamaguch 	case IXL_AQ_RC_EACCES:
   7020  1.26  yamaguch 		return EACCES;
   7021  1.26  yamaguch 	case IXL_AQ_RC_EBUSY:
   7022  1.26  yamaguch 		return EBUSY;
   7023  1.26  yamaguch 	case IXL_AQ_RC_EPERM:
   7024  1.26  yamaguch 		return EPERM;
   7025  1.26  yamaguch 	}
   7026  1.26  yamaguch 
   7027  1.26  yamaguch 	return 0;
   7028  1.26  yamaguch }
   7029  1.26  yamaguch 
   7030  1.26  yamaguch static int
   7031  1.26  yamaguch ixl_nvm_unlock(struct ixl_softc *sc)
   7032  1.26  yamaguch {
   7033  1.26  yamaguch 	struct ixl_aq_desc iaq;
   7034  1.26  yamaguch 	struct ixl_aq_rel_resource_param *param;
   7035  1.26  yamaguch 	int rv;
   7036  1.26  yamaguch 
   7037  1.26  yamaguch 	if (!ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_NVMLOCK))
   7038  1.26  yamaguch 		return 0;
   7039  1.26  yamaguch 
   7040  1.26  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   7041  1.26  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_RELEASE_RESOURCE);
   7042  1.26  yamaguch 
   7043  1.26  yamaguch 	param = (struct ixl_aq_rel_resource_param *)&iaq.iaq_param;
   7044  1.26  yamaguch 	param->resource_id = htole16(IXL_AQ_RESOURCE_ID_NVM);
   7045  1.26  yamaguch 
   7046  1.26  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   7047  1.26  yamaguch 
   7048  1.26  yamaguch 	if (rv != 0)
   7049  1.26  yamaguch 		return ETIMEDOUT;
   7050  1.26  yamaguch 
   7051  1.26  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   7052  1.26  yamaguch 	case IXL_AQ_RC_OK:
   7053  1.26  yamaguch 		break;
   7054  1.26  yamaguch 	default:
   7055  1.26  yamaguch 		return EIO;
   7056  1.26  yamaguch 	}
   7057  1.26  yamaguch 	return 0;
   7058  1.26  yamaguch }
   7059  1.26  yamaguch 
   7060  1.26  yamaguch static int
   7061  1.26  yamaguch ixl_srdone_poll(struct ixl_softc *sc)
   7062  1.26  yamaguch {
   7063  1.26  yamaguch 	int wait_count;
   7064  1.26  yamaguch 	uint32_t reg;
   7065  1.26  yamaguch 
   7066  1.26  yamaguch 	for (wait_count = 0; wait_count < IXL_SRRD_SRCTL_ATTEMPTS;
   7067  1.26  yamaguch 	    wait_count++) {
   7068  1.26  yamaguch 		reg = ixl_rd(sc, I40E_GLNVM_SRCTL);
   7069  1.26  yamaguch 		if (ISSET(reg, I40E_GLNVM_SRCTL_DONE_MASK))
   7070  1.26  yamaguch 			break;
   7071  1.26  yamaguch 
   7072  1.26  yamaguch 		delaymsec(5);
   7073  1.26  yamaguch 	}
   7074  1.26  yamaguch 
   7075  1.26  yamaguch 	if (wait_count == IXL_SRRD_SRCTL_ATTEMPTS)
   7076  1.26  yamaguch 		return -1;
   7077  1.26  yamaguch 
   7078  1.26  yamaguch 	return 0;
   7079  1.26  yamaguch }
   7080  1.26  yamaguch 
   7081  1.26  yamaguch static int
   7082  1.26  yamaguch ixl_nvm_read_srctl(struct ixl_softc *sc, uint16_t offset, uint16_t *data)
   7083  1.26  yamaguch {
   7084  1.26  yamaguch 	uint32_t reg;
   7085  1.26  yamaguch 
   7086  1.26  yamaguch 	if (ixl_srdone_poll(sc) != 0)
   7087  1.26  yamaguch 		return ETIMEDOUT;
   7088  1.26  yamaguch 
   7089  1.26  yamaguch 	reg = ((uint32_t)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
   7090  1.26  yamaguch 	    __BIT(I40E_GLNVM_SRCTL_START_SHIFT);
   7091  1.26  yamaguch 	ixl_wr(sc, I40E_GLNVM_SRCTL, reg);
   7092  1.26  yamaguch 
   7093  1.26  yamaguch 	if (ixl_srdone_poll(sc) != 0) {
   7094  1.26  yamaguch 		aprint_debug("NVM read error: couldn't access "
   7095  1.26  yamaguch 		    "Shadow RAM address: 0x%x\n", offset);
   7096  1.26  yamaguch 		return ETIMEDOUT;
   7097  1.26  yamaguch 	}
   7098  1.26  yamaguch 
   7099  1.26  yamaguch 	reg = ixl_rd(sc, I40E_GLNVM_SRDATA);
   7100  1.26  yamaguch 	*data = (uint16_t)__SHIFTOUT(reg, I40E_GLNVM_SRDATA_RDDATA_MASK);
   7101  1.26  yamaguch 
   7102  1.26  yamaguch 	return 0;
   7103  1.26  yamaguch }
   7104  1.26  yamaguch 
   7105  1.26  yamaguch static int
   7106  1.26  yamaguch ixl_nvm_read_aq(struct ixl_softc *sc, uint16_t offset_word,
   7107  1.26  yamaguch     void *data, size_t len)
   7108  1.26  yamaguch {
   7109  1.26  yamaguch 	struct ixl_dmamem *idm;
   7110  1.26  yamaguch 	struct ixl_aq_desc iaq;
   7111  1.26  yamaguch 	struct ixl_aq_nvm_param *param;
   7112  1.26  yamaguch 	uint32_t offset_bytes;
   7113  1.26  yamaguch 	int rv;
   7114  1.26  yamaguch 
   7115  1.26  yamaguch 	idm = &sc->sc_aqbuf;
   7116  1.26  yamaguch 	if (len > IXL_DMA_LEN(idm))
   7117  1.26  yamaguch 		return ENOMEM;
   7118  1.26  yamaguch 
   7119  1.26  yamaguch 	memset(IXL_DMA_KVA(idm), 0, IXL_DMA_LEN(idm));
   7120  1.26  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   7121  1.26  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_NVM_READ);
   7122  1.26  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   7123  1.26  yamaguch 	    ((len > I40E_AQ_LARGE_BUF) ? IXL_AQ_LB : 0));
   7124  1.26  yamaguch 	iaq.iaq_datalen = htole16(len);
   7125  1.26  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(idm));
   7126  1.26  yamaguch 
   7127  1.26  yamaguch 	param = (struct ixl_aq_nvm_param *)iaq.iaq_param;
   7128  1.26  yamaguch 	param->command_flags = IXL_AQ_NVM_LAST_CMD;
   7129  1.26  yamaguch 	param->module_pointer = 0;
   7130  1.26  yamaguch 	param->length = htole16(len);
   7131  1.26  yamaguch 	offset_bytes = (uint32_t)offset_word * 2;
   7132  1.26  yamaguch 	offset_bytes &= 0x00FFFFFF;
   7133  1.26  yamaguch 	param->offset = htole32(offset_bytes);
   7134  1.26  yamaguch 
   7135  1.26  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   7136  1.26  yamaguch 	    BUS_DMASYNC_PREREAD);
   7137  1.26  yamaguch 
   7138  1.26  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   7139  1.26  yamaguch 
   7140  1.26  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   7141  1.26  yamaguch 	    BUS_DMASYNC_POSTREAD);
   7142  1.26  yamaguch 
   7143  1.26  yamaguch 	if (rv != 0) {
   7144  1.26  yamaguch 		return ETIMEDOUT;
   7145  1.26  yamaguch 	}
   7146  1.26  yamaguch 
   7147  1.26  yamaguch 	switch (le16toh(iaq.iaq_retval)) {
   7148  1.26  yamaguch 	case IXL_AQ_RC_OK:
   7149  1.26  yamaguch 		break;
   7150  1.26  yamaguch 	case IXL_AQ_RC_EPERM:
   7151  1.26  yamaguch 		return EPERM;
   7152  1.26  yamaguch 	case IXL_AQ_RC_EINVAL:
   7153  1.26  yamaguch 		return EINVAL;
   7154  1.26  yamaguch 	case IXL_AQ_RC_EBUSY:
   7155  1.26  yamaguch 		return EBUSY;
   7156  1.26  yamaguch 	case IXL_AQ_RC_EIO:
   7157  1.26  yamaguch 	default:
   7158  1.26  yamaguch 		return EIO;
   7159  1.26  yamaguch 	}
   7160  1.26  yamaguch 
   7161  1.26  yamaguch 	memcpy(data, IXL_DMA_KVA(idm), len);
   7162  1.26  yamaguch 
   7163  1.26  yamaguch 	return 0;
   7164  1.26  yamaguch }
   7165  1.26  yamaguch 
   7166  1.26  yamaguch static int
   7167  1.26  yamaguch ixl_rd16_nvm(struct ixl_softc *sc, uint16_t offset, uint16_t *data)
   7168  1.26  yamaguch {
   7169  1.26  yamaguch 	int error;
   7170  1.26  yamaguch 	uint16_t buf;
   7171  1.26  yamaguch 
   7172  1.26  yamaguch 	error = ixl_nvm_lock(sc, 'R');
   7173  1.26  yamaguch 	if (error)
   7174  1.26  yamaguch 		return error;
   7175  1.26  yamaguch 
   7176  1.26  yamaguch 	if (ISSET(sc->sc_aq_flags, IXL_SC_AQ_FLAG_NVMREAD)) {
   7177  1.26  yamaguch 		error = ixl_nvm_read_aq(sc, offset,
   7178  1.26  yamaguch 		    &buf, sizeof(buf));
   7179  1.26  yamaguch 		if (error == 0)
   7180  1.26  yamaguch 			*data = le16toh(buf);
   7181  1.26  yamaguch 	} else {
   7182  1.26  yamaguch 		error = ixl_nvm_read_srctl(sc, offset, &buf);
   7183  1.26  yamaguch 		if (error == 0)
   7184  1.26  yamaguch 			*data = buf;
   7185  1.26  yamaguch 	}
   7186  1.26  yamaguch 
   7187  1.26  yamaguch 	ixl_nvm_unlock(sc);
   7188  1.26  yamaguch 
   7189  1.26  yamaguch 	return error;
   7190  1.26  yamaguch }
   7191  1.26  yamaguch 
   7192   1.1  yamaguch MODULE(MODULE_CLASS_DRIVER, if_ixl, "pci");
   7193   1.1  yamaguch 
   7194   1.1  yamaguch #ifdef _MODULE
   7195   1.1  yamaguch #include "ioconf.c"
   7196   1.1  yamaguch #endif
   7197   1.1  yamaguch 
   7198  1.19  yamaguch #ifdef _MODULE
   7199  1.19  yamaguch static void
   7200  1.19  yamaguch ixl_parse_modprop(prop_dictionary_t dict)
   7201  1.19  yamaguch {
   7202  1.19  yamaguch 	prop_object_t obj;
   7203  1.19  yamaguch 	int64_t val;
   7204  1.19  yamaguch 	uint64_t uval;
   7205  1.19  yamaguch 
   7206  1.19  yamaguch 	if (dict == NULL)
   7207  1.19  yamaguch 		return;
   7208  1.19  yamaguch 
   7209  1.19  yamaguch 	obj = prop_dictionary_get(dict, "nomsix");
   7210  1.19  yamaguch 	if (obj != NULL && prop_object_type(obj) == PROP_TYPE_BOOL) {
   7211  1.19  yamaguch 		ixl_param_nomsix = prop_bool_true((prop_bool_t)obj);
   7212  1.19  yamaguch 	}
   7213  1.19  yamaguch 
   7214  1.19  yamaguch 	obj = prop_dictionary_get(dict, "stats_interval");
   7215  1.19  yamaguch 	if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
   7216  1.67   thorpej 		val = prop_number_signed_value((prop_number_t)obj);
   7217  1.19  yamaguch 
   7218  1.19  yamaguch 		/* the range has no reason */
   7219  1.24  yamaguch 		if (100 < val && val < 180000) {
   7220  1.19  yamaguch 			ixl_param_stats_interval = val;
   7221  1.19  yamaguch 		}
   7222  1.19  yamaguch 	}
   7223  1.19  yamaguch 
   7224  1.19  yamaguch 	obj = prop_dictionary_get(dict, "nqps_limit");
   7225  1.19  yamaguch 	if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
   7226  1.67   thorpej 		val = prop_number_signed_value((prop_number_t)obj);
   7227  1.19  yamaguch 
   7228  1.19  yamaguch 		if (val <= INT32_MAX)
   7229  1.19  yamaguch 			ixl_param_nqps_limit = val;
   7230  1.19  yamaguch 	}
   7231  1.19  yamaguch 
   7232  1.19  yamaguch 	obj = prop_dictionary_get(dict, "rx_ndescs");
   7233  1.19  yamaguch 	if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
   7234  1.19  yamaguch 		uval = prop_number_unsigned_integer_value((prop_number_t)obj);
   7235  1.19  yamaguch 
   7236  1.19  yamaguch 		if (uval > 8)
   7237  1.19  yamaguch 			ixl_param_rx_ndescs = uval;
   7238  1.19  yamaguch 	}
   7239  1.19  yamaguch 
   7240  1.19  yamaguch 	obj = prop_dictionary_get(dict, "tx_ndescs");
   7241  1.19  yamaguch 	if (obj != NULL && prop_object_type(obj) == PROP_TYPE_NUMBER) {
   7242  1.19  yamaguch 		uval = prop_number_unsigned_integer_value((prop_number_t)obj);
   7243  1.19  yamaguch 
   7244  1.19  yamaguch 		if (uval > IXL_TX_PKT_DESCS)
   7245  1.19  yamaguch 			ixl_param_tx_ndescs = uval;
   7246  1.19  yamaguch 	}
   7247  1.19  yamaguch 
   7248  1.19  yamaguch }
   7249  1.19  yamaguch #endif
   7250  1.19  yamaguch 
   7251   1.1  yamaguch static int
   7252   1.1  yamaguch if_ixl_modcmd(modcmd_t cmd, void *opaque)
   7253   1.1  yamaguch {
   7254   1.1  yamaguch 	int error = 0;
   7255   1.1  yamaguch 
   7256   1.1  yamaguch #ifdef _MODULE
   7257   1.1  yamaguch 	switch (cmd) {
   7258   1.1  yamaguch 	case MODULE_CMD_INIT:
   7259  1.19  yamaguch 		ixl_parse_modprop((prop_dictionary_t)opaque);
   7260   1.1  yamaguch 		error = config_init_component(cfdriver_ioconf_if_ixl,
   7261   1.1  yamaguch 		    cfattach_ioconf_if_ixl, cfdata_ioconf_if_ixl);
   7262   1.1  yamaguch 		break;
   7263   1.1  yamaguch 	case MODULE_CMD_FINI:
   7264   1.1  yamaguch 		error = config_fini_component(cfdriver_ioconf_if_ixl,
   7265   1.1  yamaguch 		    cfattach_ioconf_if_ixl, cfdata_ioconf_if_ixl);
   7266   1.1  yamaguch 		break;
   7267   1.1  yamaguch 	default:
   7268   1.1  yamaguch 		error = ENOTTY;
   7269   1.1  yamaguch 		break;
   7270   1.1  yamaguch 	}
   7271   1.1  yamaguch #endif
   7272   1.1  yamaguch 
   7273   1.1  yamaguch 	return error;
   7274   1.1  yamaguch }
   7275