18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * forcedeth: Ethernet driver for NVIDIA nForce media access controllers.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Note: This driver is a cleanroom reimplementation based on reverse
68c2ecf20Sopenharmony_ci *      engineered documentation written by Carl-Daniel Hailfinger
78c2ecf20Sopenharmony_ci *      and Andrew de Quincey.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * NVIDIA, nForce and other NVIDIA marks are trademarks or registered
108c2ecf20Sopenharmony_ci * trademarks of NVIDIA Corporation in the United States and other
118c2ecf20Sopenharmony_ci * countries.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (C) 2003,4,5 Manfred Spraul
148c2ecf20Sopenharmony_ci * Copyright (C) 2004 Andrew de Quincey (wol support)
158c2ecf20Sopenharmony_ci * Copyright (C) 2004 Carl-Daniel Hailfinger (invalid MAC handling, insane
168c2ecf20Sopenharmony_ci *		IRQ rate fixes, bigendian fixes, cleanups, verification)
178c2ecf20Sopenharmony_ci * Copyright (c) 2004,2005,2006,2007,2008,2009 NVIDIA Corporation
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Known bugs:
208c2ecf20Sopenharmony_ci * We suspect that on some hardware no TX done interrupts are generated.
218c2ecf20Sopenharmony_ci * This means recovery from netif_stop_queue only happens if the hw timer
228c2ecf20Sopenharmony_ci * interrupt fires (100 times/second, configurable with NVREG_POLL_DEFAULT)
238c2ecf20Sopenharmony_ci * and the timer is active in the IRQMask, or if a rx packet arrives by chance.
248c2ecf20Sopenharmony_ci * If your hardware reliably generates tx done interrupts, then you can remove
258c2ecf20Sopenharmony_ci * DEV_NEED_TIMERIRQ from the driver_data flags.
268c2ecf20Sopenharmony_ci * DEV_NEED_TIMERIRQ will not harm you on sane hardware, only generating a few
278c2ecf20Sopenharmony_ci * superfluous timer interrupts from the nic.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define FORCEDETH_VERSION		"0.64"
338c2ecf20Sopenharmony_ci#define DRV_NAME			"forcedeth"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/module.h>
368c2ecf20Sopenharmony_ci#include <linux/types.h>
378c2ecf20Sopenharmony_ci#include <linux/pci.h>
388c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
398c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
408c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
418c2ecf20Sopenharmony_ci#include <linux/delay.h>
428c2ecf20Sopenharmony_ci#include <linux/sched.h>
438c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
448c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
458c2ecf20Sopenharmony_ci#include <linux/timer.h>
468c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
478c2ecf20Sopenharmony_ci#include <linux/mii.h>
488c2ecf20Sopenharmony_ci#include <linux/random.h>
498c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
508c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
518c2ecf20Sopenharmony_ci#include <linux/slab.h>
528c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
538c2ecf20Sopenharmony_ci#include <linux/prefetch.h>
548c2ecf20Sopenharmony_ci#include <linux/u64_stats_sync.h>
558c2ecf20Sopenharmony_ci#include <linux/io.h>
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#include <asm/irq.h>
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define TX_WORK_PER_LOOP  64
608c2ecf20Sopenharmony_ci#define RX_WORK_PER_LOOP  64
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * Hardware access:
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define DEV_NEED_TIMERIRQ          0x0000001  /* set the timer irq flag in the irq mask */
678c2ecf20Sopenharmony_ci#define DEV_NEED_LINKTIMER         0x0000002  /* poll link settings. Relies on the timer irq */
688c2ecf20Sopenharmony_ci#define DEV_HAS_LARGEDESC          0x0000004  /* device supports jumbo frames and needs packet format 2 */
698c2ecf20Sopenharmony_ci#define DEV_HAS_HIGH_DMA           0x0000008  /* device supports 64bit dma */
708c2ecf20Sopenharmony_ci#define DEV_HAS_CHECKSUM           0x0000010  /* device supports tx and rx checksum offloads */
718c2ecf20Sopenharmony_ci#define DEV_HAS_VLAN               0x0000020  /* device supports vlan tagging and striping */
728c2ecf20Sopenharmony_ci#define DEV_HAS_MSI                0x0000040  /* device supports MSI */
738c2ecf20Sopenharmony_ci#define DEV_HAS_MSI_X              0x0000080  /* device supports MSI-X */
748c2ecf20Sopenharmony_ci#define DEV_HAS_POWER_CNTRL        0x0000100  /* device supports power savings */
758c2ecf20Sopenharmony_ci#define DEV_HAS_STATISTICS_V1      0x0000200  /* device supports hw statistics version 1 */
768c2ecf20Sopenharmony_ci#define DEV_HAS_STATISTICS_V2      0x0000400  /* device supports hw statistics version 2 */
778c2ecf20Sopenharmony_ci#define DEV_HAS_STATISTICS_V3      0x0000800  /* device supports hw statistics version 3 */
788c2ecf20Sopenharmony_ci#define DEV_HAS_STATISTICS_V12     0x0000600  /* device supports hw statistics version 1 and 2 */
798c2ecf20Sopenharmony_ci#define DEV_HAS_STATISTICS_V123    0x0000e00  /* device supports hw statistics version 1, 2, and 3 */
808c2ecf20Sopenharmony_ci#define DEV_HAS_TEST_EXTENDED      0x0001000  /* device supports extended diagnostic test */
818c2ecf20Sopenharmony_ci#define DEV_HAS_MGMT_UNIT          0x0002000  /* device supports management unit */
828c2ecf20Sopenharmony_ci#define DEV_HAS_CORRECT_MACADDR    0x0004000  /* device supports correct mac address order */
838c2ecf20Sopenharmony_ci#define DEV_HAS_COLLISION_FIX      0x0008000  /* device supports tx collision fix */
848c2ecf20Sopenharmony_ci#define DEV_HAS_PAUSEFRAME_TX_V1   0x0010000  /* device supports tx pause frames version 1 */
858c2ecf20Sopenharmony_ci#define DEV_HAS_PAUSEFRAME_TX_V2   0x0020000  /* device supports tx pause frames version 2 */
868c2ecf20Sopenharmony_ci#define DEV_HAS_PAUSEFRAME_TX_V3   0x0040000  /* device supports tx pause frames version 3 */
878c2ecf20Sopenharmony_ci#define DEV_NEED_TX_LIMIT          0x0080000  /* device needs to limit tx */
888c2ecf20Sopenharmony_ci#define DEV_NEED_TX_LIMIT2         0x0180000  /* device needs to limit tx, expect for some revs */
898c2ecf20Sopenharmony_ci#define DEV_HAS_GEAR_MODE          0x0200000  /* device supports gear mode */
908c2ecf20Sopenharmony_ci#define DEV_NEED_PHY_INIT_FIX      0x0400000  /* device needs specific phy workaround */
918c2ecf20Sopenharmony_ci#define DEV_NEED_LOW_POWER_FIX     0x0800000  /* device needs special power up workaround */
928c2ecf20Sopenharmony_ci#define DEV_NEED_MSI_FIX           0x1000000  /* device needs msi workaround */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cienum {
958c2ecf20Sopenharmony_ci	NvRegIrqStatus = 0x000,
968c2ecf20Sopenharmony_ci#define NVREG_IRQSTAT_MIIEVENT	0x040
978c2ecf20Sopenharmony_ci#define NVREG_IRQSTAT_MASK		0x83ff
988c2ecf20Sopenharmony_ci	NvRegIrqMask = 0x004,
998c2ecf20Sopenharmony_ci#define NVREG_IRQ_RX_ERROR		0x0001
1008c2ecf20Sopenharmony_ci#define NVREG_IRQ_RX			0x0002
1018c2ecf20Sopenharmony_ci#define NVREG_IRQ_RX_NOBUF		0x0004
1028c2ecf20Sopenharmony_ci#define NVREG_IRQ_TX_ERR		0x0008
1038c2ecf20Sopenharmony_ci#define NVREG_IRQ_TX_OK			0x0010
1048c2ecf20Sopenharmony_ci#define NVREG_IRQ_TIMER			0x0020
1058c2ecf20Sopenharmony_ci#define NVREG_IRQ_LINK			0x0040
1068c2ecf20Sopenharmony_ci#define NVREG_IRQ_RX_FORCED		0x0080
1078c2ecf20Sopenharmony_ci#define NVREG_IRQ_TX_FORCED		0x0100
1088c2ecf20Sopenharmony_ci#define NVREG_IRQ_RECOVER_ERROR		0x8200
1098c2ecf20Sopenharmony_ci#define NVREG_IRQMASK_THROUGHPUT	0x00df
1108c2ecf20Sopenharmony_ci#define NVREG_IRQMASK_CPU		0x0060
1118c2ecf20Sopenharmony_ci#define NVREG_IRQ_TX_ALL		(NVREG_IRQ_TX_ERR|NVREG_IRQ_TX_OK|NVREG_IRQ_TX_FORCED)
1128c2ecf20Sopenharmony_ci#define NVREG_IRQ_RX_ALL		(NVREG_IRQ_RX_ERROR|NVREG_IRQ_RX|NVREG_IRQ_RX_NOBUF|NVREG_IRQ_RX_FORCED)
1138c2ecf20Sopenharmony_ci#define NVREG_IRQ_OTHER			(NVREG_IRQ_TIMER|NVREG_IRQ_LINK|NVREG_IRQ_RECOVER_ERROR)
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	NvRegUnknownSetupReg6 = 0x008,
1168c2ecf20Sopenharmony_ci#define NVREG_UNKSETUP6_VAL		3
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * NVREG_POLL_DEFAULT is the interval length of the timer source on the nic
1208c2ecf20Sopenharmony_ci * NVREG_POLL_DEFAULT=97 would result in an interval length of 1 ms
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ci	NvRegPollingInterval = 0x00c,
1238c2ecf20Sopenharmony_ci#define NVREG_POLL_DEFAULT_THROUGHPUT	65535 /* backup tx cleanup if loop max reached */
1248c2ecf20Sopenharmony_ci#define NVREG_POLL_DEFAULT_CPU	13
1258c2ecf20Sopenharmony_ci	NvRegMSIMap0 = 0x020,
1268c2ecf20Sopenharmony_ci	NvRegMSIMap1 = 0x024,
1278c2ecf20Sopenharmony_ci	NvRegMSIIrqMask = 0x030,
1288c2ecf20Sopenharmony_ci#define NVREG_MSI_VECTOR_0_ENABLED 0x01
1298c2ecf20Sopenharmony_ci	NvRegMisc1 = 0x080,
1308c2ecf20Sopenharmony_ci#define NVREG_MISC1_PAUSE_TX	0x01
1318c2ecf20Sopenharmony_ci#define NVREG_MISC1_HD		0x02
1328c2ecf20Sopenharmony_ci#define NVREG_MISC1_FORCE	0x3b0f3c
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	NvRegMacReset = 0x34,
1358c2ecf20Sopenharmony_ci#define NVREG_MAC_RESET_ASSERT	0x0F3
1368c2ecf20Sopenharmony_ci	NvRegTransmitterControl = 0x084,
1378c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_START	0x01
1388c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_MGMT_ST	0x40000000
1398c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_SYNC_MASK		0x000f0000
1408c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_SYNC_NOT_READY	0x0
1418c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_SYNC_PHY_INIT	0x00040000
1428c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_MGMT_SEMA_MASK	0x00000f00
1438c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_MGMT_SEMA_FREE	0x0
1448c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_HOST_SEMA_MASK	0x0000f000
1458c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_HOST_SEMA_ACQ	0x0000f000
1468c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_HOST_LOADED	0x00004000
1478c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_TX_PATH_EN	0x01000000
1488c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_DATA_START	0x00100000
1498c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_DATA_READY	0x00010000
1508c2ecf20Sopenharmony_ci#define NVREG_XMITCTL_DATA_ERROR	0x00020000
1518c2ecf20Sopenharmony_ci	NvRegTransmitterStatus = 0x088,
1528c2ecf20Sopenharmony_ci#define NVREG_XMITSTAT_BUSY	0x01
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	NvRegPacketFilterFlags = 0x8c,
1558c2ecf20Sopenharmony_ci#define NVREG_PFF_PAUSE_RX	0x08
1568c2ecf20Sopenharmony_ci#define NVREG_PFF_ALWAYS	0x7F0000
1578c2ecf20Sopenharmony_ci#define NVREG_PFF_PROMISC	0x80
1588c2ecf20Sopenharmony_ci#define NVREG_PFF_MYADDR	0x20
1598c2ecf20Sopenharmony_ci#define NVREG_PFF_LOOPBACK	0x10
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	NvRegOffloadConfig = 0x90,
1628c2ecf20Sopenharmony_ci#define NVREG_OFFLOAD_HOMEPHY	0x601
1638c2ecf20Sopenharmony_ci#define NVREG_OFFLOAD_NORMAL	RX_NIC_BUFSIZE
1648c2ecf20Sopenharmony_ci	NvRegReceiverControl = 0x094,
1658c2ecf20Sopenharmony_ci#define NVREG_RCVCTL_START	0x01
1668c2ecf20Sopenharmony_ci#define NVREG_RCVCTL_RX_PATH_EN	0x01000000
1678c2ecf20Sopenharmony_ci	NvRegReceiverStatus = 0x98,
1688c2ecf20Sopenharmony_ci#define NVREG_RCVSTAT_BUSY	0x01
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	NvRegSlotTime = 0x9c,
1718c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_LEGBF_ENABLED	0x80000000
1728c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_10_100_FULL	0x00007f00
1738c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_1000_FULL	0x0003ff00
1748c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_HALF		0x0000ff00
1758c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_DEFAULT		0x00007f00
1768c2ecf20Sopenharmony_ci#define NVREG_SLOTTIME_MASK		0x000000ff
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	NvRegTxDeferral = 0xA0,
1798c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_DEFAULT		0x15050f
1808c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_RGMII_10_100		0x16070f
1818c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_RGMII_1000		0x14050f
1828c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_RGMII_STRETCH_10	0x16190f
1838c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_RGMII_STRETCH_100	0x16300f
1848c2ecf20Sopenharmony_ci#define NVREG_TX_DEFERRAL_MII_STRETCH		0x152000
1858c2ecf20Sopenharmony_ci	NvRegRxDeferral = 0xA4,
1868c2ecf20Sopenharmony_ci#define NVREG_RX_DEFERRAL_DEFAULT	0x16
1878c2ecf20Sopenharmony_ci	NvRegMacAddrA = 0xA8,
1888c2ecf20Sopenharmony_ci	NvRegMacAddrB = 0xAC,
1898c2ecf20Sopenharmony_ci	NvRegMulticastAddrA = 0xB0,
1908c2ecf20Sopenharmony_ci#define NVREG_MCASTADDRA_FORCE	0x01
1918c2ecf20Sopenharmony_ci	NvRegMulticastAddrB = 0xB4,
1928c2ecf20Sopenharmony_ci	NvRegMulticastMaskA = 0xB8,
1938c2ecf20Sopenharmony_ci#define NVREG_MCASTMASKA_NONE		0xffffffff
1948c2ecf20Sopenharmony_ci	NvRegMulticastMaskB = 0xBC,
1958c2ecf20Sopenharmony_ci#define NVREG_MCASTMASKB_NONE		0xffff
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	NvRegPhyInterface = 0xC0,
1988c2ecf20Sopenharmony_ci#define PHY_RGMII		0x10000000
1998c2ecf20Sopenharmony_ci	NvRegBackOffControl = 0xC4,
2008c2ecf20Sopenharmony_ci#define NVREG_BKOFFCTRL_DEFAULT			0x70000000
2018c2ecf20Sopenharmony_ci#define NVREG_BKOFFCTRL_SEED_MASK		0x000003ff
2028c2ecf20Sopenharmony_ci#define NVREG_BKOFFCTRL_SELECT			24
2038c2ecf20Sopenharmony_ci#define NVREG_BKOFFCTRL_GEAR			12
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	NvRegTxRingPhysAddr = 0x100,
2068c2ecf20Sopenharmony_ci	NvRegRxRingPhysAddr = 0x104,
2078c2ecf20Sopenharmony_ci	NvRegRingSizes = 0x108,
2088c2ecf20Sopenharmony_ci#define NVREG_RINGSZ_TXSHIFT 0
2098c2ecf20Sopenharmony_ci#define NVREG_RINGSZ_RXSHIFT 16
2108c2ecf20Sopenharmony_ci	NvRegTransmitPoll = 0x10c,
2118c2ecf20Sopenharmony_ci#define NVREG_TRANSMITPOLL_MAC_ADDR_REV	0x00008000
2128c2ecf20Sopenharmony_ci	NvRegLinkSpeed = 0x110,
2138c2ecf20Sopenharmony_ci#define NVREG_LINKSPEED_FORCE 0x10000
2148c2ecf20Sopenharmony_ci#define NVREG_LINKSPEED_10	1000
2158c2ecf20Sopenharmony_ci#define NVREG_LINKSPEED_100	100
2168c2ecf20Sopenharmony_ci#define NVREG_LINKSPEED_1000	50
2178c2ecf20Sopenharmony_ci#define NVREG_LINKSPEED_MASK	(0xFFF)
2188c2ecf20Sopenharmony_ci	NvRegUnknownSetupReg5 = 0x130,
2198c2ecf20Sopenharmony_ci#define NVREG_UNKSETUP5_BIT31	(1<<31)
2208c2ecf20Sopenharmony_ci	NvRegTxWatermark = 0x13c,
2218c2ecf20Sopenharmony_ci#define NVREG_TX_WM_DESC1_DEFAULT	0x0200010
2228c2ecf20Sopenharmony_ci#define NVREG_TX_WM_DESC2_3_DEFAULT	0x1e08000
2238c2ecf20Sopenharmony_ci#define NVREG_TX_WM_DESC2_3_1000	0xfe08000
2248c2ecf20Sopenharmony_ci	NvRegTxRxControl = 0x144,
2258c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_KICK	0x0001
2268c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_BIT1	0x0002
2278c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_BIT2	0x0004
2288c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_IDLE	0x0008
2298c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_RESET	0x0010
2308c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_RXCHECK	0x0400
2318c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_DESC_1	0
2328c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_DESC_2	0x002100
2338c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_DESC_3	0xc02200
2348c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_VLANSTRIP 0x00040
2358c2ecf20Sopenharmony_ci#define NVREG_TXRXCTL_VLANINS	0x00080
2368c2ecf20Sopenharmony_ci	NvRegTxRingPhysAddrHigh = 0x148,
2378c2ecf20Sopenharmony_ci	NvRegRxRingPhysAddrHigh = 0x14C,
2388c2ecf20Sopenharmony_ci	NvRegTxPauseFrame = 0x170,
2398c2ecf20Sopenharmony_ci#define NVREG_TX_PAUSEFRAME_DISABLE	0x0fff0080
2408c2ecf20Sopenharmony_ci#define NVREG_TX_PAUSEFRAME_ENABLE_V1	0x01800010
2418c2ecf20Sopenharmony_ci#define NVREG_TX_PAUSEFRAME_ENABLE_V2	0x056003f0
2428c2ecf20Sopenharmony_ci#define NVREG_TX_PAUSEFRAME_ENABLE_V3	0x09f00880
2438c2ecf20Sopenharmony_ci	NvRegTxPauseFrameLimit = 0x174,
2448c2ecf20Sopenharmony_ci#define NVREG_TX_PAUSEFRAMELIMIT_ENABLE	0x00010000
2458c2ecf20Sopenharmony_ci	NvRegMIIStatus = 0x180,
2468c2ecf20Sopenharmony_ci#define NVREG_MIISTAT_ERROR		0x0001
2478c2ecf20Sopenharmony_ci#define NVREG_MIISTAT_LINKCHANGE	0x0008
2488c2ecf20Sopenharmony_ci#define NVREG_MIISTAT_MASK_RW		0x0007
2498c2ecf20Sopenharmony_ci#define NVREG_MIISTAT_MASK_ALL		0x000f
2508c2ecf20Sopenharmony_ci	NvRegMIIMask = 0x184,
2518c2ecf20Sopenharmony_ci#define NVREG_MII_LINKCHANGE		0x0008
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	NvRegAdapterControl = 0x188,
2548c2ecf20Sopenharmony_ci#define NVREG_ADAPTCTL_START	0x02
2558c2ecf20Sopenharmony_ci#define NVREG_ADAPTCTL_LINKUP	0x04
2568c2ecf20Sopenharmony_ci#define NVREG_ADAPTCTL_PHYVALID	0x40000
2578c2ecf20Sopenharmony_ci#define NVREG_ADAPTCTL_RUNNING	0x100000
2588c2ecf20Sopenharmony_ci#define NVREG_ADAPTCTL_PHYSHIFT	24
2598c2ecf20Sopenharmony_ci	NvRegMIISpeed = 0x18c,
2608c2ecf20Sopenharmony_ci#define NVREG_MIISPEED_BIT8	(1<<8)
2618c2ecf20Sopenharmony_ci#define NVREG_MIIDELAY	5
2628c2ecf20Sopenharmony_ci	NvRegMIIControl = 0x190,
2638c2ecf20Sopenharmony_ci#define NVREG_MIICTL_INUSE	0x08000
2648c2ecf20Sopenharmony_ci#define NVREG_MIICTL_WRITE	0x00400
2658c2ecf20Sopenharmony_ci#define NVREG_MIICTL_ADDRSHIFT	5
2668c2ecf20Sopenharmony_ci	NvRegMIIData = 0x194,
2678c2ecf20Sopenharmony_ci	NvRegTxUnicast = 0x1a0,
2688c2ecf20Sopenharmony_ci	NvRegTxMulticast = 0x1a4,
2698c2ecf20Sopenharmony_ci	NvRegTxBroadcast = 0x1a8,
2708c2ecf20Sopenharmony_ci	NvRegWakeUpFlags = 0x200,
2718c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_VAL		0x7770
2728c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_BUSYSHIFT	24
2738c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_ENABLESHIFT	16
2748c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_D3SHIFT	12
2758c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_D2SHIFT	8
2768c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_D1SHIFT	4
2778c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_D0SHIFT	0
2788c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_ACCEPT_MAGPAT		0x01
2798c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_ACCEPT_WAKEUPPAT	0x02
2808c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_ACCEPT_LINKCHANGE	0x04
2818c2ecf20Sopenharmony_ci#define NVREG_WAKEUPFLAGS_ENABLE	0x1111
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	NvRegMgmtUnitGetVersion = 0x204,
2848c2ecf20Sopenharmony_ci#define NVREG_MGMTUNITGETVERSION	0x01
2858c2ecf20Sopenharmony_ci	NvRegMgmtUnitVersion = 0x208,
2868c2ecf20Sopenharmony_ci#define NVREG_MGMTUNITVERSION		0x08
2878c2ecf20Sopenharmony_ci	NvRegPowerCap = 0x268,
2888c2ecf20Sopenharmony_ci#define NVREG_POWERCAP_D3SUPP	(1<<30)
2898c2ecf20Sopenharmony_ci#define NVREG_POWERCAP_D2SUPP	(1<<26)
2908c2ecf20Sopenharmony_ci#define NVREG_POWERCAP_D1SUPP	(1<<25)
2918c2ecf20Sopenharmony_ci	NvRegPowerState = 0x26c,
2928c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_POWEREDUP	0x8000
2938c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_VALID		0x0100
2948c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_MASK		0x0003
2958c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_D0		0x0000
2968c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_D1		0x0001
2978c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_D2		0x0002
2988c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE_D3		0x0003
2998c2ecf20Sopenharmony_ci	NvRegMgmtUnitControl = 0x278,
3008c2ecf20Sopenharmony_ci#define NVREG_MGMTUNITCONTROL_INUSE	0x20000
3018c2ecf20Sopenharmony_ci	NvRegTxCnt = 0x280,
3028c2ecf20Sopenharmony_ci	NvRegTxZeroReXmt = 0x284,
3038c2ecf20Sopenharmony_ci	NvRegTxOneReXmt = 0x288,
3048c2ecf20Sopenharmony_ci	NvRegTxManyReXmt = 0x28c,
3058c2ecf20Sopenharmony_ci	NvRegTxLateCol = 0x290,
3068c2ecf20Sopenharmony_ci	NvRegTxUnderflow = 0x294,
3078c2ecf20Sopenharmony_ci	NvRegTxLossCarrier = 0x298,
3088c2ecf20Sopenharmony_ci	NvRegTxExcessDef = 0x29c,
3098c2ecf20Sopenharmony_ci	NvRegTxRetryErr = 0x2a0,
3108c2ecf20Sopenharmony_ci	NvRegRxFrameErr = 0x2a4,
3118c2ecf20Sopenharmony_ci	NvRegRxExtraByte = 0x2a8,
3128c2ecf20Sopenharmony_ci	NvRegRxLateCol = 0x2ac,
3138c2ecf20Sopenharmony_ci	NvRegRxRunt = 0x2b0,
3148c2ecf20Sopenharmony_ci	NvRegRxFrameTooLong = 0x2b4,
3158c2ecf20Sopenharmony_ci	NvRegRxOverflow = 0x2b8,
3168c2ecf20Sopenharmony_ci	NvRegRxFCSErr = 0x2bc,
3178c2ecf20Sopenharmony_ci	NvRegRxFrameAlignErr = 0x2c0,
3188c2ecf20Sopenharmony_ci	NvRegRxLenErr = 0x2c4,
3198c2ecf20Sopenharmony_ci	NvRegRxUnicast = 0x2c8,
3208c2ecf20Sopenharmony_ci	NvRegRxMulticast = 0x2cc,
3218c2ecf20Sopenharmony_ci	NvRegRxBroadcast = 0x2d0,
3228c2ecf20Sopenharmony_ci	NvRegTxDef = 0x2d4,
3238c2ecf20Sopenharmony_ci	NvRegTxFrame = 0x2d8,
3248c2ecf20Sopenharmony_ci	NvRegRxCnt = 0x2dc,
3258c2ecf20Sopenharmony_ci	NvRegTxPause = 0x2e0,
3268c2ecf20Sopenharmony_ci	NvRegRxPause = 0x2e4,
3278c2ecf20Sopenharmony_ci	NvRegRxDropFrame = 0x2e8,
3288c2ecf20Sopenharmony_ci	NvRegVlanControl = 0x300,
3298c2ecf20Sopenharmony_ci#define NVREG_VLANCONTROL_ENABLE	0x2000
3308c2ecf20Sopenharmony_ci	NvRegMSIXMap0 = 0x3e0,
3318c2ecf20Sopenharmony_ci	NvRegMSIXMap1 = 0x3e4,
3328c2ecf20Sopenharmony_ci	NvRegMSIXIrqStatus = 0x3f0,
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	NvRegPowerState2 = 0x600,
3358c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE2_POWERUP_MASK		0x0F15
3368c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE2_POWERUP_REV_A3	0x0001
3378c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE2_PHY_RESET		0x0004
3388c2ecf20Sopenharmony_ci#define NVREG_POWERSTATE2_GATE_CLOCKS		0x0F00
3398c2ecf20Sopenharmony_ci};
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/* Big endian: should work, but is untested */
3428c2ecf20Sopenharmony_cistruct ring_desc {
3438c2ecf20Sopenharmony_ci	__le32 buf;
3448c2ecf20Sopenharmony_ci	__le32 flaglen;
3458c2ecf20Sopenharmony_ci};
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistruct ring_desc_ex {
3488c2ecf20Sopenharmony_ci	__le32 bufhigh;
3498c2ecf20Sopenharmony_ci	__le32 buflow;
3508c2ecf20Sopenharmony_ci	__le32 txvlan;
3518c2ecf20Sopenharmony_ci	__le32 flaglen;
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ciunion ring_type {
3558c2ecf20Sopenharmony_ci	struct ring_desc *orig;
3568c2ecf20Sopenharmony_ci	struct ring_desc_ex *ex;
3578c2ecf20Sopenharmony_ci};
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci#define FLAG_MASK_V1 0xffff0000
3608c2ecf20Sopenharmony_ci#define FLAG_MASK_V2 0xffffc000
3618c2ecf20Sopenharmony_ci#define LEN_MASK_V1 (0xffffffff ^ FLAG_MASK_V1)
3628c2ecf20Sopenharmony_ci#define LEN_MASK_V2 (0xffffffff ^ FLAG_MASK_V2)
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci#define NV_TX_LASTPACKET	(1<<16)
3658c2ecf20Sopenharmony_ci#define NV_TX_RETRYERROR	(1<<19)
3668c2ecf20Sopenharmony_ci#define NV_TX_RETRYCOUNT_MASK	(0xF<<20)
3678c2ecf20Sopenharmony_ci#define NV_TX_FORCED_INTERRUPT	(1<<24)
3688c2ecf20Sopenharmony_ci#define NV_TX_DEFERRED		(1<<26)
3698c2ecf20Sopenharmony_ci#define NV_TX_CARRIERLOST	(1<<27)
3708c2ecf20Sopenharmony_ci#define NV_TX_LATECOLLISION	(1<<28)
3718c2ecf20Sopenharmony_ci#define NV_TX_UNDERFLOW		(1<<29)
3728c2ecf20Sopenharmony_ci#define NV_TX_ERROR		(1<<30)
3738c2ecf20Sopenharmony_ci#define NV_TX_VALID		(1<<31)
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci#define NV_TX2_LASTPACKET	(1<<29)
3768c2ecf20Sopenharmony_ci#define NV_TX2_RETRYERROR	(1<<18)
3778c2ecf20Sopenharmony_ci#define NV_TX2_RETRYCOUNT_MASK	(0xF<<19)
3788c2ecf20Sopenharmony_ci#define NV_TX2_FORCED_INTERRUPT	(1<<30)
3798c2ecf20Sopenharmony_ci#define NV_TX2_DEFERRED		(1<<25)
3808c2ecf20Sopenharmony_ci#define NV_TX2_CARRIERLOST	(1<<26)
3818c2ecf20Sopenharmony_ci#define NV_TX2_LATECOLLISION	(1<<27)
3828c2ecf20Sopenharmony_ci#define NV_TX2_UNDERFLOW	(1<<28)
3838c2ecf20Sopenharmony_ci/* error and valid are the same for both */
3848c2ecf20Sopenharmony_ci#define NV_TX2_ERROR		(1<<30)
3858c2ecf20Sopenharmony_ci#define NV_TX2_VALID		(1<<31)
3868c2ecf20Sopenharmony_ci#define NV_TX2_TSO		(1<<28)
3878c2ecf20Sopenharmony_ci#define NV_TX2_TSO_SHIFT	14
3888c2ecf20Sopenharmony_ci#define NV_TX2_TSO_MAX_SHIFT	14
3898c2ecf20Sopenharmony_ci#define NV_TX2_TSO_MAX_SIZE	(1<<NV_TX2_TSO_MAX_SHIFT)
3908c2ecf20Sopenharmony_ci#define NV_TX2_CHECKSUM_L3	(1<<27)
3918c2ecf20Sopenharmony_ci#define NV_TX2_CHECKSUM_L4	(1<<26)
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci#define NV_TX3_VLAN_TAG_PRESENT (1<<18)
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci#define NV_RX_DESCRIPTORVALID	(1<<16)
3968c2ecf20Sopenharmony_ci#define NV_RX_MISSEDFRAME	(1<<17)
3978c2ecf20Sopenharmony_ci#define NV_RX_SUBTRACT1		(1<<18)
3988c2ecf20Sopenharmony_ci#define NV_RX_ERROR1		(1<<23)
3998c2ecf20Sopenharmony_ci#define NV_RX_ERROR2		(1<<24)
4008c2ecf20Sopenharmony_ci#define NV_RX_ERROR3		(1<<25)
4018c2ecf20Sopenharmony_ci#define NV_RX_ERROR4		(1<<26)
4028c2ecf20Sopenharmony_ci#define NV_RX_CRCERR		(1<<27)
4038c2ecf20Sopenharmony_ci#define NV_RX_OVERFLOW		(1<<28)
4048c2ecf20Sopenharmony_ci#define NV_RX_FRAMINGERR	(1<<29)
4058c2ecf20Sopenharmony_ci#define NV_RX_ERROR		(1<<30)
4068c2ecf20Sopenharmony_ci#define NV_RX_AVAIL		(1<<31)
4078c2ecf20Sopenharmony_ci#define NV_RX_ERROR_MASK	(NV_RX_ERROR1|NV_RX_ERROR2|NV_RX_ERROR3|NV_RX_ERROR4|NV_RX_CRCERR|NV_RX_OVERFLOW|NV_RX_FRAMINGERR)
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci#define NV_RX2_CHECKSUMMASK	(0x1C000000)
4108c2ecf20Sopenharmony_ci#define NV_RX2_CHECKSUM_IP	(0x10000000)
4118c2ecf20Sopenharmony_ci#define NV_RX2_CHECKSUM_IP_TCP	(0x14000000)
4128c2ecf20Sopenharmony_ci#define NV_RX2_CHECKSUM_IP_UDP	(0x18000000)
4138c2ecf20Sopenharmony_ci#define NV_RX2_DESCRIPTORVALID	(1<<29)
4148c2ecf20Sopenharmony_ci#define NV_RX2_SUBTRACT1	(1<<25)
4158c2ecf20Sopenharmony_ci#define NV_RX2_ERROR1		(1<<18)
4168c2ecf20Sopenharmony_ci#define NV_RX2_ERROR2		(1<<19)
4178c2ecf20Sopenharmony_ci#define NV_RX2_ERROR3		(1<<20)
4188c2ecf20Sopenharmony_ci#define NV_RX2_ERROR4		(1<<21)
4198c2ecf20Sopenharmony_ci#define NV_RX2_CRCERR		(1<<22)
4208c2ecf20Sopenharmony_ci#define NV_RX2_OVERFLOW		(1<<23)
4218c2ecf20Sopenharmony_ci#define NV_RX2_FRAMINGERR	(1<<24)
4228c2ecf20Sopenharmony_ci/* error and avail are the same for both */
4238c2ecf20Sopenharmony_ci#define NV_RX2_ERROR		(1<<30)
4248c2ecf20Sopenharmony_ci#define NV_RX2_AVAIL		(1<<31)
4258c2ecf20Sopenharmony_ci#define NV_RX2_ERROR_MASK	(NV_RX2_ERROR1|NV_RX2_ERROR2|NV_RX2_ERROR3|NV_RX2_ERROR4|NV_RX2_CRCERR|NV_RX2_OVERFLOW|NV_RX2_FRAMINGERR)
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci#define NV_RX3_VLAN_TAG_PRESENT (1<<16)
4288c2ecf20Sopenharmony_ci#define NV_RX3_VLAN_TAG_MASK	(0x0000FFFF)
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci/* Miscellaneous hardware related defines: */
4318c2ecf20Sopenharmony_ci#define NV_PCI_REGSZ_VER1	0x270
4328c2ecf20Sopenharmony_ci#define NV_PCI_REGSZ_VER2	0x2d4
4338c2ecf20Sopenharmony_ci#define NV_PCI_REGSZ_VER3	0x604
4348c2ecf20Sopenharmony_ci#define NV_PCI_REGSZ_MAX	0x604
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/* various timeout delays: all in usec */
4378c2ecf20Sopenharmony_ci#define NV_TXRX_RESET_DELAY	4
4388c2ecf20Sopenharmony_ci#define NV_TXSTOP_DELAY1	10
4398c2ecf20Sopenharmony_ci#define NV_TXSTOP_DELAY1MAX	500000
4408c2ecf20Sopenharmony_ci#define NV_TXSTOP_DELAY2	100
4418c2ecf20Sopenharmony_ci#define NV_RXSTOP_DELAY1	10
4428c2ecf20Sopenharmony_ci#define NV_RXSTOP_DELAY1MAX	500000
4438c2ecf20Sopenharmony_ci#define NV_RXSTOP_DELAY2	100
4448c2ecf20Sopenharmony_ci#define NV_SETUP5_DELAY		5
4458c2ecf20Sopenharmony_ci#define NV_SETUP5_DELAYMAX	50000
4468c2ecf20Sopenharmony_ci#define NV_POWERUP_DELAY	5
4478c2ecf20Sopenharmony_ci#define NV_POWERUP_DELAYMAX	5000
4488c2ecf20Sopenharmony_ci#define NV_MIIBUSY_DELAY	50
4498c2ecf20Sopenharmony_ci#define NV_MIIPHY_DELAY	10
4508c2ecf20Sopenharmony_ci#define NV_MIIPHY_DELAYMAX	10000
4518c2ecf20Sopenharmony_ci#define NV_MAC_RESET_DELAY	64
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci#define NV_WAKEUPPATTERNS	5
4548c2ecf20Sopenharmony_ci#define NV_WAKEUPMASKENTRIES	4
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci/* General driver defaults */
4578c2ecf20Sopenharmony_ci#define NV_WATCHDOG_TIMEO	(5*HZ)
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci#define RX_RING_DEFAULT		512
4608c2ecf20Sopenharmony_ci#define TX_RING_DEFAULT		256
4618c2ecf20Sopenharmony_ci#define RX_RING_MIN		128
4628c2ecf20Sopenharmony_ci#define TX_RING_MIN		64
4638c2ecf20Sopenharmony_ci#define RING_MAX_DESC_VER_1	1024
4648c2ecf20Sopenharmony_ci#define RING_MAX_DESC_VER_2_3	16384
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/* rx/tx mac addr + type + vlan + align + slack*/
4678c2ecf20Sopenharmony_ci#define NV_RX_HEADERS		(64)
4688c2ecf20Sopenharmony_ci/* even more slack. */
4698c2ecf20Sopenharmony_ci#define NV_RX_ALLOC_PAD		(64)
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/* maximum mtu size */
4728c2ecf20Sopenharmony_ci#define NV_PKTLIMIT_1	ETH_DATA_LEN	/* hard limit not known */
4738c2ecf20Sopenharmony_ci#define NV_PKTLIMIT_2	9100	/* Actual limit according to NVidia: 9202 */
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci#define OOM_REFILL	(1+HZ/20)
4768c2ecf20Sopenharmony_ci#define POLL_WAIT	(1+HZ/100)
4778c2ecf20Sopenharmony_ci#define LINK_TIMEOUT	(3*HZ)
4788c2ecf20Sopenharmony_ci#define STATS_INTERVAL	(10*HZ)
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci/*
4818c2ecf20Sopenharmony_ci * desc_ver values:
4828c2ecf20Sopenharmony_ci * The nic supports three different descriptor types:
4838c2ecf20Sopenharmony_ci * - DESC_VER_1: Original
4848c2ecf20Sopenharmony_ci * - DESC_VER_2: support for jumbo frames.
4858c2ecf20Sopenharmony_ci * - DESC_VER_3: 64-bit format.
4868c2ecf20Sopenharmony_ci */
4878c2ecf20Sopenharmony_ci#define DESC_VER_1	1
4888c2ecf20Sopenharmony_ci#define DESC_VER_2	2
4898c2ecf20Sopenharmony_ci#define DESC_VER_3	3
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci/* PHY defines */
4928c2ecf20Sopenharmony_ci#define PHY_OUI_MARVELL		0x5043
4938c2ecf20Sopenharmony_ci#define PHY_OUI_CICADA		0x03f1
4948c2ecf20Sopenharmony_ci#define PHY_OUI_VITESSE		0x01c1
4958c2ecf20Sopenharmony_ci#define PHY_OUI_REALTEK		0x0732
4968c2ecf20Sopenharmony_ci#define PHY_OUI_REALTEK2	0x0020
4978c2ecf20Sopenharmony_ci#define PHYID1_OUI_MASK	0x03ff
4988c2ecf20Sopenharmony_ci#define PHYID1_OUI_SHFT	6
4998c2ecf20Sopenharmony_ci#define PHYID2_OUI_MASK	0xfc00
5008c2ecf20Sopenharmony_ci#define PHYID2_OUI_SHFT	10
5018c2ecf20Sopenharmony_ci#define PHYID2_MODEL_MASK		0x03f0
5028c2ecf20Sopenharmony_ci#define PHY_MODEL_REALTEK_8211		0x0110
5038c2ecf20Sopenharmony_ci#define PHY_REV_MASK			0x0001
5048c2ecf20Sopenharmony_ci#define PHY_REV_REALTEK_8211B		0x0000
5058c2ecf20Sopenharmony_ci#define PHY_REV_REALTEK_8211C		0x0001
5068c2ecf20Sopenharmony_ci#define PHY_MODEL_REALTEK_8201		0x0200
5078c2ecf20Sopenharmony_ci#define PHY_MODEL_MARVELL_E3016		0x0220
5088c2ecf20Sopenharmony_ci#define PHY_MARVELL_E3016_INITMASK	0x0300
5098c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT1	0x0f000
5108c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT2	0x0e00
5118c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT3	0x01000
5128c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT4	0x0200
5138c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT5	0x0004
5148c2ecf20Sopenharmony_ci#define PHY_CICADA_INIT6	0x02000
5158c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_REG1	0x1f
5168c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_REG2	0x10
5178c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_REG3	0x11
5188c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_REG4	0x12
5198c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_MSK1	0xc
5208c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT_MSK2	0x0180
5218c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT1	0x52b5
5228c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT2	0xaf8a
5238c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT3	0x8
5248c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT4	0x8f8a
5258c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT5	0xaf86
5268c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT6	0x8f86
5278c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT7	0xaf82
5288c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT8	0x0100
5298c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT9	0x8f82
5308c2ecf20Sopenharmony_ci#define PHY_VITESSE_INIT10	0x0
5318c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG1	0x1f
5328c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG2	0x19
5338c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG3	0x13
5348c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG4	0x14
5358c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG5	0x18
5368c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG6	0x11
5378c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_REG7	0x01
5388c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT1	0x0000
5398c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT2	0x8e00
5408c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT3	0x0001
5418c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT4	0xad17
5428c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT5	0xfb54
5438c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT6	0xf5c7
5448c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT7	0x1000
5458c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT8	0x0003
5468c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT9	0x0008
5478c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT10	0x0005
5488c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT11	0x0200
5498c2ecf20Sopenharmony_ci#define PHY_REALTEK_INIT_MSK1	0x0003
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci#define PHY_GIGABIT	0x0100
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci#define PHY_TIMEOUT	0x1
5548c2ecf20Sopenharmony_ci#define PHY_ERROR	0x2
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci#define PHY_100	0x1
5578c2ecf20Sopenharmony_ci#define PHY_1000	0x2
5588c2ecf20Sopenharmony_ci#define PHY_HALF	0x100
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_RX_CAPABLE 0x0001
5618c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_TX_CAPABLE 0x0002
5628c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_RX_ENABLE  0x0004
5638c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_TX_ENABLE  0x0008
5648c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_RX_REQ     0x0010
5658c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_TX_REQ     0x0020
5668c2ecf20Sopenharmony_ci#define NV_PAUSEFRAME_AUTONEG    0x0040
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/* MSI/MSI-X defines */
5698c2ecf20Sopenharmony_ci#define NV_MSI_X_MAX_VECTORS  8
5708c2ecf20Sopenharmony_ci#define NV_MSI_X_VECTORS_MASK 0x000f
5718c2ecf20Sopenharmony_ci#define NV_MSI_CAPABLE        0x0010
5728c2ecf20Sopenharmony_ci#define NV_MSI_X_CAPABLE      0x0020
5738c2ecf20Sopenharmony_ci#define NV_MSI_ENABLED        0x0040
5748c2ecf20Sopenharmony_ci#define NV_MSI_X_ENABLED      0x0080
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci#define NV_MSI_X_VECTOR_ALL   0x0
5778c2ecf20Sopenharmony_ci#define NV_MSI_X_VECTOR_RX    0x0
5788c2ecf20Sopenharmony_ci#define NV_MSI_X_VECTOR_TX    0x1
5798c2ecf20Sopenharmony_ci#define NV_MSI_X_VECTOR_OTHER 0x2
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci#define NV_MSI_PRIV_OFFSET 0x68
5828c2ecf20Sopenharmony_ci#define NV_MSI_PRIV_VALUE  0xffffffff
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci#define NV_RESTART_TX         0x1
5858c2ecf20Sopenharmony_ci#define NV_RESTART_RX         0x2
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci#define NV_TX_LIMIT_COUNT     16
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci#define NV_DYNAMIC_THRESHOLD        4
5908c2ecf20Sopenharmony_ci#define NV_DYNAMIC_MAX_QUIET_COUNT  2048
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci/* statistics */
5938c2ecf20Sopenharmony_cistruct nv_ethtool_str {
5948c2ecf20Sopenharmony_ci	char name[ETH_GSTRING_LEN];
5958c2ecf20Sopenharmony_ci};
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic const struct nv_ethtool_str nv_estats_str[] = {
5988c2ecf20Sopenharmony_ci	{ "tx_bytes" }, /* includes Ethernet FCS CRC */
5998c2ecf20Sopenharmony_ci	{ "tx_zero_rexmt" },
6008c2ecf20Sopenharmony_ci	{ "tx_one_rexmt" },
6018c2ecf20Sopenharmony_ci	{ "tx_many_rexmt" },
6028c2ecf20Sopenharmony_ci	{ "tx_late_collision" },
6038c2ecf20Sopenharmony_ci	{ "tx_fifo_errors" },
6048c2ecf20Sopenharmony_ci	{ "tx_carrier_errors" },
6058c2ecf20Sopenharmony_ci	{ "tx_excess_deferral" },
6068c2ecf20Sopenharmony_ci	{ "tx_retry_error" },
6078c2ecf20Sopenharmony_ci	{ "rx_frame_error" },
6088c2ecf20Sopenharmony_ci	{ "rx_extra_byte" },
6098c2ecf20Sopenharmony_ci	{ "rx_late_collision" },
6108c2ecf20Sopenharmony_ci	{ "rx_runt" },
6118c2ecf20Sopenharmony_ci	{ "rx_frame_too_long" },
6128c2ecf20Sopenharmony_ci	{ "rx_over_errors" },
6138c2ecf20Sopenharmony_ci	{ "rx_crc_errors" },
6148c2ecf20Sopenharmony_ci	{ "rx_frame_align_error" },
6158c2ecf20Sopenharmony_ci	{ "rx_length_error" },
6168c2ecf20Sopenharmony_ci	{ "rx_unicast" },
6178c2ecf20Sopenharmony_ci	{ "rx_multicast" },
6188c2ecf20Sopenharmony_ci	{ "rx_broadcast" },
6198c2ecf20Sopenharmony_ci	{ "rx_packets" },
6208c2ecf20Sopenharmony_ci	{ "rx_errors_total" },
6218c2ecf20Sopenharmony_ci	{ "tx_errors_total" },
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* version 2 stats */
6248c2ecf20Sopenharmony_ci	{ "tx_deferral" },
6258c2ecf20Sopenharmony_ci	{ "tx_packets" },
6268c2ecf20Sopenharmony_ci	{ "rx_bytes" }, /* includes Ethernet FCS CRC */
6278c2ecf20Sopenharmony_ci	{ "tx_pause" },
6288c2ecf20Sopenharmony_ci	{ "rx_pause" },
6298c2ecf20Sopenharmony_ci	{ "rx_drop_frame" },
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	/* version 3 stats */
6328c2ecf20Sopenharmony_ci	{ "tx_unicast" },
6338c2ecf20Sopenharmony_ci	{ "tx_multicast" },
6348c2ecf20Sopenharmony_ci	{ "tx_broadcast" }
6358c2ecf20Sopenharmony_ci};
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistruct nv_ethtool_stats {
6388c2ecf20Sopenharmony_ci	u64 tx_bytes; /* should be ifconfig->tx_bytes + 4*tx_packets */
6398c2ecf20Sopenharmony_ci	u64 tx_zero_rexmt;
6408c2ecf20Sopenharmony_ci	u64 tx_one_rexmt;
6418c2ecf20Sopenharmony_ci	u64 tx_many_rexmt;
6428c2ecf20Sopenharmony_ci	u64 tx_late_collision;
6438c2ecf20Sopenharmony_ci	u64 tx_fifo_errors;
6448c2ecf20Sopenharmony_ci	u64 tx_carrier_errors;
6458c2ecf20Sopenharmony_ci	u64 tx_excess_deferral;
6468c2ecf20Sopenharmony_ci	u64 tx_retry_error;
6478c2ecf20Sopenharmony_ci	u64 rx_frame_error;
6488c2ecf20Sopenharmony_ci	u64 rx_extra_byte;
6498c2ecf20Sopenharmony_ci	u64 rx_late_collision;
6508c2ecf20Sopenharmony_ci	u64 rx_runt;
6518c2ecf20Sopenharmony_ci	u64 rx_frame_too_long;
6528c2ecf20Sopenharmony_ci	u64 rx_over_errors;
6538c2ecf20Sopenharmony_ci	u64 rx_crc_errors;
6548c2ecf20Sopenharmony_ci	u64 rx_frame_align_error;
6558c2ecf20Sopenharmony_ci	u64 rx_length_error;
6568c2ecf20Sopenharmony_ci	u64 rx_unicast;
6578c2ecf20Sopenharmony_ci	u64 rx_multicast;
6588c2ecf20Sopenharmony_ci	u64 rx_broadcast;
6598c2ecf20Sopenharmony_ci	u64 rx_packets; /* should be ifconfig->rx_packets */
6608c2ecf20Sopenharmony_ci	u64 rx_errors_total;
6618c2ecf20Sopenharmony_ci	u64 tx_errors_total;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/* version 2 stats */
6648c2ecf20Sopenharmony_ci	u64 tx_deferral;
6658c2ecf20Sopenharmony_ci	u64 tx_packets; /* should be ifconfig->tx_packets */
6668c2ecf20Sopenharmony_ci	u64 rx_bytes;   /* should be ifconfig->rx_bytes + 4*rx_packets */
6678c2ecf20Sopenharmony_ci	u64 tx_pause;
6688c2ecf20Sopenharmony_ci	u64 rx_pause;
6698c2ecf20Sopenharmony_ci	u64 rx_drop_frame;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	/* version 3 stats */
6728c2ecf20Sopenharmony_ci	u64 tx_unicast;
6738c2ecf20Sopenharmony_ci	u64 tx_multicast;
6748c2ecf20Sopenharmony_ci	u64 tx_broadcast;
6758c2ecf20Sopenharmony_ci};
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci#define NV_DEV_STATISTICS_V3_COUNT (sizeof(struct nv_ethtool_stats)/sizeof(u64))
6788c2ecf20Sopenharmony_ci#define NV_DEV_STATISTICS_V2_COUNT (NV_DEV_STATISTICS_V3_COUNT - 3)
6798c2ecf20Sopenharmony_ci#define NV_DEV_STATISTICS_V1_COUNT (NV_DEV_STATISTICS_V2_COUNT - 6)
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci/* diagnostics */
6828c2ecf20Sopenharmony_ci#define NV_TEST_COUNT_BASE 3
6838c2ecf20Sopenharmony_ci#define NV_TEST_COUNT_EXTENDED 4
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic const struct nv_ethtool_str nv_etests_str[] = {
6868c2ecf20Sopenharmony_ci	{ "link      (online/offline)" },
6878c2ecf20Sopenharmony_ci	{ "register  (offline)       " },
6888c2ecf20Sopenharmony_ci	{ "interrupt (offline)       " },
6898c2ecf20Sopenharmony_ci	{ "loopback  (offline)       " }
6908c2ecf20Sopenharmony_ci};
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistruct register_test {
6938c2ecf20Sopenharmony_ci	__u32 reg;
6948c2ecf20Sopenharmony_ci	__u32 mask;
6958c2ecf20Sopenharmony_ci};
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic const struct register_test nv_registers_test[] = {
6988c2ecf20Sopenharmony_ci	{ NvRegUnknownSetupReg6, 0x01 },
6998c2ecf20Sopenharmony_ci	{ NvRegMisc1, 0x03c },
7008c2ecf20Sopenharmony_ci	{ NvRegOffloadConfig, 0x03ff },
7018c2ecf20Sopenharmony_ci	{ NvRegMulticastAddrA, 0xffffffff },
7028c2ecf20Sopenharmony_ci	{ NvRegTxWatermark, 0x0ff },
7038c2ecf20Sopenharmony_ci	{ NvRegWakeUpFlags, 0x07777 },
7048c2ecf20Sopenharmony_ci	{ 0, 0 }
7058c2ecf20Sopenharmony_ci};
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_cistruct nv_skb_map {
7088c2ecf20Sopenharmony_ci	struct sk_buff *skb;
7098c2ecf20Sopenharmony_ci	dma_addr_t dma;
7108c2ecf20Sopenharmony_ci	unsigned int dma_len:31;
7118c2ecf20Sopenharmony_ci	unsigned int dma_single:1;
7128c2ecf20Sopenharmony_ci	struct ring_desc_ex *first_tx_desc;
7138c2ecf20Sopenharmony_ci	struct nv_skb_map *next_tx_ctx;
7148c2ecf20Sopenharmony_ci};
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_cistruct nv_txrx_stats {
7178c2ecf20Sopenharmony_ci	u64 stat_rx_packets;
7188c2ecf20Sopenharmony_ci	u64 stat_rx_bytes; /* not always available in HW */
7198c2ecf20Sopenharmony_ci	u64 stat_rx_missed_errors;
7208c2ecf20Sopenharmony_ci	u64 stat_rx_dropped;
7218c2ecf20Sopenharmony_ci	u64 stat_tx_packets; /* not always available in HW */
7228c2ecf20Sopenharmony_ci	u64 stat_tx_bytes;
7238c2ecf20Sopenharmony_ci	u64 stat_tx_dropped;
7248c2ecf20Sopenharmony_ci};
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci#define nv_txrx_stats_inc(member) \
7278c2ecf20Sopenharmony_ci		__this_cpu_inc(np->txrx_stats->member)
7288c2ecf20Sopenharmony_ci#define nv_txrx_stats_add(member, count) \
7298c2ecf20Sopenharmony_ci		__this_cpu_add(np->txrx_stats->member, (count))
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci/*
7328c2ecf20Sopenharmony_ci * SMP locking:
7338c2ecf20Sopenharmony_ci * All hardware access under netdev_priv(dev)->lock, except the performance
7348c2ecf20Sopenharmony_ci * critical parts:
7358c2ecf20Sopenharmony_ci * - rx is (pseudo-) lockless: it relies on the single-threading provided
7368c2ecf20Sopenharmony_ci *	by the arch code for interrupts.
7378c2ecf20Sopenharmony_ci * - tx setup is lockless: it relies on netif_tx_lock. Actual submission
7388c2ecf20Sopenharmony_ci *	needs netdev_priv(dev)->lock :-(
7398c2ecf20Sopenharmony_ci * - set_multicast_list: preparation lockless, relies on netif_tx_lock.
7408c2ecf20Sopenharmony_ci *
7418c2ecf20Sopenharmony_ci * Hardware stats updates are protected by hwstats_lock:
7428c2ecf20Sopenharmony_ci * - updated by nv_do_stats_poll (timer). This is meant to avoid
7438c2ecf20Sopenharmony_ci *   integer wraparound in the NIC stats registers, at low frequency
7448c2ecf20Sopenharmony_ci *   (0.1 Hz)
7458c2ecf20Sopenharmony_ci * - updated by nv_get_ethtool_stats + nv_get_stats64
7468c2ecf20Sopenharmony_ci *
7478c2ecf20Sopenharmony_ci * Software stats are accessed only through 64b synchronization points
7488c2ecf20Sopenharmony_ci * and are not subject to other synchronization techniques (single
7498c2ecf20Sopenharmony_ci * update thread on the TX or RX paths).
7508c2ecf20Sopenharmony_ci */
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci/* in dev: base, irq */
7538c2ecf20Sopenharmony_cistruct fe_priv {
7548c2ecf20Sopenharmony_ci	spinlock_t lock;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	struct net_device *dev;
7578c2ecf20Sopenharmony_ci	struct napi_struct napi;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	/* hardware stats are updated in syscall and timer */
7608c2ecf20Sopenharmony_ci	spinlock_t hwstats_lock;
7618c2ecf20Sopenharmony_ci	struct nv_ethtool_stats estats;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	int in_shutdown;
7648c2ecf20Sopenharmony_ci	u32 linkspeed;
7658c2ecf20Sopenharmony_ci	int duplex;
7668c2ecf20Sopenharmony_ci	int autoneg;
7678c2ecf20Sopenharmony_ci	int fixed_mode;
7688c2ecf20Sopenharmony_ci	int phyaddr;
7698c2ecf20Sopenharmony_ci	int wolenabled;
7708c2ecf20Sopenharmony_ci	unsigned int phy_oui;
7718c2ecf20Sopenharmony_ci	unsigned int phy_model;
7728c2ecf20Sopenharmony_ci	unsigned int phy_rev;
7738c2ecf20Sopenharmony_ci	u16 gigabit;
7748c2ecf20Sopenharmony_ci	int intr_test;
7758c2ecf20Sopenharmony_ci	int recover_error;
7768c2ecf20Sopenharmony_ci	int quiet_count;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	/* General data: RO fields */
7798c2ecf20Sopenharmony_ci	dma_addr_t ring_addr;
7808c2ecf20Sopenharmony_ci	struct pci_dev *pci_dev;
7818c2ecf20Sopenharmony_ci	u32 orig_mac[2];
7828c2ecf20Sopenharmony_ci	u32 events;
7838c2ecf20Sopenharmony_ci	u32 irqmask;
7848c2ecf20Sopenharmony_ci	u32 desc_ver;
7858c2ecf20Sopenharmony_ci	u32 txrxctl_bits;
7868c2ecf20Sopenharmony_ci	u32 vlanctl_bits;
7878c2ecf20Sopenharmony_ci	u32 driver_data;
7888c2ecf20Sopenharmony_ci	u32 device_id;
7898c2ecf20Sopenharmony_ci	u32 register_size;
7908c2ecf20Sopenharmony_ci	u32 mac_in_use;
7918c2ecf20Sopenharmony_ci	int mgmt_version;
7928c2ecf20Sopenharmony_ci	int mgmt_sema;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	void __iomem *base;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* rx specific fields.
7978c2ecf20Sopenharmony_ci	 * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
7988c2ecf20Sopenharmony_ci	 */
7998c2ecf20Sopenharmony_ci	union ring_type get_rx, put_rx, last_rx;
8008c2ecf20Sopenharmony_ci	struct nv_skb_map *get_rx_ctx, *put_rx_ctx;
8018c2ecf20Sopenharmony_ci	struct nv_skb_map *last_rx_ctx;
8028c2ecf20Sopenharmony_ci	struct nv_skb_map *rx_skb;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	union ring_type rx_ring;
8058c2ecf20Sopenharmony_ci	unsigned int rx_buf_sz;
8068c2ecf20Sopenharmony_ci	unsigned int pkt_limit;
8078c2ecf20Sopenharmony_ci	struct timer_list oom_kick;
8088c2ecf20Sopenharmony_ci	struct timer_list nic_poll;
8098c2ecf20Sopenharmony_ci	struct timer_list stats_poll;
8108c2ecf20Sopenharmony_ci	u32 nic_poll_irq;
8118c2ecf20Sopenharmony_ci	int rx_ring_size;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	/* RX software stats */
8148c2ecf20Sopenharmony_ci	struct u64_stats_sync swstats_rx_syncp;
8158c2ecf20Sopenharmony_ci	struct nv_txrx_stats __percpu *txrx_stats;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	/* media detection workaround.
8188c2ecf20Sopenharmony_ci	 * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
8198c2ecf20Sopenharmony_ci	 */
8208c2ecf20Sopenharmony_ci	int need_linktimer;
8218c2ecf20Sopenharmony_ci	unsigned long link_timeout;
8228c2ecf20Sopenharmony_ci	/*
8238c2ecf20Sopenharmony_ci	 * tx specific fields.
8248c2ecf20Sopenharmony_ci	 */
8258c2ecf20Sopenharmony_ci	union ring_type get_tx, put_tx, last_tx;
8268c2ecf20Sopenharmony_ci	struct nv_skb_map *get_tx_ctx, *put_tx_ctx;
8278c2ecf20Sopenharmony_ci	struct nv_skb_map *last_tx_ctx;
8288c2ecf20Sopenharmony_ci	struct nv_skb_map *tx_skb;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	union ring_type tx_ring;
8318c2ecf20Sopenharmony_ci	u32 tx_flags;
8328c2ecf20Sopenharmony_ci	int tx_ring_size;
8338c2ecf20Sopenharmony_ci	int tx_limit;
8348c2ecf20Sopenharmony_ci	u32 tx_pkts_in_progress;
8358c2ecf20Sopenharmony_ci	struct nv_skb_map *tx_change_owner;
8368c2ecf20Sopenharmony_ci	struct nv_skb_map *tx_end_flip;
8378c2ecf20Sopenharmony_ci	int tx_stop;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	/* TX software stats */
8408c2ecf20Sopenharmony_ci	struct u64_stats_sync swstats_tx_syncp;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	/* msi/msi-x fields */
8438c2ecf20Sopenharmony_ci	u32 msi_flags;
8448c2ecf20Sopenharmony_ci	struct msix_entry msi_x_entry[NV_MSI_X_MAX_VECTORS];
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	/* flow control */
8478c2ecf20Sopenharmony_ci	u32 pause_flags;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	/* power saved state */
8508c2ecf20Sopenharmony_ci	u32 saved_config_space[NV_PCI_REGSZ_MAX/4];
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	/* for different msi-x irq type */
8538c2ecf20Sopenharmony_ci	char name_rx[IFNAMSIZ + 3];       /* -rx    */
8548c2ecf20Sopenharmony_ci	char name_tx[IFNAMSIZ + 3];       /* -tx    */
8558c2ecf20Sopenharmony_ci	char name_other[IFNAMSIZ + 6];    /* -other */
8568c2ecf20Sopenharmony_ci};
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci/*
8598c2ecf20Sopenharmony_ci * Maximum number of loops until we assume that a bit in the irq mask
8608c2ecf20Sopenharmony_ci * is stuck. Overridable with module param.
8618c2ecf20Sopenharmony_ci */
8628c2ecf20Sopenharmony_cistatic int max_interrupt_work = 4;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci/*
8658c2ecf20Sopenharmony_ci * Optimization can be either throuput mode or cpu mode
8668c2ecf20Sopenharmony_ci *
8678c2ecf20Sopenharmony_ci * Throughput Mode: Every tx and rx packet will generate an interrupt.
8688c2ecf20Sopenharmony_ci * CPU Mode: Interrupts are controlled by a timer.
8698c2ecf20Sopenharmony_ci */
8708c2ecf20Sopenharmony_cienum {
8718c2ecf20Sopenharmony_ci	NV_OPTIMIZATION_MODE_THROUGHPUT,
8728c2ecf20Sopenharmony_ci	NV_OPTIMIZATION_MODE_CPU,
8738c2ecf20Sopenharmony_ci	NV_OPTIMIZATION_MODE_DYNAMIC
8748c2ecf20Sopenharmony_ci};
8758c2ecf20Sopenharmony_cistatic int optimization_mode = NV_OPTIMIZATION_MODE_DYNAMIC;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci/*
8788c2ecf20Sopenharmony_ci * Poll interval for timer irq
8798c2ecf20Sopenharmony_ci *
8808c2ecf20Sopenharmony_ci * This interval determines how frequent an interrupt is generated.
8818c2ecf20Sopenharmony_ci * The is value is determined by [(time_in_micro_secs * 100) / (2^10)]
8828c2ecf20Sopenharmony_ci * Min = 0, and Max = 65535
8838c2ecf20Sopenharmony_ci */
8848c2ecf20Sopenharmony_cistatic int poll_interval = -1;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci/*
8878c2ecf20Sopenharmony_ci * MSI interrupts
8888c2ecf20Sopenharmony_ci */
8898c2ecf20Sopenharmony_cienum {
8908c2ecf20Sopenharmony_ci	NV_MSI_INT_DISABLED,
8918c2ecf20Sopenharmony_ci	NV_MSI_INT_ENABLED
8928c2ecf20Sopenharmony_ci};
8938c2ecf20Sopenharmony_cistatic int msi = NV_MSI_INT_ENABLED;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci/*
8968c2ecf20Sopenharmony_ci * MSIX interrupts
8978c2ecf20Sopenharmony_ci */
8988c2ecf20Sopenharmony_cienum {
8998c2ecf20Sopenharmony_ci	NV_MSIX_INT_DISABLED,
9008c2ecf20Sopenharmony_ci	NV_MSIX_INT_ENABLED
9018c2ecf20Sopenharmony_ci};
9028c2ecf20Sopenharmony_cistatic int msix = NV_MSIX_INT_ENABLED;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci/*
9058c2ecf20Sopenharmony_ci * DMA 64bit
9068c2ecf20Sopenharmony_ci */
9078c2ecf20Sopenharmony_cienum {
9088c2ecf20Sopenharmony_ci	NV_DMA_64BIT_DISABLED,
9098c2ecf20Sopenharmony_ci	NV_DMA_64BIT_ENABLED
9108c2ecf20Sopenharmony_ci};
9118c2ecf20Sopenharmony_cistatic int dma_64bit = NV_DMA_64BIT_ENABLED;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci/*
9148c2ecf20Sopenharmony_ci * Debug output control for tx_timeout
9158c2ecf20Sopenharmony_ci */
9168c2ecf20Sopenharmony_cistatic bool debug_tx_timeout = false;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci/*
9198c2ecf20Sopenharmony_ci * Crossover Detection
9208c2ecf20Sopenharmony_ci * Realtek 8201 phy + some OEM boards do not work properly.
9218c2ecf20Sopenharmony_ci */
9228c2ecf20Sopenharmony_cienum {
9238c2ecf20Sopenharmony_ci	NV_CROSSOVER_DETECTION_DISABLED,
9248c2ecf20Sopenharmony_ci	NV_CROSSOVER_DETECTION_ENABLED
9258c2ecf20Sopenharmony_ci};
9268c2ecf20Sopenharmony_cistatic int phy_cross = NV_CROSSOVER_DETECTION_DISABLED;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci/*
9298c2ecf20Sopenharmony_ci * Power down phy when interface is down (persists through reboot;
9308c2ecf20Sopenharmony_ci * older Linux and other OSes may not power it up again)
9318c2ecf20Sopenharmony_ci */
9328c2ecf20Sopenharmony_cistatic int phy_power_down;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic inline struct fe_priv *get_nvpriv(struct net_device *dev)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	return netdev_priv(dev);
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic inline u8 __iomem *get_hwbase(struct net_device *dev)
9408c2ecf20Sopenharmony_ci{
9418c2ecf20Sopenharmony_ci	return ((struct fe_priv *)netdev_priv(dev))->base;
9428c2ecf20Sopenharmony_ci}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_cistatic inline void pci_push(u8 __iomem *base)
9458c2ecf20Sopenharmony_ci{
9468c2ecf20Sopenharmony_ci	/* force out pending posted writes */
9478c2ecf20Sopenharmony_ci	readl(base);
9488c2ecf20Sopenharmony_ci}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_cistatic inline u32 nv_descr_getlength(struct ring_desc *prd, u32 v)
9518c2ecf20Sopenharmony_ci{
9528c2ecf20Sopenharmony_ci	return le32_to_cpu(prd->flaglen)
9538c2ecf20Sopenharmony_ci		& ((v == DESC_VER_1) ? LEN_MASK_V1 : LEN_MASK_V2);
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_cistatic inline u32 nv_descr_getlength_ex(struct ring_desc_ex *prd, u32 v)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci	return le32_to_cpu(prd->flaglen) & LEN_MASK_V2;
9598c2ecf20Sopenharmony_ci}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_cistatic bool nv_optimized(struct fe_priv *np)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1 || np->desc_ver == DESC_VER_2)
9648c2ecf20Sopenharmony_ci		return false;
9658c2ecf20Sopenharmony_ci	return true;
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic int reg_delay(struct net_device *dev, int offset, u32 mask, u32 target,
9698c2ecf20Sopenharmony_ci		     int delay, int delaymax)
9708c2ecf20Sopenharmony_ci{
9718c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	pci_push(base);
9748c2ecf20Sopenharmony_ci	do {
9758c2ecf20Sopenharmony_ci		udelay(delay);
9768c2ecf20Sopenharmony_ci		delaymax -= delay;
9778c2ecf20Sopenharmony_ci		if (delaymax < 0)
9788c2ecf20Sopenharmony_ci			return 1;
9798c2ecf20Sopenharmony_ci	} while ((readl(base + offset) & mask) != target);
9808c2ecf20Sopenharmony_ci	return 0;
9818c2ecf20Sopenharmony_ci}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci#define NV_SETUP_RX_RING 0x01
9848c2ecf20Sopenharmony_ci#define NV_SETUP_TX_RING 0x02
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_cistatic inline u32 dma_low(dma_addr_t addr)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	return addr;
9898c2ecf20Sopenharmony_ci}
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_cistatic inline u32 dma_high(dma_addr_t addr)
9928c2ecf20Sopenharmony_ci{
9938c2ecf20Sopenharmony_ci	return addr>>31>>1;	/* 0 if 32bit, shift down by 32 if 64bit */
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic void setup_hw_rings(struct net_device *dev, int rxtx_flags)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
9998c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
10028c2ecf20Sopenharmony_ci		if (rxtx_flags & NV_SETUP_RX_RING)
10038c2ecf20Sopenharmony_ci			writel(dma_low(np->ring_addr), base + NvRegRxRingPhysAddr);
10048c2ecf20Sopenharmony_ci		if (rxtx_flags & NV_SETUP_TX_RING)
10058c2ecf20Sopenharmony_ci			writel(dma_low(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc)), base + NvRegTxRingPhysAddr);
10068c2ecf20Sopenharmony_ci	} else {
10078c2ecf20Sopenharmony_ci		if (rxtx_flags & NV_SETUP_RX_RING) {
10088c2ecf20Sopenharmony_ci			writel(dma_low(np->ring_addr), base + NvRegRxRingPhysAddr);
10098c2ecf20Sopenharmony_ci			writel(dma_high(np->ring_addr), base + NvRegRxRingPhysAddrHigh);
10108c2ecf20Sopenharmony_ci		}
10118c2ecf20Sopenharmony_ci		if (rxtx_flags & NV_SETUP_TX_RING) {
10128c2ecf20Sopenharmony_ci			writel(dma_low(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc_ex)), base + NvRegTxRingPhysAddr);
10138c2ecf20Sopenharmony_ci			writel(dma_high(np->ring_addr + np->rx_ring_size*sizeof(struct ring_desc_ex)), base + NvRegTxRingPhysAddrHigh);
10148c2ecf20Sopenharmony_ci		}
10158c2ecf20Sopenharmony_ci	}
10168c2ecf20Sopenharmony_ci}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_cistatic void free_rings(struct net_device *dev)
10198c2ecf20Sopenharmony_ci{
10208c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
10238c2ecf20Sopenharmony_ci		if (np->rx_ring.orig)
10248c2ecf20Sopenharmony_ci			dma_free_coherent(&np->pci_dev->dev,
10258c2ecf20Sopenharmony_ci					  sizeof(struct ring_desc) *
10268c2ecf20Sopenharmony_ci					  (np->rx_ring_size +
10278c2ecf20Sopenharmony_ci					  np->tx_ring_size),
10288c2ecf20Sopenharmony_ci					  np->rx_ring.orig, np->ring_addr);
10298c2ecf20Sopenharmony_ci	} else {
10308c2ecf20Sopenharmony_ci		if (np->rx_ring.ex)
10318c2ecf20Sopenharmony_ci			dma_free_coherent(&np->pci_dev->dev,
10328c2ecf20Sopenharmony_ci					  sizeof(struct ring_desc_ex) *
10338c2ecf20Sopenharmony_ci					  (np->rx_ring_size +
10348c2ecf20Sopenharmony_ci					  np->tx_ring_size),
10358c2ecf20Sopenharmony_ci					  np->rx_ring.ex, np->ring_addr);
10368c2ecf20Sopenharmony_ci	}
10378c2ecf20Sopenharmony_ci	kfree(np->rx_skb);
10388c2ecf20Sopenharmony_ci	kfree(np->tx_skb);
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cistatic int using_multi_irqs(struct net_device *dev)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	if (!(np->msi_flags & NV_MSI_X_ENABLED) ||
10468c2ecf20Sopenharmony_ci	    ((np->msi_flags & NV_MSI_X_ENABLED) &&
10478c2ecf20Sopenharmony_ci	     ((np->msi_flags & NV_MSI_X_VECTORS_MASK) == 0x1)))
10488c2ecf20Sopenharmony_ci		return 0;
10498c2ecf20Sopenharmony_ci	else
10508c2ecf20Sopenharmony_ci		return 1;
10518c2ecf20Sopenharmony_ci}
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_cistatic void nv_txrx_gate(struct net_device *dev, bool gate)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
10568c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
10578c2ecf20Sopenharmony_ci	u32 powerstate;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	if (!np->mac_in_use &&
10608c2ecf20Sopenharmony_ci	    (np->driver_data & DEV_HAS_POWER_CNTRL)) {
10618c2ecf20Sopenharmony_ci		powerstate = readl(base + NvRegPowerState2);
10628c2ecf20Sopenharmony_ci		if (gate)
10638c2ecf20Sopenharmony_ci			powerstate |= NVREG_POWERSTATE2_GATE_CLOCKS;
10648c2ecf20Sopenharmony_ci		else
10658c2ecf20Sopenharmony_ci			powerstate &= ~NVREG_POWERSTATE2_GATE_CLOCKS;
10668c2ecf20Sopenharmony_ci		writel(powerstate, base + NvRegPowerState2);
10678c2ecf20Sopenharmony_ci	}
10688c2ecf20Sopenharmony_ci}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cistatic void nv_enable_irq(struct net_device *dev)
10718c2ecf20Sopenharmony_ci{
10728c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	if (!using_multi_irqs(dev)) {
10758c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_X_ENABLED)
10768c2ecf20Sopenharmony_ci			enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
10778c2ecf20Sopenharmony_ci		else
10788c2ecf20Sopenharmony_ci			enable_irq(np->pci_dev->irq);
10798c2ecf20Sopenharmony_ci	} else {
10808c2ecf20Sopenharmony_ci		enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
10818c2ecf20Sopenharmony_ci		enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
10828c2ecf20Sopenharmony_ci		enable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
10838c2ecf20Sopenharmony_ci	}
10848c2ecf20Sopenharmony_ci}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_cistatic void nv_disable_irq(struct net_device *dev)
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	if (!using_multi_irqs(dev)) {
10918c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_X_ENABLED)
10928c2ecf20Sopenharmony_ci			disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector);
10938c2ecf20Sopenharmony_ci		else
10948c2ecf20Sopenharmony_ci			disable_irq(np->pci_dev->irq);
10958c2ecf20Sopenharmony_ci	} else {
10968c2ecf20Sopenharmony_ci		disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector);
10978c2ecf20Sopenharmony_ci		disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector);
10988c2ecf20Sopenharmony_ci		disable_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector);
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci/* In MSIX mode, a write to irqmask behaves as XOR */
11038c2ecf20Sopenharmony_cistatic void nv_enable_hw_interrupts(struct net_device *dev, u32 mask)
11048c2ecf20Sopenharmony_ci{
11058c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	writel(mask, base + NvRegIrqMask);
11088c2ecf20Sopenharmony_ci}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_cistatic void nv_disable_hw_interrupts(struct net_device *dev, u32 mask)
11118c2ecf20Sopenharmony_ci{
11128c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
11138c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_X_ENABLED) {
11168c2ecf20Sopenharmony_ci		writel(mask, base + NvRegIrqMask);
11178c2ecf20Sopenharmony_ci	} else {
11188c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_ENABLED)
11198c2ecf20Sopenharmony_ci			writel(0, base + NvRegMSIIrqMask);
11208c2ecf20Sopenharmony_ci		writel(0, base + NvRegIrqMask);
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic void nv_napi_enable(struct net_device *dev)
11258c2ecf20Sopenharmony_ci{
11268c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	napi_enable(&np->napi);
11298c2ecf20Sopenharmony_ci}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_cistatic void nv_napi_disable(struct net_device *dev)
11328c2ecf20Sopenharmony_ci{
11338c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	napi_disable(&np->napi);
11368c2ecf20Sopenharmony_ci}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci#define MII_READ	(-1)
11398c2ecf20Sopenharmony_ci/* mii_rw: read/write a register on the PHY.
11408c2ecf20Sopenharmony_ci *
11418c2ecf20Sopenharmony_ci * Caller must guarantee serialization
11428c2ecf20Sopenharmony_ci */
11438c2ecf20Sopenharmony_cistatic int mii_rw(struct net_device *dev, int addr, int miireg, int value)
11448c2ecf20Sopenharmony_ci{
11458c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
11468c2ecf20Sopenharmony_ci	u32 reg;
11478c2ecf20Sopenharmony_ci	int retval;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_MASK_RW, base + NvRegMIIStatus);
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	reg = readl(base + NvRegMIIControl);
11528c2ecf20Sopenharmony_ci	if (reg & NVREG_MIICTL_INUSE) {
11538c2ecf20Sopenharmony_ci		writel(NVREG_MIICTL_INUSE, base + NvRegMIIControl);
11548c2ecf20Sopenharmony_ci		udelay(NV_MIIBUSY_DELAY);
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	reg = (addr << NVREG_MIICTL_ADDRSHIFT) | miireg;
11588c2ecf20Sopenharmony_ci	if (value != MII_READ) {
11598c2ecf20Sopenharmony_ci		writel(value, base + NvRegMIIData);
11608c2ecf20Sopenharmony_ci		reg |= NVREG_MIICTL_WRITE;
11618c2ecf20Sopenharmony_ci	}
11628c2ecf20Sopenharmony_ci	writel(reg, base + NvRegMIIControl);
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	if (reg_delay(dev, NvRegMIIControl, NVREG_MIICTL_INUSE, 0,
11658c2ecf20Sopenharmony_ci			NV_MIIPHY_DELAY, NV_MIIPHY_DELAYMAX)) {
11668c2ecf20Sopenharmony_ci		retval = -1;
11678c2ecf20Sopenharmony_ci	} else if (value != MII_READ) {
11688c2ecf20Sopenharmony_ci		/* it was a write operation - fewer failures are detectable */
11698c2ecf20Sopenharmony_ci		retval = 0;
11708c2ecf20Sopenharmony_ci	} else if (readl(base + NvRegMIIStatus) & NVREG_MIISTAT_ERROR) {
11718c2ecf20Sopenharmony_ci		retval = -1;
11728c2ecf20Sopenharmony_ci	} else {
11738c2ecf20Sopenharmony_ci		retval = readl(base + NvRegMIIData);
11748c2ecf20Sopenharmony_ci	}
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	return retval;
11778c2ecf20Sopenharmony_ci}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_cistatic int phy_reset(struct net_device *dev, u32 bmcr_setup)
11808c2ecf20Sopenharmony_ci{
11818c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
11828c2ecf20Sopenharmony_ci	u32 miicontrol;
11838c2ecf20Sopenharmony_ci	unsigned int tries = 0;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	miicontrol = BMCR_RESET | bmcr_setup;
11868c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, MII_BMCR, miicontrol))
11878c2ecf20Sopenharmony_ci		return -1;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	/* wait for 500ms */
11908c2ecf20Sopenharmony_ci	msleep(500);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	/* must wait till reset is deasserted */
11938c2ecf20Sopenharmony_ci	while (miicontrol & BMCR_RESET) {
11948c2ecf20Sopenharmony_ci		usleep_range(10000, 20000);
11958c2ecf20Sopenharmony_ci		miicontrol = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
11968c2ecf20Sopenharmony_ci		/* FIXME: 100 tries seem excessive */
11978c2ecf20Sopenharmony_ci		if (tries++ > 100)
11988c2ecf20Sopenharmony_ci			return -1;
11998c2ecf20Sopenharmony_ci	}
12008c2ecf20Sopenharmony_ci	return 0;
12018c2ecf20Sopenharmony_ci}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic int init_realtek_8211b(struct net_device *dev, struct fe_priv *np)
12048c2ecf20Sopenharmony_ci{
12058c2ecf20Sopenharmony_ci	static const struct {
12068c2ecf20Sopenharmony_ci		int reg;
12078c2ecf20Sopenharmony_ci		int init;
12088c2ecf20Sopenharmony_ci	} ri[] = {
12098c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1 },
12108c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG2, PHY_REALTEK_INIT2 },
12118c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT3 },
12128c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG3, PHY_REALTEK_INIT4 },
12138c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG4, PHY_REALTEK_INIT5 },
12148c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG5, PHY_REALTEK_INIT6 },
12158c2ecf20Sopenharmony_ci		{ PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1 },
12168c2ecf20Sopenharmony_ci	};
12178c2ecf20Sopenharmony_ci	int i;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ri); i++) {
12208c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, ri[i].reg, ri[i].init))
12218c2ecf20Sopenharmony_ci			return PHY_ERROR;
12228c2ecf20Sopenharmony_ci	}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	return 0;
12258c2ecf20Sopenharmony_ci}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_cistatic int init_realtek_8211c(struct net_device *dev, struct fe_priv *np)
12288c2ecf20Sopenharmony_ci{
12298c2ecf20Sopenharmony_ci	u32 reg;
12308c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
12318c2ecf20Sopenharmony_ci	u32 powerstate = readl(base + NvRegPowerState2);
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	/* need to perform hw phy reset */
12348c2ecf20Sopenharmony_ci	powerstate |= NVREG_POWERSTATE2_PHY_RESET;
12358c2ecf20Sopenharmony_ci	writel(powerstate, base + NvRegPowerState2);
12368c2ecf20Sopenharmony_ci	msleep(25);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	powerstate &= ~NVREG_POWERSTATE2_PHY_RESET;
12398c2ecf20Sopenharmony_ci	writel(powerstate, base + NvRegPowerState2);
12408c2ecf20Sopenharmony_ci	msleep(25);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	reg = mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG6, MII_READ);
12438c2ecf20Sopenharmony_ci	reg |= PHY_REALTEK_INIT9;
12448c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG6, reg))
12458c2ecf20Sopenharmony_ci		return PHY_ERROR;
12468c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
12478c2ecf20Sopenharmony_ci		   PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT10))
12488c2ecf20Sopenharmony_ci		return PHY_ERROR;
12498c2ecf20Sopenharmony_ci	reg = mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG7, MII_READ);
12508c2ecf20Sopenharmony_ci	if (!(reg & PHY_REALTEK_INIT11)) {
12518c2ecf20Sopenharmony_ci		reg |= PHY_REALTEK_INIT11;
12528c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG7, reg))
12538c2ecf20Sopenharmony_ci			return PHY_ERROR;
12548c2ecf20Sopenharmony_ci	}
12558c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
12568c2ecf20Sopenharmony_ci		   PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1))
12578c2ecf20Sopenharmony_ci		return PHY_ERROR;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	return 0;
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_cistatic int init_realtek_8201(struct net_device *dev, struct fe_priv *np)
12638c2ecf20Sopenharmony_ci{
12648c2ecf20Sopenharmony_ci	u32 phy_reserved;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_NEED_PHY_INIT_FIX) {
12678c2ecf20Sopenharmony_ci		phy_reserved = mii_rw(dev, np->phyaddr,
12688c2ecf20Sopenharmony_ci				      PHY_REALTEK_INIT_REG6, MII_READ);
12698c2ecf20Sopenharmony_ci		phy_reserved |= PHY_REALTEK_INIT7;
12708c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr,
12718c2ecf20Sopenharmony_ci			   PHY_REALTEK_INIT_REG6, phy_reserved))
12728c2ecf20Sopenharmony_ci			return PHY_ERROR;
12738c2ecf20Sopenharmony_ci	}
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	return 0;
12768c2ecf20Sopenharmony_ci}
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_cistatic int init_realtek_8201_cross(struct net_device *dev, struct fe_priv *np)
12798c2ecf20Sopenharmony_ci{
12808c2ecf20Sopenharmony_ci	u32 phy_reserved;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	if (phy_cross == NV_CROSSOVER_DETECTION_DISABLED) {
12838c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr,
12848c2ecf20Sopenharmony_ci			   PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT3))
12858c2ecf20Sopenharmony_ci			return PHY_ERROR;
12868c2ecf20Sopenharmony_ci		phy_reserved = mii_rw(dev, np->phyaddr,
12878c2ecf20Sopenharmony_ci				      PHY_REALTEK_INIT_REG2, MII_READ);
12888c2ecf20Sopenharmony_ci		phy_reserved &= ~PHY_REALTEK_INIT_MSK1;
12898c2ecf20Sopenharmony_ci		phy_reserved |= PHY_REALTEK_INIT3;
12908c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr,
12918c2ecf20Sopenharmony_ci			   PHY_REALTEK_INIT_REG2, phy_reserved))
12928c2ecf20Sopenharmony_ci			return PHY_ERROR;
12938c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr,
12948c2ecf20Sopenharmony_ci			   PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1))
12958c2ecf20Sopenharmony_ci			return PHY_ERROR;
12968c2ecf20Sopenharmony_ci	}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	return 0;
12998c2ecf20Sopenharmony_ci}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_cistatic int init_cicada(struct net_device *dev, struct fe_priv *np,
13028c2ecf20Sopenharmony_ci		       u32 phyinterface)
13038c2ecf20Sopenharmony_ci{
13048c2ecf20Sopenharmony_ci	u32 phy_reserved;
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	if (phyinterface & PHY_RGMII) {
13078c2ecf20Sopenharmony_ci		phy_reserved = mii_rw(dev, np->phyaddr, MII_RESV1, MII_READ);
13088c2ecf20Sopenharmony_ci		phy_reserved &= ~(PHY_CICADA_INIT1 | PHY_CICADA_INIT2);
13098c2ecf20Sopenharmony_ci		phy_reserved |= (PHY_CICADA_INIT3 | PHY_CICADA_INIT4);
13108c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, MII_RESV1, phy_reserved))
13118c2ecf20Sopenharmony_ci			return PHY_ERROR;
13128c2ecf20Sopenharmony_ci		phy_reserved = mii_rw(dev, np->phyaddr, MII_NCONFIG, MII_READ);
13138c2ecf20Sopenharmony_ci		phy_reserved |= PHY_CICADA_INIT5;
13148c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, MII_NCONFIG, phy_reserved))
13158c2ecf20Sopenharmony_ci			return PHY_ERROR;
13168c2ecf20Sopenharmony_ci	}
13178c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr, MII_SREVISION, MII_READ);
13188c2ecf20Sopenharmony_ci	phy_reserved |= PHY_CICADA_INIT6;
13198c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, MII_SREVISION, phy_reserved))
13208c2ecf20Sopenharmony_ci		return PHY_ERROR;
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	return 0;
13238c2ecf20Sopenharmony_ci}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_cistatic int init_vitesse(struct net_device *dev, struct fe_priv *np)
13268c2ecf20Sopenharmony_ci{
13278c2ecf20Sopenharmony_ci	u32 phy_reserved;
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13308c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG1, PHY_VITESSE_INIT1))
13318c2ecf20Sopenharmony_ci		return PHY_ERROR;
13328c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13338c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT2))
13348c2ecf20Sopenharmony_ci		return PHY_ERROR;
13358c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13368c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG4, MII_READ);
13378c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved))
13388c2ecf20Sopenharmony_ci		return PHY_ERROR;
13398c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13408c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG3, MII_READ);
13418c2ecf20Sopenharmony_ci	phy_reserved &= ~PHY_VITESSE_INIT_MSK1;
13428c2ecf20Sopenharmony_ci	phy_reserved |= PHY_VITESSE_INIT3;
13438c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved))
13448c2ecf20Sopenharmony_ci		return PHY_ERROR;
13458c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13468c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT4))
13478c2ecf20Sopenharmony_ci		return PHY_ERROR;
13488c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13498c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT5))
13508c2ecf20Sopenharmony_ci		return PHY_ERROR;
13518c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13528c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG4, MII_READ);
13538c2ecf20Sopenharmony_ci	phy_reserved &= ~PHY_VITESSE_INIT_MSK1;
13548c2ecf20Sopenharmony_ci	phy_reserved |= PHY_VITESSE_INIT3;
13558c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved))
13568c2ecf20Sopenharmony_ci		return PHY_ERROR;
13578c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13588c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG3, MII_READ);
13598c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved))
13608c2ecf20Sopenharmony_ci		return PHY_ERROR;
13618c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13628c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT6))
13638c2ecf20Sopenharmony_ci		return PHY_ERROR;
13648c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13658c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT7))
13668c2ecf20Sopenharmony_ci		return PHY_ERROR;
13678c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13688c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG4, MII_READ);
13698c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG4, phy_reserved))
13708c2ecf20Sopenharmony_ci		return PHY_ERROR;
13718c2ecf20Sopenharmony_ci	phy_reserved = mii_rw(dev, np->phyaddr,
13728c2ecf20Sopenharmony_ci			      PHY_VITESSE_INIT_REG3, MII_READ);
13738c2ecf20Sopenharmony_ci	phy_reserved &= ~PHY_VITESSE_INIT_MSK2;
13748c2ecf20Sopenharmony_ci	phy_reserved |= PHY_VITESSE_INIT8;
13758c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, PHY_VITESSE_INIT_REG3, phy_reserved))
13768c2ecf20Sopenharmony_ci		return PHY_ERROR;
13778c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13788c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG2, PHY_VITESSE_INIT9))
13798c2ecf20Sopenharmony_ci		return PHY_ERROR;
13808c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr,
13818c2ecf20Sopenharmony_ci		   PHY_VITESSE_INIT_REG1, PHY_VITESSE_INIT10))
13828c2ecf20Sopenharmony_ci		return PHY_ERROR;
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	return 0;
13858c2ecf20Sopenharmony_ci}
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_cistatic int phy_init(struct net_device *dev)
13888c2ecf20Sopenharmony_ci{
13898c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
13908c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
13918c2ecf20Sopenharmony_ci	u32 phyinterface;
13928c2ecf20Sopenharmony_ci	u32 mii_status, mii_control, mii_control_1000, reg;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	/* phy errata for E3016 phy */
13958c2ecf20Sopenharmony_ci	if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
13968c2ecf20Sopenharmony_ci		reg = mii_rw(dev, np->phyaddr, MII_NCONFIG, MII_READ);
13978c2ecf20Sopenharmony_ci		reg &= ~PHY_MARVELL_E3016_INITMASK;
13988c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, MII_NCONFIG, reg)) {
13998c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy write to errata reg failed\n",
14008c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14018c2ecf20Sopenharmony_ci			return PHY_ERROR;
14028c2ecf20Sopenharmony_ci		}
14038c2ecf20Sopenharmony_ci	}
14048c2ecf20Sopenharmony_ci	if (np->phy_oui == PHY_OUI_REALTEK) {
14058c2ecf20Sopenharmony_ci		if (np->phy_model == PHY_MODEL_REALTEK_8211 &&
14068c2ecf20Sopenharmony_ci		    np->phy_rev == PHY_REV_REALTEK_8211B) {
14078c2ecf20Sopenharmony_ci			if (init_realtek_8211b(dev, np)) {
14088c2ecf20Sopenharmony_ci				netdev_info(dev, "%s: phy init failed\n",
14098c2ecf20Sopenharmony_ci					    pci_name(np->pci_dev));
14108c2ecf20Sopenharmony_ci				return PHY_ERROR;
14118c2ecf20Sopenharmony_ci			}
14128c2ecf20Sopenharmony_ci		} else if (np->phy_model == PHY_MODEL_REALTEK_8211 &&
14138c2ecf20Sopenharmony_ci			   np->phy_rev == PHY_REV_REALTEK_8211C) {
14148c2ecf20Sopenharmony_ci			if (init_realtek_8211c(dev, np)) {
14158c2ecf20Sopenharmony_ci				netdev_info(dev, "%s: phy init failed\n",
14168c2ecf20Sopenharmony_ci					    pci_name(np->pci_dev));
14178c2ecf20Sopenharmony_ci				return PHY_ERROR;
14188c2ecf20Sopenharmony_ci			}
14198c2ecf20Sopenharmony_ci		} else if (np->phy_model == PHY_MODEL_REALTEK_8201) {
14208c2ecf20Sopenharmony_ci			if (init_realtek_8201(dev, np)) {
14218c2ecf20Sopenharmony_ci				netdev_info(dev, "%s: phy init failed\n",
14228c2ecf20Sopenharmony_ci					    pci_name(np->pci_dev));
14238c2ecf20Sopenharmony_ci				return PHY_ERROR;
14248c2ecf20Sopenharmony_ci			}
14258c2ecf20Sopenharmony_ci		}
14268c2ecf20Sopenharmony_ci	}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	/* set advertise register */
14298c2ecf20Sopenharmony_ci	reg = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
14308c2ecf20Sopenharmony_ci	reg |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
14318c2ecf20Sopenharmony_ci		ADVERTISE_100HALF | ADVERTISE_100FULL |
14328c2ecf20Sopenharmony_ci		ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP);
14338c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, MII_ADVERTISE, reg)) {
14348c2ecf20Sopenharmony_ci		netdev_info(dev, "%s: phy write to advertise failed\n",
14358c2ecf20Sopenharmony_ci			    pci_name(np->pci_dev));
14368c2ecf20Sopenharmony_ci		return PHY_ERROR;
14378c2ecf20Sopenharmony_ci	}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	/* get phy interface type */
14408c2ecf20Sopenharmony_ci	phyinterface = readl(base + NvRegPhyInterface);
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	/* see if gigabit phy */
14438c2ecf20Sopenharmony_ci	mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
14448c2ecf20Sopenharmony_ci	if (mii_status & PHY_GIGABIT) {
14458c2ecf20Sopenharmony_ci		np->gigabit = PHY_GIGABIT;
14468c2ecf20Sopenharmony_ci		mii_control_1000 = mii_rw(dev, np->phyaddr,
14478c2ecf20Sopenharmony_ci					  MII_CTRL1000, MII_READ);
14488c2ecf20Sopenharmony_ci		mii_control_1000 &= ~ADVERTISE_1000HALF;
14498c2ecf20Sopenharmony_ci		if (phyinterface & PHY_RGMII)
14508c2ecf20Sopenharmony_ci			mii_control_1000 |= ADVERTISE_1000FULL;
14518c2ecf20Sopenharmony_ci		else
14528c2ecf20Sopenharmony_ci			mii_control_1000 &= ~ADVERTISE_1000FULL;
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, MII_CTRL1000, mii_control_1000)) {
14558c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy init failed\n",
14568c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14578c2ecf20Sopenharmony_ci			return PHY_ERROR;
14588c2ecf20Sopenharmony_ci		}
14598c2ecf20Sopenharmony_ci	} else
14608c2ecf20Sopenharmony_ci		np->gigabit = 0;
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	mii_control = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
14638c2ecf20Sopenharmony_ci	mii_control |= BMCR_ANENABLE;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	if (np->phy_oui == PHY_OUI_REALTEK &&
14668c2ecf20Sopenharmony_ci	    np->phy_model == PHY_MODEL_REALTEK_8211 &&
14678c2ecf20Sopenharmony_ci	    np->phy_rev == PHY_REV_REALTEK_8211C) {
14688c2ecf20Sopenharmony_ci		/* start autoneg since we already performed hw reset above */
14698c2ecf20Sopenharmony_ci		mii_control |= BMCR_ANRESTART;
14708c2ecf20Sopenharmony_ci		if (mii_rw(dev, np->phyaddr, MII_BMCR, mii_control)) {
14718c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy init failed\n",
14728c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14738c2ecf20Sopenharmony_ci			return PHY_ERROR;
14748c2ecf20Sopenharmony_ci		}
14758c2ecf20Sopenharmony_ci	} else {
14768c2ecf20Sopenharmony_ci		/* reset the phy
14778c2ecf20Sopenharmony_ci		 * (certain phys need bmcr to be setup with reset)
14788c2ecf20Sopenharmony_ci		 */
14798c2ecf20Sopenharmony_ci		if (phy_reset(dev, mii_control)) {
14808c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy reset failed\n",
14818c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14828c2ecf20Sopenharmony_ci			return PHY_ERROR;
14838c2ecf20Sopenharmony_ci		}
14848c2ecf20Sopenharmony_ci	}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	/* phy vendor specific configuration */
14878c2ecf20Sopenharmony_ci	if (np->phy_oui == PHY_OUI_CICADA) {
14888c2ecf20Sopenharmony_ci		if (init_cicada(dev, np, phyinterface)) {
14898c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy init failed\n",
14908c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14918c2ecf20Sopenharmony_ci			return PHY_ERROR;
14928c2ecf20Sopenharmony_ci		}
14938c2ecf20Sopenharmony_ci	} else if (np->phy_oui == PHY_OUI_VITESSE) {
14948c2ecf20Sopenharmony_ci		if (init_vitesse(dev, np)) {
14958c2ecf20Sopenharmony_ci			netdev_info(dev, "%s: phy init failed\n",
14968c2ecf20Sopenharmony_ci				    pci_name(np->pci_dev));
14978c2ecf20Sopenharmony_ci			return PHY_ERROR;
14988c2ecf20Sopenharmony_ci		}
14998c2ecf20Sopenharmony_ci	} else if (np->phy_oui == PHY_OUI_REALTEK) {
15008c2ecf20Sopenharmony_ci		if (np->phy_model == PHY_MODEL_REALTEK_8211 &&
15018c2ecf20Sopenharmony_ci		    np->phy_rev == PHY_REV_REALTEK_8211B) {
15028c2ecf20Sopenharmony_ci			/* reset could have cleared these out, set them back */
15038c2ecf20Sopenharmony_ci			if (init_realtek_8211b(dev, np)) {
15048c2ecf20Sopenharmony_ci				netdev_info(dev, "%s: phy init failed\n",
15058c2ecf20Sopenharmony_ci					    pci_name(np->pci_dev));
15068c2ecf20Sopenharmony_ci				return PHY_ERROR;
15078c2ecf20Sopenharmony_ci			}
15088c2ecf20Sopenharmony_ci		} else if (np->phy_model == PHY_MODEL_REALTEK_8201) {
15098c2ecf20Sopenharmony_ci			if (init_realtek_8201(dev, np) ||
15108c2ecf20Sopenharmony_ci			    init_realtek_8201_cross(dev, np)) {
15118c2ecf20Sopenharmony_ci				netdev_info(dev, "%s: phy init failed\n",
15128c2ecf20Sopenharmony_ci					    pci_name(np->pci_dev));
15138c2ecf20Sopenharmony_ci				return PHY_ERROR;
15148c2ecf20Sopenharmony_ci			}
15158c2ecf20Sopenharmony_ci		}
15168c2ecf20Sopenharmony_ci	}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci	/* some phys clear out pause advertisement on reset, set it back */
15198c2ecf20Sopenharmony_ci	mii_rw(dev, np->phyaddr, MII_ADVERTISE, reg);
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	/* restart auto negotiation, power down phy */
15228c2ecf20Sopenharmony_ci	mii_control = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
15238c2ecf20Sopenharmony_ci	mii_control |= (BMCR_ANRESTART | BMCR_ANENABLE);
15248c2ecf20Sopenharmony_ci	if (phy_power_down)
15258c2ecf20Sopenharmony_ci		mii_control |= BMCR_PDOWN;
15268c2ecf20Sopenharmony_ci	if (mii_rw(dev, np->phyaddr, MII_BMCR, mii_control))
15278c2ecf20Sopenharmony_ci		return PHY_ERROR;
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	return 0;
15308c2ecf20Sopenharmony_ci}
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_cistatic void nv_start_rx(struct net_device *dev)
15338c2ecf20Sopenharmony_ci{
15348c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
15358c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
15368c2ecf20Sopenharmony_ci	u32 rx_ctrl = readl(base + NvRegReceiverControl);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	/* Already running? Stop it. */
15398c2ecf20Sopenharmony_ci	if ((readl(base + NvRegReceiverControl) & NVREG_RCVCTL_START) && !np->mac_in_use) {
15408c2ecf20Sopenharmony_ci		rx_ctrl &= ~NVREG_RCVCTL_START;
15418c2ecf20Sopenharmony_ci		writel(rx_ctrl, base + NvRegReceiverControl);
15428c2ecf20Sopenharmony_ci		pci_push(base);
15438c2ecf20Sopenharmony_ci	}
15448c2ecf20Sopenharmony_ci	writel(np->linkspeed, base + NvRegLinkSpeed);
15458c2ecf20Sopenharmony_ci	pci_push(base);
15468c2ecf20Sopenharmony_ci	rx_ctrl |= NVREG_RCVCTL_START;
15478c2ecf20Sopenharmony_ci	if (np->mac_in_use)
15488c2ecf20Sopenharmony_ci		rx_ctrl &= ~NVREG_RCVCTL_RX_PATH_EN;
15498c2ecf20Sopenharmony_ci	writel(rx_ctrl, base + NvRegReceiverControl);
15508c2ecf20Sopenharmony_ci	pci_push(base);
15518c2ecf20Sopenharmony_ci}
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_cistatic void nv_stop_rx(struct net_device *dev)
15548c2ecf20Sopenharmony_ci{
15558c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
15568c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
15578c2ecf20Sopenharmony_ci	u32 rx_ctrl = readl(base + NvRegReceiverControl);
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	if (!np->mac_in_use)
15608c2ecf20Sopenharmony_ci		rx_ctrl &= ~NVREG_RCVCTL_START;
15618c2ecf20Sopenharmony_ci	else
15628c2ecf20Sopenharmony_ci		rx_ctrl |= NVREG_RCVCTL_RX_PATH_EN;
15638c2ecf20Sopenharmony_ci	writel(rx_ctrl, base + NvRegReceiverControl);
15648c2ecf20Sopenharmony_ci	if (reg_delay(dev, NvRegReceiverStatus, NVREG_RCVSTAT_BUSY, 0,
15658c2ecf20Sopenharmony_ci		      NV_RXSTOP_DELAY1, NV_RXSTOP_DELAY1MAX))
15668c2ecf20Sopenharmony_ci		netdev_info(dev, "%s: ReceiverStatus remained busy\n",
15678c2ecf20Sopenharmony_ci			    __func__);
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	udelay(NV_RXSTOP_DELAY2);
15708c2ecf20Sopenharmony_ci	if (!np->mac_in_use)
15718c2ecf20Sopenharmony_ci		writel(0, base + NvRegLinkSpeed);
15728c2ecf20Sopenharmony_ci}
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_cistatic void nv_start_tx(struct net_device *dev)
15758c2ecf20Sopenharmony_ci{
15768c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
15778c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
15788c2ecf20Sopenharmony_ci	u32 tx_ctrl = readl(base + NvRegTransmitterControl);
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	tx_ctrl |= NVREG_XMITCTL_START;
15818c2ecf20Sopenharmony_ci	if (np->mac_in_use)
15828c2ecf20Sopenharmony_ci		tx_ctrl &= ~NVREG_XMITCTL_TX_PATH_EN;
15838c2ecf20Sopenharmony_ci	writel(tx_ctrl, base + NvRegTransmitterControl);
15848c2ecf20Sopenharmony_ci	pci_push(base);
15858c2ecf20Sopenharmony_ci}
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_cistatic void nv_stop_tx(struct net_device *dev)
15888c2ecf20Sopenharmony_ci{
15898c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
15908c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
15918c2ecf20Sopenharmony_ci	u32 tx_ctrl = readl(base + NvRegTransmitterControl);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	if (!np->mac_in_use)
15948c2ecf20Sopenharmony_ci		tx_ctrl &= ~NVREG_XMITCTL_START;
15958c2ecf20Sopenharmony_ci	else
15968c2ecf20Sopenharmony_ci		tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN;
15978c2ecf20Sopenharmony_ci	writel(tx_ctrl, base + NvRegTransmitterControl);
15988c2ecf20Sopenharmony_ci	if (reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
15998c2ecf20Sopenharmony_ci		      NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX))
16008c2ecf20Sopenharmony_ci		netdev_info(dev, "%s: TransmitterStatus remained busy\n",
16018c2ecf20Sopenharmony_ci			    __func__);
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	udelay(NV_TXSTOP_DELAY2);
16048c2ecf20Sopenharmony_ci	if (!np->mac_in_use)
16058c2ecf20Sopenharmony_ci		writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV,
16068c2ecf20Sopenharmony_ci		       base + NvRegTransmitPoll);
16078c2ecf20Sopenharmony_ci}
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_cistatic void nv_start_rxtx(struct net_device *dev)
16108c2ecf20Sopenharmony_ci{
16118c2ecf20Sopenharmony_ci	nv_start_rx(dev);
16128c2ecf20Sopenharmony_ci	nv_start_tx(dev);
16138c2ecf20Sopenharmony_ci}
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_cistatic void nv_stop_rxtx(struct net_device *dev)
16168c2ecf20Sopenharmony_ci{
16178c2ecf20Sopenharmony_ci	nv_stop_rx(dev);
16188c2ecf20Sopenharmony_ci	nv_stop_tx(dev);
16198c2ecf20Sopenharmony_ci}
16208c2ecf20Sopenharmony_ci
16218c2ecf20Sopenharmony_cistatic void nv_txrx_reset(struct net_device *dev)
16228c2ecf20Sopenharmony_ci{
16238c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
16248c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->txrxctl_bits, base + NvRegTxRxControl);
16278c2ecf20Sopenharmony_ci	pci_push(base);
16288c2ecf20Sopenharmony_ci	udelay(NV_TXRX_RESET_DELAY);
16298c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_BIT2 | np->txrxctl_bits, base + NvRegTxRxControl);
16308c2ecf20Sopenharmony_ci	pci_push(base);
16318c2ecf20Sopenharmony_ci}
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_cistatic void nv_mac_reset(struct net_device *dev)
16348c2ecf20Sopenharmony_ci{
16358c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
16368c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
16378c2ecf20Sopenharmony_ci	u32 temp1, temp2, temp3;
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->txrxctl_bits, base + NvRegTxRxControl);
16408c2ecf20Sopenharmony_ci	pci_push(base);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	/* save registers since they will be cleared on reset */
16438c2ecf20Sopenharmony_ci	temp1 = readl(base + NvRegMacAddrA);
16448c2ecf20Sopenharmony_ci	temp2 = readl(base + NvRegMacAddrB);
16458c2ecf20Sopenharmony_ci	temp3 = readl(base + NvRegTransmitPoll);
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	writel(NVREG_MAC_RESET_ASSERT, base + NvRegMacReset);
16488c2ecf20Sopenharmony_ci	pci_push(base);
16498c2ecf20Sopenharmony_ci	udelay(NV_MAC_RESET_DELAY);
16508c2ecf20Sopenharmony_ci	writel(0, base + NvRegMacReset);
16518c2ecf20Sopenharmony_ci	pci_push(base);
16528c2ecf20Sopenharmony_ci	udelay(NV_MAC_RESET_DELAY);
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	/* restore saved registers */
16558c2ecf20Sopenharmony_ci	writel(temp1, base + NvRegMacAddrA);
16568c2ecf20Sopenharmony_ci	writel(temp2, base + NvRegMacAddrB);
16578c2ecf20Sopenharmony_ci	writel(temp3, base + NvRegTransmitPoll);
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_BIT2 | np->txrxctl_bits, base + NvRegTxRxControl);
16608c2ecf20Sopenharmony_ci	pci_push(base);
16618c2ecf20Sopenharmony_ci}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci/* Caller must appropriately lock netdev_priv(dev)->hwstats_lock */
16648c2ecf20Sopenharmony_cistatic void nv_update_stats(struct net_device *dev)
16658c2ecf20Sopenharmony_ci{
16668c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
16678c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	/* If it happens that this is run in top-half context, then
16708c2ecf20Sopenharmony_ci	 * replace the spin_lock of hwstats_lock with
16718c2ecf20Sopenharmony_ci	 * spin_lock_irqsave() in calling functions. */
16728c2ecf20Sopenharmony_ci	WARN_ONCE(in_irq(), "forcedeth: estats spin_lock(_bh) from top-half");
16738c2ecf20Sopenharmony_ci	assert_spin_locked(&np->hwstats_lock);
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	/* query hardware */
16768c2ecf20Sopenharmony_ci	np->estats.tx_bytes += readl(base + NvRegTxCnt);
16778c2ecf20Sopenharmony_ci	np->estats.tx_zero_rexmt += readl(base + NvRegTxZeroReXmt);
16788c2ecf20Sopenharmony_ci	np->estats.tx_one_rexmt += readl(base + NvRegTxOneReXmt);
16798c2ecf20Sopenharmony_ci	np->estats.tx_many_rexmt += readl(base + NvRegTxManyReXmt);
16808c2ecf20Sopenharmony_ci	np->estats.tx_late_collision += readl(base + NvRegTxLateCol);
16818c2ecf20Sopenharmony_ci	np->estats.tx_fifo_errors += readl(base + NvRegTxUnderflow);
16828c2ecf20Sopenharmony_ci	np->estats.tx_carrier_errors += readl(base + NvRegTxLossCarrier);
16838c2ecf20Sopenharmony_ci	np->estats.tx_excess_deferral += readl(base + NvRegTxExcessDef);
16848c2ecf20Sopenharmony_ci	np->estats.tx_retry_error += readl(base + NvRegTxRetryErr);
16858c2ecf20Sopenharmony_ci	np->estats.rx_frame_error += readl(base + NvRegRxFrameErr);
16868c2ecf20Sopenharmony_ci	np->estats.rx_extra_byte += readl(base + NvRegRxExtraByte);
16878c2ecf20Sopenharmony_ci	np->estats.rx_late_collision += readl(base + NvRegRxLateCol);
16888c2ecf20Sopenharmony_ci	np->estats.rx_runt += readl(base + NvRegRxRunt);
16898c2ecf20Sopenharmony_ci	np->estats.rx_frame_too_long += readl(base + NvRegRxFrameTooLong);
16908c2ecf20Sopenharmony_ci	np->estats.rx_over_errors += readl(base + NvRegRxOverflow);
16918c2ecf20Sopenharmony_ci	np->estats.rx_crc_errors += readl(base + NvRegRxFCSErr);
16928c2ecf20Sopenharmony_ci	np->estats.rx_frame_align_error += readl(base + NvRegRxFrameAlignErr);
16938c2ecf20Sopenharmony_ci	np->estats.rx_length_error += readl(base + NvRegRxLenErr);
16948c2ecf20Sopenharmony_ci	np->estats.rx_unicast += readl(base + NvRegRxUnicast);
16958c2ecf20Sopenharmony_ci	np->estats.rx_multicast += readl(base + NvRegRxMulticast);
16968c2ecf20Sopenharmony_ci	np->estats.rx_broadcast += readl(base + NvRegRxBroadcast);
16978c2ecf20Sopenharmony_ci	np->estats.rx_packets =
16988c2ecf20Sopenharmony_ci		np->estats.rx_unicast +
16998c2ecf20Sopenharmony_ci		np->estats.rx_multicast +
17008c2ecf20Sopenharmony_ci		np->estats.rx_broadcast;
17018c2ecf20Sopenharmony_ci	np->estats.rx_errors_total =
17028c2ecf20Sopenharmony_ci		np->estats.rx_crc_errors +
17038c2ecf20Sopenharmony_ci		np->estats.rx_over_errors +
17048c2ecf20Sopenharmony_ci		np->estats.rx_frame_error +
17058c2ecf20Sopenharmony_ci		(np->estats.rx_frame_align_error - np->estats.rx_extra_byte) +
17068c2ecf20Sopenharmony_ci		np->estats.rx_late_collision +
17078c2ecf20Sopenharmony_ci		np->estats.rx_runt +
17088c2ecf20Sopenharmony_ci		np->estats.rx_frame_too_long;
17098c2ecf20Sopenharmony_ci	np->estats.tx_errors_total =
17108c2ecf20Sopenharmony_ci		np->estats.tx_late_collision +
17118c2ecf20Sopenharmony_ci		np->estats.tx_fifo_errors +
17128c2ecf20Sopenharmony_ci		np->estats.tx_carrier_errors +
17138c2ecf20Sopenharmony_ci		np->estats.tx_excess_deferral +
17148c2ecf20Sopenharmony_ci		np->estats.tx_retry_error;
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_HAS_STATISTICS_V2) {
17178c2ecf20Sopenharmony_ci		np->estats.tx_deferral += readl(base + NvRegTxDef);
17188c2ecf20Sopenharmony_ci		np->estats.tx_packets += readl(base + NvRegTxFrame);
17198c2ecf20Sopenharmony_ci		np->estats.rx_bytes += readl(base + NvRegRxCnt);
17208c2ecf20Sopenharmony_ci		np->estats.tx_pause += readl(base + NvRegTxPause);
17218c2ecf20Sopenharmony_ci		np->estats.rx_pause += readl(base + NvRegRxPause);
17228c2ecf20Sopenharmony_ci		np->estats.rx_drop_frame += readl(base + NvRegRxDropFrame);
17238c2ecf20Sopenharmony_ci		np->estats.rx_errors_total += np->estats.rx_drop_frame;
17248c2ecf20Sopenharmony_ci	}
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_HAS_STATISTICS_V3) {
17278c2ecf20Sopenharmony_ci		np->estats.tx_unicast += readl(base + NvRegTxUnicast);
17288c2ecf20Sopenharmony_ci		np->estats.tx_multicast += readl(base + NvRegTxMulticast);
17298c2ecf20Sopenharmony_ci		np->estats.tx_broadcast += readl(base + NvRegTxBroadcast);
17308c2ecf20Sopenharmony_ci	}
17318c2ecf20Sopenharmony_ci}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_cistatic void nv_get_stats(int cpu, struct fe_priv *np,
17348c2ecf20Sopenharmony_ci			 struct rtnl_link_stats64 *storage)
17358c2ecf20Sopenharmony_ci{
17368c2ecf20Sopenharmony_ci	struct nv_txrx_stats *src = per_cpu_ptr(np->txrx_stats, cpu);
17378c2ecf20Sopenharmony_ci	unsigned int syncp_start;
17388c2ecf20Sopenharmony_ci	u64 rx_packets, rx_bytes, rx_dropped, rx_missed_errors;
17398c2ecf20Sopenharmony_ci	u64 tx_packets, tx_bytes, tx_dropped;
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	do {
17428c2ecf20Sopenharmony_ci		syncp_start = u64_stats_fetch_begin_irq(&np->swstats_rx_syncp);
17438c2ecf20Sopenharmony_ci		rx_packets       = src->stat_rx_packets;
17448c2ecf20Sopenharmony_ci		rx_bytes         = src->stat_rx_bytes;
17458c2ecf20Sopenharmony_ci		rx_dropped       = src->stat_rx_dropped;
17468c2ecf20Sopenharmony_ci		rx_missed_errors = src->stat_rx_missed_errors;
17478c2ecf20Sopenharmony_ci	} while (u64_stats_fetch_retry_irq(&np->swstats_rx_syncp, syncp_start));
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci	storage->rx_packets       += rx_packets;
17508c2ecf20Sopenharmony_ci	storage->rx_bytes         += rx_bytes;
17518c2ecf20Sopenharmony_ci	storage->rx_dropped       += rx_dropped;
17528c2ecf20Sopenharmony_ci	storage->rx_missed_errors += rx_missed_errors;
17538c2ecf20Sopenharmony_ci
17548c2ecf20Sopenharmony_ci	do {
17558c2ecf20Sopenharmony_ci		syncp_start = u64_stats_fetch_begin_irq(&np->swstats_tx_syncp);
17568c2ecf20Sopenharmony_ci		tx_packets  = src->stat_tx_packets;
17578c2ecf20Sopenharmony_ci		tx_bytes    = src->stat_tx_bytes;
17588c2ecf20Sopenharmony_ci		tx_dropped  = src->stat_tx_dropped;
17598c2ecf20Sopenharmony_ci	} while (u64_stats_fetch_retry_irq(&np->swstats_tx_syncp, syncp_start));
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	storage->tx_packets += tx_packets;
17628c2ecf20Sopenharmony_ci	storage->tx_bytes   += tx_bytes;
17638c2ecf20Sopenharmony_ci	storage->tx_dropped += tx_dropped;
17648c2ecf20Sopenharmony_ci}
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci/*
17678c2ecf20Sopenharmony_ci * nv_get_stats64: dev->ndo_get_stats64 function
17688c2ecf20Sopenharmony_ci * Get latest stats value from the nic.
17698c2ecf20Sopenharmony_ci * Called with read_lock(&dev_base_lock) held for read -
17708c2ecf20Sopenharmony_ci * only synchronized against unregister_netdevice.
17718c2ecf20Sopenharmony_ci */
17728c2ecf20Sopenharmony_cistatic void
17738c2ecf20Sopenharmony_cinv_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *storage)
17748c2ecf20Sopenharmony_ci	__acquires(&netdev_priv(dev)->hwstats_lock)
17758c2ecf20Sopenharmony_ci	__releases(&netdev_priv(dev)->hwstats_lock)
17768c2ecf20Sopenharmony_ci{
17778c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
17788c2ecf20Sopenharmony_ci	int cpu;
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	/*
17818c2ecf20Sopenharmony_ci	 * Note: because HW stats are not always available and for
17828c2ecf20Sopenharmony_ci	 * consistency reasons, the following ifconfig stats are
17838c2ecf20Sopenharmony_ci	 * managed by software: rx_bytes, tx_bytes, rx_packets and
17848c2ecf20Sopenharmony_ci	 * tx_packets. The related hardware stats reported by ethtool
17858c2ecf20Sopenharmony_ci	 * should be equivalent to these ifconfig stats, with 4
17868c2ecf20Sopenharmony_ci	 * additional bytes per packet (Ethernet FCS CRC), except for
17878c2ecf20Sopenharmony_ci	 * tx_packets when TSO kicks in.
17888c2ecf20Sopenharmony_ci	 */
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	/* software stats */
17918c2ecf20Sopenharmony_ci	for_each_online_cpu(cpu)
17928c2ecf20Sopenharmony_ci		nv_get_stats(cpu, np, storage);
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	/* If the nic supports hw counters then retrieve latest values */
17958c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_HAS_STATISTICS_V123) {
17968c2ecf20Sopenharmony_ci		spin_lock_bh(&np->hwstats_lock);
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci		nv_update_stats(dev);
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci		/* generic stats */
18018c2ecf20Sopenharmony_ci		storage->rx_errors = np->estats.rx_errors_total;
18028c2ecf20Sopenharmony_ci		storage->tx_errors = np->estats.tx_errors_total;
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci		/* meaningful only when NIC supports stats v3 */
18058c2ecf20Sopenharmony_ci		storage->multicast = np->estats.rx_multicast;
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci		/* detailed rx_errors */
18088c2ecf20Sopenharmony_ci		storage->rx_length_errors = np->estats.rx_length_error;
18098c2ecf20Sopenharmony_ci		storage->rx_over_errors   = np->estats.rx_over_errors;
18108c2ecf20Sopenharmony_ci		storage->rx_crc_errors    = np->estats.rx_crc_errors;
18118c2ecf20Sopenharmony_ci		storage->rx_frame_errors  = np->estats.rx_frame_align_error;
18128c2ecf20Sopenharmony_ci		storage->rx_fifo_errors   = np->estats.rx_drop_frame;
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci		/* detailed tx_errors */
18158c2ecf20Sopenharmony_ci		storage->tx_carrier_errors = np->estats.tx_carrier_errors;
18168c2ecf20Sopenharmony_ci		storage->tx_fifo_errors    = np->estats.tx_fifo_errors;
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci		spin_unlock_bh(&np->hwstats_lock);
18198c2ecf20Sopenharmony_ci	}
18208c2ecf20Sopenharmony_ci}
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci/*
18238c2ecf20Sopenharmony_ci * nv_alloc_rx: fill rx ring entries.
18248c2ecf20Sopenharmony_ci * Return 1 if the allocations for the skbs failed and the
18258c2ecf20Sopenharmony_ci * rx engine is without Available descriptors
18268c2ecf20Sopenharmony_ci */
18278c2ecf20Sopenharmony_cistatic int nv_alloc_rx(struct net_device *dev)
18288c2ecf20Sopenharmony_ci{
18298c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
18308c2ecf20Sopenharmony_ci	struct ring_desc *less_rx;
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ci	less_rx = np->get_rx.orig;
18338c2ecf20Sopenharmony_ci	if (less_rx-- == np->rx_ring.orig)
18348c2ecf20Sopenharmony_ci		less_rx = np->last_rx.orig;
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci	while (np->put_rx.orig != less_rx) {
18378c2ecf20Sopenharmony_ci		struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz + NV_RX_ALLOC_PAD);
18388c2ecf20Sopenharmony_ci		if (likely(skb)) {
18398c2ecf20Sopenharmony_ci			np->put_rx_ctx->skb = skb;
18408c2ecf20Sopenharmony_ci			np->put_rx_ctx->dma = dma_map_single(&np->pci_dev->dev,
18418c2ecf20Sopenharmony_ci							     skb->data,
18428c2ecf20Sopenharmony_ci							     skb_tailroom(skb),
18438c2ecf20Sopenharmony_ci							     DMA_FROM_DEVICE);
18448c2ecf20Sopenharmony_ci			if (unlikely(dma_mapping_error(&np->pci_dev->dev,
18458c2ecf20Sopenharmony_ci						       np->put_rx_ctx->dma))) {
18468c2ecf20Sopenharmony_ci				kfree_skb(skb);
18478c2ecf20Sopenharmony_ci				goto packet_dropped;
18488c2ecf20Sopenharmony_ci			}
18498c2ecf20Sopenharmony_ci			np->put_rx_ctx->dma_len = skb_tailroom(skb);
18508c2ecf20Sopenharmony_ci			np->put_rx.orig->buf = cpu_to_le32(np->put_rx_ctx->dma);
18518c2ecf20Sopenharmony_ci			wmb();
18528c2ecf20Sopenharmony_ci			np->put_rx.orig->flaglen = cpu_to_le32(np->rx_buf_sz | NV_RX_AVAIL);
18538c2ecf20Sopenharmony_ci			if (unlikely(np->put_rx.orig++ == np->last_rx.orig))
18548c2ecf20Sopenharmony_ci				np->put_rx.orig = np->rx_ring.orig;
18558c2ecf20Sopenharmony_ci			if (unlikely(np->put_rx_ctx++ == np->last_rx_ctx))
18568c2ecf20Sopenharmony_ci				np->put_rx_ctx = np->rx_skb;
18578c2ecf20Sopenharmony_ci		} else {
18588c2ecf20Sopenharmony_cipacket_dropped:
18598c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_rx_syncp);
18608c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_rx_dropped);
18618c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_rx_syncp);
18628c2ecf20Sopenharmony_ci			return 1;
18638c2ecf20Sopenharmony_ci		}
18648c2ecf20Sopenharmony_ci	}
18658c2ecf20Sopenharmony_ci	return 0;
18668c2ecf20Sopenharmony_ci}
18678c2ecf20Sopenharmony_ci
18688c2ecf20Sopenharmony_cistatic int nv_alloc_rx_optimized(struct net_device *dev)
18698c2ecf20Sopenharmony_ci{
18708c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
18718c2ecf20Sopenharmony_ci	struct ring_desc_ex *less_rx;
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	less_rx = np->get_rx.ex;
18748c2ecf20Sopenharmony_ci	if (less_rx-- == np->rx_ring.ex)
18758c2ecf20Sopenharmony_ci		less_rx = np->last_rx.ex;
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci	while (np->put_rx.ex != less_rx) {
18788c2ecf20Sopenharmony_ci		struct sk_buff *skb = netdev_alloc_skb(dev, np->rx_buf_sz + NV_RX_ALLOC_PAD);
18798c2ecf20Sopenharmony_ci		if (likely(skb)) {
18808c2ecf20Sopenharmony_ci			np->put_rx_ctx->skb = skb;
18818c2ecf20Sopenharmony_ci			np->put_rx_ctx->dma = dma_map_single(&np->pci_dev->dev,
18828c2ecf20Sopenharmony_ci							     skb->data,
18838c2ecf20Sopenharmony_ci							     skb_tailroom(skb),
18848c2ecf20Sopenharmony_ci							     DMA_FROM_DEVICE);
18858c2ecf20Sopenharmony_ci			if (unlikely(dma_mapping_error(&np->pci_dev->dev,
18868c2ecf20Sopenharmony_ci						       np->put_rx_ctx->dma))) {
18878c2ecf20Sopenharmony_ci				kfree_skb(skb);
18888c2ecf20Sopenharmony_ci				goto packet_dropped;
18898c2ecf20Sopenharmony_ci			}
18908c2ecf20Sopenharmony_ci			np->put_rx_ctx->dma_len = skb_tailroom(skb);
18918c2ecf20Sopenharmony_ci			np->put_rx.ex->bufhigh = cpu_to_le32(dma_high(np->put_rx_ctx->dma));
18928c2ecf20Sopenharmony_ci			np->put_rx.ex->buflow = cpu_to_le32(dma_low(np->put_rx_ctx->dma));
18938c2ecf20Sopenharmony_ci			wmb();
18948c2ecf20Sopenharmony_ci			np->put_rx.ex->flaglen = cpu_to_le32(np->rx_buf_sz | NV_RX2_AVAIL);
18958c2ecf20Sopenharmony_ci			if (unlikely(np->put_rx.ex++ == np->last_rx.ex))
18968c2ecf20Sopenharmony_ci				np->put_rx.ex = np->rx_ring.ex;
18978c2ecf20Sopenharmony_ci			if (unlikely(np->put_rx_ctx++ == np->last_rx_ctx))
18988c2ecf20Sopenharmony_ci				np->put_rx_ctx = np->rx_skb;
18998c2ecf20Sopenharmony_ci		} else {
19008c2ecf20Sopenharmony_cipacket_dropped:
19018c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_rx_syncp);
19028c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_rx_dropped);
19038c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_rx_syncp);
19048c2ecf20Sopenharmony_ci			return 1;
19058c2ecf20Sopenharmony_ci		}
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci	return 0;
19088c2ecf20Sopenharmony_ci}
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci/* If rx bufs are exhausted called after 50ms to attempt to refresh */
19118c2ecf20Sopenharmony_cistatic void nv_do_rx_refill(struct timer_list *t)
19128c2ecf20Sopenharmony_ci{
19138c2ecf20Sopenharmony_ci	struct fe_priv *np = from_timer(np, t, oom_kick);
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci	/* Just reschedule NAPI rx processing */
19168c2ecf20Sopenharmony_ci	napi_schedule(&np->napi);
19178c2ecf20Sopenharmony_ci}
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_cistatic void nv_init_rx(struct net_device *dev)
19208c2ecf20Sopenharmony_ci{
19218c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
19228c2ecf20Sopenharmony_ci	int i;
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ci	np->get_rx = np->rx_ring;
19258c2ecf20Sopenharmony_ci	np->put_rx = np->rx_ring;
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci	if (!nv_optimized(np))
19288c2ecf20Sopenharmony_ci		np->last_rx.orig = &np->rx_ring.orig[np->rx_ring_size-1];
19298c2ecf20Sopenharmony_ci	else
19308c2ecf20Sopenharmony_ci		np->last_rx.ex = &np->rx_ring.ex[np->rx_ring_size-1];
19318c2ecf20Sopenharmony_ci	np->get_rx_ctx = np->rx_skb;
19328c2ecf20Sopenharmony_ci	np->put_rx_ctx = np->rx_skb;
19338c2ecf20Sopenharmony_ci	np->last_rx_ctx = &np->rx_skb[np->rx_ring_size-1];
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci	for (i = 0; i < np->rx_ring_size; i++) {
19368c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
19378c2ecf20Sopenharmony_ci			np->rx_ring.orig[i].flaglen = 0;
19388c2ecf20Sopenharmony_ci			np->rx_ring.orig[i].buf = 0;
19398c2ecf20Sopenharmony_ci		} else {
19408c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].flaglen = 0;
19418c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].txvlan = 0;
19428c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].bufhigh = 0;
19438c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].buflow = 0;
19448c2ecf20Sopenharmony_ci		}
19458c2ecf20Sopenharmony_ci		np->rx_skb[i].skb = NULL;
19468c2ecf20Sopenharmony_ci		np->rx_skb[i].dma = 0;
19478c2ecf20Sopenharmony_ci	}
19488c2ecf20Sopenharmony_ci}
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_cistatic void nv_init_tx(struct net_device *dev)
19518c2ecf20Sopenharmony_ci{
19528c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
19538c2ecf20Sopenharmony_ci	int i;
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci	np->get_tx = np->tx_ring;
19568c2ecf20Sopenharmony_ci	np->put_tx = np->tx_ring;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci	if (!nv_optimized(np))
19598c2ecf20Sopenharmony_ci		np->last_tx.orig = &np->tx_ring.orig[np->tx_ring_size-1];
19608c2ecf20Sopenharmony_ci	else
19618c2ecf20Sopenharmony_ci		np->last_tx.ex = &np->tx_ring.ex[np->tx_ring_size-1];
19628c2ecf20Sopenharmony_ci	np->get_tx_ctx = np->tx_skb;
19638c2ecf20Sopenharmony_ci	np->put_tx_ctx = np->tx_skb;
19648c2ecf20Sopenharmony_ci	np->last_tx_ctx = &np->tx_skb[np->tx_ring_size-1];
19658c2ecf20Sopenharmony_ci	netdev_reset_queue(np->dev);
19668c2ecf20Sopenharmony_ci	np->tx_pkts_in_progress = 0;
19678c2ecf20Sopenharmony_ci	np->tx_change_owner = NULL;
19688c2ecf20Sopenharmony_ci	np->tx_end_flip = NULL;
19698c2ecf20Sopenharmony_ci	np->tx_stop = 0;
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	for (i = 0; i < np->tx_ring_size; i++) {
19728c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
19738c2ecf20Sopenharmony_ci			np->tx_ring.orig[i].flaglen = 0;
19748c2ecf20Sopenharmony_ci			np->tx_ring.orig[i].buf = 0;
19758c2ecf20Sopenharmony_ci		} else {
19768c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].flaglen = 0;
19778c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].txvlan = 0;
19788c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].bufhigh = 0;
19798c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].buflow = 0;
19808c2ecf20Sopenharmony_ci		}
19818c2ecf20Sopenharmony_ci		np->tx_skb[i].skb = NULL;
19828c2ecf20Sopenharmony_ci		np->tx_skb[i].dma = 0;
19838c2ecf20Sopenharmony_ci		np->tx_skb[i].dma_len = 0;
19848c2ecf20Sopenharmony_ci		np->tx_skb[i].dma_single = 0;
19858c2ecf20Sopenharmony_ci		np->tx_skb[i].first_tx_desc = NULL;
19868c2ecf20Sopenharmony_ci		np->tx_skb[i].next_tx_ctx = NULL;
19878c2ecf20Sopenharmony_ci	}
19888c2ecf20Sopenharmony_ci}
19898c2ecf20Sopenharmony_ci
19908c2ecf20Sopenharmony_cistatic int nv_init_ring(struct net_device *dev)
19918c2ecf20Sopenharmony_ci{
19928c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_ci	nv_init_tx(dev);
19958c2ecf20Sopenharmony_ci	nv_init_rx(dev);
19968c2ecf20Sopenharmony_ci
19978c2ecf20Sopenharmony_ci	if (!nv_optimized(np))
19988c2ecf20Sopenharmony_ci		return nv_alloc_rx(dev);
19998c2ecf20Sopenharmony_ci	else
20008c2ecf20Sopenharmony_ci		return nv_alloc_rx_optimized(dev);
20018c2ecf20Sopenharmony_ci}
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_cistatic void nv_unmap_txskb(struct fe_priv *np, struct nv_skb_map *tx_skb)
20048c2ecf20Sopenharmony_ci{
20058c2ecf20Sopenharmony_ci	if (tx_skb->dma) {
20068c2ecf20Sopenharmony_ci		if (tx_skb->dma_single)
20078c2ecf20Sopenharmony_ci			dma_unmap_single(&np->pci_dev->dev, tx_skb->dma,
20088c2ecf20Sopenharmony_ci					 tx_skb->dma_len,
20098c2ecf20Sopenharmony_ci					 DMA_TO_DEVICE);
20108c2ecf20Sopenharmony_ci		else
20118c2ecf20Sopenharmony_ci			dma_unmap_page(&np->pci_dev->dev, tx_skb->dma,
20128c2ecf20Sopenharmony_ci				       tx_skb->dma_len,
20138c2ecf20Sopenharmony_ci				       DMA_TO_DEVICE);
20148c2ecf20Sopenharmony_ci		tx_skb->dma = 0;
20158c2ecf20Sopenharmony_ci	}
20168c2ecf20Sopenharmony_ci}
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_cistatic int nv_release_txskb(struct fe_priv *np, struct nv_skb_map *tx_skb)
20198c2ecf20Sopenharmony_ci{
20208c2ecf20Sopenharmony_ci	nv_unmap_txskb(np, tx_skb);
20218c2ecf20Sopenharmony_ci	if (tx_skb->skb) {
20228c2ecf20Sopenharmony_ci		dev_kfree_skb_any(tx_skb->skb);
20238c2ecf20Sopenharmony_ci		tx_skb->skb = NULL;
20248c2ecf20Sopenharmony_ci		return 1;
20258c2ecf20Sopenharmony_ci	}
20268c2ecf20Sopenharmony_ci	return 0;
20278c2ecf20Sopenharmony_ci}
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_cistatic void nv_drain_tx(struct net_device *dev)
20308c2ecf20Sopenharmony_ci{
20318c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
20328c2ecf20Sopenharmony_ci	unsigned int i;
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci	for (i = 0; i < np->tx_ring_size; i++) {
20358c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
20368c2ecf20Sopenharmony_ci			np->tx_ring.orig[i].flaglen = 0;
20378c2ecf20Sopenharmony_ci			np->tx_ring.orig[i].buf = 0;
20388c2ecf20Sopenharmony_ci		} else {
20398c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].flaglen = 0;
20408c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].txvlan = 0;
20418c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].bufhigh = 0;
20428c2ecf20Sopenharmony_ci			np->tx_ring.ex[i].buflow = 0;
20438c2ecf20Sopenharmony_ci		}
20448c2ecf20Sopenharmony_ci		if (nv_release_txskb(np, &np->tx_skb[i])) {
20458c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_tx_syncp);
20468c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_tx_dropped);
20478c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_tx_syncp);
20488c2ecf20Sopenharmony_ci		}
20498c2ecf20Sopenharmony_ci		np->tx_skb[i].dma = 0;
20508c2ecf20Sopenharmony_ci		np->tx_skb[i].dma_len = 0;
20518c2ecf20Sopenharmony_ci		np->tx_skb[i].dma_single = 0;
20528c2ecf20Sopenharmony_ci		np->tx_skb[i].first_tx_desc = NULL;
20538c2ecf20Sopenharmony_ci		np->tx_skb[i].next_tx_ctx = NULL;
20548c2ecf20Sopenharmony_ci	}
20558c2ecf20Sopenharmony_ci	np->tx_pkts_in_progress = 0;
20568c2ecf20Sopenharmony_ci	np->tx_change_owner = NULL;
20578c2ecf20Sopenharmony_ci	np->tx_end_flip = NULL;
20588c2ecf20Sopenharmony_ci}
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_cistatic void nv_drain_rx(struct net_device *dev)
20618c2ecf20Sopenharmony_ci{
20628c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
20638c2ecf20Sopenharmony_ci	int i;
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	for (i = 0; i < np->rx_ring_size; i++) {
20668c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
20678c2ecf20Sopenharmony_ci			np->rx_ring.orig[i].flaglen = 0;
20688c2ecf20Sopenharmony_ci			np->rx_ring.orig[i].buf = 0;
20698c2ecf20Sopenharmony_ci		} else {
20708c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].flaglen = 0;
20718c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].txvlan = 0;
20728c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].bufhigh = 0;
20738c2ecf20Sopenharmony_ci			np->rx_ring.ex[i].buflow = 0;
20748c2ecf20Sopenharmony_ci		}
20758c2ecf20Sopenharmony_ci		wmb();
20768c2ecf20Sopenharmony_ci		if (np->rx_skb[i].skb) {
20778c2ecf20Sopenharmony_ci			dma_unmap_single(&np->pci_dev->dev, np->rx_skb[i].dma,
20788c2ecf20Sopenharmony_ci					 (skb_end_pointer(np->rx_skb[i].skb) -
20798c2ecf20Sopenharmony_ci					 np->rx_skb[i].skb->data),
20808c2ecf20Sopenharmony_ci					 DMA_FROM_DEVICE);
20818c2ecf20Sopenharmony_ci			dev_kfree_skb(np->rx_skb[i].skb);
20828c2ecf20Sopenharmony_ci			np->rx_skb[i].skb = NULL;
20838c2ecf20Sopenharmony_ci		}
20848c2ecf20Sopenharmony_ci	}
20858c2ecf20Sopenharmony_ci}
20868c2ecf20Sopenharmony_ci
20878c2ecf20Sopenharmony_cistatic void nv_drain_rxtx(struct net_device *dev)
20888c2ecf20Sopenharmony_ci{
20898c2ecf20Sopenharmony_ci	nv_drain_tx(dev);
20908c2ecf20Sopenharmony_ci	nv_drain_rx(dev);
20918c2ecf20Sopenharmony_ci}
20928c2ecf20Sopenharmony_ci
20938c2ecf20Sopenharmony_cistatic inline u32 nv_get_empty_tx_slots(struct fe_priv *np)
20948c2ecf20Sopenharmony_ci{
20958c2ecf20Sopenharmony_ci	return (u32)(np->tx_ring_size - ((np->tx_ring_size + (np->put_tx_ctx - np->get_tx_ctx)) % np->tx_ring_size));
20968c2ecf20Sopenharmony_ci}
20978c2ecf20Sopenharmony_ci
20988c2ecf20Sopenharmony_cistatic void nv_legacybackoff_reseed(struct net_device *dev)
20998c2ecf20Sopenharmony_ci{
21008c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
21018c2ecf20Sopenharmony_ci	u32 reg;
21028c2ecf20Sopenharmony_ci	u32 low;
21038c2ecf20Sopenharmony_ci	int tx_status = 0;
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	reg = readl(base + NvRegSlotTime) & ~NVREG_SLOTTIME_MASK;
21068c2ecf20Sopenharmony_ci	get_random_bytes(&low, sizeof(low));
21078c2ecf20Sopenharmony_ci	reg |= low & NVREG_SLOTTIME_MASK;
21088c2ecf20Sopenharmony_ci
21098c2ecf20Sopenharmony_ci	/* Need to stop tx before change takes effect.
21108c2ecf20Sopenharmony_ci	 * Caller has already gained np->lock.
21118c2ecf20Sopenharmony_ci	 */
21128c2ecf20Sopenharmony_ci	tx_status = readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_START;
21138c2ecf20Sopenharmony_ci	if (tx_status)
21148c2ecf20Sopenharmony_ci		nv_stop_tx(dev);
21158c2ecf20Sopenharmony_ci	nv_stop_rx(dev);
21168c2ecf20Sopenharmony_ci	writel(reg, base + NvRegSlotTime);
21178c2ecf20Sopenharmony_ci	if (tx_status)
21188c2ecf20Sopenharmony_ci		nv_start_tx(dev);
21198c2ecf20Sopenharmony_ci	nv_start_rx(dev);
21208c2ecf20Sopenharmony_ci}
21218c2ecf20Sopenharmony_ci
21228c2ecf20Sopenharmony_ci/* Gear Backoff Seeds */
21238c2ecf20Sopenharmony_ci#define BACKOFF_SEEDSET_ROWS	8
21248c2ecf20Sopenharmony_ci#define BACKOFF_SEEDSET_LFSRS	15
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci/* Known Good seed sets */
21278c2ecf20Sopenharmony_cistatic const u32 main_seedset[BACKOFF_SEEDSET_ROWS][BACKOFF_SEEDSET_LFSRS] = {
21288c2ecf20Sopenharmony_ci	{145, 155, 165, 175, 185, 196, 235, 245, 255, 265, 275, 285, 660, 690, 874},
21298c2ecf20Sopenharmony_ci	{245, 255, 265, 575, 385, 298, 335, 345, 355, 366, 375, 385, 761, 790, 974},
21308c2ecf20Sopenharmony_ci	{145, 155, 165, 175, 185, 196, 235, 245, 255, 265, 275, 285, 660, 690, 874},
21318c2ecf20Sopenharmony_ci	{245, 255, 265, 575, 385, 298, 335, 345, 355, 366, 375, 386, 761, 790, 974},
21328c2ecf20Sopenharmony_ci	{266, 265, 276, 585, 397, 208, 345, 355, 365, 376, 385, 396, 771, 700, 984},
21338c2ecf20Sopenharmony_ci	{266, 265, 276, 586, 397, 208, 346, 355, 365, 376, 285, 396, 771, 700, 984},
21348c2ecf20Sopenharmony_ci	{366, 365, 376, 686, 497, 308, 447, 455, 466, 476, 485, 496, 871, 800,  84},
21358c2ecf20Sopenharmony_ci	{466, 465, 476, 786, 597, 408, 547, 555, 566, 576, 585, 597, 971, 900, 184} };
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_cistatic const u32 gear_seedset[BACKOFF_SEEDSET_ROWS][BACKOFF_SEEDSET_LFSRS] = {
21388c2ecf20Sopenharmony_ci	{251, 262, 273, 324, 319, 508, 375, 364, 341, 371, 398, 193, 375,  30, 295},
21398c2ecf20Sopenharmony_ci	{351, 375, 373, 469, 551, 639, 477, 464, 441, 472, 498, 293, 476, 130, 395},
21408c2ecf20Sopenharmony_ci	{351, 375, 373, 469, 551, 639, 477, 464, 441, 472, 498, 293, 476, 130, 397},
21418c2ecf20Sopenharmony_ci	{251, 262, 273, 324, 319, 508, 375, 364, 341, 371, 398, 193, 375,  30, 295},
21428c2ecf20Sopenharmony_ci	{251, 262, 273, 324, 319, 508, 375, 364, 341, 371, 398, 193, 375,  30, 295},
21438c2ecf20Sopenharmony_ci	{351, 375, 373, 469, 551, 639, 477, 464, 441, 472, 498, 293, 476, 130, 395},
21448c2ecf20Sopenharmony_ci	{351, 375, 373, 469, 551, 639, 477, 464, 441, 472, 498, 293, 476, 130, 395},
21458c2ecf20Sopenharmony_ci	{351, 375, 373, 469, 551, 639, 477, 464, 441, 472, 498, 293, 476, 130, 395} };
21468c2ecf20Sopenharmony_ci
21478c2ecf20Sopenharmony_cistatic void nv_gear_backoff_reseed(struct net_device *dev)
21488c2ecf20Sopenharmony_ci{
21498c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
21508c2ecf20Sopenharmony_ci	u32 miniseed1, miniseed2, miniseed2_reversed, miniseed3, miniseed3_reversed;
21518c2ecf20Sopenharmony_ci	u32 temp, seedset, combinedSeed;
21528c2ecf20Sopenharmony_ci	int i;
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_ci	/* Setup seed for free running LFSR */
21558c2ecf20Sopenharmony_ci	/* We are going to read the time stamp counter 3 times
21568c2ecf20Sopenharmony_ci	   and swizzle bits around to increase randomness */
21578c2ecf20Sopenharmony_ci	get_random_bytes(&miniseed1, sizeof(miniseed1));
21588c2ecf20Sopenharmony_ci	miniseed1 &= 0x0fff;
21598c2ecf20Sopenharmony_ci	if (miniseed1 == 0)
21608c2ecf20Sopenharmony_ci		miniseed1 = 0xabc;
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_ci	get_random_bytes(&miniseed2, sizeof(miniseed2));
21638c2ecf20Sopenharmony_ci	miniseed2 &= 0x0fff;
21648c2ecf20Sopenharmony_ci	if (miniseed2 == 0)
21658c2ecf20Sopenharmony_ci		miniseed2 = 0xabc;
21668c2ecf20Sopenharmony_ci	miniseed2_reversed =
21678c2ecf20Sopenharmony_ci		((miniseed2 & 0xF00) >> 8) |
21688c2ecf20Sopenharmony_ci		 (miniseed2 & 0x0F0) |
21698c2ecf20Sopenharmony_ci		 ((miniseed2 & 0x00F) << 8);
21708c2ecf20Sopenharmony_ci
21718c2ecf20Sopenharmony_ci	get_random_bytes(&miniseed3, sizeof(miniseed3));
21728c2ecf20Sopenharmony_ci	miniseed3 &= 0x0fff;
21738c2ecf20Sopenharmony_ci	if (miniseed3 == 0)
21748c2ecf20Sopenharmony_ci		miniseed3 = 0xabc;
21758c2ecf20Sopenharmony_ci	miniseed3_reversed =
21768c2ecf20Sopenharmony_ci		((miniseed3 & 0xF00) >> 8) |
21778c2ecf20Sopenharmony_ci		 (miniseed3 & 0x0F0) |
21788c2ecf20Sopenharmony_ci		 ((miniseed3 & 0x00F) << 8);
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	combinedSeed = ((miniseed1 ^ miniseed2_reversed) << 12) |
21818c2ecf20Sopenharmony_ci		       (miniseed2 ^ miniseed3_reversed);
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	/* Seeds can not be zero */
21848c2ecf20Sopenharmony_ci	if ((combinedSeed & NVREG_BKOFFCTRL_SEED_MASK) == 0)
21858c2ecf20Sopenharmony_ci		combinedSeed |= 0x08;
21868c2ecf20Sopenharmony_ci	if ((combinedSeed & (NVREG_BKOFFCTRL_SEED_MASK << NVREG_BKOFFCTRL_GEAR)) == 0)
21878c2ecf20Sopenharmony_ci		combinedSeed |= 0x8000;
21888c2ecf20Sopenharmony_ci
21898c2ecf20Sopenharmony_ci	/* No need to disable tx here */
21908c2ecf20Sopenharmony_ci	temp = NVREG_BKOFFCTRL_DEFAULT | (0 << NVREG_BKOFFCTRL_SELECT);
21918c2ecf20Sopenharmony_ci	temp |= combinedSeed & NVREG_BKOFFCTRL_SEED_MASK;
21928c2ecf20Sopenharmony_ci	temp |= combinedSeed >> NVREG_BKOFFCTRL_GEAR;
21938c2ecf20Sopenharmony_ci	writel(temp, base + NvRegBackOffControl);
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	/* Setup seeds for all gear LFSRs. */
21968c2ecf20Sopenharmony_ci	get_random_bytes(&seedset, sizeof(seedset));
21978c2ecf20Sopenharmony_ci	seedset = seedset % BACKOFF_SEEDSET_ROWS;
21988c2ecf20Sopenharmony_ci	for (i = 1; i <= BACKOFF_SEEDSET_LFSRS; i++) {
21998c2ecf20Sopenharmony_ci		temp = NVREG_BKOFFCTRL_DEFAULT | (i << NVREG_BKOFFCTRL_SELECT);
22008c2ecf20Sopenharmony_ci		temp |= main_seedset[seedset][i-1] & 0x3ff;
22018c2ecf20Sopenharmony_ci		temp |= ((gear_seedset[seedset][i-1] & 0x3ff) << NVREG_BKOFFCTRL_GEAR);
22028c2ecf20Sopenharmony_ci		writel(temp, base + NvRegBackOffControl);
22038c2ecf20Sopenharmony_ci	}
22048c2ecf20Sopenharmony_ci}
22058c2ecf20Sopenharmony_ci
22068c2ecf20Sopenharmony_ci/*
22078c2ecf20Sopenharmony_ci * nv_start_xmit: dev->hard_start_xmit function
22088c2ecf20Sopenharmony_ci * Called with netif_tx_lock held.
22098c2ecf20Sopenharmony_ci */
22108c2ecf20Sopenharmony_cistatic netdev_tx_t nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
22118c2ecf20Sopenharmony_ci{
22128c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
22138c2ecf20Sopenharmony_ci	u32 tx_flags = 0;
22148c2ecf20Sopenharmony_ci	u32 tx_flags_extra = (np->desc_ver == DESC_VER_1 ? NV_TX_LASTPACKET : NV_TX2_LASTPACKET);
22158c2ecf20Sopenharmony_ci	unsigned int fragments = skb_shinfo(skb)->nr_frags;
22168c2ecf20Sopenharmony_ci	unsigned int i;
22178c2ecf20Sopenharmony_ci	u32 offset = 0;
22188c2ecf20Sopenharmony_ci	u32 bcnt;
22198c2ecf20Sopenharmony_ci	u32 size = skb_headlen(skb);
22208c2ecf20Sopenharmony_ci	u32 entries = (size >> NV_TX2_TSO_MAX_SHIFT) + ((size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
22218c2ecf20Sopenharmony_ci	u32 empty_slots;
22228c2ecf20Sopenharmony_ci	struct ring_desc *put_tx;
22238c2ecf20Sopenharmony_ci	struct ring_desc *start_tx;
22248c2ecf20Sopenharmony_ci	struct ring_desc *prev_tx;
22258c2ecf20Sopenharmony_ci	struct nv_skb_map *prev_tx_ctx;
22268c2ecf20Sopenharmony_ci	struct nv_skb_map *tmp_tx_ctx = NULL, *start_tx_ctx = NULL;
22278c2ecf20Sopenharmony_ci	unsigned long flags;
22288c2ecf20Sopenharmony_ci	netdev_tx_t ret = NETDEV_TX_OK;
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_ci	/* add fragments to entries count */
22318c2ecf20Sopenharmony_ci	for (i = 0; i < fragments; i++) {
22328c2ecf20Sopenharmony_ci		u32 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci		entries += (frag_size >> NV_TX2_TSO_MAX_SHIFT) +
22358c2ecf20Sopenharmony_ci			   ((frag_size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
22368c2ecf20Sopenharmony_ci	}
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
22398c2ecf20Sopenharmony_ci	empty_slots = nv_get_empty_tx_slots(np);
22408c2ecf20Sopenharmony_ci	if (unlikely(empty_slots <= entries)) {
22418c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
22428c2ecf20Sopenharmony_ci		np->tx_stop = 1;
22438c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ci		/* When normal packets and/or xmit_more packets fill up
22468c2ecf20Sopenharmony_ci		 * tx_desc, it is necessary to trigger NIC tx reg.
22478c2ecf20Sopenharmony_ci		 */
22488c2ecf20Sopenharmony_ci		ret = NETDEV_TX_BUSY;
22498c2ecf20Sopenharmony_ci		goto txkick;
22508c2ecf20Sopenharmony_ci	}
22518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&np->lock, flags);
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_ci	start_tx = put_tx = np->put_tx.orig;
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	/* setup the header buffer */
22568c2ecf20Sopenharmony_ci	do {
22578c2ecf20Sopenharmony_ci		bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
22588c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma = dma_map_single(&np->pci_dev->dev,
22598c2ecf20Sopenharmony_ci						     skb->data + offset, bcnt,
22608c2ecf20Sopenharmony_ci						     DMA_TO_DEVICE);
22618c2ecf20Sopenharmony_ci		if (unlikely(dma_mapping_error(&np->pci_dev->dev,
22628c2ecf20Sopenharmony_ci					       np->put_tx_ctx->dma))) {
22638c2ecf20Sopenharmony_ci			/* on DMA mapping error - drop the packet */
22648c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
22658c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_tx_syncp);
22668c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_tx_dropped);
22678c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_tx_syncp);
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci			ret = NETDEV_TX_OK;
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci			goto dma_error;
22728c2ecf20Sopenharmony_ci		}
22738c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma_len = bcnt;
22748c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma_single = 1;
22758c2ecf20Sopenharmony_ci		put_tx->buf = cpu_to_le32(np->put_tx_ctx->dma);
22768c2ecf20Sopenharmony_ci		put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_ci		tx_flags = np->tx_flags;
22798c2ecf20Sopenharmony_ci		offset += bcnt;
22808c2ecf20Sopenharmony_ci		size -= bcnt;
22818c2ecf20Sopenharmony_ci		if (unlikely(put_tx++ == np->last_tx.orig))
22828c2ecf20Sopenharmony_ci			put_tx = np->tx_ring.orig;
22838c2ecf20Sopenharmony_ci		if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
22848c2ecf20Sopenharmony_ci			np->put_tx_ctx = np->tx_skb;
22858c2ecf20Sopenharmony_ci	} while (size);
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_ci	/* setup the fragments */
22888c2ecf20Sopenharmony_ci	for (i = 0; i < fragments; i++) {
22898c2ecf20Sopenharmony_ci		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
22908c2ecf20Sopenharmony_ci		u32 frag_size = skb_frag_size(frag);
22918c2ecf20Sopenharmony_ci		offset = 0;
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci		do {
22948c2ecf20Sopenharmony_ci			if (!start_tx_ctx)
22958c2ecf20Sopenharmony_ci				start_tx_ctx = tmp_tx_ctx = np->put_tx_ctx;
22968c2ecf20Sopenharmony_ci
22978c2ecf20Sopenharmony_ci			bcnt = (frag_size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : frag_size;
22988c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma = skb_frag_dma_map(
22998c2ecf20Sopenharmony_ci							&np->pci_dev->dev,
23008c2ecf20Sopenharmony_ci							frag, offset,
23018c2ecf20Sopenharmony_ci							bcnt,
23028c2ecf20Sopenharmony_ci							DMA_TO_DEVICE);
23038c2ecf20Sopenharmony_ci			if (unlikely(dma_mapping_error(&np->pci_dev->dev,
23048c2ecf20Sopenharmony_ci						       np->put_tx_ctx->dma))) {
23058c2ecf20Sopenharmony_ci
23068c2ecf20Sopenharmony_ci				/* Unwind the mapped fragments */
23078c2ecf20Sopenharmony_ci				do {
23088c2ecf20Sopenharmony_ci					nv_unmap_txskb(np, start_tx_ctx);
23098c2ecf20Sopenharmony_ci					if (unlikely(tmp_tx_ctx++ == np->last_tx_ctx))
23108c2ecf20Sopenharmony_ci						tmp_tx_ctx = np->tx_skb;
23118c2ecf20Sopenharmony_ci				} while (tmp_tx_ctx != np->put_tx_ctx);
23128c2ecf20Sopenharmony_ci				dev_kfree_skb_any(skb);
23138c2ecf20Sopenharmony_ci				np->put_tx_ctx = start_tx_ctx;
23148c2ecf20Sopenharmony_ci				u64_stats_update_begin(&np->swstats_tx_syncp);
23158c2ecf20Sopenharmony_ci				nv_txrx_stats_inc(stat_tx_dropped);
23168c2ecf20Sopenharmony_ci				u64_stats_update_end(&np->swstats_tx_syncp);
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ci				ret = NETDEV_TX_OK;
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci				goto dma_error;
23218c2ecf20Sopenharmony_ci			}
23228c2ecf20Sopenharmony_ci
23238c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma_len = bcnt;
23248c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma_single = 0;
23258c2ecf20Sopenharmony_ci			put_tx->buf = cpu_to_le32(np->put_tx_ctx->dma);
23268c2ecf20Sopenharmony_ci			put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci			offset += bcnt;
23298c2ecf20Sopenharmony_ci			frag_size -= bcnt;
23308c2ecf20Sopenharmony_ci			if (unlikely(put_tx++ == np->last_tx.orig))
23318c2ecf20Sopenharmony_ci				put_tx = np->tx_ring.orig;
23328c2ecf20Sopenharmony_ci			if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
23338c2ecf20Sopenharmony_ci				np->put_tx_ctx = np->tx_skb;
23348c2ecf20Sopenharmony_ci		} while (frag_size);
23358c2ecf20Sopenharmony_ci	}
23368c2ecf20Sopenharmony_ci
23378c2ecf20Sopenharmony_ci	if (unlikely(put_tx == np->tx_ring.orig))
23388c2ecf20Sopenharmony_ci		prev_tx = np->last_tx.orig;
23398c2ecf20Sopenharmony_ci	else
23408c2ecf20Sopenharmony_ci		prev_tx = put_tx - 1;
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci	if (unlikely(np->put_tx_ctx == np->tx_skb))
23438c2ecf20Sopenharmony_ci		prev_tx_ctx = np->last_tx_ctx;
23448c2ecf20Sopenharmony_ci	else
23458c2ecf20Sopenharmony_ci		prev_tx_ctx = np->put_tx_ctx - 1;
23468c2ecf20Sopenharmony_ci
23478c2ecf20Sopenharmony_ci	/* set last fragment flag  */
23488c2ecf20Sopenharmony_ci	prev_tx->flaglen |= cpu_to_le32(tx_flags_extra);
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci	/* save skb in this slot's context area */
23518c2ecf20Sopenharmony_ci	prev_tx_ctx->skb = skb;
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci	if (skb_is_gso(skb))
23548c2ecf20Sopenharmony_ci		tx_flags_extra = NV_TX2_TSO | (skb_shinfo(skb)->gso_size << NV_TX2_TSO_SHIFT);
23558c2ecf20Sopenharmony_ci	else
23568c2ecf20Sopenharmony_ci		tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
23578c2ecf20Sopenharmony_ci			 NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	/* set tx flags */
23628c2ecf20Sopenharmony_ci	start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
23638c2ecf20Sopenharmony_ci
23648c2ecf20Sopenharmony_ci	netdev_sent_queue(np->dev, skb->len);
23658c2ecf20Sopenharmony_ci
23668c2ecf20Sopenharmony_ci	skb_tx_timestamp(skb);
23678c2ecf20Sopenharmony_ci
23688c2ecf20Sopenharmony_ci	np->put_tx.orig = put_tx;
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&np->lock, flags);
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_citxkick:
23738c2ecf20Sopenharmony_ci	if (netif_queue_stopped(dev) || !netdev_xmit_more()) {
23748c2ecf20Sopenharmony_ci		u32 txrxctl_kick;
23758c2ecf20Sopenharmony_cidma_error:
23768c2ecf20Sopenharmony_ci		txrxctl_kick = NVREG_TXRXCTL_KICK | np->txrxctl_bits;
23778c2ecf20Sopenharmony_ci		writel(txrxctl_kick, get_hwbase(dev) + NvRegTxRxControl);
23788c2ecf20Sopenharmony_ci	}
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci	return ret;
23818c2ecf20Sopenharmony_ci}
23828c2ecf20Sopenharmony_ci
23838c2ecf20Sopenharmony_cistatic netdev_tx_t nv_start_xmit_optimized(struct sk_buff *skb,
23848c2ecf20Sopenharmony_ci					   struct net_device *dev)
23858c2ecf20Sopenharmony_ci{
23868c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
23878c2ecf20Sopenharmony_ci	u32 tx_flags = 0;
23888c2ecf20Sopenharmony_ci	u32 tx_flags_extra;
23898c2ecf20Sopenharmony_ci	unsigned int fragments = skb_shinfo(skb)->nr_frags;
23908c2ecf20Sopenharmony_ci	unsigned int i;
23918c2ecf20Sopenharmony_ci	u32 offset = 0;
23928c2ecf20Sopenharmony_ci	u32 bcnt;
23938c2ecf20Sopenharmony_ci	u32 size = skb_headlen(skb);
23948c2ecf20Sopenharmony_ci	u32 entries = (size >> NV_TX2_TSO_MAX_SHIFT) + ((size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
23958c2ecf20Sopenharmony_ci	u32 empty_slots;
23968c2ecf20Sopenharmony_ci	struct ring_desc_ex *put_tx;
23978c2ecf20Sopenharmony_ci	struct ring_desc_ex *start_tx;
23988c2ecf20Sopenharmony_ci	struct ring_desc_ex *prev_tx;
23998c2ecf20Sopenharmony_ci	struct nv_skb_map *prev_tx_ctx;
24008c2ecf20Sopenharmony_ci	struct nv_skb_map *start_tx_ctx = NULL;
24018c2ecf20Sopenharmony_ci	struct nv_skb_map *tmp_tx_ctx = NULL;
24028c2ecf20Sopenharmony_ci	unsigned long flags;
24038c2ecf20Sopenharmony_ci	netdev_tx_t ret = NETDEV_TX_OK;
24048c2ecf20Sopenharmony_ci
24058c2ecf20Sopenharmony_ci	/* add fragments to entries count */
24068c2ecf20Sopenharmony_ci	for (i = 0; i < fragments; i++) {
24078c2ecf20Sopenharmony_ci		u32 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
24088c2ecf20Sopenharmony_ci
24098c2ecf20Sopenharmony_ci		entries += (frag_size >> NV_TX2_TSO_MAX_SHIFT) +
24108c2ecf20Sopenharmony_ci			   ((frag_size & (NV_TX2_TSO_MAX_SIZE-1)) ? 1 : 0);
24118c2ecf20Sopenharmony_ci	}
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
24148c2ecf20Sopenharmony_ci	empty_slots = nv_get_empty_tx_slots(np);
24158c2ecf20Sopenharmony_ci	if (unlikely(empty_slots <= entries)) {
24168c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
24178c2ecf20Sopenharmony_ci		np->tx_stop = 1;
24188c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci		/* When normal packets and/or xmit_more packets fill up
24218c2ecf20Sopenharmony_ci		 * tx_desc, it is necessary to trigger NIC tx reg.
24228c2ecf20Sopenharmony_ci		 */
24238c2ecf20Sopenharmony_ci		ret = NETDEV_TX_BUSY;
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_ci		goto txkick;
24268c2ecf20Sopenharmony_ci	}
24278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&np->lock, flags);
24288c2ecf20Sopenharmony_ci
24298c2ecf20Sopenharmony_ci	start_tx = put_tx = np->put_tx.ex;
24308c2ecf20Sopenharmony_ci	start_tx_ctx = np->put_tx_ctx;
24318c2ecf20Sopenharmony_ci
24328c2ecf20Sopenharmony_ci	/* setup the header buffer */
24338c2ecf20Sopenharmony_ci	do {
24348c2ecf20Sopenharmony_ci		bcnt = (size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : size;
24358c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma = dma_map_single(&np->pci_dev->dev,
24368c2ecf20Sopenharmony_ci						     skb->data + offset, bcnt,
24378c2ecf20Sopenharmony_ci						     DMA_TO_DEVICE);
24388c2ecf20Sopenharmony_ci		if (unlikely(dma_mapping_error(&np->pci_dev->dev,
24398c2ecf20Sopenharmony_ci					       np->put_tx_ctx->dma))) {
24408c2ecf20Sopenharmony_ci			/* on DMA mapping error - drop the packet */
24418c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
24428c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_tx_syncp);
24438c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_tx_dropped);
24448c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_tx_syncp);
24458c2ecf20Sopenharmony_ci
24468c2ecf20Sopenharmony_ci			ret = NETDEV_TX_OK;
24478c2ecf20Sopenharmony_ci
24488c2ecf20Sopenharmony_ci			goto dma_error;
24498c2ecf20Sopenharmony_ci		}
24508c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma_len = bcnt;
24518c2ecf20Sopenharmony_ci		np->put_tx_ctx->dma_single = 1;
24528c2ecf20Sopenharmony_ci		put_tx->bufhigh = cpu_to_le32(dma_high(np->put_tx_ctx->dma));
24538c2ecf20Sopenharmony_ci		put_tx->buflow = cpu_to_le32(dma_low(np->put_tx_ctx->dma));
24548c2ecf20Sopenharmony_ci		put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
24558c2ecf20Sopenharmony_ci
24568c2ecf20Sopenharmony_ci		tx_flags = NV_TX2_VALID;
24578c2ecf20Sopenharmony_ci		offset += bcnt;
24588c2ecf20Sopenharmony_ci		size -= bcnt;
24598c2ecf20Sopenharmony_ci		if (unlikely(put_tx++ == np->last_tx.ex))
24608c2ecf20Sopenharmony_ci			put_tx = np->tx_ring.ex;
24618c2ecf20Sopenharmony_ci		if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
24628c2ecf20Sopenharmony_ci			np->put_tx_ctx = np->tx_skb;
24638c2ecf20Sopenharmony_ci	} while (size);
24648c2ecf20Sopenharmony_ci
24658c2ecf20Sopenharmony_ci	/* setup the fragments */
24668c2ecf20Sopenharmony_ci	for (i = 0; i < fragments; i++) {
24678c2ecf20Sopenharmony_ci		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
24688c2ecf20Sopenharmony_ci		u32 frag_size = skb_frag_size(frag);
24698c2ecf20Sopenharmony_ci		offset = 0;
24708c2ecf20Sopenharmony_ci
24718c2ecf20Sopenharmony_ci		do {
24728c2ecf20Sopenharmony_ci			bcnt = (frag_size > NV_TX2_TSO_MAX_SIZE) ? NV_TX2_TSO_MAX_SIZE : frag_size;
24738c2ecf20Sopenharmony_ci			if (!start_tx_ctx)
24748c2ecf20Sopenharmony_ci				start_tx_ctx = tmp_tx_ctx = np->put_tx_ctx;
24758c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma = skb_frag_dma_map(
24768c2ecf20Sopenharmony_ci							&np->pci_dev->dev,
24778c2ecf20Sopenharmony_ci							frag, offset,
24788c2ecf20Sopenharmony_ci							bcnt,
24798c2ecf20Sopenharmony_ci							DMA_TO_DEVICE);
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci			if (unlikely(dma_mapping_error(&np->pci_dev->dev,
24828c2ecf20Sopenharmony_ci						       np->put_tx_ctx->dma))) {
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci				/* Unwind the mapped fragments */
24858c2ecf20Sopenharmony_ci				do {
24868c2ecf20Sopenharmony_ci					nv_unmap_txskb(np, start_tx_ctx);
24878c2ecf20Sopenharmony_ci					if (unlikely(tmp_tx_ctx++ == np->last_tx_ctx))
24888c2ecf20Sopenharmony_ci						tmp_tx_ctx = np->tx_skb;
24898c2ecf20Sopenharmony_ci				} while (tmp_tx_ctx != np->put_tx_ctx);
24908c2ecf20Sopenharmony_ci				dev_kfree_skb_any(skb);
24918c2ecf20Sopenharmony_ci				np->put_tx_ctx = start_tx_ctx;
24928c2ecf20Sopenharmony_ci				u64_stats_update_begin(&np->swstats_tx_syncp);
24938c2ecf20Sopenharmony_ci				nv_txrx_stats_inc(stat_tx_dropped);
24948c2ecf20Sopenharmony_ci				u64_stats_update_end(&np->swstats_tx_syncp);
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_ci				ret = NETDEV_TX_OK;
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci				goto dma_error;
24998c2ecf20Sopenharmony_ci			}
25008c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma_len = bcnt;
25018c2ecf20Sopenharmony_ci			np->put_tx_ctx->dma_single = 0;
25028c2ecf20Sopenharmony_ci			put_tx->bufhigh = cpu_to_le32(dma_high(np->put_tx_ctx->dma));
25038c2ecf20Sopenharmony_ci			put_tx->buflow = cpu_to_le32(dma_low(np->put_tx_ctx->dma));
25048c2ecf20Sopenharmony_ci			put_tx->flaglen = cpu_to_le32((bcnt-1) | tx_flags);
25058c2ecf20Sopenharmony_ci
25068c2ecf20Sopenharmony_ci			offset += bcnt;
25078c2ecf20Sopenharmony_ci			frag_size -= bcnt;
25088c2ecf20Sopenharmony_ci			if (unlikely(put_tx++ == np->last_tx.ex))
25098c2ecf20Sopenharmony_ci				put_tx = np->tx_ring.ex;
25108c2ecf20Sopenharmony_ci			if (unlikely(np->put_tx_ctx++ == np->last_tx_ctx))
25118c2ecf20Sopenharmony_ci				np->put_tx_ctx = np->tx_skb;
25128c2ecf20Sopenharmony_ci		} while (frag_size);
25138c2ecf20Sopenharmony_ci	}
25148c2ecf20Sopenharmony_ci
25158c2ecf20Sopenharmony_ci	if (unlikely(put_tx == np->tx_ring.ex))
25168c2ecf20Sopenharmony_ci		prev_tx = np->last_tx.ex;
25178c2ecf20Sopenharmony_ci	else
25188c2ecf20Sopenharmony_ci		prev_tx = put_tx - 1;
25198c2ecf20Sopenharmony_ci
25208c2ecf20Sopenharmony_ci	if (unlikely(np->put_tx_ctx == np->tx_skb))
25218c2ecf20Sopenharmony_ci		prev_tx_ctx = np->last_tx_ctx;
25228c2ecf20Sopenharmony_ci	else
25238c2ecf20Sopenharmony_ci		prev_tx_ctx = np->put_tx_ctx - 1;
25248c2ecf20Sopenharmony_ci
25258c2ecf20Sopenharmony_ci	/* set last fragment flag  */
25268c2ecf20Sopenharmony_ci	prev_tx->flaglen |= cpu_to_le32(NV_TX2_LASTPACKET);
25278c2ecf20Sopenharmony_ci
25288c2ecf20Sopenharmony_ci	/* save skb in this slot's context area */
25298c2ecf20Sopenharmony_ci	prev_tx_ctx->skb = skb;
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci	if (skb_is_gso(skb))
25328c2ecf20Sopenharmony_ci		tx_flags_extra = NV_TX2_TSO | (skb_shinfo(skb)->gso_size << NV_TX2_TSO_SHIFT);
25338c2ecf20Sopenharmony_ci	else
25348c2ecf20Sopenharmony_ci		tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
25358c2ecf20Sopenharmony_ci			 NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
25368c2ecf20Sopenharmony_ci
25378c2ecf20Sopenharmony_ci	/* vlan tag */
25388c2ecf20Sopenharmony_ci	if (skb_vlan_tag_present(skb))
25398c2ecf20Sopenharmony_ci		start_tx->txvlan = cpu_to_le32(NV_TX3_VLAN_TAG_PRESENT |
25408c2ecf20Sopenharmony_ci					skb_vlan_tag_get(skb));
25418c2ecf20Sopenharmony_ci	else
25428c2ecf20Sopenharmony_ci		start_tx->txvlan = 0;
25438c2ecf20Sopenharmony_ci
25448c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
25458c2ecf20Sopenharmony_ci
25468c2ecf20Sopenharmony_ci	if (np->tx_limit) {
25478c2ecf20Sopenharmony_ci		/* Limit the number of outstanding tx. Setup all fragments, but
25488c2ecf20Sopenharmony_ci		 * do not set the VALID bit on the first descriptor. Save a pointer
25498c2ecf20Sopenharmony_ci		 * to that descriptor and also for next skb_map element.
25508c2ecf20Sopenharmony_ci		 */
25518c2ecf20Sopenharmony_ci
25528c2ecf20Sopenharmony_ci		if (np->tx_pkts_in_progress == NV_TX_LIMIT_COUNT) {
25538c2ecf20Sopenharmony_ci			if (!np->tx_change_owner)
25548c2ecf20Sopenharmony_ci				np->tx_change_owner = start_tx_ctx;
25558c2ecf20Sopenharmony_ci
25568c2ecf20Sopenharmony_ci			/* remove VALID bit */
25578c2ecf20Sopenharmony_ci			tx_flags &= ~NV_TX2_VALID;
25588c2ecf20Sopenharmony_ci			start_tx_ctx->first_tx_desc = start_tx;
25598c2ecf20Sopenharmony_ci			start_tx_ctx->next_tx_ctx = np->put_tx_ctx;
25608c2ecf20Sopenharmony_ci			np->tx_end_flip = np->put_tx_ctx;
25618c2ecf20Sopenharmony_ci		} else {
25628c2ecf20Sopenharmony_ci			np->tx_pkts_in_progress++;
25638c2ecf20Sopenharmony_ci		}
25648c2ecf20Sopenharmony_ci	}
25658c2ecf20Sopenharmony_ci
25668c2ecf20Sopenharmony_ci	/* set tx flags */
25678c2ecf20Sopenharmony_ci	start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
25688c2ecf20Sopenharmony_ci
25698c2ecf20Sopenharmony_ci	netdev_sent_queue(np->dev, skb->len);
25708c2ecf20Sopenharmony_ci
25718c2ecf20Sopenharmony_ci	skb_tx_timestamp(skb);
25728c2ecf20Sopenharmony_ci
25738c2ecf20Sopenharmony_ci	np->put_tx.ex = put_tx;
25748c2ecf20Sopenharmony_ci
25758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&np->lock, flags);
25768c2ecf20Sopenharmony_ci
25778c2ecf20Sopenharmony_citxkick:
25788c2ecf20Sopenharmony_ci	if (netif_queue_stopped(dev) || !netdev_xmit_more()) {
25798c2ecf20Sopenharmony_ci		u32 txrxctl_kick;
25808c2ecf20Sopenharmony_cidma_error:
25818c2ecf20Sopenharmony_ci		txrxctl_kick = NVREG_TXRXCTL_KICK | np->txrxctl_bits;
25828c2ecf20Sopenharmony_ci		writel(txrxctl_kick, get_hwbase(dev) + NvRegTxRxControl);
25838c2ecf20Sopenharmony_ci	}
25848c2ecf20Sopenharmony_ci
25858c2ecf20Sopenharmony_ci	return ret;
25868c2ecf20Sopenharmony_ci}
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_cistatic inline void nv_tx_flip_ownership(struct net_device *dev)
25898c2ecf20Sopenharmony_ci{
25908c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
25918c2ecf20Sopenharmony_ci
25928c2ecf20Sopenharmony_ci	np->tx_pkts_in_progress--;
25938c2ecf20Sopenharmony_ci	if (np->tx_change_owner) {
25948c2ecf20Sopenharmony_ci		np->tx_change_owner->first_tx_desc->flaglen |=
25958c2ecf20Sopenharmony_ci			cpu_to_le32(NV_TX2_VALID);
25968c2ecf20Sopenharmony_ci		np->tx_pkts_in_progress++;
25978c2ecf20Sopenharmony_ci
25988c2ecf20Sopenharmony_ci		np->tx_change_owner = np->tx_change_owner->next_tx_ctx;
25998c2ecf20Sopenharmony_ci		if (np->tx_change_owner == np->tx_end_flip)
26008c2ecf20Sopenharmony_ci			np->tx_change_owner = NULL;
26018c2ecf20Sopenharmony_ci
26028c2ecf20Sopenharmony_ci		writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
26038c2ecf20Sopenharmony_ci	}
26048c2ecf20Sopenharmony_ci}
26058c2ecf20Sopenharmony_ci
26068c2ecf20Sopenharmony_ci/*
26078c2ecf20Sopenharmony_ci * nv_tx_done: check for completed packets, release the skbs.
26088c2ecf20Sopenharmony_ci *
26098c2ecf20Sopenharmony_ci * Caller must own np->lock.
26108c2ecf20Sopenharmony_ci */
26118c2ecf20Sopenharmony_cistatic int nv_tx_done(struct net_device *dev, int limit)
26128c2ecf20Sopenharmony_ci{
26138c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
26148c2ecf20Sopenharmony_ci	u32 flags;
26158c2ecf20Sopenharmony_ci	int tx_work = 0;
26168c2ecf20Sopenharmony_ci	struct ring_desc *orig_get_tx = np->get_tx.orig;
26178c2ecf20Sopenharmony_ci	unsigned int bytes_compl = 0;
26188c2ecf20Sopenharmony_ci
26198c2ecf20Sopenharmony_ci	while ((np->get_tx.orig != np->put_tx.orig) &&
26208c2ecf20Sopenharmony_ci	       !((flags = le32_to_cpu(np->get_tx.orig->flaglen)) & NV_TX_VALID) &&
26218c2ecf20Sopenharmony_ci	       (tx_work < limit)) {
26228c2ecf20Sopenharmony_ci
26238c2ecf20Sopenharmony_ci		nv_unmap_txskb(np, np->get_tx_ctx);
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_ci		if (np->desc_ver == DESC_VER_1) {
26268c2ecf20Sopenharmony_ci			if (flags & NV_TX_LASTPACKET) {
26278c2ecf20Sopenharmony_ci				if (unlikely(flags & NV_TX_ERROR)) {
26288c2ecf20Sopenharmony_ci					if ((flags & NV_TX_RETRYERROR)
26298c2ecf20Sopenharmony_ci					    && !(flags & NV_TX_RETRYCOUNT_MASK))
26308c2ecf20Sopenharmony_ci						nv_legacybackoff_reseed(dev);
26318c2ecf20Sopenharmony_ci				} else {
26328c2ecf20Sopenharmony_ci					unsigned int len;
26338c2ecf20Sopenharmony_ci
26348c2ecf20Sopenharmony_ci					u64_stats_update_begin(&np->swstats_tx_syncp);
26358c2ecf20Sopenharmony_ci					nv_txrx_stats_inc(stat_tx_packets);
26368c2ecf20Sopenharmony_ci					len = np->get_tx_ctx->skb->len;
26378c2ecf20Sopenharmony_ci					nv_txrx_stats_add(stat_tx_bytes, len);
26388c2ecf20Sopenharmony_ci					u64_stats_update_end(&np->swstats_tx_syncp);
26398c2ecf20Sopenharmony_ci				}
26408c2ecf20Sopenharmony_ci				bytes_compl += np->get_tx_ctx->skb->len;
26418c2ecf20Sopenharmony_ci				dev_kfree_skb_any(np->get_tx_ctx->skb);
26428c2ecf20Sopenharmony_ci				np->get_tx_ctx->skb = NULL;
26438c2ecf20Sopenharmony_ci				tx_work++;
26448c2ecf20Sopenharmony_ci			}
26458c2ecf20Sopenharmony_ci		} else {
26468c2ecf20Sopenharmony_ci			if (flags & NV_TX2_LASTPACKET) {
26478c2ecf20Sopenharmony_ci				if (unlikely(flags & NV_TX2_ERROR)) {
26488c2ecf20Sopenharmony_ci					if ((flags & NV_TX2_RETRYERROR)
26498c2ecf20Sopenharmony_ci					    && !(flags & NV_TX2_RETRYCOUNT_MASK))
26508c2ecf20Sopenharmony_ci						nv_legacybackoff_reseed(dev);
26518c2ecf20Sopenharmony_ci				} else {
26528c2ecf20Sopenharmony_ci					unsigned int len;
26538c2ecf20Sopenharmony_ci
26548c2ecf20Sopenharmony_ci					u64_stats_update_begin(&np->swstats_tx_syncp);
26558c2ecf20Sopenharmony_ci					nv_txrx_stats_inc(stat_tx_packets);
26568c2ecf20Sopenharmony_ci					len = np->get_tx_ctx->skb->len;
26578c2ecf20Sopenharmony_ci					nv_txrx_stats_add(stat_tx_bytes, len);
26588c2ecf20Sopenharmony_ci					u64_stats_update_end(&np->swstats_tx_syncp);
26598c2ecf20Sopenharmony_ci				}
26608c2ecf20Sopenharmony_ci				bytes_compl += np->get_tx_ctx->skb->len;
26618c2ecf20Sopenharmony_ci				dev_kfree_skb_any(np->get_tx_ctx->skb);
26628c2ecf20Sopenharmony_ci				np->get_tx_ctx->skb = NULL;
26638c2ecf20Sopenharmony_ci				tx_work++;
26648c2ecf20Sopenharmony_ci			}
26658c2ecf20Sopenharmony_ci		}
26668c2ecf20Sopenharmony_ci		if (unlikely(np->get_tx.orig++ == np->last_tx.orig))
26678c2ecf20Sopenharmony_ci			np->get_tx.orig = np->tx_ring.orig;
26688c2ecf20Sopenharmony_ci		if (unlikely(np->get_tx_ctx++ == np->last_tx_ctx))
26698c2ecf20Sopenharmony_ci			np->get_tx_ctx = np->tx_skb;
26708c2ecf20Sopenharmony_ci	}
26718c2ecf20Sopenharmony_ci
26728c2ecf20Sopenharmony_ci	netdev_completed_queue(np->dev, tx_work, bytes_compl);
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci	if (unlikely((np->tx_stop == 1) && (np->get_tx.orig != orig_get_tx))) {
26758c2ecf20Sopenharmony_ci		np->tx_stop = 0;
26768c2ecf20Sopenharmony_ci		netif_wake_queue(dev);
26778c2ecf20Sopenharmony_ci	}
26788c2ecf20Sopenharmony_ci	return tx_work;
26798c2ecf20Sopenharmony_ci}
26808c2ecf20Sopenharmony_ci
26818c2ecf20Sopenharmony_cistatic int nv_tx_done_optimized(struct net_device *dev, int limit)
26828c2ecf20Sopenharmony_ci{
26838c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
26848c2ecf20Sopenharmony_ci	u32 flags;
26858c2ecf20Sopenharmony_ci	int tx_work = 0;
26868c2ecf20Sopenharmony_ci	struct ring_desc_ex *orig_get_tx = np->get_tx.ex;
26878c2ecf20Sopenharmony_ci	unsigned long bytes_cleaned = 0;
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci	while ((np->get_tx.ex != np->put_tx.ex) &&
26908c2ecf20Sopenharmony_ci	       !((flags = le32_to_cpu(np->get_tx.ex->flaglen)) & NV_TX2_VALID) &&
26918c2ecf20Sopenharmony_ci	       (tx_work < limit)) {
26928c2ecf20Sopenharmony_ci
26938c2ecf20Sopenharmony_ci		nv_unmap_txskb(np, np->get_tx_ctx);
26948c2ecf20Sopenharmony_ci
26958c2ecf20Sopenharmony_ci		if (flags & NV_TX2_LASTPACKET) {
26968c2ecf20Sopenharmony_ci			if (unlikely(flags & NV_TX2_ERROR)) {
26978c2ecf20Sopenharmony_ci				if ((flags & NV_TX2_RETRYERROR)
26988c2ecf20Sopenharmony_ci				    && !(flags & NV_TX2_RETRYCOUNT_MASK)) {
26998c2ecf20Sopenharmony_ci					if (np->driver_data & DEV_HAS_GEAR_MODE)
27008c2ecf20Sopenharmony_ci						nv_gear_backoff_reseed(dev);
27018c2ecf20Sopenharmony_ci					else
27028c2ecf20Sopenharmony_ci						nv_legacybackoff_reseed(dev);
27038c2ecf20Sopenharmony_ci				}
27048c2ecf20Sopenharmony_ci			} else {
27058c2ecf20Sopenharmony_ci				unsigned int len;
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_ci				u64_stats_update_begin(&np->swstats_tx_syncp);
27088c2ecf20Sopenharmony_ci				nv_txrx_stats_inc(stat_tx_packets);
27098c2ecf20Sopenharmony_ci				len = np->get_tx_ctx->skb->len;
27108c2ecf20Sopenharmony_ci				nv_txrx_stats_add(stat_tx_bytes, len);
27118c2ecf20Sopenharmony_ci				u64_stats_update_end(&np->swstats_tx_syncp);
27128c2ecf20Sopenharmony_ci			}
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci			bytes_cleaned += np->get_tx_ctx->skb->len;
27158c2ecf20Sopenharmony_ci			dev_kfree_skb_any(np->get_tx_ctx->skb);
27168c2ecf20Sopenharmony_ci			np->get_tx_ctx->skb = NULL;
27178c2ecf20Sopenharmony_ci			tx_work++;
27188c2ecf20Sopenharmony_ci
27198c2ecf20Sopenharmony_ci			if (np->tx_limit)
27208c2ecf20Sopenharmony_ci				nv_tx_flip_ownership(dev);
27218c2ecf20Sopenharmony_ci		}
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci		if (unlikely(np->get_tx.ex++ == np->last_tx.ex))
27248c2ecf20Sopenharmony_ci			np->get_tx.ex = np->tx_ring.ex;
27258c2ecf20Sopenharmony_ci		if (unlikely(np->get_tx_ctx++ == np->last_tx_ctx))
27268c2ecf20Sopenharmony_ci			np->get_tx_ctx = np->tx_skb;
27278c2ecf20Sopenharmony_ci	}
27288c2ecf20Sopenharmony_ci
27298c2ecf20Sopenharmony_ci	netdev_completed_queue(np->dev, tx_work, bytes_cleaned);
27308c2ecf20Sopenharmony_ci
27318c2ecf20Sopenharmony_ci	if (unlikely((np->tx_stop == 1) && (np->get_tx.ex != orig_get_tx))) {
27328c2ecf20Sopenharmony_ci		np->tx_stop = 0;
27338c2ecf20Sopenharmony_ci		netif_wake_queue(dev);
27348c2ecf20Sopenharmony_ci	}
27358c2ecf20Sopenharmony_ci	return tx_work;
27368c2ecf20Sopenharmony_ci}
27378c2ecf20Sopenharmony_ci
27388c2ecf20Sopenharmony_ci/*
27398c2ecf20Sopenharmony_ci * nv_tx_timeout: dev->tx_timeout function
27408c2ecf20Sopenharmony_ci * Called with netif_tx_lock held.
27418c2ecf20Sopenharmony_ci */
27428c2ecf20Sopenharmony_cistatic void nv_tx_timeout(struct net_device *dev, unsigned int txqueue)
27438c2ecf20Sopenharmony_ci{
27448c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
27458c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
27468c2ecf20Sopenharmony_ci	u32 status;
27478c2ecf20Sopenharmony_ci	union ring_type put_tx;
27488c2ecf20Sopenharmony_ci	int saved_tx_limit;
27498c2ecf20Sopenharmony_ci
27508c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_X_ENABLED)
27518c2ecf20Sopenharmony_ci		status = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
27528c2ecf20Sopenharmony_ci	else
27538c2ecf20Sopenharmony_ci		status = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
27548c2ecf20Sopenharmony_ci
27558c2ecf20Sopenharmony_ci	netdev_warn(dev, "Got tx_timeout. irq status: %08x\n", status);
27568c2ecf20Sopenharmony_ci
27578c2ecf20Sopenharmony_ci	if (unlikely(debug_tx_timeout)) {
27588c2ecf20Sopenharmony_ci		int i;
27598c2ecf20Sopenharmony_ci
27608c2ecf20Sopenharmony_ci		netdev_info(dev, "Ring at %lx\n", (unsigned long)np->ring_addr);
27618c2ecf20Sopenharmony_ci		netdev_info(dev, "Dumping tx registers\n");
27628c2ecf20Sopenharmony_ci		for (i = 0; i <= np->register_size; i += 32) {
27638c2ecf20Sopenharmony_ci			netdev_info(dev,
27648c2ecf20Sopenharmony_ci				    "%3x: %08x %08x %08x %08x "
27658c2ecf20Sopenharmony_ci				    "%08x %08x %08x %08x\n",
27668c2ecf20Sopenharmony_ci				    i,
27678c2ecf20Sopenharmony_ci				    readl(base + i + 0), readl(base + i + 4),
27688c2ecf20Sopenharmony_ci				    readl(base + i + 8), readl(base + i + 12),
27698c2ecf20Sopenharmony_ci				    readl(base + i + 16), readl(base + i + 20),
27708c2ecf20Sopenharmony_ci				    readl(base + i + 24), readl(base + i + 28));
27718c2ecf20Sopenharmony_ci		}
27728c2ecf20Sopenharmony_ci		netdev_info(dev, "Dumping tx ring\n");
27738c2ecf20Sopenharmony_ci		for (i = 0; i < np->tx_ring_size; i += 4) {
27748c2ecf20Sopenharmony_ci			if (!nv_optimized(np)) {
27758c2ecf20Sopenharmony_ci				netdev_info(dev,
27768c2ecf20Sopenharmony_ci					    "%03x: %08x %08x // %08x %08x "
27778c2ecf20Sopenharmony_ci					    "// %08x %08x // %08x %08x\n",
27788c2ecf20Sopenharmony_ci					    i,
27798c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i].buf),
27808c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i].flaglen),
27818c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+1].buf),
27828c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+1].flaglen),
27838c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+2].buf),
27848c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+2].flaglen),
27858c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+3].buf),
27868c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.orig[i+3].flaglen));
27878c2ecf20Sopenharmony_ci			} else {
27888c2ecf20Sopenharmony_ci				netdev_info(dev,
27898c2ecf20Sopenharmony_ci					    "%03x: %08x %08x %08x "
27908c2ecf20Sopenharmony_ci					    "// %08x %08x %08x "
27918c2ecf20Sopenharmony_ci					    "// %08x %08x %08x "
27928c2ecf20Sopenharmony_ci					    "// %08x %08x %08x\n",
27938c2ecf20Sopenharmony_ci					    i,
27948c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i].bufhigh),
27958c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i].buflow),
27968c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i].flaglen),
27978c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+1].bufhigh),
27988c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+1].buflow),
27998c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+1].flaglen),
28008c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+2].bufhigh),
28018c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+2].buflow),
28028c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+2].flaglen),
28038c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+3].bufhigh),
28048c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+3].buflow),
28058c2ecf20Sopenharmony_ci					    le32_to_cpu(np->tx_ring.ex[i+3].flaglen));
28068c2ecf20Sopenharmony_ci			}
28078c2ecf20Sopenharmony_ci		}
28088c2ecf20Sopenharmony_ci	}
28098c2ecf20Sopenharmony_ci
28108c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
28118c2ecf20Sopenharmony_ci
28128c2ecf20Sopenharmony_ci	/* 1) stop tx engine */
28138c2ecf20Sopenharmony_ci	nv_stop_tx(dev);
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci	/* 2) complete any outstanding tx and do not give HW any limited tx pkts */
28168c2ecf20Sopenharmony_ci	saved_tx_limit = np->tx_limit;
28178c2ecf20Sopenharmony_ci	np->tx_limit = 0; /* prevent giving HW any limited pkts */
28188c2ecf20Sopenharmony_ci	np->tx_stop = 0;  /* prevent waking tx queue */
28198c2ecf20Sopenharmony_ci	if (!nv_optimized(np))
28208c2ecf20Sopenharmony_ci		nv_tx_done(dev, np->tx_ring_size);
28218c2ecf20Sopenharmony_ci	else
28228c2ecf20Sopenharmony_ci		nv_tx_done_optimized(dev, np->tx_ring_size);
28238c2ecf20Sopenharmony_ci
28248c2ecf20Sopenharmony_ci	/* save current HW position */
28258c2ecf20Sopenharmony_ci	if (np->tx_change_owner)
28268c2ecf20Sopenharmony_ci		put_tx.ex = np->tx_change_owner->first_tx_desc;
28278c2ecf20Sopenharmony_ci	else
28288c2ecf20Sopenharmony_ci		put_tx = np->put_tx;
28298c2ecf20Sopenharmony_ci
28308c2ecf20Sopenharmony_ci	/* 3) clear all tx state */
28318c2ecf20Sopenharmony_ci	nv_drain_tx(dev);
28328c2ecf20Sopenharmony_ci	nv_init_tx(dev);
28338c2ecf20Sopenharmony_ci
28348c2ecf20Sopenharmony_ci	/* 4) restore state to current HW position */
28358c2ecf20Sopenharmony_ci	np->get_tx = np->put_tx = put_tx;
28368c2ecf20Sopenharmony_ci	np->tx_limit = saved_tx_limit;
28378c2ecf20Sopenharmony_ci
28388c2ecf20Sopenharmony_ci	/* 5) restart tx engine */
28398c2ecf20Sopenharmony_ci	nv_start_tx(dev);
28408c2ecf20Sopenharmony_ci	netif_wake_queue(dev);
28418c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
28428c2ecf20Sopenharmony_ci}
28438c2ecf20Sopenharmony_ci
28448c2ecf20Sopenharmony_ci/*
28458c2ecf20Sopenharmony_ci * Called when the nic notices a mismatch between the actual data len on the
28468c2ecf20Sopenharmony_ci * wire and the len indicated in the 802 header
28478c2ecf20Sopenharmony_ci */
28488c2ecf20Sopenharmony_cistatic int nv_getlen(struct net_device *dev, void *packet, int datalen)
28498c2ecf20Sopenharmony_ci{
28508c2ecf20Sopenharmony_ci	int hdrlen;	/* length of the 802 header */
28518c2ecf20Sopenharmony_ci	int protolen;	/* length as stored in the proto field */
28528c2ecf20Sopenharmony_ci
28538c2ecf20Sopenharmony_ci	/* 1) calculate len according to header */
28548c2ecf20Sopenharmony_ci	if (((struct vlan_ethhdr *)packet)->h_vlan_proto == htons(ETH_P_8021Q)) {
28558c2ecf20Sopenharmony_ci		protolen = ntohs(((struct vlan_ethhdr *)packet)->h_vlan_encapsulated_proto);
28568c2ecf20Sopenharmony_ci		hdrlen = VLAN_HLEN;
28578c2ecf20Sopenharmony_ci	} else {
28588c2ecf20Sopenharmony_ci		protolen = ntohs(((struct ethhdr *)packet)->h_proto);
28598c2ecf20Sopenharmony_ci		hdrlen = ETH_HLEN;
28608c2ecf20Sopenharmony_ci	}
28618c2ecf20Sopenharmony_ci	if (protolen > ETH_DATA_LEN)
28628c2ecf20Sopenharmony_ci		return datalen; /* Value in proto field not a len, no checks possible */
28638c2ecf20Sopenharmony_ci
28648c2ecf20Sopenharmony_ci	protolen += hdrlen;
28658c2ecf20Sopenharmony_ci	/* consistency checks: */
28668c2ecf20Sopenharmony_ci	if (datalen > ETH_ZLEN) {
28678c2ecf20Sopenharmony_ci		if (datalen >= protolen) {
28688c2ecf20Sopenharmony_ci			/* more data on wire than in 802 header, trim of
28698c2ecf20Sopenharmony_ci			 * additional data.
28708c2ecf20Sopenharmony_ci			 */
28718c2ecf20Sopenharmony_ci			return protolen;
28728c2ecf20Sopenharmony_ci		} else {
28738c2ecf20Sopenharmony_ci			/* less data on wire than mentioned in header.
28748c2ecf20Sopenharmony_ci			 * Discard the packet.
28758c2ecf20Sopenharmony_ci			 */
28768c2ecf20Sopenharmony_ci			return -1;
28778c2ecf20Sopenharmony_ci		}
28788c2ecf20Sopenharmony_ci	} else {
28798c2ecf20Sopenharmony_ci		/* short packet. Accept only if 802 values are also short */
28808c2ecf20Sopenharmony_ci		if (protolen > ETH_ZLEN) {
28818c2ecf20Sopenharmony_ci			return -1;
28828c2ecf20Sopenharmony_ci		}
28838c2ecf20Sopenharmony_ci		return datalen;
28848c2ecf20Sopenharmony_ci	}
28858c2ecf20Sopenharmony_ci}
28868c2ecf20Sopenharmony_ci
28878c2ecf20Sopenharmony_cistatic void rx_missing_handler(u32 flags, struct fe_priv *np)
28888c2ecf20Sopenharmony_ci{
28898c2ecf20Sopenharmony_ci	if (flags & NV_RX_MISSEDFRAME) {
28908c2ecf20Sopenharmony_ci		u64_stats_update_begin(&np->swstats_rx_syncp);
28918c2ecf20Sopenharmony_ci		nv_txrx_stats_inc(stat_rx_missed_errors);
28928c2ecf20Sopenharmony_ci		u64_stats_update_end(&np->swstats_rx_syncp);
28938c2ecf20Sopenharmony_ci	}
28948c2ecf20Sopenharmony_ci}
28958c2ecf20Sopenharmony_ci
28968c2ecf20Sopenharmony_cistatic int nv_rx_process(struct net_device *dev, int limit)
28978c2ecf20Sopenharmony_ci{
28988c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
28998c2ecf20Sopenharmony_ci	u32 flags;
29008c2ecf20Sopenharmony_ci	int rx_work = 0;
29018c2ecf20Sopenharmony_ci	struct sk_buff *skb;
29028c2ecf20Sopenharmony_ci	int len;
29038c2ecf20Sopenharmony_ci
29048c2ecf20Sopenharmony_ci	while ((np->get_rx.orig != np->put_rx.orig) &&
29058c2ecf20Sopenharmony_ci	      !((flags = le32_to_cpu(np->get_rx.orig->flaglen)) & NV_RX_AVAIL) &&
29068c2ecf20Sopenharmony_ci		(rx_work < limit)) {
29078c2ecf20Sopenharmony_ci
29088c2ecf20Sopenharmony_ci		/*
29098c2ecf20Sopenharmony_ci		 * the packet is for us - immediately tear down the pci mapping.
29108c2ecf20Sopenharmony_ci		 * TODO: check if a prefetch of the first cacheline improves
29118c2ecf20Sopenharmony_ci		 * the performance.
29128c2ecf20Sopenharmony_ci		 */
29138c2ecf20Sopenharmony_ci		dma_unmap_single(&np->pci_dev->dev, np->get_rx_ctx->dma,
29148c2ecf20Sopenharmony_ci				 np->get_rx_ctx->dma_len,
29158c2ecf20Sopenharmony_ci				 DMA_FROM_DEVICE);
29168c2ecf20Sopenharmony_ci		skb = np->get_rx_ctx->skb;
29178c2ecf20Sopenharmony_ci		np->get_rx_ctx->skb = NULL;
29188c2ecf20Sopenharmony_ci
29198c2ecf20Sopenharmony_ci		/* look at what we actually got: */
29208c2ecf20Sopenharmony_ci		if (np->desc_ver == DESC_VER_1) {
29218c2ecf20Sopenharmony_ci			if (likely(flags & NV_RX_DESCRIPTORVALID)) {
29228c2ecf20Sopenharmony_ci				len = flags & LEN_MASK_V1;
29238c2ecf20Sopenharmony_ci				if (unlikely(flags & NV_RX_ERROR)) {
29248c2ecf20Sopenharmony_ci					if ((flags & NV_RX_ERROR_MASK) == NV_RX_ERROR4) {
29258c2ecf20Sopenharmony_ci						len = nv_getlen(dev, skb->data, len);
29268c2ecf20Sopenharmony_ci						if (len < 0) {
29278c2ecf20Sopenharmony_ci							dev_kfree_skb(skb);
29288c2ecf20Sopenharmony_ci							goto next_pkt;
29298c2ecf20Sopenharmony_ci						}
29308c2ecf20Sopenharmony_ci					}
29318c2ecf20Sopenharmony_ci					/* framing errors are soft errors */
29328c2ecf20Sopenharmony_ci					else if ((flags & NV_RX_ERROR_MASK) == NV_RX_FRAMINGERR) {
29338c2ecf20Sopenharmony_ci						if (flags & NV_RX_SUBTRACT1)
29348c2ecf20Sopenharmony_ci							len--;
29358c2ecf20Sopenharmony_ci					}
29368c2ecf20Sopenharmony_ci					/* the rest are hard errors */
29378c2ecf20Sopenharmony_ci					else {
29388c2ecf20Sopenharmony_ci						rx_missing_handler(flags, np);
29398c2ecf20Sopenharmony_ci						dev_kfree_skb(skb);
29408c2ecf20Sopenharmony_ci						goto next_pkt;
29418c2ecf20Sopenharmony_ci					}
29428c2ecf20Sopenharmony_ci				}
29438c2ecf20Sopenharmony_ci			} else {
29448c2ecf20Sopenharmony_ci				dev_kfree_skb(skb);
29458c2ecf20Sopenharmony_ci				goto next_pkt;
29468c2ecf20Sopenharmony_ci			}
29478c2ecf20Sopenharmony_ci		} else {
29488c2ecf20Sopenharmony_ci			if (likely(flags & NV_RX2_DESCRIPTORVALID)) {
29498c2ecf20Sopenharmony_ci				len = flags & LEN_MASK_V2;
29508c2ecf20Sopenharmony_ci				if (unlikely(flags & NV_RX2_ERROR)) {
29518c2ecf20Sopenharmony_ci					if ((flags & NV_RX2_ERROR_MASK) == NV_RX2_ERROR4) {
29528c2ecf20Sopenharmony_ci						len = nv_getlen(dev, skb->data, len);
29538c2ecf20Sopenharmony_ci						if (len < 0) {
29548c2ecf20Sopenharmony_ci							dev_kfree_skb(skb);
29558c2ecf20Sopenharmony_ci							goto next_pkt;
29568c2ecf20Sopenharmony_ci						}
29578c2ecf20Sopenharmony_ci					}
29588c2ecf20Sopenharmony_ci					/* framing errors are soft errors */
29598c2ecf20Sopenharmony_ci					else if ((flags & NV_RX2_ERROR_MASK) == NV_RX2_FRAMINGERR) {
29608c2ecf20Sopenharmony_ci						if (flags & NV_RX2_SUBTRACT1)
29618c2ecf20Sopenharmony_ci							len--;
29628c2ecf20Sopenharmony_ci					}
29638c2ecf20Sopenharmony_ci					/* the rest are hard errors */
29648c2ecf20Sopenharmony_ci					else {
29658c2ecf20Sopenharmony_ci						dev_kfree_skb(skb);
29668c2ecf20Sopenharmony_ci						goto next_pkt;
29678c2ecf20Sopenharmony_ci					}
29688c2ecf20Sopenharmony_ci				}
29698c2ecf20Sopenharmony_ci				if (((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUM_IP_TCP) || /*ip and tcp */
29708c2ecf20Sopenharmony_ci				    ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUM_IP_UDP))   /*ip and udp */
29718c2ecf20Sopenharmony_ci					skb->ip_summed = CHECKSUM_UNNECESSARY;
29728c2ecf20Sopenharmony_ci			} else {
29738c2ecf20Sopenharmony_ci				dev_kfree_skb(skb);
29748c2ecf20Sopenharmony_ci				goto next_pkt;
29758c2ecf20Sopenharmony_ci			}
29768c2ecf20Sopenharmony_ci		}
29778c2ecf20Sopenharmony_ci		/* got a valid packet - forward it to the network core */
29788c2ecf20Sopenharmony_ci		skb_put(skb, len);
29798c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, dev);
29808c2ecf20Sopenharmony_ci		napi_gro_receive(&np->napi, skb);
29818c2ecf20Sopenharmony_ci		u64_stats_update_begin(&np->swstats_rx_syncp);
29828c2ecf20Sopenharmony_ci		nv_txrx_stats_inc(stat_rx_packets);
29838c2ecf20Sopenharmony_ci		nv_txrx_stats_add(stat_rx_bytes, len);
29848c2ecf20Sopenharmony_ci		u64_stats_update_end(&np->swstats_rx_syncp);
29858c2ecf20Sopenharmony_cinext_pkt:
29868c2ecf20Sopenharmony_ci		if (unlikely(np->get_rx.orig++ == np->last_rx.orig))
29878c2ecf20Sopenharmony_ci			np->get_rx.orig = np->rx_ring.orig;
29888c2ecf20Sopenharmony_ci		if (unlikely(np->get_rx_ctx++ == np->last_rx_ctx))
29898c2ecf20Sopenharmony_ci			np->get_rx_ctx = np->rx_skb;
29908c2ecf20Sopenharmony_ci
29918c2ecf20Sopenharmony_ci		rx_work++;
29928c2ecf20Sopenharmony_ci	}
29938c2ecf20Sopenharmony_ci
29948c2ecf20Sopenharmony_ci	return rx_work;
29958c2ecf20Sopenharmony_ci}
29968c2ecf20Sopenharmony_ci
29978c2ecf20Sopenharmony_cistatic int nv_rx_process_optimized(struct net_device *dev, int limit)
29988c2ecf20Sopenharmony_ci{
29998c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
30008c2ecf20Sopenharmony_ci	u32 flags;
30018c2ecf20Sopenharmony_ci	u32 vlanflags = 0;
30028c2ecf20Sopenharmony_ci	int rx_work = 0;
30038c2ecf20Sopenharmony_ci	struct sk_buff *skb;
30048c2ecf20Sopenharmony_ci	int len;
30058c2ecf20Sopenharmony_ci
30068c2ecf20Sopenharmony_ci	while ((np->get_rx.ex != np->put_rx.ex) &&
30078c2ecf20Sopenharmony_ci	      !((flags = le32_to_cpu(np->get_rx.ex->flaglen)) & NV_RX2_AVAIL) &&
30088c2ecf20Sopenharmony_ci	      (rx_work < limit)) {
30098c2ecf20Sopenharmony_ci
30108c2ecf20Sopenharmony_ci		/*
30118c2ecf20Sopenharmony_ci		 * the packet is for us - immediately tear down the pci mapping.
30128c2ecf20Sopenharmony_ci		 * TODO: check if a prefetch of the first cacheline improves
30138c2ecf20Sopenharmony_ci		 * the performance.
30148c2ecf20Sopenharmony_ci		 */
30158c2ecf20Sopenharmony_ci		dma_unmap_single(&np->pci_dev->dev, np->get_rx_ctx->dma,
30168c2ecf20Sopenharmony_ci				 np->get_rx_ctx->dma_len,
30178c2ecf20Sopenharmony_ci				 DMA_FROM_DEVICE);
30188c2ecf20Sopenharmony_ci		skb = np->get_rx_ctx->skb;
30198c2ecf20Sopenharmony_ci		np->get_rx_ctx->skb = NULL;
30208c2ecf20Sopenharmony_ci
30218c2ecf20Sopenharmony_ci		/* look at what we actually got: */
30228c2ecf20Sopenharmony_ci		if (likely(flags & NV_RX2_DESCRIPTORVALID)) {
30238c2ecf20Sopenharmony_ci			len = flags & LEN_MASK_V2;
30248c2ecf20Sopenharmony_ci			if (unlikely(flags & NV_RX2_ERROR)) {
30258c2ecf20Sopenharmony_ci				if ((flags & NV_RX2_ERROR_MASK) == NV_RX2_ERROR4) {
30268c2ecf20Sopenharmony_ci					len = nv_getlen(dev, skb->data, len);
30278c2ecf20Sopenharmony_ci					if (len < 0) {
30288c2ecf20Sopenharmony_ci						dev_kfree_skb(skb);
30298c2ecf20Sopenharmony_ci						goto next_pkt;
30308c2ecf20Sopenharmony_ci					}
30318c2ecf20Sopenharmony_ci				}
30328c2ecf20Sopenharmony_ci				/* framing errors are soft errors */
30338c2ecf20Sopenharmony_ci				else if ((flags & NV_RX2_ERROR_MASK) == NV_RX2_FRAMINGERR) {
30348c2ecf20Sopenharmony_ci					if (flags & NV_RX2_SUBTRACT1)
30358c2ecf20Sopenharmony_ci						len--;
30368c2ecf20Sopenharmony_ci				}
30378c2ecf20Sopenharmony_ci				/* the rest are hard errors */
30388c2ecf20Sopenharmony_ci				else {
30398c2ecf20Sopenharmony_ci					dev_kfree_skb(skb);
30408c2ecf20Sopenharmony_ci					goto next_pkt;
30418c2ecf20Sopenharmony_ci				}
30428c2ecf20Sopenharmony_ci			}
30438c2ecf20Sopenharmony_ci
30448c2ecf20Sopenharmony_ci			if (((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUM_IP_TCP) || /*ip and tcp */
30458c2ecf20Sopenharmony_ci			    ((flags & NV_RX2_CHECKSUMMASK) == NV_RX2_CHECKSUM_IP_UDP))   /*ip and udp */
30468c2ecf20Sopenharmony_ci				skb->ip_summed = CHECKSUM_UNNECESSARY;
30478c2ecf20Sopenharmony_ci
30488c2ecf20Sopenharmony_ci			/* got a valid packet - forward it to the network core */
30498c2ecf20Sopenharmony_ci			skb_put(skb, len);
30508c2ecf20Sopenharmony_ci			skb->protocol = eth_type_trans(skb, dev);
30518c2ecf20Sopenharmony_ci			prefetch(skb->data);
30528c2ecf20Sopenharmony_ci
30538c2ecf20Sopenharmony_ci			vlanflags = le32_to_cpu(np->get_rx.ex->buflow);
30548c2ecf20Sopenharmony_ci
30558c2ecf20Sopenharmony_ci			/*
30568c2ecf20Sopenharmony_ci			 * There's need to check for NETIF_F_HW_VLAN_CTAG_RX
30578c2ecf20Sopenharmony_ci			 * here. Even if vlan rx accel is disabled,
30588c2ecf20Sopenharmony_ci			 * NV_RX3_VLAN_TAG_PRESENT is pseudo randomly set.
30598c2ecf20Sopenharmony_ci			 */
30608c2ecf20Sopenharmony_ci			if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
30618c2ecf20Sopenharmony_ci			    vlanflags & NV_RX3_VLAN_TAG_PRESENT) {
30628c2ecf20Sopenharmony_ci				u16 vid = vlanflags & NV_RX3_VLAN_TAG_MASK;
30638c2ecf20Sopenharmony_ci
30648c2ecf20Sopenharmony_ci				__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
30658c2ecf20Sopenharmony_ci			}
30668c2ecf20Sopenharmony_ci			napi_gro_receive(&np->napi, skb);
30678c2ecf20Sopenharmony_ci			u64_stats_update_begin(&np->swstats_rx_syncp);
30688c2ecf20Sopenharmony_ci			nv_txrx_stats_inc(stat_rx_packets);
30698c2ecf20Sopenharmony_ci			nv_txrx_stats_add(stat_rx_bytes, len);
30708c2ecf20Sopenharmony_ci			u64_stats_update_end(&np->swstats_rx_syncp);
30718c2ecf20Sopenharmony_ci		} else {
30728c2ecf20Sopenharmony_ci			dev_kfree_skb(skb);
30738c2ecf20Sopenharmony_ci		}
30748c2ecf20Sopenharmony_cinext_pkt:
30758c2ecf20Sopenharmony_ci		if (unlikely(np->get_rx.ex++ == np->last_rx.ex))
30768c2ecf20Sopenharmony_ci			np->get_rx.ex = np->rx_ring.ex;
30778c2ecf20Sopenharmony_ci		if (unlikely(np->get_rx_ctx++ == np->last_rx_ctx))
30788c2ecf20Sopenharmony_ci			np->get_rx_ctx = np->rx_skb;
30798c2ecf20Sopenharmony_ci
30808c2ecf20Sopenharmony_ci		rx_work++;
30818c2ecf20Sopenharmony_ci	}
30828c2ecf20Sopenharmony_ci
30838c2ecf20Sopenharmony_ci	return rx_work;
30848c2ecf20Sopenharmony_ci}
30858c2ecf20Sopenharmony_ci
30868c2ecf20Sopenharmony_cistatic void set_bufsize(struct net_device *dev)
30878c2ecf20Sopenharmony_ci{
30888c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
30898c2ecf20Sopenharmony_ci
30908c2ecf20Sopenharmony_ci	if (dev->mtu <= ETH_DATA_LEN)
30918c2ecf20Sopenharmony_ci		np->rx_buf_sz = ETH_DATA_LEN + NV_RX_HEADERS;
30928c2ecf20Sopenharmony_ci	else
30938c2ecf20Sopenharmony_ci		np->rx_buf_sz = dev->mtu + NV_RX_HEADERS;
30948c2ecf20Sopenharmony_ci}
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_ci/*
30978c2ecf20Sopenharmony_ci * nv_change_mtu: dev->change_mtu function
30988c2ecf20Sopenharmony_ci * Called with dev_base_lock held for read.
30998c2ecf20Sopenharmony_ci */
31008c2ecf20Sopenharmony_cistatic int nv_change_mtu(struct net_device *dev, int new_mtu)
31018c2ecf20Sopenharmony_ci{
31028c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
31038c2ecf20Sopenharmony_ci	int old_mtu;
31048c2ecf20Sopenharmony_ci
31058c2ecf20Sopenharmony_ci	old_mtu = dev->mtu;
31068c2ecf20Sopenharmony_ci	dev->mtu = new_mtu;
31078c2ecf20Sopenharmony_ci
31088c2ecf20Sopenharmony_ci	/* return early if the buffer sizes will not change */
31098c2ecf20Sopenharmony_ci	if (old_mtu <= ETH_DATA_LEN && new_mtu <= ETH_DATA_LEN)
31108c2ecf20Sopenharmony_ci		return 0;
31118c2ecf20Sopenharmony_ci
31128c2ecf20Sopenharmony_ci	/* synchronized against open : rtnl_lock() held by caller */
31138c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
31148c2ecf20Sopenharmony_ci		u8 __iomem *base = get_hwbase(dev);
31158c2ecf20Sopenharmony_ci		/*
31168c2ecf20Sopenharmony_ci		 * It seems that the nic preloads valid ring entries into an
31178c2ecf20Sopenharmony_ci		 * internal buffer. The procedure for flushing everything is
31188c2ecf20Sopenharmony_ci		 * guessed, there is probably a simpler approach.
31198c2ecf20Sopenharmony_ci		 * Changing the MTU is a rare event, it shouldn't matter.
31208c2ecf20Sopenharmony_ci		 */
31218c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
31228c2ecf20Sopenharmony_ci		nv_napi_disable(dev);
31238c2ecf20Sopenharmony_ci		netif_tx_lock_bh(dev);
31248c2ecf20Sopenharmony_ci		netif_addr_lock(dev);
31258c2ecf20Sopenharmony_ci		spin_lock(&np->lock);
31268c2ecf20Sopenharmony_ci		/* stop engines */
31278c2ecf20Sopenharmony_ci		nv_stop_rxtx(dev);
31288c2ecf20Sopenharmony_ci		nv_txrx_reset(dev);
31298c2ecf20Sopenharmony_ci		/* drain rx queue */
31308c2ecf20Sopenharmony_ci		nv_drain_rxtx(dev);
31318c2ecf20Sopenharmony_ci		/* reinit driver view of the rx queue */
31328c2ecf20Sopenharmony_ci		set_bufsize(dev);
31338c2ecf20Sopenharmony_ci		if (nv_init_ring(dev)) {
31348c2ecf20Sopenharmony_ci			if (!np->in_shutdown)
31358c2ecf20Sopenharmony_ci				mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
31368c2ecf20Sopenharmony_ci		}
31378c2ecf20Sopenharmony_ci		/* reinit nic view of the rx queue */
31388c2ecf20Sopenharmony_ci		writel(np->rx_buf_sz, base + NvRegOffloadConfig);
31398c2ecf20Sopenharmony_ci		setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
31408c2ecf20Sopenharmony_ci		writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
31418c2ecf20Sopenharmony_ci			base + NvRegRingSizes);
31428c2ecf20Sopenharmony_ci		pci_push(base);
31438c2ecf20Sopenharmony_ci		writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
31448c2ecf20Sopenharmony_ci		pci_push(base);
31458c2ecf20Sopenharmony_ci
31468c2ecf20Sopenharmony_ci		/* restart rx engine */
31478c2ecf20Sopenharmony_ci		nv_start_rxtx(dev);
31488c2ecf20Sopenharmony_ci		spin_unlock(&np->lock);
31498c2ecf20Sopenharmony_ci		netif_addr_unlock(dev);
31508c2ecf20Sopenharmony_ci		netif_tx_unlock_bh(dev);
31518c2ecf20Sopenharmony_ci		nv_napi_enable(dev);
31528c2ecf20Sopenharmony_ci		nv_enable_irq(dev);
31538c2ecf20Sopenharmony_ci	}
31548c2ecf20Sopenharmony_ci	return 0;
31558c2ecf20Sopenharmony_ci}
31568c2ecf20Sopenharmony_ci
31578c2ecf20Sopenharmony_cistatic void nv_copy_mac_to_hw(struct net_device *dev)
31588c2ecf20Sopenharmony_ci{
31598c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
31608c2ecf20Sopenharmony_ci	u32 mac[2];
31618c2ecf20Sopenharmony_ci
31628c2ecf20Sopenharmony_ci	mac[0] = (dev->dev_addr[0] << 0) + (dev->dev_addr[1] << 8) +
31638c2ecf20Sopenharmony_ci			(dev->dev_addr[2] << 16) + (dev->dev_addr[3] << 24);
31648c2ecf20Sopenharmony_ci	mac[1] = (dev->dev_addr[4] << 0) + (dev->dev_addr[5] << 8);
31658c2ecf20Sopenharmony_ci
31668c2ecf20Sopenharmony_ci	writel(mac[0], base + NvRegMacAddrA);
31678c2ecf20Sopenharmony_ci	writel(mac[1], base + NvRegMacAddrB);
31688c2ecf20Sopenharmony_ci}
31698c2ecf20Sopenharmony_ci
31708c2ecf20Sopenharmony_ci/*
31718c2ecf20Sopenharmony_ci * nv_set_mac_address: dev->set_mac_address function
31728c2ecf20Sopenharmony_ci * Called with rtnl_lock() held.
31738c2ecf20Sopenharmony_ci */
31748c2ecf20Sopenharmony_cistatic int nv_set_mac_address(struct net_device *dev, void *addr)
31758c2ecf20Sopenharmony_ci{
31768c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
31778c2ecf20Sopenharmony_ci	struct sockaddr *macaddr = (struct sockaddr *)addr;
31788c2ecf20Sopenharmony_ci
31798c2ecf20Sopenharmony_ci	if (!is_valid_ether_addr(macaddr->sa_data))
31808c2ecf20Sopenharmony_ci		return -EADDRNOTAVAIL;
31818c2ecf20Sopenharmony_ci
31828c2ecf20Sopenharmony_ci	/* synchronized against open : rtnl_lock() held by caller */
31838c2ecf20Sopenharmony_ci	memcpy(dev->dev_addr, macaddr->sa_data, ETH_ALEN);
31848c2ecf20Sopenharmony_ci
31858c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
31868c2ecf20Sopenharmony_ci		netif_tx_lock_bh(dev);
31878c2ecf20Sopenharmony_ci		netif_addr_lock(dev);
31888c2ecf20Sopenharmony_ci		spin_lock_irq(&np->lock);
31898c2ecf20Sopenharmony_ci
31908c2ecf20Sopenharmony_ci		/* stop rx engine */
31918c2ecf20Sopenharmony_ci		nv_stop_rx(dev);
31928c2ecf20Sopenharmony_ci
31938c2ecf20Sopenharmony_ci		/* set mac address */
31948c2ecf20Sopenharmony_ci		nv_copy_mac_to_hw(dev);
31958c2ecf20Sopenharmony_ci
31968c2ecf20Sopenharmony_ci		/* restart rx engine */
31978c2ecf20Sopenharmony_ci		nv_start_rx(dev);
31988c2ecf20Sopenharmony_ci		spin_unlock_irq(&np->lock);
31998c2ecf20Sopenharmony_ci		netif_addr_unlock(dev);
32008c2ecf20Sopenharmony_ci		netif_tx_unlock_bh(dev);
32018c2ecf20Sopenharmony_ci	} else {
32028c2ecf20Sopenharmony_ci		nv_copy_mac_to_hw(dev);
32038c2ecf20Sopenharmony_ci	}
32048c2ecf20Sopenharmony_ci	return 0;
32058c2ecf20Sopenharmony_ci}
32068c2ecf20Sopenharmony_ci
32078c2ecf20Sopenharmony_ci/*
32088c2ecf20Sopenharmony_ci * nv_set_multicast: dev->set_multicast function
32098c2ecf20Sopenharmony_ci * Called with netif_tx_lock held.
32108c2ecf20Sopenharmony_ci */
32118c2ecf20Sopenharmony_cistatic void nv_set_multicast(struct net_device *dev)
32128c2ecf20Sopenharmony_ci{
32138c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
32148c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
32158c2ecf20Sopenharmony_ci	u32 addr[2];
32168c2ecf20Sopenharmony_ci	u32 mask[2];
32178c2ecf20Sopenharmony_ci	u32 pff = readl(base + NvRegPacketFilterFlags) & NVREG_PFF_PAUSE_RX;
32188c2ecf20Sopenharmony_ci
32198c2ecf20Sopenharmony_ci	memset(addr, 0, sizeof(addr));
32208c2ecf20Sopenharmony_ci	memset(mask, 0, sizeof(mask));
32218c2ecf20Sopenharmony_ci
32228c2ecf20Sopenharmony_ci	if (dev->flags & IFF_PROMISC) {
32238c2ecf20Sopenharmony_ci		pff |= NVREG_PFF_PROMISC;
32248c2ecf20Sopenharmony_ci	} else {
32258c2ecf20Sopenharmony_ci		pff |= NVREG_PFF_MYADDR;
32268c2ecf20Sopenharmony_ci
32278c2ecf20Sopenharmony_ci		if (dev->flags & IFF_ALLMULTI || !netdev_mc_empty(dev)) {
32288c2ecf20Sopenharmony_ci			u32 alwaysOff[2];
32298c2ecf20Sopenharmony_ci			u32 alwaysOn[2];
32308c2ecf20Sopenharmony_ci
32318c2ecf20Sopenharmony_ci			alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0xffffffff;
32328c2ecf20Sopenharmony_ci			if (dev->flags & IFF_ALLMULTI) {
32338c2ecf20Sopenharmony_ci				alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0;
32348c2ecf20Sopenharmony_ci			} else {
32358c2ecf20Sopenharmony_ci				struct netdev_hw_addr *ha;
32368c2ecf20Sopenharmony_ci
32378c2ecf20Sopenharmony_ci				netdev_for_each_mc_addr(ha, dev) {
32388c2ecf20Sopenharmony_ci					unsigned char *hw_addr = ha->addr;
32398c2ecf20Sopenharmony_ci					u32 a, b;
32408c2ecf20Sopenharmony_ci
32418c2ecf20Sopenharmony_ci					a = le32_to_cpu(*(__le32 *) hw_addr);
32428c2ecf20Sopenharmony_ci					b = le16_to_cpu(*(__le16 *) (&hw_addr[4]));
32438c2ecf20Sopenharmony_ci					alwaysOn[0] &= a;
32448c2ecf20Sopenharmony_ci					alwaysOff[0] &= ~a;
32458c2ecf20Sopenharmony_ci					alwaysOn[1] &= b;
32468c2ecf20Sopenharmony_ci					alwaysOff[1] &= ~b;
32478c2ecf20Sopenharmony_ci				}
32488c2ecf20Sopenharmony_ci			}
32498c2ecf20Sopenharmony_ci			addr[0] = alwaysOn[0];
32508c2ecf20Sopenharmony_ci			addr[1] = alwaysOn[1];
32518c2ecf20Sopenharmony_ci			mask[0] = alwaysOn[0] | alwaysOff[0];
32528c2ecf20Sopenharmony_ci			mask[1] = alwaysOn[1] | alwaysOff[1];
32538c2ecf20Sopenharmony_ci		} else {
32548c2ecf20Sopenharmony_ci			mask[0] = NVREG_MCASTMASKA_NONE;
32558c2ecf20Sopenharmony_ci			mask[1] = NVREG_MCASTMASKB_NONE;
32568c2ecf20Sopenharmony_ci		}
32578c2ecf20Sopenharmony_ci	}
32588c2ecf20Sopenharmony_ci	addr[0] |= NVREG_MCASTADDRA_FORCE;
32598c2ecf20Sopenharmony_ci	pff |= NVREG_PFF_ALWAYS;
32608c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
32618c2ecf20Sopenharmony_ci	nv_stop_rx(dev);
32628c2ecf20Sopenharmony_ci	writel(addr[0], base + NvRegMulticastAddrA);
32638c2ecf20Sopenharmony_ci	writel(addr[1], base + NvRegMulticastAddrB);
32648c2ecf20Sopenharmony_ci	writel(mask[0], base + NvRegMulticastMaskA);
32658c2ecf20Sopenharmony_ci	writel(mask[1], base + NvRegMulticastMaskB);
32668c2ecf20Sopenharmony_ci	writel(pff, base + NvRegPacketFilterFlags);
32678c2ecf20Sopenharmony_ci	nv_start_rx(dev);
32688c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
32698c2ecf20Sopenharmony_ci}
32708c2ecf20Sopenharmony_ci
32718c2ecf20Sopenharmony_cistatic void nv_update_pause(struct net_device *dev, u32 pause_flags)
32728c2ecf20Sopenharmony_ci{
32738c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
32748c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
32758c2ecf20Sopenharmony_ci
32768c2ecf20Sopenharmony_ci	np->pause_flags &= ~(NV_PAUSEFRAME_TX_ENABLE | NV_PAUSEFRAME_RX_ENABLE);
32778c2ecf20Sopenharmony_ci
32788c2ecf20Sopenharmony_ci	if (np->pause_flags & NV_PAUSEFRAME_RX_CAPABLE) {
32798c2ecf20Sopenharmony_ci		u32 pff = readl(base + NvRegPacketFilterFlags) & ~NVREG_PFF_PAUSE_RX;
32808c2ecf20Sopenharmony_ci		if (pause_flags & NV_PAUSEFRAME_RX_ENABLE) {
32818c2ecf20Sopenharmony_ci			writel(pff|NVREG_PFF_PAUSE_RX, base + NvRegPacketFilterFlags);
32828c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
32838c2ecf20Sopenharmony_ci		} else {
32848c2ecf20Sopenharmony_ci			writel(pff, base + NvRegPacketFilterFlags);
32858c2ecf20Sopenharmony_ci		}
32868c2ecf20Sopenharmony_ci	}
32878c2ecf20Sopenharmony_ci	if (np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE) {
32888c2ecf20Sopenharmony_ci		u32 regmisc = readl(base + NvRegMisc1) & ~NVREG_MISC1_PAUSE_TX;
32898c2ecf20Sopenharmony_ci		if (pause_flags & NV_PAUSEFRAME_TX_ENABLE) {
32908c2ecf20Sopenharmony_ci			u32 pause_enable = NVREG_TX_PAUSEFRAME_ENABLE_V1;
32918c2ecf20Sopenharmony_ci			if (np->driver_data & DEV_HAS_PAUSEFRAME_TX_V2)
32928c2ecf20Sopenharmony_ci				pause_enable = NVREG_TX_PAUSEFRAME_ENABLE_V2;
32938c2ecf20Sopenharmony_ci			if (np->driver_data & DEV_HAS_PAUSEFRAME_TX_V3) {
32948c2ecf20Sopenharmony_ci				pause_enable = NVREG_TX_PAUSEFRAME_ENABLE_V3;
32958c2ecf20Sopenharmony_ci				/* limit the number of tx pause frames to a default of 8 */
32968c2ecf20Sopenharmony_ci				writel(readl(base + NvRegTxPauseFrameLimit)|NVREG_TX_PAUSEFRAMELIMIT_ENABLE, base + NvRegTxPauseFrameLimit);
32978c2ecf20Sopenharmony_ci			}
32988c2ecf20Sopenharmony_ci			writel(pause_enable,  base + NvRegTxPauseFrame);
32998c2ecf20Sopenharmony_ci			writel(regmisc|NVREG_MISC1_PAUSE_TX, base + NvRegMisc1);
33008c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
33018c2ecf20Sopenharmony_ci		} else {
33028c2ecf20Sopenharmony_ci			writel(NVREG_TX_PAUSEFRAME_DISABLE,  base + NvRegTxPauseFrame);
33038c2ecf20Sopenharmony_ci			writel(regmisc, base + NvRegMisc1);
33048c2ecf20Sopenharmony_ci		}
33058c2ecf20Sopenharmony_ci	}
33068c2ecf20Sopenharmony_ci}
33078c2ecf20Sopenharmony_ci
33088c2ecf20Sopenharmony_cistatic void nv_force_linkspeed(struct net_device *dev, int speed, int duplex)
33098c2ecf20Sopenharmony_ci{
33108c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
33118c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
33128c2ecf20Sopenharmony_ci	u32 phyreg, txreg;
33138c2ecf20Sopenharmony_ci	int mii_status;
33148c2ecf20Sopenharmony_ci
33158c2ecf20Sopenharmony_ci	np->linkspeed = NVREG_LINKSPEED_FORCE|speed;
33168c2ecf20Sopenharmony_ci	np->duplex = duplex;
33178c2ecf20Sopenharmony_ci
33188c2ecf20Sopenharmony_ci	/* see if gigabit phy */
33198c2ecf20Sopenharmony_ci	mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
33208c2ecf20Sopenharmony_ci	if (mii_status & PHY_GIGABIT) {
33218c2ecf20Sopenharmony_ci		np->gigabit = PHY_GIGABIT;
33228c2ecf20Sopenharmony_ci		phyreg = readl(base + NvRegSlotTime);
33238c2ecf20Sopenharmony_ci		phyreg &= ~(0x3FF00);
33248c2ecf20Sopenharmony_ci		if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_10)
33258c2ecf20Sopenharmony_ci			phyreg |= NVREG_SLOTTIME_10_100_FULL;
33268c2ecf20Sopenharmony_ci		else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
33278c2ecf20Sopenharmony_ci			phyreg |= NVREG_SLOTTIME_10_100_FULL;
33288c2ecf20Sopenharmony_ci		else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
33298c2ecf20Sopenharmony_ci			phyreg |= NVREG_SLOTTIME_1000_FULL;
33308c2ecf20Sopenharmony_ci		writel(phyreg, base + NvRegSlotTime);
33318c2ecf20Sopenharmony_ci	}
33328c2ecf20Sopenharmony_ci
33338c2ecf20Sopenharmony_ci	phyreg = readl(base + NvRegPhyInterface);
33348c2ecf20Sopenharmony_ci	phyreg &= ~(PHY_HALF|PHY_100|PHY_1000);
33358c2ecf20Sopenharmony_ci	if (np->duplex == 0)
33368c2ecf20Sopenharmony_ci		phyreg |= PHY_HALF;
33378c2ecf20Sopenharmony_ci	if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_100)
33388c2ecf20Sopenharmony_ci		phyreg |= PHY_100;
33398c2ecf20Sopenharmony_ci	else if ((np->linkspeed & NVREG_LINKSPEED_MASK) ==
33408c2ecf20Sopenharmony_ci							NVREG_LINKSPEED_1000)
33418c2ecf20Sopenharmony_ci		phyreg |= PHY_1000;
33428c2ecf20Sopenharmony_ci	writel(phyreg, base + NvRegPhyInterface);
33438c2ecf20Sopenharmony_ci
33448c2ecf20Sopenharmony_ci	if (phyreg & PHY_RGMII) {
33458c2ecf20Sopenharmony_ci		if ((np->linkspeed & NVREG_LINKSPEED_MASK) ==
33468c2ecf20Sopenharmony_ci							NVREG_LINKSPEED_1000)
33478c2ecf20Sopenharmony_ci			txreg = NVREG_TX_DEFERRAL_RGMII_1000;
33488c2ecf20Sopenharmony_ci		else
33498c2ecf20Sopenharmony_ci			txreg = NVREG_TX_DEFERRAL_RGMII_10_100;
33508c2ecf20Sopenharmony_ci	} else {
33518c2ecf20Sopenharmony_ci		txreg = NVREG_TX_DEFERRAL_DEFAULT;
33528c2ecf20Sopenharmony_ci	}
33538c2ecf20Sopenharmony_ci	writel(txreg, base + NvRegTxDeferral);
33548c2ecf20Sopenharmony_ci
33558c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1) {
33568c2ecf20Sopenharmony_ci		txreg = NVREG_TX_WM_DESC1_DEFAULT;
33578c2ecf20Sopenharmony_ci	} else {
33588c2ecf20Sopenharmony_ci		if ((np->linkspeed & NVREG_LINKSPEED_MASK) ==
33598c2ecf20Sopenharmony_ci					 NVREG_LINKSPEED_1000)
33608c2ecf20Sopenharmony_ci			txreg = NVREG_TX_WM_DESC2_3_1000;
33618c2ecf20Sopenharmony_ci		else
33628c2ecf20Sopenharmony_ci			txreg = NVREG_TX_WM_DESC2_3_DEFAULT;
33638c2ecf20Sopenharmony_ci	}
33648c2ecf20Sopenharmony_ci	writel(txreg, base + NvRegTxWatermark);
33658c2ecf20Sopenharmony_ci
33668c2ecf20Sopenharmony_ci	writel(NVREG_MISC1_FORCE | (np->duplex ? 0 : NVREG_MISC1_HD),
33678c2ecf20Sopenharmony_ci			base + NvRegMisc1);
33688c2ecf20Sopenharmony_ci	pci_push(base);
33698c2ecf20Sopenharmony_ci	writel(np->linkspeed, base + NvRegLinkSpeed);
33708c2ecf20Sopenharmony_ci	pci_push(base);
33718c2ecf20Sopenharmony_ci}
33728c2ecf20Sopenharmony_ci
33738c2ecf20Sopenharmony_ci/**
33748c2ecf20Sopenharmony_ci * nv_update_linkspeed - Setup the MAC according to the link partner
33758c2ecf20Sopenharmony_ci * @dev: Network device to be configured
33768c2ecf20Sopenharmony_ci *
33778c2ecf20Sopenharmony_ci * The function queries the PHY and checks if there is a link partner.
33788c2ecf20Sopenharmony_ci * If yes, then it sets up the MAC accordingly. Otherwise, the MAC is
33798c2ecf20Sopenharmony_ci * set to 10 MBit HD.
33808c2ecf20Sopenharmony_ci *
33818c2ecf20Sopenharmony_ci * The function returns 0 if there is no link partner and 1 if there is
33828c2ecf20Sopenharmony_ci * a good link partner.
33838c2ecf20Sopenharmony_ci */
33848c2ecf20Sopenharmony_cistatic int nv_update_linkspeed(struct net_device *dev)
33858c2ecf20Sopenharmony_ci{
33868c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
33878c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
33888c2ecf20Sopenharmony_ci	int adv = 0;
33898c2ecf20Sopenharmony_ci	int lpa = 0;
33908c2ecf20Sopenharmony_ci	int adv_lpa, adv_pause, lpa_pause;
33918c2ecf20Sopenharmony_ci	int newls = np->linkspeed;
33928c2ecf20Sopenharmony_ci	int newdup = np->duplex;
33938c2ecf20Sopenharmony_ci	int mii_status;
33948c2ecf20Sopenharmony_ci	u32 bmcr;
33958c2ecf20Sopenharmony_ci	int retval = 0;
33968c2ecf20Sopenharmony_ci	u32 control_1000, status_1000, phyreg, pause_flags, txreg;
33978c2ecf20Sopenharmony_ci	u32 txrxFlags = 0;
33988c2ecf20Sopenharmony_ci	u32 phy_exp;
33998c2ecf20Sopenharmony_ci
34008c2ecf20Sopenharmony_ci	/* If device loopback is enabled, set carrier on and enable max link
34018c2ecf20Sopenharmony_ci	 * speed.
34028c2ecf20Sopenharmony_ci	 */
34038c2ecf20Sopenharmony_ci	bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
34048c2ecf20Sopenharmony_ci	if (bmcr & BMCR_LOOPBACK) {
34058c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
34068c2ecf20Sopenharmony_ci			nv_force_linkspeed(dev, NVREG_LINKSPEED_1000, 1);
34078c2ecf20Sopenharmony_ci			if (!netif_carrier_ok(dev))
34088c2ecf20Sopenharmony_ci				netif_carrier_on(dev);
34098c2ecf20Sopenharmony_ci		}
34108c2ecf20Sopenharmony_ci		return 1;
34118c2ecf20Sopenharmony_ci	}
34128c2ecf20Sopenharmony_ci
34138c2ecf20Sopenharmony_ci	/* BMSR_LSTATUS is latched, read it twice:
34148c2ecf20Sopenharmony_ci	 * we want the current value.
34158c2ecf20Sopenharmony_ci	 */
34168c2ecf20Sopenharmony_ci	mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
34178c2ecf20Sopenharmony_ci	mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
34188c2ecf20Sopenharmony_ci
34198c2ecf20Sopenharmony_ci	if (!(mii_status & BMSR_LSTATUS)) {
34208c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34218c2ecf20Sopenharmony_ci		newdup = 0;
34228c2ecf20Sopenharmony_ci		retval = 0;
34238c2ecf20Sopenharmony_ci		goto set_speed;
34248c2ecf20Sopenharmony_ci	}
34258c2ecf20Sopenharmony_ci
34268c2ecf20Sopenharmony_ci	if (np->autoneg == 0) {
34278c2ecf20Sopenharmony_ci		if (np->fixed_mode & LPA_100FULL) {
34288c2ecf20Sopenharmony_ci			newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
34298c2ecf20Sopenharmony_ci			newdup = 1;
34308c2ecf20Sopenharmony_ci		} else if (np->fixed_mode & LPA_100HALF) {
34318c2ecf20Sopenharmony_ci			newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
34328c2ecf20Sopenharmony_ci			newdup = 0;
34338c2ecf20Sopenharmony_ci		} else if (np->fixed_mode & LPA_10FULL) {
34348c2ecf20Sopenharmony_ci			newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34358c2ecf20Sopenharmony_ci			newdup = 1;
34368c2ecf20Sopenharmony_ci		} else {
34378c2ecf20Sopenharmony_ci			newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34388c2ecf20Sopenharmony_ci			newdup = 0;
34398c2ecf20Sopenharmony_ci		}
34408c2ecf20Sopenharmony_ci		retval = 1;
34418c2ecf20Sopenharmony_ci		goto set_speed;
34428c2ecf20Sopenharmony_ci	}
34438c2ecf20Sopenharmony_ci	/* check auto negotiation is complete */
34448c2ecf20Sopenharmony_ci	if (!(mii_status & BMSR_ANEGCOMPLETE)) {
34458c2ecf20Sopenharmony_ci		/* still in autonegotiation - configure nic for 10 MBit HD and wait. */
34468c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34478c2ecf20Sopenharmony_ci		newdup = 0;
34488c2ecf20Sopenharmony_ci		retval = 0;
34498c2ecf20Sopenharmony_ci		goto set_speed;
34508c2ecf20Sopenharmony_ci	}
34518c2ecf20Sopenharmony_ci
34528c2ecf20Sopenharmony_ci	adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
34538c2ecf20Sopenharmony_ci	lpa = mii_rw(dev, np->phyaddr, MII_LPA, MII_READ);
34548c2ecf20Sopenharmony_ci
34558c2ecf20Sopenharmony_ci	retval = 1;
34568c2ecf20Sopenharmony_ci	if (np->gigabit == PHY_GIGABIT) {
34578c2ecf20Sopenharmony_ci		control_1000 = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
34588c2ecf20Sopenharmony_ci		status_1000 = mii_rw(dev, np->phyaddr, MII_STAT1000, MII_READ);
34598c2ecf20Sopenharmony_ci
34608c2ecf20Sopenharmony_ci		if ((control_1000 & ADVERTISE_1000FULL) &&
34618c2ecf20Sopenharmony_ci			(status_1000 & LPA_1000FULL)) {
34628c2ecf20Sopenharmony_ci			newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_1000;
34638c2ecf20Sopenharmony_ci			newdup = 1;
34648c2ecf20Sopenharmony_ci			goto set_speed;
34658c2ecf20Sopenharmony_ci		}
34668c2ecf20Sopenharmony_ci	}
34678c2ecf20Sopenharmony_ci
34688c2ecf20Sopenharmony_ci	/* FIXME: handle parallel detection properly */
34698c2ecf20Sopenharmony_ci	adv_lpa = lpa & adv;
34708c2ecf20Sopenharmony_ci	if (adv_lpa & LPA_100FULL) {
34718c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
34728c2ecf20Sopenharmony_ci		newdup = 1;
34738c2ecf20Sopenharmony_ci	} else if (adv_lpa & LPA_100HALF) {
34748c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_100;
34758c2ecf20Sopenharmony_ci		newdup = 0;
34768c2ecf20Sopenharmony_ci	} else if (adv_lpa & LPA_10FULL) {
34778c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34788c2ecf20Sopenharmony_ci		newdup = 1;
34798c2ecf20Sopenharmony_ci	} else if (adv_lpa & LPA_10HALF) {
34808c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34818c2ecf20Sopenharmony_ci		newdup = 0;
34828c2ecf20Sopenharmony_ci	} else {
34838c2ecf20Sopenharmony_ci		newls = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
34848c2ecf20Sopenharmony_ci		newdup = 0;
34858c2ecf20Sopenharmony_ci	}
34868c2ecf20Sopenharmony_ci
34878c2ecf20Sopenharmony_ciset_speed:
34888c2ecf20Sopenharmony_ci	if (np->duplex == newdup && np->linkspeed == newls)
34898c2ecf20Sopenharmony_ci		return retval;
34908c2ecf20Sopenharmony_ci
34918c2ecf20Sopenharmony_ci	np->duplex = newdup;
34928c2ecf20Sopenharmony_ci	np->linkspeed = newls;
34938c2ecf20Sopenharmony_ci
34948c2ecf20Sopenharmony_ci	/* The transmitter and receiver must be restarted for safe update */
34958c2ecf20Sopenharmony_ci	if (readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_START) {
34968c2ecf20Sopenharmony_ci		txrxFlags |= NV_RESTART_TX;
34978c2ecf20Sopenharmony_ci		nv_stop_tx(dev);
34988c2ecf20Sopenharmony_ci	}
34998c2ecf20Sopenharmony_ci	if (readl(base + NvRegReceiverControl) & NVREG_RCVCTL_START) {
35008c2ecf20Sopenharmony_ci		txrxFlags |= NV_RESTART_RX;
35018c2ecf20Sopenharmony_ci		nv_stop_rx(dev);
35028c2ecf20Sopenharmony_ci	}
35038c2ecf20Sopenharmony_ci
35048c2ecf20Sopenharmony_ci	if (np->gigabit == PHY_GIGABIT) {
35058c2ecf20Sopenharmony_ci		phyreg = readl(base + NvRegSlotTime);
35068c2ecf20Sopenharmony_ci		phyreg &= ~(0x3FF00);
35078c2ecf20Sopenharmony_ci		if (((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_10) ||
35088c2ecf20Sopenharmony_ci		    ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100))
35098c2ecf20Sopenharmony_ci			phyreg |= NVREG_SLOTTIME_10_100_FULL;
35108c2ecf20Sopenharmony_ci		else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
35118c2ecf20Sopenharmony_ci			phyreg |= NVREG_SLOTTIME_1000_FULL;
35128c2ecf20Sopenharmony_ci		writel(phyreg, base + NvRegSlotTime);
35138c2ecf20Sopenharmony_ci	}
35148c2ecf20Sopenharmony_ci
35158c2ecf20Sopenharmony_ci	phyreg = readl(base + NvRegPhyInterface);
35168c2ecf20Sopenharmony_ci	phyreg &= ~(PHY_HALF|PHY_100|PHY_1000);
35178c2ecf20Sopenharmony_ci	if (np->duplex == 0)
35188c2ecf20Sopenharmony_ci		phyreg |= PHY_HALF;
35198c2ecf20Sopenharmony_ci	if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_100)
35208c2ecf20Sopenharmony_ci		phyreg |= PHY_100;
35218c2ecf20Sopenharmony_ci	else if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000)
35228c2ecf20Sopenharmony_ci		phyreg |= PHY_1000;
35238c2ecf20Sopenharmony_ci	writel(phyreg, base + NvRegPhyInterface);
35248c2ecf20Sopenharmony_ci
35258c2ecf20Sopenharmony_ci	phy_exp = mii_rw(dev, np->phyaddr, MII_EXPANSION, MII_READ) & EXPANSION_NWAY; /* autoneg capable */
35268c2ecf20Sopenharmony_ci	if (phyreg & PHY_RGMII) {
35278c2ecf20Sopenharmony_ci		if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000) {
35288c2ecf20Sopenharmony_ci			txreg = NVREG_TX_DEFERRAL_RGMII_1000;
35298c2ecf20Sopenharmony_ci		} else {
35308c2ecf20Sopenharmony_ci			if (!phy_exp && !np->duplex && (np->driver_data & DEV_HAS_COLLISION_FIX)) {
35318c2ecf20Sopenharmony_ci				if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_10)
35328c2ecf20Sopenharmony_ci					txreg = NVREG_TX_DEFERRAL_RGMII_STRETCH_10;
35338c2ecf20Sopenharmony_ci				else
35348c2ecf20Sopenharmony_ci					txreg = NVREG_TX_DEFERRAL_RGMII_STRETCH_100;
35358c2ecf20Sopenharmony_ci			} else {
35368c2ecf20Sopenharmony_ci				txreg = NVREG_TX_DEFERRAL_RGMII_10_100;
35378c2ecf20Sopenharmony_ci			}
35388c2ecf20Sopenharmony_ci		}
35398c2ecf20Sopenharmony_ci	} else {
35408c2ecf20Sopenharmony_ci		if (!phy_exp && !np->duplex && (np->driver_data & DEV_HAS_COLLISION_FIX))
35418c2ecf20Sopenharmony_ci			txreg = NVREG_TX_DEFERRAL_MII_STRETCH;
35428c2ecf20Sopenharmony_ci		else
35438c2ecf20Sopenharmony_ci			txreg = NVREG_TX_DEFERRAL_DEFAULT;
35448c2ecf20Sopenharmony_ci	}
35458c2ecf20Sopenharmony_ci	writel(txreg, base + NvRegTxDeferral);
35468c2ecf20Sopenharmony_ci
35478c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1) {
35488c2ecf20Sopenharmony_ci		txreg = NVREG_TX_WM_DESC1_DEFAULT;
35498c2ecf20Sopenharmony_ci	} else {
35508c2ecf20Sopenharmony_ci		if ((np->linkspeed & NVREG_LINKSPEED_MASK) == NVREG_LINKSPEED_1000)
35518c2ecf20Sopenharmony_ci			txreg = NVREG_TX_WM_DESC2_3_1000;
35528c2ecf20Sopenharmony_ci		else
35538c2ecf20Sopenharmony_ci			txreg = NVREG_TX_WM_DESC2_3_DEFAULT;
35548c2ecf20Sopenharmony_ci	}
35558c2ecf20Sopenharmony_ci	writel(txreg, base + NvRegTxWatermark);
35568c2ecf20Sopenharmony_ci
35578c2ecf20Sopenharmony_ci	writel(NVREG_MISC1_FORCE | (np->duplex ? 0 : NVREG_MISC1_HD),
35588c2ecf20Sopenharmony_ci		base + NvRegMisc1);
35598c2ecf20Sopenharmony_ci	pci_push(base);
35608c2ecf20Sopenharmony_ci	writel(np->linkspeed, base + NvRegLinkSpeed);
35618c2ecf20Sopenharmony_ci	pci_push(base);
35628c2ecf20Sopenharmony_ci
35638c2ecf20Sopenharmony_ci	pause_flags = 0;
35648c2ecf20Sopenharmony_ci	/* setup pause frame */
35658c2ecf20Sopenharmony_ci	if (netif_running(dev) && (np->duplex != 0)) {
35668c2ecf20Sopenharmony_ci		if (np->autoneg && np->pause_flags & NV_PAUSEFRAME_AUTONEG) {
35678c2ecf20Sopenharmony_ci			adv_pause = adv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
35688c2ecf20Sopenharmony_ci			lpa_pause = lpa & (LPA_PAUSE_CAP | LPA_PAUSE_ASYM);
35698c2ecf20Sopenharmony_ci
35708c2ecf20Sopenharmony_ci			switch (adv_pause) {
35718c2ecf20Sopenharmony_ci			case ADVERTISE_PAUSE_CAP:
35728c2ecf20Sopenharmony_ci				if (lpa_pause & LPA_PAUSE_CAP) {
35738c2ecf20Sopenharmony_ci					pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
35748c2ecf20Sopenharmony_ci					if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
35758c2ecf20Sopenharmony_ci						pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
35768c2ecf20Sopenharmony_ci				}
35778c2ecf20Sopenharmony_ci				break;
35788c2ecf20Sopenharmony_ci			case ADVERTISE_PAUSE_ASYM:
35798c2ecf20Sopenharmony_ci				if (lpa_pause == (LPA_PAUSE_CAP | LPA_PAUSE_ASYM))
35808c2ecf20Sopenharmony_ci					pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
35818c2ecf20Sopenharmony_ci				break;
35828c2ecf20Sopenharmony_ci			case ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM:
35838c2ecf20Sopenharmony_ci				if (lpa_pause & LPA_PAUSE_CAP) {
35848c2ecf20Sopenharmony_ci					pause_flags |=  NV_PAUSEFRAME_RX_ENABLE;
35858c2ecf20Sopenharmony_ci					if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
35868c2ecf20Sopenharmony_ci						pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
35878c2ecf20Sopenharmony_ci				}
35888c2ecf20Sopenharmony_ci				if (lpa_pause == LPA_PAUSE_ASYM)
35898c2ecf20Sopenharmony_ci					pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
35908c2ecf20Sopenharmony_ci				break;
35918c2ecf20Sopenharmony_ci			}
35928c2ecf20Sopenharmony_ci		} else {
35938c2ecf20Sopenharmony_ci			pause_flags = np->pause_flags;
35948c2ecf20Sopenharmony_ci		}
35958c2ecf20Sopenharmony_ci	}
35968c2ecf20Sopenharmony_ci	nv_update_pause(dev, pause_flags);
35978c2ecf20Sopenharmony_ci
35988c2ecf20Sopenharmony_ci	if (txrxFlags & NV_RESTART_TX)
35998c2ecf20Sopenharmony_ci		nv_start_tx(dev);
36008c2ecf20Sopenharmony_ci	if (txrxFlags & NV_RESTART_RX)
36018c2ecf20Sopenharmony_ci		nv_start_rx(dev);
36028c2ecf20Sopenharmony_ci
36038c2ecf20Sopenharmony_ci	return retval;
36048c2ecf20Sopenharmony_ci}
36058c2ecf20Sopenharmony_ci
36068c2ecf20Sopenharmony_cistatic void nv_linkchange(struct net_device *dev)
36078c2ecf20Sopenharmony_ci{
36088c2ecf20Sopenharmony_ci	if (nv_update_linkspeed(dev)) {
36098c2ecf20Sopenharmony_ci		if (!netif_carrier_ok(dev)) {
36108c2ecf20Sopenharmony_ci			netif_carrier_on(dev);
36118c2ecf20Sopenharmony_ci			netdev_info(dev, "link up\n");
36128c2ecf20Sopenharmony_ci			nv_txrx_gate(dev, false);
36138c2ecf20Sopenharmony_ci			nv_start_rx(dev);
36148c2ecf20Sopenharmony_ci		}
36158c2ecf20Sopenharmony_ci	} else {
36168c2ecf20Sopenharmony_ci		if (netif_carrier_ok(dev)) {
36178c2ecf20Sopenharmony_ci			netif_carrier_off(dev);
36188c2ecf20Sopenharmony_ci			netdev_info(dev, "link down\n");
36198c2ecf20Sopenharmony_ci			nv_txrx_gate(dev, true);
36208c2ecf20Sopenharmony_ci			nv_stop_rx(dev);
36218c2ecf20Sopenharmony_ci		}
36228c2ecf20Sopenharmony_ci	}
36238c2ecf20Sopenharmony_ci}
36248c2ecf20Sopenharmony_ci
36258c2ecf20Sopenharmony_cistatic void nv_link_irq(struct net_device *dev)
36268c2ecf20Sopenharmony_ci{
36278c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
36288c2ecf20Sopenharmony_ci	u32 miistat;
36298c2ecf20Sopenharmony_ci
36308c2ecf20Sopenharmony_ci	miistat = readl(base + NvRegMIIStatus);
36318c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_LINKCHANGE, base + NvRegMIIStatus);
36328c2ecf20Sopenharmony_ci
36338c2ecf20Sopenharmony_ci	if (miistat & (NVREG_MIISTAT_LINKCHANGE))
36348c2ecf20Sopenharmony_ci		nv_linkchange(dev);
36358c2ecf20Sopenharmony_ci}
36368c2ecf20Sopenharmony_ci
36378c2ecf20Sopenharmony_cistatic void nv_msi_workaround(struct fe_priv *np)
36388c2ecf20Sopenharmony_ci{
36398c2ecf20Sopenharmony_ci
36408c2ecf20Sopenharmony_ci	/* Need to toggle the msi irq mask within the ethernet device,
36418c2ecf20Sopenharmony_ci	 * otherwise, future interrupts will not be detected.
36428c2ecf20Sopenharmony_ci	 */
36438c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_ENABLED) {
36448c2ecf20Sopenharmony_ci		u8 __iomem *base = np->base;
36458c2ecf20Sopenharmony_ci
36468c2ecf20Sopenharmony_ci		writel(0, base + NvRegMSIIrqMask);
36478c2ecf20Sopenharmony_ci		writel(NVREG_MSI_VECTOR_0_ENABLED, base + NvRegMSIIrqMask);
36488c2ecf20Sopenharmony_ci	}
36498c2ecf20Sopenharmony_ci}
36508c2ecf20Sopenharmony_ci
36518c2ecf20Sopenharmony_cistatic inline int nv_change_interrupt_mode(struct net_device *dev, int total_work)
36528c2ecf20Sopenharmony_ci{
36538c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
36548c2ecf20Sopenharmony_ci
36558c2ecf20Sopenharmony_ci	if (optimization_mode == NV_OPTIMIZATION_MODE_DYNAMIC) {
36568c2ecf20Sopenharmony_ci		if (total_work > NV_DYNAMIC_THRESHOLD) {
36578c2ecf20Sopenharmony_ci			/* transition to poll based interrupts */
36588c2ecf20Sopenharmony_ci			np->quiet_count = 0;
36598c2ecf20Sopenharmony_ci			if (np->irqmask != NVREG_IRQMASK_CPU) {
36608c2ecf20Sopenharmony_ci				np->irqmask = NVREG_IRQMASK_CPU;
36618c2ecf20Sopenharmony_ci				return 1;
36628c2ecf20Sopenharmony_ci			}
36638c2ecf20Sopenharmony_ci		} else {
36648c2ecf20Sopenharmony_ci			if (np->quiet_count < NV_DYNAMIC_MAX_QUIET_COUNT) {
36658c2ecf20Sopenharmony_ci				np->quiet_count++;
36668c2ecf20Sopenharmony_ci			} else {
36678c2ecf20Sopenharmony_ci				/* reached a period of low activity, switch
36688c2ecf20Sopenharmony_ci				   to per tx/rx packet interrupts */
36698c2ecf20Sopenharmony_ci				if (np->irqmask != NVREG_IRQMASK_THROUGHPUT) {
36708c2ecf20Sopenharmony_ci					np->irqmask = NVREG_IRQMASK_THROUGHPUT;
36718c2ecf20Sopenharmony_ci					return 1;
36728c2ecf20Sopenharmony_ci				}
36738c2ecf20Sopenharmony_ci			}
36748c2ecf20Sopenharmony_ci		}
36758c2ecf20Sopenharmony_ci	}
36768c2ecf20Sopenharmony_ci	return 0;
36778c2ecf20Sopenharmony_ci}
36788c2ecf20Sopenharmony_ci
36798c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq(int foo, void *data)
36808c2ecf20Sopenharmony_ci{
36818c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
36828c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
36838c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
36848c2ecf20Sopenharmony_ci
36858c2ecf20Sopenharmony_ci	if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
36868c2ecf20Sopenharmony_ci		np->events = readl(base + NvRegIrqStatus);
36878c2ecf20Sopenharmony_ci		writel(np->events, base + NvRegIrqStatus);
36888c2ecf20Sopenharmony_ci	} else {
36898c2ecf20Sopenharmony_ci		np->events = readl(base + NvRegMSIXIrqStatus);
36908c2ecf20Sopenharmony_ci		writel(np->events, base + NvRegMSIXIrqStatus);
36918c2ecf20Sopenharmony_ci	}
36928c2ecf20Sopenharmony_ci	if (!(np->events & np->irqmask))
36938c2ecf20Sopenharmony_ci		return IRQ_NONE;
36948c2ecf20Sopenharmony_ci
36958c2ecf20Sopenharmony_ci	nv_msi_workaround(np);
36968c2ecf20Sopenharmony_ci
36978c2ecf20Sopenharmony_ci	if (napi_schedule_prep(&np->napi)) {
36988c2ecf20Sopenharmony_ci		/*
36998c2ecf20Sopenharmony_ci		 * Disable further irq's (msix not enabled with napi)
37008c2ecf20Sopenharmony_ci		 */
37018c2ecf20Sopenharmony_ci		writel(0, base + NvRegIrqMask);
37028c2ecf20Sopenharmony_ci		__napi_schedule(&np->napi);
37038c2ecf20Sopenharmony_ci	}
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
37068c2ecf20Sopenharmony_ci}
37078c2ecf20Sopenharmony_ci
37088c2ecf20Sopenharmony_ci/* All _optimized functions are used to help increase performance
37098c2ecf20Sopenharmony_ci * (reduce CPU and increase throughput). They use descripter version 3,
37108c2ecf20Sopenharmony_ci * compiler directives, and reduce memory accesses.
37118c2ecf20Sopenharmony_ci */
37128c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq_optimized(int foo, void *data)
37138c2ecf20Sopenharmony_ci{
37148c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
37158c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
37168c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
37178c2ecf20Sopenharmony_ci
37188c2ecf20Sopenharmony_ci	if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
37198c2ecf20Sopenharmony_ci		np->events = readl(base + NvRegIrqStatus);
37208c2ecf20Sopenharmony_ci		writel(np->events, base + NvRegIrqStatus);
37218c2ecf20Sopenharmony_ci	} else {
37228c2ecf20Sopenharmony_ci		np->events = readl(base + NvRegMSIXIrqStatus);
37238c2ecf20Sopenharmony_ci		writel(np->events, base + NvRegMSIXIrqStatus);
37248c2ecf20Sopenharmony_ci	}
37258c2ecf20Sopenharmony_ci	if (!(np->events & np->irqmask))
37268c2ecf20Sopenharmony_ci		return IRQ_NONE;
37278c2ecf20Sopenharmony_ci
37288c2ecf20Sopenharmony_ci	nv_msi_workaround(np);
37298c2ecf20Sopenharmony_ci
37308c2ecf20Sopenharmony_ci	if (napi_schedule_prep(&np->napi)) {
37318c2ecf20Sopenharmony_ci		/*
37328c2ecf20Sopenharmony_ci		 * Disable further irq's (msix not enabled with napi)
37338c2ecf20Sopenharmony_ci		 */
37348c2ecf20Sopenharmony_ci		writel(0, base + NvRegIrqMask);
37358c2ecf20Sopenharmony_ci		__napi_schedule(&np->napi);
37368c2ecf20Sopenharmony_ci	}
37378c2ecf20Sopenharmony_ci
37388c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
37398c2ecf20Sopenharmony_ci}
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq_tx(int foo, void *data)
37428c2ecf20Sopenharmony_ci{
37438c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
37448c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
37458c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
37468c2ecf20Sopenharmony_ci	u32 events;
37478c2ecf20Sopenharmony_ci	int i;
37488c2ecf20Sopenharmony_ci	unsigned long flags;
37498c2ecf20Sopenharmony_ci
37508c2ecf20Sopenharmony_ci	for (i = 0;; i++) {
37518c2ecf20Sopenharmony_ci		events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_TX_ALL;
37528c2ecf20Sopenharmony_ci		writel(events, base + NvRegMSIXIrqStatus);
37538c2ecf20Sopenharmony_ci		netdev_dbg(dev, "tx irq events: %08x\n", events);
37548c2ecf20Sopenharmony_ci		if (!(events & np->irqmask))
37558c2ecf20Sopenharmony_ci			break;
37568c2ecf20Sopenharmony_ci
37578c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
37588c2ecf20Sopenharmony_ci		nv_tx_done_optimized(dev, TX_WORK_PER_LOOP);
37598c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
37608c2ecf20Sopenharmony_ci
37618c2ecf20Sopenharmony_ci		if (unlikely(i > max_interrupt_work)) {
37628c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
37638c2ecf20Sopenharmony_ci			/* disable interrupts on the nic */
37648c2ecf20Sopenharmony_ci			writel(NVREG_IRQ_TX_ALL, base + NvRegIrqMask);
37658c2ecf20Sopenharmony_ci			pci_push(base);
37668c2ecf20Sopenharmony_ci
37678c2ecf20Sopenharmony_ci			if (!np->in_shutdown) {
37688c2ecf20Sopenharmony_ci				np->nic_poll_irq |= NVREG_IRQ_TX_ALL;
37698c2ecf20Sopenharmony_ci				mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
37708c2ecf20Sopenharmony_ci			}
37718c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
37728c2ecf20Sopenharmony_ci			netdev_dbg(dev, "%s: too many iterations (%d)\n",
37738c2ecf20Sopenharmony_ci				   __func__, i);
37748c2ecf20Sopenharmony_ci			break;
37758c2ecf20Sopenharmony_ci		}
37768c2ecf20Sopenharmony_ci
37778c2ecf20Sopenharmony_ci	}
37788c2ecf20Sopenharmony_ci
37798c2ecf20Sopenharmony_ci	return IRQ_RETVAL(i);
37808c2ecf20Sopenharmony_ci}
37818c2ecf20Sopenharmony_ci
37828c2ecf20Sopenharmony_cistatic int nv_napi_poll(struct napi_struct *napi, int budget)
37838c2ecf20Sopenharmony_ci{
37848c2ecf20Sopenharmony_ci	struct fe_priv *np = container_of(napi, struct fe_priv, napi);
37858c2ecf20Sopenharmony_ci	struct net_device *dev = np->dev;
37868c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
37878c2ecf20Sopenharmony_ci	unsigned long flags;
37888c2ecf20Sopenharmony_ci	int retcode;
37898c2ecf20Sopenharmony_ci	int rx_count, tx_work = 0, rx_work = 0;
37908c2ecf20Sopenharmony_ci
37918c2ecf20Sopenharmony_ci	do {
37928c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
37938c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
37948c2ecf20Sopenharmony_ci			tx_work += nv_tx_done(dev, np->tx_ring_size);
37958c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
37968c2ecf20Sopenharmony_ci
37978c2ecf20Sopenharmony_ci			rx_count = nv_rx_process(dev, budget - rx_work);
37988c2ecf20Sopenharmony_ci			retcode = nv_alloc_rx(dev);
37998c2ecf20Sopenharmony_ci		} else {
38008c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
38018c2ecf20Sopenharmony_ci			tx_work += nv_tx_done_optimized(dev, np->tx_ring_size);
38028c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
38038c2ecf20Sopenharmony_ci
38048c2ecf20Sopenharmony_ci			rx_count = nv_rx_process_optimized(dev,
38058c2ecf20Sopenharmony_ci			    budget - rx_work);
38068c2ecf20Sopenharmony_ci			retcode = nv_alloc_rx_optimized(dev);
38078c2ecf20Sopenharmony_ci		}
38088c2ecf20Sopenharmony_ci	} while (retcode == 0 &&
38098c2ecf20Sopenharmony_ci		 rx_count > 0 && (rx_work += rx_count) < budget);
38108c2ecf20Sopenharmony_ci
38118c2ecf20Sopenharmony_ci	if (retcode) {
38128c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
38138c2ecf20Sopenharmony_ci		if (!np->in_shutdown)
38148c2ecf20Sopenharmony_ci			mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
38158c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
38168c2ecf20Sopenharmony_ci	}
38178c2ecf20Sopenharmony_ci
38188c2ecf20Sopenharmony_ci	nv_change_interrupt_mode(dev, tx_work + rx_work);
38198c2ecf20Sopenharmony_ci
38208c2ecf20Sopenharmony_ci	if (unlikely(np->events & NVREG_IRQ_LINK)) {
38218c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
38228c2ecf20Sopenharmony_ci		nv_link_irq(dev);
38238c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
38248c2ecf20Sopenharmony_ci	}
38258c2ecf20Sopenharmony_ci	if (unlikely(np->need_linktimer && time_after(jiffies, np->link_timeout))) {
38268c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
38278c2ecf20Sopenharmony_ci		nv_linkchange(dev);
38288c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
38298c2ecf20Sopenharmony_ci		np->link_timeout = jiffies + LINK_TIMEOUT;
38308c2ecf20Sopenharmony_ci	}
38318c2ecf20Sopenharmony_ci	if (unlikely(np->events & NVREG_IRQ_RECOVER_ERROR)) {
38328c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
38338c2ecf20Sopenharmony_ci		if (!np->in_shutdown) {
38348c2ecf20Sopenharmony_ci			np->nic_poll_irq = np->irqmask;
38358c2ecf20Sopenharmony_ci			np->recover_error = 1;
38368c2ecf20Sopenharmony_ci			mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
38378c2ecf20Sopenharmony_ci		}
38388c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
38398c2ecf20Sopenharmony_ci		napi_complete(napi);
38408c2ecf20Sopenharmony_ci		return rx_work;
38418c2ecf20Sopenharmony_ci	}
38428c2ecf20Sopenharmony_ci
38438c2ecf20Sopenharmony_ci	if (rx_work < budget) {
38448c2ecf20Sopenharmony_ci		/* re-enable interrupts
38458c2ecf20Sopenharmony_ci		   (msix not enabled in napi) */
38468c2ecf20Sopenharmony_ci		napi_complete_done(napi, rx_work);
38478c2ecf20Sopenharmony_ci
38488c2ecf20Sopenharmony_ci		writel(np->irqmask, base + NvRegIrqMask);
38498c2ecf20Sopenharmony_ci	}
38508c2ecf20Sopenharmony_ci	return rx_work;
38518c2ecf20Sopenharmony_ci}
38528c2ecf20Sopenharmony_ci
38538c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq_rx(int foo, void *data)
38548c2ecf20Sopenharmony_ci{
38558c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
38568c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
38578c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
38588c2ecf20Sopenharmony_ci	u32 events;
38598c2ecf20Sopenharmony_ci	int i;
38608c2ecf20Sopenharmony_ci	unsigned long flags;
38618c2ecf20Sopenharmony_ci
38628c2ecf20Sopenharmony_ci	for (i = 0;; i++) {
38638c2ecf20Sopenharmony_ci		events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_RX_ALL;
38648c2ecf20Sopenharmony_ci		writel(events, base + NvRegMSIXIrqStatus);
38658c2ecf20Sopenharmony_ci		netdev_dbg(dev, "rx irq events: %08x\n", events);
38668c2ecf20Sopenharmony_ci		if (!(events & np->irqmask))
38678c2ecf20Sopenharmony_ci			break;
38688c2ecf20Sopenharmony_ci
38698c2ecf20Sopenharmony_ci		if (nv_rx_process_optimized(dev, RX_WORK_PER_LOOP)) {
38708c2ecf20Sopenharmony_ci			if (unlikely(nv_alloc_rx_optimized(dev))) {
38718c2ecf20Sopenharmony_ci				spin_lock_irqsave(&np->lock, flags);
38728c2ecf20Sopenharmony_ci				if (!np->in_shutdown)
38738c2ecf20Sopenharmony_ci					mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
38748c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&np->lock, flags);
38758c2ecf20Sopenharmony_ci			}
38768c2ecf20Sopenharmony_ci		}
38778c2ecf20Sopenharmony_ci
38788c2ecf20Sopenharmony_ci		if (unlikely(i > max_interrupt_work)) {
38798c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
38808c2ecf20Sopenharmony_ci			/* disable interrupts on the nic */
38818c2ecf20Sopenharmony_ci			writel(NVREG_IRQ_RX_ALL, base + NvRegIrqMask);
38828c2ecf20Sopenharmony_ci			pci_push(base);
38838c2ecf20Sopenharmony_ci
38848c2ecf20Sopenharmony_ci			if (!np->in_shutdown) {
38858c2ecf20Sopenharmony_ci				np->nic_poll_irq |= NVREG_IRQ_RX_ALL;
38868c2ecf20Sopenharmony_ci				mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
38878c2ecf20Sopenharmony_ci			}
38888c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
38898c2ecf20Sopenharmony_ci			netdev_dbg(dev, "%s: too many iterations (%d)\n",
38908c2ecf20Sopenharmony_ci				   __func__, i);
38918c2ecf20Sopenharmony_ci			break;
38928c2ecf20Sopenharmony_ci		}
38938c2ecf20Sopenharmony_ci	}
38948c2ecf20Sopenharmony_ci
38958c2ecf20Sopenharmony_ci	return IRQ_RETVAL(i);
38968c2ecf20Sopenharmony_ci}
38978c2ecf20Sopenharmony_ci
38988c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq_other(int foo, void *data)
38998c2ecf20Sopenharmony_ci{
39008c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
39018c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
39028c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
39038c2ecf20Sopenharmony_ci	u32 events;
39048c2ecf20Sopenharmony_ci	int i;
39058c2ecf20Sopenharmony_ci	unsigned long flags;
39068c2ecf20Sopenharmony_ci
39078c2ecf20Sopenharmony_ci	for (i = 0;; i++) {
39088c2ecf20Sopenharmony_ci		events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQ_OTHER;
39098c2ecf20Sopenharmony_ci		writel(events, base + NvRegMSIXIrqStatus);
39108c2ecf20Sopenharmony_ci		netdev_dbg(dev, "irq events: %08x\n", events);
39118c2ecf20Sopenharmony_ci		if (!(events & np->irqmask))
39128c2ecf20Sopenharmony_ci			break;
39138c2ecf20Sopenharmony_ci
39148c2ecf20Sopenharmony_ci		/* check tx in case we reached max loop limit in tx isr */
39158c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
39168c2ecf20Sopenharmony_ci		nv_tx_done_optimized(dev, TX_WORK_PER_LOOP);
39178c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
39188c2ecf20Sopenharmony_ci
39198c2ecf20Sopenharmony_ci		if (events & NVREG_IRQ_LINK) {
39208c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
39218c2ecf20Sopenharmony_ci			nv_link_irq(dev);
39228c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
39238c2ecf20Sopenharmony_ci		}
39248c2ecf20Sopenharmony_ci		if (np->need_linktimer && time_after(jiffies, np->link_timeout)) {
39258c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
39268c2ecf20Sopenharmony_ci			nv_linkchange(dev);
39278c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
39288c2ecf20Sopenharmony_ci			np->link_timeout = jiffies + LINK_TIMEOUT;
39298c2ecf20Sopenharmony_ci		}
39308c2ecf20Sopenharmony_ci		if (events & NVREG_IRQ_RECOVER_ERROR) {
39318c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
39328c2ecf20Sopenharmony_ci			/* disable interrupts on the nic */
39338c2ecf20Sopenharmony_ci			writel(NVREG_IRQ_OTHER, base + NvRegIrqMask);
39348c2ecf20Sopenharmony_ci			pci_push(base);
39358c2ecf20Sopenharmony_ci
39368c2ecf20Sopenharmony_ci			if (!np->in_shutdown) {
39378c2ecf20Sopenharmony_ci				np->nic_poll_irq |= NVREG_IRQ_OTHER;
39388c2ecf20Sopenharmony_ci				np->recover_error = 1;
39398c2ecf20Sopenharmony_ci				mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
39408c2ecf20Sopenharmony_ci			}
39418c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
39428c2ecf20Sopenharmony_ci			break;
39438c2ecf20Sopenharmony_ci		}
39448c2ecf20Sopenharmony_ci		if (unlikely(i > max_interrupt_work)) {
39458c2ecf20Sopenharmony_ci			spin_lock_irqsave(&np->lock, flags);
39468c2ecf20Sopenharmony_ci			/* disable interrupts on the nic */
39478c2ecf20Sopenharmony_ci			writel(NVREG_IRQ_OTHER, base + NvRegIrqMask);
39488c2ecf20Sopenharmony_ci			pci_push(base);
39498c2ecf20Sopenharmony_ci
39508c2ecf20Sopenharmony_ci			if (!np->in_shutdown) {
39518c2ecf20Sopenharmony_ci				np->nic_poll_irq |= NVREG_IRQ_OTHER;
39528c2ecf20Sopenharmony_ci				mod_timer(&np->nic_poll, jiffies + POLL_WAIT);
39538c2ecf20Sopenharmony_ci			}
39548c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
39558c2ecf20Sopenharmony_ci			netdev_dbg(dev, "%s: too many iterations (%d)\n",
39568c2ecf20Sopenharmony_ci				   __func__, i);
39578c2ecf20Sopenharmony_ci			break;
39588c2ecf20Sopenharmony_ci		}
39598c2ecf20Sopenharmony_ci
39608c2ecf20Sopenharmony_ci	}
39618c2ecf20Sopenharmony_ci
39628c2ecf20Sopenharmony_ci	return IRQ_RETVAL(i);
39638c2ecf20Sopenharmony_ci}
39648c2ecf20Sopenharmony_ci
39658c2ecf20Sopenharmony_cistatic irqreturn_t nv_nic_irq_test(int foo, void *data)
39668c2ecf20Sopenharmony_ci{
39678c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *) data;
39688c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
39698c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
39708c2ecf20Sopenharmony_ci	u32 events;
39718c2ecf20Sopenharmony_ci
39728c2ecf20Sopenharmony_ci	if (!(np->msi_flags & NV_MSI_X_ENABLED)) {
39738c2ecf20Sopenharmony_ci		events = readl(base + NvRegIrqStatus) & NVREG_IRQSTAT_MASK;
39748c2ecf20Sopenharmony_ci		writel(events & NVREG_IRQ_TIMER, base + NvRegIrqStatus);
39758c2ecf20Sopenharmony_ci	} else {
39768c2ecf20Sopenharmony_ci		events = readl(base + NvRegMSIXIrqStatus) & NVREG_IRQSTAT_MASK;
39778c2ecf20Sopenharmony_ci		writel(events & NVREG_IRQ_TIMER, base + NvRegMSIXIrqStatus);
39788c2ecf20Sopenharmony_ci	}
39798c2ecf20Sopenharmony_ci	pci_push(base);
39808c2ecf20Sopenharmony_ci	if (!(events & NVREG_IRQ_TIMER))
39818c2ecf20Sopenharmony_ci		return IRQ_RETVAL(0);
39828c2ecf20Sopenharmony_ci
39838c2ecf20Sopenharmony_ci	nv_msi_workaround(np);
39848c2ecf20Sopenharmony_ci
39858c2ecf20Sopenharmony_ci	spin_lock(&np->lock);
39868c2ecf20Sopenharmony_ci	np->intr_test = 1;
39878c2ecf20Sopenharmony_ci	spin_unlock(&np->lock);
39888c2ecf20Sopenharmony_ci
39898c2ecf20Sopenharmony_ci	return IRQ_RETVAL(1);
39908c2ecf20Sopenharmony_ci}
39918c2ecf20Sopenharmony_ci
39928c2ecf20Sopenharmony_cistatic void set_msix_vector_map(struct net_device *dev, u32 vector, u32 irqmask)
39938c2ecf20Sopenharmony_ci{
39948c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
39958c2ecf20Sopenharmony_ci	int i;
39968c2ecf20Sopenharmony_ci	u32 msixmap = 0;
39978c2ecf20Sopenharmony_ci
39988c2ecf20Sopenharmony_ci	/* Each interrupt bit can be mapped to a MSIX vector (4 bits).
39998c2ecf20Sopenharmony_ci	 * MSIXMap0 represents the first 8 interrupts and MSIXMap1 represents
40008c2ecf20Sopenharmony_ci	 * the remaining 8 interrupts.
40018c2ecf20Sopenharmony_ci	 */
40028c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
40038c2ecf20Sopenharmony_ci		if ((irqmask >> i) & 0x1)
40048c2ecf20Sopenharmony_ci			msixmap |= vector << (i << 2);
40058c2ecf20Sopenharmony_ci	}
40068c2ecf20Sopenharmony_ci	writel(readl(base + NvRegMSIXMap0) | msixmap, base + NvRegMSIXMap0);
40078c2ecf20Sopenharmony_ci
40088c2ecf20Sopenharmony_ci	msixmap = 0;
40098c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
40108c2ecf20Sopenharmony_ci		if ((irqmask >> (i + 8)) & 0x1)
40118c2ecf20Sopenharmony_ci			msixmap |= vector << (i << 2);
40128c2ecf20Sopenharmony_ci	}
40138c2ecf20Sopenharmony_ci	writel(readl(base + NvRegMSIXMap1) | msixmap, base + NvRegMSIXMap1);
40148c2ecf20Sopenharmony_ci}
40158c2ecf20Sopenharmony_ci
40168c2ecf20Sopenharmony_cistatic int nv_request_irq(struct net_device *dev, int intr_test)
40178c2ecf20Sopenharmony_ci{
40188c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
40198c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
40208c2ecf20Sopenharmony_ci	int ret;
40218c2ecf20Sopenharmony_ci	int i;
40228c2ecf20Sopenharmony_ci	irqreturn_t (*handler)(int foo, void *data);
40238c2ecf20Sopenharmony_ci
40248c2ecf20Sopenharmony_ci	if (intr_test) {
40258c2ecf20Sopenharmony_ci		handler = nv_nic_irq_test;
40268c2ecf20Sopenharmony_ci	} else {
40278c2ecf20Sopenharmony_ci		if (nv_optimized(np))
40288c2ecf20Sopenharmony_ci			handler = nv_nic_irq_optimized;
40298c2ecf20Sopenharmony_ci		else
40308c2ecf20Sopenharmony_ci			handler = nv_nic_irq;
40318c2ecf20Sopenharmony_ci	}
40328c2ecf20Sopenharmony_ci
40338c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_X_CAPABLE) {
40348c2ecf20Sopenharmony_ci		for (i = 0; i < (np->msi_flags & NV_MSI_X_VECTORS_MASK); i++)
40358c2ecf20Sopenharmony_ci			np->msi_x_entry[i].entry = i;
40368c2ecf20Sopenharmony_ci		ret = pci_enable_msix_range(np->pci_dev,
40378c2ecf20Sopenharmony_ci					    np->msi_x_entry,
40388c2ecf20Sopenharmony_ci					    np->msi_flags & NV_MSI_X_VECTORS_MASK,
40398c2ecf20Sopenharmony_ci					    np->msi_flags & NV_MSI_X_VECTORS_MASK);
40408c2ecf20Sopenharmony_ci		if (ret > 0) {
40418c2ecf20Sopenharmony_ci			np->msi_flags |= NV_MSI_X_ENABLED;
40428c2ecf20Sopenharmony_ci			if (optimization_mode == NV_OPTIMIZATION_MODE_THROUGHPUT && !intr_test) {
40438c2ecf20Sopenharmony_ci				/* Request irq for rx handling */
40448c2ecf20Sopenharmony_ci				sprintf(np->name_rx, "%s-rx", dev->name);
40458c2ecf20Sopenharmony_ci				ret = request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector,
40468c2ecf20Sopenharmony_ci						  nv_nic_irq_rx, IRQF_SHARED, np->name_rx, dev);
40478c2ecf20Sopenharmony_ci				if (ret) {
40488c2ecf20Sopenharmony_ci					netdev_info(dev,
40498c2ecf20Sopenharmony_ci						    "request_irq failed for rx %d\n",
40508c2ecf20Sopenharmony_ci						    ret);
40518c2ecf20Sopenharmony_ci					pci_disable_msix(np->pci_dev);
40528c2ecf20Sopenharmony_ci					np->msi_flags &= ~NV_MSI_X_ENABLED;
40538c2ecf20Sopenharmony_ci					goto out_err;
40548c2ecf20Sopenharmony_ci				}
40558c2ecf20Sopenharmony_ci				/* Request irq for tx handling */
40568c2ecf20Sopenharmony_ci				sprintf(np->name_tx, "%s-tx", dev->name);
40578c2ecf20Sopenharmony_ci				ret = request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector,
40588c2ecf20Sopenharmony_ci						  nv_nic_irq_tx, IRQF_SHARED, np->name_tx, dev);
40598c2ecf20Sopenharmony_ci				if (ret) {
40608c2ecf20Sopenharmony_ci					netdev_info(dev,
40618c2ecf20Sopenharmony_ci						    "request_irq failed for tx %d\n",
40628c2ecf20Sopenharmony_ci						    ret);
40638c2ecf20Sopenharmony_ci					pci_disable_msix(np->pci_dev);
40648c2ecf20Sopenharmony_ci					np->msi_flags &= ~NV_MSI_X_ENABLED;
40658c2ecf20Sopenharmony_ci					goto out_free_rx;
40668c2ecf20Sopenharmony_ci				}
40678c2ecf20Sopenharmony_ci				/* Request irq for link and timer handling */
40688c2ecf20Sopenharmony_ci				sprintf(np->name_other, "%s-other", dev->name);
40698c2ecf20Sopenharmony_ci				ret = request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector,
40708c2ecf20Sopenharmony_ci						  nv_nic_irq_other, IRQF_SHARED, np->name_other, dev);
40718c2ecf20Sopenharmony_ci				if (ret) {
40728c2ecf20Sopenharmony_ci					netdev_info(dev,
40738c2ecf20Sopenharmony_ci						    "request_irq failed for link %d\n",
40748c2ecf20Sopenharmony_ci						    ret);
40758c2ecf20Sopenharmony_ci					pci_disable_msix(np->pci_dev);
40768c2ecf20Sopenharmony_ci					np->msi_flags &= ~NV_MSI_X_ENABLED;
40778c2ecf20Sopenharmony_ci					goto out_free_tx;
40788c2ecf20Sopenharmony_ci				}
40798c2ecf20Sopenharmony_ci				/* map interrupts to their respective vector */
40808c2ecf20Sopenharmony_ci				writel(0, base + NvRegMSIXMap0);
40818c2ecf20Sopenharmony_ci				writel(0, base + NvRegMSIXMap1);
40828c2ecf20Sopenharmony_ci				set_msix_vector_map(dev, NV_MSI_X_VECTOR_RX, NVREG_IRQ_RX_ALL);
40838c2ecf20Sopenharmony_ci				set_msix_vector_map(dev, NV_MSI_X_VECTOR_TX, NVREG_IRQ_TX_ALL);
40848c2ecf20Sopenharmony_ci				set_msix_vector_map(dev, NV_MSI_X_VECTOR_OTHER, NVREG_IRQ_OTHER);
40858c2ecf20Sopenharmony_ci			} else {
40868c2ecf20Sopenharmony_ci				/* Request irq for all interrupts */
40878c2ecf20Sopenharmony_ci				ret = request_irq(np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector,
40888c2ecf20Sopenharmony_ci						  handler, IRQF_SHARED, dev->name, dev);
40898c2ecf20Sopenharmony_ci				if (ret) {
40908c2ecf20Sopenharmony_ci					netdev_info(dev,
40918c2ecf20Sopenharmony_ci						    "request_irq failed %d\n",
40928c2ecf20Sopenharmony_ci						    ret);
40938c2ecf20Sopenharmony_ci					pci_disable_msix(np->pci_dev);
40948c2ecf20Sopenharmony_ci					np->msi_flags &= ~NV_MSI_X_ENABLED;
40958c2ecf20Sopenharmony_ci					goto out_err;
40968c2ecf20Sopenharmony_ci				}
40978c2ecf20Sopenharmony_ci
40988c2ecf20Sopenharmony_ci				/* map interrupts to vector 0 */
40998c2ecf20Sopenharmony_ci				writel(0, base + NvRegMSIXMap0);
41008c2ecf20Sopenharmony_ci				writel(0, base + NvRegMSIXMap1);
41018c2ecf20Sopenharmony_ci			}
41028c2ecf20Sopenharmony_ci			netdev_info(dev, "MSI-X enabled\n");
41038c2ecf20Sopenharmony_ci			return 0;
41048c2ecf20Sopenharmony_ci		}
41058c2ecf20Sopenharmony_ci	}
41068c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_CAPABLE) {
41078c2ecf20Sopenharmony_ci		ret = pci_enable_msi(np->pci_dev);
41088c2ecf20Sopenharmony_ci		if (ret == 0) {
41098c2ecf20Sopenharmony_ci			np->msi_flags |= NV_MSI_ENABLED;
41108c2ecf20Sopenharmony_ci			ret = request_irq(np->pci_dev->irq, handler, IRQF_SHARED, dev->name, dev);
41118c2ecf20Sopenharmony_ci			if (ret) {
41128c2ecf20Sopenharmony_ci				netdev_info(dev, "request_irq failed %d\n",
41138c2ecf20Sopenharmony_ci					    ret);
41148c2ecf20Sopenharmony_ci				pci_disable_msi(np->pci_dev);
41158c2ecf20Sopenharmony_ci				np->msi_flags &= ~NV_MSI_ENABLED;
41168c2ecf20Sopenharmony_ci				goto out_err;
41178c2ecf20Sopenharmony_ci			}
41188c2ecf20Sopenharmony_ci
41198c2ecf20Sopenharmony_ci			/* map interrupts to vector 0 */
41208c2ecf20Sopenharmony_ci			writel(0, base + NvRegMSIMap0);
41218c2ecf20Sopenharmony_ci			writel(0, base + NvRegMSIMap1);
41228c2ecf20Sopenharmony_ci			/* enable msi vector 0 */
41238c2ecf20Sopenharmony_ci			writel(NVREG_MSI_VECTOR_0_ENABLED, base + NvRegMSIIrqMask);
41248c2ecf20Sopenharmony_ci			netdev_info(dev, "MSI enabled\n");
41258c2ecf20Sopenharmony_ci			return 0;
41268c2ecf20Sopenharmony_ci		}
41278c2ecf20Sopenharmony_ci	}
41288c2ecf20Sopenharmony_ci
41298c2ecf20Sopenharmony_ci	if (request_irq(np->pci_dev->irq, handler, IRQF_SHARED, dev->name, dev) != 0)
41308c2ecf20Sopenharmony_ci		goto out_err;
41318c2ecf20Sopenharmony_ci
41328c2ecf20Sopenharmony_ci	return 0;
41338c2ecf20Sopenharmony_ciout_free_tx:
41348c2ecf20Sopenharmony_ci	free_irq(np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector, dev);
41358c2ecf20Sopenharmony_ciout_free_rx:
41368c2ecf20Sopenharmony_ci	free_irq(np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector, dev);
41378c2ecf20Sopenharmony_ciout_err:
41388c2ecf20Sopenharmony_ci	return 1;
41398c2ecf20Sopenharmony_ci}
41408c2ecf20Sopenharmony_ci
41418c2ecf20Sopenharmony_cistatic void nv_free_irq(struct net_device *dev)
41428c2ecf20Sopenharmony_ci{
41438c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
41448c2ecf20Sopenharmony_ci	int i;
41458c2ecf20Sopenharmony_ci
41468c2ecf20Sopenharmony_ci	if (np->msi_flags & NV_MSI_X_ENABLED) {
41478c2ecf20Sopenharmony_ci		for (i = 0; i < (np->msi_flags & NV_MSI_X_VECTORS_MASK); i++)
41488c2ecf20Sopenharmony_ci			free_irq(np->msi_x_entry[i].vector, dev);
41498c2ecf20Sopenharmony_ci		pci_disable_msix(np->pci_dev);
41508c2ecf20Sopenharmony_ci		np->msi_flags &= ~NV_MSI_X_ENABLED;
41518c2ecf20Sopenharmony_ci	} else {
41528c2ecf20Sopenharmony_ci		free_irq(np->pci_dev->irq, dev);
41538c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_ENABLED) {
41548c2ecf20Sopenharmony_ci			pci_disable_msi(np->pci_dev);
41558c2ecf20Sopenharmony_ci			np->msi_flags &= ~NV_MSI_ENABLED;
41568c2ecf20Sopenharmony_ci		}
41578c2ecf20Sopenharmony_ci	}
41588c2ecf20Sopenharmony_ci}
41598c2ecf20Sopenharmony_ci
41608c2ecf20Sopenharmony_cistatic void nv_do_nic_poll(struct timer_list *t)
41618c2ecf20Sopenharmony_ci{
41628c2ecf20Sopenharmony_ci	struct fe_priv *np = from_timer(np, t, nic_poll);
41638c2ecf20Sopenharmony_ci	struct net_device *dev = np->dev;
41648c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
41658c2ecf20Sopenharmony_ci	u32 mask = 0;
41668c2ecf20Sopenharmony_ci	unsigned long flags;
41678c2ecf20Sopenharmony_ci	unsigned int irq = 0;
41688c2ecf20Sopenharmony_ci
41698c2ecf20Sopenharmony_ci	/*
41708c2ecf20Sopenharmony_ci	 * First disable irq(s) and then
41718c2ecf20Sopenharmony_ci	 * reenable interrupts on the nic, we have to do this before calling
41728c2ecf20Sopenharmony_ci	 * nv_nic_irq because that may decide to do otherwise
41738c2ecf20Sopenharmony_ci	 */
41748c2ecf20Sopenharmony_ci
41758c2ecf20Sopenharmony_ci	if (!using_multi_irqs(dev)) {
41768c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_X_ENABLED)
41778c2ecf20Sopenharmony_ci			irq = np->msi_x_entry[NV_MSI_X_VECTOR_ALL].vector;
41788c2ecf20Sopenharmony_ci		else
41798c2ecf20Sopenharmony_ci			irq = np->pci_dev->irq;
41808c2ecf20Sopenharmony_ci		mask = np->irqmask;
41818c2ecf20Sopenharmony_ci	} else {
41828c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_RX_ALL) {
41838c2ecf20Sopenharmony_ci			irq = np->msi_x_entry[NV_MSI_X_VECTOR_RX].vector;
41848c2ecf20Sopenharmony_ci			mask |= NVREG_IRQ_RX_ALL;
41858c2ecf20Sopenharmony_ci		}
41868c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_TX_ALL) {
41878c2ecf20Sopenharmony_ci			irq = np->msi_x_entry[NV_MSI_X_VECTOR_TX].vector;
41888c2ecf20Sopenharmony_ci			mask |= NVREG_IRQ_TX_ALL;
41898c2ecf20Sopenharmony_ci		}
41908c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_OTHER) {
41918c2ecf20Sopenharmony_ci			irq = np->msi_x_entry[NV_MSI_X_VECTOR_OTHER].vector;
41928c2ecf20Sopenharmony_ci			mask |= NVREG_IRQ_OTHER;
41938c2ecf20Sopenharmony_ci		}
41948c2ecf20Sopenharmony_ci	}
41958c2ecf20Sopenharmony_ci
41968c2ecf20Sopenharmony_ci	disable_irq_nosync_lockdep_irqsave(irq, &flags);
41978c2ecf20Sopenharmony_ci	synchronize_irq(irq);
41988c2ecf20Sopenharmony_ci
41998c2ecf20Sopenharmony_ci	if (np->recover_error) {
42008c2ecf20Sopenharmony_ci		np->recover_error = 0;
42018c2ecf20Sopenharmony_ci		netdev_info(dev, "MAC in recoverable error state\n");
42028c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
42038c2ecf20Sopenharmony_ci			netif_tx_lock_bh(dev);
42048c2ecf20Sopenharmony_ci			netif_addr_lock(dev);
42058c2ecf20Sopenharmony_ci			spin_lock(&np->lock);
42068c2ecf20Sopenharmony_ci			/* stop engines */
42078c2ecf20Sopenharmony_ci			nv_stop_rxtx(dev);
42088c2ecf20Sopenharmony_ci			if (np->driver_data & DEV_HAS_POWER_CNTRL)
42098c2ecf20Sopenharmony_ci				nv_mac_reset(dev);
42108c2ecf20Sopenharmony_ci			nv_txrx_reset(dev);
42118c2ecf20Sopenharmony_ci			/* drain rx queue */
42128c2ecf20Sopenharmony_ci			nv_drain_rxtx(dev);
42138c2ecf20Sopenharmony_ci			/* reinit driver view of the rx queue */
42148c2ecf20Sopenharmony_ci			set_bufsize(dev);
42158c2ecf20Sopenharmony_ci			if (nv_init_ring(dev)) {
42168c2ecf20Sopenharmony_ci				if (!np->in_shutdown)
42178c2ecf20Sopenharmony_ci					mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
42188c2ecf20Sopenharmony_ci			}
42198c2ecf20Sopenharmony_ci			/* reinit nic view of the rx queue */
42208c2ecf20Sopenharmony_ci			writel(np->rx_buf_sz, base + NvRegOffloadConfig);
42218c2ecf20Sopenharmony_ci			setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
42228c2ecf20Sopenharmony_ci			writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
42238c2ecf20Sopenharmony_ci				base + NvRegRingSizes);
42248c2ecf20Sopenharmony_ci			pci_push(base);
42258c2ecf20Sopenharmony_ci			writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
42268c2ecf20Sopenharmony_ci			pci_push(base);
42278c2ecf20Sopenharmony_ci			/* clear interrupts */
42288c2ecf20Sopenharmony_ci			if (!(np->msi_flags & NV_MSI_X_ENABLED))
42298c2ecf20Sopenharmony_ci				writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
42308c2ecf20Sopenharmony_ci			else
42318c2ecf20Sopenharmony_ci				writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
42328c2ecf20Sopenharmony_ci
42338c2ecf20Sopenharmony_ci			/* restart rx engine */
42348c2ecf20Sopenharmony_ci			nv_start_rxtx(dev);
42358c2ecf20Sopenharmony_ci			spin_unlock(&np->lock);
42368c2ecf20Sopenharmony_ci			netif_addr_unlock(dev);
42378c2ecf20Sopenharmony_ci			netif_tx_unlock_bh(dev);
42388c2ecf20Sopenharmony_ci		}
42398c2ecf20Sopenharmony_ci	}
42408c2ecf20Sopenharmony_ci
42418c2ecf20Sopenharmony_ci	writel(mask, base + NvRegIrqMask);
42428c2ecf20Sopenharmony_ci	pci_push(base);
42438c2ecf20Sopenharmony_ci
42448c2ecf20Sopenharmony_ci	if (!using_multi_irqs(dev)) {
42458c2ecf20Sopenharmony_ci		np->nic_poll_irq = 0;
42468c2ecf20Sopenharmony_ci		if (nv_optimized(np))
42478c2ecf20Sopenharmony_ci			nv_nic_irq_optimized(0, dev);
42488c2ecf20Sopenharmony_ci		else
42498c2ecf20Sopenharmony_ci			nv_nic_irq(0, dev);
42508c2ecf20Sopenharmony_ci	} else {
42518c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_RX_ALL) {
42528c2ecf20Sopenharmony_ci			np->nic_poll_irq &= ~NVREG_IRQ_RX_ALL;
42538c2ecf20Sopenharmony_ci			nv_nic_irq_rx(0, dev);
42548c2ecf20Sopenharmony_ci		}
42558c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_TX_ALL) {
42568c2ecf20Sopenharmony_ci			np->nic_poll_irq &= ~NVREG_IRQ_TX_ALL;
42578c2ecf20Sopenharmony_ci			nv_nic_irq_tx(0, dev);
42588c2ecf20Sopenharmony_ci		}
42598c2ecf20Sopenharmony_ci		if (np->nic_poll_irq & NVREG_IRQ_OTHER) {
42608c2ecf20Sopenharmony_ci			np->nic_poll_irq &= ~NVREG_IRQ_OTHER;
42618c2ecf20Sopenharmony_ci			nv_nic_irq_other(0, dev);
42628c2ecf20Sopenharmony_ci		}
42638c2ecf20Sopenharmony_ci	}
42648c2ecf20Sopenharmony_ci
42658c2ecf20Sopenharmony_ci	enable_irq_lockdep_irqrestore(irq, &flags);
42668c2ecf20Sopenharmony_ci}
42678c2ecf20Sopenharmony_ci
42688c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_POLL_CONTROLLER
42698c2ecf20Sopenharmony_cistatic void nv_poll_controller(struct net_device *dev)
42708c2ecf20Sopenharmony_ci{
42718c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
42728c2ecf20Sopenharmony_ci
42738c2ecf20Sopenharmony_ci	nv_do_nic_poll(&np->nic_poll);
42748c2ecf20Sopenharmony_ci}
42758c2ecf20Sopenharmony_ci#endif
42768c2ecf20Sopenharmony_ci
42778c2ecf20Sopenharmony_cistatic void nv_do_stats_poll(struct timer_list *t)
42788c2ecf20Sopenharmony_ci	__acquires(&netdev_priv(dev)->hwstats_lock)
42798c2ecf20Sopenharmony_ci	__releases(&netdev_priv(dev)->hwstats_lock)
42808c2ecf20Sopenharmony_ci{
42818c2ecf20Sopenharmony_ci	struct fe_priv *np = from_timer(np, t, stats_poll);
42828c2ecf20Sopenharmony_ci	struct net_device *dev = np->dev;
42838c2ecf20Sopenharmony_ci
42848c2ecf20Sopenharmony_ci	/* If lock is currently taken, the stats are being refreshed
42858c2ecf20Sopenharmony_ci	 * and hence fresh enough */
42868c2ecf20Sopenharmony_ci	if (spin_trylock(&np->hwstats_lock)) {
42878c2ecf20Sopenharmony_ci		nv_update_stats(dev);
42888c2ecf20Sopenharmony_ci		spin_unlock(&np->hwstats_lock);
42898c2ecf20Sopenharmony_ci	}
42908c2ecf20Sopenharmony_ci
42918c2ecf20Sopenharmony_ci	if (!np->in_shutdown)
42928c2ecf20Sopenharmony_ci		mod_timer(&np->stats_poll,
42938c2ecf20Sopenharmony_ci			round_jiffies(jiffies + STATS_INTERVAL));
42948c2ecf20Sopenharmony_ci}
42958c2ecf20Sopenharmony_ci
42968c2ecf20Sopenharmony_cistatic void nv_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
42978c2ecf20Sopenharmony_ci{
42988c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
42998c2ecf20Sopenharmony_ci	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
43008c2ecf20Sopenharmony_ci	strlcpy(info->version, FORCEDETH_VERSION, sizeof(info->version));
43018c2ecf20Sopenharmony_ci	strlcpy(info->bus_info, pci_name(np->pci_dev), sizeof(info->bus_info));
43028c2ecf20Sopenharmony_ci}
43038c2ecf20Sopenharmony_ci
43048c2ecf20Sopenharmony_cistatic void nv_get_wol(struct net_device *dev, struct ethtool_wolinfo *wolinfo)
43058c2ecf20Sopenharmony_ci{
43068c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
43078c2ecf20Sopenharmony_ci	wolinfo->supported = WAKE_MAGIC;
43088c2ecf20Sopenharmony_ci
43098c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
43108c2ecf20Sopenharmony_ci	if (np->wolenabled)
43118c2ecf20Sopenharmony_ci		wolinfo->wolopts = WAKE_MAGIC;
43128c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
43138c2ecf20Sopenharmony_ci}
43148c2ecf20Sopenharmony_ci
43158c2ecf20Sopenharmony_cistatic int nv_set_wol(struct net_device *dev, struct ethtool_wolinfo *wolinfo)
43168c2ecf20Sopenharmony_ci{
43178c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
43188c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
43198c2ecf20Sopenharmony_ci	u32 flags = 0;
43208c2ecf20Sopenharmony_ci
43218c2ecf20Sopenharmony_ci	if (wolinfo->wolopts == 0) {
43228c2ecf20Sopenharmony_ci		np->wolenabled = 0;
43238c2ecf20Sopenharmony_ci	} else if (wolinfo->wolopts & WAKE_MAGIC) {
43248c2ecf20Sopenharmony_ci		np->wolenabled = 1;
43258c2ecf20Sopenharmony_ci		flags = NVREG_WAKEUPFLAGS_ENABLE;
43268c2ecf20Sopenharmony_ci	}
43278c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
43288c2ecf20Sopenharmony_ci		spin_lock_irq(&np->lock);
43298c2ecf20Sopenharmony_ci		writel(flags, base + NvRegWakeUpFlags);
43308c2ecf20Sopenharmony_ci		spin_unlock_irq(&np->lock);
43318c2ecf20Sopenharmony_ci	}
43328c2ecf20Sopenharmony_ci	device_set_wakeup_enable(&np->pci_dev->dev, np->wolenabled);
43338c2ecf20Sopenharmony_ci	return 0;
43348c2ecf20Sopenharmony_ci}
43358c2ecf20Sopenharmony_ci
43368c2ecf20Sopenharmony_cistatic int nv_get_link_ksettings(struct net_device *dev,
43378c2ecf20Sopenharmony_ci				 struct ethtool_link_ksettings *cmd)
43388c2ecf20Sopenharmony_ci{
43398c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
43408c2ecf20Sopenharmony_ci	u32 speed, supported, advertising;
43418c2ecf20Sopenharmony_ci	int adv;
43428c2ecf20Sopenharmony_ci
43438c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
43448c2ecf20Sopenharmony_ci	cmd->base.port = PORT_MII;
43458c2ecf20Sopenharmony_ci	if (!netif_running(dev)) {
43468c2ecf20Sopenharmony_ci		/* We do not track link speed / duplex setting if the
43478c2ecf20Sopenharmony_ci		 * interface is disabled. Force a link check */
43488c2ecf20Sopenharmony_ci		if (nv_update_linkspeed(dev)) {
43498c2ecf20Sopenharmony_ci			netif_carrier_on(dev);
43508c2ecf20Sopenharmony_ci		} else {
43518c2ecf20Sopenharmony_ci			netif_carrier_off(dev);
43528c2ecf20Sopenharmony_ci		}
43538c2ecf20Sopenharmony_ci	}
43548c2ecf20Sopenharmony_ci
43558c2ecf20Sopenharmony_ci	if (netif_carrier_ok(dev)) {
43568c2ecf20Sopenharmony_ci		switch (np->linkspeed & (NVREG_LINKSPEED_MASK)) {
43578c2ecf20Sopenharmony_ci		case NVREG_LINKSPEED_10:
43588c2ecf20Sopenharmony_ci			speed = SPEED_10;
43598c2ecf20Sopenharmony_ci			break;
43608c2ecf20Sopenharmony_ci		case NVREG_LINKSPEED_100:
43618c2ecf20Sopenharmony_ci			speed = SPEED_100;
43628c2ecf20Sopenharmony_ci			break;
43638c2ecf20Sopenharmony_ci		case NVREG_LINKSPEED_1000:
43648c2ecf20Sopenharmony_ci			speed = SPEED_1000;
43658c2ecf20Sopenharmony_ci			break;
43668c2ecf20Sopenharmony_ci		default:
43678c2ecf20Sopenharmony_ci			speed = -1;
43688c2ecf20Sopenharmony_ci			break;
43698c2ecf20Sopenharmony_ci		}
43708c2ecf20Sopenharmony_ci		cmd->base.duplex = DUPLEX_HALF;
43718c2ecf20Sopenharmony_ci		if (np->duplex)
43728c2ecf20Sopenharmony_ci			cmd->base.duplex = DUPLEX_FULL;
43738c2ecf20Sopenharmony_ci	} else {
43748c2ecf20Sopenharmony_ci		speed = SPEED_UNKNOWN;
43758c2ecf20Sopenharmony_ci		cmd->base.duplex = DUPLEX_UNKNOWN;
43768c2ecf20Sopenharmony_ci	}
43778c2ecf20Sopenharmony_ci	cmd->base.speed = speed;
43788c2ecf20Sopenharmony_ci	cmd->base.autoneg = np->autoneg;
43798c2ecf20Sopenharmony_ci
43808c2ecf20Sopenharmony_ci	advertising = ADVERTISED_MII;
43818c2ecf20Sopenharmony_ci	if (np->autoneg) {
43828c2ecf20Sopenharmony_ci		advertising |= ADVERTISED_Autoneg;
43838c2ecf20Sopenharmony_ci		adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
43848c2ecf20Sopenharmony_ci		if (adv & ADVERTISE_10HALF)
43858c2ecf20Sopenharmony_ci			advertising |= ADVERTISED_10baseT_Half;
43868c2ecf20Sopenharmony_ci		if (adv & ADVERTISE_10FULL)
43878c2ecf20Sopenharmony_ci			advertising |= ADVERTISED_10baseT_Full;
43888c2ecf20Sopenharmony_ci		if (adv & ADVERTISE_100HALF)
43898c2ecf20Sopenharmony_ci			advertising |= ADVERTISED_100baseT_Half;
43908c2ecf20Sopenharmony_ci		if (adv & ADVERTISE_100FULL)
43918c2ecf20Sopenharmony_ci			advertising |= ADVERTISED_100baseT_Full;
43928c2ecf20Sopenharmony_ci		if (np->gigabit == PHY_GIGABIT) {
43938c2ecf20Sopenharmony_ci			adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
43948c2ecf20Sopenharmony_ci			if (adv & ADVERTISE_1000FULL)
43958c2ecf20Sopenharmony_ci				advertising |= ADVERTISED_1000baseT_Full;
43968c2ecf20Sopenharmony_ci		}
43978c2ecf20Sopenharmony_ci	}
43988c2ecf20Sopenharmony_ci	supported = (SUPPORTED_Autoneg |
43998c2ecf20Sopenharmony_ci		SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
44008c2ecf20Sopenharmony_ci		SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
44018c2ecf20Sopenharmony_ci		SUPPORTED_MII);
44028c2ecf20Sopenharmony_ci	if (np->gigabit == PHY_GIGABIT)
44038c2ecf20Sopenharmony_ci		supported |= SUPPORTED_1000baseT_Full;
44048c2ecf20Sopenharmony_ci
44058c2ecf20Sopenharmony_ci	cmd->base.phy_address = np->phyaddr;
44068c2ecf20Sopenharmony_ci
44078c2ecf20Sopenharmony_ci	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
44088c2ecf20Sopenharmony_ci						supported);
44098c2ecf20Sopenharmony_ci	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
44108c2ecf20Sopenharmony_ci						advertising);
44118c2ecf20Sopenharmony_ci
44128c2ecf20Sopenharmony_ci	/* ignore maxtxpkt, maxrxpkt for now */
44138c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
44148c2ecf20Sopenharmony_ci	return 0;
44158c2ecf20Sopenharmony_ci}
44168c2ecf20Sopenharmony_ci
44178c2ecf20Sopenharmony_cistatic int nv_set_link_ksettings(struct net_device *dev,
44188c2ecf20Sopenharmony_ci				 const struct ethtool_link_ksettings *cmd)
44198c2ecf20Sopenharmony_ci{
44208c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
44218c2ecf20Sopenharmony_ci	u32 speed = cmd->base.speed;
44228c2ecf20Sopenharmony_ci	u32 advertising;
44238c2ecf20Sopenharmony_ci
44248c2ecf20Sopenharmony_ci	ethtool_convert_link_mode_to_legacy_u32(&advertising,
44258c2ecf20Sopenharmony_ci						cmd->link_modes.advertising);
44268c2ecf20Sopenharmony_ci
44278c2ecf20Sopenharmony_ci	if (cmd->base.port != PORT_MII)
44288c2ecf20Sopenharmony_ci		return -EINVAL;
44298c2ecf20Sopenharmony_ci	if (cmd->base.phy_address != np->phyaddr) {
44308c2ecf20Sopenharmony_ci		/* TODO: support switching between multiple phys. Should be
44318c2ecf20Sopenharmony_ci		 * trivial, but not enabled due to lack of test hardware. */
44328c2ecf20Sopenharmony_ci		return -EINVAL;
44338c2ecf20Sopenharmony_ci	}
44348c2ecf20Sopenharmony_ci	if (cmd->base.autoneg == AUTONEG_ENABLE) {
44358c2ecf20Sopenharmony_ci		u32 mask;
44368c2ecf20Sopenharmony_ci
44378c2ecf20Sopenharmony_ci		mask = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
44388c2ecf20Sopenharmony_ci			  ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
44398c2ecf20Sopenharmony_ci		if (np->gigabit == PHY_GIGABIT)
44408c2ecf20Sopenharmony_ci			mask |= ADVERTISED_1000baseT_Full;
44418c2ecf20Sopenharmony_ci
44428c2ecf20Sopenharmony_ci		if ((advertising & mask) == 0)
44438c2ecf20Sopenharmony_ci			return -EINVAL;
44448c2ecf20Sopenharmony_ci
44458c2ecf20Sopenharmony_ci	} else if (cmd->base.autoneg == AUTONEG_DISABLE) {
44468c2ecf20Sopenharmony_ci		/* Note: autonegotiation disable, speed 1000 intentionally
44478c2ecf20Sopenharmony_ci		 * forbidden - no one should need that. */
44488c2ecf20Sopenharmony_ci
44498c2ecf20Sopenharmony_ci		if (speed != SPEED_10 && speed != SPEED_100)
44508c2ecf20Sopenharmony_ci			return -EINVAL;
44518c2ecf20Sopenharmony_ci		if (cmd->base.duplex != DUPLEX_HALF &&
44528c2ecf20Sopenharmony_ci		    cmd->base.duplex != DUPLEX_FULL)
44538c2ecf20Sopenharmony_ci			return -EINVAL;
44548c2ecf20Sopenharmony_ci	} else {
44558c2ecf20Sopenharmony_ci		return -EINVAL;
44568c2ecf20Sopenharmony_ci	}
44578c2ecf20Sopenharmony_ci
44588c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
44598c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
44608c2ecf20Sopenharmony_ci		unsigned long flags;
44618c2ecf20Sopenharmony_ci
44628c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
44638c2ecf20Sopenharmony_ci		netif_tx_lock_bh(dev);
44648c2ecf20Sopenharmony_ci		netif_addr_lock(dev);
44658c2ecf20Sopenharmony_ci		/* with plain spinlock lockdep complains */
44668c2ecf20Sopenharmony_ci		spin_lock_irqsave(&np->lock, flags);
44678c2ecf20Sopenharmony_ci		/* stop engines */
44688c2ecf20Sopenharmony_ci		/* FIXME:
44698c2ecf20Sopenharmony_ci		 * this can take some time, and interrupts are disabled
44708c2ecf20Sopenharmony_ci		 * due to spin_lock_irqsave, but let's hope no daemon
44718c2ecf20Sopenharmony_ci		 * is going to change the settings very often...
44728c2ecf20Sopenharmony_ci		 * Worst case:
44738c2ecf20Sopenharmony_ci		 * NV_RXSTOP_DELAY1MAX + NV_TXSTOP_DELAY1MAX
44748c2ecf20Sopenharmony_ci		 * + some minor delays, which is up to a second approximately
44758c2ecf20Sopenharmony_ci		 */
44768c2ecf20Sopenharmony_ci		nv_stop_rxtx(dev);
44778c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
44788c2ecf20Sopenharmony_ci		netif_addr_unlock(dev);
44798c2ecf20Sopenharmony_ci		netif_tx_unlock_bh(dev);
44808c2ecf20Sopenharmony_ci	}
44818c2ecf20Sopenharmony_ci
44828c2ecf20Sopenharmony_ci	if (cmd->base.autoneg == AUTONEG_ENABLE) {
44838c2ecf20Sopenharmony_ci		int adv, bmcr;
44848c2ecf20Sopenharmony_ci
44858c2ecf20Sopenharmony_ci		np->autoneg = 1;
44868c2ecf20Sopenharmony_ci
44878c2ecf20Sopenharmony_ci		/* advertise only what has been requested */
44888c2ecf20Sopenharmony_ci		adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
44898c2ecf20Sopenharmony_ci		adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
44908c2ecf20Sopenharmony_ci		if (advertising & ADVERTISED_10baseT_Half)
44918c2ecf20Sopenharmony_ci			adv |= ADVERTISE_10HALF;
44928c2ecf20Sopenharmony_ci		if (advertising & ADVERTISED_10baseT_Full)
44938c2ecf20Sopenharmony_ci			adv |= ADVERTISE_10FULL;
44948c2ecf20Sopenharmony_ci		if (advertising & ADVERTISED_100baseT_Half)
44958c2ecf20Sopenharmony_ci			adv |= ADVERTISE_100HALF;
44968c2ecf20Sopenharmony_ci		if (advertising & ADVERTISED_100baseT_Full)
44978c2ecf20Sopenharmony_ci			adv |= ADVERTISE_100FULL;
44988c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_RX_REQ)  /* for rx we set both advertisements but disable tx pause */
44998c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
45008c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
45018c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_ASYM;
45028c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
45038c2ecf20Sopenharmony_ci
45048c2ecf20Sopenharmony_ci		if (np->gigabit == PHY_GIGABIT) {
45058c2ecf20Sopenharmony_ci			adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
45068c2ecf20Sopenharmony_ci			adv &= ~ADVERTISE_1000FULL;
45078c2ecf20Sopenharmony_ci			if (advertising & ADVERTISED_1000baseT_Full)
45088c2ecf20Sopenharmony_ci				adv |= ADVERTISE_1000FULL;
45098c2ecf20Sopenharmony_ci			mii_rw(dev, np->phyaddr, MII_CTRL1000, adv);
45108c2ecf20Sopenharmony_ci		}
45118c2ecf20Sopenharmony_ci
45128c2ecf20Sopenharmony_ci		if (netif_running(dev))
45138c2ecf20Sopenharmony_ci			netdev_info(dev, "link down\n");
45148c2ecf20Sopenharmony_ci		bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
45158c2ecf20Sopenharmony_ci		if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
45168c2ecf20Sopenharmony_ci			bmcr |= BMCR_ANENABLE;
45178c2ecf20Sopenharmony_ci			/* reset the phy in order for settings to stick,
45188c2ecf20Sopenharmony_ci			 * and cause autoneg to start */
45198c2ecf20Sopenharmony_ci			if (phy_reset(dev, bmcr)) {
45208c2ecf20Sopenharmony_ci				netdev_info(dev, "phy reset failed\n");
45218c2ecf20Sopenharmony_ci				return -EINVAL;
45228c2ecf20Sopenharmony_ci			}
45238c2ecf20Sopenharmony_ci		} else {
45248c2ecf20Sopenharmony_ci			bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
45258c2ecf20Sopenharmony_ci			mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
45268c2ecf20Sopenharmony_ci		}
45278c2ecf20Sopenharmony_ci	} else {
45288c2ecf20Sopenharmony_ci		int adv, bmcr;
45298c2ecf20Sopenharmony_ci
45308c2ecf20Sopenharmony_ci		np->autoneg = 0;
45318c2ecf20Sopenharmony_ci
45328c2ecf20Sopenharmony_ci		adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
45338c2ecf20Sopenharmony_ci		adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
45348c2ecf20Sopenharmony_ci		if (speed == SPEED_10 && cmd->base.duplex == DUPLEX_HALF)
45358c2ecf20Sopenharmony_ci			adv |= ADVERTISE_10HALF;
45368c2ecf20Sopenharmony_ci		if (speed == SPEED_10 && cmd->base.duplex == DUPLEX_FULL)
45378c2ecf20Sopenharmony_ci			adv |= ADVERTISE_10FULL;
45388c2ecf20Sopenharmony_ci		if (speed == SPEED_100 && cmd->base.duplex == DUPLEX_HALF)
45398c2ecf20Sopenharmony_ci			adv |= ADVERTISE_100HALF;
45408c2ecf20Sopenharmony_ci		if (speed == SPEED_100 && cmd->base.duplex == DUPLEX_FULL)
45418c2ecf20Sopenharmony_ci			adv |= ADVERTISE_100FULL;
45428c2ecf20Sopenharmony_ci		np->pause_flags &= ~(NV_PAUSEFRAME_AUTONEG|NV_PAUSEFRAME_RX_ENABLE|NV_PAUSEFRAME_TX_ENABLE);
45438c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_RX_REQ) {/* for rx we set both advertisements but disable tx pause */
45448c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
45458c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
45468c2ecf20Sopenharmony_ci		}
45478c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_TX_REQ) {
45488c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_ASYM;
45498c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
45508c2ecf20Sopenharmony_ci		}
45518c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
45528c2ecf20Sopenharmony_ci		np->fixed_mode = adv;
45538c2ecf20Sopenharmony_ci
45548c2ecf20Sopenharmony_ci		if (np->gigabit == PHY_GIGABIT) {
45558c2ecf20Sopenharmony_ci			adv = mii_rw(dev, np->phyaddr, MII_CTRL1000, MII_READ);
45568c2ecf20Sopenharmony_ci			adv &= ~ADVERTISE_1000FULL;
45578c2ecf20Sopenharmony_ci			mii_rw(dev, np->phyaddr, MII_CTRL1000, adv);
45588c2ecf20Sopenharmony_ci		}
45598c2ecf20Sopenharmony_ci
45608c2ecf20Sopenharmony_ci		bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
45618c2ecf20Sopenharmony_ci		bmcr &= ~(BMCR_ANENABLE|BMCR_SPEED100|BMCR_SPEED1000|BMCR_FULLDPLX);
45628c2ecf20Sopenharmony_ci		if (np->fixed_mode & (ADVERTISE_10FULL|ADVERTISE_100FULL))
45638c2ecf20Sopenharmony_ci			bmcr |= BMCR_FULLDPLX;
45648c2ecf20Sopenharmony_ci		if (np->fixed_mode & (ADVERTISE_100HALF|ADVERTISE_100FULL))
45658c2ecf20Sopenharmony_ci			bmcr |= BMCR_SPEED100;
45668c2ecf20Sopenharmony_ci		if (np->phy_oui == PHY_OUI_MARVELL) {
45678c2ecf20Sopenharmony_ci			/* reset the phy in order for forced mode settings to stick */
45688c2ecf20Sopenharmony_ci			if (phy_reset(dev, bmcr)) {
45698c2ecf20Sopenharmony_ci				netdev_info(dev, "phy reset failed\n");
45708c2ecf20Sopenharmony_ci				return -EINVAL;
45718c2ecf20Sopenharmony_ci			}
45728c2ecf20Sopenharmony_ci		} else {
45738c2ecf20Sopenharmony_ci			mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
45748c2ecf20Sopenharmony_ci			if (netif_running(dev)) {
45758c2ecf20Sopenharmony_ci				/* Wait a bit and then reconfigure the nic. */
45768c2ecf20Sopenharmony_ci				udelay(10);
45778c2ecf20Sopenharmony_ci				nv_linkchange(dev);
45788c2ecf20Sopenharmony_ci			}
45798c2ecf20Sopenharmony_ci		}
45808c2ecf20Sopenharmony_ci	}
45818c2ecf20Sopenharmony_ci
45828c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
45838c2ecf20Sopenharmony_ci		nv_start_rxtx(dev);
45848c2ecf20Sopenharmony_ci		nv_enable_irq(dev);
45858c2ecf20Sopenharmony_ci	}
45868c2ecf20Sopenharmony_ci
45878c2ecf20Sopenharmony_ci	return 0;
45888c2ecf20Sopenharmony_ci}
45898c2ecf20Sopenharmony_ci
45908c2ecf20Sopenharmony_ci#define FORCEDETH_REGS_VER	1
45918c2ecf20Sopenharmony_ci
45928c2ecf20Sopenharmony_cistatic int nv_get_regs_len(struct net_device *dev)
45938c2ecf20Sopenharmony_ci{
45948c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
45958c2ecf20Sopenharmony_ci	return np->register_size;
45968c2ecf20Sopenharmony_ci}
45978c2ecf20Sopenharmony_ci
45988c2ecf20Sopenharmony_cistatic void nv_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *buf)
45998c2ecf20Sopenharmony_ci{
46008c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
46018c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
46028c2ecf20Sopenharmony_ci	u32 *rbuf = buf;
46038c2ecf20Sopenharmony_ci	int i;
46048c2ecf20Sopenharmony_ci
46058c2ecf20Sopenharmony_ci	regs->version = FORCEDETH_REGS_VER;
46068c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
46078c2ecf20Sopenharmony_ci	for (i = 0; i < np->register_size/sizeof(u32); i++)
46088c2ecf20Sopenharmony_ci		rbuf[i] = readl(base + i*sizeof(u32));
46098c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
46108c2ecf20Sopenharmony_ci}
46118c2ecf20Sopenharmony_ci
46128c2ecf20Sopenharmony_cistatic int nv_nway_reset(struct net_device *dev)
46138c2ecf20Sopenharmony_ci{
46148c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
46158c2ecf20Sopenharmony_ci	int ret;
46168c2ecf20Sopenharmony_ci
46178c2ecf20Sopenharmony_ci	if (np->autoneg) {
46188c2ecf20Sopenharmony_ci		int bmcr;
46198c2ecf20Sopenharmony_ci
46208c2ecf20Sopenharmony_ci		netif_carrier_off(dev);
46218c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
46228c2ecf20Sopenharmony_ci			nv_disable_irq(dev);
46238c2ecf20Sopenharmony_ci			netif_tx_lock_bh(dev);
46248c2ecf20Sopenharmony_ci			netif_addr_lock(dev);
46258c2ecf20Sopenharmony_ci			spin_lock(&np->lock);
46268c2ecf20Sopenharmony_ci			/* stop engines */
46278c2ecf20Sopenharmony_ci			nv_stop_rxtx(dev);
46288c2ecf20Sopenharmony_ci			spin_unlock(&np->lock);
46298c2ecf20Sopenharmony_ci			netif_addr_unlock(dev);
46308c2ecf20Sopenharmony_ci			netif_tx_unlock_bh(dev);
46318c2ecf20Sopenharmony_ci			netdev_info(dev, "link down\n");
46328c2ecf20Sopenharmony_ci		}
46338c2ecf20Sopenharmony_ci
46348c2ecf20Sopenharmony_ci		bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
46358c2ecf20Sopenharmony_ci		if (np->phy_model == PHY_MODEL_MARVELL_E3016) {
46368c2ecf20Sopenharmony_ci			bmcr |= BMCR_ANENABLE;
46378c2ecf20Sopenharmony_ci			/* reset the phy in order for settings to stick*/
46388c2ecf20Sopenharmony_ci			if (phy_reset(dev, bmcr)) {
46398c2ecf20Sopenharmony_ci				netdev_info(dev, "phy reset failed\n");
46408c2ecf20Sopenharmony_ci				return -EINVAL;
46418c2ecf20Sopenharmony_ci			}
46428c2ecf20Sopenharmony_ci		} else {
46438c2ecf20Sopenharmony_ci			bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
46448c2ecf20Sopenharmony_ci			mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
46458c2ecf20Sopenharmony_ci		}
46468c2ecf20Sopenharmony_ci
46478c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
46488c2ecf20Sopenharmony_ci			nv_start_rxtx(dev);
46498c2ecf20Sopenharmony_ci			nv_enable_irq(dev);
46508c2ecf20Sopenharmony_ci		}
46518c2ecf20Sopenharmony_ci		ret = 0;
46528c2ecf20Sopenharmony_ci	} else {
46538c2ecf20Sopenharmony_ci		ret = -EINVAL;
46548c2ecf20Sopenharmony_ci	}
46558c2ecf20Sopenharmony_ci
46568c2ecf20Sopenharmony_ci	return ret;
46578c2ecf20Sopenharmony_ci}
46588c2ecf20Sopenharmony_ci
46598c2ecf20Sopenharmony_cistatic void nv_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
46608c2ecf20Sopenharmony_ci{
46618c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
46628c2ecf20Sopenharmony_ci
46638c2ecf20Sopenharmony_ci	ring->rx_max_pending = (np->desc_ver == DESC_VER_1) ? RING_MAX_DESC_VER_1 : RING_MAX_DESC_VER_2_3;
46648c2ecf20Sopenharmony_ci	ring->tx_max_pending = (np->desc_ver == DESC_VER_1) ? RING_MAX_DESC_VER_1 : RING_MAX_DESC_VER_2_3;
46658c2ecf20Sopenharmony_ci
46668c2ecf20Sopenharmony_ci	ring->rx_pending = np->rx_ring_size;
46678c2ecf20Sopenharmony_ci	ring->tx_pending = np->tx_ring_size;
46688c2ecf20Sopenharmony_ci}
46698c2ecf20Sopenharmony_ci
46708c2ecf20Sopenharmony_cistatic int nv_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
46718c2ecf20Sopenharmony_ci{
46728c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
46738c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
46748c2ecf20Sopenharmony_ci	u8 *rxtx_ring, *rx_skbuff, *tx_skbuff;
46758c2ecf20Sopenharmony_ci	dma_addr_t ring_addr;
46768c2ecf20Sopenharmony_ci
46778c2ecf20Sopenharmony_ci	if (ring->rx_pending < RX_RING_MIN ||
46788c2ecf20Sopenharmony_ci	    ring->tx_pending < TX_RING_MIN ||
46798c2ecf20Sopenharmony_ci	    ring->rx_mini_pending != 0 ||
46808c2ecf20Sopenharmony_ci	    ring->rx_jumbo_pending != 0 ||
46818c2ecf20Sopenharmony_ci	    (np->desc_ver == DESC_VER_1 &&
46828c2ecf20Sopenharmony_ci	     (ring->rx_pending > RING_MAX_DESC_VER_1 ||
46838c2ecf20Sopenharmony_ci	      ring->tx_pending > RING_MAX_DESC_VER_1)) ||
46848c2ecf20Sopenharmony_ci	    (np->desc_ver != DESC_VER_1 &&
46858c2ecf20Sopenharmony_ci	     (ring->rx_pending > RING_MAX_DESC_VER_2_3 ||
46868c2ecf20Sopenharmony_ci	      ring->tx_pending > RING_MAX_DESC_VER_2_3))) {
46878c2ecf20Sopenharmony_ci		return -EINVAL;
46888c2ecf20Sopenharmony_ci	}
46898c2ecf20Sopenharmony_ci
46908c2ecf20Sopenharmony_ci	/* allocate new rings */
46918c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
46928c2ecf20Sopenharmony_ci		rxtx_ring = dma_alloc_coherent(&np->pci_dev->dev,
46938c2ecf20Sopenharmony_ci					       sizeof(struct ring_desc) *
46948c2ecf20Sopenharmony_ci					       (ring->rx_pending +
46958c2ecf20Sopenharmony_ci					       ring->tx_pending),
46968c2ecf20Sopenharmony_ci					       &ring_addr, GFP_ATOMIC);
46978c2ecf20Sopenharmony_ci	} else {
46988c2ecf20Sopenharmony_ci		rxtx_ring = dma_alloc_coherent(&np->pci_dev->dev,
46998c2ecf20Sopenharmony_ci					       sizeof(struct ring_desc_ex) *
47008c2ecf20Sopenharmony_ci					       (ring->rx_pending +
47018c2ecf20Sopenharmony_ci					       ring->tx_pending),
47028c2ecf20Sopenharmony_ci					       &ring_addr, GFP_ATOMIC);
47038c2ecf20Sopenharmony_ci	}
47048c2ecf20Sopenharmony_ci	rx_skbuff = kmalloc_array(ring->rx_pending, sizeof(struct nv_skb_map),
47058c2ecf20Sopenharmony_ci				  GFP_KERNEL);
47068c2ecf20Sopenharmony_ci	tx_skbuff = kmalloc_array(ring->tx_pending, sizeof(struct nv_skb_map),
47078c2ecf20Sopenharmony_ci				  GFP_KERNEL);
47088c2ecf20Sopenharmony_ci	if (!rxtx_ring || !rx_skbuff || !tx_skbuff) {
47098c2ecf20Sopenharmony_ci		/* fall back to old rings */
47108c2ecf20Sopenharmony_ci		if (!nv_optimized(np)) {
47118c2ecf20Sopenharmony_ci			if (rxtx_ring)
47128c2ecf20Sopenharmony_ci				dma_free_coherent(&np->pci_dev->dev,
47138c2ecf20Sopenharmony_ci						  sizeof(struct ring_desc) *
47148c2ecf20Sopenharmony_ci						  (ring->rx_pending +
47158c2ecf20Sopenharmony_ci						  ring->tx_pending),
47168c2ecf20Sopenharmony_ci						  rxtx_ring, ring_addr);
47178c2ecf20Sopenharmony_ci		} else {
47188c2ecf20Sopenharmony_ci			if (rxtx_ring)
47198c2ecf20Sopenharmony_ci				dma_free_coherent(&np->pci_dev->dev,
47208c2ecf20Sopenharmony_ci						  sizeof(struct ring_desc_ex) *
47218c2ecf20Sopenharmony_ci						  (ring->rx_pending +
47228c2ecf20Sopenharmony_ci						  ring->tx_pending),
47238c2ecf20Sopenharmony_ci						  rxtx_ring, ring_addr);
47248c2ecf20Sopenharmony_ci		}
47258c2ecf20Sopenharmony_ci
47268c2ecf20Sopenharmony_ci		kfree(rx_skbuff);
47278c2ecf20Sopenharmony_ci		kfree(tx_skbuff);
47288c2ecf20Sopenharmony_ci		goto exit;
47298c2ecf20Sopenharmony_ci	}
47308c2ecf20Sopenharmony_ci
47318c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
47328c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
47338c2ecf20Sopenharmony_ci		nv_napi_disable(dev);
47348c2ecf20Sopenharmony_ci		netif_tx_lock_bh(dev);
47358c2ecf20Sopenharmony_ci		netif_addr_lock(dev);
47368c2ecf20Sopenharmony_ci		spin_lock(&np->lock);
47378c2ecf20Sopenharmony_ci		/* stop engines */
47388c2ecf20Sopenharmony_ci		nv_stop_rxtx(dev);
47398c2ecf20Sopenharmony_ci		nv_txrx_reset(dev);
47408c2ecf20Sopenharmony_ci		/* drain queues */
47418c2ecf20Sopenharmony_ci		nv_drain_rxtx(dev);
47428c2ecf20Sopenharmony_ci		/* delete queues */
47438c2ecf20Sopenharmony_ci		free_rings(dev);
47448c2ecf20Sopenharmony_ci	}
47458c2ecf20Sopenharmony_ci
47468c2ecf20Sopenharmony_ci	/* set new values */
47478c2ecf20Sopenharmony_ci	np->rx_ring_size = ring->rx_pending;
47488c2ecf20Sopenharmony_ci	np->tx_ring_size = ring->tx_pending;
47498c2ecf20Sopenharmony_ci
47508c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
47518c2ecf20Sopenharmony_ci		np->rx_ring.orig = (struct ring_desc *)rxtx_ring;
47528c2ecf20Sopenharmony_ci		np->tx_ring.orig = &np->rx_ring.orig[np->rx_ring_size];
47538c2ecf20Sopenharmony_ci	} else {
47548c2ecf20Sopenharmony_ci		np->rx_ring.ex = (struct ring_desc_ex *)rxtx_ring;
47558c2ecf20Sopenharmony_ci		np->tx_ring.ex = &np->rx_ring.ex[np->rx_ring_size];
47568c2ecf20Sopenharmony_ci	}
47578c2ecf20Sopenharmony_ci	np->rx_skb = (struct nv_skb_map *)rx_skbuff;
47588c2ecf20Sopenharmony_ci	np->tx_skb = (struct nv_skb_map *)tx_skbuff;
47598c2ecf20Sopenharmony_ci	np->ring_addr = ring_addr;
47608c2ecf20Sopenharmony_ci
47618c2ecf20Sopenharmony_ci	memset(np->rx_skb, 0, sizeof(struct nv_skb_map) * np->rx_ring_size);
47628c2ecf20Sopenharmony_ci	memset(np->tx_skb, 0, sizeof(struct nv_skb_map) * np->tx_ring_size);
47638c2ecf20Sopenharmony_ci
47648c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
47658c2ecf20Sopenharmony_ci		/* reinit driver view of the queues */
47668c2ecf20Sopenharmony_ci		set_bufsize(dev);
47678c2ecf20Sopenharmony_ci		if (nv_init_ring(dev)) {
47688c2ecf20Sopenharmony_ci			if (!np->in_shutdown)
47698c2ecf20Sopenharmony_ci				mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
47708c2ecf20Sopenharmony_ci		}
47718c2ecf20Sopenharmony_ci
47728c2ecf20Sopenharmony_ci		/* reinit nic view of the queues */
47738c2ecf20Sopenharmony_ci		writel(np->rx_buf_sz, base + NvRegOffloadConfig);
47748c2ecf20Sopenharmony_ci		setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
47758c2ecf20Sopenharmony_ci		writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
47768c2ecf20Sopenharmony_ci			base + NvRegRingSizes);
47778c2ecf20Sopenharmony_ci		pci_push(base);
47788c2ecf20Sopenharmony_ci		writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
47798c2ecf20Sopenharmony_ci		pci_push(base);
47808c2ecf20Sopenharmony_ci
47818c2ecf20Sopenharmony_ci		/* restart engines */
47828c2ecf20Sopenharmony_ci		nv_start_rxtx(dev);
47838c2ecf20Sopenharmony_ci		spin_unlock(&np->lock);
47848c2ecf20Sopenharmony_ci		netif_addr_unlock(dev);
47858c2ecf20Sopenharmony_ci		netif_tx_unlock_bh(dev);
47868c2ecf20Sopenharmony_ci		nv_napi_enable(dev);
47878c2ecf20Sopenharmony_ci		nv_enable_irq(dev);
47888c2ecf20Sopenharmony_ci	}
47898c2ecf20Sopenharmony_ci	return 0;
47908c2ecf20Sopenharmony_ciexit:
47918c2ecf20Sopenharmony_ci	return -ENOMEM;
47928c2ecf20Sopenharmony_ci}
47938c2ecf20Sopenharmony_ci
47948c2ecf20Sopenharmony_cistatic void nv_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam* pause)
47958c2ecf20Sopenharmony_ci{
47968c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
47978c2ecf20Sopenharmony_ci
47988c2ecf20Sopenharmony_ci	pause->autoneg = (np->pause_flags & NV_PAUSEFRAME_AUTONEG) != 0;
47998c2ecf20Sopenharmony_ci	pause->rx_pause = (np->pause_flags & NV_PAUSEFRAME_RX_ENABLE) != 0;
48008c2ecf20Sopenharmony_ci	pause->tx_pause = (np->pause_flags & NV_PAUSEFRAME_TX_ENABLE) != 0;
48018c2ecf20Sopenharmony_ci}
48028c2ecf20Sopenharmony_ci
48038c2ecf20Sopenharmony_cistatic int nv_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam* pause)
48048c2ecf20Sopenharmony_ci{
48058c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
48068c2ecf20Sopenharmony_ci	int adv, bmcr;
48078c2ecf20Sopenharmony_ci
48088c2ecf20Sopenharmony_ci	if ((!np->autoneg && np->duplex == 0) ||
48098c2ecf20Sopenharmony_ci	    (np->autoneg && !pause->autoneg && np->duplex == 0)) {
48108c2ecf20Sopenharmony_ci		netdev_info(dev, "can not set pause settings when forced link is in half duplex\n");
48118c2ecf20Sopenharmony_ci		return -EINVAL;
48128c2ecf20Sopenharmony_ci	}
48138c2ecf20Sopenharmony_ci	if (pause->tx_pause && !(np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE)) {
48148c2ecf20Sopenharmony_ci		netdev_info(dev, "hardware does not support tx pause frames\n");
48158c2ecf20Sopenharmony_ci		return -EINVAL;
48168c2ecf20Sopenharmony_ci	}
48178c2ecf20Sopenharmony_ci
48188c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
48198c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
48208c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
48218c2ecf20Sopenharmony_ci		netif_tx_lock_bh(dev);
48228c2ecf20Sopenharmony_ci		netif_addr_lock(dev);
48238c2ecf20Sopenharmony_ci		spin_lock(&np->lock);
48248c2ecf20Sopenharmony_ci		/* stop engines */
48258c2ecf20Sopenharmony_ci		nv_stop_rxtx(dev);
48268c2ecf20Sopenharmony_ci		spin_unlock(&np->lock);
48278c2ecf20Sopenharmony_ci		netif_addr_unlock(dev);
48288c2ecf20Sopenharmony_ci		netif_tx_unlock_bh(dev);
48298c2ecf20Sopenharmony_ci	}
48308c2ecf20Sopenharmony_ci
48318c2ecf20Sopenharmony_ci	np->pause_flags &= ~(NV_PAUSEFRAME_RX_REQ|NV_PAUSEFRAME_TX_REQ);
48328c2ecf20Sopenharmony_ci	if (pause->rx_pause)
48338c2ecf20Sopenharmony_ci		np->pause_flags |= NV_PAUSEFRAME_RX_REQ;
48348c2ecf20Sopenharmony_ci	if (pause->tx_pause)
48358c2ecf20Sopenharmony_ci		np->pause_flags |= NV_PAUSEFRAME_TX_REQ;
48368c2ecf20Sopenharmony_ci
48378c2ecf20Sopenharmony_ci	if (np->autoneg && pause->autoneg) {
48388c2ecf20Sopenharmony_ci		np->pause_flags |= NV_PAUSEFRAME_AUTONEG;
48398c2ecf20Sopenharmony_ci
48408c2ecf20Sopenharmony_ci		adv = mii_rw(dev, np->phyaddr, MII_ADVERTISE, MII_READ);
48418c2ecf20Sopenharmony_ci		adv &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
48428c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_RX_REQ) /* for rx we set both advertisements but disable tx pause */
48438c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
48448c2ecf20Sopenharmony_ci		if (np->pause_flags & NV_PAUSEFRAME_TX_REQ)
48458c2ecf20Sopenharmony_ci			adv |=  ADVERTISE_PAUSE_ASYM;
48468c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_ADVERTISE, adv);
48478c2ecf20Sopenharmony_ci
48488c2ecf20Sopenharmony_ci		if (netif_running(dev))
48498c2ecf20Sopenharmony_ci			netdev_info(dev, "link down\n");
48508c2ecf20Sopenharmony_ci		bmcr = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
48518c2ecf20Sopenharmony_ci		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
48528c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_BMCR, bmcr);
48538c2ecf20Sopenharmony_ci	} else {
48548c2ecf20Sopenharmony_ci		np->pause_flags &= ~(NV_PAUSEFRAME_AUTONEG|NV_PAUSEFRAME_RX_ENABLE|NV_PAUSEFRAME_TX_ENABLE);
48558c2ecf20Sopenharmony_ci		if (pause->rx_pause)
48568c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_RX_ENABLE;
48578c2ecf20Sopenharmony_ci		if (pause->tx_pause)
48588c2ecf20Sopenharmony_ci			np->pause_flags |= NV_PAUSEFRAME_TX_ENABLE;
48598c2ecf20Sopenharmony_ci
48608c2ecf20Sopenharmony_ci		if (!netif_running(dev))
48618c2ecf20Sopenharmony_ci			nv_update_linkspeed(dev);
48628c2ecf20Sopenharmony_ci		else
48638c2ecf20Sopenharmony_ci			nv_update_pause(dev, np->pause_flags);
48648c2ecf20Sopenharmony_ci	}
48658c2ecf20Sopenharmony_ci
48668c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
48678c2ecf20Sopenharmony_ci		nv_start_rxtx(dev);
48688c2ecf20Sopenharmony_ci		nv_enable_irq(dev);
48698c2ecf20Sopenharmony_ci	}
48708c2ecf20Sopenharmony_ci	return 0;
48718c2ecf20Sopenharmony_ci}
48728c2ecf20Sopenharmony_ci
48738c2ecf20Sopenharmony_cistatic int nv_set_loopback(struct net_device *dev, netdev_features_t features)
48748c2ecf20Sopenharmony_ci{
48758c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
48768c2ecf20Sopenharmony_ci	unsigned long flags;
48778c2ecf20Sopenharmony_ci	u32 miicontrol;
48788c2ecf20Sopenharmony_ci	int err, retval = 0;
48798c2ecf20Sopenharmony_ci
48808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
48818c2ecf20Sopenharmony_ci	miicontrol = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
48828c2ecf20Sopenharmony_ci	if (features & NETIF_F_LOOPBACK) {
48838c2ecf20Sopenharmony_ci		if (miicontrol & BMCR_LOOPBACK) {
48848c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
48858c2ecf20Sopenharmony_ci			netdev_info(dev, "Loopback already enabled\n");
48868c2ecf20Sopenharmony_ci			return 0;
48878c2ecf20Sopenharmony_ci		}
48888c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
48898c2ecf20Sopenharmony_ci		/* Turn on loopback mode */
48908c2ecf20Sopenharmony_ci		miicontrol |= BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
48918c2ecf20Sopenharmony_ci		err = mii_rw(dev, np->phyaddr, MII_BMCR, miicontrol);
48928c2ecf20Sopenharmony_ci		if (err) {
48938c2ecf20Sopenharmony_ci			retval = PHY_ERROR;
48948c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
48958c2ecf20Sopenharmony_ci			phy_init(dev);
48968c2ecf20Sopenharmony_ci		} else {
48978c2ecf20Sopenharmony_ci			if (netif_running(dev)) {
48988c2ecf20Sopenharmony_ci				/* Force 1000 Mbps full-duplex */
48998c2ecf20Sopenharmony_ci				nv_force_linkspeed(dev, NVREG_LINKSPEED_1000,
49008c2ecf20Sopenharmony_ci									 1);
49018c2ecf20Sopenharmony_ci				/* Force link up */
49028c2ecf20Sopenharmony_ci				netif_carrier_on(dev);
49038c2ecf20Sopenharmony_ci			}
49048c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
49058c2ecf20Sopenharmony_ci			netdev_info(dev,
49068c2ecf20Sopenharmony_ci				"Internal PHY loopback mode enabled.\n");
49078c2ecf20Sopenharmony_ci		}
49088c2ecf20Sopenharmony_ci	} else {
49098c2ecf20Sopenharmony_ci		if (!(miicontrol & BMCR_LOOPBACK)) {
49108c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&np->lock, flags);
49118c2ecf20Sopenharmony_ci			netdev_info(dev, "Loopback already disabled\n");
49128c2ecf20Sopenharmony_ci			return 0;
49138c2ecf20Sopenharmony_ci		}
49148c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
49158c2ecf20Sopenharmony_ci		/* Turn off loopback */
49168c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&np->lock, flags);
49178c2ecf20Sopenharmony_ci		netdev_info(dev, "Internal PHY loopback mode disabled.\n");
49188c2ecf20Sopenharmony_ci		phy_init(dev);
49198c2ecf20Sopenharmony_ci	}
49208c2ecf20Sopenharmony_ci	msleep(500);
49218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&np->lock, flags);
49228c2ecf20Sopenharmony_ci	nv_enable_irq(dev);
49238c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&np->lock, flags);
49248c2ecf20Sopenharmony_ci
49258c2ecf20Sopenharmony_ci	return retval;
49268c2ecf20Sopenharmony_ci}
49278c2ecf20Sopenharmony_ci
49288c2ecf20Sopenharmony_cistatic netdev_features_t nv_fix_features(struct net_device *dev,
49298c2ecf20Sopenharmony_ci	netdev_features_t features)
49308c2ecf20Sopenharmony_ci{
49318c2ecf20Sopenharmony_ci	/* vlan is dependent on rx checksum offload */
49328c2ecf20Sopenharmony_ci	if (features & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
49338c2ecf20Sopenharmony_ci		features |= NETIF_F_RXCSUM;
49348c2ecf20Sopenharmony_ci
49358c2ecf20Sopenharmony_ci	return features;
49368c2ecf20Sopenharmony_ci}
49378c2ecf20Sopenharmony_ci
49388c2ecf20Sopenharmony_cistatic void nv_vlan_mode(struct net_device *dev, netdev_features_t features)
49398c2ecf20Sopenharmony_ci{
49408c2ecf20Sopenharmony_ci	struct fe_priv *np = get_nvpriv(dev);
49418c2ecf20Sopenharmony_ci
49428c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
49438c2ecf20Sopenharmony_ci
49448c2ecf20Sopenharmony_ci	if (features & NETIF_F_HW_VLAN_CTAG_RX)
49458c2ecf20Sopenharmony_ci		np->txrxctl_bits |= NVREG_TXRXCTL_VLANSTRIP;
49468c2ecf20Sopenharmony_ci	else
49478c2ecf20Sopenharmony_ci		np->txrxctl_bits &= ~NVREG_TXRXCTL_VLANSTRIP;
49488c2ecf20Sopenharmony_ci
49498c2ecf20Sopenharmony_ci	if (features & NETIF_F_HW_VLAN_CTAG_TX)
49508c2ecf20Sopenharmony_ci		np->txrxctl_bits |= NVREG_TXRXCTL_VLANINS;
49518c2ecf20Sopenharmony_ci	else
49528c2ecf20Sopenharmony_ci		np->txrxctl_bits &= ~NVREG_TXRXCTL_VLANINS;
49538c2ecf20Sopenharmony_ci
49548c2ecf20Sopenharmony_ci	writel(np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
49558c2ecf20Sopenharmony_ci
49568c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
49578c2ecf20Sopenharmony_ci}
49588c2ecf20Sopenharmony_ci
49598c2ecf20Sopenharmony_cistatic int nv_set_features(struct net_device *dev, netdev_features_t features)
49608c2ecf20Sopenharmony_ci{
49618c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
49628c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
49638c2ecf20Sopenharmony_ci	netdev_features_t changed = dev->features ^ features;
49648c2ecf20Sopenharmony_ci	int retval;
49658c2ecf20Sopenharmony_ci
49668c2ecf20Sopenharmony_ci	if ((changed & NETIF_F_LOOPBACK) && netif_running(dev)) {
49678c2ecf20Sopenharmony_ci		retval = nv_set_loopback(dev, features);
49688c2ecf20Sopenharmony_ci		if (retval != 0)
49698c2ecf20Sopenharmony_ci			return retval;
49708c2ecf20Sopenharmony_ci	}
49718c2ecf20Sopenharmony_ci
49728c2ecf20Sopenharmony_ci	if (changed & NETIF_F_RXCSUM) {
49738c2ecf20Sopenharmony_ci		spin_lock_irq(&np->lock);
49748c2ecf20Sopenharmony_ci
49758c2ecf20Sopenharmony_ci		if (features & NETIF_F_RXCSUM)
49768c2ecf20Sopenharmony_ci			np->txrxctl_bits |= NVREG_TXRXCTL_RXCHECK;
49778c2ecf20Sopenharmony_ci		else
49788c2ecf20Sopenharmony_ci			np->txrxctl_bits &= ~NVREG_TXRXCTL_RXCHECK;
49798c2ecf20Sopenharmony_ci
49808c2ecf20Sopenharmony_ci		if (netif_running(dev))
49818c2ecf20Sopenharmony_ci			writel(np->txrxctl_bits, base + NvRegTxRxControl);
49828c2ecf20Sopenharmony_ci
49838c2ecf20Sopenharmony_ci		spin_unlock_irq(&np->lock);
49848c2ecf20Sopenharmony_ci	}
49858c2ecf20Sopenharmony_ci
49868c2ecf20Sopenharmony_ci	if (changed & (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX))
49878c2ecf20Sopenharmony_ci		nv_vlan_mode(dev, features);
49888c2ecf20Sopenharmony_ci
49898c2ecf20Sopenharmony_ci	return 0;
49908c2ecf20Sopenharmony_ci}
49918c2ecf20Sopenharmony_ci
49928c2ecf20Sopenharmony_cistatic int nv_get_sset_count(struct net_device *dev, int sset)
49938c2ecf20Sopenharmony_ci{
49948c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
49958c2ecf20Sopenharmony_ci
49968c2ecf20Sopenharmony_ci	switch (sset) {
49978c2ecf20Sopenharmony_ci	case ETH_SS_TEST:
49988c2ecf20Sopenharmony_ci		if (np->driver_data & DEV_HAS_TEST_EXTENDED)
49998c2ecf20Sopenharmony_ci			return NV_TEST_COUNT_EXTENDED;
50008c2ecf20Sopenharmony_ci		else
50018c2ecf20Sopenharmony_ci			return NV_TEST_COUNT_BASE;
50028c2ecf20Sopenharmony_ci	case ETH_SS_STATS:
50038c2ecf20Sopenharmony_ci		if (np->driver_data & DEV_HAS_STATISTICS_V3)
50048c2ecf20Sopenharmony_ci			return NV_DEV_STATISTICS_V3_COUNT;
50058c2ecf20Sopenharmony_ci		else if (np->driver_data & DEV_HAS_STATISTICS_V2)
50068c2ecf20Sopenharmony_ci			return NV_DEV_STATISTICS_V2_COUNT;
50078c2ecf20Sopenharmony_ci		else if (np->driver_data & DEV_HAS_STATISTICS_V1)
50088c2ecf20Sopenharmony_ci			return NV_DEV_STATISTICS_V1_COUNT;
50098c2ecf20Sopenharmony_ci		else
50108c2ecf20Sopenharmony_ci			return 0;
50118c2ecf20Sopenharmony_ci	default:
50128c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
50138c2ecf20Sopenharmony_ci	}
50148c2ecf20Sopenharmony_ci}
50158c2ecf20Sopenharmony_ci
50168c2ecf20Sopenharmony_cistatic void nv_get_ethtool_stats(struct net_device *dev,
50178c2ecf20Sopenharmony_ci				 struct ethtool_stats *estats, u64 *buffer)
50188c2ecf20Sopenharmony_ci	__acquires(&netdev_priv(dev)->hwstats_lock)
50198c2ecf20Sopenharmony_ci	__releases(&netdev_priv(dev)->hwstats_lock)
50208c2ecf20Sopenharmony_ci{
50218c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
50228c2ecf20Sopenharmony_ci
50238c2ecf20Sopenharmony_ci	spin_lock_bh(&np->hwstats_lock);
50248c2ecf20Sopenharmony_ci	nv_update_stats(dev);
50258c2ecf20Sopenharmony_ci	memcpy(buffer, &np->estats,
50268c2ecf20Sopenharmony_ci	       nv_get_sset_count(dev, ETH_SS_STATS)*sizeof(u64));
50278c2ecf20Sopenharmony_ci	spin_unlock_bh(&np->hwstats_lock);
50288c2ecf20Sopenharmony_ci}
50298c2ecf20Sopenharmony_ci
50308c2ecf20Sopenharmony_cistatic int nv_link_test(struct net_device *dev)
50318c2ecf20Sopenharmony_ci{
50328c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
50338c2ecf20Sopenharmony_ci	int mii_status;
50348c2ecf20Sopenharmony_ci
50358c2ecf20Sopenharmony_ci	mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
50368c2ecf20Sopenharmony_ci	mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
50378c2ecf20Sopenharmony_ci
50388c2ecf20Sopenharmony_ci	/* check phy link status */
50398c2ecf20Sopenharmony_ci	if (!(mii_status & BMSR_LSTATUS))
50408c2ecf20Sopenharmony_ci		return 0;
50418c2ecf20Sopenharmony_ci	else
50428c2ecf20Sopenharmony_ci		return 1;
50438c2ecf20Sopenharmony_ci}
50448c2ecf20Sopenharmony_ci
50458c2ecf20Sopenharmony_cistatic int nv_register_test(struct net_device *dev)
50468c2ecf20Sopenharmony_ci{
50478c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
50488c2ecf20Sopenharmony_ci	int i = 0;
50498c2ecf20Sopenharmony_ci	u32 orig_read, new_read;
50508c2ecf20Sopenharmony_ci
50518c2ecf20Sopenharmony_ci	do {
50528c2ecf20Sopenharmony_ci		orig_read = readl(base + nv_registers_test[i].reg);
50538c2ecf20Sopenharmony_ci
50548c2ecf20Sopenharmony_ci		/* xor with mask to toggle bits */
50558c2ecf20Sopenharmony_ci		orig_read ^= nv_registers_test[i].mask;
50568c2ecf20Sopenharmony_ci
50578c2ecf20Sopenharmony_ci		writel(orig_read, base + nv_registers_test[i].reg);
50588c2ecf20Sopenharmony_ci
50598c2ecf20Sopenharmony_ci		new_read = readl(base + nv_registers_test[i].reg);
50608c2ecf20Sopenharmony_ci
50618c2ecf20Sopenharmony_ci		if ((new_read & nv_registers_test[i].mask) != (orig_read & nv_registers_test[i].mask))
50628c2ecf20Sopenharmony_ci			return 0;
50638c2ecf20Sopenharmony_ci
50648c2ecf20Sopenharmony_ci		/* restore original value */
50658c2ecf20Sopenharmony_ci		orig_read ^= nv_registers_test[i].mask;
50668c2ecf20Sopenharmony_ci		writel(orig_read, base + nv_registers_test[i].reg);
50678c2ecf20Sopenharmony_ci
50688c2ecf20Sopenharmony_ci	} while (nv_registers_test[++i].reg != 0);
50698c2ecf20Sopenharmony_ci
50708c2ecf20Sopenharmony_ci	return 1;
50718c2ecf20Sopenharmony_ci}
50728c2ecf20Sopenharmony_ci
50738c2ecf20Sopenharmony_cistatic int nv_interrupt_test(struct net_device *dev)
50748c2ecf20Sopenharmony_ci{
50758c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
50768c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
50778c2ecf20Sopenharmony_ci	int ret = 1;
50788c2ecf20Sopenharmony_ci	int testcnt;
50798c2ecf20Sopenharmony_ci	u32 save_msi_flags, save_poll_interval = 0;
50808c2ecf20Sopenharmony_ci
50818c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
50828c2ecf20Sopenharmony_ci		/* free current irq */
50838c2ecf20Sopenharmony_ci		nv_free_irq(dev);
50848c2ecf20Sopenharmony_ci		save_poll_interval = readl(base+NvRegPollingInterval);
50858c2ecf20Sopenharmony_ci	}
50868c2ecf20Sopenharmony_ci
50878c2ecf20Sopenharmony_ci	/* flag to test interrupt handler */
50888c2ecf20Sopenharmony_ci	np->intr_test = 0;
50898c2ecf20Sopenharmony_ci
50908c2ecf20Sopenharmony_ci	/* setup test irq */
50918c2ecf20Sopenharmony_ci	save_msi_flags = np->msi_flags;
50928c2ecf20Sopenharmony_ci	np->msi_flags &= ~NV_MSI_X_VECTORS_MASK;
50938c2ecf20Sopenharmony_ci	np->msi_flags |= 0x001; /* setup 1 vector */
50948c2ecf20Sopenharmony_ci	if (nv_request_irq(dev, 1))
50958c2ecf20Sopenharmony_ci		return 0;
50968c2ecf20Sopenharmony_ci
50978c2ecf20Sopenharmony_ci	/* setup timer interrupt */
50988c2ecf20Sopenharmony_ci	writel(NVREG_POLL_DEFAULT_CPU, base + NvRegPollingInterval);
50998c2ecf20Sopenharmony_ci	writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
51008c2ecf20Sopenharmony_ci
51018c2ecf20Sopenharmony_ci	nv_enable_hw_interrupts(dev, NVREG_IRQ_TIMER);
51028c2ecf20Sopenharmony_ci
51038c2ecf20Sopenharmony_ci	/* wait for at least one interrupt */
51048c2ecf20Sopenharmony_ci	msleep(100);
51058c2ecf20Sopenharmony_ci
51068c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
51078c2ecf20Sopenharmony_ci
51088c2ecf20Sopenharmony_ci	/* flag should be set within ISR */
51098c2ecf20Sopenharmony_ci	testcnt = np->intr_test;
51108c2ecf20Sopenharmony_ci	if (!testcnt)
51118c2ecf20Sopenharmony_ci		ret = 2;
51128c2ecf20Sopenharmony_ci
51138c2ecf20Sopenharmony_ci	nv_disable_hw_interrupts(dev, NVREG_IRQ_TIMER);
51148c2ecf20Sopenharmony_ci	if (!(np->msi_flags & NV_MSI_X_ENABLED))
51158c2ecf20Sopenharmony_ci		writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
51168c2ecf20Sopenharmony_ci	else
51178c2ecf20Sopenharmony_ci		writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
51188c2ecf20Sopenharmony_ci
51198c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
51208c2ecf20Sopenharmony_ci
51218c2ecf20Sopenharmony_ci	nv_free_irq(dev);
51228c2ecf20Sopenharmony_ci
51238c2ecf20Sopenharmony_ci	np->msi_flags = save_msi_flags;
51248c2ecf20Sopenharmony_ci
51258c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
51268c2ecf20Sopenharmony_ci		writel(save_poll_interval, base + NvRegPollingInterval);
51278c2ecf20Sopenharmony_ci		writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
51288c2ecf20Sopenharmony_ci		/* restore original irq */
51298c2ecf20Sopenharmony_ci		if (nv_request_irq(dev, 0))
51308c2ecf20Sopenharmony_ci			return 0;
51318c2ecf20Sopenharmony_ci	}
51328c2ecf20Sopenharmony_ci
51338c2ecf20Sopenharmony_ci	return ret;
51348c2ecf20Sopenharmony_ci}
51358c2ecf20Sopenharmony_ci
51368c2ecf20Sopenharmony_cistatic int nv_loopback_test(struct net_device *dev)
51378c2ecf20Sopenharmony_ci{
51388c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
51398c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
51408c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb, *rx_skb;
51418c2ecf20Sopenharmony_ci	dma_addr_t test_dma_addr;
51428c2ecf20Sopenharmony_ci	u32 tx_flags_extra = (np->desc_ver == DESC_VER_1 ? NV_TX_LASTPACKET : NV_TX2_LASTPACKET);
51438c2ecf20Sopenharmony_ci	u32 flags;
51448c2ecf20Sopenharmony_ci	int len, i, pkt_len;
51458c2ecf20Sopenharmony_ci	u8 *pkt_data;
51468c2ecf20Sopenharmony_ci	u32 filter_flags = 0;
51478c2ecf20Sopenharmony_ci	u32 misc1_flags = 0;
51488c2ecf20Sopenharmony_ci	int ret = 1;
51498c2ecf20Sopenharmony_ci
51508c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
51518c2ecf20Sopenharmony_ci		nv_disable_irq(dev);
51528c2ecf20Sopenharmony_ci		filter_flags = readl(base + NvRegPacketFilterFlags);
51538c2ecf20Sopenharmony_ci		misc1_flags = readl(base + NvRegMisc1);
51548c2ecf20Sopenharmony_ci	} else {
51558c2ecf20Sopenharmony_ci		nv_txrx_reset(dev);
51568c2ecf20Sopenharmony_ci	}
51578c2ecf20Sopenharmony_ci
51588c2ecf20Sopenharmony_ci	/* reinit driver view of the rx queue */
51598c2ecf20Sopenharmony_ci	set_bufsize(dev);
51608c2ecf20Sopenharmony_ci	nv_init_ring(dev);
51618c2ecf20Sopenharmony_ci
51628c2ecf20Sopenharmony_ci	/* setup hardware for loopback */
51638c2ecf20Sopenharmony_ci	writel(NVREG_MISC1_FORCE, base + NvRegMisc1);
51648c2ecf20Sopenharmony_ci	writel(NVREG_PFF_ALWAYS | NVREG_PFF_LOOPBACK, base + NvRegPacketFilterFlags);
51658c2ecf20Sopenharmony_ci
51668c2ecf20Sopenharmony_ci	/* reinit nic view of the rx queue */
51678c2ecf20Sopenharmony_ci	writel(np->rx_buf_sz, base + NvRegOffloadConfig);
51688c2ecf20Sopenharmony_ci	setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
51698c2ecf20Sopenharmony_ci	writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
51708c2ecf20Sopenharmony_ci		base + NvRegRingSizes);
51718c2ecf20Sopenharmony_ci	pci_push(base);
51728c2ecf20Sopenharmony_ci
51738c2ecf20Sopenharmony_ci	/* restart rx engine */
51748c2ecf20Sopenharmony_ci	nv_start_rxtx(dev);
51758c2ecf20Sopenharmony_ci
51768c2ecf20Sopenharmony_ci	/* setup packet for tx */
51778c2ecf20Sopenharmony_ci	pkt_len = ETH_DATA_LEN;
51788c2ecf20Sopenharmony_ci	tx_skb = netdev_alloc_skb(dev, pkt_len);
51798c2ecf20Sopenharmony_ci	if (!tx_skb) {
51808c2ecf20Sopenharmony_ci		ret = 0;
51818c2ecf20Sopenharmony_ci		goto out;
51828c2ecf20Sopenharmony_ci	}
51838c2ecf20Sopenharmony_ci	test_dma_addr = dma_map_single(&np->pci_dev->dev, tx_skb->data,
51848c2ecf20Sopenharmony_ci				       skb_tailroom(tx_skb),
51858c2ecf20Sopenharmony_ci				       DMA_FROM_DEVICE);
51868c2ecf20Sopenharmony_ci	if (unlikely(dma_mapping_error(&np->pci_dev->dev,
51878c2ecf20Sopenharmony_ci				       test_dma_addr))) {
51888c2ecf20Sopenharmony_ci		dev_kfree_skb_any(tx_skb);
51898c2ecf20Sopenharmony_ci		goto out;
51908c2ecf20Sopenharmony_ci	}
51918c2ecf20Sopenharmony_ci	pkt_data = skb_put(tx_skb, pkt_len);
51928c2ecf20Sopenharmony_ci	for (i = 0; i < pkt_len; i++)
51938c2ecf20Sopenharmony_ci		pkt_data[i] = (u8)(i & 0xff);
51948c2ecf20Sopenharmony_ci
51958c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
51968c2ecf20Sopenharmony_ci		np->tx_ring.orig[0].buf = cpu_to_le32(test_dma_addr);
51978c2ecf20Sopenharmony_ci		np->tx_ring.orig[0].flaglen = cpu_to_le32((pkt_len-1) | np->tx_flags | tx_flags_extra);
51988c2ecf20Sopenharmony_ci	} else {
51998c2ecf20Sopenharmony_ci		np->tx_ring.ex[0].bufhigh = cpu_to_le32(dma_high(test_dma_addr));
52008c2ecf20Sopenharmony_ci		np->tx_ring.ex[0].buflow = cpu_to_le32(dma_low(test_dma_addr));
52018c2ecf20Sopenharmony_ci		np->tx_ring.ex[0].flaglen = cpu_to_le32((pkt_len-1) | np->tx_flags | tx_flags_extra);
52028c2ecf20Sopenharmony_ci	}
52038c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
52048c2ecf20Sopenharmony_ci	pci_push(get_hwbase(dev));
52058c2ecf20Sopenharmony_ci
52068c2ecf20Sopenharmony_ci	msleep(500);
52078c2ecf20Sopenharmony_ci
52088c2ecf20Sopenharmony_ci	/* check for rx of the packet */
52098c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
52108c2ecf20Sopenharmony_ci		flags = le32_to_cpu(np->rx_ring.orig[0].flaglen);
52118c2ecf20Sopenharmony_ci		len = nv_descr_getlength(&np->rx_ring.orig[0], np->desc_ver);
52128c2ecf20Sopenharmony_ci
52138c2ecf20Sopenharmony_ci	} else {
52148c2ecf20Sopenharmony_ci		flags = le32_to_cpu(np->rx_ring.ex[0].flaglen);
52158c2ecf20Sopenharmony_ci		len = nv_descr_getlength_ex(&np->rx_ring.ex[0], np->desc_ver);
52168c2ecf20Sopenharmony_ci	}
52178c2ecf20Sopenharmony_ci
52188c2ecf20Sopenharmony_ci	if (flags & NV_RX_AVAIL) {
52198c2ecf20Sopenharmony_ci		ret = 0;
52208c2ecf20Sopenharmony_ci	} else if (np->desc_ver == DESC_VER_1) {
52218c2ecf20Sopenharmony_ci		if (flags & NV_RX_ERROR)
52228c2ecf20Sopenharmony_ci			ret = 0;
52238c2ecf20Sopenharmony_ci	} else {
52248c2ecf20Sopenharmony_ci		if (flags & NV_RX2_ERROR)
52258c2ecf20Sopenharmony_ci			ret = 0;
52268c2ecf20Sopenharmony_ci	}
52278c2ecf20Sopenharmony_ci
52288c2ecf20Sopenharmony_ci	if (ret) {
52298c2ecf20Sopenharmony_ci		if (len != pkt_len) {
52308c2ecf20Sopenharmony_ci			ret = 0;
52318c2ecf20Sopenharmony_ci		} else {
52328c2ecf20Sopenharmony_ci			rx_skb = np->rx_skb[0].skb;
52338c2ecf20Sopenharmony_ci			for (i = 0; i < pkt_len; i++) {
52348c2ecf20Sopenharmony_ci				if (rx_skb->data[i] != (u8)(i & 0xff)) {
52358c2ecf20Sopenharmony_ci					ret = 0;
52368c2ecf20Sopenharmony_ci					break;
52378c2ecf20Sopenharmony_ci				}
52388c2ecf20Sopenharmony_ci			}
52398c2ecf20Sopenharmony_ci		}
52408c2ecf20Sopenharmony_ci	}
52418c2ecf20Sopenharmony_ci
52428c2ecf20Sopenharmony_ci	dma_unmap_single(&np->pci_dev->dev, test_dma_addr,
52438c2ecf20Sopenharmony_ci			 (skb_end_pointer(tx_skb) - tx_skb->data),
52448c2ecf20Sopenharmony_ci			 DMA_TO_DEVICE);
52458c2ecf20Sopenharmony_ci	dev_kfree_skb_any(tx_skb);
52468c2ecf20Sopenharmony_ci out:
52478c2ecf20Sopenharmony_ci	/* stop engines */
52488c2ecf20Sopenharmony_ci	nv_stop_rxtx(dev);
52498c2ecf20Sopenharmony_ci	nv_txrx_reset(dev);
52508c2ecf20Sopenharmony_ci	/* drain rx queue */
52518c2ecf20Sopenharmony_ci	nv_drain_rxtx(dev);
52528c2ecf20Sopenharmony_ci
52538c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
52548c2ecf20Sopenharmony_ci		writel(misc1_flags, base + NvRegMisc1);
52558c2ecf20Sopenharmony_ci		writel(filter_flags, base + NvRegPacketFilterFlags);
52568c2ecf20Sopenharmony_ci		nv_enable_irq(dev);
52578c2ecf20Sopenharmony_ci	}
52588c2ecf20Sopenharmony_ci
52598c2ecf20Sopenharmony_ci	return ret;
52608c2ecf20Sopenharmony_ci}
52618c2ecf20Sopenharmony_ci
52628c2ecf20Sopenharmony_cistatic void nv_self_test(struct net_device *dev, struct ethtool_test *test, u64 *buffer)
52638c2ecf20Sopenharmony_ci{
52648c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
52658c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
52668c2ecf20Sopenharmony_ci	int result, count;
52678c2ecf20Sopenharmony_ci
52688c2ecf20Sopenharmony_ci	count = nv_get_sset_count(dev, ETH_SS_TEST);
52698c2ecf20Sopenharmony_ci	memset(buffer, 0, count * sizeof(u64));
52708c2ecf20Sopenharmony_ci
52718c2ecf20Sopenharmony_ci	if (!nv_link_test(dev)) {
52728c2ecf20Sopenharmony_ci		test->flags |= ETH_TEST_FL_FAILED;
52738c2ecf20Sopenharmony_ci		buffer[0] = 1;
52748c2ecf20Sopenharmony_ci	}
52758c2ecf20Sopenharmony_ci
52768c2ecf20Sopenharmony_ci	if (test->flags & ETH_TEST_FL_OFFLINE) {
52778c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
52788c2ecf20Sopenharmony_ci			netif_stop_queue(dev);
52798c2ecf20Sopenharmony_ci			nv_napi_disable(dev);
52808c2ecf20Sopenharmony_ci			netif_tx_lock_bh(dev);
52818c2ecf20Sopenharmony_ci			netif_addr_lock(dev);
52828c2ecf20Sopenharmony_ci			spin_lock_irq(&np->lock);
52838c2ecf20Sopenharmony_ci			nv_disable_hw_interrupts(dev, np->irqmask);
52848c2ecf20Sopenharmony_ci			if (!(np->msi_flags & NV_MSI_X_ENABLED))
52858c2ecf20Sopenharmony_ci				writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
52868c2ecf20Sopenharmony_ci			else
52878c2ecf20Sopenharmony_ci				writel(NVREG_IRQSTAT_MASK, base + NvRegMSIXIrqStatus);
52888c2ecf20Sopenharmony_ci			/* stop engines */
52898c2ecf20Sopenharmony_ci			nv_stop_rxtx(dev);
52908c2ecf20Sopenharmony_ci			nv_txrx_reset(dev);
52918c2ecf20Sopenharmony_ci			/* drain rx queue */
52928c2ecf20Sopenharmony_ci			nv_drain_rxtx(dev);
52938c2ecf20Sopenharmony_ci			spin_unlock_irq(&np->lock);
52948c2ecf20Sopenharmony_ci			netif_addr_unlock(dev);
52958c2ecf20Sopenharmony_ci			netif_tx_unlock_bh(dev);
52968c2ecf20Sopenharmony_ci		}
52978c2ecf20Sopenharmony_ci
52988c2ecf20Sopenharmony_ci		if (!nv_register_test(dev)) {
52998c2ecf20Sopenharmony_ci			test->flags |= ETH_TEST_FL_FAILED;
53008c2ecf20Sopenharmony_ci			buffer[1] = 1;
53018c2ecf20Sopenharmony_ci		}
53028c2ecf20Sopenharmony_ci
53038c2ecf20Sopenharmony_ci		result = nv_interrupt_test(dev);
53048c2ecf20Sopenharmony_ci		if (result != 1) {
53058c2ecf20Sopenharmony_ci			test->flags |= ETH_TEST_FL_FAILED;
53068c2ecf20Sopenharmony_ci			buffer[2] = 1;
53078c2ecf20Sopenharmony_ci		}
53088c2ecf20Sopenharmony_ci		if (result == 0) {
53098c2ecf20Sopenharmony_ci			/* bail out */
53108c2ecf20Sopenharmony_ci			return;
53118c2ecf20Sopenharmony_ci		}
53128c2ecf20Sopenharmony_ci
53138c2ecf20Sopenharmony_ci		if (count > NV_TEST_COUNT_BASE && !nv_loopback_test(dev)) {
53148c2ecf20Sopenharmony_ci			test->flags |= ETH_TEST_FL_FAILED;
53158c2ecf20Sopenharmony_ci			buffer[3] = 1;
53168c2ecf20Sopenharmony_ci		}
53178c2ecf20Sopenharmony_ci
53188c2ecf20Sopenharmony_ci		if (netif_running(dev)) {
53198c2ecf20Sopenharmony_ci			/* reinit driver view of the rx queue */
53208c2ecf20Sopenharmony_ci			set_bufsize(dev);
53218c2ecf20Sopenharmony_ci			if (nv_init_ring(dev)) {
53228c2ecf20Sopenharmony_ci				if (!np->in_shutdown)
53238c2ecf20Sopenharmony_ci					mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
53248c2ecf20Sopenharmony_ci			}
53258c2ecf20Sopenharmony_ci			/* reinit nic view of the rx queue */
53268c2ecf20Sopenharmony_ci			writel(np->rx_buf_sz, base + NvRegOffloadConfig);
53278c2ecf20Sopenharmony_ci			setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
53288c2ecf20Sopenharmony_ci			writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
53298c2ecf20Sopenharmony_ci				base + NvRegRingSizes);
53308c2ecf20Sopenharmony_ci			pci_push(base);
53318c2ecf20Sopenharmony_ci			writel(NVREG_TXRXCTL_KICK|np->txrxctl_bits, get_hwbase(dev) + NvRegTxRxControl);
53328c2ecf20Sopenharmony_ci			pci_push(base);
53338c2ecf20Sopenharmony_ci			/* restart rx engine */
53348c2ecf20Sopenharmony_ci			nv_start_rxtx(dev);
53358c2ecf20Sopenharmony_ci			netif_start_queue(dev);
53368c2ecf20Sopenharmony_ci			nv_napi_enable(dev);
53378c2ecf20Sopenharmony_ci			nv_enable_hw_interrupts(dev, np->irqmask);
53388c2ecf20Sopenharmony_ci		}
53398c2ecf20Sopenharmony_ci	}
53408c2ecf20Sopenharmony_ci}
53418c2ecf20Sopenharmony_ci
53428c2ecf20Sopenharmony_cistatic void nv_get_strings(struct net_device *dev, u32 stringset, u8 *buffer)
53438c2ecf20Sopenharmony_ci{
53448c2ecf20Sopenharmony_ci	switch (stringset) {
53458c2ecf20Sopenharmony_ci	case ETH_SS_STATS:
53468c2ecf20Sopenharmony_ci		memcpy(buffer, &nv_estats_str, nv_get_sset_count(dev, ETH_SS_STATS)*sizeof(struct nv_ethtool_str));
53478c2ecf20Sopenharmony_ci		break;
53488c2ecf20Sopenharmony_ci	case ETH_SS_TEST:
53498c2ecf20Sopenharmony_ci		memcpy(buffer, &nv_etests_str, nv_get_sset_count(dev, ETH_SS_TEST)*sizeof(struct nv_ethtool_str));
53508c2ecf20Sopenharmony_ci		break;
53518c2ecf20Sopenharmony_ci	}
53528c2ecf20Sopenharmony_ci}
53538c2ecf20Sopenharmony_ci
53548c2ecf20Sopenharmony_cistatic const struct ethtool_ops ops = {
53558c2ecf20Sopenharmony_ci	.get_drvinfo = nv_get_drvinfo,
53568c2ecf20Sopenharmony_ci	.get_link = ethtool_op_get_link,
53578c2ecf20Sopenharmony_ci	.get_wol = nv_get_wol,
53588c2ecf20Sopenharmony_ci	.set_wol = nv_set_wol,
53598c2ecf20Sopenharmony_ci	.get_regs_len = nv_get_regs_len,
53608c2ecf20Sopenharmony_ci	.get_regs = nv_get_regs,
53618c2ecf20Sopenharmony_ci	.nway_reset = nv_nway_reset,
53628c2ecf20Sopenharmony_ci	.get_ringparam = nv_get_ringparam,
53638c2ecf20Sopenharmony_ci	.set_ringparam = nv_set_ringparam,
53648c2ecf20Sopenharmony_ci	.get_pauseparam = nv_get_pauseparam,
53658c2ecf20Sopenharmony_ci	.set_pauseparam = nv_set_pauseparam,
53668c2ecf20Sopenharmony_ci	.get_strings = nv_get_strings,
53678c2ecf20Sopenharmony_ci	.get_ethtool_stats = nv_get_ethtool_stats,
53688c2ecf20Sopenharmony_ci	.get_sset_count = nv_get_sset_count,
53698c2ecf20Sopenharmony_ci	.self_test = nv_self_test,
53708c2ecf20Sopenharmony_ci	.get_ts_info = ethtool_op_get_ts_info,
53718c2ecf20Sopenharmony_ci	.get_link_ksettings = nv_get_link_ksettings,
53728c2ecf20Sopenharmony_ci	.set_link_ksettings = nv_set_link_ksettings,
53738c2ecf20Sopenharmony_ci};
53748c2ecf20Sopenharmony_ci
53758c2ecf20Sopenharmony_ci/* The mgmt unit and driver use a semaphore to access the phy during init */
53768c2ecf20Sopenharmony_cistatic int nv_mgmt_acquire_sema(struct net_device *dev)
53778c2ecf20Sopenharmony_ci{
53788c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
53798c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
53808c2ecf20Sopenharmony_ci	int i;
53818c2ecf20Sopenharmony_ci	u32 tx_ctrl, mgmt_sema;
53828c2ecf20Sopenharmony_ci
53838c2ecf20Sopenharmony_ci	for (i = 0; i < 10; i++) {
53848c2ecf20Sopenharmony_ci		mgmt_sema = readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_MGMT_SEMA_MASK;
53858c2ecf20Sopenharmony_ci		if (mgmt_sema == NVREG_XMITCTL_MGMT_SEMA_FREE)
53868c2ecf20Sopenharmony_ci			break;
53878c2ecf20Sopenharmony_ci		msleep(500);
53888c2ecf20Sopenharmony_ci	}
53898c2ecf20Sopenharmony_ci
53908c2ecf20Sopenharmony_ci	if (mgmt_sema != NVREG_XMITCTL_MGMT_SEMA_FREE)
53918c2ecf20Sopenharmony_ci		return 0;
53928c2ecf20Sopenharmony_ci
53938c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
53948c2ecf20Sopenharmony_ci		tx_ctrl = readl(base + NvRegTransmitterControl);
53958c2ecf20Sopenharmony_ci		tx_ctrl |= NVREG_XMITCTL_HOST_SEMA_ACQ;
53968c2ecf20Sopenharmony_ci		writel(tx_ctrl, base + NvRegTransmitterControl);
53978c2ecf20Sopenharmony_ci
53988c2ecf20Sopenharmony_ci		/* verify that semaphore was acquired */
53998c2ecf20Sopenharmony_ci		tx_ctrl = readl(base + NvRegTransmitterControl);
54008c2ecf20Sopenharmony_ci		if (((tx_ctrl & NVREG_XMITCTL_HOST_SEMA_MASK) == NVREG_XMITCTL_HOST_SEMA_ACQ) &&
54018c2ecf20Sopenharmony_ci		    ((tx_ctrl & NVREG_XMITCTL_MGMT_SEMA_MASK) == NVREG_XMITCTL_MGMT_SEMA_FREE)) {
54028c2ecf20Sopenharmony_ci			np->mgmt_sema = 1;
54038c2ecf20Sopenharmony_ci			return 1;
54048c2ecf20Sopenharmony_ci		} else
54058c2ecf20Sopenharmony_ci			udelay(50);
54068c2ecf20Sopenharmony_ci	}
54078c2ecf20Sopenharmony_ci
54088c2ecf20Sopenharmony_ci	return 0;
54098c2ecf20Sopenharmony_ci}
54108c2ecf20Sopenharmony_ci
54118c2ecf20Sopenharmony_cistatic void nv_mgmt_release_sema(struct net_device *dev)
54128c2ecf20Sopenharmony_ci{
54138c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
54148c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
54158c2ecf20Sopenharmony_ci	u32 tx_ctrl;
54168c2ecf20Sopenharmony_ci
54178c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_HAS_MGMT_UNIT) {
54188c2ecf20Sopenharmony_ci		if (np->mgmt_sema) {
54198c2ecf20Sopenharmony_ci			tx_ctrl = readl(base + NvRegTransmitterControl);
54208c2ecf20Sopenharmony_ci			tx_ctrl &= ~NVREG_XMITCTL_HOST_SEMA_ACQ;
54218c2ecf20Sopenharmony_ci			writel(tx_ctrl, base + NvRegTransmitterControl);
54228c2ecf20Sopenharmony_ci		}
54238c2ecf20Sopenharmony_ci	}
54248c2ecf20Sopenharmony_ci}
54258c2ecf20Sopenharmony_ci
54268c2ecf20Sopenharmony_ci
54278c2ecf20Sopenharmony_cistatic int nv_mgmt_get_version(struct net_device *dev)
54288c2ecf20Sopenharmony_ci{
54298c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
54308c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
54318c2ecf20Sopenharmony_ci	u32 data_ready = readl(base + NvRegTransmitterControl);
54328c2ecf20Sopenharmony_ci	u32 data_ready2 = 0;
54338c2ecf20Sopenharmony_ci	unsigned long start;
54348c2ecf20Sopenharmony_ci	int ready = 0;
54358c2ecf20Sopenharmony_ci
54368c2ecf20Sopenharmony_ci	writel(NVREG_MGMTUNITGETVERSION, base + NvRegMgmtUnitGetVersion);
54378c2ecf20Sopenharmony_ci	writel(data_ready ^ NVREG_XMITCTL_DATA_START, base + NvRegTransmitterControl);
54388c2ecf20Sopenharmony_ci	start = jiffies;
54398c2ecf20Sopenharmony_ci	while (time_before(jiffies, start + 5*HZ)) {
54408c2ecf20Sopenharmony_ci		data_ready2 = readl(base + NvRegTransmitterControl);
54418c2ecf20Sopenharmony_ci		if ((data_ready & NVREG_XMITCTL_DATA_READY) != (data_ready2 & NVREG_XMITCTL_DATA_READY)) {
54428c2ecf20Sopenharmony_ci			ready = 1;
54438c2ecf20Sopenharmony_ci			break;
54448c2ecf20Sopenharmony_ci		}
54458c2ecf20Sopenharmony_ci		schedule_timeout_uninterruptible(1);
54468c2ecf20Sopenharmony_ci	}
54478c2ecf20Sopenharmony_ci
54488c2ecf20Sopenharmony_ci	if (!ready || (data_ready2 & NVREG_XMITCTL_DATA_ERROR))
54498c2ecf20Sopenharmony_ci		return 0;
54508c2ecf20Sopenharmony_ci
54518c2ecf20Sopenharmony_ci	np->mgmt_version = readl(base + NvRegMgmtUnitVersion) & NVREG_MGMTUNITVERSION;
54528c2ecf20Sopenharmony_ci
54538c2ecf20Sopenharmony_ci	return 1;
54548c2ecf20Sopenharmony_ci}
54558c2ecf20Sopenharmony_ci
54568c2ecf20Sopenharmony_cistatic int nv_open(struct net_device *dev)
54578c2ecf20Sopenharmony_ci{
54588c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
54598c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
54608c2ecf20Sopenharmony_ci	int ret = 1;
54618c2ecf20Sopenharmony_ci	int oom, i;
54628c2ecf20Sopenharmony_ci	u32 low;
54638c2ecf20Sopenharmony_ci
54648c2ecf20Sopenharmony_ci	/* power up phy */
54658c2ecf20Sopenharmony_ci	mii_rw(dev, np->phyaddr, MII_BMCR,
54668c2ecf20Sopenharmony_ci	       mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ) & ~BMCR_PDOWN);
54678c2ecf20Sopenharmony_ci
54688c2ecf20Sopenharmony_ci	nv_txrx_gate(dev, false);
54698c2ecf20Sopenharmony_ci	/* erase previous misconfiguration */
54708c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_HAS_POWER_CNTRL)
54718c2ecf20Sopenharmony_ci		nv_mac_reset(dev);
54728c2ecf20Sopenharmony_ci	writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
54738c2ecf20Sopenharmony_ci	writel(0, base + NvRegMulticastAddrB);
54748c2ecf20Sopenharmony_ci	writel(NVREG_MCASTMASKA_NONE, base + NvRegMulticastMaskA);
54758c2ecf20Sopenharmony_ci	writel(NVREG_MCASTMASKB_NONE, base + NvRegMulticastMaskB);
54768c2ecf20Sopenharmony_ci	writel(0, base + NvRegPacketFilterFlags);
54778c2ecf20Sopenharmony_ci
54788c2ecf20Sopenharmony_ci	writel(0, base + NvRegTransmitterControl);
54798c2ecf20Sopenharmony_ci	writel(0, base + NvRegReceiverControl);
54808c2ecf20Sopenharmony_ci
54818c2ecf20Sopenharmony_ci	writel(0, base + NvRegAdapterControl);
54828c2ecf20Sopenharmony_ci
54838c2ecf20Sopenharmony_ci	if (np->pause_flags & NV_PAUSEFRAME_TX_CAPABLE)
54848c2ecf20Sopenharmony_ci		writel(NVREG_TX_PAUSEFRAME_DISABLE,  base + NvRegTxPauseFrame);
54858c2ecf20Sopenharmony_ci
54868c2ecf20Sopenharmony_ci	/* initialize descriptor rings */
54878c2ecf20Sopenharmony_ci	set_bufsize(dev);
54888c2ecf20Sopenharmony_ci	oom = nv_init_ring(dev);
54898c2ecf20Sopenharmony_ci
54908c2ecf20Sopenharmony_ci	writel(0, base + NvRegLinkSpeed);
54918c2ecf20Sopenharmony_ci	writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
54928c2ecf20Sopenharmony_ci	nv_txrx_reset(dev);
54938c2ecf20Sopenharmony_ci	writel(0, base + NvRegUnknownSetupReg6);
54948c2ecf20Sopenharmony_ci
54958c2ecf20Sopenharmony_ci	np->in_shutdown = 0;
54968c2ecf20Sopenharmony_ci
54978c2ecf20Sopenharmony_ci	/* give hw rings */
54988c2ecf20Sopenharmony_ci	setup_hw_rings(dev, NV_SETUP_RX_RING | NV_SETUP_TX_RING);
54998c2ecf20Sopenharmony_ci	writel(((np->rx_ring_size-1) << NVREG_RINGSZ_RXSHIFT) + ((np->tx_ring_size-1) << NVREG_RINGSZ_TXSHIFT),
55008c2ecf20Sopenharmony_ci		base + NvRegRingSizes);
55018c2ecf20Sopenharmony_ci
55028c2ecf20Sopenharmony_ci	writel(np->linkspeed, base + NvRegLinkSpeed);
55038c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1)
55048c2ecf20Sopenharmony_ci		writel(NVREG_TX_WM_DESC1_DEFAULT, base + NvRegTxWatermark);
55058c2ecf20Sopenharmony_ci	else
55068c2ecf20Sopenharmony_ci		writel(NVREG_TX_WM_DESC2_3_DEFAULT, base + NvRegTxWatermark);
55078c2ecf20Sopenharmony_ci	writel(np->txrxctl_bits, base + NvRegTxRxControl);
55088c2ecf20Sopenharmony_ci	writel(np->vlanctl_bits, base + NvRegVlanControl);
55098c2ecf20Sopenharmony_ci	pci_push(base);
55108c2ecf20Sopenharmony_ci	writel(NVREG_TXRXCTL_BIT1|np->txrxctl_bits, base + NvRegTxRxControl);
55118c2ecf20Sopenharmony_ci	if (reg_delay(dev, NvRegUnknownSetupReg5,
55128c2ecf20Sopenharmony_ci		      NVREG_UNKSETUP5_BIT31, NVREG_UNKSETUP5_BIT31,
55138c2ecf20Sopenharmony_ci		      NV_SETUP5_DELAY, NV_SETUP5_DELAYMAX))
55148c2ecf20Sopenharmony_ci		netdev_info(dev,
55158c2ecf20Sopenharmony_ci			    "%s: SetupReg5, Bit 31 remained off\n", __func__);
55168c2ecf20Sopenharmony_ci
55178c2ecf20Sopenharmony_ci	writel(0, base + NvRegMIIMask);
55188c2ecf20Sopenharmony_ci	writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
55198c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_MASK_ALL, base + NvRegMIIStatus);
55208c2ecf20Sopenharmony_ci
55218c2ecf20Sopenharmony_ci	writel(NVREG_MISC1_FORCE | NVREG_MISC1_HD, base + NvRegMisc1);
55228c2ecf20Sopenharmony_ci	writel(readl(base + NvRegTransmitterStatus), base + NvRegTransmitterStatus);
55238c2ecf20Sopenharmony_ci	writel(NVREG_PFF_ALWAYS, base + NvRegPacketFilterFlags);
55248c2ecf20Sopenharmony_ci	writel(np->rx_buf_sz, base + NvRegOffloadConfig);
55258c2ecf20Sopenharmony_ci
55268c2ecf20Sopenharmony_ci	writel(readl(base + NvRegReceiverStatus), base + NvRegReceiverStatus);
55278c2ecf20Sopenharmony_ci
55288c2ecf20Sopenharmony_ci	get_random_bytes(&low, sizeof(low));
55298c2ecf20Sopenharmony_ci	low &= NVREG_SLOTTIME_MASK;
55308c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1) {
55318c2ecf20Sopenharmony_ci		writel(low|NVREG_SLOTTIME_DEFAULT, base + NvRegSlotTime);
55328c2ecf20Sopenharmony_ci	} else {
55338c2ecf20Sopenharmony_ci		if (!(np->driver_data & DEV_HAS_GEAR_MODE)) {
55348c2ecf20Sopenharmony_ci			/* setup legacy backoff */
55358c2ecf20Sopenharmony_ci			writel(NVREG_SLOTTIME_LEGBF_ENABLED|NVREG_SLOTTIME_10_100_FULL|low, base + NvRegSlotTime);
55368c2ecf20Sopenharmony_ci		} else {
55378c2ecf20Sopenharmony_ci			writel(NVREG_SLOTTIME_10_100_FULL, base + NvRegSlotTime);
55388c2ecf20Sopenharmony_ci			nv_gear_backoff_reseed(dev);
55398c2ecf20Sopenharmony_ci		}
55408c2ecf20Sopenharmony_ci	}
55418c2ecf20Sopenharmony_ci	writel(NVREG_TX_DEFERRAL_DEFAULT, base + NvRegTxDeferral);
55428c2ecf20Sopenharmony_ci	writel(NVREG_RX_DEFERRAL_DEFAULT, base + NvRegRxDeferral);
55438c2ecf20Sopenharmony_ci	if (poll_interval == -1) {
55448c2ecf20Sopenharmony_ci		if (optimization_mode == NV_OPTIMIZATION_MODE_THROUGHPUT)
55458c2ecf20Sopenharmony_ci			writel(NVREG_POLL_DEFAULT_THROUGHPUT, base + NvRegPollingInterval);
55468c2ecf20Sopenharmony_ci		else
55478c2ecf20Sopenharmony_ci			writel(NVREG_POLL_DEFAULT_CPU, base + NvRegPollingInterval);
55488c2ecf20Sopenharmony_ci	} else
55498c2ecf20Sopenharmony_ci		writel(poll_interval & 0xFFFF, base + NvRegPollingInterval);
55508c2ecf20Sopenharmony_ci	writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
55518c2ecf20Sopenharmony_ci	writel((np->phyaddr << NVREG_ADAPTCTL_PHYSHIFT)|NVREG_ADAPTCTL_PHYVALID|NVREG_ADAPTCTL_RUNNING,
55528c2ecf20Sopenharmony_ci			base + NvRegAdapterControl);
55538c2ecf20Sopenharmony_ci	writel(NVREG_MIISPEED_BIT8|NVREG_MIIDELAY, base + NvRegMIISpeed);
55548c2ecf20Sopenharmony_ci	writel(NVREG_MII_LINKCHANGE, base + NvRegMIIMask);
55558c2ecf20Sopenharmony_ci	if (np->wolenabled)
55568c2ecf20Sopenharmony_ci		writel(NVREG_WAKEUPFLAGS_ENABLE , base + NvRegWakeUpFlags);
55578c2ecf20Sopenharmony_ci
55588c2ecf20Sopenharmony_ci	i = readl(base + NvRegPowerState);
55598c2ecf20Sopenharmony_ci	if ((i & NVREG_POWERSTATE_POWEREDUP) == 0)
55608c2ecf20Sopenharmony_ci		writel(NVREG_POWERSTATE_POWEREDUP|i, base + NvRegPowerState);
55618c2ecf20Sopenharmony_ci
55628c2ecf20Sopenharmony_ci	pci_push(base);
55638c2ecf20Sopenharmony_ci	udelay(10);
55648c2ecf20Sopenharmony_ci	writel(readl(base + NvRegPowerState) | NVREG_POWERSTATE_VALID, base + NvRegPowerState);
55658c2ecf20Sopenharmony_ci
55668c2ecf20Sopenharmony_ci	nv_disable_hw_interrupts(dev, np->irqmask);
55678c2ecf20Sopenharmony_ci	pci_push(base);
55688c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_MASK_ALL, base + NvRegMIIStatus);
55698c2ecf20Sopenharmony_ci	writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
55708c2ecf20Sopenharmony_ci	pci_push(base);
55718c2ecf20Sopenharmony_ci
55728c2ecf20Sopenharmony_ci	if (nv_request_irq(dev, 0))
55738c2ecf20Sopenharmony_ci		goto out_drain;
55748c2ecf20Sopenharmony_ci
55758c2ecf20Sopenharmony_ci	/* ask for interrupts */
55768c2ecf20Sopenharmony_ci	nv_enable_hw_interrupts(dev, np->irqmask);
55778c2ecf20Sopenharmony_ci
55788c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
55798c2ecf20Sopenharmony_ci	writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
55808c2ecf20Sopenharmony_ci	writel(0, base + NvRegMulticastAddrB);
55818c2ecf20Sopenharmony_ci	writel(NVREG_MCASTMASKA_NONE, base + NvRegMulticastMaskA);
55828c2ecf20Sopenharmony_ci	writel(NVREG_MCASTMASKB_NONE, base + NvRegMulticastMaskB);
55838c2ecf20Sopenharmony_ci	writel(NVREG_PFF_ALWAYS|NVREG_PFF_MYADDR, base + NvRegPacketFilterFlags);
55848c2ecf20Sopenharmony_ci	/* One manual link speed update: Interrupts are enabled, future link
55858c2ecf20Sopenharmony_ci	 * speed changes cause interrupts and are handled by nv_link_irq().
55868c2ecf20Sopenharmony_ci	 */
55878c2ecf20Sopenharmony_ci	readl(base + NvRegMIIStatus);
55888c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_MASK_ALL, base + NvRegMIIStatus);
55898c2ecf20Sopenharmony_ci
55908c2ecf20Sopenharmony_ci	/* set linkspeed to invalid value, thus force nv_update_linkspeed
55918c2ecf20Sopenharmony_ci	 * to init hw */
55928c2ecf20Sopenharmony_ci	np->linkspeed = 0;
55938c2ecf20Sopenharmony_ci	ret = nv_update_linkspeed(dev);
55948c2ecf20Sopenharmony_ci	nv_start_rxtx(dev);
55958c2ecf20Sopenharmony_ci	netif_start_queue(dev);
55968c2ecf20Sopenharmony_ci	nv_napi_enable(dev);
55978c2ecf20Sopenharmony_ci
55988c2ecf20Sopenharmony_ci	if (ret) {
55998c2ecf20Sopenharmony_ci		netif_carrier_on(dev);
56008c2ecf20Sopenharmony_ci	} else {
56018c2ecf20Sopenharmony_ci		netdev_info(dev, "no link during initialization\n");
56028c2ecf20Sopenharmony_ci		netif_carrier_off(dev);
56038c2ecf20Sopenharmony_ci	}
56048c2ecf20Sopenharmony_ci	if (oom)
56058c2ecf20Sopenharmony_ci		mod_timer(&np->oom_kick, jiffies + OOM_REFILL);
56068c2ecf20Sopenharmony_ci
56078c2ecf20Sopenharmony_ci	/* start statistics timer */
56088c2ecf20Sopenharmony_ci	if (np->driver_data & (DEV_HAS_STATISTICS_V1|DEV_HAS_STATISTICS_V2|DEV_HAS_STATISTICS_V3))
56098c2ecf20Sopenharmony_ci		mod_timer(&np->stats_poll,
56108c2ecf20Sopenharmony_ci			round_jiffies(jiffies + STATS_INTERVAL));
56118c2ecf20Sopenharmony_ci
56128c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
56138c2ecf20Sopenharmony_ci
56148c2ecf20Sopenharmony_ci	/* If the loopback feature was set while the device was down, make sure
56158c2ecf20Sopenharmony_ci	 * that it's set correctly now.
56168c2ecf20Sopenharmony_ci	 */
56178c2ecf20Sopenharmony_ci	if (dev->features & NETIF_F_LOOPBACK)
56188c2ecf20Sopenharmony_ci		nv_set_loopback(dev, dev->features);
56198c2ecf20Sopenharmony_ci
56208c2ecf20Sopenharmony_ci	return 0;
56218c2ecf20Sopenharmony_ciout_drain:
56228c2ecf20Sopenharmony_ci	nv_drain_rxtx(dev);
56238c2ecf20Sopenharmony_ci	return ret;
56248c2ecf20Sopenharmony_ci}
56258c2ecf20Sopenharmony_ci
56268c2ecf20Sopenharmony_cistatic int nv_close(struct net_device *dev)
56278c2ecf20Sopenharmony_ci{
56288c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
56298c2ecf20Sopenharmony_ci	u8 __iomem *base;
56308c2ecf20Sopenharmony_ci
56318c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
56328c2ecf20Sopenharmony_ci	np->in_shutdown = 1;
56338c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
56348c2ecf20Sopenharmony_ci	nv_napi_disable(dev);
56358c2ecf20Sopenharmony_ci	synchronize_irq(np->pci_dev->irq);
56368c2ecf20Sopenharmony_ci
56378c2ecf20Sopenharmony_ci	del_timer_sync(&np->oom_kick);
56388c2ecf20Sopenharmony_ci	del_timer_sync(&np->nic_poll);
56398c2ecf20Sopenharmony_ci	del_timer_sync(&np->stats_poll);
56408c2ecf20Sopenharmony_ci
56418c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
56428c2ecf20Sopenharmony_ci	spin_lock_irq(&np->lock);
56438c2ecf20Sopenharmony_ci	nv_update_pause(dev, 0); /* otherwise stop_tx bricks NIC */
56448c2ecf20Sopenharmony_ci	nv_stop_rxtx(dev);
56458c2ecf20Sopenharmony_ci	nv_txrx_reset(dev);
56468c2ecf20Sopenharmony_ci
56478c2ecf20Sopenharmony_ci	/* disable interrupts on the nic or we will lock up */
56488c2ecf20Sopenharmony_ci	base = get_hwbase(dev);
56498c2ecf20Sopenharmony_ci	nv_disable_hw_interrupts(dev, np->irqmask);
56508c2ecf20Sopenharmony_ci	pci_push(base);
56518c2ecf20Sopenharmony_ci
56528c2ecf20Sopenharmony_ci	spin_unlock_irq(&np->lock);
56538c2ecf20Sopenharmony_ci
56548c2ecf20Sopenharmony_ci	nv_free_irq(dev);
56558c2ecf20Sopenharmony_ci
56568c2ecf20Sopenharmony_ci	nv_drain_rxtx(dev);
56578c2ecf20Sopenharmony_ci
56588c2ecf20Sopenharmony_ci	if (np->wolenabled || !phy_power_down) {
56598c2ecf20Sopenharmony_ci		nv_txrx_gate(dev, false);
56608c2ecf20Sopenharmony_ci		writel(NVREG_PFF_ALWAYS|NVREG_PFF_MYADDR, base + NvRegPacketFilterFlags);
56618c2ecf20Sopenharmony_ci		nv_start_rx(dev);
56628c2ecf20Sopenharmony_ci	} else {
56638c2ecf20Sopenharmony_ci		/* power down phy */
56648c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_BMCR,
56658c2ecf20Sopenharmony_ci		       mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ)|BMCR_PDOWN);
56668c2ecf20Sopenharmony_ci		nv_txrx_gate(dev, true);
56678c2ecf20Sopenharmony_ci	}
56688c2ecf20Sopenharmony_ci
56698c2ecf20Sopenharmony_ci	/* FIXME: power down nic */
56708c2ecf20Sopenharmony_ci
56718c2ecf20Sopenharmony_ci	return 0;
56728c2ecf20Sopenharmony_ci}
56738c2ecf20Sopenharmony_ci
56748c2ecf20Sopenharmony_cistatic const struct net_device_ops nv_netdev_ops = {
56758c2ecf20Sopenharmony_ci	.ndo_open		= nv_open,
56768c2ecf20Sopenharmony_ci	.ndo_stop		= nv_close,
56778c2ecf20Sopenharmony_ci	.ndo_get_stats64	= nv_get_stats64,
56788c2ecf20Sopenharmony_ci	.ndo_start_xmit		= nv_start_xmit,
56798c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= nv_tx_timeout,
56808c2ecf20Sopenharmony_ci	.ndo_change_mtu		= nv_change_mtu,
56818c2ecf20Sopenharmony_ci	.ndo_fix_features	= nv_fix_features,
56828c2ecf20Sopenharmony_ci	.ndo_set_features	= nv_set_features,
56838c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
56848c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= nv_set_mac_address,
56858c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= nv_set_multicast,
56868c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_POLL_CONTROLLER
56878c2ecf20Sopenharmony_ci	.ndo_poll_controller	= nv_poll_controller,
56888c2ecf20Sopenharmony_ci#endif
56898c2ecf20Sopenharmony_ci};
56908c2ecf20Sopenharmony_ci
56918c2ecf20Sopenharmony_cistatic const struct net_device_ops nv_netdev_ops_optimized = {
56928c2ecf20Sopenharmony_ci	.ndo_open		= nv_open,
56938c2ecf20Sopenharmony_ci	.ndo_stop		= nv_close,
56948c2ecf20Sopenharmony_ci	.ndo_get_stats64	= nv_get_stats64,
56958c2ecf20Sopenharmony_ci	.ndo_start_xmit		= nv_start_xmit_optimized,
56968c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= nv_tx_timeout,
56978c2ecf20Sopenharmony_ci	.ndo_change_mtu		= nv_change_mtu,
56988c2ecf20Sopenharmony_ci	.ndo_fix_features	= nv_fix_features,
56998c2ecf20Sopenharmony_ci	.ndo_set_features	= nv_set_features,
57008c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
57018c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= nv_set_mac_address,
57028c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= nv_set_multicast,
57038c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_POLL_CONTROLLER
57048c2ecf20Sopenharmony_ci	.ndo_poll_controller	= nv_poll_controller,
57058c2ecf20Sopenharmony_ci#endif
57068c2ecf20Sopenharmony_ci};
57078c2ecf20Sopenharmony_ci
57088c2ecf20Sopenharmony_cistatic int nv_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
57098c2ecf20Sopenharmony_ci{
57108c2ecf20Sopenharmony_ci	struct net_device *dev;
57118c2ecf20Sopenharmony_ci	struct fe_priv *np;
57128c2ecf20Sopenharmony_ci	unsigned long addr;
57138c2ecf20Sopenharmony_ci	u8 __iomem *base;
57148c2ecf20Sopenharmony_ci	int err, i;
57158c2ecf20Sopenharmony_ci	u32 powerstate, txreg;
57168c2ecf20Sopenharmony_ci	u32 phystate_orig = 0, phystate;
57178c2ecf20Sopenharmony_ci	int phyinitialized = 0;
57188c2ecf20Sopenharmony_ci	static int printed_version;
57198c2ecf20Sopenharmony_ci
57208c2ecf20Sopenharmony_ci	if (!printed_version++)
57218c2ecf20Sopenharmony_ci		pr_info("Reverse Engineered nForce ethernet driver. Version %s.\n",
57228c2ecf20Sopenharmony_ci			FORCEDETH_VERSION);
57238c2ecf20Sopenharmony_ci
57248c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(struct fe_priv));
57258c2ecf20Sopenharmony_ci	err = -ENOMEM;
57268c2ecf20Sopenharmony_ci	if (!dev)
57278c2ecf20Sopenharmony_ci		goto out;
57288c2ecf20Sopenharmony_ci
57298c2ecf20Sopenharmony_ci	np = netdev_priv(dev);
57308c2ecf20Sopenharmony_ci	np->dev = dev;
57318c2ecf20Sopenharmony_ci	np->pci_dev = pci_dev;
57328c2ecf20Sopenharmony_ci	spin_lock_init(&np->lock);
57338c2ecf20Sopenharmony_ci	spin_lock_init(&np->hwstats_lock);
57348c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, &pci_dev->dev);
57358c2ecf20Sopenharmony_ci	u64_stats_init(&np->swstats_rx_syncp);
57368c2ecf20Sopenharmony_ci	u64_stats_init(&np->swstats_tx_syncp);
57378c2ecf20Sopenharmony_ci	np->txrx_stats = alloc_percpu(struct nv_txrx_stats);
57388c2ecf20Sopenharmony_ci	if (!np->txrx_stats) {
57398c2ecf20Sopenharmony_ci		pr_err("np->txrx_stats, alloc memory error.\n");
57408c2ecf20Sopenharmony_ci		err = -ENOMEM;
57418c2ecf20Sopenharmony_ci		goto out_alloc_percpu;
57428c2ecf20Sopenharmony_ci	}
57438c2ecf20Sopenharmony_ci
57448c2ecf20Sopenharmony_ci	timer_setup(&np->oom_kick, nv_do_rx_refill, 0);
57458c2ecf20Sopenharmony_ci	timer_setup(&np->nic_poll, nv_do_nic_poll, 0);
57468c2ecf20Sopenharmony_ci	timer_setup(&np->stats_poll, nv_do_stats_poll, TIMER_DEFERRABLE);
57478c2ecf20Sopenharmony_ci
57488c2ecf20Sopenharmony_ci	err = pci_enable_device(pci_dev);
57498c2ecf20Sopenharmony_ci	if (err)
57508c2ecf20Sopenharmony_ci		goto out_free;
57518c2ecf20Sopenharmony_ci
57528c2ecf20Sopenharmony_ci	pci_set_master(pci_dev);
57538c2ecf20Sopenharmony_ci
57548c2ecf20Sopenharmony_ci	err = pci_request_regions(pci_dev, DRV_NAME);
57558c2ecf20Sopenharmony_ci	if (err < 0)
57568c2ecf20Sopenharmony_ci		goto out_disable;
57578c2ecf20Sopenharmony_ci
57588c2ecf20Sopenharmony_ci	if (id->driver_data & (DEV_HAS_VLAN|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V2|DEV_HAS_STATISTICS_V3))
57598c2ecf20Sopenharmony_ci		np->register_size = NV_PCI_REGSZ_VER3;
57608c2ecf20Sopenharmony_ci	else if (id->driver_data & DEV_HAS_STATISTICS_V1)
57618c2ecf20Sopenharmony_ci		np->register_size = NV_PCI_REGSZ_VER2;
57628c2ecf20Sopenharmony_ci	else
57638c2ecf20Sopenharmony_ci		np->register_size = NV_PCI_REGSZ_VER1;
57648c2ecf20Sopenharmony_ci
57658c2ecf20Sopenharmony_ci	err = -EINVAL;
57668c2ecf20Sopenharmony_ci	addr = 0;
57678c2ecf20Sopenharmony_ci	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
57688c2ecf20Sopenharmony_ci		if (pci_resource_flags(pci_dev, i) & IORESOURCE_MEM &&
57698c2ecf20Sopenharmony_ci				pci_resource_len(pci_dev, i) >= np->register_size) {
57708c2ecf20Sopenharmony_ci			addr = pci_resource_start(pci_dev, i);
57718c2ecf20Sopenharmony_ci			break;
57728c2ecf20Sopenharmony_ci		}
57738c2ecf20Sopenharmony_ci	}
57748c2ecf20Sopenharmony_ci	if (i == DEVICE_COUNT_RESOURCE) {
57758c2ecf20Sopenharmony_ci		dev_info(&pci_dev->dev, "Couldn't find register window\n");
57768c2ecf20Sopenharmony_ci		goto out_relreg;
57778c2ecf20Sopenharmony_ci	}
57788c2ecf20Sopenharmony_ci
57798c2ecf20Sopenharmony_ci	/* copy of driver data */
57808c2ecf20Sopenharmony_ci	np->driver_data = id->driver_data;
57818c2ecf20Sopenharmony_ci	/* copy of device id */
57828c2ecf20Sopenharmony_ci	np->device_id = id->device;
57838c2ecf20Sopenharmony_ci
57848c2ecf20Sopenharmony_ci	/* handle different descriptor versions */
57858c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_HIGH_DMA) {
57868c2ecf20Sopenharmony_ci		/* packet format 3: supports 40-bit addressing */
57878c2ecf20Sopenharmony_ci		np->desc_ver = DESC_VER_3;
57888c2ecf20Sopenharmony_ci		np->txrxctl_bits = NVREG_TXRXCTL_DESC_3;
57898c2ecf20Sopenharmony_ci		if (dma_64bit) {
57908c2ecf20Sopenharmony_ci			if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(39)))
57918c2ecf20Sopenharmony_ci				dev_info(&pci_dev->dev,
57928c2ecf20Sopenharmony_ci					 "64-bit DMA failed, using 32-bit addressing\n");
57938c2ecf20Sopenharmony_ci			else
57948c2ecf20Sopenharmony_ci				dev->features |= NETIF_F_HIGHDMA;
57958c2ecf20Sopenharmony_ci			if (pci_set_consistent_dma_mask(pci_dev, DMA_BIT_MASK(39))) {
57968c2ecf20Sopenharmony_ci				dev_info(&pci_dev->dev,
57978c2ecf20Sopenharmony_ci					 "64-bit DMA (consistent) failed, using 32-bit ring buffers\n");
57988c2ecf20Sopenharmony_ci			}
57998c2ecf20Sopenharmony_ci		}
58008c2ecf20Sopenharmony_ci	} else if (id->driver_data & DEV_HAS_LARGEDESC) {
58018c2ecf20Sopenharmony_ci		/* packet format 2: supports jumbo frames */
58028c2ecf20Sopenharmony_ci		np->desc_ver = DESC_VER_2;
58038c2ecf20Sopenharmony_ci		np->txrxctl_bits = NVREG_TXRXCTL_DESC_2;
58048c2ecf20Sopenharmony_ci	} else {
58058c2ecf20Sopenharmony_ci		/* original packet format */
58068c2ecf20Sopenharmony_ci		np->desc_ver = DESC_VER_1;
58078c2ecf20Sopenharmony_ci		np->txrxctl_bits = NVREG_TXRXCTL_DESC_1;
58088c2ecf20Sopenharmony_ci	}
58098c2ecf20Sopenharmony_ci
58108c2ecf20Sopenharmony_ci	np->pkt_limit = NV_PKTLIMIT_1;
58118c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_LARGEDESC)
58128c2ecf20Sopenharmony_ci		np->pkt_limit = NV_PKTLIMIT_2;
58138c2ecf20Sopenharmony_ci
58148c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_CHECKSUM) {
58158c2ecf20Sopenharmony_ci		np->txrxctl_bits |= NVREG_TXRXCTL_RXCHECK;
58168c2ecf20Sopenharmony_ci		dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_SG |
58178c2ecf20Sopenharmony_ci			NETIF_F_TSO | NETIF_F_RXCSUM;
58188c2ecf20Sopenharmony_ci	}
58198c2ecf20Sopenharmony_ci
58208c2ecf20Sopenharmony_ci	np->vlanctl_bits = 0;
58218c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_VLAN) {
58228c2ecf20Sopenharmony_ci		np->vlanctl_bits = NVREG_VLANCONTROL_ENABLE;
58238c2ecf20Sopenharmony_ci		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX |
58248c2ecf20Sopenharmony_ci				    NETIF_F_HW_VLAN_CTAG_TX;
58258c2ecf20Sopenharmony_ci	}
58268c2ecf20Sopenharmony_ci
58278c2ecf20Sopenharmony_ci	dev->features |= dev->hw_features;
58288c2ecf20Sopenharmony_ci
58298c2ecf20Sopenharmony_ci	/* Add loopback capability to the device. */
58308c2ecf20Sopenharmony_ci	dev->hw_features |= NETIF_F_LOOPBACK;
58318c2ecf20Sopenharmony_ci
58328c2ecf20Sopenharmony_ci	/* MTU range: 64 - 1500 or 9100 */
58338c2ecf20Sopenharmony_ci	dev->min_mtu = ETH_ZLEN + ETH_FCS_LEN;
58348c2ecf20Sopenharmony_ci	dev->max_mtu = np->pkt_limit;
58358c2ecf20Sopenharmony_ci
58368c2ecf20Sopenharmony_ci	np->pause_flags = NV_PAUSEFRAME_RX_CAPABLE | NV_PAUSEFRAME_RX_REQ | NV_PAUSEFRAME_AUTONEG;
58378c2ecf20Sopenharmony_ci	if ((id->driver_data & DEV_HAS_PAUSEFRAME_TX_V1) ||
58388c2ecf20Sopenharmony_ci	    (id->driver_data & DEV_HAS_PAUSEFRAME_TX_V2) ||
58398c2ecf20Sopenharmony_ci	    (id->driver_data & DEV_HAS_PAUSEFRAME_TX_V3)) {
58408c2ecf20Sopenharmony_ci		np->pause_flags |= NV_PAUSEFRAME_TX_CAPABLE | NV_PAUSEFRAME_TX_REQ;
58418c2ecf20Sopenharmony_ci	}
58428c2ecf20Sopenharmony_ci
58438c2ecf20Sopenharmony_ci	err = -ENOMEM;
58448c2ecf20Sopenharmony_ci	np->base = ioremap(addr, np->register_size);
58458c2ecf20Sopenharmony_ci	if (!np->base)
58468c2ecf20Sopenharmony_ci		goto out_relreg;
58478c2ecf20Sopenharmony_ci
58488c2ecf20Sopenharmony_ci	np->rx_ring_size = RX_RING_DEFAULT;
58498c2ecf20Sopenharmony_ci	np->tx_ring_size = TX_RING_DEFAULT;
58508c2ecf20Sopenharmony_ci
58518c2ecf20Sopenharmony_ci	if (!nv_optimized(np)) {
58528c2ecf20Sopenharmony_ci		np->rx_ring.orig = dma_alloc_coherent(&pci_dev->dev,
58538c2ecf20Sopenharmony_ci						      sizeof(struct ring_desc) *
58548c2ecf20Sopenharmony_ci						      (np->rx_ring_size +
58558c2ecf20Sopenharmony_ci						      np->tx_ring_size),
58568c2ecf20Sopenharmony_ci						      &np->ring_addr,
58578c2ecf20Sopenharmony_ci						      GFP_KERNEL);
58588c2ecf20Sopenharmony_ci		if (!np->rx_ring.orig)
58598c2ecf20Sopenharmony_ci			goto out_unmap;
58608c2ecf20Sopenharmony_ci		np->tx_ring.orig = &np->rx_ring.orig[np->rx_ring_size];
58618c2ecf20Sopenharmony_ci	} else {
58628c2ecf20Sopenharmony_ci		np->rx_ring.ex = dma_alloc_coherent(&pci_dev->dev,
58638c2ecf20Sopenharmony_ci						    sizeof(struct ring_desc_ex) *
58648c2ecf20Sopenharmony_ci						    (np->rx_ring_size +
58658c2ecf20Sopenharmony_ci						    np->tx_ring_size),
58668c2ecf20Sopenharmony_ci						    &np->ring_addr, GFP_KERNEL);
58678c2ecf20Sopenharmony_ci		if (!np->rx_ring.ex)
58688c2ecf20Sopenharmony_ci			goto out_unmap;
58698c2ecf20Sopenharmony_ci		np->tx_ring.ex = &np->rx_ring.ex[np->rx_ring_size];
58708c2ecf20Sopenharmony_ci	}
58718c2ecf20Sopenharmony_ci	np->rx_skb = kcalloc(np->rx_ring_size, sizeof(struct nv_skb_map), GFP_KERNEL);
58728c2ecf20Sopenharmony_ci	np->tx_skb = kcalloc(np->tx_ring_size, sizeof(struct nv_skb_map), GFP_KERNEL);
58738c2ecf20Sopenharmony_ci	if (!np->rx_skb || !np->tx_skb)
58748c2ecf20Sopenharmony_ci		goto out_freering;
58758c2ecf20Sopenharmony_ci
58768c2ecf20Sopenharmony_ci	if (!nv_optimized(np))
58778c2ecf20Sopenharmony_ci		dev->netdev_ops = &nv_netdev_ops;
58788c2ecf20Sopenharmony_ci	else
58798c2ecf20Sopenharmony_ci		dev->netdev_ops = &nv_netdev_ops_optimized;
58808c2ecf20Sopenharmony_ci
58818c2ecf20Sopenharmony_ci	netif_napi_add(dev, &np->napi, nv_napi_poll, RX_WORK_PER_LOOP);
58828c2ecf20Sopenharmony_ci	dev->ethtool_ops = &ops;
58838c2ecf20Sopenharmony_ci	dev->watchdog_timeo = NV_WATCHDOG_TIMEO;
58848c2ecf20Sopenharmony_ci
58858c2ecf20Sopenharmony_ci	pci_set_drvdata(pci_dev, dev);
58868c2ecf20Sopenharmony_ci
58878c2ecf20Sopenharmony_ci	/* read the mac address */
58888c2ecf20Sopenharmony_ci	base = get_hwbase(dev);
58898c2ecf20Sopenharmony_ci	np->orig_mac[0] = readl(base + NvRegMacAddrA);
58908c2ecf20Sopenharmony_ci	np->orig_mac[1] = readl(base + NvRegMacAddrB);
58918c2ecf20Sopenharmony_ci
58928c2ecf20Sopenharmony_ci	/* check the workaround bit for correct mac address order */
58938c2ecf20Sopenharmony_ci	txreg = readl(base + NvRegTransmitPoll);
58948c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_CORRECT_MACADDR) {
58958c2ecf20Sopenharmony_ci		/* mac address is already in correct order */
58968c2ecf20Sopenharmony_ci		dev->dev_addr[0] = (np->orig_mac[0] >>  0) & 0xff;
58978c2ecf20Sopenharmony_ci		dev->dev_addr[1] = (np->orig_mac[0] >>  8) & 0xff;
58988c2ecf20Sopenharmony_ci		dev->dev_addr[2] = (np->orig_mac[0] >> 16) & 0xff;
58998c2ecf20Sopenharmony_ci		dev->dev_addr[3] = (np->orig_mac[0] >> 24) & 0xff;
59008c2ecf20Sopenharmony_ci		dev->dev_addr[4] = (np->orig_mac[1] >>  0) & 0xff;
59018c2ecf20Sopenharmony_ci		dev->dev_addr[5] = (np->orig_mac[1] >>  8) & 0xff;
59028c2ecf20Sopenharmony_ci	} else if (txreg & NVREG_TRANSMITPOLL_MAC_ADDR_REV) {
59038c2ecf20Sopenharmony_ci		/* mac address is already in correct order */
59048c2ecf20Sopenharmony_ci		dev->dev_addr[0] = (np->orig_mac[0] >>  0) & 0xff;
59058c2ecf20Sopenharmony_ci		dev->dev_addr[1] = (np->orig_mac[0] >>  8) & 0xff;
59068c2ecf20Sopenharmony_ci		dev->dev_addr[2] = (np->orig_mac[0] >> 16) & 0xff;
59078c2ecf20Sopenharmony_ci		dev->dev_addr[3] = (np->orig_mac[0] >> 24) & 0xff;
59088c2ecf20Sopenharmony_ci		dev->dev_addr[4] = (np->orig_mac[1] >>  0) & 0xff;
59098c2ecf20Sopenharmony_ci		dev->dev_addr[5] = (np->orig_mac[1] >>  8) & 0xff;
59108c2ecf20Sopenharmony_ci		/*
59118c2ecf20Sopenharmony_ci		 * Set orig mac address back to the reversed version.
59128c2ecf20Sopenharmony_ci		 * This flag will be cleared during low power transition.
59138c2ecf20Sopenharmony_ci		 * Therefore, we should always put back the reversed address.
59148c2ecf20Sopenharmony_ci		 */
59158c2ecf20Sopenharmony_ci		np->orig_mac[0] = (dev->dev_addr[5] << 0) + (dev->dev_addr[4] << 8) +
59168c2ecf20Sopenharmony_ci			(dev->dev_addr[3] << 16) + (dev->dev_addr[2] << 24);
59178c2ecf20Sopenharmony_ci		np->orig_mac[1] = (dev->dev_addr[1] << 0) + (dev->dev_addr[0] << 8);
59188c2ecf20Sopenharmony_ci	} else {
59198c2ecf20Sopenharmony_ci		/* need to reverse mac address to correct order */
59208c2ecf20Sopenharmony_ci		dev->dev_addr[0] = (np->orig_mac[1] >>  8) & 0xff;
59218c2ecf20Sopenharmony_ci		dev->dev_addr[1] = (np->orig_mac[1] >>  0) & 0xff;
59228c2ecf20Sopenharmony_ci		dev->dev_addr[2] = (np->orig_mac[0] >> 24) & 0xff;
59238c2ecf20Sopenharmony_ci		dev->dev_addr[3] = (np->orig_mac[0] >> 16) & 0xff;
59248c2ecf20Sopenharmony_ci		dev->dev_addr[4] = (np->orig_mac[0] >>  8) & 0xff;
59258c2ecf20Sopenharmony_ci		dev->dev_addr[5] = (np->orig_mac[0] >>  0) & 0xff;
59268c2ecf20Sopenharmony_ci		writel(txreg|NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
59278c2ecf20Sopenharmony_ci		dev_dbg(&pci_dev->dev,
59288c2ecf20Sopenharmony_ci			"%s: set workaround bit for reversed mac addr\n",
59298c2ecf20Sopenharmony_ci			__func__);
59308c2ecf20Sopenharmony_ci	}
59318c2ecf20Sopenharmony_ci
59328c2ecf20Sopenharmony_ci	if (!is_valid_ether_addr(dev->dev_addr)) {
59338c2ecf20Sopenharmony_ci		/*
59348c2ecf20Sopenharmony_ci		 * Bad mac address. At least one bios sets the mac address
59358c2ecf20Sopenharmony_ci		 * to 01:23:45:67:89:ab
59368c2ecf20Sopenharmony_ci		 */
59378c2ecf20Sopenharmony_ci		dev_err(&pci_dev->dev,
59388c2ecf20Sopenharmony_ci			"Invalid MAC address detected: %pM - Please complain to your hardware vendor.\n",
59398c2ecf20Sopenharmony_ci			dev->dev_addr);
59408c2ecf20Sopenharmony_ci		eth_hw_addr_random(dev);
59418c2ecf20Sopenharmony_ci		dev_err(&pci_dev->dev,
59428c2ecf20Sopenharmony_ci			"Using random MAC address: %pM\n", dev->dev_addr);
59438c2ecf20Sopenharmony_ci	}
59448c2ecf20Sopenharmony_ci
59458c2ecf20Sopenharmony_ci	/* set mac address */
59468c2ecf20Sopenharmony_ci	nv_copy_mac_to_hw(dev);
59478c2ecf20Sopenharmony_ci
59488c2ecf20Sopenharmony_ci	/* disable WOL */
59498c2ecf20Sopenharmony_ci	writel(0, base + NvRegWakeUpFlags);
59508c2ecf20Sopenharmony_ci	np->wolenabled = 0;
59518c2ecf20Sopenharmony_ci	device_set_wakeup_enable(&pci_dev->dev, false);
59528c2ecf20Sopenharmony_ci
59538c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_POWER_CNTRL) {
59548c2ecf20Sopenharmony_ci
59558c2ecf20Sopenharmony_ci		/* take phy and nic out of low power mode */
59568c2ecf20Sopenharmony_ci		powerstate = readl(base + NvRegPowerState2);
59578c2ecf20Sopenharmony_ci		powerstate &= ~NVREG_POWERSTATE2_POWERUP_MASK;
59588c2ecf20Sopenharmony_ci		if ((id->driver_data & DEV_NEED_LOW_POWER_FIX) &&
59598c2ecf20Sopenharmony_ci		    pci_dev->revision >= 0xA3)
59608c2ecf20Sopenharmony_ci			powerstate |= NVREG_POWERSTATE2_POWERUP_REV_A3;
59618c2ecf20Sopenharmony_ci		writel(powerstate, base + NvRegPowerState2);
59628c2ecf20Sopenharmony_ci	}
59638c2ecf20Sopenharmony_ci
59648c2ecf20Sopenharmony_ci	if (np->desc_ver == DESC_VER_1)
59658c2ecf20Sopenharmony_ci		np->tx_flags = NV_TX_VALID;
59668c2ecf20Sopenharmony_ci	else
59678c2ecf20Sopenharmony_ci		np->tx_flags = NV_TX2_VALID;
59688c2ecf20Sopenharmony_ci
59698c2ecf20Sopenharmony_ci	np->msi_flags = 0;
59708c2ecf20Sopenharmony_ci	if ((id->driver_data & DEV_HAS_MSI) && msi)
59718c2ecf20Sopenharmony_ci		np->msi_flags |= NV_MSI_CAPABLE;
59728c2ecf20Sopenharmony_ci
59738c2ecf20Sopenharmony_ci	if ((id->driver_data & DEV_HAS_MSI_X) && msix) {
59748c2ecf20Sopenharmony_ci		/* msix has had reported issues when modifying irqmask
59758c2ecf20Sopenharmony_ci		   as in the case of napi, therefore, disable for now
59768c2ecf20Sopenharmony_ci		*/
59778c2ecf20Sopenharmony_ci#if 0
59788c2ecf20Sopenharmony_ci		np->msi_flags |= NV_MSI_X_CAPABLE;
59798c2ecf20Sopenharmony_ci#endif
59808c2ecf20Sopenharmony_ci	}
59818c2ecf20Sopenharmony_ci
59828c2ecf20Sopenharmony_ci	if (optimization_mode == NV_OPTIMIZATION_MODE_CPU) {
59838c2ecf20Sopenharmony_ci		np->irqmask = NVREG_IRQMASK_CPU;
59848c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_X_CAPABLE) /* set number of vectors */
59858c2ecf20Sopenharmony_ci			np->msi_flags |= 0x0001;
59868c2ecf20Sopenharmony_ci	} else if (optimization_mode == NV_OPTIMIZATION_MODE_DYNAMIC &&
59878c2ecf20Sopenharmony_ci		   !(id->driver_data & DEV_NEED_TIMERIRQ)) {
59888c2ecf20Sopenharmony_ci		/* start off in throughput mode */
59898c2ecf20Sopenharmony_ci		np->irqmask = NVREG_IRQMASK_THROUGHPUT;
59908c2ecf20Sopenharmony_ci		/* remove support for msix mode */
59918c2ecf20Sopenharmony_ci		np->msi_flags &= ~NV_MSI_X_CAPABLE;
59928c2ecf20Sopenharmony_ci	} else {
59938c2ecf20Sopenharmony_ci		optimization_mode = NV_OPTIMIZATION_MODE_THROUGHPUT;
59948c2ecf20Sopenharmony_ci		np->irqmask = NVREG_IRQMASK_THROUGHPUT;
59958c2ecf20Sopenharmony_ci		if (np->msi_flags & NV_MSI_X_CAPABLE) /* set number of vectors */
59968c2ecf20Sopenharmony_ci			np->msi_flags |= 0x0003;
59978c2ecf20Sopenharmony_ci	}
59988c2ecf20Sopenharmony_ci
59998c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_NEED_TIMERIRQ)
60008c2ecf20Sopenharmony_ci		np->irqmask |= NVREG_IRQ_TIMER;
60018c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_NEED_LINKTIMER) {
60028c2ecf20Sopenharmony_ci		np->need_linktimer = 1;
60038c2ecf20Sopenharmony_ci		np->link_timeout = jiffies + LINK_TIMEOUT;
60048c2ecf20Sopenharmony_ci	} else {
60058c2ecf20Sopenharmony_ci		np->need_linktimer = 0;
60068c2ecf20Sopenharmony_ci	}
60078c2ecf20Sopenharmony_ci
60088c2ecf20Sopenharmony_ci	/* Limit the number of tx's outstanding for hw bug */
60098c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_NEED_TX_LIMIT) {
60108c2ecf20Sopenharmony_ci		np->tx_limit = 1;
60118c2ecf20Sopenharmony_ci		if (((id->driver_data & DEV_NEED_TX_LIMIT2) == DEV_NEED_TX_LIMIT2) &&
60128c2ecf20Sopenharmony_ci		    pci_dev->revision >= 0xA2)
60138c2ecf20Sopenharmony_ci			np->tx_limit = 0;
60148c2ecf20Sopenharmony_ci	}
60158c2ecf20Sopenharmony_ci
60168c2ecf20Sopenharmony_ci	/* clear phy state and temporarily halt phy interrupts */
60178c2ecf20Sopenharmony_ci	writel(0, base + NvRegMIIMask);
60188c2ecf20Sopenharmony_ci	phystate = readl(base + NvRegAdapterControl);
60198c2ecf20Sopenharmony_ci	if (phystate & NVREG_ADAPTCTL_RUNNING) {
60208c2ecf20Sopenharmony_ci		phystate_orig = 1;
60218c2ecf20Sopenharmony_ci		phystate &= ~NVREG_ADAPTCTL_RUNNING;
60228c2ecf20Sopenharmony_ci		writel(phystate, base + NvRegAdapterControl);
60238c2ecf20Sopenharmony_ci	}
60248c2ecf20Sopenharmony_ci	writel(NVREG_MIISTAT_MASK_ALL, base + NvRegMIIStatus);
60258c2ecf20Sopenharmony_ci
60268c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_MGMT_UNIT) {
60278c2ecf20Sopenharmony_ci		/* management unit running on the mac? */
60288c2ecf20Sopenharmony_ci		if ((readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_MGMT_ST) &&
60298c2ecf20Sopenharmony_ci		    (readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_SYNC_PHY_INIT) &&
60308c2ecf20Sopenharmony_ci		    nv_mgmt_acquire_sema(dev) &&
60318c2ecf20Sopenharmony_ci		    nv_mgmt_get_version(dev)) {
60328c2ecf20Sopenharmony_ci			np->mac_in_use = 1;
60338c2ecf20Sopenharmony_ci			if (np->mgmt_version > 0)
60348c2ecf20Sopenharmony_ci				np->mac_in_use = readl(base + NvRegMgmtUnitControl) & NVREG_MGMTUNITCONTROL_INUSE;
60358c2ecf20Sopenharmony_ci			/* management unit setup the phy already? */
60368c2ecf20Sopenharmony_ci			if (np->mac_in_use &&
60378c2ecf20Sopenharmony_ci			    ((readl(base + NvRegTransmitterControl) & NVREG_XMITCTL_SYNC_MASK) ==
60388c2ecf20Sopenharmony_ci			     NVREG_XMITCTL_SYNC_PHY_INIT)) {
60398c2ecf20Sopenharmony_ci				/* phy is inited by mgmt unit */
60408c2ecf20Sopenharmony_ci				phyinitialized = 1;
60418c2ecf20Sopenharmony_ci			} else {
60428c2ecf20Sopenharmony_ci				/* we need to init the phy */
60438c2ecf20Sopenharmony_ci			}
60448c2ecf20Sopenharmony_ci		}
60458c2ecf20Sopenharmony_ci	}
60468c2ecf20Sopenharmony_ci
60478c2ecf20Sopenharmony_ci	/* find a suitable phy */
60488c2ecf20Sopenharmony_ci	for (i = 1; i <= 32; i++) {
60498c2ecf20Sopenharmony_ci		int id1, id2;
60508c2ecf20Sopenharmony_ci		int phyaddr = i & 0x1F;
60518c2ecf20Sopenharmony_ci
60528c2ecf20Sopenharmony_ci		spin_lock_irq(&np->lock);
60538c2ecf20Sopenharmony_ci		id1 = mii_rw(dev, phyaddr, MII_PHYSID1, MII_READ);
60548c2ecf20Sopenharmony_ci		spin_unlock_irq(&np->lock);
60558c2ecf20Sopenharmony_ci		if (id1 < 0 || id1 == 0xffff)
60568c2ecf20Sopenharmony_ci			continue;
60578c2ecf20Sopenharmony_ci		spin_lock_irq(&np->lock);
60588c2ecf20Sopenharmony_ci		id2 = mii_rw(dev, phyaddr, MII_PHYSID2, MII_READ);
60598c2ecf20Sopenharmony_ci		spin_unlock_irq(&np->lock);
60608c2ecf20Sopenharmony_ci		if (id2 < 0 || id2 == 0xffff)
60618c2ecf20Sopenharmony_ci			continue;
60628c2ecf20Sopenharmony_ci
60638c2ecf20Sopenharmony_ci		np->phy_model = id2 & PHYID2_MODEL_MASK;
60648c2ecf20Sopenharmony_ci		id1 = (id1 & PHYID1_OUI_MASK) << PHYID1_OUI_SHFT;
60658c2ecf20Sopenharmony_ci		id2 = (id2 & PHYID2_OUI_MASK) >> PHYID2_OUI_SHFT;
60668c2ecf20Sopenharmony_ci		np->phyaddr = phyaddr;
60678c2ecf20Sopenharmony_ci		np->phy_oui = id1 | id2;
60688c2ecf20Sopenharmony_ci
60698c2ecf20Sopenharmony_ci		/* Realtek hardcoded phy id1 to all zero's on certain phys */
60708c2ecf20Sopenharmony_ci		if (np->phy_oui == PHY_OUI_REALTEK2)
60718c2ecf20Sopenharmony_ci			np->phy_oui = PHY_OUI_REALTEK;
60728c2ecf20Sopenharmony_ci		/* Setup phy revision for Realtek */
60738c2ecf20Sopenharmony_ci		if (np->phy_oui == PHY_OUI_REALTEK && np->phy_model == PHY_MODEL_REALTEK_8211)
60748c2ecf20Sopenharmony_ci			np->phy_rev = mii_rw(dev, phyaddr, MII_RESV1, MII_READ) & PHY_REV_MASK;
60758c2ecf20Sopenharmony_ci
60768c2ecf20Sopenharmony_ci		break;
60778c2ecf20Sopenharmony_ci	}
60788c2ecf20Sopenharmony_ci	if (i == 33) {
60798c2ecf20Sopenharmony_ci		dev_info(&pci_dev->dev, "open: Could not find a valid PHY\n");
60808c2ecf20Sopenharmony_ci		goto out_error;
60818c2ecf20Sopenharmony_ci	}
60828c2ecf20Sopenharmony_ci
60838c2ecf20Sopenharmony_ci	if (!phyinitialized) {
60848c2ecf20Sopenharmony_ci		/* reset it */
60858c2ecf20Sopenharmony_ci		phy_init(dev);
60868c2ecf20Sopenharmony_ci	} else {
60878c2ecf20Sopenharmony_ci		/* see if it is a gigabit phy */
60888c2ecf20Sopenharmony_ci		u32 mii_status = mii_rw(dev, np->phyaddr, MII_BMSR, MII_READ);
60898c2ecf20Sopenharmony_ci		if (mii_status & PHY_GIGABIT)
60908c2ecf20Sopenharmony_ci			np->gigabit = PHY_GIGABIT;
60918c2ecf20Sopenharmony_ci	}
60928c2ecf20Sopenharmony_ci
60938c2ecf20Sopenharmony_ci	/* set default link speed settings */
60948c2ecf20Sopenharmony_ci	np->linkspeed = NVREG_LINKSPEED_FORCE|NVREG_LINKSPEED_10;
60958c2ecf20Sopenharmony_ci	np->duplex = 0;
60968c2ecf20Sopenharmony_ci	np->autoneg = 1;
60978c2ecf20Sopenharmony_ci
60988c2ecf20Sopenharmony_ci	err = register_netdev(dev);
60998c2ecf20Sopenharmony_ci	if (err) {
61008c2ecf20Sopenharmony_ci		dev_info(&pci_dev->dev, "unable to register netdev: %d\n", err);
61018c2ecf20Sopenharmony_ci		goto out_error;
61028c2ecf20Sopenharmony_ci	}
61038c2ecf20Sopenharmony_ci
61048c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
61058c2ecf20Sopenharmony_ci
61068c2ecf20Sopenharmony_ci	/* Some NICs freeze when TX pause is enabled while NIC is
61078c2ecf20Sopenharmony_ci	 * down, and this stays across warm reboots. The sequence
61088c2ecf20Sopenharmony_ci	 * below should be enough to recover from that state.
61098c2ecf20Sopenharmony_ci	 */
61108c2ecf20Sopenharmony_ci	nv_update_pause(dev, 0);
61118c2ecf20Sopenharmony_ci	nv_start_tx(dev);
61128c2ecf20Sopenharmony_ci	nv_stop_tx(dev);
61138c2ecf20Sopenharmony_ci
61148c2ecf20Sopenharmony_ci	if (id->driver_data & DEV_HAS_VLAN)
61158c2ecf20Sopenharmony_ci		nv_vlan_mode(dev, dev->features);
61168c2ecf20Sopenharmony_ci
61178c2ecf20Sopenharmony_ci	dev_info(&pci_dev->dev, "ifname %s, PHY OUI 0x%x @ %d, addr %pM\n",
61188c2ecf20Sopenharmony_ci		 dev->name, np->phy_oui, np->phyaddr, dev->dev_addr);
61198c2ecf20Sopenharmony_ci
61208c2ecf20Sopenharmony_ci	dev_info(&pci_dev->dev, "%s%s%s%s%s%s%s%s%s%s%sdesc-v%u\n",
61218c2ecf20Sopenharmony_ci		 dev->features & NETIF_F_HIGHDMA ? "highdma " : "",
61228c2ecf20Sopenharmony_ci		 dev->features & (NETIF_F_IP_CSUM | NETIF_F_SG) ?
61238c2ecf20Sopenharmony_ci			"csum " : "",
61248c2ecf20Sopenharmony_ci		 dev->features & (NETIF_F_HW_VLAN_CTAG_RX |
61258c2ecf20Sopenharmony_ci				  NETIF_F_HW_VLAN_CTAG_TX) ?
61268c2ecf20Sopenharmony_ci			"vlan " : "",
61278c2ecf20Sopenharmony_ci		 dev->features & (NETIF_F_LOOPBACK) ?
61288c2ecf20Sopenharmony_ci			"loopback " : "",
61298c2ecf20Sopenharmony_ci		 id->driver_data & DEV_HAS_POWER_CNTRL ? "pwrctl " : "",
61308c2ecf20Sopenharmony_ci		 id->driver_data & DEV_HAS_MGMT_UNIT ? "mgmt " : "",
61318c2ecf20Sopenharmony_ci		 id->driver_data & DEV_NEED_TIMERIRQ ? "timirq " : "",
61328c2ecf20Sopenharmony_ci		 np->gigabit == PHY_GIGABIT ? "gbit " : "",
61338c2ecf20Sopenharmony_ci		 np->need_linktimer ? "lnktim " : "",
61348c2ecf20Sopenharmony_ci		 np->msi_flags & NV_MSI_CAPABLE ? "msi " : "",
61358c2ecf20Sopenharmony_ci		 np->msi_flags & NV_MSI_X_CAPABLE ? "msi-x " : "",
61368c2ecf20Sopenharmony_ci		 np->desc_ver);
61378c2ecf20Sopenharmony_ci
61388c2ecf20Sopenharmony_ci	return 0;
61398c2ecf20Sopenharmony_ci
61408c2ecf20Sopenharmony_ciout_error:
61418c2ecf20Sopenharmony_ci	nv_mgmt_release_sema(dev);
61428c2ecf20Sopenharmony_ci	if (phystate_orig)
61438c2ecf20Sopenharmony_ci		writel(phystate|NVREG_ADAPTCTL_RUNNING, base + NvRegAdapterControl);
61448c2ecf20Sopenharmony_ciout_freering:
61458c2ecf20Sopenharmony_ci	free_rings(dev);
61468c2ecf20Sopenharmony_ciout_unmap:
61478c2ecf20Sopenharmony_ci	iounmap(get_hwbase(dev));
61488c2ecf20Sopenharmony_ciout_relreg:
61498c2ecf20Sopenharmony_ci	pci_release_regions(pci_dev);
61508c2ecf20Sopenharmony_ciout_disable:
61518c2ecf20Sopenharmony_ci	pci_disable_device(pci_dev);
61528c2ecf20Sopenharmony_ciout_free:
61538c2ecf20Sopenharmony_ci	free_percpu(np->txrx_stats);
61548c2ecf20Sopenharmony_ciout_alloc_percpu:
61558c2ecf20Sopenharmony_ci	free_netdev(dev);
61568c2ecf20Sopenharmony_ciout:
61578c2ecf20Sopenharmony_ci	return err;
61588c2ecf20Sopenharmony_ci}
61598c2ecf20Sopenharmony_ci
61608c2ecf20Sopenharmony_cistatic void nv_restore_phy(struct net_device *dev)
61618c2ecf20Sopenharmony_ci{
61628c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
61638c2ecf20Sopenharmony_ci	u16 phy_reserved, mii_control;
61648c2ecf20Sopenharmony_ci
61658c2ecf20Sopenharmony_ci	if (np->phy_oui == PHY_OUI_REALTEK &&
61668c2ecf20Sopenharmony_ci	    np->phy_model == PHY_MODEL_REALTEK_8201 &&
61678c2ecf20Sopenharmony_ci	    phy_cross == NV_CROSSOVER_DETECTION_DISABLED) {
61688c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT3);
61698c2ecf20Sopenharmony_ci		phy_reserved = mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG2, MII_READ);
61708c2ecf20Sopenharmony_ci		phy_reserved &= ~PHY_REALTEK_INIT_MSK1;
61718c2ecf20Sopenharmony_ci		phy_reserved |= PHY_REALTEK_INIT8;
61728c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG2, phy_reserved);
61738c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, PHY_REALTEK_INIT_REG1, PHY_REALTEK_INIT1);
61748c2ecf20Sopenharmony_ci
61758c2ecf20Sopenharmony_ci		/* restart auto negotiation */
61768c2ecf20Sopenharmony_ci		mii_control = mii_rw(dev, np->phyaddr, MII_BMCR, MII_READ);
61778c2ecf20Sopenharmony_ci		mii_control |= (BMCR_ANRESTART | BMCR_ANENABLE);
61788c2ecf20Sopenharmony_ci		mii_rw(dev, np->phyaddr, MII_BMCR, mii_control);
61798c2ecf20Sopenharmony_ci	}
61808c2ecf20Sopenharmony_ci}
61818c2ecf20Sopenharmony_ci
61828c2ecf20Sopenharmony_cistatic void nv_restore_mac_addr(struct pci_dev *pci_dev)
61838c2ecf20Sopenharmony_ci{
61848c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(pci_dev);
61858c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
61868c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
61878c2ecf20Sopenharmony_ci
61888c2ecf20Sopenharmony_ci	/* special op: write back the misordered MAC address - otherwise
61898c2ecf20Sopenharmony_ci	 * the next nv_probe would see a wrong address.
61908c2ecf20Sopenharmony_ci	 */
61918c2ecf20Sopenharmony_ci	writel(np->orig_mac[0], base + NvRegMacAddrA);
61928c2ecf20Sopenharmony_ci	writel(np->orig_mac[1], base + NvRegMacAddrB);
61938c2ecf20Sopenharmony_ci	writel(readl(base + NvRegTransmitPoll) & ~NVREG_TRANSMITPOLL_MAC_ADDR_REV,
61948c2ecf20Sopenharmony_ci	       base + NvRegTransmitPoll);
61958c2ecf20Sopenharmony_ci}
61968c2ecf20Sopenharmony_ci
61978c2ecf20Sopenharmony_cistatic void nv_remove(struct pci_dev *pci_dev)
61988c2ecf20Sopenharmony_ci{
61998c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(pci_dev);
62008c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
62018c2ecf20Sopenharmony_ci
62028c2ecf20Sopenharmony_ci	free_percpu(np->txrx_stats);
62038c2ecf20Sopenharmony_ci
62048c2ecf20Sopenharmony_ci	unregister_netdev(dev);
62058c2ecf20Sopenharmony_ci
62068c2ecf20Sopenharmony_ci	nv_restore_mac_addr(pci_dev);
62078c2ecf20Sopenharmony_ci
62088c2ecf20Sopenharmony_ci	/* restore any phy related changes */
62098c2ecf20Sopenharmony_ci	nv_restore_phy(dev);
62108c2ecf20Sopenharmony_ci
62118c2ecf20Sopenharmony_ci	nv_mgmt_release_sema(dev);
62128c2ecf20Sopenharmony_ci
62138c2ecf20Sopenharmony_ci	/* free all structures */
62148c2ecf20Sopenharmony_ci	free_rings(dev);
62158c2ecf20Sopenharmony_ci	iounmap(get_hwbase(dev));
62168c2ecf20Sopenharmony_ci	pci_release_regions(pci_dev);
62178c2ecf20Sopenharmony_ci	pci_disable_device(pci_dev);
62188c2ecf20Sopenharmony_ci	free_netdev(dev);
62198c2ecf20Sopenharmony_ci}
62208c2ecf20Sopenharmony_ci
62218c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
62228c2ecf20Sopenharmony_cistatic int nv_suspend(struct device *device)
62238c2ecf20Sopenharmony_ci{
62248c2ecf20Sopenharmony_ci	struct net_device *dev = dev_get_drvdata(device);
62258c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
62268c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
62278c2ecf20Sopenharmony_ci	int i;
62288c2ecf20Sopenharmony_ci
62298c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
62308c2ecf20Sopenharmony_ci		/* Gross. */
62318c2ecf20Sopenharmony_ci		nv_close(dev);
62328c2ecf20Sopenharmony_ci	}
62338c2ecf20Sopenharmony_ci	netif_device_detach(dev);
62348c2ecf20Sopenharmony_ci
62358c2ecf20Sopenharmony_ci	/* save non-pci configuration space */
62368c2ecf20Sopenharmony_ci	for (i = 0; i <= np->register_size/sizeof(u32); i++)
62378c2ecf20Sopenharmony_ci		np->saved_config_space[i] = readl(base + i*sizeof(u32));
62388c2ecf20Sopenharmony_ci
62398c2ecf20Sopenharmony_ci	return 0;
62408c2ecf20Sopenharmony_ci}
62418c2ecf20Sopenharmony_ci
62428c2ecf20Sopenharmony_cistatic int nv_resume(struct device *device)
62438c2ecf20Sopenharmony_ci{
62448c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(device);
62458c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(pdev);
62468c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
62478c2ecf20Sopenharmony_ci	u8 __iomem *base = get_hwbase(dev);
62488c2ecf20Sopenharmony_ci	int i, rc = 0;
62498c2ecf20Sopenharmony_ci
62508c2ecf20Sopenharmony_ci	/* restore non-pci configuration space */
62518c2ecf20Sopenharmony_ci	for (i = 0; i <= np->register_size/sizeof(u32); i++)
62528c2ecf20Sopenharmony_ci		writel(np->saved_config_space[i], base+i*sizeof(u32));
62538c2ecf20Sopenharmony_ci
62548c2ecf20Sopenharmony_ci	if (np->driver_data & DEV_NEED_MSI_FIX)
62558c2ecf20Sopenharmony_ci		pci_write_config_dword(pdev, NV_MSI_PRIV_OFFSET, NV_MSI_PRIV_VALUE);
62568c2ecf20Sopenharmony_ci
62578c2ecf20Sopenharmony_ci	/* restore phy state, including autoneg */
62588c2ecf20Sopenharmony_ci	phy_init(dev);
62598c2ecf20Sopenharmony_ci
62608c2ecf20Sopenharmony_ci	netif_device_attach(dev);
62618c2ecf20Sopenharmony_ci	if (netif_running(dev)) {
62628c2ecf20Sopenharmony_ci		rc = nv_open(dev);
62638c2ecf20Sopenharmony_ci		nv_set_multicast(dev);
62648c2ecf20Sopenharmony_ci	}
62658c2ecf20Sopenharmony_ci	return rc;
62668c2ecf20Sopenharmony_ci}
62678c2ecf20Sopenharmony_ci
62688c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(nv_pm_ops, nv_suspend, nv_resume);
62698c2ecf20Sopenharmony_ci#define NV_PM_OPS (&nv_pm_ops)
62708c2ecf20Sopenharmony_ci
62718c2ecf20Sopenharmony_ci#else
62728c2ecf20Sopenharmony_ci#define NV_PM_OPS NULL
62738c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
62748c2ecf20Sopenharmony_ci
62758c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
62768c2ecf20Sopenharmony_cistatic void nv_shutdown(struct pci_dev *pdev)
62778c2ecf20Sopenharmony_ci{
62788c2ecf20Sopenharmony_ci	struct net_device *dev = pci_get_drvdata(pdev);
62798c2ecf20Sopenharmony_ci	struct fe_priv *np = netdev_priv(dev);
62808c2ecf20Sopenharmony_ci
62818c2ecf20Sopenharmony_ci	if (netif_running(dev))
62828c2ecf20Sopenharmony_ci		nv_close(dev);
62838c2ecf20Sopenharmony_ci
62848c2ecf20Sopenharmony_ci	/*
62858c2ecf20Sopenharmony_ci	 * Restore the MAC so a kernel started by kexec won't get confused.
62868c2ecf20Sopenharmony_ci	 * If we really go for poweroff, we must not restore the MAC,
62878c2ecf20Sopenharmony_ci	 * otherwise the MAC for WOL will be reversed at least on some boards.
62888c2ecf20Sopenharmony_ci	 */
62898c2ecf20Sopenharmony_ci	if (system_state != SYSTEM_POWER_OFF)
62908c2ecf20Sopenharmony_ci		nv_restore_mac_addr(pdev);
62918c2ecf20Sopenharmony_ci
62928c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
62938c2ecf20Sopenharmony_ci	/*
62948c2ecf20Sopenharmony_ci	 * Apparently it is not possible to reinitialise from D3 hot,
62958c2ecf20Sopenharmony_ci	 * only put the device into D3 if we really go for poweroff.
62968c2ecf20Sopenharmony_ci	 */
62978c2ecf20Sopenharmony_ci	if (system_state == SYSTEM_POWER_OFF) {
62988c2ecf20Sopenharmony_ci		pci_wake_from_d3(pdev, np->wolenabled);
62998c2ecf20Sopenharmony_ci		pci_set_power_state(pdev, PCI_D3hot);
63008c2ecf20Sopenharmony_ci	}
63018c2ecf20Sopenharmony_ci}
63028c2ecf20Sopenharmony_ci#else
63038c2ecf20Sopenharmony_ci#define nv_shutdown NULL
63048c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
63058c2ecf20Sopenharmony_ci
63068c2ecf20Sopenharmony_cistatic const struct pci_device_id pci_tbl[] = {
63078c2ecf20Sopenharmony_ci	{	/* nForce Ethernet Controller */
63088c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x01C3),
63098c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
63108c2ecf20Sopenharmony_ci	},
63118c2ecf20Sopenharmony_ci	{	/* nForce2 Ethernet Controller */
63128c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0066),
63138c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
63148c2ecf20Sopenharmony_ci	},
63158c2ecf20Sopenharmony_ci	{	/* nForce3 Ethernet Controller */
63168c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x00D6),
63178c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
63188c2ecf20Sopenharmony_ci	},
63198c2ecf20Sopenharmony_ci	{	/* nForce3 Ethernet Controller */
63208c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0086),
63218c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
63228c2ecf20Sopenharmony_ci	},
63238c2ecf20Sopenharmony_ci	{	/* nForce3 Ethernet Controller */
63248c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x008C),
63258c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
63268c2ecf20Sopenharmony_ci	},
63278c2ecf20Sopenharmony_ci	{	/* nForce3 Ethernet Controller */
63288c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x00E6),
63298c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
63308c2ecf20Sopenharmony_ci	},
63318c2ecf20Sopenharmony_ci	{	/* nForce3 Ethernet Controller */
63328c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x00DF),
63338c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM,
63348c2ecf20Sopenharmony_ci	},
63358c2ecf20Sopenharmony_ci	{	/* CK804 Ethernet Controller */
63368c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0056),
63378c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1|DEV_NEED_TX_LIMIT,
63388c2ecf20Sopenharmony_ci	},
63398c2ecf20Sopenharmony_ci	{	/* CK804 Ethernet Controller */
63408c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0057),
63418c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1|DEV_NEED_TX_LIMIT,
63428c2ecf20Sopenharmony_ci	},
63438c2ecf20Sopenharmony_ci	{	/* MCP04 Ethernet Controller */
63448c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0037),
63458c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1|DEV_NEED_TX_LIMIT,
63468c2ecf20Sopenharmony_ci	},
63478c2ecf20Sopenharmony_ci	{	/* MCP04 Ethernet Controller */
63488c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0038),
63498c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_STATISTICS_V1|DEV_NEED_TX_LIMIT,
63508c2ecf20Sopenharmony_ci	},
63518c2ecf20Sopenharmony_ci	{	/* MCP51 Ethernet Controller */
63528c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0268),
63538c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V1|DEV_NEED_LOW_POWER_FIX,
63548c2ecf20Sopenharmony_ci	},
63558c2ecf20Sopenharmony_ci	{	/* MCP51 Ethernet Controller */
63568c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0269),
63578c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_STATISTICS_V1|DEV_NEED_LOW_POWER_FIX,
63588c2ecf20Sopenharmony_ci	},
63598c2ecf20Sopenharmony_ci	{	/* MCP55 Ethernet Controller */
63608c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0372),
63618c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_VLAN|DEV_HAS_MSI|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_NEED_TX_LIMIT|DEV_NEED_MSI_FIX,
63628c2ecf20Sopenharmony_ci	},
63638c2ecf20Sopenharmony_ci	{	/* MCP55 Ethernet Controller */
63648c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0373),
63658c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_VLAN|DEV_HAS_MSI|DEV_HAS_MSI_X|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_NEED_TX_LIMIT|DEV_NEED_MSI_FIX,
63668c2ecf20Sopenharmony_ci	},
63678c2ecf20Sopenharmony_ci	{	/* MCP61 Ethernet Controller */
63688c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x03E5),
63698c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_MSI_FIX,
63708c2ecf20Sopenharmony_ci	},
63718c2ecf20Sopenharmony_ci	{	/* MCP61 Ethernet Controller */
63728c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x03E6),
63738c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_MSI_FIX,
63748c2ecf20Sopenharmony_ci	},
63758c2ecf20Sopenharmony_ci	{	/* MCP61 Ethernet Controller */
63768c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x03EE),
63778c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_MSI_FIX,
63788c2ecf20Sopenharmony_ci	},
63798c2ecf20Sopenharmony_ci	{	/* MCP61 Ethernet Controller */
63808c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x03EF),
63818c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_MSI_FIX,
63828c2ecf20Sopenharmony_ci	},
63838c2ecf20Sopenharmony_ci	{	/* MCP65 Ethernet Controller */
63848c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0450),
63858c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_TX_LIMIT|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
63868c2ecf20Sopenharmony_ci	},
63878c2ecf20Sopenharmony_ci	{	/* MCP65 Ethernet Controller */
63888c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0451),
63898c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_TX_LIMIT|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
63908c2ecf20Sopenharmony_ci	},
63918c2ecf20Sopenharmony_ci	{	/* MCP65 Ethernet Controller */
63928c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0452),
63938c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_TX_LIMIT|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
63948c2ecf20Sopenharmony_ci	},
63958c2ecf20Sopenharmony_ci	{	/* MCP65 Ethernet Controller */
63968c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0453),
63978c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_NEED_TX_LIMIT|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
63988c2ecf20Sopenharmony_ci	},
63998c2ecf20Sopenharmony_ci	{	/* MCP67 Ethernet Controller */
64008c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x054C),
64018c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64028c2ecf20Sopenharmony_ci	},
64038c2ecf20Sopenharmony_ci	{	/* MCP67 Ethernet Controller */
64048c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x054D),
64058c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64068c2ecf20Sopenharmony_ci	},
64078c2ecf20Sopenharmony_ci	{	/* MCP67 Ethernet Controller */
64088c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x054E),
64098c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64108c2ecf20Sopenharmony_ci	},
64118c2ecf20Sopenharmony_ci	{	/* MCP67 Ethernet Controller */
64128c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x054F),
64138c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64148c2ecf20Sopenharmony_ci	},
64158c2ecf20Sopenharmony_ci	{	/* MCP73 Ethernet Controller */
64168c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x07DC),
64178c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64188c2ecf20Sopenharmony_ci	},
64198c2ecf20Sopenharmony_ci	{	/* MCP73 Ethernet Controller */
64208c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x07DD),
64218c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64228c2ecf20Sopenharmony_ci	},
64238c2ecf20Sopenharmony_ci	{	/* MCP73 Ethernet Controller */
64248c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x07DE),
64258c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64268c2ecf20Sopenharmony_ci	},
64278c2ecf20Sopenharmony_ci	{	/* MCP73 Ethernet Controller */
64288c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x07DF),
64298c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_HIGH_DMA|DEV_HAS_POWER_CNTRL|DEV_HAS_MSI|DEV_HAS_PAUSEFRAME_TX_V1|DEV_HAS_STATISTICS_V12|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_HAS_GEAR_MODE|DEV_NEED_MSI_FIX,
64308c2ecf20Sopenharmony_ci	},
64318c2ecf20Sopenharmony_ci	{	/* MCP77 Ethernet Controller */
64328c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0760),
64338c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V2|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64348c2ecf20Sopenharmony_ci	},
64358c2ecf20Sopenharmony_ci	{	/* MCP77 Ethernet Controller */
64368c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0761),
64378c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V2|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64388c2ecf20Sopenharmony_ci	},
64398c2ecf20Sopenharmony_ci	{	/* MCP77 Ethernet Controller */
64408c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0762),
64418c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V2|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64428c2ecf20Sopenharmony_ci	},
64438c2ecf20Sopenharmony_ci	{	/* MCP77 Ethernet Controller */
64448c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0763),
64458c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V2|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_MGMT_UNIT|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64468c2ecf20Sopenharmony_ci	},
64478c2ecf20Sopenharmony_ci	{	/* MCP79 Ethernet Controller */
64488c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0AB0),
64498c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V3|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64508c2ecf20Sopenharmony_ci	},
64518c2ecf20Sopenharmony_ci	{	/* MCP79 Ethernet Controller */
64528c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0AB1),
64538c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V3|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64548c2ecf20Sopenharmony_ci	},
64558c2ecf20Sopenharmony_ci	{	/* MCP79 Ethernet Controller */
64568c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0AB2),
64578c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V3|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64588c2ecf20Sopenharmony_ci	},
64598c2ecf20Sopenharmony_ci	{	/* MCP79 Ethernet Controller */
64608c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0AB3),
64618c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V3|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_NEED_TX_LIMIT2|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX|DEV_NEED_MSI_FIX,
64628c2ecf20Sopenharmony_ci	},
64638c2ecf20Sopenharmony_ci	{	/* MCP89 Ethernet Controller */
64648c2ecf20Sopenharmony_ci		PCI_DEVICE(0x10DE, 0x0D7D),
64658c2ecf20Sopenharmony_ci		.driver_data = DEV_NEED_LINKTIMER|DEV_HAS_LARGEDESC|DEV_HAS_CHECKSUM|DEV_HAS_HIGH_DMA|DEV_HAS_MSI|DEV_HAS_POWER_CNTRL|DEV_HAS_PAUSEFRAME_TX_V3|DEV_HAS_STATISTICS_V123|DEV_HAS_TEST_EXTENDED|DEV_HAS_CORRECT_MACADDR|DEV_HAS_COLLISION_FIX|DEV_HAS_GEAR_MODE|DEV_NEED_PHY_INIT_FIX,
64668c2ecf20Sopenharmony_ci	},
64678c2ecf20Sopenharmony_ci	{0,},
64688c2ecf20Sopenharmony_ci};
64698c2ecf20Sopenharmony_ci
64708c2ecf20Sopenharmony_cistatic struct pci_driver forcedeth_pci_driver = {
64718c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
64728c2ecf20Sopenharmony_ci	.id_table	= pci_tbl,
64738c2ecf20Sopenharmony_ci	.probe		= nv_probe,
64748c2ecf20Sopenharmony_ci	.remove		= nv_remove,
64758c2ecf20Sopenharmony_ci	.shutdown	= nv_shutdown,
64768c2ecf20Sopenharmony_ci	.driver.pm	= NV_PM_OPS,
64778c2ecf20Sopenharmony_ci};
64788c2ecf20Sopenharmony_ci
64798c2ecf20Sopenharmony_cimodule_param(max_interrupt_work, int, 0);
64808c2ecf20Sopenharmony_ciMODULE_PARM_DESC(max_interrupt_work, "forcedeth maximum events handled per interrupt");
64818c2ecf20Sopenharmony_cimodule_param(optimization_mode, int, 0);
64828c2ecf20Sopenharmony_ciMODULE_PARM_DESC(optimization_mode, "In throughput mode (0), every tx & rx packet will generate an interrupt. In CPU mode (1), interrupts are controlled by a timer. In dynamic mode (2), the mode toggles between throughput and CPU mode based on network load.");
64838c2ecf20Sopenharmony_cimodule_param(poll_interval, int, 0);
64848c2ecf20Sopenharmony_ciMODULE_PARM_DESC(poll_interval, "Interval determines how frequent timer interrupt is generated by [(time_in_micro_secs * 100) / (2^10)]. Min is 0 and Max is 65535.");
64858c2ecf20Sopenharmony_cimodule_param(msi, int, 0);
64868c2ecf20Sopenharmony_ciMODULE_PARM_DESC(msi, "MSI interrupts are enabled by setting to 1 and disabled by setting to 0.");
64878c2ecf20Sopenharmony_cimodule_param(msix, int, 0);
64888c2ecf20Sopenharmony_ciMODULE_PARM_DESC(msix, "MSIX interrupts are enabled by setting to 1 and disabled by setting to 0.");
64898c2ecf20Sopenharmony_cimodule_param(dma_64bit, int, 0);
64908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dma_64bit, "High DMA is enabled by setting to 1 and disabled by setting to 0.");
64918c2ecf20Sopenharmony_cimodule_param(phy_cross, int, 0);
64928c2ecf20Sopenharmony_ciMODULE_PARM_DESC(phy_cross, "Phy crossover detection for Realtek 8201 phy is enabled by setting to 1 and disabled by setting to 0.");
64938c2ecf20Sopenharmony_cimodule_param(phy_power_down, int, 0);
64948c2ecf20Sopenharmony_ciMODULE_PARM_DESC(phy_power_down, "Power down phy and disable link when interface is down (1), or leave phy powered up (0).");
64958c2ecf20Sopenharmony_cimodule_param(debug_tx_timeout, bool, 0);
64968c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug_tx_timeout,
64978c2ecf20Sopenharmony_ci		 "Dump tx related registers and ring when tx_timeout happens");
64988c2ecf20Sopenharmony_ci
64998c2ecf20Sopenharmony_cimodule_pci_driver(forcedeth_pci_driver);
65008c2ecf20Sopenharmony_ciMODULE_AUTHOR("Manfred Spraul <manfred@colorfullife.com>");
65018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Reverse Engineered nForce ethernet driver");
65028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
65038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, pci_tbl);
6504