Home | History | Annotate | Line # | Download | only in pci
if_aq.c revision 1.41
      1  1.41       ryo /*	$NetBSD: if_aq.c,v 1.41 2023/01/14 13:17:20 ryo Exp $	*/
      2   1.1       ryo 
      3   1.1       ryo /**
      4   1.1       ryo  * aQuantia Corporation Network Driver
      5   1.1       ryo  * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
      6   1.1       ryo  *
      7   1.1       ryo  * Redistribution and use in source and binary forms, with or without
      8   1.1       ryo  * modification, are permitted provided that the following conditions
      9   1.1       ryo  * are met:
     10   1.1       ryo  *
     11   1.1       ryo  *   (1) Redistributions of source code must retain the above
     12   1.1       ryo  *   copyright notice, this list of conditions and the following
     13   1.1       ryo  *   disclaimer.
     14   1.1       ryo  *
     15   1.1       ryo  *   (2) Redistributions in binary form must reproduce the above
     16   1.1       ryo  *   copyright notice, this list of conditions and the following
     17   1.1       ryo  *   disclaimer in the documentation and/or other materials provided
     18   1.1       ryo  *   with the distribution.
     19   1.1       ryo  *
     20   1.1       ryo  *   (3) The name of the author may not be used to endorse or promote
     21   1.1       ryo  *   products derived from this software without specific prior
     22   1.1       ryo  *   written permission.
     23   1.1       ryo  *
     24   1.1       ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     25   1.1       ryo  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     26   1.1       ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1       ryo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     28   1.1       ryo  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1       ryo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     30   1.1       ryo  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31   1.1       ryo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     32   1.1       ryo  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33   1.1       ryo  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34   1.1       ryo  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.1       ryo  *
     36   1.1       ryo  */
     37   1.1       ryo 
     38   1.1       ryo /*-
     39   1.1       ryo  * Copyright (c) 2020 Ryo Shimizu <ryo (at) nerv.org>
     40   1.1       ryo  * All rights reserved.
     41   1.1       ryo  *
     42   1.1       ryo  * Redistribution and use in source and binary forms, with or without
     43   1.1       ryo  * modification, are permitted provided that the following conditions
     44   1.1       ryo  * are met:
     45   1.1       ryo  * 1. Redistributions of source code must retain the above copyright
     46   1.1       ryo  *    notice, this list of conditions and the following disclaimer.
     47   1.1       ryo  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1       ryo  *    notice, this list of conditions and the following disclaimer in the
     49   1.1       ryo  *    documentation and/or other materials provided with the distribution.
     50   1.1       ryo  *
     51   1.1       ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52   1.1       ryo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     53   1.1       ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     54   1.1       ryo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     55   1.1       ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     56   1.1       ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     57   1.1       ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58   1.1       ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     59   1.1       ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     60   1.1       ryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61   1.1       ryo  * POSSIBILITY OF SUCH DAMAGE.
     62   1.1       ryo  */
     63   1.1       ryo 
     64   1.1       ryo #include <sys/cdefs.h>
     65  1.41       ryo __KERNEL_RCSID(0, "$NetBSD: if_aq.c,v 1.41 2023/01/14 13:17:20 ryo Exp $");
     66   1.1       ryo 
     67   1.1       ryo #ifdef _KERNEL_OPT
     68   1.1       ryo #include "opt_if_aq.h"
     69   1.4       ryo #include "sysmon_envsys.h"
     70   1.1       ryo #endif
     71   1.1       ryo 
     72   1.1       ryo #include <sys/param.h>
     73   1.1       ryo #include <sys/types.h>
     74   1.1       ryo #include <sys/bitops.h>
     75   1.1       ryo #include <sys/cprng.h>
     76   1.1       ryo #include <sys/cpu.h>
     77   1.1       ryo #include <sys/interrupt.h>
     78   1.1       ryo #include <sys/module.h>
     79   1.1       ryo #include <sys/pcq.h>
     80   1.1       ryo 
     81   1.1       ryo #include <net/bpf.h>
     82   1.1       ryo #include <net/if.h>
     83   1.1       ryo #include <net/if_dl.h>
     84   1.1       ryo #include <net/if_media.h>
     85   1.1       ryo #include <net/if_ether.h>
     86   1.1       ryo #include <net/rss_config.h>
     87   1.1       ryo 
     88   1.1       ryo #include <dev/pci/pcivar.h>
     89   1.1       ryo #include <dev/pci/pcireg.h>
     90   1.1       ryo #include <dev/pci/pcidevs.h>
     91   1.4       ryo #include <dev/sysmon/sysmonvar.h>
     92   1.1       ryo 
     93   1.1       ryo /* driver configuration */
     94   1.1       ryo #define CONFIG_INTR_MODERATION_ENABLE	true	/* delayed interrupt */
     95  1.39    andvar #undef CONFIG_LRO_SUPPORT			/* no LRO not supported */
     96   1.1       ryo #undef CONFIG_NO_TXRX_INDEPENDENT		/* share TX/RX interrupts */
     97   1.1       ryo 
     98   1.1       ryo #define AQ_NINTR_MAX			(AQ_RSSQUEUE_MAX + AQ_RSSQUEUE_MAX + 1)
     99   1.1       ryo 					/* TX + RX + LINK. must be <= 32 */
    100   1.1       ryo #define AQ_LINKSTAT_IRQ			31	/* for legacy mode */
    101   1.1       ryo 
    102   1.1       ryo #define AQ_TXD_NUM			2048	/* per ring. 8*n && 32~8184 */
    103   1.1       ryo #define AQ_RXD_NUM			2048	/* per ring. 8*n && 32~8184 */
    104   1.1       ryo /* minimum required to send a packet (vlan needs additional TX descriptor) */
    105   1.1       ryo #define AQ_TXD_MIN			(1 + 1)
    106   1.1       ryo 
    107   1.1       ryo 
    108   1.1       ryo /* hardware specification */
    109   1.1       ryo #define AQ_RINGS_NUM			32
    110   1.1       ryo #define AQ_RSSQUEUE_MAX			8
    111   1.1       ryo #define AQ_RX_DESCRIPTOR_MIN		32
    112   1.1       ryo #define AQ_TX_DESCRIPTOR_MIN		32
    113   1.1       ryo #define AQ_RX_DESCRIPTOR_MAX		8184
    114   1.1       ryo #define AQ_TX_DESCRIPTOR_MAX		8184
    115   1.1       ryo #define AQ_TRAFFICCLASS_NUM		8
    116   1.1       ryo #define AQ_RSS_HASHKEY_SIZE		40
    117   1.1       ryo #define AQ_RSS_INDIRECTION_TABLE_MAX	64
    118   1.1       ryo 
    119  1.23       ryo #define AQ_JUMBO_MTU_REV_A		9000
    120  1.23       ryo #define AQ_JUMBO_MTU_REV_B		16338
    121  1.23       ryo 
    122   1.1       ryo /*
    123   1.1       ryo  * TERMINOLOGY
    124   1.1       ryo  *	MPI = MAC PHY INTERFACE?
    125   1.1       ryo  *	RPO = RX Protocol Offloading
    126   1.1       ryo  *	TPO = TX Protocol Offloading
    127   1.1       ryo  *	RPF = RX Packet Filter
    128   1.1       ryo  *	TPB = TX Packet buffer
    129   1.1       ryo  *	RPB = RX Packet buffer
    130   1.1       ryo  */
    131   1.1       ryo 
    132   1.1       ryo /* registers */
    133   1.1       ryo #define AQ_FW_SOFTRESET_REG			0x0000
    134   1.1       ryo #define  AQ_FW_SOFTRESET_RESET			__BIT(15) /* soft reset bit */
    135   1.1       ryo #define  AQ_FW_SOFTRESET_DIS			__BIT(14) /* reset disable */
    136   1.1       ryo 
    137   1.1       ryo #define AQ_FW_VERSION_REG			0x0018
    138   1.1       ryo #define AQ_HW_REVISION_REG			0x001c
    139   1.1       ryo #define AQ_GLB_NVR_INTERFACE1_REG		0x0100
    140   1.1       ryo 
    141   1.1       ryo #define AQ_FW_MBOX_CMD_REG			0x0200
    142   1.1       ryo #define  AQ_FW_MBOX_CMD_EXECUTE			0x00008000
    143   1.1       ryo #define  AQ_FW_MBOX_CMD_BUSY			0x00000100
    144   1.1       ryo #define AQ_FW_MBOX_ADDR_REG			0x0208
    145   1.1       ryo #define AQ_FW_MBOX_VAL_REG			0x020c
    146   1.1       ryo 
    147   1.1       ryo #define FW2X_LED_MIN_VERSION			0x03010026	/* >= 3.1.38 */
    148   1.1       ryo #define FW2X_LED_REG				0x031c
    149   1.1       ryo #define  FW2X_LED_DEFAULT			0x00000000
    150   1.1       ryo #define  FW2X_LED_NONE				0x0000003f
    151   1.1       ryo #define  FW2X_LINKLED				__BITS(0,1)
    152   1.1       ryo #define   FW2X_LINKLED_ACTIVE			0
    153   1.1       ryo #define   FW2X_LINKLED_ON			1
    154   1.1       ryo #define   FW2X_LINKLED_BLINK			2
    155   1.1       ryo #define   FW2X_LINKLED_OFF			3
    156   1.1       ryo #define  FW2X_STATUSLED				__BITS(2,5)
    157   1.1       ryo #define   FW2X_STATUSLED_ORANGE			0
    158   1.1       ryo #define   FW2X_STATUSLED_ORANGE_BLINK		2
    159   1.1       ryo #define   FW2X_STATUSLED_OFF			3
    160   1.1       ryo #define   FW2X_STATUSLED_GREEN			4
    161   1.1       ryo #define   FW2X_STATUSLED_ORANGE_GREEN_BLINK	8
    162   1.1       ryo #define   FW2X_STATUSLED_GREEN_BLINK		10
    163   1.1       ryo 
    164   1.1       ryo #define FW_MPI_MBOX_ADDR_REG			0x0360
    165   1.1       ryo #define FW1X_MPI_INIT1_REG			0x0364
    166   1.1       ryo #define FW1X_MPI_CONTROL_REG			0x0368
    167   1.1       ryo #define FW1X_MPI_STATE_REG			0x036c
    168   1.1       ryo #define  FW1X_MPI_STATE_MODE			__BITS(7,0)
    169   1.1       ryo #define  FW1X_MPI_STATE_SPEED			__BITS(32,16)
    170   1.1       ryo #define  FW1X_MPI_STATE_DISABLE_DIRTYWAKE	__BITS(25)
    171   1.1       ryo #define  FW1X_MPI_STATE_DOWNSHIFT		__BITS(31,28)
    172   1.1       ryo #define FW1X_MPI_INIT2_REG			0x0370
    173   1.1       ryo #define FW1X_MPI_EFUSEADDR_REG			0x0374
    174   1.1       ryo 
    175   1.1       ryo #define FW2X_MPI_EFUSEADDR_REG			0x0364
    176   1.1       ryo #define FW2X_MPI_CONTROL_REG			0x0368	/* 64bit */
    177   1.1       ryo #define FW2X_MPI_STATE_REG			0x0370	/* 64bit */
    178   1.1       ryo #define FW_BOOT_EXIT_CODE_REG			0x0388
    179   1.1       ryo #define  RBL_STATUS_DEAD			0x0000dead
    180   1.1       ryo #define  RBL_STATUS_SUCCESS			0x0000abba
    181   1.1       ryo #define  RBL_STATUS_FAILURE			0x00000bad
    182   1.1       ryo #define  RBL_STATUS_HOST_BOOT			0x0000f1a7
    183   1.1       ryo 
    184   1.1       ryo #define AQ_FW_GLB_CPU_SEM_REG(i)		(0x03a0 + (i) * 4)
    185   1.1       ryo #define AQ_FW_SEM_RAM_REG			AQ_FW_GLB_CPU_SEM_REG(2)
    186   1.1       ryo 
    187   1.1       ryo #define AQ_FW_GLB_CTL2_REG			0x0404
    188   1.1       ryo #define  AQ_FW_GLB_CTL2_MCP_UP_FORCE_INTERRUPT	__BIT(1)
    189   1.1       ryo 
    190   1.1       ryo #define AQ_GLB_GENERAL_PROVISIONING9_REG	0x0520
    191   1.1       ryo #define AQ_GLB_NVR_PROVISIONING2_REG		0x0534
    192   1.1       ryo 
    193   1.1       ryo #define FW_MPI_DAISY_CHAIN_STATUS_REG		0x0704
    194   1.1       ryo 
    195   1.1       ryo #define AQ_PCI_REG_CONTROL_6_REG		0x1014
    196   1.1       ryo 
    197   1.1       ryo // msix bitmap */
    198   1.1       ryo #define AQ_INTR_STATUS_REG			0x2000	/* intr status */
    199   1.1       ryo #define AQ_INTR_STATUS_CLR_REG			0x2050	/* intr status clear */
    200   1.1       ryo #define AQ_INTR_MASK_REG			0x2060	/* intr mask set */
    201   1.1       ryo #define AQ_INTR_MASK_CLR_REG			0x2070	/* intr mask clear */
    202   1.1       ryo #define AQ_INTR_AUTOMASK_REG			0x2090
    203   1.1       ryo 
    204   1.1       ryo /* AQ_INTR_IRQ_MAP_TXRX_REG[AQ_RINGS_NUM] 0x2100-0x2140 */
    205   1.1       ryo #define AQ_INTR_IRQ_MAP_TXRX_REG(i)		(0x2100 + ((i) / 2) * 4)
    206   1.1       ryo #define AQ_INTR_IRQ_MAP_TX_REG(i)		AQ_INTR_IRQ_MAP_TXRX_REG(i)
    207   1.1       ryo #define  AQ_INTR_IRQ_MAP_TX_IRQMAP(i)		(__BITS(28,24) >> (((i) & 1)*8))
    208   1.1       ryo #define  AQ_INTR_IRQ_MAP_TX_EN(i)		(__BIT(31)     >> (((i) & 1)*8))
    209   1.1       ryo #define AQ_INTR_IRQ_MAP_RX_REG(i)		AQ_INTR_IRQ_MAP_TXRX_REG(i)
    210   1.1       ryo #define  AQ_INTR_IRQ_MAP_RX_IRQMAP(i)		(__BITS(12,8)  >> (((i) & 1)*8))
    211   1.1       ryo #define  AQ_INTR_IRQ_MAP_RX_EN(i)		(__BIT(15)     >> (((i) & 1)*8))
    212   1.1       ryo 
    213   1.1       ryo /* AQ_GEN_INTR_MAP_REG[AQ_RINGS_NUM] 0x2180-0x2200 */
    214   1.1       ryo #define AQ_GEN_INTR_MAP_REG(i)			(0x2180 + (i) * 4)
    215   1.5   msaitoh #define  AQ_B0_ERR_INT				8U
    216   1.1       ryo 
    217   1.1       ryo #define AQ_INTR_CTRL_REG			0x2300
    218   1.1       ryo #define  AQ_INTR_CTRL_IRQMODE			__BITS(1,0)
    219   1.1       ryo #define  AQ_INTR_CTRL_IRQMODE_LEGACY		0
    220   1.1       ryo #define  AQ_INTR_CTRL_IRQMODE_MSI		1
    221   1.1       ryo #define  AQ_INTR_CTRL_IRQMODE_MSIX		2
    222   1.1       ryo #define  AQ_INTR_CTRL_MULTIVEC			__BIT(2)
    223   1.1       ryo #define  AQ_INTR_CTRL_AUTO_MASK			__BIT(5)
    224   1.1       ryo #define  AQ_INTR_CTRL_CLR_ON_READ		__BIT(7)
    225   1.1       ryo #define  AQ_INTR_CTRL_RESET_DIS			__BIT(29)
    226   1.1       ryo #define  AQ_INTR_CTRL_RESET_IRQ			__BIT(31)
    227   1.1       ryo 
    228   1.1       ryo #define AQ_MBOXIF_POWER_GATING_CONTROL_REG	0x32a8
    229   1.1       ryo 
    230   1.1       ryo #define FW_MPI_RESETCTRL_REG			0x4000
    231   1.1       ryo #define  FW_MPI_RESETCTRL_RESET_DIS		__BIT(29)
    232   1.1       ryo 
    233   1.1       ryo #define RX_SYSCONTROL_REG			0x5000
    234   1.1       ryo #define  RX_SYSCONTROL_RPB_DMA_LOOPBACK		__BIT(6)
    235   1.1       ryo #define  RX_SYSCONTROL_RPF_TPO_LOOPBACK		__BIT(8)
    236   1.1       ryo #define  RX_SYSCONTROL_RESET_DIS		__BIT(29)
    237   1.1       ryo 
    238   1.1       ryo #define RX_TCP_RSS_HASH_REG			0x5040
    239   1.1       ryo #define  RX_TCP_RSS_HASH_RPF2			__BITS(19,16)
    240   1.1       ryo #define  RX_TCP_RSS_HASH_TYPE			__BITS(15,0)
    241   1.1       ryo 
    242   1.1       ryo /* for RPF_*_REG.ACTION */
    243   1.1       ryo #define RPF_ACTION_DISCARD			0
    244   1.1       ryo #define RPF_ACTION_HOST				1
    245   1.1       ryo #define RPF_ACTION_MANAGEMENT			2
    246   1.1       ryo #define RPF_ACTION_HOST_MANAGEMENT		3
    247   1.1       ryo #define RPF_ACTION_WOL				4
    248   1.1       ryo 
    249   1.1       ryo #define RPF_L2BC_REG				0x5100
    250   1.1       ryo #define  RPF_L2BC_EN				__BIT(0)
    251   1.1       ryo #define  RPF_L2BC_PROMISC			__BIT(3)
    252   1.1       ryo #define  RPF_L2BC_ACTION			__BITS(12,14)
    253   1.1       ryo #define  RPF_L2BC_THRESHOLD			__BITS(31,16)
    254   1.1       ryo 
    255   1.1       ryo /* RPF_L2UC_*_REG[34] (actual [38]?) */
    256   1.1       ryo #define RPF_L2UC_LSW_REG(i)			(0x5110 + (i) * 8)
    257   1.1       ryo #define RPF_L2UC_MSW_REG(i)			(0x5114 + (i) * 8)
    258   1.1       ryo #define  RPF_L2UC_MSW_MACADDR_HI		__BITS(15,0)
    259   1.1       ryo #define  RPF_L2UC_MSW_ACTION			__BITS(18,16)
    260   1.1       ryo #define  RPF_L2UC_MSW_EN			__BIT(31)
    261   1.1       ryo #define AQ_HW_MAC_OWN			0	/* index of own address */
    262   1.1       ryo #define AQ_HW_MAC_NUM			34
    263   1.1       ryo 
    264   1.9       ryo /* RPF_MCAST_FILTER_REG[8] 0x5250-0x5270 */
    265   1.1       ryo #define RPF_MCAST_FILTER_REG(i)			(0x5250 + (i) * 4)
    266   1.1       ryo #define  RPF_MCAST_FILTER_EN			__BIT(31)
    267   1.1       ryo #define RPF_MCAST_FILTER_MASK_REG		0x5270
    268   1.1       ryo #define  RPF_MCAST_FILTER_MASK_ALLMULTI		__BIT(14)
    269   1.1       ryo 
    270   1.1       ryo #define RPF_VLAN_MODE_REG			0x5280
    271   1.1       ryo #define  RPF_VLAN_MODE_PROMISC			__BIT(1)
    272   1.1       ryo #define  RPF_VLAN_MODE_ACCEPT_UNTAGGED		__BIT(2)
    273   1.1       ryo #define  RPF_VLAN_MODE_UNTAGGED_ACTION		__BITS(5,3)
    274   1.1       ryo 
    275   1.1       ryo #define RPF_VLAN_TPID_REG			0x5284
    276   1.1       ryo #define  RPF_VLAN_TPID_OUTER			__BITS(31,16)
    277   1.1       ryo #define  RPF_VLAN_TPID_INNER			__BITS(15,0)
    278   1.1       ryo 
    279   1.9       ryo /* RPF_VLAN_FILTER_REG[RPF_VLAN_MAX_FILTERS] 0x5290-0x52d0 */
    280   1.1       ryo #define RPF_VLAN_MAX_FILTERS			16
    281   1.1       ryo #define RPF_VLAN_FILTER_REG(i)			(0x5290 + (i) * 4)
    282   1.1       ryo #define  RPF_VLAN_FILTER_EN			__BIT(31)
    283   1.1       ryo #define  RPF_VLAN_FILTER_RXQ_EN			__BIT(28)
    284   1.1       ryo #define  RPF_VLAN_FILTER_RXQ			__BITS(24,20)
    285   1.1       ryo #define  RPF_VLAN_FILTER_ACTION			__BITS(18,16)
    286   1.1       ryo #define  RPF_VLAN_FILTER_ID			__BITS(11,0)
    287   1.1       ryo 
    288   1.1       ryo /* RPF_ETHERTYPE_FILTER_REG[AQ_RINGS_NUM] 0x5300-0x5380 */
    289   1.1       ryo #define RPF_ETHERTYPE_FILTER_REG(i)		(0x5300 + (i) * 4)
    290   1.1       ryo #define  RPF_ETHERTYPE_FILTER_EN		__BIT(31)
    291   1.1       ryo #define  RPF_ETHERTYPE_FILTER_PRIO_EN		__BIT(30)
    292   1.1       ryo #define  RPF_ETHERTYPE_FILTER_RXQF_EN		__BIT(29)
    293   1.1       ryo #define  RPF_ETHERTYPE_FILTER_PRIO		__BITS(28,26)
    294   1.1       ryo #define  RPF_ETHERTYPE_FILTER_RXQF		__BITS(24,20)
    295   1.1       ryo #define  RPF_ETHERTYPE_FILTER_MNG_RXQF		__BIT(19)
    296   1.1       ryo #define  RPF_ETHERTYPE_FILTER_ACTION		__BITS(18,16)
    297   1.1       ryo #define  RPF_ETHERTYPE_FILTER_VAL		__BITS(15,0)
    298   1.1       ryo 
    299   1.1       ryo /* RPF_L3_FILTER_REG[8] 0x5380-0x53a0 */
    300   1.1       ryo #define RPF_L3_FILTER_REG(i)			(0x5380 + (i) * 4)
    301   1.1       ryo #define  RPF_L3_FILTER_L4_EN			__BIT(31)
    302   1.1       ryo #define  RPF_L3_FILTER_IPV6_EN			__BIT(30)
    303   1.1       ryo #define  RPF_L3_FILTER_SRCADDR_EN		__BIT(29)
    304   1.1       ryo #define  RPF_L3_FILTER_DSTADDR_EN		__BIT(28)
    305   1.1       ryo #define  RPF_L3_FILTER_L4_SRCPORT_EN		__BIT(27)
    306   1.1       ryo #define  RPF_L3_FILTER_L4_DSTPORT_EN		__BIT(26)
    307   1.1       ryo #define  RPF_L3_FILTER_L4_PROTO_EN		__BIT(25)
    308   1.1       ryo #define  RPF_L3_FILTER_ARP_EN			__BIT(24)
    309   1.1       ryo #define  RPF_L3_FILTER_L4_RXQUEUE_EN		__BIT(23)
    310   1.1       ryo #define  RPF_L3_FILTER_L4_RXQUEUE_MANAGEMENT_EN	__BIT(22)
    311   1.1       ryo #define  RPF_L3_FILTER_L4_ACTION		__BITS(16,18)
    312   1.1       ryo #define  RPF_L3_FILTER_L4_RXQUEUE		__BITS(12,8)
    313   1.1       ryo #define  RPF_L3_FILTER_L4_PROTO			__BITS(2,0)
    314   1.1       ryo #define   RPF_L3_FILTER_L4_PROTO_TCP		0
    315   1.1       ryo #define   RPF_L3_FILTER_L4_PROTO_UDP		1
    316   1.1       ryo #define   RPF_L3_FILTER_L4_PROTO_SCTP		2
    317   1.1       ryo #define   RPF_L3_FILTER_L4_PROTO_ICMP		3
    318   1.1       ryo /* parameters of RPF_L3_FILTER_REG[8] */
    319   1.1       ryo #define RPF_L3_FILTER_SRCADDR_REG(i)		(0x53b0 + (i) * 4)
    320   1.1       ryo #define RPF_L3_FILTER_DSTADDR_REG(i)		(0x53d0 + (i) * 4)
    321   1.1       ryo #define RPF_L3_FILTER_L4_SRCPORT_REG(i)		(0x5400 + (i) * 4)
    322   1.1       ryo #define RPF_L3_FILTER_L4_DSTPORT_REG(i)		(0x5420 + (i) * 4)
    323   1.1       ryo 
    324   1.1       ryo #define RX_FLR_RSS_CONTROL1_REG			0x54c0
    325   1.1       ryo #define  RX_FLR_RSS_CONTROL1_EN			__BIT(31)
    326   1.1       ryo 
    327   1.1       ryo #define RPF_RPB_RX_TC_UPT_REG			0x54c4
    328   1.1       ryo #define  RPF_RPB_RX_TC_UPT_MASK(i)		(0x00000007 << ((i) * 4))
    329   1.1       ryo 
    330   1.1       ryo #define RPF_RSS_KEY_ADDR_REG			0x54d0
    331   1.1       ryo #define  RPF_RSS_KEY_ADDR			__BITS(4,0)
    332   1.1       ryo #define  RPF_RSS_KEY_WR_EN			__BIT(5)
    333   1.1       ryo #define RPF_RSS_KEY_WR_DATA_REG			0x54d4
    334   1.1       ryo #define RPF_RSS_KEY_RD_DATA_REG			0x54d8
    335   1.1       ryo 
    336   1.1       ryo #define RPF_RSS_REDIR_ADDR_REG			0x54e0
    337   1.1       ryo #define  RPF_RSS_REDIR_ADDR			__BITS(3,0)
    338   1.1       ryo #define  RPF_RSS_REDIR_WR_EN			__BIT(4)
    339   1.1       ryo 
    340   1.1       ryo #define RPF_RSS_REDIR_WR_DATA_REG		0x54e4
    341   1.1       ryo #define  RPF_RSS_REDIR_WR_DATA			__BITS(15,0)
    342   1.1       ryo 
    343   1.1       ryo #define RPO_HWCSUM_REG				0x5580
    344   1.1       ryo #define  RPO_HWCSUM_IP4CSUM_EN			__BIT(1)
    345   1.1       ryo #define  RPO_HWCSUM_L4CSUM_EN			__BIT(0) /* TCP/UDP/SCTP */
    346   1.1       ryo 
    347   1.1       ryo #define RPO_LRO_ENABLE_REG			0x5590
    348   1.1       ryo 
    349   1.1       ryo #define RPO_LRO_CONF_REG			0x5594
    350   1.1       ryo #define  RPO_LRO_CONF_QSESSION_LIMIT		__BITS(13,12)
    351   1.1       ryo #define  RPO_LRO_CONF_TOTAL_DESC_LIMIT		__BITS(6,5)
    352   1.1       ryo #define  RPO_LRO_CONF_PATCHOPTIMIZATION_EN	__BIT(15)
    353   1.1       ryo #define  RPO_LRO_CONF_MIN_PAYLOAD_OF_FIRST_PKT	__BITS(4,0)
    354   1.1       ryo #define RPO_LRO_RSC_MAX_REG			0x5598
    355   1.1       ryo 
    356   1.1       ryo /* RPO_LRO_LDES_MAX_REG[32/8] 0x55a0-0x55b0 */
    357   1.1       ryo #define RPO_LRO_LDES_MAX_REG(i)			(0x55a0 + (i / 8) * 4)
    358   1.1       ryo #define  RPO_LRO_LDES_MAX_MASK(i)		(0x00000003 << ((i & 7) * 4))
    359   1.1       ryo #define RPO_LRO_TB_DIV_REG			0x5620
    360   1.1       ryo #define  RPO_LRO_TB_DIV				__BITS(20,31)
    361   1.1       ryo #define RPO_LRO_INACTIVE_IVAL_REG		0x5620
    362   1.1       ryo #define  RPO_LRO_INACTIVE_IVAL			__BITS(10,19)
    363   1.1       ryo #define RPO_LRO_MAX_COALESCING_IVAL_REG		0x5620
    364   1.1       ryo #define  RPO_LRO_MAX_COALESCING_IVAL		__BITS(9,0)
    365   1.1       ryo 
    366   1.1       ryo #define RPB_RPF_RX_REG				0x5700
    367   1.1       ryo #define  RPB_RPF_RX_TC_MODE			__BIT(8)
    368   1.1       ryo #define  RPB_RPF_RX_FC_MODE			__BITS(5,4)
    369   1.1       ryo #define  RPB_RPF_RX_BUF_EN			__BIT(0)
    370   1.1       ryo 
    371   1.1       ryo /* RPB_RXB_BUFSIZE_REG[AQ_TRAFFICCLASS_NUM] 0x5710-0x5790 */
    372   1.1       ryo #define RPB_RXB_BUFSIZE_REG(i)			(0x5710 + (i) * 0x10)
    373   1.1       ryo #define  RPB_RXB_BUFSIZE			__BITS(8,0)
    374   1.1       ryo #define RPB_RXB_XOFF_REG(i)			(0x5714 + (i) * 0x10)
    375   1.1       ryo #define  RPB_RXB_XOFF_EN			__BIT(31)
    376   1.1       ryo #define  RPB_RXB_XOFF_THRESH_HI			__BITS(29,16)
    377   1.1       ryo #define  RPB_RXB_XOFF_THRESH_LO			__BITS(13,0)
    378   1.1       ryo 
    379   1.1       ryo #define RX_DMA_DESC_CACHE_INIT_REG		0x5a00
    380   1.1       ryo #define  RX_DMA_DESC_CACHE_INIT			__BIT(0)
    381   1.1       ryo 
    382   1.1       ryo #define RX_DMA_INT_DESC_WRWB_EN_REG		0x05a30
    383   1.1       ryo #define  RX_DMA_INT_DESC_WRWB_EN		__BIT(2)
    384   1.1       ryo #define  RX_DMA_INT_DESC_MODERATE_EN		__BIT(3)
    385   1.1       ryo 
    386   1.1       ryo /* RX_INTR_MODERATION_CTL_REG[AQ_RINGS_NUM] 0x5a40-0x5ac0 */
    387   1.1       ryo #define RX_INTR_MODERATION_CTL_REG(i)		(0x5a40 + (i) * 4)
    388   1.1       ryo #define  RX_INTR_MODERATION_CTL_EN		__BIT(1)
    389   1.1       ryo #define  RX_INTR_MODERATION_CTL_MIN		__BITS(15,8)
    390   1.1       ryo #define  RX_INTR_MODERATION_CTL_MAX		__BITS(24,16)
    391   1.1       ryo 
    392   1.1       ryo /* RX_DMA_DESC_*[AQ_RINGS_NUM] 0x5b00-0x5f00 */
    393   1.1       ryo #define RX_DMA_DESC_BASE_ADDRLSW_REG(i)		(0x5b00 + (i) * 0x20)
    394   1.1       ryo #define RX_DMA_DESC_BASE_ADDRMSW_REG(i)		(0x5b04 + (i) * 0x20)
    395   1.1       ryo #define RX_DMA_DESC_REG(i)			(0x5b08 + (i) * 0x20)
    396   1.1       ryo #define  RX_DMA_DESC_LEN			__BITS(12,3)	/* RXD_NUM/8 */
    397   1.1       ryo #define  RX_DMA_DESC_RESET			__BIT(25)
    398   1.1       ryo #define  RX_DMA_DESC_HEADER_SPLIT		__BIT(28)
    399   1.1       ryo #define  RX_DMA_DESC_VLAN_STRIP			__BIT(29)
    400   1.1       ryo #define  RX_DMA_DESC_EN				__BIT(31)
    401   1.1       ryo #define RX_DMA_DESC_HEAD_PTR_REG(i)		(0x5b0c + (i) * 0x20)
    402   1.1       ryo #define  RX_DMA_DESC_HEAD_PTR			__BITS(12,0)
    403   1.1       ryo #define RX_DMA_DESC_TAIL_PTR_REG(i)		(0x5b10 + (i) * 0x20)
    404   1.1       ryo #define RX_DMA_DESC_BUFSIZE_REG(i)		(0x5b18 + (i) * 0x20)
    405   1.1       ryo #define  RX_DMA_DESC_BUFSIZE_DATA		__BITS(4,0)
    406   1.1       ryo #define  RX_DMA_DESC_BUFSIZE_HDR		__BITS(12,8)
    407   1.1       ryo 
    408   1.1       ryo /* RX_DMA_DCAD_REG[AQ_RINGS_NUM] 0x6100-0x6180 */
    409   1.1       ryo #define RX_DMA_DCAD_REG(i)			(0x6100 + (i) * 4)
    410   1.1       ryo #define  RX_DMA_DCAD_CPUID			__BITS(7,0)
    411   1.1       ryo #define  RX_DMA_DCAD_PAYLOAD_EN			__BIT(29)
    412   1.1       ryo #define  RX_DMA_DCAD_HEADER_EN			__BIT(30)
    413   1.1       ryo #define  RX_DMA_DCAD_DESC_EN			__BIT(31)
    414   1.1       ryo 
    415   1.1       ryo #define RX_DMA_DCA_REG				0x6180
    416   1.1       ryo #define  RX_DMA_DCA_EN				__BIT(31)
    417   1.1       ryo #define  RX_DMA_DCA_MODE			__BITS(3,0)
    418   1.1       ryo 
    419   1.1       ryo /* counters */
    420   1.1       ryo #define RX_DMA_GOOD_PKT_COUNTERLSW		0x6800
    421   1.1       ryo #define RX_DMA_GOOD_OCTET_COUNTERLSW		0x6808
    422   1.1       ryo #define RX_DMA_DROP_PKT_CNT_REG			0x6818
    423   1.1       ryo #define RX_DMA_COALESCED_PKT_CNT_REG		0x6820
    424   1.1       ryo 
    425   1.1       ryo #define TX_SYSCONTROL_REG			0x7000
    426   1.1       ryo #define  TX_SYSCONTROL_TPB_DMA_LOOPBACK		__BIT(6)
    427   1.1       ryo #define  TX_SYSCONTROL_TPO_PKT_LOOPBACK		__BIT(7)
    428   1.1       ryo #define  TX_SYSCONTROL_RESET_DIS		__BIT(29)
    429   1.1       ryo 
    430   1.1       ryo #define TX_TPO2_REG				0x7040
    431   1.1       ryo #define  TX_TPO2_EN				__BIT(16)
    432   1.1       ryo 
    433   1.1       ryo #define TPS_DESC_VM_ARB_MODE_REG		0x7300
    434   1.1       ryo #define  TPS_DESC_VM_ARB_MODE			__BIT(0)
    435   1.1       ryo #define TPS_DESC_RATE_REG			0x7310
    436   1.1       ryo #define  TPS_DESC_RATE_TA_RST			__BIT(31)
    437   1.1       ryo #define  TPS_DESC_RATE_LIM			__BITS(10,0)
    438   1.1       ryo #define TPS_DESC_TC_ARB_MODE_REG		0x7200
    439   1.1       ryo #define  TPS_DESC_TC_ARB_MODE			__BITS(1,0)
    440   1.1       ryo #define TPS_DATA_TC_ARB_MODE_REG		0x7100
    441   1.1       ryo #define  TPS_DATA_TC_ARB_MODE			__BIT(0)
    442   1.1       ryo 
    443   1.1       ryo /* TPS_DATA_TCT_REG[AQ_TRAFFICCLASS_NUM] 0x7110-0x7130 */
    444   1.1       ryo #define TPS_DATA_TCT_REG(i)			(0x7110 + (i) * 4)
    445   1.1       ryo #define  TPS_DATA_TCT_CREDIT_MAX		__BITS(16,27)
    446   1.1       ryo #define  TPS_DATA_TCT_WEIGHT			__BITS(8,0)
    447   1.1       ryo /* TPS_DATA_TCT_REG[AQ_TRAFFICCLASS_NUM] 0x7210-0x7230 */
    448   1.1       ryo #define TPS_DESC_TCT_REG(i)			(0x7210 + (i) * 4)
    449   1.1       ryo #define  TPS_DESC_TCT_CREDIT_MAX		__BITS(16,27)
    450   1.1       ryo #define  TPS_DESC_TCT_WEIGHT			__BITS(8,0)
    451   1.1       ryo 
    452   1.1       ryo #define AQ_HW_TXBUF_MAX		160
    453   1.1       ryo #define AQ_HW_RXBUF_MAX		320
    454   1.1       ryo 
    455   1.1       ryo #define TPO_HWCSUM_REG				0x7800
    456   1.1       ryo #define  TPO_HWCSUM_IP4CSUM_EN			__BIT(1)
    457   1.1       ryo #define  TPO_HWCSUM_L4CSUM_EN			__BIT(0) /* TCP/UDP/SCTP */
    458   1.1       ryo 
    459   1.1       ryo #define TDM_LSO_EN_REG				0x7810
    460   1.1       ryo 
    461   1.1       ryo #define THM_LSO_TCP_FLAG1_REG			0x7820
    462   1.1       ryo #define  THM_LSO_TCP_FLAG1_FIRST		__BITS(11,0)
    463   1.1       ryo #define  THM_LSO_TCP_FLAG1_MID			__BITS(27,16)
    464   1.1       ryo #define THM_LSO_TCP_FLAG2_REG			0x7824
    465   1.1       ryo #define  THM_LSO_TCP_FLAG2_LAST			__BITS(11,0)
    466   1.1       ryo 
    467   1.1       ryo #define TPB_TX_BUF_REG				0x7900
    468   1.1       ryo #define  TPB_TX_BUF_EN				__BIT(0)
    469   1.1       ryo #define  TPB_TX_BUF_SCP_INS_EN			__BIT(2)
    470   1.1       ryo #define  TPB_TX_BUF_TC_MODE_EN			__BIT(8)
    471   1.1       ryo 
    472   1.1       ryo /* TPB_TXB_BUFSIZE_REG[AQ_TRAFFICCLASS_NUM] 0x7910-7990 */
    473   1.1       ryo #define TPB_TXB_BUFSIZE_REG(i)			(0x7910 + (i) * 0x10)
    474   1.1       ryo #define  TPB_TXB_BUFSIZE			__BITS(7,0)
    475   1.1       ryo #define TPB_TXB_THRESH_REG(i)			(0x7914 + (i) * 0x10)
    476   1.1       ryo #define  TPB_TXB_THRESH_HI			__BITS(16,28)
    477   1.1       ryo #define  TPB_TXB_THRESH_LO			__BITS(12,0)
    478   1.1       ryo 
    479   1.1       ryo #define AQ_HW_TX_DMA_TOTAL_REQ_LIMIT_REG	0x7b20
    480   1.1       ryo #define TX_DMA_INT_DESC_WRWB_EN_REG		0x7b40
    481   1.1       ryo #define  TX_DMA_INT_DESC_WRWB_EN		__BIT(1)
    482   1.1       ryo #define  TX_DMA_INT_DESC_MODERATE_EN		__BIT(4)
    483   1.1       ryo 
    484   1.1       ryo /* TX_DMA_DESC_*[AQ_RINGS_NUM] 0x7c00-0x8400 */
    485   1.1       ryo #define TX_DMA_DESC_BASE_ADDRLSW_REG(i)		(0x7c00 + (i) * 0x40)
    486   1.1       ryo #define TX_DMA_DESC_BASE_ADDRMSW_REG(i)		(0x7c04 + (i) * 0x40)
    487   1.1       ryo #define TX_DMA_DESC_REG(i)			(0x7c08 + (i) * 0x40)
    488   1.1       ryo #define  TX_DMA_DESC_LEN			__BITS(12, 3)	/* TXD_NUM/8 */
    489   1.1       ryo #define  TX_DMA_DESC_EN				__BIT(31)
    490   1.1       ryo #define TX_DMA_DESC_HEAD_PTR_REG(i)		(0x7c0c + (i) * 0x40)
    491   1.1       ryo #define  TX_DMA_DESC_HEAD_PTR			__BITS(12,0)
    492   1.1       ryo #define TX_DMA_DESC_TAIL_PTR_REG(i)		(0x7c10 + (i) * 0x40)
    493   1.1       ryo #define TX_DMA_DESC_WRWB_THRESH_REG(i)		(0x7c18 + (i) * 0x40)
    494   1.1       ryo #define  TX_DMA_DESC_WRWB_THRESH		__BITS(14,8)
    495   1.1       ryo 
    496   1.1       ryo /* TDM_DCAD_REG[AQ_RINGS_NUM] 0x8400-0x8480 */
    497   1.1       ryo #define TDM_DCAD_REG(i)				(0x8400 + (i) * 4)
    498   1.1       ryo #define  TDM_DCAD_CPUID				__BITS(7,0)
    499   1.1       ryo #define  TDM_DCAD_CPUID_EN			__BIT(31)
    500   1.1       ryo 
    501   1.1       ryo #define TDM_DCA_REG				0x8480
    502   1.1       ryo #define  TDM_DCA_EN				__BIT(31)
    503   1.1       ryo #define  TDM_DCA_MODE				__BITS(3,0)
    504   1.1       ryo 
    505   1.1       ryo /* TX_INTR_MODERATION_CTL_REG[AQ_RINGS_NUM] 0x8980-0x8a00 */
    506   1.1       ryo #define TX_INTR_MODERATION_CTL_REG(i)		(0x8980 + (i) * 4)
    507   1.1       ryo #define  TX_INTR_MODERATION_CTL_EN		__BIT(1)
    508   1.1       ryo #define  TX_INTR_MODERATION_CTL_MIN		__BITS(15,8)
    509   1.1       ryo #define  TX_INTR_MODERATION_CTL_MAX		__BITS(24,16)
    510   1.1       ryo 
    511   1.1       ryo #define FW1X_CTRL_10G				__BIT(0)
    512   1.1       ryo #define FW1X_CTRL_5G				__BIT(1)
    513   1.1       ryo #define FW1X_CTRL_5GSR				__BIT(2)
    514   1.1       ryo #define FW1X_CTRL_2G5				__BIT(3)
    515   1.1       ryo #define FW1X_CTRL_1G				__BIT(4)
    516   1.1       ryo #define FW1X_CTRL_100M				__BIT(5)
    517   1.1       ryo 
    518   1.1       ryo #define FW2X_CTRL_10BASET_HD			__BIT(0)
    519   1.1       ryo #define FW2X_CTRL_10BASET_FD			__BIT(1)
    520   1.1       ryo #define FW2X_CTRL_100BASETX_HD			__BIT(2)
    521   1.1       ryo #define FW2X_CTRL_100BASET4_HD			__BIT(3)
    522   1.1       ryo #define FW2X_CTRL_100BASET2_HD			__BIT(4)
    523   1.1       ryo #define FW2X_CTRL_100BASETX_FD			__BIT(5)
    524   1.1       ryo #define FW2X_CTRL_100BASET2_FD			__BIT(6)
    525   1.1       ryo #define FW2X_CTRL_1000BASET_HD			__BIT(7)
    526   1.1       ryo #define FW2X_CTRL_1000BASET_FD			__BIT(8)
    527   1.1       ryo #define FW2X_CTRL_2P5GBASET_FD			__BIT(9)
    528   1.1       ryo #define FW2X_CTRL_5GBASET_FD			__BIT(10)
    529   1.1       ryo #define FW2X_CTRL_10GBASET_FD			__BIT(11)
    530   1.1       ryo #define FW2X_CTRL_RESERVED1			__BIT(32)
    531   1.1       ryo #define FW2X_CTRL_10BASET_EEE			__BIT(33)
    532   1.1       ryo #define FW2X_CTRL_RESERVED2			__BIT(34)
    533   1.1       ryo #define FW2X_CTRL_PAUSE				__BIT(35)
    534   1.1       ryo #define FW2X_CTRL_ASYMMETRIC_PAUSE		__BIT(36)
    535   1.1       ryo #define FW2X_CTRL_100BASETX_EEE			__BIT(37)
    536   1.1       ryo #define FW2X_CTRL_RESERVED3			__BIT(38)
    537   1.1       ryo #define FW2X_CTRL_RESERVED4			__BIT(39)
    538   1.1       ryo #define FW2X_CTRL_1000BASET_FD_EEE		__BIT(40)
    539   1.1       ryo #define FW2X_CTRL_2P5GBASET_FD_EEE		__BIT(41)
    540   1.1       ryo #define FW2X_CTRL_5GBASET_FD_EEE		__BIT(42)
    541   1.1       ryo #define FW2X_CTRL_10GBASET_FD_EEE		__BIT(43)
    542   1.1       ryo #define FW2X_CTRL_RESERVED5			__BIT(44)
    543   1.1       ryo #define FW2X_CTRL_RESERVED6			__BIT(45)
    544   1.1       ryo #define FW2X_CTRL_RESERVED7			__BIT(46)
    545   1.1       ryo #define FW2X_CTRL_RESERVED8			__BIT(47)
    546   1.1       ryo #define FW2X_CTRL_RESERVED9			__BIT(48)
    547   1.1       ryo #define FW2X_CTRL_CABLE_DIAG			__BIT(49)
    548   1.1       ryo #define FW2X_CTRL_TEMPERATURE			__BIT(50)
    549   1.1       ryo #define FW2X_CTRL_DOWNSHIFT			__BIT(51)
    550   1.1       ryo #define FW2X_CTRL_PTP_AVB_EN			__BIT(52)
    551   1.1       ryo #define FW2X_CTRL_MEDIA_DETECT			__BIT(53)
    552   1.1       ryo #define FW2X_CTRL_LINK_DROP			__BIT(54)
    553   1.1       ryo #define FW2X_CTRL_SLEEP_PROXY			__BIT(55)
    554   1.1       ryo #define FW2X_CTRL_WOL				__BIT(56)
    555   1.1       ryo #define FW2X_CTRL_MAC_STOP			__BIT(57)
    556   1.1       ryo #define FW2X_CTRL_EXT_LOOPBACK			__BIT(58)
    557   1.1       ryo #define FW2X_CTRL_INT_LOOPBACK			__BIT(59)
    558   1.1       ryo #define FW2X_CTRL_EFUSE_AGENT			__BIT(60)
    559   1.1       ryo #define FW2X_CTRL_WOL_TIMER			__BIT(61)
    560   1.1       ryo #define FW2X_CTRL_STATISTICS			__BIT(62)
    561   1.1       ryo #define FW2X_CTRL_TRANSACTION_ID		__BIT(63)
    562   1.1       ryo 
    563   1.1       ryo #define FW2X_SNPRINTB			\
    564   1.1       ryo 	"\177\020"			\
    565   1.1       ryo 	"b\x23" "PAUSE\0"		\
    566   1.1       ryo 	"b\x24" "ASYMMETRIC-PAUSE\0"	\
    567   1.1       ryo 	"b\x31" "CABLE-DIAG\0"		\
    568   1.1       ryo 	"b\x32" "TEMPERATURE\0"		\
    569   1.1       ryo 	"b\x33" "DOWNSHIFT\0"		\
    570   1.1       ryo 	"b\x34" "PTP-AVB\0"		\
    571   1.1       ryo 	"b\x35" "MEDIA-DETECT\0"	\
    572   1.1       ryo 	"b\x36" "LINK-DROP\0"		\
    573   1.1       ryo 	"b\x37" "SLEEP-PROXY\0"		\
    574   1.1       ryo 	"b\x38" "WOL\0"			\
    575   1.1       ryo 	"b\x39" "MAC-STOP\0"		\
    576   1.1       ryo 	"b\x3a" "EXT-LOOPBACK\0"	\
    577   1.1       ryo 	"b\x3b" "INT-LOOPBACK\0"	\
    578   1.1       ryo 	"b\x3c" "EFUSE-AGENT\0"		\
    579   1.1       ryo 	"b\x3d" "WOL-TIMER\0"		\
    580   1.1       ryo 	"b\x3e" "STATISTICS\0"		\
    581   1.1       ryo 	"b\x3f" "TRANSACTION-ID\0"	\
    582   1.1       ryo 	"\0"
    583   1.1       ryo 
    584   1.1       ryo #define FW2X_CTRL_RATE_100M			FW2X_CTRL_100BASETX_FD
    585   1.1       ryo #define FW2X_CTRL_RATE_1G			FW2X_CTRL_1000BASET_FD
    586   1.1       ryo #define FW2X_CTRL_RATE_2G5			FW2X_CTRL_2P5GBASET_FD
    587   1.1       ryo #define FW2X_CTRL_RATE_5G			FW2X_CTRL_5GBASET_FD
    588   1.1       ryo #define FW2X_CTRL_RATE_10G			FW2X_CTRL_10GBASET_FD
    589   1.1       ryo #define FW2X_CTRL_RATE_MASK		\
    590   1.1       ryo 	(FW2X_CTRL_RATE_100M |		\
    591   1.1       ryo 	 FW2X_CTRL_RATE_1G |		\
    592   1.1       ryo 	 FW2X_CTRL_RATE_2G5 |		\
    593   1.1       ryo 	 FW2X_CTRL_RATE_5G |		\
    594   1.1       ryo 	 FW2X_CTRL_RATE_10G)
    595   1.1       ryo #define FW2X_CTRL_EEE_MASK		\
    596   1.1       ryo 	(FW2X_CTRL_10BASET_EEE |	\
    597   1.1       ryo 	 FW2X_CTRL_100BASETX_EEE |	\
    598   1.1       ryo 	 FW2X_CTRL_1000BASET_FD_EEE |	\
    599   1.1       ryo 	 FW2X_CTRL_2P5GBASET_FD_EEE |	\
    600   1.1       ryo 	 FW2X_CTRL_5GBASET_FD_EEE |	\
    601   1.1       ryo 	 FW2X_CTRL_10GBASET_FD_EEE)
    602   1.1       ryo 
    603   1.1       ryo typedef enum aq_fw_bootloader_mode {
    604   1.1       ryo 	FW_BOOT_MODE_UNKNOWN = 0,
    605   1.1       ryo 	FW_BOOT_MODE_FLB,
    606   1.1       ryo 	FW_BOOT_MODE_RBL_FLASH,
    607   1.1       ryo 	FW_BOOT_MODE_RBL_HOST_BOOTLOAD
    608   1.1       ryo } aq_fw_bootloader_mode_t;
    609   1.1       ryo 
    610   1.1       ryo #define AQ_WRITE_REG(sc, reg, val)				\
    611   1.1       ryo 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
    612   1.1       ryo 
    613   1.1       ryo #define AQ_READ_REG(sc, reg)					\
    614   1.1       ryo 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
    615   1.1       ryo 
    616   1.1       ryo #define AQ_READ64_REG(sc, reg)					\
    617   1.1       ryo 	((uint64_t)AQ_READ_REG(sc, reg) |			\
    618   1.1       ryo 	(((uint64_t)AQ_READ_REG(sc, (reg) + 4)) << 32))
    619   1.1       ryo 
    620   1.1       ryo #define AQ_WRITE64_REG(sc, reg, val)				\
    621   1.1       ryo 	do {							\
    622   1.1       ryo 		AQ_WRITE_REG(sc, reg, (uint32_t)val);		\
    623   1.1       ryo 		AQ_WRITE_REG(sc, reg + 4, (uint32_t)(val >> 32)); \
    624   1.1       ryo 	} while (/* CONSTCOND */0)
    625   1.1       ryo 
    626   1.1       ryo #define AQ_READ_REG_BIT(sc, reg, mask)				\
    627   1.1       ryo 	__SHIFTOUT(AQ_READ_REG(sc, reg), mask)
    628   1.1       ryo 
    629   1.1       ryo #define AQ_WRITE_REG_BIT(sc, reg, mask, val)			\
    630   1.1       ryo 	do {							\
    631   1.1       ryo 		uint32_t _v;					\
    632   1.1       ryo 		_v = AQ_READ_REG((sc), (reg));			\
    633   1.1       ryo 		_v &= ~(mask);					\
    634   1.1       ryo 		if ((val) != 0)					\
    635   1.1       ryo 			_v |= __SHIFTIN((val), (mask));		\
    636   1.1       ryo 		AQ_WRITE_REG((sc), (reg), _v);			\
    637   1.1       ryo 	} while (/* CONSTCOND */ 0)
    638   1.1       ryo 
    639   1.1       ryo #define WAIT_FOR(expr, us, n, errp)				\
    640   1.1       ryo 	do {							\
    641   1.1       ryo 		unsigned int _n;				\
    642   1.1       ryo 		for (_n = n; (!(expr)) && _n != 0; --_n) {	\
    643   1.1       ryo 			delay((us));				\
    644   1.1       ryo 		}						\
    645   1.1       ryo 		if ((errp != NULL)) {				\
    646   1.1       ryo 			if (_n == 0)				\
    647   1.1       ryo 				*(errp) = ETIMEDOUT;		\
    648   1.1       ryo 			else					\
    649   1.1       ryo 				*(errp) = 0;			\
    650   1.1       ryo 		}						\
    651   1.1       ryo 	} while (/* CONSTCOND */ 0)
    652   1.1       ryo 
    653   1.1       ryo #define msec_delay(x)	DELAY(1000 * (x))
    654   1.1       ryo 
    655   1.1       ryo typedef struct aq_mailbox_header {
    656   1.1       ryo 	uint32_t version;
    657   1.1       ryo 	uint32_t transaction_id;
    658   1.1       ryo 	int32_t error;
    659  1.19       ryo } __packed __aligned(4) aq_mailbox_header_t;
    660   1.1       ryo 
    661   1.1       ryo typedef struct aq_hw_stats_s {
    662   1.1       ryo 	uint32_t uprc;
    663   1.1       ryo 	uint32_t mprc;
    664   1.1       ryo 	uint32_t bprc;
    665   1.1       ryo 	uint32_t erpt;
    666   1.1       ryo 	uint32_t uptc;
    667   1.1       ryo 	uint32_t mptc;
    668   1.1       ryo 	uint32_t bptc;
    669   1.1       ryo 	uint32_t erpr;
    670   1.1       ryo 	uint32_t mbtc;
    671   1.1       ryo 	uint32_t bbtc;
    672   1.1       ryo 	uint32_t mbrc;
    673   1.1       ryo 	uint32_t bbrc;
    674   1.1       ryo 	uint32_t ubrc;
    675   1.1       ryo 	uint32_t ubtc;
    676   1.1       ryo 	uint32_t ptc;
    677   1.1       ryo 	uint32_t prc;
    678   1.1       ryo 	uint32_t dpc;	/* not exists in fw2x_msm_statistics */
    679   1.1       ryo 	uint32_t cprc;	/* not exists in fw2x_msm_statistics */
    680  1.19       ryo } __packed __aligned(4) aq_hw_stats_s_t;
    681   1.1       ryo 
    682   1.1       ryo typedef struct fw1x_mailbox {
    683   1.1       ryo 	aq_mailbox_header_t header;
    684   1.1       ryo 	aq_hw_stats_s_t msm;
    685  1.19       ryo } __packed __aligned(4) fw1x_mailbox_t;
    686   1.1       ryo 
    687   1.1       ryo typedef struct fw2x_msm_statistics {
    688   1.1       ryo 	uint32_t uprc;
    689   1.1       ryo 	uint32_t mprc;
    690   1.1       ryo 	uint32_t bprc;
    691   1.1       ryo 	uint32_t erpt;
    692   1.1       ryo 	uint32_t uptc;
    693   1.1       ryo 	uint32_t mptc;
    694   1.1       ryo 	uint32_t bptc;
    695   1.1       ryo 	uint32_t erpr;
    696   1.1       ryo 	uint32_t mbtc;
    697   1.1       ryo 	uint32_t bbtc;
    698   1.1       ryo 	uint32_t mbrc;
    699   1.1       ryo 	uint32_t bbrc;
    700   1.1       ryo 	uint32_t ubrc;
    701   1.1       ryo 	uint32_t ubtc;
    702   1.1       ryo 	uint32_t ptc;
    703   1.1       ryo 	uint32_t prc;
    704  1.19       ryo } __packed __aligned(4) fw2x_msm_statistics_t;
    705   1.1       ryo 
    706   1.1       ryo typedef struct fw2x_phy_cable_diag_data {
    707   1.1       ryo 	uint32_t lane_data[4];
    708  1.19       ryo } __packed __aligned(4) fw2x_phy_cable_diag_data_t;
    709   1.1       ryo 
    710   1.1       ryo typedef struct fw2x_capabilities {
    711   1.1       ryo 	uint32_t caps_lo;
    712   1.1       ryo 	uint32_t caps_hi;
    713  1.19       ryo } __packed __aligned(4) fw2x_capabilities_t;
    714   1.1       ryo 
    715   1.1       ryo typedef struct fw2x_mailbox {		/* struct fwHostInterface */
    716   1.1       ryo 	aq_mailbox_header_t header;
    717   1.1       ryo 	fw2x_msm_statistics_t msm;	/* msmStatistics_t msm; */
    718   1.4       ryo 
    719   1.4       ryo 	uint32_t phy_info1;
    720   1.4       ryo #define PHYINFO1_FAULT_CODE	__BITS(31,16)
    721   1.4       ryo #define PHYINFO1_PHY_H_BIT	__BITS(0,15)
    722   1.4       ryo 	uint32_t phy_info2;
    723   1.4       ryo #define PHYINFO2_TEMPERATURE	__BITS(15,0)
    724   1.4       ryo #define PHYINFO2_CABLE_LEN	__BITS(23,16)
    725   1.4       ryo 
    726   1.1       ryo 	fw2x_phy_cable_diag_data_t diag_data;
    727   1.1       ryo 	uint32_t reserved[8];
    728   1.1       ryo 
    729   1.1       ryo 	fw2x_capabilities_t caps;
    730   1.1       ryo 
    731   1.1       ryo 	/* ... */
    732  1.19       ryo } __packed __aligned(4) fw2x_mailbox_t;
    733   1.1       ryo 
    734   1.1       ryo typedef enum aq_link_speed {
    735   1.1       ryo 	AQ_LINK_NONE	= 0,
    736   1.1       ryo 	AQ_LINK_100M	= (1 << 0),
    737   1.1       ryo 	AQ_LINK_1G	= (1 << 1),
    738   1.1       ryo 	AQ_LINK_2G5	= (1 << 2),
    739   1.1       ryo 	AQ_LINK_5G	= (1 << 3),
    740   1.1       ryo 	AQ_LINK_10G	= (1 << 4)
    741   1.1       ryo } aq_link_speed_t;
    742   1.1       ryo #define AQ_LINK_ALL	(AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | \
    743   1.1       ryo 			 AQ_LINK_5G | AQ_LINK_10G )
    744   1.1       ryo #define AQ_LINK_AUTO	AQ_LINK_ALL
    745   1.1       ryo 
    746   1.1       ryo typedef enum aq_link_fc {
    747   1.1       ryo 	AQ_FC_NONE = 0,
    748   1.1       ryo 	AQ_FC_RX = __BIT(0),
    749   1.1       ryo 	AQ_FC_TX = __BIT(1),
    750   1.1       ryo 	AQ_FC_ALL = (AQ_FC_RX | AQ_FC_TX)
    751   1.1       ryo } aq_link_fc_t;
    752   1.1       ryo 
    753   1.1       ryo typedef enum aq_link_eee {
    754   1.1       ryo 	AQ_EEE_DISABLE = 0,
    755   1.1       ryo 	AQ_EEE_ENABLE = 1
    756   1.1       ryo } aq_link_eee_t;
    757   1.1       ryo 
    758   1.1       ryo typedef enum aq_hw_fw_mpi_state {
    759   1.1       ryo 	MPI_DEINIT	= 0,
    760   1.1       ryo 	MPI_RESET	= 1,
    761   1.1       ryo 	MPI_INIT	= 2,
    762   1.1       ryo 	MPI_POWER	= 4
    763   1.1       ryo } aq_hw_fw_mpi_state_t;
    764   1.1       ryo 
    765   1.1       ryo enum aq_media_type {
    766   1.1       ryo 	AQ_MEDIA_TYPE_UNKNOWN = 0,
    767   1.1       ryo 	AQ_MEDIA_TYPE_FIBRE,
    768   1.1       ryo 	AQ_MEDIA_TYPE_TP
    769   1.1       ryo };
    770   1.1       ryo 
    771   1.1       ryo struct aq_rx_desc_read {
    772   1.1       ryo 	uint64_t buf_addr;
    773   1.1       ryo 	uint64_t hdr_addr;
    774  1.19       ryo } __packed __aligned(8);
    775   1.1       ryo 
    776   1.1       ryo struct aq_rx_desc_wb {
    777   1.1       ryo 	uint32_t type;
    778   1.1       ryo #define RXDESC_TYPE_RSSTYPE		__BITS(3,0)
    779   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_NONE		0
    780   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV4		2
    781   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV6		3
    782   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV4_TCP		4
    783   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV6_TCP		5
    784   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV4_UDP		6
    785   1.1       ryo #define  RXDESC_TYPE_RSSTYPE_IPV6_UDP		7
    786   1.1       ryo #define RXDESC_TYPE_PKTTYPE_ETHER	__BITS(5,4)
    787   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_ETHER_IPV4		0
    788   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_ETHER_IPV6		1
    789   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_ETHER_OTHERS	2
    790   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_ETHER_ARP		3
    791   1.1       ryo #define RXDESC_TYPE_PKTTYPE_PROTO	__BITS(8,6)
    792   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_PROTO_TCP		0
    793   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_PROTO_UDP		1
    794   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_PROTO_SCTP		2
    795   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_PROTO_ICMP		3
    796   1.1       ryo #define  RXDESC_TYPE_PKTTYPE_PROTO_OTHERS	4
    797   1.1       ryo #define RXDESC_TYPE_PKTTYPE_VLAN	__BIT(9)
    798   1.1       ryo #define RXDESC_TYPE_PKTTYPE_VLAN_DOUBLE	__BIT(10)
    799   1.1       ryo #define RXDESC_TYPE_MAC_DMA_ERR		__BIT(12)
    800   1.1       ryo #define RXDESC_TYPE_RESERVED		__BITS(18,13)
    801   1.1       ryo #define RXDESC_TYPE_IPV4_CSUM_CHECKED	__BIT(19)	/* PKTTYPE_ETHER_IPV4 */
    802   1.1       ryo #define RXDESC_TYPE_TCPUDP_CSUM_CHECKED	__BIT(20)
    803   1.1       ryo #define RXDESC_TYPE_SPH			__BIT(21)
    804   1.1       ryo #define RXDESC_TYPE_HDR_LEN		__BITS(31,22)
    805   1.1       ryo 	uint32_t rss_hash;
    806   1.1       ryo 	uint16_t status;
    807   1.1       ryo #define RXDESC_STATUS_DD		__BIT(0)
    808   1.1       ryo #define RXDESC_STATUS_EOP		__BIT(1)
    809   1.1       ryo #define RXDESC_STATUS_MACERR		__BIT(2)
    810   1.1       ryo #define RXDESC_STATUS_IPV4_CSUM_NG	__BIT(3)
    811   1.1       ryo #define RXDESC_STATUS_TCPUDP_CSUM_ERROR	__BIT(4)
    812   1.1       ryo #define RXDESC_STATUS_TCPUDP_CSUM_OK	__BIT(5)
    813   1.1       ryo 
    814   1.1       ryo #define RXDESC_STATUS_STAT		__BITS(2,5)
    815   1.1       ryo #define RXDESC_STATUS_ESTAT		__BITS(6,11)
    816   1.1       ryo #define RXDESC_STATUS_RSC_CNT		__BITS(12,15)
    817   1.1       ryo 	uint16_t pkt_len;
    818   1.1       ryo 	uint16_t next_desc_ptr;
    819   1.1       ryo 	uint16_t vlan;
    820  1.19       ryo } __packed __aligned(4);
    821   1.1       ryo 
    822   1.1       ryo typedef union aq_rx_desc {
    823   1.1       ryo 	struct aq_rx_desc_read read;
    824   1.1       ryo 	struct aq_rx_desc_wb wb;
    825  1.19       ryo } __packed __aligned(8) aq_rx_desc_t;
    826   1.1       ryo 
    827   1.1       ryo typedef struct aq_tx_desc {
    828   1.1       ryo 	uint64_t buf_addr;
    829   1.1       ryo 	uint32_t ctl1;
    830   1.1       ryo #define AQ_TXDESC_CTL1_TYPE_MASK	0x00000003
    831   1.1       ryo #define AQ_TXDESC_CTL1_TYPE_TXD		0x00000001
    832   1.1       ryo #define AQ_TXDESC_CTL1_TYPE_TXC		0x00000002
    833   1.1       ryo #define AQ_TXDESC_CTL1_BLEN		__BITS(19,4)	/* TXD */
    834   1.1       ryo #define AQ_TXDESC_CTL1_DD		__BIT(20)	/* TXD */
    835   1.1       ryo #define AQ_TXDESC_CTL1_EOP		__BIT(21)	/* TXD */
    836   1.1       ryo #define AQ_TXDESC_CTL1_CMD_VLAN		__BIT(22)	/* TXD */
    837   1.1       ryo #define AQ_TXDESC_CTL1_CMD_FCS		__BIT(23)	/* TXD */
    838   1.1       ryo #define AQ_TXDESC_CTL1_CMD_IP4CSUM	__BIT(24)	/* TXD */
    839   1.1       ryo #define AQ_TXDESC_CTL1_CMD_L4CSUM	__BIT(25)	/* TXD */
    840   1.1       ryo #define AQ_TXDESC_CTL1_CMD_LSO		__BIT(26)	/* TXD */
    841   1.1       ryo #define AQ_TXDESC_CTL1_CMD_WB		__BIT(27)	/* TXD */
    842   1.1       ryo #define AQ_TXDESC_CTL1_CMD_VXLAN	__BIT(28)	/* TXD */
    843   1.1       ryo #define AQ_TXDESC_CTL1_VID		__BITS(15,4)	/* TXC */
    844   1.1       ryo #define AQ_TXDESC_CTL1_LSO_IPV6		__BIT(21)	/* TXC */
    845   1.1       ryo #define AQ_TXDESC_CTL1_LSO_TCP		__BIT(22)	/* TXC */
    846   1.1       ryo 	uint32_t ctl2;
    847   1.1       ryo #define AQ_TXDESC_CTL2_LEN		__BITS(31,14)
    848   1.1       ryo #define AQ_TXDESC_CTL2_CTX_EN		__BIT(13)
    849   1.1       ryo #define AQ_TXDESC_CTL2_CTX_IDX		__BIT(12)
    850  1.19       ryo } __packed __aligned(8) aq_tx_desc_t;
    851   1.1       ryo 
    852   1.1       ryo struct aq_txring {
    853   1.1       ryo 	struct aq_softc *txr_sc;
    854   1.1       ryo 	int txr_index;
    855   1.1       ryo 	kmutex_t txr_mutex;
    856   1.1       ryo 	bool txr_active;
    857  1.33     skrll 	bool txr_stopping;
    858  1.33     skrll 	bool txr_sending;
    859  1.33     skrll 	time_t txr_lastsent;
    860   1.1       ryo 
    861   1.1       ryo 	pcq_t *txr_pcq;
    862   1.1       ryo 	void *txr_softint;
    863   1.1       ryo 
    864   1.1       ryo 	aq_tx_desc_t *txr_txdesc;	/* aq_tx_desc_t[AQ_TXD_NUM] */
    865   1.1       ryo 	bus_dmamap_t txr_txdesc_dmamap;
    866   1.1       ryo 	bus_dma_segment_t txr_txdesc_seg[1];
    867   1.1       ryo 	bus_size_t txr_txdesc_size;
    868   1.1       ryo 
    869   1.1       ryo 	struct {
    870   1.1       ryo 		struct mbuf *m;
    871   1.1       ryo 		bus_dmamap_t dmamap;
    872   1.1       ryo 	} txr_mbufs[AQ_TXD_NUM];
    873   1.1       ryo 	unsigned int txr_prodidx;
    874   1.1       ryo 	unsigned int txr_considx;
    875   1.1       ryo 	int txr_nfree;
    876   1.1       ryo };
    877   1.1       ryo 
    878   1.1       ryo struct aq_rxring {
    879   1.1       ryo 	struct aq_softc *rxr_sc;
    880   1.1       ryo 	int rxr_index;
    881   1.1       ryo 	kmutex_t rxr_mutex;
    882   1.1       ryo 	bool rxr_active;
    883  1.28       ryo 	bool rxr_discarding;
    884  1.33     skrll 	bool rxr_stopping;
    885  1.28       ryo 	struct mbuf *rxr_receiving_m;		/* receiving jumboframe */
    886  1.28       ryo 	struct mbuf *rxr_receiving_m_last;	/* last mbuf of jumboframe */
    887   1.1       ryo 
    888   1.1       ryo 	aq_rx_desc_t *rxr_rxdesc;	/* aq_rx_desc_t[AQ_RXD_NUM] */
    889   1.1       ryo 	bus_dmamap_t rxr_rxdesc_dmamap;
    890   1.1       ryo 	bus_dma_segment_t rxr_rxdesc_seg[1];
    891   1.1       ryo 	bus_size_t rxr_rxdesc_size;
    892   1.1       ryo 	struct {
    893   1.1       ryo 		struct mbuf *m;
    894   1.1       ryo 		bus_dmamap_t dmamap;
    895   1.1       ryo 	} rxr_mbufs[AQ_RXD_NUM];
    896   1.1       ryo 	unsigned int rxr_readidx;
    897   1.1       ryo };
    898   1.1       ryo 
    899   1.1       ryo struct aq_queue {
    900   1.1       ryo 	struct aq_softc *sc;
    901   1.1       ryo 	struct aq_txring txring;
    902   1.1       ryo 	struct aq_rxring rxring;
    903   1.1       ryo };
    904   1.1       ryo 
    905   1.1       ryo struct aq_softc;
    906   1.1       ryo struct aq_firmware_ops {
    907   1.1       ryo 	int (*reset)(struct aq_softc *);
    908   1.1       ryo 	int (*set_mode)(struct aq_softc *, aq_hw_fw_mpi_state_t,
    909   1.1       ryo 	    aq_link_speed_t, aq_link_fc_t, aq_link_eee_t);
    910   1.1       ryo 	int (*get_mode)(struct aq_softc *, aq_hw_fw_mpi_state_t *,
    911   1.1       ryo 	    aq_link_speed_t *, aq_link_fc_t *, aq_link_eee_t *);
    912   1.1       ryo 	int (*get_stats)(struct aq_softc *, aq_hw_stats_s_t *);
    913   1.4       ryo #if NSYSMON_ENVSYS > 0
    914   1.4       ryo 	int (*get_temperature)(struct aq_softc *, uint32_t *);
    915   1.4       ryo #endif
    916   1.1       ryo };
    917   1.1       ryo 
    918   1.1       ryo #ifdef AQ_EVENT_COUNTERS
    919   1.1       ryo #define AQ_EVCNT_DECL(name)						\
    920   1.1       ryo 	char sc_evcount_##name##_name[32];				\
    921   1.1       ryo 	struct evcnt sc_evcount_##name##_ev;
    922   1.1       ryo #define AQ_EVCNT_ATTACH(sc, name, desc, evtype)				\
    923   1.1       ryo 	do {								\
    924   1.1       ryo 		snprintf((sc)->sc_evcount_##name##_name,		\
    925   1.1       ryo 		    sizeof((sc)->sc_evcount_##name##_name),		\
    926   1.1       ryo 		    "%s", desc);					\
    927   1.1       ryo 		evcnt_attach_dynamic(&(sc)->sc_evcount_##name##_ev,	\
    928   1.1       ryo 		    (evtype), NULL, device_xname((sc)->sc_dev),		\
    929   1.1       ryo 		    (sc)->sc_evcount_##name##_name);			\
    930   1.1       ryo 	} while (/*CONSTCOND*/0)
    931   1.1       ryo #define AQ_EVCNT_ATTACH_MISC(sc, name, desc)				\
    932   1.1       ryo 	AQ_EVCNT_ATTACH(sc, name, desc, EVCNT_TYPE_MISC)
    933   1.1       ryo #define AQ_EVCNT_DETACH(sc, name)					\
    934  1.41       ryo 	if ((sc)->sc_evcount_##name##_name[0] != '\0')			\
    935  1.41       ryo 		evcnt_detach(&(sc)->sc_evcount_##name##_ev)
    936   1.1       ryo #define AQ_EVCNT_ADD(sc, name, val)					\
    937   1.1       ryo 	((sc)->sc_evcount_##name##_ev.ev_count += (val))
    938   1.1       ryo #endif /* AQ_EVENT_COUNTERS */
    939   1.1       ryo 
    940   1.1       ryo #define AQ_LOCK(sc)		mutex_enter(&(sc)->sc_mutex);
    941   1.1       ryo #define AQ_UNLOCK(sc)		mutex_exit(&(sc)->sc_mutex);
    942  1.33     skrll #define AQ_LOCKED(sc)		KASSERT(mutex_owned(&(sc)->sc_mutex));
    943   1.1       ryo 
    944   1.4       ryo /* lock for FW2X_MPI_{CONTROL,STATE]_REG read-modify-write */
    945   1.4       ryo #define AQ_MPI_LOCK(sc)		mutex_enter(&(sc)->sc_mpi_mutex);
    946   1.4       ryo #define AQ_MPI_UNLOCK(sc)	mutex_exit(&(sc)->sc_mpi_mutex);
    947  1.33     skrll #define AQ_MPI_LOCKED(sc)	KASSERT(mutex_owned(&(sc)->sc_mpi_mutex));
    948   1.4       ryo 
    949   1.4       ryo 
    950   1.1       ryo struct aq_softc {
    951   1.1       ryo 	device_t sc_dev;
    952   1.1       ryo 
    953   1.1       ryo 	bus_space_tag_t sc_iot;
    954   1.1       ryo 	bus_space_handle_t sc_ioh;
    955   1.1       ryo 	bus_size_t sc_iosize;
    956  1.17   msaitoh 	bus_dma_tag_t sc_dmat;
    957   1.1       ryo 
    958   1.1       ryo 	void *sc_ihs[AQ_NINTR_MAX];
    959   1.1       ryo 	pci_intr_handle_t *sc_intrs;
    960   1.1       ryo 
    961   1.1       ryo 	int sc_tx_irq[AQ_RSSQUEUE_MAX];
    962   1.1       ryo 	int sc_rx_irq[AQ_RSSQUEUE_MAX];
    963   1.1       ryo 	int sc_linkstat_irq;
    964   1.1       ryo 	bool sc_use_txrx_independent_intr;
    965   1.1       ryo 	bool sc_poll_linkstat;
    966   1.1       ryo 	bool sc_detect_linkstat;
    967   1.1       ryo 
    968   1.4       ryo #if NSYSMON_ENVSYS > 0
    969   1.4       ryo 	struct sysmon_envsys *sc_sme;
    970   1.4       ryo 	envsys_data_t sc_sensor_temp;
    971   1.4       ryo #endif
    972   1.4       ryo 
    973   1.1       ryo 	callout_t sc_tick_ch;
    974   1.1       ryo 
    975   1.1       ryo 	int sc_nintrs;
    976   1.1       ryo 	bool sc_msix;
    977   1.1       ryo 
    978   1.1       ryo 	struct aq_queue sc_queue[AQ_RSSQUEUE_MAX];
    979   1.1       ryo 	int sc_nqueues;
    980   1.1       ryo 
    981   1.1       ryo 	pci_chipset_tag_t sc_pc;
    982   1.1       ryo 	pcitag_t sc_pcitag;
    983   1.1       ryo 	uint16_t sc_product;
    984   1.1       ryo 	uint16_t sc_revision;
    985   1.1       ryo 
    986   1.1       ryo 	kmutex_t sc_mutex;
    987   1.4       ryo 	kmutex_t sc_mpi_mutex;
    988   1.1       ryo 
    989   1.8      maxv 	const struct aq_firmware_ops *sc_fw_ops;
    990   1.1       ryo 	uint64_t sc_fw_caps;
    991   1.1       ryo 	enum aq_media_type sc_media_type;
    992   1.1       ryo 	aq_link_speed_t sc_available_rates;
    993   1.1       ryo 
    994   1.1       ryo 	aq_link_speed_t sc_link_rate;
    995   1.1       ryo 	aq_link_fc_t sc_link_fc;
    996   1.1       ryo 	aq_link_eee_t sc_link_eee;
    997   1.1       ryo 
    998   1.1       ryo 	uint32_t sc_fw_version;
    999   1.1       ryo #define FW_VERSION_MAJOR(sc)	(((sc)->sc_fw_version >> 24) & 0xff)
   1000   1.1       ryo #define FW_VERSION_MINOR(sc)	(((sc)->sc_fw_version >> 16) & 0xff)
   1001   1.1       ryo #define FW_VERSION_BUILD(sc)	((sc)->sc_fw_version & 0xffff)
   1002   1.1       ryo 	uint32_t sc_features;
   1003   1.1       ryo #define FEATURES_MIPS		0x00000001
   1004   1.1       ryo #define FEATURES_TPO2		0x00000002
   1005   1.1       ryo #define FEATURES_RPF2		0x00000004
   1006   1.1       ryo #define FEATURES_MPI_AQ		0x00000008
   1007   1.1       ryo #define FEATURES_REV_A0		0x10000000
   1008   1.1       ryo #define FEATURES_REV_A		(FEATURES_REV_A0)
   1009   1.1       ryo #define FEATURES_REV_B0		0x20000000
   1010   1.1       ryo #define FEATURES_REV_B1		0x40000000
   1011   1.1       ryo #define FEATURES_REV_B		(FEATURES_REV_B0|FEATURES_REV_B1)
   1012  1.40       ryo 	int sc_max_mtu;
   1013   1.1       ryo 	uint32_t sc_mbox_addr;
   1014   1.1       ryo 
   1015   1.1       ryo 	bool sc_rbl_enabled;
   1016   1.1       ryo 	bool sc_fast_start_enabled;
   1017   1.1       ryo 	bool sc_flash_present;
   1018   1.1       ryo 
   1019   1.1       ryo 	bool sc_intr_moderation_enable;
   1020   1.1       ryo 	bool sc_rss_enable;
   1021   1.1       ryo 
   1022   1.1       ryo 	struct ethercom sc_ethercom;
   1023   1.1       ryo 	struct ether_addr sc_enaddr;
   1024   1.1       ryo 	struct ifmedia sc_media;
   1025   1.1       ryo 	int sc_ec_capenable;		/* last ec_capenable */
   1026   1.1       ryo 	unsigned short sc_if_flags;	/* last if_flags */
   1027   1.1       ryo 
   1028  1.33     skrll 	bool sc_tx_sending;
   1029  1.33     skrll 	bool sc_stopping;
   1030  1.33     skrll 
   1031  1.33     skrll 	struct workqueue *sc_reset_wq;
   1032  1.33     skrll 	struct work sc_reset_work;
   1033  1.33     skrll 	volatile unsigned sc_reset_pending;
   1034  1.33     skrll 
   1035  1.33     skrll 	bool sc_trigger_reset;
   1036  1.33     skrll 
   1037   1.1       ryo #ifdef AQ_EVENT_COUNTERS
   1038   1.1       ryo 	aq_hw_stats_s_t sc_statistics[2];
   1039   1.1       ryo 	int sc_statistics_idx;
   1040   1.1       ryo 	bool sc_poll_statistics;
   1041   1.1       ryo 
   1042   1.1       ryo 	AQ_EVCNT_DECL(uprc);
   1043   1.1       ryo 	AQ_EVCNT_DECL(mprc);
   1044   1.1       ryo 	AQ_EVCNT_DECL(bprc);
   1045   1.1       ryo 	AQ_EVCNT_DECL(erpt);
   1046   1.1       ryo 	AQ_EVCNT_DECL(uptc);
   1047   1.1       ryo 	AQ_EVCNT_DECL(mptc);
   1048   1.1       ryo 	AQ_EVCNT_DECL(bptc);
   1049   1.1       ryo 	AQ_EVCNT_DECL(erpr);
   1050   1.1       ryo 	AQ_EVCNT_DECL(mbtc);
   1051   1.1       ryo 	AQ_EVCNT_DECL(bbtc);
   1052   1.1       ryo 	AQ_EVCNT_DECL(mbrc);
   1053   1.1       ryo 	AQ_EVCNT_DECL(bbrc);
   1054   1.1       ryo 	AQ_EVCNT_DECL(ubrc);
   1055   1.1       ryo 	AQ_EVCNT_DECL(ubtc);
   1056   1.1       ryo 	AQ_EVCNT_DECL(ptc);
   1057   1.1       ryo 	AQ_EVCNT_DECL(prc);
   1058   1.1       ryo 	AQ_EVCNT_DECL(dpc);
   1059   1.1       ryo 	AQ_EVCNT_DECL(cprc);
   1060   1.1       ryo #endif
   1061   1.1       ryo };
   1062   1.1       ryo 
   1063   1.1       ryo static int aq_match(device_t, cfdata_t, void *);
   1064   1.1       ryo static void aq_attach(device_t, device_t, void *);
   1065   1.1       ryo static int aq_detach(device_t, int);
   1066   1.1       ryo 
   1067   1.1       ryo static int aq_setup_msix(struct aq_softc *, struct pci_attach_args *, int,
   1068   1.1       ryo     bool, bool);
   1069   1.1       ryo static int aq_setup_legacy(struct aq_softc *, struct pci_attach_args *,
   1070   1.1       ryo     pci_intr_type_t);
   1071   1.1       ryo static int aq_establish_msix_intr(struct aq_softc *, bool, bool);
   1072   1.1       ryo 
   1073   1.1       ryo static int aq_ifmedia_change(struct ifnet * const);
   1074   1.1       ryo static void aq_ifmedia_status(struct ifnet * const, struct ifmediareq *);
   1075  1.10       ryo static int aq_vlan_cb(struct ethercom *ec, uint16_t vid, bool set);
   1076   1.1       ryo static int aq_ifflags_cb(struct ethercom *);
   1077   1.1       ryo static int aq_init(struct ifnet *);
   1078  1.33     skrll static int aq_init_locked(struct ifnet *);
   1079   1.1       ryo static void aq_send_common_locked(struct ifnet *, struct aq_softc *,
   1080   1.1       ryo     struct aq_txring *, bool);
   1081   1.1       ryo static int aq_transmit(struct ifnet *, struct mbuf *);
   1082   1.1       ryo static void aq_deferred_transmit(void *);
   1083   1.1       ryo static void aq_start(struct ifnet *);
   1084   1.1       ryo static void aq_stop(struct ifnet *, int);
   1085  1.33     skrll static void aq_stop_locked(struct ifnet *, bool);
   1086   1.1       ryo static int aq_ioctl(struct ifnet *, unsigned long, void *);
   1087   1.1       ryo 
   1088   1.1       ryo static int aq_txrx_rings_alloc(struct aq_softc *);
   1089   1.1       ryo static void aq_txrx_rings_free(struct aq_softc *);
   1090   1.1       ryo static int aq_tx_pcq_alloc(struct aq_softc *, struct aq_txring *);
   1091   1.1       ryo static void aq_tx_pcq_free(struct aq_softc *, struct aq_txring *);
   1092   1.1       ryo 
   1093   1.1       ryo static void aq_initmedia(struct aq_softc *);
   1094   1.1       ryo static void aq_enable_intr(struct aq_softc *, bool, bool);
   1095   1.1       ryo 
   1096  1.33     skrll static void aq_handle_reset_work(struct work *, void *);
   1097  1.33     skrll static void aq_unset_stopping_flags(struct aq_softc *);
   1098  1.33     skrll static void aq_set_stopping_flags(struct aq_softc *);
   1099  1.33     skrll 
   1100   1.4       ryo #if NSYSMON_ENVSYS > 0
   1101   1.4       ryo static void aq_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
   1102   1.4       ryo #endif
   1103   1.1       ryo static void aq_tick(void *);
   1104   1.1       ryo static int aq_legacy_intr(void *);
   1105   1.1       ryo static int aq_link_intr(void *);
   1106   1.1       ryo static int aq_txrx_intr(void *);
   1107   1.1       ryo static int aq_tx_intr(void *);
   1108   1.1       ryo static int aq_rx_intr(void *);
   1109   1.1       ryo 
   1110   1.1       ryo static int aq_set_linkmode(struct aq_softc *, aq_link_speed_t, aq_link_fc_t,
   1111   1.1       ryo     aq_link_eee_t);
   1112   1.1       ryo static int aq_get_linkmode(struct aq_softc *, aq_link_speed_t *, aq_link_fc_t *,
   1113   1.1       ryo     aq_link_eee_t *);
   1114   1.1       ryo 
   1115   1.1       ryo static int aq_fw_reset(struct aq_softc *);
   1116   1.1       ryo static int aq_fw_version_init(struct aq_softc *);
   1117   1.1       ryo static int aq_hw_init(struct aq_softc *);
   1118   1.1       ryo static int aq_hw_init_ucp(struct aq_softc *);
   1119   1.1       ryo static int aq_hw_reset(struct aq_softc *);
   1120   1.1       ryo static int aq_fw_downld_dwords(struct aq_softc *, uint32_t, uint32_t *,
   1121   1.1       ryo     uint32_t);
   1122   1.1       ryo static int aq_get_mac_addr(struct aq_softc *);
   1123   1.1       ryo static int aq_init_rss(struct aq_softc *);
   1124   1.1       ryo static int aq_set_capability(struct aq_softc *);
   1125   1.1       ryo 
   1126   1.1       ryo static int fw1x_reset(struct aq_softc *);
   1127   1.1       ryo static int fw1x_set_mode(struct aq_softc *, aq_hw_fw_mpi_state_t,
   1128   1.1       ryo     aq_link_speed_t, aq_link_fc_t, aq_link_eee_t);
   1129   1.1       ryo static int fw1x_get_mode(struct aq_softc *, aq_hw_fw_mpi_state_t *,
   1130   1.1       ryo     aq_link_speed_t *, aq_link_fc_t *, aq_link_eee_t *);
   1131   1.1       ryo static int fw1x_get_stats(struct aq_softc *, aq_hw_stats_s_t *);
   1132   1.1       ryo 
   1133   1.1       ryo static int fw2x_reset(struct aq_softc *);
   1134   1.1       ryo static int fw2x_set_mode(struct aq_softc *, aq_hw_fw_mpi_state_t,
   1135   1.1       ryo     aq_link_speed_t, aq_link_fc_t, aq_link_eee_t);
   1136   1.1       ryo static int fw2x_get_mode(struct aq_softc *, aq_hw_fw_mpi_state_t *,
   1137   1.1       ryo     aq_link_speed_t *, aq_link_fc_t *, aq_link_eee_t *);
   1138   1.1       ryo static int fw2x_get_stats(struct aq_softc *, aq_hw_stats_s_t *);
   1139   1.4       ryo #if NSYSMON_ENVSYS > 0
   1140   1.4       ryo static int fw2x_get_temperature(struct aq_softc *, uint32_t *);
   1141   1.4       ryo #endif
   1142   1.1       ryo 
   1143  1.33     skrll #ifndef AQ_WATCHDOG_TIMEOUT
   1144  1.33     skrll #define AQ_WATCHDOG_TIMEOUT 5
   1145  1.33     skrll #endif
   1146  1.33     skrll static int aq_watchdog_timeout = AQ_WATCHDOG_TIMEOUT;
   1147  1.33     skrll 
   1148  1.33     skrll 
   1149   1.8      maxv static const struct aq_firmware_ops aq_fw1x_ops = {
   1150   1.1       ryo 	.reset = fw1x_reset,
   1151   1.1       ryo 	.set_mode = fw1x_set_mode,
   1152   1.1       ryo 	.get_mode = fw1x_get_mode,
   1153   1.4       ryo 	.get_stats = fw1x_get_stats,
   1154   1.4       ryo #if NSYSMON_ENVSYS > 0
   1155   1.4       ryo 	.get_temperature = NULL
   1156   1.4       ryo #endif
   1157   1.1       ryo };
   1158   1.1       ryo 
   1159   1.8      maxv static const struct aq_firmware_ops aq_fw2x_ops = {
   1160   1.1       ryo 	.reset = fw2x_reset,
   1161   1.1       ryo 	.set_mode = fw2x_set_mode,
   1162   1.1       ryo 	.get_mode = fw2x_get_mode,
   1163   1.4       ryo 	.get_stats = fw2x_get_stats,
   1164   1.4       ryo #if NSYSMON_ENVSYS > 0
   1165   1.4       ryo 	.get_temperature = fw2x_get_temperature
   1166   1.4       ryo #endif
   1167   1.1       ryo };
   1168   1.1       ryo 
   1169   1.1       ryo CFATTACH_DECL3_NEW(aq, sizeof(struct aq_softc),
   1170   1.1       ryo     aq_match, aq_attach, aq_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
   1171   1.1       ryo 
   1172   1.1       ryo static const struct aq_product {
   1173   1.1       ryo 	pci_vendor_id_t aq_vendor;
   1174   1.1       ryo 	pci_product_id_t aq_product;
   1175   1.1       ryo 	const char *aq_name;
   1176   1.1       ryo 	enum aq_media_type aq_media_type;
   1177   1.1       ryo 	aq_link_speed_t aq_available_rates;
   1178   1.1       ryo } aq_products[] = {
   1179  1.14       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC100,
   1180  1.14       ryo 	  "Aquantia AQC100 10 Gigabit Network Adapter",
   1181  1.14       ryo 	  AQ_MEDIA_TYPE_FIBRE, AQ_LINK_ALL
   1182  1.14       ryo 	},
   1183   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC107,
   1184   1.1       ryo 	  "Aquantia AQC107 10 Gigabit Network Adapter",
   1185   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_ALL
   1186   1.1       ryo 	},
   1187   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC108,
   1188   1.1       ryo 	  "Aquantia AQC108 5 Gigabit Network Adapter",
   1189   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | AQ_LINK_5G
   1190   1.1       ryo 	},
   1191   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC109,
   1192   1.1       ryo 	  "Aquantia AQC109 2.5 Gigabit Network Adapter",
   1193   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5
   1194   1.1       ryo 	},
   1195   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC111,
   1196   1.1       ryo 	  "Aquantia AQC111 5 Gigabit Network Adapter",
   1197   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | AQ_LINK_5G
   1198   1.1       ryo 	},
   1199   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC112,
   1200   1.1       ryo 	  "Aquantia AQC112 2.5 Gigabit Network Adapter",
   1201   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5
   1202   1.1       ryo 	},
   1203  1.15       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC100S,
   1204  1.16       ryo 	  "Aquantia AQC100S 10 Gigabit Network Adapter",
   1205  1.15       ryo 	  AQ_MEDIA_TYPE_FIBRE, AQ_LINK_ALL
   1206  1.15       ryo 	},
   1207   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC107S,
   1208   1.1       ryo 	  "Aquantia AQC107S 10 Gigabit Network Adapter",
   1209   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_ALL
   1210   1.1       ryo 	},
   1211   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC108S,
   1212   1.1       ryo 	  "Aquantia AQC108S 5 Gigabit Network Adapter",
   1213   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | AQ_LINK_5G
   1214   1.1       ryo 	},
   1215   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC109S,
   1216   1.1       ryo 	  "Aquantia AQC109S 2.5 Gigabit Network Adapter",
   1217   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5
   1218   1.1       ryo 	},
   1219   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC111S,
   1220   1.1       ryo 	  "Aquantia AQC111S 5 Gigabit Network Adapter",
   1221   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | AQ_LINK_5G
   1222   1.1       ryo 	},
   1223   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_AQC112S,
   1224   1.1       ryo 	  "Aquantia AQC112S 2.5 Gigabit Network Adapter",
   1225   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5
   1226   1.1       ryo 	},
   1227  1.15       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_D100,
   1228  1.15       ryo 	  "Aquantia D100 10 Gigabit Network Adapter",
   1229  1.15       ryo 	  AQ_MEDIA_TYPE_FIBRE, AQ_LINK_ALL
   1230  1.15       ryo 	},
   1231   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_D107,
   1232   1.1       ryo 	  "Aquantia D107 10 Gigabit Network Adapter",
   1233   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_ALL
   1234   1.1       ryo 	},
   1235   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_D108,
   1236   1.1       ryo 	  "Aquantia D108 5 Gigabit Network Adapter",
   1237   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5 | AQ_LINK_5G
   1238   1.1       ryo 	},
   1239   1.1       ryo 	{ PCI_VENDOR_AQUANTIA, PCI_PRODUCT_AQUANTIA_D109,
   1240   1.1       ryo 	  "Aquantia D109 2.5 Gigabit Network Adapter",
   1241   1.1       ryo 	  AQ_MEDIA_TYPE_TP, AQ_LINK_100M | AQ_LINK_1G | AQ_LINK_2G5
   1242   1.1       ryo 	}
   1243   1.1       ryo };
   1244   1.1       ryo 
   1245   1.1       ryo static const struct aq_product *
   1246   1.1       ryo aq_lookup(const struct pci_attach_args *pa)
   1247   1.1       ryo {
   1248   1.1       ryo 	unsigned int i;
   1249   1.1       ryo 
   1250   1.1       ryo 	for (i = 0; i < __arraycount(aq_products); i++) {
   1251   1.1       ryo 		if (PCI_VENDOR(pa->pa_id)  == aq_products[i].aq_vendor &&
   1252   1.1       ryo 		    PCI_PRODUCT(pa->pa_id) == aq_products[i].aq_product)
   1253   1.1       ryo 			return &aq_products[i];
   1254   1.1       ryo 	}
   1255   1.1       ryo 	return NULL;
   1256   1.1       ryo }
   1257   1.1       ryo 
   1258   1.1       ryo static int
   1259   1.1       ryo aq_match(device_t parent, cfdata_t cf, void *aux)
   1260   1.1       ryo {
   1261  1.32     skrll 	struct pci_attach_args * const pa = aux;
   1262   1.1       ryo 
   1263   1.1       ryo 	if (aq_lookup(pa) != NULL)
   1264   1.1       ryo 		return 1;
   1265   1.1       ryo 
   1266   1.1       ryo 	return 0;
   1267   1.1       ryo }
   1268   1.1       ryo 
   1269   1.1       ryo static void
   1270   1.1       ryo aq_attach(device_t parent, device_t self, void *aux)
   1271   1.1       ryo {
   1272  1.32     skrll 	struct aq_softc * const sc = device_private(self);
   1273  1.32     skrll 	struct pci_attach_args * const pa = aux;
   1274  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   1275   1.1       ryo 	pci_chipset_tag_t pc;
   1276   1.1       ryo 	pcitag_t tag;
   1277   1.1       ryo 	pcireg_t command, memtype, bar;
   1278   1.1       ryo 	const struct aq_product *aqp;
   1279   1.1       ryo 	int error;
   1280   1.1       ryo 
   1281   1.1       ryo 	sc->sc_dev = self;
   1282   1.1       ryo 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NET);
   1283   1.4       ryo 	mutex_init(&sc->sc_mpi_mutex, MUTEX_DEFAULT, IPL_NET);
   1284   1.1       ryo 
   1285   1.1       ryo 	sc->sc_pc = pc = pa->pa_pc;
   1286   1.1       ryo 	sc->sc_pcitag = tag = pa->pa_tag;
   1287   1.1       ryo 	sc->sc_dmat = pci_dma64_available(pa) ? pa->pa_dmat64 : pa->pa_dmat;
   1288   1.1       ryo 
   1289   1.1       ryo 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1290   1.1       ryo 	command |= PCI_COMMAND_MASTER_ENABLE;
   1291   1.1       ryo 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
   1292   1.1       ryo 
   1293   1.1       ryo 	sc->sc_product = PCI_PRODUCT(pa->pa_id);
   1294   1.1       ryo 	sc->sc_revision = PCI_REVISION(pa->pa_class);
   1295   1.1       ryo 
   1296   1.1       ryo 	aqp = aq_lookup(pa);
   1297   1.1       ryo 	KASSERT(aqp != NULL);
   1298   1.1       ryo 
   1299   1.1       ryo 	pci_aprint_devinfo_fancy(pa, "Ethernet controller", aqp->aq_name, 1);
   1300   1.1       ryo 
   1301   1.1       ryo 	bar = pci_conf_read(pc, tag, PCI_BAR(0));
   1302   1.1       ryo 	if ((PCI_MAPREG_MEM_ADDR(bar) == 0) ||
   1303   1.1       ryo 	    (PCI_MAPREG_TYPE(bar) != PCI_MAPREG_TYPE_MEM)) {
   1304   1.1       ryo 		aprint_error_dev(sc->sc_dev, "wrong BAR type\n");
   1305   1.1       ryo 		return;
   1306   1.1       ryo 	}
   1307   1.1       ryo 	memtype = pci_mapreg_type(pc, tag, PCI_BAR(0));
   1308   1.1       ryo 	if (pci_mapreg_map(pa, PCI_BAR(0), memtype, 0, &sc->sc_iot, &sc->sc_ioh,
   1309   1.1       ryo 	    NULL, &sc->sc_iosize) != 0) {
   1310   1.1       ryo 		aprint_error_dev(sc->sc_dev, "unable to map register\n");
   1311   1.1       ryo 		return;
   1312   1.1       ryo 	}
   1313   1.1       ryo 
   1314  1.31       ryo 	error = aq_fw_reset(sc);
   1315  1.31       ryo 	if (error != 0)
   1316  1.31       ryo 		goto attach_failure;
   1317  1.31       ryo 
   1318   1.1       ryo 	sc->sc_nqueues = MIN(ncpu, AQ_RSSQUEUE_MAX);
   1319   1.1       ryo 
   1320   1.1       ryo 	/* max queue num is 8, and must be 2^n */
   1321   1.1       ryo 	if (ncpu >= 8)
   1322   1.1       ryo 		sc->sc_nqueues = 8;
   1323   1.1       ryo 	else if (ncpu >= 4)
   1324   1.1       ryo 		sc->sc_nqueues = 4;
   1325   1.1       ryo 	else if (ncpu >= 2)
   1326   1.1       ryo 		sc->sc_nqueues = 2;
   1327   1.1       ryo 	else
   1328   1.1       ryo 		sc->sc_nqueues = 1;
   1329   1.1       ryo 
   1330   1.1       ryo 	int msixcount = pci_msix_count(pa->pa_pc, pa->pa_tag);
   1331   1.1       ryo #ifndef CONFIG_NO_TXRX_INDEPENDENT
   1332   1.1       ryo 	if (msixcount >= (sc->sc_nqueues * 2 + 1)) {
   1333   1.1       ryo 		/* TX intrs + RX intrs + LINKSTAT intrs */
   1334   1.1       ryo 		sc->sc_use_txrx_independent_intr = true;
   1335   1.1       ryo 		sc->sc_poll_linkstat = false;
   1336   1.1       ryo 		sc->sc_msix = true;
   1337   1.1       ryo 	} else if (msixcount >= (sc->sc_nqueues * 2)) {
   1338   1.1       ryo 		/* TX intrs + RX intrs */
   1339   1.1       ryo 		sc->sc_use_txrx_independent_intr = true;
   1340   1.1       ryo 		sc->sc_poll_linkstat = true;
   1341   1.1       ryo 		sc->sc_msix = true;
   1342   1.1       ryo 	} else
   1343   1.1       ryo #endif
   1344   1.1       ryo 	if (msixcount >= (sc->sc_nqueues + 1)) {
   1345   1.1       ryo 		/* TX/RX intrs LINKSTAT intrs */
   1346   1.1       ryo 		sc->sc_use_txrx_independent_intr = false;
   1347   1.1       ryo 		sc->sc_poll_linkstat = false;
   1348   1.1       ryo 		sc->sc_msix = true;
   1349   1.1       ryo 	} else if (msixcount >= sc->sc_nqueues) {
   1350   1.1       ryo 		/* TX/RX intrs */
   1351   1.1       ryo 		sc->sc_use_txrx_independent_intr = false;
   1352   1.1       ryo 		sc->sc_poll_linkstat = true;
   1353   1.1       ryo 		sc->sc_msix = true;
   1354   1.1       ryo 	} else {
   1355   1.1       ryo 		/* giving up using MSI-X */
   1356   1.1       ryo 		sc->sc_msix = false;
   1357   1.1       ryo 	}
   1358   1.1       ryo 
   1359  1.31       ryo 	/* on FW Ver1 or FIBRE, linkstat interrupt does not occur on boot? */
   1360  1.31       ryo 	if (aqp->aq_media_type == AQ_MEDIA_TYPE_FIBRE ||
   1361  1.31       ryo 	    FW_VERSION_MAJOR(sc) == 1)
   1362  1.13       ryo 		sc->sc_poll_linkstat = true;
   1363  1.13       ryo 
   1364  1.13       ryo #ifdef AQ_FORCE_POLL_LINKSTAT
   1365  1.13       ryo 	sc->sc_poll_linkstat = true;
   1366  1.13       ryo #endif
   1367  1.13       ryo 
   1368   1.1       ryo 	aprint_debug_dev(sc->sc_dev,
   1369   1.1       ryo 	    "ncpu=%d, pci_msix_count=%d."
   1370   1.1       ryo 	    " allocate %d interrupts for %d%s queues%s\n",
   1371   1.1       ryo 	    ncpu, msixcount,
   1372   1.1       ryo 	    (sc->sc_use_txrx_independent_intr ?
   1373   1.1       ryo 	    (sc->sc_nqueues * 2) : sc->sc_nqueues) +
   1374   1.1       ryo 	    (sc->sc_poll_linkstat ? 0 : 1),
   1375   1.1       ryo 	    sc->sc_nqueues,
   1376   1.1       ryo 	    sc->sc_use_txrx_independent_intr ? "*2" : "",
   1377   1.1       ryo 	    sc->sc_poll_linkstat ? "" : ", and link status");
   1378   1.1       ryo 
   1379   1.1       ryo 	if (sc->sc_msix)
   1380   1.1       ryo 		error = aq_setup_msix(sc, pa, sc->sc_nqueues,
   1381   1.1       ryo 		    sc->sc_use_txrx_independent_intr, !sc->sc_poll_linkstat);
   1382   1.1       ryo 	else
   1383   1.1       ryo 		error = ENODEV;
   1384   1.1       ryo 
   1385   1.1       ryo 	if (error != 0) {
   1386   1.1       ryo 		/* if MSI-X failed, fallback to MSI with single queue */
   1387   1.1       ryo 		sc->sc_use_txrx_independent_intr = false;
   1388   1.1       ryo 		sc->sc_poll_linkstat = false;
   1389   1.1       ryo 		sc->sc_msix = false;
   1390   1.1       ryo 		sc->sc_nqueues = 1;
   1391   1.1       ryo 		error = aq_setup_legacy(sc, pa, PCI_INTR_TYPE_MSI);
   1392   1.1       ryo 	}
   1393   1.1       ryo 	if (error != 0) {
   1394   1.1       ryo 		/* if MSI failed, fallback to INTx */
   1395   1.1       ryo 		error = aq_setup_legacy(sc, pa, PCI_INTR_TYPE_INTX);
   1396   1.1       ryo 	}
   1397   1.1       ryo 	if (error != 0)
   1398  1.31       ryo 		goto attach_failure;
   1399   1.1       ryo 
   1400  1.33     skrll 	callout_init(&sc->sc_tick_ch, CALLOUT_MPSAFE);
   1401   1.1       ryo 	callout_setfunc(&sc->sc_tick_ch, aq_tick, sc);
   1402   1.1       ryo 
   1403  1.33     skrll 	char wqname[MAXCOMLEN];
   1404  1.33     skrll 	snprintf(wqname, sizeof(wqname), "%sReset", device_xname(sc->sc_dev));
   1405  1.33     skrll 	error = workqueue_create(&sc->sc_reset_wq, wqname,
   1406  1.33     skrll 	    aq_handle_reset_work, sc, PRI_SOFTNET, IPL_SOFTCLOCK,
   1407  1.33     skrll 	    WQ_MPSAFE);
   1408  1.33     skrll 	if (error) {
   1409  1.33     skrll 		aprint_error_dev(sc->sc_dev,
   1410  1.33     skrll 		    "unable to create reset workqueue\n");
   1411  1.33     skrll 		goto attach_failure;
   1412  1.33     skrll 	}
   1413  1.33     skrll 
   1414   1.1       ryo 	sc->sc_intr_moderation_enable = CONFIG_INTR_MODERATION_ENABLE;
   1415   1.1       ryo 
   1416   1.1       ryo 	if (sc->sc_msix && (sc->sc_nqueues > 1))
   1417   1.1       ryo 		sc->sc_rss_enable = true;
   1418   1.1       ryo 	else
   1419   1.1       ryo 		sc->sc_rss_enable = false;
   1420   1.1       ryo 
   1421   1.1       ryo 	error = aq_txrx_rings_alloc(sc);
   1422   1.1       ryo 	if (error != 0)
   1423   1.1       ryo 		goto attach_failure;
   1424   1.1       ryo 
   1425   1.1       ryo 	error = aq_fw_version_init(sc);
   1426   1.1       ryo 	if (error != 0)
   1427   1.1       ryo 		goto attach_failure;
   1428   1.1       ryo 
   1429   1.1       ryo 	error = aq_hw_init_ucp(sc);
   1430   1.1       ryo 	if (error < 0)
   1431   1.1       ryo 		goto attach_failure;
   1432   1.1       ryo 
   1433   1.1       ryo 	KASSERT(sc->sc_mbox_addr != 0);
   1434   1.1       ryo 	error = aq_hw_reset(sc);
   1435   1.1       ryo 	if (error != 0)
   1436   1.1       ryo 		goto attach_failure;
   1437   1.1       ryo 
   1438   1.1       ryo 	aq_get_mac_addr(sc);
   1439   1.1       ryo 	aq_init_rss(sc);
   1440   1.1       ryo 
   1441   1.1       ryo 	error = aq_hw_init(sc);	/* initialize and interrupts */
   1442   1.1       ryo 	if (error != 0)
   1443   1.1       ryo 		goto attach_failure;
   1444   1.1       ryo 
   1445   1.1       ryo 	sc->sc_media_type = aqp->aq_media_type;
   1446   1.1       ryo 	sc->sc_available_rates = aqp->aq_available_rates;
   1447   1.1       ryo 
   1448   1.1       ryo 	sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
   1449   1.1       ryo 	ifmedia_init(&sc->sc_media, IFM_IMASK,
   1450   1.1       ryo 	    aq_ifmedia_change, aq_ifmedia_status);
   1451   1.1       ryo 	aq_initmedia(sc);
   1452   1.1       ryo 
   1453   1.1       ryo 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
   1454   1.1       ryo 	ifp->if_softc = sc;
   1455   1.1       ryo 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1456  1.20       ryo 	ifp->if_extflags = IFEF_MPSAFE;
   1457   1.1       ryo 	ifp->if_baudrate = IF_Gbps(10);
   1458   1.1       ryo 	ifp->if_init = aq_init;
   1459   1.1       ryo 	ifp->if_ioctl = aq_ioctl;
   1460   1.1       ryo 	if (sc->sc_msix && (sc->sc_nqueues > 1))
   1461   1.1       ryo 		ifp->if_transmit = aq_transmit;
   1462   1.1       ryo 	ifp->if_start = aq_start;
   1463   1.1       ryo 	ifp->if_stop = aq_stop;
   1464  1.33     skrll 	ifp->if_watchdog = NULL;
   1465   1.1       ryo 	IFQ_SET_READY(&ifp->if_snd);
   1466   1.1       ryo 
   1467   1.1       ryo 	/* initialize capabilities */
   1468   1.1       ryo 	sc->sc_ethercom.ec_capabilities = 0;
   1469   1.1       ryo 	sc->sc_ethercom.ec_capenable = 0;
   1470   1.1       ryo #if notyet
   1471   1.1       ryo 	/* TODO */
   1472   1.1       ryo 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_EEE;
   1473   1.1       ryo #endif
   1474   1.1       ryo 	sc->sc_ethercom.ec_capabilities |=
   1475   1.1       ryo 	    ETHERCAP_JUMBO_MTU |
   1476   1.1       ryo 	    ETHERCAP_VLAN_MTU |
   1477  1.10       ryo 	    ETHERCAP_VLAN_HWTAGGING |
   1478  1.10       ryo 	    ETHERCAP_VLAN_HWFILTER;
   1479   1.1       ryo 	sc->sc_ethercom.ec_capenable |=
   1480  1.10       ryo 	    ETHERCAP_VLAN_HWTAGGING |
   1481  1.10       ryo 	    ETHERCAP_VLAN_HWFILTER;
   1482   1.1       ryo 
   1483   1.1       ryo 	ifp->if_capabilities = 0;
   1484   1.1       ryo 	ifp->if_capenable = 0;
   1485   1.1       ryo #ifdef CONFIG_LRO_SUPPORT
   1486   1.1       ryo 	ifp->if_capabilities |= IFCAP_LRO;
   1487   1.1       ryo 	ifp->if_capenable |= IFCAP_LRO;
   1488   1.1       ryo #endif
   1489   1.1       ryo #if notyet
   1490   1.1       ryo 	/* TSO */
   1491   1.1       ryo 	ifp->if_capabilities |= IFCAP_TSOv4 | IFCAP_TSOv6;
   1492   1.1       ryo #endif
   1493   1.1       ryo 
   1494  1.25       ryo 	/* TX hardware checksum offloading */
   1495   1.1       ryo 	ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx;
   1496   1.1       ryo 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv6_Tx;
   1497   1.1       ryo 	ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv6_Tx;
   1498  1.25       ryo 	/* RX hardware checksum offloading */
   1499   1.1       ryo 	ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx;
   1500  1.21       ryo 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_TCPv6_Rx;
   1501  1.21       ryo 	ifp->if_capabilities |= IFCAP_CSUM_UDPv4_Rx | IFCAP_CSUM_UDPv6_Rx;
   1502   1.1       ryo 
   1503  1.27  riastrad 	if_initialize(ifp);
   1504  1.20       ryo 	ifp->if_percpuq = if_percpuq_create(ifp);
   1505   1.1       ryo 	if_deferred_start_init(ifp, NULL);
   1506   1.1       ryo 	ether_ifattach(ifp, sc->sc_enaddr.ether_addr_octet);
   1507  1.10       ryo 	ether_set_vlan_cb(&sc->sc_ethercom, aq_vlan_cb);
   1508   1.1       ryo 	ether_set_ifflags_cb(&sc->sc_ethercom, aq_ifflags_cb);
   1509  1.20       ryo 	if_register(ifp);
   1510   1.1       ryo 
   1511  1.37  riastrad 	/* only intr about link */
   1512  1.37  riastrad 	aq_enable_intr(sc, /*link*/true, /*txrx*/false);
   1513   1.1       ryo 
   1514   1.1       ryo 	/* update media */
   1515   1.1       ryo 	aq_ifmedia_change(ifp);
   1516   1.1       ryo 
   1517   1.4       ryo #if NSYSMON_ENVSYS > 0
   1518   1.4       ryo 	/* temperature monitoring */
   1519   1.4       ryo 	if (sc->sc_fw_ops != NULL && sc->sc_fw_ops->get_temperature != NULL &&
   1520   1.4       ryo 	    (sc->sc_fw_caps & FW2X_CTRL_TEMPERATURE) != 0) {
   1521   1.4       ryo 
   1522   1.4       ryo 		sc->sc_sme = sysmon_envsys_create();
   1523   1.4       ryo 		sc->sc_sme->sme_name = device_xname(self);
   1524   1.4       ryo 		sc->sc_sme->sme_cookie = sc;
   1525   1.4       ryo 		sc->sc_sme->sme_flags = 0;
   1526   1.4       ryo 		sc->sc_sme->sme_refresh = aq_temp_refresh;
   1527   1.4       ryo 		sc->sc_sensor_temp.units = ENVSYS_STEMP;
   1528   1.4       ryo 		sc->sc_sensor_temp.state = ENVSYS_SINVALID;
   1529   1.4       ryo 		snprintf(sc->sc_sensor_temp.desc, ENVSYS_DESCLEN, "PHY");
   1530   1.4       ryo 
   1531   1.4       ryo 		sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
   1532  1.26   mlelstv 		if (sysmon_envsys_register(sc->sc_sme)) {
   1533  1.26   mlelstv 			sysmon_envsys_destroy(sc->sc_sme);
   1534  1.26   mlelstv 			sc->sc_sme = NULL;
   1535  1.26   mlelstv 			goto attach_failure;
   1536  1.26   mlelstv 		}
   1537   1.4       ryo 
   1538   1.4       ryo 		/*
   1539   1.4       ryo 		 * for unknown reasons, the first call of fw2x_get_temperature()
   1540   1.4       ryo 		 * will always fail (firmware matter?), so run once now.
   1541   1.4       ryo 		 */
   1542   1.4       ryo 		aq_temp_refresh(sc->sc_sme, &sc->sc_sensor_temp);
   1543   1.4       ryo 	}
   1544   1.4       ryo #endif
   1545   1.4       ryo 
   1546   1.1       ryo #ifdef AQ_EVENT_COUNTERS
   1547   1.1       ryo 	/* get starting statistics values */
   1548   1.1       ryo 	if (sc->sc_fw_ops != NULL && sc->sc_fw_ops->get_stats != NULL &&
   1549   1.1       ryo 	    sc->sc_fw_ops->get_stats(sc, &sc->sc_statistics[0]) == 0) {
   1550   1.1       ryo 		sc->sc_poll_statistics = true;
   1551   1.1       ryo 	}
   1552   1.1       ryo 
   1553   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, uprc, "RX unicast packet");
   1554   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, bprc, "RX broadcast packet");
   1555   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, mprc, "RX multicast packet");
   1556   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, erpr, "RX error packet");
   1557   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, ubrc, "RX unicast bytes");
   1558   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, bbrc, "RX broadcast bytes");
   1559   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, mbrc, "RX multicast bytes");
   1560   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, prc, "RX good packet");
   1561   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, uptc, "TX unicast packet");
   1562   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, bptc, "TX broadcast packet");
   1563   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, mptc, "TX multicast packet");
   1564   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, erpt, "TX error packet");
   1565   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, ubtc, "TX unicast bytes");
   1566   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, bbtc, "TX broadcast bytes");
   1567   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, mbtc, "TX multicast bytes");
   1568   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, ptc, "TX good packet");
   1569   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, dpc, "DMA drop packet");
   1570   1.1       ryo 	AQ_EVCNT_ATTACH_MISC(sc, cprc, "RX coalesced packet");
   1571   1.1       ryo #endif
   1572   1.1       ryo 
   1573  1.29   msaitoh 	if (pmf_device_register(self, NULL, NULL))
   1574  1.29   msaitoh 		pmf_class_network_register(self, ifp);
   1575  1.29   msaitoh 	else
   1576  1.29   msaitoh 		aprint_error_dev(self, "couldn't establish power handler\n");
   1577  1.29   msaitoh 
   1578   1.1       ryo 	return;
   1579   1.1       ryo 
   1580   1.1       ryo  attach_failure:
   1581   1.1       ryo 	aq_detach(self, 0);
   1582   1.1       ryo }
   1583   1.1       ryo 
   1584   1.1       ryo static int
   1585   1.1       ryo aq_detach(device_t self, int flags __unused)
   1586   1.1       ryo {
   1587  1.32     skrll 	struct aq_softc * const sc = device_private(self);
   1588  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   1589  1.32     skrll 	int i;
   1590   1.1       ryo 
   1591  1.41       ryo 	if (sc->sc_dev == NULL)
   1592  1.41       ryo 		return 0;
   1593  1.41       ryo 
   1594   1.1       ryo 	if (sc->sc_iosize != 0) {
   1595   1.1       ryo 		if (ifp->if_softc != NULL) {
   1596  1.35     skrll 			IFNET_LOCK(ifp);
   1597  1.35     skrll 			aq_stop(ifp, 1);
   1598  1.35     skrll 			IFNET_UNLOCK(ifp);
   1599   1.1       ryo 		}
   1600   1.1       ryo 
   1601   1.1       ryo 		for (i = 0; i < AQ_NINTR_MAX; i++) {
   1602   1.1       ryo 			if (sc->sc_ihs[i] != NULL) {
   1603   1.1       ryo 				pci_intr_disestablish(sc->sc_pc, sc->sc_ihs[i]);
   1604   1.1       ryo 				sc->sc_ihs[i] = NULL;
   1605   1.1       ryo 			}
   1606   1.1       ryo 		}
   1607   1.1       ryo 		if (sc->sc_nintrs > 0) {
   1608  1.41       ryo 			callout_stop(&sc->sc_tick_ch);
   1609  1.41       ryo 
   1610   1.1       ryo 			pci_intr_release(sc->sc_pc, sc->sc_intrs,
   1611   1.1       ryo 			    sc->sc_nintrs);
   1612   1.1       ryo 			sc->sc_intrs = NULL;
   1613   1.1       ryo 			sc->sc_nintrs = 0;
   1614   1.1       ryo 		}
   1615   1.1       ryo 
   1616  1.41       ryo 		if (sc->sc_reset_wq != NULL) {
   1617  1.41       ryo 			workqueue_destroy(sc->sc_reset_wq);
   1618  1.41       ryo 			sc->sc_reset_wq = NULL;
   1619  1.41       ryo 		}
   1620  1.41       ryo 
   1621   1.1       ryo 		aq_txrx_rings_free(sc);
   1622   1.1       ryo 
   1623   1.1       ryo 		if (ifp->if_softc != NULL) {
   1624   1.1       ryo 			ether_ifdetach(ifp);
   1625   1.1       ryo 			if_detach(ifp);
   1626   1.1       ryo 		}
   1627   1.1       ryo 
   1628   1.1       ryo 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
   1629   1.1       ryo 		sc->sc_iosize = 0;
   1630   1.1       ryo 	}
   1631   1.1       ryo 
   1632   1.4       ryo #if NSYSMON_ENVSYS > 0
   1633   1.4       ryo 	if (sc->sc_sme != NULL) {
   1634   1.4       ryo 		/* all sensors associated with this will also be detached */
   1635   1.4       ryo 		sysmon_envsys_unregister(sc->sc_sme);
   1636   1.4       ryo 	}
   1637   1.4       ryo #endif
   1638   1.4       ryo 
   1639   1.1       ryo #ifdef AQ_EVENT_COUNTERS
   1640   1.1       ryo 	AQ_EVCNT_DETACH(sc, uprc);
   1641   1.1       ryo 	AQ_EVCNT_DETACH(sc, mprc);
   1642   1.1       ryo 	AQ_EVCNT_DETACH(sc, bprc);
   1643   1.1       ryo 	AQ_EVCNT_DETACH(sc, erpt);
   1644   1.1       ryo 	AQ_EVCNT_DETACH(sc, uptc);
   1645   1.1       ryo 	AQ_EVCNT_DETACH(sc, mptc);
   1646   1.1       ryo 	AQ_EVCNT_DETACH(sc, bptc);
   1647   1.1       ryo 	AQ_EVCNT_DETACH(sc, erpr);
   1648   1.1       ryo 	AQ_EVCNT_DETACH(sc, mbtc);
   1649   1.1       ryo 	AQ_EVCNT_DETACH(sc, bbtc);
   1650   1.1       ryo 	AQ_EVCNT_DETACH(sc, mbrc);
   1651   1.1       ryo 	AQ_EVCNT_DETACH(sc, bbrc);
   1652   1.1       ryo 	AQ_EVCNT_DETACH(sc, ubrc);
   1653   1.1       ryo 	AQ_EVCNT_DETACH(sc, ubtc);
   1654   1.1       ryo 	AQ_EVCNT_DETACH(sc, ptc);
   1655   1.1       ryo 	AQ_EVCNT_DETACH(sc, prc);
   1656   1.1       ryo 	AQ_EVCNT_DETACH(sc, dpc);
   1657   1.1       ryo 	AQ_EVCNT_DETACH(sc, cprc);
   1658   1.1       ryo #endif
   1659   1.1       ryo 
   1660  1.41       ryo 	if (sc->sc_ethercom.ec_ifmedia != NULL) {
   1661  1.41       ryo 		ifmedia_fini(&sc->sc_media);
   1662  1.41       ryo 		sc->sc_ethercom.ec_ifmedia = NULL;
   1663  1.41       ryo 	}
   1664   1.7   thorpej 
   1665   1.4       ryo 	mutex_destroy(&sc->sc_mpi_mutex);
   1666   1.1       ryo 	mutex_destroy(&sc->sc_mutex);
   1667  1.41       ryo 	sc->sc_dev = NULL;
   1668   1.1       ryo 
   1669   1.1       ryo 	return 0;
   1670   1.1       ryo }
   1671   1.1       ryo 
   1672   1.1       ryo static int
   1673   1.1       ryo aq_establish_intr(struct aq_softc *sc, int intno, kcpuset_t *affinity,
   1674   1.1       ryo     int (*func)(void *), void *arg, const char *xname)
   1675   1.1       ryo {
   1676   1.1       ryo 	char intrbuf[PCI_INTRSTR_LEN];
   1677   1.1       ryo 	pci_chipset_tag_t pc = sc->sc_pc;
   1678   1.1       ryo 	void *vih;
   1679   1.1       ryo 	const char *intrstr = NULL;
   1680   1.1       ryo 
   1681   1.1       ryo 	intrstr = pci_intr_string(pc, sc->sc_intrs[intno], intrbuf,
   1682   1.1       ryo 	    sizeof(intrbuf));
   1683   1.1       ryo 
   1684   1.1       ryo 	pci_intr_setattr(pc, &sc->sc_intrs[intno], PCI_INTR_MPSAFE, true);
   1685   1.1       ryo 
   1686   1.1       ryo 	vih = pci_intr_establish_xname(pc, sc->sc_intrs[intno],
   1687   1.1       ryo 	    IPL_NET, func, arg, xname);
   1688   1.1       ryo 	if (vih == NULL) {
   1689   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1690   1.1       ryo 		    "unable to establish MSI-X%s%s for %s\n",
   1691   1.1       ryo 		    intrstr ? " at " : "",
   1692   1.1       ryo 		    intrstr ? intrstr : "", xname);
   1693   1.1       ryo 		return EIO;
   1694   1.1       ryo 	}
   1695   1.1       ryo 	sc->sc_ihs[intno] = vih;
   1696   1.1       ryo 
   1697   1.1       ryo 	if (affinity != NULL) {
   1698   1.1       ryo 		/* Round-robin affinity */
   1699   1.1       ryo 		kcpuset_zero(affinity);
   1700   1.1       ryo 		kcpuset_set(affinity, intno % ncpu);
   1701   1.1       ryo 		interrupt_distribute(vih, affinity, NULL);
   1702   1.1       ryo 	}
   1703   1.1       ryo 
   1704   1.1       ryo 	return 0;
   1705   1.1       ryo }
   1706   1.1       ryo 
   1707   1.1       ryo static int
   1708   1.1       ryo aq_establish_msix_intr(struct aq_softc *sc, bool txrx_independent,
   1709   1.1       ryo     bool linkintr)
   1710   1.1       ryo {
   1711   1.1       ryo 	kcpuset_t *affinity;
   1712   1.1       ryo 	int error, intno, i;
   1713   1.1       ryo 	char intr_xname[INTRDEVNAMEBUF];
   1714   1.1       ryo 
   1715   1.1       ryo 	kcpuset_create(&affinity, false);
   1716   1.1       ryo 
   1717   1.1       ryo 	intno = 0;
   1718   1.1       ryo 
   1719   1.1       ryo 	if (txrx_independent) {
   1720   1.1       ryo 		for (i = 0; i < sc->sc_nqueues; i++) {
   1721   1.1       ryo 			snprintf(intr_xname, sizeof(intr_xname), "%s RX%d",
   1722   1.1       ryo 			    device_xname(sc->sc_dev), i);
   1723   1.1       ryo 			sc->sc_rx_irq[i] = intno;
   1724   1.1       ryo 			error = aq_establish_intr(sc, intno++, affinity,
   1725   1.1       ryo 			   aq_rx_intr, &sc->sc_queue[i].rxring, intr_xname);
   1726   1.1       ryo 			if (error != 0)
   1727   1.1       ryo 				goto fail;
   1728   1.1       ryo 		}
   1729   1.1       ryo 		for (i = 0; i < sc->sc_nqueues; i++) {
   1730   1.1       ryo 			snprintf(intr_xname, sizeof(intr_xname), "%s TX%d",
   1731   1.1       ryo 			    device_xname(sc->sc_dev), i);
   1732   1.1       ryo 			sc->sc_tx_irq[i] = intno;
   1733   1.1       ryo 			error = aq_establish_intr(sc, intno++, affinity,
   1734   1.1       ryo 			    aq_tx_intr, &sc->sc_queue[i].txring, intr_xname);
   1735   1.1       ryo 			if (error != 0)
   1736   1.1       ryo 				goto fail;
   1737   1.1       ryo 		}
   1738   1.1       ryo 	} else {
   1739   1.1       ryo 		for (i = 0; i < sc->sc_nqueues; i++) {
   1740   1.1       ryo 			snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
   1741   1.1       ryo 			    device_xname(sc->sc_dev), i);
   1742   1.1       ryo 			sc->sc_rx_irq[i] = intno;
   1743   1.1       ryo 			sc->sc_tx_irq[i] = intno;
   1744   1.1       ryo 			error = aq_establish_intr(sc, intno++, affinity,
   1745   1.1       ryo 			    aq_txrx_intr, &sc->sc_queue[i], intr_xname);
   1746   1.1       ryo 			if (error != 0)
   1747   1.1       ryo 				goto fail;
   1748   1.1       ryo 		}
   1749   1.1       ryo 	}
   1750   1.1       ryo 
   1751   1.1       ryo 	if (linkintr) {
   1752   1.1       ryo 		snprintf(intr_xname, sizeof(intr_xname), "%s LINK",
   1753   1.1       ryo 		    device_xname(sc->sc_dev));
   1754   1.1       ryo 		sc->sc_linkstat_irq = intno;
   1755   1.1       ryo 		error = aq_establish_intr(sc, intno++, affinity,
   1756   1.1       ryo 		    aq_link_intr, sc, intr_xname);
   1757   1.1       ryo 		if (error != 0)
   1758   1.1       ryo 			goto fail;
   1759   1.1       ryo 	}
   1760   1.1       ryo 
   1761   1.1       ryo 	kcpuset_destroy(affinity);
   1762   1.1       ryo 	return 0;
   1763   1.1       ryo 
   1764   1.1       ryo  fail:
   1765   1.1       ryo 	for (i = 0; i < AQ_NINTR_MAX; i++) {
   1766   1.1       ryo 		if (sc->sc_ihs[i] != NULL) {
   1767   1.1       ryo 			pci_intr_disestablish(sc->sc_pc, sc->sc_ihs[i]);
   1768   1.1       ryo 			sc->sc_ihs[i] = NULL;
   1769   1.1       ryo 		}
   1770   1.1       ryo 	}
   1771   1.1       ryo 
   1772   1.1       ryo 	kcpuset_destroy(affinity);
   1773   1.1       ryo 	return ENOMEM;
   1774   1.1       ryo }
   1775   1.1       ryo 
   1776   1.1       ryo static int
   1777   1.1       ryo aq_setup_msix(struct aq_softc *sc, struct pci_attach_args *pa, int nqueue,
   1778   1.1       ryo     bool txrx_independent, bool linkintr)
   1779   1.1       ryo {
   1780   1.1       ryo 	int error, nintr;
   1781   1.1       ryo 
   1782   1.1       ryo 	if (txrx_independent)
   1783   1.1       ryo 		nintr = nqueue * 2;
   1784   1.1       ryo 	else
   1785   1.1       ryo 		nintr = nqueue;
   1786   1.1       ryo 
   1787   1.1       ryo 	if (linkintr)
   1788   1.1       ryo 		nintr++;
   1789   1.1       ryo 
   1790   1.1       ryo 	error = pci_msix_alloc_exact(pa, &sc->sc_intrs, nintr);
   1791   1.1       ryo 	if (error != 0) {
   1792   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1793   1.1       ryo 		    "failed to allocate MSI-X interrupts\n");
   1794   1.1       ryo 		goto fail;
   1795   1.1       ryo 	}
   1796   1.1       ryo 
   1797   1.1       ryo 	error = aq_establish_msix_intr(sc, txrx_independent, linkintr);
   1798   1.1       ryo 	if (error == 0) {
   1799   1.1       ryo 		sc->sc_nintrs = nintr;
   1800   1.1       ryo 	} else {
   1801   1.1       ryo 		pci_intr_release(sc->sc_pc, sc->sc_intrs, nintr);
   1802   1.1       ryo 		sc->sc_nintrs = 0;
   1803   1.1       ryo 	}
   1804   1.1       ryo  fail:
   1805   1.1       ryo 	return error;
   1806   1.1       ryo 
   1807   1.1       ryo }
   1808   1.1       ryo 
   1809   1.1       ryo static int
   1810   1.1       ryo aq_setup_legacy(struct aq_softc *sc, struct pci_attach_args *pa,
   1811   1.1       ryo     pci_intr_type_t inttype)
   1812   1.1       ryo {
   1813   1.1       ryo 	int counts[PCI_INTR_TYPE_SIZE];
   1814   1.1       ryo 	int error, nintr;
   1815   1.1       ryo 
   1816   1.1       ryo 	nintr = 1;
   1817   1.1       ryo 
   1818   1.1       ryo 	memset(counts, 0, sizeof(counts));
   1819   1.1       ryo 	counts[inttype] = nintr;
   1820   1.1       ryo 
   1821   1.1       ryo 	error = pci_intr_alloc(pa, &sc->sc_intrs, counts, inttype);
   1822   1.1       ryo 	if (error != 0) {
   1823   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1824   1.1       ryo 		    "failed to allocate%s interrupts\n",
   1825   1.1       ryo 		    (inttype == PCI_INTR_TYPE_MSI) ? " MSI" : "");
   1826   1.1       ryo 		return error;
   1827   1.1       ryo 	}
   1828   1.1       ryo 	error = aq_establish_intr(sc, 0, NULL, aq_legacy_intr, sc,
   1829   1.1       ryo 	    device_xname(sc->sc_dev));
   1830   1.1       ryo 	if (error == 0) {
   1831   1.1       ryo 		sc->sc_nintrs = nintr;
   1832   1.1       ryo 	} else {
   1833   1.1       ryo 		pci_intr_release(sc->sc_pc, sc->sc_intrs, nintr);
   1834   1.1       ryo 		sc->sc_nintrs = 0;
   1835   1.1       ryo 	}
   1836   1.1       ryo 	return error;
   1837   1.1       ryo }
   1838   1.1       ryo 
   1839   1.1       ryo static void
   1840   1.1       ryo global_software_reset(struct aq_softc *sc)
   1841   1.1       ryo {
   1842   1.1       ryo 	uint32_t v;
   1843   1.1       ryo 
   1844   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_SYSCONTROL_REG, RX_SYSCONTROL_RESET_DIS, 0);
   1845   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TX_SYSCONTROL_REG, TX_SYSCONTROL_RESET_DIS, 0);
   1846   1.1       ryo 	AQ_WRITE_REG_BIT(sc, FW_MPI_RESETCTRL_REG,
   1847   1.1       ryo 	    FW_MPI_RESETCTRL_RESET_DIS, 0);
   1848   1.1       ryo 
   1849   1.1       ryo 	v = AQ_READ_REG(sc, AQ_FW_SOFTRESET_REG);
   1850   1.1       ryo 	v &= ~AQ_FW_SOFTRESET_DIS;
   1851   1.1       ryo 	v |= AQ_FW_SOFTRESET_RESET;
   1852   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_SOFTRESET_REG, v);
   1853   1.1       ryo }
   1854   1.1       ryo 
   1855   1.1       ryo static int
   1856   1.1       ryo mac_soft_reset_rbl(struct aq_softc *sc, aq_fw_bootloader_mode_t *mode)
   1857   1.1       ryo {
   1858   1.1       ryo 	int timo;
   1859   1.1       ryo 
   1860   1.1       ryo 	aprint_debug_dev(sc->sc_dev, "RBL> MAC reset STARTED!\n");
   1861   1.1       ryo 
   1862   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x40e1);
   1863   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CPU_SEM_REG(0), 1);
   1864   1.1       ryo 	AQ_WRITE_REG(sc, AQ_MBOXIF_POWER_GATING_CONTROL_REG, 0);
   1865   1.1       ryo 
   1866   1.1       ryo 	/* MAC FW will reload PHY FW if 1E.1000.3 was cleaned - #undone */
   1867   1.1       ryo 	AQ_WRITE_REG(sc, FW_BOOT_EXIT_CODE_REG, RBL_STATUS_DEAD);
   1868   1.1       ryo 
   1869   1.1       ryo 	global_software_reset(sc);
   1870   1.1       ryo 
   1871   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x40e0);
   1872   1.1       ryo 
   1873   1.1       ryo 	/* Wait for RBL to finish boot process. */
   1874   1.1       ryo #define RBL_TIMEOUT_MS	10000
   1875   1.1       ryo 	uint16_t rbl_status;
   1876   1.1       ryo 	for (timo = RBL_TIMEOUT_MS; timo > 0; timo--) {
   1877   1.1       ryo 		rbl_status = AQ_READ_REG(sc, FW_BOOT_EXIT_CODE_REG) & 0xffff;
   1878   1.1       ryo 		if (rbl_status != 0 && rbl_status != RBL_STATUS_DEAD)
   1879   1.1       ryo 			break;
   1880   1.1       ryo 		msec_delay(1);
   1881   1.1       ryo 	}
   1882   1.1       ryo 	if (timo <= 0) {
   1883   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1884   1.1       ryo 		    "RBL> RBL restart failed: timeout\n");
   1885   1.1       ryo 		return EBUSY;
   1886   1.1       ryo 	}
   1887   1.1       ryo 	switch (rbl_status) {
   1888   1.1       ryo 	case RBL_STATUS_SUCCESS:
   1889   1.1       ryo 		if (mode != NULL)
   1890   1.1       ryo 			*mode = FW_BOOT_MODE_RBL_FLASH;
   1891   1.1       ryo 		aprint_debug_dev(sc->sc_dev, "RBL> reset complete! [Flash]\n");
   1892   1.1       ryo 		break;
   1893   1.1       ryo 	case RBL_STATUS_HOST_BOOT:
   1894   1.1       ryo 		if (mode != NULL)
   1895   1.1       ryo 			*mode = FW_BOOT_MODE_RBL_HOST_BOOTLOAD;
   1896   1.1       ryo 		aprint_debug_dev(sc->sc_dev,
   1897   1.1       ryo 		    "RBL> reset complete! [Host Bootload]\n");
   1898   1.1       ryo 		break;
   1899   1.1       ryo 	case RBL_STATUS_FAILURE:
   1900   1.1       ryo 	default:
   1901   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1902   1.1       ryo 		    "unknown RBL status 0x%x\n", rbl_status);
   1903   1.1       ryo 		return EBUSY;
   1904   1.1       ryo 	}
   1905   1.1       ryo 
   1906   1.1       ryo 	return 0;
   1907   1.1       ryo }
   1908   1.1       ryo 
   1909   1.1       ryo static int
   1910   1.1       ryo mac_soft_reset_flb(struct aq_softc *sc)
   1911   1.1       ryo {
   1912   1.1       ryo 	uint32_t v;
   1913   1.1       ryo 	int timo;
   1914   1.1       ryo 
   1915   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x40e1);
   1916   1.1       ryo 	/*
   1917   1.1       ryo 	 * Let Felicity hardware to complete SMBUS transaction before
   1918   1.1       ryo 	 * Global software reset.
   1919   1.1       ryo 	 */
   1920   1.1       ryo 	msec_delay(50);
   1921   1.1       ryo 
   1922   1.1       ryo 	/*
   1923   1.1       ryo 	 * If SPI burst transaction was interrupted(before running the script),
   1924   1.1       ryo 	 * global software reset may not clear SPI interface.
   1925   1.1       ryo 	 * Clean it up manually before global reset.
   1926   1.1       ryo 	 */
   1927   1.1       ryo 	AQ_WRITE_REG(sc, AQ_GLB_NVR_PROVISIONING2_REG, 0x00a0);
   1928   1.1       ryo 	AQ_WRITE_REG(sc, AQ_GLB_NVR_INTERFACE1_REG, 0x009f);
   1929   1.1       ryo 	AQ_WRITE_REG(sc, AQ_GLB_NVR_INTERFACE1_REG, 0x809f);
   1930   1.1       ryo 	msec_delay(50);
   1931   1.1       ryo 
   1932   1.1       ryo 	v = AQ_READ_REG(sc, AQ_FW_SOFTRESET_REG);
   1933   1.1       ryo 	v &= ~AQ_FW_SOFTRESET_DIS;
   1934   1.1       ryo 	v |= AQ_FW_SOFTRESET_RESET;
   1935   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_SOFTRESET_REG, v);
   1936   1.1       ryo 
   1937   1.1       ryo 	/* Kickstart. */
   1938   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x80e0);
   1939   1.1       ryo 	AQ_WRITE_REG(sc, AQ_MBOXIF_POWER_GATING_CONTROL_REG, 0);
   1940   1.1       ryo 	if (!sc->sc_fast_start_enabled)
   1941   1.1       ryo 		AQ_WRITE_REG(sc, AQ_GLB_GENERAL_PROVISIONING9_REG, 1);
   1942   1.1       ryo 
   1943   1.1       ryo 	/*
   1944   1.1       ryo 	 * For the case SPI burst transaction was interrupted (by MCP reset
   1945   1.1       ryo 	 * above), wait until it is completed by hardware.
   1946   1.1       ryo 	 */
   1947   1.1       ryo 	msec_delay(50);
   1948   1.1       ryo 
   1949   1.1       ryo 	/* MAC Kickstart */
   1950   1.1       ryo 	if (!sc->sc_fast_start_enabled) {
   1951   1.1       ryo 		AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x180e0);
   1952   1.1       ryo 
   1953   1.1       ryo 		uint32_t flb_status;
   1954   1.1       ryo 		for (timo = 0; timo < 1000; timo++) {
   1955   1.1       ryo 			flb_status = AQ_READ_REG(sc,
   1956   1.1       ryo 			    FW_MPI_DAISY_CHAIN_STATUS_REG) & 0x10;
   1957   1.1       ryo 			if (flb_status != 0)
   1958   1.1       ryo 				break;
   1959   1.1       ryo 			msec_delay(1);
   1960   1.1       ryo 		}
   1961   1.1       ryo 		if (flb_status == 0) {
   1962   1.1       ryo 			aprint_error_dev(sc->sc_dev,
   1963   1.1       ryo 			    "FLB> MAC kickstart failed: timed out\n");
   1964   1.1       ryo 			return ETIMEDOUT;
   1965   1.1       ryo 		}
   1966   1.1       ryo 		aprint_debug_dev(sc->sc_dev,
   1967   1.1       ryo 		    "FLB> MAC kickstart done, %d ms\n", timo);
   1968   1.1       ryo 		/* FW reset */
   1969   1.1       ryo 		AQ_WRITE_REG(sc, AQ_FW_GLB_CTL2_REG, 0x80e0);
   1970   1.1       ryo 		/*
   1971   1.1       ryo 		 * Let Felicity hardware complete SMBUS transaction before
   1972   1.1       ryo 		 * Global software reset.
   1973   1.1       ryo 		 */
   1974   1.1       ryo 		msec_delay(50);
   1975   1.1       ryo 		sc->sc_fast_start_enabled = true;
   1976   1.1       ryo 	}
   1977   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_GLB_CPU_SEM_REG(0), 1);
   1978   1.1       ryo 
   1979   1.1       ryo 	/* PHY Kickstart: #undone */
   1980   1.1       ryo 	global_software_reset(sc);
   1981   1.1       ryo 
   1982   1.1       ryo 	for (timo = 0; timo < 1000; timo++) {
   1983   1.1       ryo 		if (AQ_READ_REG(sc, AQ_FW_VERSION_REG) != 0)
   1984   1.1       ryo 			break;
   1985   1.1       ryo 		msec_delay(10);
   1986   1.1       ryo 	}
   1987   1.1       ryo 	if (timo >= 1000) {
   1988   1.1       ryo 		aprint_error_dev(sc->sc_dev, "FLB> Global Soft Reset failed\n");
   1989   1.1       ryo 		return ETIMEDOUT;
   1990   1.1       ryo 	}
   1991   1.1       ryo 	aprint_debug_dev(sc->sc_dev, "FLB> F/W restart: %d ms\n", timo * 10);
   1992   1.1       ryo 	return 0;
   1993   1.1       ryo 
   1994   1.1       ryo }
   1995   1.1       ryo 
   1996   1.1       ryo static int
   1997   1.1       ryo mac_soft_reset(struct aq_softc *sc, aq_fw_bootloader_mode_t *mode)
   1998   1.1       ryo {
   1999   1.1       ryo 	if (sc->sc_rbl_enabled)
   2000   1.1       ryo 		return mac_soft_reset_rbl(sc, mode);
   2001   1.1       ryo 
   2002   1.1       ryo 	if (mode != NULL)
   2003   1.1       ryo 		*mode = FW_BOOT_MODE_FLB;
   2004   1.1       ryo 	return mac_soft_reset_flb(sc);
   2005   1.1       ryo }
   2006   1.1       ryo 
   2007   1.1       ryo static int
   2008   1.1       ryo aq_fw_read_version(struct aq_softc *sc)
   2009   1.1       ryo {
   2010   1.1       ryo 	int i, error = EBUSY;
   2011   1.1       ryo #define MAC_FW_START_TIMEOUT_MS	10000
   2012   1.1       ryo 	for (i = 0; i < MAC_FW_START_TIMEOUT_MS; i++) {
   2013   1.1       ryo 		sc->sc_fw_version = AQ_READ_REG(sc, AQ_FW_VERSION_REG);
   2014   1.1       ryo 		if (sc->sc_fw_version != 0) {
   2015   1.1       ryo 			error = 0;
   2016   1.1       ryo 			break;
   2017   1.1       ryo 		}
   2018   1.1       ryo 		delay(1000);
   2019   1.1       ryo 	}
   2020   1.1       ryo 	return error;
   2021   1.1       ryo }
   2022   1.1       ryo 
   2023   1.1       ryo static int
   2024   1.1       ryo aq_fw_reset(struct aq_softc *sc)
   2025   1.1       ryo {
   2026   1.1       ryo 	uint32_t ver, v, bootExitCode;
   2027   1.1       ryo 	int i, error;
   2028   1.1       ryo 
   2029   1.1       ryo 	ver = AQ_READ_REG(sc, AQ_FW_VERSION_REG);
   2030   1.1       ryo 
   2031   1.1       ryo 	for (i = 1000; i > 0; i--) {
   2032   1.1       ryo 		v = AQ_READ_REG(sc, FW_MPI_DAISY_CHAIN_STATUS_REG);
   2033   1.1       ryo 		bootExitCode = AQ_READ_REG(sc, FW_BOOT_EXIT_CODE_REG);
   2034   1.1       ryo 		if (v != 0x06000000 || bootExitCode != 0)
   2035   1.1       ryo 			break;
   2036   1.1       ryo 	}
   2037   1.1       ryo 	if (i <= 0) {
   2038   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2039   1.1       ryo 		    "F/W reset failed. Neither RBL nor FLB started\n");
   2040   1.1       ryo 		return ETIMEDOUT;
   2041   1.1       ryo 	}
   2042   1.1       ryo 	sc->sc_rbl_enabled = (bootExitCode != 0);
   2043   1.1       ryo 
   2044   1.1       ryo 	/*
   2045   1.1       ryo 	 * Having FW version 0 is an indicator that cold start
   2046   1.1       ryo 	 * is in progress. This means two things:
   2047   1.1       ryo 	 * 1) Driver have to wait for FW/HW to finish boot (500ms giveup)
   2048   1.1       ryo 	 * 2) Driver may skip reset sequence and save time.
   2049   1.1       ryo 	 */
   2050   1.1       ryo 	if (sc->sc_fast_start_enabled && (ver != 0)) {
   2051   1.1       ryo 		error = aq_fw_read_version(sc);
   2052   1.1       ryo 		/* Skip reset as it just completed */
   2053   1.1       ryo 		if (error == 0)
   2054   1.1       ryo 			return 0;
   2055   1.1       ryo 	}
   2056   1.1       ryo 
   2057   1.1       ryo 	aq_fw_bootloader_mode_t mode = FW_BOOT_MODE_UNKNOWN;
   2058   1.1       ryo 	error = mac_soft_reset(sc, &mode);
   2059   1.1       ryo 	if (error != 0) {
   2060   1.1       ryo 		aprint_error_dev(sc->sc_dev, "MAC reset failed: %d\n", error);
   2061   1.1       ryo 		return error;
   2062   1.1       ryo 	}
   2063   1.1       ryo 
   2064   1.1       ryo 	switch (mode) {
   2065   1.1       ryo 	case FW_BOOT_MODE_FLB:
   2066   1.1       ryo 		aprint_debug_dev(sc->sc_dev,
   2067   1.1       ryo 		    "FLB> F/W successfully loaded from flash.\n");
   2068   1.1       ryo 		sc->sc_flash_present = true;
   2069   1.1       ryo 		return aq_fw_read_version(sc);
   2070   1.1       ryo 	case FW_BOOT_MODE_RBL_FLASH:
   2071   1.1       ryo 		aprint_debug_dev(sc->sc_dev,
   2072   1.1       ryo 		    "RBL> F/W loaded from flash. Host Bootload disabled.\n");
   2073   1.1       ryo 		sc->sc_flash_present = true;
   2074   1.1       ryo 		return aq_fw_read_version(sc);
   2075   1.1       ryo 	case FW_BOOT_MODE_UNKNOWN:
   2076   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2077   1.1       ryo 		    "F/W bootload error: unknown bootloader type\n");
   2078   1.1       ryo 		return ENOTSUP;
   2079   1.1       ryo 	case FW_BOOT_MODE_RBL_HOST_BOOTLOAD:
   2080   1.1       ryo 		aprint_debug_dev(sc->sc_dev, "RBL> Host Bootload mode\n");
   2081   1.1       ryo 		break;
   2082   1.1       ryo 	}
   2083   1.1       ryo 
   2084   1.1       ryo 	/*
   2085   1.1       ryo 	 * XXX: TODO: add support Host Boot
   2086   1.1       ryo 	 */
   2087   1.1       ryo 	aprint_error_dev(sc->sc_dev,
   2088   1.1       ryo 	    "RBL> F/W Host Bootload not implemented\n");
   2089   1.1       ryo 	return ENOTSUP;
   2090   1.1       ryo }
   2091   1.1       ryo 
   2092   1.1       ryo static int
   2093   1.1       ryo aq_hw_reset(struct aq_softc *sc)
   2094   1.1       ryo {
   2095   1.1       ryo 	int error;
   2096   1.1       ryo 
   2097   1.1       ryo 	/* disable irq */
   2098   1.1       ryo 	AQ_WRITE_REG_BIT(sc, AQ_INTR_CTRL_REG, AQ_INTR_CTRL_RESET_DIS, 0);
   2099   1.1       ryo 
   2100   1.1       ryo 	/* apply */
   2101   1.1       ryo 	AQ_WRITE_REG_BIT(sc, AQ_INTR_CTRL_REG, AQ_INTR_CTRL_RESET_IRQ, 1);
   2102   1.1       ryo 
   2103   1.1       ryo 	/* wait ack 10 times by 1ms */
   2104   1.1       ryo 	WAIT_FOR(
   2105   1.1       ryo 	    (AQ_READ_REG(sc, AQ_INTR_CTRL_REG) & AQ_INTR_CTRL_RESET_IRQ) == 0,
   2106   1.1       ryo 	    1000, 10, &error);
   2107   1.1       ryo 	if (error != 0) {
   2108   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2109   1.1       ryo 		    "atlantic: IRQ reset failed: %d\n", error);
   2110   1.1       ryo 		return error;
   2111   1.1       ryo 	}
   2112   1.1       ryo 
   2113   1.1       ryo 	return sc->sc_fw_ops->reset(sc);
   2114   1.1       ryo }
   2115   1.1       ryo 
   2116   1.1       ryo static int
   2117   1.1       ryo aq_hw_init_ucp(struct aq_softc *sc)
   2118   1.1       ryo {
   2119   1.1       ryo 	int timo;
   2120   1.1       ryo 
   2121   1.1       ryo 	if (FW_VERSION_MAJOR(sc) == 1) {
   2122  1.30       ryo 		if (AQ_READ_REG(sc, FW1X_MPI_INIT2_REG) == 0)
   2123  1.30       ryo 			AQ_WRITE_REG(sc, FW1X_MPI_INIT2_REG, 0xfefefefe);
   2124   1.1       ryo 		AQ_WRITE_REG(sc, FW1X_MPI_INIT1_REG, 0);
   2125   1.1       ryo 	}
   2126   1.1       ryo 
   2127  1.30       ryo 	/* Wait a maximum of 10sec. It usually takes about 5sec. */
   2128  1.30       ryo 	for (timo = 10000; timo > 0; timo--) {
   2129   1.1       ryo 		sc->sc_mbox_addr = AQ_READ_REG(sc, FW_MPI_MBOX_ADDR_REG);
   2130   1.1       ryo 		if (sc->sc_mbox_addr != 0)
   2131   1.1       ryo 			break;
   2132   1.1       ryo 		delay(1000);
   2133   1.1       ryo 	}
   2134  1.30       ryo 	if (sc->sc_mbox_addr == 0) {
   2135  1.30       ryo 		aprint_error_dev(sc->sc_dev, "cannot get mbox addr\n");
   2136  1.30       ryo 		return ETIMEDOUT;
   2137  1.30       ryo 	}
   2138   1.1       ryo 
   2139   1.1       ryo #define AQ_FW_MIN_VERSION	0x01050006
   2140   1.1       ryo #define AQ_FW_MIN_VERSION_STR	"1.5.6"
   2141   1.1       ryo 	if (sc->sc_fw_version < AQ_FW_MIN_VERSION) {
   2142   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2143   1.1       ryo 		    "atlantic: wrong FW version: " AQ_FW_MIN_VERSION_STR
   2144   1.1       ryo 		    " or later required, this is %d.%d.%d\n",
   2145   1.1       ryo 		    FW_VERSION_MAJOR(sc),
   2146   1.1       ryo 		    FW_VERSION_MINOR(sc),
   2147   1.1       ryo 		    FW_VERSION_BUILD(sc));
   2148   1.1       ryo 		return ENOTSUP;
   2149   1.1       ryo 	}
   2150   1.1       ryo 
   2151   1.1       ryo 	return 0;
   2152   1.1       ryo }
   2153   1.1       ryo 
   2154   1.1       ryo static int
   2155   1.1       ryo aq_fw_version_init(struct aq_softc *sc)
   2156   1.1       ryo {
   2157   1.1       ryo 	int error = 0;
   2158   1.1       ryo 	char fw_vers[sizeof("F/W version xxxxx.xxxxx.xxxxx")];
   2159   1.1       ryo 
   2160   1.1       ryo 	if (FW_VERSION_MAJOR(sc) == 1) {
   2161   1.1       ryo 		sc->sc_fw_ops = &aq_fw1x_ops;
   2162   1.1       ryo 	} else if ((FW_VERSION_MAJOR(sc) == 2) || (FW_VERSION_MAJOR(sc) == 3)) {
   2163   1.1       ryo 		sc->sc_fw_ops = &aq_fw2x_ops;
   2164   1.1       ryo 	} else {
   2165   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2166   1.1       ryo 		    "Unsupported F/W version %d.%d.%d\n",
   2167   1.1       ryo 		    FW_VERSION_MAJOR(sc), FW_VERSION_MINOR(sc),
   2168   1.1       ryo 		    FW_VERSION_BUILD(sc));
   2169   1.1       ryo 		return ENOTSUP;
   2170   1.1       ryo 	}
   2171   1.1       ryo 	snprintf(fw_vers, sizeof(fw_vers), "F/W version %d.%d.%d",
   2172   1.1       ryo 	    FW_VERSION_MAJOR(sc), FW_VERSION_MINOR(sc), FW_VERSION_BUILD(sc));
   2173   1.1       ryo 
   2174   1.1       ryo 	/* detect revision */
   2175   1.1       ryo 	uint32_t hwrev = AQ_READ_REG(sc, AQ_HW_REVISION_REG);
   2176   1.1       ryo 	switch (hwrev & 0x0000000f) {
   2177   1.1       ryo 	case 0x01:
   2178   1.1       ryo 		aprint_normal_dev(sc->sc_dev, "Atlantic revision A0, %s\n",
   2179   1.1       ryo 		    fw_vers);
   2180   1.1       ryo 		sc->sc_features |= FEATURES_REV_A0 |
   2181   1.1       ryo 		    FEATURES_MPI_AQ | FEATURES_MIPS;
   2182  1.23       ryo 		sc->sc_max_mtu = AQ_JUMBO_MTU_REV_A;
   2183   1.1       ryo 		break;
   2184   1.1       ryo 	case 0x02:
   2185   1.1       ryo 		aprint_normal_dev(sc->sc_dev, "Atlantic revision B0, %s\n",
   2186   1.1       ryo 		    fw_vers);
   2187   1.1       ryo 		sc->sc_features |= FEATURES_REV_B0 |
   2188   1.1       ryo 		    FEATURES_MPI_AQ | FEATURES_MIPS |
   2189   1.1       ryo 		    FEATURES_TPO2 | FEATURES_RPF2;
   2190  1.23       ryo 		sc->sc_max_mtu = AQ_JUMBO_MTU_REV_B;
   2191   1.1       ryo 		break;
   2192   1.1       ryo 	case 0x0A:
   2193   1.1       ryo 		aprint_normal_dev(sc->sc_dev, "Atlantic revision B1, %s\n",
   2194   1.1       ryo 		    fw_vers);
   2195   1.1       ryo 		sc->sc_features |= FEATURES_REV_B1 |
   2196   1.1       ryo 		    FEATURES_MPI_AQ | FEATURES_MIPS |
   2197   1.1       ryo 		    FEATURES_TPO2 | FEATURES_RPF2;
   2198  1.23       ryo 		sc->sc_max_mtu = AQ_JUMBO_MTU_REV_B;
   2199   1.1       ryo 		break;
   2200   1.1       ryo 	default:
   2201   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2202   1.1       ryo 		    "Unknown revision (0x%08x)\n", hwrev);
   2203  1.23       ryo 		sc->sc_features = 0;
   2204  1.23       ryo 		sc->sc_max_mtu = ETHERMTU;
   2205   1.1       ryo 		error = ENOTSUP;
   2206   1.1       ryo 		break;
   2207   1.1       ryo 	}
   2208   1.1       ryo 	return error;
   2209   1.1       ryo }
   2210   1.1       ryo 
   2211   1.1       ryo static int
   2212   1.1       ryo fw1x_reset(struct aq_softc *sc)
   2213   1.1       ryo {
   2214   1.1       ryo 	struct aq_mailbox_header mbox;
   2215   1.1       ryo 	const int retryCount = 1000;
   2216   1.1       ryo 	uint32_t tid0;
   2217   1.1       ryo 	int i;
   2218   1.1       ryo 
   2219   1.1       ryo 	tid0 = ~0;	/*< Initial value of MBOX transactionId. */
   2220   1.1       ryo 
   2221   1.1       ryo 	for (i = 0; i < retryCount; ++i) {
   2222   1.1       ryo 		/*
   2223   1.1       ryo 		 * Read the beginning of Statistics structure to capture
   2224   1.1       ryo 		 * the Transaction ID.
   2225   1.1       ryo 		 */
   2226   1.1       ryo 		aq_fw_downld_dwords(sc, sc->sc_mbox_addr,
   2227   1.1       ryo 		    (uint32_t *)&mbox, sizeof(mbox) / sizeof(uint32_t));
   2228   1.1       ryo 
   2229   1.1       ryo 		/* Successfully read the stats. */
   2230   1.1       ryo 		if (tid0 == ~0U) {
   2231   1.1       ryo 			/* We have read the initial value. */
   2232   1.1       ryo 			tid0 = mbox.transaction_id;
   2233   1.1       ryo 			continue;
   2234   1.1       ryo 		} else if (mbox.transaction_id != tid0) {
   2235   1.1       ryo 			/*
   2236   1.1       ryo 			 * Compare transaction ID to initial value.
   2237   1.1       ryo 			 * If it's different means f/w is alive.
   2238   1.1       ryo 			 * We're done.
   2239   1.1       ryo 			 */
   2240   1.1       ryo 			return 0;
   2241   1.1       ryo 		}
   2242   1.1       ryo 
   2243   1.1       ryo 		/*
   2244   1.1       ryo 		 * Transaction ID value haven't changed since last time.
   2245   1.1       ryo 		 * Try reading the stats again.
   2246   1.1       ryo 		 */
   2247   1.1       ryo 		delay(10);
   2248   1.1       ryo 	}
   2249   1.1       ryo 	aprint_error_dev(sc->sc_dev, "F/W 1.x reset finalize timeout\n");
   2250   1.1       ryo 	return EBUSY;
   2251   1.1       ryo }
   2252   1.1       ryo 
   2253   1.1       ryo static int
   2254   1.1       ryo fw1x_set_mode(struct aq_softc *sc, aq_hw_fw_mpi_state_t mode,
   2255   1.1       ryo     aq_link_speed_t speed, aq_link_fc_t fc, aq_link_eee_t eee)
   2256   1.1       ryo {
   2257   1.1       ryo 	uint32_t mpictrl = 0;
   2258   1.1       ryo 	uint32_t mpispeed = 0;
   2259   1.1       ryo 
   2260   1.1       ryo 	if (speed & AQ_LINK_10G)
   2261   1.1       ryo 		mpispeed |= FW1X_CTRL_10G;
   2262   1.1       ryo 	if (speed & AQ_LINK_5G)
   2263   1.1       ryo 		mpispeed |= (FW1X_CTRL_5G | FW1X_CTRL_5GSR);
   2264   1.1       ryo 	if (speed & AQ_LINK_2G5)
   2265   1.1       ryo 		mpispeed |= FW1X_CTRL_2G5;
   2266   1.1       ryo 	if (speed & AQ_LINK_1G)
   2267   1.1       ryo 		mpispeed |= FW1X_CTRL_1G;
   2268   1.1       ryo 	if (speed & AQ_LINK_100M)
   2269   1.1       ryo 		mpispeed |= FW1X_CTRL_100M;
   2270   1.1       ryo 
   2271   1.1       ryo 	mpictrl |= __SHIFTIN(mode, FW1X_MPI_STATE_MODE);
   2272   1.1       ryo 	mpictrl |= __SHIFTIN(mpispeed, FW1X_MPI_STATE_SPEED);
   2273   1.1       ryo 	AQ_WRITE_REG(sc, FW1X_MPI_CONTROL_REG, mpictrl);
   2274   1.1       ryo 	return 0;
   2275   1.1       ryo }
   2276   1.1       ryo 
   2277   1.1       ryo static int
   2278   1.1       ryo fw1x_get_mode(struct aq_softc *sc, aq_hw_fw_mpi_state_t *modep,
   2279   1.1       ryo     aq_link_speed_t *speedp, aq_link_fc_t *fcp, aq_link_eee_t *eeep)
   2280   1.1       ryo {
   2281   1.1       ryo 	uint32_t mpistate, mpi_speed;
   2282   1.1       ryo 	aq_link_speed_t speed = AQ_LINK_NONE;
   2283   1.1       ryo 
   2284   1.1       ryo 	mpistate = AQ_READ_REG(sc, FW1X_MPI_STATE_REG);
   2285   1.1       ryo 
   2286   1.1       ryo 	if (modep != NULL)
   2287   1.1       ryo 		*modep = __SHIFTOUT(mpistate, FW1X_MPI_STATE_MODE);
   2288   1.1       ryo 
   2289   1.1       ryo 	mpi_speed = __SHIFTOUT(mpistate, FW1X_MPI_STATE_SPEED);
   2290   1.1       ryo 	if (mpi_speed & FW1X_CTRL_10G)
   2291   1.1       ryo 		speed = AQ_LINK_10G;
   2292   1.1       ryo 	else if (mpi_speed & (FW1X_CTRL_5G|FW1X_CTRL_5GSR))
   2293   1.1       ryo 		speed = AQ_LINK_5G;
   2294   1.1       ryo 	else if (mpi_speed & FW1X_CTRL_2G5)
   2295   1.1       ryo 		speed = AQ_LINK_2G5;
   2296   1.1       ryo 	else if (mpi_speed & FW1X_CTRL_1G)
   2297   1.1       ryo 		speed = AQ_LINK_1G;
   2298   1.1       ryo 	else if (mpi_speed & FW1X_CTRL_100M)
   2299   1.1       ryo 		speed = AQ_LINK_100M;
   2300   1.1       ryo 
   2301   1.1       ryo 	if (speedp != NULL)
   2302   1.1       ryo 		*speedp = speed;
   2303   1.1       ryo 
   2304   1.1       ryo 	if (fcp != NULL)
   2305   1.1       ryo 		*fcp = AQ_FC_NONE;
   2306   1.1       ryo 
   2307   1.1       ryo 	if (eeep != NULL)
   2308   1.1       ryo 		*eeep = AQ_EEE_DISABLE;
   2309   1.1       ryo 
   2310   1.1       ryo 	return 0;
   2311   1.1       ryo }
   2312   1.1       ryo 
   2313   1.1       ryo static int
   2314   1.1       ryo fw1x_get_stats(struct aq_softc *sc, aq_hw_stats_s_t *stats)
   2315   1.1       ryo {
   2316   1.1       ryo 	int error;
   2317   1.1       ryo 
   2318   1.1       ryo 	error = aq_fw_downld_dwords(sc,
   2319   1.1       ryo 	    sc->sc_mbox_addr + offsetof(fw1x_mailbox_t, msm), (uint32_t *)stats,
   2320   1.1       ryo 	    sizeof(aq_hw_stats_s_t) / sizeof(uint32_t));
   2321   1.1       ryo 	if (error < 0) {
   2322   1.1       ryo 		device_printf(sc->sc_dev,
   2323   1.1       ryo 		    "fw1x> download statistics data FAILED, error %d", error);
   2324   1.1       ryo 		return error;
   2325   1.1       ryo 	}
   2326   1.1       ryo 
   2327   1.1       ryo 	stats->dpc = AQ_READ_REG(sc, RX_DMA_DROP_PKT_CNT_REG);
   2328   1.1       ryo 	stats->cprc = AQ_READ_REG(sc, RX_DMA_COALESCED_PKT_CNT_REG);
   2329   1.1       ryo 	return 0;
   2330   1.1       ryo }
   2331   1.1       ryo 
   2332   1.1       ryo static int
   2333   1.1       ryo fw2x_reset(struct aq_softc *sc)
   2334   1.1       ryo {
   2335   1.1       ryo 	fw2x_capabilities_t caps = { 0 };
   2336   1.1       ryo 	int error;
   2337   1.1       ryo 
   2338   1.1       ryo 	error = aq_fw_downld_dwords(sc,
   2339   1.1       ryo 	    sc->sc_mbox_addr + offsetof(fw2x_mailbox_t, caps),
   2340   1.1       ryo 	    (uint32_t *)&caps, sizeof caps / sizeof(uint32_t));
   2341   1.1       ryo 	if (error != 0) {
   2342   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2343   1.1       ryo 		    "fw2x> can't get F/W capabilities mask, error %d\n",
   2344   1.1       ryo 		    error);
   2345   1.1       ryo 		return error;
   2346   1.1       ryo 	}
   2347   1.1       ryo 	sc->sc_fw_caps = caps.caps_lo | ((uint64_t)caps.caps_hi << 32);
   2348   1.1       ryo 
   2349   1.1       ryo 	char buf[256];
   2350   1.1       ryo 	snprintb(buf, sizeof(buf), FW2X_SNPRINTB, sc->sc_fw_caps);
   2351   1.1       ryo 	aprint_verbose_dev(sc->sc_dev, "fw2x> F/W capabilities=%s\n", buf);
   2352   1.1       ryo 
   2353   1.1       ryo 	return 0;
   2354   1.1       ryo }
   2355   1.1       ryo 
   2356   1.1       ryo static int
   2357   1.1       ryo fw2x_set_mode(struct aq_softc *sc, aq_hw_fw_mpi_state_t mode,
   2358   1.1       ryo     aq_link_speed_t speed, aq_link_fc_t fc, aq_link_eee_t eee)
   2359   1.1       ryo {
   2360   1.4       ryo 	uint64_t mpi_ctrl;
   2361   1.4       ryo 	int error = 0;
   2362   1.4       ryo 
   2363   1.4       ryo 	AQ_MPI_LOCK(sc);
   2364   1.4       ryo 
   2365   1.4       ryo 	mpi_ctrl = AQ_READ64_REG(sc, FW2X_MPI_CONTROL_REG);
   2366   1.1       ryo 
   2367   1.1       ryo 	switch (mode) {
   2368   1.1       ryo 	case MPI_INIT:
   2369   1.1       ryo 		mpi_ctrl &= ~FW2X_CTRL_RATE_MASK;
   2370   1.1       ryo 		if (speed & AQ_LINK_10G)
   2371   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_RATE_10G;
   2372   1.1       ryo 		if (speed & AQ_LINK_5G)
   2373   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_RATE_5G;
   2374   1.1       ryo 		if (speed & AQ_LINK_2G5)
   2375   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_RATE_2G5;
   2376   1.1       ryo 		if (speed & AQ_LINK_1G)
   2377   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_RATE_1G;
   2378   1.1       ryo 		if (speed & AQ_LINK_100M)
   2379   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_RATE_100M;
   2380   1.1       ryo 
   2381   1.1       ryo 		mpi_ctrl &= ~FW2X_CTRL_LINK_DROP;
   2382   1.1       ryo 
   2383   1.1       ryo 		mpi_ctrl &= ~FW2X_CTRL_EEE_MASK;
   2384   1.1       ryo 		if (eee == AQ_EEE_ENABLE)
   2385   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_EEE_MASK;
   2386   1.1       ryo 
   2387   1.1       ryo 		mpi_ctrl &= ~(FW2X_CTRL_PAUSE | FW2X_CTRL_ASYMMETRIC_PAUSE);
   2388   1.1       ryo 		if (fc & AQ_FC_RX)
   2389   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_PAUSE;
   2390   1.1       ryo 		if (fc & AQ_FC_TX)
   2391   1.1       ryo 			mpi_ctrl |= FW2X_CTRL_ASYMMETRIC_PAUSE;
   2392   1.1       ryo 		break;
   2393   1.1       ryo 	case MPI_DEINIT:
   2394   1.1       ryo 		mpi_ctrl &= ~(FW2X_CTRL_RATE_MASK | FW2X_CTRL_EEE_MASK);
   2395   1.1       ryo 		mpi_ctrl &= ~(FW2X_CTRL_PAUSE | FW2X_CTRL_ASYMMETRIC_PAUSE);
   2396   1.1       ryo 		break;
   2397   1.1       ryo 	default:
   2398   1.1       ryo 		device_printf(sc->sc_dev, "fw2x> unknown MPI state %d\n", mode);
   2399   1.4       ryo 		error =  EINVAL;
   2400   1.4       ryo 		goto failure;
   2401   1.1       ryo 	}
   2402   1.4       ryo 	AQ_WRITE64_REG(sc, FW2X_MPI_CONTROL_REG, mpi_ctrl);
   2403   1.1       ryo 
   2404   1.4       ryo  failure:
   2405   1.4       ryo 	AQ_MPI_UNLOCK(sc);
   2406   1.4       ryo 	return error;
   2407   1.1       ryo }
   2408   1.1       ryo 
   2409   1.1       ryo static int
   2410   1.1       ryo fw2x_get_mode(struct aq_softc *sc, aq_hw_fw_mpi_state_t *modep,
   2411   1.1       ryo     aq_link_speed_t *speedp, aq_link_fc_t *fcp, aq_link_eee_t *eeep)
   2412   1.1       ryo {
   2413   1.1       ryo 	uint64_t mpi_state = AQ_READ64_REG(sc, FW2X_MPI_STATE_REG);
   2414   1.1       ryo 
   2415   1.1       ryo 	if (modep != NULL) {
   2416   1.1       ryo 		uint64_t mpi_ctrl = AQ_READ64_REG(sc, FW2X_MPI_CONTROL_REG);
   2417   1.1       ryo 		if (mpi_ctrl & FW2X_CTRL_RATE_MASK)
   2418   1.1       ryo 			*modep = MPI_INIT;
   2419   1.1       ryo 		else
   2420   1.1       ryo 			*modep = MPI_DEINIT;
   2421   1.1       ryo 	}
   2422   1.1       ryo 
   2423   1.1       ryo 	aq_link_speed_t speed = AQ_LINK_NONE;
   2424   1.1       ryo 	if (mpi_state & FW2X_CTRL_RATE_10G)
   2425   1.1       ryo 		speed = AQ_LINK_10G;
   2426   1.1       ryo 	else if (mpi_state & FW2X_CTRL_RATE_5G)
   2427   1.1       ryo 		speed = AQ_LINK_5G;
   2428   1.1       ryo 	else if (mpi_state & FW2X_CTRL_RATE_2G5)
   2429   1.1       ryo 		speed = AQ_LINK_2G5;
   2430   1.1       ryo 	else if (mpi_state & FW2X_CTRL_RATE_1G)
   2431   1.1       ryo 		speed = AQ_LINK_1G;
   2432   1.1       ryo 	else if (mpi_state & FW2X_CTRL_RATE_100M)
   2433   1.1       ryo 		speed = AQ_LINK_100M;
   2434   1.1       ryo 
   2435   1.1       ryo 	if (speedp != NULL)
   2436   1.1       ryo 		*speedp = speed;
   2437   1.1       ryo 
   2438   1.1       ryo 	aq_link_fc_t fc = AQ_FC_NONE;
   2439   1.1       ryo 	if (mpi_state & FW2X_CTRL_PAUSE)
   2440   1.1       ryo 		fc |= AQ_FC_RX;
   2441   1.1       ryo 	if (mpi_state & FW2X_CTRL_ASYMMETRIC_PAUSE)
   2442   1.1       ryo 		fc |= AQ_FC_TX;
   2443   1.1       ryo 	if (fcp != NULL)
   2444   1.1       ryo 		*fcp = fc;
   2445   1.1       ryo 
   2446   1.1       ryo 	/* XXX: TODO: EEE */
   2447   1.1       ryo 	if (eeep != NULL)
   2448   1.1       ryo 		*eeep = AQ_EEE_DISABLE;
   2449   1.1       ryo 
   2450   1.1       ryo 	return 0;
   2451   1.1       ryo }
   2452   1.1       ryo 
   2453   1.1       ryo static int
   2454   1.1       ryo toggle_mpi_ctrl_and_wait(struct aq_softc *sc, uint64_t mask,
   2455   1.1       ryo     uint32_t timeout_ms, uint32_t try_count)
   2456   1.1       ryo {
   2457   1.1       ryo 	uint64_t mpi_ctrl = AQ_READ64_REG(sc, FW2X_MPI_CONTROL_REG);
   2458   1.1       ryo 	uint64_t mpi_state = AQ_READ64_REG(sc, FW2X_MPI_STATE_REG);
   2459   1.1       ryo 	int error;
   2460   1.1       ryo 
   2461   1.1       ryo 	/* First, check that control and state values are consistent */
   2462   1.1       ryo 	if ((mpi_ctrl & mask) != (mpi_state & mask)) {
   2463   1.1       ryo 		device_printf(sc->sc_dev,
   2464   1.1       ryo 		    "fw2x> MPI control (%#llx) and state (%#llx)"
   2465   1.1       ryo 		    " are not consistent for mask %#llx!\n",
   2466   1.1       ryo 		    (unsigned long long)mpi_ctrl, (unsigned long long)mpi_state,
   2467   1.1       ryo 		    (unsigned long long)mask);
   2468   1.1       ryo 		return EINVAL;
   2469   1.1       ryo 	}
   2470   1.1       ryo 
   2471   1.1       ryo 	/* Invert bits (toggle) in control register */
   2472   1.1       ryo 	mpi_ctrl ^= mask;
   2473   1.1       ryo 	AQ_WRITE64_REG(sc, FW2X_MPI_CONTROL_REG, mpi_ctrl);
   2474   1.1       ryo 
   2475   1.1       ryo 	/* Clear all bits except masked */
   2476   1.1       ryo 	mpi_ctrl &= mask;
   2477   1.1       ryo 
   2478   1.1       ryo 	/* Wait for FW reflecting change in state register */
   2479   1.1       ryo 	WAIT_FOR((AQ_READ64_REG(sc, FW2X_MPI_CONTROL_REG) & mask) == mpi_ctrl,
   2480   1.1       ryo 	    1000 * timeout_ms, try_count, &error);
   2481   1.1       ryo 	if (error != 0) {
   2482   1.1       ryo 		device_printf(sc->sc_dev,
   2483   1.1       ryo 		    "f/w2x> timeout while waiting for response"
   2484   1.1       ryo 		    " in state register for bit %#llx!",
   2485   1.1       ryo 		    (unsigned long long)mask);
   2486   1.1       ryo 		return error;
   2487   1.1       ryo 	}
   2488   1.1       ryo 	return 0;
   2489   1.1       ryo }
   2490   1.1       ryo 
   2491   1.1       ryo static int
   2492   1.1       ryo fw2x_get_stats(struct aq_softc *sc, aq_hw_stats_s_t *stats)
   2493   1.1       ryo {
   2494   1.1       ryo 	int error;
   2495   1.1       ryo 
   2496   1.4       ryo 	AQ_MPI_LOCK(sc);
   2497   1.1       ryo 	/* Say to F/W to update the statistics */
   2498   1.1       ryo 	error = toggle_mpi_ctrl_and_wait(sc, FW2X_CTRL_STATISTICS, 1, 25);
   2499   1.1       ryo 	if (error != 0) {
   2500   1.1       ryo 		device_printf(sc->sc_dev,
   2501   1.1       ryo 		    "fw2x> statistics update error %d\n", error);
   2502   1.4       ryo 		goto failure;
   2503   1.1       ryo 	}
   2504   1.1       ryo 
   2505   1.1       ryo 	CTASSERT(sizeof(fw2x_msm_statistics_t) <= sizeof(struct aq_hw_stats_s));
   2506   1.1       ryo 	error = aq_fw_downld_dwords(sc,
   2507   1.1       ryo 	    sc->sc_mbox_addr + offsetof(fw2x_mailbox_t, msm), (uint32_t *)stats,
   2508   1.1       ryo 	    sizeof(fw2x_msm_statistics_t) / sizeof(uint32_t));
   2509   1.1       ryo 	if (error != 0) {
   2510   1.1       ryo 		device_printf(sc->sc_dev,
   2511   1.1       ryo 		    "fw2x> download statistics data FAILED, error %d", error);
   2512   1.4       ryo 		goto failure;
   2513   1.1       ryo 	}
   2514   1.1       ryo 	stats->dpc = AQ_READ_REG(sc, RX_DMA_DROP_PKT_CNT_REG);
   2515   1.1       ryo 	stats->cprc = AQ_READ_REG(sc, RX_DMA_COALESCED_PKT_CNT_REG);
   2516   1.1       ryo 
   2517   1.4       ryo  failure:
   2518   1.4       ryo 	AQ_MPI_UNLOCK(sc);
   2519   1.4       ryo 	return error;
   2520   1.4       ryo }
   2521   1.4       ryo 
   2522   1.4       ryo #if NSYSMON_ENVSYS > 0
   2523   1.4       ryo static int
   2524   1.4       ryo fw2x_get_temperature(struct aq_softc *sc, uint32_t *temp)
   2525   1.4       ryo {
   2526   1.4       ryo 	int error;
   2527   1.4       ryo 	uint32_t value, celsius;
   2528   1.4       ryo 
   2529   1.4       ryo 	AQ_MPI_LOCK(sc);
   2530   1.4       ryo 
   2531   1.4       ryo 	/* Say to F/W to update the temperature */
   2532   1.4       ryo 	error = toggle_mpi_ctrl_and_wait(sc, FW2X_CTRL_TEMPERATURE, 1, 25);
   2533   1.4       ryo 	if (error != 0)
   2534   1.4       ryo 		goto failure;
   2535   1.4       ryo 
   2536   1.4       ryo 	error = aq_fw_downld_dwords(sc,
   2537   1.4       ryo 	    sc->sc_mbox_addr + offsetof(fw2x_mailbox_t, phy_info2),
   2538   1.4       ryo 	    &value, sizeof(value) / sizeof(uint32_t));
   2539   1.4       ryo 	if (error != 0)
   2540   1.4       ryo 		goto failure;
   2541   1.4       ryo 
   2542   1.4       ryo 	/* 1/256 decrees C to microkelvin */
   2543   1.4       ryo 	celsius = __SHIFTOUT(value, PHYINFO2_TEMPERATURE);
   2544   1.4       ryo 	if (celsius == 0) {
   2545   1.4       ryo 		error = EIO;
   2546   1.4       ryo 		goto failure;
   2547   1.4       ryo 	}
   2548   1.4       ryo 	*temp = celsius * (1000000 / 256) + 273150000;
   2549   1.4       ryo 
   2550   1.4       ryo  failure:
   2551   1.4       ryo 	AQ_MPI_UNLOCK(sc);
   2552   1.1       ryo 	return 0;
   2553   1.1       ryo }
   2554   1.4       ryo #endif
   2555   1.1       ryo 
   2556   1.1       ryo static int
   2557   1.1       ryo aq_fw_downld_dwords(struct aq_softc *sc, uint32_t addr, uint32_t *p,
   2558   1.1       ryo     uint32_t cnt)
   2559   1.1       ryo {
   2560   1.1       ryo 	uint32_t v;
   2561   1.1       ryo 	int error = 0;
   2562   1.1       ryo 
   2563   1.1       ryo 	WAIT_FOR(AQ_READ_REG(sc, AQ_FW_SEM_RAM_REG) == 1, 1, 10000, &error);
   2564   1.1       ryo 	if (error != 0) {
   2565   1.1       ryo 		AQ_WRITE_REG(sc, AQ_FW_SEM_RAM_REG, 1);
   2566   1.1       ryo 		v = AQ_READ_REG(sc, AQ_FW_SEM_RAM_REG);
   2567   1.1       ryo 		if (v == 0) {
   2568   1.1       ryo 			device_printf(sc->sc_dev,
   2569   1.1       ryo 			    "%s:%d: timeout\n", __func__, __LINE__);
   2570   1.1       ryo 			return ETIMEDOUT;
   2571   1.1       ryo 		}
   2572   1.1       ryo 	}
   2573   1.1       ryo 
   2574   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_MBOX_ADDR_REG, addr);
   2575   1.1       ryo 
   2576   1.1       ryo 	error = 0;
   2577   1.1       ryo 	for (; cnt > 0 && error == 0; cnt--) {
   2578   1.1       ryo 		/* execute mailbox interface */
   2579   1.1       ryo 		AQ_WRITE_REG_BIT(sc, AQ_FW_MBOX_CMD_REG,
   2580   1.1       ryo 		    AQ_FW_MBOX_CMD_EXECUTE, 1);
   2581   1.1       ryo 		if (sc->sc_features & FEATURES_REV_B1) {
   2582   1.1       ryo 			WAIT_FOR(AQ_READ_REG(sc, AQ_FW_MBOX_ADDR_REG) != addr,
   2583   1.1       ryo 			    1, 1000, &error);
   2584   1.1       ryo 		} else {
   2585   1.1       ryo 			WAIT_FOR((AQ_READ_REG(sc, AQ_FW_MBOX_CMD_REG) &
   2586   1.1       ryo 			    AQ_FW_MBOX_CMD_BUSY) == 0,
   2587   1.1       ryo 			    1, 1000, &error);
   2588   1.1       ryo 		}
   2589   1.1       ryo 		*p++ = AQ_READ_REG(sc, AQ_FW_MBOX_VAL_REG);
   2590   1.1       ryo 		addr += sizeof(uint32_t);
   2591   1.1       ryo 	}
   2592   1.1       ryo 	AQ_WRITE_REG(sc, AQ_FW_SEM_RAM_REG, 1);
   2593   1.1       ryo 
   2594   1.1       ryo 	if (error != 0)
   2595   1.1       ryo 		device_printf(sc->sc_dev,
   2596   1.1       ryo 		    "%s:%d: timeout\n", __func__, __LINE__);
   2597   1.1       ryo 
   2598   1.1       ryo 	return error;
   2599   1.1       ryo }
   2600   1.1       ryo 
   2601   1.1       ryo /* read my mac address */
   2602   1.1       ryo static int
   2603   1.1       ryo aq_get_mac_addr(struct aq_softc *sc)
   2604   1.1       ryo {
   2605   1.1       ryo 	uint32_t mac_addr[2];
   2606   1.1       ryo 	uint32_t efuse_shadow_addr;
   2607   1.1       ryo 	int err;
   2608   1.1       ryo 
   2609   1.1       ryo 	efuse_shadow_addr = 0;
   2610   1.1       ryo 	if (FW_VERSION_MAJOR(sc) >= 2)
   2611   1.1       ryo 		efuse_shadow_addr = AQ_READ_REG(sc, FW2X_MPI_EFUSEADDR_REG);
   2612   1.1       ryo 	else
   2613   1.1       ryo 		efuse_shadow_addr = AQ_READ_REG(sc, FW1X_MPI_EFUSEADDR_REG);
   2614   1.1       ryo 
   2615   1.1       ryo 	if (efuse_shadow_addr == 0) {
   2616   1.1       ryo 		aprint_error_dev(sc->sc_dev, "cannot get efuse addr\n");
   2617   1.1       ryo 		return ENXIO;
   2618   1.1       ryo 	}
   2619   1.1       ryo 
   2620   1.1       ryo 	memset(mac_addr, 0, sizeof(mac_addr));
   2621   1.1       ryo 	err = aq_fw_downld_dwords(sc, efuse_shadow_addr + (40 * 4),
   2622   1.1       ryo 	    mac_addr, __arraycount(mac_addr));
   2623   1.1       ryo 	if (err < 0)
   2624   1.1       ryo 		return err;
   2625   1.1       ryo 
   2626   1.1       ryo 	if (mac_addr[0] == 0 && mac_addr[1] == 0) {
   2627   1.1       ryo 		aprint_error_dev(sc->sc_dev, "mac address not found\n");
   2628   1.1       ryo 		return ENXIO;
   2629   1.1       ryo 	}
   2630   1.1       ryo 
   2631  1.18       ryo 	mac_addr[0] = htobe32(mac_addr[0]);
   2632  1.18       ryo 	mac_addr[1] = htobe32(mac_addr[1]);
   2633   1.1       ryo 
   2634   1.1       ryo 	memcpy(sc->sc_enaddr.ether_addr_octet,
   2635   1.1       ryo 	    (uint8_t *)mac_addr, ETHER_ADDR_LEN);
   2636   1.1       ryo 	aprint_normal_dev(sc->sc_dev, "Etheraddr: %s\n",
   2637   1.1       ryo 	    ether_sprintf(sc->sc_enaddr.ether_addr_octet));
   2638   1.1       ryo 
   2639   1.1       ryo 	return 0;
   2640   1.1       ryo }
   2641   1.1       ryo 
   2642   1.1       ryo /* set multicast filter. index 0 for own address */
   2643   1.1       ryo static int
   2644   1.1       ryo aq_set_mac_addr(struct aq_softc *sc, int index, uint8_t *enaddr)
   2645   1.1       ryo {
   2646   1.1       ryo 	uint32_t h, l;
   2647   1.1       ryo 
   2648   1.1       ryo 	if (index >= AQ_HW_MAC_NUM)
   2649   1.1       ryo 		return EINVAL;
   2650   1.1       ryo 
   2651   1.1       ryo 	if (enaddr == NULL) {
   2652   1.1       ryo 		/* disable */
   2653   1.1       ryo 		AQ_WRITE_REG_BIT(sc,
   2654   1.1       ryo 		    RPF_L2UC_MSW_REG(index), RPF_L2UC_MSW_EN, 0);
   2655   1.1       ryo 		return 0;
   2656   1.1       ryo 	}
   2657   1.1       ryo 
   2658   1.1       ryo 	h = (enaddr[0] <<  8) | (enaddr[1]);
   2659   1.5   msaitoh 	l = ((uint32_t)enaddr[2] << 24) | (enaddr[3] << 16) |
   2660   1.1       ryo 	    (enaddr[4] <<  8) | (enaddr[5]);
   2661   1.1       ryo 
   2662   1.1       ryo 	/* disable, set, and enable */
   2663   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(index), RPF_L2UC_MSW_EN, 0);
   2664   1.1       ryo 	AQ_WRITE_REG(sc, RPF_L2UC_LSW_REG(index), l);
   2665   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(index),
   2666   1.1       ryo 	    RPF_L2UC_MSW_MACADDR_HI, h);
   2667   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(index), RPF_L2UC_MSW_ACTION, 1);
   2668   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(index), RPF_L2UC_MSW_EN, 1);
   2669   1.1       ryo 
   2670   1.1       ryo 	return 0;
   2671   1.1       ryo }
   2672   1.1       ryo 
   2673   1.1       ryo static int
   2674   1.1       ryo aq_set_capability(struct aq_softc *sc)
   2675   1.1       ryo {
   2676  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   2677   1.1       ryo 	int ip4csum_tx =
   2678   1.1       ryo 	    ((ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) == 0) ? 0 : 1;
   2679   1.1       ryo 	int ip4csum_rx =
   2680   1.1       ryo 	    ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) == 0) ? 0 : 1;
   2681   1.1       ryo 	int l4csum_tx = ((ifp->if_capenable &
   2682   1.1       ryo 	   (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx |
   2683   1.1       ryo 	   IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx)) == 0) ? 0 : 1;
   2684   1.1       ryo 	int l4csum_rx =
   2685   1.1       ryo 	   ((ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   2686   1.1       ryo 	   IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)) == 0) ? 0 : 1;
   2687   1.1       ryo 	uint32_t lso =
   2688   1.1       ryo 	   ((ifp->if_capenable & (IFCAP_TSOv4 | IFCAP_TSOv6)) == 0) ?
   2689   1.1       ryo 	   0 : 0xffffffff;
   2690   1.1       ryo 	uint32_t lro = ((ifp->if_capenable & IFCAP_LRO) == 0) ?
   2691   1.1       ryo 	    0 : 0xffffffff;
   2692   1.1       ryo 	uint32_t i, v;
   2693   1.1       ryo 
   2694   1.1       ryo 	/* TX checksums offloads*/
   2695   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPO_HWCSUM_REG, TPO_HWCSUM_IP4CSUM_EN, ip4csum_tx);
   2696   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPO_HWCSUM_REG, TPO_HWCSUM_L4CSUM_EN, l4csum_tx);
   2697   1.1       ryo 
   2698   1.1       ryo 	/* RX checksums offloads*/
   2699   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_HWCSUM_REG, RPO_HWCSUM_IP4CSUM_EN, ip4csum_rx);
   2700   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_HWCSUM_REG, RPO_HWCSUM_L4CSUM_EN, l4csum_rx);
   2701   1.1       ryo 
   2702   1.1       ryo 	/* LSO offloads*/
   2703   1.1       ryo 	AQ_WRITE_REG(sc, TDM_LSO_EN_REG, lso);
   2704   1.1       ryo 
   2705   1.1       ryo #define AQ_B0_LRO_RXD_MAX	16
   2706   1.1       ryo 	v = (8 < AQ_B0_LRO_RXD_MAX) ? 3 :
   2707   1.1       ryo 	    (4 < AQ_B0_LRO_RXD_MAX) ? 2 :
   2708   1.1       ryo 	    (2 < AQ_B0_LRO_RXD_MAX) ? 1 : 0;
   2709   1.1       ryo 	for (i = 0; i < AQ_RINGS_NUM; i++) {
   2710   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPO_LRO_LDES_MAX_REG(i),
   2711   1.1       ryo 		    RPO_LRO_LDES_MAX_MASK(i), v);
   2712   1.1       ryo 	}
   2713   1.1       ryo 
   2714   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_TB_DIV_REG, RPO_LRO_TB_DIV, 0x61a);
   2715   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_INACTIVE_IVAL_REG,
   2716   1.1       ryo 	    RPO_LRO_INACTIVE_IVAL, 0);
   2717   1.1       ryo 	/*
   2718   1.1       ryo 	 * the LRO timebase divider is 5 uS (0x61a),
   2719   1.1       ryo 	 * to get a maximum coalescing interval of 250 uS,
   2720   1.1       ryo 	 * we need to multiply by 50(0x32) to get
   2721   1.1       ryo 	 * the default value 250 uS
   2722   1.1       ryo 	 */
   2723   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_MAX_COALESCING_IVAL_REG,
   2724   1.1       ryo 	    RPO_LRO_MAX_COALESCING_IVAL, 50);
   2725   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_CONF_REG,
   2726   1.1       ryo 	    RPO_LRO_CONF_QSESSION_LIMIT, 1);
   2727   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_CONF_REG,
   2728   1.1       ryo 	    RPO_LRO_CONF_TOTAL_DESC_LIMIT, 2);
   2729   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_CONF_REG,
   2730   1.1       ryo 	    RPO_LRO_CONF_PATCHOPTIMIZATION_EN, 0);
   2731   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPO_LRO_CONF_REG,
   2732   1.1       ryo 	    RPO_LRO_CONF_MIN_PAYLOAD_OF_FIRST_PKT, 10);
   2733   1.1       ryo 	AQ_WRITE_REG(sc, RPO_LRO_RSC_MAX_REG, 1);
   2734   1.1       ryo 	AQ_WRITE_REG(sc, RPO_LRO_ENABLE_REG, lro);
   2735   1.1       ryo 
   2736   1.1       ryo 	return 0;
   2737   1.1       ryo }
   2738   1.1       ryo 
   2739   1.1       ryo static int
   2740   1.1       ryo aq_set_filter(struct aq_softc *sc)
   2741   1.1       ryo {
   2742  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   2743  1.32     skrll 	struct ethercom * const ec = &sc->sc_ethercom;
   2744   1.1       ryo 	struct ether_multi *enm;
   2745   1.1       ryo 	struct ether_multistep step;
   2746   1.1       ryo 	int idx, error = 0;
   2747   1.1       ryo 
   2748   1.1       ryo 	if (ifp->if_flags & IFF_PROMISC) {
   2749   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_L2BC_REG, RPF_L2BC_PROMISC,
   2750   1.1       ryo 		    (ifp->if_flags & IFF_PROMISC) ? 1 : 0);
   2751   1.1       ryo 		ec->ec_flags |= ETHER_F_ALLMULTI;
   2752   1.1       ryo 		goto done;
   2753   1.1       ryo 	}
   2754   1.1       ryo 
   2755   1.1       ryo 	/* clear all table */
   2756   1.1       ryo 	for (idx = 0; idx < AQ_HW_MAC_NUM; idx++) {
   2757   1.1       ryo 		if (idx == AQ_HW_MAC_OWN)	/* already used for own */
   2758   1.1       ryo 			continue;
   2759   1.1       ryo 		aq_set_mac_addr(sc, idx, NULL);
   2760   1.1       ryo 	}
   2761   1.1       ryo 
   2762   1.1       ryo 	/* don't accept all multicast */
   2763   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_MCAST_FILTER_MASK_REG,
   2764   1.1       ryo 	    RPF_MCAST_FILTER_MASK_ALLMULTI, 0);
   2765   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_MCAST_FILTER_REG(0),
   2766   1.1       ryo 	    RPF_MCAST_FILTER_EN, 0);
   2767   1.1       ryo 
   2768   1.1       ryo 	idx = 0;
   2769   1.1       ryo 	ETHER_LOCK(ec);
   2770   1.1       ryo 	ETHER_FIRST_MULTI(step, ec, enm);
   2771   1.1       ryo 	while (enm != NULL) {
   2772   1.1       ryo 		if (idx == AQ_HW_MAC_OWN)
   2773   1.1       ryo 			idx++;
   2774   1.1       ryo 
   2775   1.1       ryo 		if ((idx >= AQ_HW_MAC_NUM) ||
   2776   1.1       ryo 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2777   1.1       ryo 			/*
   2778   1.1       ryo 			 * too many filters.
   2779   1.1       ryo 			 * fallback to accept all multicast addresses.
   2780   1.1       ryo 			 */
   2781   1.1       ryo 			AQ_WRITE_REG_BIT(sc, RPF_MCAST_FILTER_MASK_REG,
   2782   1.1       ryo 			    RPF_MCAST_FILTER_MASK_ALLMULTI, 1);
   2783   1.1       ryo 			AQ_WRITE_REG_BIT(sc, RPF_MCAST_FILTER_REG(0),
   2784   1.1       ryo 			    RPF_MCAST_FILTER_EN, 1);
   2785   1.1       ryo 			ec->ec_flags |= ETHER_F_ALLMULTI;
   2786   1.1       ryo 			ETHER_UNLOCK(ec);
   2787   1.1       ryo 			goto done;
   2788   1.1       ryo 		}
   2789   1.1       ryo 
   2790   1.1       ryo 		/* add a filter */
   2791   1.1       ryo 		aq_set_mac_addr(sc, idx++, enm->enm_addrlo);
   2792   1.1       ryo 
   2793   1.1       ryo 		ETHER_NEXT_MULTI(step, enm);
   2794   1.1       ryo 	}
   2795   1.1       ryo 	ec->ec_flags &= ~ETHER_F_ALLMULTI;
   2796   1.1       ryo 	ETHER_UNLOCK(ec);
   2797   1.1       ryo 
   2798   1.1       ryo  done:
   2799   1.1       ryo 	return error;
   2800   1.1       ryo }
   2801   1.1       ryo 
   2802   1.1       ryo static int
   2803   1.1       ryo aq_ifmedia_change(struct ifnet * const ifp)
   2804   1.1       ryo {
   2805  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   2806  1.33     skrll 
   2807   1.1       ryo 	aq_link_speed_t rate = AQ_LINK_NONE;
   2808   1.1       ryo 	aq_link_fc_t fc = AQ_FC_NONE;
   2809   1.1       ryo 	aq_link_eee_t eee = AQ_EEE_DISABLE;
   2810   1.1       ryo 
   2811   1.1       ryo 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   2812   1.1       ryo 		return EINVAL;
   2813   1.1       ryo 
   2814   1.1       ryo 	switch (IFM_SUBTYPE(sc->sc_media.ifm_media)) {
   2815   1.1       ryo 	case IFM_AUTO:
   2816   1.1       ryo 		rate = AQ_LINK_AUTO;
   2817   1.1       ryo 		break;
   2818   1.1       ryo 	case IFM_NONE:
   2819   1.1       ryo 		rate = AQ_LINK_NONE;
   2820   1.1       ryo 		break;
   2821   1.1       ryo 	case IFM_100_TX:
   2822   1.1       ryo 		rate = AQ_LINK_100M;
   2823   1.1       ryo 		break;
   2824   1.1       ryo 	case IFM_1000_T:
   2825   1.1       ryo 		rate = AQ_LINK_1G;
   2826   1.1       ryo 		break;
   2827   1.1       ryo 	case IFM_2500_T:
   2828   1.1       ryo 		rate = AQ_LINK_2G5;
   2829   1.1       ryo 		break;
   2830   1.1       ryo 	case IFM_5000_T:
   2831   1.1       ryo 		rate = AQ_LINK_5G;
   2832   1.1       ryo 		break;
   2833   1.1       ryo 	case IFM_10G_T:
   2834   1.1       ryo 		rate = AQ_LINK_10G;
   2835   1.1       ryo 		break;
   2836   1.1       ryo 	default:
   2837   1.1       ryo 		device_printf(sc->sc_dev, "unknown media: 0x%X\n",
   2838   1.1       ryo 		    IFM_SUBTYPE(sc->sc_media.ifm_media));
   2839   1.1       ryo 		return ENODEV;
   2840   1.1       ryo 	}
   2841   1.1       ryo 
   2842   1.1       ryo 	if (sc->sc_media.ifm_media & IFM_FLOW)
   2843   1.1       ryo 		fc = AQ_FC_ALL;
   2844   1.1       ryo 
   2845   1.1       ryo 	/* XXX: todo EEE */
   2846   1.1       ryo 
   2847   1.1       ryo 	/* re-initialize hardware with new parameters */
   2848   1.1       ryo 	aq_set_linkmode(sc, rate, fc, eee);
   2849   1.1       ryo 
   2850   1.1       ryo 	return 0;
   2851   1.1       ryo }
   2852   1.1       ryo 
   2853   1.1       ryo static void
   2854   1.1       ryo aq_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *ifmr)
   2855   1.1       ryo {
   2856  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   2857   1.1       ryo 
   2858  1.11       ryo 	/* update ifm_active */
   2859   1.1       ryo 	ifmr->ifm_active = IFM_ETHER;
   2860  1.11       ryo 	if (sc->sc_link_fc & AQ_FC_RX)
   2861  1.11       ryo 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
   2862  1.11       ryo 	if (sc->sc_link_fc & AQ_FC_TX)
   2863  1.11       ryo 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
   2864  1.11       ryo 
   2865  1.11       ryo 	switch (sc->sc_link_rate) {
   2866  1.11       ryo 	case AQ_LINK_100M:
   2867  1.11       ryo 		/* XXX: need to detect fulldup or halfdup */
   2868  1.11       ryo 		ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   2869  1.11       ryo 		break;
   2870  1.11       ryo 	case AQ_LINK_1G:
   2871  1.11       ryo 		ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   2872  1.11       ryo 		break;
   2873  1.11       ryo 	case AQ_LINK_2G5:
   2874  1.11       ryo 		ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
   2875  1.11       ryo 		break;
   2876  1.11       ryo 	case AQ_LINK_5G:
   2877  1.11       ryo 		ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
   2878  1.11       ryo 		break;
   2879  1.11       ryo 	case AQ_LINK_10G:
   2880  1.11       ryo 		ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   2881  1.11       ryo 		break;
   2882  1.11       ryo 	default:
   2883  1.11       ryo 		ifmr->ifm_active |= IFM_NONE;
   2884  1.11       ryo 		break;
   2885  1.11       ryo 	}
   2886  1.11       ryo 
   2887  1.11       ryo 	/* update ifm_status */
   2888   1.1       ryo 	ifmr->ifm_status = IFM_AVALID;
   2889   1.1       ryo 	if (sc->sc_link_rate != AQ_LINK_NONE)
   2890   1.1       ryo 		ifmr->ifm_status |= IFM_ACTIVE;
   2891   1.1       ryo }
   2892   1.1       ryo 
   2893   1.1       ryo static void
   2894   1.1       ryo aq_initmedia(struct aq_softc *sc)
   2895   1.1       ryo {
   2896   1.1       ryo #define IFMEDIA_ETHER_ADD(sc, media)	\
   2897   1.1       ryo 	ifmedia_add(&(sc)->sc_media, IFM_ETHER | media, 0, NULL);
   2898   1.1       ryo 
   2899   1.1       ryo 	IFMEDIA_ETHER_ADD(sc, IFM_NONE);
   2900   1.1       ryo 	if (sc->sc_available_rates & AQ_LINK_100M) {
   2901   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_100_TX);
   2902   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_100_TX | IFM_FLOW);
   2903   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_100_TX | IFM_FDX | IFM_FLOW);
   2904   1.1       ryo 	}
   2905   1.1       ryo 	if (sc->sc_available_rates & AQ_LINK_1G) {
   2906   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_1000_T | IFM_FDX);
   2907   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_1000_T | IFM_FDX | IFM_FLOW);
   2908   1.1       ryo 	}
   2909   1.1       ryo 	if (sc->sc_available_rates & AQ_LINK_2G5) {
   2910   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_2500_T | IFM_FDX);
   2911   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_2500_T | IFM_FDX | IFM_FLOW);
   2912   1.1       ryo 	}
   2913   1.1       ryo 	if (sc->sc_available_rates & AQ_LINK_5G) {
   2914   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_5000_T | IFM_FDX);
   2915   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_5000_T | IFM_FDX | IFM_FLOW);
   2916   1.1       ryo 	}
   2917   1.1       ryo 	if (sc->sc_available_rates & AQ_LINK_10G) {
   2918   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_10G_T | IFM_FDX);
   2919   1.1       ryo 		IFMEDIA_ETHER_ADD(sc, IFM_10G_T | IFM_FDX | IFM_FLOW);
   2920   1.1       ryo 	}
   2921   1.1       ryo 	IFMEDIA_ETHER_ADD(sc, IFM_AUTO);
   2922   1.1       ryo 	IFMEDIA_ETHER_ADD(sc, IFM_AUTO | IFM_FLOW);
   2923   1.1       ryo 
   2924   1.1       ryo 	/* default: auto without flowcontrol */
   2925   1.1       ryo 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_AUTO);
   2926   1.1       ryo 	aq_set_linkmode(sc, AQ_LINK_AUTO, AQ_FC_NONE, AQ_EEE_DISABLE);
   2927   1.1       ryo }
   2928   1.1       ryo 
   2929   1.1       ryo static int
   2930   1.1       ryo aq_set_linkmode(struct aq_softc *sc, aq_link_speed_t speed, aq_link_fc_t fc,
   2931   1.1       ryo     aq_link_eee_t eee)
   2932   1.1       ryo {
   2933   1.1       ryo 	return sc->sc_fw_ops->set_mode(sc, MPI_INIT, speed, fc, eee);
   2934   1.1       ryo }
   2935   1.1       ryo 
   2936   1.1       ryo static int
   2937   1.1       ryo aq_get_linkmode(struct aq_softc *sc, aq_link_speed_t *speed, aq_link_fc_t *fc,
   2938   1.1       ryo    aq_link_eee_t *eee)
   2939   1.1       ryo {
   2940   1.1       ryo 	aq_hw_fw_mpi_state_t mode;
   2941   1.1       ryo 	int error;
   2942   1.1       ryo 
   2943   1.1       ryo 	error = sc->sc_fw_ops->get_mode(sc, &mode, speed, fc, eee);
   2944   1.1       ryo 	if (error != 0)
   2945   1.1       ryo 		return error;
   2946   1.1       ryo 	if (mode != MPI_INIT)
   2947   1.1       ryo 		return ENXIO;
   2948   1.1       ryo 
   2949   1.1       ryo 	return 0;
   2950   1.1       ryo }
   2951   1.1       ryo 
   2952   1.1       ryo static void
   2953   1.1       ryo aq_hw_init_tx_path(struct aq_softc *sc)
   2954   1.1       ryo {
   2955   1.1       ryo 	/* Tx TC/RSS number config */
   2956   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TX_BUF_REG, TPB_TX_BUF_TC_MODE_EN, 1);
   2957   1.1       ryo 
   2958   1.1       ryo 	AQ_WRITE_REG_BIT(sc, THM_LSO_TCP_FLAG1_REG,
   2959   1.1       ryo 	    THM_LSO_TCP_FLAG1_FIRST, 0x0ff6);
   2960   1.1       ryo 	AQ_WRITE_REG_BIT(sc, THM_LSO_TCP_FLAG1_REG,
   2961   1.1       ryo 	    THM_LSO_TCP_FLAG1_MID,   0x0ff6);
   2962   1.1       ryo 	AQ_WRITE_REG_BIT(sc, THM_LSO_TCP_FLAG2_REG,
   2963   1.1       ryo 	   THM_LSO_TCP_FLAG2_LAST,  0x0f7f);
   2964   1.1       ryo 
   2965   1.1       ryo 	/* misc */
   2966   1.1       ryo 	AQ_WRITE_REG(sc, TX_TPO2_REG,
   2967   1.1       ryo 	   (sc->sc_features & FEATURES_TPO2) ? TX_TPO2_EN : 0);
   2968   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TDM_DCA_REG, TDM_DCA_EN, 0);
   2969   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TDM_DCA_REG, TDM_DCA_MODE, 0);
   2970   1.1       ryo 
   2971   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TX_BUF_REG, TPB_TX_BUF_SCP_INS_EN, 1);
   2972   1.1       ryo }
   2973   1.1       ryo 
   2974   1.1       ryo static void
   2975   1.1       ryo aq_hw_init_rx_path(struct aq_softc *sc)
   2976   1.1       ryo {
   2977   1.1       ryo 	int i;
   2978   1.1       ryo 
   2979   1.1       ryo 	/* clear setting */
   2980   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_TC_MODE, 0);
   2981   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_FC_MODE, 0);
   2982   1.1       ryo 	AQ_WRITE_REG(sc, RX_FLR_RSS_CONTROL1_REG, 0);
   2983   1.1       ryo 	for (i = 0; i < 32; i++) {
   2984   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_ETHERTYPE_FILTER_REG(i),
   2985   1.1       ryo 		   RPF_ETHERTYPE_FILTER_EN, 0);
   2986   1.1       ryo 	}
   2987   1.1       ryo 
   2988   1.1       ryo 	if (sc->sc_rss_enable) {
   2989   1.1       ryo 		/* Rx TC/RSS number config */
   2990   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_TC_MODE, 1);
   2991   1.1       ryo 
   2992   1.1       ryo 		/* Rx flow control */
   2993   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_FC_MODE, 1);
   2994   1.1       ryo 
   2995   1.1       ryo 		/* RSS Ring selection */
   2996   1.1       ryo 		switch (sc->sc_nqueues) {
   2997   1.1       ryo 		case 2:
   2998   1.1       ryo 			AQ_WRITE_REG(sc, RX_FLR_RSS_CONTROL1_REG,
   2999   1.1       ryo 			    RX_FLR_RSS_CONTROL1_EN | 0x11111111);
   3000   1.1       ryo 			break;
   3001   1.1       ryo 		case 4:
   3002   1.1       ryo 			AQ_WRITE_REG(sc, RX_FLR_RSS_CONTROL1_REG,
   3003   1.1       ryo 			    RX_FLR_RSS_CONTROL1_EN | 0x22222222);
   3004   1.1       ryo 			break;
   3005   1.1       ryo 		case 8:
   3006   1.1       ryo 			AQ_WRITE_REG(sc, RX_FLR_RSS_CONTROL1_REG,
   3007   1.1       ryo 			    RX_FLR_RSS_CONTROL1_EN | 0x33333333);
   3008   1.1       ryo 			break;
   3009   1.1       ryo 		}
   3010   1.1       ryo 	}
   3011   1.1       ryo 
   3012   1.1       ryo 	/* L2 and Multicast filters */
   3013   1.1       ryo 	for (i = 0; i < AQ_HW_MAC_NUM; i++) {
   3014   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(i), RPF_L2UC_MSW_EN, 0);
   3015   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_L2UC_MSW_REG(i), RPF_L2UC_MSW_ACTION,
   3016   1.1       ryo 		    RPF_ACTION_HOST);
   3017   1.1       ryo 	}
   3018   1.1       ryo 	AQ_WRITE_REG(sc, RPF_MCAST_FILTER_MASK_REG, 0);
   3019   1.1       ryo 	AQ_WRITE_REG(sc, RPF_MCAST_FILTER_REG(0), 0x00010fff);
   3020   1.1       ryo 
   3021   1.1       ryo 	/* Vlan filters */
   3022   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_VLAN_TPID_REG, RPF_VLAN_TPID_OUTER,
   3023   1.1       ryo 	    ETHERTYPE_QINQ);
   3024   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_VLAN_TPID_REG, RPF_VLAN_TPID_INNER,
   3025   1.1       ryo 	    ETHERTYPE_VLAN);
   3026  1.10       ryo 	AQ_WRITE_REG_BIT(sc, RPF_VLAN_MODE_REG, RPF_VLAN_MODE_PROMISC, 0);
   3027   1.1       ryo 
   3028   1.1       ryo 	if (sc->sc_features & FEATURES_REV_B) {
   3029   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_MODE_REG,
   3030   1.1       ryo 		    RPF_VLAN_MODE_ACCEPT_UNTAGGED, 1);
   3031   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_MODE_REG,
   3032   1.1       ryo 		    RPF_VLAN_MODE_UNTAGGED_ACTION, RPF_ACTION_HOST);
   3033   1.1       ryo 	}
   3034   1.1       ryo 
   3035   1.1       ryo 	/* misc */
   3036   1.1       ryo 	if (sc->sc_features & FEATURES_RPF2)
   3037   1.1       ryo 		AQ_WRITE_REG(sc, RX_TCP_RSS_HASH_REG, RX_TCP_RSS_HASH_RPF2);
   3038   1.1       ryo 	else
   3039   1.1       ryo 		AQ_WRITE_REG(sc, RX_TCP_RSS_HASH_REG, 0);
   3040   1.1       ryo 
   3041   1.1       ryo 	/*
   3042   1.1       ryo 	 * XXX: RX_TCP_RSS_HASH_REG:
   3043   1.1       ryo 	 *  linux   set 0x000f0000
   3044   1.1       ryo 	 *  freebsd set 0x000f001e
   3045   1.1       ryo 	 */
   3046   1.1       ryo 	/* RSS hash type set for IP/TCP */
   3047   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_TCP_RSS_HASH_REG,
   3048   1.1       ryo 	    RX_TCP_RSS_HASH_TYPE, 0x001e);
   3049   1.1       ryo 
   3050   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2BC_REG, RPF_L2BC_EN, 1);
   3051   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2BC_REG, RPF_L2BC_ACTION, RPF_ACTION_HOST);
   3052   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPF_L2BC_REG, RPF_L2BC_THRESHOLD, 0xffff);
   3053   1.1       ryo 
   3054   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_DMA_DCA_REG, RX_DMA_DCA_EN, 0);
   3055   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_DMA_DCA_REG, RX_DMA_DCA_MODE, 0);
   3056   1.1       ryo }
   3057   1.1       ryo 
   3058   1.1       ryo static void
   3059   1.1       ryo aq_hw_interrupt_moderation_set(struct aq_softc *sc)
   3060   1.1       ryo {
   3061   1.1       ryo 	int i;
   3062   1.1       ryo 
   3063   1.1       ryo 	if (sc->sc_intr_moderation_enable) {
   3064   1.1       ryo 		unsigned int tx_min, rx_min;	/* 0-255 */
   3065   1.1       ryo 		unsigned int tx_max, rx_max;	/* 0-511? */
   3066   1.1       ryo 
   3067   1.1       ryo 		switch (sc->sc_link_rate) {
   3068   1.1       ryo 		case AQ_LINK_100M:
   3069   1.1       ryo 			tx_min = 0x4f;
   3070   1.1       ryo 			tx_max = 0xff;
   3071   1.1       ryo 			rx_min = 0x04;
   3072   1.1       ryo 			rx_max = 0x50;
   3073   1.1       ryo 			break;
   3074   1.1       ryo 		case AQ_LINK_1G:
   3075   1.1       ryo 		default:
   3076   1.1       ryo 			tx_min = 0x4f;
   3077   1.1       ryo 			tx_max = 0xff;
   3078   1.1       ryo 			rx_min = 0x30;
   3079   1.1       ryo 			rx_max = 0x80;
   3080   1.1       ryo 			break;
   3081   1.1       ryo 		case AQ_LINK_2G5:
   3082   1.1       ryo 			tx_min = 0x4f;
   3083   1.1       ryo 			tx_max = 0xff;
   3084   1.1       ryo 			rx_min = 0x18;
   3085   1.1       ryo 			rx_max = 0xe0;
   3086   1.1       ryo 			break;
   3087   1.1       ryo 		case AQ_LINK_5G:
   3088   1.1       ryo 			tx_min = 0x4f;
   3089   1.1       ryo 			tx_max = 0xff;
   3090   1.1       ryo 			rx_min = 0x0c;
   3091   1.1       ryo 			rx_max = 0x70;
   3092   1.1       ryo 			break;
   3093   1.1       ryo 		case AQ_LINK_10G:
   3094   1.1       ryo 			tx_min = 0x4f;
   3095   1.1       ryo 			tx_max = 0x1ff;
   3096   1.1       ryo 			rx_min = 0x06;	/* freebsd use 80 */
   3097   1.1       ryo 			rx_max = 0x38;	/* freebsd use 120 */
   3098   1.1       ryo 			break;
   3099   1.1       ryo 		}
   3100   1.1       ryo 
   3101   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_INT_DESC_WRWB_EN_REG,
   3102   1.1       ryo 		    TX_DMA_INT_DESC_WRWB_EN, 0);
   3103   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_INT_DESC_WRWB_EN_REG,
   3104   1.1       ryo 		    TX_DMA_INT_DESC_MODERATE_EN, 1);
   3105   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_INT_DESC_WRWB_EN_REG,
   3106   1.1       ryo 		    RX_DMA_INT_DESC_WRWB_EN, 0);
   3107   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_INT_DESC_WRWB_EN_REG,
   3108   1.1       ryo 		    RX_DMA_INT_DESC_MODERATE_EN, 1);
   3109   1.1       ryo 
   3110   1.1       ryo 		for (i = 0; i < AQ_RINGS_NUM; i++) {
   3111   1.1       ryo 			AQ_WRITE_REG(sc, TX_INTR_MODERATION_CTL_REG(i),
   3112   1.1       ryo 			    __SHIFTIN(tx_min, TX_INTR_MODERATION_CTL_MIN) |
   3113   1.1       ryo 			    __SHIFTIN(tx_max, TX_INTR_MODERATION_CTL_MAX) |
   3114   1.1       ryo 			    TX_INTR_MODERATION_CTL_EN);
   3115   1.1       ryo 		}
   3116   1.1       ryo 		for (i = 0; i < AQ_RINGS_NUM; i++) {
   3117   1.1       ryo 			AQ_WRITE_REG(sc, RX_INTR_MODERATION_CTL_REG(i),
   3118   1.1       ryo 			    __SHIFTIN(rx_min, RX_INTR_MODERATION_CTL_MIN) |
   3119   1.1       ryo 			    __SHIFTIN(rx_max, RX_INTR_MODERATION_CTL_MAX) |
   3120   1.1       ryo 			    RX_INTR_MODERATION_CTL_EN);
   3121   1.1       ryo 		}
   3122   1.1       ryo 
   3123   1.1       ryo 	} else {
   3124   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_INT_DESC_WRWB_EN_REG,
   3125   1.1       ryo 		    TX_DMA_INT_DESC_WRWB_EN, 1);
   3126   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_INT_DESC_WRWB_EN_REG,
   3127   1.1       ryo 		    TX_DMA_INT_DESC_MODERATE_EN, 0);
   3128   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_INT_DESC_WRWB_EN_REG,
   3129   1.1       ryo 		    RX_DMA_INT_DESC_WRWB_EN, 1);
   3130   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_INT_DESC_WRWB_EN_REG,
   3131   1.1       ryo 		    RX_DMA_INT_DESC_MODERATE_EN, 0);
   3132   1.1       ryo 
   3133   1.1       ryo 		for (i = 0; i < AQ_RINGS_NUM; i++) {
   3134   1.1       ryo 			AQ_WRITE_REG(sc, TX_INTR_MODERATION_CTL_REG(i), 0);
   3135   1.1       ryo 		}
   3136   1.1       ryo 		for (i = 0; i < AQ_RINGS_NUM; i++) {
   3137   1.1       ryo 			AQ_WRITE_REG(sc, RX_INTR_MODERATION_CTL_REG(i), 0);
   3138   1.1       ryo 		}
   3139   1.1       ryo 	}
   3140   1.1       ryo }
   3141   1.1       ryo 
   3142   1.1       ryo static void
   3143   1.1       ryo aq_hw_qos_set(struct aq_softc *sc)
   3144   1.1       ryo {
   3145   1.1       ryo 	uint32_t tc = 0;
   3146   1.1       ryo 	uint32_t buff_size;
   3147   1.1       ryo 
   3148   1.1       ryo 	/* TPS Descriptor rate init */
   3149   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_RATE_REG, TPS_DESC_RATE_TA_RST, 0);
   3150   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_RATE_REG, TPS_DESC_RATE_LIM, 0xa);
   3151   1.1       ryo 
   3152   1.1       ryo 	/* TPS VM init */
   3153   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_VM_ARB_MODE_REG, TPS_DESC_VM_ARB_MODE, 0);
   3154   1.1       ryo 
   3155   1.1       ryo 	/* TPS TC credits init */
   3156   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_TC_ARB_MODE_REG, TPS_DESC_TC_ARB_MODE, 0);
   3157   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DATA_TC_ARB_MODE_REG, TPS_DATA_TC_ARB_MODE, 0);
   3158   1.1       ryo 
   3159   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DATA_TCT_REG(tc),
   3160   1.1       ryo 	    TPS_DATA_TCT_CREDIT_MAX, 0xfff);
   3161   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DATA_TCT_REG(tc),
   3162   1.1       ryo 	    TPS_DATA_TCT_WEIGHT, 0x64);
   3163   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_TCT_REG(tc),
   3164   1.1       ryo 	    TPS_DESC_TCT_CREDIT_MAX, 0x50);
   3165   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPS_DESC_TCT_REG(tc),
   3166   1.1       ryo 	    TPS_DESC_TCT_WEIGHT, 0x1e);
   3167   1.1       ryo 
   3168   1.1       ryo 	/* Tx buf size */
   3169   1.1       ryo 	tc = 0;
   3170   1.1       ryo 	buff_size = AQ_HW_TXBUF_MAX;
   3171   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TXB_BUFSIZE_REG(tc), TPB_TXB_BUFSIZE,
   3172   1.1       ryo 	    buff_size);
   3173   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TXB_THRESH_REG(tc), TPB_TXB_THRESH_HI,
   3174   1.1       ryo 	    (buff_size * (1024 / 32) * 66) / 100);
   3175   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TXB_THRESH_REG(tc), TPB_TXB_THRESH_LO,
   3176   1.1       ryo 	    (buff_size * (1024 / 32) * 50) / 100);
   3177   1.1       ryo 
   3178   1.1       ryo 	/* QoS Rx buf size per TC */
   3179   1.1       ryo 	tc = 0;
   3180   1.1       ryo 	buff_size = AQ_HW_RXBUF_MAX;
   3181   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RXB_BUFSIZE_REG(tc), RPB_RXB_BUFSIZE,
   3182   1.1       ryo 	    buff_size);
   3183   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RXB_XOFF_REG(tc), RPB_RXB_XOFF_EN, 0);
   3184   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RXB_XOFF_REG(tc), RPB_RXB_XOFF_THRESH_HI,
   3185   1.1       ryo 	    (buff_size * (1024 / 32) * 66) / 100);
   3186   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RXB_XOFF_REG(tc), RPB_RXB_XOFF_THRESH_LO,
   3187   1.1       ryo 	    (buff_size * (1024 / 32) * 50) / 100);
   3188   1.1       ryo 
   3189   1.1       ryo 	/* QoS 802.1p priority -> TC mapping */
   3190   1.1       ryo 	int i_priority;
   3191   1.1       ryo 	for (i_priority = 0; i_priority < 8; i_priority++) {
   3192   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RPB_RX_TC_UPT_REG,
   3193   1.1       ryo 		    RPF_RPB_RX_TC_UPT_MASK(i_priority), 0);
   3194   1.1       ryo 	}
   3195   1.1       ryo }
   3196   1.1       ryo 
   3197   1.1       ryo /* called once from aq_attach */
   3198   1.1       ryo static int
   3199   1.1       ryo aq_init_rss(struct aq_softc *sc)
   3200   1.1       ryo {
   3201   1.1       ryo 	CTASSERT(AQ_RSS_HASHKEY_SIZE == RSS_KEYSIZE);
   3202   1.1       ryo 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
   3203   1.1       ryo 	uint8_t rss_table[AQ_RSS_INDIRECTION_TABLE_MAX];
   3204   1.1       ryo 	unsigned int i;
   3205   1.1       ryo 	int error;
   3206   1.1       ryo 
   3207   1.1       ryo 	/* initialize rss key */
   3208   1.1       ryo 	rss_getkey((uint8_t *)rss_key);
   3209   1.1       ryo 
   3210   1.1       ryo 	/* hash to ring table */
   3211   1.1       ryo 	for (i = 0; i < AQ_RSS_INDIRECTION_TABLE_MAX; i++) {
   3212   1.1       ryo 		rss_table[i] = i % sc->sc_nqueues;
   3213   1.1       ryo 	}
   3214   1.1       ryo 
   3215   1.1       ryo 	/*
   3216   1.1       ryo 	 * set rss key
   3217   1.1       ryo 	 */
   3218   1.1       ryo 	for (i = 0; i < __arraycount(rss_key); i++) {
   3219   1.1       ryo 		uint32_t key_data = sc->sc_rss_enable ? ntohl(rss_key[i]) : 0;
   3220   1.1       ryo 		AQ_WRITE_REG(sc, RPF_RSS_KEY_WR_DATA_REG, key_data);
   3221   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RSS_KEY_ADDR_REG,
   3222   1.1       ryo 		    RPF_RSS_KEY_ADDR, __arraycount(rss_key) - 1 - i);
   3223   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RSS_KEY_ADDR_REG,
   3224   1.1       ryo 		    RPF_RSS_KEY_WR_EN, 1);
   3225   1.1       ryo 		WAIT_FOR(AQ_READ_REG_BIT(sc, RPF_RSS_KEY_ADDR_REG,
   3226   1.1       ryo 		    RPF_RSS_KEY_WR_EN) == 0, 1000, 10, &error);
   3227   1.1       ryo 		if (error != 0) {
   3228   1.1       ryo 			device_printf(sc->sc_dev, "%s: rss key write timeout\n",
   3229   1.1       ryo 			    __func__);
   3230   1.1       ryo 			goto rss_set_timeout;
   3231   1.1       ryo 		}
   3232   1.1       ryo 	}
   3233   1.1       ryo 
   3234   1.1       ryo 	/*
   3235   1.1       ryo 	 * set rss indirection table
   3236   1.1       ryo 	 *
   3237   1.1       ryo 	 * AQ's rss redirect table is consist of 3bit*64 (192bit) packed array.
   3238   1.1       ryo 	 * we'll make it by __BITMAP(3) macros.
   3239   1.1       ryo 	 */
   3240   1.1       ryo 	__BITMAP_TYPE(, uint16_t, 3 * AQ_RSS_INDIRECTION_TABLE_MAX) bit3x64;
   3241   1.1       ryo 	__BITMAP_ZERO(&bit3x64);
   3242   1.1       ryo 
   3243   1.1       ryo #define AQ_3BIT_PACKED_ARRAY_SET(bitmap, idx, val)		\
   3244   1.1       ryo 	do {							\
   3245   1.1       ryo 		if (val & 1) {					\
   3246   1.1       ryo 			__BITMAP_SET((idx) * 3, (bitmap));	\
   3247   1.1       ryo 		} else {					\
   3248   1.1       ryo 			__BITMAP_CLR((idx) * 3, (bitmap));	\
   3249   1.1       ryo 		}						\
   3250   1.1       ryo 		if (val & 2) {					\
   3251   1.1       ryo 			__BITMAP_SET((idx) * 3 + 1, (bitmap));	\
   3252   1.1       ryo 		} else {					\
   3253   1.1       ryo 			__BITMAP_CLR((idx) * 3 + 1, (bitmap));	\
   3254   1.1       ryo 		}						\
   3255   1.1       ryo 		if (val & 4) {					\
   3256   1.1       ryo 			__BITMAP_SET((idx) * 3 + 2, (bitmap));	\
   3257   1.1       ryo 		} else {					\
   3258   1.1       ryo 			__BITMAP_CLR((idx) * 3 + 2, (bitmap));	\
   3259   1.1       ryo 		}						\
   3260   1.1       ryo 	} while (0 /* CONSTCOND */)
   3261   1.1       ryo 
   3262   1.1       ryo 	for (i = 0; i < AQ_RSS_INDIRECTION_TABLE_MAX; i++) {
   3263   1.1       ryo 		AQ_3BIT_PACKED_ARRAY_SET(&bit3x64, i, rss_table[i]);
   3264   1.1       ryo 	}
   3265   1.1       ryo 
   3266   1.1       ryo 	/* write 192bit data in steps of 16bit */
   3267   1.1       ryo 	for (i = 0; i < (int)__arraycount(bit3x64._b); i++) {
   3268   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RSS_REDIR_WR_DATA_REG,
   3269   1.1       ryo 		    RPF_RSS_REDIR_WR_DATA, bit3x64._b[i]);
   3270   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RSS_REDIR_ADDR_REG,
   3271   1.1       ryo 		    RPF_RSS_REDIR_ADDR, i);
   3272   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_RSS_REDIR_ADDR_REG,
   3273   1.1       ryo 		    RPF_RSS_REDIR_WR_EN, 1);
   3274   1.1       ryo 
   3275   1.1       ryo 		WAIT_FOR(AQ_READ_REG_BIT(sc, RPF_RSS_REDIR_ADDR_REG,
   3276   1.1       ryo 		    RPF_RSS_REDIR_WR_EN) == 0, 1000, 10, &error);
   3277   1.1       ryo 		if (error != 0)
   3278   1.1       ryo 			break;
   3279   1.1       ryo 	}
   3280   1.1       ryo 
   3281   1.1       ryo  rss_set_timeout:
   3282   1.1       ryo 	return error;
   3283   1.1       ryo }
   3284   1.1       ryo 
   3285   1.1       ryo static void
   3286   1.1       ryo aq_hw_l3_filter_set(struct aq_softc *sc)
   3287   1.1       ryo {
   3288   1.1       ryo 	int i;
   3289   1.1       ryo 
   3290   1.1       ryo 	/* clear all filter */
   3291   1.1       ryo 	for (i = 0; i < 8; i++) {
   3292   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_L3_FILTER_REG(i),
   3293   1.1       ryo 		    RPF_L3_FILTER_L4_EN, 0);
   3294   1.1       ryo 	}
   3295   1.1       ryo }
   3296   1.1       ryo 
   3297   1.1       ryo static void
   3298  1.10       ryo aq_set_vlan_filters(struct aq_softc *sc)
   3299   1.1       ryo {
   3300  1.32     skrll 	struct ethercom * const ec = &sc->sc_ethercom;
   3301  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   3302  1.10       ryo 	struct vlanid_list *vlanidp;
   3303   1.1       ryo 	int i;
   3304   1.1       ryo 
   3305  1.10       ryo 	ETHER_LOCK(ec);
   3306  1.10       ryo 
   3307  1.10       ryo 	/* disable all vlan filters */
   3308  1.10       ryo 	for (i = 0; i < RPF_VLAN_MAX_FILTERS; i++)
   3309  1.10       ryo 		AQ_WRITE_REG(sc, RPF_VLAN_FILTER_REG(i), 0);
   3310  1.10       ryo 
   3311  1.10       ryo 	/* count VID */
   3312  1.10       ryo 	i = 0;
   3313  1.10       ryo 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list)
   3314  1.10       ryo 		i++;
   3315  1.10       ryo 
   3316  1.10       ryo 	if (((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_HWFILTER) == 0) ||
   3317  1.10       ryo 	    (ifp->if_flags & IFF_PROMISC) ||
   3318  1.10       ryo 	    (i > RPF_VLAN_MAX_FILTERS)) {
   3319  1.10       ryo 		/*
   3320  1.10       ryo 		 * no vlan hwfilter, in promiscuous mode, or too many VID?
   3321  1.10       ryo 		 * must receive all VID
   3322  1.10       ryo 		 */
   3323  1.10       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_MODE_REG,
   3324  1.10       ryo 		    RPF_VLAN_MODE_PROMISC, 1);
   3325  1.10       ryo 		goto done;
   3326  1.10       ryo 	}
   3327  1.10       ryo 
   3328  1.10       ryo 	/* receive only selected VID */
   3329  1.10       ryo 	AQ_WRITE_REG_BIT(sc, RPF_VLAN_MODE_REG, RPF_VLAN_MODE_PROMISC, 0);
   3330  1.10       ryo 	i = 0;
   3331  1.10       ryo 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
   3332   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_FILTER_REG(i),
   3333  1.10       ryo 		    RPF_VLAN_FILTER_EN, 1);
   3334   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_FILTER_REG(i),
   3335   1.1       ryo 		    RPF_VLAN_FILTER_RXQ_EN, 0);
   3336  1.10       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_FILTER_REG(i),
   3337  1.10       ryo 		    RPF_VLAN_FILTER_RXQ, 0);
   3338  1.10       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_FILTER_REG(i),
   3339  1.10       ryo 		    RPF_VLAN_FILTER_ACTION, RPF_ACTION_HOST);
   3340  1.10       ryo 		AQ_WRITE_REG_BIT(sc, RPF_VLAN_FILTER_REG(i),
   3341  1.10       ryo 		    RPF_VLAN_FILTER_ID, vlanidp->vid);
   3342  1.10       ryo 		i++;
   3343   1.1       ryo 	}
   3344  1.10       ryo 
   3345  1.10       ryo  done:
   3346  1.10       ryo 	ETHER_UNLOCK(ec);
   3347   1.1       ryo }
   3348   1.1       ryo 
   3349   1.1       ryo static int
   3350   1.1       ryo aq_hw_init(struct aq_softc *sc)
   3351   1.1       ryo {
   3352   1.1       ryo 	uint32_t v;
   3353   1.1       ryo 
   3354   1.1       ryo 	/* Force limit MRRS on RDM/TDM to 2K */
   3355   1.1       ryo 	v = AQ_READ_REG(sc, AQ_PCI_REG_CONTROL_6_REG);
   3356   1.1       ryo 	AQ_WRITE_REG(sc, AQ_PCI_REG_CONTROL_6_REG, (v & ~0x0707) | 0x0404);
   3357   1.1       ryo 
   3358   1.1       ryo 	/*
   3359   1.1       ryo 	 * TX DMA total request limit. B0 hardware is not capable to
   3360   1.1       ryo 	 * handle more than (8K-MRRS) incoming DMA data.
   3361   1.1       ryo 	 * Value 24 in 256byte units
   3362   1.1       ryo 	 */
   3363   1.1       ryo 	AQ_WRITE_REG(sc, AQ_HW_TX_DMA_TOTAL_REQ_LIMIT_REG, 24);
   3364   1.1       ryo 
   3365   1.1       ryo 	aq_hw_init_tx_path(sc);
   3366   1.1       ryo 	aq_hw_init_rx_path(sc);
   3367   1.1       ryo 
   3368   1.1       ryo 	aq_hw_interrupt_moderation_set(sc);
   3369   1.1       ryo 
   3370   1.1       ryo 	aq_set_mac_addr(sc, AQ_HW_MAC_OWN, sc->sc_enaddr.ether_addr_octet);
   3371   1.1       ryo 	aq_set_linkmode(sc, AQ_LINK_NONE, AQ_FC_NONE, AQ_EEE_DISABLE);
   3372   1.1       ryo 
   3373   1.1       ryo 	aq_hw_qos_set(sc);
   3374   1.1       ryo 
   3375   1.1       ryo 	/* Enable interrupt */
   3376   1.1       ryo 	int irqmode;
   3377   1.1       ryo 	if (sc->sc_msix)
   3378   1.1       ryo 		irqmode =  AQ_INTR_CTRL_IRQMODE_MSIX;
   3379   1.1       ryo 	else
   3380   1.1       ryo 		irqmode =  AQ_INTR_CTRL_IRQMODE_MSI;
   3381   1.1       ryo 
   3382   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_CTRL_REG, AQ_INTR_CTRL_RESET_DIS);
   3383   1.1       ryo 	AQ_WRITE_REG_BIT(sc, AQ_INTR_CTRL_REG, AQ_INTR_CTRL_MULTIVEC,
   3384   1.1       ryo 	    sc->sc_msix ? 1 : 0);
   3385   1.1       ryo 	AQ_WRITE_REG_BIT(sc, AQ_INTR_CTRL_REG, AQ_INTR_CTRL_IRQMODE, irqmode);
   3386   1.1       ryo 
   3387   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_AUTOMASK_REG, 0xffffffff);
   3388   1.1       ryo 
   3389   1.1       ryo 	AQ_WRITE_REG(sc, AQ_GEN_INTR_MAP_REG(0),
   3390   1.5   msaitoh 	    ((AQ_B0_ERR_INT << 24) | (1U << 31)) |
   3391   1.1       ryo 	    ((AQ_B0_ERR_INT << 16) | (1 << 23))
   3392   1.1       ryo 	);
   3393   1.1       ryo 
   3394   1.1       ryo 	/* link interrupt */
   3395   1.1       ryo 	if (!sc->sc_msix)
   3396   1.1       ryo 		sc->sc_linkstat_irq = AQ_LINKSTAT_IRQ;
   3397   1.1       ryo 	AQ_WRITE_REG(sc, AQ_GEN_INTR_MAP_REG(3),
   3398   1.1       ryo 	    __BIT(7) | sc->sc_linkstat_irq);
   3399   1.1       ryo 
   3400   1.1       ryo 	return 0;
   3401   1.1       ryo }
   3402   1.1       ryo 
   3403   1.1       ryo static int
   3404   1.1       ryo aq_update_link_status(struct aq_softc *sc)
   3405   1.1       ryo {
   3406  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   3407   1.1       ryo 	aq_link_speed_t rate = AQ_LINK_NONE;
   3408   1.1       ryo 	aq_link_fc_t fc = AQ_FC_NONE;
   3409   1.1       ryo 	aq_link_eee_t eee = AQ_EEE_DISABLE;
   3410   1.1       ryo 	unsigned int speed;
   3411   1.1       ryo 	int changed = 0;
   3412   1.1       ryo 
   3413   1.1       ryo 	aq_get_linkmode(sc, &rate, &fc, &eee);
   3414   1.1       ryo 
   3415   1.1       ryo 	if (sc->sc_link_rate != rate)
   3416   1.1       ryo 		changed = 1;
   3417   1.1       ryo 	if (sc->sc_link_fc != fc)
   3418   1.1       ryo 		changed = 1;
   3419   1.1       ryo 	if (sc->sc_link_eee != eee)
   3420   1.1       ryo 		changed = 1;
   3421   1.1       ryo 
   3422   1.1       ryo 	if (changed) {
   3423   1.1       ryo 		switch (rate) {
   3424   1.1       ryo 		case AQ_LINK_100M:
   3425   1.1       ryo 			speed = 100;
   3426   1.1       ryo 			break;
   3427   1.1       ryo 		case AQ_LINK_1G:
   3428   1.1       ryo 			speed = 1000;
   3429   1.1       ryo 			break;
   3430   1.1       ryo 		case AQ_LINK_2G5:
   3431   1.1       ryo 			speed = 2500;
   3432   1.1       ryo 			break;
   3433   1.1       ryo 		case AQ_LINK_5G:
   3434   1.1       ryo 			speed = 5000;
   3435   1.1       ryo 			break;
   3436   1.1       ryo 		case AQ_LINK_10G:
   3437   1.1       ryo 			speed = 10000;
   3438   1.1       ryo 			break;
   3439   1.1       ryo 		case AQ_LINK_NONE:
   3440   1.1       ryo 		default:
   3441   1.1       ryo 			speed = 0;
   3442   1.1       ryo 			break;
   3443   1.1       ryo 		}
   3444   1.1       ryo 
   3445   1.1       ryo 		if (sc->sc_link_rate == AQ_LINK_NONE) {
   3446   1.1       ryo 			/* link DOWN -> UP */
   3447   1.1       ryo 			device_printf(sc->sc_dev, "link is UP: speed=%u\n",
   3448   1.1       ryo 			    speed);
   3449   1.1       ryo 			if_link_state_change(ifp, LINK_STATE_UP);
   3450   1.1       ryo 		} else if (rate == AQ_LINK_NONE) {
   3451   1.1       ryo 			/* link UP -> DOWN */
   3452   1.1       ryo 			device_printf(sc->sc_dev, "link is DOWN\n");
   3453   1.1       ryo 			if_link_state_change(ifp, LINK_STATE_DOWN);
   3454   1.1       ryo 		} else {
   3455   1.1       ryo 			device_printf(sc->sc_dev,
   3456   1.1       ryo 			    "link mode changed: speed=%u, fc=0x%x, eee=%x\n",
   3457   1.1       ryo 			    speed, fc, eee);
   3458   1.1       ryo 		}
   3459   1.1       ryo 
   3460   1.1       ryo 		sc->sc_link_rate = rate;
   3461   1.1       ryo 		sc->sc_link_fc = fc;
   3462   1.1       ryo 		sc->sc_link_eee = eee;
   3463   1.1       ryo 
   3464   1.1       ryo 		/* update interrupt timing according to new link speed */
   3465   1.1       ryo 		aq_hw_interrupt_moderation_set(sc);
   3466   1.1       ryo 	}
   3467   1.1       ryo 
   3468   1.1       ryo 	return changed;
   3469   1.1       ryo }
   3470   1.1       ryo 
   3471   1.1       ryo #ifdef AQ_EVENT_COUNTERS
   3472   1.1       ryo static void
   3473   1.1       ryo aq_update_statistics(struct aq_softc *sc)
   3474   1.1       ryo {
   3475   1.1       ryo 	int prev = sc->sc_statistics_idx;
   3476   1.1       ryo 	int cur = prev ^ 1;
   3477   1.1       ryo 
   3478   1.1       ryo 	sc->sc_fw_ops->get_stats(sc, &sc->sc_statistics[cur]);
   3479   1.1       ryo 
   3480   1.1       ryo 	/*
   3481   1.1       ryo 	 * aq's internal statistics counter is 32bit.
   3482   1.1       ryo 	 * cauculate delta, and add to evcount
   3483   1.1       ryo 	 */
   3484   1.1       ryo #define ADD_DELTA(cur, prev, name)				\
   3485   1.1       ryo 	do {							\
   3486   1.1       ryo 		uint32_t n;					\
   3487   1.1       ryo 		n = (uint32_t)(sc->sc_statistics[cur].name -	\
   3488   1.1       ryo 		    sc->sc_statistics[prev].name);		\
   3489   1.1       ryo 		if (n != 0) {					\
   3490   1.1       ryo 			AQ_EVCNT_ADD(sc, name, n);		\
   3491   1.1       ryo 		}						\
   3492   1.1       ryo 	} while (/*CONSTCOND*/0);
   3493   1.1       ryo 
   3494   1.1       ryo 	ADD_DELTA(cur, prev, uprc);
   3495   1.1       ryo 	ADD_DELTA(cur, prev, mprc);
   3496   1.1       ryo 	ADD_DELTA(cur, prev, bprc);
   3497   1.1       ryo 	ADD_DELTA(cur, prev, prc);
   3498   1.1       ryo 	ADD_DELTA(cur, prev, erpr);
   3499   1.1       ryo 	ADD_DELTA(cur, prev, uptc);
   3500   1.1       ryo 	ADD_DELTA(cur, prev, mptc);
   3501   1.1       ryo 	ADD_DELTA(cur, prev, bptc);
   3502   1.1       ryo 	ADD_DELTA(cur, prev, ptc);
   3503   1.1       ryo 	ADD_DELTA(cur, prev, erpt);
   3504   1.1       ryo 	ADD_DELTA(cur, prev, mbtc);
   3505   1.1       ryo 	ADD_DELTA(cur, prev, bbtc);
   3506   1.1       ryo 	ADD_DELTA(cur, prev, mbrc);
   3507   1.1       ryo 	ADD_DELTA(cur, prev, bbrc);
   3508   1.1       ryo 	ADD_DELTA(cur, prev, ubrc);
   3509   1.1       ryo 	ADD_DELTA(cur, prev, ubtc);
   3510   1.1       ryo 	ADD_DELTA(cur, prev, dpc);
   3511   1.1       ryo 	ADD_DELTA(cur, prev, cprc);
   3512   1.1       ryo 
   3513   1.1       ryo 	sc->sc_statistics_idx = cur;
   3514   1.1       ryo }
   3515   1.1       ryo #endif /* AQ_EVENT_COUNTERS */
   3516   1.1       ryo 
   3517   1.1       ryo /* allocate and map one DMA block */
   3518   1.1       ryo static int
   3519   1.1       ryo _alloc_dma(struct aq_softc *sc, bus_size_t size, bus_size_t *sizep,
   3520   1.1       ryo     void **addrp, bus_dmamap_t *mapp, bus_dma_segment_t *seg)
   3521   1.1       ryo {
   3522   1.1       ryo 	int nsegs, error;
   3523   1.1       ryo 
   3524   1.1       ryo 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seg,
   3525   1.1       ryo 	    1, &nsegs, 0)) != 0) {
   3526   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3527   1.1       ryo 		    "unable to allocate DMA buffer, error=%d\n", error);
   3528   1.1       ryo 		goto fail_alloc;
   3529   1.1       ryo 	}
   3530   1.1       ryo 
   3531   1.1       ryo 	if ((error = bus_dmamem_map(sc->sc_dmat, seg, 1, size, addrp,
   3532   1.1       ryo 	    BUS_DMA_COHERENT)) != 0) {
   3533   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3534   1.1       ryo 		    "unable to map DMA buffer, error=%d\n", error);
   3535   1.1       ryo 		goto fail_map;
   3536   1.1       ryo 	}
   3537   1.1       ryo 
   3538   1.1       ryo 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   3539   1.1       ryo 	    0, mapp)) != 0) {
   3540   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3541   1.1       ryo 		    "unable to create DMA map, error=%d\n", error);
   3542   1.1       ryo 		goto fail_create;
   3543   1.1       ryo 	}
   3544   1.1       ryo 
   3545   1.1       ryo 	if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
   3546   1.1       ryo 	    0)) != 0) {
   3547   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3548   1.1       ryo 		    "unable to load DMA map, error=%d\n", error);
   3549   1.1       ryo 		goto fail_load;
   3550   1.1       ryo 	}
   3551   1.1       ryo 
   3552   1.1       ryo 	*sizep = size;
   3553   1.1       ryo 	return 0;
   3554   1.1       ryo 
   3555   1.1       ryo  fail_load:
   3556   1.1       ryo 	bus_dmamap_destroy(sc->sc_dmat, *mapp);
   3557   1.1       ryo 	*mapp = NULL;
   3558   1.1       ryo  fail_create:
   3559   1.1       ryo 	bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
   3560   1.1       ryo 	*addrp = NULL;
   3561   1.1       ryo  fail_map:
   3562   1.1       ryo 	bus_dmamem_free(sc->sc_dmat, seg, 1);
   3563   1.1       ryo 	memset(seg, 0, sizeof(*seg));
   3564   1.1       ryo  fail_alloc:
   3565   1.1       ryo 	*sizep = 0;
   3566   1.1       ryo 	return error;
   3567   1.1       ryo }
   3568   1.1       ryo 
   3569   1.1       ryo static void
   3570   1.1       ryo _free_dma(struct aq_softc *sc, bus_size_t *sizep, void **addrp,
   3571   1.1       ryo     bus_dmamap_t *mapp, bus_dma_segment_t *seg)
   3572   1.1       ryo {
   3573   1.1       ryo 	if (*mapp != NULL) {
   3574   1.1       ryo 		bus_dmamap_destroy(sc->sc_dmat, *mapp);
   3575   1.1       ryo 		*mapp = NULL;
   3576   1.1       ryo 	}
   3577   1.1       ryo 	if (*addrp != NULL) {
   3578   1.1       ryo 		bus_dmamem_unmap(sc->sc_dmat, *addrp, *sizep);
   3579   1.1       ryo 		*addrp = NULL;
   3580   1.1       ryo 	}
   3581   1.1       ryo 	if (*sizep != 0) {
   3582   1.1       ryo 		bus_dmamem_free(sc->sc_dmat, seg, 1);
   3583   1.1       ryo 		memset(seg, 0, sizeof(*seg));
   3584   1.1       ryo 		*sizep = 0;
   3585   1.1       ryo 	}
   3586   1.1       ryo }
   3587   1.1       ryo 
   3588   1.1       ryo static int
   3589   1.1       ryo aq_txring_alloc(struct aq_softc *sc, struct aq_txring *txring)
   3590   1.1       ryo {
   3591   1.1       ryo 	int i, error;
   3592   1.1       ryo 
   3593   1.1       ryo 	/* allocate tx descriptors */
   3594   1.1       ryo 	error = _alloc_dma(sc, sizeof(aq_tx_desc_t) * AQ_TXD_NUM,
   3595   1.1       ryo 	    &txring->txr_txdesc_size, (void **)&txring->txr_txdesc,
   3596   1.1       ryo 	    &txring->txr_txdesc_dmamap, txring->txr_txdesc_seg);
   3597   1.1       ryo 	if (error != 0)
   3598   1.1       ryo 		return error;
   3599   1.1       ryo 
   3600   1.1       ryo 	memset(txring->txr_txdesc, 0, sizeof(aq_tx_desc_t) * AQ_TXD_NUM);
   3601   1.1       ryo 
   3602   1.1       ryo 	/* fill tx ring with dmamap */
   3603   1.1       ryo 	for (i = 0; i < AQ_TXD_NUM; i++) {
   3604   1.1       ryo #define AQ_MAXDMASIZE	(16 * 1024)
   3605   1.1       ryo #define AQ_NTXSEGS	32
   3606   1.1       ryo 		/* XXX: TODO: error check */
   3607   1.1       ryo 		bus_dmamap_create(sc->sc_dmat, AQ_MAXDMASIZE, AQ_NTXSEGS,
   3608   1.1       ryo 		    AQ_MAXDMASIZE, 0, 0, &txring->txr_mbufs[i].dmamap);
   3609   1.1       ryo 	}
   3610   1.1       ryo 	return 0;
   3611   1.1       ryo }
   3612   1.1       ryo 
   3613   1.1       ryo static void
   3614   1.1       ryo aq_txring_free(struct aq_softc *sc, struct aq_txring *txring)
   3615   1.1       ryo {
   3616   1.1       ryo 	int i;
   3617   1.1       ryo 
   3618   1.1       ryo 	_free_dma(sc, &txring->txr_txdesc_size, (void **)&txring->txr_txdesc,
   3619   1.1       ryo 	    &txring->txr_txdesc_dmamap, txring->txr_txdesc_seg);
   3620   1.1       ryo 
   3621   1.1       ryo 	for (i = 0; i < AQ_TXD_NUM; i++) {
   3622   1.1       ryo 		if (txring->txr_mbufs[i].dmamap != NULL) {
   3623   1.1       ryo 			if (txring->txr_mbufs[i].m != NULL) {
   3624   1.1       ryo 				bus_dmamap_unload(sc->sc_dmat,
   3625   1.1       ryo 				    txring->txr_mbufs[i].dmamap);
   3626   1.1       ryo 				m_freem(txring->txr_mbufs[i].m);
   3627   1.1       ryo 				txring->txr_mbufs[i].m = NULL;
   3628   1.1       ryo 			}
   3629   1.1       ryo 			bus_dmamap_destroy(sc->sc_dmat,
   3630   1.1       ryo 			    txring->txr_mbufs[i].dmamap);
   3631   1.1       ryo 			txring->txr_mbufs[i].dmamap = NULL;
   3632   1.1       ryo 		}
   3633   1.1       ryo 	}
   3634   1.1       ryo }
   3635   1.1       ryo 
   3636   1.1       ryo static int
   3637   1.1       ryo aq_rxring_alloc(struct aq_softc *sc, struct aq_rxring *rxring)
   3638   1.1       ryo {
   3639   1.1       ryo 	int i, error;
   3640   1.1       ryo 
   3641   1.1       ryo 	/* allocate rx descriptors */
   3642   1.1       ryo 	error = _alloc_dma(sc, sizeof(aq_rx_desc_t) * AQ_RXD_NUM,
   3643   1.1       ryo 	    &rxring->rxr_rxdesc_size, (void **)&rxring->rxr_rxdesc,
   3644   1.1       ryo 	    &rxring->rxr_rxdesc_dmamap, rxring->rxr_rxdesc_seg);
   3645   1.1       ryo 	if (error != 0)
   3646   1.1       ryo 		return error;
   3647   1.1       ryo 
   3648   1.1       ryo 	memset(rxring->rxr_rxdesc, 0, sizeof(aq_rx_desc_t) * AQ_RXD_NUM);
   3649   1.1       ryo 
   3650   1.1       ryo 	/* fill rxring with dmamaps */
   3651   1.1       ryo 	for (i = 0; i < AQ_RXD_NUM; i++) {
   3652   1.1       ryo 		rxring->rxr_mbufs[i].m = NULL;
   3653   1.1       ryo 		/* XXX: TODO: error check */
   3654   1.1       ryo 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
   3655   1.1       ryo 		    &rxring->rxr_mbufs[i].dmamap);
   3656   1.1       ryo 	}
   3657   1.1       ryo 	return 0;
   3658   1.1       ryo }
   3659   1.1       ryo 
   3660   1.1       ryo static void
   3661   1.1       ryo aq_rxdrain(struct aq_softc *sc, struct aq_rxring *rxring)
   3662   1.1       ryo {
   3663   1.1       ryo 	int i;
   3664   1.1       ryo 
   3665   1.1       ryo 	/* free all mbufs allocated for RX */
   3666   1.1       ryo 	for (i = 0; i < AQ_RXD_NUM; i++) {
   3667   1.1       ryo 		if (rxring->rxr_mbufs[i].m != NULL) {
   3668   1.1       ryo 			bus_dmamap_unload(sc->sc_dmat,
   3669   1.1       ryo 			    rxring->rxr_mbufs[i].dmamap);
   3670   1.1       ryo 			m_freem(rxring->rxr_mbufs[i].m);
   3671   1.1       ryo 			rxring->rxr_mbufs[i].m = NULL;
   3672   1.1       ryo 		}
   3673   1.1       ryo 	}
   3674   1.1       ryo }
   3675   1.1       ryo 
   3676   1.1       ryo static void
   3677   1.1       ryo aq_rxring_free(struct aq_softc *sc, struct aq_rxring *rxring)
   3678   1.1       ryo {
   3679   1.1       ryo 	int i;
   3680   1.1       ryo 
   3681   1.1       ryo 	/* free all mbufs and dmamaps */
   3682   1.1       ryo 	aq_rxdrain(sc, rxring);
   3683   1.1       ryo 	for (i = 0; i < AQ_RXD_NUM; i++) {
   3684   1.1       ryo 		if (rxring->rxr_mbufs[i].dmamap != NULL) {
   3685   1.1       ryo 			bus_dmamap_destroy(sc->sc_dmat,
   3686   1.1       ryo 			    rxring->rxr_mbufs[i].dmamap);
   3687   1.1       ryo 			rxring->rxr_mbufs[i].dmamap = NULL;
   3688   1.1       ryo 		}
   3689   1.1       ryo 	}
   3690   1.1       ryo 
   3691   1.1       ryo 	/* free RX descriptor */
   3692   1.1       ryo 	_free_dma(sc, &rxring->rxr_rxdesc_size, (void **)&rxring->rxr_rxdesc,
   3693   1.1       ryo 	    &rxring->rxr_rxdesc_dmamap, rxring->rxr_rxdesc_seg);
   3694   1.1       ryo }
   3695   1.1       ryo 
   3696   1.1       ryo static void
   3697   1.1       ryo aq_rxring_setmbuf(struct aq_softc *sc, struct aq_rxring *rxring, int idx,
   3698   1.1       ryo     struct mbuf *m)
   3699   1.1       ryo {
   3700   1.1       ryo 	int error;
   3701   1.1       ryo 
   3702   1.1       ryo 	/* if mbuf already exists, unload and free */
   3703   1.1       ryo 	if (rxring->rxr_mbufs[idx].m != NULL) {
   3704   1.1       ryo 		bus_dmamap_unload(sc->sc_dmat, rxring->rxr_mbufs[idx].dmamap);
   3705   1.1       ryo 		m_freem(rxring->rxr_mbufs[idx].m);
   3706   1.1       ryo 		rxring->rxr_mbufs[idx].m = NULL;
   3707   1.1       ryo 	}
   3708   1.1       ryo 
   3709   1.1       ryo 	rxring->rxr_mbufs[idx].m = m;
   3710   1.1       ryo 
   3711   1.1       ryo 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   3712   1.1       ryo 	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxring->rxr_mbufs[idx].dmamap,
   3713   1.1       ryo 	    m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   3714   1.1       ryo 	if (error) {
   3715   1.1       ryo 		device_printf(sc->sc_dev,
   3716   1.1       ryo 		    "unable to load rx DMA map %d, error = %d\n", idx, error);
   3717   1.1       ryo 		panic("%s: unable to load rx DMA map. error=%d",
   3718   1.1       ryo 		    __func__, error);
   3719   1.1       ryo 	}
   3720   1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, rxring->rxr_mbufs[idx].dmamap, 0,
   3721   1.1       ryo 	    rxring->rxr_mbufs[idx].dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   3722   1.1       ryo }
   3723   1.1       ryo 
   3724   1.1       ryo static inline void
   3725   1.1       ryo aq_rxring_reset_desc(struct aq_softc *sc, struct aq_rxring *rxring, int idx)
   3726   1.1       ryo {
   3727   1.1       ryo 	/* refill rxdesc, and sync */
   3728   1.1       ryo 	rxring->rxr_rxdesc[idx].read.buf_addr =
   3729   1.1       ryo 	   htole64(rxring->rxr_mbufs[idx].dmamap->dm_segs[0].ds_addr);
   3730   1.1       ryo 	rxring->rxr_rxdesc[idx].read.hdr_addr = 0;
   3731   1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, rxring->rxr_rxdesc_dmamap,
   3732   1.1       ryo 	    sizeof(aq_rx_desc_t) * idx, sizeof(aq_rx_desc_t),
   3733   1.1       ryo 	    BUS_DMASYNC_PREWRITE);
   3734   1.1       ryo }
   3735   1.1       ryo 
   3736   1.1       ryo static struct mbuf *
   3737   1.1       ryo aq_alloc_mbuf(void)
   3738   1.1       ryo {
   3739   1.1       ryo 	struct mbuf *m;
   3740   1.1       ryo 
   3741   1.1       ryo 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   3742   1.1       ryo 	if (m == NULL)
   3743   1.1       ryo 		return NULL;
   3744   1.1       ryo 
   3745   1.1       ryo 	MCLGET(m, M_DONTWAIT);
   3746   1.1       ryo 	if ((m->m_flags & M_EXT) == 0) {
   3747   1.1       ryo 		m_freem(m);
   3748   1.1       ryo 		return NULL;
   3749   1.1       ryo 	}
   3750   1.1       ryo 
   3751   1.1       ryo 	return m;
   3752   1.1       ryo }
   3753   1.1       ryo 
   3754   1.1       ryo /* allocate mbuf and unload dmamap */
   3755   1.1       ryo static int
   3756   1.1       ryo aq_rxring_add(struct aq_softc *sc, struct aq_rxring *rxring, int idx)
   3757   1.1       ryo {
   3758   1.1       ryo 	struct mbuf *m;
   3759   1.1       ryo 
   3760   1.1       ryo 	m = aq_alloc_mbuf();
   3761   1.1       ryo 	if (m == NULL)
   3762   1.1       ryo 		return ENOBUFS;
   3763   1.1       ryo 
   3764   1.1       ryo 	aq_rxring_setmbuf(sc, rxring, idx, m);
   3765   1.1       ryo 	return 0;
   3766   1.1       ryo }
   3767   1.1       ryo 
   3768   1.1       ryo static int
   3769   1.1       ryo aq_txrx_rings_alloc(struct aq_softc *sc)
   3770   1.1       ryo {
   3771   1.1       ryo 	int n, error;
   3772   1.1       ryo 
   3773   1.1       ryo 	for (n = 0; n < sc->sc_nqueues; n++) {
   3774   1.1       ryo 		sc->sc_queue[n].sc = sc;
   3775   1.1       ryo 		sc->sc_queue[n].txring.txr_sc = sc;
   3776   1.1       ryo 		sc->sc_queue[n].txring.txr_index = n;
   3777   1.1       ryo 		mutex_init(&sc->sc_queue[n].txring.txr_mutex, MUTEX_DEFAULT,
   3778   1.1       ryo 		    IPL_NET);
   3779   1.1       ryo 		error = aq_txring_alloc(sc, &sc->sc_queue[n].txring);
   3780   1.1       ryo 		if (error != 0)
   3781   1.1       ryo 			goto failure;
   3782   1.1       ryo 
   3783   1.1       ryo 		error = aq_tx_pcq_alloc(sc, &sc->sc_queue[n].txring);
   3784   1.1       ryo 		if (error != 0)
   3785   1.1       ryo 			goto failure;
   3786   1.1       ryo 
   3787   1.1       ryo 		sc->sc_queue[n].rxring.rxr_sc = sc;
   3788   1.1       ryo 		sc->sc_queue[n].rxring.rxr_index = n;
   3789   1.1       ryo 		mutex_init(&sc->sc_queue[n].rxring.rxr_mutex, MUTEX_DEFAULT,
   3790   1.1       ryo 		   IPL_NET);
   3791   1.1       ryo 		error = aq_rxring_alloc(sc, &sc->sc_queue[n].rxring);
   3792   1.1       ryo 		if (error != 0)
   3793   1.1       ryo 			break;
   3794   1.1       ryo 	}
   3795   1.1       ryo 
   3796   1.1       ryo  failure:
   3797   1.1       ryo 	return error;
   3798   1.1       ryo }
   3799   1.1       ryo 
   3800   1.1       ryo static void
   3801   1.1       ryo aq_txrx_rings_free(struct aq_softc *sc)
   3802   1.1       ryo {
   3803   1.1       ryo 	int n;
   3804   1.1       ryo 
   3805   1.1       ryo 	for (n = 0; n < sc->sc_nqueues; n++) {
   3806   1.1       ryo 		aq_txring_free(sc, &sc->sc_queue[n].txring);
   3807   1.1       ryo 		mutex_destroy(&sc->sc_queue[n].txring.txr_mutex);
   3808   1.1       ryo 
   3809   1.1       ryo 		aq_tx_pcq_free(sc, &sc->sc_queue[n].txring);
   3810   1.1       ryo 
   3811   1.1       ryo 		aq_rxring_free(sc, &sc->sc_queue[n].rxring);
   3812   1.1       ryo 		mutex_destroy(&sc->sc_queue[n].rxring.rxr_mutex);
   3813   1.1       ryo 	}
   3814   1.1       ryo }
   3815   1.1       ryo 
   3816   1.1       ryo static int
   3817   1.1       ryo aq_tx_pcq_alloc(struct aq_softc *sc, struct aq_txring *txring)
   3818   1.1       ryo {
   3819   1.1       ryo 	int error = 0;
   3820   1.1       ryo 	txring->txr_softint = NULL;
   3821   1.1       ryo 
   3822   1.1       ryo 	txring->txr_pcq = pcq_create(AQ_TXD_NUM, KM_NOSLEEP);
   3823   1.1       ryo 	if (txring->txr_pcq == NULL) {
   3824   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3825   1.1       ryo 		    "unable to allocate pcq for TXring[%d]\n",
   3826   1.1       ryo 		    txring->txr_index);
   3827   1.1       ryo 		error = ENOMEM;
   3828   1.1       ryo 		goto done;
   3829   1.1       ryo 	}
   3830   1.1       ryo 
   3831   1.1       ryo 	txring->txr_softint = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   3832   1.1       ryo 	    aq_deferred_transmit, txring);
   3833   1.1       ryo 	if (txring->txr_softint == NULL) {
   3834   1.1       ryo 		aprint_error_dev(sc->sc_dev,
   3835   1.1       ryo 		    "unable to establish softint for TXring[%d]\n",
   3836   1.1       ryo 		    txring->txr_index);
   3837   1.1       ryo 		error = ENOENT;
   3838   1.1       ryo 	}
   3839   1.1       ryo 
   3840   1.1       ryo  done:
   3841   1.1       ryo 	return error;
   3842   1.1       ryo }
   3843   1.1       ryo 
   3844   1.1       ryo static void
   3845   1.1       ryo aq_tx_pcq_free(struct aq_softc *sc, struct aq_txring *txring)
   3846   1.1       ryo {
   3847   1.1       ryo 	struct mbuf *m;
   3848   1.1       ryo 
   3849   1.1       ryo 	if (txring->txr_softint != NULL) {
   3850   1.1       ryo 		softint_disestablish(txring->txr_softint);
   3851   1.1       ryo 		txring->txr_softint = NULL;
   3852   1.1       ryo 	}
   3853   1.1       ryo 
   3854   1.1       ryo 	if (txring->txr_pcq != NULL) {
   3855   1.1       ryo 		while ((m = pcq_get(txring->txr_pcq)) != NULL)
   3856   1.1       ryo 			m_freem(m);
   3857   1.1       ryo 		pcq_destroy(txring->txr_pcq);
   3858   1.1       ryo 		txring->txr_pcq = NULL;
   3859   1.1       ryo 	}
   3860   1.1       ryo }
   3861   1.1       ryo 
   3862   1.4       ryo #if NSYSMON_ENVSYS > 0
   3863   1.4       ryo static void
   3864   1.4       ryo aq_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
   3865   1.4       ryo {
   3866   1.4       ryo 	struct aq_softc *sc;
   3867   1.4       ryo 	uint32_t temp;
   3868   1.4       ryo 	int error;
   3869   1.4       ryo 
   3870   1.4       ryo 	sc = sme->sme_cookie;
   3871   1.4       ryo 
   3872   1.4       ryo 	error = sc->sc_fw_ops->get_temperature(sc, &temp);
   3873   1.4       ryo 	if (error == 0) {
   3874   1.4       ryo 		edata->value_cur = temp;
   3875   1.4       ryo 		edata->state = ENVSYS_SVALID;
   3876   1.4       ryo 	} else {
   3877   1.4       ryo 		edata->state = ENVSYS_SINVALID;
   3878   1.4       ryo 	}
   3879   1.4       ryo }
   3880   1.4       ryo #endif
   3881   1.4       ryo 
   3882  1.33     skrll 
   3883  1.33     skrll 
   3884  1.33     skrll static bool
   3885  1.33     skrll aq_watchdog_check(struct aq_softc * const sc)
   3886  1.33     skrll {
   3887  1.33     skrll 
   3888  1.33     skrll 	AQ_LOCKED(sc);
   3889  1.33     skrll 
   3890  1.33     skrll 	bool ok = true;
   3891  1.40       ryo 	for (int n = 0; n < sc->sc_nqueues; n++) {
   3892  1.33     skrll 		struct aq_txring *txring = &sc->sc_queue[n].txring;
   3893  1.33     skrll 
   3894  1.33     skrll 		mutex_enter(&txring->txr_mutex);
   3895  1.33     skrll 		if (txring->txr_sending &&
   3896  1.33     skrll 		    time_uptime - txring->txr_lastsent > aq_watchdog_timeout)
   3897  1.33     skrll 			ok = false;
   3898  1.33     skrll 
   3899  1.33     skrll 		mutex_exit(&txring->txr_mutex);
   3900  1.33     skrll 
   3901  1.33     skrll 		if (!ok)
   3902  1.33     skrll 			return false;
   3903  1.33     skrll 	}
   3904  1.33     skrll 
   3905  1.33     skrll 	if (sc->sc_trigger_reset) {
   3906  1.33     skrll 		/* debug operation, no need for atomicity or reliability */
   3907  1.33     skrll 		sc->sc_trigger_reset = 0;
   3908  1.33     skrll 		return false;
   3909  1.33     skrll 	}
   3910  1.33     skrll 
   3911  1.33     skrll 	return true;
   3912  1.33     skrll }
   3913  1.33     skrll 
   3914  1.33     skrll 
   3915  1.33     skrll 
   3916  1.33     skrll static bool
   3917  1.33     skrll aq_watchdog_tick(struct ifnet *ifp)
   3918  1.33     skrll {
   3919  1.33     skrll 	struct aq_softc * const sc = ifp->if_softc;
   3920  1.33     skrll 
   3921  1.33     skrll 	AQ_LOCKED(sc);
   3922  1.33     skrll 
   3923  1.33     skrll 	if (!sc->sc_trigger_reset && aq_watchdog_check(sc))
   3924  1.33     skrll 		return true;
   3925  1.33     skrll 
   3926  1.33     skrll 	if (atomic_swap_uint(&sc->sc_reset_pending, 1) == 0) {
   3927  1.33     skrll 		workqueue_enqueue(sc->sc_reset_wq, &sc->sc_reset_work, NULL);
   3928  1.33     skrll 	}
   3929  1.33     skrll 
   3930  1.33     skrll 	return false;
   3931  1.33     skrll }
   3932  1.33     skrll 
   3933   1.1       ryo static void
   3934   1.1       ryo aq_tick(void *arg)
   3935   1.1       ryo {
   3936  1.33     skrll 	struct aq_softc * const sc = arg;
   3937  1.33     skrll 
   3938  1.33     skrll 	AQ_LOCK(sc);
   3939  1.33     skrll 	if (sc->sc_stopping) {
   3940  1.33     skrll 		AQ_UNLOCK(sc);
   3941  1.33     skrll 		return;
   3942  1.33     skrll 	}
   3943   1.1       ryo 
   3944   1.1       ryo 	if (sc->sc_poll_linkstat || sc->sc_detect_linkstat) {
   3945   1.1       ryo 		sc->sc_detect_linkstat = false;
   3946   1.1       ryo 		aq_update_link_status(sc);
   3947   1.1       ryo 	}
   3948   1.1       ryo 
   3949   1.1       ryo #ifdef AQ_EVENT_COUNTERS
   3950   1.1       ryo 	if (sc->sc_poll_statistics)
   3951   1.1       ryo 		aq_update_statistics(sc);
   3952   1.1       ryo #endif
   3953   1.1       ryo 
   3954  1.33     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   3955  1.33     skrll 	const bool ok = aq_watchdog_tick(ifp);
   3956  1.33     skrll 	if (ok)
   3957   1.1       ryo 		callout_schedule(&sc->sc_tick_ch, hz);
   3958  1.33     skrll 
   3959  1.33     skrll 	AQ_UNLOCK(sc);
   3960   1.1       ryo }
   3961   1.1       ryo 
   3962   1.1       ryo /* interrupt enable/disable */
   3963   1.1       ryo static void
   3964   1.1       ryo aq_enable_intr(struct aq_softc *sc, bool link, bool txrx)
   3965   1.1       ryo {
   3966   1.1       ryo 	uint32_t imask = 0;
   3967   1.1       ryo 	int i;
   3968   1.1       ryo 
   3969   1.1       ryo 	if (txrx) {
   3970   1.1       ryo 		for (i = 0; i < sc->sc_nqueues; i++) {
   3971   1.1       ryo 			imask |= __BIT(sc->sc_tx_irq[i]);
   3972   1.1       ryo 			imask |= __BIT(sc->sc_rx_irq[i]);
   3973   1.1       ryo 		}
   3974   1.1       ryo 	}
   3975   1.1       ryo 
   3976   1.1       ryo 	if (link)
   3977   1.1       ryo 		imask |= __BIT(sc->sc_linkstat_irq);
   3978   1.1       ryo 
   3979   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_MASK_REG, imask);
   3980   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_STATUS_CLR_REG, 0xffffffff);
   3981   1.1       ryo }
   3982   1.1       ryo 
   3983   1.1       ryo static int
   3984   1.1       ryo aq_legacy_intr(void *arg)
   3985   1.1       ryo {
   3986   1.1       ryo 	struct aq_softc *sc = arg;
   3987   1.1       ryo 	uint32_t status;
   3988   1.1       ryo 	int nintr = 0;
   3989   1.1       ryo 
   3990   1.1       ryo 	status = AQ_READ_REG(sc, AQ_INTR_STATUS_REG);
   3991   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_STATUS_CLR_REG, 0xffffffff);
   3992   1.1       ryo 
   3993   1.1       ryo 	if (status & __BIT(sc->sc_linkstat_irq)) {
   3994  1.34  riastrad 		AQ_LOCK(sc);
   3995   1.1       ryo 		sc->sc_detect_linkstat = true;
   3996  1.34  riastrad 		if (!sc->sc_stopping)
   3997  1.34  riastrad 			callout_schedule(&sc->sc_tick_ch, 0);
   3998  1.34  riastrad 		AQ_UNLOCK(sc);
   3999   1.1       ryo 		nintr++;
   4000   1.1       ryo 	}
   4001   1.1       ryo 
   4002   1.1       ryo 	if (status & __BIT(sc->sc_rx_irq[0])) {
   4003   1.1       ryo 		nintr += aq_rx_intr(&sc->sc_queue[0].rxring);
   4004   1.1       ryo 	}
   4005   1.1       ryo 
   4006   1.1       ryo 	if (status & __BIT(sc->sc_tx_irq[0])) {
   4007   1.1       ryo 		nintr += aq_tx_intr(&sc->sc_queue[0].txring);
   4008   1.1       ryo 	}
   4009   1.1       ryo 
   4010   1.1       ryo 	return nintr;
   4011   1.1       ryo }
   4012   1.1       ryo 
   4013   1.1       ryo static int
   4014   1.1       ryo aq_txrx_intr(void *arg)
   4015   1.1       ryo {
   4016   1.1       ryo 	struct aq_queue *queue = arg;
   4017   1.1       ryo 	struct aq_softc *sc = queue->sc;
   4018   1.1       ryo 	struct aq_txring *txring = &queue->txring;
   4019   1.1       ryo 	struct aq_rxring *rxring = &queue->rxring;
   4020   1.1       ryo 	uint32_t status;
   4021   1.1       ryo 	int nintr = 0;
   4022   1.1       ryo 	int txringidx, rxringidx, txirq, rxirq;
   4023   1.1       ryo 
   4024   1.1       ryo 	txringidx = txring->txr_index;
   4025   1.1       ryo 	rxringidx = rxring->rxr_index;
   4026   1.1       ryo 	txirq = sc->sc_tx_irq[txringidx];
   4027   1.1       ryo 	rxirq = sc->sc_rx_irq[rxringidx];
   4028   1.1       ryo 
   4029   1.1       ryo 	status = AQ_READ_REG(sc, AQ_INTR_STATUS_REG);
   4030   1.1       ryo 	if ((status & (__BIT(txirq) | __BIT(rxirq))) == 0) {
   4031   1.1       ryo 		/* stray interrupt? */
   4032   1.1       ryo 		return 0;
   4033   1.1       ryo 	}
   4034   1.1       ryo 
   4035   1.1       ryo 	nintr += aq_rx_intr(rxring);
   4036   1.1       ryo 	nintr += aq_tx_intr(txring);
   4037   1.1       ryo 
   4038   1.1       ryo 	return nintr;
   4039   1.1       ryo }
   4040   1.1       ryo 
   4041   1.1       ryo static int
   4042   1.1       ryo aq_link_intr(void *arg)
   4043   1.1       ryo {
   4044  1.33     skrll 	struct aq_softc * const sc = arg;
   4045   1.1       ryo 	uint32_t status;
   4046   1.1       ryo 	int nintr = 0;
   4047   1.1       ryo 
   4048   1.1       ryo 	status = AQ_READ_REG(sc, AQ_INTR_STATUS_REG);
   4049   1.1       ryo 	if (status & __BIT(sc->sc_linkstat_irq)) {
   4050  1.34  riastrad 		AQ_LOCK(sc);
   4051   1.1       ryo 		sc->sc_detect_linkstat = true;
   4052  1.34  riastrad 		if (!sc->sc_stopping)
   4053  1.34  riastrad 			callout_schedule(&sc->sc_tick_ch, 0);
   4054  1.34  riastrad 		AQ_UNLOCK(sc);
   4055   1.1       ryo 		AQ_WRITE_REG(sc, AQ_INTR_STATUS_CLR_REG,
   4056   1.1       ryo 		    __BIT(sc->sc_linkstat_irq));
   4057   1.1       ryo 		nintr++;
   4058   1.1       ryo 	}
   4059   1.1       ryo 
   4060   1.1       ryo 	return nintr;
   4061   1.1       ryo }
   4062   1.1       ryo 
   4063   1.1       ryo static void
   4064   1.1       ryo aq_txring_reset(struct aq_softc *sc, struct aq_txring *txring, bool start)
   4065   1.1       ryo {
   4066   1.1       ryo 	const int ringidx = txring->txr_index;
   4067   1.1       ryo 	int i;
   4068   1.1       ryo 
   4069   1.1       ryo 	mutex_enter(&txring->txr_mutex);
   4070   1.1       ryo 
   4071   1.1       ryo 	txring->txr_prodidx = 0;
   4072   1.1       ryo 	txring->txr_considx = 0;
   4073   1.1       ryo 	txring->txr_nfree = AQ_TXD_NUM;
   4074   1.1       ryo 	txring->txr_active = false;
   4075   1.1       ryo 
   4076   1.1       ryo 	/* free mbufs untransmitted */
   4077   1.1       ryo 	for (i = 0; i < AQ_TXD_NUM; i++) {
   4078   1.1       ryo 		if (txring->txr_mbufs[i].m != NULL) {
   4079   1.1       ryo 			m_freem(txring->txr_mbufs[i].m);
   4080   1.1       ryo 			txring->txr_mbufs[i].m = NULL;
   4081   1.1       ryo 		}
   4082   1.1       ryo 	}
   4083   1.1       ryo 
   4084   1.1       ryo 	/* disable DMA */
   4085   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TX_DMA_DESC_REG(ringidx), TX_DMA_DESC_EN, 0);
   4086   1.1       ryo 
   4087   1.1       ryo 	if (start) {
   4088   1.1       ryo 		/* TX descriptor physical address */
   4089   1.1       ryo 		paddr_t paddr = txring->txr_txdesc_dmamap->dm_segs[0].ds_addr;
   4090   1.1       ryo 		AQ_WRITE_REG(sc, TX_DMA_DESC_BASE_ADDRLSW_REG(ringidx), paddr);
   4091   1.1       ryo 		AQ_WRITE_REG(sc, TX_DMA_DESC_BASE_ADDRMSW_REG(ringidx),
   4092   1.1       ryo 		    (uint32_t)((uint64_t)paddr >> 32));
   4093   1.1       ryo 
   4094   1.1       ryo 		/* TX descriptor size */
   4095   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_DESC_REG(ringidx), TX_DMA_DESC_LEN,
   4096   1.1       ryo 		    AQ_TXD_NUM / 8);
   4097   1.1       ryo 
   4098   1.1       ryo 		/* reload TAIL pointer */
   4099   1.1       ryo 		txring->txr_prodidx = txring->txr_considx =
   4100   1.1       ryo 		    AQ_READ_REG(sc, TX_DMA_DESC_TAIL_PTR_REG(ringidx));
   4101   1.1       ryo 		AQ_WRITE_REG(sc, TX_DMA_DESC_WRWB_THRESH_REG(ringidx), 0);
   4102   1.1       ryo 
   4103   1.1       ryo 		/* Mapping interrupt vector */
   4104   1.1       ryo 		AQ_WRITE_REG_BIT(sc, AQ_INTR_IRQ_MAP_TX_REG(ringidx),
   4105   1.1       ryo 		    AQ_INTR_IRQ_MAP_TX_IRQMAP(ringidx), sc->sc_tx_irq[ringidx]);
   4106   1.1       ryo 		AQ_WRITE_REG_BIT(sc, AQ_INTR_IRQ_MAP_TX_REG(ringidx),
   4107   1.1       ryo 		    AQ_INTR_IRQ_MAP_TX_EN(ringidx), true);
   4108   1.1       ryo 
   4109   1.1       ryo 		/* enable DMA */
   4110   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TX_DMA_DESC_REG(ringidx),
   4111   1.1       ryo 		    TX_DMA_DESC_EN, 1);
   4112   1.1       ryo 
   4113   1.1       ryo 		const int cpuid = 0;	/* XXX? */
   4114   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TDM_DCAD_REG(ringidx),
   4115   1.1       ryo 		    TDM_DCAD_CPUID, cpuid);
   4116   1.1       ryo 		AQ_WRITE_REG_BIT(sc, TDM_DCAD_REG(ringidx),
   4117   1.1       ryo 		    TDM_DCAD_CPUID_EN, 0);
   4118   1.1       ryo 
   4119   1.1       ryo 		txring->txr_active = true;
   4120   1.1       ryo 	}
   4121   1.1       ryo 
   4122   1.1       ryo 	mutex_exit(&txring->txr_mutex);
   4123   1.1       ryo }
   4124   1.1       ryo 
   4125   1.1       ryo static int
   4126   1.1       ryo aq_rxring_reset(struct aq_softc *sc, struct aq_rxring *rxring, bool start)
   4127   1.1       ryo {
   4128   1.1       ryo 	const int ringidx = rxring->rxr_index;
   4129   1.1       ryo 	int i;
   4130   1.1       ryo 	int error = 0;
   4131   1.1       ryo 
   4132   1.1       ryo 	mutex_enter(&rxring->rxr_mutex);
   4133   1.1       ryo 	rxring->rxr_active = false;
   4134  1.28       ryo 	rxring->rxr_discarding = false;
   4135  1.28       ryo 	if (rxring->rxr_receiving_m != NULL) {
   4136  1.28       ryo 		m_freem(rxring->rxr_receiving_m);
   4137  1.28       ryo 		rxring->rxr_receiving_m = NULL;
   4138  1.28       ryo 		rxring->rxr_receiving_m_last = NULL;
   4139  1.28       ryo 	}
   4140   1.1       ryo 
   4141   1.1       ryo 	/* disable DMA */
   4142   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(ringidx), RX_DMA_DESC_EN, 0);
   4143   1.1       ryo 
   4144   1.1       ryo 	/* free all RX mbufs */
   4145   1.1       ryo 	aq_rxdrain(sc, rxring);
   4146   1.1       ryo 
   4147   1.1       ryo 	if (start) {
   4148   1.1       ryo 		for (i = 0; i < AQ_RXD_NUM; i++) {
   4149   1.1       ryo 			error = aq_rxring_add(sc, rxring, i);
   4150   1.1       ryo 			if (error != 0) {
   4151   1.1       ryo 				aq_rxdrain(sc, rxring);
   4152   1.1       ryo 				return error;
   4153   1.1       ryo 			}
   4154   1.1       ryo 			aq_rxring_reset_desc(sc, rxring, i);
   4155   1.1       ryo 		}
   4156   1.1       ryo 
   4157   1.1       ryo 		/* RX descriptor physical address */
   4158   1.1       ryo 		paddr_t paddr = rxring->rxr_rxdesc_dmamap->dm_segs[0].ds_addr;
   4159   1.1       ryo 		AQ_WRITE_REG(sc, RX_DMA_DESC_BASE_ADDRLSW_REG(ringidx), paddr);
   4160   1.1       ryo 		AQ_WRITE_REG(sc, RX_DMA_DESC_BASE_ADDRMSW_REG(ringidx),
   4161   1.1       ryo 		    (uint32_t)((uint64_t)paddr >> 32));
   4162   1.1       ryo 
   4163   1.1       ryo 		/* RX descriptor size */
   4164   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(ringidx), RX_DMA_DESC_LEN,
   4165   1.1       ryo 		    AQ_RXD_NUM / 8);
   4166   1.1       ryo 
   4167   1.1       ryo 		/* maximum receive frame size */
   4168   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_BUFSIZE_REG(ringidx),
   4169   1.1       ryo 		    RX_DMA_DESC_BUFSIZE_DATA, MCLBYTES / 1024);
   4170   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_BUFSIZE_REG(ringidx),
   4171   1.1       ryo 		    RX_DMA_DESC_BUFSIZE_HDR, 0 / 1024);
   4172   1.1       ryo 
   4173   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(ringidx),
   4174   1.1       ryo 		    RX_DMA_DESC_HEADER_SPLIT, 0);
   4175   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(ringidx),
   4176   1.1       ryo 		    RX_DMA_DESC_VLAN_STRIP,
   4177   1.1       ryo 		    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_HWTAGGING) ?
   4178   1.1       ryo 		    1 : 0);
   4179   1.1       ryo 
   4180   1.1       ryo 		/*
   4181   1.1       ryo 		 * reload TAIL pointer, and update readidx
   4182   1.1       ryo 		 * (HEAD pointer cannot write)
   4183   1.1       ryo 		 */
   4184   1.1       ryo 		rxring->rxr_readidx = AQ_READ_REG_BIT(sc,
   4185   1.1       ryo 		    RX_DMA_DESC_HEAD_PTR_REG(ringidx), RX_DMA_DESC_HEAD_PTR);
   4186   1.1       ryo 		AQ_WRITE_REG(sc, RX_DMA_DESC_TAIL_PTR_REG(ringidx),
   4187   1.1       ryo 		    (rxring->rxr_readidx + AQ_RXD_NUM - 1) % AQ_RXD_NUM);
   4188   1.1       ryo 
   4189   1.1       ryo 		/* Rx ring set mode */
   4190   1.1       ryo 
   4191   1.1       ryo 		/* Mapping interrupt vector */
   4192   1.1       ryo 		AQ_WRITE_REG_BIT(sc, AQ_INTR_IRQ_MAP_RX_REG(ringidx),
   4193   1.1       ryo 		    AQ_INTR_IRQ_MAP_RX_IRQMAP(ringidx), sc->sc_rx_irq[ringidx]);
   4194   1.1       ryo 		AQ_WRITE_REG_BIT(sc, AQ_INTR_IRQ_MAP_RX_REG(ringidx),
   4195   1.1       ryo 		    AQ_INTR_IRQ_MAP_RX_EN(ringidx), 1);
   4196   1.1       ryo 
   4197   1.1       ryo 		const int cpuid = 0;	/* XXX? */
   4198   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DCAD_REG(ringidx),
   4199   1.1       ryo 		    RX_DMA_DCAD_CPUID, cpuid);
   4200   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DCAD_REG(ringidx),
   4201   1.1       ryo 		    RX_DMA_DCAD_DESC_EN, 0);
   4202   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DCAD_REG(ringidx),
   4203   1.1       ryo 		    RX_DMA_DCAD_HEADER_EN, 0);
   4204   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DCAD_REG(ringidx),
   4205   1.1       ryo 		    RX_DMA_DCAD_PAYLOAD_EN, 0);
   4206   1.1       ryo 
   4207   1.1       ryo 		/* enable DMA. start receiving */
   4208   1.1       ryo 		AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(ringidx),
   4209   1.1       ryo 		    RX_DMA_DESC_EN, 1);
   4210   1.1       ryo 
   4211   1.1       ryo 		rxring->rxr_active = true;
   4212   1.1       ryo 	}
   4213   1.1       ryo 
   4214   1.1       ryo 	mutex_exit(&rxring->rxr_mutex);
   4215   1.1       ryo 	return error;
   4216   1.1       ryo }
   4217   1.1       ryo 
   4218   1.1       ryo #define TXRING_NEXTIDX(idx)	\
   4219   1.1       ryo 	(((idx) >= (AQ_TXD_NUM - 1)) ? 0 : ((idx) + 1))
   4220   1.1       ryo #define RXRING_NEXTIDX(idx)	\
   4221   1.1       ryo 	(((idx) >= (AQ_RXD_NUM - 1)) ? 0 : ((idx) + 1))
   4222   1.1       ryo 
   4223   1.1       ryo static int
   4224   1.1       ryo aq_encap_txring(struct aq_softc *sc, struct aq_txring *txring, struct mbuf **mp)
   4225   1.1       ryo {
   4226   1.1       ryo 	bus_dmamap_t map;
   4227   1.1       ryo 	struct mbuf *m = *mp;
   4228   1.1       ryo 	uint32_t ctl1, ctl1_ctx, ctl2;
   4229   1.1       ryo 	int idx, i, error;
   4230   1.1       ryo 
   4231   1.1       ryo 	idx = txring->txr_prodidx;
   4232   1.1       ryo 	map = txring->txr_mbufs[idx].dmamap;
   4233   1.1       ryo 
   4234   1.1       ryo 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   4235   1.1       ryo 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   4236   1.1       ryo 	if (error == EFBIG) {
   4237   1.1       ryo 		struct mbuf *n;
   4238   1.1       ryo 		n = m_defrag(m, M_DONTWAIT);
   4239   1.1       ryo 		if (n == NULL)
   4240   1.1       ryo 			return EFBIG;
   4241   1.1       ryo 		/* m_defrag() preserve m */
   4242   1.1       ryo 		KASSERT(n == m);
   4243   1.1       ryo 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   4244   1.1       ryo 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   4245   1.1       ryo 	}
   4246   1.1       ryo 	if (error != 0)
   4247   1.1       ryo 		return error;
   4248   1.1       ryo 
   4249   1.1       ryo 	/*
   4250   1.1       ryo 	 * check spaces of free descriptors.
   4251   1.1       ryo 	 * +1 is additional descriptor for context (vlan, etc,.)
   4252   1.1       ryo 	 */
   4253   1.1       ryo 	if ((map->dm_nsegs + 1) > txring->txr_nfree) {
   4254   1.1       ryo 		device_printf(sc->sc_dev,
   4255   1.1       ryo 		    "TX: not enough descriptors left %d for %d segs\n",
   4256   1.1       ryo 		    txring->txr_nfree, map->dm_nsegs + 1);
   4257   1.1       ryo 		bus_dmamap_unload(sc->sc_dmat, map);
   4258   1.1       ryo 		return ENOBUFS;
   4259   1.1       ryo 	}
   4260   1.1       ryo 
   4261   1.1       ryo 	/* sync dma for mbuf */
   4262   1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   4263   1.1       ryo 	    BUS_DMASYNC_PREWRITE);
   4264   1.1       ryo 
   4265   1.1       ryo 	ctl1_ctx = 0;
   4266   1.1       ryo 	ctl2 = __SHIFTIN(m->m_pkthdr.len, AQ_TXDESC_CTL2_LEN);
   4267   1.1       ryo 
   4268   1.1       ryo 	if (vlan_has_tag(m)) {
   4269   1.1       ryo 		ctl1 = AQ_TXDESC_CTL1_TYPE_TXC;
   4270   1.1       ryo 		ctl1 |= __SHIFTIN(vlan_get_tag(m), AQ_TXDESC_CTL1_VID);
   4271   1.1       ryo 
   4272   1.1       ryo 		ctl1_ctx |= AQ_TXDESC_CTL1_CMD_VLAN;
   4273   1.1       ryo 		ctl2 |= AQ_TXDESC_CTL2_CTX_EN;
   4274   1.1       ryo 
   4275   1.1       ryo 		/* fill context descriptor and forward index */
   4276   1.1       ryo 		txring->txr_txdesc[idx].buf_addr = 0;
   4277   1.1       ryo 		txring->txr_txdesc[idx].ctl1 = htole32(ctl1);
   4278   1.1       ryo 		txring->txr_txdesc[idx].ctl2 = 0;
   4279   1.1       ryo 
   4280   1.1       ryo 		idx = TXRING_NEXTIDX(idx);
   4281   1.1       ryo 		txring->txr_nfree--;
   4282   1.1       ryo 	}
   4283   1.1       ryo 
   4284   1.1       ryo 	if (m->m_pkthdr.csum_flags & M_CSUM_IPv4)
   4285   1.1       ryo 		ctl1_ctx |= AQ_TXDESC_CTL1_CMD_IP4CSUM;
   4286   1.1       ryo 	if (m->m_pkthdr.csum_flags &
   4287   1.1       ryo 	    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
   4288   1.1       ryo 		ctl1_ctx |= AQ_TXDESC_CTL1_CMD_L4CSUM;
   4289   1.1       ryo 	}
   4290   1.1       ryo 
   4291   1.1       ryo 	/* fill descriptor(s) */
   4292   1.1       ryo 	for (i = 0; i < map->dm_nsegs; i++) {
   4293   1.1       ryo 		ctl1 = ctl1_ctx | AQ_TXDESC_CTL1_TYPE_TXD |
   4294   1.1       ryo 		    __SHIFTIN(map->dm_segs[i].ds_len, AQ_TXDESC_CTL1_BLEN);
   4295   1.1       ryo 		ctl1 |= AQ_TXDESC_CTL1_CMD_FCS;
   4296   1.1       ryo 
   4297   1.1       ryo 		if (i == 0) {
   4298   1.1       ryo 			/* remember mbuf of these descriptors */
   4299   1.1       ryo 			txring->txr_mbufs[idx].m = m;
   4300   1.1       ryo 		} else {
   4301   1.1       ryo 			txring->txr_mbufs[idx].m = NULL;
   4302   1.1       ryo 		}
   4303   1.1       ryo 
   4304   1.1       ryo 		if (i == map->dm_nsegs - 1) {
   4305   1.1       ryo 			/* last segment, mark an EndOfPacket, and cause intr */
   4306   1.1       ryo 			ctl1 |= AQ_TXDESC_CTL1_EOP | AQ_TXDESC_CTL1_CMD_WB;
   4307   1.1       ryo 		}
   4308   1.1       ryo 
   4309   1.1       ryo 		txring->txr_txdesc[idx].buf_addr =
   4310   1.1       ryo 		    htole64(map->dm_segs[i].ds_addr);
   4311   1.1       ryo 		txring->txr_txdesc[idx].ctl1 = htole32(ctl1);
   4312   1.1       ryo 		txring->txr_txdesc[idx].ctl2 = htole32(ctl2);
   4313   1.1       ryo 
   4314   1.1       ryo 		bus_dmamap_sync(sc->sc_dmat, txring->txr_txdesc_dmamap,
   4315   1.1       ryo 		    sizeof(aq_tx_desc_t) * idx, sizeof(aq_tx_desc_t),
   4316   1.1       ryo 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4317   1.1       ryo 
   4318   1.1       ryo 		idx = TXRING_NEXTIDX(idx);
   4319   1.1       ryo 		txring->txr_nfree--;
   4320   1.1       ryo 	}
   4321   1.1       ryo 
   4322   1.1       ryo 	txring->txr_prodidx = idx;
   4323   1.1       ryo 
   4324   1.1       ryo 	return 0;
   4325   1.1       ryo }
   4326   1.1       ryo 
   4327   1.1       ryo static int
   4328   1.1       ryo aq_tx_intr(void *arg)
   4329   1.1       ryo {
   4330  1.32     skrll 	struct aq_txring * const txring = arg;
   4331  1.32     skrll 	struct aq_softc * const sc = txring->txr_sc;
   4332  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   4333   1.3       ryo 	struct mbuf *m;
   4334   1.1       ryo 	const int ringidx = txring->txr_index;
   4335   1.1       ryo 	unsigned int idx, hw_head, n = 0;
   4336   1.1       ryo 
   4337   1.1       ryo 	mutex_enter(&txring->txr_mutex);
   4338   1.1       ryo 
   4339   1.1       ryo 	if (!txring->txr_active)
   4340   1.1       ryo 		goto tx_intr_done;
   4341   1.1       ryo 
   4342   1.1       ryo 	hw_head = AQ_READ_REG_BIT(sc, TX_DMA_DESC_HEAD_PTR_REG(ringidx),
   4343   1.1       ryo 	    TX_DMA_DESC_HEAD_PTR);
   4344   1.1       ryo 	if (hw_head == txring->txr_considx) {
   4345  1.33     skrll 		txring->txr_sending = false;
   4346   1.1       ryo 		goto tx_intr_done;
   4347   1.1       ryo 	}
   4348   1.1       ryo 
   4349   1.6   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   4350   1.6   thorpej 
   4351   1.1       ryo 	for (idx = txring->txr_considx; idx != hw_head;
   4352   1.1       ryo 	    idx = TXRING_NEXTIDX(idx), n++) {
   4353   1.1       ryo 
   4354   1.3       ryo 		if ((m = txring->txr_mbufs[idx].m) != NULL) {
   4355   1.1       ryo 			bus_dmamap_unload(sc->sc_dmat,
   4356   1.1       ryo 			    txring->txr_mbufs[idx].dmamap);
   4357   1.3       ryo 
   4358   1.6   thorpej 			if_statinc_ref(nsr, if_opackets);
   4359   1.6   thorpej 			if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   4360   1.3       ryo 			if (m->m_flags & M_MCAST)
   4361   1.6   thorpej 				if_statinc_ref(nsr, if_omcasts);
   4362   1.3       ryo 
   4363   1.3       ryo 			m_freem(m);
   4364   1.1       ryo 			txring->txr_mbufs[idx].m = NULL;
   4365   1.1       ryo 		}
   4366   1.1       ryo 
   4367   1.1       ryo 		txring->txr_nfree++;
   4368   1.1       ryo 	}
   4369   1.1       ryo 	txring->txr_considx = idx;
   4370   1.1       ryo 
   4371   1.6   thorpej 	IF_STAT_PUTREF(ifp);
   4372   1.6   thorpej 
   4373   1.1       ryo 	/* no more pending TX packet, cancel watchdog */
   4374   1.1       ryo 	if (txring->txr_nfree >= AQ_TXD_NUM)
   4375  1.33     skrll 		txring->txr_sending = false;
   4376   1.1       ryo 
   4377   1.1       ryo  tx_intr_done:
   4378   1.1       ryo 	mutex_exit(&txring->txr_mutex);
   4379   1.1       ryo 
   4380   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_STATUS_CLR_REG, __BIT(sc->sc_tx_irq[ringidx]));
   4381   1.1       ryo 	return n;
   4382   1.1       ryo }
   4383   1.1       ryo 
   4384   1.1       ryo static int
   4385   1.1       ryo aq_rx_intr(void *arg)
   4386   1.1       ryo {
   4387  1.32     skrll 	struct aq_rxring * const rxring = arg;
   4388  1.32     skrll 	struct aq_softc * const sc = rxring->rxr_sc;
   4389  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   4390   1.1       ryo 	const int ringidx = rxring->rxr_index;
   4391   1.1       ryo 	aq_rx_desc_t *rxd;
   4392   1.1       ryo 	struct mbuf *m, *m0, *mprev, *new_m;
   4393   1.1       ryo 	uint32_t rxd_type, rxd_hash __unused;
   4394   1.1       ryo 	uint16_t rxd_status, rxd_pktlen;
   4395   1.1       ryo 	uint16_t rxd_nextdescptr __unused, rxd_vlan __unused;
   4396   1.1       ryo 	unsigned int idx, n = 0;
   4397  1.28       ryo 	bool discarding;
   4398   1.1       ryo 
   4399   1.1       ryo 	mutex_enter(&rxring->rxr_mutex);
   4400   1.1       ryo 
   4401   1.1       ryo 	if (!rxring->rxr_active)
   4402   1.1       ryo 		goto rx_intr_done;
   4403   1.1       ryo 
   4404   1.1       ryo 	if (rxring->rxr_readidx == AQ_READ_REG_BIT(sc,
   4405   1.1       ryo 	    RX_DMA_DESC_HEAD_PTR_REG(ringidx), RX_DMA_DESC_HEAD_PTR)) {
   4406   1.1       ryo 		goto rx_intr_done;
   4407   1.1       ryo 	}
   4408   1.1       ryo 
   4409   1.6   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   4410   1.6   thorpej 
   4411  1.28       ryo 	/* restore ring context */
   4412  1.28       ryo 	discarding = rxring->rxr_discarding;
   4413  1.28       ryo 	m0 = rxring->rxr_receiving_m;
   4414  1.28       ryo 	mprev = rxring->rxr_receiving_m_last;
   4415  1.28       ryo 
   4416   1.1       ryo 	for (idx = rxring->rxr_readidx;
   4417   1.1       ryo 	    idx != AQ_READ_REG_BIT(sc, RX_DMA_DESC_HEAD_PTR_REG(ringidx),
   4418   1.1       ryo 	    RX_DMA_DESC_HEAD_PTR); idx = RXRING_NEXTIDX(idx), n++) {
   4419   1.1       ryo 
   4420   1.1       ryo 		bus_dmamap_sync(sc->sc_dmat, rxring->rxr_rxdesc_dmamap,
   4421   1.1       ryo 		    sizeof(aq_rx_desc_t) * idx, sizeof(aq_rx_desc_t),
   4422   1.1       ryo 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   4423   1.1       ryo 
   4424   1.1       ryo 		rxd = &rxring->rxr_rxdesc[idx];
   4425   1.1       ryo 		rxd_status = le16toh(rxd->wb.status);
   4426   1.1       ryo 
   4427   1.1       ryo 		if ((rxd_status & RXDESC_STATUS_DD) == 0)
   4428   1.1       ryo 			break;	/* not yet done */
   4429   1.1       ryo 
   4430   1.1       ryo 		rxd_type = le32toh(rxd->wb.type);
   4431   1.1       ryo 		rxd_pktlen = le16toh(rxd->wb.pkt_len);
   4432   1.1       ryo 		rxd_nextdescptr = le16toh(rxd->wb.next_desc_ptr);
   4433   1.1       ryo 		rxd_hash = le32toh(rxd->wb.rss_hash);
   4434   1.1       ryo 		rxd_vlan = le16toh(rxd->wb.vlan);
   4435   1.1       ryo 
   4436  1.28       ryo 		/*
   4437  1.28       ryo 		 * Some segments are being dropped while receiving jumboframe.
   4438  1.28       ryo 		 * Discard until EOP.
   4439  1.28       ryo 		 */
   4440  1.28       ryo 		if (discarding)
   4441  1.28       ryo 			goto rx_next;
   4442  1.28       ryo 
   4443   1.1       ryo 		if ((rxd_status & RXDESC_STATUS_MACERR) ||
   4444   1.1       ryo 		    (rxd_type & RXDESC_TYPE_MAC_DMA_ERR)) {
   4445   1.6   thorpej 			if_statinc_ref(nsr, if_ierrors);
   4446  1.28       ryo 			if (m0 != NULL) {
   4447  1.28       ryo 				m_freem(m0);
   4448  1.28       ryo 				m0 = mprev = NULL;
   4449  1.28       ryo 			}
   4450  1.28       ryo 			discarding = true;
   4451   1.1       ryo 			goto rx_next;
   4452   1.1       ryo 		}
   4453   1.1       ryo 
   4454   1.1       ryo 		bus_dmamap_sync(sc->sc_dmat, rxring->rxr_mbufs[idx].dmamap, 0,
   4455   1.1       ryo 		    rxring->rxr_mbufs[idx].dmamap->dm_mapsize,
   4456   1.1       ryo 		    BUS_DMASYNC_POSTREAD);
   4457   1.1       ryo 		m = rxring->rxr_mbufs[idx].m;
   4458   1.1       ryo 
   4459   1.1       ryo 		new_m = aq_alloc_mbuf();
   4460   1.1       ryo 		if (new_m == NULL) {
   4461   1.1       ryo 			/*
   4462   1.1       ryo 			 * cannot allocate new mbuf.
   4463   1.1       ryo 			 * discard this packet, and reuse mbuf for next.
   4464   1.1       ryo 			 */
   4465   1.6   thorpej 			if_statinc_ref(nsr, if_iqdrops);
   4466  1.28       ryo 			if (m0 != NULL) {
   4467  1.28       ryo 				m_freem(m0);
   4468  1.28       ryo 				m0 = mprev = NULL;
   4469  1.28       ryo 			}
   4470  1.28       ryo 			discarding = true;
   4471   1.1       ryo 			goto rx_next;
   4472   1.1       ryo 		}
   4473   1.1       ryo 		rxring->rxr_mbufs[idx].m = NULL;
   4474   1.1       ryo 		aq_rxring_setmbuf(sc, rxring, idx, new_m);
   4475   1.1       ryo 
   4476   1.1       ryo 		if (m0 == NULL) {
   4477   1.1       ryo 			m0 = m;
   4478   1.1       ryo 		} else {
   4479   1.1       ryo 			if (m->m_flags & M_PKTHDR)
   4480   1.1       ryo 				m_remove_pkthdr(m);
   4481   1.1       ryo 			mprev->m_next = m;
   4482   1.1       ryo 		}
   4483   1.1       ryo 		mprev = m;
   4484   1.1       ryo 
   4485   1.1       ryo 		if ((rxd_status & RXDESC_STATUS_EOP) == 0) {
   4486  1.28       ryo 			/* to be continued in the next segment */
   4487   1.1       ryo 			m->m_len = MCLBYTES;
   4488   1.1       ryo 		} else {
   4489  1.28       ryo 			/* the last segment */
   4490  1.24       ryo 			int mlen = rxd_pktlen % MCLBYTES;
   4491  1.24       ryo 			if (mlen == 0)
   4492  1.24       ryo 				mlen = MCLBYTES;
   4493  1.24       ryo 			m->m_len = mlen;
   4494   1.1       ryo 			m0->m_pkthdr.len = rxd_pktlen;
   4495   1.1       ryo 			/* VLAN offloading */
   4496   1.1       ryo 			if ((sc->sc_ethercom.ec_capenable &
   4497   1.1       ryo 			    ETHERCAP_VLAN_HWTAGGING) &&
   4498   1.1       ryo 			    (__SHIFTOUT(rxd_type, RXDESC_TYPE_PKTTYPE_VLAN) ||
   4499   1.1       ryo 			    __SHIFTOUT(rxd_type,
   4500   1.1       ryo 			    RXDESC_TYPE_PKTTYPE_VLAN_DOUBLE))) {
   4501   1.1       ryo 				vlan_set_tag(m0, rxd_vlan);
   4502   1.1       ryo 			}
   4503   1.1       ryo 
   4504   1.1       ryo 			/* Checksum offloading */
   4505   1.1       ryo 			unsigned int pkttype_eth =
   4506   1.1       ryo 			    __SHIFTOUT(rxd_type, RXDESC_TYPE_PKTTYPE_ETHER);
   4507   1.1       ryo 			if ((ifp->if_capabilities & IFCAP_CSUM_IPv4_Rx) &&
   4508   1.1       ryo 			    (pkttype_eth == RXDESC_TYPE_PKTTYPE_ETHER_IPV4) &&
   4509   1.1       ryo 			    __SHIFTOUT(rxd_type,
   4510   1.1       ryo 			    RXDESC_TYPE_IPV4_CSUM_CHECKED)) {
   4511   1.1       ryo 				m0->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   4512   1.1       ryo 				if (__SHIFTOUT(rxd_status,
   4513   1.1       ryo 				    RXDESC_STATUS_IPV4_CSUM_NG))
   4514   1.1       ryo 					m0->m_pkthdr.csum_flags |=
   4515   1.1       ryo 					    M_CSUM_IPv4_BAD;
   4516   1.1       ryo 			}
   4517  1.21       ryo 
   4518   1.1       ryo 			/*
   4519  1.21       ryo 			 * aq will always mark BAD for fragment packets,
   4520  1.21       ryo 			 * but this is not a problem because the IP stack
   4521  1.21       ryo 			 * ignores the CSUM flag in fragment packets.
   4522   1.1       ryo 			 */
   4523   1.1       ryo 			if (__SHIFTOUT(rxd_type,
   4524   1.1       ryo 			    RXDESC_TYPE_TCPUDP_CSUM_CHECKED)) {
   4525   1.1       ryo 				bool checked = false;
   4526   1.1       ryo 				unsigned int pkttype_proto =
   4527   1.1       ryo 				    __SHIFTOUT(rxd_type,
   4528   1.1       ryo 				    RXDESC_TYPE_PKTTYPE_PROTO);
   4529   1.1       ryo 
   4530   1.1       ryo 				if (pkttype_proto ==
   4531   1.1       ryo 				    RXDESC_TYPE_PKTTYPE_PROTO_TCP) {
   4532   1.1       ryo 					if ((pkttype_eth ==
   4533   1.1       ryo 					    RXDESC_TYPE_PKTTYPE_ETHER_IPV4) &&
   4534   1.1       ryo 					    (ifp->if_capabilities &
   4535   1.1       ryo 					    IFCAP_CSUM_TCPv4_Rx)) {
   4536   1.1       ryo 						m0->m_pkthdr.csum_flags |=
   4537   1.1       ryo 						    M_CSUM_TCPv4;
   4538   1.1       ryo 						checked = true;
   4539   1.1       ryo 					} else if ((pkttype_eth ==
   4540   1.1       ryo 					    RXDESC_TYPE_PKTTYPE_ETHER_IPV6) &&
   4541   1.1       ryo 					    (ifp->if_capabilities &
   4542   1.1       ryo 					    IFCAP_CSUM_TCPv6_Rx)) {
   4543   1.1       ryo 						m0->m_pkthdr.csum_flags |=
   4544   1.1       ryo 						    M_CSUM_TCPv6;
   4545   1.1       ryo 						checked = true;
   4546   1.1       ryo 					}
   4547   1.1       ryo 				} else if (pkttype_proto ==
   4548   1.1       ryo 				    RXDESC_TYPE_PKTTYPE_PROTO_UDP) {
   4549   1.1       ryo 					if ((pkttype_eth ==
   4550   1.1       ryo 					    RXDESC_TYPE_PKTTYPE_ETHER_IPV4) &&
   4551   1.1       ryo 					    (ifp->if_capabilities &
   4552   1.1       ryo 					    IFCAP_CSUM_UDPv4_Rx)) {
   4553   1.1       ryo 						m0->m_pkthdr.csum_flags |=
   4554   1.1       ryo 						    M_CSUM_UDPv4;
   4555   1.1       ryo 						checked = true;
   4556   1.1       ryo 					} else if ((pkttype_eth ==
   4557   1.1       ryo 					    RXDESC_TYPE_PKTTYPE_ETHER_IPV6) &&
   4558   1.1       ryo 					    (ifp->if_capabilities &
   4559   1.1       ryo 					    IFCAP_CSUM_UDPv6_Rx)) {
   4560   1.1       ryo 						m0->m_pkthdr.csum_flags |=
   4561   1.1       ryo 						    M_CSUM_UDPv6;
   4562   1.1       ryo 						checked = true;
   4563   1.1       ryo 					}
   4564   1.1       ryo 				}
   4565   1.1       ryo 				if (checked &&
   4566   1.1       ryo 				    (__SHIFTOUT(rxd_status,
   4567   1.1       ryo 				    RXDESC_STATUS_TCPUDP_CSUM_ERROR) ||
   4568   1.1       ryo 				    !__SHIFTOUT(rxd_status,
   4569   1.1       ryo 				    RXDESC_STATUS_TCPUDP_CSUM_OK))) {
   4570   1.1       ryo 					m0->m_pkthdr.csum_flags |=
   4571   1.1       ryo 					    M_CSUM_TCP_UDP_BAD;
   4572   1.1       ryo 				}
   4573   1.1       ryo 			}
   4574  1.21       ryo 
   4575   1.1       ryo 			m_set_rcvif(m0, ifp);
   4576   1.6   thorpej 			if_statinc_ref(nsr, if_ipackets);
   4577   1.6   thorpej 			if_statadd_ref(nsr, if_ibytes, m0->m_pkthdr.len);
   4578   1.1       ryo 			if_percpuq_enqueue(ifp->if_percpuq, m0);
   4579   1.1       ryo 			m0 = mprev = NULL;
   4580   1.1       ryo 		}
   4581   1.1       ryo 
   4582   1.1       ryo  rx_next:
   4583  1.28       ryo 		if (discarding && (rxd_status & RXDESC_STATUS_EOP) != 0)
   4584  1.28       ryo 			discarding = false;
   4585  1.28       ryo 
   4586   1.1       ryo 		aq_rxring_reset_desc(sc, rxring, idx);
   4587   1.1       ryo 		AQ_WRITE_REG(sc, RX_DMA_DESC_TAIL_PTR_REG(ringidx), idx);
   4588   1.1       ryo 	}
   4589  1.28       ryo 	/* save ring context */
   4590   1.1       ryo 	rxring->rxr_readidx = idx;
   4591  1.28       ryo 	rxring->rxr_discarding = discarding;
   4592  1.28       ryo 	rxring->rxr_receiving_m = m0;
   4593  1.28       ryo 	rxring->rxr_receiving_m_last = mprev;
   4594   1.1       ryo 
   4595   1.6   thorpej 	IF_STAT_PUTREF(ifp);
   4596   1.6   thorpej 
   4597   1.1       ryo  rx_intr_done:
   4598   1.1       ryo 	mutex_exit(&rxring->rxr_mutex);
   4599   1.1       ryo 
   4600   1.1       ryo 	AQ_WRITE_REG(sc, AQ_INTR_STATUS_CLR_REG, __BIT(sc->sc_rx_irq[ringidx]));
   4601   1.1       ryo 	return n;
   4602   1.1       ryo }
   4603   1.1       ryo 
   4604   1.1       ryo static int
   4605  1.10       ryo aq_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
   4606  1.10       ryo {
   4607  1.10       ryo 	struct ifnet *ifp = &ec->ec_if;
   4608  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4609  1.10       ryo 
   4610  1.10       ryo 	aq_set_vlan_filters(sc);
   4611  1.10       ryo 	return 0;
   4612  1.10       ryo }
   4613  1.10       ryo 
   4614  1.10       ryo static int
   4615   1.1       ryo aq_ifflags_cb(struct ethercom *ec)
   4616   1.1       ryo {
   4617  1.32     skrll 	struct ifnet * const ifp = &ec->ec_if;
   4618  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4619   1.1       ryo 	int i, ecchange, error = 0;
   4620   1.1       ryo 	unsigned short iffchange;
   4621   1.1       ryo 
   4622   1.1       ryo 	AQ_LOCK(sc);
   4623   1.1       ryo 
   4624   1.1       ryo 	iffchange = ifp->if_flags ^ sc->sc_if_flags;
   4625   1.1       ryo 	if ((iffchange & IFF_PROMISC) != 0)
   4626   1.1       ryo 		error = aq_set_filter(sc);
   4627   1.1       ryo 
   4628   1.1       ryo 	ecchange = ec->ec_capenable ^ sc->sc_ec_capenable;
   4629   1.1       ryo 	if (ecchange & ETHERCAP_VLAN_HWTAGGING) {
   4630   1.1       ryo 		for (i = 0; i < AQ_RINGS_NUM; i++) {
   4631   1.1       ryo 			AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_REG(i),
   4632   1.1       ryo 			    RX_DMA_DESC_VLAN_STRIP,
   4633   1.1       ryo 			    (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) ?
   4634   1.1       ryo 			    1 : 0);
   4635   1.1       ryo 		}
   4636   1.1       ryo 	}
   4637   1.1       ryo 
   4638  1.10       ryo 	/* vlan configuration depends on also interface promiscuous mode */
   4639  1.10       ryo 	if ((ecchange & ETHERCAP_VLAN_HWFILTER) || (iffchange & IFF_PROMISC))
   4640  1.10       ryo 		aq_set_vlan_filters(sc);
   4641  1.10       ryo 
   4642   1.1       ryo 	sc->sc_ec_capenable = ec->ec_capenable;
   4643   1.1       ryo 	sc->sc_if_flags = ifp->if_flags;
   4644   1.1       ryo 
   4645   1.1       ryo 	AQ_UNLOCK(sc);
   4646   1.1       ryo 
   4647   1.1       ryo 	return error;
   4648   1.1       ryo }
   4649   1.1       ryo 
   4650  1.33     skrll 
   4651   1.1       ryo static int
   4652   1.1       ryo aq_init(struct ifnet *ifp)
   4653   1.1       ryo {
   4654  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4655  1.33     skrll 
   4656  1.33     skrll 	AQ_LOCK(sc);
   4657  1.33     skrll 
   4658  1.33     skrll 	int ret = aq_init_locked(ifp);
   4659  1.33     skrll 
   4660  1.33     skrll 	AQ_UNLOCK(sc);
   4661  1.33     skrll 
   4662  1.33     skrll 	return ret;
   4663  1.33     skrll }
   4664  1.33     skrll 
   4665  1.33     skrll static int
   4666  1.33     skrll aq_init_locked(struct ifnet *ifp)
   4667  1.33     skrll {
   4668  1.33     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4669   1.1       ryo 	int i, error = 0;
   4670   1.1       ryo 
   4671  1.33     skrll 	KASSERT(IFNET_LOCKED(ifp));
   4672  1.33     skrll 	AQ_LOCKED(sc);
   4673  1.22       ryo 
   4674  1.33     skrll 	aq_stop_locked(ifp, false);
   4675   1.1       ryo 
   4676  1.10       ryo 	aq_set_vlan_filters(sc);
   4677   1.1       ryo 	aq_set_capability(sc);
   4678   1.1       ryo 
   4679   1.1       ryo 	for (i = 0; i < sc->sc_nqueues; i++) {
   4680   1.1       ryo 		aq_txring_reset(sc, &sc->sc_queue[i].txring, true);
   4681   1.1       ryo 	}
   4682   1.1       ryo 
   4683   1.1       ryo 	/* invalidate RX descriptor cache */
   4684   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_CACHE_INIT_REG, RX_DMA_DESC_CACHE_INIT,
   4685   1.1       ryo 	    AQ_READ_REG_BIT(sc,
   4686   1.1       ryo 	    RX_DMA_DESC_CACHE_INIT_REG, RX_DMA_DESC_CACHE_INIT) ^ 1);
   4687   1.1       ryo 
   4688   1.1       ryo 	/* start RX */
   4689   1.1       ryo 	for (i = 0; i < sc->sc_nqueues; i++) {
   4690   1.1       ryo 		error = aq_rxring_reset(sc, &sc->sc_queue[i].rxring, true);
   4691   1.1       ryo 		if (error != 0) {
   4692   1.1       ryo 			device_printf(sc->sc_dev, "%s: cannot allocate rxbuf\n",
   4693   1.1       ryo 			    __func__);
   4694   1.1       ryo 			goto aq_init_failure;
   4695   1.1       ryo 		}
   4696   1.1       ryo 	}
   4697   1.1       ryo 	aq_init_rss(sc);
   4698   1.1       ryo 	aq_hw_l3_filter_set(sc);
   4699   1.1       ryo 
   4700  1.33     skrll 	/* ring reset? */
   4701  1.33     skrll 	aq_unset_stopping_flags(sc);
   4702  1.33     skrll 
   4703  1.33     skrll 	callout_schedule(&sc->sc_tick_ch, hz);
   4704   1.1       ryo 
   4705   1.1       ryo 	/* ready */
   4706   1.1       ryo 	ifp->if_flags |= IFF_RUNNING;
   4707   1.1       ryo 
   4708   1.1       ryo 	/* start TX and RX */
   4709  1.37  riastrad 	aq_enable_intr(sc, /*link*/true, /*txrx*/true);
   4710   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TX_BUF_REG, TPB_TX_BUF_EN, 1);
   4711   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_BUF_EN, 1);
   4712   1.1       ryo 
   4713   1.1       ryo  aq_init_failure:
   4714   1.1       ryo 	sc->sc_if_flags = ifp->if_flags;
   4715   1.1       ryo 
   4716   1.1       ryo 	return error;
   4717   1.1       ryo }
   4718   1.1       ryo 
   4719   1.1       ryo static void
   4720   1.1       ryo aq_send_common_locked(struct ifnet *ifp, struct aq_softc *sc,
   4721   1.1       ryo     struct aq_txring *txring, bool is_transmit)
   4722   1.1       ryo {
   4723   1.1       ryo 	struct mbuf *m;
   4724   1.1       ryo 	int npkt, error;
   4725   1.1       ryo 
   4726  1.33     skrll 	if (txring->txr_nfree < AQ_TXD_MIN)
   4727   1.1       ryo 		return;
   4728   1.1       ryo 
   4729   1.1       ryo 	for (npkt = 0; ; npkt++) {
   4730   1.1       ryo 		if (is_transmit)
   4731   1.1       ryo 			m = pcq_peek(txring->txr_pcq);
   4732   1.1       ryo 		else
   4733   1.1       ryo 			IFQ_POLL(&ifp->if_snd, m);
   4734   1.1       ryo 
   4735   1.1       ryo 		if (m == NULL)
   4736   1.1       ryo 			break;
   4737   1.1       ryo 
   4738   1.1       ryo 		if (is_transmit)
   4739   1.1       ryo 			pcq_get(txring->txr_pcq);
   4740   1.1       ryo 		else
   4741   1.1       ryo 			IFQ_DEQUEUE(&ifp->if_snd, m);
   4742   1.1       ryo 
   4743   1.1       ryo 		error = aq_encap_txring(sc, txring, &m);
   4744   1.1       ryo 		if (error != 0) {
   4745   1.1       ryo 			/* too many mbuf chains? or not enough descriptors? */
   4746   1.1       ryo 			m_freem(m);
   4747   1.6   thorpej 			if_statinc(ifp, if_oerrors);
   4748   1.1       ryo 			break;
   4749   1.1       ryo 		}
   4750   1.1       ryo 
   4751   1.1       ryo 		/* update tail ptr */
   4752   1.1       ryo 		AQ_WRITE_REG(sc, TX_DMA_DESC_TAIL_PTR_REG(txring->txr_index),
   4753   1.1       ryo 		    txring->txr_prodidx);
   4754   1.1       ryo 
   4755   1.1       ryo 		/* Pass the packet to any BPF listeners */
   4756   1.1       ryo 		bpf_mtap(ifp, m, BPF_D_OUT);
   4757   1.1       ryo 	}
   4758   1.1       ryo 
   4759  1.33     skrll 	if (npkt) {
   4760  1.33     skrll 		/* Set a watchdog timer in case the chip flakes out. */
   4761  1.33     skrll 		txring->txr_lastsent = time_uptime;
   4762  1.33     skrll 		txring->txr_sending = true;
   4763  1.33     skrll 	}
   4764   1.1       ryo }
   4765   1.1       ryo 
   4766   1.1       ryo static void
   4767   1.1       ryo aq_start(struct ifnet *ifp)
   4768   1.1       ryo {
   4769  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4770  1.32     skrll 	/* aq_start() always use TX ring[0] */
   4771  1.32     skrll 	struct aq_txring * const txring = &sc->sc_queue[0].txring;
   4772   1.1       ryo 
   4773   1.1       ryo 	mutex_enter(&txring->txr_mutex);
   4774  1.33     skrll 	if (txring->txr_active && !txring->txr_stopping)
   4775   1.1       ryo 		aq_send_common_locked(ifp, sc, txring, false);
   4776   1.1       ryo 	mutex_exit(&txring->txr_mutex);
   4777   1.1       ryo }
   4778   1.1       ryo 
   4779   1.1       ryo static inline unsigned int
   4780   1.1       ryo aq_select_txqueue(struct aq_softc *sc, struct mbuf *m)
   4781   1.1       ryo {
   4782   1.1       ryo 	return (cpu_index(curcpu()) % sc->sc_nqueues);
   4783   1.1       ryo }
   4784   1.1       ryo 
   4785   1.1       ryo static int
   4786   1.1       ryo aq_transmit(struct ifnet *ifp, struct mbuf *m)
   4787   1.1       ryo {
   4788  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4789  1.32     skrll 	const int ringidx = aq_select_txqueue(sc, m);
   4790  1.32     skrll 	struct aq_txring * const txring = &sc->sc_queue[ringidx].txring;
   4791   1.1       ryo 
   4792   1.1       ryo 	if (__predict_false(!pcq_put(txring->txr_pcq, m))) {
   4793   1.1       ryo 		m_freem(m);
   4794   1.1       ryo 		return ENOBUFS;
   4795   1.1       ryo 	}
   4796   1.1       ryo 
   4797   1.1       ryo 	if (mutex_tryenter(&txring->txr_mutex)) {
   4798   1.1       ryo 		aq_send_common_locked(ifp, sc, txring, true);
   4799   1.1       ryo 		mutex_exit(&txring->txr_mutex);
   4800   1.1       ryo 	} else {
   4801   1.1       ryo 		softint_schedule(txring->txr_softint);
   4802   1.1       ryo 	}
   4803   1.1       ryo 	return 0;
   4804   1.1       ryo }
   4805   1.1       ryo 
   4806   1.1       ryo static void
   4807   1.1       ryo aq_deferred_transmit(void *arg)
   4808   1.1       ryo {
   4809  1.32     skrll 	struct aq_txring * const txring = arg;
   4810  1.32     skrll 	struct aq_softc * const sc = txring->txr_sc;
   4811  1.32     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   4812   1.1       ryo 
   4813   1.1       ryo 	mutex_enter(&txring->txr_mutex);
   4814   1.1       ryo 	if (pcq_peek(txring->txr_pcq) != NULL)
   4815   1.1       ryo 		aq_send_common_locked(ifp, sc, txring, true);
   4816   1.1       ryo 	mutex_exit(&txring->txr_mutex);
   4817   1.1       ryo }
   4818   1.1       ryo 
   4819  1.33     skrll 
   4820  1.33     skrll static void
   4821  1.33     skrll aq_unset_stopping_flags(struct aq_softc *sc)
   4822  1.33     skrll {
   4823  1.33     skrll 
   4824  1.33     skrll 	AQ_LOCKED(sc);
   4825  1.33     skrll 
   4826  1.33     skrll 	/* Must unset stopping flags in ascending order. */
   4827  1.40       ryo 	for (int i = 0; i < sc->sc_nqueues; i++) {
   4828  1.33     skrll 		struct aq_txring *txr = &sc->sc_queue[i].txring;
   4829  1.33     skrll 		struct aq_rxring *rxr = &sc->sc_queue[i].rxring;
   4830  1.33     skrll 
   4831  1.33     skrll 		mutex_enter(&txr->txr_mutex);
   4832  1.33     skrll 		txr->txr_stopping = false;
   4833  1.33     skrll 		mutex_exit(&txr->txr_mutex);
   4834  1.33     skrll 
   4835  1.33     skrll 		mutex_enter(&rxr->rxr_mutex);
   4836  1.33     skrll 		rxr->rxr_stopping = false;
   4837  1.33     skrll 		mutex_exit(&rxr->rxr_mutex);
   4838  1.33     skrll 	}
   4839  1.33     skrll 
   4840  1.33     skrll 	sc->sc_stopping = false;
   4841  1.33     skrll }
   4842  1.33     skrll 
   4843  1.33     skrll static void
   4844  1.33     skrll aq_set_stopping_flags(struct aq_softc *sc)
   4845  1.33     skrll {
   4846  1.33     skrll 
   4847  1.33     skrll 	AQ_LOCKED(sc);
   4848  1.33     skrll 
   4849  1.33     skrll 	/* Must unset stopping flags in ascending order. */
   4850  1.40       ryo 	for (int i = 0; i < sc->sc_nqueues; i++) {
   4851  1.33     skrll 		struct aq_txring *txr = &sc->sc_queue[i].txring;
   4852  1.33     skrll 		struct aq_rxring *rxr = &sc->sc_queue[i].rxring;
   4853  1.33     skrll 
   4854  1.33     skrll 		mutex_enter(&txr->txr_mutex);
   4855  1.33     skrll 		txr->txr_stopping = true;
   4856  1.33     skrll 		mutex_exit(&txr->txr_mutex);
   4857  1.33     skrll 
   4858  1.33     skrll 		mutex_enter(&rxr->rxr_mutex);
   4859  1.33     skrll 		rxr->rxr_stopping = true;
   4860  1.33     skrll 		mutex_exit(&rxr->rxr_mutex);
   4861  1.33     skrll 	}
   4862  1.33     skrll 
   4863  1.33     skrll 	sc->sc_stopping = true;
   4864  1.33     skrll }
   4865  1.33     skrll 
   4866  1.33     skrll 
   4867   1.1       ryo static void
   4868   1.1       ryo aq_stop(struct ifnet *ifp, int disable)
   4869   1.1       ryo {
   4870  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4871  1.33     skrll 
   4872  1.33     skrll 	ASSERT_SLEEPABLE();
   4873  1.33     skrll 	KASSERT(IFNET_LOCKED(ifp));
   4874  1.33     skrll 
   4875  1.33     skrll 	AQ_LOCK(sc);
   4876  1.33     skrll 	aq_stop_locked(ifp, disable ? true : false);
   4877  1.33     skrll 	AQ_UNLOCK(sc);
   4878  1.33     skrll }
   4879  1.33     skrll 
   4880  1.33     skrll 
   4881  1.33     skrll 
   4882  1.33     skrll static void
   4883  1.33     skrll aq_stop_locked(struct ifnet *ifp, bool disable)
   4884  1.33     skrll {
   4885  1.33     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4886   1.1       ryo 	int i;
   4887   1.1       ryo 
   4888  1.33     skrll 	KASSERT(IFNET_LOCKED(ifp));
   4889  1.33     skrll 	AQ_LOCKED(sc);
   4890   1.1       ryo 
   4891  1.33     skrll 	aq_set_stopping_flags(sc);
   4892   1.1       ryo 
   4893  1.22       ryo 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   4894  1.22       ryo 		goto already_stopped;
   4895  1.22       ryo 
   4896   1.1       ryo 	/* disable tx/rx interrupts */
   4897  1.37  riastrad 	aq_enable_intr(sc, /*link*/true, /*txrx*/false);
   4898   1.1       ryo 
   4899   1.1       ryo 	AQ_WRITE_REG_BIT(sc, TPB_TX_BUF_REG, TPB_TX_BUF_EN, 0);
   4900   1.1       ryo 	for (i = 0; i < sc->sc_nqueues; i++) {
   4901   1.1       ryo 		aq_txring_reset(sc, &sc->sc_queue[i].txring, false);
   4902   1.1       ryo 	}
   4903   1.1       ryo 
   4904   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RPB_RPF_RX_REG, RPB_RPF_RX_BUF_EN, 0);
   4905   1.1       ryo 	for (i = 0; i < sc->sc_nqueues; i++) {
   4906   1.1       ryo 		aq_rxring_reset(sc, &sc->sc_queue[i].rxring, false);
   4907   1.1       ryo 	}
   4908   1.1       ryo 
   4909   1.1       ryo 	/* invalidate RX descriptor cache */
   4910   1.1       ryo 	AQ_WRITE_REG_BIT(sc, RX_DMA_DESC_CACHE_INIT_REG, RX_DMA_DESC_CACHE_INIT,
   4911   1.1       ryo 	    AQ_READ_REG_BIT(sc,
   4912   1.1       ryo 	    RX_DMA_DESC_CACHE_INIT_REG, RX_DMA_DESC_CACHE_INIT) ^ 1);
   4913   1.1       ryo 
   4914  1.22       ryo  already_stopped:
   4915  1.37  riastrad 	aq_enable_intr(sc, /*link*/false, /*txrx*/false);
   4916  1.36  riastrad 	callout_halt(&sc->sc_tick_ch, &sc->sc_mutex);
   4917   1.1       ryo 
   4918  1.33     skrll 	ifp->if_flags &= ~IFF_RUNNING;
   4919  1.33     skrll 	sc->sc_if_flags = ifp->if_flags;
   4920  1.33     skrll }
   4921   1.1       ryo 
   4922   1.1       ryo 
   4923   1.1       ryo static void
   4924  1.33     skrll aq_handle_reset_work(struct work *work, void *arg)
   4925   1.1       ryo {
   4926  1.33     skrll 	struct aq_softc * const sc = arg;
   4927  1.33     skrll 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   4928  1.33     skrll 
   4929  1.33     skrll 	printf("%s: watchdog timeout -- resetting\n", ifp->if_xname);
   4930   1.1       ryo 
   4931   1.1       ryo 	AQ_LOCK(sc);
   4932   1.1       ryo 
   4933   1.1       ryo 	device_printf(sc->sc_dev, "%s: INTR_MASK/STATUS = %08x/%08x\n",
   4934   1.1       ryo 	    __func__, AQ_READ_REG(sc, AQ_INTR_MASK_REG),
   4935   1.1       ryo 	    AQ_READ_REG(sc, AQ_INTR_STATUS_REG));
   4936   1.1       ryo 
   4937  1.40       ryo 	for (int n = 0; n < sc->sc_nqueues; n++) {
   4938  1.33     skrll 		struct aq_txring *txring = &sc->sc_queue[n].txring;
   4939  1.33     skrll 		u_int head = AQ_READ_REG_BIT(sc,
   4940   1.1       ryo 		    TX_DMA_DESC_HEAD_PTR_REG(txring->txr_index),
   4941  1.33     skrll 		    TX_DMA_DESC_HEAD_PTR);
   4942  1.33     skrll 		u_int tail = AQ_READ_REG(sc,
   4943   1.1       ryo 		    TX_DMA_DESC_TAIL_PTR_REG(txring->txr_index));
   4944   1.1       ryo 
   4945  1.33     skrll 		device_printf(sc->sc_dev, "%s: TXring[%u] HEAD/TAIL=%u/%u\n",
   4946   1.1       ryo 		    __func__, txring->txr_index, head, tail);
   4947   1.1       ryo 
   4948   1.1       ryo 		aq_tx_intr(txring);
   4949   1.1       ryo 	}
   4950   1.1       ryo 
   4951   1.1       ryo 	AQ_UNLOCK(sc);
   4952   1.1       ryo 
   4953  1.33     skrll 	/* Don't want ioctl operations to happen */
   4954  1.33     skrll 	IFNET_LOCK(ifp);
   4955  1.33     skrll 
   4956  1.33     skrll 	/* reset the interface. */
   4957   1.1       ryo 	aq_init(ifp);
   4958  1.33     skrll 
   4959  1.33     skrll 	IFNET_UNLOCK(ifp);
   4960  1.33     skrll 
   4961  1.33     skrll 	atomic_store_relaxed(&sc->sc_reset_pending, 0);
   4962   1.1       ryo }
   4963   1.1       ryo 
   4964   1.1       ryo static int
   4965   1.1       ryo aq_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   4966   1.1       ryo {
   4967  1.32     skrll 	struct aq_softc * const sc = ifp->if_softc;
   4968  1.32     skrll 	struct ifreq * const ifr = data;
   4969  1.32     skrll 	int error = 0;
   4970   1.1       ryo 
   4971  1.33     skrll 	switch (cmd) {
   4972  1.33     skrll 	case SIOCADDMULTI:
   4973  1.33     skrll 	case SIOCDELMULTI:
   4974  1.33     skrll 		break;
   4975  1.33     skrll 	default:
   4976  1.33     skrll 		KASSERT(IFNET_LOCKED(ifp));
   4977  1.33     skrll 	}
   4978  1.33     skrll 
   4979  1.32     skrll 	const int s = splnet();
   4980  1.23       ryo 	switch (cmd) {
   4981  1.23       ryo 	case SIOCSIFMTU:
   4982  1.23       ryo 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > sc->sc_max_mtu) {
   4983  1.23       ryo 			error = EINVAL;
   4984  1.23       ryo 		} else {
   4985  1.23       ryo 			ifp->if_mtu = ifr->ifr_mtu;
   4986  1.23       ryo 			error = 0;	/* no need to reset (no ENETRESET) */
   4987  1.23       ryo 		}
   4988  1.23       ryo 		break;
   4989  1.23       ryo 	default:
   4990  1.23       ryo 		error = ether_ioctl(ifp, cmd, data);
   4991  1.23       ryo 		break;
   4992  1.23       ryo 	}
   4993   1.1       ryo 	splx(s);
   4994   1.1       ryo 
   4995   1.1       ryo 	if (error != ENETRESET)
   4996   1.1       ryo 		return error;
   4997   1.1       ryo 
   4998   1.1       ryo 	switch (cmd) {
   4999   1.1       ryo 	case SIOCSIFCAP:
   5000   1.1       ryo 		error = aq_set_capability(sc);
   5001   1.1       ryo 		break;
   5002   1.1       ryo 	case SIOCADDMULTI:
   5003   1.1       ryo 	case SIOCDELMULTI:
   5004  1.33     skrll 		AQ_LOCK(sc);
   5005  1.33     skrll 		if ((sc->sc_if_flags & IFF_RUNNING) != 0) {
   5006  1.33     skrll 		       /*
   5007  1.33     skrll 			* Multicast list has changed; set the hardware filter
   5008  1.33     skrll 			* accordingly.
   5009  1.33     skrll 			*/
   5010  1.33     skrll 			error = aq_set_filter(sc);
   5011  1.33     skrll 		}
   5012  1.33     skrll 		AQ_UNLOCK(sc);
   5013   1.1       ryo 		break;
   5014   1.1       ryo 	}
   5015   1.1       ryo 
   5016   1.1       ryo 	return error;
   5017   1.1       ryo }
   5018   1.1       ryo 
   5019   1.1       ryo 
   5020   1.1       ryo MODULE(MODULE_CLASS_DRIVER, if_aq, "pci");
   5021   1.1       ryo 
   5022   1.1       ryo #ifdef _MODULE
   5023   1.1       ryo #include "ioconf.c"
   5024   1.1       ryo #endif
   5025   1.1       ryo 
   5026   1.1       ryo static int
   5027   1.1       ryo if_aq_modcmd(modcmd_t cmd, void *opaque)
   5028   1.1       ryo {
   5029   1.1       ryo 	int error = 0;
   5030   1.1       ryo 
   5031   1.1       ryo 	switch (cmd) {
   5032   1.1       ryo 	case MODULE_CMD_INIT:
   5033   1.1       ryo #ifdef _MODULE
   5034   1.1       ryo 		error = config_init_component(cfdriver_ioconf_if_aq,
   5035   1.1       ryo 		    cfattach_ioconf_if_aq, cfdata_ioconf_if_aq);
   5036   1.1       ryo #endif
   5037   1.1       ryo 		return error;
   5038   1.1       ryo 	case MODULE_CMD_FINI:
   5039   1.1       ryo #ifdef _MODULE
   5040   1.1       ryo 		error = config_fini_component(cfdriver_ioconf_if_aq,
   5041   1.1       ryo 		    cfattach_ioconf_if_aq, cfdata_ioconf_if_aq);
   5042   1.1       ryo #endif
   5043   1.1       ryo 		return error;
   5044   1.1       ryo 	default:
   5045   1.1       ryo 		return ENOTTY;
   5046   1.1       ryo 	}
   5047   1.1       ryo }
   5048