Home | History | Annotate | Line # | Download | only in pci
if_ixl.c revision 1.1
      1  1.1  yamaguch /*	$NetBSD: if_ixl.c,v 1.1 2019/12/10 12:08:52 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.1  yamaguch 
     78  1.1  yamaguch #ifdef _KERNEL_OPT
     79  1.1  yamaguch #include "opt_net_mpsafe.h"
     80  1.1  yamaguch #endif
     81  1.1  yamaguch 
     82  1.1  yamaguch #include <sys/param.h>
     83  1.1  yamaguch #include <sys/types.h>
     84  1.1  yamaguch 
     85  1.1  yamaguch #include <sys/cpu.h>
     86  1.1  yamaguch #include <sys/device.h>
     87  1.1  yamaguch #include <sys/evcnt.h>
     88  1.1  yamaguch #include <sys/interrupt.h>
     89  1.1  yamaguch #include <sys/kmem.h>
     90  1.1  yamaguch #include <sys/malloc.h>
     91  1.1  yamaguch #include <sys/module.h>
     92  1.1  yamaguch #include <sys/mutex.h>
     93  1.1  yamaguch #include <sys/pcq.h>
     94  1.1  yamaguch #include <sys/syslog.h>
     95  1.1  yamaguch #include <sys/workqueue.h>
     96  1.1  yamaguch 
     97  1.1  yamaguch #include <sys/bus.h>
     98  1.1  yamaguch 
     99  1.1  yamaguch #include <net/bpf.h>
    100  1.1  yamaguch #include <net/if.h>
    101  1.1  yamaguch #include <net/if_dl.h>
    102  1.1  yamaguch #include <net/if_media.h>
    103  1.1  yamaguch #include <net/if_ether.h>
    104  1.1  yamaguch #include <net/rss_config.h>
    105  1.1  yamaguch 
    106  1.1  yamaguch #include <dev/pci/pcivar.h>
    107  1.1  yamaguch #include <dev/pci/pcidevs.h>
    108  1.1  yamaguch 
    109  1.1  yamaguch #include <dev/pci/if_ixlreg.h>
    110  1.1  yamaguch #include <dev/pci/if_ixlvar.h>
    111  1.1  yamaguch 
    112  1.1  yamaguch struct ixl_softc; /* defined */
    113  1.1  yamaguch 
    114  1.1  yamaguch #define I40E_PF_RESET_WAIT_COUNT	200
    115  1.1  yamaguch #define I40E_AQ_LARGE_BUF		512
    116  1.1  yamaguch 
    117  1.1  yamaguch /* bitfields for Tx queue mapping in QTX_CTL */
    118  1.1  yamaguch #define I40E_QTX_CTL_VF_QUEUE		0x0
    119  1.1  yamaguch #define I40E_QTX_CTL_VM_QUEUE		0x1
    120  1.1  yamaguch #define I40E_QTX_CTL_PF_QUEUE		0x2
    121  1.1  yamaguch 
    122  1.1  yamaguch #define I40E_QUEUE_TYPE_EOL		0x7ff
    123  1.1  yamaguch #define I40E_INTR_NOTX_QUEUE		0
    124  1.1  yamaguch 
    125  1.1  yamaguch #define I40E_QUEUE_TYPE_RX		0x0
    126  1.1  yamaguch #define I40E_QUEUE_TYPE_TX		0x1
    127  1.1  yamaguch #define I40E_QUEUE_TYPE_PE_CEQ		0x2
    128  1.1  yamaguch #define I40E_QUEUE_TYPE_UNKNOWN		0x3
    129  1.1  yamaguch 
    130  1.1  yamaguch #define I40E_ITR_INDEX_RX		0x0
    131  1.1  yamaguch #define I40E_ITR_INDEX_TX		0x1
    132  1.1  yamaguch #define I40E_ITR_INDEX_OTHER		0x2
    133  1.1  yamaguch #define I40E_ITR_INDEX_NONE		0x3
    134  1.1  yamaguch 
    135  1.1  yamaguch #define I40E_INTR_NOTX_QUEUE		0
    136  1.1  yamaguch #define I40E_INTR_NOTX_INTR		0
    137  1.1  yamaguch #define I40E_INTR_NOTX_RX_QUEUE		0
    138  1.1  yamaguch #define I40E_INTR_NOTX_TX_QUEUE		1
    139  1.1  yamaguch #define I40E_INTR_NOTX_RX_MASK		I40E_PFINT_ICR0_QUEUE_0_MASK
    140  1.1  yamaguch #define I40E_INTR_NOTX_TX_MASK		I40E_PFINT_ICR0_QUEUE_1_MASK
    141  1.1  yamaguch 
    142  1.1  yamaguch #define BIT_ULL(a)	(1ULL << (a))
    143  1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_BASE			\
    144  1.1  yamaguch 	(BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |	\
    145  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |	\
    146  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |	\
    147  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |	\
    148  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4) |	\
    149  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |	\
    150  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |	\
    151  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |	\
    152  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |	\
    153  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6) |	\
    154  1.1  yamaguch 	 BIT_ULL(I40E_FILTER_PCTYPE_L2_PAYLOAD))
    155  1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_XL710	IXL_RSS_HENA_DEFAULT_BASE
    156  1.1  yamaguch #define IXL_RSS_HENA_DEFAULT_X722	(IXL_RSS_HENA_DEFAULT_XL710 |	\
    157  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |		\
    158  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |		\
    159  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |		\
    160  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |		\
    161  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) |	\
    162  1.1  yamaguch 	BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK))
    163  1.1  yamaguch #define I40E_HASH_LUT_SIZE_128		0
    164  1.1  yamaguch #define IXL_RSS_KEY_SIZE_REG		13
    165  1.1  yamaguch 
    166  1.1  yamaguch #define IXL_ICR0_CRIT_ERR_MASK			\
    167  1.1  yamaguch 	(I40E_PFINT_ICR0_PCI_EXCEPTION_MASK |	\
    168  1.1  yamaguch 	I40E_PFINT_ICR0_ECC_ERR_MASK |		\
    169  1.1  yamaguch 	I40E_PFINT_ICR0_PE_CRITERR_MASK)
    170  1.1  yamaguch 
    171  1.1  yamaguch #define IXL_TX_PKT_DESCS		8
    172  1.1  yamaguch #define IXL_TX_QUEUE_ALIGN		128
    173  1.1  yamaguch #define IXL_RX_QUEUE_ALIGN		128
    174  1.1  yamaguch 
    175  1.1  yamaguch #define IXL_HARDMTU			9712 /* 9726 - ETHER_HDR_LEN */
    176  1.1  yamaguch 
    177  1.1  yamaguch #define IXL_PCIREG			PCI_MAPREG_START
    178  1.1  yamaguch 
    179  1.1  yamaguch #define IXL_ITR0			0x0
    180  1.1  yamaguch #define IXL_ITR1			0x1
    181  1.1  yamaguch #define IXL_ITR2			0x2
    182  1.1  yamaguch #define IXL_NOITR			0x3
    183  1.1  yamaguch 
    184  1.1  yamaguch #define IXL_AQ_NUM			256
    185  1.1  yamaguch #define IXL_AQ_MASK			(IXL_AQ_NUM - 1)
    186  1.1  yamaguch #define IXL_AQ_ALIGN			64 /* lol */
    187  1.1  yamaguch #define IXL_AQ_BUFLEN			4096
    188  1.1  yamaguch 
    189  1.1  yamaguch #define IXL_HMC_ROUNDUP			512
    190  1.1  yamaguch #define IXL_HMC_PGSIZE			4096
    191  1.1  yamaguch #define IXL_HMC_DVASZ			sizeof(uint64_t)
    192  1.1  yamaguch #define IXL_HMC_PGS			(IXL_HMC_PGSIZE / IXL_HMC_DVASZ)
    193  1.1  yamaguch #define IXL_HMC_L2SZ			(IXL_HMC_PGSIZE * IXL_HMC_PGS)
    194  1.1  yamaguch #define IXL_HMC_PDVALID			1ULL
    195  1.1  yamaguch 
    196  1.1  yamaguch #define IXL_ATQ_EXEC_TIMEOUT		(10 * hz)
    197  1.1  yamaguch 
    198  1.1  yamaguch struct ixl_aq_regs {
    199  1.1  yamaguch 	bus_size_t		atq_tail;
    200  1.1  yamaguch 	bus_size_t		atq_head;
    201  1.1  yamaguch 	bus_size_t		atq_len;
    202  1.1  yamaguch 	bus_size_t		atq_bal;
    203  1.1  yamaguch 	bus_size_t		atq_bah;
    204  1.1  yamaguch 
    205  1.1  yamaguch 	bus_size_t		arq_tail;
    206  1.1  yamaguch 	bus_size_t		arq_head;
    207  1.1  yamaguch 	bus_size_t		arq_len;
    208  1.1  yamaguch 	bus_size_t		arq_bal;
    209  1.1  yamaguch 	bus_size_t		arq_bah;
    210  1.1  yamaguch 
    211  1.1  yamaguch 	uint32_t		atq_len_enable;
    212  1.1  yamaguch 	uint32_t		atq_tail_mask;
    213  1.1  yamaguch 	uint32_t		atq_head_mask;
    214  1.1  yamaguch 
    215  1.1  yamaguch 	uint32_t		arq_len_enable;
    216  1.1  yamaguch 	uint32_t		arq_tail_mask;
    217  1.1  yamaguch 	uint32_t		arq_head_mask;
    218  1.1  yamaguch };
    219  1.1  yamaguch 
    220  1.1  yamaguch struct ixl_phy_type {
    221  1.1  yamaguch 	uint64_t	phy_type;
    222  1.1  yamaguch 	uint64_t	ifm_type;
    223  1.1  yamaguch };
    224  1.1  yamaguch 
    225  1.1  yamaguch struct ixl_speed_type {
    226  1.1  yamaguch 	uint8_t		dev_speed;
    227  1.1  yamaguch 	uint64_t	net_speed;
    228  1.1  yamaguch };
    229  1.1  yamaguch 
    230  1.1  yamaguch struct ixl_aq_buf {
    231  1.1  yamaguch 	SIMPLEQ_ENTRY(ixl_aq_buf)
    232  1.1  yamaguch 				 aqb_entry;
    233  1.1  yamaguch 	void			*aqb_data;
    234  1.1  yamaguch 	bus_dmamap_t		 aqb_map;
    235  1.1  yamaguch 	bus_dma_segment_t	 aqb_seg;
    236  1.1  yamaguch 	size_t			 aqb_size;
    237  1.1  yamaguch 	int			 aqb_nsegs;
    238  1.1  yamaguch };
    239  1.1  yamaguch SIMPLEQ_HEAD(ixl_aq_bufs, ixl_aq_buf);
    240  1.1  yamaguch 
    241  1.1  yamaguch struct ixl_dmamem {
    242  1.1  yamaguch 	bus_dmamap_t		 ixm_map;
    243  1.1  yamaguch 	bus_dma_segment_t	 ixm_seg;
    244  1.1  yamaguch 	int			 ixm_nsegs;
    245  1.1  yamaguch 	size_t			 ixm_size;
    246  1.1  yamaguch 	void			*ixm_kva;
    247  1.1  yamaguch };
    248  1.1  yamaguch 
    249  1.1  yamaguch #define IXL_DMA_MAP(_ixm)	((_ixm)->ixm_map)
    250  1.1  yamaguch #define IXL_DMA_DVA(_ixm)	((_ixm)->ixm_map->dm_segs[0].ds_addr)
    251  1.1  yamaguch #define IXL_DMA_KVA(_ixm)	((void *)(_ixm)->ixm_kva)
    252  1.1  yamaguch #define IXL_DMA_LEN(_ixm)	((_ixm)->ixm_size)
    253  1.1  yamaguch 
    254  1.1  yamaguch struct ixl_hmc_entry {
    255  1.1  yamaguch 	uint64_t		 hmc_base;
    256  1.1  yamaguch 	uint32_t		 hmc_count;
    257  1.1  yamaguch 	uint64_t		 hmc_size;
    258  1.1  yamaguch };
    259  1.1  yamaguch 
    260  1.1  yamaguch enum  ixl_hmc_types {
    261  1.1  yamaguch 	IXL_HMC_LAN_TX = 0,
    262  1.1  yamaguch 	IXL_HMC_LAN_RX,
    263  1.1  yamaguch 	IXL_HMC_FCOE_CTX,
    264  1.1  yamaguch 	IXL_HMC_FCOE_FILTER,
    265  1.1  yamaguch 	IXL_HMC_COUNT
    266  1.1  yamaguch };
    267  1.1  yamaguch 
    268  1.1  yamaguch struct ixl_hmc_pack {
    269  1.1  yamaguch 	uint16_t		offset;
    270  1.1  yamaguch 	uint16_t		width;
    271  1.1  yamaguch 	uint16_t		lsb;
    272  1.1  yamaguch };
    273  1.1  yamaguch 
    274  1.1  yamaguch /*
    275  1.1  yamaguch  * these hmc objects have weird sizes and alignments, so these are abstract
    276  1.1  yamaguch  * representations of them that are nice for c to populate.
    277  1.1  yamaguch  *
    278  1.1  yamaguch  * the packing code relies on little-endian values being stored in the fields,
    279  1.1  yamaguch  * no high bits in the fields being set, and the fields must be packed in the
    280  1.1  yamaguch  * same order as they are in the ctx structure.
    281  1.1  yamaguch  */
    282  1.1  yamaguch 
    283  1.1  yamaguch struct ixl_hmc_rxq {
    284  1.1  yamaguch 	uint16_t		 head;
    285  1.1  yamaguch 	uint8_t			 cpuid;
    286  1.1  yamaguch 	uint64_t		 base;
    287  1.1  yamaguch #define IXL_HMC_RXQ_BASE_UNIT		128
    288  1.1  yamaguch 	uint16_t		 qlen;
    289  1.1  yamaguch 	uint16_t		 dbuff;
    290  1.1  yamaguch #define IXL_HMC_RXQ_DBUFF_UNIT		128
    291  1.1  yamaguch 	uint8_t			 hbuff;
    292  1.1  yamaguch #define IXL_HMC_RXQ_HBUFF_UNIT		64
    293  1.1  yamaguch 	uint8_t			 dtype;
    294  1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_NOSPLIT	0x0
    295  1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_HSPLIT	0x1
    296  1.1  yamaguch #define IXL_HMC_RXQ_DTYPE_SPLIT_ALWAYS	0x2
    297  1.1  yamaguch 	uint8_t			 dsize;
    298  1.1  yamaguch #define IXL_HMC_RXQ_DSIZE_16		0
    299  1.1  yamaguch #define IXL_HMC_RXQ_DSIZE_32		1
    300  1.1  yamaguch 	uint8_t			 crcstrip;
    301  1.1  yamaguch 	uint8_t			 fc_ena;
    302  1.1  yamaguch 	uint8_t			 l2sel;
    303  1.1  yamaguch 	uint8_t			 hsplit_0;
    304  1.1  yamaguch 	uint8_t			 hsplit_1;
    305  1.1  yamaguch 	uint8_t			 showiv;
    306  1.1  yamaguch 	uint16_t		 rxmax;
    307  1.1  yamaguch 	uint8_t			 tphrdesc_ena;
    308  1.1  yamaguch 	uint8_t			 tphwdesc_ena;
    309  1.1  yamaguch 	uint8_t			 tphdata_ena;
    310  1.1  yamaguch 	uint8_t			 tphhead_ena;
    311  1.1  yamaguch 	uint8_t			 lrxqthresh;
    312  1.1  yamaguch 	uint8_t			 prefena;
    313  1.1  yamaguch };
    314  1.1  yamaguch 
    315  1.1  yamaguch static const struct ixl_hmc_pack ixl_hmc_pack_rxq[] = {
    316  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, head),		13,	0 },
    317  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, cpuid),		8,	13 },
    318  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, base),		57,	32 },
    319  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, qlen),		13,	89 },
    320  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dbuff),		7,	102 },
    321  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hbuff),		5,	109 },
    322  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dtype),		2,	114 },
    323  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, dsize),		1,	116 },
    324  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, crcstrip),	1,	117 },
    325  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, fc_ena),		1,	118 },
    326  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, l2sel),		1,	119 },
    327  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hsplit_0),	4,	120 },
    328  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, hsplit_1),	2,	124 },
    329  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, showiv),		1,	127 },
    330  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, rxmax),		14,	174 },
    331  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphrdesc_ena),	1,	193 },
    332  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphwdesc_ena),	1,	194 },
    333  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphdata_ena),	1,	195 },
    334  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, tphhead_ena),	1,	196 },
    335  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, lrxqthresh),	3,	198 },
    336  1.1  yamaguch 	{ offsetof(struct ixl_hmc_rxq, prefena),	1,	201 },
    337  1.1  yamaguch };
    338  1.1  yamaguch 
    339  1.1  yamaguch #define IXL_HMC_RXQ_MINSIZE (201 + 1)
    340  1.1  yamaguch 
    341  1.1  yamaguch struct ixl_hmc_txq {
    342  1.1  yamaguch 	uint16_t		head;
    343  1.1  yamaguch 	uint8_t			new_context;
    344  1.1  yamaguch 	uint64_t		base;
    345  1.1  yamaguch #define IXL_HMC_TXQ_BASE_UNIT		128
    346  1.1  yamaguch 	uint8_t			fc_ena;
    347  1.1  yamaguch 	uint8_t			timesync_ena;
    348  1.1  yamaguch 	uint8_t			fd_ena;
    349  1.1  yamaguch 	uint8_t			alt_vlan_ena;
    350  1.1  yamaguch 	uint16_t		thead_wb;
    351  1.1  yamaguch 	uint8_t			cpuid;
    352  1.1  yamaguch 	uint8_t			head_wb_ena;
    353  1.1  yamaguch #define IXL_HMC_TXQ_DESC_WB		0
    354  1.1  yamaguch #define IXL_HMC_TXQ_HEAD_WB		1
    355  1.1  yamaguch 	uint16_t		qlen;
    356  1.1  yamaguch 	uint8_t			tphrdesc_ena;
    357  1.1  yamaguch 	uint8_t			tphrpacket_ena;
    358  1.1  yamaguch 	uint8_t			tphwdesc_ena;
    359  1.1  yamaguch 	uint64_t		head_wb_addr;
    360  1.1  yamaguch 	uint32_t		crc;
    361  1.1  yamaguch 	uint16_t		rdylist;
    362  1.1  yamaguch 	uint8_t			rdylist_act;
    363  1.1  yamaguch };
    364  1.1  yamaguch 
    365  1.1  yamaguch static const struct ixl_hmc_pack ixl_hmc_pack_txq[] = {
    366  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head),		13,	0 },
    367  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, new_context),	1,	30 },
    368  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, base),		57,	32 },
    369  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, fc_ena),		1,	89 },
    370  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, timesync_ena),	1,	90 },
    371  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, fd_ena),		1,	91 },
    372  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, alt_vlan_ena),	1,	92 },
    373  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, cpuid),		8,	96 },
    374  1.1  yamaguch /* line 1 */
    375  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, thead_wb),	13,	0 + 128 },
    376  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head_wb_ena),	1,	32 + 128 },
    377  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, qlen),		13,	33 + 128 },
    378  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphrdesc_ena),	1,	46 + 128 },
    379  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphrpacket_ena),	1,	47 + 128 },
    380  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, tphwdesc_ena),	1,	48 + 128 },
    381  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, head_wb_addr),	64,	64 + 128 },
    382  1.1  yamaguch /* line 7 */
    383  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, crc),		32,	0 + (7*128) },
    384  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, rdylist),	10,	84 + (7*128) },
    385  1.1  yamaguch 	{ offsetof(struct ixl_hmc_txq, rdylist_act),	1,	94 + (7*128) },
    386  1.1  yamaguch };
    387  1.1  yamaguch 
    388  1.1  yamaguch #define IXL_HMC_TXQ_MINSIZE (94 + (7*128) + 1)
    389  1.1  yamaguch 
    390  1.1  yamaguch struct ixl_work {
    391  1.1  yamaguch 	struct work	 ixw_cookie;
    392  1.1  yamaguch 	void		(*ixw_func)(void *);
    393  1.1  yamaguch 	void		*ixw_arg;
    394  1.1  yamaguch 	unsigned int	 ixw_added;
    395  1.1  yamaguch };
    396  1.1  yamaguch #define IXL_WORKQUEUE_PRI	PRI_SOFTNET
    397  1.1  yamaguch 
    398  1.1  yamaguch struct ixl_tx_map {
    399  1.1  yamaguch 	struct mbuf		*txm_m;
    400  1.1  yamaguch 	bus_dmamap_t		 txm_map;
    401  1.1  yamaguch 	unsigned int		 txm_eop;
    402  1.1  yamaguch };
    403  1.1  yamaguch 
    404  1.1  yamaguch struct ixl_tx_ring {
    405  1.1  yamaguch 	kmutex_t		 txr_lock;
    406  1.1  yamaguch 	struct ixl_softc	*txr_sc;
    407  1.1  yamaguch 
    408  1.1  yamaguch 	unsigned int		 txr_prod;
    409  1.1  yamaguch 	unsigned int		 txr_cons;
    410  1.1  yamaguch 
    411  1.1  yamaguch 	struct ixl_tx_map	*txr_maps;
    412  1.1  yamaguch 	struct ixl_dmamem	 txr_mem;
    413  1.1  yamaguch 
    414  1.1  yamaguch 	bus_size_t		 txr_tail;
    415  1.1  yamaguch 	unsigned int		 txr_qid;
    416  1.1  yamaguch 	pcq_t			*txr_intrq;
    417  1.1  yamaguch 	void			*txr_si;
    418  1.1  yamaguch 
    419  1.1  yamaguch 	uint64_t		 txr_oerrors;	/* if_oerrors */
    420  1.1  yamaguch 	uint64_t		 txr_opackets;	/* if_opackets */
    421  1.1  yamaguch 	uint64_t		 txr_obytes;	/* if_obytes */
    422  1.1  yamaguch 	uint64_t		 txr_omcasts;	/* if_omcasts */
    423  1.1  yamaguch 
    424  1.1  yamaguch 	struct evcnt		 txr_defragged;
    425  1.1  yamaguch 	struct evcnt		 txr_defrag_failed;
    426  1.1  yamaguch 	struct evcnt		 txr_pcqdrop;
    427  1.1  yamaguch 	struct evcnt		 txr_transmitdef;
    428  1.1  yamaguch 	struct evcnt		 txr_intr;
    429  1.1  yamaguch 	struct evcnt		 txr_defer;
    430  1.1  yamaguch };
    431  1.1  yamaguch 
    432  1.1  yamaguch struct ixl_rx_map {
    433  1.1  yamaguch 	struct mbuf		*rxm_m;
    434  1.1  yamaguch 	bus_dmamap_t		 rxm_map;
    435  1.1  yamaguch };
    436  1.1  yamaguch 
    437  1.1  yamaguch struct ixl_rx_ring {
    438  1.1  yamaguch 	kmutex_t		 rxr_lock;
    439  1.1  yamaguch 
    440  1.1  yamaguch 	unsigned int		 rxr_prod;
    441  1.1  yamaguch 	unsigned int		 rxr_cons;
    442  1.1  yamaguch 
    443  1.1  yamaguch 	struct ixl_rx_map	*rxr_maps;
    444  1.1  yamaguch 	struct ixl_dmamem	 rxr_mem;
    445  1.1  yamaguch 
    446  1.1  yamaguch 	struct mbuf		*rxr_m_head;
    447  1.1  yamaguch 	struct mbuf		**rxr_m_tail;
    448  1.1  yamaguch 
    449  1.1  yamaguch 	bus_size_t		 rxr_tail;
    450  1.1  yamaguch 	unsigned int		 rxr_qid;
    451  1.1  yamaguch 
    452  1.1  yamaguch 	uint64_t		 rxr_ipackets;	/* if_ipackets */
    453  1.1  yamaguch 	uint64_t		 rxr_ibytes;	/* if_ibytes */
    454  1.1  yamaguch 	uint64_t		 rxr_iqdrops;	/* iqdrops */
    455  1.1  yamaguch 	uint64_t		 rxr_ierrors;	/* if_ierrors */
    456  1.1  yamaguch 
    457  1.1  yamaguch 	struct evcnt		 rxr_mgethdr_failed;
    458  1.1  yamaguch 	struct evcnt		 rxr_mgetcl_failed;
    459  1.1  yamaguch 	struct evcnt		 rxr_mbuf_load_failed;
    460  1.1  yamaguch 	struct evcnt		 rxr_intr;
    461  1.1  yamaguch 	struct evcnt		 rxr_defer;
    462  1.1  yamaguch };
    463  1.1  yamaguch 
    464  1.1  yamaguch struct ixl_queue_pair {
    465  1.1  yamaguch 	struct ixl_softc	*qp_sc;
    466  1.1  yamaguch 	struct ixl_tx_ring	*qp_txr;
    467  1.1  yamaguch 	struct ixl_rx_ring	*qp_rxr;
    468  1.1  yamaguch 
    469  1.1  yamaguch 	char			 qp_name[16];
    470  1.1  yamaguch 
    471  1.1  yamaguch 	void			*qp_si;
    472  1.1  yamaguch 	struct ixl_work		 qp_task;
    473  1.1  yamaguch 	bool			 qp_workqueue;
    474  1.1  yamaguch };
    475  1.1  yamaguch 
    476  1.1  yamaguch struct ixl_atq {
    477  1.1  yamaguch 	struct ixl_aq_desc	 iatq_desc;
    478  1.1  yamaguch 	void			(*iatq_fn)(struct ixl_softc *);
    479  1.1  yamaguch };
    480  1.1  yamaguch SIMPLEQ_HEAD(ixl_atq_list, ixl_atq);
    481  1.1  yamaguch 
    482  1.1  yamaguch struct ixl_product {
    483  1.1  yamaguch 	unsigned int	 vendor_id;
    484  1.1  yamaguch 	unsigned int	 product_id;
    485  1.1  yamaguch };
    486  1.1  yamaguch 
    487  1.1  yamaguch /*
    488  1.1  yamaguch  * Locking notes:
    489  1.1  yamaguch  * + a field in ixl_tx_ring is protected by txr_lock (a spin mutex), and
    490  1.1  yamaguch  *   a field in ixl_rx_ring is protected by rxr_lock (a spin mutex).
    491  1.1  yamaguch  *    - more than one lock of them cannot be held at once.
    492  1.1  yamaguch  * + a field named sc_atq_* in ixl_softc is protected by sc_atq_lock
    493  1.1  yamaguch  *   (a spin mutex).
    494  1.1  yamaguch  *    - the lock cannot held with txr_lock or rxr_lock.
    495  1.1  yamaguch  * + a field named sc_arq_* is not protected by any lock.
    496  1.1  yamaguch  *    - operations for sc_arq_* is done in one context related to
    497  1.1  yamaguch  *      sc_arq_task.
    498  1.1  yamaguch  * + other fields in ixl_softc is protected by sc_cfg_lock
    499  1.1  yamaguch  *   (an adaptive mutex)
    500  1.1  yamaguch  *    - It must be held before another lock is held, and It can be
    501  1.1  yamaguch  *      released after the other lock is released.
    502  1.1  yamaguch  * */
    503  1.1  yamaguch 
    504  1.1  yamaguch struct ixl_softc {
    505  1.1  yamaguch 	device_t		 sc_dev;
    506  1.1  yamaguch 	struct ethercom		 sc_ec;
    507  1.1  yamaguch 	bool			 sc_attached;
    508  1.1  yamaguch 	bool			 sc_dead;
    509  1.1  yamaguch 	bool			 sc_rxctl_atq;
    510  1.1  yamaguch 	struct sysctllog	*sc_sysctllog;
    511  1.1  yamaguch 	struct workqueue	*sc_workq;
    512  1.1  yamaguch 	struct workqueue	*sc_workq_txrx;
    513  1.1  yamaguch 	uint8_t			 sc_enaddr[ETHER_ADDR_LEN];
    514  1.1  yamaguch 	struct ifmedia		 sc_media;
    515  1.1  yamaguch 	uint64_t		 sc_media_status;
    516  1.1  yamaguch 	uint64_t		 sc_media_active;
    517  1.1  yamaguch 	kmutex_t		 sc_cfg_lock;
    518  1.1  yamaguch 	enum i40e_mac_type	 sc_mac_type;
    519  1.1  yamaguch 	uint32_t		 sc_rss_table_size;
    520  1.1  yamaguch 	uint32_t		 sc_rss_table_entry_width;
    521  1.1  yamaguch 	bool			 sc_txrx_workqueue;
    522  1.1  yamaguch 	u_int			 sc_tx_process_limit;
    523  1.1  yamaguch 	u_int			 sc_rx_process_limit;
    524  1.1  yamaguch 	u_int			 sc_tx_intr_process_limit;
    525  1.1  yamaguch 	u_int			 sc_rx_intr_process_limit;
    526  1.1  yamaguch 
    527  1.1  yamaguch 	struct pci_attach_args	 sc_pa;
    528  1.1  yamaguch 	pci_intr_handle_t	*sc_ihp;
    529  1.1  yamaguch 	void			**sc_ihs;
    530  1.1  yamaguch 	unsigned int		 sc_nintrs;
    531  1.1  yamaguch 
    532  1.1  yamaguch 	bus_dma_tag_t		 sc_dmat;
    533  1.1  yamaguch 	bus_space_tag_t		 sc_memt;
    534  1.1  yamaguch 	bus_space_handle_t	 sc_memh;
    535  1.1  yamaguch 	bus_size_t		 sc_mems;
    536  1.1  yamaguch 
    537  1.1  yamaguch 	uint8_t			 sc_pf_id;
    538  1.1  yamaguch 	uint16_t		 sc_uplink_seid;	/* le */
    539  1.1  yamaguch 	uint16_t		 sc_downlink_seid;	/* le */
    540  1.1  yamaguch 	uint16_t		 sc_vsi_number;		/* le */
    541  1.1  yamaguch 	uint16_t		 sc_seid;
    542  1.1  yamaguch 	unsigned int		 sc_base_queue;
    543  1.1  yamaguch 
    544  1.1  yamaguch 	pci_intr_type_t		 sc_intrtype;
    545  1.1  yamaguch 	unsigned int		 sc_msix_vector_queue;
    546  1.1  yamaguch 
    547  1.1  yamaguch 	struct ixl_dmamem	 sc_scratch;
    548  1.1  yamaguch 
    549  1.1  yamaguch 	const struct ixl_aq_regs *
    550  1.1  yamaguch 				 sc_aq_regs;
    551  1.1  yamaguch 
    552  1.1  yamaguch 	kmutex_t		 sc_atq_lock;
    553  1.1  yamaguch 	kcondvar_t		 sc_atq_cv;
    554  1.1  yamaguch 	struct ixl_dmamem	 sc_atq;
    555  1.1  yamaguch 	unsigned int		 sc_atq_prod;
    556  1.1  yamaguch 	unsigned int		 sc_atq_cons;
    557  1.1  yamaguch 
    558  1.1  yamaguch 	struct ixl_dmamem	 sc_arq;
    559  1.1  yamaguch 	struct ixl_work		 sc_arq_task;
    560  1.1  yamaguch 	struct ixl_aq_bufs	 sc_arq_idle;
    561  1.1  yamaguch 	struct ixl_aq_buf	*sc_arq_live[IXL_AQ_NUM];
    562  1.1  yamaguch 	unsigned int		 sc_arq_prod;
    563  1.1  yamaguch 	unsigned int		 sc_arq_cons;
    564  1.1  yamaguch 
    565  1.1  yamaguch 	struct ixl_work		 sc_link_state_task;
    566  1.1  yamaguch 	struct ixl_atq		 sc_link_state_atq;
    567  1.1  yamaguch 
    568  1.1  yamaguch 	struct ixl_dmamem	 sc_hmc_sd;
    569  1.1  yamaguch 	struct ixl_dmamem	 sc_hmc_pd;
    570  1.1  yamaguch 	struct ixl_hmc_entry	 sc_hmc_entries[IXL_HMC_COUNT];
    571  1.1  yamaguch 
    572  1.1  yamaguch 	unsigned int		 sc_tx_ring_ndescs;
    573  1.1  yamaguch 	unsigned int		 sc_rx_ring_ndescs;
    574  1.1  yamaguch 	unsigned int		 sc_nqueue_pairs;
    575  1.1  yamaguch 	unsigned int		 sc_nqueue_pairs_max;
    576  1.1  yamaguch 	unsigned int		 sc_nqueue_pairs_device;
    577  1.1  yamaguch 	struct ixl_queue_pair	*sc_qps;
    578  1.1  yamaguch 
    579  1.1  yamaguch 	struct evcnt		 sc_event_atq;
    580  1.1  yamaguch 	struct evcnt		 sc_event_link;
    581  1.1  yamaguch 	struct evcnt		 sc_event_ecc_err;
    582  1.1  yamaguch 	struct evcnt		 sc_event_pci_exception;
    583  1.1  yamaguch 	struct evcnt		 sc_event_crit_err;
    584  1.1  yamaguch };
    585  1.1  yamaguch 
    586  1.1  yamaguch #define IXL_TXRX_PROCESS_UNLIMIT	UINT_MAX
    587  1.1  yamaguch #define IXL_TX_PROCESS_LIMIT		256
    588  1.1  yamaguch #define IXL_RX_PROCESS_LIMIT		256
    589  1.1  yamaguch #define IXL_TX_INTR_PROCESS_LIMIT	256
    590  1.1  yamaguch #define IXL_RX_INTR_PROCESS_LIMIT	0U
    591  1.1  yamaguch 
    592  1.1  yamaguch #define delaymsec(_x)	DELAY(1000 * (_x))
    593  1.1  yamaguch #ifdef IXL_DEBUG
    594  1.1  yamaguch #define DDPRINTF(sc, fmt, args...)	\
    595  1.1  yamaguch do {					\
    596  1.1  yamaguch 	if (sc != NULL)				\
    597  1.1  yamaguch 		device_printf(sc->sc_dev, "");	\
    598  1.1  yamaguch 	printf("%s:\t" fmt, __func__, ##args);	\
    599  1.1  yamaguch } while (0)
    600  1.1  yamaguch #else
    601  1.1  yamaguch #define DDPRINTF(sc, fmt, args...)	__nothing
    602  1.1  yamaguch #endif
    603  1.1  yamaguch #define IXL_NOMSIX	false
    604  1.1  yamaguch 
    605  1.1  yamaguch static enum i40e_mac_type
    606  1.1  yamaguch     ixl_mactype(pci_product_id_t);
    607  1.1  yamaguch static void	ixl_clear_hw(struct ixl_softc *);
    608  1.1  yamaguch static int	ixl_pf_reset(struct ixl_softc *);
    609  1.1  yamaguch 
    610  1.1  yamaguch static int	ixl_dmamem_alloc(struct ixl_softc *, struct ixl_dmamem *,
    611  1.1  yamaguch 		    bus_size_t, bus_size_t);
    612  1.1  yamaguch static void	ixl_dmamem_free(struct ixl_softc *, struct ixl_dmamem *);
    613  1.1  yamaguch 
    614  1.1  yamaguch static int	ixl_arq_fill(struct ixl_softc *);
    615  1.1  yamaguch static void	ixl_arq_unfill(struct ixl_softc *);
    616  1.1  yamaguch 
    617  1.1  yamaguch static int	ixl_atq_poll(struct ixl_softc *, struct ixl_aq_desc *,
    618  1.1  yamaguch 		    unsigned int);
    619  1.1  yamaguch static void	ixl_atq_set(struct ixl_atq *, void (*)(struct ixl_softc *));
    620  1.1  yamaguch static int	ixl_atq_post(struct ixl_softc *, struct ixl_atq *);
    621  1.1  yamaguch static int	ixl_atq_post_locked(struct ixl_softc *, struct ixl_atq *);
    622  1.1  yamaguch static void	ixl_atq_done(struct ixl_softc *);
    623  1.1  yamaguch static int	ixl_atq_exec(struct ixl_softc *, struct ixl_atq *);
    624  1.1  yamaguch static int	ixl_get_version(struct ixl_softc *);
    625  1.1  yamaguch static int	ixl_get_hw_capabilities(struct ixl_softc *);
    626  1.1  yamaguch static int	ixl_pxe_clear(struct ixl_softc *);
    627  1.1  yamaguch static int	ixl_lldp_shut(struct ixl_softc *);
    628  1.1  yamaguch static int	ixl_get_mac(struct ixl_softc *);
    629  1.1  yamaguch static int	ixl_get_switch_config(struct ixl_softc *);
    630  1.1  yamaguch static int	ixl_phy_mask_ints(struct ixl_softc *);
    631  1.1  yamaguch static int	ixl_get_phy_types(struct ixl_softc *, uint64_t *);
    632  1.1  yamaguch static int	ixl_restart_an(struct ixl_softc *);
    633  1.1  yamaguch static int	ixl_hmc(struct ixl_softc *);
    634  1.1  yamaguch static void	ixl_hmc_free(struct ixl_softc *);
    635  1.1  yamaguch static int	ixl_get_vsi(struct ixl_softc *);
    636  1.1  yamaguch static int	ixl_set_vsi(struct ixl_softc *);
    637  1.1  yamaguch static void	ixl_set_filter_control(struct ixl_softc *);
    638  1.1  yamaguch static int	ixl_get_link_status(struct ixl_softc *);
    639  1.1  yamaguch static int	ixl_set_link_status(struct ixl_softc *,
    640  1.1  yamaguch 		    const struct ixl_aq_desc *);
    641  1.1  yamaguch static void	ixl_config_rss(struct ixl_softc *);
    642  1.1  yamaguch static int	ixl_add_macvlan(struct ixl_softc *, const uint8_t *,
    643  1.1  yamaguch 		    uint16_t, uint16_t);
    644  1.1  yamaguch static int	ixl_remove_macvlan(struct ixl_softc *, uint8_t *, uint16_t,
    645  1.1  yamaguch 		    uint16_t);
    646  1.1  yamaguch static void	ixl_arq(void *);
    647  1.1  yamaguch static void	ixl_link_state_update(void *);
    648  1.1  yamaguch static void	ixl_hmc_pack(void *, const void *,
    649  1.1  yamaguch 		    const struct ixl_hmc_pack *, unsigned int);
    650  1.1  yamaguch static uint32_t	ixl_rd_rx_csr(struct ixl_softc *, uint32_t);
    651  1.1  yamaguch static void	ixl_wr_rx_csr(struct ixl_softc *, uint32_t, uint32_t);
    652  1.1  yamaguch 
    653  1.1  yamaguch static int	ixl_match(device_t, cfdata_t, void *);
    654  1.1  yamaguch static void	ixl_attach(device_t, device_t, void *);
    655  1.1  yamaguch static int	ixl_detach(device_t, int);
    656  1.1  yamaguch 
    657  1.1  yamaguch static void	ixl_media_add(struct ixl_softc *, uint64_t);
    658  1.1  yamaguch static int	ixl_media_change(struct ifnet *);
    659  1.1  yamaguch static void	ixl_media_status(struct ifnet *, struct ifmediareq *);
    660  1.1  yamaguch static void	ixl_watchdog(struct ifnet *);
    661  1.1  yamaguch static int	ixl_ioctl(struct ifnet *, u_long, void *);
    662  1.1  yamaguch static void	ixl_start(struct ifnet *);
    663  1.1  yamaguch static int	ixl_transmit(struct ifnet *, struct mbuf *);
    664  1.1  yamaguch static void	ixl_deferred_transmit(void *);
    665  1.1  yamaguch static int	ixl_intr(void *);
    666  1.1  yamaguch static int	ixl_queue_intr(void *);
    667  1.1  yamaguch static int	ixl_other_intr(void *);
    668  1.1  yamaguch static void	ixl_handle_queue(void *);
    669  1.1  yamaguch static void	ixl_sched_handle_queue(struct ixl_softc *,
    670  1.1  yamaguch 		    struct ixl_queue_pair *);
    671  1.1  yamaguch static int	ixl_init(struct ifnet *);
    672  1.1  yamaguch static int	ixl_init_locked(struct ixl_softc *);
    673  1.1  yamaguch static void	ixl_stop(struct ifnet *, int);
    674  1.1  yamaguch static void	ixl_stop_locked(struct ixl_softc *);
    675  1.1  yamaguch static int	ixl_iff(struct ixl_softc *);
    676  1.1  yamaguch static int	ixl_ifflags_cb(struct ethercom *);
    677  1.1  yamaguch static int	ixl_setup_interrupts(struct ixl_softc *);
    678  1.1  yamaguch static int	ixl_establish_intx(struct ixl_softc *);
    679  1.1  yamaguch static int	ixl_establish_msix(struct ixl_softc *);
    680  1.1  yamaguch static void	ixl_set_affinity_msix(struct ixl_softc *);
    681  1.1  yamaguch static void	ixl_enable_queue_intr(struct ixl_softc *,
    682  1.1  yamaguch 		    struct ixl_queue_pair *);
    683  1.1  yamaguch static void	ixl_disable_queue_intr(struct ixl_softc *,
    684  1.1  yamaguch 		    struct ixl_queue_pair *);
    685  1.1  yamaguch static void	ixl_enable_other_intr(struct ixl_softc *);
    686  1.1  yamaguch static void	ixl_disable_other_intr(struct ixl_softc *);
    687  1.1  yamaguch static void	ixl_config_queue_intr(struct ixl_softc *);
    688  1.1  yamaguch static void	ixl_config_other_intr(struct ixl_softc *);
    689  1.1  yamaguch 
    690  1.1  yamaguch static struct ixl_tx_ring *
    691  1.1  yamaguch 		ixl_txr_alloc(struct ixl_softc *, unsigned int);
    692  1.1  yamaguch static void	ixl_txr_qdis(struct ixl_softc *, struct ixl_tx_ring *, int);
    693  1.1  yamaguch static void	ixl_txr_config(struct ixl_softc *, struct ixl_tx_ring *);
    694  1.1  yamaguch static int	ixl_txr_enabled(struct ixl_softc *, struct ixl_tx_ring *);
    695  1.1  yamaguch static int	ixl_txr_disabled(struct ixl_softc *, struct ixl_tx_ring *);
    696  1.1  yamaguch static void	ixl_txr_unconfig(struct ixl_softc *, struct ixl_tx_ring *);
    697  1.1  yamaguch static void	ixl_txr_clean(struct ixl_softc *, struct ixl_tx_ring *);
    698  1.1  yamaguch static void	ixl_txr_free(struct ixl_softc *, struct ixl_tx_ring *);
    699  1.1  yamaguch static int	ixl_txeof(struct ixl_softc *, struct ixl_tx_ring *, u_int);
    700  1.1  yamaguch 
    701  1.1  yamaguch static struct ixl_rx_ring *
    702  1.1  yamaguch 		ixl_rxr_alloc(struct ixl_softc *, unsigned int);
    703  1.1  yamaguch static void	ixl_rxr_config(struct ixl_softc *, struct ixl_rx_ring *);
    704  1.1  yamaguch static int	ixl_rxr_enabled(struct ixl_softc *, struct ixl_rx_ring *);
    705  1.1  yamaguch static int	ixl_rxr_disabled(struct ixl_softc *, struct ixl_rx_ring *);
    706  1.1  yamaguch static void	ixl_rxr_unconfig(struct ixl_softc *, struct ixl_rx_ring *);
    707  1.1  yamaguch static void	ixl_rxr_clean(struct ixl_softc *, struct ixl_rx_ring *);
    708  1.1  yamaguch static void	ixl_rxr_free(struct ixl_softc *, struct ixl_rx_ring *);
    709  1.1  yamaguch static int	ixl_rxeof(struct ixl_softc *, struct ixl_rx_ring *, u_int);
    710  1.1  yamaguch static int	ixl_rxfill(struct ixl_softc *, struct ixl_rx_ring *);
    711  1.1  yamaguch 
    712  1.1  yamaguch static struct workqueue *
    713  1.1  yamaguch     ixl_workq_create(const char *, pri_t, int, int);
    714  1.1  yamaguch static void	ixl_workq_destroy(struct workqueue *);
    715  1.1  yamaguch static int	ixl_workqs_teardown(device_t);
    716  1.1  yamaguch static void	ixl_work_set(struct ixl_work *, void (*)(void *), void *);
    717  1.1  yamaguch static void	ixl_work_add(struct workqueue *, struct ixl_work *);
    718  1.1  yamaguch static void	ixl_work_wait(struct workqueue *, struct ixl_work *);
    719  1.1  yamaguch static void	ixl_workq_work(struct work *, void *);
    720  1.1  yamaguch static const struct ixl_product *
    721  1.1  yamaguch 		ixl_lookup(const struct pci_attach_args *pa);
    722  1.1  yamaguch static void	ixl_link_status(struct ixl_softc *);
    723  1.1  yamaguch static int	ixl_set_macvlan(struct ixl_softc *);
    724  1.1  yamaguch static int	ixl_setup_interrupts(struct ixl_softc *);;
    725  1.1  yamaguch static void	ixl_teardown_interrupts(struct ixl_softc *);
    726  1.1  yamaguch static int	ixl_setup_stats(struct ixl_softc *);
    727  1.1  yamaguch static void	ixl_teardown_stats(struct ixl_softc *);
    728  1.1  yamaguch static int	ixl_setup_sysctls(struct ixl_softc *);
    729  1.1  yamaguch static void	ixl_teardown_sysctls(struct ixl_softc *);
    730  1.1  yamaguch static int	ixl_queue_pairs_alloc(struct ixl_softc *);
    731  1.1  yamaguch static void	ixl_queue_pairs_free(struct ixl_softc *);
    732  1.1  yamaguch 
    733  1.1  yamaguch static const struct ixl_phy_type ixl_phy_type_map[] = {
    734  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_SGMII,		IFM_1000_SGMII },
    735  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_KX,	IFM_1000_KX },
    736  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_KX4,	IFM_10G_KX4 },
    737  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_KR,	IFM_10G_KR },
    738  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_KR4,	IFM_40G_KR4 },
    739  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_XAUI |
    740  1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_XFI,		IFM_10G_CX4 },
    741  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_SFI,		IFM_10G_SFI },
    742  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_XLAUI |
    743  1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_XLPPI,		IFM_40G_XLPPI },
    744  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_CR4_CU |
    745  1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_40GBASE_CR4,	IFM_40G_CR4 },
    746  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_CR1_CU |
    747  1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_10GBASE_CR1,	IFM_10G_CR1 },
    748  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_AOC,	IFM_10G_AOC },
    749  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_AOC,	IFM_40G_AOC },
    750  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_100BASE_TX,	IFM_100_TX },
    751  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_T_OPTICAL |
    752  1.1  yamaguch 	  1ULL << IXL_PHY_TYPE_1000BASE_T,	IFM_1000_T },
    753  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_T,	IFM_10G_T },
    754  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_SR,	IFM_10G_SR },
    755  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_LR,	IFM_10G_LR },
    756  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_10GBASE_SFPP_CU,	IFM_10G_TWINAX },
    757  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_SR4,	IFM_40G_SR4 },
    758  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_40GBASE_LR4,	IFM_40G_LR4 },
    759  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_SX,	IFM_1000_SX },
    760  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_1000BASE_LX,	IFM_1000_LX },
    761  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_20GBASE_KR2,	IFM_20G_KR2 },
    762  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_KR,	IFM_25G_KR },
    763  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_CR,	IFM_25G_CR },
    764  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_SR,	IFM_25G_SR },
    765  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_LR,	IFM_25G_LR },
    766  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_AOC,	IFM_25G_AOC },
    767  1.1  yamaguch 	{ 1ULL << IXL_PHY_TYPE_25GBASE_ACC,	IFM_25G_CR },
    768  1.1  yamaguch };
    769  1.1  yamaguch 
    770  1.1  yamaguch static const struct ixl_speed_type ixl_speed_type_map[] = {
    771  1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_40GB,		IF_Gbps(40) },
    772  1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_25GB,		IF_Gbps(25) },
    773  1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_10GB,		IF_Gbps(10) },
    774  1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_1000MB,		IF_Mbps(1000) },
    775  1.1  yamaguch 	{ IXL_AQ_LINK_SPEED_100MB,		IF_Mbps(100)},
    776  1.1  yamaguch };
    777  1.1  yamaguch 
    778  1.1  yamaguch static const struct ixl_aq_regs ixl_pf_aq_regs = {
    779  1.1  yamaguch 	.atq_tail	= I40E_PF_ATQT,
    780  1.1  yamaguch 	.atq_tail_mask	= I40E_PF_ATQT_ATQT_MASK,
    781  1.1  yamaguch 	.atq_head	= I40E_PF_ATQH,
    782  1.1  yamaguch 	.atq_head_mask	= I40E_PF_ATQH_ATQH_MASK,
    783  1.1  yamaguch 	.atq_len	= I40E_PF_ATQLEN,
    784  1.1  yamaguch 	.atq_bal	= I40E_PF_ATQBAL,
    785  1.1  yamaguch 	.atq_bah	= I40E_PF_ATQBAH,
    786  1.1  yamaguch 	.atq_len_enable	= I40E_PF_ATQLEN_ATQENABLE_MASK,
    787  1.1  yamaguch 
    788  1.1  yamaguch 	.arq_tail	= I40E_PF_ARQT,
    789  1.1  yamaguch 	.arq_tail_mask	= I40E_PF_ARQT_ARQT_MASK,
    790  1.1  yamaguch 	.arq_head	= I40E_PF_ARQH,
    791  1.1  yamaguch 	.arq_head_mask	= I40E_PF_ARQH_ARQH_MASK,
    792  1.1  yamaguch 	.arq_len	= I40E_PF_ARQLEN,
    793  1.1  yamaguch 	.arq_bal	= I40E_PF_ARQBAL,
    794  1.1  yamaguch 	.arq_bah	= I40E_PF_ARQBAH,
    795  1.1  yamaguch 	.arq_len_enable	= I40E_PF_ARQLEN_ARQENABLE_MASK,
    796  1.1  yamaguch };
    797  1.1  yamaguch 
    798  1.1  yamaguch #define ixl_rd(_s, _r)			\
    799  1.1  yamaguch 	bus_space_read_4((_s)->sc_memt, (_s)->sc_memh, (_r))
    800  1.1  yamaguch #define ixl_wr(_s, _r, _v)		\
    801  1.1  yamaguch 	bus_space_write_4((_s)->sc_memt, (_s)->sc_memh, (_r), (_v))
    802  1.1  yamaguch #define ixl_barrier(_s, _r, _l, _o) \
    803  1.1  yamaguch     bus_space_barrier((_s)->sc_memt, (_s)->sc_memh, (_r), (_l), (_o))
    804  1.1  yamaguch #define ixl_flush(_s)	(void)ixl_rd((_s), I40E_GLGEN_STAT)
    805  1.1  yamaguch #define ixl_nqueues(_sc)	(1 << ((_sc)->sc_nqueue_pairs - 1))
    806  1.1  yamaguch #ifdef _LP64
    807  1.1  yamaguch #define ixl_dmamem_hi(_ixm)	(uint32_t)(IXL_DMA_DVA(_ixm) >> 32)
    808  1.1  yamaguch #else
    809  1.1  yamaguch #define ixl_dmamem_hi(_ixm)	0
    810  1.1  yamaguch #endif
    811  1.1  yamaguch #define ixl_dmamem_lo(_ixm)	(uint32_t)IXL_DMA_DVA(_ixm)
    812  1.1  yamaguch 
    813  1.1  yamaguch static inline void
    814  1.1  yamaguch ixl_aq_dva(struct ixl_aq_desc *iaq, bus_addr_t addr)
    815  1.1  yamaguch {
    816  1.1  yamaguch #ifdef _LP64
    817  1.1  yamaguch 	iaq->iaq_param[2] = htole32(addr >> 32);
    818  1.1  yamaguch #else
    819  1.1  yamaguch 	iaq->iaq_param[2] = htole32(0);
    820  1.1  yamaguch #endif
    821  1.1  yamaguch 	iaq->iaq_param[3] = htole32(addr);
    822  1.1  yamaguch }
    823  1.1  yamaguch 
    824  1.1  yamaguch static inline unsigned int
    825  1.1  yamaguch ixl_rxr_unrefreshed(unsigned int prod, unsigned int cons, unsigned int ndescs)
    826  1.1  yamaguch {
    827  1.1  yamaguch 	unsigned int num;
    828  1.1  yamaguch 
    829  1.1  yamaguch 	if (prod  < cons)
    830  1.1  yamaguch 		num = cons - prod;
    831  1.1  yamaguch 	else
    832  1.1  yamaguch 		num  = (ndescs - prod) + cons;
    833  1.1  yamaguch 
    834  1.1  yamaguch 	if (__predict_true(num > 0)) {
    835  1.1  yamaguch 		/* device cannot receive packets if all descripter is filled */
    836  1.1  yamaguch 		num -= 1;
    837  1.1  yamaguch 	}
    838  1.1  yamaguch 
    839  1.1  yamaguch 	return num;
    840  1.1  yamaguch }
    841  1.1  yamaguch 
    842  1.1  yamaguch CFATTACH_DECL3_NEW(ixl, sizeof(struct ixl_softc),
    843  1.1  yamaguch     ixl_match, ixl_attach, ixl_detach, NULL, NULL, NULL,
    844  1.1  yamaguch     DVF_DETACH_SHUTDOWN);
    845  1.1  yamaguch 
    846  1.1  yamaguch static const struct ixl_product ixl_products[] = {
    847  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_SFP },
    848  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_KX_B },
    849  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_KX_C },
    850  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_A },
    851  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_B },
    852  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_QSFP_C },
    853  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X710_10G_T },
    854  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_20G_BP_1 },
    855  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XL710_20G_BP_2 },
    856  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X710_T4_10G },
    857  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XXV710_25G_BP },
    858  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_XXV710_25G_SFP28 },
    859  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_KX },
    860  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_QSFP },
    861  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_SFP },
    862  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_1G_BASET },
    863  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_10G_BASET },
    864  1.1  yamaguch 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_X722_I_SFP },
    865  1.1  yamaguch 	/* required last entry */
    866  1.1  yamaguch 	{0, 0}
    867  1.1  yamaguch };
    868  1.1  yamaguch 
    869  1.1  yamaguch static const struct ixl_product *
    870  1.1  yamaguch ixl_lookup(const struct pci_attach_args *pa)
    871  1.1  yamaguch {
    872  1.1  yamaguch 	const struct ixl_product *ixlp;
    873  1.1  yamaguch 
    874  1.1  yamaguch 	for (ixlp = ixl_products; ixlp->vendor_id != 0; ixlp++) {
    875  1.1  yamaguch 		if (PCI_VENDOR(pa->pa_id) == ixlp->vendor_id &&
    876  1.1  yamaguch 		    PCI_PRODUCT(pa->pa_id) == ixlp->product_id)
    877  1.1  yamaguch 			return ixlp;
    878  1.1  yamaguch 	}
    879  1.1  yamaguch 
    880  1.1  yamaguch 	return NULL;
    881  1.1  yamaguch }
    882  1.1  yamaguch 
    883  1.1  yamaguch static int
    884  1.1  yamaguch ixl_match(device_t parent, cfdata_t match, void *aux)
    885  1.1  yamaguch {
    886  1.1  yamaguch 	const struct pci_attach_args *pa = aux;
    887  1.1  yamaguch 
    888  1.1  yamaguch 	return (ixl_lookup(pa) != NULL) ? 1 : 0;
    889  1.1  yamaguch }
    890  1.1  yamaguch 
    891  1.1  yamaguch static void
    892  1.1  yamaguch ixl_attach(device_t parent, device_t self, void *aux)
    893  1.1  yamaguch {
    894  1.1  yamaguch 	struct ixl_softc *sc;
    895  1.1  yamaguch 	struct pci_attach_args *pa = aux;
    896  1.1  yamaguch 	struct ifnet *ifp;
    897  1.1  yamaguch 	pcireg_t memtype, reg;
    898  1.1  yamaguch 	uint32_t firstq, port, ari, func;
    899  1.1  yamaguch 	uint64_t phy_types = 0;
    900  1.1  yamaguch 	char xnamebuf[32];
    901  1.1  yamaguch 	int tries, rv;
    902  1.1  yamaguch 
    903  1.1  yamaguch 	sc = device_private(self);
    904  1.1  yamaguch 	sc->sc_dev = self;
    905  1.1  yamaguch 	ifp = &sc->sc_ec.ec_if;
    906  1.1  yamaguch 
    907  1.1  yamaguch 	sc->sc_pa = *pa;
    908  1.1  yamaguch 	sc->sc_dmat = (pci_dma64_available(pa)) ?
    909  1.1  yamaguch 	    pa->pa_dmat64 : pa->pa_dmat;
    910  1.1  yamaguch 	sc->sc_aq_regs = &ixl_pf_aq_regs;
    911  1.1  yamaguch 
    912  1.1  yamaguch 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    913  1.1  yamaguch 	sc->sc_mac_type = ixl_mactype(PCI_PRODUCT(reg));
    914  1.1  yamaguch 
    915  1.1  yamaguch 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IXL_PCIREG);
    916  1.1  yamaguch 	if (pci_mapreg_map(pa, IXL_PCIREG, memtype, 0,
    917  1.1  yamaguch 	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems)) {
    918  1.1  yamaguch 		aprint_error(": unable to map registers\n");
    919  1.1  yamaguch 		return;
    920  1.1  yamaguch 	}
    921  1.1  yamaguch 
    922  1.1  yamaguch 	mutex_init(&sc->sc_cfg_lock, MUTEX_DEFAULT, IPL_SOFTNET);
    923  1.1  yamaguch 
    924  1.1  yamaguch 	firstq = ixl_rd(sc, I40E_PFLAN_QALLOC);
    925  1.1  yamaguch 	firstq &= I40E_PFLAN_QALLOC_FIRSTQ_MASK;
    926  1.1  yamaguch 	firstq >>= I40E_PFLAN_QALLOC_FIRSTQ_SHIFT;
    927  1.1  yamaguch 	sc->sc_base_queue = firstq;
    928  1.1  yamaguch 
    929  1.1  yamaguch 	ixl_clear_hw(sc);
    930  1.1  yamaguch 	if (ixl_pf_reset(sc) == -1) {
    931  1.1  yamaguch 		/* error printed by ixl pf_reset */
    932  1.1  yamaguch 		goto unmap;
    933  1.1  yamaguch 	}
    934  1.1  yamaguch 
    935  1.1  yamaguch 	port = ixl_rd(sc, I40E_PFGEN_PORTNUM);
    936  1.1  yamaguch 	port &= I40E_PFGEN_PORTNUM_PORT_NUM_MASK;
    937  1.1  yamaguch 	port >>= I40E_PFGEN_PORTNUM_PORT_NUM_SHIFT;
    938  1.1  yamaguch 	aprint_normal(": port %u", port);
    939  1.1  yamaguch 
    940  1.1  yamaguch 	ari = ixl_rd(sc, I40E_GLPCI_CAPSUP);
    941  1.1  yamaguch 	ari &= I40E_GLPCI_CAPSUP_ARI_EN_MASK;
    942  1.1  yamaguch 	ari >>= I40E_GLPCI_CAPSUP_ARI_EN_SHIFT;
    943  1.1  yamaguch 
    944  1.1  yamaguch 	func = ixl_rd(sc, I40E_PF_FUNC_RID);
    945  1.1  yamaguch 	sc->sc_pf_id = func & (ari ? 0xff : 0x7);
    946  1.1  yamaguch 
    947  1.1  yamaguch 	/* initialise the adminq */
    948  1.1  yamaguch 
    949  1.1  yamaguch 	mutex_init(&sc->sc_atq_lock, MUTEX_DEFAULT, IPL_NET);
    950  1.1  yamaguch 
    951  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_atq,
    952  1.1  yamaguch 	    sizeof(struct ixl_aq_desc) * IXL_AQ_NUM, IXL_AQ_ALIGN) != 0) {
    953  1.1  yamaguch 		aprint_error("\n" "%s: unable to allocate atq\n",
    954  1.1  yamaguch 		    device_xname(self));
    955  1.1  yamaguch 		goto unmap;
    956  1.1  yamaguch 	}
    957  1.1  yamaguch 
    958  1.1  yamaguch 	SIMPLEQ_INIT(&sc->sc_arq_idle);
    959  1.1  yamaguch 	ixl_work_set(&sc->sc_arq_task, ixl_arq, sc);
    960  1.1  yamaguch 	sc->sc_arq_cons = 0;
    961  1.1  yamaguch 	sc->sc_arq_prod = 0;
    962  1.1  yamaguch 
    963  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_arq,
    964  1.1  yamaguch 	    sizeof(struct ixl_aq_desc) * IXL_AQ_NUM, IXL_AQ_ALIGN) != 0) {
    965  1.1  yamaguch 		aprint_error("\n" "%s: unable to allocate arq\n",
    966  1.1  yamaguch 		    device_xname(self));
    967  1.1  yamaguch 		goto free_atq;
    968  1.1  yamaguch 	}
    969  1.1  yamaguch 
    970  1.1  yamaguch 	if (!ixl_arq_fill(sc)) {
    971  1.1  yamaguch 		aprint_error("\n" "%s: unable to fill arq descriptors\n",
    972  1.1  yamaguch 		    device_xname(self));
    973  1.1  yamaguch 		goto free_arq;
    974  1.1  yamaguch 	}
    975  1.1  yamaguch 
    976  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
    977  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
    978  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    979  1.1  yamaguch 
    980  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
    981  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
    982  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    983  1.1  yamaguch 
    984  1.1  yamaguch 	for (tries = 0; tries < 10; tries++) {
    985  1.1  yamaguch 		sc->sc_atq_cons = 0;
    986  1.1  yamaguch 		sc->sc_atq_prod = 0;
    987  1.1  yamaguch 
    988  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
    989  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
    990  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
    991  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
    992  1.1  yamaguch 
    993  1.1  yamaguch 		ixl_barrier(sc, 0, sc->sc_mems, BUS_SPACE_BARRIER_WRITE);
    994  1.1  yamaguch 
    995  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_bal,
    996  1.1  yamaguch 		    ixl_dmamem_lo(&sc->sc_atq));
    997  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_bah,
    998  1.1  yamaguch 		    ixl_dmamem_hi(&sc->sc_atq));
    999  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->atq_len,
   1000  1.1  yamaguch 		    sc->sc_aq_regs->atq_len_enable | IXL_AQ_NUM);
   1001  1.1  yamaguch 
   1002  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_bal,
   1003  1.1  yamaguch 		    ixl_dmamem_lo(&sc->sc_arq));
   1004  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_bah,
   1005  1.1  yamaguch 		    ixl_dmamem_hi(&sc->sc_arq));
   1006  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_len,
   1007  1.1  yamaguch 		    sc->sc_aq_regs->arq_len_enable | IXL_AQ_NUM);
   1008  1.1  yamaguch 
   1009  1.1  yamaguch 		rv = ixl_get_version(sc);
   1010  1.1  yamaguch 		if (rv == 0)
   1011  1.1  yamaguch 			break;
   1012  1.1  yamaguch 		if (rv != ETIMEDOUT) {
   1013  1.1  yamaguch 			aprint_error(", unable to get firmware version\n");
   1014  1.1  yamaguch 			goto shutdown;
   1015  1.1  yamaguch 		}
   1016  1.1  yamaguch 
   1017  1.1  yamaguch 		delaymsec(100);
   1018  1.1  yamaguch 	}
   1019  1.1  yamaguch 
   1020  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
   1021  1.1  yamaguch 
   1022  1.1  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722)
   1023  1.1  yamaguch 		sc->sc_nqueue_pairs_device = 128;
   1024  1.1  yamaguch 	else
   1025  1.1  yamaguch 		sc->sc_nqueue_pairs_device = 64;
   1026  1.1  yamaguch 
   1027  1.1  yamaguch 	rv = ixl_get_hw_capabilities(sc);
   1028  1.1  yamaguch 	if (rv != 0) {
   1029  1.1  yamaguch 		aprint_error(", GET HW CAPABILITIES %s\n",
   1030  1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1031  1.1  yamaguch 		goto shutdown;
   1032  1.1  yamaguch 	}
   1033  1.1  yamaguch 
   1034  1.1  yamaguch 	sc->sc_nqueue_pairs = sc->sc_nqueue_pairs_max =
   1035  1.1  yamaguch 	    MIN((int)sc->sc_nqueue_pairs_device, ncpu);
   1036  1.1  yamaguch 	sc->sc_tx_ring_ndescs = 1024;
   1037  1.1  yamaguch 	sc->sc_rx_ring_ndescs = 1024;
   1038  1.1  yamaguch 
   1039  1.1  yamaguch 	KASSERT(IXL_TXRX_PROCESS_UNLIMIT > sc->sc_rx_ring_ndescs);
   1040  1.1  yamaguch 	KASSERT(IXL_TXRX_PROCESS_UNLIMIT > sc->sc_tx_ring_ndescs);
   1041  1.1  yamaguch 
   1042  1.1  yamaguch 	if (ixl_get_mac(sc) != 0) {
   1043  1.1  yamaguch 		/* error printed by ixl_get_mac */
   1044  1.1  yamaguch 		goto shutdown;
   1045  1.1  yamaguch 	}
   1046  1.1  yamaguch 
   1047  1.1  yamaguch 	aprint_normal("\n");
   1048  1.1  yamaguch 	aprint_naive("\n");
   1049  1.1  yamaguch 
   1050  1.1  yamaguch 	aprint_normal_dev(self, "Ethernet address %s\n",
   1051  1.1  yamaguch 	    ether_sprintf(sc->sc_enaddr));
   1052  1.1  yamaguch 
   1053  1.1  yamaguch 	rv = ixl_pxe_clear(sc);
   1054  1.1  yamaguch 	if (rv != 0) {
   1055  1.1  yamaguch 		aprint_debug_dev(self, "CLEAR PXE MODE %s\n",
   1056  1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1057  1.1  yamaguch 	}
   1058  1.1  yamaguch 
   1059  1.1  yamaguch 	ixl_set_filter_control(sc);
   1060  1.1  yamaguch 
   1061  1.1  yamaguch 	if (ixl_hmc(sc) != 0) {
   1062  1.1  yamaguch 		/* error printed by ixl_hmc */
   1063  1.1  yamaguch 		goto shutdown;
   1064  1.1  yamaguch 	}
   1065  1.1  yamaguch 
   1066  1.1  yamaguch 	if (ixl_lldp_shut(sc) != 0) {
   1067  1.1  yamaguch 		/* error printed by ixl_lldp_shut */
   1068  1.1  yamaguch 		goto free_hmc;
   1069  1.1  yamaguch 	}
   1070  1.1  yamaguch 
   1071  1.1  yamaguch 	if (ixl_phy_mask_ints(sc) != 0) {
   1072  1.1  yamaguch 		/* error printed by ixl_phy_mask_ints */
   1073  1.1  yamaguch 		goto free_hmc;
   1074  1.1  yamaguch 	}
   1075  1.1  yamaguch 
   1076  1.1  yamaguch 	if (ixl_restart_an(sc) != 0) {
   1077  1.1  yamaguch 		/* error printed by ixl_restart_an */
   1078  1.1  yamaguch 		goto free_hmc;
   1079  1.1  yamaguch 	}
   1080  1.1  yamaguch 
   1081  1.1  yamaguch 	if (ixl_get_switch_config(sc) != 0) {
   1082  1.1  yamaguch 		/* error printed by ixl_get_switch_config */
   1083  1.1  yamaguch 		goto free_hmc;
   1084  1.1  yamaguch 	}
   1085  1.1  yamaguch 
   1086  1.1  yamaguch 	if (ixl_get_phy_types(sc, &phy_types) != 0) {
   1087  1.1  yamaguch 		/* error printed by ixl_get_phy_abilities */
   1088  1.1  yamaguch 		goto free_hmc;
   1089  1.1  yamaguch 	}
   1090  1.1  yamaguch 
   1091  1.1  yamaguch 	rv = ixl_get_link_status(sc);
   1092  1.1  yamaguch 	if (rv != 0) {
   1093  1.1  yamaguch 		aprint_error_dev(self, "GET LINK STATUS %s\n",
   1094  1.1  yamaguch 		    rv == ETIMEDOUT ? "timeout" : "error");
   1095  1.1  yamaguch 		goto free_hmc;
   1096  1.1  yamaguch 	}
   1097  1.1  yamaguch 
   1098  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_scratch,
   1099  1.1  yamaguch 	    sizeof(struct ixl_aq_vsi_data), 8) != 0) {
   1100  1.1  yamaguch 		aprint_error_dev(self, "unable to allocate scratch buffer\n");
   1101  1.1  yamaguch 		goto free_hmc;
   1102  1.1  yamaguch 	}
   1103  1.1  yamaguch 
   1104  1.1  yamaguch 	if (ixl_get_vsi(sc) != 0) {
   1105  1.1  yamaguch 		/* error printed by ixl_get_vsi */
   1106  1.1  yamaguch 		goto free_scratch;
   1107  1.1  yamaguch 	}
   1108  1.1  yamaguch 
   1109  1.1  yamaguch 	if (ixl_set_vsi(sc) != 0) {
   1110  1.1  yamaguch 		/* error printed by ixl_set_vsi */
   1111  1.1  yamaguch 		goto free_scratch;
   1112  1.1  yamaguch 	}
   1113  1.1  yamaguch 
   1114  1.1  yamaguch 	if (ixl_queue_pairs_alloc(sc) != 0) {
   1115  1.1  yamaguch 		/* error printed by ixl_queue_pairs_alloc */
   1116  1.1  yamaguch 		goto free_scratch;
   1117  1.1  yamaguch 	}
   1118  1.1  yamaguch 
   1119  1.1  yamaguch 	if (ixl_setup_interrupts(sc) != 0) {
   1120  1.1  yamaguch 		/* error printed by ixl_setup_interrupts */
   1121  1.1  yamaguch 		goto free_queue_pairs;
   1122  1.1  yamaguch 	}
   1123  1.1  yamaguch 
   1124  1.1  yamaguch 	if (ixl_setup_stats(sc) != 0) {
   1125  1.1  yamaguch 		aprint_error_dev(self, "failed to setup event counters\n");
   1126  1.1  yamaguch 		goto teardown_intrs;
   1127  1.1  yamaguch 	}
   1128  1.1  yamaguch 
   1129  1.1  yamaguch 	if (ixl_setup_sysctls(sc) != 0) {
   1130  1.1  yamaguch 		/* error printed by ixl_setup_sysctls */
   1131  1.1  yamaguch 		goto teardown_stats;
   1132  1.1  yamaguch 	}
   1133  1.1  yamaguch 
   1134  1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_cfg", device_xname(self));
   1135  1.1  yamaguch 	sc->sc_workq = ixl_workq_create(xnamebuf, IXL_WORKQUEUE_PRI,
   1136  1.1  yamaguch 	    IPL_NET, WQ_PERCPU | WQ_MPSAFE);
   1137  1.1  yamaguch 	if (sc->sc_workq == NULL)
   1138  1.1  yamaguch 		goto teardown_sysctls;
   1139  1.1  yamaguch 
   1140  1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_wq_txrx", device_xname(self));
   1141  1.1  yamaguch 	sc->sc_workq_txrx = ixl_workq_create(xnamebuf, IXL_WORKQUEUE_PRI,
   1142  1.1  yamaguch 	    IPL_NET, WQ_PERCPU | WQ_MPSAFE);
   1143  1.1  yamaguch 	if (sc->sc_workq_txrx == NULL)
   1144  1.1  yamaguch 		goto teardown_wqs;
   1145  1.1  yamaguch 
   1146  1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s_atq_cv", device_xname(self));
   1147  1.1  yamaguch 	cv_init(&sc->sc_atq_cv, xnamebuf);
   1148  1.1  yamaguch 
   1149  1.1  yamaguch 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
   1150  1.1  yamaguch 
   1151  1.1  yamaguch 	ifp->if_softc = sc;
   1152  1.1  yamaguch 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1153  1.1  yamaguch 	ifp->if_extflags = IFEF_MPSAFE;
   1154  1.1  yamaguch 	ifp->if_ioctl = ixl_ioctl;
   1155  1.1  yamaguch 	ifp->if_start = ixl_start;
   1156  1.1  yamaguch 	ifp->if_transmit = ixl_transmit;
   1157  1.1  yamaguch 	ifp->if_watchdog = ixl_watchdog;
   1158  1.1  yamaguch 	ifp->if_init = ixl_init;
   1159  1.1  yamaguch 	ifp->if_stop = ixl_stop;
   1160  1.1  yamaguch 	IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_tx_ring_ndescs);
   1161  1.1  yamaguch 	IFQ_SET_READY(&ifp->if_snd);
   1162  1.1  yamaguch #if 0
   1163  1.1  yamaguch 	ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx |
   1164  1.1  yamaguch 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1165  1.1  yamaguch 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   1166  1.1  yamaguch #endif
   1167  1.1  yamaguch 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1168  1.1  yamaguch #if 0
   1169  1.1  yamaguch 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
   1170  1.1  yamaguch #endif
   1171  1.1  yamaguch 
   1172  1.1  yamaguch 	sc->sc_ec.ec_ifmedia = &sc->sc_media;
   1173  1.1  yamaguch 	ifmedia_init(&sc->sc_media, IFM_IMASK, ixl_media_change,
   1174  1.1  yamaguch 	    ixl_media_status);
   1175  1.1  yamaguch 
   1176  1.1  yamaguch 	ixl_media_add(sc, phy_types);
   1177  1.1  yamaguch 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
   1178  1.1  yamaguch 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
   1179  1.1  yamaguch 
   1180  1.1  yamaguch 	if_attach(ifp);
   1181  1.1  yamaguch 	if_deferred_start_init(ifp, NULL);
   1182  1.1  yamaguch 	ether_ifattach(ifp, sc->sc_enaddr);
   1183  1.1  yamaguch 	ether_set_ifflags_cb(&sc->sc_ec, ixl_ifflags_cb);
   1184  1.1  yamaguch 	(void)ixl_get_link_status(sc);
   1185  1.1  yamaguch 
   1186  1.1  yamaguch 	ixl_work_set(&sc->sc_link_state_task, ixl_link_state_update, sc);
   1187  1.1  yamaguch 
   1188  1.1  yamaguch 	ixl_config_other_intr(sc);
   1189  1.1  yamaguch 
   1190  1.1  yamaguch 	ixl_set_macvlan(sc);
   1191  1.1  yamaguch 
   1192  1.1  yamaguch 	ixl_enable_other_intr(sc);
   1193  1.1  yamaguch 
   1194  1.1  yamaguch 	sc->sc_txrx_workqueue = true;
   1195  1.1  yamaguch 	sc->sc_tx_process_limit = IXL_TX_PROCESS_LIMIT;
   1196  1.1  yamaguch 	sc->sc_rx_process_limit = IXL_RX_PROCESS_LIMIT;
   1197  1.1  yamaguch 	sc->sc_tx_intr_process_limit = IXL_TX_INTR_PROCESS_LIMIT;
   1198  1.1  yamaguch 	sc->sc_rx_intr_process_limit = IXL_RX_INTR_PROCESS_LIMIT;
   1199  1.1  yamaguch 
   1200  1.1  yamaguch 	if (pmf_device_register(self, NULL, NULL) != true)
   1201  1.1  yamaguch 		aprint_debug_dev(self, "couldn't establish power handler\n");
   1202  1.1  yamaguch 	sc->sc_attached = true;
   1203  1.1  yamaguch 	return;
   1204  1.1  yamaguch 
   1205  1.1  yamaguch teardown_wqs:
   1206  1.1  yamaguch 	config_finalize_register(self, ixl_workqs_teardown);
   1207  1.1  yamaguch teardown_sysctls:
   1208  1.1  yamaguch 	ixl_teardown_sysctls(sc);
   1209  1.1  yamaguch teardown_stats:
   1210  1.1  yamaguch 	ixl_teardown_stats(sc);
   1211  1.1  yamaguch teardown_intrs:
   1212  1.1  yamaguch 	ixl_teardown_interrupts(sc);
   1213  1.1  yamaguch free_queue_pairs:
   1214  1.1  yamaguch 	ixl_queue_pairs_free(sc);
   1215  1.1  yamaguch free_scratch:
   1216  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_scratch);
   1217  1.1  yamaguch free_hmc:
   1218  1.1  yamaguch 	ixl_hmc_free(sc);
   1219  1.1  yamaguch shutdown:
   1220  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
   1221  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
   1222  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
   1223  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
   1224  1.1  yamaguch 
   1225  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bal, 0);
   1226  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bah, 0);
   1227  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_len, 0);
   1228  1.1  yamaguch 
   1229  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bal, 0);
   1230  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bah, 0);
   1231  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_len, 0);
   1232  1.1  yamaguch 
   1233  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   1234  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   1235  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   1236  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   1237  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   1238  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1239  1.1  yamaguch 
   1240  1.1  yamaguch 	ixl_arq_unfill(sc);
   1241  1.1  yamaguch free_arq:
   1242  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_arq);
   1243  1.1  yamaguch free_atq:
   1244  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_atq);
   1245  1.1  yamaguch unmap:
   1246  1.1  yamaguch 	mutex_destroy(&sc->sc_atq_lock);
   1247  1.1  yamaguch 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
   1248  1.1  yamaguch 	mutex_destroy(&sc->sc_cfg_lock);
   1249  1.1  yamaguch 	sc->sc_mems = 0;
   1250  1.1  yamaguch 
   1251  1.1  yamaguch 	sc->sc_attached = false;
   1252  1.1  yamaguch }
   1253  1.1  yamaguch 
   1254  1.1  yamaguch static int
   1255  1.1  yamaguch ixl_detach(device_t self, int flags)
   1256  1.1  yamaguch {
   1257  1.1  yamaguch 	struct ixl_softc *sc = device_private(self);
   1258  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1259  1.1  yamaguch 
   1260  1.1  yamaguch 	if (!sc->sc_attached)
   1261  1.1  yamaguch 		return 0;
   1262  1.1  yamaguch 
   1263  1.1  yamaguch 	ixl_stop(ifp, 1);
   1264  1.1  yamaguch 
   1265  1.1  yamaguch 	if (sc->sc_workq != NULL) {
   1266  1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq);
   1267  1.1  yamaguch 		sc->sc_workq = NULL;
   1268  1.1  yamaguch 	}
   1269  1.1  yamaguch 
   1270  1.1  yamaguch 	if (sc->sc_workq_txrx != NULL) {
   1271  1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq_txrx);
   1272  1.1  yamaguch 		sc->sc_workq_txrx = NULL;
   1273  1.1  yamaguch 	}
   1274  1.1  yamaguch 
   1275  1.1  yamaguch 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
   1276  1.1  yamaguch 	ether_ifdetach(ifp);
   1277  1.1  yamaguch 	if_detach(ifp);
   1278  1.1  yamaguch 
   1279  1.1  yamaguch 	ixl_teardown_interrupts(sc);
   1280  1.1  yamaguch 	ixl_teardown_stats(sc);
   1281  1.1  yamaguch 
   1282  1.1  yamaguch 	ixl_queue_pairs_free(sc);
   1283  1.1  yamaguch 
   1284  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_scratch);
   1285  1.1  yamaguch 	ixl_hmc_free(sc);
   1286  1.1  yamaguch 
   1287  1.1  yamaguch 	/* shutdown */
   1288  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_head, 0);
   1289  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_head, 0);
   1290  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, 0);
   1291  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_tail, 0);
   1292  1.1  yamaguch 
   1293  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bal, 0);
   1294  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_bah, 0);
   1295  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_len, 0);
   1296  1.1  yamaguch 
   1297  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bal, 0);
   1298  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_bah, 0);
   1299  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->arq_len, 0);
   1300  1.1  yamaguch 
   1301  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   1302  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   1303  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   1304  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   1305  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   1306  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1307  1.1  yamaguch 
   1308  1.1  yamaguch 	ixl_arq_unfill(sc);
   1309  1.1  yamaguch 
   1310  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_arq);
   1311  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_atq);
   1312  1.1  yamaguch 
   1313  1.1  yamaguch 	cv_destroy(&sc->sc_atq_cv);
   1314  1.1  yamaguch 	mutex_destroy(&sc->sc_atq_lock);
   1315  1.1  yamaguch 
   1316  1.1  yamaguch 	if (sc->sc_mems != 0) {
   1317  1.1  yamaguch 		bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
   1318  1.1  yamaguch 		sc->sc_mems = 0;
   1319  1.1  yamaguch 	}
   1320  1.1  yamaguch 
   1321  1.1  yamaguch 	mutex_destroy(&sc->sc_cfg_lock);
   1322  1.1  yamaguch 
   1323  1.1  yamaguch 	return 0;
   1324  1.1  yamaguch }
   1325  1.1  yamaguch 
   1326  1.1  yamaguch static int
   1327  1.1  yamaguch ixl_workqs_teardown(device_t self)
   1328  1.1  yamaguch {
   1329  1.1  yamaguch 	struct ixl_softc *sc = device_private(self);
   1330  1.1  yamaguch 
   1331  1.1  yamaguch 	if (sc->sc_workq != NULL) {
   1332  1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq);
   1333  1.1  yamaguch 		sc->sc_workq = NULL;
   1334  1.1  yamaguch 	}
   1335  1.1  yamaguch 
   1336  1.1  yamaguch 	if (sc->sc_workq_txrx != NULL) {
   1337  1.1  yamaguch 		ixl_workq_destroy(sc->sc_workq_txrx);
   1338  1.1  yamaguch 		sc->sc_workq_txrx = NULL;
   1339  1.1  yamaguch 	}
   1340  1.1  yamaguch 
   1341  1.1  yamaguch 	return 0;
   1342  1.1  yamaguch }
   1343  1.1  yamaguch 
   1344  1.1  yamaguch static void
   1345  1.1  yamaguch ixl_media_add(struct ixl_softc *sc, uint64_t phy_types)
   1346  1.1  yamaguch {
   1347  1.1  yamaguch 	struct ifmedia *ifm = &sc->sc_media;
   1348  1.1  yamaguch 	const struct ixl_phy_type *itype;
   1349  1.1  yamaguch 	unsigned int i;
   1350  1.1  yamaguch 
   1351  1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_phy_type_map); i++) {
   1352  1.1  yamaguch 		itype = &ixl_phy_type_map[i];
   1353  1.1  yamaguch 
   1354  1.1  yamaguch 		if (ISSET(phy_types, itype->phy_type)) {
   1355  1.1  yamaguch 			ifmedia_add(ifm,
   1356  1.1  yamaguch 			    IFM_ETHER | IFM_FDX | itype->ifm_type, 0, NULL);
   1357  1.1  yamaguch 
   1358  1.1  yamaguch 			if (itype->ifm_type == IFM_100_TX) {
   1359  1.1  yamaguch 				ifmedia_add(ifm, IFM_ETHER | itype->ifm_type,
   1360  1.1  yamaguch 				    0, NULL);
   1361  1.1  yamaguch 			}
   1362  1.1  yamaguch 		}
   1363  1.1  yamaguch 	}
   1364  1.1  yamaguch }
   1365  1.1  yamaguch 
   1366  1.1  yamaguch static void
   1367  1.1  yamaguch ixl_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1368  1.1  yamaguch {
   1369  1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1370  1.1  yamaguch 
   1371  1.1  yamaguch 	ifmr->ifm_status = sc->sc_media_status;
   1372  1.1  yamaguch 	ifmr->ifm_active = sc->sc_media_active;
   1373  1.1  yamaguch 
   1374  1.1  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   1375  1.1  yamaguch 	if (ifp->if_link_state == LINK_STATE_UP)
   1376  1.1  yamaguch 		SET(ifmr->ifm_status, IFM_ACTIVE);
   1377  1.1  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   1378  1.1  yamaguch }
   1379  1.1  yamaguch 
   1380  1.1  yamaguch static int
   1381  1.1  yamaguch ixl_media_change(struct ifnet *ifp)
   1382  1.1  yamaguch {
   1383  1.1  yamaguch 
   1384  1.1  yamaguch 	return 0;
   1385  1.1  yamaguch }
   1386  1.1  yamaguch 
   1387  1.1  yamaguch static void
   1388  1.1  yamaguch ixl_watchdog(struct ifnet *ifp)
   1389  1.1  yamaguch {
   1390  1.1  yamaguch 
   1391  1.1  yamaguch }
   1392  1.1  yamaguch 
   1393  1.1  yamaguch static void
   1394  1.1  yamaguch ixl_del_all_multiaddr(struct ixl_softc *sc)
   1395  1.1  yamaguch {
   1396  1.1  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   1397  1.1  yamaguch 	struct ether_multi *enm;
   1398  1.1  yamaguch 	struct ether_multistep step;
   1399  1.1  yamaguch 
   1400  1.1  yamaguch 	ETHER_LOCK(ec);
   1401  1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1402  1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1403  1.1  yamaguch 		ixl_remove_macvlan(sc, enm->enm_addrlo, 0,
   1404  1.1  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1405  1.1  yamaguch 	}
   1406  1.1  yamaguch 	ETHER_UNLOCK(ec);
   1407  1.1  yamaguch }
   1408  1.1  yamaguch 
   1409  1.1  yamaguch static int
   1410  1.1  yamaguch ixl_add_multi(struct ixl_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
   1411  1.1  yamaguch {
   1412  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1413  1.1  yamaguch 	int rv;
   1414  1.1  yamaguch 
   1415  1.1  yamaguch 	if (ISSET(ifp->if_flags, IFF_ALLMULTI))
   1416  1.1  yamaguch 		return 0;
   1417  1.1  yamaguch 
   1418  1.1  yamaguch 	if (memcmp(addrlo, addrhi, ETHER_ADDR_LEN) != 0) {
   1419  1.1  yamaguch 		ixl_del_all_multiaddr(sc);
   1420  1.1  yamaguch 		SET(ifp->if_flags, IFF_ALLMULTI);
   1421  1.1  yamaguch 		return 0;
   1422  1.1  yamaguch 	}
   1423  1.1  yamaguch 
   1424  1.1  yamaguch 	rv = ixl_add_macvlan(sc, addrlo, 0,
   1425  1.1  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   1426  1.1  yamaguch 
   1427  1.1  yamaguch 	if (rv == IXL_AQ_RC_ENOSPC) {
   1428  1.1  yamaguch 		ixl_del_all_multiaddr(sc);
   1429  1.1  yamaguch 		SET(ifp->if_flags, IFF_ALLMULTI);
   1430  1.1  yamaguch 		return 0;
   1431  1.1  yamaguch 	}
   1432  1.1  yamaguch 
   1433  1.1  yamaguch 	if (rv != IXL_AQ_RC_OK)
   1434  1.1  yamaguch 		return EIO;
   1435  1.1  yamaguch 
   1436  1.1  yamaguch 	return 0;
   1437  1.1  yamaguch }
   1438  1.1  yamaguch 
   1439  1.1  yamaguch static void
   1440  1.1  yamaguch ixl_del_multi(struct ixl_softc *sc, uint8_t *addrlo, uint8_t *addrhi)
   1441  1.1  yamaguch {
   1442  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1443  1.1  yamaguch 	struct ethercom *ec = &sc->sc_ec;
   1444  1.1  yamaguch 	struct ether_multi *enm, *enm_last;
   1445  1.1  yamaguch 	struct ether_multistep step;
   1446  1.1  yamaguch 	int rv;
   1447  1.1  yamaguch 
   1448  1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_ALLMULTI)) {
   1449  1.1  yamaguch 		ixl_remove_macvlan(sc, addrlo, 0,
   1450  1.1  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1451  1.1  yamaguch 		return;
   1452  1.1  yamaguch 	}
   1453  1.1  yamaguch 
   1454  1.1  yamaguch 	ETHER_LOCK(ec);
   1455  1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1456  1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1457  1.1  yamaguch 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1458  1.1  yamaguch 		    ETHER_ADDR_LEN) != 0) {
   1459  1.1  yamaguch 			ETHER_UNLOCK(ec);
   1460  1.1  yamaguch 			return;
   1461  1.1  yamaguch 		}
   1462  1.1  yamaguch 	}
   1463  1.1  yamaguch 
   1464  1.1  yamaguch 	for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1465  1.1  yamaguch 	    ETHER_NEXT_MULTI(step, enm)) {
   1466  1.1  yamaguch 		rv = ixl_add_macvlan(sc, enm->enm_addrlo, 0,
   1467  1.1  yamaguch 		    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   1468  1.1  yamaguch 		if (rv != IXL_AQ_RC_OK)
   1469  1.1  yamaguch 			break;
   1470  1.1  yamaguch 	}
   1471  1.1  yamaguch 
   1472  1.1  yamaguch 	if (enm != NULL) {
   1473  1.1  yamaguch 		enm_last = enm;
   1474  1.1  yamaguch 		for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
   1475  1.1  yamaguch 		    ETHER_NEXT_MULTI(step, enm)) {
   1476  1.1  yamaguch 			if (enm == enm_last)
   1477  1.1  yamaguch 				break;
   1478  1.1  yamaguch 
   1479  1.1  yamaguch 			ixl_remove_macvlan(sc, enm->enm_addrlo, 0,
   1480  1.1  yamaguch 			    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1481  1.1  yamaguch 		}
   1482  1.1  yamaguch 	} else {
   1483  1.1  yamaguch 		CLR(ifp->if_flags, IFF_ALLMULTI);
   1484  1.1  yamaguch 	}
   1485  1.1  yamaguch 
   1486  1.1  yamaguch 	ETHER_UNLOCK(ec);
   1487  1.1  yamaguch }
   1488  1.1  yamaguch 
   1489  1.1  yamaguch static int
   1490  1.1  yamaguch ixl_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1491  1.1  yamaguch {
   1492  1.1  yamaguch 	struct ifreq *ifr = (struct ifreq *)data;
   1493  1.1  yamaguch 	struct ixl_softc *sc = (struct ixl_softc *)ifp->if_softc;
   1494  1.1  yamaguch 	struct ixl_tx_ring *txr;
   1495  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   1496  1.1  yamaguch 	const struct sockaddr *sa;
   1497  1.1  yamaguch 	uint8_t addrhi[ETHER_ADDR_LEN], addrlo[ETHER_ADDR_LEN];
   1498  1.1  yamaguch 	int s, error = 0;
   1499  1.1  yamaguch 	unsigned int i;
   1500  1.1  yamaguch 
   1501  1.1  yamaguch 	switch (cmd) {
   1502  1.1  yamaguch 	case SIOCADDMULTI:
   1503  1.1  yamaguch 		sa = ifreq_getaddr(SIOCADDMULTI, ifr);
   1504  1.1  yamaguch 		if (ether_addmulti(sa, &sc->sc_ec) == ENETRESET) {
   1505  1.1  yamaguch 			error = ether_multiaddr(sa, addrlo, addrhi);
   1506  1.1  yamaguch 			if (error != 0)
   1507  1.1  yamaguch 				return error;
   1508  1.1  yamaguch 
   1509  1.1  yamaguch 			if (ixl_add_multi(sc, addrlo, addrhi) != 0) {
   1510  1.1  yamaguch 				ether_delmulti(sa, &sc->sc_ec);
   1511  1.1  yamaguch 				error = EIO;
   1512  1.1  yamaguch 			}
   1513  1.1  yamaguch 		}
   1514  1.1  yamaguch 		break;
   1515  1.1  yamaguch 
   1516  1.1  yamaguch 	case SIOCDELMULTI:
   1517  1.1  yamaguch 		sa = ifreq_getaddr(SIOCDELMULTI, ifr);
   1518  1.1  yamaguch 		if (ether_delmulti(sa, &sc->sc_ec) == ENETRESET) {
   1519  1.1  yamaguch 			error = ether_multiaddr(sa, addrlo, addrhi);
   1520  1.1  yamaguch 			if (error != 0)
   1521  1.1  yamaguch 				return error;
   1522  1.1  yamaguch 
   1523  1.1  yamaguch 			ixl_del_multi(sc, addrlo, addrhi);
   1524  1.1  yamaguch 		}
   1525  1.1  yamaguch 		break;
   1526  1.1  yamaguch 
   1527  1.1  yamaguch 	case SIOCGIFDATA:
   1528  1.1  yamaguch 	case SIOCZIFDATA:
   1529  1.1  yamaguch 		ifp->if_ipackets = 0;
   1530  1.1  yamaguch 		ifp->if_ibytes = 0;
   1531  1.1  yamaguch 		ifp->if_iqdrops = 0;
   1532  1.1  yamaguch 		ifp->if_ierrors = 0;
   1533  1.1  yamaguch 		ifp->if_opackets = 0;
   1534  1.1  yamaguch 		ifp->if_obytes = 0;
   1535  1.1  yamaguch 		ifp->if_omcasts = 0;
   1536  1.1  yamaguch 
   1537  1.1  yamaguch 		for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   1538  1.1  yamaguch 			txr = sc->sc_qps[i].qp_txr;
   1539  1.1  yamaguch 			rxr = sc->sc_qps[i].qp_rxr;
   1540  1.1  yamaguch 
   1541  1.1  yamaguch 			mutex_enter(&rxr->rxr_lock);
   1542  1.1  yamaguch 			ifp->if_ipackets += rxr->rxr_ipackets;
   1543  1.1  yamaguch 			ifp->if_ibytes += rxr->rxr_ibytes;
   1544  1.1  yamaguch 			ifp->if_iqdrops += rxr->rxr_iqdrops;
   1545  1.1  yamaguch 			ifp->if_ierrors += rxr->rxr_ierrors;
   1546  1.1  yamaguch 			if (cmd == SIOCZIFDATA) {
   1547  1.1  yamaguch 				rxr->rxr_ipackets = 0;
   1548  1.1  yamaguch 				rxr->rxr_ibytes = 0;
   1549  1.1  yamaguch 				rxr->rxr_iqdrops = 0;
   1550  1.1  yamaguch 				rxr->rxr_ierrors = 0;
   1551  1.1  yamaguch 			}
   1552  1.1  yamaguch 			mutex_exit(&rxr->rxr_lock);
   1553  1.1  yamaguch 
   1554  1.1  yamaguch 			mutex_enter(&txr->txr_lock);
   1555  1.1  yamaguch 			ifp->if_opackets += txr->txr_opackets;
   1556  1.1  yamaguch 			ifp->if_obytes += txr->txr_opackets;
   1557  1.1  yamaguch 			ifp->if_omcasts += txr->txr_omcasts;
   1558  1.1  yamaguch 			if (cmd == SIOCZIFDATA) {
   1559  1.1  yamaguch 				txr->txr_opackets = 0;
   1560  1.1  yamaguch 				txr->txr_opackets = 0;
   1561  1.1  yamaguch 				txr->txr_omcasts = 0;
   1562  1.1  yamaguch 			}
   1563  1.1  yamaguch 			mutex_exit(&txr->txr_lock);
   1564  1.1  yamaguch 		}
   1565  1.1  yamaguch 		/* FALLTHROUGH */
   1566  1.1  yamaguch 	default:
   1567  1.1  yamaguch 		s = splnet();
   1568  1.1  yamaguch 		error = ether_ioctl(ifp, cmd, data);
   1569  1.1  yamaguch 		splx(s);
   1570  1.1  yamaguch 	}
   1571  1.1  yamaguch 
   1572  1.1  yamaguch 	if (error == ENETRESET)
   1573  1.1  yamaguch 		error = ixl_iff(sc);
   1574  1.1  yamaguch 
   1575  1.1  yamaguch 	return error;
   1576  1.1  yamaguch }
   1577  1.1  yamaguch 
   1578  1.1  yamaguch static enum i40e_mac_type
   1579  1.1  yamaguch ixl_mactype(pci_product_id_t id)
   1580  1.1  yamaguch {
   1581  1.1  yamaguch 
   1582  1.1  yamaguch 	switch (id) {
   1583  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_SFP:
   1584  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_KX_B:
   1585  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_KX_C:
   1586  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_A:
   1587  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_B:
   1588  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_QSFP_C:
   1589  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X710_10G_T:
   1590  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_20G_BP_1:
   1591  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XL710_20G_BP_2:
   1592  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X710_T4_10G:
   1593  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XXV710_25G_BP:
   1594  1.1  yamaguch 	case PCI_PRODUCT_INTEL_XXV710_25G_SFP28:
   1595  1.1  yamaguch 		return I40E_MAC_XL710;
   1596  1.1  yamaguch 
   1597  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_KX:
   1598  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_QSFP:
   1599  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_SFP:
   1600  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_1G_BASET:
   1601  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_10G_BASET:
   1602  1.1  yamaguch 	case PCI_PRODUCT_INTEL_X722_I_SFP:
   1603  1.1  yamaguch 		return I40E_MAC_X722;
   1604  1.1  yamaguch 	}
   1605  1.1  yamaguch 
   1606  1.1  yamaguch 	return I40E_MAC_GENERIC;
   1607  1.1  yamaguch }
   1608  1.1  yamaguch 
   1609  1.1  yamaguch static inline void *
   1610  1.1  yamaguch ixl_hmc_kva(struct ixl_softc *sc, enum ixl_hmc_types type, unsigned int i)
   1611  1.1  yamaguch {
   1612  1.1  yamaguch 	uint8_t *kva = IXL_DMA_KVA(&sc->sc_hmc_pd);
   1613  1.1  yamaguch 	struct ixl_hmc_entry *e = &sc->sc_hmc_entries[type];
   1614  1.1  yamaguch 
   1615  1.1  yamaguch 	if (i >= e->hmc_count)
   1616  1.1  yamaguch 		return NULL;
   1617  1.1  yamaguch 
   1618  1.1  yamaguch 	kva += e->hmc_base;
   1619  1.1  yamaguch 	kva += i * e->hmc_size;
   1620  1.1  yamaguch 
   1621  1.1  yamaguch 	return kva;
   1622  1.1  yamaguch }
   1623  1.1  yamaguch 
   1624  1.1  yamaguch static inline size_t
   1625  1.1  yamaguch ixl_hmc_len(struct ixl_softc *sc, enum ixl_hmc_types type)
   1626  1.1  yamaguch {
   1627  1.1  yamaguch 	struct ixl_hmc_entry *e = &sc->sc_hmc_entries[type];
   1628  1.1  yamaguch 
   1629  1.1  yamaguch 	return e->hmc_size;
   1630  1.1  yamaguch }
   1631  1.1  yamaguch 
   1632  1.1  yamaguch static void
   1633  1.1  yamaguch ixl_enable_queue_intr(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   1634  1.1  yamaguch {
   1635  1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   1636  1.1  yamaguch 
   1637  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTLN(rxr->rxr_qid),
   1638  1.1  yamaguch 	    I40E_PFINT_DYN_CTLN_INTENA_MASK |
   1639  1.1  yamaguch 	    I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
   1640  1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT));
   1641  1.1  yamaguch 	ixl_flush(sc);
   1642  1.1  yamaguch }
   1643  1.1  yamaguch 
   1644  1.1  yamaguch static void
   1645  1.1  yamaguch ixl_disable_queue_intr(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   1646  1.1  yamaguch {
   1647  1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   1648  1.1  yamaguch 
   1649  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTLN(rxr->rxr_qid),
   1650  1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT));
   1651  1.1  yamaguch 	ixl_flush(sc);
   1652  1.1  yamaguch }
   1653  1.1  yamaguch 
   1654  1.1  yamaguch static void
   1655  1.1  yamaguch ixl_enable_other_intr(struct ixl_softc *sc)
   1656  1.1  yamaguch {
   1657  1.1  yamaguch 
   1658  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTL0,
   1659  1.1  yamaguch 	    I40E_PFINT_DYN_CTL0_INTENA_MASK |
   1660  1.1  yamaguch 	    I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
   1661  1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT));
   1662  1.1  yamaguch 	ixl_flush(sc);
   1663  1.1  yamaguch }
   1664  1.1  yamaguch 
   1665  1.1  yamaguch static void
   1666  1.1  yamaguch ixl_disable_other_intr(struct ixl_softc *sc)
   1667  1.1  yamaguch {
   1668  1.1  yamaguch 
   1669  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_DYN_CTL0,
   1670  1.1  yamaguch 	    (IXL_NOITR << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT));
   1671  1.1  yamaguch 	ixl_flush(sc);
   1672  1.1  yamaguch }
   1673  1.1  yamaguch 
   1674  1.1  yamaguch static int
   1675  1.1  yamaguch ixl_reinit(struct ixl_softc *sc)
   1676  1.1  yamaguch {
   1677  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   1678  1.1  yamaguch 	struct ixl_tx_ring *txr;
   1679  1.1  yamaguch 	unsigned int i;
   1680  1.1  yamaguch 	uint32_t reg;
   1681  1.1  yamaguch 
   1682  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   1683  1.1  yamaguch 
   1684  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1685  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   1686  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   1687  1.1  yamaguch 
   1688  1.1  yamaguch 		txr->txr_cons = txr->txr_prod = 0;
   1689  1.1  yamaguch 		rxr->rxr_cons = rxr->rxr_prod = 0;
   1690  1.1  yamaguch 
   1691  1.1  yamaguch 		ixl_txr_config(sc, txr);
   1692  1.1  yamaguch 		ixl_rxr_config(sc, rxr);
   1693  1.1  yamaguch 	}
   1694  1.1  yamaguch 
   1695  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   1696  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_PREWRITE);
   1697  1.1  yamaguch 
   1698  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1699  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   1700  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   1701  1.1  yamaguch 
   1702  1.1  yamaguch 		ixl_wr(sc, I40E_QTX_CTL(i), I40E_QTX_CTL_PF_QUEUE |
   1703  1.1  yamaguch 		    (sc->sc_pf_id << I40E_QTX_CTL_PF_INDX_SHIFT));
   1704  1.1  yamaguch 		ixl_flush(sc);
   1705  1.1  yamaguch 
   1706  1.1  yamaguch 		ixl_wr(sc, txr->txr_tail, txr->txr_prod);
   1707  1.1  yamaguch 		ixl_wr(sc, rxr->rxr_tail, rxr->rxr_prod);
   1708  1.1  yamaguch 
   1709  1.1  yamaguch 
   1710  1.1  yamaguch 		/* ixl_rxfill() needs lock held */
   1711  1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   1712  1.1  yamaguch 		ixl_rxfill(sc, rxr);
   1713  1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   1714  1.1  yamaguch 
   1715  1.1  yamaguch 		reg = ixl_rd(sc, I40E_QRX_ENA(i));
   1716  1.1  yamaguch 		SET(reg, I40E_QRX_ENA_QENA_REQ_MASK);
   1717  1.1  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), reg);
   1718  1.1  yamaguch 		if (ixl_rxr_enabled(sc, rxr) != 0)
   1719  1.1  yamaguch 			goto stop;
   1720  1.1  yamaguch 
   1721  1.1  yamaguch 		ixl_txr_qdis(sc, txr, 1);
   1722  1.1  yamaguch 
   1723  1.1  yamaguch 		reg = ixl_rd(sc, I40E_QTX_ENA(i));
   1724  1.1  yamaguch 		SET(reg, I40E_QTX_ENA_QENA_REQ_MASK);
   1725  1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), reg);
   1726  1.1  yamaguch 
   1727  1.1  yamaguch 		if (ixl_txr_enabled(sc, txr) != 0)
   1728  1.1  yamaguch 			goto stop;
   1729  1.1  yamaguch 	}
   1730  1.1  yamaguch 
   1731  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   1732  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_POSTWRITE);
   1733  1.1  yamaguch 
   1734  1.1  yamaguch 	return 0;
   1735  1.1  yamaguch 
   1736  1.1  yamaguch stop:
   1737  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   1738  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd), BUS_DMASYNC_POSTWRITE);
   1739  1.1  yamaguch 
   1740  1.1  yamaguch 	return ETIMEDOUT;
   1741  1.1  yamaguch }
   1742  1.1  yamaguch 
   1743  1.1  yamaguch static int
   1744  1.1  yamaguch ixl_init_locked(struct ixl_softc *sc)
   1745  1.1  yamaguch {
   1746  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1747  1.1  yamaguch 	unsigned int i;
   1748  1.1  yamaguch 	int error;
   1749  1.1  yamaguch 
   1750  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   1751  1.1  yamaguch 
   1752  1.1  yamaguch 	if (sc->sc_dead) {
   1753  1.1  yamaguch 		return ENXIO;
   1754  1.1  yamaguch 	}
   1755  1.1  yamaguch 
   1756  1.1  yamaguch 	if (sc->sc_intrtype != PCI_INTR_TYPE_MSIX)
   1757  1.1  yamaguch 		sc->sc_nqueue_pairs = 1;
   1758  1.1  yamaguch 	else
   1759  1.1  yamaguch 		sc->sc_nqueue_pairs = sc->sc_nqueue_pairs_max;
   1760  1.1  yamaguch 
   1761  1.1  yamaguch 	error = ixl_reinit(sc);
   1762  1.1  yamaguch 	if (error) {
   1763  1.1  yamaguch 		ixl_stop_locked(sc);
   1764  1.1  yamaguch 		return error;
   1765  1.1  yamaguch 	}
   1766  1.1  yamaguch 
   1767  1.1  yamaguch 	SET(ifp->if_flags, IFF_RUNNING);
   1768  1.1  yamaguch 	CLR(ifp->if_flags, IFF_OACTIVE);
   1769  1.1  yamaguch 	ixl_link_status(sc);
   1770  1.1  yamaguch 
   1771  1.1  yamaguch 	ixl_config_rss(sc);
   1772  1.1  yamaguch 	ixl_config_queue_intr(sc);
   1773  1.1  yamaguch 
   1774  1.1  yamaguch 	ixl_enable_other_intr(sc);
   1775  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1776  1.1  yamaguch 		ixl_enable_queue_intr(sc, &sc->sc_qps[i]);
   1777  1.1  yamaguch 	}
   1778  1.1  yamaguch 
   1779  1.1  yamaguch 	error = ixl_iff(sc);
   1780  1.1  yamaguch 	if (error) {
   1781  1.1  yamaguch 		ixl_stop_locked(sc);
   1782  1.1  yamaguch 		return error;
   1783  1.1  yamaguch 	}
   1784  1.1  yamaguch 
   1785  1.1  yamaguch 	return 0;
   1786  1.1  yamaguch }
   1787  1.1  yamaguch 
   1788  1.1  yamaguch static int
   1789  1.1  yamaguch ixl_init(struct ifnet *ifp)
   1790  1.1  yamaguch {
   1791  1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1792  1.1  yamaguch 	int error;
   1793  1.1  yamaguch 
   1794  1.1  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   1795  1.1  yamaguch 	error = ixl_init_locked(sc);
   1796  1.1  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   1797  1.1  yamaguch 
   1798  1.1  yamaguch 	return error;
   1799  1.1  yamaguch }
   1800  1.1  yamaguch 
   1801  1.1  yamaguch static int
   1802  1.1  yamaguch ixl_iff(struct ixl_softc *sc)
   1803  1.1  yamaguch {
   1804  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1805  1.1  yamaguch 	struct ixl_atq iatq;
   1806  1.1  yamaguch 	struct ixl_aq_desc *iaq;
   1807  1.1  yamaguch 	struct ixl_aq_vsi_promisc_param *param;
   1808  1.1  yamaguch 	int error;
   1809  1.1  yamaguch 
   1810  1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   1811  1.1  yamaguch 		return 0;
   1812  1.1  yamaguch 
   1813  1.1  yamaguch 	memset(&iatq, 0, sizeof(iatq));
   1814  1.1  yamaguch 
   1815  1.1  yamaguch 	iaq = &iatq.iatq_desc;
   1816  1.1  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_SET_VSI_PROMISC);
   1817  1.1  yamaguch 
   1818  1.1  yamaguch 	param = (struct ixl_aq_vsi_promisc_param *)&iaq->iaq_param;
   1819  1.1  yamaguch 	param->flags = htole16(IXL_AQ_VSI_PROMISC_FLAG_BCAST |
   1820  1.1  yamaguch 	    IXL_AQ_VSI_PROMISC_FLAG_VLAN);
   1821  1.1  yamaguch 	if (ISSET(ifp->if_flags, IFF_PROMISC)) {
   1822  1.1  yamaguch 		param->flags |= htole16(IXL_AQ_VSI_PROMISC_FLAG_UCAST |
   1823  1.1  yamaguch 		    IXL_AQ_VSI_PROMISC_FLAG_MCAST);
   1824  1.1  yamaguch 	} else if (ISSET(ifp->if_flags, IFF_ALLMULTI)) {
   1825  1.1  yamaguch 		param->flags |= htole16(IXL_AQ_VSI_PROMISC_FLAG_MCAST);
   1826  1.1  yamaguch 	}
   1827  1.1  yamaguch 	param->valid_flags = htole16(IXL_AQ_VSI_PROMISC_FLAG_UCAST |
   1828  1.1  yamaguch 	    IXL_AQ_VSI_PROMISC_FLAG_MCAST | IXL_AQ_VSI_PROMISC_FLAG_BCAST |
   1829  1.1  yamaguch 	    IXL_AQ_VSI_PROMISC_FLAG_VLAN);
   1830  1.1  yamaguch 	param->seid = sc->sc_seid;
   1831  1.1  yamaguch 
   1832  1.1  yamaguch 	error = ixl_atq_exec(sc, &iatq);
   1833  1.1  yamaguch 	if (error)
   1834  1.1  yamaguch 		return error;
   1835  1.1  yamaguch 
   1836  1.1  yamaguch 	if (iaq->iaq_retval != htole16(IXL_AQ_RC_OK))
   1837  1.1  yamaguch 		return EIO;
   1838  1.1  yamaguch 
   1839  1.1  yamaguch 	if (memcmp(sc->sc_enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN) != 0) {
   1840  1.1  yamaguch 		ixl_remove_macvlan(sc, sc->sc_enaddr, 0,
   1841  1.1  yamaguch 		    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   1842  1.1  yamaguch 
   1843  1.1  yamaguch 		memcpy(sc->sc_enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1844  1.1  yamaguch 		ixl_add_macvlan(sc, sc->sc_enaddr, 0,
   1845  1.1  yamaguch 		    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   1846  1.1  yamaguch 	}
   1847  1.1  yamaguch 	return 0;
   1848  1.1  yamaguch }
   1849  1.1  yamaguch 
   1850  1.1  yamaguch static void
   1851  1.1  yamaguch ixl_stop_rendezvous(struct ixl_softc *sc)
   1852  1.1  yamaguch {
   1853  1.1  yamaguch 	struct ixl_tx_ring *txr;
   1854  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   1855  1.1  yamaguch 	unsigned int i;
   1856  1.1  yamaguch 
   1857  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1858  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   1859  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   1860  1.1  yamaguch 
   1861  1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   1862  1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   1863  1.1  yamaguch 
   1864  1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   1865  1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   1866  1.1  yamaguch 
   1867  1.1  yamaguch 		ixl_work_wait(sc->sc_workq_txrx,
   1868  1.1  yamaguch 		    &sc->sc_qps[i].qp_task);
   1869  1.1  yamaguch 	}
   1870  1.1  yamaguch 
   1871  1.1  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   1872  1.1  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   1873  1.1  yamaguch 
   1874  1.1  yamaguch 	ixl_work_wait(sc->sc_workq, &sc->sc_arq_task);
   1875  1.1  yamaguch 	ixl_work_wait(sc->sc_workq, &sc->sc_link_state_task);
   1876  1.1  yamaguch 
   1877  1.1  yamaguch }
   1878  1.1  yamaguch 
   1879  1.1  yamaguch static void
   1880  1.1  yamaguch ixl_stop_locked(struct ixl_softc *sc)
   1881  1.1  yamaguch {
   1882  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1883  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   1884  1.1  yamaguch 	struct ixl_tx_ring *txr;
   1885  1.1  yamaguch 	unsigned int i;
   1886  1.1  yamaguch 	uint32_t reg;
   1887  1.1  yamaguch 
   1888  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   1889  1.1  yamaguch 
   1890  1.1  yamaguch 	CLR(ifp->if_flags, IFF_RUNNING | IFF_OACTIVE);
   1891  1.1  yamaguch 
   1892  1.1  yamaguch 	ixl_disable_other_intr(sc);
   1893  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1894  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   1895  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   1896  1.1  yamaguch 
   1897  1.1  yamaguch 		ixl_disable_queue_intr(sc, &sc->sc_qps[i]);
   1898  1.1  yamaguch 
   1899  1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   1900  1.1  yamaguch 		ixl_txr_qdis(sc, txr, 0);
   1901  1.1  yamaguch 		/* XXX wait at least 400 usec for all tx queues in one go */
   1902  1.1  yamaguch 		ixl_flush(sc);
   1903  1.1  yamaguch 		DELAY(500);
   1904  1.1  yamaguch 
   1905  1.1  yamaguch 		reg = ixl_rd(sc, I40E_QTX_ENA(i));
   1906  1.1  yamaguch 		CLR(reg, I40E_QTX_ENA_QENA_REQ_MASK);
   1907  1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), reg);
   1908  1.1  yamaguch 		/* XXX wait 50ms from completaion of the TX queue disable*/
   1909  1.1  yamaguch 		ixl_flush(sc);
   1910  1.1  yamaguch 		DELAY(50);
   1911  1.1  yamaguch 
   1912  1.1  yamaguch 		if (ixl_txr_disabled(sc, txr) != 0) {
   1913  1.1  yamaguch 			mutex_exit(&txr->txr_lock);
   1914  1.1  yamaguch 			goto die;
   1915  1.1  yamaguch 		}
   1916  1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   1917  1.1  yamaguch 
   1918  1.1  yamaguch 		mutex_enter(&rxr->rxr_lock);
   1919  1.1  yamaguch 		reg = ixl_rd(sc, I40E_QRX_ENA(i));
   1920  1.1  yamaguch 		CLR(reg, I40E_QRX_ENA_QENA_REQ_MASK);
   1921  1.1  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), reg);
   1922  1.1  yamaguch 		/* XXX wait 50ms from completion of the RX queue disable */
   1923  1.1  yamaguch 		ixl_flush(sc);
   1924  1.1  yamaguch 		DELAY(50);
   1925  1.1  yamaguch 
   1926  1.1  yamaguch 		if (ixl_rxr_disabled(sc, rxr) != 0) {
   1927  1.1  yamaguch 			mutex_exit(&rxr->rxr_lock);
   1928  1.1  yamaguch 			goto die;
   1929  1.1  yamaguch 		}
   1930  1.1  yamaguch 		mutex_exit(&rxr->rxr_lock);
   1931  1.1  yamaguch 	}
   1932  1.1  yamaguch 
   1933  1.1  yamaguch 	ixl_stop_rendezvous(sc);
   1934  1.1  yamaguch 
   1935  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   1936  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   1937  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   1938  1.1  yamaguch 
   1939  1.1  yamaguch 		ixl_txr_unconfig(sc, txr);
   1940  1.1  yamaguch 		ixl_rxr_unconfig(sc, rxr);
   1941  1.1  yamaguch 
   1942  1.1  yamaguch 		ixl_txr_clean(sc, txr);
   1943  1.1  yamaguch 		ixl_rxr_clean(sc, rxr);
   1944  1.1  yamaguch 	}
   1945  1.1  yamaguch 
   1946  1.1  yamaguch 	return;
   1947  1.1  yamaguch die:
   1948  1.1  yamaguch 	sc->sc_dead = true;
   1949  1.1  yamaguch 	log(LOG_CRIT, "%s: failed to shut down rings",
   1950  1.1  yamaguch 	    device_xname(sc->sc_dev));
   1951  1.1  yamaguch 	return;
   1952  1.1  yamaguch }
   1953  1.1  yamaguch 
   1954  1.1  yamaguch static void
   1955  1.1  yamaguch ixl_stop(struct ifnet *ifp, int disable)
   1956  1.1  yamaguch {
   1957  1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   1958  1.1  yamaguch 
   1959  1.1  yamaguch 	mutex_enter(&sc->sc_cfg_lock);
   1960  1.1  yamaguch 	ixl_stop_locked(sc);
   1961  1.1  yamaguch 	mutex_exit(&sc->sc_cfg_lock);
   1962  1.1  yamaguch }
   1963  1.1  yamaguch 
   1964  1.1  yamaguch static int
   1965  1.1  yamaguch ixl_queue_pairs_alloc(struct ixl_softc *sc)
   1966  1.1  yamaguch {
   1967  1.1  yamaguch 	struct ixl_queue_pair *qp;
   1968  1.1  yamaguch 	unsigned int i;
   1969  1.1  yamaguch 	size_t sz;
   1970  1.1  yamaguch 
   1971  1.1  yamaguch 	sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   1972  1.1  yamaguch 	sc->sc_qps = kmem_zalloc(sz, KM_SLEEP);
   1973  1.1  yamaguch 
   1974  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   1975  1.1  yamaguch 		qp = &sc->sc_qps[i];
   1976  1.1  yamaguch 
   1977  1.1  yamaguch 		qp->qp_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1978  1.1  yamaguch 		    ixl_handle_queue, qp);
   1979  1.1  yamaguch 
   1980  1.1  yamaguch 		qp->qp_txr = ixl_txr_alloc(sc, i);
   1981  1.1  yamaguch 		if (qp->qp_txr == NULL)
   1982  1.1  yamaguch 			goto free;
   1983  1.1  yamaguch 
   1984  1.1  yamaguch 		qp->qp_rxr = ixl_rxr_alloc(sc, i);
   1985  1.1  yamaguch 		if (qp->qp_rxr == NULL)
   1986  1.1  yamaguch 			goto free;
   1987  1.1  yamaguch 
   1988  1.1  yamaguch 		qp->qp_sc = sc;
   1989  1.1  yamaguch 		ixl_work_set(&qp->qp_task, ixl_handle_queue, qp);
   1990  1.1  yamaguch 		snprintf(qp->qp_name, sizeof(qp->qp_name),
   1991  1.1  yamaguch 		    "%s-TXRX%d", device_xname(sc->sc_dev), i);
   1992  1.1  yamaguch 	}
   1993  1.1  yamaguch 
   1994  1.1  yamaguch 	return 0;
   1995  1.1  yamaguch free:
   1996  1.1  yamaguch 	if (sc->sc_qps != NULL) {
   1997  1.1  yamaguch 		for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   1998  1.1  yamaguch 			qp = &sc->sc_qps[i];
   1999  1.1  yamaguch 
   2000  1.1  yamaguch 			if (qp->qp_txr != NULL)
   2001  1.1  yamaguch 				ixl_txr_free(sc, qp->qp_txr);
   2002  1.1  yamaguch 			if (qp->qp_rxr != NULL)
   2003  1.1  yamaguch 				ixl_rxr_free(sc, qp->qp_rxr);
   2004  1.1  yamaguch 		}
   2005  1.1  yamaguch 
   2006  1.1  yamaguch 		sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   2007  1.1  yamaguch 		kmem_free(sc->sc_qps, sz);
   2008  1.1  yamaguch 		sc->sc_qps = NULL;
   2009  1.1  yamaguch 	}
   2010  1.1  yamaguch 
   2011  1.1  yamaguch 	return -1;
   2012  1.1  yamaguch }
   2013  1.1  yamaguch 
   2014  1.1  yamaguch static void
   2015  1.1  yamaguch ixl_queue_pairs_free(struct ixl_softc *sc)
   2016  1.1  yamaguch {
   2017  1.1  yamaguch 	struct ixl_queue_pair *qp;
   2018  1.1  yamaguch 	unsigned int i;
   2019  1.1  yamaguch 	size_t sz;
   2020  1.1  yamaguch 
   2021  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   2022  1.1  yamaguch 		qp = &sc->sc_qps[i];
   2023  1.1  yamaguch 		ixl_txr_free(sc, qp->qp_txr);
   2024  1.1  yamaguch 		ixl_rxr_free(sc, qp->qp_rxr);
   2025  1.1  yamaguch 	}
   2026  1.1  yamaguch 
   2027  1.1  yamaguch 	sz = sizeof(sc->sc_qps[0]) * sc->sc_nqueue_pairs_max;
   2028  1.1  yamaguch 	kmem_free(sc->sc_qps, sz);
   2029  1.1  yamaguch 	sc->sc_qps = NULL;
   2030  1.1  yamaguch }
   2031  1.1  yamaguch 
   2032  1.1  yamaguch static struct ixl_tx_ring *
   2033  1.1  yamaguch ixl_txr_alloc(struct ixl_softc *sc, unsigned int qid)
   2034  1.1  yamaguch {
   2035  1.1  yamaguch 	struct ixl_tx_ring *txr = NULL;
   2036  1.1  yamaguch 	struct ixl_tx_map *maps = NULL, *txm;
   2037  1.1  yamaguch 	unsigned int i;
   2038  1.1  yamaguch 
   2039  1.1  yamaguch 	txr = kmem_zalloc(sizeof(*txr), KM_SLEEP);
   2040  1.1  yamaguch 	maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_tx_ring_ndescs,
   2041  1.1  yamaguch 	    KM_SLEEP);
   2042  1.1  yamaguch 
   2043  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &txr->txr_mem,
   2044  1.1  yamaguch 	    sizeof(struct ixl_tx_desc) * sc->sc_tx_ring_ndescs,
   2045  1.1  yamaguch 	    IXL_TX_QUEUE_ALIGN) != 0)
   2046  1.1  yamaguch 	    goto free;
   2047  1.1  yamaguch 
   2048  1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2049  1.1  yamaguch 		txm = &maps[i];
   2050  1.1  yamaguch 
   2051  1.1  yamaguch 		if (bus_dmamap_create(sc->sc_dmat,
   2052  1.1  yamaguch 		    IXL_HARDMTU, IXL_TX_PKT_DESCS, IXL_HARDMTU, 0,
   2053  1.1  yamaguch 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &txm->txm_map) != 0)
   2054  1.1  yamaguch 			goto uncreate;
   2055  1.1  yamaguch 
   2056  1.1  yamaguch 		txm->txm_eop = -1;
   2057  1.1  yamaguch 		txm->txm_m = NULL;
   2058  1.1  yamaguch 	}
   2059  1.1  yamaguch 
   2060  1.1  yamaguch 	txr->txr_cons = txr->txr_prod = 0;
   2061  1.1  yamaguch 	txr->txr_maps = maps;
   2062  1.1  yamaguch 
   2063  1.1  yamaguch 	txr->txr_intrq = pcq_create(sc->sc_tx_ring_ndescs, KM_NOSLEEP);
   2064  1.1  yamaguch 	if (txr->txr_intrq == NULL)
   2065  1.1  yamaguch 		goto uncreate;
   2066  1.1  yamaguch 
   2067  1.1  yamaguch 	txr->txr_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   2068  1.1  yamaguch 	    ixl_deferred_transmit, txr);
   2069  1.1  yamaguch 	if (txr->txr_si == NULL)
   2070  1.1  yamaguch 		goto destroy_pcq;
   2071  1.1  yamaguch 
   2072  1.1  yamaguch 	txr->txr_tail = I40E_QTX_TAIL(qid);
   2073  1.1  yamaguch 	txr->txr_qid = qid;
   2074  1.1  yamaguch 	txr->txr_sc = sc;
   2075  1.1  yamaguch 	mutex_init(&txr->txr_lock, MUTEX_DEFAULT, IPL_NET);
   2076  1.1  yamaguch 
   2077  1.1  yamaguch 	return txr;
   2078  1.1  yamaguch 
   2079  1.1  yamaguch destroy_pcq:
   2080  1.1  yamaguch 	pcq_destroy(txr->txr_intrq);
   2081  1.1  yamaguch uncreate:
   2082  1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2083  1.1  yamaguch 		txm = &maps[i];
   2084  1.1  yamaguch 
   2085  1.1  yamaguch 		if (txm->txm_map == NULL)
   2086  1.1  yamaguch 			continue;
   2087  1.1  yamaguch 
   2088  1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, txm->txm_map);
   2089  1.1  yamaguch 	}
   2090  1.1  yamaguch 
   2091  1.1  yamaguch 	ixl_dmamem_free(sc, &txr->txr_mem);
   2092  1.1  yamaguch free:
   2093  1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
   2094  1.1  yamaguch 	kmem_free(txr, sizeof(*txr));
   2095  1.1  yamaguch 
   2096  1.1  yamaguch 	return NULL;
   2097  1.1  yamaguch }
   2098  1.1  yamaguch 
   2099  1.1  yamaguch static void
   2100  1.1  yamaguch ixl_txr_qdis(struct ixl_softc *sc, struct ixl_tx_ring *txr, int enable)
   2101  1.1  yamaguch {
   2102  1.1  yamaguch 	unsigned int qid;
   2103  1.1  yamaguch 	bus_size_t reg;
   2104  1.1  yamaguch 	uint32_t r;
   2105  1.1  yamaguch 
   2106  1.1  yamaguch 	qid = txr->txr_qid + sc->sc_base_queue;
   2107  1.1  yamaguch 	reg = I40E_GLLAN_TXPRE_QDIS(qid / 128);
   2108  1.1  yamaguch 	qid %= 128;
   2109  1.1  yamaguch 
   2110  1.1  yamaguch 	r = ixl_rd(sc, reg);
   2111  1.1  yamaguch 	CLR(r, I40E_GLLAN_TXPRE_QDIS_QINDX_MASK);
   2112  1.1  yamaguch 	SET(r, qid << I40E_GLLAN_TXPRE_QDIS_QINDX_SHIFT);
   2113  1.1  yamaguch 	SET(r, enable ? I40E_GLLAN_TXPRE_QDIS_CLEAR_QDIS_MASK :
   2114  1.1  yamaguch 	    I40E_GLLAN_TXPRE_QDIS_SET_QDIS_MASK);
   2115  1.1  yamaguch 	ixl_wr(sc, reg, r);
   2116  1.1  yamaguch }
   2117  1.1  yamaguch 
   2118  1.1  yamaguch static void
   2119  1.1  yamaguch ixl_txr_config(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2120  1.1  yamaguch {
   2121  1.1  yamaguch 	struct ixl_hmc_txq txq;
   2122  1.1  yamaguch 	struct ixl_aq_vsi_data *data = IXL_DMA_KVA(&sc->sc_scratch);
   2123  1.1  yamaguch 	void *hmc;
   2124  1.1  yamaguch 
   2125  1.1  yamaguch 	memset(&txq, 0, sizeof(txq));
   2126  1.1  yamaguch 	txq.head = htole16(txr->txr_cons);
   2127  1.1  yamaguch 	txq.new_context = 1;
   2128  1.1  yamaguch 	txq.base = htole64(IXL_DMA_DVA(&txr->txr_mem) / IXL_HMC_TXQ_BASE_UNIT);
   2129  1.1  yamaguch 	txq.head_wb_ena = IXL_HMC_TXQ_DESC_WB;
   2130  1.1  yamaguch 	txq.qlen = htole16(sc->sc_tx_ring_ndescs);
   2131  1.1  yamaguch 	txq.tphrdesc_ena = 0;
   2132  1.1  yamaguch 	txq.tphrpacket_ena = 0;
   2133  1.1  yamaguch 	txq.tphwdesc_ena = 0;
   2134  1.1  yamaguch 	txq.rdylist = data->qs_handle[0];
   2135  1.1  yamaguch 
   2136  1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_TX, txr->txr_qid);
   2137  1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_TX));
   2138  1.1  yamaguch 	ixl_hmc_pack(hmc, &txq, ixl_hmc_pack_txq,
   2139  1.1  yamaguch 	    __arraycount(ixl_hmc_pack_txq));
   2140  1.1  yamaguch }
   2141  1.1  yamaguch 
   2142  1.1  yamaguch static void
   2143  1.1  yamaguch ixl_txr_unconfig(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2144  1.1  yamaguch {
   2145  1.1  yamaguch 	void *hmc;
   2146  1.1  yamaguch 
   2147  1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_TX, txr->txr_qid);
   2148  1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_TX));
   2149  1.1  yamaguch }
   2150  1.1  yamaguch 
   2151  1.1  yamaguch static void
   2152  1.1  yamaguch ixl_txr_clean(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2153  1.1  yamaguch {
   2154  1.1  yamaguch 	struct ixl_tx_map *maps, *txm;
   2155  1.1  yamaguch 	bus_dmamap_t map;
   2156  1.1  yamaguch 	unsigned int i;
   2157  1.1  yamaguch 
   2158  1.1  yamaguch 	maps = txr->txr_maps;
   2159  1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2160  1.1  yamaguch 		txm = &maps[i];
   2161  1.1  yamaguch 
   2162  1.1  yamaguch 		if (txm->txm_m == NULL)
   2163  1.1  yamaguch 			continue;
   2164  1.1  yamaguch 
   2165  1.1  yamaguch 		map = txm->txm_map;
   2166  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2167  1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   2168  1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2169  1.1  yamaguch 
   2170  1.1  yamaguch 		m_freem(txm->txm_m);
   2171  1.1  yamaguch 		txm->txm_m = NULL;
   2172  1.1  yamaguch 	}
   2173  1.1  yamaguch }
   2174  1.1  yamaguch 
   2175  1.1  yamaguch static int
   2176  1.1  yamaguch ixl_txr_enabled(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2177  1.1  yamaguch {
   2178  1.1  yamaguch 	bus_size_t ena = I40E_QTX_ENA(txr->txr_qid);
   2179  1.1  yamaguch 	uint32_t reg;
   2180  1.1  yamaguch 	int i;
   2181  1.1  yamaguch 
   2182  1.1  yamaguch 	for (i = 0; i < 10; i++) {
   2183  1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2184  1.1  yamaguch 		if (ISSET(reg, I40E_QTX_ENA_QENA_STAT_MASK))
   2185  1.1  yamaguch 			return 0;
   2186  1.1  yamaguch 
   2187  1.1  yamaguch 		delaymsec(10);
   2188  1.1  yamaguch 	}
   2189  1.1  yamaguch 
   2190  1.1  yamaguch 	return ETIMEDOUT;
   2191  1.1  yamaguch }
   2192  1.1  yamaguch 
   2193  1.1  yamaguch static int
   2194  1.1  yamaguch ixl_txr_disabled(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2195  1.1  yamaguch {
   2196  1.1  yamaguch 	bus_size_t ena = I40E_QTX_ENA(txr->txr_qid);
   2197  1.1  yamaguch 	uint32_t reg;
   2198  1.1  yamaguch 	int i;
   2199  1.1  yamaguch 
   2200  1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2201  1.1  yamaguch 
   2202  1.1  yamaguch 	for (i = 0; i < 20; i++) {
   2203  1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2204  1.1  yamaguch 		if (ISSET(reg, I40E_QTX_ENA_QENA_STAT_MASK) == 0)
   2205  1.1  yamaguch 			return 0;
   2206  1.1  yamaguch 
   2207  1.1  yamaguch 		delaymsec(10);
   2208  1.1  yamaguch 	}
   2209  1.1  yamaguch 
   2210  1.1  yamaguch 	return ETIMEDOUT;
   2211  1.1  yamaguch }
   2212  1.1  yamaguch 
   2213  1.1  yamaguch static void
   2214  1.1  yamaguch ixl_txr_free(struct ixl_softc *sc, struct ixl_tx_ring *txr)
   2215  1.1  yamaguch {
   2216  1.1  yamaguch 	struct ixl_tx_map *maps, *txm;
   2217  1.1  yamaguch 	struct mbuf *m;
   2218  1.1  yamaguch 	unsigned int i;
   2219  1.1  yamaguch 
   2220  1.1  yamaguch 	softint_disestablish(txr->txr_si);
   2221  1.1  yamaguch 	while ((m = pcq_get(txr->txr_intrq)) != NULL)
   2222  1.1  yamaguch 		m_freem(m);
   2223  1.1  yamaguch 	pcq_destroy(txr->txr_intrq);
   2224  1.1  yamaguch 
   2225  1.1  yamaguch 	maps = txr->txr_maps;
   2226  1.1  yamaguch 	for (i = 0; i < sc->sc_tx_ring_ndescs; i++) {
   2227  1.1  yamaguch 		txm = &maps[i];
   2228  1.1  yamaguch 
   2229  1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, txm->txm_map);
   2230  1.1  yamaguch 	}
   2231  1.1  yamaguch 
   2232  1.1  yamaguch 	ixl_dmamem_free(sc, &txr->txr_mem);
   2233  1.1  yamaguch 	mutex_destroy(&txr->txr_lock);
   2234  1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_tx_ring_ndescs);
   2235  1.1  yamaguch 	kmem_free(txr, sizeof(*txr));
   2236  1.1  yamaguch }
   2237  1.1  yamaguch 
   2238  1.1  yamaguch static inline int
   2239  1.1  yamaguch ixl_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf **m0,
   2240  1.1  yamaguch     struct ixl_tx_ring *txr)
   2241  1.1  yamaguch {
   2242  1.1  yamaguch 	struct mbuf *m;
   2243  1.1  yamaguch 	int error;
   2244  1.1  yamaguch 
   2245  1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2246  1.1  yamaguch 
   2247  1.1  yamaguch 	m = *m0;
   2248  1.1  yamaguch 
   2249  1.1  yamaguch 	error = bus_dmamap_load_mbuf(dmat, map, m,
   2250  1.1  yamaguch 	    BUS_DMA_STREAMING | BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2251  1.1  yamaguch 	if (error != EFBIG)
   2252  1.1  yamaguch 		return error;
   2253  1.1  yamaguch 
   2254  1.1  yamaguch 	m = m_defrag(m, M_DONTWAIT);
   2255  1.1  yamaguch 	if (m != NULL) {
   2256  1.1  yamaguch 		*m0 = m;
   2257  1.1  yamaguch 		txr->txr_defragged.ev_count++;
   2258  1.1  yamaguch 
   2259  1.1  yamaguch 		error = bus_dmamap_load_mbuf(dmat, map, m,
   2260  1.1  yamaguch 		    BUS_DMA_STREAMING | BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2261  1.1  yamaguch 	} else {
   2262  1.1  yamaguch 		txr->txr_defrag_failed.ev_count++;
   2263  1.1  yamaguch 		error = ENOBUFS;
   2264  1.1  yamaguch 	}
   2265  1.1  yamaguch 
   2266  1.1  yamaguch 	return error;
   2267  1.1  yamaguch }
   2268  1.1  yamaguch 
   2269  1.1  yamaguch static void
   2270  1.1  yamaguch ixl_tx_common_locked(struct ifnet *ifp, struct ixl_tx_ring *txr,
   2271  1.1  yamaguch     bool is_transmit)
   2272  1.1  yamaguch {
   2273  1.1  yamaguch 	struct ixl_softc *sc = ifp->if_softc;
   2274  1.1  yamaguch 	struct ixl_tx_desc *ring, *txd;
   2275  1.1  yamaguch 	struct ixl_tx_map *txm;
   2276  1.1  yamaguch 	bus_dmamap_t map;
   2277  1.1  yamaguch 	struct mbuf *m;
   2278  1.1  yamaguch 	uint64_t cmd;
   2279  1.1  yamaguch 	unsigned int prod, free, last, i;
   2280  1.1  yamaguch 	unsigned int mask;
   2281  1.1  yamaguch 	int post = 0;
   2282  1.1  yamaguch 
   2283  1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2284  1.1  yamaguch 
   2285  1.1  yamaguch 	if (ifp->if_link_state != LINK_STATE_UP
   2286  1.1  yamaguch 	    || !ISSET(ifp->if_flags, IFF_RUNNING)
   2287  1.1  yamaguch 	    || (!is_transmit && ISSET(ifp->if_flags, IFF_OACTIVE))) {
   2288  1.1  yamaguch 		if (!is_transmit)
   2289  1.1  yamaguch 			IFQ_PURGE(&ifp->if_snd);
   2290  1.1  yamaguch 		return;
   2291  1.1  yamaguch 	}
   2292  1.1  yamaguch 
   2293  1.1  yamaguch 	prod = txr->txr_prod;
   2294  1.1  yamaguch 	free = txr->txr_cons;
   2295  1.1  yamaguch 	if (free <= prod)
   2296  1.1  yamaguch 		free += sc->sc_tx_ring_ndescs;
   2297  1.1  yamaguch 	free -= prod;
   2298  1.1  yamaguch 
   2299  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2300  1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTWRITE);
   2301  1.1  yamaguch 
   2302  1.1  yamaguch 	ring = IXL_DMA_KVA(&txr->txr_mem);
   2303  1.1  yamaguch 	mask = sc->sc_tx_ring_ndescs - 1;
   2304  1.1  yamaguch 	last = prod;
   2305  1.1  yamaguch 	cmd = 0;
   2306  1.1  yamaguch 	txd = NULL;
   2307  1.1  yamaguch 
   2308  1.1  yamaguch 	for (;;) {
   2309  1.1  yamaguch 		if (free <= IXL_TX_PKT_DESCS) {
   2310  1.1  yamaguch 			if (!is_transmit)
   2311  1.1  yamaguch 				SET(ifp->if_flags, IFF_OACTIVE);
   2312  1.1  yamaguch 			break;
   2313  1.1  yamaguch 		}
   2314  1.1  yamaguch 
   2315  1.1  yamaguch 		if (is_transmit)
   2316  1.1  yamaguch 			m = pcq_get(txr->txr_intrq);
   2317  1.1  yamaguch 		else
   2318  1.1  yamaguch 			IFQ_DEQUEUE(&ifp->if_snd, m);
   2319  1.1  yamaguch 
   2320  1.1  yamaguch 		if (m == NULL)
   2321  1.1  yamaguch 			break;
   2322  1.1  yamaguch 
   2323  1.1  yamaguch 		txm = &txr->txr_maps[prod];
   2324  1.1  yamaguch 		map = txm->txm_map;
   2325  1.1  yamaguch 
   2326  1.1  yamaguch 		if (ixl_load_mbuf(sc->sc_dmat, map, &m, txr) != 0) {
   2327  1.1  yamaguch 			txr->txr_oerrors++;
   2328  1.1  yamaguch 			m_freem(m);
   2329  1.1  yamaguch 			continue;
   2330  1.1  yamaguch 		}
   2331  1.1  yamaguch 
   2332  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0,
   2333  1.1  yamaguch 		    map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2334  1.1  yamaguch 
   2335  1.1  yamaguch 		for (i = 0; i < (unsigned int)map->dm_nsegs; i++) {
   2336  1.1  yamaguch 			txd = &ring[prod];
   2337  1.1  yamaguch 
   2338  1.1  yamaguch 			cmd = (uint64_t)map->dm_segs[i].ds_len <<
   2339  1.1  yamaguch 			    IXL_TX_DESC_BSIZE_SHIFT;
   2340  1.1  yamaguch 			cmd |= IXL_TX_DESC_DTYPE_DATA | IXL_TX_DESC_CMD_ICRC;
   2341  1.1  yamaguch 
   2342  1.1  yamaguch 			txd->addr = htole64(map->dm_segs[i].ds_addr);
   2343  1.1  yamaguch 			txd->cmd = htole64(cmd);
   2344  1.1  yamaguch 
   2345  1.1  yamaguch 			last = prod;
   2346  1.1  yamaguch 
   2347  1.1  yamaguch 			prod++;
   2348  1.1  yamaguch 			prod &= mask;
   2349  1.1  yamaguch 		}
   2350  1.1  yamaguch 		cmd |= IXL_TX_DESC_CMD_EOP | IXL_TX_DESC_CMD_RS;
   2351  1.1  yamaguch 		txd->cmd = htole64(cmd);
   2352  1.1  yamaguch 
   2353  1.1  yamaguch 		txm->txm_m = m;
   2354  1.1  yamaguch 		txm->txm_eop = last;
   2355  1.1  yamaguch 
   2356  1.1  yamaguch 		bpf_mtap(ifp, m, BPF_D_OUT);
   2357  1.1  yamaguch 
   2358  1.1  yamaguch 		free -= i;
   2359  1.1  yamaguch 		post = 1;
   2360  1.1  yamaguch 	}
   2361  1.1  yamaguch 
   2362  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2363  1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREWRITE);
   2364  1.1  yamaguch 
   2365  1.1  yamaguch 	if (post) {
   2366  1.1  yamaguch 		txr->txr_prod = prod;
   2367  1.1  yamaguch 		ixl_wr(sc, txr->txr_tail, prod);
   2368  1.1  yamaguch 	}
   2369  1.1  yamaguch }
   2370  1.1  yamaguch 
   2371  1.1  yamaguch static int
   2372  1.1  yamaguch ixl_txeof(struct ixl_softc *sc, struct ixl_tx_ring *txr, u_int txlimit)
   2373  1.1  yamaguch {
   2374  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2375  1.1  yamaguch 	struct ixl_tx_desc *ring, *txd;
   2376  1.1  yamaguch 	struct ixl_tx_map *txm;
   2377  1.1  yamaguch 	struct mbuf *m;
   2378  1.1  yamaguch 	bus_dmamap_t map;
   2379  1.1  yamaguch 	unsigned int cons, prod, last;
   2380  1.1  yamaguch 	unsigned int mask;
   2381  1.1  yamaguch 	uint64_t dtype;
   2382  1.1  yamaguch 	int done = 0, more = 0;
   2383  1.1  yamaguch 
   2384  1.1  yamaguch 	KASSERT(mutex_owned(&txr->txr_lock));
   2385  1.1  yamaguch 
   2386  1.1  yamaguch 	prod = txr->txr_prod;
   2387  1.1  yamaguch 	cons = txr->txr_cons;
   2388  1.1  yamaguch 
   2389  1.1  yamaguch 	if (cons == prod)
   2390  1.1  yamaguch 		return 0;
   2391  1.1  yamaguch 
   2392  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2393  1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_POSTREAD);
   2394  1.1  yamaguch 
   2395  1.1  yamaguch 	ring = IXL_DMA_KVA(&txr->txr_mem);
   2396  1.1  yamaguch 	mask = sc->sc_tx_ring_ndescs - 1;
   2397  1.1  yamaguch 
   2398  1.1  yamaguch 	do {
   2399  1.1  yamaguch 		if (txlimit-- <= 0) {
   2400  1.1  yamaguch 			more = 1;
   2401  1.1  yamaguch 			break;
   2402  1.1  yamaguch 		}
   2403  1.1  yamaguch 
   2404  1.1  yamaguch 		txm = &txr->txr_maps[cons];
   2405  1.1  yamaguch 		last = txm->txm_eop;
   2406  1.1  yamaguch 		txd = &ring[last];
   2407  1.1  yamaguch 
   2408  1.1  yamaguch 		dtype = txd->cmd & htole64(IXL_TX_DESC_DTYPE_MASK);
   2409  1.1  yamaguch 		if (dtype != htole64(IXL_TX_DESC_DTYPE_DONE))
   2410  1.1  yamaguch 			break;
   2411  1.1  yamaguch 
   2412  1.1  yamaguch 		map = txm->txm_map;
   2413  1.1  yamaguch 
   2414  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2415  1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   2416  1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2417  1.1  yamaguch 
   2418  1.1  yamaguch 		m = txm->txm_m;
   2419  1.1  yamaguch 		if (m != NULL) {
   2420  1.1  yamaguch 			txr->txr_opackets++;
   2421  1.1  yamaguch 			txr->txr_obytes += m->m_pkthdr.len;
   2422  1.1  yamaguch 			if (ISSET(m->m_flags, M_MCAST))
   2423  1.1  yamaguch 				txr->txr_omcasts++;
   2424  1.1  yamaguch 			m_freem(m);
   2425  1.1  yamaguch 		}
   2426  1.1  yamaguch 
   2427  1.1  yamaguch 		txm->txm_m = NULL;
   2428  1.1  yamaguch 		txm->txm_eop = -1;
   2429  1.1  yamaguch 
   2430  1.1  yamaguch 		cons = last + 1;
   2431  1.1  yamaguch 		cons &= mask;
   2432  1.1  yamaguch 		done = 1;
   2433  1.1  yamaguch 	} while (cons != prod);
   2434  1.1  yamaguch 
   2435  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&txr->txr_mem),
   2436  1.1  yamaguch 	    0, IXL_DMA_LEN(&txr->txr_mem), BUS_DMASYNC_PREREAD);
   2437  1.1  yamaguch 
   2438  1.1  yamaguch 	txr->txr_cons = cons;
   2439  1.1  yamaguch 
   2440  1.1  yamaguch 	if (done) {
   2441  1.1  yamaguch 		softint_schedule(txr->txr_si);
   2442  1.1  yamaguch 		if (txr->txr_qid == 0) {
   2443  1.1  yamaguch 			CLR(ifp->if_flags, IFF_OACTIVE);
   2444  1.1  yamaguch 			if_schedule_deferred_start(ifp);
   2445  1.1  yamaguch 		}
   2446  1.1  yamaguch 	}
   2447  1.1  yamaguch 
   2448  1.1  yamaguch 	return more;
   2449  1.1  yamaguch }
   2450  1.1  yamaguch 
   2451  1.1  yamaguch static void
   2452  1.1  yamaguch ixl_start(struct ifnet *ifp)
   2453  1.1  yamaguch {
   2454  1.1  yamaguch 	struct ixl_softc	*sc;
   2455  1.1  yamaguch 	struct ixl_tx_ring	*txr;
   2456  1.1  yamaguch 
   2457  1.1  yamaguch 	sc = ifp->if_softc;
   2458  1.1  yamaguch 	txr = sc->sc_qps[0].qp_txr;
   2459  1.1  yamaguch 
   2460  1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   2461  1.1  yamaguch 	ixl_tx_common_locked(ifp, txr, false);
   2462  1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   2463  1.1  yamaguch }
   2464  1.1  yamaguch 
   2465  1.1  yamaguch static inline unsigned int
   2466  1.1  yamaguch ixl_select_txqueue(struct ixl_softc *sc, struct mbuf *m)
   2467  1.1  yamaguch {
   2468  1.1  yamaguch 	u_int cpuid;
   2469  1.1  yamaguch 
   2470  1.1  yamaguch 	cpuid = cpu_index(curcpu());
   2471  1.1  yamaguch 
   2472  1.1  yamaguch 	return (unsigned int)(cpuid % sc->sc_nqueue_pairs);
   2473  1.1  yamaguch }
   2474  1.1  yamaguch 
   2475  1.1  yamaguch static int
   2476  1.1  yamaguch ixl_transmit(struct ifnet *ifp, struct mbuf *m)
   2477  1.1  yamaguch {
   2478  1.1  yamaguch 	struct ixl_softc *sc;
   2479  1.1  yamaguch 	struct ixl_tx_ring *txr;
   2480  1.1  yamaguch 	unsigned int qid;
   2481  1.1  yamaguch 
   2482  1.1  yamaguch 	sc = ifp->if_softc;
   2483  1.1  yamaguch 	qid = ixl_select_txqueue(sc, m);
   2484  1.1  yamaguch 
   2485  1.1  yamaguch 	txr = sc->sc_qps[qid].qp_txr;
   2486  1.1  yamaguch 
   2487  1.1  yamaguch 	if (__predict_false(!pcq_put(txr->txr_intrq, m))) {
   2488  1.1  yamaguch 		mutex_enter(&txr->txr_lock);
   2489  1.1  yamaguch 		txr->txr_pcqdrop.ev_count++;
   2490  1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   2491  1.1  yamaguch 
   2492  1.1  yamaguch 		m_freem(m);
   2493  1.1  yamaguch 		return ENOBUFS;
   2494  1.1  yamaguch 	}
   2495  1.1  yamaguch 
   2496  1.1  yamaguch 	if (mutex_tryenter(&txr->txr_lock)) {
   2497  1.1  yamaguch 		ixl_tx_common_locked(ifp, txr, true);
   2498  1.1  yamaguch 		mutex_exit(&txr->txr_lock);
   2499  1.1  yamaguch 	} else {
   2500  1.1  yamaguch 		softint_schedule(txr->txr_si);
   2501  1.1  yamaguch 	}
   2502  1.1  yamaguch 
   2503  1.1  yamaguch 	return 0;
   2504  1.1  yamaguch }
   2505  1.1  yamaguch 
   2506  1.1  yamaguch static void
   2507  1.1  yamaguch ixl_deferred_transmit(void *xtxr)
   2508  1.1  yamaguch {
   2509  1.1  yamaguch 	struct ixl_tx_ring *txr = xtxr;
   2510  1.1  yamaguch 	struct ixl_softc *sc = txr->txr_sc;
   2511  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2512  1.1  yamaguch 
   2513  1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   2514  1.1  yamaguch 	txr->txr_transmitdef.ev_count++;
   2515  1.1  yamaguch 	if (pcq_peek(txr->txr_intrq) != NULL)
   2516  1.1  yamaguch 		ixl_tx_common_locked(ifp, txr, true);
   2517  1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   2518  1.1  yamaguch }
   2519  1.1  yamaguch 
   2520  1.1  yamaguch static struct ixl_rx_ring *
   2521  1.1  yamaguch ixl_rxr_alloc(struct ixl_softc *sc, unsigned int qid)
   2522  1.1  yamaguch {
   2523  1.1  yamaguch 	struct ixl_rx_ring *rxr = NULL;
   2524  1.1  yamaguch 	struct ixl_rx_map *maps = NULL, *rxm;
   2525  1.1  yamaguch 	unsigned int i;
   2526  1.1  yamaguch 
   2527  1.1  yamaguch 	rxr = kmem_zalloc(sizeof(*rxr), KM_SLEEP);
   2528  1.1  yamaguch 	maps = kmem_zalloc(sizeof(maps[0]) * sc->sc_rx_ring_ndescs,
   2529  1.1  yamaguch 	    KM_SLEEP);
   2530  1.1  yamaguch 
   2531  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &rxr->rxr_mem,
   2532  1.1  yamaguch 	    sizeof(struct ixl_rx_rd_desc_16) * sc->sc_rx_ring_ndescs,
   2533  1.1  yamaguch 	    IXL_RX_QUEUE_ALIGN) != 0)
   2534  1.1  yamaguch 		goto free;
   2535  1.1  yamaguch 
   2536  1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   2537  1.1  yamaguch 		rxm = &maps[i];
   2538  1.1  yamaguch 
   2539  1.1  yamaguch 		if (bus_dmamap_create(sc->sc_dmat,
   2540  1.1  yamaguch 		    IXL_HARDMTU, 1, IXL_HARDMTU, 0,
   2541  1.1  yamaguch 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &rxm->rxm_map) != 0)
   2542  1.1  yamaguch 			goto uncreate;
   2543  1.1  yamaguch 
   2544  1.1  yamaguch 		rxm->rxm_m = NULL;
   2545  1.1  yamaguch 	}
   2546  1.1  yamaguch 
   2547  1.1  yamaguch 	rxr->rxr_cons = rxr->rxr_prod = 0;
   2548  1.1  yamaguch 	rxr->rxr_m_head = NULL;
   2549  1.1  yamaguch 	rxr->rxr_m_tail = &rxr->rxr_m_head;
   2550  1.1  yamaguch 	rxr->rxr_maps = maps;
   2551  1.1  yamaguch 
   2552  1.1  yamaguch 	rxr->rxr_tail = I40E_QRX_TAIL(qid);
   2553  1.1  yamaguch 	rxr->rxr_qid = qid;
   2554  1.1  yamaguch 	mutex_init(&rxr->rxr_lock, MUTEX_DEFAULT, IPL_NET);
   2555  1.1  yamaguch 
   2556  1.1  yamaguch 	return rxr;
   2557  1.1  yamaguch 
   2558  1.1  yamaguch uncreate:
   2559  1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   2560  1.1  yamaguch 		rxm = &maps[i];
   2561  1.1  yamaguch 
   2562  1.1  yamaguch 		if (rxm->rxm_map == NULL)
   2563  1.1  yamaguch 			continue;
   2564  1.1  yamaguch 
   2565  1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, rxm->rxm_map);
   2566  1.1  yamaguch 	}
   2567  1.1  yamaguch 
   2568  1.1  yamaguch 	ixl_dmamem_free(sc, &rxr->rxr_mem);
   2569  1.1  yamaguch free:
   2570  1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
   2571  1.1  yamaguch 	kmem_free(rxr, sizeof(*rxr));
   2572  1.1  yamaguch 
   2573  1.1  yamaguch 	return NULL;
   2574  1.1  yamaguch }
   2575  1.1  yamaguch 
   2576  1.1  yamaguch static void
   2577  1.1  yamaguch ixl_rxr_clean(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2578  1.1  yamaguch {
   2579  1.1  yamaguch 	struct ixl_rx_map *maps, *rxm;
   2580  1.1  yamaguch 	bus_dmamap_t map;
   2581  1.1  yamaguch 	unsigned int i;
   2582  1.1  yamaguch 
   2583  1.1  yamaguch 	maps = rxr->rxr_maps;
   2584  1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   2585  1.1  yamaguch 		rxm = &maps[i];
   2586  1.1  yamaguch 
   2587  1.1  yamaguch 		if (rxm->rxm_m == NULL)
   2588  1.1  yamaguch 			continue;
   2589  1.1  yamaguch 
   2590  1.1  yamaguch 		map = rxm->rxm_map;
   2591  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2592  1.1  yamaguch 		    BUS_DMASYNC_POSTWRITE);
   2593  1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2594  1.1  yamaguch 
   2595  1.1  yamaguch 		m_freem(rxm->rxm_m);
   2596  1.1  yamaguch 		rxm->rxm_m = NULL;
   2597  1.1  yamaguch 	}
   2598  1.1  yamaguch 
   2599  1.1  yamaguch 	m_freem(rxr->rxr_m_head);
   2600  1.1  yamaguch 	rxr->rxr_m_head = NULL;
   2601  1.1  yamaguch 	rxr->rxr_m_tail = &rxr->rxr_m_head;
   2602  1.1  yamaguch 
   2603  1.1  yamaguch 	rxr->rxr_prod = rxr->rxr_cons = 0;
   2604  1.1  yamaguch }
   2605  1.1  yamaguch 
   2606  1.1  yamaguch static int
   2607  1.1  yamaguch ixl_rxr_enabled(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2608  1.1  yamaguch {
   2609  1.1  yamaguch 	bus_size_t ena = I40E_QRX_ENA(rxr->rxr_qid);
   2610  1.1  yamaguch 	uint32_t reg;
   2611  1.1  yamaguch 	int i;
   2612  1.1  yamaguch 
   2613  1.1  yamaguch 	for (i = 0; i < 10; i++) {
   2614  1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2615  1.1  yamaguch 		if (ISSET(reg, I40E_QRX_ENA_QENA_STAT_MASK))
   2616  1.1  yamaguch 			return 0;
   2617  1.1  yamaguch 
   2618  1.1  yamaguch 		delaymsec(10);
   2619  1.1  yamaguch 	}
   2620  1.1  yamaguch 
   2621  1.1  yamaguch 	return ETIMEDOUT;
   2622  1.1  yamaguch }
   2623  1.1  yamaguch 
   2624  1.1  yamaguch static int
   2625  1.1  yamaguch ixl_rxr_disabled(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2626  1.1  yamaguch {
   2627  1.1  yamaguch 	bus_size_t ena = I40E_QRX_ENA(rxr->rxr_qid);
   2628  1.1  yamaguch 	uint32_t reg;
   2629  1.1  yamaguch 	int i;
   2630  1.1  yamaguch 
   2631  1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   2632  1.1  yamaguch 
   2633  1.1  yamaguch 	for (i = 0; i < 20; i++) {
   2634  1.1  yamaguch 		reg = ixl_rd(sc, ena);
   2635  1.1  yamaguch 		if (ISSET(reg, I40E_QRX_ENA_QENA_STAT_MASK) == 0)
   2636  1.1  yamaguch 			return 0;
   2637  1.1  yamaguch 
   2638  1.1  yamaguch 		delaymsec(10);
   2639  1.1  yamaguch 	}
   2640  1.1  yamaguch 
   2641  1.1  yamaguch 	return ETIMEDOUT;
   2642  1.1  yamaguch }
   2643  1.1  yamaguch 
   2644  1.1  yamaguch static void
   2645  1.1  yamaguch ixl_rxr_config(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2646  1.1  yamaguch {
   2647  1.1  yamaguch 	struct ixl_hmc_rxq rxq;
   2648  1.1  yamaguch 	void *hmc;
   2649  1.1  yamaguch 
   2650  1.1  yamaguch 	memset(&rxq, 0, sizeof(rxq));
   2651  1.1  yamaguch 
   2652  1.1  yamaguch 	rxq.head = htole16(rxr->rxr_cons);
   2653  1.1  yamaguch 	rxq.base = htole64(IXL_DMA_DVA(&rxr->rxr_mem) / IXL_HMC_RXQ_BASE_UNIT);
   2654  1.1  yamaguch 	rxq.qlen = htole16(sc->sc_rx_ring_ndescs);
   2655  1.1  yamaguch 	rxq.dbuff = htole16(MCLBYTES / IXL_HMC_RXQ_DBUFF_UNIT);
   2656  1.1  yamaguch 	rxq.hbuff = 0;
   2657  1.1  yamaguch 	rxq.dtype = IXL_HMC_RXQ_DTYPE_NOSPLIT;
   2658  1.1  yamaguch 	rxq.dsize = IXL_HMC_RXQ_DSIZE_16;
   2659  1.1  yamaguch 	rxq.crcstrip = 1;
   2660  1.1  yamaguch 	rxq.l2sel = 0;
   2661  1.1  yamaguch 	rxq.showiv = 0;
   2662  1.1  yamaguch 	rxq.rxmax = htole16(IXL_HARDMTU);
   2663  1.1  yamaguch 	rxq.tphrdesc_ena = 0;
   2664  1.1  yamaguch 	rxq.tphwdesc_ena = 0;
   2665  1.1  yamaguch 	rxq.tphdata_ena = 0;
   2666  1.1  yamaguch 	rxq.tphhead_ena = 0;
   2667  1.1  yamaguch 	rxq.lrxqthresh = 0;
   2668  1.1  yamaguch 	rxq.prefena = 1;
   2669  1.1  yamaguch 
   2670  1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_RX, rxr->rxr_qid);
   2671  1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_RX));
   2672  1.1  yamaguch 	ixl_hmc_pack(hmc, &rxq, ixl_hmc_pack_rxq,
   2673  1.1  yamaguch 	    __arraycount(ixl_hmc_pack_rxq));
   2674  1.1  yamaguch }
   2675  1.1  yamaguch 
   2676  1.1  yamaguch static void
   2677  1.1  yamaguch ixl_rxr_unconfig(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2678  1.1  yamaguch {
   2679  1.1  yamaguch 	void *hmc;
   2680  1.1  yamaguch 
   2681  1.1  yamaguch 	hmc = ixl_hmc_kva(sc, IXL_HMC_LAN_RX, rxr->rxr_qid);
   2682  1.1  yamaguch 	memset(hmc, 0, ixl_hmc_len(sc, IXL_HMC_LAN_RX));
   2683  1.1  yamaguch }
   2684  1.1  yamaguch 
   2685  1.1  yamaguch static void
   2686  1.1  yamaguch ixl_rxr_free(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2687  1.1  yamaguch {
   2688  1.1  yamaguch 	struct ixl_rx_map *maps, *rxm;
   2689  1.1  yamaguch 	unsigned int i;
   2690  1.1  yamaguch 
   2691  1.1  yamaguch 	maps = rxr->rxr_maps;
   2692  1.1  yamaguch 	for (i = 0; i < sc->sc_rx_ring_ndescs; i++) {
   2693  1.1  yamaguch 		rxm = &maps[i];
   2694  1.1  yamaguch 
   2695  1.1  yamaguch 		bus_dmamap_destroy(sc->sc_dmat, rxm->rxm_map);
   2696  1.1  yamaguch 	}
   2697  1.1  yamaguch 
   2698  1.1  yamaguch 	ixl_dmamem_free(sc, &rxr->rxr_mem);
   2699  1.1  yamaguch 	mutex_destroy(&rxr->rxr_lock);
   2700  1.1  yamaguch 	kmem_free(maps, sizeof(maps[0]) * sc->sc_rx_ring_ndescs);
   2701  1.1  yamaguch 	kmem_free(rxr, sizeof(*rxr));
   2702  1.1  yamaguch }
   2703  1.1  yamaguch 
   2704  1.1  yamaguch static int
   2705  1.1  yamaguch ixl_rxeof(struct ixl_softc *sc, struct ixl_rx_ring *rxr, u_int rxlimit)
   2706  1.1  yamaguch {
   2707  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2708  1.1  yamaguch 	struct ixl_rx_wb_desc_16 *ring, *rxd;
   2709  1.1  yamaguch 	struct ixl_rx_map *rxm;
   2710  1.1  yamaguch 	bus_dmamap_t map;
   2711  1.1  yamaguch 	unsigned int cons, prod;
   2712  1.1  yamaguch 	struct mbuf *m;
   2713  1.1  yamaguch 	uint64_t word;
   2714  1.1  yamaguch 	unsigned int len;
   2715  1.1  yamaguch 	unsigned int mask;
   2716  1.1  yamaguch 	int done = 0, more = 0;
   2717  1.1  yamaguch 
   2718  1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   2719  1.1  yamaguch 
   2720  1.1  yamaguch 	if (!ISSET(ifp->if_flags, IFF_RUNNING))
   2721  1.1  yamaguch 		return 0;
   2722  1.1  yamaguch 
   2723  1.1  yamaguch 	prod = rxr->rxr_prod;
   2724  1.1  yamaguch 	cons = rxr->rxr_cons;
   2725  1.1  yamaguch 
   2726  1.1  yamaguch 	if (cons == prod)
   2727  1.1  yamaguch 		return 0;
   2728  1.1  yamaguch 
   2729  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
   2730  1.1  yamaguch 	    0, IXL_DMA_LEN(&rxr->rxr_mem),
   2731  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   2732  1.1  yamaguch 
   2733  1.1  yamaguch 	ring = IXL_DMA_KVA(&rxr->rxr_mem);
   2734  1.1  yamaguch 	mask = sc->sc_rx_ring_ndescs - 1;
   2735  1.1  yamaguch 
   2736  1.1  yamaguch 	do {
   2737  1.1  yamaguch 		if (rxlimit-- <= 0) {
   2738  1.1  yamaguch 			more = 1;
   2739  1.1  yamaguch 			break;
   2740  1.1  yamaguch 		}
   2741  1.1  yamaguch 
   2742  1.1  yamaguch 		rxd = &ring[cons];
   2743  1.1  yamaguch 
   2744  1.1  yamaguch 		word = le64toh(rxd->qword1);
   2745  1.1  yamaguch 
   2746  1.1  yamaguch 		if (!ISSET(word, IXL_RX_DESC_DD))
   2747  1.1  yamaguch 			break;
   2748  1.1  yamaguch 
   2749  1.1  yamaguch 		rxm = &rxr->rxr_maps[cons];
   2750  1.1  yamaguch 
   2751  1.1  yamaguch 		map = rxm->rxm_map;
   2752  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2753  1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   2754  1.1  yamaguch 		bus_dmamap_unload(sc->sc_dmat, map);
   2755  1.1  yamaguch 
   2756  1.1  yamaguch 		m = rxm->rxm_m;
   2757  1.1  yamaguch 		rxm->rxm_m = NULL;
   2758  1.1  yamaguch 
   2759  1.1  yamaguch 		KASSERT(m != NULL);
   2760  1.1  yamaguch 
   2761  1.1  yamaguch 		len = (word & IXL_RX_DESC_PLEN_MASK) >> IXL_RX_DESC_PLEN_SHIFT;
   2762  1.1  yamaguch 		m->m_len = len;
   2763  1.1  yamaguch 		m->m_pkthdr.len = 0;
   2764  1.1  yamaguch 
   2765  1.1  yamaguch 		m->m_next = NULL;
   2766  1.1  yamaguch 		*rxr->rxr_m_tail = m;
   2767  1.1  yamaguch 		rxr->rxr_m_tail = &m->m_next;
   2768  1.1  yamaguch 
   2769  1.1  yamaguch 		m = rxr->rxr_m_head;
   2770  1.1  yamaguch 		m->m_pkthdr.len += len;
   2771  1.1  yamaguch 
   2772  1.1  yamaguch 		if (ISSET(word, IXL_RX_DESC_EOP)) {
   2773  1.1  yamaguch 			if (!ISSET(word,
   2774  1.1  yamaguch 			    IXL_RX_DESC_RXE | IXL_RX_DESC_OVERSIZE)) {
   2775  1.1  yamaguch 				m_set_rcvif(m, ifp);
   2776  1.1  yamaguch 				rxr->rxr_ipackets++;
   2777  1.1  yamaguch 				rxr->rxr_ibytes += m->m_pkthdr.len;
   2778  1.1  yamaguch 				if_percpuq_enqueue(ifp->if_percpuq, m);
   2779  1.1  yamaguch 			} else {
   2780  1.1  yamaguch 				rxr->rxr_ierrors++;
   2781  1.1  yamaguch 				m_freem(m);
   2782  1.1  yamaguch 			}
   2783  1.1  yamaguch 
   2784  1.1  yamaguch 			rxr->rxr_m_head = NULL;
   2785  1.1  yamaguch 			rxr->rxr_m_tail = &rxr->rxr_m_head;
   2786  1.1  yamaguch 		}
   2787  1.1  yamaguch 
   2788  1.1  yamaguch 		cons++;
   2789  1.1  yamaguch 		cons &= mask;
   2790  1.1  yamaguch 
   2791  1.1  yamaguch 		done = 1;
   2792  1.1  yamaguch 	} while (cons != prod);
   2793  1.1  yamaguch 
   2794  1.1  yamaguch 	if (done) {
   2795  1.1  yamaguch 		rxr->rxr_cons = cons;
   2796  1.1  yamaguch 		if (ixl_rxfill(sc, rxr) == -1)
   2797  1.1  yamaguch 			rxr->rxr_iqdrops++;
   2798  1.1  yamaguch 	}
   2799  1.1  yamaguch 
   2800  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&rxr->rxr_mem),
   2801  1.1  yamaguch 	    0, IXL_DMA_LEN(&rxr->rxr_mem),
   2802  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2803  1.1  yamaguch 
   2804  1.1  yamaguch 	return more;
   2805  1.1  yamaguch }
   2806  1.1  yamaguch 
   2807  1.1  yamaguch static int
   2808  1.1  yamaguch ixl_rxfill(struct ixl_softc *sc, struct ixl_rx_ring *rxr)
   2809  1.1  yamaguch {
   2810  1.1  yamaguch 	struct ixl_rx_rd_desc_16 *ring, *rxd;
   2811  1.1  yamaguch 	struct ixl_rx_map *rxm;
   2812  1.1  yamaguch 	bus_dmamap_t map;
   2813  1.1  yamaguch 	struct mbuf *m;
   2814  1.1  yamaguch 	unsigned int prod;
   2815  1.1  yamaguch 	unsigned int slots;
   2816  1.1  yamaguch 	unsigned int mask;
   2817  1.1  yamaguch 	int post = 0, error = 0;
   2818  1.1  yamaguch 
   2819  1.1  yamaguch 	KASSERT(mutex_owned(&rxr->rxr_lock));
   2820  1.1  yamaguch 
   2821  1.1  yamaguch 	prod = rxr->rxr_prod;
   2822  1.1  yamaguch 	slots = ixl_rxr_unrefreshed(rxr->rxr_prod, rxr->rxr_cons,
   2823  1.1  yamaguch 	    sc->sc_rx_ring_ndescs);
   2824  1.1  yamaguch 
   2825  1.1  yamaguch 	ring = IXL_DMA_KVA(&rxr->rxr_mem);
   2826  1.1  yamaguch 	mask = sc->sc_rx_ring_ndescs - 1;
   2827  1.1  yamaguch 
   2828  1.1  yamaguch 	if (__predict_false(slots <= 0))
   2829  1.1  yamaguch 		return -1;
   2830  1.1  yamaguch 
   2831  1.1  yamaguch 	do {
   2832  1.1  yamaguch 		rxm = &rxr->rxr_maps[prod];
   2833  1.1  yamaguch 
   2834  1.1  yamaguch 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   2835  1.1  yamaguch 		if (m == NULL) {
   2836  1.1  yamaguch 			rxr->rxr_mgethdr_failed.ev_count++;
   2837  1.1  yamaguch 			error = -1;
   2838  1.1  yamaguch 			break;
   2839  1.1  yamaguch 		}
   2840  1.1  yamaguch 
   2841  1.1  yamaguch 		MCLGET(m, M_DONTWAIT);
   2842  1.1  yamaguch 		if (!ISSET(m->m_flags, M_EXT)) {
   2843  1.1  yamaguch 			rxr->rxr_mgetcl_failed.ev_count++;
   2844  1.1  yamaguch 			error = -1;
   2845  1.1  yamaguch 			m_freem(m);
   2846  1.1  yamaguch 			break;
   2847  1.1  yamaguch 		}
   2848  1.1  yamaguch 
   2849  1.1  yamaguch 		m->m_len = m->m_pkthdr.len = MCLBYTES + ETHER_ALIGN;
   2850  1.1  yamaguch 		m_adj(m, ETHER_ALIGN);
   2851  1.1  yamaguch 
   2852  1.1  yamaguch 		map = rxm->rxm_map;
   2853  1.1  yamaguch 
   2854  1.1  yamaguch 		if (bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   2855  1.1  yamaguch 		    BUS_DMA_READ | BUS_DMA_NOWAIT) != 0) {
   2856  1.1  yamaguch 			rxr->rxr_mbuf_load_failed.ev_count++;
   2857  1.1  yamaguch 			error = -1;
   2858  1.1  yamaguch 			m_freem(m);
   2859  1.1  yamaguch 			break;
   2860  1.1  yamaguch 		}
   2861  1.1  yamaguch 
   2862  1.1  yamaguch 		rxm->rxm_m = m;
   2863  1.1  yamaguch 
   2864  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   2865  1.1  yamaguch 		    BUS_DMASYNC_PREREAD);
   2866  1.1  yamaguch 
   2867  1.1  yamaguch 		rxd = &ring[prod];
   2868  1.1  yamaguch 
   2869  1.1  yamaguch 		rxd->paddr = htole64(map->dm_segs[0].ds_addr);
   2870  1.1  yamaguch 		rxd->haddr = htole64(0);
   2871  1.1  yamaguch 
   2872  1.1  yamaguch 		prod++;
   2873  1.1  yamaguch 		prod &= mask;
   2874  1.1  yamaguch 
   2875  1.1  yamaguch 		post = 1;
   2876  1.1  yamaguch 
   2877  1.1  yamaguch 	} while (--slots);
   2878  1.1  yamaguch 
   2879  1.1  yamaguch 	if (post) {
   2880  1.1  yamaguch 		rxr->rxr_prod = prod;
   2881  1.1  yamaguch 		ixl_wr(sc, rxr->rxr_tail, prod);
   2882  1.1  yamaguch 	}
   2883  1.1  yamaguch 
   2884  1.1  yamaguch 	return error;
   2885  1.1  yamaguch }
   2886  1.1  yamaguch 
   2887  1.1  yamaguch static inline int
   2888  1.1  yamaguch ixl_handle_queue_common(struct ixl_softc *sc, struct ixl_queue_pair *qp,
   2889  1.1  yamaguch     u_int txlimit, struct evcnt *txevcnt,
   2890  1.1  yamaguch     u_int rxlimit, struct evcnt *rxevcnt)
   2891  1.1  yamaguch {
   2892  1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   2893  1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   2894  1.1  yamaguch 	int txmore, rxmore;
   2895  1.1  yamaguch 	int rv;
   2896  1.1  yamaguch 
   2897  1.1  yamaguch 	KASSERT(!mutex_owned(&txr->txr_lock));
   2898  1.1  yamaguch 	KASSERT(!mutex_owned(&rxr->rxr_lock));
   2899  1.1  yamaguch 
   2900  1.1  yamaguch 	mutex_enter(&txr->txr_lock);
   2901  1.1  yamaguch 	txevcnt->ev_count++;
   2902  1.1  yamaguch 	txmore = ixl_txeof(sc, txr, txlimit);
   2903  1.1  yamaguch 	mutex_exit(&txr->txr_lock);
   2904  1.1  yamaguch 
   2905  1.1  yamaguch 	mutex_enter(&rxr->rxr_lock);
   2906  1.1  yamaguch 	rxevcnt->ev_count++;
   2907  1.1  yamaguch 	rxmore = ixl_rxeof(sc, rxr, rxlimit);
   2908  1.1  yamaguch 	mutex_exit(&rxr->rxr_lock);
   2909  1.1  yamaguch 
   2910  1.1  yamaguch 	rv = txmore | (rxmore << 1);
   2911  1.1  yamaguch 
   2912  1.1  yamaguch 	return rv;
   2913  1.1  yamaguch }
   2914  1.1  yamaguch 
   2915  1.1  yamaguch static void
   2916  1.1  yamaguch ixl_sched_handle_queue(struct ixl_softc *sc, struct ixl_queue_pair *qp)
   2917  1.1  yamaguch {
   2918  1.1  yamaguch 
   2919  1.1  yamaguch 	if (qp->qp_workqueue)
   2920  1.1  yamaguch 		ixl_work_add(sc->sc_workq_txrx, &qp->qp_task);
   2921  1.1  yamaguch 	else
   2922  1.1  yamaguch 		softint_schedule(qp->qp_si);
   2923  1.1  yamaguch }
   2924  1.1  yamaguch 
   2925  1.1  yamaguch static int
   2926  1.1  yamaguch ixl_intr(void *xsc)
   2927  1.1  yamaguch {
   2928  1.1  yamaguch 	struct ixl_softc *sc = xsc;
   2929  1.1  yamaguch 	struct ixl_tx_ring *txr;
   2930  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   2931  1.1  yamaguch 	uint32_t icr, rxintr, txintr;
   2932  1.1  yamaguch 	int rv = 0;
   2933  1.1  yamaguch 	unsigned int i;
   2934  1.1  yamaguch 
   2935  1.1  yamaguch 	KASSERT(sc != NULL);
   2936  1.1  yamaguch 
   2937  1.1  yamaguch 	ixl_enable_other_intr(sc);
   2938  1.1  yamaguch 	icr = ixl_rd(sc, I40E_PFINT_ICR0);
   2939  1.1  yamaguch 
   2940  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ADMINQ_MASK)) {
   2941  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_atq.ev_count);
   2942  1.1  yamaguch 		ixl_atq_done(sc);
   2943  1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_arq_task);
   2944  1.1  yamaguch 		rv = 1;
   2945  1.1  yamaguch 	}
   2946  1.1  yamaguch 
   2947  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_LINK_STAT_CHANGE_MASK)) {
   2948  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_link.ev_count);
   2949  1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_link_state_task);
   2950  1.1  yamaguch 		rv = 1;
   2951  1.1  yamaguch 	}
   2952  1.1  yamaguch 
   2953  1.1  yamaguch 	rxintr = icr & I40E_INTR_NOTX_RX_MASK;
   2954  1.1  yamaguch 	txintr = icr & I40E_INTR_NOTX_TX_MASK;
   2955  1.1  yamaguch 
   2956  1.1  yamaguch 	if (txintr || rxintr) {
   2957  1.1  yamaguch 		for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   2958  1.1  yamaguch 			txr = sc->sc_qps[i].qp_txr;
   2959  1.1  yamaguch 			rxr = sc->sc_qps[i].qp_rxr;
   2960  1.1  yamaguch 
   2961  1.1  yamaguch 			ixl_handle_queue_common(sc, &sc->sc_qps[i],
   2962  1.1  yamaguch 			    IXL_TXRX_PROCESS_UNLIMIT, &txr->txr_intr,
   2963  1.1  yamaguch 			    IXL_TXRX_PROCESS_UNLIMIT, &rxr->rxr_intr);
   2964  1.1  yamaguch 		}
   2965  1.1  yamaguch 		rv = 1;
   2966  1.1  yamaguch 	}
   2967  1.1  yamaguch 
   2968  1.1  yamaguch 	return rv;
   2969  1.1  yamaguch }
   2970  1.1  yamaguch 
   2971  1.1  yamaguch static int
   2972  1.1  yamaguch ixl_queue_intr(void *xqp)
   2973  1.1  yamaguch {
   2974  1.1  yamaguch 	struct ixl_queue_pair *qp = xqp;
   2975  1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   2976  1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   2977  1.1  yamaguch 	struct ixl_softc *sc = qp->qp_sc;
   2978  1.1  yamaguch 	u_int txlimit, rxlimit;
   2979  1.1  yamaguch 	int more;
   2980  1.1  yamaguch 
   2981  1.1  yamaguch 	txlimit = sc->sc_tx_intr_process_limit;
   2982  1.1  yamaguch 	rxlimit = sc->sc_rx_intr_process_limit;
   2983  1.1  yamaguch 	qp->qp_workqueue = sc->sc_txrx_workqueue;
   2984  1.1  yamaguch 
   2985  1.1  yamaguch 	more = ixl_handle_queue_common(sc, qp,
   2986  1.1  yamaguch 	    txlimit, &txr->txr_intr, rxlimit, &rxr->rxr_intr);
   2987  1.1  yamaguch 
   2988  1.1  yamaguch 	if (more != 0) {
   2989  1.1  yamaguch 		ixl_sched_handle_queue(sc, qp);
   2990  1.1  yamaguch 	} else {
   2991  1.1  yamaguch 		/* for ALTQ */
   2992  1.1  yamaguch 		if (txr->txr_qid == 0)
   2993  1.1  yamaguch 			if_schedule_deferred_start(&sc->sc_ec.ec_if);
   2994  1.1  yamaguch 		softint_schedule(txr->txr_si);
   2995  1.1  yamaguch 
   2996  1.1  yamaguch 		ixl_enable_queue_intr(sc, qp);
   2997  1.1  yamaguch 	}
   2998  1.1  yamaguch 
   2999  1.1  yamaguch 	return 1;
   3000  1.1  yamaguch }
   3001  1.1  yamaguch 
   3002  1.1  yamaguch static void
   3003  1.1  yamaguch ixl_handle_queue(void *xqp)
   3004  1.1  yamaguch {
   3005  1.1  yamaguch 	struct ixl_queue_pair *qp = xqp;
   3006  1.1  yamaguch 	struct ixl_softc *sc = qp->qp_sc;
   3007  1.1  yamaguch 	struct ixl_tx_ring *txr = qp->qp_txr;
   3008  1.1  yamaguch 	struct ixl_rx_ring *rxr = qp->qp_rxr;
   3009  1.1  yamaguch 	u_int txlimit, rxlimit;
   3010  1.1  yamaguch 	int more;
   3011  1.1  yamaguch 
   3012  1.1  yamaguch 	txlimit = sc->sc_tx_process_limit;
   3013  1.1  yamaguch 	rxlimit = sc->sc_rx_process_limit;
   3014  1.1  yamaguch 
   3015  1.1  yamaguch 	more = ixl_handle_queue_common(sc, qp,
   3016  1.1  yamaguch 	    txlimit, &txr->txr_defer, rxlimit, &rxr->rxr_defer);
   3017  1.1  yamaguch 
   3018  1.1  yamaguch 	if (more != 0)
   3019  1.1  yamaguch 		ixl_sched_handle_queue(sc, qp);
   3020  1.1  yamaguch 	else
   3021  1.1  yamaguch 		ixl_enable_queue_intr(sc, qp);
   3022  1.1  yamaguch }
   3023  1.1  yamaguch 
   3024  1.1  yamaguch static inline void
   3025  1.1  yamaguch ixl_print_hmc_error(struct ixl_softc *sc, uint32_t reg)
   3026  1.1  yamaguch {
   3027  1.1  yamaguch 	uint32_t hmc_idx, hmc_isvf;
   3028  1.1  yamaguch 	uint32_t hmc_errtype, hmc_objtype, hmc_data;
   3029  1.1  yamaguch 
   3030  1.1  yamaguch 	hmc_idx = reg & I40E_PFHMC_ERRORINFO_PMF_INDEX_MASK;
   3031  1.1  yamaguch 	hmc_idx = hmc_idx >> I40E_PFHMC_ERRORINFO_PMF_INDEX_SHIFT;
   3032  1.1  yamaguch 	hmc_isvf = reg & I40E_PFHMC_ERRORINFO_PMF_ISVF_MASK;
   3033  1.1  yamaguch 	hmc_isvf = hmc_isvf >> I40E_PFHMC_ERRORINFO_PMF_ISVF_SHIFT;
   3034  1.1  yamaguch 	hmc_errtype = reg & I40E_PFHMC_ERRORINFO_HMC_ERROR_TYPE_MASK;
   3035  1.1  yamaguch 	hmc_errtype = hmc_errtype >> I40E_PFHMC_ERRORINFO_HMC_ERROR_TYPE_SHIFT;
   3036  1.1  yamaguch 	hmc_objtype = reg & I40E_PFHMC_ERRORINFO_HMC_OBJECT_TYPE_MASK;
   3037  1.1  yamaguch 	hmc_objtype = hmc_objtype >> I40E_PFHMC_ERRORINFO_HMC_OBJECT_TYPE_SHIFT;
   3038  1.1  yamaguch 	hmc_data = ixl_rd(sc, I40E_PFHMC_ERRORDATA);
   3039  1.1  yamaguch 
   3040  1.1  yamaguch 	device_printf(sc->sc_dev,
   3041  1.1  yamaguch 	    "HMC Error (idx=0x%x, isvf=0x%x, err=0x%x, obj=0x%x, data=0x%x)\n",
   3042  1.1  yamaguch 	    hmc_idx, hmc_isvf, hmc_errtype, hmc_objtype, hmc_data);
   3043  1.1  yamaguch }
   3044  1.1  yamaguch 
   3045  1.1  yamaguch static int
   3046  1.1  yamaguch ixl_other_intr(void *xsc)
   3047  1.1  yamaguch {
   3048  1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3049  1.1  yamaguch 	uint32_t icr, mask, reg;
   3050  1.1  yamaguch 	int rv;
   3051  1.1  yamaguch 
   3052  1.1  yamaguch 	icr = ixl_rd(sc, I40E_PFINT_ICR0);
   3053  1.1  yamaguch 	mask = ixl_rd(sc, I40E_PFINT_ICR0_ENA);
   3054  1.1  yamaguch 
   3055  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ADMINQ_MASK)) {
   3056  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_atq.ev_count);
   3057  1.1  yamaguch 		ixl_atq_done(sc);
   3058  1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_arq_task);
   3059  1.1  yamaguch 		rv = 1;
   3060  1.1  yamaguch 	}
   3061  1.1  yamaguch 
   3062  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_LINK_STAT_CHANGE_MASK)) {
   3063  1.1  yamaguch 		if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3064  1.1  yamaguch 			device_printf(sc->sc_dev, "link stat changed\n");
   3065  1.1  yamaguch 
   3066  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_link.ev_count);
   3067  1.1  yamaguch 		ixl_work_add(sc->sc_workq, &sc->sc_link_state_task);
   3068  1.1  yamaguch 		rv = 1;
   3069  1.1  yamaguch 	}
   3070  1.1  yamaguch 
   3071  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_GRST_MASK)) {
   3072  1.1  yamaguch 		CLR(mask, I40E_PFINT_ICR0_ENA_GRST_MASK);
   3073  1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLGEN_RSTAT);
   3074  1.1  yamaguch 		reg = reg & I40E_GLGEN_RSTAT_RESET_TYPE_MASK;
   3075  1.1  yamaguch 		reg = reg >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
   3076  1.1  yamaguch 
   3077  1.1  yamaguch 		device_printf(sc->sc_dev, "GRST: %s\n",
   3078  1.1  yamaguch 		    reg == I40E_RESET_CORER ? "CORER" :
   3079  1.1  yamaguch 		    reg == I40E_RESET_GLOBR ? "GLOBR" :
   3080  1.1  yamaguch 		    reg == I40E_RESET_EMPR ? "EMPR" :
   3081  1.1  yamaguch 		    "POR");
   3082  1.1  yamaguch 	}
   3083  1.1  yamaguch 
   3084  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_ECC_ERR_MASK))
   3085  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_ecc_err.ev_count);
   3086  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_PCI_EXCEPTION_MASK))
   3087  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_pci_exception.ev_count);
   3088  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_PE_CRITERR_MASK))
   3089  1.1  yamaguch 		atomic_inc_64(&sc->sc_event_crit_err.ev_count);
   3090  1.1  yamaguch 
   3091  1.1  yamaguch 	if (ISSET(icr, IXL_ICR0_CRIT_ERR_MASK)) {
   3092  1.1  yamaguch 		CLR(mask, IXL_ICR0_CRIT_ERR_MASK);
   3093  1.1  yamaguch 		device_printf(sc->sc_dev, "critical error\n");
   3094  1.1  yamaguch 	}
   3095  1.1  yamaguch 
   3096  1.1  yamaguch 	if (ISSET(icr, I40E_PFINT_ICR0_HMC_ERR_MASK)) {
   3097  1.1  yamaguch 		reg = ixl_rd(sc, I40E_PFHMC_ERRORINFO);
   3098  1.1  yamaguch 		if (ISSET(reg, I40E_PFHMC_ERRORINFO_ERROR_DETECTED_MASK))
   3099  1.1  yamaguch 			ixl_print_hmc_error(sc, reg);
   3100  1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_ERRORINFO, 0);
   3101  1.1  yamaguch 	}
   3102  1.1  yamaguch 
   3103  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, mask);
   3104  1.1  yamaguch 	ixl_flush(sc);
   3105  1.1  yamaguch 	ixl_enable_other_intr(sc);
   3106  1.1  yamaguch 	return rv;
   3107  1.1  yamaguch }
   3108  1.1  yamaguch 
   3109  1.1  yamaguch static void
   3110  1.1  yamaguch ixl_link_state_update_done(struct ixl_softc *sc)
   3111  1.1  yamaguch {
   3112  1.1  yamaguch 
   3113  1.1  yamaguch 	/* IXL_AQ_OP_PHY_LINK_STATUS already posted to admin reply queue */
   3114  1.1  yamaguch }
   3115  1.1  yamaguch 
   3116  1.1  yamaguch static void
   3117  1.1  yamaguch ixl_link_state_update(void *xsc)
   3118  1.1  yamaguch {
   3119  1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3120  1.1  yamaguch 	struct ixl_aq_desc *iaq;
   3121  1.1  yamaguch 	struct ixl_aq_link_param *param;
   3122  1.1  yamaguch 
   3123  1.1  yamaguch 	memset(&sc->sc_link_state_atq, 0, sizeof(sc->sc_link_state_atq));
   3124  1.1  yamaguch 	iaq = &sc->sc_link_state_atq.iatq_desc;
   3125  1.1  yamaguch 	iaq->iaq_opcode = htole16(IXL_AQ_OP_PHY_LINK_STATUS);
   3126  1.1  yamaguch 	param = (struct ixl_aq_link_param *)iaq->iaq_param;
   3127  1.1  yamaguch 	param->notify = IXL_AQ_LINK_NOTIFY;
   3128  1.1  yamaguch 
   3129  1.1  yamaguch 	ixl_atq_set(&sc->sc_link_state_atq, ixl_link_state_update_done);
   3130  1.1  yamaguch 	(void)ixl_atq_post(sc, &sc->sc_link_state_atq);
   3131  1.1  yamaguch }
   3132  1.1  yamaguch 
   3133  1.1  yamaguch static void
   3134  1.1  yamaguch ixl_arq_link_status(struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   3135  1.1  yamaguch {
   3136  1.1  yamaguch 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3137  1.1  yamaguch 	int link_state;
   3138  1.1  yamaguch 
   3139  1.1  yamaguch 	link_state = ixl_set_link_status(sc, iaq);
   3140  1.1  yamaguch 
   3141  1.1  yamaguch 	if (ifp->if_link_state != link_state)
   3142  1.1  yamaguch 		if_link_state_change(ifp, link_state);
   3143  1.1  yamaguch 
   3144  1.1  yamaguch 	if (link_state != LINK_STATE_DOWN) {
   3145  1.1  yamaguch 		if_schedule_deferred_start(ifp);
   3146  1.1  yamaguch 	}
   3147  1.1  yamaguch }
   3148  1.1  yamaguch 
   3149  1.1  yamaguch static void
   3150  1.1  yamaguch ixl_aq_dump(const struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   3151  1.1  yamaguch {
   3152  1.1  yamaguch 	char	 buf[512];
   3153  1.1  yamaguch 	size_t	 len;
   3154  1.1  yamaguch 
   3155  1.1  yamaguch 	len = sizeof(buf);
   3156  1.1  yamaguch 	buf[--len] = '\0';
   3157  1.1  yamaguch 
   3158  1.1  yamaguch 	snprintb(buf, len, IXL_AQ_FLAGS_FMT, le16toh(iaq->iaq_flags));
   3159  1.1  yamaguch 	device_printf(sc->sc_dev, "flags %s opcode %04x\n",
   3160  1.1  yamaguch 	    buf, le16toh(iaq->iaq_opcode));
   3161  1.1  yamaguch 	device_printf(sc->sc_dev, "datalen %u retval %u\n",
   3162  1.1  yamaguch 	    le16toh(iaq->iaq_datalen), le16toh(iaq->iaq_retval));
   3163  1.1  yamaguch 	device_printf(sc->sc_dev, "cookie %016" PRIx64 "\n", iaq->iaq_cookie);
   3164  1.1  yamaguch 	device_printf(sc->sc_dev, "%08x %08x %08x %08x\n",
   3165  1.1  yamaguch 	    le32toh(iaq->iaq_param[0]), le32toh(iaq->iaq_param[1]),
   3166  1.1  yamaguch 	    le32toh(iaq->iaq_param[2]), le32toh(iaq->iaq_param[3]));
   3167  1.1  yamaguch }
   3168  1.1  yamaguch 
   3169  1.1  yamaguch static void
   3170  1.1  yamaguch ixl_arq(void *xsc)
   3171  1.1  yamaguch {
   3172  1.1  yamaguch 	struct ixl_softc *sc = xsc;
   3173  1.1  yamaguch 	struct ixl_aq_desc *arq, *iaq;
   3174  1.1  yamaguch 	struct ixl_aq_buf *aqb;
   3175  1.1  yamaguch 	unsigned int cons = sc->sc_arq_cons;
   3176  1.1  yamaguch 	unsigned int prod;
   3177  1.1  yamaguch 	int done = 0;
   3178  1.1  yamaguch 
   3179  1.1  yamaguch 	prod = ixl_rd(sc, sc->sc_aq_regs->arq_head) &
   3180  1.1  yamaguch 	    sc->sc_aq_regs->arq_head_mask;
   3181  1.1  yamaguch 
   3182  1.1  yamaguch 	if (cons == prod)
   3183  1.1  yamaguch 		goto done;
   3184  1.1  yamaguch 
   3185  1.1  yamaguch 	arq = IXL_DMA_KVA(&sc->sc_arq);
   3186  1.1  yamaguch 
   3187  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   3188  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_arq),
   3189  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3190  1.1  yamaguch 
   3191  1.1  yamaguch 	do {
   3192  1.1  yamaguch 		iaq = &arq[cons];
   3193  1.1  yamaguch 		aqb = sc->sc_arq_live[cons];
   3194  1.1  yamaguch 
   3195  1.1  yamaguch 		KASSERT(aqb != NULL);
   3196  1.1  yamaguch 
   3197  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, IXL_AQ_BUFLEN,
   3198  1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   3199  1.1  yamaguch 
   3200  1.1  yamaguch 		if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3201  1.1  yamaguch 			ixl_aq_dump(sc, iaq);
   3202  1.1  yamaguch 
   3203  1.1  yamaguch 		switch (iaq->iaq_opcode) {
   3204  1.1  yamaguch 		case htole16(IXL_AQ_OP_PHY_LINK_STATUS):
   3205  1.1  yamaguch 			ixl_arq_link_status(sc, iaq);
   3206  1.1  yamaguch 			break;
   3207  1.1  yamaguch 		}
   3208  1.1  yamaguch 
   3209  1.1  yamaguch 		memset(iaq, 0, sizeof(*iaq));
   3210  1.1  yamaguch 		sc->sc_arq_live[cons] = NULL;
   3211  1.1  yamaguch 		SIMPLEQ_INSERT_TAIL(&sc->sc_arq_idle, aqb, aqb_entry);
   3212  1.1  yamaguch 
   3213  1.1  yamaguch 		cons++;
   3214  1.1  yamaguch 		cons &= IXL_AQ_MASK;
   3215  1.1  yamaguch 
   3216  1.1  yamaguch 		done = 1;
   3217  1.1  yamaguch 	} while (cons != prod);
   3218  1.1  yamaguch 
   3219  1.1  yamaguch 	if (done) {
   3220  1.1  yamaguch 		sc->sc_arq_cons = cons;
   3221  1.1  yamaguch 		ixl_arq_fill(sc);
   3222  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_arq),
   3223  1.1  yamaguch 		    0, IXL_DMA_LEN(&sc->sc_arq),
   3224  1.1  yamaguch 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3225  1.1  yamaguch 	}
   3226  1.1  yamaguch 
   3227  1.1  yamaguch done:
   3228  1.1  yamaguch 	ixl_enable_other_intr(sc);
   3229  1.1  yamaguch }
   3230  1.1  yamaguch 
   3231  1.1  yamaguch static void
   3232  1.1  yamaguch ixl_atq_set(struct ixl_atq *iatq, void (*fn)(struct ixl_softc *))
   3233  1.1  yamaguch {
   3234  1.1  yamaguch 	iatq->iatq_fn = fn;
   3235  1.1  yamaguch }
   3236  1.1  yamaguch 
   3237  1.1  yamaguch static int
   3238  1.1  yamaguch ixl_atq_post_locked(struct ixl_softc *sc, struct ixl_atq *iatq)
   3239  1.1  yamaguch {
   3240  1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3241  1.1  yamaguch 	unsigned int prod, cons, prod_next;
   3242  1.1  yamaguch 
   3243  1.1  yamaguch 	/* assert locked */
   3244  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3245  1.1  yamaguch 
   3246  1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3247  1.1  yamaguch 	prod = sc->sc_atq_prod;
   3248  1.1  yamaguch 	cons = sc->sc_atq_cons;
   3249  1.1  yamaguch 	prod_next = (prod +1) & IXL_AQ_MASK;
   3250  1.1  yamaguch 
   3251  1.1  yamaguch 	if (cons == prod_next)
   3252  1.1  yamaguch 		return ENOMEM;
   3253  1.1  yamaguch 
   3254  1.1  yamaguch 	slot = &atq[prod];
   3255  1.1  yamaguch 
   3256  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3257  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTWRITE);
   3258  1.1  yamaguch 
   3259  1.1  yamaguch 	*slot = iatq->iatq_desc;
   3260  1.1  yamaguch 	slot->iaq_cookie = (uint64_t)((intptr_t)iatq);
   3261  1.1  yamaguch 
   3262  1.1  yamaguch 	if (ISSET(sc->sc_ec.ec_if.if_flags, IFF_DEBUG))
   3263  1.1  yamaguch 		ixl_aq_dump(sc, slot);
   3264  1.1  yamaguch 
   3265  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3266  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREWRITE);
   3267  1.1  yamaguch 
   3268  1.1  yamaguch 	sc->sc_atq_prod = prod_next;
   3269  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, sc->sc_atq_prod);
   3270  1.1  yamaguch 
   3271  1.1  yamaguch 	return 0;
   3272  1.1  yamaguch }
   3273  1.1  yamaguch 
   3274  1.1  yamaguch static int
   3275  1.1  yamaguch ixl_atq_post(struct ixl_softc *sc, struct ixl_atq *iatq)
   3276  1.1  yamaguch {
   3277  1.1  yamaguch 	int rv;
   3278  1.1  yamaguch 
   3279  1.1  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3280  1.1  yamaguch 	rv = ixl_atq_post_locked(sc, iatq);
   3281  1.1  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3282  1.1  yamaguch 
   3283  1.1  yamaguch 	return rv;
   3284  1.1  yamaguch }
   3285  1.1  yamaguch 
   3286  1.1  yamaguch static void
   3287  1.1  yamaguch ixl_atq_done_locked(struct ixl_softc *sc)
   3288  1.1  yamaguch {
   3289  1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3290  1.1  yamaguch 	struct ixl_atq *iatq;
   3291  1.1  yamaguch 	unsigned int cons;
   3292  1.1  yamaguch 	unsigned int prod;
   3293  1.1  yamaguch 
   3294  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3295  1.1  yamaguch 
   3296  1.1  yamaguch 	prod = sc->sc_atq_prod;
   3297  1.1  yamaguch 	cons = sc->sc_atq_cons;
   3298  1.1  yamaguch 
   3299  1.1  yamaguch 	if (prod == cons)
   3300  1.1  yamaguch 		return;
   3301  1.1  yamaguch 
   3302  1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3303  1.1  yamaguch 
   3304  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3305  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   3306  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3307  1.1  yamaguch 
   3308  1.1  yamaguch 	do {
   3309  1.1  yamaguch 		slot = &atq[cons];
   3310  1.1  yamaguch 		if (!ISSET(slot->iaq_flags, htole16(IXL_AQ_DD)))
   3311  1.1  yamaguch 			break;
   3312  1.1  yamaguch 
   3313  1.1  yamaguch 		iatq = (struct ixl_atq *)((intptr_t)slot->iaq_cookie);
   3314  1.1  yamaguch 		iatq->iatq_desc = *slot;
   3315  1.1  yamaguch 
   3316  1.1  yamaguch 		memset(slot, 0, sizeof(*slot));
   3317  1.1  yamaguch 
   3318  1.1  yamaguch 		(*iatq->iatq_fn)(sc);
   3319  1.1  yamaguch 
   3320  1.1  yamaguch 		cons++;
   3321  1.1  yamaguch 		cons &= IXL_AQ_MASK;
   3322  1.1  yamaguch 	} while (cons != prod);
   3323  1.1  yamaguch 
   3324  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3325  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq),
   3326  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3327  1.1  yamaguch 
   3328  1.1  yamaguch 	sc->sc_atq_cons = cons;
   3329  1.1  yamaguch }
   3330  1.1  yamaguch 
   3331  1.1  yamaguch static void
   3332  1.1  yamaguch ixl_atq_done(struct ixl_softc *sc)
   3333  1.1  yamaguch {
   3334  1.1  yamaguch 
   3335  1.1  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3336  1.1  yamaguch 	ixl_atq_done_locked(sc);
   3337  1.1  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3338  1.1  yamaguch }
   3339  1.1  yamaguch 
   3340  1.1  yamaguch static void
   3341  1.1  yamaguch ixl_wakeup(struct ixl_softc *sc)
   3342  1.1  yamaguch {
   3343  1.1  yamaguch 
   3344  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_atq_lock));
   3345  1.1  yamaguch 
   3346  1.1  yamaguch 	cv_signal(&sc->sc_atq_cv);
   3347  1.1  yamaguch }
   3348  1.1  yamaguch 
   3349  1.1  yamaguch static int
   3350  1.1  yamaguch ixl_atq_exec(struct ixl_softc *sc, struct ixl_atq *iatq)
   3351  1.1  yamaguch {
   3352  1.1  yamaguch 	int error;
   3353  1.1  yamaguch 
   3354  1.1  yamaguch 	KASSERT(iatq->iatq_desc.iaq_cookie == 0);
   3355  1.1  yamaguch 
   3356  1.1  yamaguch 	ixl_atq_set(iatq, ixl_wakeup);
   3357  1.1  yamaguch 
   3358  1.1  yamaguch 	mutex_enter(&sc->sc_atq_lock);
   3359  1.1  yamaguch 	error = ixl_atq_post_locked(sc, iatq);
   3360  1.1  yamaguch 	if (error) {
   3361  1.1  yamaguch 		mutex_exit(&sc->sc_atq_lock);
   3362  1.1  yamaguch 		return error;
   3363  1.1  yamaguch 	}
   3364  1.1  yamaguch 
   3365  1.1  yamaguch 	error = cv_timedwait(&sc->sc_atq_cv, &sc->sc_atq_lock,
   3366  1.1  yamaguch 	    IXL_ATQ_EXEC_TIMEOUT);
   3367  1.1  yamaguch 	mutex_exit(&sc->sc_atq_lock);
   3368  1.1  yamaguch 
   3369  1.1  yamaguch 	return error;
   3370  1.1  yamaguch }
   3371  1.1  yamaguch 
   3372  1.1  yamaguch static int
   3373  1.1  yamaguch ixl_atq_poll(struct ixl_softc *sc, struct ixl_aq_desc *iaq, unsigned int tm)
   3374  1.1  yamaguch {
   3375  1.1  yamaguch 	struct ixl_aq_desc *atq, *slot;
   3376  1.1  yamaguch 	unsigned int prod;
   3377  1.1  yamaguch 	unsigned int t = 0;
   3378  1.1  yamaguch 
   3379  1.1  yamaguch 	atq = IXL_DMA_KVA(&sc->sc_atq);
   3380  1.1  yamaguch 	prod = sc->sc_atq_prod;
   3381  1.1  yamaguch 	slot = atq + prod;
   3382  1.1  yamaguch 
   3383  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3384  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTWRITE);
   3385  1.1  yamaguch 
   3386  1.1  yamaguch 	*slot = *iaq;
   3387  1.1  yamaguch 	slot->iaq_flags |= htole16(IXL_AQ_SI);
   3388  1.1  yamaguch 
   3389  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3390  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREWRITE);
   3391  1.1  yamaguch 
   3392  1.1  yamaguch 	prod++;
   3393  1.1  yamaguch 	prod &= IXL_AQ_MASK;
   3394  1.1  yamaguch 	sc->sc_atq_prod = prod;
   3395  1.1  yamaguch 	ixl_wr(sc, sc->sc_aq_regs->atq_tail, prod);
   3396  1.1  yamaguch 
   3397  1.1  yamaguch 	while (ixl_rd(sc, sc->sc_aq_regs->atq_head) != prod) {
   3398  1.1  yamaguch 		delaymsec(1);
   3399  1.1  yamaguch 
   3400  1.1  yamaguch 		if (t++ > tm)
   3401  1.1  yamaguch 			return ETIMEDOUT;
   3402  1.1  yamaguch 	}
   3403  1.1  yamaguch 
   3404  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3405  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_POSTREAD);
   3406  1.1  yamaguch 	*iaq = *slot;
   3407  1.1  yamaguch 	memset(slot, 0, sizeof(*slot));
   3408  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_atq),
   3409  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_atq), BUS_DMASYNC_PREREAD);
   3410  1.1  yamaguch 
   3411  1.1  yamaguch 	sc->sc_atq_cons = prod;
   3412  1.1  yamaguch 
   3413  1.1  yamaguch 	return 0;
   3414  1.1  yamaguch }
   3415  1.1  yamaguch 
   3416  1.1  yamaguch static int
   3417  1.1  yamaguch ixl_get_version(struct ixl_softc *sc)
   3418  1.1  yamaguch {
   3419  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3420  1.1  yamaguch 	uint32_t fwbuild, fwver, apiver;
   3421  1.1  yamaguch 	uint16_t api_maj_ver, api_min_ver;
   3422  1.1  yamaguch 
   3423  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3424  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_GET_VERSION);
   3425  1.1  yamaguch 
   3426  1.1  yamaguch 	iaq.iaq_retval = le16toh(23);
   3427  1.1  yamaguch 
   3428  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 2000) != 0)
   3429  1.1  yamaguch 		return ETIMEDOUT;
   3430  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK))
   3431  1.1  yamaguch 		return EIO;
   3432  1.1  yamaguch 
   3433  1.1  yamaguch 	fwbuild = le32toh(iaq.iaq_param[1]);
   3434  1.1  yamaguch 	fwver = le32toh(iaq.iaq_param[2]);
   3435  1.1  yamaguch 	apiver = le32toh(iaq.iaq_param[3]);
   3436  1.1  yamaguch 
   3437  1.1  yamaguch 	api_maj_ver = (uint16_t)apiver;
   3438  1.1  yamaguch 	api_min_ver = (uint16_t)(apiver >> 16);
   3439  1.1  yamaguch 
   3440  1.1  yamaguch 	aprint_normal(", FW %hu.%hu.%05u API %hu.%hu", (uint16_t)fwver,
   3441  1.1  yamaguch 	    (uint16_t)(fwver >> 16), fwbuild, api_maj_ver, api_min_ver);
   3442  1.1  yamaguch 
   3443  1.1  yamaguch 	sc->sc_rxctl_atq = true;
   3444  1.1  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722) {
   3445  1.1  yamaguch 		if (api_maj_ver == 1 && api_min_ver < 5) {
   3446  1.1  yamaguch 			sc->sc_rxctl_atq = false;
   3447  1.1  yamaguch 		}
   3448  1.1  yamaguch 	}
   3449  1.1  yamaguch 
   3450  1.1  yamaguch 	return 0;
   3451  1.1  yamaguch }
   3452  1.1  yamaguch 
   3453  1.1  yamaguch static int
   3454  1.1  yamaguch ixl_pxe_clear(struct ixl_softc *sc)
   3455  1.1  yamaguch {
   3456  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3457  1.1  yamaguch 	int rv;
   3458  1.1  yamaguch 
   3459  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3460  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_CLEAR_PXE_MODE);
   3461  1.1  yamaguch 	iaq.iaq_param[0] = htole32(0x2);
   3462  1.1  yamaguch 
   3463  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3464  1.1  yamaguch 
   3465  1.1  yamaguch 	ixl_wr(sc, I40E_GLLAN_RCTL_0, 0x1);
   3466  1.1  yamaguch 
   3467  1.1  yamaguch 	if (rv != 0)
   3468  1.1  yamaguch 		return ETIMEDOUT;
   3469  1.1  yamaguch 
   3470  1.1  yamaguch 	switch (iaq.iaq_retval) {
   3471  1.1  yamaguch 	case htole16(IXL_AQ_RC_OK):
   3472  1.1  yamaguch 	case htole16(IXL_AQ_RC_EEXIST):
   3473  1.1  yamaguch 		break;
   3474  1.1  yamaguch 	default:
   3475  1.1  yamaguch 		return EIO;
   3476  1.1  yamaguch 	}
   3477  1.1  yamaguch 
   3478  1.1  yamaguch 	return 0;
   3479  1.1  yamaguch }
   3480  1.1  yamaguch 
   3481  1.1  yamaguch static int
   3482  1.1  yamaguch ixl_lldp_shut(struct ixl_softc *sc)
   3483  1.1  yamaguch {
   3484  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3485  1.1  yamaguch 
   3486  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3487  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_LLDP_STOP_AGENT);
   3488  1.1  yamaguch 	iaq.iaq_param[0] = htole32(IXL_LLDP_SHUTDOWN);
   3489  1.1  yamaguch 
   3490  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   3491  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "STOP LLDP AGENT timeout\n");
   3492  1.1  yamaguch 		return -1;
   3493  1.1  yamaguch 	}
   3494  1.1  yamaguch 
   3495  1.1  yamaguch 	switch (iaq.iaq_retval) {
   3496  1.1  yamaguch 	case htole16(IXL_AQ_RC_EMODE):
   3497  1.1  yamaguch 	case htole16(IXL_AQ_RC_EPERM):
   3498  1.1  yamaguch 		/* ignore silently */
   3499  1.1  yamaguch 	default:
   3500  1.1  yamaguch 		break;
   3501  1.1  yamaguch 	}
   3502  1.1  yamaguch 
   3503  1.1  yamaguch 	return 0;
   3504  1.1  yamaguch }
   3505  1.1  yamaguch 
   3506  1.1  yamaguch static void
   3507  1.1  yamaguch ixl_parse_hw_capability(struct ixl_softc *sc, struct ixl_aq_capability *cap)
   3508  1.1  yamaguch {
   3509  1.1  yamaguch 	uint16_t id;
   3510  1.1  yamaguch 	uint32_t number, logical_id;
   3511  1.1  yamaguch 
   3512  1.1  yamaguch 	id = le16toh(cap->cap_id);
   3513  1.1  yamaguch 	number = le32toh(cap->number);
   3514  1.1  yamaguch 	logical_id = le32toh(cap->logical_id);
   3515  1.1  yamaguch 
   3516  1.1  yamaguch 	switch (id) {
   3517  1.1  yamaguch 	case IXL_AQ_CAP_RSS:
   3518  1.1  yamaguch 		sc->sc_rss_table_size = number;
   3519  1.1  yamaguch 		sc->sc_rss_table_entry_width = logical_id;
   3520  1.1  yamaguch 		break;
   3521  1.1  yamaguch 	case IXL_AQ_CAP_RXQ:
   3522  1.1  yamaguch 	case IXL_AQ_CAP_TXQ:
   3523  1.1  yamaguch 		sc->sc_nqueue_pairs_device = MIN(number,
   3524  1.1  yamaguch 		    sc->sc_nqueue_pairs_device);
   3525  1.1  yamaguch 		break;
   3526  1.1  yamaguch 	}
   3527  1.1  yamaguch }
   3528  1.1  yamaguch 
   3529  1.1  yamaguch static int
   3530  1.1  yamaguch ixl_get_hw_capabilities(struct ixl_softc *sc)
   3531  1.1  yamaguch {
   3532  1.1  yamaguch 	struct ixl_dmamem idm;
   3533  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3534  1.1  yamaguch 	struct ixl_aq_capability *caps;
   3535  1.1  yamaguch 	size_t i, ncaps;
   3536  1.1  yamaguch 	bus_size_t caps_size;
   3537  1.1  yamaguch 	uint16_t status;
   3538  1.1  yamaguch 	int rv;
   3539  1.1  yamaguch 
   3540  1.1  yamaguch 	caps_size = sizeof(caps[0]) * 40;
   3541  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3542  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_LIST_FUNC_CAP);
   3543  1.1  yamaguch 
   3544  1.1  yamaguch 	do {
   3545  1.1  yamaguch 		if (ixl_dmamem_alloc(sc, &idm, caps_size, 0) != 0) {
   3546  1.1  yamaguch 			return -1;
   3547  1.1  yamaguch 		}
   3548  1.1  yamaguch 
   3549  1.1  yamaguch 		iaq.iaq_flags = htole16(IXL_AQ_BUF |
   3550  1.1  yamaguch 		    (caps_size > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   3551  1.1  yamaguch 		iaq.iaq_datalen = htole16(caps_size);
   3552  1.1  yamaguch 		ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   3553  1.1  yamaguch 
   3554  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0,
   3555  1.1  yamaguch 		    IXL_DMA_LEN(&idm), BUS_DMASYNC_PREREAD);
   3556  1.1  yamaguch 
   3557  1.1  yamaguch 		rv = ixl_atq_poll(sc, &iaq, 250);
   3558  1.1  yamaguch 
   3559  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0,
   3560  1.1  yamaguch 		    IXL_DMA_LEN(&idm), BUS_DMASYNC_POSTREAD);
   3561  1.1  yamaguch 
   3562  1.1  yamaguch 		if (rv != 0) {
   3563  1.1  yamaguch 			aprint_error(", HW capabilities timeout\n");
   3564  1.1  yamaguch 			goto done;
   3565  1.1  yamaguch 		}
   3566  1.1  yamaguch 
   3567  1.1  yamaguch 		status = le16toh(iaq.iaq_retval);
   3568  1.1  yamaguch 
   3569  1.1  yamaguch 		if (status == IXL_AQ_RC_ENOMEM) {
   3570  1.1  yamaguch 			caps_size = le16toh(iaq.iaq_datalen);
   3571  1.1  yamaguch 			ixl_dmamem_free(sc, &idm);
   3572  1.1  yamaguch 		}
   3573  1.1  yamaguch 	} while (status == IXL_AQ_RC_ENOMEM);
   3574  1.1  yamaguch 
   3575  1.1  yamaguch 	if (status != IXL_AQ_RC_OK) {
   3576  1.1  yamaguch 		aprint_error(", HW capabilities error\n");
   3577  1.1  yamaguch 		goto done;
   3578  1.1  yamaguch 	}
   3579  1.1  yamaguch 
   3580  1.1  yamaguch 	caps = IXL_DMA_KVA(&idm);
   3581  1.1  yamaguch 	ncaps = le16toh(iaq.iaq_param[1]);
   3582  1.1  yamaguch 
   3583  1.1  yamaguch 	for (i = 0; i < ncaps; i++) {
   3584  1.1  yamaguch 		ixl_parse_hw_capability(sc, &caps[i]);
   3585  1.1  yamaguch 	}
   3586  1.1  yamaguch 
   3587  1.1  yamaguch done:
   3588  1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   3589  1.1  yamaguch 	return rv;
   3590  1.1  yamaguch }
   3591  1.1  yamaguch 
   3592  1.1  yamaguch static int
   3593  1.1  yamaguch ixl_get_mac(struct ixl_softc *sc)
   3594  1.1  yamaguch {
   3595  1.1  yamaguch 	struct ixl_dmamem idm;
   3596  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3597  1.1  yamaguch 	struct ixl_aq_mac_addresses *addrs;
   3598  1.1  yamaguch 	int rv;
   3599  1.1  yamaguch 
   3600  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, sizeof(*addrs), 0) != 0) {
   3601  1.1  yamaguch 		aprint_error(", unable to allocate mac addresses\n");
   3602  1.1  yamaguch 		return -1;
   3603  1.1  yamaguch 	}
   3604  1.1  yamaguch 
   3605  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3606  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF);
   3607  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_MAC_ADDRESS_READ);
   3608  1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*addrs));
   3609  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   3610  1.1  yamaguch 
   3611  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   3612  1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   3613  1.1  yamaguch 
   3614  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3615  1.1  yamaguch 
   3616  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   3617  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   3618  1.1  yamaguch 
   3619  1.1  yamaguch 	if (rv != 0) {
   3620  1.1  yamaguch 		aprint_error(", MAC ADDRESS READ timeout\n");
   3621  1.1  yamaguch 		rv = -1;
   3622  1.1  yamaguch 		goto done;
   3623  1.1  yamaguch 	}
   3624  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3625  1.1  yamaguch 		aprint_error(", MAC ADDRESS READ error\n");
   3626  1.1  yamaguch 		rv = -1;
   3627  1.1  yamaguch 		goto done;
   3628  1.1  yamaguch 	}
   3629  1.1  yamaguch 
   3630  1.1  yamaguch 	addrs = IXL_DMA_KVA(&idm);
   3631  1.1  yamaguch 	if (!ISSET(iaq.iaq_param[0], htole32(IXL_AQ_MAC_PORT_VALID))) {
   3632  1.1  yamaguch 		printf(", port address is not valid\n");
   3633  1.1  yamaguch 		goto done;
   3634  1.1  yamaguch 	}
   3635  1.1  yamaguch 
   3636  1.1  yamaguch 	memcpy(sc->sc_enaddr, addrs->port, ETHER_ADDR_LEN);
   3637  1.1  yamaguch 	rv = 0;
   3638  1.1  yamaguch 
   3639  1.1  yamaguch done:
   3640  1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   3641  1.1  yamaguch 	return rv;
   3642  1.1  yamaguch }
   3643  1.1  yamaguch 
   3644  1.1  yamaguch static int
   3645  1.1  yamaguch ixl_get_switch_config(struct ixl_softc *sc)
   3646  1.1  yamaguch {
   3647  1.1  yamaguch 	struct ixl_dmamem idm;
   3648  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3649  1.1  yamaguch 	struct ixl_aq_switch_config *hdr;
   3650  1.1  yamaguch 	struct ixl_aq_switch_config_element *elms, *elm;
   3651  1.1  yamaguch 	unsigned int nelm, i;
   3652  1.1  yamaguch 	int rv;
   3653  1.1  yamaguch 
   3654  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, IXL_AQ_BUFLEN, 0) != 0) {
   3655  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   3656  1.1  yamaguch 		    "unable to allocate switch config buffer\n");
   3657  1.1  yamaguch 		return -1;
   3658  1.1  yamaguch 	}
   3659  1.1  yamaguch 
   3660  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3661  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   3662  1.1  yamaguch 	    (IXL_AQ_BUFLEN > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   3663  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_SWITCH_GET_CONFIG);
   3664  1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_AQ_BUFLEN);
   3665  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&idm));
   3666  1.1  yamaguch 
   3667  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   3668  1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   3669  1.1  yamaguch 
   3670  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3671  1.1  yamaguch 
   3672  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&idm), 0, IXL_DMA_LEN(&idm),
   3673  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   3674  1.1  yamaguch 
   3675  1.1  yamaguch 	if (rv != 0) {
   3676  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET SWITCH CONFIG timeout\n");
   3677  1.1  yamaguch 		rv = -1;
   3678  1.1  yamaguch 		goto done;
   3679  1.1  yamaguch 	}
   3680  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3681  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET SWITCH CONFIG error\n");
   3682  1.1  yamaguch 		rv = -1;
   3683  1.1  yamaguch 		goto done;
   3684  1.1  yamaguch 	}
   3685  1.1  yamaguch 
   3686  1.1  yamaguch 	hdr = IXL_DMA_KVA(&idm);
   3687  1.1  yamaguch 	elms = (struct ixl_aq_switch_config_element *)(hdr + 1);
   3688  1.1  yamaguch 
   3689  1.1  yamaguch 	nelm = le16toh(hdr->num_reported);
   3690  1.1  yamaguch 	if (nelm < 1) {
   3691  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "no switch config available\n");
   3692  1.1  yamaguch 		rv = -1;
   3693  1.1  yamaguch 		goto done;
   3694  1.1  yamaguch 	}
   3695  1.1  yamaguch 
   3696  1.1  yamaguch 	for (i = 0; i < nelm; i++) {
   3697  1.1  yamaguch 		elm = &elms[i];
   3698  1.1  yamaguch 
   3699  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   3700  1.1  yamaguch 		    "type %x revision %u seid %04x\n",
   3701  1.1  yamaguch 		    elm->type, elm->revision, le16toh(elm->seid));
   3702  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   3703  1.1  yamaguch 		    "uplink %04x downlink %04x\n",
   3704  1.1  yamaguch 		    le16toh(elm->uplink_seid),
   3705  1.1  yamaguch 		    le16toh(elm->downlink_seid));
   3706  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   3707  1.1  yamaguch 		    "conntype %x scheduler %04x extra %04x\n",
   3708  1.1  yamaguch 		    elm->connection_type,
   3709  1.1  yamaguch 		    le16toh(elm->scheduler_id),
   3710  1.1  yamaguch 		    le16toh(elm->element_info));
   3711  1.1  yamaguch 	}
   3712  1.1  yamaguch 
   3713  1.1  yamaguch 	elm = &elms[0];
   3714  1.1  yamaguch 
   3715  1.1  yamaguch 	sc->sc_uplink_seid = elm->uplink_seid;
   3716  1.1  yamaguch 	sc->sc_downlink_seid = elm->downlink_seid;
   3717  1.1  yamaguch 	sc->sc_seid = elm->seid;
   3718  1.1  yamaguch 
   3719  1.1  yamaguch 	if ((sc->sc_uplink_seid == htole16(0)) !=
   3720  1.1  yamaguch 	    (sc->sc_downlink_seid == htole16(0))) {
   3721  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SEIDs are misconfigured\n");
   3722  1.1  yamaguch 		rv = -1;
   3723  1.1  yamaguch 		goto done;
   3724  1.1  yamaguch 	}
   3725  1.1  yamaguch 
   3726  1.1  yamaguch done:
   3727  1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   3728  1.1  yamaguch 	return rv;
   3729  1.1  yamaguch }
   3730  1.1  yamaguch 
   3731  1.1  yamaguch static int
   3732  1.1  yamaguch ixl_phy_mask_ints(struct ixl_softc *sc)
   3733  1.1  yamaguch {
   3734  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3735  1.1  yamaguch 
   3736  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3737  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_SET_EVENT_MASK);
   3738  1.1  yamaguch 	iaq.iaq_param[2] = htole32(IXL_AQ_PHY_EV_MASK &
   3739  1.1  yamaguch 	    ~(IXL_AQ_PHY_EV_LINK_UPDOWN | IXL_AQ_PHY_EV_MODULE_QUAL_FAIL |
   3740  1.1  yamaguch 	      IXL_AQ_PHY_EV_MEDIA_NA));
   3741  1.1  yamaguch 
   3742  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   3743  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SET PHY EVENT MASK timeout\n");
   3744  1.1  yamaguch 		return -1;
   3745  1.1  yamaguch 	}
   3746  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3747  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "SET PHY EVENT MASK error\n");
   3748  1.1  yamaguch 		return -1;
   3749  1.1  yamaguch 	}
   3750  1.1  yamaguch 
   3751  1.1  yamaguch 	return 0;
   3752  1.1  yamaguch }
   3753  1.1  yamaguch 
   3754  1.1  yamaguch static int
   3755  1.1  yamaguch ixl_get_phy_abilities(struct ixl_softc *sc,struct ixl_dmamem *idm)
   3756  1.1  yamaguch {
   3757  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3758  1.1  yamaguch 	int rv;
   3759  1.1  yamaguch 
   3760  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3761  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   3762  1.1  yamaguch 	    (IXL_DMA_LEN(idm) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   3763  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_GET_ABILITIES);
   3764  1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(idm));
   3765  1.1  yamaguch 	iaq.iaq_param[0] = htole32(IXL_AQ_PHY_REPORT_INIT);
   3766  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(idm));
   3767  1.1  yamaguch 
   3768  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   3769  1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   3770  1.1  yamaguch 
   3771  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3772  1.1  yamaguch 
   3773  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(idm), 0, IXL_DMA_LEN(idm),
   3774  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   3775  1.1  yamaguch 
   3776  1.1  yamaguch 	if (rv != 0)
   3777  1.1  yamaguch 		return -1;
   3778  1.1  yamaguch 
   3779  1.1  yamaguch 	return le16toh(iaq.iaq_retval);
   3780  1.1  yamaguch }
   3781  1.1  yamaguch 
   3782  1.1  yamaguch static int
   3783  1.1  yamaguch ixl_get_phy_types(struct ixl_softc *sc, uint64_t *phy_types_ptr)
   3784  1.1  yamaguch {
   3785  1.1  yamaguch 	struct ixl_dmamem idm;
   3786  1.1  yamaguch 	struct ixl_aq_phy_abilities *phy;
   3787  1.1  yamaguch 	uint64_t phy_types;
   3788  1.1  yamaguch 	int rv;
   3789  1.1  yamaguch 
   3790  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &idm, IXL_AQ_BUFLEN, 0) != 0) {
   3791  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   3792  1.1  yamaguch 		    "unable to allocate switch config buffer\n");
   3793  1.1  yamaguch 		return -1;
   3794  1.1  yamaguch 	}
   3795  1.1  yamaguch 
   3796  1.1  yamaguch 	rv = ixl_get_phy_abilities(sc, &idm);
   3797  1.1  yamaguch 	switch (rv) {
   3798  1.1  yamaguch 	case -1:
   3799  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET PHY ABILITIES timeout\n");
   3800  1.1  yamaguch 		goto done;
   3801  1.1  yamaguch 	case IXL_AQ_RC_OK:
   3802  1.1  yamaguch 		break;
   3803  1.1  yamaguch 	case IXL_AQ_RC_EIO:
   3804  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,"unable to query phy types\n");
   3805  1.1  yamaguch 		break;
   3806  1.1  yamaguch 	default:
   3807  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   3808  1.1  yamaguch 		    "GET PHY ABILITIIES error %u\n", rv);
   3809  1.1  yamaguch 		goto done;
   3810  1.1  yamaguch 	}
   3811  1.1  yamaguch 
   3812  1.1  yamaguch 	phy = IXL_DMA_KVA(&idm);
   3813  1.1  yamaguch 
   3814  1.1  yamaguch 	phy_types = le32toh(phy->phy_type);
   3815  1.1  yamaguch 	phy_types |= (uint64_t)le32toh(phy->phy_type_ext) << 32;
   3816  1.1  yamaguch 
   3817  1.1  yamaguch 	*phy_types_ptr = phy_types;
   3818  1.1  yamaguch 
   3819  1.1  yamaguch 	rv = 0;
   3820  1.1  yamaguch 
   3821  1.1  yamaguch done:
   3822  1.1  yamaguch 	ixl_dmamem_free(sc, &idm);
   3823  1.1  yamaguch 	return rv;
   3824  1.1  yamaguch }
   3825  1.1  yamaguch 
   3826  1.1  yamaguch static int
   3827  1.1  yamaguch ixl_get_link_status(struct ixl_softc *sc)
   3828  1.1  yamaguch {
   3829  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3830  1.1  yamaguch 	struct ixl_aq_link_param *param;
   3831  1.1  yamaguch 	int link;
   3832  1.1  yamaguch 
   3833  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3834  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_LINK_STATUS);
   3835  1.1  yamaguch 	param = (struct ixl_aq_link_param *)iaq.iaq_param;
   3836  1.1  yamaguch 	param->notify = IXL_AQ_LINK_NOTIFY;
   3837  1.1  yamaguch 
   3838  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   3839  1.1  yamaguch 		return ETIMEDOUT;
   3840  1.1  yamaguch 	}
   3841  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3842  1.1  yamaguch 		return EIO;
   3843  1.1  yamaguch 	}
   3844  1.1  yamaguch 
   3845  1.1  yamaguch 	link = ixl_set_link_status(sc, &iaq);
   3846  1.1  yamaguch 	sc->sc_ec.ec_if.if_link_state = link;
   3847  1.1  yamaguch 
   3848  1.1  yamaguch 	return 0;
   3849  1.1  yamaguch }
   3850  1.1  yamaguch 
   3851  1.1  yamaguch static int
   3852  1.1  yamaguch ixl_get_vsi(struct ixl_softc *sc)
   3853  1.1  yamaguch {
   3854  1.1  yamaguch 	struct ixl_dmamem *vsi = &sc->sc_scratch;
   3855  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3856  1.1  yamaguch 	struct ixl_aq_vsi_param *param;
   3857  1.1  yamaguch 	struct ixl_aq_vsi_reply *reply;
   3858  1.1  yamaguch 	int rv;
   3859  1.1  yamaguch 
   3860  1.1  yamaguch 	/* grumble, vsi info isn't "known" at compile time */
   3861  1.1  yamaguch 
   3862  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3863  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF |
   3864  1.1  yamaguch 	    (IXL_DMA_LEN(vsi) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   3865  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_GET_VSI_PARAMS);
   3866  1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(vsi));
   3867  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(vsi));
   3868  1.1  yamaguch 
   3869  1.1  yamaguch 	param = (struct ixl_aq_vsi_param *)iaq.iaq_param;
   3870  1.1  yamaguch 	param->uplink_seid = sc->sc_seid;
   3871  1.1  yamaguch 
   3872  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   3873  1.1  yamaguch 	    BUS_DMASYNC_PREREAD);
   3874  1.1  yamaguch 
   3875  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3876  1.1  yamaguch 
   3877  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   3878  1.1  yamaguch 	    BUS_DMASYNC_POSTREAD);
   3879  1.1  yamaguch 
   3880  1.1  yamaguch 	if (rv != 0) {
   3881  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET VSI timeout\n");
   3882  1.1  yamaguch 		return -1;
   3883  1.1  yamaguch 	}
   3884  1.1  yamaguch 
   3885  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3886  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "GET VSI error %u\n",
   3887  1.1  yamaguch 		    le16toh(iaq.iaq_retval));
   3888  1.1  yamaguch 		return -1;
   3889  1.1  yamaguch 	}
   3890  1.1  yamaguch 
   3891  1.1  yamaguch 	reply = (struct ixl_aq_vsi_reply *)iaq.iaq_param;
   3892  1.1  yamaguch 	sc->sc_vsi_number = reply->vsi_number;
   3893  1.1  yamaguch 
   3894  1.1  yamaguch 	return 0;
   3895  1.1  yamaguch }
   3896  1.1  yamaguch 
   3897  1.1  yamaguch static int
   3898  1.1  yamaguch ixl_set_vsi(struct ixl_softc *sc)
   3899  1.1  yamaguch {
   3900  1.1  yamaguch 	struct ixl_dmamem *vsi = &sc->sc_scratch;
   3901  1.1  yamaguch 	struct ixl_aq_desc iaq;
   3902  1.1  yamaguch 	struct ixl_aq_vsi_param *param;
   3903  1.1  yamaguch 	struct ixl_aq_vsi_data *data = IXL_DMA_KVA(vsi);
   3904  1.1  yamaguch 	unsigned int qnum;
   3905  1.1  yamaguch 	int rv;
   3906  1.1  yamaguch 
   3907  1.1  yamaguch 	qnum = sc->sc_nqueue_pairs - 1;
   3908  1.1  yamaguch 
   3909  1.1  yamaguch 	data->valid_sections = htole16(IXL_AQ_VSI_VALID_QUEUE_MAP |
   3910  1.1  yamaguch 	    IXL_AQ_VSI_VALID_VLAN);
   3911  1.1  yamaguch 
   3912  1.1  yamaguch 	CLR(data->mapping_flags, htole16(IXL_AQ_VSI_QUE_MAP_MASK));
   3913  1.1  yamaguch 	SET(data->mapping_flags, htole16(IXL_AQ_VSI_QUE_MAP_CONTIG));
   3914  1.1  yamaguch 	data->queue_mapping[0] = htole16(0);
   3915  1.1  yamaguch 	data->tc_mapping[0] = htole16((0 << IXL_AQ_VSI_TC_Q_OFFSET_SHIFT) |
   3916  1.1  yamaguch 	    (qnum << IXL_AQ_VSI_TC_Q_NUMBER_SHIFT));
   3917  1.1  yamaguch 
   3918  1.1  yamaguch 	CLR(data->port_vlan_flags,
   3919  1.1  yamaguch 	    htole16(IXL_AQ_VSI_PVLAN_MODE_MASK | IXL_AQ_VSI_PVLAN_EMOD_MASK));
   3920  1.1  yamaguch 	SET(data->port_vlan_flags,
   3921  1.1  yamaguch 	    htole16(IXL_AQ_VSI_PVLAN_MODE_ALL | IXL_AQ_VSI_PVLAN_EMOD_NOTHING));
   3922  1.1  yamaguch 
   3923  1.1  yamaguch 	/* grumble, vsi info isn't "known" at compile time */
   3924  1.1  yamaguch 
   3925  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   3926  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD |
   3927  1.1  yamaguch 	    (IXL_DMA_LEN(vsi) > I40E_AQ_LARGE_BUF ? IXL_AQ_LB : 0));
   3928  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_UPD_VSI_PARAMS);
   3929  1.1  yamaguch 	iaq.iaq_datalen = htole16(IXL_DMA_LEN(vsi));
   3930  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(vsi));
   3931  1.1  yamaguch 
   3932  1.1  yamaguch 	param = (struct ixl_aq_vsi_param *)iaq.iaq_param;
   3933  1.1  yamaguch 	param->uplink_seid = sc->sc_seid;
   3934  1.1  yamaguch 
   3935  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   3936  1.1  yamaguch 	    BUS_DMASYNC_PREWRITE);
   3937  1.1  yamaguch 
   3938  1.1  yamaguch 	rv = ixl_atq_poll(sc, &iaq, 250);
   3939  1.1  yamaguch 
   3940  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(vsi), 0, IXL_DMA_LEN(vsi),
   3941  1.1  yamaguch 	    BUS_DMASYNC_POSTWRITE);
   3942  1.1  yamaguch 
   3943  1.1  yamaguch 	if (rv != 0) {
   3944  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "UPDATE VSI timeout\n");
   3945  1.1  yamaguch 		return -1;
   3946  1.1  yamaguch 	}
   3947  1.1  yamaguch 
   3948  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   3949  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "UPDATE VSI error %u\n",
   3950  1.1  yamaguch 		    le16toh(iaq.iaq_retval));
   3951  1.1  yamaguch 		return -1;
   3952  1.1  yamaguch 	}
   3953  1.1  yamaguch 
   3954  1.1  yamaguch 	return 0;
   3955  1.1  yamaguch }
   3956  1.1  yamaguch 
   3957  1.1  yamaguch static void
   3958  1.1  yamaguch ixl_set_filter_control(struct ixl_softc *sc)
   3959  1.1  yamaguch {
   3960  1.1  yamaguch 	uint32_t reg;
   3961  1.1  yamaguch 
   3962  1.1  yamaguch 	reg = ixl_rd_rx_csr(sc, I40E_PFQF_CTL_0);
   3963  1.1  yamaguch 
   3964  1.1  yamaguch 	CLR(reg, I40E_PFQF_CTL_0_HASHLUTSIZE_MASK);
   3965  1.1  yamaguch 	SET(reg, I40E_HASH_LUT_SIZE_128 << I40E_PFQF_CTL_0_HASHLUTSIZE_SHIFT);
   3966  1.1  yamaguch 
   3967  1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_FD_ENA_MASK);
   3968  1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_ETYPE_ENA_MASK);
   3969  1.1  yamaguch 	SET(reg, I40E_PFQF_CTL_0_MACVLAN_ENA_MASK);
   3970  1.1  yamaguch 
   3971  1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_CTL_0, reg);
   3972  1.1  yamaguch }
   3973  1.1  yamaguch 
   3974  1.1  yamaguch static inline void
   3975  1.1  yamaguch ixl_get_default_rss_key(uint32_t *buf, size_t len)
   3976  1.1  yamaguch {
   3977  1.1  yamaguch 	size_t cplen;
   3978  1.1  yamaguch 	uint8_t rss_seed[RSS_KEYSIZE];
   3979  1.1  yamaguch 
   3980  1.1  yamaguch 	rss_getkey(rss_seed);
   3981  1.1  yamaguch 	memset(buf, 0, len);
   3982  1.1  yamaguch 
   3983  1.1  yamaguch 	cplen = MIN(len, sizeof(rss_seed));
   3984  1.1  yamaguch 	memcpy(buf, rss_seed, cplen);
   3985  1.1  yamaguch }
   3986  1.1  yamaguch 
   3987  1.1  yamaguch static void
   3988  1.1  yamaguch ixl_set_rss_key(struct ixl_softc *sc)
   3989  1.1  yamaguch {
   3990  1.1  yamaguch 	uint32_t rss_seed[IXL_RSS_KEY_SIZE_REG];
   3991  1.1  yamaguch 	size_t i;
   3992  1.1  yamaguch 
   3993  1.1  yamaguch 	ixl_get_default_rss_key(rss_seed, sizeof(rss_seed));
   3994  1.1  yamaguch 
   3995  1.1  yamaguch 	for (i = 0; i < IXL_RSS_KEY_SIZE_REG; i++) {
   3996  1.1  yamaguch 		ixl_wr_rx_csr(sc, I40E_PFQF_HKEY(i), rss_seed[i]);
   3997  1.1  yamaguch 	}
   3998  1.1  yamaguch }
   3999  1.1  yamaguch 
   4000  1.1  yamaguch static void
   4001  1.1  yamaguch ixl_set_rss_pctype(struct ixl_softc *sc)
   4002  1.1  yamaguch {
   4003  1.1  yamaguch 	uint64_t set_hena = 0;
   4004  1.1  yamaguch 	uint32_t hena0, hena1;
   4005  1.1  yamaguch 
   4006  1.1  yamaguch 	if (sc->sc_mac_type == I40E_MAC_X722)
   4007  1.1  yamaguch 		set_hena = IXL_RSS_HENA_DEFAULT_X722;
   4008  1.1  yamaguch 	else
   4009  1.1  yamaguch 		set_hena = IXL_RSS_HENA_DEFAULT_XL710;
   4010  1.1  yamaguch 
   4011  1.1  yamaguch 	hena0 = ixl_rd_rx_csr(sc, I40E_PFQF_HENA(0));
   4012  1.1  yamaguch 	hena1 = ixl_rd_rx_csr(sc, I40E_PFQF_HENA(1));
   4013  1.1  yamaguch 
   4014  1.1  yamaguch 	SET(hena0, set_hena);
   4015  1.1  yamaguch 	SET(hena1, set_hena >> 32);
   4016  1.1  yamaguch 
   4017  1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_HENA(0), hena0);
   4018  1.1  yamaguch 	ixl_wr_rx_csr(sc, I40E_PFQF_HENA(1), hena1);
   4019  1.1  yamaguch }
   4020  1.1  yamaguch 
   4021  1.1  yamaguch static void
   4022  1.1  yamaguch ixl_set_rss_hlut(struct ixl_softc *sc)
   4023  1.1  yamaguch {
   4024  1.1  yamaguch 	unsigned int qid;
   4025  1.1  yamaguch 	uint8_t hlut_buf[512], lut_mask;
   4026  1.1  yamaguch 	uint32_t *hluts;
   4027  1.1  yamaguch 	size_t i, hluts_num;
   4028  1.1  yamaguch 
   4029  1.1  yamaguch 	lut_mask = (0x01 << sc->sc_rss_table_entry_width) - 1;
   4030  1.1  yamaguch 
   4031  1.1  yamaguch 	for (i = 0; i < sc->sc_rss_table_size; i++) {
   4032  1.1  yamaguch 		qid = i % sc->sc_nqueue_pairs;
   4033  1.1  yamaguch 		hlut_buf[i] = qid & lut_mask;
   4034  1.1  yamaguch 	}
   4035  1.1  yamaguch 
   4036  1.1  yamaguch 	hluts = (uint32_t *)hlut_buf;
   4037  1.1  yamaguch 	hluts_num = sc->sc_rss_table_size >> 2;
   4038  1.1  yamaguch 	for (i = 0; i < hluts_num; i++) {
   4039  1.1  yamaguch 		ixl_wr(sc, I40E_PFQF_HLUT(i), hluts[i]);
   4040  1.1  yamaguch 	}
   4041  1.1  yamaguch 	ixl_flush(sc);
   4042  1.1  yamaguch }
   4043  1.1  yamaguch 
   4044  1.1  yamaguch static void
   4045  1.1  yamaguch ixl_config_rss(struct ixl_softc *sc)
   4046  1.1  yamaguch {
   4047  1.1  yamaguch 
   4048  1.1  yamaguch 	KASSERT(mutex_owned(&sc->sc_cfg_lock));
   4049  1.1  yamaguch 
   4050  1.1  yamaguch 	ixl_set_rss_key(sc);
   4051  1.1  yamaguch 	ixl_set_rss_pctype(sc);
   4052  1.1  yamaguch 	ixl_set_rss_hlut(sc);
   4053  1.1  yamaguch }
   4054  1.1  yamaguch 
   4055  1.1  yamaguch static const struct ixl_phy_type *
   4056  1.1  yamaguch ixl_search_phy_type(uint8_t phy_type)
   4057  1.1  yamaguch {
   4058  1.1  yamaguch 	const struct ixl_phy_type *itype;
   4059  1.1  yamaguch 	uint64_t mask;
   4060  1.1  yamaguch 	unsigned int i;
   4061  1.1  yamaguch 
   4062  1.1  yamaguch 	if (phy_type >= 64)
   4063  1.1  yamaguch 		return NULL;
   4064  1.1  yamaguch 
   4065  1.1  yamaguch 	mask = 1ULL << phy_type;
   4066  1.1  yamaguch 
   4067  1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_phy_type_map); i++) {
   4068  1.1  yamaguch 		itype = &ixl_phy_type_map[i];
   4069  1.1  yamaguch 
   4070  1.1  yamaguch 		if (ISSET(itype->phy_type, mask))
   4071  1.1  yamaguch 			return itype;
   4072  1.1  yamaguch 	}
   4073  1.1  yamaguch 
   4074  1.1  yamaguch 	return NULL;
   4075  1.1  yamaguch }
   4076  1.1  yamaguch 
   4077  1.1  yamaguch static uint64_t
   4078  1.1  yamaguch ixl_search_link_speed(uint8_t link_speed)
   4079  1.1  yamaguch {
   4080  1.1  yamaguch 	const struct ixl_speed_type *type;
   4081  1.1  yamaguch 	unsigned int i;
   4082  1.1  yamaguch 
   4083  1.1  yamaguch 	for (i = 0; i < __arraycount(ixl_speed_type_map); i++) {
   4084  1.1  yamaguch 		type = &ixl_speed_type_map[i];
   4085  1.1  yamaguch 
   4086  1.1  yamaguch 		if (ISSET(type->dev_speed, link_speed))
   4087  1.1  yamaguch 			return type->net_speed;
   4088  1.1  yamaguch 	}
   4089  1.1  yamaguch 
   4090  1.1  yamaguch 	return 0;
   4091  1.1  yamaguch }
   4092  1.1  yamaguch 
   4093  1.1  yamaguch static int
   4094  1.1  yamaguch ixl_restart_an(struct ixl_softc *sc)
   4095  1.1  yamaguch {
   4096  1.1  yamaguch 	struct ixl_aq_desc iaq;
   4097  1.1  yamaguch 
   4098  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4099  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_PHY_RESTART_AN);
   4100  1.1  yamaguch 	iaq.iaq_param[0] =
   4101  1.1  yamaguch 	    htole32(IXL_AQ_PHY_RESTART_AN | IXL_AQ_PHY_LINK_ENABLE);
   4102  1.1  yamaguch 
   4103  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4104  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "RESTART AN timeout\n");
   4105  1.1  yamaguch 		return -1;
   4106  1.1  yamaguch 	}
   4107  1.1  yamaguch 	if (iaq.iaq_retval != htole16(IXL_AQ_RC_OK)) {
   4108  1.1  yamaguch 		aprint_error_dev(sc->sc_dev, "RESTART AN error\n");
   4109  1.1  yamaguch 		return -1;
   4110  1.1  yamaguch 	}
   4111  1.1  yamaguch 
   4112  1.1  yamaguch 	return 0;
   4113  1.1  yamaguch }
   4114  1.1  yamaguch 
   4115  1.1  yamaguch static int
   4116  1.1  yamaguch ixl_add_macvlan(struct ixl_softc *sc, const uint8_t *macaddr,
   4117  1.1  yamaguch     uint16_t vlan, uint16_t flags)
   4118  1.1  yamaguch {
   4119  1.1  yamaguch 	struct ixl_aq_desc iaq;
   4120  1.1  yamaguch 	struct ixl_aq_add_macvlan *param;
   4121  1.1  yamaguch 	struct ixl_aq_add_macvlan_elem *elem;
   4122  1.1  yamaguch 
   4123  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4124  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
   4125  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_ADD_MACVLAN);
   4126  1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*elem));
   4127  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&sc->sc_scratch));
   4128  1.1  yamaguch 
   4129  1.1  yamaguch 	param = (struct ixl_aq_add_macvlan *)&iaq.iaq_param;
   4130  1.1  yamaguch 	param->num_addrs = htole16(1);
   4131  1.1  yamaguch 	param->seid0 = htole16(0x8000) | sc->sc_seid;
   4132  1.1  yamaguch 	param->seid1 = 0;
   4133  1.1  yamaguch 	param->seid2 = 0;
   4134  1.1  yamaguch 
   4135  1.1  yamaguch 	elem = IXL_DMA_KVA(&sc->sc_scratch);
   4136  1.1  yamaguch 	memset(elem, 0, sizeof(*elem));
   4137  1.1  yamaguch 	memcpy(elem->macaddr, macaddr, ETHER_ADDR_LEN);
   4138  1.1  yamaguch 	elem->flags = htole16(IXL_AQ_OP_ADD_MACVLAN_PERFECT_MATCH | flags);
   4139  1.1  yamaguch 	elem->vlan = htole16(vlan);
   4140  1.1  yamaguch 
   4141  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4142  1.1  yamaguch 		return IXL_AQ_RC_EINVAL;
   4143  1.1  yamaguch 	}
   4144  1.1  yamaguch 
   4145  1.1  yamaguch 	return le16toh(iaq.iaq_retval);
   4146  1.1  yamaguch }
   4147  1.1  yamaguch 
   4148  1.1  yamaguch static int
   4149  1.1  yamaguch ixl_remove_macvlan(struct ixl_softc *sc, uint8_t *macaddr,
   4150  1.1  yamaguch     uint16_t vlan, uint16_t flags)
   4151  1.1  yamaguch {
   4152  1.1  yamaguch 	struct ixl_aq_desc iaq;
   4153  1.1  yamaguch 	struct ixl_aq_remove_macvlan *param;
   4154  1.1  yamaguch 	struct ixl_aq_remove_macvlan_elem *elem;
   4155  1.1  yamaguch 
   4156  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   4157  1.1  yamaguch 	iaq.iaq_flags = htole16(IXL_AQ_BUF | IXL_AQ_RD);
   4158  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_REMOVE_MACVLAN);
   4159  1.1  yamaguch 	iaq.iaq_datalen = htole16(sizeof(*elem));
   4160  1.1  yamaguch 	ixl_aq_dva(&iaq, IXL_DMA_DVA(&sc->sc_scratch));
   4161  1.1  yamaguch 
   4162  1.1  yamaguch 	param = (struct ixl_aq_remove_macvlan *)&iaq.iaq_param;
   4163  1.1  yamaguch 	param->num_addrs = htole16(1);
   4164  1.1  yamaguch 	param->seid0 = htole16(0x8000) | sc->sc_seid;
   4165  1.1  yamaguch 	param->seid1 = 0;
   4166  1.1  yamaguch 	param->seid2 = 0;
   4167  1.1  yamaguch 
   4168  1.1  yamaguch 	elem = IXL_DMA_KVA(&sc->sc_scratch);
   4169  1.1  yamaguch 	memset(elem, 0, sizeof(*elem));
   4170  1.1  yamaguch 	memcpy(elem->macaddr, macaddr, ETHER_ADDR_LEN);
   4171  1.1  yamaguch 	elem->flags = htole16(IXL_AQ_OP_REMOVE_MACVLAN_PERFECT_MATCH | flags);
   4172  1.1  yamaguch 	elem->vlan = htole16(vlan);
   4173  1.1  yamaguch 
   4174  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0) {
   4175  1.1  yamaguch 		return IXL_AQ_RC_EINVAL;
   4176  1.1  yamaguch 	}
   4177  1.1  yamaguch 
   4178  1.1  yamaguch 	return le16toh(iaq.iaq_retval);
   4179  1.1  yamaguch }
   4180  1.1  yamaguch 
   4181  1.1  yamaguch static int
   4182  1.1  yamaguch ixl_hmc(struct ixl_softc *sc)
   4183  1.1  yamaguch {
   4184  1.1  yamaguch 	struct {
   4185  1.1  yamaguch 		uint32_t   count;
   4186  1.1  yamaguch 		uint32_t   minsize;
   4187  1.1  yamaguch 		bus_size_t objsiz;
   4188  1.1  yamaguch 		bus_size_t setoff;
   4189  1.1  yamaguch 		bus_size_t setcnt;
   4190  1.1  yamaguch 	} regs[] = {
   4191  1.1  yamaguch 		{
   4192  1.1  yamaguch 			0,
   4193  1.1  yamaguch 			IXL_HMC_TXQ_MINSIZE,
   4194  1.1  yamaguch 			I40E_GLHMC_LANTXOBJSZ,
   4195  1.1  yamaguch 			I40E_GLHMC_LANTXBASE(sc->sc_pf_id),
   4196  1.1  yamaguch 			I40E_GLHMC_LANTXCNT(sc->sc_pf_id),
   4197  1.1  yamaguch 		},
   4198  1.1  yamaguch 		{
   4199  1.1  yamaguch 			0,
   4200  1.1  yamaguch 			IXL_HMC_RXQ_MINSIZE,
   4201  1.1  yamaguch 			I40E_GLHMC_LANRXOBJSZ,
   4202  1.1  yamaguch 			I40E_GLHMC_LANRXBASE(sc->sc_pf_id),
   4203  1.1  yamaguch 			I40E_GLHMC_LANRXCNT(sc->sc_pf_id),
   4204  1.1  yamaguch 		},
   4205  1.1  yamaguch 		{
   4206  1.1  yamaguch 			0,
   4207  1.1  yamaguch 			0,
   4208  1.1  yamaguch 			I40E_GLHMC_FCOEDDPOBJSZ,
   4209  1.1  yamaguch 			I40E_GLHMC_FCOEDDPBASE(sc->sc_pf_id),
   4210  1.1  yamaguch 			I40E_GLHMC_FCOEDDPCNT(sc->sc_pf_id),
   4211  1.1  yamaguch 		},
   4212  1.1  yamaguch 		{
   4213  1.1  yamaguch 			0,
   4214  1.1  yamaguch 			0,
   4215  1.1  yamaguch 			I40E_GLHMC_FCOEFOBJSZ,
   4216  1.1  yamaguch 			I40E_GLHMC_FCOEFBASE(sc->sc_pf_id),
   4217  1.1  yamaguch 			I40E_GLHMC_FCOEFCNT(sc->sc_pf_id),
   4218  1.1  yamaguch 		},
   4219  1.1  yamaguch 	};
   4220  1.1  yamaguch 	struct ixl_hmc_entry *e;
   4221  1.1  yamaguch 	uint64_t size, dva;
   4222  1.1  yamaguch 	uint8_t *kva;
   4223  1.1  yamaguch 	uint64_t *sdpage;
   4224  1.1  yamaguch 	unsigned int i;
   4225  1.1  yamaguch 	int npages, tables;
   4226  1.1  yamaguch 	uint32_t reg;
   4227  1.1  yamaguch 
   4228  1.1  yamaguch 	CTASSERT(__arraycount(regs) <= __arraycount(sc->sc_hmc_entries));
   4229  1.1  yamaguch 
   4230  1.1  yamaguch 	regs[IXL_HMC_LAN_TX].count = regs[IXL_HMC_LAN_RX].count =
   4231  1.1  yamaguch 	    ixl_rd(sc, I40E_GLHMC_LANQMAX);
   4232  1.1  yamaguch 
   4233  1.1  yamaguch 	size = 0;
   4234  1.1  yamaguch 	for (i = 0; i < __arraycount(regs); i++) {
   4235  1.1  yamaguch 		e = &sc->sc_hmc_entries[i];
   4236  1.1  yamaguch 
   4237  1.1  yamaguch 		e->hmc_count = regs[i].count;
   4238  1.1  yamaguch 		reg = ixl_rd(sc, regs[i].objsiz);
   4239  1.1  yamaguch 		e->hmc_size = BIT_ULL(0x3F & reg);
   4240  1.1  yamaguch 		e->hmc_base = size;
   4241  1.1  yamaguch 
   4242  1.1  yamaguch 		if ((e->hmc_size * 8) < regs[i].minsize) {
   4243  1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   4244  1.1  yamaguch 			    "kernel hmc entry is too big\n");
   4245  1.1  yamaguch 			return -1;
   4246  1.1  yamaguch 		}
   4247  1.1  yamaguch 
   4248  1.1  yamaguch 		size += roundup(e->hmc_size * e->hmc_count, IXL_HMC_ROUNDUP);
   4249  1.1  yamaguch 	}
   4250  1.1  yamaguch 	size = roundup(size, IXL_HMC_PGSIZE);
   4251  1.1  yamaguch 	npages = size / IXL_HMC_PGSIZE;
   4252  1.1  yamaguch 
   4253  1.1  yamaguch 	tables = roundup(size, IXL_HMC_L2SZ) / IXL_HMC_L2SZ;
   4254  1.1  yamaguch 
   4255  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_hmc_pd, size, IXL_HMC_PGSIZE) != 0) {
   4256  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4257  1.1  yamaguch 		    "unable to allocate hmc pd memory\n");
   4258  1.1  yamaguch 		return -1;
   4259  1.1  yamaguch 	}
   4260  1.1  yamaguch 
   4261  1.1  yamaguch 	if (ixl_dmamem_alloc(sc, &sc->sc_hmc_sd, tables * IXL_HMC_PGSIZE,
   4262  1.1  yamaguch 	    IXL_HMC_PGSIZE) != 0) {
   4263  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4264  1.1  yamaguch 		    "unable to allocate hmc sd memory\n");
   4265  1.1  yamaguch 		ixl_dmamem_free(sc, &sc->sc_hmc_pd);
   4266  1.1  yamaguch 		return -1;
   4267  1.1  yamaguch 	}
   4268  1.1  yamaguch 
   4269  1.1  yamaguch 	kva = IXL_DMA_KVA(&sc->sc_hmc_pd);
   4270  1.1  yamaguch 	memset(kva, 0, IXL_DMA_LEN(&sc->sc_hmc_pd));
   4271  1.1  yamaguch 
   4272  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_pd),
   4273  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_pd),
   4274  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   4275  1.1  yamaguch 
   4276  1.1  yamaguch 	dva = IXL_DMA_DVA(&sc->sc_hmc_pd);
   4277  1.1  yamaguch 	sdpage = IXL_DMA_KVA(&sc->sc_hmc_sd);
   4278  1.1  yamaguch 	memset(sdpage, 0, IXL_DMA_LEN(&sc->sc_hmc_sd));
   4279  1.1  yamaguch 
   4280  1.1  yamaguch 	for (i = 0; (int)i < npages; i++) {
   4281  1.1  yamaguch 		*sdpage = htole64(dva | IXL_HMC_PDVALID);
   4282  1.1  yamaguch 		sdpage++;
   4283  1.1  yamaguch 
   4284  1.1  yamaguch 		dva += IXL_HMC_PGSIZE;
   4285  1.1  yamaguch 	}
   4286  1.1  yamaguch 
   4287  1.1  yamaguch 	bus_dmamap_sync(sc->sc_dmat, IXL_DMA_MAP(&sc->sc_hmc_sd),
   4288  1.1  yamaguch 	    0, IXL_DMA_LEN(&sc->sc_hmc_sd),
   4289  1.1  yamaguch 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   4290  1.1  yamaguch 
   4291  1.1  yamaguch 	dva = IXL_DMA_DVA(&sc->sc_hmc_sd);
   4292  1.1  yamaguch 	for (i = 0; (int)i < tables; i++) {
   4293  1.1  yamaguch 		uint32_t count;
   4294  1.1  yamaguch 
   4295  1.1  yamaguch 		KASSERT(npages >= 0);
   4296  1.1  yamaguch 
   4297  1.1  yamaguch 		count = ((unsigned int)npages > IXL_HMC_PGS) ?
   4298  1.1  yamaguch 		    IXL_HMC_PGS : (unsigned int)npages;
   4299  1.1  yamaguch 
   4300  1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDDATAHIGH, dva >> 32);
   4301  1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDDATALOW, dva |
   4302  1.1  yamaguch 		    (count << I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |
   4303  1.1  yamaguch 		    (1U << I40E_PFHMC_SDDATALOW_PMSDVALID_SHIFT));
   4304  1.1  yamaguch 		ixl_barrier(sc, 0, sc->sc_mems, BUS_SPACE_BARRIER_WRITE);
   4305  1.1  yamaguch 		ixl_wr(sc, I40E_PFHMC_SDCMD,
   4306  1.1  yamaguch 		    (1U << I40E_PFHMC_SDCMD_PMSDWR_SHIFT) | i);
   4307  1.1  yamaguch 
   4308  1.1  yamaguch 		npages -= IXL_HMC_PGS;
   4309  1.1  yamaguch 		dva += IXL_HMC_PGSIZE;
   4310  1.1  yamaguch 	}
   4311  1.1  yamaguch 
   4312  1.1  yamaguch 	for (i = 0; i < __arraycount(regs); i++) {
   4313  1.1  yamaguch 		e = &sc->sc_hmc_entries[i];
   4314  1.1  yamaguch 
   4315  1.1  yamaguch 		ixl_wr(sc, regs[i].setoff, e->hmc_base / IXL_HMC_ROUNDUP);
   4316  1.1  yamaguch 		ixl_wr(sc, regs[i].setcnt, e->hmc_count);
   4317  1.1  yamaguch 	}
   4318  1.1  yamaguch 
   4319  1.1  yamaguch 	return 0;
   4320  1.1  yamaguch }
   4321  1.1  yamaguch 
   4322  1.1  yamaguch static void
   4323  1.1  yamaguch ixl_hmc_free(struct ixl_softc *sc)
   4324  1.1  yamaguch {
   4325  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_hmc_sd);
   4326  1.1  yamaguch 	ixl_dmamem_free(sc, &sc->sc_hmc_pd);
   4327  1.1  yamaguch }
   4328  1.1  yamaguch 
   4329  1.1  yamaguch static void
   4330  1.1  yamaguch ixl_hmc_pack(void *d, const void *s, const struct ixl_hmc_pack *packing,
   4331  1.1  yamaguch     unsigned int npacking)
   4332  1.1  yamaguch {
   4333  1.1  yamaguch 	uint8_t *dst = d;
   4334  1.1  yamaguch 	const uint8_t *src = s;
   4335  1.1  yamaguch 	unsigned int i;
   4336  1.1  yamaguch 
   4337  1.1  yamaguch 	for (i = 0; i < npacking; i++) {
   4338  1.1  yamaguch 		const struct ixl_hmc_pack *pack = &packing[i];
   4339  1.1  yamaguch 		unsigned int offset = pack->lsb / 8;
   4340  1.1  yamaguch 		unsigned int align = pack->lsb % 8;
   4341  1.1  yamaguch 		const uint8_t *in = src + pack->offset;
   4342  1.1  yamaguch 		uint8_t *out = dst + offset;
   4343  1.1  yamaguch 		int width = pack->width;
   4344  1.1  yamaguch 		unsigned int inbits = 0;
   4345  1.1  yamaguch 
   4346  1.1  yamaguch 		if (align) {
   4347  1.1  yamaguch 			inbits = (*in++) << align;
   4348  1.1  yamaguch 			*out++ |= (inbits & 0xff);
   4349  1.1  yamaguch 			inbits >>= 8;
   4350  1.1  yamaguch 
   4351  1.1  yamaguch 			width -= 8 - align;
   4352  1.1  yamaguch 		}
   4353  1.1  yamaguch 
   4354  1.1  yamaguch 		while (width >= 8) {
   4355  1.1  yamaguch 			inbits |= (*in++) << align;
   4356  1.1  yamaguch 			*out++ = (inbits & 0xff);
   4357  1.1  yamaguch 			inbits >>= 8;
   4358  1.1  yamaguch 
   4359  1.1  yamaguch 			width -= 8;
   4360  1.1  yamaguch 		}
   4361  1.1  yamaguch 
   4362  1.1  yamaguch 		if (width > 0) {
   4363  1.1  yamaguch 			inbits |= (*in) << align;
   4364  1.1  yamaguch 			*out |= (inbits & ((1 << width) - 1));
   4365  1.1  yamaguch 		}
   4366  1.1  yamaguch 	}
   4367  1.1  yamaguch }
   4368  1.1  yamaguch 
   4369  1.1  yamaguch static struct ixl_aq_buf *
   4370  1.1  yamaguch ixl_aqb_alloc(struct ixl_softc *sc)
   4371  1.1  yamaguch {
   4372  1.1  yamaguch 	struct ixl_aq_buf *aqb;
   4373  1.1  yamaguch 
   4374  1.1  yamaguch 	aqb = malloc(sizeof(*aqb), M_DEVBUF, M_WAITOK);
   4375  1.1  yamaguch 	if (aqb == NULL)
   4376  1.1  yamaguch 		return NULL;
   4377  1.1  yamaguch 
   4378  1.1  yamaguch 	aqb->aqb_size = IXL_AQ_BUFLEN;
   4379  1.1  yamaguch 
   4380  1.1  yamaguch 	if (bus_dmamap_create(sc->sc_dmat, aqb->aqb_size, 1,
   4381  1.1  yamaguch 	    aqb->aqb_size, 0,
   4382  1.1  yamaguch 	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &aqb->aqb_map) != 0)
   4383  1.1  yamaguch 		goto free;
   4384  1.1  yamaguch 	if (bus_dmamem_alloc(sc->sc_dmat, aqb->aqb_size,
   4385  1.1  yamaguch 	    IXL_AQ_ALIGN, 0, &aqb->aqb_seg, 1, &aqb->aqb_nsegs,
   4386  1.1  yamaguch 	    BUS_DMA_WAITOK) != 0)
   4387  1.1  yamaguch 		goto destroy;
   4388  1.1  yamaguch 	if (bus_dmamem_map(sc->sc_dmat, &aqb->aqb_seg, aqb->aqb_nsegs,
   4389  1.1  yamaguch 	    aqb->aqb_size, &aqb->aqb_data, BUS_DMA_WAITOK) != 0)
   4390  1.1  yamaguch 		goto dma_free;
   4391  1.1  yamaguch 	if (bus_dmamap_load(sc->sc_dmat, aqb->aqb_map, aqb->aqb_data,
   4392  1.1  yamaguch 	    aqb->aqb_size, NULL, BUS_DMA_WAITOK) != 0)
   4393  1.1  yamaguch 		goto unmap;
   4394  1.1  yamaguch 
   4395  1.1  yamaguch 	return aqb;
   4396  1.1  yamaguch unmap:
   4397  1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, aqb->aqb_data, aqb->aqb_size);
   4398  1.1  yamaguch dma_free:
   4399  1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &aqb->aqb_seg, 1);
   4400  1.1  yamaguch destroy:
   4401  1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, aqb->aqb_map);
   4402  1.1  yamaguch free:
   4403  1.1  yamaguch 	free(aqb, M_DEVBUF);
   4404  1.1  yamaguch 
   4405  1.1  yamaguch 	return NULL;
   4406  1.1  yamaguch }
   4407  1.1  yamaguch 
   4408  1.1  yamaguch static void
   4409  1.1  yamaguch ixl_aqb_free(struct ixl_softc *sc, struct ixl_aq_buf *aqb)
   4410  1.1  yamaguch {
   4411  1.1  yamaguch 	bus_dmamap_unload(sc->sc_dmat, aqb->aqb_map);
   4412  1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, aqb->aqb_data, aqb->aqb_size);
   4413  1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &aqb->aqb_seg, 1);
   4414  1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, aqb->aqb_map);
   4415  1.1  yamaguch 	free(aqb, M_DEVBUF);
   4416  1.1  yamaguch }
   4417  1.1  yamaguch 
   4418  1.1  yamaguch static int
   4419  1.1  yamaguch ixl_arq_fill(struct ixl_softc *sc)
   4420  1.1  yamaguch {
   4421  1.1  yamaguch 	struct ixl_aq_buf *aqb;
   4422  1.1  yamaguch 	struct ixl_aq_desc *arq, *iaq;
   4423  1.1  yamaguch 	unsigned int prod = sc->sc_arq_prod;
   4424  1.1  yamaguch 	unsigned int n;
   4425  1.1  yamaguch 	int post = 0;
   4426  1.1  yamaguch 
   4427  1.1  yamaguch 	n = ixl_rxr_unrefreshed(sc->sc_arq_prod, sc->sc_arq_cons,
   4428  1.1  yamaguch 	    IXL_AQ_NUM);
   4429  1.1  yamaguch 	arq = IXL_DMA_KVA(&sc->sc_arq);
   4430  1.1  yamaguch 
   4431  1.1  yamaguch 	if (__predict_false(n <= 0))
   4432  1.1  yamaguch 		return 0;
   4433  1.1  yamaguch 
   4434  1.1  yamaguch 	do {
   4435  1.1  yamaguch 		aqb = sc->sc_arq_live[prod];
   4436  1.1  yamaguch 		iaq = &arq[prod];
   4437  1.1  yamaguch 
   4438  1.1  yamaguch 		if (aqb == NULL) {
   4439  1.1  yamaguch 			aqb = SIMPLEQ_FIRST(&sc->sc_arq_idle);
   4440  1.1  yamaguch 			if (aqb != NULL) {
   4441  1.1  yamaguch 				SIMPLEQ_REMOVE(&sc->sc_arq_idle, aqb,
   4442  1.1  yamaguch 				    ixl_aq_buf, aqb_entry);
   4443  1.1  yamaguch 			} else if ((aqb = ixl_aqb_alloc(sc)) == NULL) {
   4444  1.1  yamaguch 				break;
   4445  1.1  yamaguch 			}
   4446  1.1  yamaguch 
   4447  1.1  yamaguch 			sc->sc_arq_live[prod] = aqb;
   4448  1.1  yamaguch 			memset(aqb->aqb_data, 0, aqb->aqb_size);
   4449  1.1  yamaguch 
   4450  1.1  yamaguch 			bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0,
   4451  1.1  yamaguch 			    aqb->aqb_size, BUS_DMASYNC_PREREAD);
   4452  1.1  yamaguch 
   4453  1.1  yamaguch 			iaq->iaq_flags = htole16(IXL_AQ_BUF |
   4454  1.1  yamaguch 			    (IXL_AQ_BUFLEN > I40E_AQ_LARGE_BUF ?
   4455  1.1  yamaguch 			    IXL_AQ_LB : 0));
   4456  1.1  yamaguch 			iaq->iaq_opcode = 0;
   4457  1.1  yamaguch 			iaq->iaq_datalen = htole16(aqb->aqb_size);
   4458  1.1  yamaguch 			iaq->iaq_retval = 0;
   4459  1.1  yamaguch 			iaq->iaq_cookie = 0;
   4460  1.1  yamaguch 			iaq->iaq_param[0] = 0;
   4461  1.1  yamaguch 			iaq->iaq_param[1] = 0;
   4462  1.1  yamaguch 			ixl_aq_dva(iaq, aqb->aqb_map->dm_segs[0].ds_addr);
   4463  1.1  yamaguch 		}
   4464  1.1  yamaguch 
   4465  1.1  yamaguch 		prod++;
   4466  1.1  yamaguch 		prod &= IXL_AQ_MASK;
   4467  1.1  yamaguch 
   4468  1.1  yamaguch 		post = 1;
   4469  1.1  yamaguch 
   4470  1.1  yamaguch 	} while (--n);
   4471  1.1  yamaguch 
   4472  1.1  yamaguch 	if (post) {
   4473  1.1  yamaguch 		sc->sc_arq_prod = prod;
   4474  1.1  yamaguch 		ixl_wr(sc, sc->sc_aq_regs->arq_tail, sc->sc_arq_prod);
   4475  1.1  yamaguch 	}
   4476  1.1  yamaguch 
   4477  1.1  yamaguch 	return post;
   4478  1.1  yamaguch }
   4479  1.1  yamaguch 
   4480  1.1  yamaguch static void
   4481  1.1  yamaguch ixl_arq_unfill(struct ixl_softc *sc)
   4482  1.1  yamaguch {
   4483  1.1  yamaguch 	struct ixl_aq_buf *aqb;
   4484  1.1  yamaguch 	unsigned int i;
   4485  1.1  yamaguch 
   4486  1.1  yamaguch 	for (i = 0; i < __arraycount(sc->sc_arq_live); i++) {
   4487  1.1  yamaguch 		aqb = sc->sc_arq_live[i];
   4488  1.1  yamaguch 		if (aqb == NULL)
   4489  1.1  yamaguch 			continue;
   4490  1.1  yamaguch 
   4491  1.1  yamaguch 		sc->sc_arq_live[i] = NULL;
   4492  1.1  yamaguch 		bus_dmamap_sync(sc->sc_dmat, aqb->aqb_map, 0, aqb->aqb_size,
   4493  1.1  yamaguch 		    BUS_DMASYNC_POSTREAD);
   4494  1.1  yamaguch 		ixl_aqb_free(sc, aqb);
   4495  1.1  yamaguch 	}
   4496  1.1  yamaguch 
   4497  1.1  yamaguch 	while ((aqb = SIMPLEQ_FIRST(&sc->sc_arq_idle)) != NULL) {
   4498  1.1  yamaguch 		SIMPLEQ_REMOVE(&sc->sc_arq_idle, aqb,
   4499  1.1  yamaguch 		    ixl_aq_buf, aqb_entry);
   4500  1.1  yamaguch 		ixl_aqb_free(sc, aqb);
   4501  1.1  yamaguch 	}
   4502  1.1  yamaguch }
   4503  1.1  yamaguch 
   4504  1.1  yamaguch static void
   4505  1.1  yamaguch ixl_clear_hw(struct ixl_softc *sc)
   4506  1.1  yamaguch {
   4507  1.1  yamaguch 	uint32_t num_queues, base_queue;
   4508  1.1  yamaguch 	uint32_t num_pf_int;
   4509  1.1  yamaguch 	uint32_t num_vf_int;
   4510  1.1  yamaguch 	uint32_t num_vfs;
   4511  1.1  yamaguch 	uint32_t i, j;
   4512  1.1  yamaguch 	uint32_t val;
   4513  1.1  yamaguch 	uint32_t eol = 0x7ff;
   4514  1.1  yamaguch 
   4515  1.1  yamaguch 	/* get number of interrupts, queues, and vfs */
   4516  1.1  yamaguch 	val = ixl_rd(sc, I40E_GLPCI_CNF2);
   4517  1.1  yamaguch 	num_pf_int = (val & I40E_GLPCI_CNF2_MSI_X_PF_N_MASK) >>
   4518  1.1  yamaguch 	    I40E_GLPCI_CNF2_MSI_X_PF_N_SHIFT;
   4519  1.1  yamaguch 	num_vf_int = (val & I40E_GLPCI_CNF2_MSI_X_VF_N_MASK) >>
   4520  1.1  yamaguch 	    I40E_GLPCI_CNF2_MSI_X_VF_N_SHIFT;
   4521  1.1  yamaguch 
   4522  1.1  yamaguch 	val = ixl_rd(sc, I40E_PFLAN_QALLOC);
   4523  1.1  yamaguch 	base_queue = (val & I40E_PFLAN_QALLOC_FIRSTQ_MASK) >>
   4524  1.1  yamaguch 	    I40E_PFLAN_QALLOC_FIRSTQ_SHIFT;
   4525  1.1  yamaguch 	j = (val & I40E_PFLAN_QALLOC_LASTQ_MASK) >>
   4526  1.1  yamaguch 	    I40E_PFLAN_QALLOC_LASTQ_SHIFT;
   4527  1.1  yamaguch 	if (val & I40E_PFLAN_QALLOC_VALID_MASK)
   4528  1.1  yamaguch 		num_queues = (j - base_queue) + 1;
   4529  1.1  yamaguch 	else
   4530  1.1  yamaguch 		num_queues = 0;
   4531  1.1  yamaguch 
   4532  1.1  yamaguch 	val = ixl_rd(sc, I40E_PF_VT_PFALLOC);
   4533  1.1  yamaguch 	i = (val & I40E_PF_VT_PFALLOC_FIRSTVF_MASK) >>
   4534  1.1  yamaguch 	    I40E_PF_VT_PFALLOC_FIRSTVF_SHIFT;
   4535  1.1  yamaguch 	j = (val & I40E_PF_VT_PFALLOC_LASTVF_MASK) >>
   4536  1.1  yamaguch 	    I40E_PF_VT_PFALLOC_LASTVF_SHIFT;
   4537  1.1  yamaguch 	if (val & I40E_PF_VT_PFALLOC_VALID_MASK)
   4538  1.1  yamaguch 		num_vfs = (j - i) + 1;
   4539  1.1  yamaguch 	else
   4540  1.1  yamaguch 		num_vfs = 0;
   4541  1.1  yamaguch 
   4542  1.1  yamaguch 	/* stop all the interrupts */
   4543  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, 0);
   4544  1.1  yamaguch 	ixl_flush(sc);
   4545  1.1  yamaguch 	val = 0x3 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
   4546  1.1  yamaguch 	for (i = 0; i < num_pf_int - 2; i++)
   4547  1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_DYN_CTLN(i), val);
   4548  1.1  yamaguch 	ixl_flush(sc);
   4549  1.1  yamaguch 
   4550  1.1  yamaguch 	/* Set the FIRSTQ_INDX field to 0x7FF in PFINT_LNKLSTx */
   4551  1.1  yamaguch 	val = eol << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
   4552  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_LNKLST0, val);
   4553  1.1  yamaguch 	for (i = 0; i < num_pf_int - 2; i++)
   4554  1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLSTN(i), val);
   4555  1.1  yamaguch 	val = eol << I40E_VPINT_LNKLST0_FIRSTQ_INDX_SHIFT;
   4556  1.1  yamaguch 	for (i = 0; i < num_vfs; i++)
   4557  1.1  yamaguch 		ixl_wr(sc, I40E_VPINT_LNKLST0(i), val);
   4558  1.1  yamaguch 	for (i = 0; i < num_vf_int - 2; i++)
   4559  1.1  yamaguch 		ixl_wr(sc, I40E_VPINT_LNKLSTN(i), val);
   4560  1.1  yamaguch 
   4561  1.1  yamaguch 	/* warn the HW of the coming Tx disables */
   4562  1.1  yamaguch 	for (i = 0; i < num_queues; i++) {
   4563  1.1  yamaguch 		uint32_t abs_queue_idx = base_queue + i;
   4564  1.1  yamaguch 		uint32_t reg_block = 0;
   4565  1.1  yamaguch 
   4566  1.1  yamaguch 		if (abs_queue_idx >= 128) {
   4567  1.1  yamaguch 			reg_block = abs_queue_idx / 128;
   4568  1.1  yamaguch 			abs_queue_idx %= 128;
   4569  1.1  yamaguch 		}
   4570  1.1  yamaguch 
   4571  1.1  yamaguch 		val = ixl_rd(sc, I40E_GLLAN_TXPRE_QDIS(reg_block));
   4572  1.1  yamaguch 		val &= ~I40E_GLLAN_TXPRE_QDIS_QINDX_MASK;
   4573  1.1  yamaguch 		val |= (abs_queue_idx << I40E_GLLAN_TXPRE_QDIS_QINDX_SHIFT);
   4574  1.1  yamaguch 		val |= I40E_GLLAN_TXPRE_QDIS_SET_QDIS_MASK;
   4575  1.1  yamaguch 
   4576  1.1  yamaguch 		ixl_wr(sc, I40E_GLLAN_TXPRE_QDIS(reg_block), val);
   4577  1.1  yamaguch 	}
   4578  1.1  yamaguch 	delaymsec(400);
   4579  1.1  yamaguch 
   4580  1.1  yamaguch 	/* stop all the queues */
   4581  1.1  yamaguch 	for (i = 0; i < num_queues; i++) {
   4582  1.1  yamaguch 		ixl_wr(sc, I40E_QINT_TQCTL(i), 0);
   4583  1.1  yamaguch 		ixl_wr(sc, I40E_QTX_ENA(i), 0);
   4584  1.1  yamaguch 		ixl_wr(sc, I40E_QINT_RQCTL(i), 0);
   4585  1.1  yamaguch 		ixl_wr(sc, I40E_QRX_ENA(i), 0);
   4586  1.1  yamaguch 	}
   4587  1.1  yamaguch 
   4588  1.1  yamaguch 	/* short wait for all queue disables to settle */
   4589  1.1  yamaguch 	delaymsec(50);
   4590  1.1  yamaguch }
   4591  1.1  yamaguch 
   4592  1.1  yamaguch static int
   4593  1.1  yamaguch ixl_pf_reset(struct ixl_softc *sc)
   4594  1.1  yamaguch {
   4595  1.1  yamaguch 	uint32_t cnt = 0;
   4596  1.1  yamaguch 	uint32_t cnt1 = 0;
   4597  1.1  yamaguch 	uint32_t reg = 0, reg0 = 0;
   4598  1.1  yamaguch 	uint32_t grst_del;
   4599  1.1  yamaguch 
   4600  1.1  yamaguch 	/*
   4601  1.1  yamaguch 	 * Poll for Global Reset steady state in case of recent GRST.
   4602  1.1  yamaguch 	 * The grst delay value is in 100ms units, and we'll wait a
   4603  1.1  yamaguch 	 * couple counts longer to be sure we don't just miss the end.
   4604  1.1  yamaguch 	 */
   4605  1.1  yamaguch 	grst_del = ixl_rd(sc, I40E_GLGEN_RSTCTL);
   4606  1.1  yamaguch 	grst_del &= I40E_GLGEN_RSTCTL_GRSTDEL_MASK;
   4607  1.1  yamaguch 	grst_del >>= I40E_GLGEN_RSTCTL_GRSTDEL_SHIFT;
   4608  1.1  yamaguch 
   4609  1.1  yamaguch 	grst_del = grst_del * 20;
   4610  1.1  yamaguch 
   4611  1.1  yamaguch 	for (cnt = 0; cnt < grst_del; cnt++) {
   4612  1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLGEN_RSTAT);
   4613  1.1  yamaguch 		if (!(reg & I40E_GLGEN_RSTAT_DEVSTATE_MASK))
   4614  1.1  yamaguch 			break;
   4615  1.1  yamaguch 		delaymsec(100);
   4616  1.1  yamaguch 	}
   4617  1.1  yamaguch 	if (reg & I40E_GLGEN_RSTAT_DEVSTATE_MASK) {
   4618  1.1  yamaguch 		aprint_error(", Global reset polling failed to complete\n");
   4619  1.1  yamaguch 		return -1;
   4620  1.1  yamaguch 	}
   4621  1.1  yamaguch 
   4622  1.1  yamaguch 	/* Now Wait for the FW to be ready */
   4623  1.1  yamaguch 	for (cnt1 = 0; cnt1 < I40E_PF_RESET_WAIT_COUNT; cnt1++) {
   4624  1.1  yamaguch 		reg = ixl_rd(sc, I40E_GLNVM_ULD);
   4625  1.1  yamaguch 		reg &= (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   4626  1.1  yamaguch 		    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK);
   4627  1.1  yamaguch 		if (reg == (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   4628  1.1  yamaguch 		    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK))
   4629  1.1  yamaguch 			break;
   4630  1.1  yamaguch 
   4631  1.1  yamaguch 		delaymsec(10);
   4632  1.1  yamaguch 	}
   4633  1.1  yamaguch 	if (!(reg & (I40E_GLNVM_ULD_CONF_CORE_DONE_MASK |
   4634  1.1  yamaguch 	    I40E_GLNVM_ULD_CONF_GLOBAL_DONE_MASK))) {
   4635  1.1  yamaguch 		aprint_error(", wait for FW Reset complete timed out "
   4636  1.1  yamaguch 		    "(I40E_GLNVM_ULD = 0x%x)\n", reg);
   4637  1.1  yamaguch 		return -1;
   4638  1.1  yamaguch 	}
   4639  1.1  yamaguch 
   4640  1.1  yamaguch 	/*
   4641  1.1  yamaguch 	 * If there was a Global Reset in progress when we got here,
   4642  1.1  yamaguch 	 * we don't need to do the PF Reset
   4643  1.1  yamaguch 	 */
   4644  1.1  yamaguch 	if (cnt == 0) {
   4645  1.1  yamaguch 		reg = ixl_rd(sc, I40E_PFGEN_CTRL);
   4646  1.1  yamaguch 		ixl_wr(sc, I40E_PFGEN_CTRL, reg | I40E_PFGEN_CTRL_PFSWR_MASK);
   4647  1.1  yamaguch 		for (cnt = 0; cnt < I40E_PF_RESET_WAIT_COUNT; cnt++) {
   4648  1.1  yamaguch 			reg = ixl_rd(sc, I40E_PFGEN_CTRL);
   4649  1.1  yamaguch 			if (!(reg & I40E_PFGEN_CTRL_PFSWR_MASK))
   4650  1.1  yamaguch 				break;
   4651  1.1  yamaguch 			delaymsec(1);
   4652  1.1  yamaguch 
   4653  1.1  yamaguch 			reg0 = ixl_rd(sc, I40E_GLGEN_RSTAT);
   4654  1.1  yamaguch 			if (reg0 & I40E_GLGEN_RSTAT_DEVSTATE_MASK) {
   4655  1.1  yamaguch 				aprint_error(", Core reset upcoming."
   4656  1.1  yamaguch 				    " Skipping PF reset reset request\n");
   4657  1.1  yamaguch 				return -1;
   4658  1.1  yamaguch 			}
   4659  1.1  yamaguch 		}
   4660  1.1  yamaguch 		if (reg & I40E_PFGEN_CTRL_PFSWR_MASK) {
   4661  1.1  yamaguch 			aprint_error(", PF reset polling failed to complete"
   4662  1.1  yamaguch 			    "(I40E_PFGEN_CTRL= 0x%x)\n", reg);
   4663  1.1  yamaguch 			return -1;
   4664  1.1  yamaguch 		}
   4665  1.1  yamaguch 	}
   4666  1.1  yamaguch 
   4667  1.1  yamaguch 	return 0;
   4668  1.1  yamaguch }
   4669  1.1  yamaguch 
   4670  1.1  yamaguch static int
   4671  1.1  yamaguch ixl_dmamem_alloc(struct ixl_softc *sc, struct ixl_dmamem *ixm,
   4672  1.1  yamaguch     bus_size_t size, bus_size_t align)
   4673  1.1  yamaguch {
   4674  1.1  yamaguch 	ixm->ixm_size = size;
   4675  1.1  yamaguch 
   4676  1.1  yamaguch 	if (bus_dmamap_create(sc->sc_dmat, ixm->ixm_size, 1,
   4677  1.1  yamaguch 	    ixm->ixm_size, 0,
   4678  1.1  yamaguch 	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
   4679  1.1  yamaguch 	    &ixm->ixm_map) != 0)
   4680  1.1  yamaguch 		return 1;
   4681  1.1  yamaguch 	if (bus_dmamem_alloc(sc->sc_dmat, ixm->ixm_size,
   4682  1.1  yamaguch 	    align, 0, &ixm->ixm_seg, 1, &ixm->ixm_nsegs,
   4683  1.1  yamaguch 	    BUS_DMA_WAITOK) != 0)
   4684  1.1  yamaguch 		goto destroy;
   4685  1.1  yamaguch 	if (bus_dmamem_map(sc->sc_dmat, &ixm->ixm_seg, ixm->ixm_nsegs,
   4686  1.1  yamaguch 	    ixm->ixm_size, &ixm->ixm_kva, BUS_DMA_WAITOK) != 0)
   4687  1.1  yamaguch 		goto free;
   4688  1.1  yamaguch 	if (bus_dmamap_load(sc->sc_dmat, ixm->ixm_map, ixm->ixm_kva,
   4689  1.1  yamaguch 	    ixm->ixm_size, NULL, BUS_DMA_WAITOK) != 0)
   4690  1.1  yamaguch 		goto unmap;
   4691  1.1  yamaguch 
   4692  1.1  yamaguch 	memset(ixm->ixm_kva, 0, ixm->ixm_size);
   4693  1.1  yamaguch 
   4694  1.1  yamaguch 	return 0;
   4695  1.1  yamaguch unmap:
   4696  1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, ixm->ixm_kva, ixm->ixm_size);
   4697  1.1  yamaguch free:
   4698  1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &ixm->ixm_seg, 1);
   4699  1.1  yamaguch destroy:
   4700  1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, ixm->ixm_map);
   4701  1.1  yamaguch 	return 1;
   4702  1.1  yamaguch }
   4703  1.1  yamaguch 
   4704  1.1  yamaguch static void
   4705  1.1  yamaguch ixl_dmamem_free(struct ixl_softc *sc, struct ixl_dmamem *ixm)
   4706  1.1  yamaguch {
   4707  1.1  yamaguch 	bus_dmamap_unload(sc->sc_dmat, ixm->ixm_map);
   4708  1.1  yamaguch 	bus_dmamem_unmap(sc->sc_dmat, ixm->ixm_kva, ixm->ixm_size);
   4709  1.1  yamaguch 	bus_dmamem_free(sc->sc_dmat, &ixm->ixm_seg, 1);
   4710  1.1  yamaguch 	bus_dmamap_destroy(sc->sc_dmat, ixm->ixm_map);
   4711  1.1  yamaguch }
   4712  1.1  yamaguch 
   4713  1.1  yamaguch static int
   4714  1.1  yamaguch ixl_set_macvlan(struct ixl_softc *sc)
   4715  1.1  yamaguch {
   4716  1.1  yamaguch 	int	 error, rv = 0;
   4717  1.1  yamaguch 
   4718  1.1  yamaguch 	/* remove default mac filter and replace it so we can see vlans */
   4719  1.1  yamaguch 
   4720  1.1  yamaguch 	error = ixl_remove_macvlan(sc, sc->sc_enaddr, 0, 0);
   4721  1.1  yamaguch 	if (error != IXL_AQ_RC_OK) {
   4722  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev, "unable to remove macvlan\n");
   4723  1.1  yamaguch 		rv = -1;
   4724  1.1  yamaguch 	}
   4725  1.1  yamaguch 
   4726  1.1  yamaguch 	error = ixl_remove_macvlan(sc, sc->sc_enaddr, 0,
   4727  1.1  yamaguch 	    IXL_AQ_OP_REMOVE_MACVLAN_IGNORE_VLAN);
   4728  1.1  yamaguch 	if (error != IXL_AQ_RC_OK && error != IXL_AQ_RC_ENOENT) {
   4729  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   4730  1.1  yamaguch 		    "unable to remove macvlan(IGNORE_VLAN)\n");
   4731  1.1  yamaguch 		rv = -1;
   4732  1.1  yamaguch 	}
   4733  1.1  yamaguch 
   4734  1.1  yamaguch 	error = ixl_add_macvlan(sc, sc->sc_enaddr, 0,
   4735  1.1  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   4736  1.1  yamaguch 	if (error != IXL_AQ_RC_OK) {
   4737  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev, "unable to add mac address\n");
   4738  1.1  yamaguch 		rv = -1;
   4739  1.1  yamaguch 	}
   4740  1.1  yamaguch 
   4741  1.1  yamaguch 	error = ixl_add_macvlan(sc, etherbroadcastaddr, 0,
   4742  1.1  yamaguch 	    IXL_AQ_OP_ADD_MACVLAN_IGNORE_VLAN);
   4743  1.1  yamaguch 	if (error != IXL_AQ_RC_OK) {
   4744  1.1  yamaguch 		aprint_debug_dev(sc->sc_dev,
   4745  1.1  yamaguch 		    "unable to add broadcast mac address\n");
   4746  1.1  yamaguch 		rv = -1;
   4747  1.1  yamaguch 	}
   4748  1.1  yamaguch 
   4749  1.1  yamaguch 	return rv;
   4750  1.1  yamaguch }
   4751  1.1  yamaguch 
   4752  1.1  yamaguch static void
   4753  1.1  yamaguch ixl_link_status(struct ixl_softc *sc)
   4754  1.1  yamaguch {
   4755  1.1  yamaguch 
   4756  1.1  yamaguch 	(void)ixl_get_link_status(sc);
   4757  1.1  yamaguch }
   4758  1.1  yamaguch 
   4759  1.1  yamaguch static int
   4760  1.1  yamaguch ixl_ifflags_cb(struct ethercom *ec)
   4761  1.1  yamaguch {
   4762  1.1  yamaguch 
   4763  1.1  yamaguch 	return 0;
   4764  1.1  yamaguch }
   4765  1.1  yamaguch 
   4766  1.1  yamaguch static int
   4767  1.1  yamaguch ixl_set_link_status(struct ixl_softc *sc, const struct ixl_aq_desc *iaq)
   4768  1.1  yamaguch {
   4769  1.1  yamaguch 	const struct ixl_aq_link_status *status;
   4770  1.1  yamaguch 	const struct ixl_phy_type *itype;
   4771  1.1  yamaguch 
   4772  1.1  yamaguch 	uint64_t ifm_active = IFM_ETHER;
   4773  1.1  yamaguch 	uint64_t ifm_status = IFM_AVALID;
   4774  1.1  yamaguch 	int link_state = LINK_STATE_DOWN;
   4775  1.1  yamaguch 	uint64_t baudrate = 0;
   4776  1.1  yamaguch 
   4777  1.1  yamaguch 	status = (const struct ixl_aq_link_status *)iaq->iaq_param;
   4778  1.1  yamaguch 	if (!ISSET(status->link_info, IXL_AQ_LINK_UP_FUNCTION))
   4779  1.1  yamaguch 		goto done;
   4780  1.1  yamaguch 
   4781  1.1  yamaguch 	ifm_active |= IFM_FDX;
   4782  1.1  yamaguch 	ifm_status |= IFM_ACTIVE;
   4783  1.1  yamaguch 	link_state = LINK_STATE_UP;
   4784  1.1  yamaguch 
   4785  1.1  yamaguch 	itype = ixl_search_phy_type(status->phy_type);
   4786  1.1  yamaguch 	if (itype != NULL)
   4787  1.1  yamaguch 		ifm_active |= itype->ifm_type;
   4788  1.1  yamaguch 
   4789  1.1  yamaguch 	if (ISSET(status->an_info, IXL_AQ_LINK_PAUSE_TX))
   4790  1.1  yamaguch 		ifm_active |= IFM_ETH_TXPAUSE;
   4791  1.1  yamaguch 	if (ISSET(status->an_info, IXL_AQ_LINK_PAUSE_RX))
   4792  1.1  yamaguch 		ifm_active |= IFM_ETH_RXPAUSE;
   4793  1.1  yamaguch 
   4794  1.1  yamaguch 	baudrate = ixl_search_link_speed(status->link_speed);
   4795  1.1  yamaguch 
   4796  1.1  yamaguch done:
   4797  1.1  yamaguch 	/* NET_ASSERT_LOCKED() except during attach */
   4798  1.1  yamaguch 	sc->sc_media_active = ifm_active;
   4799  1.1  yamaguch 	sc->sc_media_status = ifm_status;
   4800  1.1  yamaguch 
   4801  1.1  yamaguch 	sc->sc_ec.ec_if.if_baudrate = baudrate;
   4802  1.1  yamaguch 
   4803  1.1  yamaguch 	return link_state;
   4804  1.1  yamaguch }
   4805  1.1  yamaguch 
   4806  1.1  yamaguch static int
   4807  1.1  yamaguch ixl_establish_intx(struct ixl_softc *sc)
   4808  1.1  yamaguch {
   4809  1.1  yamaguch 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
   4810  1.1  yamaguch 	pci_intr_handle_t *intr;
   4811  1.1  yamaguch 	char xnamebuf[32];
   4812  1.1  yamaguch 	char intrbuf[PCI_INTRSTR_LEN];
   4813  1.1  yamaguch 	char const *intrstr;
   4814  1.1  yamaguch 
   4815  1.1  yamaguch 	KASSERT(sc->sc_nintrs == 1);
   4816  1.1  yamaguch 
   4817  1.1  yamaguch 	intr = &sc->sc_ihp[0];
   4818  1.1  yamaguch 
   4819  1.1  yamaguch 	intrstr = pci_intr_string(pc, *intr, intrbuf, sizeof(intrbuf));
   4820  1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s:legacy",
   4821  1.1  yamaguch 	    device_xname(sc->sc_dev));
   4822  1.1  yamaguch 
   4823  1.1  yamaguch 	sc->sc_ihs[0] = pci_intr_establish_xname(pc, *intr, IPL_NET, ixl_intr,
   4824  1.1  yamaguch 	    sc, xnamebuf);
   4825  1.1  yamaguch 
   4826  1.1  yamaguch 	if (sc->sc_ihs[0] == NULL) {
   4827  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4828  1.1  yamaguch 		    "unable to establish interrupt at %s\n", intrstr);
   4829  1.1  yamaguch 		return -1;
   4830  1.1  yamaguch 	}
   4831  1.1  yamaguch 
   4832  1.1  yamaguch 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   4833  1.1  yamaguch 	return 0;
   4834  1.1  yamaguch }
   4835  1.1  yamaguch 
   4836  1.1  yamaguch static int
   4837  1.1  yamaguch ixl_establish_msix(struct ixl_softc *sc)
   4838  1.1  yamaguch {
   4839  1.1  yamaguch 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
   4840  1.1  yamaguch 	unsigned int vector = 0;
   4841  1.1  yamaguch 	unsigned int i;
   4842  1.1  yamaguch 	char xnamebuf[32];
   4843  1.1  yamaguch 	char intrbuf[PCI_INTRSTR_LEN];
   4844  1.1  yamaguch 	char const *intrstr;
   4845  1.1  yamaguch 
   4846  1.1  yamaguch 	/* the "other" intr is mapped to vector 0 */
   4847  1.1  yamaguch 	vector = 0;
   4848  1.1  yamaguch 	intrstr = pci_intr_string(pc, sc->sc_ihp[vector],
   4849  1.1  yamaguch 	    intrbuf, sizeof(intrbuf));
   4850  1.1  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s others",
   4851  1.1  yamaguch 	    device_xname(sc->sc_dev));
   4852  1.1  yamaguch 	sc->sc_ihs[vector] = pci_intr_establish_xname(pc,
   4853  1.1  yamaguch 	    sc->sc_ihp[vector], IPL_NET, ixl_other_intr,
   4854  1.1  yamaguch 	    sc, xnamebuf);
   4855  1.1  yamaguch 	if (sc->sc_ihs[vector] == NULL) {
   4856  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   4857  1.1  yamaguch 		    "unable to establish interrupt at %s\n", intrstr);
   4858  1.1  yamaguch 		goto fail;
   4859  1.1  yamaguch 	}
   4860  1.1  yamaguch 	vector++;
   4861  1.1  yamaguch 	aprint_normal_dev(sc->sc_dev, "interrupt at %s\n", intrstr);
   4862  1.1  yamaguch 
   4863  1.1  yamaguch 	sc->sc_msix_vector_queue = vector;
   4864  1.1  yamaguch 
   4865  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   4866  1.1  yamaguch 		intrstr = pci_intr_string(pc, sc->sc_ihp[vector],
   4867  1.1  yamaguch 		    intrbuf, sizeof(intrbuf));
   4868  1.1  yamaguch 		snprintf(xnamebuf, sizeof(xnamebuf), "%s TXRX%d",
   4869  1.1  yamaguch 		    device_xname(sc->sc_dev), i);
   4870  1.1  yamaguch 
   4871  1.1  yamaguch 		sc->sc_ihs[vector] = pci_intr_establish_xname(pc,
   4872  1.1  yamaguch 		    sc->sc_ihp[vector], IPL_NET, ixl_queue_intr,
   4873  1.1  yamaguch 		    (void *)&sc->sc_qps[i], xnamebuf);
   4874  1.1  yamaguch 
   4875  1.1  yamaguch 		if (sc->sc_ihs[vector] == NULL) {
   4876  1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   4877  1.1  yamaguch 			    "unable to establish interrupt at %s\n", intrstr);
   4878  1.1  yamaguch 			goto fail;
   4879  1.1  yamaguch 		}
   4880  1.1  yamaguch 		vector++;
   4881  1.1  yamaguch 		aprint_normal_dev(sc->sc_dev,
   4882  1.1  yamaguch 		    "interrupt at %s\n", intrstr);
   4883  1.1  yamaguch 	}
   4884  1.1  yamaguch 
   4885  1.1  yamaguch 	return 0;
   4886  1.1  yamaguch fail:
   4887  1.1  yamaguch 	for (i = 0; i < vector; i++) {
   4888  1.1  yamaguch 		pci_intr_disestablish(pc, sc->sc_ihs[i]);
   4889  1.1  yamaguch 	}
   4890  1.1  yamaguch 
   4891  1.1  yamaguch 	sc->sc_msix_vector_queue = 0;
   4892  1.1  yamaguch 	sc->sc_msix_vector_queue = 0;
   4893  1.1  yamaguch 
   4894  1.1  yamaguch 	return -1;
   4895  1.1  yamaguch }
   4896  1.1  yamaguch 
   4897  1.1  yamaguch static void
   4898  1.1  yamaguch ixl_set_affinity_msix(struct ixl_softc *sc)
   4899  1.1  yamaguch {
   4900  1.1  yamaguch 	kcpuset_t *affinity;
   4901  1.1  yamaguch 	pci_chipset_tag_t pc = sc->sc_pa.pa_pc;
   4902  1.1  yamaguch 	int affinity_to, r;
   4903  1.1  yamaguch 	unsigned int i, vector;
   4904  1.1  yamaguch 	char intrbuf[PCI_INTRSTR_LEN];
   4905  1.1  yamaguch 	char const *intrstr;
   4906  1.1  yamaguch 
   4907  1.1  yamaguch 	affinity_to = 0;
   4908  1.1  yamaguch 	kcpuset_create(&affinity, false);
   4909  1.1  yamaguch 
   4910  1.1  yamaguch 	vector = sc->sc_msix_vector_queue;
   4911  1.1  yamaguch 
   4912  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   4913  1.1  yamaguch 		affinity_to = i % ncpu;
   4914  1.1  yamaguch 
   4915  1.1  yamaguch 		kcpuset_zero(affinity);
   4916  1.1  yamaguch 		kcpuset_set(affinity, affinity_to);
   4917  1.1  yamaguch 
   4918  1.1  yamaguch 		intrstr = pci_intr_string(pc, sc->sc_ihp[vector + i],
   4919  1.1  yamaguch 		    intrbuf, sizeof(intrbuf));
   4920  1.1  yamaguch 		r = interrupt_distribute(sc->sc_ihs[vector + i],
   4921  1.1  yamaguch 		    affinity, NULL);
   4922  1.1  yamaguch 		if (r == 0) {
   4923  1.1  yamaguch 			aprint_normal_dev(sc->sc_dev,
   4924  1.1  yamaguch 			    "for TXRX%u interrupting at %s affinity to %u\n",
   4925  1.1  yamaguch 			    i, intrstr, affinity_to);
   4926  1.1  yamaguch 		} else {
   4927  1.1  yamaguch 			aprint_normal_dev(sc->sc_dev,
   4928  1.1  yamaguch 			    "for TXRX%u interrupting at %s\n",
   4929  1.1  yamaguch 			    i, intrstr);
   4930  1.1  yamaguch 		}
   4931  1.1  yamaguch 	}
   4932  1.1  yamaguch 
   4933  1.1  yamaguch 	vector = 0; /* vector 0 means "other" interrupt */
   4934  1.1  yamaguch 	affinity_to = (affinity_to + 1) % ncpu;
   4935  1.1  yamaguch 	kcpuset_zero(affinity);
   4936  1.1  yamaguch 	kcpuset_set(affinity, affinity_to);
   4937  1.1  yamaguch 
   4938  1.1  yamaguch 	intrstr = pci_intr_string(pc, sc->sc_ihp[vector],
   4939  1.1  yamaguch 	    intrbuf, sizeof(intrbuf));
   4940  1.1  yamaguch 	r = interrupt_distribute(sc->sc_ihs[vector], affinity, NULL);
   4941  1.1  yamaguch 	if (r == 0) {
   4942  1.1  yamaguch 		aprint_normal_dev(sc->sc_dev,
   4943  1.1  yamaguch 		    "for other interrupting at %s affinity to %u\n",
   4944  1.1  yamaguch 		    intrstr, affinity_to);
   4945  1.1  yamaguch 	} else {
   4946  1.1  yamaguch 		aprint_normal_dev(sc->sc_dev,
   4947  1.1  yamaguch 		    "for other interrupting at %s", intrstr);
   4948  1.1  yamaguch 	}
   4949  1.1  yamaguch 
   4950  1.1  yamaguch 	kcpuset_destroy(affinity);
   4951  1.1  yamaguch }
   4952  1.1  yamaguch 
   4953  1.1  yamaguch static void
   4954  1.1  yamaguch ixl_config_queue_intr(struct ixl_softc *sc)
   4955  1.1  yamaguch {
   4956  1.1  yamaguch 	unsigned int i, vector;
   4957  1.1  yamaguch 
   4958  1.1  yamaguch 	if (sc->sc_intrtype == PCI_INTR_TYPE_MSIX) {
   4959  1.1  yamaguch 		vector = sc->sc_msix_vector_queue;
   4960  1.1  yamaguch 	} else {
   4961  1.1  yamaguch 		vector = I40E_INTR_NOTX_INTR;
   4962  1.1  yamaguch 
   4963  1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLST0,
   4964  1.1  yamaguch 		    (I40E_INTR_NOTX_QUEUE <<
   4965  1.1  yamaguch 		     I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT) |
   4966  1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   4967  1.1  yamaguch 		     I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
   4968  1.1  yamaguch 	}
   4969  1.1  yamaguch 
   4970  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs; i++) {
   4971  1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_DYN_CTLN(i), 0);
   4972  1.1  yamaguch 		ixl_flush(sc);
   4973  1.1  yamaguch 
   4974  1.1  yamaguch 		ixl_wr(sc, I40E_PFINT_LNKLSTN(i),
   4975  1.1  yamaguch 		    ((i) << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT) |
   4976  1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   4977  1.1  yamaguch 		     I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT));
   4978  1.1  yamaguch 
   4979  1.1  yamaguch 		ixl_wr(sc, I40E_QINT_RQCTL(i),
   4980  1.1  yamaguch 		    (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
   4981  1.1  yamaguch 		    (I40E_ITR_INDEX_RX <<
   4982  1.1  yamaguch 		     I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
   4983  1.1  yamaguch 		    (I40E_INTR_NOTX_RX_QUEUE <<
   4984  1.1  yamaguch 		     I40E_QINT_RQCTL_MSIX0_INDX_SHIFT) |
   4985  1.1  yamaguch 		    (i << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
   4986  1.1  yamaguch 		    (I40E_QUEUE_TYPE_TX <<
   4987  1.1  yamaguch 		     I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
   4988  1.1  yamaguch 		    I40E_QINT_RQCTL_CAUSE_ENA_MASK);
   4989  1.1  yamaguch 
   4990  1.1  yamaguch 		ixl_wr(sc, I40E_QINT_TQCTL(i),
   4991  1.1  yamaguch 		    (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
   4992  1.1  yamaguch 		    (I40E_ITR_INDEX_TX <<
   4993  1.1  yamaguch 		     I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
   4994  1.1  yamaguch 		    (I40E_INTR_NOTX_TX_QUEUE <<
   4995  1.1  yamaguch 		     I40E_QINT_TQCTL_MSIX0_INDX_SHIFT) |
   4996  1.1  yamaguch 		    (I40E_QUEUE_TYPE_EOL <<
   4997  1.1  yamaguch 		     I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
   4998  1.1  yamaguch 		    (I40E_QUEUE_TYPE_RX <<
   4999  1.1  yamaguch 		     I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT) |
   5000  1.1  yamaguch 		     I40E_QINT_TQCTL_CAUSE_ENA_MASK);
   5001  1.1  yamaguch 
   5002  1.1  yamaguch 		if (sc->sc_intrtype == PCI_INTR_TYPE_MSIX)
   5003  1.1  yamaguch 			vector++;
   5004  1.1  yamaguch 	}
   5005  1.1  yamaguch 	ixl_flush(sc);
   5006  1.1  yamaguch 
   5007  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_RX), 0x7a);
   5008  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_TX), 0x7a);
   5009  1.1  yamaguch 	ixl_flush(sc);
   5010  1.1  yamaguch }
   5011  1.1  yamaguch 
   5012  1.1  yamaguch static void
   5013  1.1  yamaguch ixl_config_other_intr(struct ixl_softc *sc)
   5014  1.1  yamaguch {
   5015  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA, 0);
   5016  1.1  yamaguch 	(void)ixl_rd(sc, I40E_PFINT_ICR0);
   5017  1.1  yamaguch 
   5018  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ICR0_ENA,
   5019  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
   5020  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_GRST_MASK |
   5021  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_ADMINQ_MASK |
   5022  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
   5023  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
   5024  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_VFLR_MASK |
   5025  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK |
   5026  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
   5027  1.1  yamaguch 	    I40E_PFINT_ICR0_ENA_LINK_STAT_CHANGE_MASK);
   5028  1.1  yamaguch 
   5029  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_LNKLST0, 0x7FF);
   5030  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_ITR0(I40E_ITR_INDEX_OTHER), 0);
   5031  1.1  yamaguch 	ixl_wr(sc, I40E_PFINT_STAT_CTL0,
   5032  1.1  yamaguch 	    (I40E_ITR_INDEX_OTHER <<
   5033  1.1  yamaguch 	     I40E_PFINT_STAT_CTL0_OTHER_ITR_INDX_SHIFT));
   5034  1.1  yamaguch 	ixl_flush(sc);
   5035  1.1  yamaguch }
   5036  1.1  yamaguch 
   5037  1.1  yamaguch static int
   5038  1.1  yamaguch ixl_setup_interrupts(struct ixl_softc *sc)
   5039  1.1  yamaguch {
   5040  1.1  yamaguch 	struct pci_attach_args *pa = &sc->sc_pa;
   5041  1.1  yamaguch 	pci_intr_type_t max_type, intr_type;
   5042  1.1  yamaguch 	int counts[PCI_INTR_TYPE_SIZE];
   5043  1.1  yamaguch 	int error;
   5044  1.1  yamaguch 	unsigned int i;
   5045  1.1  yamaguch 	bool retry, nomsix = IXL_NOMSIX;
   5046  1.1  yamaguch 
   5047  1.1  yamaguch 	memset(counts, 0, sizeof(counts));
   5048  1.1  yamaguch 	max_type = PCI_INTR_TYPE_MSIX;
   5049  1.1  yamaguch 	/* QPs + other interrupt */
   5050  1.1  yamaguch 	counts[PCI_INTR_TYPE_MSIX] = sc->sc_nqueue_pairs_max + 1;
   5051  1.1  yamaguch 	counts[PCI_INTR_TYPE_INTX] = 1;
   5052  1.1  yamaguch 
   5053  1.1  yamaguch 	if (nomsix)
   5054  1.1  yamaguch 		counts[PCI_INTR_TYPE_MSIX] = 0;
   5055  1.1  yamaguch 
   5056  1.1  yamaguch 	do {
   5057  1.1  yamaguch 		retry = false;
   5058  1.1  yamaguch 		error = pci_intr_alloc(pa, &sc->sc_ihp, counts, max_type);
   5059  1.1  yamaguch 		if (error != 0) {
   5060  1.1  yamaguch 			aprint_error_dev(sc->sc_dev,
   5061  1.1  yamaguch 			    "couldn't map interrupt\n");
   5062  1.1  yamaguch 			break;
   5063  1.1  yamaguch 		}
   5064  1.1  yamaguch 		for (i = 0; i < sc->sc_nintrs; i++) {
   5065  1.1  yamaguch 			pci_intr_setattr(pa->pa_pc, &sc->sc_ihp[i],
   5066  1.1  yamaguch 			    PCI_INTR_MPSAFE, true);
   5067  1.1  yamaguch 		}
   5068  1.1  yamaguch 
   5069  1.1  yamaguch 		intr_type = pci_intr_type(pa->pa_pc, sc->sc_ihp[0]);
   5070  1.1  yamaguch 		sc->sc_nintrs = counts[intr_type];
   5071  1.1  yamaguch 		KASSERT(sc->sc_nintrs > 0);
   5072  1.1  yamaguch 
   5073  1.1  yamaguch 		sc->sc_ihs = kmem_alloc(sizeof(sc->sc_ihs[0]) * sc->sc_nintrs,
   5074  1.1  yamaguch 		    KM_SLEEP);
   5075  1.1  yamaguch 
   5076  1.1  yamaguch 		if (intr_type == PCI_INTR_TYPE_MSIX) {
   5077  1.1  yamaguch 			error = ixl_establish_msix(sc);
   5078  1.1  yamaguch 			if (error) {
   5079  1.1  yamaguch 				counts[PCI_INTR_TYPE_MSIX] = 0;
   5080  1.1  yamaguch 				retry = true;
   5081  1.1  yamaguch 			} else {
   5082  1.1  yamaguch 				ixl_set_affinity_msix(sc);
   5083  1.1  yamaguch 			}
   5084  1.1  yamaguch 		} else if (intr_type == PCI_INTR_TYPE_INTX) {
   5085  1.1  yamaguch 			error = ixl_establish_intx(sc);
   5086  1.1  yamaguch 		} else {
   5087  1.1  yamaguch 			error = -1;
   5088  1.1  yamaguch 		}
   5089  1.1  yamaguch 
   5090  1.1  yamaguch 		if (error) {
   5091  1.1  yamaguch 			kmem_free(sc->sc_ihs,
   5092  1.1  yamaguch 			    sizeof(sc->sc_ihs[0]) * sc->sc_nintrs);
   5093  1.1  yamaguch 			pci_intr_release(pa->pa_pc, sc->sc_ihp, sc->sc_nintrs);
   5094  1.1  yamaguch 		} else {
   5095  1.1  yamaguch 			sc->sc_intrtype = intr_type;
   5096  1.1  yamaguch 		}
   5097  1.1  yamaguch 	} while (retry);
   5098  1.1  yamaguch 
   5099  1.1  yamaguch 	return error;
   5100  1.1  yamaguch }
   5101  1.1  yamaguch 
   5102  1.1  yamaguch static void
   5103  1.1  yamaguch ixl_teardown_interrupts(struct ixl_softc *sc)
   5104  1.1  yamaguch {
   5105  1.1  yamaguch 	struct pci_attach_args *pa = &sc->sc_pa;
   5106  1.1  yamaguch 	unsigned int i;
   5107  1.1  yamaguch 
   5108  1.1  yamaguch 	for (i = 0; i < sc->sc_nintrs; i++) {
   5109  1.1  yamaguch 		pci_intr_disestablish(pa->pa_pc, sc->sc_ihs[i]);
   5110  1.1  yamaguch 	}
   5111  1.1  yamaguch 
   5112  1.1  yamaguch 	pci_intr_release(pa->pa_pc, sc->sc_ihp, sc->sc_nintrs);
   5113  1.1  yamaguch 
   5114  1.1  yamaguch 	kmem_free(sc->sc_ihs, sizeof(sc->sc_ihs[0]) * sc->sc_nintrs);
   5115  1.1  yamaguch 	sc->sc_ihs = NULL;
   5116  1.1  yamaguch 	sc->sc_nintrs = 0;
   5117  1.1  yamaguch }
   5118  1.1  yamaguch 
   5119  1.1  yamaguch static int
   5120  1.1  yamaguch ixl_setup_stats(struct ixl_softc *sc)
   5121  1.1  yamaguch {
   5122  1.1  yamaguch 	struct ixl_queue_pair *qp;
   5123  1.1  yamaguch 	struct ixl_tx_ring *txr;
   5124  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   5125  1.1  yamaguch 	unsigned int i;
   5126  1.1  yamaguch 
   5127  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   5128  1.1  yamaguch 		qp = &sc->sc_qps[i];
   5129  1.1  yamaguch 		txr = qp->qp_txr;
   5130  1.1  yamaguch 		rxr = qp->qp_rxr;
   5131  1.1  yamaguch 
   5132  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defragged, EVCNT_TYPE_MISC,
   5133  1.1  yamaguch 		    NULL, qp->qp_name, "m_defrag successed");
   5134  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defrag_failed, EVCNT_TYPE_MISC,
   5135  1.1  yamaguch 		    NULL, qp->qp_name, "m_defrag_failed");
   5136  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_pcqdrop, EVCNT_TYPE_MISC,
   5137  1.1  yamaguch 		    NULL, qp->qp_name, "Dropped in pcq");
   5138  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_transmitdef, EVCNT_TYPE_MISC,
   5139  1.1  yamaguch 		    NULL, qp->qp_name, "Deferred transmit");
   5140  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_intr, EVCNT_TYPE_INTR,
   5141  1.1  yamaguch 		    NULL, qp->qp_name, "Interrupt on queue");
   5142  1.1  yamaguch 		evcnt_attach_dynamic(&txr->txr_defer, EVCNT_TYPE_MISC,
   5143  1.1  yamaguch 		    NULL, qp->qp_name, "Handled queue in softint/workqueue");
   5144  1.1  yamaguch 
   5145  1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mgethdr_failed, EVCNT_TYPE_MISC,
   5146  1.1  yamaguch 		    NULL, qp->qp_name, "MGETHDR failed");
   5147  1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mgetcl_failed, EVCNT_TYPE_MISC,
   5148  1.1  yamaguch 		    NULL, qp->qp_name, "MCLGET failed");
   5149  1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_mbuf_load_failed,
   5150  1.1  yamaguch 		    EVCNT_TYPE_MISC, NULL, qp->qp_name,
   5151  1.1  yamaguch 		    "bus_dmamap_load_mbuf failed");
   5152  1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_intr, EVCNT_TYPE_INTR,
   5153  1.1  yamaguch 		    NULL, qp->qp_name, "Interrupt on queue");
   5154  1.1  yamaguch 		evcnt_attach_dynamic(&rxr->rxr_defer, EVCNT_TYPE_MISC,
   5155  1.1  yamaguch 		    NULL, qp->qp_name, "Handled queue in softint/workqueue");
   5156  1.1  yamaguch 	}
   5157  1.1  yamaguch 
   5158  1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_atq, EVCNT_TYPE_INTR,
   5159  1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Interrupt for other events");
   5160  1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_link, EVCNT_TYPE_MISC,
   5161  1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Link status event");
   5162  1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_ecc_err, EVCNT_TYPE_MISC,
   5163  1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "ECC error");
   5164  1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_pci_exception, EVCNT_TYPE_MISC,
   5165  1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "PCI exception");
   5166  1.1  yamaguch 	evcnt_attach_dynamic(&sc->sc_event_crit_err, EVCNT_TYPE_MISC,
   5167  1.1  yamaguch 	    NULL, device_xname(sc->sc_dev), "Critical error");
   5168  1.1  yamaguch 
   5169  1.1  yamaguch 	return 0;
   5170  1.1  yamaguch }
   5171  1.1  yamaguch 
   5172  1.1  yamaguch static void
   5173  1.1  yamaguch ixl_teardown_stats(struct ixl_softc *sc)
   5174  1.1  yamaguch {
   5175  1.1  yamaguch 	struct ixl_tx_ring *txr;
   5176  1.1  yamaguch 	struct ixl_rx_ring *rxr;
   5177  1.1  yamaguch 	unsigned int i;
   5178  1.1  yamaguch 
   5179  1.1  yamaguch 	for (i = 0; i < sc->sc_nqueue_pairs_max; i++) {
   5180  1.1  yamaguch 		txr = sc->sc_qps[i].qp_txr;
   5181  1.1  yamaguch 		rxr = sc->sc_qps[i].qp_rxr;
   5182  1.1  yamaguch 
   5183  1.1  yamaguch 		evcnt_detach(&txr->txr_defragged);
   5184  1.1  yamaguch 		evcnt_detach(&txr->txr_defrag_failed);
   5185  1.1  yamaguch 		evcnt_detach(&txr->txr_pcqdrop);
   5186  1.1  yamaguch 		evcnt_detach(&txr->txr_transmitdef);
   5187  1.1  yamaguch 		evcnt_detach(&txr->txr_intr);
   5188  1.1  yamaguch 		evcnt_detach(&txr->txr_defer);
   5189  1.1  yamaguch 
   5190  1.1  yamaguch 		evcnt_detach(&rxr->rxr_mgethdr_failed);
   5191  1.1  yamaguch 		evcnt_detach(&rxr->rxr_mgetcl_failed);
   5192  1.1  yamaguch 		evcnt_detach(&rxr->rxr_mbuf_load_failed);
   5193  1.1  yamaguch 		evcnt_detach(&rxr->rxr_intr);
   5194  1.1  yamaguch 		evcnt_detach(&rxr->rxr_defer);
   5195  1.1  yamaguch 	}
   5196  1.1  yamaguch 
   5197  1.1  yamaguch 	evcnt_detach(&sc->sc_event_atq);
   5198  1.1  yamaguch 	evcnt_detach(&sc->sc_event_link);
   5199  1.1  yamaguch 	evcnt_detach(&sc->sc_event_ecc_err);
   5200  1.1  yamaguch 	evcnt_detach(&sc->sc_event_pci_exception);
   5201  1.1  yamaguch 	evcnt_detach(&sc->sc_event_crit_err);
   5202  1.1  yamaguch }
   5203  1.1  yamaguch 
   5204  1.1  yamaguch static int
   5205  1.1  yamaguch ixl_setup_sysctls(struct ixl_softc *sc)
   5206  1.1  yamaguch {
   5207  1.1  yamaguch 	const char *devname;
   5208  1.1  yamaguch 	struct sysctllog **log;
   5209  1.1  yamaguch 	const struct sysctlnode *rnode, *rxnode, *txnode;
   5210  1.1  yamaguch 	int error;
   5211  1.1  yamaguch 
   5212  1.1  yamaguch 	log = &sc->sc_sysctllog;
   5213  1.1  yamaguch 	devname = device_xname(sc->sc_dev);
   5214  1.1  yamaguch 
   5215  1.1  yamaguch 	error = sysctl_createv(log, 0, NULL, &rnode,
   5216  1.1  yamaguch 	    0, CTLTYPE_NODE, devname,
   5217  1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings"),
   5218  1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   5219  1.1  yamaguch 	if (error)
   5220  1.1  yamaguch 		goto out;
   5221  1.1  yamaguch 
   5222  1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, NULL,
   5223  1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
   5224  1.1  yamaguch 	    SYSCTL_DESCR("Use workqueue for packet processing"),
   5225  1.1  yamaguch 	    NULL, 0, &sc->sc_txrx_workqueue, 0, CTL_CREATE, CTL_EOL);
   5226  1.1  yamaguch 	if (error)
   5227  1.1  yamaguch 		goto out;
   5228  1.1  yamaguch 
   5229  1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, &rxnode,
   5230  1.1  yamaguch 	    0, CTLTYPE_NODE, "rx",
   5231  1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings for Rx"),
   5232  1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   5233  1.1  yamaguch 	if (error)
   5234  1.1  yamaguch 		goto out;
   5235  1.1  yamaguch 
   5236  1.1  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   5237  1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   5238  1.1  yamaguch 	    SYSCTL_DESCR("max number of Rx packets"
   5239  1.1  yamaguch 	    " to process for interrupt processing"),
   5240  1.1  yamaguch 	    NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   5241  1.1  yamaguch 	if (error)
   5242  1.1  yamaguch 		goto out;
   5243  1.1  yamaguch 
   5244  1.1  yamaguch 	error = sysctl_createv(log, 0, &rxnode, NULL,
   5245  1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   5246  1.1  yamaguch 	    SYSCTL_DESCR("max number of Rx packets"
   5247  1.1  yamaguch 	    " to process for deferred processing"),
   5248  1.1  yamaguch 	    NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
   5249  1.1  yamaguch 	if (error)
   5250  1.1  yamaguch 		goto out;
   5251  1.1  yamaguch 
   5252  1.1  yamaguch 	error = sysctl_createv(log, 0, &rnode, &txnode,
   5253  1.1  yamaguch 	    0, CTLTYPE_NODE, "tx",
   5254  1.1  yamaguch 	    SYSCTL_DESCR("ixl information and settings for Tx"),
   5255  1.1  yamaguch 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
   5256  1.1  yamaguch 	if (error)
   5257  1.1  yamaguch 		goto out;
   5258  1.1  yamaguch 
   5259  1.1  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   5260  1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
   5261  1.1  yamaguch 	    SYSCTL_DESCR("max number of Tx packets"
   5262  1.1  yamaguch 	    " to process for interrupt processing"),
   5263  1.1  yamaguch 	    NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
   5264  1.1  yamaguch 	if (error)
   5265  1.1  yamaguch 		goto out;
   5266  1.1  yamaguch 
   5267  1.1  yamaguch 	error = sysctl_createv(log, 0, &txnode, NULL,
   5268  1.1  yamaguch 	    CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
   5269  1.1  yamaguch 	    SYSCTL_DESCR("max number of Tx packets"
   5270  1.1  yamaguch 	    " to process for deferred processing"),
   5271  1.1  yamaguch 	    NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
   5272  1.1  yamaguch 	if (error)
   5273  1.1  yamaguch 		goto out;
   5274  1.1  yamaguch 
   5275  1.1  yamaguch out:
   5276  1.1  yamaguch 	if (error) {
   5277  1.1  yamaguch 		aprint_error_dev(sc->sc_dev,
   5278  1.1  yamaguch 		    "unable to create sysctl node\n");
   5279  1.1  yamaguch 		sysctl_teardown(log);
   5280  1.1  yamaguch 	}
   5281  1.1  yamaguch 
   5282  1.1  yamaguch 	return error;
   5283  1.1  yamaguch }
   5284  1.1  yamaguch 
   5285  1.1  yamaguch static void
   5286  1.1  yamaguch ixl_teardown_sysctls(struct ixl_softc *sc)
   5287  1.1  yamaguch {
   5288  1.1  yamaguch 
   5289  1.1  yamaguch 	sysctl_teardown(&sc->sc_sysctllog);
   5290  1.1  yamaguch }
   5291  1.1  yamaguch 
   5292  1.1  yamaguch static struct workqueue *
   5293  1.1  yamaguch ixl_workq_create(const char *name, pri_t prio, int ipl, int flags)
   5294  1.1  yamaguch {
   5295  1.1  yamaguch 	struct workqueue *wq;
   5296  1.1  yamaguch 	int error;
   5297  1.1  yamaguch 
   5298  1.1  yamaguch 	error = workqueue_create(&wq, name, ixl_workq_work, NULL,
   5299  1.1  yamaguch 	    prio, ipl, flags);
   5300  1.1  yamaguch 
   5301  1.1  yamaguch 	if (error)
   5302  1.1  yamaguch 		return NULL;
   5303  1.1  yamaguch 
   5304  1.1  yamaguch 	return wq;
   5305  1.1  yamaguch }
   5306  1.1  yamaguch 
   5307  1.1  yamaguch static void
   5308  1.1  yamaguch ixl_workq_destroy(struct workqueue *wq)
   5309  1.1  yamaguch {
   5310  1.1  yamaguch 
   5311  1.1  yamaguch 	workqueue_destroy(wq);
   5312  1.1  yamaguch }
   5313  1.1  yamaguch 
   5314  1.1  yamaguch static void
   5315  1.1  yamaguch ixl_work_set(struct ixl_work *work, void (*func)(void *), void *arg)
   5316  1.1  yamaguch {
   5317  1.1  yamaguch 
   5318  1.1  yamaguch 	memset(work, 0, sizeof(*work));
   5319  1.1  yamaguch 	work->ixw_func = func;
   5320  1.1  yamaguch 	work->ixw_arg = arg;
   5321  1.1  yamaguch }
   5322  1.1  yamaguch 
   5323  1.1  yamaguch static void
   5324  1.1  yamaguch ixl_work_add(struct workqueue *wq, struct ixl_work *work)
   5325  1.1  yamaguch {
   5326  1.1  yamaguch 	if (atomic_cas_uint(&work->ixw_added, 0, 1) != 0)
   5327  1.1  yamaguch 		return;
   5328  1.1  yamaguch 
   5329  1.1  yamaguch 	workqueue_enqueue(wq, &work->ixw_cookie, NULL);
   5330  1.1  yamaguch }
   5331  1.1  yamaguch 
   5332  1.1  yamaguch static void
   5333  1.1  yamaguch ixl_work_wait(struct workqueue *wq, struct ixl_work *work)
   5334  1.1  yamaguch {
   5335  1.1  yamaguch 
   5336  1.1  yamaguch 	workqueue_wait(wq, &work->ixw_cookie);
   5337  1.1  yamaguch }
   5338  1.1  yamaguch 
   5339  1.1  yamaguch static void
   5340  1.1  yamaguch ixl_workq_work(struct work *wk, void *context)
   5341  1.1  yamaguch {
   5342  1.1  yamaguch 	struct ixl_work *work;
   5343  1.1  yamaguch 
   5344  1.1  yamaguch 	work = container_of(wk, struct ixl_work, ixw_cookie);
   5345  1.1  yamaguch 
   5346  1.1  yamaguch 	atomic_swap_uint(&work->ixw_added, 0);
   5347  1.1  yamaguch 	kpreempt_disable();
   5348  1.1  yamaguch 	work->ixw_func(work->ixw_arg);
   5349  1.1  yamaguch 	kpreempt_enable();
   5350  1.1  yamaguch }
   5351  1.1  yamaguch 
   5352  1.1  yamaguch static int
   5353  1.1  yamaguch ixl_rx_ctl_read(struct ixl_softc *sc, uint32_t reg, uint32_t *rv)
   5354  1.1  yamaguch {
   5355  1.1  yamaguch 	struct ixl_aq_desc iaq;
   5356  1.1  yamaguch 
   5357  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   5358  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_RX_CTL_REG_READ);
   5359  1.1  yamaguch 	iaq.iaq_param[1] = htole32(reg);
   5360  1.1  yamaguch 
   5361  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0)
   5362  1.1  yamaguch 		return ETIMEDOUT;
   5363  1.1  yamaguch 
   5364  1.1  yamaguch 	switch (htole16(iaq.iaq_retval)) {
   5365  1.1  yamaguch 	case IXL_AQ_RC_OK:
   5366  1.1  yamaguch 		/* success */
   5367  1.1  yamaguch 		break;
   5368  1.1  yamaguch 	case IXL_AQ_RC_EACCES:
   5369  1.1  yamaguch 		return EPERM;
   5370  1.1  yamaguch 	case IXL_AQ_RC_EAGAIN:
   5371  1.1  yamaguch 		return EAGAIN;
   5372  1.1  yamaguch 	default:
   5373  1.1  yamaguch 		return EIO;
   5374  1.1  yamaguch 	}
   5375  1.1  yamaguch 
   5376  1.1  yamaguch 	*rv = htole32(iaq.iaq_param[3]);
   5377  1.1  yamaguch 	return 0;
   5378  1.1  yamaguch }
   5379  1.1  yamaguch 
   5380  1.1  yamaguch 
   5381  1.1  yamaguch 
   5382  1.1  yamaguch static uint32_t
   5383  1.1  yamaguch ixl_rd_rx_csr(struct ixl_softc *sc, uint32_t reg)
   5384  1.1  yamaguch {
   5385  1.1  yamaguch 	uint32_t val;
   5386  1.1  yamaguch 	int rv, retry, retry_limit;
   5387  1.1  yamaguch 
   5388  1.1  yamaguch 	retry_limit = sc->sc_rxctl_atq ? 5 : 0;
   5389  1.1  yamaguch 
   5390  1.1  yamaguch 	for (retry = 0; retry < retry_limit; retry++) {
   5391  1.1  yamaguch 		rv = ixl_rx_ctl_read(sc, reg, &val);
   5392  1.1  yamaguch 		if (rv == 0)
   5393  1.1  yamaguch 			return val;
   5394  1.1  yamaguch 		else if (rv == EAGAIN)
   5395  1.1  yamaguch 			delaymsec(1);
   5396  1.1  yamaguch 		else
   5397  1.1  yamaguch 			break;
   5398  1.1  yamaguch 	}
   5399  1.1  yamaguch 
   5400  1.1  yamaguch 	val = ixl_rd(sc, reg);
   5401  1.1  yamaguch 
   5402  1.1  yamaguch 	return val;
   5403  1.1  yamaguch }
   5404  1.1  yamaguch 
   5405  1.1  yamaguch static int
   5406  1.1  yamaguch ixl_rx_ctl_write(struct ixl_softc *sc, uint32_t reg, uint32_t value)
   5407  1.1  yamaguch {
   5408  1.1  yamaguch 	struct ixl_aq_desc iaq;
   5409  1.1  yamaguch 
   5410  1.1  yamaguch 	memset(&iaq, 0, sizeof(iaq));
   5411  1.1  yamaguch 	iaq.iaq_opcode = htole16(IXL_AQ_OP_RX_CTL_REG_WRITE);
   5412  1.1  yamaguch 	iaq.iaq_param[1] = htole32(reg);
   5413  1.1  yamaguch 	iaq.iaq_param[3] = htole32(value);
   5414  1.1  yamaguch 
   5415  1.1  yamaguch 	if (ixl_atq_poll(sc, &iaq, 250) != 0)
   5416  1.1  yamaguch 		return ETIMEDOUT;
   5417  1.1  yamaguch 
   5418  1.1  yamaguch 	switch (htole16(iaq.iaq_retval)) {
   5419  1.1  yamaguch 	case IXL_AQ_RC_OK:
   5420  1.1  yamaguch 		/* success */
   5421  1.1  yamaguch 		break;
   5422  1.1  yamaguch 	case IXL_AQ_RC_EACCES:
   5423  1.1  yamaguch 		return EPERM;
   5424  1.1  yamaguch 	case IXL_AQ_RC_EAGAIN:
   5425  1.1  yamaguch 		return EAGAIN;
   5426  1.1  yamaguch 	default:
   5427  1.1  yamaguch 		return EIO;
   5428  1.1  yamaguch 	}
   5429  1.1  yamaguch 
   5430  1.1  yamaguch 	return 0;
   5431  1.1  yamaguch }
   5432  1.1  yamaguch 
   5433  1.1  yamaguch static void
   5434  1.1  yamaguch ixl_wr_rx_csr(struct ixl_softc *sc, uint32_t reg, uint32_t value)
   5435  1.1  yamaguch {
   5436  1.1  yamaguch 	int rv, retry, retry_limit;
   5437  1.1  yamaguch 
   5438  1.1  yamaguch 	retry_limit = sc->sc_rxctl_atq ? 5 : 0;
   5439  1.1  yamaguch 
   5440  1.1  yamaguch 	for (retry = 0; retry < retry_limit; retry++) {
   5441  1.1  yamaguch 		rv = ixl_rx_ctl_write(sc, reg, value);
   5442  1.1  yamaguch 		if (rv == 0)
   5443  1.1  yamaguch 			return;
   5444  1.1  yamaguch 		else if (rv == EAGAIN)
   5445  1.1  yamaguch 			delaymsec(1);
   5446  1.1  yamaguch 		else
   5447  1.1  yamaguch 			break;
   5448  1.1  yamaguch 	}
   5449  1.1  yamaguch 
   5450  1.1  yamaguch 	ixl_wr(sc, reg, value);
   5451  1.1  yamaguch }
   5452  1.1  yamaguch 
   5453  1.1  yamaguch MODULE(MODULE_CLASS_DRIVER, if_ixl, "pci");
   5454  1.1  yamaguch 
   5455  1.1  yamaguch #ifdef _MODULE
   5456  1.1  yamaguch #include "ioconf.c"
   5457  1.1  yamaguch #endif
   5458  1.1  yamaguch 
   5459  1.1  yamaguch static int
   5460  1.1  yamaguch if_ixl_modcmd(modcmd_t cmd, void *opaque)
   5461  1.1  yamaguch {
   5462  1.1  yamaguch 	int error = 0;
   5463  1.1  yamaguch 
   5464  1.1  yamaguch #ifdef _MODULE
   5465  1.1  yamaguch 	switch (cmd) {
   5466  1.1  yamaguch 	case MODULE_CMD_INIT:
   5467  1.1  yamaguch 		error = config_init_component(cfdriver_ioconf_if_ixl,
   5468  1.1  yamaguch 		    cfattach_ioconf_if_ixl, cfdata_ioconf_if_ixl);
   5469  1.1  yamaguch 		break;
   5470  1.1  yamaguch 	case MODULE_CMD_FINI:
   5471  1.1  yamaguch 		error = config_fini_component(cfdriver_ioconf_if_ixl,
   5472  1.1  yamaguch 		    cfattach_ioconf_if_ixl, cfdata_ioconf_if_ixl);
   5473  1.1  yamaguch 		break;
   5474  1.1  yamaguch 	default:
   5475  1.1  yamaguch 		error = ENOTTY;
   5476  1.1  yamaguch 		break;
   5477  1.1  yamaguch 	}
   5478  1.1  yamaguch #endif
   5479  1.1  yamaguch 
   5480  1.1  yamaguch 	return error;
   5481  1.1  yamaguch }
   5482