xref: /openbmc/linux/drivers/net/ethernet/marvell/sky2.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
18fe76f5aSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2527a6266SJeff Kirsher /*
3527a6266SJeff Kirsher  * New driver for Marvell Yukon 2 chipset.
4527a6266SJeff Kirsher  * Based on earlier sk98lin, and skge driver.
5527a6266SJeff Kirsher  *
6527a6266SJeff Kirsher  * This driver intentionally does not support all the features
7527a6266SJeff Kirsher  * of the original driver such as link fail-over and link management because
8527a6266SJeff Kirsher  * those should be done at higher levels.
9527a6266SJeff Kirsher  *
10527a6266SJeff Kirsher  * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
11527a6266SJeff Kirsher  */
12527a6266SJeff Kirsher 
13527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14527a6266SJeff Kirsher 
15527a6266SJeff Kirsher #include <linux/crc32.h>
16527a6266SJeff Kirsher #include <linux/kernel.h>
17527a6266SJeff Kirsher #include <linux/module.h>
18527a6266SJeff Kirsher #include <linux/netdevice.h>
19527a6266SJeff Kirsher #include <linux/dma-mapping.h>
20527a6266SJeff Kirsher #include <linux/etherdevice.h>
21527a6266SJeff Kirsher #include <linux/ethtool.h>
22527a6266SJeff Kirsher #include <linux/pci.h>
23527a6266SJeff Kirsher #include <linux/interrupt.h>
24527a6266SJeff Kirsher #include <linux/ip.h>
25527a6266SJeff Kirsher #include <linux/slab.h>
26527a6266SJeff Kirsher #include <net/ip.h>
27527a6266SJeff Kirsher #include <linux/tcp.h>
28527a6266SJeff Kirsher #include <linux/in.h>
29527a6266SJeff Kirsher #include <linux/delay.h>
30527a6266SJeff Kirsher #include <linux/workqueue.h>
31527a6266SJeff Kirsher #include <linux/if_vlan.h>
32527a6266SJeff Kirsher #include <linux/prefetch.h>
33527a6266SJeff Kirsher #include <linux/debugfs.h>
34527a6266SJeff Kirsher #include <linux/mii.h>
353ee2f8ceSTim Harvey #include <linux/of_net.h>
36b33b7cd6SKai-Heng Feng #include <linux/dmi.h>
37527a6266SJeff Kirsher 
38527a6266SJeff Kirsher #include <asm/irq.h>
39527a6266SJeff Kirsher 
40527a6266SJeff Kirsher #include "sky2.h"
41527a6266SJeff Kirsher 
42527a6266SJeff Kirsher #define DRV_NAME		"sky2"
43d9fa7c86Sstephen hemminger #define DRV_VERSION		"1.30"
44527a6266SJeff Kirsher 
45527a6266SJeff Kirsher /*
46527a6266SJeff Kirsher  * The Yukon II chipset takes 64 bit command blocks (called list elements)
47527a6266SJeff Kirsher  * that are organized into three (receive, transmit, status) different rings
48527a6266SJeff Kirsher  * similar to Tigon3.
49527a6266SJeff Kirsher  */
50527a6266SJeff Kirsher 
51527a6266SJeff Kirsher #define RX_LE_SIZE	    	1024
52527a6266SJeff Kirsher #define RX_LE_BYTES		(RX_LE_SIZE*sizeof(struct sky2_rx_le))
53527a6266SJeff Kirsher #define RX_MAX_PENDING		(RX_LE_SIZE/6 - 2)
54527a6266SJeff Kirsher #define RX_DEF_PENDING		RX_MAX_PENDING
55527a6266SJeff Kirsher 
56527a6266SJeff Kirsher /* This is the worst case number of transmit list elements for a single skb:
57df4a17a9SYangyang Li  * VLAN:GSO + CKSUM + Data + skb_frags * DMA
58df4a17a9SYangyang Li  */
59527a6266SJeff Kirsher #define MAX_SKB_TX_LE	(2 + (sizeof(dma_addr_t)/sizeof(u32))*(MAX_SKB_FRAGS+1))
60527a6266SJeff Kirsher #define TX_MIN_PENDING		(MAX_SKB_TX_LE+1)
61527a6266SJeff Kirsher #define TX_MAX_PENDING		1024
62b1cb8256Sstephen hemminger #define TX_DEF_PENDING		63
63527a6266SJeff Kirsher 
64527a6266SJeff Kirsher #define TX_WATCHDOG		(5 * HZ)
65527a6266SJeff Kirsher #define PHY_RETRIES		1000
66527a6266SJeff Kirsher 
67527a6266SJeff Kirsher #define SKY2_EEPROM_MAGIC	0x9955aabb
68527a6266SJeff Kirsher 
69527a6266SJeff Kirsher #define RING_NEXT(x, s)	(((x)+1) & ((s)-1))
70527a6266SJeff Kirsher 
71527a6266SJeff Kirsher static const u32 default_msg =
72527a6266SJeff Kirsher     NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
73527a6266SJeff Kirsher     | NETIF_MSG_TIMER | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR
74527a6266SJeff Kirsher     | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
75527a6266SJeff Kirsher 
76527a6266SJeff Kirsher static int debug = -1;		/* defaults above */
77527a6266SJeff Kirsher module_param(debug, int, 0);
78527a6266SJeff Kirsher MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
79527a6266SJeff Kirsher 
80527a6266SJeff Kirsher static int copybreak __read_mostly = 128;
81527a6266SJeff Kirsher module_param(copybreak, int, 0);
82527a6266SJeff Kirsher MODULE_PARM_DESC(copybreak, "Receive copy threshold");
83527a6266SJeff Kirsher 
84b33b7cd6SKai-Heng Feng static int disable_msi = -1;
85527a6266SJeff Kirsher module_param(disable_msi, int, 0);
86527a6266SJeff Kirsher MODULE_PARM_DESC(disable_msi, "Disable Message Signaled Interrupt (MSI)");
87527a6266SJeff Kirsher 
885676cc7bSstephen hemminger static int legacy_pme = 0;
895676cc7bSstephen hemminger module_param(legacy_pme, int, 0);
905676cc7bSstephen hemminger MODULE_PARM_DESC(legacy_pme, "Legacy power management");
915676cc7bSstephen hemminger 
929baa3c34SBenoit Taine static const struct pci_device_id sky2_id_table[] = {
93527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9000) }, /* SK-9Sxx */
94527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9E00) }, /* SK-9Exx */
95527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9E01) }, /* SK-9E21M */
96527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4b00) },	/* DGE-560T */
97527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4001) }, 	/* DGE-550SX */
98527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4B02) },	/* DGE-560SX */
99527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4B03) },	/* DGE-550T */
100527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4340) }, /* 88E8021 */
101527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4341) }, /* 88E8022 */
102527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4342) }, /* 88E8061 */
103527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4343) }, /* 88E8062 */
104527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4344) }, /* 88E8021 */
105527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4345) }, /* 88E8022 */
106527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4346) }, /* 88E8061 */
107527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4347) }, /* 88E8062 */
108527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4350) }, /* 88E8035 */
109527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4351) }, /* 88E8036 */
110527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4352) }, /* 88E8038 */
111527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4353) }, /* 88E8039 */
112527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4354) }, /* 88E8040 */
113527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4355) }, /* 88E8040T */
114527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4356) }, /* 88EC033 */
115527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4357) }, /* 88E8042 */
116527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x435A) }, /* 88E8048 */
117527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4360) }, /* 88E8052 */
118527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4361) }, /* 88E8050 */
119527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4362) }, /* 88E8053 */
120527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4363) }, /* 88E8055 */
121527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4364) }, /* 88E8056 */
122527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4365) }, /* 88E8070 */
123527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4366) }, /* 88EC036 */
124527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4367) }, /* 88EC032 */
125527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4368) }, /* 88EC034 */
126527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4369) }, /* 88EC042 */
127527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436A) }, /* 88E8058 */
128527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436B) }, /* 88E8071 */
129527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436C) }, /* 88E8072 */
130527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436D) }, /* 88E8055 */
131527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4370) }, /* 88E8075 */
132527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4380) }, /* 88E8057 */
133527a6266SJeff Kirsher 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4381) }, /* 88E8059 */
1340e767324SMirko Lindner 	{ PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4382) }, /* 88E8079 */
135527a6266SJeff Kirsher 	{ 0 }
136527a6266SJeff Kirsher };
137527a6266SJeff Kirsher 
138527a6266SJeff Kirsher MODULE_DEVICE_TABLE(pci, sky2_id_table);
139527a6266SJeff Kirsher 
140527a6266SJeff Kirsher /* Avoid conditionals by using array */
141527a6266SJeff Kirsher static const unsigned txqaddr[] = { Q_XA1, Q_XA2 };
142527a6266SJeff Kirsher static const unsigned rxqaddr[] = { Q_R1, Q_R2 };
143527a6266SJeff Kirsher static const u32 portirq_msk[] = { Y2_IS_PORT_1, Y2_IS_PORT_2 };
144527a6266SJeff Kirsher 
145527a6266SJeff Kirsher static void sky2_set_multicast(struct net_device *dev);
1460bdb0bd0Sstephen hemminger static irqreturn_t sky2_intr(int irq, void *dev_id);
147527a6266SJeff Kirsher 
148527a6266SJeff Kirsher /* Access to PHY via serial interconnect */
gm_phy_write(struct sky2_hw * hw,unsigned port,u16 reg,u16 val)149527a6266SJeff Kirsher static int gm_phy_write(struct sky2_hw *hw, unsigned port, u16 reg, u16 val)
150527a6266SJeff Kirsher {
151527a6266SJeff Kirsher 	int i;
152527a6266SJeff Kirsher 
153527a6266SJeff Kirsher 	gma_write16(hw, port, GM_SMI_DATA, val);
154527a6266SJeff Kirsher 	gma_write16(hw, port, GM_SMI_CTRL,
155527a6266SJeff Kirsher 		    GM_SMI_CT_PHY_AD(PHY_ADDR_MARV) | GM_SMI_CT_REG_AD(reg));
156527a6266SJeff Kirsher 
157527a6266SJeff Kirsher 	for (i = 0; i < PHY_RETRIES; i++) {
158527a6266SJeff Kirsher 		u16 ctrl = gma_read16(hw, port, GM_SMI_CTRL);
159527a6266SJeff Kirsher 		if (ctrl == 0xffff)
160527a6266SJeff Kirsher 			goto io_error;
161527a6266SJeff Kirsher 
162527a6266SJeff Kirsher 		if (!(ctrl & GM_SMI_CT_BUSY))
163527a6266SJeff Kirsher 			return 0;
164527a6266SJeff Kirsher 
165527a6266SJeff Kirsher 		udelay(10);
166527a6266SJeff Kirsher 	}
167527a6266SJeff Kirsher 
168527a6266SJeff Kirsher 	dev_warn(&hw->pdev->dev, "%s: phy write timeout\n", hw->dev[port]->name);
169527a6266SJeff Kirsher 	return -ETIMEDOUT;
170527a6266SJeff Kirsher 
171527a6266SJeff Kirsher io_error:
172527a6266SJeff Kirsher 	dev_err(&hw->pdev->dev, "%s: phy I/O error\n", hw->dev[port]->name);
173527a6266SJeff Kirsher 	return -EIO;
174527a6266SJeff Kirsher }
175527a6266SJeff Kirsher 
__gm_phy_read(struct sky2_hw * hw,unsigned port,u16 reg,u16 * val)176527a6266SJeff Kirsher static int __gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg, u16 *val)
177527a6266SJeff Kirsher {
178527a6266SJeff Kirsher 	int i;
179527a6266SJeff Kirsher 
180527a6266SJeff Kirsher 	gma_write16(hw, port, GM_SMI_CTRL, GM_SMI_CT_PHY_AD(PHY_ADDR_MARV)
181527a6266SJeff Kirsher 		    | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
182527a6266SJeff Kirsher 
183527a6266SJeff Kirsher 	for (i = 0; i < PHY_RETRIES; i++) {
184527a6266SJeff Kirsher 		u16 ctrl = gma_read16(hw, port, GM_SMI_CTRL);
185527a6266SJeff Kirsher 		if (ctrl == 0xffff)
186527a6266SJeff Kirsher 			goto io_error;
187527a6266SJeff Kirsher 
188527a6266SJeff Kirsher 		if (ctrl & GM_SMI_CT_RD_VAL) {
189527a6266SJeff Kirsher 			*val = gma_read16(hw, port, GM_SMI_DATA);
190527a6266SJeff Kirsher 			return 0;
191527a6266SJeff Kirsher 		}
192527a6266SJeff Kirsher 
193527a6266SJeff Kirsher 		udelay(10);
194527a6266SJeff Kirsher 	}
195527a6266SJeff Kirsher 
196527a6266SJeff Kirsher 	dev_warn(&hw->pdev->dev, "%s: phy read timeout\n", hw->dev[port]->name);
197527a6266SJeff Kirsher 	return -ETIMEDOUT;
198527a6266SJeff Kirsher io_error:
199527a6266SJeff Kirsher 	dev_err(&hw->pdev->dev, "%s: phy I/O error\n", hw->dev[port]->name);
200527a6266SJeff Kirsher 	return -EIO;
201527a6266SJeff Kirsher }
202527a6266SJeff Kirsher 
gm_phy_read(struct sky2_hw * hw,unsigned port,u16 reg)203527a6266SJeff Kirsher static inline u16 gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg)
204527a6266SJeff Kirsher {
20528b18e4eSTom Rix 	u16 v = 0;
206527a6266SJeff Kirsher 	__gm_phy_read(hw, port, reg, &v);
207527a6266SJeff Kirsher 	return v;
208527a6266SJeff Kirsher }
209527a6266SJeff Kirsher 
210527a6266SJeff Kirsher 
sky2_power_on(struct sky2_hw * hw)211527a6266SJeff Kirsher static void sky2_power_on(struct sky2_hw *hw)
212527a6266SJeff Kirsher {
213527a6266SJeff Kirsher 	/* switch power to VCC (WA for VAUX problem) */
214527a6266SJeff Kirsher 	sky2_write8(hw, B0_POWER_CTRL,
215527a6266SJeff Kirsher 		    PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
216527a6266SJeff Kirsher 
217527a6266SJeff Kirsher 	/* disable Core Clock Division, */
218527a6266SJeff Kirsher 	sky2_write32(hw, B2_Y2_CLK_CTRL, Y2_CLK_DIV_DIS);
219527a6266SJeff Kirsher 
220527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > CHIP_REV_YU_XL_A1)
221527a6266SJeff Kirsher 		/* enable bits are inverted */
222527a6266SJeff Kirsher 		sky2_write8(hw, B2_Y2_CLK_GATE,
223527a6266SJeff Kirsher 			    Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
224527a6266SJeff Kirsher 			    Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
225527a6266SJeff Kirsher 			    Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
226527a6266SJeff Kirsher 	else
227527a6266SJeff Kirsher 		sky2_write8(hw, B2_Y2_CLK_GATE, 0);
228527a6266SJeff Kirsher 
229527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_ADV_POWER_CTL) {
230527a6266SJeff Kirsher 		u32 reg;
231527a6266SJeff Kirsher 
232527a6266SJeff Kirsher 		sky2_pci_write32(hw, PCI_DEV_REG3, 0);
233527a6266SJeff Kirsher 
234527a6266SJeff Kirsher 		reg = sky2_pci_read32(hw, PCI_DEV_REG4);
235527a6266SJeff Kirsher 		/* set all bits to 0 except bits 15..12 and 8 */
236527a6266SJeff Kirsher 		reg &= P_ASPM_CONTROL_MSK;
237527a6266SJeff Kirsher 		sky2_pci_write32(hw, PCI_DEV_REG4, reg);
238527a6266SJeff Kirsher 
239527a6266SJeff Kirsher 		reg = sky2_pci_read32(hw, PCI_DEV_REG5);
240527a6266SJeff Kirsher 		/* set all bits to 0 except bits 28 & 27 */
241527a6266SJeff Kirsher 		reg &= P_CTL_TIM_VMAIN_AV_MSK;
242527a6266SJeff Kirsher 		sky2_pci_write32(hw, PCI_DEV_REG5, reg);
243527a6266SJeff Kirsher 
244527a6266SJeff Kirsher 		sky2_pci_write32(hw, PCI_CFG_REG_1, 0);
245527a6266SJeff Kirsher 
246527a6266SJeff Kirsher 		sky2_write16(hw, B0_CTST, Y2_HW_WOL_ON);
247527a6266SJeff Kirsher 
248527a6266SJeff Kirsher 		/* Enable workaround for dev 4.107 on Yukon-Ultra & Extreme */
249527a6266SJeff Kirsher 		reg = sky2_read32(hw, B2_GP_IO);
250527a6266SJeff Kirsher 		reg |= GLB_GPIO_STAT_RACE_DIS;
251527a6266SJeff Kirsher 		sky2_write32(hw, B2_GP_IO, reg);
252527a6266SJeff Kirsher 
253527a6266SJeff Kirsher 		sky2_read32(hw, B2_GP_IO);
254527a6266SJeff Kirsher 	}
255527a6266SJeff Kirsher 
256527a6266SJeff Kirsher 	/* Turn on "driver loaded" LED */
257527a6266SJeff Kirsher 	sky2_write16(hw, B0_CTST, Y2_LED_STAT_ON);
258527a6266SJeff Kirsher }
259527a6266SJeff Kirsher 
sky2_power_aux(struct sky2_hw * hw)260527a6266SJeff Kirsher static void sky2_power_aux(struct sky2_hw *hw)
261527a6266SJeff Kirsher {
262527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > CHIP_REV_YU_XL_A1)
263527a6266SJeff Kirsher 		sky2_write8(hw, B2_Y2_CLK_GATE, 0);
264527a6266SJeff Kirsher 	else
265527a6266SJeff Kirsher 		/* enable bits are inverted */
266527a6266SJeff Kirsher 		sky2_write8(hw, B2_Y2_CLK_GATE,
267527a6266SJeff Kirsher 			    Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
268527a6266SJeff Kirsher 			    Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
269527a6266SJeff Kirsher 			    Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
270527a6266SJeff Kirsher 
271527a6266SJeff Kirsher 	/* switch power to VAUX if supported and PME from D3cold */
272527a6266SJeff Kirsher 	if ( (sky2_read32(hw, B0_CTST) & Y2_VAUX_AVAIL) &&
273527a6266SJeff Kirsher 	     pci_pme_capable(hw->pdev, PCI_D3cold))
274527a6266SJeff Kirsher 		sky2_write8(hw, B0_POWER_CTRL,
275527a6266SJeff Kirsher 			    (PC_VAUX_ENA | PC_VCC_ENA |
276527a6266SJeff Kirsher 			     PC_VAUX_ON | PC_VCC_OFF));
277527a6266SJeff Kirsher 
278527a6266SJeff Kirsher 	/* turn off "driver loaded LED" */
279527a6266SJeff Kirsher 	sky2_write16(hw, B0_CTST, Y2_LED_STAT_OFF);
280527a6266SJeff Kirsher }
281527a6266SJeff Kirsher 
sky2_gmac_reset(struct sky2_hw * hw,unsigned port)282527a6266SJeff Kirsher static void sky2_gmac_reset(struct sky2_hw *hw, unsigned port)
283527a6266SJeff Kirsher {
284527a6266SJeff Kirsher 	u16 reg;
285527a6266SJeff Kirsher 
286527a6266SJeff Kirsher 	/* disable all GMAC IRQ's */
287527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
288527a6266SJeff Kirsher 
289527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H1, 0);	/* clear MC hash */
290527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H2, 0);
291527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H3, 0);
292527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H4, 0);
293527a6266SJeff Kirsher 
294527a6266SJeff Kirsher 	reg = gma_read16(hw, port, GM_RX_CTRL);
295527a6266SJeff Kirsher 	reg |= GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA;
296527a6266SJeff Kirsher 	gma_write16(hw, port, GM_RX_CTRL, reg);
297527a6266SJeff Kirsher }
298527a6266SJeff Kirsher 
299527a6266SJeff Kirsher /* flow control to advertise bits */
300527a6266SJeff Kirsher static const u16 copper_fc_adv[] = {
301527a6266SJeff Kirsher 	[FC_NONE]	= 0,
302527a6266SJeff Kirsher 	[FC_TX]		= PHY_M_AN_ASP,
303527a6266SJeff Kirsher 	[FC_RX]		= PHY_M_AN_PC,
304527a6266SJeff Kirsher 	[FC_BOTH]	= PHY_M_AN_PC | PHY_M_AN_ASP,
305527a6266SJeff Kirsher };
306527a6266SJeff Kirsher 
307527a6266SJeff Kirsher /* flow control to advertise bits when using 1000BaseX */
308527a6266SJeff Kirsher static const u16 fiber_fc_adv[] = {
309527a6266SJeff Kirsher 	[FC_NONE] = PHY_M_P_NO_PAUSE_X,
310527a6266SJeff Kirsher 	[FC_TX]   = PHY_M_P_ASYM_MD_X,
311527a6266SJeff Kirsher 	[FC_RX]	  = PHY_M_P_SYM_MD_X,
312527a6266SJeff Kirsher 	[FC_BOTH] = PHY_M_P_BOTH_MD_X,
313527a6266SJeff Kirsher };
314527a6266SJeff Kirsher 
315527a6266SJeff Kirsher /* flow control to GMA disable bits */
316527a6266SJeff Kirsher static const u16 gm_fc_disable[] = {
317527a6266SJeff Kirsher 	[FC_NONE] = GM_GPCR_FC_RX_DIS | GM_GPCR_FC_TX_DIS,
318527a6266SJeff Kirsher 	[FC_TX]	  = GM_GPCR_FC_RX_DIS,
319527a6266SJeff Kirsher 	[FC_RX]	  = GM_GPCR_FC_TX_DIS,
320527a6266SJeff Kirsher 	[FC_BOTH] = 0,
321527a6266SJeff Kirsher };
322527a6266SJeff Kirsher 
323527a6266SJeff Kirsher 
sky2_phy_init(struct sky2_hw * hw,unsigned port)324527a6266SJeff Kirsher static void sky2_phy_init(struct sky2_hw *hw, unsigned port)
325527a6266SJeff Kirsher {
326527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
327527a6266SJeff Kirsher 	u16 ctrl, ct1000, adv, pg, ledctrl, ledover, reg;
328527a6266SJeff Kirsher 
329527a6266SJeff Kirsher 	if ( (sky2->flags & SKY2_FLAG_AUTO_SPEED) &&
330527a6266SJeff Kirsher 	    !(hw->flags & SKY2_HW_NEWER_PHY)) {
331527a6266SJeff Kirsher 		u16 ectrl = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
332527a6266SJeff Kirsher 
333527a6266SJeff Kirsher 		ectrl &= ~(PHY_M_EC_M_DSC_MSK | PHY_M_EC_S_DSC_MSK |
334527a6266SJeff Kirsher 			   PHY_M_EC_MAC_S_MSK);
335527a6266SJeff Kirsher 		ectrl |= PHY_M_EC_MAC_S(MAC_TX_CLK_25_MHZ);
336527a6266SJeff Kirsher 
337527a6266SJeff Kirsher 		/* on PHY 88E1040 Rev.D0 (and newer) downshift control changed */
338527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_EC)
339527a6266SJeff Kirsher 			/* set downshift counter to 3x and enable downshift */
340527a6266SJeff Kirsher 			ectrl |= PHY_M_EC_DSC_2(2) | PHY_M_EC_DOWN_S_ENA;
341527a6266SJeff Kirsher 		else
342527a6266SJeff Kirsher 			/* set master & slave downshift counter to 1x */
343527a6266SJeff Kirsher 			ectrl |= PHY_M_EC_M_DSC(0) | PHY_M_EC_S_DSC(1);
344527a6266SJeff Kirsher 
345527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_CTRL, ectrl);
346527a6266SJeff Kirsher 	}
347527a6266SJeff Kirsher 
348527a6266SJeff Kirsher 	ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
349527a6266SJeff Kirsher 	if (sky2_is_copper(hw)) {
350527a6266SJeff Kirsher 		if (!(hw->flags & SKY2_HW_GIGABIT)) {
351527a6266SJeff Kirsher 			/* enable automatic crossover */
352527a6266SJeff Kirsher 			ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO) >> 1;
353527a6266SJeff Kirsher 
354527a6266SJeff Kirsher 			if (hw->chip_id == CHIP_ID_YUKON_FE_P &&
355527a6266SJeff Kirsher 			    hw->chip_rev == CHIP_REV_YU_FE2_A0) {
356527a6266SJeff Kirsher 				u16 spec;
357527a6266SJeff Kirsher 
358527a6266SJeff Kirsher 				/* Enable Class A driver for FE+ A0 */
359527a6266SJeff Kirsher 				spec = gm_phy_read(hw, port, PHY_MARV_FE_SPEC_2);
360527a6266SJeff Kirsher 				spec |= PHY_M_FESC_SEL_CL_A;
361527a6266SJeff Kirsher 				gm_phy_write(hw, port, PHY_MARV_FE_SPEC_2, spec);
362527a6266SJeff Kirsher 			}
363527a6266SJeff Kirsher 		} else {
364527a6266SJeff Kirsher 			/* disable energy detect */
365527a6266SJeff Kirsher 			ctrl &= ~PHY_M_PC_EN_DET_MSK;
366527a6266SJeff Kirsher 
367527a6266SJeff Kirsher 			/* enable automatic crossover */
368527a6266SJeff Kirsher 			ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO);
369527a6266SJeff Kirsher 
370527a6266SJeff Kirsher 			/* downshift on PHY 88E1112 and 88E1149 is changed */
371527a6266SJeff Kirsher 			if ( (sky2->flags & SKY2_FLAG_AUTO_SPEED) &&
372527a6266SJeff Kirsher 			     (hw->flags & SKY2_HW_NEWER_PHY)) {
373527a6266SJeff Kirsher 				/* set downshift counter to 3x and enable downshift */
374527a6266SJeff Kirsher 				ctrl &= ~PHY_M_PC_DSC_MSK;
375527a6266SJeff Kirsher 				ctrl |= PHY_M_PC_DSC(2) | PHY_M_PC_DOWN_S_ENA;
376527a6266SJeff Kirsher 			}
377527a6266SJeff Kirsher 		}
378527a6266SJeff Kirsher 	} else {
379527a6266SJeff Kirsher 		/* workaround for deviation #4.88 (CRC errors) */
380527a6266SJeff Kirsher 		/* disable Automatic Crossover */
381527a6266SJeff Kirsher 
382527a6266SJeff Kirsher 		ctrl &= ~PHY_M_PC_MDIX_MSK;
383527a6266SJeff Kirsher 	}
384527a6266SJeff Kirsher 
385527a6266SJeff Kirsher 	gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
386527a6266SJeff Kirsher 
387527a6266SJeff Kirsher 	/* special setup for PHY 88E1112 Fiber */
388527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL && (hw->flags & SKY2_HW_FIBRE_PHY)) {
389527a6266SJeff Kirsher 		pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
390527a6266SJeff Kirsher 
391527a6266SJeff Kirsher 		/* Fiber: select 1000BASE-X only mode MAC Specific Ctrl Reg. */
392527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 2);
393527a6266SJeff Kirsher 		ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
394527a6266SJeff Kirsher 		ctrl &= ~PHY_M_MAC_MD_MSK;
395527a6266SJeff Kirsher 		ctrl |= PHY_M_MAC_MODE_SEL(PHY_M_MAC_MD_1000BX);
396527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
397527a6266SJeff Kirsher 
398527a6266SJeff Kirsher 		if (hw->pmd_type  == 'P') {
399527a6266SJeff Kirsher 			/* select page 1 to access Fiber registers */
400527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 1);
401527a6266SJeff Kirsher 
402527a6266SJeff Kirsher 			/* for SFP-module set SIGDET polarity to low */
403527a6266SJeff Kirsher 			ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
404527a6266SJeff Kirsher 			ctrl |= PHY_M_FIB_SIGD_POL;
405527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
406527a6266SJeff Kirsher 		}
407527a6266SJeff Kirsher 
408527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
409527a6266SJeff Kirsher 	}
410527a6266SJeff Kirsher 
411527a6266SJeff Kirsher 	ctrl = PHY_CT_RESET;
412527a6266SJeff Kirsher 	ct1000 = 0;
413527a6266SJeff Kirsher 	adv = PHY_AN_CSMA;
414527a6266SJeff Kirsher 	reg = 0;
415527a6266SJeff Kirsher 
416527a6266SJeff Kirsher 	if (sky2->flags & SKY2_FLAG_AUTO_SPEED) {
417527a6266SJeff Kirsher 		if (sky2_is_copper(hw)) {
418527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_1000baseT_Full)
419527a6266SJeff Kirsher 				ct1000 |= PHY_M_1000C_AFD;
420527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_1000baseT_Half)
421527a6266SJeff Kirsher 				ct1000 |= PHY_M_1000C_AHD;
422527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_100baseT_Full)
423527a6266SJeff Kirsher 				adv |= PHY_M_AN_100_FD;
424527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_100baseT_Half)
425527a6266SJeff Kirsher 				adv |= PHY_M_AN_100_HD;
426527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_10baseT_Full)
427527a6266SJeff Kirsher 				adv |= PHY_M_AN_10_FD;
428527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_10baseT_Half)
429527a6266SJeff Kirsher 				adv |= PHY_M_AN_10_HD;
430527a6266SJeff Kirsher 
431527a6266SJeff Kirsher 		} else {	/* special defines for FIBER (88E1040S only) */
432527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_1000baseT_Full)
433527a6266SJeff Kirsher 				adv |= PHY_M_AN_1000X_AFD;
434527a6266SJeff Kirsher 			if (sky2->advertising & ADVERTISED_1000baseT_Half)
435527a6266SJeff Kirsher 				adv |= PHY_M_AN_1000X_AHD;
436527a6266SJeff Kirsher 		}
437527a6266SJeff Kirsher 
438527a6266SJeff Kirsher 		/* Restart Auto-negotiation */
439527a6266SJeff Kirsher 		ctrl |= PHY_CT_ANE | PHY_CT_RE_CFG;
440527a6266SJeff Kirsher 	} else {
441527a6266SJeff Kirsher 		/* forced speed/duplex settings */
442527a6266SJeff Kirsher 		ct1000 = PHY_M_1000C_MSE;
443527a6266SJeff Kirsher 
444527a6266SJeff Kirsher 		/* Disable auto update for duplex flow control and duplex */
445527a6266SJeff Kirsher 		reg |= GM_GPCR_AU_DUP_DIS | GM_GPCR_AU_SPD_DIS;
446527a6266SJeff Kirsher 
447527a6266SJeff Kirsher 		switch (sky2->speed) {
448527a6266SJeff Kirsher 		case SPEED_1000:
449527a6266SJeff Kirsher 			ctrl |= PHY_CT_SP1000;
450527a6266SJeff Kirsher 			reg |= GM_GPCR_SPEED_1000;
451527a6266SJeff Kirsher 			break;
452527a6266SJeff Kirsher 		case SPEED_100:
453527a6266SJeff Kirsher 			ctrl |= PHY_CT_SP100;
454527a6266SJeff Kirsher 			reg |= GM_GPCR_SPEED_100;
455527a6266SJeff Kirsher 			break;
456527a6266SJeff Kirsher 		}
457527a6266SJeff Kirsher 
458527a6266SJeff Kirsher 		if (sky2->duplex == DUPLEX_FULL) {
459527a6266SJeff Kirsher 			reg |= GM_GPCR_DUP_FULL;
460527a6266SJeff Kirsher 			ctrl |= PHY_CT_DUP_MD;
461527a6266SJeff Kirsher 		} else if (sky2->speed < SPEED_1000)
462527a6266SJeff Kirsher 			sky2->flow_mode = FC_NONE;
463527a6266SJeff Kirsher 	}
464527a6266SJeff Kirsher 
465527a6266SJeff Kirsher 	if (sky2->flags & SKY2_FLAG_AUTO_PAUSE) {
466527a6266SJeff Kirsher 		if (sky2_is_copper(hw))
467527a6266SJeff Kirsher 			adv |= copper_fc_adv[sky2->flow_mode];
468527a6266SJeff Kirsher 		else
469527a6266SJeff Kirsher 			adv |= fiber_fc_adv[sky2->flow_mode];
470527a6266SJeff Kirsher 	} else {
471527a6266SJeff Kirsher 		reg |= GM_GPCR_AU_FCT_DIS;
472527a6266SJeff Kirsher 		reg |= gm_fc_disable[sky2->flow_mode];
473527a6266SJeff Kirsher 
474527a6266SJeff Kirsher 		/* Forward pause packets to GMAC? */
475527a6266SJeff Kirsher 		if (sky2->flow_mode & FC_RX)
476527a6266SJeff Kirsher 			sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
477527a6266SJeff Kirsher 		else
478527a6266SJeff Kirsher 			sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
479527a6266SJeff Kirsher 	}
480527a6266SJeff Kirsher 
481527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL, reg);
482527a6266SJeff Kirsher 
483527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_GIGABIT)
484527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, ct1000);
485527a6266SJeff Kirsher 
486527a6266SJeff Kirsher 	gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, adv);
487527a6266SJeff Kirsher 	gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
488527a6266SJeff Kirsher 
489527a6266SJeff Kirsher 	/* Setup Phy LED's */
490527a6266SJeff Kirsher 	ledctrl = PHY_M_LED_PULS_DUR(PULS_170MS);
491527a6266SJeff Kirsher 	ledover = 0;
492527a6266SJeff Kirsher 
493527a6266SJeff Kirsher 	switch (hw->chip_id) {
494527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE:
495527a6266SJeff Kirsher 		/* on 88E3082 these bits are at 11..9 (shifted left) */
496527a6266SJeff Kirsher 		ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) << 1;
497527a6266SJeff Kirsher 
498527a6266SJeff Kirsher 		ctrl = gm_phy_read(hw, port, PHY_MARV_FE_LED_PAR);
499527a6266SJeff Kirsher 
500527a6266SJeff Kirsher 		/* delete ACT LED control bits */
501527a6266SJeff Kirsher 		ctrl &= ~PHY_M_FELP_LED1_MSK;
502527a6266SJeff Kirsher 		/* change ACT LED control to blink mode */
503527a6266SJeff Kirsher 		ctrl |= PHY_M_FELP_LED1_CTRL(LED_PAR_CTRL_ACT_BL);
504527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_FE_LED_PAR, ctrl);
505527a6266SJeff Kirsher 		break;
506527a6266SJeff Kirsher 
507527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE_P:
508527a6266SJeff Kirsher 		/* Enable Link Partner Next Page */
509527a6266SJeff Kirsher 		ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
510527a6266SJeff Kirsher 		ctrl |= PHY_M_PC_ENA_LIP_NP;
511527a6266SJeff Kirsher 
512527a6266SJeff Kirsher 		/* disable Energy Detect and enable scrambler */
513527a6266SJeff Kirsher 		ctrl &= ~(PHY_M_PC_ENA_ENE_DT | PHY_M_PC_DIS_SCRAMB);
514527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
515527a6266SJeff Kirsher 
516527a6266SJeff Kirsher 		/* set LED2 -> ACT, LED1 -> LINK, LED0 -> SPEED */
517527a6266SJeff Kirsher 		ctrl = PHY_M_FELP_LED2_CTRL(LED_PAR_CTRL_ACT_BL) |
518527a6266SJeff Kirsher 			PHY_M_FELP_LED1_CTRL(LED_PAR_CTRL_LINK) |
519527a6266SJeff Kirsher 			PHY_M_FELP_LED0_CTRL(LED_PAR_CTRL_SPEED);
520527a6266SJeff Kirsher 
521527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_FE_LED_PAR, ctrl);
522527a6266SJeff Kirsher 		break;
523527a6266SJeff Kirsher 
524527a6266SJeff Kirsher 	case CHIP_ID_YUKON_XL:
525527a6266SJeff Kirsher 		pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
526527a6266SJeff Kirsher 
527527a6266SJeff Kirsher 		/* select page 3 to access LED control register */
528527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
529527a6266SJeff Kirsher 
530527a6266SJeff Kirsher 		/* set LED Function Control register */
531527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
532527a6266SJeff Kirsher 			     (PHY_M_LEDC_LOS_CTRL(1) |	/* LINK/ACT */
533527a6266SJeff Kirsher 			      PHY_M_LEDC_INIT_CTRL(7) |	/* 10 Mbps */
534527a6266SJeff Kirsher 			      PHY_M_LEDC_STA1_CTRL(7) |	/* 100 Mbps */
535527a6266SJeff Kirsher 			      PHY_M_LEDC_STA0_CTRL(7)));	/* 1000 Mbps */
536527a6266SJeff Kirsher 
537527a6266SJeff Kirsher 		/* set Polarity Control register */
538527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_STAT,
539527a6266SJeff Kirsher 			     (PHY_M_POLC_LS1_P_MIX(4) |
540527a6266SJeff Kirsher 			      PHY_M_POLC_IS0_P_MIX(4) |
541527a6266SJeff Kirsher 			      PHY_M_POLC_LOS_CTRL(2) |
542527a6266SJeff Kirsher 			      PHY_M_POLC_INIT_CTRL(2) |
543527a6266SJeff Kirsher 			      PHY_M_POLC_STA1_CTRL(2) |
544527a6266SJeff Kirsher 			      PHY_M_POLC_STA0_CTRL(2)));
545527a6266SJeff Kirsher 
546527a6266SJeff Kirsher 		/* restore page register */
547527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
548527a6266SJeff Kirsher 		break;
549527a6266SJeff Kirsher 
550527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EC_U:
551527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EX:
552527a6266SJeff Kirsher 	case CHIP_ID_YUKON_SUPR:
553527a6266SJeff Kirsher 		pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
554527a6266SJeff Kirsher 
555527a6266SJeff Kirsher 		/* select page 3 to access LED control register */
556527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
557527a6266SJeff Kirsher 
558527a6266SJeff Kirsher 		/* set LED Function Control register */
559527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
560527a6266SJeff Kirsher 			     (PHY_M_LEDC_LOS_CTRL(1) |	/* LINK/ACT */
561527a6266SJeff Kirsher 			      PHY_M_LEDC_INIT_CTRL(8) |	/* 10 Mbps */
562527a6266SJeff Kirsher 			      PHY_M_LEDC_STA1_CTRL(7) |	/* 100 Mbps */
563527a6266SJeff Kirsher 			      PHY_M_LEDC_STA0_CTRL(7)));/* 1000 Mbps */
564527a6266SJeff Kirsher 
565527a6266SJeff Kirsher 		/* set Blink Rate in LED Timer Control Register */
566527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_INT_MASK,
567527a6266SJeff Kirsher 			     ledctrl | PHY_M_LED_BLINK_RT(BLINK_84MS));
568527a6266SJeff Kirsher 		/* restore page register */
569527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
570527a6266SJeff Kirsher 		break;
571527a6266SJeff Kirsher 
572527a6266SJeff Kirsher 	default:
573527a6266SJeff Kirsher 		/* set Tx LED (LED_TX) to blink mode on Rx OR Tx activity */
574527a6266SJeff Kirsher 		ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) | PHY_M_LEDC_TX_CTRL;
575527a6266SJeff Kirsher 
576527a6266SJeff Kirsher 		/* turn off the Rx LED (LED_RX) */
577527a6266SJeff Kirsher 		ledover |= PHY_M_LED_MO_RX(MO_LED_OFF);
578527a6266SJeff Kirsher 	}
579527a6266SJeff Kirsher 
580527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_UL_2) {
581527a6266SJeff Kirsher 		/* apply fixes in PHY AFE */
582527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 255);
583527a6266SJeff Kirsher 
584527a6266SJeff Kirsher 		/* increase differential signal amplitude in 10BASE-T */
585527a6266SJeff Kirsher 		gm_phy_write(hw, port, 0x18, 0xaa99);
586527a6266SJeff Kirsher 		gm_phy_write(hw, port, 0x17, 0x2011);
587527a6266SJeff Kirsher 
588527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_EC_U) {
589527a6266SJeff Kirsher 			/* fix for IEEE A/B Symmetry failure in 1000BASE-T */
590527a6266SJeff Kirsher 			gm_phy_write(hw, port, 0x18, 0xa204);
591527a6266SJeff Kirsher 			gm_phy_write(hw, port, 0x17, 0x2002);
592527a6266SJeff Kirsher 		}
593527a6266SJeff Kirsher 
594527a6266SJeff Kirsher 		/* set page register to 0 */
595527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
596527a6266SJeff Kirsher 	} else if (hw->chip_id == CHIP_ID_YUKON_FE_P &&
597527a6266SJeff Kirsher 		   hw->chip_rev == CHIP_REV_YU_FE2_A0) {
598527a6266SJeff Kirsher 		/* apply workaround for integrated resistors calibration */
599527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PAGE_ADDR, 17);
600527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PAGE_DATA, 0x3f60);
601527a6266SJeff Kirsher 	} else if (hw->chip_id == CHIP_ID_YUKON_OPT && hw->chip_rev == 0) {
602527a6266SJeff Kirsher 		/* apply fixes in PHY AFE */
603527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0x00ff);
604527a6266SJeff Kirsher 
605527a6266SJeff Kirsher 		/* apply RDAC termination workaround */
606527a6266SJeff Kirsher 		gm_phy_write(hw, port, 24, 0x2800);
607527a6266SJeff Kirsher 		gm_phy_write(hw, port, 23, 0x2001);
608527a6266SJeff Kirsher 
609527a6266SJeff Kirsher 		/* set page register back to 0 */
610527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
611527a6266SJeff Kirsher 	} else if (hw->chip_id != CHIP_ID_YUKON_EX &&
612527a6266SJeff Kirsher 		   hw->chip_id < CHIP_ID_YUKON_SUPR) {
613527a6266SJeff Kirsher 		/* no effect on Yukon-XL */
614527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_LED_CTRL, ledctrl);
615527a6266SJeff Kirsher 
616527a6266SJeff Kirsher 		if (!(sky2->flags & SKY2_FLAG_AUTO_SPEED) ||
617527a6266SJeff Kirsher 		    sky2->speed == SPEED_100) {
618527a6266SJeff Kirsher 			/* turn on 100 Mbps LED (LED_LINK100) */
619527a6266SJeff Kirsher 			ledover |= PHY_M_LED_MO_100(MO_LED_ON);
620527a6266SJeff Kirsher 		}
621527a6266SJeff Kirsher 
622527a6266SJeff Kirsher 		if (ledover)
623527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_LED_OVER, ledover);
624527a6266SJeff Kirsher 
625527a6266SJeff Kirsher 	} else if (hw->chip_id == CHIP_ID_YUKON_PRM &&
626527a6266SJeff Kirsher 		   (sky2_read8(hw, B2_MAC_CFG) & 0xf) == 0x7) {
627527a6266SJeff Kirsher 		int i;
628527a6266SJeff Kirsher 		/* This a phy register setup workaround copied from vendor driver. */
629527a6266SJeff Kirsher 		static const struct {
630527a6266SJeff Kirsher 			u16 reg, val;
631527a6266SJeff Kirsher 		} eee_afe[] = {
632527a6266SJeff Kirsher 			{ 0x156, 0x58ce },
633527a6266SJeff Kirsher 			{ 0x153, 0x99eb },
634527a6266SJeff Kirsher 			{ 0x141, 0x8064 },
635527a6266SJeff Kirsher 			/* { 0x155, 0x130b },*/
636527a6266SJeff Kirsher 			{ 0x000, 0x0000 },
637527a6266SJeff Kirsher 			{ 0x151, 0x8433 },
638527a6266SJeff Kirsher 			{ 0x14b, 0x8c44 },
639527a6266SJeff Kirsher 			{ 0x14c, 0x0f90 },
640527a6266SJeff Kirsher 			{ 0x14f, 0x39aa },
641527a6266SJeff Kirsher 			/* { 0x154, 0x2f39 },*/
642527a6266SJeff Kirsher 			{ 0x14d, 0xba33 },
643527a6266SJeff Kirsher 			{ 0x144, 0x0048 },
644527a6266SJeff Kirsher 			{ 0x152, 0x2010 },
645527a6266SJeff Kirsher 			/* { 0x158, 0x1223 },*/
646527a6266SJeff Kirsher 			{ 0x140, 0x4444 },
647527a6266SJeff Kirsher 			{ 0x154, 0x2f3b },
648527a6266SJeff Kirsher 			{ 0x158, 0xb203 },
649527a6266SJeff Kirsher 			{ 0x157, 0x2029 },
650527a6266SJeff Kirsher 		};
651527a6266SJeff Kirsher 
652527a6266SJeff Kirsher 		/* Start Workaround for OptimaEEE Rev.Z0 */
653527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0x00fb);
654527a6266SJeff Kirsher 
655527a6266SJeff Kirsher 		gm_phy_write(hw, port,  1, 0x4099);
656527a6266SJeff Kirsher 		gm_phy_write(hw, port,  3, 0x1120);
657527a6266SJeff Kirsher 		gm_phy_write(hw, port, 11, 0x113c);
658527a6266SJeff Kirsher 		gm_phy_write(hw, port, 14, 0x8100);
659527a6266SJeff Kirsher 		gm_phy_write(hw, port, 15, 0x112a);
660527a6266SJeff Kirsher 		gm_phy_write(hw, port, 17, 0x1008);
661527a6266SJeff Kirsher 
662527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0x00fc);
663527a6266SJeff Kirsher 		gm_phy_write(hw, port,  1, 0x20b0);
664527a6266SJeff Kirsher 
665527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0x00ff);
666527a6266SJeff Kirsher 
667527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(eee_afe); i++) {
668527a6266SJeff Kirsher 			/* apply AFE settings */
669527a6266SJeff Kirsher 			gm_phy_write(hw, port, 17, eee_afe[i].val);
670527a6266SJeff Kirsher 			gm_phy_write(hw, port, 16, eee_afe[i].reg | 1u<<13);
671527a6266SJeff Kirsher 		}
672527a6266SJeff Kirsher 
673527a6266SJeff Kirsher 		/* End Workaround for OptimaEEE */
674527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
675527a6266SJeff Kirsher 
676527a6266SJeff Kirsher 		/* Enable 10Base-Te (EEE) */
677527a6266SJeff Kirsher 		if (hw->chip_id >= CHIP_ID_YUKON_PRM) {
678527a6266SJeff Kirsher 			reg = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
679527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_EXT_CTRL,
680527a6266SJeff Kirsher 				     reg | PHY_M_10B_TE_ENABLE);
681527a6266SJeff Kirsher 		}
682527a6266SJeff Kirsher 	}
683527a6266SJeff Kirsher 
684527a6266SJeff Kirsher 	/* Enable phy interrupt on auto-negotiation complete (or link up) */
685527a6266SJeff Kirsher 	if (sky2->flags & SKY2_FLAG_AUTO_SPEED)
686527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_AN_COMPL);
687527a6266SJeff Kirsher 	else
688527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
689527a6266SJeff Kirsher }
690527a6266SJeff Kirsher 
691527a6266SJeff Kirsher static const u32 phy_power[] = { PCI_Y2_PHY1_POWD, PCI_Y2_PHY2_POWD };
692527a6266SJeff Kirsher static const u32 coma_mode[] = { PCI_Y2_PHY1_COMA, PCI_Y2_PHY2_COMA };
693527a6266SJeff Kirsher 
sky2_phy_power_up(struct sky2_hw * hw,unsigned port)694527a6266SJeff Kirsher static void sky2_phy_power_up(struct sky2_hw *hw, unsigned port)
695527a6266SJeff Kirsher {
696527a6266SJeff Kirsher 	u32 reg1;
697527a6266SJeff Kirsher 
698527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
699527a6266SJeff Kirsher 	reg1 = sky2_pci_read32(hw, PCI_DEV_REG1);
700527a6266SJeff Kirsher 	reg1 &= ~phy_power[port];
701527a6266SJeff Kirsher 
702527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > CHIP_REV_YU_XL_A1)
703527a6266SJeff Kirsher 		reg1 |= coma_mode[port];
704527a6266SJeff Kirsher 
705527a6266SJeff Kirsher 	sky2_pci_write32(hw, PCI_DEV_REG1, reg1);
706527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
707527a6266SJeff Kirsher 	sky2_pci_read32(hw, PCI_DEV_REG1);
708527a6266SJeff Kirsher 
709527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_FE)
710527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_CTRL, PHY_CT_ANE);
711527a6266SJeff Kirsher 	else if (hw->flags & SKY2_HW_ADV_POWER_CTL)
712527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
713527a6266SJeff Kirsher }
714527a6266SJeff Kirsher 
sky2_phy_power_down(struct sky2_hw * hw,unsigned port)715527a6266SJeff Kirsher static void sky2_phy_power_down(struct sky2_hw *hw, unsigned port)
716527a6266SJeff Kirsher {
717527a6266SJeff Kirsher 	u32 reg1;
718527a6266SJeff Kirsher 	u16 ctrl;
719527a6266SJeff Kirsher 
720527a6266SJeff Kirsher 	/* release GPHY Control reset */
721527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
722527a6266SJeff Kirsher 
723527a6266SJeff Kirsher 	/* release GMAC reset */
724527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
725527a6266SJeff Kirsher 
726527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_NEWER_PHY) {
727527a6266SJeff Kirsher 		/* select page 2 to access MAC control register */
728527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 2);
729527a6266SJeff Kirsher 
730527a6266SJeff Kirsher 		ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
731527a6266SJeff Kirsher 		/* allow GMII Power Down */
732527a6266SJeff Kirsher 		ctrl &= ~PHY_M_MAC_GMIF_PUP;
733527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
734527a6266SJeff Kirsher 
735527a6266SJeff Kirsher 		/* set page register back to 0 */
736527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
737527a6266SJeff Kirsher 	}
738527a6266SJeff Kirsher 
739527a6266SJeff Kirsher 	/* setup General Purpose Control Register */
740527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL,
741527a6266SJeff Kirsher 		    GM_GPCR_FL_PASS | GM_GPCR_SPEED_100 |
742527a6266SJeff Kirsher 		    GM_GPCR_AU_DUP_DIS | GM_GPCR_AU_FCT_DIS |
743527a6266SJeff Kirsher 		    GM_GPCR_AU_SPD_DIS);
744527a6266SJeff Kirsher 
745527a6266SJeff Kirsher 	if (hw->chip_id != CHIP_ID_YUKON_EC) {
746527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_EC_U) {
747527a6266SJeff Kirsher 			/* select page 2 to access MAC control register */
748527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 2);
749527a6266SJeff Kirsher 
750527a6266SJeff Kirsher 			ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
751527a6266SJeff Kirsher 			/* enable Power Down */
752527a6266SJeff Kirsher 			ctrl |= PHY_M_PC_POW_D_ENA;
753527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
754527a6266SJeff Kirsher 
755527a6266SJeff Kirsher 			/* set page register back to 0 */
756527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
757527a6266SJeff Kirsher 		}
758527a6266SJeff Kirsher 
759527a6266SJeff Kirsher 		/* set IEEE compatible Power Down Mode (dev. #4.99) */
760527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_CTRL, PHY_CT_PDOWN);
761527a6266SJeff Kirsher 	}
762527a6266SJeff Kirsher 
763527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
764527a6266SJeff Kirsher 	reg1 = sky2_pci_read32(hw, PCI_DEV_REG1);
765527a6266SJeff Kirsher 	reg1 |= phy_power[port];		/* set PHY to PowerDown/COMA Mode */
766527a6266SJeff Kirsher 	sky2_pci_write32(hw, PCI_DEV_REG1, reg1);
767527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
768527a6266SJeff Kirsher }
769527a6266SJeff Kirsher 
770527a6266SJeff Kirsher /* configure IPG according to used link speed */
sky2_set_ipg(struct sky2_port * sky2)771527a6266SJeff Kirsher static void sky2_set_ipg(struct sky2_port *sky2)
772527a6266SJeff Kirsher {
773527a6266SJeff Kirsher 	u16 reg;
774527a6266SJeff Kirsher 
775527a6266SJeff Kirsher 	reg = gma_read16(sky2->hw, sky2->port, GM_SERIAL_MODE);
776527a6266SJeff Kirsher 	reg &= ~GM_SMOD_IPG_MSK;
777527a6266SJeff Kirsher 	if (sky2->speed > SPEED_100)
778527a6266SJeff Kirsher 		reg |= IPG_DATA_VAL(IPG_DATA_DEF_1000);
779527a6266SJeff Kirsher 	else
780527a6266SJeff Kirsher 		reg |= IPG_DATA_VAL(IPG_DATA_DEF_10_100);
781527a6266SJeff Kirsher 	gma_write16(sky2->hw, sky2->port, GM_SERIAL_MODE, reg);
782527a6266SJeff Kirsher }
783527a6266SJeff Kirsher 
784527a6266SJeff Kirsher /* Enable Rx/Tx */
sky2_enable_rx_tx(struct sky2_port * sky2)785527a6266SJeff Kirsher static void sky2_enable_rx_tx(struct sky2_port *sky2)
786527a6266SJeff Kirsher {
787527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
788527a6266SJeff Kirsher 	unsigned port = sky2->port;
789527a6266SJeff Kirsher 	u16 reg;
790527a6266SJeff Kirsher 
791527a6266SJeff Kirsher 	reg = gma_read16(hw, port, GM_GP_CTRL);
792527a6266SJeff Kirsher 	reg |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
793527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL, reg);
794527a6266SJeff Kirsher }
795527a6266SJeff Kirsher 
796527a6266SJeff Kirsher /* Force a renegotiation */
sky2_phy_reinit(struct sky2_port * sky2)797527a6266SJeff Kirsher static void sky2_phy_reinit(struct sky2_port *sky2)
798527a6266SJeff Kirsher {
799527a6266SJeff Kirsher 	spin_lock_bh(&sky2->phy_lock);
800527a6266SJeff Kirsher 	sky2_phy_init(sky2->hw, sky2->port);
801527a6266SJeff Kirsher 	sky2_enable_rx_tx(sky2);
802527a6266SJeff Kirsher 	spin_unlock_bh(&sky2->phy_lock);
803527a6266SJeff Kirsher }
804527a6266SJeff Kirsher 
805527a6266SJeff Kirsher /* Put device in state to listen for Wake On Lan */
sky2_wol_init(struct sky2_port * sky2)806527a6266SJeff Kirsher static void sky2_wol_init(struct sky2_port *sky2)
807527a6266SJeff Kirsher {
808527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
809527a6266SJeff Kirsher 	unsigned port = sky2->port;
810527a6266SJeff Kirsher 	enum flow_control save_mode;
811527a6266SJeff Kirsher 	u16 ctrl;
812527a6266SJeff Kirsher 
813527a6266SJeff Kirsher 	/* Bring hardware out of reset */
814527a6266SJeff Kirsher 	sky2_write16(hw, B0_CTST, CS_RST_CLR);
815527a6266SJeff Kirsher 	sky2_write16(hw, SK_REG(port, GMAC_LINK_CTRL), GMLC_RST_CLR);
816527a6266SJeff Kirsher 
817527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
818527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
819527a6266SJeff Kirsher 
820527a6266SJeff Kirsher 	/* Force to 10/100
821527a6266SJeff Kirsher 	 * sky2_reset will re-enable on resume
822527a6266SJeff Kirsher 	 */
823527a6266SJeff Kirsher 	save_mode = sky2->flow_mode;
824527a6266SJeff Kirsher 	ctrl = sky2->advertising;
825527a6266SJeff Kirsher 
826527a6266SJeff Kirsher 	sky2->advertising &= ~(ADVERTISED_1000baseT_Half|ADVERTISED_1000baseT_Full);
827527a6266SJeff Kirsher 	sky2->flow_mode = FC_NONE;
828527a6266SJeff Kirsher 
829527a6266SJeff Kirsher 	spin_lock_bh(&sky2->phy_lock);
830527a6266SJeff Kirsher 	sky2_phy_power_up(hw, port);
831527a6266SJeff Kirsher 	sky2_phy_init(hw, port);
832527a6266SJeff Kirsher 	spin_unlock_bh(&sky2->phy_lock);
833527a6266SJeff Kirsher 
834527a6266SJeff Kirsher 	sky2->flow_mode = save_mode;
835527a6266SJeff Kirsher 	sky2->advertising = ctrl;
836527a6266SJeff Kirsher 
837527a6266SJeff Kirsher 	/* Set GMAC to no flow control and auto update for speed/duplex */
838527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL,
839527a6266SJeff Kirsher 		    GM_GPCR_FC_TX_DIS|GM_GPCR_TX_ENA|GM_GPCR_RX_ENA|
840527a6266SJeff Kirsher 		    GM_GPCR_DUP_FULL|GM_GPCR_FC_RX_DIS|GM_GPCR_AU_FCT_DIS);
841527a6266SJeff Kirsher 
842527a6266SJeff Kirsher 	/* Set WOL address */
843527a6266SJeff Kirsher 	memcpy_toio(hw->regs + WOL_REGS(port, WOL_MAC_ADDR),
844527a6266SJeff Kirsher 		    sky2->netdev->dev_addr, ETH_ALEN);
845527a6266SJeff Kirsher 
846527a6266SJeff Kirsher 	/* Turn on appropriate WOL control bits */
847527a6266SJeff Kirsher 	sky2_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), WOL_CTL_CLEAR_RESULT);
848527a6266SJeff Kirsher 	ctrl = 0;
849527a6266SJeff Kirsher 	if (sky2->wol & WAKE_PHY)
850527a6266SJeff Kirsher 		ctrl |= WOL_CTL_ENA_PME_ON_LINK_CHG|WOL_CTL_ENA_LINK_CHG_UNIT;
851527a6266SJeff Kirsher 	else
852527a6266SJeff Kirsher 		ctrl |= WOL_CTL_DIS_PME_ON_LINK_CHG|WOL_CTL_DIS_LINK_CHG_UNIT;
853527a6266SJeff Kirsher 
854527a6266SJeff Kirsher 	if (sky2->wol & WAKE_MAGIC)
855527a6266SJeff Kirsher 		ctrl |= WOL_CTL_ENA_PME_ON_MAGIC_PKT|WOL_CTL_ENA_MAGIC_PKT_UNIT;
856527a6266SJeff Kirsher 	else
857527a6266SJeff Kirsher 		ctrl |= WOL_CTL_DIS_PME_ON_MAGIC_PKT|WOL_CTL_DIS_MAGIC_PKT_UNIT;
858527a6266SJeff Kirsher 
859527a6266SJeff Kirsher 	ctrl |= WOL_CTL_DIS_PME_ON_PATTERN|WOL_CTL_DIS_PATTERN_UNIT;
860527a6266SJeff Kirsher 	sky2_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), ctrl);
861527a6266SJeff Kirsher 
862527a6266SJeff Kirsher 	/* Disable PiG firmware */
863527a6266SJeff Kirsher 	sky2_write16(hw, B0_CTST, Y2_HW_WOL_OFF);
864527a6266SJeff Kirsher 
8655676cc7bSstephen hemminger 	/* Needed by some broken BIOSes, use PCI rather than PCI-e for WOL */
8665676cc7bSstephen hemminger 	if (legacy_pme) {
8675676cc7bSstephen hemminger 		u32 reg1 = sky2_pci_read32(hw, PCI_DEV_REG1);
8685676cc7bSstephen hemminger 		reg1 |= PCI_Y2_PME_LEGACY;
8695676cc7bSstephen hemminger 		sky2_pci_write32(hw, PCI_DEV_REG1, reg1);
8705676cc7bSstephen hemminger 	}
8715676cc7bSstephen hemminger 
872527a6266SJeff Kirsher 	/* block receiver */
873527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
874f9687c44Sstephen hemminger 	sky2_read32(hw, B0_CTST);
875527a6266SJeff Kirsher }
876527a6266SJeff Kirsher 
sky2_set_tx_stfwd(struct sky2_hw * hw,unsigned port)877527a6266SJeff Kirsher static void sky2_set_tx_stfwd(struct sky2_hw *hw, unsigned port)
878527a6266SJeff Kirsher {
879527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
880527a6266SJeff Kirsher 
881527a6266SJeff Kirsher 	if ( (hw->chip_id == CHIP_ID_YUKON_EX &&
882527a6266SJeff Kirsher 	      hw->chip_rev != CHIP_REV_YU_EX_A0) ||
883527a6266SJeff Kirsher 	     hw->chip_id >= CHIP_ID_YUKON_FE_P) {
884527a6266SJeff Kirsher 		/* Yukon-Extreme B0 and further Extreme devices */
885527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_STFW_ENA);
886527a6266SJeff Kirsher 	} else if (dev->mtu > ETH_DATA_LEN) {
887527a6266SJeff Kirsher 		/* set Tx GMAC FIFO Almost Empty Threshold */
888527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_AE_THR),
889527a6266SJeff Kirsher 			     (ECU_JUMBO_WM << 16) | ECU_AE_THR);
890527a6266SJeff Kirsher 
891527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_STFW_DIS);
892527a6266SJeff Kirsher 	} else
893527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_STFW_ENA);
894527a6266SJeff Kirsher }
895527a6266SJeff Kirsher 
sky2_mac_init(struct sky2_hw * hw,unsigned port)896527a6266SJeff Kirsher static void sky2_mac_init(struct sky2_hw *hw, unsigned port)
897527a6266SJeff Kirsher {
898527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
899527a6266SJeff Kirsher 	u16 reg;
900527a6266SJeff Kirsher 	u32 rx_reg;
901527a6266SJeff Kirsher 	int i;
902527a6266SJeff Kirsher 	const u8 *addr = hw->dev[port]->dev_addr;
903527a6266SJeff Kirsher 
904527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
905527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
906527a6266SJeff Kirsher 
907527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
908527a6266SJeff Kirsher 
909527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL &&
910527a6266SJeff Kirsher 	    hw->chip_rev == CHIP_REV_YU_XL_A0 &&
911527a6266SJeff Kirsher 	    port == 1) {
912527a6266SJeff Kirsher 		/* WA DEV_472 -- looks like crossed wires on port 2 */
913527a6266SJeff Kirsher 		/* clear GMAC 1 Control reset */
914527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(0, GMAC_CTRL), GMC_RST_CLR);
915527a6266SJeff Kirsher 		do {
916527a6266SJeff Kirsher 			sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_SET);
917527a6266SJeff Kirsher 			sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_CLR);
918527a6266SJeff Kirsher 		} while (gm_phy_read(hw, 1, PHY_MARV_ID0) != PHY_MARV_ID0_VAL ||
919527a6266SJeff Kirsher 			 gm_phy_read(hw, 1, PHY_MARV_ID1) != PHY_MARV_ID1_Y2 ||
920527a6266SJeff Kirsher 			 gm_phy_read(hw, 1, PHY_MARV_INT_MASK) != 0);
921527a6266SJeff Kirsher 	}
922527a6266SJeff Kirsher 
923527a6266SJeff Kirsher 	sky2_read16(hw, SK_REG(port, GMAC_IRQ_SRC));
924527a6266SJeff Kirsher 
925527a6266SJeff Kirsher 	/* Enable Transmit FIFO Underrun */
926527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), GMAC_DEF_MSK);
927527a6266SJeff Kirsher 
928527a6266SJeff Kirsher 	spin_lock_bh(&sky2->phy_lock);
929527a6266SJeff Kirsher 	sky2_phy_power_up(hw, port);
930527a6266SJeff Kirsher 	sky2_phy_init(hw, port);
931527a6266SJeff Kirsher 	spin_unlock_bh(&sky2->phy_lock);
932527a6266SJeff Kirsher 
933527a6266SJeff Kirsher 	/* MIB clear */
934527a6266SJeff Kirsher 	reg = gma_read16(hw, port, GM_PHY_ADDR);
935527a6266SJeff Kirsher 	gma_write16(hw, port, GM_PHY_ADDR, reg | GM_PAR_MIB_CLR);
936527a6266SJeff Kirsher 
937527a6266SJeff Kirsher 	for (i = GM_MIB_CNT_BASE; i <= GM_MIB_CNT_END; i += 4)
938527a6266SJeff Kirsher 		gma_read16(hw, port, i);
939527a6266SJeff Kirsher 	gma_write16(hw, port, GM_PHY_ADDR, reg);
940527a6266SJeff Kirsher 
941527a6266SJeff Kirsher 	/* transmit control */
942527a6266SJeff Kirsher 	gma_write16(hw, port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
943527a6266SJeff Kirsher 
944527a6266SJeff Kirsher 	/* receive control reg: unicast + multicast + no FCS  */
945527a6266SJeff Kirsher 	gma_write16(hw, port, GM_RX_CTRL,
946527a6266SJeff Kirsher 		    GM_RXCR_UCF_ENA | GM_RXCR_CRC_DIS | GM_RXCR_MCF_ENA);
947527a6266SJeff Kirsher 
948527a6266SJeff Kirsher 	/* transmit flow control */
949527a6266SJeff Kirsher 	gma_write16(hw, port, GM_TX_FLOW_CTRL, 0xffff);
950527a6266SJeff Kirsher 
951527a6266SJeff Kirsher 	/* transmit parameter */
952527a6266SJeff Kirsher 	gma_write16(hw, port, GM_TX_PARAM,
953527a6266SJeff Kirsher 		    TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) |
954527a6266SJeff Kirsher 		    TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
955527a6266SJeff Kirsher 		    TX_IPG_JAM_DATA(TX_IPG_JAM_DEF) |
956527a6266SJeff Kirsher 		    TX_BACK_OFF_LIM(TX_BOF_LIM_DEF));
957527a6266SJeff Kirsher 
958527a6266SJeff Kirsher 	/* serial mode register */
959527a6266SJeff Kirsher 	reg = DATA_BLIND_VAL(DATA_BLIND_DEF) |
960527a6266SJeff Kirsher 		GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF_1000);
961527a6266SJeff Kirsher 
962527a6266SJeff Kirsher 	if (hw->dev[port]->mtu > ETH_DATA_LEN)
963527a6266SJeff Kirsher 		reg |= GM_SMOD_JUMBO_ENA;
964527a6266SJeff Kirsher 
965527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EC_U &&
966527a6266SJeff Kirsher 	    hw->chip_rev == CHIP_REV_YU_EC_U_B1)
967527a6266SJeff Kirsher 		reg |= GM_NEW_FLOW_CTRL;
968527a6266SJeff Kirsher 
969527a6266SJeff Kirsher 	gma_write16(hw, port, GM_SERIAL_MODE, reg);
970527a6266SJeff Kirsher 
971527a6266SJeff Kirsher 	/* virtual address for data */
972527a6266SJeff Kirsher 	gma_set_addr(hw, port, GM_SRC_ADDR_2L, addr);
973527a6266SJeff Kirsher 
974527a6266SJeff Kirsher 	/* physical address: used for pause frames */
975527a6266SJeff Kirsher 	gma_set_addr(hw, port, GM_SRC_ADDR_1L, addr);
976527a6266SJeff Kirsher 
977527a6266SJeff Kirsher 	/* ignore counter overflows */
978527a6266SJeff Kirsher 	gma_write16(hw, port, GM_TX_IRQ_MSK, 0);
979527a6266SJeff Kirsher 	gma_write16(hw, port, GM_RX_IRQ_MSK, 0);
980527a6266SJeff Kirsher 	gma_write16(hw, port, GM_TR_IRQ_MSK, 0);
981527a6266SJeff Kirsher 
982527a6266SJeff Kirsher 	/* Configure Rx MAC FIFO */
983527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_CLR);
984527a6266SJeff Kirsher 	rx_reg = GMF_OPER_ON | GMF_RX_F_FL_ON;
985527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EX ||
986527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_FE_P)
987527a6266SJeff Kirsher 		rx_reg |= GMF_RX_OVER_ON;
988527a6266SJeff Kirsher 
989527a6266SJeff Kirsher 	sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T), rx_reg);
990527a6266SJeff Kirsher 
991527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL) {
992527a6266SJeff Kirsher 		/* Hardware errata - clear flush mask */
993527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(port, RX_GMF_FL_MSK), 0);
994527a6266SJeff Kirsher 	} else {
995527a6266SJeff Kirsher 		/* Flush Rx MAC FIFO on any flow control or error */
996527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(port, RX_GMF_FL_MSK), GMR_FS_ANY_ERR);
997527a6266SJeff Kirsher 	}
998527a6266SJeff Kirsher 
999527a6266SJeff Kirsher 	/* Set threshold to 0xa (64 bytes) + 1 to workaround pause bug  */
1000527a6266SJeff Kirsher 	reg = RX_GMF_FL_THR_DEF + 1;
1001527a6266SJeff Kirsher 	/* Another magic mystery workaround from sk98lin */
1002527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_FE_P &&
1003527a6266SJeff Kirsher 	    hw->chip_rev == CHIP_REV_YU_FE2_A0)
1004527a6266SJeff Kirsher 		reg = 0x178;
1005527a6266SJeff Kirsher 	sky2_write16(hw, SK_REG(port, RX_GMF_FL_THR), reg);
1006527a6266SJeff Kirsher 
1007527a6266SJeff Kirsher 	/* Configure Tx MAC FIFO */
1008527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_CLR);
1009527a6266SJeff Kirsher 	sky2_write16(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_OPER_ON);
1010527a6266SJeff Kirsher 
1011527a6266SJeff Kirsher 	/* On chips without ram buffer, pause is controlled by MAC level */
1012527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_RAM_BUFFER)) {
1013527a6266SJeff Kirsher 		/* Pause threshold is scaled by 8 in bytes */
1014527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_FE_P &&
1015527a6266SJeff Kirsher 		    hw->chip_rev == CHIP_REV_YU_FE2_A0)
1016527a6266SJeff Kirsher 			reg = 1568 / 8;
1017527a6266SJeff Kirsher 		else
1018527a6266SJeff Kirsher 			reg = 1024 / 8;
1019527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(port, RX_GMF_UP_THR), reg);
1020527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(port, RX_GMF_LP_THR), 768 / 8);
1021527a6266SJeff Kirsher 
1022527a6266SJeff Kirsher 		sky2_set_tx_stfwd(hw, port);
1023527a6266SJeff Kirsher 	}
1024527a6266SJeff Kirsher 
1025527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_FE_P &&
1026527a6266SJeff Kirsher 	    hw->chip_rev == CHIP_REV_YU_FE2_A0) {
1027527a6266SJeff Kirsher 		/* disable dynamic watermark */
1028527a6266SJeff Kirsher 		reg = sky2_read16(hw, SK_REG(port, TX_GMF_EA));
1029527a6266SJeff Kirsher 		reg &= ~TX_DYN_WM_ENA;
1030527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(port, TX_GMF_EA), reg);
1031527a6266SJeff Kirsher 	}
1032527a6266SJeff Kirsher }
1033527a6266SJeff Kirsher 
1034527a6266SJeff Kirsher /* Assign Ram Buffer allocation to queue */
sky2_ramset(struct sky2_hw * hw,u16 q,u32 start,u32 space)1035527a6266SJeff Kirsher static void sky2_ramset(struct sky2_hw *hw, u16 q, u32 start, u32 space)
1036527a6266SJeff Kirsher {
1037527a6266SJeff Kirsher 	u32 end;
1038527a6266SJeff Kirsher 
1039527a6266SJeff Kirsher 	/* convert from K bytes to qwords used for hw register */
1040527a6266SJeff Kirsher 	start *= 1024/8;
1041527a6266SJeff Kirsher 	space *= 1024/8;
1042527a6266SJeff Kirsher 	end = start + space - 1;
1043527a6266SJeff Kirsher 
1044527a6266SJeff Kirsher 	sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_RST_CLR);
1045527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(q, RB_START), start);
1046527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(q, RB_END), end);
1047527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(q, RB_WP), start);
1048527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(q, RB_RP), start);
1049527a6266SJeff Kirsher 
1050527a6266SJeff Kirsher 	if (q == Q_R1 || q == Q_R2) {
1051527a6266SJeff Kirsher 		u32 tp = space - space/4;
1052527a6266SJeff Kirsher 
1053527a6266SJeff Kirsher 		/* On receive queue's set the thresholds
1054527a6266SJeff Kirsher 		 * give receiver priority when > 3/4 full
1055527a6266SJeff Kirsher 		 * send pause when down to 2K
1056527a6266SJeff Kirsher 		 */
1057527a6266SJeff Kirsher 		sky2_write32(hw, RB_ADDR(q, RB_RX_UTHP), tp);
1058527a6266SJeff Kirsher 		sky2_write32(hw, RB_ADDR(q, RB_RX_LTHP), space/2);
1059527a6266SJeff Kirsher 
106074f9f42cSMirko Lindner 		tp = space - 8192/8;
1061527a6266SJeff Kirsher 		sky2_write32(hw, RB_ADDR(q, RB_RX_UTPP), tp);
1062527a6266SJeff Kirsher 		sky2_write32(hw, RB_ADDR(q, RB_RX_LTPP), space/4);
1063527a6266SJeff Kirsher 	} else {
1064527a6266SJeff Kirsher 		/* Enable store & forward on Tx queue's because
1065527a6266SJeff Kirsher 		 * Tx FIFO is only 1K on Yukon
1066527a6266SJeff Kirsher 		 */
1067527a6266SJeff Kirsher 		sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_STFWD);
1068527a6266SJeff Kirsher 	}
1069527a6266SJeff Kirsher 
1070527a6266SJeff Kirsher 	sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_OP_MD);
1071527a6266SJeff Kirsher 	sky2_read8(hw, RB_ADDR(q, RB_CTRL));
1072527a6266SJeff Kirsher }
1073527a6266SJeff Kirsher 
1074527a6266SJeff Kirsher /* Setup Bus Memory Interface */
sky2_qset(struct sky2_hw * hw,u16 q)1075527a6266SJeff Kirsher static void sky2_qset(struct sky2_hw *hw, u16 q)
1076527a6266SJeff Kirsher {
1077527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_CLR_RESET);
1078527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_OPER_INIT);
1079527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_FIFO_OP_ON);
1080527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(q, Q_WM),  BMU_WM_DEFAULT);
1081527a6266SJeff Kirsher }
1082527a6266SJeff Kirsher 
1083527a6266SJeff Kirsher /* Setup prefetch unit registers. This is the interface between
1084527a6266SJeff Kirsher  * hardware and driver list elements
1085527a6266SJeff Kirsher  */
sky2_prefetch_init(struct sky2_hw * hw,u32 qaddr,dma_addr_t addr,u32 last)1086527a6266SJeff Kirsher static void sky2_prefetch_init(struct sky2_hw *hw, u32 qaddr,
1087527a6266SJeff Kirsher 			       dma_addr_t addr, u32 last)
1088527a6266SJeff Kirsher {
1089527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
1090527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_CLR);
1091527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_HI), upper_32_bits(addr));
1092527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_LO), lower_32_bits(addr));
1093527a6266SJeff Kirsher 	sky2_write16(hw, Y2_QADDR(qaddr, PREF_UNIT_LAST_IDX), last);
1094527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_OP_ON);
1095527a6266SJeff Kirsher 
1096527a6266SJeff Kirsher 	sky2_read32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL));
1097527a6266SJeff Kirsher }
1098527a6266SJeff Kirsher 
get_tx_le(struct sky2_port * sky2,u16 * slot)1099527a6266SJeff Kirsher static inline struct sky2_tx_le *get_tx_le(struct sky2_port *sky2, u16 *slot)
1100527a6266SJeff Kirsher {
1101527a6266SJeff Kirsher 	struct sky2_tx_le *le = sky2->tx_le + *slot;
1102527a6266SJeff Kirsher 
1103527a6266SJeff Kirsher 	*slot = RING_NEXT(*slot, sky2->tx_ring_size);
1104527a6266SJeff Kirsher 	le->ctrl = 0;
1105527a6266SJeff Kirsher 	return le;
1106527a6266SJeff Kirsher }
1107527a6266SJeff Kirsher 
tx_init(struct sky2_port * sky2)1108527a6266SJeff Kirsher static void tx_init(struct sky2_port *sky2)
1109527a6266SJeff Kirsher {
1110527a6266SJeff Kirsher 	struct sky2_tx_le *le;
1111527a6266SJeff Kirsher 
1112527a6266SJeff Kirsher 	sky2->tx_prod = sky2->tx_cons = 0;
1113527a6266SJeff Kirsher 	sky2->tx_tcpsum = 0;
1114527a6266SJeff Kirsher 	sky2->tx_last_mss = 0;
1115ec2a5466Sstephen hemminger 	netdev_reset_queue(sky2->netdev);
1116527a6266SJeff Kirsher 
1117527a6266SJeff Kirsher 	le = get_tx_le(sky2, &sky2->tx_prod);
1118527a6266SJeff Kirsher 	le->addr = 0;
1119527a6266SJeff Kirsher 	le->opcode = OP_ADDR64 | HW_OWNER;
1120527a6266SJeff Kirsher 	sky2->tx_last_upper = 0;
1121527a6266SJeff Kirsher }
1122527a6266SJeff Kirsher 
1123527a6266SJeff Kirsher /* Update chip's next pointer */
sky2_put_idx(struct sky2_hw * hw,unsigned q,u16 idx)1124527a6266SJeff Kirsher static inline void sky2_put_idx(struct sky2_hw *hw, unsigned q, u16 idx)
1125527a6266SJeff Kirsher {
1126527a6266SJeff Kirsher 	/* Make sure write' to descriptors are complete before we tell hardware */
1127527a6266SJeff Kirsher 	wmb();
1128527a6266SJeff Kirsher 	sky2_write16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX), idx);
1129527a6266SJeff Kirsher }
1130527a6266SJeff Kirsher 
1131527a6266SJeff Kirsher 
sky2_next_rx(struct sky2_port * sky2)1132527a6266SJeff Kirsher static inline struct sky2_rx_le *sky2_next_rx(struct sky2_port *sky2)
1133527a6266SJeff Kirsher {
1134527a6266SJeff Kirsher 	struct sky2_rx_le *le = sky2->rx_le + sky2->rx_put;
1135527a6266SJeff Kirsher 	sky2->rx_put = RING_NEXT(sky2->rx_put, RX_LE_SIZE);
1136527a6266SJeff Kirsher 	le->ctrl = 0;
1137527a6266SJeff Kirsher 	return le;
1138527a6266SJeff Kirsher }
1139527a6266SJeff Kirsher 
sky2_get_rx_threshold(struct sky2_port * sky2)1140527a6266SJeff Kirsher static unsigned sky2_get_rx_threshold(struct sky2_port *sky2)
1141527a6266SJeff Kirsher {
1142527a6266SJeff Kirsher 	unsigned size;
1143527a6266SJeff Kirsher 
1144527a6266SJeff Kirsher 	/* Space needed for frame data + headers rounded up */
1145527a6266SJeff Kirsher 	size = roundup(sky2->netdev->mtu + ETH_HLEN + VLAN_HLEN, 8);
1146527a6266SJeff Kirsher 
1147527a6266SJeff Kirsher 	/* Stopping point for hardware truncation */
1148527a6266SJeff Kirsher 	return (size - 8) / sizeof(u32);
1149527a6266SJeff Kirsher }
1150527a6266SJeff Kirsher 
sky2_get_rx_data_size(struct sky2_port * sky2)1151527a6266SJeff Kirsher static unsigned sky2_get_rx_data_size(struct sky2_port *sky2)
1152527a6266SJeff Kirsher {
1153527a6266SJeff Kirsher 	struct rx_ring_info *re;
1154527a6266SJeff Kirsher 	unsigned size;
1155527a6266SJeff Kirsher 
1156527a6266SJeff Kirsher 	/* Space needed for frame data + headers rounded up */
1157527a6266SJeff Kirsher 	size = roundup(sky2->netdev->mtu + ETH_HLEN + VLAN_HLEN, 8);
1158527a6266SJeff Kirsher 
1159527a6266SJeff Kirsher 	sky2->rx_nfrags = size >> PAGE_SHIFT;
1160527a6266SJeff Kirsher 	BUG_ON(sky2->rx_nfrags > ARRAY_SIZE(re->frag_addr));
1161527a6266SJeff Kirsher 
1162527a6266SJeff Kirsher 	/* Compute residue after pages */
1163527a6266SJeff Kirsher 	size -= sky2->rx_nfrags << PAGE_SHIFT;
1164527a6266SJeff Kirsher 
1165527a6266SJeff Kirsher 	/* Optimize to handle small packets and headers */
1166527a6266SJeff Kirsher 	if (size < copybreak)
1167527a6266SJeff Kirsher 		size = copybreak;
1168527a6266SJeff Kirsher 	if (size < ETH_HLEN)
1169527a6266SJeff Kirsher 		size = ETH_HLEN;
1170527a6266SJeff Kirsher 
1171527a6266SJeff Kirsher 	return size;
1172527a6266SJeff Kirsher }
1173527a6266SJeff Kirsher 
1174527a6266SJeff Kirsher /* Build description to hardware for one receive segment */
sky2_rx_add(struct sky2_port * sky2,u8 op,dma_addr_t map,unsigned len)1175527a6266SJeff Kirsher static void sky2_rx_add(struct sky2_port *sky2, u8 op,
1176527a6266SJeff Kirsher 			dma_addr_t map, unsigned len)
1177527a6266SJeff Kirsher {
1178527a6266SJeff Kirsher 	struct sky2_rx_le *le;
1179527a6266SJeff Kirsher 
1180527a6266SJeff Kirsher 	if (sizeof(dma_addr_t) > sizeof(u32)) {
1181527a6266SJeff Kirsher 		le = sky2_next_rx(sky2);
1182527a6266SJeff Kirsher 		le->addr = cpu_to_le32(upper_32_bits(map));
1183527a6266SJeff Kirsher 		le->opcode = OP_ADDR64 | HW_OWNER;
1184527a6266SJeff Kirsher 	}
1185527a6266SJeff Kirsher 
1186527a6266SJeff Kirsher 	le = sky2_next_rx(sky2);
1187527a6266SJeff Kirsher 	le->addr = cpu_to_le32(lower_32_bits(map));
1188527a6266SJeff Kirsher 	le->length = cpu_to_le16(len);
1189527a6266SJeff Kirsher 	le->opcode = op | HW_OWNER;
1190527a6266SJeff Kirsher }
1191527a6266SJeff Kirsher 
1192527a6266SJeff Kirsher /* Build description to hardware for one possibly fragmented skb */
sky2_rx_submit(struct sky2_port * sky2,const struct rx_ring_info * re)1193527a6266SJeff Kirsher static void sky2_rx_submit(struct sky2_port *sky2,
1194527a6266SJeff Kirsher 			   const struct rx_ring_info *re)
1195527a6266SJeff Kirsher {
1196527a6266SJeff Kirsher 	int i;
1197527a6266SJeff Kirsher 
1198527a6266SJeff Kirsher 	sky2_rx_add(sky2, OP_PACKET, re->data_addr, sky2->rx_data_size);
1199527a6266SJeff Kirsher 
1200527a6266SJeff Kirsher 	for (i = 0; i < skb_shinfo(re->skb)->nr_frags; i++)
1201527a6266SJeff Kirsher 		sky2_rx_add(sky2, OP_BUFFER, re->frag_addr[i], PAGE_SIZE);
1202527a6266SJeff Kirsher }
1203527a6266SJeff Kirsher 
1204527a6266SJeff Kirsher 
sky2_rx_map_skb(struct pci_dev * pdev,struct rx_ring_info * re,unsigned size)1205527a6266SJeff Kirsher static int sky2_rx_map_skb(struct pci_dev *pdev, struct rx_ring_info *re,
1206527a6266SJeff Kirsher 			    unsigned size)
1207527a6266SJeff Kirsher {
1208527a6266SJeff Kirsher 	struct sk_buff *skb = re->skb;
1209527a6266SJeff Kirsher 	int i;
1210527a6266SJeff Kirsher 
1211c86768cfSChristophe JAILLET 	re->data_addr = dma_map_single(&pdev->dev, skb->data, size,
1212c86768cfSChristophe JAILLET 				       DMA_FROM_DEVICE);
1213c86768cfSChristophe JAILLET 	if (dma_mapping_error(&pdev->dev, re->data_addr))
1214527a6266SJeff Kirsher 		goto mapping_error;
1215527a6266SJeff Kirsher 
1216527a6266SJeff Kirsher 	dma_unmap_len_set(re, data_size, size);
1217527a6266SJeff Kirsher 
1218527a6266SJeff Kirsher 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12199e903e08SEric Dumazet 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1220527a6266SJeff Kirsher 
1221950a5a4fSIan Campbell 		re->frag_addr[i] = skb_frag_dma_map(&pdev->dev, frag, 0,
12229e903e08SEric Dumazet 						    skb_frag_size(frag),
12235d6bcdfeSIan Campbell 						    DMA_FROM_DEVICE);
1224527a6266SJeff Kirsher 
12255d6bcdfeSIan Campbell 		if (dma_mapping_error(&pdev->dev, re->frag_addr[i]))
1226527a6266SJeff Kirsher 			goto map_page_error;
1227527a6266SJeff Kirsher 	}
1228527a6266SJeff Kirsher 	return 0;
1229527a6266SJeff Kirsher 
1230527a6266SJeff Kirsher map_page_error:
1231527a6266SJeff Kirsher 	while (--i >= 0) {
1232c86768cfSChristophe JAILLET 		dma_unmap_page(&pdev->dev, re->frag_addr[i],
12339e903e08SEric Dumazet 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
1234c86768cfSChristophe JAILLET 			       DMA_FROM_DEVICE);
1235527a6266SJeff Kirsher 	}
1236527a6266SJeff Kirsher 
1237c86768cfSChristophe JAILLET 	dma_unmap_single(&pdev->dev, re->data_addr,
1238c86768cfSChristophe JAILLET 			 dma_unmap_len(re, data_size), DMA_FROM_DEVICE);
1239527a6266SJeff Kirsher 
1240527a6266SJeff Kirsher mapping_error:
1241527a6266SJeff Kirsher 	if (net_ratelimit())
1242527a6266SJeff Kirsher 		dev_warn(&pdev->dev, "%s: rx mapping error\n",
1243527a6266SJeff Kirsher 			 skb->dev->name);
1244527a6266SJeff Kirsher 	return -EIO;
1245527a6266SJeff Kirsher }
1246527a6266SJeff Kirsher 
sky2_rx_unmap_skb(struct pci_dev * pdev,struct rx_ring_info * re)1247527a6266SJeff Kirsher static void sky2_rx_unmap_skb(struct pci_dev *pdev, struct rx_ring_info *re)
1248527a6266SJeff Kirsher {
1249527a6266SJeff Kirsher 	struct sk_buff *skb = re->skb;
1250527a6266SJeff Kirsher 	int i;
1251527a6266SJeff Kirsher 
1252c86768cfSChristophe JAILLET 	dma_unmap_single(&pdev->dev, re->data_addr,
1253c86768cfSChristophe JAILLET 			 dma_unmap_len(re, data_size), DMA_FROM_DEVICE);
1254527a6266SJeff Kirsher 
1255527a6266SJeff Kirsher 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1256c86768cfSChristophe JAILLET 		dma_unmap_page(&pdev->dev, re->frag_addr[i],
12579e903e08SEric Dumazet 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
1258c86768cfSChristophe JAILLET 			       DMA_FROM_DEVICE);
1259527a6266SJeff Kirsher }
1260527a6266SJeff Kirsher 
1261527a6266SJeff Kirsher /* Tell chip where to start receive checksum.
1262527a6266SJeff Kirsher  * Actually has two checksums, but set both same to avoid possible byte
1263527a6266SJeff Kirsher  * order problems.
1264527a6266SJeff Kirsher  */
rx_set_checksum(struct sky2_port * sky2)1265527a6266SJeff Kirsher static void rx_set_checksum(struct sky2_port *sky2)
1266527a6266SJeff Kirsher {
1267527a6266SJeff Kirsher 	struct sky2_rx_le *le = sky2_next_rx(sky2);
1268527a6266SJeff Kirsher 
1269527a6266SJeff Kirsher 	le->addr = cpu_to_le32((ETH_HLEN << 16) | ETH_HLEN);
1270527a6266SJeff Kirsher 	le->ctrl = 0;
1271527a6266SJeff Kirsher 	le->opcode = OP_TCPSTART | HW_OWNER;
1272527a6266SJeff Kirsher 
1273527a6266SJeff Kirsher 	sky2_write32(sky2->hw,
1274527a6266SJeff Kirsher 		     Q_ADDR(rxqaddr[sky2->port], Q_CSR),
1275527a6266SJeff Kirsher 		     (sky2->netdev->features & NETIF_F_RXCSUM)
1276527a6266SJeff Kirsher 		     ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
1277527a6266SJeff Kirsher }
1278527a6266SJeff Kirsher 
1279527a6266SJeff Kirsher /* Enable/disable receive hash calculation (RSS) */
rx_set_rss(struct net_device * dev,netdev_features_t features)1280c8f44affSMichał Mirosław static void rx_set_rss(struct net_device *dev, netdev_features_t features)
1281527a6266SJeff Kirsher {
1282527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
1283527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1284527a6266SJeff Kirsher 	int i, nkeys = 4;
1285527a6266SJeff Kirsher 
1286527a6266SJeff Kirsher 	/* Supports IPv6 and other modes */
1287527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_NEW_LE) {
1288527a6266SJeff Kirsher 		nkeys = 10;
1289527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(sky2->port, RSS_CFG), HASH_ALL);
1290527a6266SJeff Kirsher 	}
1291527a6266SJeff Kirsher 
1292527a6266SJeff Kirsher 	/* Program RSS initial values */
1293527a6266SJeff Kirsher 	if (features & NETIF_F_RXHASH) {
12942e95b2a8SIan Morris 		u32 rss_key[10];
12952e95b2a8SIan Morris 
12962e95b2a8SIan Morris 		netdev_rss_key_fill(rss_key, sizeof(rss_key));
1297527a6266SJeff Kirsher 		for (i = 0; i < nkeys; i++)
1298527a6266SJeff Kirsher 			sky2_write32(hw, SK_REG(sky2->port, RSS_KEY + i * 4),
12992e95b2a8SIan Morris 				     rss_key[i]);
1300527a6266SJeff Kirsher 
1301527a6266SJeff Kirsher 		/* Need to turn on (undocumented) flag to make hashing work  */
1302527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T),
1303527a6266SJeff Kirsher 			     RX_STFW_ENA);
1304527a6266SJeff Kirsher 
1305527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(rxqaddr[sky2->port], Q_CSR),
1306527a6266SJeff Kirsher 			     BMU_ENA_RX_RSS_HASH);
1307527a6266SJeff Kirsher 	} else
1308527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(rxqaddr[sky2->port], Q_CSR),
1309527a6266SJeff Kirsher 			     BMU_DIS_RX_RSS_HASH);
1310527a6266SJeff Kirsher }
1311527a6266SJeff Kirsher 
1312527a6266SJeff Kirsher /*
1313527a6266SJeff Kirsher  * The RX Stop command will not work for Yukon-2 if the BMU does not
1314527a6266SJeff Kirsher  * reach the end of packet and since we can't make sure that we have
1315527a6266SJeff Kirsher  * incoming data, we must reset the BMU while it is not doing a DMA
1316527a6266SJeff Kirsher  * transfer. Since it is possible that the RX path is still active,
1317527a6266SJeff Kirsher  * the RX RAM buffer will be stopped first, so any possible incoming
1318527a6266SJeff Kirsher  * data will not trigger a DMA. After the RAM buffer is stopped, the
1319527a6266SJeff Kirsher  * BMU is polled until any DMA in progress is ended and only then it
1320527a6266SJeff Kirsher  * will be reset.
1321527a6266SJeff Kirsher  */
sky2_rx_stop(struct sky2_port * sky2)1322527a6266SJeff Kirsher static void sky2_rx_stop(struct sky2_port *sky2)
1323527a6266SJeff Kirsher {
1324527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1325527a6266SJeff Kirsher 	unsigned rxq = rxqaddr[sky2->port];
1326527a6266SJeff Kirsher 	int i;
1327527a6266SJeff Kirsher 
1328527a6266SJeff Kirsher 	/* disable the RAM Buffer receive queue */
1329527a6266SJeff Kirsher 	sky2_write8(hw, RB_ADDR(rxq, RB_CTRL), RB_DIS_OP_MD);
1330527a6266SJeff Kirsher 
1331527a6266SJeff Kirsher 	for (i = 0; i < 0xffff; i++)
1332527a6266SJeff Kirsher 		if (sky2_read8(hw, RB_ADDR(rxq, Q_RSL))
1333527a6266SJeff Kirsher 		    == sky2_read8(hw, RB_ADDR(rxq, Q_RL)))
1334527a6266SJeff Kirsher 			goto stopped;
1335527a6266SJeff Kirsher 
1336527a6266SJeff Kirsher 	netdev_warn(sky2->netdev, "receiver stop failed\n");
1337527a6266SJeff Kirsher stopped:
1338527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(rxq, Q_CSR), BMU_RST_SET | BMU_FIFO_RST);
1339527a6266SJeff Kirsher 
1340527a6266SJeff Kirsher 	/* reset the Rx prefetch unit */
1341527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(rxq, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
1342527a6266SJeff Kirsher }
1343527a6266SJeff Kirsher 
1344527a6266SJeff Kirsher /* Clean out receive buffer area, assumes receiver hardware stopped */
sky2_rx_clean(struct sky2_port * sky2)1345527a6266SJeff Kirsher static void sky2_rx_clean(struct sky2_port *sky2)
1346527a6266SJeff Kirsher {
1347527a6266SJeff Kirsher 	unsigned i;
1348527a6266SJeff Kirsher 
1349799d2fffSMirko Lindner 	if (sky2->rx_le)
1350527a6266SJeff Kirsher 		memset(sky2->rx_le, 0, RX_LE_BYTES);
1351799d2fffSMirko Lindner 
1352527a6266SJeff Kirsher 	for (i = 0; i < sky2->rx_pending; i++) {
1353527a6266SJeff Kirsher 		struct rx_ring_info *re = sky2->rx_ring + i;
1354527a6266SJeff Kirsher 
1355527a6266SJeff Kirsher 		if (re->skb) {
1356527a6266SJeff Kirsher 			sky2_rx_unmap_skb(sky2->hw->pdev, re);
1357527a6266SJeff Kirsher 			kfree_skb(re->skb);
1358527a6266SJeff Kirsher 			re->skb = NULL;
1359527a6266SJeff Kirsher 		}
1360527a6266SJeff Kirsher 	}
1361527a6266SJeff Kirsher }
1362527a6266SJeff Kirsher 
1363527a6266SJeff Kirsher /* Basic MII support */
sky2_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1364527a6266SJeff Kirsher static int sky2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1365527a6266SJeff Kirsher {
1366527a6266SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
1367527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
1368527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1369527a6266SJeff Kirsher 	int err = -EOPNOTSUPP;
1370527a6266SJeff Kirsher 
1371527a6266SJeff Kirsher 	if (!netif_running(dev))
1372527a6266SJeff Kirsher 		return -ENODEV;	/* Phy still in reset */
1373527a6266SJeff Kirsher 
1374527a6266SJeff Kirsher 	switch (cmd) {
1375527a6266SJeff Kirsher 	case SIOCGMIIPHY:
1376527a6266SJeff Kirsher 		data->phy_id = PHY_ADDR_MARV;
1377527a6266SJeff Kirsher 
1378df561f66SGustavo A. R. Silva 		fallthrough;
1379527a6266SJeff Kirsher 	case SIOCGMIIREG: {
1380527a6266SJeff Kirsher 		u16 val = 0;
1381527a6266SJeff Kirsher 
1382527a6266SJeff Kirsher 		spin_lock_bh(&sky2->phy_lock);
1383527a6266SJeff Kirsher 		err = __gm_phy_read(hw, sky2->port, data->reg_num & 0x1f, &val);
1384527a6266SJeff Kirsher 		spin_unlock_bh(&sky2->phy_lock);
1385527a6266SJeff Kirsher 
1386527a6266SJeff Kirsher 		data->val_out = val;
1387527a6266SJeff Kirsher 		break;
1388527a6266SJeff Kirsher 	}
1389527a6266SJeff Kirsher 
1390527a6266SJeff Kirsher 	case SIOCSMIIREG:
1391527a6266SJeff Kirsher 		spin_lock_bh(&sky2->phy_lock);
1392527a6266SJeff Kirsher 		err = gm_phy_write(hw, sky2->port, data->reg_num & 0x1f,
1393527a6266SJeff Kirsher 				   data->val_in);
1394527a6266SJeff Kirsher 		spin_unlock_bh(&sky2->phy_lock);
1395527a6266SJeff Kirsher 		break;
1396527a6266SJeff Kirsher 	}
1397527a6266SJeff Kirsher 	return err;
1398527a6266SJeff Kirsher }
1399527a6266SJeff Kirsher 
1400527a6266SJeff Kirsher #define SKY2_VLAN_OFFLOADS (NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO)
1401527a6266SJeff Kirsher 
sky2_vlan_mode(struct net_device * dev,netdev_features_t features)1402c8f44affSMichał Mirosław static void sky2_vlan_mode(struct net_device *dev, netdev_features_t features)
1403527a6266SJeff Kirsher {
1404527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
1405527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1406527a6266SJeff Kirsher 	u16 port = sky2->port;
1407527a6266SJeff Kirsher 
1408f646968fSPatrick McHardy 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
1409527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T),
1410527a6266SJeff Kirsher 			     RX_VLAN_STRIP_ON);
1411527a6266SJeff Kirsher 	else
1412527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T),
1413527a6266SJeff Kirsher 			     RX_VLAN_STRIP_OFF);
1414527a6266SJeff Kirsher 
1415f646968fSPatrick McHardy 	if (features & NETIF_F_HW_VLAN_CTAG_TX) {
1416527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
1417527a6266SJeff Kirsher 			     TX_VLAN_TAG_ON);
1418527a6266SJeff Kirsher 
1419527a6266SJeff Kirsher 		dev->vlan_features |= SKY2_VLAN_OFFLOADS;
1420527a6266SJeff Kirsher 	} else {
1421527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
1422527a6266SJeff Kirsher 			     TX_VLAN_TAG_OFF);
1423527a6266SJeff Kirsher 
1424527a6266SJeff Kirsher 		/* Can't do transmit offload of vlan without hw vlan */
1425527a6266SJeff Kirsher 		dev->vlan_features &= ~SKY2_VLAN_OFFLOADS;
1426527a6266SJeff Kirsher 	}
1427527a6266SJeff Kirsher }
1428527a6266SJeff Kirsher 
1429527a6266SJeff Kirsher /* Amount of required worst case padding in rx buffer */
sky2_rx_pad(const struct sky2_hw * hw)1430527a6266SJeff Kirsher static inline unsigned sky2_rx_pad(const struct sky2_hw *hw)
1431527a6266SJeff Kirsher {
1432527a6266SJeff Kirsher 	return (hw->flags & SKY2_HW_RAM_BUFFER) ? 8 : 2;
1433527a6266SJeff Kirsher }
1434527a6266SJeff Kirsher 
1435527a6266SJeff Kirsher /*
1436527a6266SJeff Kirsher  * Allocate an skb for receiving. If the MTU is large enough
1437527a6266SJeff Kirsher  * make the skb non-linear with a fragment list of pages.
1438527a6266SJeff Kirsher  */
sky2_rx_alloc(struct sky2_port * sky2,gfp_t gfp)1439527a6266SJeff Kirsher static struct sk_buff *sky2_rx_alloc(struct sky2_port *sky2, gfp_t gfp)
1440527a6266SJeff Kirsher {
1441527a6266SJeff Kirsher 	struct sk_buff *skb;
1442527a6266SJeff Kirsher 	int i;
1443527a6266SJeff Kirsher 
1444527a6266SJeff Kirsher 	skb = __netdev_alloc_skb(sky2->netdev,
1445527a6266SJeff Kirsher 				 sky2->rx_data_size + sky2_rx_pad(sky2->hw),
1446527a6266SJeff Kirsher 				 gfp);
1447527a6266SJeff Kirsher 	if (!skb)
1448527a6266SJeff Kirsher 		goto nomem;
1449527a6266SJeff Kirsher 
1450527a6266SJeff Kirsher 	if (sky2->hw->flags & SKY2_HW_RAM_BUFFER) {
1451527a6266SJeff Kirsher 		unsigned char *start;
1452527a6266SJeff Kirsher 		/*
1453527a6266SJeff Kirsher 		 * Workaround for a bug in FIFO that cause hang
1454527a6266SJeff Kirsher 		 * if the FIFO if the receive buffer is not 64 byte aligned.
1455527a6266SJeff Kirsher 		 * The buffer returned from netdev_alloc_skb is
1456527a6266SJeff Kirsher 		 * aligned except if slab debugging is enabled.
1457527a6266SJeff Kirsher 		 */
1458527a6266SJeff Kirsher 		start = PTR_ALIGN(skb->data, 8);
1459527a6266SJeff Kirsher 		skb_reserve(skb, start - skb->data);
1460527a6266SJeff Kirsher 	} else
1461527a6266SJeff Kirsher 		skb_reserve(skb, NET_IP_ALIGN);
1462527a6266SJeff Kirsher 
1463527a6266SJeff Kirsher 	for (i = 0; i < sky2->rx_nfrags; i++) {
1464527a6266SJeff Kirsher 		struct page *page = alloc_page(gfp);
1465527a6266SJeff Kirsher 
1466527a6266SJeff Kirsher 		if (!page)
1467527a6266SJeff Kirsher 			goto free_partial;
1468527a6266SJeff Kirsher 		skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
1469527a6266SJeff Kirsher 	}
1470527a6266SJeff Kirsher 
1471527a6266SJeff Kirsher 	return skb;
1472527a6266SJeff Kirsher free_partial:
1473527a6266SJeff Kirsher 	kfree_skb(skb);
1474527a6266SJeff Kirsher nomem:
1475527a6266SJeff Kirsher 	return NULL;
1476527a6266SJeff Kirsher }
1477527a6266SJeff Kirsher 
sky2_rx_update(struct sky2_port * sky2,unsigned rxq)1478527a6266SJeff Kirsher static inline void sky2_rx_update(struct sky2_port *sky2, unsigned rxq)
1479527a6266SJeff Kirsher {
1480527a6266SJeff Kirsher 	sky2_put_idx(sky2->hw, rxq, sky2->rx_put);
1481527a6266SJeff Kirsher }
1482527a6266SJeff Kirsher 
sky2_alloc_rx_skbs(struct sky2_port * sky2)1483527a6266SJeff Kirsher static int sky2_alloc_rx_skbs(struct sky2_port *sky2)
1484527a6266SJeff Kirsher {
1485527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1486527a6266SJeff Kirsher 	unsigned i;
1487527a6266SJeff Kirsher 
1488527a6266SJeff Kirsher 	sky2->rx_data_size = sky2_get_rx_data_size(sky2);
1489527a6266SJeff Kirsher 
1490527a6266SJeff Kirsher 	/* Fill Rx ring */
1491527a6266SJeff Kirsher 	for (i = 0; i < sky2->rx_pending; i++) {
1492527a6266SJeff Kirsher 		struct rx_ring_info *re = sky2->rx_ring + i;
1493527a6266SJeff Kirsher 
1494527a6266SJeff Kirsher 		re->skb = sky2_rx_alloc(sky2, GFP_KERNEL);
1495527a6266SJeff Kirsher 		if (!re->skb)
1496527a6266SJeff Kirsher 			return -ENOMEM;
1497527a6266SJeff Kirsher 
1498527a6266SJeff Kirsher 		if (sky2_rx_map_skb(hw->pdev, re, sky2->rx_data_size)) {
1499527a6266SJeff Kirsher 			dev_kfree_skb(re->skb);
1500527a6266SJeff Kirsher 			re->skb = NULL;
1501527a6266SJeff Kirsher 			return -ENOMEM;
1502527a6266SJeff Kirsher 		}
1503527a6266SJeff Kirsher 	}
1504527a6266SJeff Kirsher 	return 0;
1505527a6266SJeff Kirsher }
1506527a6266SJeff Kirsher 
1507527a6266SJeff Kirsher /*
1508527a6266SJeff Kirsher  * Setup receiver buffer pool.
1509527a6266SJeff Kirsher  * Normal case this ends up creating one list element for skb
1510527a6266SJeff Kirsher  * in the receive ring. Worst case if using large MTU and each
1511527a6266SJeff Kirsher  * allocation falls on a different 64 bit region, that results
1512527a6266SJeff Kirsher  * in 6 list elements per ring entry.
1513527a6266SJeff Kirsher  * One element is used for checksum enable/disable, and one
1514527a6266SJeff Kirsher  * extra to avoid wrap.
1515527a6266SJeff Kirsher  */
sky2_rx_start(struct sky2_port * sky2)1516527a6266SJeff Kirsher static void sky2_rx_start(struct sky2_port *sky2)
1517527a6266SJeff Kirsher {
1518527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1519527a6266SJeff Kirsher 	struct rx_ring_info *re;
1520527a6266SJeff Kirsher 	unsigned rxq = rxqaddr[sky2->port];
1521527a6266SJeff Kirsher 	unsigned i, thresh;
1522527a6266SJeff Kirsher 
1523527a6266SJeff Kirsher 	sky2->rx_put = sky2->rx_next = 0;
1524527a6266SJeff Kirsher 	sky2_qset(hw, rxq);
1525527a6266SJeff Kirsher 
1526527a6266SJeff Kirsher 	/* On PCI express lowering the watermark gives better performance */
1527527a6266SJeff Kirsher 	if (pci_is_pcie(hw->pdev))
1528527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(rxq, Q_WM), BMU_WM_PEX);
1529527a6266SJeff Kirsher 
1530527a6266SJeff Kirsher 	/* These chips have no ram buffer?
1531df4a17a9SYangyang Li 	 * MAC Rx RAM Read is controlled by hardware
1532df4a17a9SYangyang Li 	 */
1533527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EC_U &&
1534527a6266SJeff Kirsher 	    hw->chip_rev > CHIP_REV_YU_EC_U_A0)
1535527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(rxq, Q_TEST), F_M_RX_RAM_DIS);
1536527a6266SJeff Kirsher 
1537527a6266SJeff Kirsher 	sky2_prefetch_init(hw, rxq, sky2->rx_le_map, RX_LE_SIZE - 1);
1538527a6266SJeff Kirsher 
1539527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_NEW_LE))
1540527a6266SJeff Kirsher 		rx_set_checksum(sky2);
1541527a6266SJeff Kirsher 
1542527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_RSS_BROKEN))
1543527a6266SJeff Kirsher 		rx_set_rss(sky2->netdev, sky2->netdev->features);
1544527a6266SJeff Kirsher 
1545527a6266SJeff Kirsher 	/* submit Rx ring */
1546527a6266SJeff Kirsher 	for (i = 0; i < sky2->rx_pending; i++) {
1547527a6266SJeff Kirsher 		re = sky2->rx_ring + i;
1548527a6266SJeff Kirsher 		sky2_rx_submit(sky2, re);
1549527a6266SJeff Kirsher 	}
1550527a6266SJeff Kirsher 
1551527a6266SJeff Kirsher 	/*
1552527a6266SJeff Kirsher 	 * The receiver hangs if it receives frames larger than the
1553527a6266SJeff Kirsher 	 * packet buffer. As a workaround, truncate oversize frames, but
1554527a6266SJeff Kirsher 	 * the register is limited to 9 bits, so if you do frames > 2052
1555527a6266SJeff Kirsher 	 * you better get the MTU right!
1556527a6266SJeff Kirsher 	 */
1557527a6266SJeff Kirsher 	thresh = sky2_get_rx_threshold(sky2);
1558527a6266SJeff Kirsher 	if (thresh > 0x1ff)
1559527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T), RX_TRUNC_OFF);
1560527a6266SJeff Kirsher 	else {
1561527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(sky2->port, RX_GMF_TR_THR), thresh);
1562527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T), RX_TRUNC_ON);
1563527a6266SJeff Kirsher 	}
1564527a6266SJeff Kirsher 
1565527a6266SJeff Kirsher 	/* Tell chip about available buffers */
1566527a6266SJeff Kirsher 	sky2_rx_update(sky2, rxq);
1567527a6266SJeff Kirsher 
1568527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EX ||
1569527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_SUPR) {
1570527a6266SJeff Kirsher 		/*
1571527a6266SJeff Kirsher 		 * Disable flushing of non ASF packets;
1572527a6266SJeff Kirsher 		 * must be done after initializing the BMUs;
1573527a6266SJeff Kirsher 		 * drivers without ASF support should do this too, otherwise
1574527a6266SJeff Kirsher 		 * it may happen that they cannot run on ASF devices;
1575527a6266SJeff Kirsher 		 * remember that the MAC FIFO isn't reset during initialization.
1576527a6266SJeff Kirsher 		 */
1577527a6266SJeff Kirsher 		sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T), RX_MACSEC_FLUSH_OFF);
1578527a6266SJeff Kirsher 	}
1579527a6266SJeff Kirsher 
1580527a6266SJeff Kirsher 	if (hw->chip_id >= CHIP_ID_YUKON_SUPR) {
1581527a6266SJeff Kirsher 		/* Enable RX Home Address & Routing Header checksum fix */
1582527a6266SJeff Kirsher 		sky2_write16(hw, SK_REG(sky2->port, RX_GMF_FL_CTRL),
1583527a6266SJeff Kirsher 			     RX_IPV6_SA_MOB_ENA | RX_IPV6_DA_MOB_ENA);
1584527a6266SJeff Kirsher 
1585527a6266SJeff Kirsher 		/* Enable TX Home Address & Routing Header checksum fix */
1586527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(txqaddr[sky2->port], Q_TEST),
1587527a6266SJeff Kirsher 			     TBMU_TEST_HOME_ADD_FIX_EN | TBMU_TEST_ROUTING_ADD_FIX_EN);
1588527a6266SJeff Kirsher 	}
1589527a6266SJeff Kirsher }
1590527a6266SJeff Kirsher 
sky2_alloc_buffers(struct sky2_port * sky2)1591527a6266SJeff Kirsher static int sky2_alloc_buffers(struct sky2_port *sky2)
1592527a6266SJeff Kirsher {
1593527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1594527a6266SJeff Kirsher 
1595527a6266SJeff Kirsher 	/* must be power of 2 */
1596c86768cfSChristophe JAILLET 	sky2->tx_le = dma_alloc_coherent(&hw->pdev->dev,
1597c86768cfSChristophe JAILLET 					 sky2->tx_ring_size * sizeof(struct sky2_tx_le),
1598c86768cfSChristophe JAILLET 					 &sky2->tx_le_map, GFP_KERNEL);
1599527a6266SJeff Kirsher 	if (!sky2->tx_le)
1600527a6266SJeff Kirsher 		goto nomem;
1601527a6266SJeff Kirsher 
1602527a6266SJeff Kirsher 	sky2->tx_ring = kcalloc(sky2->tx_ring_size, sizeof(struct tx_ring_info),
1603527a6266SJeff Kirsher 				GFP_KERNEL);
1604527a6266SJeff Kirsher 	if (!sky2->tx_ring)
1605527a6266SJeff Kirsher 		goto nomem;
1606527a6266SJeff Kirsher 
1607c86768cfSChristophe JAILLET 	sky2->rx_le = dma_alloc_coherent(&hw->pdev->dev, RX_LE_BYTES,
1608c86768cfSChristophe JAILLET 					 &sky2->rx_le_map, GFP_KERNEL);
1609527a6266SJeff Kirsher 	if (!sky2->rx_le)
1610527a6266SJeff Kirsher 		goto nomem;
1611527a6266SJeff Kirsher 
1612527a6266SJeff Kirsher 	sky2->rx_ring = kcalloc(sky2->rx_pending, sizeof(struct rx_ring_info),
1613527a6266SJeff Kirsher 				GFP_KERNEL);
1614527a6266SJeff Kirsher 	if (!sky2->rx_ring)
1615527a6266SJeff Kirsher 		goto nomem;
1616527a6266SJeff Kirsher 
1617527a6266SJeff Kirsher 	return sky2_alloc_rx_skbs(sky2);
1618527a6266SJeff Kirsher nomem:
1619527a6266SJeff Kirsher 	return -ENOMEM;
1620527a6266SJeff Kirsher }
1621527a6266SJeff Kirsher 
sky2_free_buffers(struct sky2_port * sky2)1622527a6266SJeff Kirsher static void sky2_free_buffers(struct sky2_port *sky2)
1623527a6266SJeff Kirsher {
1624527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1625527a6266SJeff Kirsher 
1626527a6266SJeff Kirsher 	sky2_rx_clean(sky2);
1627527a6266SJeff Kirsher 
1628527a6266SJeff Kirsher 	if (sky2->rx_le) {
1629c86768cfSChristophe JAILLET 		dma_free_coherent(&hw->pdev->dev, RX_LE_BYTES, sky2->rx_le,
1630c86768cfSChristophe JAILLET 				  sky2->rx_le_map);
1631527a6266SJeff Kirsher 		sky2->rx_le = NULL;
1632527a6266SJeff Kirsher 	}
1633527a6266SJeff Kirsher 	if (sky2->tx_le) {
1634c86768cfSChristophe JAILLET 		dma_free_coherent(&hw->pdev->dev,
1635527a6266SJeff Kirsher 				  sky2->tx_ring_size * sizeof(struct sky2_tx_le),
1636527a6266SJeff Kirsher 				  sky2->tx_le, sky2->tx_le_map);
1637527a6266SJeff Kirsher 		sky2->tx_le = NULL;
1638527a6266SJeff Kirsher 	}
1639527a6266SJeff Kirsher 	kfree(sky2->tx_ring);
1640527a6266SJeff Kirsher 	kfree(sky2->rx_ring);
1641527a6266SJeff Kirsher 
1642527a6266SJeff Kirsher 	sky2->tx_ring = NULL;
1643527a6266SJeff Kirsher 	sky2->rx_ring = NULL;
1644527a6266SJeff Kirsher }
1645527a6266SJeff Kirsher 
sky2_hw_up(struct sky2_port * sky2)1646527a6266SJeff Kirsher static void sky2_hw_up(struct sky2_port *sky2)
1647527a6266SJeff Kirsher {
1648527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1649527a6266SJeff Kirsher 	unsigned port = sky2->port;
1650527a6266SJeff Kirsher 	u32 ramsize;
1651527a6266SJeff Kirsher 	int cap;
1652527a6266SJeff Kirsher 	struct net_device *otherdev = hw->dev[sky2->port^1];
1653527a6266SJeff Kirsher 
1654527a6266SJeff Kirsher 	tx_init(sky2);
1655527a6266SJeff Kirsher 
1656527a6266SJeff Kirsher 	/*
1657527a6266SJeff Kirsher 	 * On dual port PCI-X card, there is an problem where status
1658527a6266SJeff Kirsher 	 * can be received out of order due to split transactions
1659527a6266SJeff Kirsher 	 */
1660527a6266SJeff Kirsher 	if (otherdev && netif_running(otherdev) &&
1661527a6266SJeff Kirsher 	    (cap = pci_find_capability(hw->pdev, PCI_CAP_ID_PCIX))) {
1662527a6266SJeff Kirsher 		u16 cmd;
1663527a6266SJeff Kirsher 
1664527a6266SJeff Kirsher 		cmd = sky2_pci_read16(hw, cap + PCI_X_CMD);
1665527a6266SJeff Kirsher 		cmd &= ~PCI_X_CMD_MAX_SPLIT;
1666527a6266SJeff Kirsher 		sky2_pci_write16(hw, cap + PCI_X_CMD, cmd);
1667527a6266SJeff Kirsher 	}
1668527a6266SJeff Kirsher 
1669527a6266SJeff Kirsher 	sky2_mac_init(hw, port);
1670527a6266SJeff Kirsher 
1671527a6266SJeff Kirsher 	/* Register is number of 4K blocks on internal RAM buffer. */
1672527a6266SJeff Kirsher 	ramsize = sky2_read8(hw, B2_E_0) * 4;
1673527a6266SJeff Kirsher 	if (ramsize > 0) {
1674527a6266SJeff Kirsher 		u32 rxspace;
1675527a6266SJeff Kirsher 
1676527a6266SJeff Kirsher 		netdev_dbg(sky2->netdev, "ram buffer %dK\n", ramsize);
1677527a6266SJeff Kirsher 		if (ramsize < 16)
1678527a6266SJeff Kirsher 			rxspace = ramsize / 2;
1679527a6266SJeff Kirsher 		else
1680527a6266SJeff Kirsher 			rxspace = 8 + (2*(ramsize - 16))/3;
1681527a6266SJeff Kirsher 
1682527a6266SJeff Kirsher 		sky2_ramset(hw, rxqaddr[port], 0, rxspace);
1683527a6266SJeff Kirsher 		sky2_ramset(hw, txqaddr[port], rxspace, ramsize - rxspace);
1684527a6266SJeff Kirsher 
1685527a6266SJeff Kirsher 		/* Make sure SyncQ is disabled */
1686527a6266SJeff Kirsher 		sky2_write8(hw, RB_ADDR(port == 0 ? Q_XS1 : Q_XS2, RB_CTRL),
1687527a6266SJeff Kirsher 			    RB_RST_SET);
1688527a6266SJeff Kirsher 	}
1689527a6266SJeff Kirsher 
1690527a6266SJeff Kirsher 	sky2_qset(hw, txqaddr[port]);
1691527a6266SJeff Kirsher 
1692527a6266SJeff Kirsher 	/* This is copied from sk98lin 10.0.5.3; no one tells me about erratta's */
1693527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EX && hw->chip_rev == CHIP_REV_YU_EX_B0)
1694527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(txqaddr[port], Q_TEST), F_TX_CHK_AUTO_OFF);
1695527a6266SJeff Kirsher 
1696527a6266SJeff Kirsher 	/* Set almost empty threshold */
1697527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EC_U &&
1698527a6266SJeff Kirsher 	    hw->chip_rev == CHIP_REV_YU_EC_U_A0)
1699527a6266SJeff Kirsher 		sky2_write16(hw, Q_ADDR(txqaddr[port], Q_AL), ECU_TXFF_LEV);
1700527a6266SJeff Kirsher 
1701527a6266SJeff Kirsher 	sky2_prefetch_init(hw, txqaddr[port], sky2->tx_le_map,
1702527a6266SJeff Kirsher 			   sky2->tx_ring_size - 1);
1703527a6266SJeff Kirsher 
1704527a6266SJeff Kirsher 	sky2_vlan_mode(sky2->netdev, sky2->netdev->features);
1705527a6266SJeff Kirsher 	netdev_update_features(sky2->netdev);
1706527a6266SJeff Kirsher 
1707527a6266SJeff Kirsher 	sky2_rx_start(sky2);
1708527a6266SJeff Kirsher }
1709527a6266SJeff Kirsher 
17100bdb0bd0Sstephen hemminger /* Setup device IRQ and enable napi to process */
sky2_setup_irq(struct sky2_hw * hw,const char * name)17110bdb0bd0Sstephen hemminger static int sky2_setup_irq(struct sky2_hw *hw, const char *name)
17120bdb0bd0Sstephen hemminger {
17130bdb0bd0Sstephen hemminger 	struct pci_dev *pdev = hw->pdev;
17140bdb0bd0Sstephen hemminger 	int err;
17150bdb0bd0Sstephen hemminger 
17160bdb0bd0Sstephen hemminger 	err = request_irq(pdev->irq, sky2_intr,
17170bdb0bd0Sstephen hemminger 			  (hw->flags & SKY2_HW_USE_MSI) ? 0 : IRQF_SHARED,
17180bdb0bd0Sstephen hemminger 			  name, hw);
17190bdb0bd0Sstephen hemminger 	if (err)
17200bdb0bd0Sstephen hemminger 		dev_err(&pdev->dev, "cannot assign irq %d\n", pdev->irq);
17210bdb0bd0Sstephen hemminger 	else {
1722282edcecSstephen hemminger 		hw->flags |= SKY2_HW_IRQ_SETUP;
1723282edcecSstephen hemminger 
17240bdb0bd0Sstephen hemminger 		napi_enable(&hw->napi);
17250bdb0bd0Sstephen hemminger 		sky2_write32(hw, B0_IMSK, Y2_IS_BASE);
17260bdb0bd0Sstephen hemminger 		sky2_read32(hw, B0_IMSK);
17270bdb0bd0Sstephen hemminger 	}
17280bdb0bd0Sstephen hemminger 
17290bdb0bd0Sstephen hemminger 	return err;
17300bdb0bd0Sstephen hemminger }
17310bdb0bd0Sstephen hemminger 
17320bdb0bd0Sstephen hemminger 
1733527a6266SJeff Kirsher /* Bring up network interface. */
sky2_open(struct net_device * dev)1734926d0977Sstephen hemminger static int sky2_open(struct net_device *dev)
1735527a6266SJeff Kirsher {
1736527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
1737527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1738527a6266SJeff Kirsher 	unsigned port = sky2->port;
1739527a6266SJeff Kirsher 	u32 imask;
1740527a6266SJeff Kirsher 	int err;
1741527a6266SJeff Kirsher 
1742527a6266SJeff Kirsher 	netif_carrier_off(dev);
1743527a6266SJeff Kirsher 
1744527a6266SJeff Kirsher 	err = sky2_alloc_buffers(sky2);
1745527a6266SJeff Kirsher 	if (err)
1746527a6266SJeff Kirsher 		goto err_out;
1747527a6266SJeff Kirsher 
17480bdb0bd0Sstephen hemminger 	/* With single port, IRQ is setup when device is brought up */
17490bdb0bd0Sstephen hemminger 	if (hw->ports == 1 && (err = sky2_setup_irq(hw, dev->name)))
17500bdb0bd0Sstephen hemminger 		goto err_out;
17510bdb0bd0Sstephen hemminger 
1752527a6266SJeff Kirsher 	sky2_hw_up(sky2);
1753527a6266SJeff Kirsher 
17542240eb4aSLino Sanfilippo 	/* Enable interrupts from phy/mac for port */
17552240eb4aSLino Sanfilippo 	imask = sky2_read32(hw, B0_IMSK);
17562240eb4aSLino Sanfilippo 
17571401a800Sstephen hemminger 	if (hw->chip_id == CHIP_ID_YUKON_OPT ||
17581401a800Sstephen hemminger 	    hw->chip_id == CHIP_ID_YUKON_PRM ||
17591401a800Sstephen hemminger 	    hw->chip_id == CHIP_ID_YUKON_OP_2)
17601401a800Sstephen hemminger 		imask |= Y2_IS_PHY_QLNK;	/* enable PHY Quick Link */
17611401a800Sstephen hemminger 
1762527a6266SJeff Kirsher 	imask |= portirq_msk[port];
1763527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, imask);
1764527a6266SJeff Kirsher 	sky2_read32(hw, B0_IMSK);
1765527a6266SJeff Kirsher 
1766527a6266SJeff Kirsher 	netif_info(sky2, ifup, dev, "enabling interface\n");
1767527a6266SJeff Kirsher 
1768527a6266SJeff Kirsher 	return 0;
1769527a6266SJeff Kirsher 
1770527a6266SJeff Kirsher err_out:
1771527a6266SJeff Kirsher 	sky2_free_buffers(sky2);
1772527a6266SJeff Kirsher 	return err;
1773527a6266SJeff Kirsher }
1774527a6266SJeff Kirsher 
1775527a6266SJeff Kirsher /* Modular subtraction in ring */
tx_inuse(const struct sky2_port * sky2)1776527a6266SJeff Kirsher static inline int tx_inuse(const struct sky2_port *sky2)
1777527a6266SJeff Kirsher {
1778527a6266SJeff Kirsher 	return (sky2->tx_prod - sky2->tx_cons) & (sky2->tx_ring_size - 1);
1779527a6266SJeff Kirsher }
1780527a6266SJeff Kirsher 
1781527a6266SJeff Kirsher /* Number of list elements available for next tx */
tx_avail(const struct sky2_port * sky2)1782527a6266SJeff Kirsher static inline int tx_avail(const struct sky2_port *sky2)
1783527a6266SJeff Kirsher {
1784527a6266SJeff Kirsher 	return sky2->tx_pending - tx_inuse(sky2);
1785527a6266SJeff Kirsher }
1786527a6266SJeff Kirsher 
1787527a6266SJeff Kirsher /* Estimate of number of transmit list elements required */
tx_le_req(const struct sk_buff * skb)1788527a6266SJeff Kirsher static unsigned tx_le_req(const struct sk_buff *skb)
1789527a6266SJeff Kirsher {
1790527a6266SJeff Kirsher 	unsigned count;
1791527a6266SJeff Kirsher 
1792527a6266SJeff Kirsher 	count = (skb_shinfo(skb)->nr_frags + 1)
1793527a6266SJeff Kirsher 		* (sizeof(dma_addr_t) / sizeof(u32));
1794527a6266SJeff Kirsher 
1795527a6266SJeff Kirsher 	if (skb_is_gso(skb))
1796527a6266SJeff Kirsher 		++count;
1797527a6266SJeff Kirsher 	else if (sizeof(dma_addr_t) == sizeof(u32))
1798527a6266SJeff Kirsher 		++count;	/* possible vlan */
1799527a6266SJeff Kirsher 
1800527a6266SJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1801527a6266SJeff Kirsher 		++count;
1802527a6266SJeff Kirsher 
1803527a6266SJeff Kirsher 	return count;
1804527a6266SJeff Kirsher }
1805527a6266SJeff Kirsher 
sky2_tx_unmap(struct pci_dev * pdev,struct tx_ring_info * re)1806527a6266SJeff Kirsher static void sky2_tx_unmap(struct pci_dev *pdev, struct tx_ring_info *re)
1807527a6266SJeff Kirsher {
1808527a6266SJeff Kirsher 	if (re->flags & TX_MAP_SINGLE)
1809c86768cfSChristophe JAILLET 		dma_unmap_single(&pdev->dev, dma_unmap_addr(re, mapaddr),
1810c86768cfSChristophe JAILLET 				 dma_unmap_len(re, maplen), DMA_TO_DEVICE);
1811527a6266SJeff Kirsher 	else if (re->flags & TX_MAP_PAGE)
1812c86768cfSChristophe JAILLET 		dma_unmap_page(&pdev->dev, dma_unmap_addr(re, mapaddr),
1813c86768cfSChristophe JAILLET 			       dma_unmap_len(re, maplen), DMA_TO_DEVICE);
1814527a6266SJeff Kirsher 	re->flags = 0;
1815527a6266SJeff Kirsher }
1816527a6266SJeff Kirsher 
1817527a6266SJeff Kirsher /*
1818527a6266SJeff Kirsher  * Put one packet in ring for transmit.
1819527a6266SJeff Kirsher  * A single packet can generate multiple list elements, and
1820527a6266SJeff Kirsher  * the number of ring elements will probably be less than the number
1821527a6266SJeff Kirsher  * of list elements used.
1822527a6266SJeff Kirsher  */
sky2_xmit_frame(struct sk_buff * skb,struct net_device * dev)1823527a6266SJeff Kirsher static netdev_tx_t sky2_xmit_frame(struct sk_buff *skb,
1824527a6266SJeff Kirsher 				   struct net_device *dev)
1825527a6266SJeff Kirsher {
1826527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
1827527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
1828527a6266SJeff Kirsher 	struct sky2_tx_le *le = NULL;
1829527a6266SJeff Kirsher 	struct tx_ring_info *re;
1830527a6266SJeff Kirsher 	unsigned i, len;
1831527a6266SJeff Kirsher 	dma_addr_t mapping;
1832527a6266SJeff Kirsher 	u32 upper;
1833527a6266SJeff Kirsher 	u16 slot;
1834527a6266SJeff Kirsher 	u16 mss;
1835527a6266SJeff Kirsher 	u8 ctrl;
1836527a6266SJeff Kirsher 
1837527a6266SJeff Kirsher 	if (unlikely(tx_avail(sky2) < tx_le_req(skb)))
1838527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
1839527a6266SJeff Kirsher 
1840527a6266SJeff Kirsher 	len = skb_headlen(skb);
1841c86768cfSChristophe JAILLET 	mapping = dma_map_single(&hw->pdev->dev, skb->data, len,
1842c86768cfSChristophe JAILLET 				 DMA_TO_DEVICE);
1843527a6266SJeff Kirsher 
1844c86768cfSChristophe JAILLET 	if (dma_mapping_error(&hw->pdev->dev, mapping))
1845527a6266SJeff Kirsher 		goto mapping_error;
1846527a6266SJeff Kirsher 
1847527a6266SJeff Kirsher 	slot = sky2->tx_prod;
1848527a6266SJeff Kirsher 	netif_printk(sky2, tx_queued, KERN_DEBUG, dev,
1849527a6266SJeff Kirsher 		     "tx queued, slot %u, len %d\n", slot, skb->len);
1850527a6266SJeff Kirsher 
1851527a6266SJeff Kirsher 	/* Send high bits if needed */
1852527a6266SJeff Kirsher 	upper = upper_32_bits(mapping);
1853527a6266SJeff Kirsher 	if (upper != sky2->tx_last_upper) {
1854527a6266SJeff Kirsher 		le = get_tx_le(sky2, &slot);
1855527a6266SJeff Kirsher 		le->addr = cpu_to_le32(upper);
1856527a6266SJeff Kirsher 		sky2->tx_last_upper = upper;
1857527a6266SJeff Kirsher 		le->opcode = OP_ADDR64 | HW_OWNER;
1858527a6266SJeff Kirsher 	}
1859527a6266SJeff Kirsher 
1860527a6266SJeff Kirsher 	/* Check for TCP Segmentation Offload */
1861527a6266SJeff Kirsher 	mss = skb_shinfo(skb)->gso_size;
1862527a6266SJeff Kirsher 	if (mss != 0) {
1863527a6266SJeff Kirsher 
1864527a6266SJeff Kirsher 		if (!(hw->flags & SKY2_HW_NEW_LE))
1865504148feSEric Dumazet 			mss += skb_tcp_all_headers(skb);
1866527a6266SJeff Kirsher 
1867527a6266SJeff Kirsher 		if (mss != sky2->tx_last_mss) {
1868527a6266SJeff Kirsher 			le = get_tx_le(sky2, &slot);
1869527a6266SJeff Kirsher 			le->addr = cpu_to_le32(mss);
1870527a6266SJeff Kirsher 
1871527a6266SJeff Kirsher 			if (hw->flags & SKY2_HW_NEW_LE)
1872527a6266SJeff Kirsher 				le->opcode = OP_MSS | HW_OWNER;
1873527a6266SJeff Kirsher 			else
1874527a6266SJeff Kirsher 				le->opcode = OP_LRGLEN | HW_OWNER;
1875527a6266SJeff Kirsher 			sky2->tx_last_mss = mss;
1876527a6266SJeff Kirsher 		}
1877527a6266SJeff Kirsher 	}
1878527a6266SJeff Kirsher 
1879527a6266SJeff Kirsher 	ctrl = 0;
1880527a6266SJeff Kirsher 
1881527a6266SJeff Kirsher 	/* Add VLAN tag, can piggyback on LRGLEN or ADDR64 */
1882df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
1883527a6266SJeff Kirsher 		if (!le) {
1884527a6266SJeff Kirsher 			le = get_tx_le(sky2, &slot);
1885527a6266SJeff Kirsher 			le->addr = 0;
1886527a6266SJeff Kirsher 			le->opcode = OP_VLAN|HW_OWNER;
1887527a6266SJeff Kirsher 		} else
1888527a6266SJeff Kirsher 			le->opcode |= OP_VLAN;
1889df8a39deSJiri Pirko 		le->length = cpu_to_be16(skb_vlan_tag_get(skb));
1890527a6266SJeff Kirsher 		ctrl |= INS_VLAN;
1891527a6266SJeff Kirsher 	}
1892527a6266SJeff Kirsher 
1893527a6266SJeff Kirsher 	/* Handle TCP checksum offload */
1894527a6266SJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1895527a6266SJeff Kirsher 		/* On Yukon EX (some versions) encoding change. */
1896527a6266SJeff Kirsher 		if (hw->flags & SKY2_HW_AUTO_TX_SUM)
1897527a6266SJeff Kirsher 			ctrl |= CALSUM;	/* auto checksum */
1898527a6266SJeff Kirsher 		else {
1899527a6266SJeff Kirsher 			const unsigned offset = skb_transport_offset(skb);
1900527a6266SJeff Kirsher 			u32 tcpsum;
1901527a6266SJeff Kirsher 
1902527a6266SJeff Kirsher 			tcpsum = offset << 16;			/* sum start */
1903527a6266SJeff Kirsher 			tcpsum |= offset + skb->csum_offset;	/* sum write */
1904527a6266SJeff Kirsher 
1905527a6266SJeff Kirsher 			ctrl |= CALSUM | WR_SUM | INIT_SUM | LOCK_SUM;
1906527a6266SJeff Kirsher 			if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1907527a6266SJeff Kirsher 				ctrl |= UDPTCP;
1908527a6266SJeff Kirsher 
1909527a6266SJeff Kirsher 			if (tcpsum != sky2->tx_tcpsum) {
1910527a6266SJeff Kirsher 				sky2->tx_tcpsum = tcpsum;
1911527a6266SJeff Kirsher 
1912527a6266SJeff Kirsher 				le = get_tx_le(sky2, &slot);
1913527a6266SJeff Kirsher 				le->addr = cpu_to_le32(tcpsum);
1914527a6266SJeff Kirsher 				le->length = 0;	/* initial checksum value */
1915527a6266SJeff Kirsher 				le->ctrl = 1;	/* one packet */
1916527a6266SJeff Kirsher 				le->opcode = OP_TCPLISW | HW_OWNER;
1917527a6266SJeff Kirsher 			}
1918527a6266SJeff Kirsher 		}
1919527a6266SJeff Kirsher 	}
1920527a6266SJeff Kirsher 
1921527a6266SJeff Kirsher 	re = sky2->tx_ring + slot;
1922527a6266SJeff Kirsher 	re->flags = TX_MAP_SINGLE;
1923527a6266SJeff Kirsher 	dma_unmap_addr_set(re, mapaddr, mapping);
1924527a6266SJeff Kirsher 	dma_unmap_len_set(re, maplen, len);
1925527a6266SJeff Kirsher 
1926527a6266SJeff Kirsher 	le = get_tx_le(sky2, &slot);
1927527a6266SJeff Kirsher 	le->addr = cpu_to_le32(lower_32_bits(mapping));
1928527a6266SJeff Kirsher 	le->length = cpu_to_le16(len);
1929527a6266SJeff Kirsher 	le->ctrl = ctrl;
1930527a6266SJeff Kirsher 	le->opcode = mss ? (OP_LARGESEND | HW_OWNER) : (OP_PACKET | HW_OWNER);
1931527a6266SJeff Kirsher 
1932527a6266SJeff Kirsher 
1933527a6266SJeff Kirsher 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1934527a6266SJeff Kirsher 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1935527a6266SJeff Kirsher 
1936950a5a4fSIan Campbell 		mapping = skb_frag_dma_map(&hw->pdev->dev, frag, 0,
19379e903e08SEric Dumazet 					   skb_frag_size(frag), DMA_TO_DEVICE);
1938527a6266SJeff Kirsher 
19395d6bcdfeSIan Campbell 		if (dma_mapping_error(&hw->pdev->dev, mapping))
1940527a6266SJeff Kirsher 			goto mapping_unwind;
1941527a6266SJeff Kirsher 
1942527a6266SJeff Kirsher 		upper = upper_32_bits(mapping);
1943527a6266SJeff Kirsher 		if (upper != sky2->tx_last_upper) {
1944527a6266SJeff Kirsher 			le = get_tx_le(sky2, &slot);
1945527a6266SJeff Kirsher 			le->addr = cpu_to_le32(upper);
1946527a6266SJeff Kirsher 			sky2->tx_last_upper = upper;
1947527a6266SJeff Kirsher 			le->opcode = OP_ADDR64 | HW_OWNER;
1948527a6266SJeff Kirsher 		}
1949527a6266SJeff Kirsher 
1950527a6266SJeff Kirsher 		re = sky2->tx_ring + slot;
1951527a6266SJeff Kirsher 		re->flags = TX_MAP_PAGE;
1952527a6266SJeff Kirsher 		dma_unmap_addr_set(re, mapaddr, mapping);
19539e903e08SEric Dumazet 		dma_unmap_len_set(re, maplen, skb_frag_size(frag));
1954527a6266SJeff Kirsher 
1955527a6266SJeff Kirsher 		le = get_tx_le(sky2, &slot);
1956527a6266SJeff Kirsher 		le->addr = cpu_to_le32(lower_32_bits(mapping));
19579e903e08SEric Dumazet 		le->length = cpu_to_le16(skb_frag_size(frag));
1958527a6266SJeff Kirsher 		le->ctrl = ctrl;
1959527a6266SJeff Kirsher 		le->opcode = OP_BUFFER | HW_OWNER;
1960527a6266SJeff Kirsher 	}
1961527a6266SJeff Kirsher 
1962527a6266SJeff Kirsher 	re->skb = skb;
1963527a6266SJeff Kirsher 	le->ctrl |= EOP;
1964527a6266SJeff Kirsher 
1965527a6266SJeff Kirsher 	sky2->tx_prod = slot;
1966527a6266SJeff Kirsher 
1967527a6266SJeff Kirsher 	if (tx_avail(sky2) <= MAX_SKB_TX_LE)
1968527a6266SJeff Kirsher 		netif_stop_queue(dev);
1969527a6266SJeff Kirsher 
1970ec2a5466Sstephen hemminger 	netdev_sent_queue(dev, skb->len);
1971527a6266SJeff Kirsher 	sky2_put_idx(hw, txqaddr[sky2->port], sky2->tx_prod);
1972527a6266SJeff Kirsher 
1973527a6266SJeff Kirsher 	return NETDEV_TX_OK;
1974527a6266SJeff Kirsher 
1975527a6266SJeff Kirsher mapping_unwind:
1976527a6266SJeff Kirsher 	for (i = sky2->tx_prod; i != slot; i = RING_NEXT(i, sky2->tx_ring_size)) {
1977527a6266SJeff Kirsher 		re = sky2->tx_ring + i;
1978527a6266SJeff Kirsher 
1979527a6266SJeff Kirsher 		sky2_tx_unmap(hw->pdev, re);
1980527a6266SJeff Kirsher 	}
1981527a6266SJeff Kirsher 
1982527a6266SJeff Kirsher mapping_error:
1983527a6266SJeff Kirsher 	if (net_ratelimit())
1984527a6266SJeff Kirsher 		dev_warn(&hw->pdev->dev, "%s: tx mapping error\n", dev->name);
19852d4186ceSEric W. Biederman 	dev_kfree_skb_any(skb);
1986527a6266SJeff Kirsher 	return NETDEV_TX_OK;
1987527a6266SJeff Kirsher }
1988527a6266SJeff Kirsher 
1989527a6266SJeff Kirsher /*
1990527a6266SJeff Kirsher  * Free ring elements from starting at tx_cons until "done"
1991527a6266SJeff Kirsher  *
1992527a6266SJeff Kirsher  * NB:
1993527a6266SJeff Kirsher  *  1. The hardware will tell us about partial completion of multi-part
1994527a6266SJeff Kirsher  *     buffers so make sure not to free skb to early.
1995527a6266SJeff Kirsher  *  2. This may run in parallel start_xmit because the it only
1996527a6266SJeff Kirsher  *     looks at the tail of the queue of FIFO (tx_cons), not
1997527a6266SJeff Kirsher  *     the head (tx_prod)
1998527a6266SJeff Kirsher  */
sky2_tx_complete(struct sky2_port * sky2,u16 done)1999527a6266SJeff Kirsher static void sky2_tx_complete(struct sky2_port *sky2, u16 done)
2000527a6266SJeff Kirsher {
2001527a6266SJeff Kirsher 	struct net_device *dev = sky2->netdev;
2002ec2a5466Sstephen hemminger 	u16 idx;
2003ec2a5466Sstephen hemminger 	unsigned int bytes_compl = 0, pkts_compl = 0;
2004527a6266SJeff Kirsher 
2005527a6266SJeff Kirsher 	BUG_ON(done >= sky2->tx_ring_size);
2006527a6266SJeff Kirsher 
2007527a6266SJeff Kirsher 	for (idx = sky2->tx_cons; idx != done;
2008527a6266SJeff Kirsher 	     idx = RING_NEXT(idx, sky2->tx_ring_size)) {
2009527a6266SJeff Kirsher 		struct tx_ring_info *re = sky2->tx_ring + idx;
2010527a6266SJeff Kirsher 		struct sk_buff *skb = re->skb;
2011527a6266SJeff Kirsher 
2012527a6266SJeff Kirsher 		sky2_tx_unmap(sky2->hw->pdev, re);
2013527a6266SJeff Kirsher 
2014527a6266SJeff Kirsher 		if (skb) {
2015527a6266SJeff Kirsher 			netif_printk(sky2, tx_done, KERN_DEBUG, dev,
2016527a6266SJeff Kirsher 				     "tx done %u\n", idx);
2017527a6266SJeff Kirsher 
2018ec2a5466Sstephen hemminger 			pkts_compl++;
2019ec2a5466Sstephen hemminger 			bytes_compl += skb->len;
2020527a6266SJeff Kirsher 
2021527a6266SJeff Kirsher 			re->skb = NULL;
2022527a6266SJeff Kirsher 			dev_kfree_skb_any(skb);
2023527a6266SJeff Kirsher 
2024527a6266SJeff Kirsher 			sky2->tx_next = RING_NEXT(idx, sky2->tx_ring_size);
2025527a6266SJeff Kirsher 		}
2026527a6266SJeff Kirsher 	}
2027527a6266SJeff Kirsher 
2028527a6266SJeff Kirsher 	sky2->tx_cons = idx;
2029527a6266SJeff Kirsher 	smp_mb();
2030ec2a5466Sstephen hemminger 
2031ec2a5466Sstephen hemminger 	netdev_completed_queue(dev, pkts_compl, bytes_compl);
2032ec2a5466Sstephen hemminger 
2033ec2a5466Sstephen hemminger 	u64_stats_update_begin(&sky2->tx_stats.syncp);
2034ec2a5466Sstephen hemminger 	sky2->tx_stats.packets += pkts_compl;
2035ec2a5466Sstephen hemminger 	sky2->tx_stats.bytes += bytes_compl;
2036ec2a5466Sstephen hemminger 	u64_stats_update_end(&sky2->tx_stats.syncp);
2037527a6266SJeff Kirsher }
2038527a6266SJeff Kirsher 
sky2_tx_reset(struct sky2_hw * hw,unsigned port)2039527a6266SJeff Kirsher static void sky2_tx_reset(struct sky2_hw *hw, unsigned port)
2040527a6266SJeff Kirsher {
2041527a6266SJeff Kirsher 	/* Disable Force Sync bit and Enable Alloc bit */
2042527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, TXA_CTRL),
2043527a6266SJeff Kirsher 		    TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
2044527a6266SJeff Kirsher 
2045527a6266SJeff Kirsher 	/* Stop Interval Timer and Limit Counter of Tx Arbiter */
2046527a6266SJeff Kirsher 	sky2_write32(hw, SK_REG(port, TXA_ITI_INI), 0L);
2047527a6266SJeff Kirsher 	sky2_write32(hw, SK_REG(port, TXA_LIM_INI), 0L);
2048527a6266SJeff Kirsher 
2049527a6266SJeff Kirsher 	/* Reset the PCI FIFO of the async Tx queue */
2050527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR),
2051527a6266SJeff Kirsher 		     BMU_RST_SET | BMU_FIFO_RST);
2052527a6266SJeff Kirsher 
2053527a6266SJeff Kirsher 	/* Reset the Tx prefetch units */
2054527a6266SJeff Kirsher 	sky2_write32(hw, Y2_QADDR(txqaddr[port], PREF_UNIT_CTRL),
2055527a6266SJeff Kirsher 		     PREF_UNIT_RST_SET);
2056527a6266SJeff Kirsher 
2057527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL), RB_RST_SET);
2058527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_SET);
2059f9687c44Sstephen hemminger 
2060f9687c44Sstephen hemminger 	sky2_read32(hw, B0_CTST);
2061527a6266SJeff Kirsher }
2062527a6266SJeff Kirsher 
sky2_hw_down(struct sky2_port * sky2)2063527a6266SJeff Kirsher static void sky2_hw_down(struct sky2_port *sky2)
2064527a6266SJeff Kirsher {
2065527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2066527a6266SJeff Kirsher 	unsigned port = sky2->port;
2067527a6266SJeff Kirsher 	u16 ctrl;
2068527a6266SJeff Kirsher 
2069527a6266SJeff Kirsher 	/* Force flow control off */
2070527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
2071527a6266SJeff Kirsher 
2072527a6266SJeff Kirsher 	/* Stop transmitter */
2073527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_STOP);
2074527a6266SJeff Kirsher 	sky2_read32(hw, Q_ADDR(txqaddr[port], Q_CSR));
2075527a6266SJeff Kirsher 
2076527a6266SJeff Kirsher 	sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL),
2077527a6266SJeff Kirsher 		     RB_RST_SET | RB_DIS_OP_MD);
2078527a6266SJeff Kirsher 
2079527a6266SJeff Kirsher 	ctrl = gma_read16(hw, port, GM_GP_CTRL);
2080527a6266SJeff Kirsher 	ctrl &= ~(GM_GPCR_TX_ENA | GM_GPCR_RX_ENA);
2081527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL, ctrl);
2082527a6266SJeff Kirsher 
2083527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
2084527a6266SJeff Kirsher 
2085527a6266SJeff Kirsher 	/* Workaround shared GMAC reset */
2086527a6266SJeff Kirsher 	if (!(hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0 &&
2087527a6266SJeff Kirsher 	      port == 0 && hw->dev[1] && netif_running(hw->dev[1])))
2088527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
2089527a6266SJeff Kirsher 
2090527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
2091527a6266SJeff Kirsher 
20928a9ea323SLinus Torvalds 	/* Force any delayed status interrupt and NAPI */
2093527a6266SJeff Kirsher 	sky2_write32(hw, STAT_LEV_TIMER_CNT, 0);
2094527a6266SJeff Kirsher 	sky2_write32(hw, STAT_TX_TIMER_CNT, 0);
2095527a6266SJeff Kirsher 	sky2_write32(hw, STAT_ISR_TIMER_CNT, 0);
2096527a6266SJeff Kirsher 	sky2_read8(hw, STAT_ISR_TIMER_CTRL);
2097527a6266SJeff Kirsher 
2098527a6266SJeff Kirsher 	sky2_rx_stop(sky2);
2099527a6266SJeff Kirsher 
2100527a6266SJeff Kirsher 	spin_lock_bh(&sky2->phy_lock);
2101527a6266SJeff Kirsher 	sky2_phy_power_down(hw, port);
2102527a6266SJeff Kirsher 	spin_unlock_bh(&sky2->phy_lock);
2103527a6266SJeff Kirsher 
2104527a6266SJeff Kirsher 	sky2_tx_reset(hw, port);
2105527a6266SJeff Kirsher 
2106527a6266SJeff Kirsher 	/* Free any pending frames stuck in HW queue */
2107527a6266SJeff Kirsher 	sky2_tx_complete(sky2, sky2->tx_prod);
2108527a6266SJeff Kirsher }
2109527a6266SJeff Kirsher 
2110527a6266SJeff Kirsher /* Network shutdown */
sky2_close(struct net_device * dev)2111926d0977Sstephen hemminger static int sky2_close(struct net_device *dev)
2112527a6266SJeff Kirsher {
2113527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2114527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2115527a6266SJeff Kirsher 
2116527a6266SJeff Kirsher 	/* Never really got started! */
2117527a6266SJeff Kirsher 	if (!sky2->tx_le)
2118527a6266SJeff Kirsher 		return 0;
2119527a6266SJeff Kirsher 
2120527a6266SJeff Kirsher 	netif_info(sky2, ifdown, dev, "disabling interface\n");
2121527a6266SJeff Kirsher 
21221401a800Sstephen hemminger 	if (hw->ports == 1) {
21231401a800Sstephen hemminger 		sky2_write32(hw, B0_IMSK, 0);
2124527a6266SJeff Kirsher 		sky2_read32(hw, B0_IMSK);
2125527a6266SJeff Kirsher 
21260bdb0bd0Sstephen hemminger 		napi_disable(&hw->napi);
21270bdb0bd0Sstephen hemminger 		free_irq(hw->pdev->irq, hw);
2128282edcecSstephen hemminger 		hw->flags &= ~SKY2_HW_IRQ_SETUP;
21290bdb0bd0Sstephen hemminger 	} else {
21301401a800Sstephen hemminger 		u32 imask;
21311401a800Sstephen hemminger 
21321401a800Sstephen hemminger 		/* Disable port IRQ */
21331401a800Sstephen hemminger 		imask  = sky2_read32(hw, B0_IMSK);
21341401a800Sstephen hemminger 		imask &= ~portirq_msk[sky2->port];
21351401a800Sstephen hemminger 		sky2_write32(hw, B0_IMSK, imask);
21361401a800Sstephen hemminger 		sky2_read32(hw, B0_IMSK);
21371401a800Sstephen hemminger 
2138527a6266SJeff Kirsher 		synchronize_irq(hw->pdev->irq);
2139527a6266SJeff Kirsher 		napi_synchronize(&hw->napi);
21400bdb0bd0Sstephen hemminger 	}
2141527a6266SJeff Kirsher 
2142527a6266SJeff Kirsher 	sky2_hw_down(sky2);
2143527a6266SJeff Kirsher 
2144527a6266SJeff Kirsher 	sky2_free_buffers(sky2);
2145527a6266SJeff Kirsher 
2146527a6266SJeff Kirsher 	return 0;
2147527a6266SJeff Kirsher }
2148527a6266SJeff Kirsher 
sky2_phy_speed(const struct sky2_hw * hw,u16 aux)2149527a6266SJeff Kirsher static u16 sky2_phy_speed(const struct sky2_hw *hw, u16 aux)
2150527a6266SJeff Kirsher {
2151527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_FIBRE_PHY)
2152527a6266SJeff Kirsher 		return SPEED_1000;
2153527a6266SJeff Kirsher 
2154527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_GIGABIT)) {
2155527a6266SJeff Kirsher 		if (aux & PHY_M_PS_SPEED_100)
2156527a6266SJeff Kirsher 			return SPEED_100;
2157527a6266SJeff Kirsher 		else
2158527a6266SJeff Kirsher 			return SPEED_10;
2159527a6266SJeff Kirsher 	}
2160527a6266SJeff Kirsher 
2161527a6266SJeff Kirsher 	switch (aux & PHY_M_PS_SPEED_MSK) {
2162527a6266SJeff Kirsher 	case PHY_M_PS_SPEED_1000:
2163527a6266SJeff Kirsher 		return SPEED_1000;
2164527a6266SJeff Kirsher 	case PHY_M_PS_SPEED_100:
2165527a6266SJeff Kirsher 		return SPEED_100;
2166527a6266SJeff Kirsher 	default:
2167527a6266SJeff Kirsher 		return SPEED_10;
2168527a6266SJeff Kirsher 	}
2169527a6266SJeff Kirsher }
2170527a6266SJeff Kirsher 
sky2_link_up(struct sky2_port * sky2)2171527a6266SJeff Kirsher static void sky2_link_up(struct sky2_port *sky2)
2172527a6266SJeff Kirsher {
2173527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2174527a6266SJeff Kirsher 	unsigned port = sky2->port;
2175527a6266SJeff Kirsher 	static const char *fc_name[] = {
2176527a6266SJeff Kirsher 		[FC_NONE]	= "none",
2177527a6266SJeff Kirsher 		[FC_TX]		= "tx",
2178527a6266SJeff Kirsher 		[FC_RX]		= "rx",
2179527a6266SJeff Kirsher 		[FC_BOTH]	= "both",
2180527a6266SJeff Kirsher 	};
2181527a6266SJeff Kirsher 
2182527a6266SJeff Kirsher 	sky2_set_ipg(sky2);
2183527a6266SJeff Kirsher 
2184527a6266SJeff Kirsher 	sky2_enable_rx_tx(sky2);
2185527a6266SJeff Kirsher 
2186527a6266SJeff Kirsher 	gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
2187527a6266SJeff Kirsher 
2188527a6266SJeff Kirsher 	netif_carrier_on(sky2->netdev);
2189527a6266SJeff Kirsher 
2190527a6266SJeff Kirsher 	mod_timer(&hw->watchdog_timer, jiffies + 1);
2191527a6266SJeff Kirsher 
2192527a6266SJeff Kirsher 	/* Turn on link LED */
2193527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, LNK_LED_REG),
2194527a6266SJeff Kirsher 		    LINKLED_ON | LINKLED_BLINK_OFF | LINKLED_LINKSYNC_OFF);
2195527a6266SJeff Kirsher 
2196527a6266SJeff Kirsher 	netif_info(sky2, link, sky2->netdev,
2197527a6266SJeff Kirsher 		   "Link is up at %d Mbps, %s duplex, flow control %s\n",
2198527a6266SJeff Kirsher 		   sky2->speed,
2199527a6266SJeff Kirsher 		   sky2->duplex == DUPLEX_FULL ? "full" : "half",
2200527a6266SJeff Kirsher 		   fc_name[sky2->flow_status]);
2201527a6266SJeff Kirsher }
2202527a6266SJeff Kirsher 
sky2_link_down(struct sky2_port * sky2)2203527a6266SJeff Kirsher static void sky2_link_down(struct sky2_port *sky2)
2204527a6266SJeff Kirsher {
2205527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2206527a6266SJeff Kirsher 	unsigned port = sky2->port;
2207527a6266SJeff Kirsher 	u16 reg;
2208527a6266SJeff Kirsher 
2209527a6266SJeff Kirsher 	gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);
2210527a6266SJeff Kirsher 
2211527a6266SJeff Kirsher 	reg = gma_read16(hw, port, GM_GP_CTRL);
2212527a6266SJeff Kirsher 	reg &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
2213527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL, reg);
2214527a6266SJeff Kirsher 
2215527a6266SJeff Kirsher 	netif_carrier_off(sky2->netdev);
2216527a6266SJeff Kirsher 
2217527a6266SJeff Kirsher 	/* Turn off link LED */
2218527a6266SJeff Kirsher 	sky2_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_OFF);
2219527a6266SJeff Kirsher 
2220527a6266SJeff Kirsher 	netif_info(sky2, link, sky2->netdev, "Link is down\n");
2221527a6266SJeff Kirsher 
2222527a6266SJeff Kirsher 	sky2_phy_init(hw, port);
2223527a6266SJeff Kirsher }
2224527a6266SJeff Kirsher 
sky2_flow(int rx,int tx)2225527a6266SJeff Kirsher static enum flow_control sky2_flow(int rx, int tx)
2226527a6266SJeff Kirsher {
2227527a6266SJeff Kirsher 	if (rx)
2228527a6266SJeff Kirsher 		return tx ? FC_BOTH : FC_RX;
2229527a6266SJeff Kirsher 	else
2230527a6266SJeff Kirsher 		return tx ? FC_TX : FC_NONE;
2231527a6266SJeff Kirsher }
2232527a6266SJeff Kirsher 
sky2_autoneg_done(struct sky2_port * sky2,u16 aux)2233527a6266SJeff Kirsher static int sky2_autoneg_done(struct sky2_port *sky2, u16 aux)
2234527a6266SJeff Kirsher {
2235527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2236527a6266SJeff Kirsher 	unsigned port = sky2->port;
2237527a6266SJeff Kirsher 	u16 advert, lpa;
2238527a6266SJeff Kirsher 
2239527a6266SJeff Kirsher 	advert = gm_phy_read(hw, port, PHY_MARV_AUNE_ADV);
2240527a6266SJeff Kirsher 	lpa = gm_phy_read(hw, port, PHY_MARV_AUNE_LP);
2241527a6266SJeff Kirsher 	if (lpa & PHY_M_AN_RF) {
2242527a6266SJeff Kirsher 		netdev_err(sky2->netdev, "remote fault\n");
2243527a6266SJeff Kirsher 		return -1;
2244527a6266SJeff Kirsher 	}
2245527a6266SJeff Kirsher 
2246527a6266SJeff Kirsher 	if (!(aux & PHY_M_PS_SPDUP_RES)) {
2247527a6266SJeff Kirsher 		netdev_err(sky2->netdev, "speed/duplex mismatch\n");
2248527a6266SJeff Kirsher 		return -1;
2249527a6266SJeff Kirsher 	}
2250527a6266SJeff Kirsher 
2251527a6266SJeff Kirsher 	sky2->speed = sky2_phy_speed(hw, aux);
2252527a6266SJeff Kirsher 	sky2->duplex = (aux & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
2253527a6266SJeff Kirsher 
2254527a6266SJeff Kirsher 	/* Since the pause result bits seem to in different positions on
2255527a6266SJeff Kirsher 	 * different chips. look at registers.
2256527a6266SJeff Kirsher 	 */
2257527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_FIBRE_PHY) {
2258527a6266SJeff Kirsher 		/* Shift for bits in fiber PHY */
2259527a6266SJeff Kirsher 		advert &= ~(ADVERTISE_PAUSE_CAP|ADVERTISE_PAUSE_ASYM);
2260527a6266SJeff Kirsher 		lpa &= ~(LPA_PAUSE_CAP|LPA_PAUSE_ASYM);
2261527a6266SJeff Kirsher 
2262527a6266SJeff Kirsher 		if (advert & ADVERTISE_1000XPAUSE)
2263527a6266SJeff Kirsher 			advert |= ADVERTISE_PAUSE_CAP;
2264527a6266SJeff Kirsher 		if (advert & ADVERTISE_1000XPSE_ASYM)
2265527a6266SJeff Kirsher 			advert |= ADVERTISE_PAUSE_ASYM;
2266527a6266SJeff Kirsher 		if (lpa & LPA_1000XPAUSE)
2267527a6266SJeff Kirsher 			lpa |= LPA_PAUSE_CAP;
2268527a6266SJeff Kirsher 		if (lpa & LPA_1000XPAUSE_ASYM)
2269527a6266SJeff Kirsher 			lpa |= LPA_PAUSE_ASYM;
2270527a6266SJeff Kirsher 	}
2271527a6266SJeff Kirsher 
2272527a6266SJeff Kirsher 	sky2->flow_status = FC_NONE;
2273527a6266SJeff Kirsher 	if (advert & ADVERTISE_PAUSE_CAP) {
2274527a6266SJeff Kirsher 		if (lpa & LPA_PAUSE_CAP)
2275527a6266SJeff Kirsher 			sky2->flow_status = FC_BOTH;
2276527a6266SJeff Kirsher 		else if (advert & ADVERTISE_PAUSE_ASYM)
2277527a6266SJeff Kirsher 			sky2->flow_status = FC_RX;
2278527a6266SJeff Kirsher 	} else if (advert & ADVERTISE_PAUSE_ASYM) {
2279527a6266SJeff Kirsher 		if ((lpa & LPA_PAUSE_CAP) && (lpa & LPA_PAUSE_ASYM))
2280527a6266SJeff Kirsher 			sky2->flow_status = FC_TX;
2281527a6266SJeff Kirsher 	}
2282527a6266SJeff Kirsher 
2283527a6266SJeff Kirsher 	if (sky2->duplex == DUPLEX_HALF && sky2->speed < SPEED_1000 &&
2284527a6266SJeff Kirsher 	    !(hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX))
2285527a6266SJeff Kirsher 		sky2->flow_status = FC_NONE;
2286527a6266SJeff Kirsher 
2287527a6266SJeff Kirsher 	if (sky2->flow_status & FC_TX)
2288527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
2289527a6266SJeff Kirsher 	else
2290527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
2291527a6266SJeff Kirsher 
2292527a6266SJeff Kirsher 	return 0;
2293527a6266SJeff Kirsher }
2294527a6266SJeff Kirsher 
2295527a6266SJeff Kirsher /* Interrupt from PHY */
sky2_phy_intr(struct sky2_hw * hw,unsigned port)2296527a6266SJeff Kirsher static void sky2_phy_intr(struct sky2_hw *hw, unsigned port)
2297527a6266SJeff Kirsher {
2298527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
2299527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2300527a6266SJeff Kirsher 	u16 istatus, phystat;
2301527a6266SJeff Kirsher 
2302527a6266SJeff Kirsher 	if (!netif_running(dev))
2303527a6266SJeff Kirsher 		return;
2304527a6266SJeff Kirsher 
2305527a6266SJeff Kirsher 	spin_lock(&sky2->phy_lock);
2306527a6266SJeff Kirsher 	istatus = gm_phy_read(hw, port, PHY_MARV_INT_STAT);
2307527a6266SJeff Kirsher 	phystat = gm_phy_read(hw, port, PHY_MARV_PHY_STAT);
2308527a6266SJeff Kirsher 
2309527a6266SJeff Kirsher 	netif_info(sky2, intr, sky2->netdev, "phy interrupt status 0x%x 0x%x\n",
2310527a6266SJeff Kirsher 		   istatus, phystat);
2311527a6266SJeff Kirsher 
2312527a6266SJeff Kirsher 	if (istatus & PHY_M_IS_AN_COMPL) {
2313527a6266SJeff Kirsher 		if (sky2_autoneg_done(sky2, phystat) == 0 &&
2314527a6266SJeff Kirsher 		    !netif_carrier_ok(dev))
2315527a6266SJeff Kirsher 			sky2_link_up(sky2);
2316527a6266SJeff Kirsher 		goto out;
2317527a6266SJeff Kirsher 	}
2318527a6266SJeff Kirsher 
2319527a6266SJeff Kirsher 	if (istatus & PHY_M_IS_LSP_CHANGE)
2320527a6266SJeff Kirsher 		sky2->speed = sky2_phy_speed(hw, phystat);
2321527a6266SJeff Kirsher 
2322527a6266SJeff Kirsher 	if (istatus & PHY_M_IS_DUP_CHANGE)
2323527a6266SJeff Kirsher 		sky2->duplex =
2324527a6266SJeff Kirsher 		    (phystat & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
2325527a6266SJeff Kirsher 
2326527a6266SJeff Kirsher 	if (istatus & PHY_M_IS_LST_CHANGE) {
2327527a6266SJeff Kirsher 		if (phystat & PHY_M_PS_LINK_UP)
2328527a6266SJeff Kirsher 			sky2_link_up(sky2);
2329527a6266SJeff Kirsher 		else
2330527a6266SJeff Kirsher 			sky2_link_down(sky2);
2331527a6266SJeff Kirsher 	}
2332527a6266SJeff Kirsher out:
2333527a6266SJeff Kirsher 	spin_unlock(&sky2->phy_lock);
2334527a6266SJeff Kirsher }
2335527a6266SJeff Kirsher 
2336527a6266SJeff Kirsher /* Special quick link interrupt (Yukon-2 Optima only) */
sky2_qlink_intr(struct sky2_hw * hw)2337527a6266SJeff Kirsher static void sky2_qlink_intr(struct sky2_hw *hw)
2338527a6266SJeff Kirsher {
2339527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(hw->dev[0]);
2340527a6266SJeff Kirsher 	u32 imask;
2341527a6266SJeff Kirsher 	u16 phy;
2342527a6266SJeff Kirsher 
2343527a6266SJeff Kirsher 	/* disable irq */
2344527a6266SJeff Kirsher 	imask = sky2_read32(hw, B0_IMSK);
2345527a6266SJeff Kirsher 	imask &= ~Y2_IS_PHY_QLNK;
2346527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, imask);
2347527a6266SJeff Kirsher 
2348527a6266SJeff Kirsher 	/* reset PHY Link Detect */
2349527a6266SJeff Kirsher 	phy = sky2_pci_read16(hw, PSM_CONFIG_REG4);
2350527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
2351527a6266SJeff Kirsher 	sky2_pci_write16(hw, PSM_CONFIG_REG4, phy | 1);
2352527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2353527a6266SJeff Kirsher 
2354527a6266SJeff Kirsher 	sky2_link_up(sky2);
2355527a6266SJeff Kirsher }
2356527a6266SJeff Kirsher 
2357527a6266SJeff Kirsher /* Transmit timeout is only called if we are running, carrier is up
2358527a6266SJeff Kirsher  * and tx queue is full (stopped).
2359527a6266SJeff Kirsher  */
sky2_tx_timeout(struct net_device * dev,unsigned int txqueue)23600290bd29SMichael S. Tsirkin static void sky2_tx_timeout(struct net_device *dev, unsigned int txqueue)
2361527a6266SJeff Kirsher {
2362527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2363527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2364527a6266SJeff Kirsher 
2365527a6266SJeff Kirsher 	netif_err(sky2, timer, dev, "tx timeout\n");
2366527a6266SJeff Kirsher 
2367527a6266SJeff Kirsher 	netdev_printk(KERN_DEBUG, dev, "transmit ring %u .. %u report=%u done=%u\n",
2368527a6266SJeff Kirsher 		      sky2->tx_cons, sky2->tx_prod,
2369527a6266SJeff Kirsher 		      sky2_read16(hw, sky2->port == 0 ? STAT_TXA1_RIDX : STAT_TXA2_RIDX),
2370527a6266SJeff Kirsher 		      sky2_read16(hw, Q_ADDR(txqaddr[sky2->port], Q_DONE)));
2371527a6266SJeff Kirsher 
2372527a6266SJeff Kirsher 	/* can't restart safely under softirq */
2373527a6266SJeff Kirsher 	schedule_work(&hw->restart_work);
2374527a6266SJeff Kirsher }
2375527a6266SJeff Kirsher 
sky2_change_mtu(struct net_device * dev,int new_mtu)2376527a6266SJeff Kirsher static int sky2_change_mtu(struct net_device *dev, int new_mtu)
2377527a6266SJeff Kirsher {
2378527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2379527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2380527a6266SJeff Kirsher 	unsigned port = sky2->port;
2381527a6266SJeff Kirsher 	int err;
2382527a6266SJeff Kirsher 	u16 ctl, mode;
2383527a6266SJeff Kirsher 	u32 imask;
2384527a6266SJeff Kirsher 
2385527a6266SJeff Kirsher 	if (!netif_running(dev)) {
2386527a6266SJeff Kirsher 		dev->mtu = new_mtu;
2387527a6266SJeff Kirsher 		netdev_update_features(dev);
2388527a6266SJeff Kirsher 		return 0;
2389527a6266SJeff Kirsher 	}
2390527a6266SJeff Kirsher 
2391527a6266SJeff Kirsher 	imask = sky2_read32(hw, B0_IMSK);
2392527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, 0);
2393ea589e9bSLino Sanfilippo 	sky2_read32(hw, B0_IMSK);
2394527a6266SJeff Kirsher 
2395860e9538SFlorian Westphal 	netif_trans_update(dev);	/* prevent tx timeout */
2396527a6266SJeff Kirsher 	napi_disable(&hw->napi);
2397527a6266SJeff Kirsher 	netif_tx_disable(dev);
2398527a6266SJeff Kirsher 
2399527a6266SJeff Kirsher 	synchronize_irq(hw->pdev->irq);
2400527a6266SJeff Kirsher 
2401527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_RAM_BUFFER))
2402527a6266SJeff Kirsher 		sky2_set_tx_stfwd(hw, port);
2403527a6266SJeff Kirsher 
2404527a6266SJeff Kirsher 	ctl = gma_read16(hw, port, GM_GP_CTRL);
2405527a6266SJeff Kirsher 	gma_write16(hw, port, GM_GP_CTRL, ctl & ~GM_GPCR_RX_ENA);
2406527a6266SJeff Kirsher 	sky2_rx_stop(sky2);
2407527a6266SJeff Kirsher 	sky2_rx_clean(sky2);
2408527a6266SJeff Kirsher 
2409527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2410527a6266SJeff Kirsher 	netdev_update_features(dev);
2411527a6266SJeff Kirsher 
2412527a6266SJeff Kirsher 	mode = DATA_BLIND_VAL(DATA_BLIND_DEF) |	GM_SMOD_VLAN_ENA;
2413527a6266SJeff Kirsher 	if (sky2->speed > SPEED_100)
2414527a6266SJeff Kirsher 		mode |= IPG_DATA_VAL(IPG_DATA_DEF_1000);
2415527a6266SJeff Kirsher 	else
2416527a6266SJeff Kirsher 		mode |= IPG_DATA_VAL(IPG_DATA_DEF_10_100);
2417527a6266SJeff Kirsher 
2418527a6266SJeff Kirsher 	if (dev->mtu > ETH_DATA_LEN)
2419527a6266SJeff Kirsher 		mode |= GM_SMOD_JUMBO_ENA;
2420527a6266SJeff Kirsher 
2421527a6266SJeff Kirsher 	gma_write16(hw, port, GM_SERIAL_MODE, mode);
2422527a6266SJeff Kirsher 
2423527a6266SJeff Kirsher 	sky2_write8(hw, RB_ADDR(rxqaddr[port], RB_CTRL), RB_ENA_OP_MD);
2424527a6266SJeff Kirsher 
2425527a6266SJeff Kirsher 	err = sky2_alloc_rx_skbs(sky2);
2426527a6266SJeff Kirsher 	if (!err)
2427527a6266SJeff Kirsher 		sky2_rx_start(sky2);
2428527a6266SJeff Kirsher 	else
2429527a6266SJeff Kirsher 		sky2_rx_clean(sky2);
2430527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, imask);
2431527a6266SJeff Kirsher 
2432527a6266SJeff Kirsher 	sky2_read32(hw, B0_Y2_SP_LISR);
2433527a6266SJeff Kirsher 	napi_enable(&hw->napi);
2434527a6266SJeff Kirsher 
2435527a6266SJeff Kirsher 	if (err)
2436527a6266SJeff Kirsher 		dev_close(dev);
2437527a6266SJeff Kirsher 	else {
2438527a6266SJeff Kirsher 		gma_write16(hw, port, GM_GP_CTRL, ctl);
2439527a6266SJeff Kirsher 
2440527a6266SJeff Kirsher 		netif_wake_queue(dev);
2441527a6266SJeff Kirsher 	}
2442527a6266SJeff Kirsher 
2443527a6266SJeff Kirsher 	return err;
2444527a6266SJeff Kirsher }
2445527a6266SJeff Kirsher 
needs_copy(const struct rx_ring_info * re,unsigned length)2446857504d0Sstephen hemminger static inline bool needs_copy(const struct rx_ring_info *re,
2447857504d0Sstephen hemminger 			      unsigned length)
2448857504d0Sstephen hemminger {
2449857504d0Sstephen hemminger #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2450857504d0Sstephen hemminger 	/* Some architectures need the IP header to be aligned */
2451857504d0Sstephen hemminger 	if (!IS_ALIGNED(re->data_addr + ETH_HLEN, sizeof(u32)))
2452857504d0Sstephen hemminger 		return true;
2453857504d0Sstephen hemminger #endif
2454857504d0Sstephen hemminger 	return length < copybreak;
2455857504d0Sstephen hemminger }
2456857504d0Sstephen hemminger 
2457527a6266SJeff Kirsher /* For small just reuse existing skb for next receive */
receive_copy(struct sky2_port * sky2,const struct rx_ring_info * re,unsigned length)2458527a6266SJeff Kirsher static struct sk_buff *receive_copy(struct sky2_port *sky2,
2459527a6266SJeff Kirsher 				    const struct rx_ring_info *re,
2460527a6266SJeff Kirsher 				    unsigned length)
2461527a6266SJeff Kirsher {
2462527a6266SJeff Kirsher 	struct sk_buff *skb;
2463527a6266SJeff Kirsher 
2464527a6266SJeff Kirsher 	skb = netdev_alloc_skb_ip_align(sky2->netdev, length);
2465527a6266SJeff Kirsher 	if (likely(skb)) {
2466c86768cfSChristophe JAILLET 		dma_sync_single_for_cpu(&sky2->hw->pdev->dev, re->data_addr,
2467c86768cfSChristophe JAILLET 					length, DMA_FROM_DEVICE);
2468527a6266SJeff Kirsher 		skb_copy_from_linear_data(re->skb, skb->data, length);
2469527a6266SJeff Kirsher 		skb->ip_summed = re->skb->ip_summed;
2470527a6266SJeff Kirsher 		skb->csum = re->skb->csum;
2471b408f94dSTom Herbert 		skb_copy_hash(skb, re->skb);
24723149a271SMichał Mirosław 		__vlan_hwaccel_copy_tag(skb, re->skb);
24733f42941bSstephen hemminger 
2474c86768cfSChristophe JAILLET 		dma_sync_single_for_device(&sky2->hw->pdev->dev,
2475c86768cfSChristophe JAILLET 					   re->data_addr, length,
2476c86768cfSChristophe JAILLET 					   DMA_FROM_DEVICE);
24773149a271SMichał Mirosław 		__vlan_hwaccel_clear_tag(re->skb);
2478b408f94dSTom Herbert 		skb_clear_hash(re->skb);
2479527a6266SJeff Kirsher 		re->skb->ip_summed = CHECKSUM_NONE;
2480527a6266SJeff Kirsher 		skb_put(skb, length);
2481527a6266SJeff Kirsher 	}
2482527a6266SJeff Kirsher 	return skb;
2483527a6266SJeff Kirsher }
2484527a6266SJeff Kirsher 
2485527a6266SJeff Kirsher /* Adjust length of skb with fragments to match received data */
skb_put_frags(struct sk_buff * skb,unsigned int hdr_space,unsigned int length)2486527a6266SJeff Kirsher static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
2487527a6266SJeff Kirsher 			  unsigned int length)
2488527a6266SJeff Kirsher {
2489527a6266SJeff Kirsher 	int i, num_frags;
2490527a6266SJeff Kirsher 	unsigned int size;
2491527a6266SJeff Kirsher 
2492527a6266SJeff Kirsher 	/* put header into skb */
2493527a6266SJeff Kirsher 	size = min(length, hdr_space);
2494527a6266SJeff Kirsher 	skb->tail += size;
2495527a6266SJeff Kirsher 	skb->len += size;
2496527a6266SJeff Kirsher 	length -= size;
2497527a6266SJeff Kirsher 
2498527a6266SJeff Kirsher 	num_frags = skb_shinfo(skb)->nr_frags;
2499527a6266SJeff Kirsher 	for (i = 0; i < num_frags; i++) {
2500527a6266SJeff Kirsher 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2501527a6266SJeff Kirsher 
2502527a6266SJeff Kirsher 		if (length == 0) {
2503527a6266SJeff Kirsher 			/* don't need this page */
2504c420c989SMatteo Croce 			__skb_frag_unref(frag, false);
2505527a6266SJeff Kirsher 			--skb_shinfo(skb)->nr_frags;
2506527a6266SJeff Kirsher 		} else {
2507527a6266SJeff Kirsher 			size = min(length, (unsigned) PAGE_SIZE);
2508527a6266SJeff Kirsher 
25099e903e08SEric Dumazet 			skb_frag_size_set(frag, size);
2510527a6266SJeff Kirsher 			skb->data_len += size;
25117ae60b3fSEric Dumazet 			skb->truesize += PAGE_SIZE;
2512527a6266SJeff Kirsher 			skb->len += size;
2513527a6266SJeff Kirsher 			length -= size;
2514527a6266SJeff Kirsher 		}
2515527a6266SJeff Kirsher 	}
2516527a6266SJeff Kirsher }
2517527a6266SJeff Kirsher 
2518527a6266SJeff Kirsher /* Normal packet - take skb from ring element and put in a new one  */
receive_new(struct sky2_port * sky2,struct rx_ring_info * re,unsigned int length)2519527a6266SJeff Kirsher static struct sk_buff *receive_new(struct sky2_port *sky2,
2520527a6266SJeff Kirsher 				   struct rx_ring_info *re,
2521527a6266SJeff Kirsher 				   unsigned int length)
2522527a6266SJeff Kirsher {
2523527a6266SJeff Kirsher 	struct sk_buff *skb;
2524527a6266SJeff Kirsher 	struct rx_ring_info nre;
2525527a6266SJeff Kirsher 	unsigned hdr_space = sky2->rx_data_size;
2526527a6266SJeff Kirsher 
2527527a6266SJeff Kirsher 	nre.skb = sky2_rx_alloc(sky2, GFP_ATOMIC);
2528527a6266SJeff Kirsher 	if (unlikely(!nre.skb))
2529527a6266SJeff Kirsher 		goto nobuf;
2530527a6266SJeff Kirsher 
2531527a6266SJeff Kirsher 	if (sky2_rx_map_skb(sky2->hw->pdev, &nre, hdr_space))
2532527a6266SJeff Kirsher 		goto nomap;
2533527a6266SJeff Kirsher 
2534527a6266SJeff Kirsher 	skb = re->skb;
2535527a6266SJeff Kirsher 	sky2_rx_unmap_skb(sky2->hw->pdev, re);
2536527a6266SJeff Kirsher 	prefetch(skb->data);
2537527a6266SJeff Kirsher 	*re = nre;
2538527a6266SJeff Kirsher 
2539527a6266SJeff Kirsher 	if (skb_shinfo(skb)->nr_frags)
2540527a6266SJeff Kirsher 		skb_put_frags(skb, hdr_space, length);
2541527a6266SJeff Kirsher 	else
2542527a6266SJeff Kirsher 		skb_put(skb, length);
2543527a6266SJeff Kirsher 	return skb;
2544527a6266SJeff Kirsher 
2545527a6266SJeff Kirsher nomap:
2546527a6266SJeff Kirsher 	dev_kfree_skb(nre.skb);
2547527a6266SJeff Kirsher nobuf:
2548527a6266SJeff Kirsher 	return NULL;
2549527a6266SJeff Kirsher }
2550527a6266SJeff Kirsher 
2551527a6266SJeff Kirsher /*
2552527a6266SJeff Kirsher  * Receive one packet.
2553527a6266SJeff Kirsher  * For larger packets, get new buffer.
2554527a6266SJeff Kirsher  */
sky2_receive(struct net_device * dev,u16 length,u32 status)2555527a6266SJeff Kirsher static struct sk_buff *sky2_receive(struct net_device *dev,
2556527a6266SJeff Kirsher 				    u16 length, u32 status)
2557527a6266SJeff Kirsher {
2558527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2559527a6266SJeff Kirsher 	struct rx_ring_info *re = sky2->rx_ring + sky2->rx_next;
2560527a6266SJeff Kirsher 	struct sk_buff *skb = NULL;
2561527a6266SJeff Kirsher 	u16 count = (status & GMR_FS_LEN) >> 16;
2562527a6266SJeff Kirsher 
2563527a6266SJeff Kirsher 	netif_printk(sky2, rx_status, KERN_DEBUG, dev,
2564527a6266SJeff Kirsher 		     "rx slot %u status 0x%x len %d\n",
2565527a6266SJeff Kirsher 		     sky2->rx_next, status, length);
2566527a6266SJeff Kirsher 
2567527a6266SJeff Kirsher 	sky2->rx_next = (sky2->rx_next + 1) % sky2->rx_pending;
2568527a6266SJeff Kirsher 	prefetch(sky2->rx_ring + sky2->rx_next);
2569527a6266SJeff Kirsher 
2570df8a39deSJiri Pirko 	if (skb_vlan_tag_present(re->skb))
2571e072b3faSstephen hemminger 		count -= VLAN_HLEN;	/* Account for vlan tag */
2572e072b3faSstephen hemminger 
2573527a6266SJeff Kirsher 	/* This chip has hardware problems that generates bogus status.
2574527a6266SJeff Kirsher 	 * So do only marginal checking and expect higher level protocols
2575527a6266SJeff Kirsher 	 * to handle crap frames.
2576527a6266SJeff Kirsher 	 */
2577527a6266SJeff Kirsher 	if (sky2->hw->chip_id == CHIP_ID_YUKON_FE_P &&
2578527a6266SJeff Kirsher 	    sky2->hw->chip_rev == CHIP_REV_YU_FE2_A0 &&
2579527a6266SJeff Kirsher 	    length != count)
2580527a6266SJeff Kirsher 		goto okay;
2581527a6266SJeff Kirsher 
2582527a6266SJeff Kirsher 	if (status & GMR_FS_ANY_ERR)
2583527a6266SJeff Kirsher 		goto error;
2584527a6266SJeff Kirsher 
2585527a6266SJeff Kirsher 	if (!(status & GMR_FS_RX_OK))
2586527a6266SJeff Kirsher 		goto resubmit;
2587527a6266SJeff Kirsher 
2588527a6266SJeff Kirsher 	/* if length reported by DMA does not match PHY, packet was truncated */
2589527a6266SJeff Kirsher 	if (length != count)
2590527a6266SJeff Kirsher 		goto error;
2591527a6266SJeff Kirsher 
2592527a6266SJeff Kirsher okay:
2593857504d0Sstephen hemminger 	if (needs_copy(re, length))
2594527a6266SJeff Kirsher 		skb = receive_copy(sky2, re, length);
2595527a6266SJeff Kirsher 	else
2596527a6266SJeff Kirsher 		skb = receive_new(sky2, re, length);
2597527a6266SJeff Kirsher 
2598527a6266SJeff Kirsher 	dev->stats.rx_dropped += (skb == NULL);
2599527a6266SJeff Kirsher 
2600527a6266SJeff Kirsher resubmit:
2601527a6266SJeff Kirsher 	sky2_rx_submit(sky2, re);
2602527a6266SJeff Kirsher 
2603527a6266SJeff Kirsher 	return skb;
2604527a6266SJeff Kirsher 
2605527a6266SJeff Kirsher error:
2606527a6266SJeff Kirsher 	++dev->stats.rx_errors;
2607527a6266SJeff Kirsher 
2608527a6266SJeff Kirsher 	if (net_ratelimit())
2609527a6266SJeff Kirsher 		netif_info(sky2, rx_err, dev,
2610527a6266SJeff Kirsher 			   "rx error, status 0x%x length %d\n", status, length);
2611527a6266SJeff Kirsher 
2612527a6266SJeff Kirsher 	goto resubmit;
2613527a6266SJeff Kirsher }
2614527a6266SJeff Kirsher 
2615527a6266SJeff Kirsher /* Transmit complete */
sky2_tx_done(struct net_device * dev,u16 last)2616527a6266SJeff Kirsher static inline void sky2_tx_done(struct net_device *dev, u16 last)
2617527a6266SJeff Kirsher {
2618527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2619527a6266SJeff Kirsher 
2620527a6266SJeff Kirsher 	if (netif_running(dev)) {
2621527a6266SJeff Kirsher 		sky2_tx_complete(sky2, last);
2622527a6266SJeff Kirsher 
2623926d0977Sstephen hemminger 		/* Wake unless it's detached, and called e.g. from sky2_close() */
2624527a6266SJeff Kirsher 		if (tx_avail(sky2) > MAX_SKB_TX_LE + 4)
2625527a6266SJeff Kirsher 			netif_wake_queue(dev);
2626527a6266SJeff Kirsher 	}
2627527a6266SJeff Kirsher }
2628527a6266SJeff Kirsher 
sky2_skb_rx(const struct sky2_port * sky2,struct sk_buff * skb)2629527a6266SJeff Kirsher static inline void sky2_skb_rx(const struct sky2_port *sky2,
2630e072b3faSstephen hemminger 			       struct sk_buff *skb)
2631527a6266SJeff Kirsher {
2632527a6266SJeff Kirsher 	if (skb->ip_summed == CHECKSUM_NONE)
2633527a6266SJeff Kirsher 		netif_receive_skb(skb);
2634527a6266SJeff Kirsher 	else
2635527a6266SJeff Kirsher 		napi_gro_receive(&sky2->hw->napi, skb);
2636527a6266SJeff Kirsher }
2637527a6266SJeff Kirsher 
sky2_rx_done(struct sky2_hw * hw,unsigned port,unsigned packets,unsigned bytes)2638527a6266SJeff Kirsher static inline void sky2_rx_done(struct sky2_hw *hw, unsigned port,
2639527a6266SJeff Kirsher 				unsigned packets, unsigned bytes)
2640527a6266SJeff Kirsher {
2641527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
2642527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2643527a6266SJeff Kirsher 
2644527a6266SJeff Kirsher 	if (packets == 0)
2645527a6266SJeff Kirsher 		return;
2646527a6266SJeff Kirsher 
2647527a6266SJeff Kirsher 	u64_stats_update_begin(&sky2->rx_stats.syncp);
2648527a6266SJeff Kirsher 	sky2->rx_stats.packets += packets;
2649527a6266SJeff Kirsher 	sky2->rx_stats.bytes += bytes;
2650527a6266SJeff Kirsher 	u64_stats_update_end(&sky2->rx_stats.syncp);
2651527a6266SJeff Kirsher 
26524a7c9726STobias Klauser 	sky2->last_rx = jiffies;
2653527a6266SJeff Kirsher 	sky2_rx_update(netdev_priv(dev), rxqaddr[port]);
2654527a6266SJeff Kirsher }
2655527a6266SJeff Kirsher 
sky2_rx_checksum(struct sky2_port * sky2,u32 status)2656527a6266SJeff Kirsher static void sky2_rx_checksum(struct sky2_port *sky2, u32 status)
2657527a6266SJeff Kirsher {
2658527a6266SJeff Kirsher 	/* If this happens then driver assuming wrong format for chip type */
2659527a6266SJeff Kirsher 	BUG_ON(sky2->hw->flags & SKY2_HW_NEW_LE);
2660527a6266SJeff Kirsher 
2661527a6266SJeff Kirsher 	/* Both checksum counters are programmed to start at
2662527a6266SJeff Kirsher 	 * the same offset, so unless there is a problem they
2663527a6266SJeff Kirsher 	 * should match. This failure is an early indication that
2664527a6266SJeff Kirsher 	 * hardware receive checksumming won't work.
2665527a6266SJeff Kirsher 	 */
2666527a6266SJeff Kirsher 	if (likely((u16)(status >> 16) == (u16)status)) {
2667527a6266SJeff Kirsher 		struct sk_buff *skb = sky2->rx_ring[sky2->rx_next].skb;
2668527a6266SJeff Kirsher 		skb->ip_summed = CHECKSUM_COMPLETE;
2669527a6266SJeff Kirsher 		skb->csum = le16_to_cpu(status);
2670527a6266SJeff Kirsher 	} else {
2671527a6266SJeff Kirsher 		dev_notice(&sky2->hw->pdev->dev,
2672527a6266SJeff Kirsher 			   "%s: receive checksum problem (status = %#x)\n",
2673527a6266SJeff Kirsher 			   sky2->netdev->name, status);
2674527a6266SJeff Kirsher 
2675527a6266SJeff Kirsher 		/* Disable checksum offload
2676527a6266SJeff Kirsher 		 * It will be reenabled on next ndo_set_features, but if it's
2677527a6266SJeff Kirsher 		 * really broken, will get disabled again
2678527a6266SJeff Kirsher 		 */
2679527a6266SJeff Kirsher 		sky2->netdev->features &= ~NETIF_F_RXCSUM;
2680527a6266SJeff Kirsher 		sky2_write32(sky2->hw, Q_ADDR(rxqaddr[sky2->port], Q_CSR),
2681527a6266SJeff Kirsher 			     BMU_DIS_RX_CHKSUM);
2682527a6266SJeff Kirsher 	}
2683527a6266SJeff Kirsher }
2684527a6266SJeff Kirsher 
sky2_rx_tag(struct sky2_port * sky2,u16 length)2685e072b3faSstephen hemminger static void sky2_rx_tag(struct sky2_port *sky2, u16 length)
2686e072b3faSstephen hemminger {
2687e072b3faSstephen hemminger 	struct sk_buff *skb;
2688e072b3faSstephen hemminger 
2689e072b3faSstephen hemminger 	skb = sky2->rx_ring[sky2->rx_next].skb;
269086a9bad3SPatrick McHardy 	__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(length));
2691e072b3faSstephen hemminger }
2692e072b3faSstephen hemminger 
sky2_rx_hash(struct sky2_port * sky2,u32 status)2693527a6266SJeff Kirsher static void sky2_rx_hash(struct sky2_port *sky2, u32 status)
2694527a6266SJeff Kirsher {
2695527a6266SJeff Kirsher 	struct sk_buff *skb;
2696527a6266SJeff Kirsher 
2697527a6266SJeff Kirsher 	skb = sky2->rx_ring[sky2->rx_next].skb;
2698b408f94dSTom Herbert 	skb_set_hash(skb, le32_to_cpu(status), PKT_HASH_TYPE_L3);
2699527a6266SJeff Kirsher }
2700527a6266SJeff Kirsher 
2701527a6266SJeff Kirsher /* Process status response ring */
sky2_status_intr(struct sky2_hw * hw,int to_do,u16 idx)2702527a6266SJeff Kirsher static int sky2_status_intr(struct sky2_hw *hw, int to_do, u16 idx)
2703527a6266SJeff Kirsher {
2704527a6266SJeff Kirsher 	int work_done = 0;
2705527a6266SJeff Kirsher 	unsigned int total_bytes[2] = { 0 };
2706527a6266SJeff Kirsher 	unsigned int total_packets[2] = { 0 };
2707527a6266SJeff Kirsher 
270821ceda26SEric W. Biederman 	if (to_do <= 0)
270921ceda26SEric W. Biederman 		return work_done;
271021ceda26SEric W. Biederman 
2711527a6266SJeff Kirsher 	rmb();
2712527a6266SJeff Kirsher 	do {
2713527a6266SJeff Kirsher 		struct sky2_port *sky2;
2714527a6266SJeff Kirsher 		struct sky2_status_le *le  = hw->st_le + hw->st_idx;
2715527a6266SJeff Kirsher 		unsigned port;
2716527a6266SJeff Kirsher 		struct net_device *dev;
2717527a6266SJeff Kirsher 		struct sk_buff *skb;
2718527a6266SJeff Kirsher 		u32 status;
2719527a6266SJeff Kirsher 		u16 length;
2720527a6266SJeff Kirsher 		u8 opcode = le->opcode;
2721527a6266SJeff Kirsher 
2722527a6266SJeff Kirsher 		if (!(opcode & HW_OWNER))
2723527a6266SJeff Kirsher 			break;
2724527a6266SJeff Kirsher 
2725527a6266SJeff Kirsher 		hw->st_idx = RING_NEXT(hw->st_idx, hw->st_size);
2726527a6266SJeff Kirsher 
2727527a6266SJeff Kirsher 		port = le->css & CSS_LINK_BIT;
2728527a6266SJeff Kirsher 		dev = hw->dev[port];
2729527a6266SJeff Kirsher 		sky2 = netdev_priv(dev);
2730527a6266SJeff Kirsher 		length = le16_to_cpu(le->length);
2731527a6266SJeff Kirsher 		status = le32_to_cpu(le->status);
2732527a6266SJeff Kirsher 
2733527a6266SJeff Kirsher 		le->opcode = 0;
2734527a6266SJeff Kirsher 		switch (opcode & ~HW_OWNER) {
2735527a6266SJeff Kirsher 		case OP_RXSTAT:
2736527a6266SJeff Kirsher 			total_packets[port]++;
2737527a6266SJeff Kirsher 			total_bytes[port] += length;
2738527a6266SJeff Kirsher 
2739527a6266SJeff Kirsher 			skb = sky2_receive(dev, length, status);
2740527a6266SJeff Kirsher 			if (!skb)
2741527a6266SJeff Kirsher 				break;
2742527a6266SJeff Kirsher 
2743527a6266SJeff Kirsher 			/* This chip reports checksum status differently */
2744527a6266SJeff Kirsher 			if (hw->flags & SKY2_HW_NEW_LE) {
2745527a6266SJeff Kirsher 				if ((dev->features & NETIF_F_RXCSUM) &&
2746527a6266SJeff Kirsher 				    (le->css & (CSS_ISIPV4 | CSS_ISIPV6)) &&
2747527a6266SJeff Kirsher 				    (le->css & CSS_TCPUDPCSOK))
2748527a6266SJeff Kirsher 					skb->ip_summed = CHECKSUM_UNNECESSARY;
2749527a6266SJeff Kirsher 				else
2750527a6266SJeff Kirsher 					skb->ip_summed = CHECKSUM_NONE;
2751527a6266SJeff Kirsher 			}
2752527a6266SJeff Kirsher 
2753527a6266SJeff Kirsher 			skb->protocol = eth_type_trans(skb, dev);
2754e072b3faSstephen hemminger 			sky2_skb_rx(sky2, skb);
2755527a6266SJeff Kirsher 
2756527a6266SJeff Kirsher 			/* Stop after net poll weight */
2757527a6266SJeff Kirsher 			if (++work_done >= to_do)
2758527a6266SJeff Kirsher 				goto exit_loop;
2759527a6266SJeff Kirsher 			break;
2760527a6266SJeff Kirsher 
2761527a6266SJeff Kirsher 		case OP_RXVLAN:
2762e072b3faSstephen hemminger 			sky2_rx_tag(sky2, length);
2763527a6266SJeff Kirsher 			break;
2764527a6266SJeff Kirsher 
2765527a6266SJeff Kirsher 		case OP_RXCHKSVLAN:
2766e072b3faSstephen hemminger 			sky2_rx_tag(sky2, length);
2767df561f66SGustavo A. R. Silva 			fallthrough;
2768527a6266SJeff Kirsher 		case OP_RXCHKS:
2769527a6266SJeff Kirsher 			if (likely(dev->features & NETIF_F_RXCSUM))
2770527a6266SJeff Kirsher 				sky2_rx_checksum(sky2, status);
2771527a6266SJeff Kirsher 			break;
2772527a6266SJeff Kirsher 
2773527a6266SJeff Kirsher 		case OP_RSS_HASH:
2774527a6266SJeff Kirsher 			sky2_rx_hash(sky2, status);
2775527a6266SJeff Kirsher 			break;
2776527a6266SJeff Kirsher 
2777527a6266SJeff Kirsher 		case OP_TXINDEXLE:
2778527a6266SJeff Kirsher 			/* TX index reports status for both ports */
2779527a6266SJeff Kirsher 			sky2_tx_done(hw->dev[0], status & 0xfff);
2780527a6266SJeff Kirsher 			if (hw->dev[1])
2781527a6266SJeff Kirsher 				sky2_tx_done(hw->dev[1],
2782527a6266SJeff Kirsher 				     ((status >> 24) & 0xff)
2783527a6266SJeff Kirsher 					     | (u16)(length & 0xf) << 8);
2784527a6266SJeff Kirsher 			break;
2785527a6266SJeff Kirsher 
2786527a6266SJeff Kirsher 		default:
2787527a6266SJeff Kirsher 			if (net_ratelimit())
2788fe3881cfSJoe Perches 				pr_warn("unknown status opcode 0x%x\n", opcode);
2789527a6266SJeff Kirsher 		}
2790527a6266SJeff Kirsher 	} while (hw->st_idx != idx);
2791527a6266SJeff Kirsher 
2792527a6266SJeff Kirsher 	/* Fully processed status ring so clear irq */
2793527a6266SJeff Kirsher 	sky2_write32(hw, STAT_CTRL, SC_STAT_CLR_IRQ);
2794527a6266SJeff Kirsher 
2795527a6266SJeff Kirsher exit_loop:
2796527a6266SJeff Kirsher 	sky2_rx_done(hw, 0, total_packets[0], total_bytes[0]);
2797527a6266SJeff Kirsher 	sky2_rx_done(hw, 1, total_packets[1], total_bytes[1]);
2798527a6266SJeff Kirsher 
2799527a6266SJeff Kirsher 	return work_done;
2800527a6266SJeff Kirsher }
2801527a6266SJeff Kirsher 
sky2_hw_error(struct sky2_hw * hw,unsigned port,u32 status)2802527a6266SJeff Kirsher static void sky2_hw_error(struct sky2_hw *hw, unsigned port, u32 status)
2803527a6266SJeff Kirsher {
2804527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
2805527a6266SJeff Kirsher 
2806527a6266SJeff Kirsher 	if (net_ratelimit())
2807527a6266SJeff Kirsher 		netdev_info(dev, "hw error interrupt status 0x%x\n", status);
2808527a6266SJeff Kirsher 
2809527a6266SJeff Kirsher 	if (status & Y2_IS_PAR_RD1) {
2810527a6266SJeff Kirsher 		if (net_ratelimit())
2811527a6266SJeff Kirsher 			netdev_err(dev, "ram data read parity error\n");
2812527a6266SJeff Kirsher 		/* Clear IRQ */
2813527a6266SJeff Kirsher 		sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_RD_PERR);
2814527a6266SJeff Kirsher 	}
2815527a6266SJeff Kirsher 
2816527a6266SJeff Kirsher 	if (status & Y2_IS_PAR_WR1) {
2817527a6266SJeff Kirsher 		if (net_ratelimit())
2818527a6266SJeff Kirsher 			netdev_err(dev, "ram data write parity error\n");
2819527a6266SJeff Kirsher 
2820527a6266SJeff Kirsher 		sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_WR_PERR);
2821527a6266SJeff Kirsher 	}
2822527a6266SJeff Kirsher 
2823527a6266SJeff Kirsher 	if (status & Y2_IS_PAR_MAC1) {
2824527a6266SJeff Kirsher 		if (net_ratelimit())
2825527a6266SJeff Kirsher 			netdev_err(dev, "MAC parity error\n");
2826527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_PE);
2827527a6266SJeff Kirsher 	}
2828527a6266SJeff Kirsher 
2829527a6266SJeff Kirsher 	if (status & Y2_IS_PAR_RX1) {
2830527a6266SJeff Kirsher 		if (net_ratelimit())
2831527a6266SJeff Kirsher 			netdev_err(dev, "RX parity error\n");
2832527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(rxqaddr[port], Q_CSR), BMU_CLR_IRQ_PAR);
2833527a6266SJeff Kirsher 	}
2834527a6266SJeff Kirsher 
2835527a6266SJeff Kirsher 	if (status & Y2_IS_TCP_TXA1) {
2836527a6266SJeff Kirsher 		if (net_ratelimit())
2837527a6266SJeff Kirsher 			netdev_err(dev, "TCP segmentation error\n");
2838527a6266SJeff Kirsher 		sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_CLR_IRQ_TCP);
2839527a6266SJeff Kirsher 	}
2840527a6266SJeff Kirsher }
2841527a6266SJeff Kirsher 
sky2_hw_intr(struct sky2_hw * hw)2842527a6266SJeff Kirsher static void sky2_hw_intr(struct sky2_hw *hw)
2843527a6266SJeff Kirsher {
2844527a6266SJeff Kirsher 	struct pci_dev *pdev = hw->pdev;
2845527a6266SJeff Kirsher 	u32 status = sky2_read32(hw, B0_HWE_ISRC);
2846527a6266SJeff Kirsher 	u32 hwmsk = sky2_read32(hw, B0_HWE_IMSK);
2847527a6266SJeff Kirsher 
2848527a6266SJeff Kirsher 	status &= hwmsk;
2849527a6266SJeff Kirsher 
2850527a6266SJeff Kirsher 	if (status & Y2_IS_TIST_OV)
2851527a6266SJeff Kirsher 		sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
2852527a6266SJeff Kirsher 
2853527a6266SJeff Kirsher 	if (status & (Y2_IS_MST_ERR | Y2_IS_IRQ_STAT)) {
2854527a6266SJeff Kirsher 		u16 pci_err;
2855527a6266SJeff Kirsher 
2856527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
2857527a6266SJeff Kirsher 		pci_err = sky2_pci_read16(hw, PCI_STATUS);
2858527a6266SJeff Kirsher 		if (net_ratelimit())
2859527a6266SJeff Kirsher 			dev_err(&pdev->dev, "PCI hardware error (0x%x)\n",
2860527a6266SJeff Kirsher 			        pci_err);
2861527a6266SJeff Kirsher 
2862527a6266SJeff Kirsher 		sky2_pci_write16(hw, PCI_STATUS,
2863527a6266SJeff Kirsher 				      pci_err | PCI_STATUS_ERROR_BITS);
2864527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2865527a6266SJeff Kirsher 	}
2866527a6266SJeff Kirsher 
2867527a6266SJeff Kirsher 	if (status & Y2_IS_PCI_EXP) {
2868527a6266SJeff Kirsher 		/* PCI-Express uncorrectable Error occurred */
2869527a6266SJeff Kirsher 		u32 err;
2870527a6266SJeff Kirsher 
2871527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
2872527a6266SJeff Kirsher 		err = sky2_read32(hw, Y2_CFG_AER + PCI_ERR_UNCOR_STATUS);
2873527a6266SJeff Kirsher 		sky2_write32(hw, Y2_CFG_AER + PCI_ERR_UNCOR_STATUS,
2874527a6266SJeff Kirsher 			     0xfffffffful);
2875527a6266SJeff Kirsher 		if (net_ratelimit())
2876527a6266SJeff Kirsher 			dev_err(&pdev->dev, "PCI Express error (0x%x)\n", err);
2877527a6266SJeff Kirsher 
2878527a6266SJeff Kirsher 		sky2_read32(hw, Y2_CFG_AER + PCI_ERR_UNCOR_STATUS);
2879527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2880527a6266SJeff Kirsher 	}
2881527a6266SJeff Kirsher 
2882527a6266SJeff Kirsher 	if (status & Y2_HWE_L1_MASK)
2883527a6266SJeff Kirsher 		sky2_hw_error(hw, 0, status);
2884527a6266SJeff Kirsher 	status >>= 8;
2885527a6266SJeff Kirsher 	if (status & Y2_HWE_L1_MASK)
2886527a6266SJeff Kirsher 		sky2_hw_error(hw, 1, status);
2887527a6266SJeff Kirsher }
2888527a6266SJeff Kirsher 
sky2_mac_intr(struct sky2_hw * hw,unsigned port)2889527a6266SJeff Kirsher static void sky2_mac_intr(struct sky2_hw *hw, unsigned port)
2890527a6266SJeff Kirsher {
2891527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
2892527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2893527a6266SJeff Kirsher 	u8 status = sky2_read8(hw, SK_REG(port, GMAC_IRQ_SRC));
2894527a6266SJeff Kirsher 
2895527a6266SJeff Kirsher 	netif_info(sky2, intr, dev, "mac interrupt status 0x%x\n", status);
2896527a6266SJeff Kirsher 
2897527a6266SJeff Kirsher 	if (status & GM_IS_RX_CO_OV)
2898527a6266SJeff Kirsher 		gma_read16(hw, port, GM_RX_IRQ_SRC);
2899527a6266SJeff Kirsher 
2900527a6266SJeff Kirsher 	if (status & GM_IS_TX_CO_OV)
2901527a6266SJeff Kirsher 		gma_read16(hw, port, GM_TX_IRQ_SRC);
2902527a6266SJeff Kirsher 
2903527a6266SJeff Kirsher 	if (status & GM_IS_RX_FF_OR) {
2904527a6266SJeff Kirsher 		++dev->stats.rx_fifo_errors;
2905527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_CLI_RX_FO);
2906527a6266SJeff Kirsher 	}
2907527a6266SJeff Kirsher 
2908527a6266SJeff Kirsher 	if (status & GM_IS_TX_FF_UR) {
2909527a6266SJeff Kirsher 		++dev->stats.tx_fifo_errors;
2910527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_FU);
2911527a6266SJeff Kirsher 	}
2912527a6266SJeff Kirsher }
2913527a6266SJeff Kirsher 
2914527a6266SJeff Kirsher /* This should never happen it is a bug. */
sky2_le_error(struct sky2_hw * hw,unsigned port,u16 q)2915527a6266SJeff Kirsher static void sky2_le_error(struct sky2_hw *hw, unsigned port, u16 q)
2916527a6266SJeff Kirsher {
2917527a6266SJeff Kirsher 	struct net_device *dev = hw->dev[port];
2918527a6266SJeff Kirsher 	u16 idx = sky2_read16(hw, Y2_QADDR(q, PREF_UNIT_GET_IDX));
2919527a6266SJeff Kirsher 
2920527a6266SJeff Kirsher 	dev_err(&hw->pdev->dev, "%s: descriptor error q=%#x get=%u put=%u\n",
2921527a6266SJeff Kirsher 		dev->name, (unsigned) q, (unsigned) idx,
2922527a6266SJeff Kirsher 		(unsigned) sky2_read16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX)));
2923527a6266SJeff Kirsher 
2924527a6266SJeff Kirsher 	sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_CLR_IRQ_CHK);
2925527a6266SJeff Kirsher }
2926527a6266SJeff Kirsher 
sky2_rx_hung(struct net_device * dev)2927527a6266SJeff Kirsher static int sky2_rx_hung(struct net_device *dev)
2928527a6266SJeff Kirsher {
2929527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
2930527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
2931527a6266SJeff Kirsher 	unsigned port = sky2->port;
2932527a6266SJeff Kirsher 	unsigned rxq = rxqaddr[port];
2933527a6266SJeff Kirsher 	u32 mac_rp = sky2_read32(hw, SK_REG(port, RX_GMF_RP));
2934527a6266SJeff Kirsher 	u8 mac_lev = sky2_read8(hw, SK_REG(port, RX_GMF_RLEV));
2935527a6266SJeff Kirsher 	u8 fifo_rp = sky2_read8(hw, Q_ADDR(rxq, Q_RP));
2936527a6266SJeff Kirsher 	u8 fifo_lev = sky2_read8(hw, Q_ADDR(rxq, Q_RL));
2937527a6266SJeff Kirsher 
2938527a6266SJeff Kirsher 	/* If idle and MAC or PCI is stuck */
29394a7c9726STobias Klauser 	if (sky2->check.last == sky2->last_rx &&
2940527a6266SJeff Kirsher 	    ((mac_rp == sky2->check.mac_rp &&
2941527a6266SJeff Kirsher 	      mac_lev != 0 && mac_lev >= sky2->check.mac_lev) ||
2942527a6266SJeff Kirsher 	     /* Check if the PCI RX hang */
2943527a6266SJeff Kirsher 	     (fifo_rp == sky2->check.fifo_rp &&
2944527a6266SJeff Kirsher 	      fifo_lev != 0 && fifo_lev >= sky2->check.fifo_lev))) {
2945527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
2946527a6266SJeff Kirsher 			      "hung mac %d:%d fifo %d (%d:%d)\n",
2947527a6266SJeff Kirsher 			      mac_lev, mac_rp, fifo_lev,
2948527a6266SJeff Kirsher 			      fifo_rp, sky2_read8(hw, Q_ADDR(rxq, Q_WP)));
2949527a6266SJeff Kirsher 		return 1;
2950527a6266SJeff Kirsher 	} else {
29514a7c9726STobias Klauser 		sky2->check.last = sky2->last_rx;
2952527a6266SJeff Kirsher 		sky2->check.mac_rp = mac_rp;
2953527a6266SJeff Kirsher 		sky2->check.mac_lev = mac_lev;
2954527a6266SJeff Kirsher 		sky2->check.fifo_rp = fifo_rp;
2955527a6266SJeff Kirsher 		sky2->check.fifo_lev = fifo_lev;
2956527a6266SJeff Kirsher 		return 0;
2957527a6266SJeff Kirsher 	}
2958527a6266SJeff Kirsher }
2959527a6266SJeff Kirsher 
sky2_watchdog(struct timer_list * t)2960e99e88a9SKees Cook static void sky2_watchdog(struct timer_list *t)
2961527a6266SJeff Kirsher {
2962e99e88a9SKees Cook 	struct sky2_hw *hw = from_timer(hw, t, watchdog_timer);
2963527a6266SJeff Kirsher 
2964527a6266SJeff Kirsher 	/* Check for lost IRQ once a second */
2965527a6266SJeff Kirsher 	if (sky2_read32(hw, B0_ISRC)) {
2966527a6266SJeff Kirsher 		napi_schedule(&hw->napi);
2967527a6266SJeff Kirsher 	} else {
2968527a6266SJeff Kirsher 		int i, active = 0;
2969527a6266SJeff Kirsher 
2970527a6266SJeff Kirsher 		for (i = 0; i < hw->ports; i++) {
2971527a6266SJeff Kirsher 			struct net_device *dev = hw->dev[i];
2972527a6266SJeff Kirsher 			if (!netif_running(dev))
2973527a6266SJeff Kirsher 				continue;
2974527a6266SJeff Kirsher 			++active;
2975527a6266SJeff Kirsher 
2976527a6266SJeff Kirsher 			/* For chips with Rx FIFO, check if stuck */
2977527a6266SJeff Kirsher 			if ((hw->flags & SKY2_HW_RAM_BUFFER) &&
2978527a6266SJeff Kirsher 			     sky2_rx_hung(dev)) {
2979527a6266SJeff Kirsher 				netdev_info(dev, "receiver hang detected\n");
2980527a6266SJeff Kirsher 				schedule_work(&hw->restart_work);
2981527a6266SJeff Kirsher 				return;
2982527a6266SJeff Kirsher 			}
2983527a6266SJeff Kirsher 		}
2984527a6266SJeff Kirsher 
2985527a6266SJeff Kirsher 		if (active == 0)
2986527a6266SJeff Kirsher 			return;
2987527a6266SJeff Kirsher 	}
2988527a6266SJeff Kirsher 
2989527a6266SJeff Kirsher 	mod_timer(&hw->watchdog_timer, round_jiffies(jiffies + HZ));
2990527a6266SJeff Kirsher }
2991527a6266SJeff Kirsher 
2992527a6266SJeff Kirsher /* Hardware/software error handling */
sky2_err_intr(struct sky2_hw * hw,u32 status)2993527a6266SJeff Kirsher static void sky2_err_intr(struct sky2_hw *hw, u32 status)
2994527a6266SJeff Kirsher {
2995527a6266SJeff Kirsher 	if (net_ratelimit())
2996527a6266SJeff Kirsher 		dev_warn(&hw->pdev->dev, "error interrupt status=%#x\n", status);
2997527a6266SJeff Kirsher 
2998527a6266SJeff Kirsher 	if (status & Y2_IS_HW_ERR)
2999527a6266SJeff Kirsher 		sky2_hw_intr(hw);
3000527a6266SJeff Kirsher 
3001527a6266SJeff Kirsher 	if (status & Y2_IS_IRQ_MAC1)
3002527a6266SJeff Kirsher 		sky2_mac_intr(hw, 0);
3003527a6266SJeff Kirsher 
3004527a6266SJeff Kirsher 	if (status & Y2_IS_IRQ_MAC2)
3005527a6266SJeff Kirsher 		sky2_mac_intr(hw, 1);
3006527a6266SJeff Kirsher 
3007527a6266SJeff Kirsher 	if (status & Y2_IS_CHK_RX1)
3008527a6266SJeff Kirsher 		sky2_le_error(hw, 0, Q_R1);
3009527a6266SJeff Kirsher 
3010527a6266SJeff Kirsher 	if (status & Y2_IS_CHK_RX2)
3011527a6266SJeff Kirsher 		sky2_le_error(hw, 1, Q_R2);
3012527a6266SJeff Kirsher 
3013527a6266SJeff Kirsher 	if (status & Y2_IS_CHK_TXA1)
3014527a6266SJeff Kirsher 		sky2_le_error(hw, 0, Q_XA1);
3015527a6266SJeff Kirsher 
3016527a6266SJeff Kirsher 	if (status & Y2_IS_CHK_TXA2)
3017527a6266SJeff Kirsher 		sky2_le_error(hw, 1, Q_XA2);
3018527a6266SJeff Kirsher }
3019527a6266SJeff Kirsher 
sky2_poll(struct napi_struct * napi,int work_limit)3020527a6266SJeff Kirsher static int sky2_poll(struct napi_struct *napi, int work_limit)
3021527a6266SJeff Kirsher {
3022527a6266SJeff Kirsher 	struct sky2_hw *hw = container_of(napi, struct sky2_hw, napi);
3023527a6266SJeff Kirsher 	u32 status = sky2_read32(hw, B0_Y2_SP_EISR);
3024527a6266SJeff Kirsher 	int work_done = 0;
3025527a6266SJeff Kirsher 	u16 idx;
3026527a6266SJeff Kirsher 
3027527a6266SJeff Kirsher 	if (unlikely(status & Y2_IS_ERROR))
3028527a6266SJeff Kirsher 		sky2_err_intr(hw, status);
3029527a6266SJeff Kirsher 
3030527a6266SJeff Kirsher 	if (status & Y2_IS_IRQ_PHY1)
3031527a6266SJeff Kirsher 		sky2_phy_intr(hw, 0);
3032527a6266SJeff Kirsher 
3033527a6266SJeff Kirsher 	if (status & Y2_IS_IRQ_PHY2)
3034527a6266SJeff Kirsher 		sky2_phy_intr(hw, 1);
3035527a6266SJeff Kirsher 
3036527a6266SJeff Kirsher 	if (status & Y2_IS_PHY_QLNK)
3037527a6266SJeff Kirsher 		sky2_qlink_intr(hw);
3038527a6266SJeff Kirsher 
3039527a6266SJeff Kirsher 	while ((idx = sky2_read16(hw, STAT_PUT_IDX)) != hw->st_idx) {
3040527a6266SJeff Kirsher 		work_done += sky2_status_intr(hw, work_limit - work_done, idx);
3041527a6266SJeff Kirsher 
3042527a6266SJeff Kirsher 		if (work_done >= work_limit)
3043527a6266SJeff Kirsher 			goto done;
3044527a6266SJeff Kirsher 	}
3045527a6266SJeff Kirsher 
3046f4b63ea0Sstephen hemminger 	napi_complete_done(napi, work_done);
3047527a6266SJeff Kirsher 	sky2_read32(hw, B0_Y2_SP_LISR);
3048527a6266SJeff Kirsher done:
3049527a6266SJeff Kirsher 
3050527a6266SJeff Kirsher 	return work_done;
3051527a6266SJeff Kirsher }
3052527a6266SJeff Kirsher 
sky2_intr(int irq,void * dev_id)3053527a6266SJeff Kirsher static irqreturn_t sky2_intr(int irq, void *dev_id)
3054527a6266SJeff Kirsher {
3055527a6266SJeff Kirsher 	struct sky2_hw *hw = dev_id;
3056527a6266SJeff Kirsher 	u32 status;
3057527a6266SJeff Kirsher 
3058527a6266SJeff Kirsher 	/* Reading this mask interrupts as side effect */
3059527a6266SJeff Kirsher 	status = sky2_read32(hw, B0_Y2_SP_ISRC2);
3060d663d181SMirko Lindner 	if (status == 0 || status == ~0) {
3061d663d181SMirko Lindner 		sky2_write32(hw, B0_Y2_SP_ICR, 2);
3062527a6266SJeff Kirsher 		return IRQ_NONE;
3063d663d181SMirko Lindner 	}
3064527a6266SJeff Kirsher 
3065527a6266SJeff Kirsher 	prefetch(&hw->st_le[hw->st_idx]);
3066527a6266SJeff Kirsher 
3067527a6266SJeff Kirsher 	napi_schedule(&hw->napi);
3068527a6266SJeff Kirsher 
3069527a6266SJeff Kirsher 	return IRQ_HANDLED;
3070527a6266SJeff Kirsher }
3071527a6266SJeff Kirsher 
3072527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
sky2_netpoll(struct net_device * dev)3073527a6266SJeff Kirsher static void sky2_netpoll(struct net_device *dev)
3074527a6266SJeff Kirsher {
3075527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3076527a6266SJeff Kirsher 
3077527a6266SJeff Kirsher 	napi_schedule(&sky2->hw->napi);
3078527a6266SJeff Kirsher }
3079527a6266SJeff Kirsher #endif
3080527a6266SJeff Kirsher 
3081527a6266SJeff Kirsher /* Chip internal frequency for clock calculations */
sky2_mhz(const struct sky2_hw * hw)3082527a6266SJeff Kirsher static u32 sky2_mhz(const struct sky2_hw *hw)
3083527a6266SJeff Kirsher {
3084527a6266SJeff Kirsher 	switch (hw->chip_id) {
3085527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EC:
3086527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EC_U:
3087527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EX:
3088527a6266SJeff Kirsher 	case CHIP_ID_YUKON_SUPR:
3089527a6266SJeff Kirsher 	case CHIP_ID_YUKON_UL_2:
3090527a6266SJeff Kirsher 	case CHIP_ID_YUKON_OPT:
3091527a6266SJeff Kirsher 	case CHIP_ID_YUKON_PRM:
3092527a6266SJeff Kirsher 	case CHIP_ID_YUKON_OP_2:
3093527a6266SJeff Kirsher 		return 125;
3094527a6266SJeff Kirsher 
3095527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE:
3096527a6266SJeff Kirsher 		return 100;
3097527a6266SJeff Kirsher 
3098527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE_P:
3099527a6266SJeff Kirsher 		return 50;
3100527a6266SJeff Kirsher 
3101527a6266SJeff Kirsher 	case CHIP_ID_YUKON_XL:
3102527a6266SJeff Kirsher 		return 156;
3103527a6266SJeff Kirsher 
3104527a6266SJeff Kirsher 	default:
3105527a6266SJeff Kirsher 		BUG();
3106527a6266SJeff Kirsher 	}
3107527a6266SJeff Kirsher }
3108527a6266SJeff Kirsher 
sky2_us2clk(const struct sky2_hw * hw,u32 us)3109527a6266SJeff Kirsher static inline u32 sky2_us2clk(const struct sky2_hw *hw, u32 us)
3110527a6266SJeff Kirsher {
3111527a6266SJeff Kirsher 	return sky2_mhz(hw) * us;
3112527a6266SJeff Kirsher }
3113527a6266SJeff Kirsher 
sky2_clk2us(const struct sky2_hw * hw,u32 clk)3114527a6266SJeff Kirsher static inline u32 sky2_clk2us(const struct sky2_hw *hw, u32 clk)
3115527a6266SJeff Kirsher {
3116527a6266SJeff Kirsher 	return clk / sky2_mhz(hw);
3117527a6266SJeff Kirsher }
3118527a6266SJeff Kirsher 
3119527a6266SJeff Kirsher 
sky2_init(struct sky2_hw * hw)3120853e3f4cSBill Pemberton static int sky2_init(struct sky2_hw *hw)
3121527a6266SJeff Kirsher {
3122527a6266SJeff Kirsher 	u8 t8;
3123527a6266SJeff Kirsher 
3124527a6266SJeff Kirsher 	/* Enable all clocks and check for bad PCI access */
3125527a6266SJeff Kirsher 	sky2_pci_write32(hw, PCI_DEV_REG3, 0);
3126527a6266SJeff Kirsher 
3127527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_RST_CLR);
3128527a6266SJeff Kirsher 
3129527a6266SJeff Kirsher 	hw->chip_id = sky2_read8(hw, B2_CHIP_ID);
3130527a6266SJeff Kirsher 	hw->chip_rev = (sky2_read8(hw, B2_MAC_CFG) & CFG_CHIP_R_MSK) >> 4;
3131527a6266SJeff Kirsher 
3132527a6266SJeff Kirsher 	switch (hw->chip_id) {
3133527a6266SJeff Kirsher 	case CHIP_ID_YUKON_XL:
3134527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT | SKY2_HW_NEWER_PHY;
3135527a6266SJeff Kirsher 		if (hw->chip_rev < CHIP_REV_YU_XL_A2)
3136527a6266SJeff Kirsher 			hw->flags |= SKY2_HW_RSS_BROKEN;
3137527a6266SJeff Kirsher 		break;
3138527a6266SJeff Kirsher 
3139527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EC_U:
3140527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT
3141527a6266SJeff Kirsher 			| SKY2_HW_NEWER_PHY
3142527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL;
3143527a6266SJeff Kirsher 		break;
3144527a6266SJeff Kirsher 
3145527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EX:
3146527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT
3147527a6266SJeff Kirsher 			| SKY2_HW_NEWER_PHY
3148527a6266SJeff Kirsher 			| SKY2_HW_NEW_LE
3149527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL
3150527a6266SJeff Kirsher 			| SKY2_HW_RSS_CHKSUM;
3151527a6266SJeff Kirsher 
3152527a6266SJeff Kirsher 		/* New transmit checksum */
3153527a6266SJeff Kirsher 		if (hw->chip_rev != CHIP_REV_YU_EX_B0)
3154527a6266SJeff Kirsher 			hw->flags |= SKY2_HW_AUTO_TX_SUM;
3155527a6266SJeff Kirsher 		break;
3156527a6266SJeff Kirsher 
3157527a6266SJeff Kirsher 	case CHIP_ID_YUKON_EC:
3158527a6266SJeff Kirsher 		/* This rev is really old, and requires untested workarounds */
3159527a6266SJeff Kirsher 		if (hw->chip_rev == CHIP_REV_YU_EC_A1) {
3160527a6266SJeff Kirsher 			dev_err(&hw->pdev->dev, "unsupported revision Yukon-EC rev A1\n");
3161527a6266SJeff Kirsher 			return -EOPNOTSUPP;
3162527a6266SJeff Kirsher 		}
3163527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT | SKY2_HW_RSS_BROKEN;
3164527a6266SJeff Kirsher 		break;
3165527a6266SJeff Kirsher 
3166527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE:
3167527a6266SJeff Kirsher 		hw->flags = SKY2_HW_RSS_BROKEN;
3168527a6266SJeff Kirsher 		break;
3169527a6266SJeff Kirsher 
3170527a6266SJeff Kirsher 	case CHIP_ID_YUKON_FE_P:
3171527a6266SJeff Kirsher 		hw->flags = SKY2_HW_NEWER_PHY
3172527a6266SJeff Kirsher 			| SKY2_HW_NEW_LE
3173527a6266SJeff Kirsher 			| SKY2_HW_AUTO_TX_SUM
3174527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL;
3175527a6266SJeff Kirsher 
3176527a6266SJeff Kirsher 		/* The workaround for status conflicts VLAN tag detection. */
3177527a6266SJeff Kirsher 		if (hw->chip_rev == CHIP_REV_YU_FE2_A0)
3178527a6266SJeff Kirsher 			hw->flags |= SKY2_HW_VLAN_BROKEN | SKY2_HW_RSS_CHKSUM;
3179527a6266SJeff Kirsher 		break;
3180527a6266SJeff Kirsher 
3181527a6266SJeff Kirsher 	case CHIP_ID_YUKON_SUPR:
3182527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT
3183527a6266SJeff Kirsher 			| SKY2_HW_NEWER_PHY
3184527a6266SJeff Kirsher 			| SKY2_HW_NEW_LE
3185527a6266SJeff Kirsher 			| SKY2_HW_AUTO_TX_SUM
3186527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL;
3187527a6266SJeff Kirsher 
3188527a6266SJeff Kirsher 		if (hw->chip_rev == CHIP_REV_YU_SU_A0)
3189527a6266SJeff Kirsher 			hw->flags |= SKY2_HW_RSS_CHKSUM;
3190527a6266SJeff Kirsher 		break;
3191527a6266SJeff Kirsher 
3192527a6266SJeff Kirsher 	case CHIP_ID_YUKON_UL_2:
3193527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT
3194527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL;
3195527a6266SJeff Kirsher 		break;
3196527a6266SJeff Kirsher 
3197527a6266SJeff Kirsher 	case CHIP_ID_YUKON_OPT:
3198527a6266SJeff Kirsher 	case CHIP_ID_YUKON_PRM:
3199527a6266SJeff Kirsher 	case CHIP_ID_YUKON_OP_2:
3200527a6266SJeff Kirsher 		hw->flags = SKY2_HW_GIGABIT
3201527a6266SJeff Kirsher 			| SKY2_HW_NEW_LE
3202527a6266SJeff Kirsher 			| SKY2_HW_ADV_POWER_CTL;
3203527a6266SJeff Kirsher 		break;
3204527a6266SJeff Kirsher 
3205527a6266SJeff Kirsher 	default:
3206527a6266SJeff Kirsher 		dev_err(&hw->pdev->dev, "unsupported chip type 0x%x\n",
3207527a6266SJeff Kirsher 			hw->chip_id);
3208527a6266SJeff Kirsher 		return -EOPNOTSUPP;
3209527a6266SJeff Kirsher 	}
3210527a6266SJeff Kirsher 
3211527a6266SJeff Kirsher 	hw->pmd_type = sky2_read8(hw, B2_PMD_TYP);
3212527a6266SJeff Kirsher 	if (hw->pmd_type == 'L' || hw->pmd_type == 'S' || hw->pmd_type == 'P')
3213527a6266SJeff Kirsher 		hw->flags |= SKY2_HW_FIBRE_PHY;
3214527a6266SJeff Kirsher 
3215527a6266SJeff Kirsher 	hw->ports = 1;
3216527a6266SJeff Kirsher 	t8 = sky2_read8(hw, B2_Y2_HW_RES);
3217527a6266SJeff Kirsher 	if ((t8 & CFG_DUAL_MAC_MSK) == CFG_DUAL_MAC_MSK) {
3218527a6266SJeff Kirsher 		if (!(sky2_read8(hw, B2_Y2_CLK_GATE) & Y2_STATUS_LNK2_INAC))
3219527a6266SJeff Kirsher 			++hw->ports;
3220527a6266SJeff Kirsher 	}
3221527a6266SJeff Kirsher 
3222527a6266SJeff Kirsher 	if (sky2_read8(hw, B2_E_0))
3223527a6266SJeff Kirsher 		hw->flags |= SKY2_HW_RAM_BUFFER;
3224527a6266SJeff Kirsher 
3225527a6266SJeff Kirsher 	return 0;
3226527a6266SJeff Kirsher }
3227527a6266SJeff Kirsher 
sky2_reset(struct sky2_hw * hw)3228527a6266SJeff Kirsher static void sky2_reset(struct sky2_hw *hw)
3229527a6266SJeff Kirsher {
3230527a6266SJeff Kirsher 	struct pci_dev *pdev = hw->pdev;
3231527a6266SJeff Kirsher 	u16 status;
3232527a6266SJeff Kirsher 	int i;
3233527a6266SJeff Kirsher 	u32 hwe_mask = Y2_HWE_ALL_MASK;
3234527a6266SJeff Kirsher 
3235527a6266SJeff Kirsher 	/* disable ASF */
3236527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EX
3237527a6266SJeff Kirsher 	    || hw->chip_id == CHIP_ID_YUKON_SUPR) {
3238527a6266SJeff Kirsher 		sky2_write32(hw, CPU_WDOG, 0);
3239527a6266SJeff Kirsher 		status = sky2_read16(hw, HCU_CCSR);
3240527a6266SJeff Kirsher 		status &= ~(HCU_CCSR_AHB_RST | HCU_CCSR_CPU_RST_MODE |
3241527a6266SJeff Kirsher 			    HCU_CCSR_UC_STATE_MSK);
3242527a6266SJeff Kirsher 		/*
3243527a6266SJeff Kirsher 		 * CPU clock divider shouldn't be used because
3244527a6266SJeff Kirsher 		 * - ASF firmware may malfunction
3245527a6266SJeff Kirsher 		 * - Yukon-Supreme: Parallel FLASH doesn't support divided clocks
3246527a6266SJeff Kirsher 		 */
3247527a6266SJeff Kirsher 		status &= ~HCU_CCSR_CPU_CLK_DIVIDE_MSK;
3248527a6266SJeff Kirsher 		sky2_write16(hw, HCU_CCSR, status);
3249527a6266SJeff Kirsher 		sky2_write32(hw, CPU_WDOG, 0);
3250527a6266SJeff Kirsher 	} else
3251527a6266SJeff Kirsher 		sky2_write8(hw, B28_Y2_ASF_STAT_CMD, Y2_ASF_RESET);
3252527a6266SJeff Kirsher 	sky2_write16(hw, B0_CTST, Y2_ASF_DISABLE);
3253527a6266SJeff Kirsher 
3254527a6266SJeff Kirsher 	/* do a SW reset */
3255527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_RST_SET);
3256527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_RST_CLR);
3257527a6266SJeff Kirsher 
3258527a6266SJeff Kirsher 	/* allow writes to PCI config */
3259527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
3260527a6266SJeff Kirsher 
3261527a6266SJeff Kirsher 	/* clear PCI errors, if any */
3262527a6266SJeff Kirsher 	status = sky2_pci_read16(hw, PCI_STATUS);
3263527a6266SJeff Kirsher 	status |= PCI_STATUS_ERROR_BITS;
3264527a6266SJeff Kirsher 	sky2_pci_write16(hw, PCI_STATUS, status);
3265527a6266SJeff Kirsher 
3266527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_MRST_CLR);
3267527a6266SJeff Kirsher 
3268527a6266SJeff Kirsher 	if (pci_is_pcie(pdev)) {
3269527a6266SJeff Kirsher 		sky2_write32(hw, Y2_CFG_AER + PCI_ERR_UNCOR_STATUS,
3270527a6266SJeff Kirsher 			     0xfffffffful);
3271527a6266SJeff Kirsher 
3272527a6266SJeff Kirsher 		/* If error bit is stuck on ignore it */
3273527a6266SJeff Kirsher 		if (sky2_read32(hw, B0_HWE_ISRC) & Y2_IS_PCI_EXP)
3274527a6266SJeff Kirsher 			dev_info(&pdev->dev, "ignoring stuck error report bit\n");
3275527a6266SJeff Kirsher 		else
3276527a6266SJeff Kirsher 			hwe_mask |= Y2_IS_PCI_EXP;
3277527a6266SJeff Kirsher 	}
3278527a6266SJeff Kirsher 
3279527a6266SJeff Kirsher 	sky2_power_on(hw);
3280527a6266SJeff Kirsher 	sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
3281527a6266SJeff Kirsher 
3282527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
3283527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_SET);
3284527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_CLR);
3285527a6266SJeff Kirsher 
3286527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_EX ||
3287527a6266SJeff Kirsher 		    hw->chip_id == CHIP_ID_YUKON_SUPR)
3288527a6266SJeff Kirsher 			sky2_write16(hw, SK_REG(i, GMAC_CTRL),
3289527a6266SJeff Kirsher 				     GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON
3290527a6266SJeff Kirsher 				     | GMC_BYP_RETR_ON);
3291527a6266SJeff Kirsher 
3292527a6266SJeff Kirsher 	}
3293527a6266SJeff Kirsher 
3294527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_SUPR && hw->chip_rev > CHIP_REV_YU_SU_B0) {
3295527a6266SJeff Kirsher 		/* enable MACSec clock gating */
3296527a6266SJeff Kirsher 		sky2_pci_write32(hw, PCI_DEV_REG3, P_CLK_MACSEC_DIS);
3297527a6266SJeff Kirsher 	}
3298527a6266SJeff Kirsher 
3299527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_OPT ||
3300527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_PRM ||
3301527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_OP_2) {
3302527a6266SJeff Kirsher 		u16 reg;
3303527a6266SJeff Kirsher 
3304527a6266SJeff Kirsher 		if (hw->chip_id == CHIP_ID_YUKON_OPT && hw->chip_rev == 0) {
3305527a6266SJeff Kirsher 			/* disable PCI-E PHY power down (set PHY reg 0x80, bit 7 */
3306527a6266SJeff Kirsher 			sky2_write32(hw, Y2_PEX_PHY_DATA, (0x80UL << 16) | (1 << 7));
3307527a6266SJeff Kirsher 
3308527a6266SJeff Kirsher 			/* set PHY Link Detect Timer to 1.1 second (11x 100ms) */
3309527a6266SJeff Kirsher 			reg = 10;
3310527a6266SJeff Kirsher 
3311527a6266SJeff Kirsher 			/* re-enable PEX PM in PEX PHY debug reg. 8 (clear bit 12) */
3312527a6266SJeff Kirsher 			sky2_write32(hw, Y2_PEX_PHY_DATA, PEX_DB_ACCESS | (0x08UL << 16));
3313527a6266SJeff Kirsher 		} else {
3314527a6266SJeff Kirsher 			/* set PHY Link Detect Timer to 0.4 second (4x 100ms) */
3315527a6266SJeff Kirsher 			reg = 3;
3316527a6266SJeff Kirsher 		}
3317527a6266SJeff Kirsher 
3318527a6266SJeff Kirsher 		reg <<= PSM_CONFIG_REG4_TIMER_PHY_LINK_DETECT_BASE;
3319527a6266SJeff Kirsher 		reg |= PSM_CONFIG_REG4_RST_PHY_LINK_DETECT;
3320527a6266SJeff Kirsher 
3321527a6266SJeff Kirsher 		/* reset PHY Link Detect */
3322527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
3323527a6266SJeff Kirsher 		sky2_pci_write16(hw, PSM_CONFIG_REG4, reg);
3324527a6266SJeff Kirsher 
3325527a6266SJeff Kirsher 		/* check if PSMv2 was running before */
3326527a6266SJeff Kirsher 		reg = sky2_pci_read16(hw, PSM_CONFIG_REG3);
3327527a6266SJeff Kirsher 		if (reg & PCI_EXP_LNKCTL_ASPMC)
3328527a6266SJeff Kirsher 			/* restore the PCIe Link Control register */
3329527a6266SJeff Kirsher 			sky2_pci_write16(hw, pdev->pcie_cap + PCI_EXP_LNKCTL,
3330527a6266SJeff Kirsher 					 reg);
3331527a6266SJeff Kirsher 
33320e767324SMirko Lindner 		if (hw->chip_id == CHIP_ID_YUKON_PRM &&
33330e767324SMirko Lindner 			hw->chip_rev == CHIP_REV_YU_PRM_A0) {
33340e767324SMirko Lindner 			/* change PHY Interrupt polarity to low active */
33350e767324SMirko Lindner 			reg = sky2_read16(hw, GPHY_CTRL);
33360e767324SMirko Lindner 			sky2_write16(hw, GPHY_CTRL, reg | GPC_INTPOL);
33370e767324SMirko Lindner 
33380e767324SMirko Lindner 			/* adapt HW for low active PHY Interrupt */
33390e767324SMirko Lindner 			reg = sky2_read16(hw, Y2_CFG_SPC + PCI_LDO_CTRL);
33400e767324SMirko Lindner 			sky2_write16(hw, Y2_CFG_SPC + PCI_LDO_CTRL, reg | PHY_M_UNDOC1);
33410e767324SMirko Lindner 		}
33420e767324SMirko Lindner 
3343527a6266SJeff Kirsher 		sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
3344527a6266SJeff Kirsher 
3345527a6266SJeff Kirsher 		/* re-enable PEX PM in PEX PHY debug reg. 8 (clear bit 12) */
3346527a6266SJeff Kirsher 		sky2_write32(hw, Y2_PEX_PHY_DATA, PEX_DB_ACCESS | (0x08UL << 16));
3347527a6266SJeff Kirsher 	}
3348527a6266SJeff Kirsher 
3349527a6266SJeff Kirsher 	/* Clear I2C IRQ noise */
3350527a6266SJeff Kirsher 	sky2_write32(hw, B2_I2C_IRQ, 1);
3351527a6266SJeff Kirsher 
3352527a6266SJeff Kirsher 	/* turn off hardware timer (unused) */
3353527a6266SJeff Kirsher 	sky2_write8(hw, B2_TI_CTRL, TIM_STOP);
3354527a6266SJeff Kirsher 	sky2_write8(hw, B2_TI_CTRL, TIM_CLR_IRQ);
3355527a6266SJeff Kirsher 
3356527a6266SJeff Kirsher 	/* Turn off descriptor polling */
3357527a6266SJeff Kirsher 	sky2_write32(hw, B28_DPT_CTRL, DPT_STOP);
3358527a6266SJeff Kirsher 
3359527a6266SJeff Kirsher 	/* Turn off receive timestamp */
3360527a6266SJeff Kirsher 	sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_STOP);
3361527a6266SJeff Kirsher 	sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
3362527a6266SJeff Kirsher 
3363527a6266SJeff Kirsher 	/* enable the Tx Arbiters */
3364527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++)
3365527a6266SJeff Kirsher 		sky2_write8(hw, SK_REG(i, TXA_CTRL), TXA_ENA_ARB);
3366527a6266SJeff Kirsher 
3367527a6266SJeff Kirsher 	/* Initialize ram interface */
3368527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
3369527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_CTRL), RI_RST_CLR);
3370527a6266SJeff Kirsher 
3371527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R1), SK_RI_TO_53);
3372527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA1), SK_RI_TO_53);
3373527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS1), SK_RI_TO_53);
3374527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R1), SK_RI_TO_53);
3375527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA1), SK_RI_TO_53);
3376527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS1), SK_RI_TO_53);
3377527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R2), SK_RI_TO_53);
3378527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA2), SK_RI_TO_53);
3379527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS2), SK_RI_TO_53);
3380527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R2), SK_RI_TO_53);
3381527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA2), SK_RI_TO_53);
3382527a6266SJeff Kirsher 		sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS2), SK_RI_TO_53);
3383527a6266SJeff Kirsher 	}
3384527a6266SJeff Kirsher 
3385527a6266SJeff Kirsher 	sky2_write32(hw, B0_HWE_IMSK, hwe_mask);
3386527a6266SJeff Kirsher 
3387527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++)
3388527a6266SJeff Kirsher 		sky2_gmac_reset(hw, i);
3389527a6266SJeff Kirsher 
3390527a6266SJeff Kirsher 	memset(hw->st_le, 0, hw->st_size * sizeof(struct sky2_status_le));
3391527a6266SJeff Kirsher 	hw->st_idx = 0;
3392527a6266SJeff Kirsher 
3393527a6266SJeff Kirsher 	sky2_write32(hw, STAT_CTRL, SC_STAT_RST_SET);
3394527a6266SJeff Kirsher 	sky2_write32(hw, STAT_CTRL, SC_STAT_RST_CLR);
3395527a6266SJeff Kirsher 
3396527a6266SJeff Kirsher 	sky2_write32(hw, STAT_LIST_ADDR_LO, hw->st_dma);
3397527a6266SJeff Kirsher 	sky2_write32(hw, STAT_LIST_ADDR_HI, (u64) hw->st_dma >> 32);
3398527a6266SJeff Kirsher 
3399527a6266SJeff Kirsher 	/* Set the list last index */
3400527a6266SJeff Kirsher 	sky2_write16(hw, STAT_LAST_IDX, hw->st_size - 1);
3401527a6266SJeff Kirsher 
3402527a6266SJeff Kirsher 	sky2_write16(hw, STAT_TX_IDX_TH, 10);
3403527a6266SJeff Kirsher 	sky2_write8(hw, STAT_FIFO_WM, 16);
3404527a6266SJeff Kirsher 
3405527a6266SJeff Kirsher 	/* set Status-FIFO ISR watermark */
3406527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0)
3407527a6266SJeff Kirsher 		sky2_write8(hw, STAT_FIFO_ISR_WM, 4);
3408527a6266SJeff Kirsher 	else
3409527a6266SJeff Kirsher 		sky2_write8(hw, STAT_FIFO_ISR_WM, 16);
3410527a6266SJeff Kirsher 
3411527a6266SJeff Kirsher 	sky2_write32(hw, STAT_TX_TIMER_INI, sky2_us2clk(hw, 1000));
3412527a6266SJeff Kirsher 	sky2_write32(hw, STAT_ISR_TIMER_INI, sky2_us2clk(hw, 20));
3413527a6266SJeff Kirsher 	sky2_write32(hw, STAT_LEV_TIMER_INI, sky2_us2clk(hw, 100));
3414527a6266SJeff Kirsher 
3415527a6266SJeff Kirsher 	/* enable status unit */
3416527a6266SJeff Kirsher 	sky2_write32(hw, STAT_CTRL, SC_STAT_OP_ON);
3417527a6266SJeff Kirsher 
3418527a6266SJeff Kirsher 	sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
3419527a6266SJeff Kirsher 	sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
3420527a6266SJeff Kirsher 	sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
3421527a6266SJeff Kirsher }
3422527a6266SJeff Kirsher 
3423527a6266SJeff Kirsher /* Take device down (offline).
3424527a6266SJeff Kirsher  * Equivalent to doing dev_stop() but this does not
3425527a6266SJeff Kirsher  * inform upper layers of the transition.
3426527a6266SJeff Kirsher  */
sky2_detach(struct net_device * dev)3427527a6266SJeff Kirsher static void sky2_detach(struct net_device *dev)
3428527a6266SJeff Kirsher {
3429527a6266SJeff Kirsher 	if (netif_running(dev)) {
3430527a6266SJeff Kirsher 		netif_tx_lock(dev);
3431527a6266SJeff Kirsher 		netif_device_detach(dev);	/* stop txq */
3432527a6266SJeff Kirsher 		netif_tx_unlock(dev);
3433926d0977Sstephen hemminger 		sky2_close(dev);
3434527a6266SJeff Kirsher 	}
3435527a6266SJeff Kirsher }
3436527a6266SJeff Kirsher 
3437527a6266SJeff Kirsher /* Bring device back after doing sky2_detach */
sky2_reattach(struct net_device * dev)3438527a6266SJeff Kirsher static int sky2_reattach(struct net_device *dev)
3439527a6266SJeff Kirsher {
3440527a6266SJeff Kirsher 	int err = 0;
3441527a6266SJeff Kirsher 
3442527a6266SJeff Kirsher 	if (netif_running(dev)) {
3443926d0977Sstephen hemminger 		err = sky2_open(dev);
3444527a6266SJeff Kirsher 		if (err) {
3445527a6266SJeff Kirsher 			netdev_info(dev, "could not restart %d\n", err);
3446527a6266SJeff Kirsher 			dev_close(dev);
3447527a6266SJeff Kirsher 		} else {
3448527a6266SJeff Kirsher 			netif_device_attach(dev);
3449527a6266SJeff Kirsher 			sky2_set_multicast(dev);
3450527a6266SJeff Kirsher 		}
3451527a6266SJeff Kirsher 	}
3452527a6266SJeff Kirsher 
3453527a6266SJeff Kirsher 	return err;
3454527a6266SJeff Kirsher }
3455527a6266SJeff Kirsher 
sky2_all_down(struct sky2_hw * hw)3456527a6266SJeff Kirsher static void sky2_all_down(struct sky2_hw *hw)
3457527a6266SJeff Kirsher {
3458527a6266SJeff Kirsher 	int i;
3459527a6266SJeff Kirsher 
3460282edcecSstephen hemminger 	if (hw->flags & SKY2_HW_IRQ_SETUP) {
3461527a6266SJeff Kirsher 		sky2_write32(hw, B0_IMSK, 0);
3462ea589e9bSLino Sanfilippo 		sky2_read32(hw, B0_IMSK);
34631401a800Sstephen hemminger 
3464527a6266SJeff Kirsher 		synchronize_irq(hw->pdev->irq);
3465527a6266SJeff Kirsher 		napi_disable(&hw->napi);
3466282edcecSstephen hemminger 	}
3467527a6266SJeff Kirsher 
3468527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
3469527a6266SJeff Kirsher 		struct net_device *dev = hw->dev[i];
3470527a6266SJeff Kirsher 		struct sky2_port *sky2 = netdev_priv(dev);
3471527a6266SJeff Kirsher 
3472527a6266SJeff Kirsher 		if (!netif_running(dev))
3473527a6266SJeff Kirsher 			continue;
3474527a6266SJeff Kirsher 
3475527a6266SJeff Kirsher 		netif_carrier_off(dev);
3476527a6266SJeff Kirsher 		netif_tx_disable(dev);
3477527a6266SJeff Kirsher 		sky2_hw_down(sky2);
3478527a6266SJeff Kirsher 	}
3479527a6266SJeff Kirsher }
3480527a6266SJeff Kirsher 
sky2_all_up(struct sky2_hw * hw)3481527a6266SJeff Kirsher static void sky2_all_up(struct sky2_hw *hw)
3482527a6266SJeff Kirsher {
3483527a6266SJeff Kirsher 	u32 imask = Y2_IS_BASE;
3484527a6266SJeff Kirsher 	int i;
3485527a6266SJeff Kirsher 
3486527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
3487527a6266SJeff Kirsher 		struct net_device *dev = hw->dev[i];
3488527a6266SJeff Kirsher 		struct sky2_port *sky2 = netdev_priv(dev);
3489527a6266SJeff Kirsher 
3490527a6266SJeff Kirsher 		if (!netif_running(dev))
3491527a6266SJeff Kirsher 			continue;
3492527a6266SJeff Kirsher 
3493527a6266SJeff Kirsher 		sky2_hw_up(sky2);
3494527a6266SJeff Kirsher 		sky2_set_multicast(dev);
3495527a6266SJeff Kirsher 		imask |= portirq_msk[i];
3496527a6266SJeff Kirsher 		netif_wake_queue(dev);
3497527a6266SJeff Kirsher 	}
3498527a6266SJeff Kirsher 
3499282edcecSstephen hemminger 	if (hw->flags & SKY2_HW_IRQ_SETUP) {
3500527a6266SJeff Kirsher 		sky2_write32(hw, B0_IMSK, imask);
3501527a6266SJeff Kirsher 		sky2_read32(hw, B0_IMSK);
3502527a6266SJeff Kirsher 		sky2_read32(hw, B0_Y2_SP_LISR);
3503527a6266SJeff Kirsher 		napi_enable(&hw->napi);
3504527a6266SJeff Kirsher 	}
35051401a800Sstephen hemminger }
3506527a6266SJeff Kirsher 
sky2_restart(struct work_struct * work)3507527a6266SJeff Kirsher static void sky2_restart(struct work_struct *work)
3508527a6266SJeff Kirsher {
3509527a6266SJeff Kirsher 	struct sky2_hw *hw = container_of(work, struct sky2_hw, restart_work);
3510527a6266SJeff Kirsher 
3511527a6266SJeff Kirsher 	rtnl_lock();
3512527a6266SJeff Kirsher 
3513527a6266SJeff Kirsher 	sky2_all_down(hw);
3514527a6266SJeff Kirsher 	sky2_reset(hw);
3515527a6266SJeff Kirsher 	sky2_all_up(hw);
3516527a6266SJeff Kirsher 
3517527a6266SJeff Kirsher 	rtnl_unlock();
3518527a6266SJeff Kirsher }
3519527a6266SJeff Kirsher 
sky2_wol_supported(const struct sky2_hw * hw)3520527a6266SJeff Kirsher static inline u8 sky2_wol_supported(const struct sky2_hw *hw)
3521527a6266SJeff Kirsher {
3522527a6266SJeff Kirsher 	return sky2_is_copper(hw) ? (WAKE_PHY | WAKE_MAGIC) : 0;
3523527a6266SJeff Kirsher }
3524527a6266SJeff Kirsher 
sky2_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)3525527a6266SJeff Kirsher static void sky2_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3526527a6266SJeff Kirsher {
3527527a6266SJeff Kirsher 	const struct sky2_port *sky2 = netdev_priv(dev);
3528527a6266SJeff Kirsher 
3529527a6266SJeff Kirsher 	wol->supported = sky2_wol_supported(sky2->hw);
3530527a6266SJeff Kirsher 	wol->wolopts = sky2->wol;
3531527a6266SJeff Kirsher }
3532527a6266SJeff Kirsher 
sky2_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)3533527a6266SJeff Kirsher static int sky2_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3534527a6266SJeff Kirsher {
3535527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3536527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3537527a6266SJeff Kirsher 	bool enable_wakeup = false;
3538527a6266SJeff Kirsher 	int i;
3539527a6266SJeff Kirsher 
3540527a6266SJeff Kirsher 	if ((wol->wolopts & ~sky2_wol_supported(sky2->hw)) ||
3541527a6266SJeff Kirsher 	    !device_can_wakeup(&hw->pdev->dev))
3542527a6266SJeff Kirsher 		return -EOPNOTSUPP;
3543527a6266SJeff Kirsher 
3544527a6266SJeff Kirsher 	sky2->wol = wol->wolopts;
3545527a6266SJeff Kirsher 
3546527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
3547527a6266SJeff Kirsher 		struct net_device *dev = hw->dev[i];
3548527a6266SJeff Kirsher 		struct sky2_port *sky2 = netdev_priv(dev);
3549527a6266SJeff Kirsher 
3550527a6266SJeff Kirsher 		if (sky2->wol)
3551527a6266SJeff Kirsher 			enable_wakeup = true;
3552527a6266SJeff Kirsher 	}
3553527a6266SJeff Kirsher 	device_set_wakeup_enable(&hw->pdev->dev, enable_wakeup);
3554527a6266SJeff Kirsher 
3555527a6266SJeff Kirsher 	return 0;
3556527a6266SJeff Kirsher }
3557527a6266SJeff Kirsher 
sky2_supported_modes(const struct sky2_hw * hw)3558527a6266SJeff Kirsher static u32 sky2_supported_modes(const struct sky2_hw *hw)
3559527a6266SJeff Kirsher {
3560527a6266SJeff Kirsher 	if (sky2_is_copper(hw)) {
3561527a6266SJeff Kirsher 		u32 modes = SUPPORTED_10baseT_Half
3562527a6266SJeff Kirsher 			| SUPPORTED_10baseT_Full
3563527a6266SJeff Kirsher 			| SUPPORTED_100baseT_Half
3564527a6266SJeff Kirsher 			| SUPPORTED_100baseT_Full;
3565527a6266SJeff Kirsher 
3566527a6266SJeff Kirsher 		if (hw->flags & SKY2_HW_GIGABIT)
3567527a6266SJeff Kirsher 			modes |= SUPPORTED_1000baseT_Half
3568527a6266SJeff Kirsher 				| SUPPORTED_1000baseT_Full;
3569527a6266SJeff Kirsher 		return modes;
3570527a6266SJeff Kirsher 	} else
3571527a6266SJeff Kirsher 		return SUPPORTED_1000baseT_Half
3572527a6266SJeff Kirsher 			| SUPPORTED_1000baseT_Full;
3573527a6266SJeff Kirsher }
3574527a6266SJeff Kirsher 
sky2_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)357555f78fcdSPhilippe Reynes static int sky2_get_link_ksettings(struct net_device *dev,
357655f78fcdSPhilippe Reynes 				   struct ethtool_link_ksettings *cmd)
3577527a6266SJeff Kirsher {
3578527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3579527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
358055f78fcdSPhilippe Reynes 	u32 supported, advertising;
3581527a6266SJeff Kirsher 
358255f78fcdSPhilippe Reynes 	supported = sky2_supported_modes(hw);
358355f78fcdSPhilippe Reynes 	cmd->base.phy_address = PHY_ADDR_MARV;
3584527a6266SJeff Kirsher 	if (sky2_is_copper(hw)) {
358555f78fcdSPhilippe Reynes 		cmd->base.port = PORT_TP;
358655f78fcdSPhilippe Reynes 		cmd->base.speed = sky2->speed;
358755f78fcdSPhilippe Reynes 		supported |=  SUPPORTED_Autoneg | SUPPORTED_TP;
3588527a6266SJeff Kirsher 	} else {
358955f78fcdSPhilippe Reynes 		cmd->base.speed = SPEED_1000;
359055f78fcdSPhilippe Reynes 		cmd->base.port = PORT_FIBRE;
359155f78fcdSPhilippe Reynes 		supported |=  SUPPORTED_Autoneg | SUPPORTED_FIBRE;
3592527a6266SJeff Kirsher 	}
3593527a6266SJeff Kirsher 
359455f78fcdSPhilippe Reynes 	advertising = sky2->advertising;
359555f78fcdSPhilippe Reynes 	cmd->base.autoneg = (sky2->flags & SKY2_FLAG_AUTO_SPEED)
3596527a6266SJeff Kirsher 		? AUTONEG_ENABLE : AUTONEG_DISABLE;
359755f78fcdSPhilippe Reynes 	cmd->base.duplex = sky2->duplex;
359855f78fcdSPhilippe Reynes 
359955f78fcdSPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
360055f78fcdSPhilippe Reynes 						supported);
360155f78fcdSPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
360255f78fcdSPhilippe Reynes 						advertising);
360355f78fcdSPhilippe Reynes 
3604527a6266SJeff Kirsher 	return 0;
3605527a6266SJeff Kirsher }
3606527a6266SJeff Kirsher 
sky2_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)360755f78fcdSPhilippe Reynes static int sky2_set_link_ksettings(struct net_device *dev,
360855f78fcdSPhilippe Reynes 				   const struct ethtool_link_ksettings *cmd)
3609527a6266SJeff Kirsher {
3610527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3611527a6266SJeff Kirsher 	const struct sky2_hw *hw = sky2->hw;
3612527a6266SJeff Kirsher 	u32 supported = sky2_supported_modes(hw);
361355f78fcdSPhilippe Reynes 	u32 new_advertising;
3614527a6266SJeff Kirsher 
361555f78fcdSPhilippe Reynes 	ethtool_convert_link_mode_to_legacy_u32(&new_advertising,
361655f78fcdSPhilippe Reynes 						cmd->link_modes.advertising);
361755f78fcdSPhilippe Reynes 
361855f78fcdSPhilippe Reynes 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
361955f78fcdSPhilippe Reynes 		if (new_advertising & ~supported)
3620527a6266SJeff Kirsher 			return -EINVAL;
3621527a6266SJeff Kirsher 
3622527a6266SJeff Kirsher 		if (sky2_is_copper(hw))
362355f78fcdSPhilippe Reynes 			sky2->advertising = new_advertising |
3624527a6266SJeff Kirsher 					    ADVERTISED_TP |
3625527a6266SJeff Kirsher 					    ADVERTISED_Autoneg;
3626527a6266SJeff Kirsher 		else
362755f78fcdSPhilippe Reynes 			sky2->advertising = new_advertising |
3628527a6266SJeff Kirsher 					    ADVERTISED_FIBRE |
3629527a6266SJeff Kirsher 					    ADVERTISED_Autoneg;
3630527a6266SJeff Kirsher 
3631527a6266SJeff Kirsher 		sky2->flags |= SKY2_FLAG_AUTO_SPEED;
3632527a6266SJeff Kirsher 		sky2->duplex = -1;
3633527a6266SJeff Kirsher 		sky2->speed = -1;
3634527a6266SJeff Kirsher 	} else {
3635527a6266SJeff Kirsher 		u32 setting;
363655f78fcdSPhilippe Reynes 		u32 speed = cmd->base.speed;
3637527a6266SJeff Kirsher 
3638527a6266SJeff Kirsher 		switch (speed) {
3639527a6266SJeff Kirsher 		case SPEED_1000:
364055f78fcdSPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL)
3641527a6266SJeff Kirsher 				setting = SUPPORTED_1000baseT_Full;
364255f78fcdSPhilippe Reynes 			else if (cmd->base.duplex == DUPLEX_HALF)
3643527a6266SJeff Kirsher 				setting = SUPPORTED_1000baseT_Half;
3644527a6266SJeff Kirsher 			else
3645527a6266SJeff Kirsher 				return -EINVAL;
3646527a6266SJeff Kirsher 			break;
3647527a6266SJeff Kirsher 		case SPEED_100:
364855f78fcdSPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL)
3649527a6266SJeff Kirsher 				setting = SUPPORTED_100baseT_Full;
365055f78fcdSPhilippe Reynes 			else if (cmd->base.duplex == DUPLEX_HALF)
3651527a6266SJeff Kirsher 				setting = SUPPORTED_100baseT_Half;
3652527a6266SJeff Kirsher 			else
3653527a6266SJeff Kirsher 				return -EINVAL;
3654527a6266SJeff Kirsher 			break;
3655527a6266SJeff Kirsher 
3656527a6266SJeff Kirsher 		case SPEED_10:
365755f78fcdSPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL)
3658527a6266SJeff Kirsher 				setting = SUPPORTED_10baseT_Full;
365955f78fcdSPhilippe Reynes 			else if (cmd->base.duplex == DUPLEX_HALF)
3660527a6266SJeff Kirsher 				setting = SUPPORTED_10baseT_Half;
3661527a6266SJeff Kirsher 			else
3662527a6266SJeff Kirsher 				return -EINVAL;
3663527a6266SJeff Kirsher 			break;
3664527a6266SJeff Kirsher 		default:
3665527a6266SJeff Kirsher 			return -EINVAL;
3666527a6266SJeff Kirsher 		}
3667527a6266SJeff Kirsher 
3668527a6266SJeff Kirsher 		if ((setting & supported) == 0)
3669527a6266SJeff Kirsher 			return -EINVAL;
3670527a6266SJeff Kirsher 
3671527a6266SJeff Kirsher 		sky2->speed = speed;
367255f78fcdSPhilippe Reynes 		sky2->duplex = cmd->base.duplex;
3673527a6266SJeff Kirsher 		sky2->flags &= ~SKY2_FLAG_AUTO_SPEED;
3674527a6266SJeff Kirsher 	}
3675527a6266SJeff Kirsher 
3676527a6266SJeff Kirsher 	if (netif_running(dev)) {
3677527a6266SJeff Kirsher 		sky2_phy_reinit(sky2);
3678527a6266SJeff Kirsher 		sky2_set_multicast(dev);
3679527a6266SJeff Kirsher 	}
3680527a6266SJeff Kirsher 
3681527a6266SJeff Kirsher 	return 0;
3682527a6266SJeff Kirsher }
3683527a6266SJeff Kirsher 
sky2_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)3684527a6266SJeff Kirsher static void sky2_get_drvinfo(struct net_device *dev,
3685527a6266SJeff Kirsher 			     struct ethtool_drvinfo *info)
3686527a6266SJeff Kirsher {
3687527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3688527a6266SJeff Kirsher 
3689f029c781SWolfram Sang 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
3690f029c781SWolfram Sang 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
3691f029c781SWolfram Sang 	strscpy(info->bus_info, pci_name(sky2->hw->pdev),
369268aad78cSRick Jones 		sizeof(info->bus_info));
3693527a6266SJeff Kirsher }
3694527a6266SJeff Kirsher 
3695527a6266SJeff Kirsher static const struct sky2_stat {
3696527a6266SJeff Kirsher 	char name[ETH_GSTRING_LEN];
3697527a6266SJeff Kirsher 	u16 offset;
3698527a6266SJeff Kirsher } sky2_stats[] = {
3699527a6266SJeff Kirsher 	{ "tx_bytes",	   GM_TXO_OK_HI },
3700527a6266SJeff Kirsher 	{ "rx_bytes",	   GM_RXO_OK_HI },
3701527a6266SJeff Kirsher 	{ "tx_broadcast",  GM_TXF_BC_OK },
3702527a6266SJeff Kirsher 	{ "rx_broadcast",  GM_RXF_BC_OK },
3703527a6266SJeff Kirsher 	{ "tx_multicast",  GM_TXF_MC_OK },
3704527a6266SJeff Kirsher 	{ "rx_multicast",  GM_RXF_MC_OK },
3705527a6266SJeff Kirsher 	{ "tx_unicast",    GM_TXF_UC_OK },
3706527a6266SJeff Kirsher 	{ "rx_unicast",    GM_RXF_UC_OK },
3707527a6266SJeff Kirsher 	{ "tx_mac_pause",  GM_TXF_MPAUSE },
3708527a6266SJeff Kirsher 	{ "rx_mac_pause",  GM_RXF_MPAUSE },
3709527a6266SJeff Kirsher 	{ "collisions",    GM_TXF_COL },
3710527a6266SJeff Kirsher 	{ "late_collision",GM_TXF_LAT_COL },
3711527a6266SJeff Kirsher 	{ "aborted", 	   GM_TXF_ABO_COL },
3712527a6266SJeff Kirsher 	{ "single_collisions", GM_TXF_SNG_COL },
3713527a6266SJeff Kirsher 	{ "multi_collisions", GM_TXF_MUL_COL },
3714527a6266SJeff Kirsher 
3715527a6266SJeff Kirsher 	{ "rx_short",      GM_RXF_SHT },
3716527a6266SJeff Kirsher 	{ "rx_runt", 	   GM_RXE_FRAG },
3717527a6266SJeff Kirsher 	{ "rx_64_byte_packets", GM_RXF_64B },
3718527a6266SJeff Kirsher 	{ "rx_65_to_127_byte_packets", GM_RXF_127B },
3719527a6266SJeff Kirsher 	{ "rx_128_to_255_byte_packets", GM_RXF_255B },
3720527a6266SJeff Kirsher 	{ "rx_256_to_511_byte_packets", GM_RXF_511B },
3721527a6266SJeff Kirsher 	{ "rx_512_to_1023_byte_packets", GM_RXF_1023B },
3722527a6266SJeff Kirsher 	{ "rx_1024_to_1518_byte_packets", GM_RXF_1518B },
3723527a6266SJeff Kirsher 	{ "rx_1518_to_max_byte_packets", GM_RXF_MAX_SZ },
3724527a6266SJeff Kirsher 	{ "rx_too_long",   GM_RXF_LNG_ERR },
3725527a6266SJeff Kirsher 	{ "rx_fifo_overflow", GM_RXE_FIFO_OV },
3726527a6266SJeff Kirsher 	{ "rx_jabber",     GM_RXF_JAB_PKT },
3727527a6266SJeff Kirsher 	{ "rx_fcs_error",   GM_RXF_FCS_ERR },
3728527a6266SJeff Kirsher 
3729527a6266SJeff Kirsher 	{ "tx_64_byte_packets", GM_TXF_64B },
3730527a6266SJeff Kirsher 	{ "tx_65_to_127_byte_packets", GM_TXF_127B },
3731527a6266SJeff Kirsher 	{ "tx_128_to_255_byte_packets", GM_TXF_255B },
3732527a6266SJeff Kirsher 	{ "tx_256_to_511_byte_packets", GM_TXF_511B },
3733527a6266SJeff Kirsher 	{ "tx_512_to_1023_byte_packets", GM_TXF_1023B },
3734527a6266SJeff Kirsher 	{ "tx_1024_to_1518_byte_packets", GM_TXF_1518B },
3735527a6266SJeff Kirsher 	{ "tx_1519_to_max_byte_packets", GM_TXF_MAX_SZ },
3736527a6266SJeff Kirsher 	{ "tx_fifo_underrun", GM_TXE_FIFO_UR },
3737527a6266SJeff Kirsher };
3738527a6266SJeff Kirsher 
sky2_get_msglevel(struct net_device * netdev)3739527a6266SJeff Kirsher static u32 sky2_get_msglevel(struct net_device *netdev)
3740527a6266SJeff Kirsher {
3741527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(netdev);
3742527a6266SJeff Kirsher 	return sky2->msg_enable;
3743527a6266SJeff Kirsher }
3744527a6266SJeff Kirsher 
sky2_nway_reset(struct net_device * dev)3745527a6266SJeff Kirsher static int sky2_nway_reset(struct net_device *dev)
3746527a6266SJeff Kirsher {
3747527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3748527a6266SJeff Kirsher 
3749527a6266SJeff Kirsher 	if (!netif_running(dev) || !(sky2->flags & SKY2_FLAG_AUTO_SPEED))
3750527a6266SJeff Kirsher 		return -EINVAL;
3751527a6266SJeff Kirsher 
3752527a6266SJeff Kirsher 	sky2_phy_reinit(sky2);
3753527a6266SJeff Kirsher 	sky2_set_multicast(dev);
3754527a6266SJeff Kirsher 
3755527a6266SJeff Kirsher 	return 0;
3756527a6266SJeff Kirsher }
3757527a6266SJeff Kirsher 
sky2_phy_stats(struct sky2_port * sky2,u64 * data,unsigned count)3758527a6266SJeff Kirsher static void sky2_phy_stats(struct sky2_port *sky2, u64 * data, unsigned count)
3759527a6266SJeff Kirsher {
3760527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3761527a6266SJeff Kirsher 	unsigned port = sky2->port;
3762527a6266SJeff Kirsher 	int i;
3763527a6266SJeff Kirsher 
3764527a6266SJeff Kirsher 	data[0] = get_stats64(hw, port, GM_TXO_OK_LO);
3765527a6266SJeff Kirsher 	data[1] = get_stats64(hw, port, GM_RXO_OK_LO);
3766527a6266SJeff Kirsher 
3767527a6266SJeff Kirsher 	for (i = 2; i < count; i++)
3768527a6266SJeff Kirsher 		data[i] = get_stats32(hw, port, sky2_stats[i].offset);
3769527a6266SJeff Kirsher }
3770527a6266SJeff Kirsher 
sky2_set_msglevel(struct net_device * netdev,u32 value)3771527a6266SJeff Kirsher static void sky2_set_msglevel(struct net_device *netdev, u32 value)
3772527a6266SJeff Kirsher {
3773527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(netdev);
3774527a6266SJeff Kirsher 	sky2->msg_enable = value;
3775527a6266SJeff Kirsher }
3776527a6266SJeff Kirsher 
sky2_get_sset_count(struct net_device * dev,int sset)3777527a6266SJeff Kirsher static int sky2_get_sset_count(struct net_device *dev, int sset)
3778527a6266SJeff Kirsher {
3779527a6266SJeff Kirsher 	switch (sset) {
3780527a6266SJeff Kirsher 	case ETH_SS_STATS:
3781527a6266SJeff Kirsher 		return ARRAY_SIZE(sky2_stats);
3782527a6266SJeff Kirsher 	default:
3783527a6266SJeff Kirsher 		return -EOPNOTSUPP;
3784527a6266SJeff Kirsher 	}
3785527a6266SJeff Kirsher }
3786527a6266SJeff Kirsher 
sky2_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)3787527a6266SJeff Kirsher static void sky2_get_ethtool_stats(struct net_device *dev,
3788527a6266SJeff Kirsher 				   struct ethtool_stats *stats, u64 * data)
3789527a6266SJeff Kirsher {
3790527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3791527a6266SJeff Kirsher 
3792527a6266SJeff Kirsher 	sky2_phy_stats(sky2, data, ARRAY_SIZE(sky2_stats));
3793527a6266SJeff Kirsher }
3794527a6266SJeff Kirsher 
sky2_get_strings(struct net_device * dev,u32 stringset,u8 * data)3795527a6266SJeff Kirsher static void sky2_get_strings(struct net_device *dev, u32 stringset, u8 * data)
3796527a6266SJeff Kirsher {
3797527a6266SJeff Kirsher 	int i;
3798527a6266SJeff Kirsher 
3799527a6266SJeff Kirsher 	switch (stringset) {
3800527a6266SJeff Kirsher 	case ETH_SS_STATS:
3801527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(sky2_stats); i++)
3802527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
3803527a6266SJeff Kirsher 			       sky2_stats[i].name, ETH_GSTRING_LEN);
3804527a6266SJeff Kirsher 		break;
3805527a6266SJeff Kirsher 	}
3806527a6266SJeff Kirsher }
3807527a6266SJeff Kirsher 
sky2_set_mac_address(struct net_device * dev,void * p)3808527a6266SJeff Kirsher static int sky2_set_mac_address(struct net_device *dev, void *p)
3809527a6266SJeff Kirsher {
3810527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3811527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3812527a6266SJeff Kirsher 	unsigned port = sky2->port;
3813527a6266SJeff Kirsher 	const struct sockaddr *addr = p;
3814527a6266SJeff Kirsher 
3815527a6266SJeff Kirsher 	if (!is_valid_ether_addr(addr->sa_data))
3816527a6266SJeff Kirsher 		return -EADDRNOTAVAIL;
3817527a6266SJeff Kirsher 
3818a96d317fSJakub Kicinski 	eth_hw_addr_set(dev, addr->sa_data);
3819527a6266SJeff Kirsher 	memcpy_toio(hw->regs + B2_MAC_1 + port * 8,
3820527a6266SJeff Kirsher 		    dev->dev_addr, ETH_ALEN);
3821527a6266SJeff Kirsher 	memcpy_toio(hw->regs + B2_MAC_2 + port * 8,
3822527a6266SJeff Kirsher 		    dev->dev_addr, ETH_ALEN);
3823527a6266SJeff Kirsher 
3824527a6266SJeff Kirsher 	/* virtual address for data */
3825527a6266SJeff Kirsher 	gma_set_addr(hw, port, GM_SRC_ADDR_2L, dev->dev_addr);
3826527a6266SJeff Kirsher 
3827527a6266SJeff Kirsher 	/* physical address: used for pause frames */
3828527a6266SJeff Kirsher 	gma_set_addr(hw, port, GM_SRC_ADDR_1L, dev->dev_addr);
3829527a6266SJeff Kirsher 
3830527a6266SJeff Kirsher 	return 0;
3831527a6266SJeff Kirsher }
3832527a6266SJeff Kirsher 
sky2_add_filter(u8 filter[8],const u8 * addr)3833527a6266SJeff Kirsher static inline void sky2_add_filter(u8 filter[8], const u8 *addr)
3834527a6266SJeff Kirsher {
3835527a6266SJeff Kirsher 	u32 bit;
3836527a6266SJeff Kirsher 
3837527a6266SJeff Kirsher 	bit = ether_crc(ETH_ALEN, addr) & 63;
3838527a6266SJeff Kirsher 	filter[bit >> 3] |= 1 << (bit & 7);
3839527a6266SJeff Kirsher }
3840527a6266SJeff Kirsher 
sky2_set_multicast(struct net_device * dev)3841527a6266SJeff Kirsher static void sky2_set_multicast(struct net_device *dev)
3842527a6266SJeff Kirsher {
3843527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3844527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3845527a6266SJeff Kirsher 	unsigned port = sky2->port;
3846527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
3847527a6266SJeff Kirsher 	u16 reg;
3848527a6266SJeff Kirsher 	u8 filter[8];
3849527a6266SJeff Kirsher 	int rx_pause;
3850527a6266SJeff Kirsher 	static const u8 pause_mc_addr[ETH_ALEN] = { 0x1, 0x80, 0xc2, 0x0, 0x0, 0x1 };
3851527a6266SJeff Kirsher 
3852527a6266SJeff Kirsher 	rx_pause = (sky2->flow_status == FC_RX || sky2->flow_status == FC_BOTH);
3853527a6266SJeff Kirsher 	memset(filter, 0, sizeof(filter));
3854527a6266SJeff Kirsher 
3855527a6266SJeff Kirsher 	reg = gma_read16(hw, port, GM_RX_CTRL);
3856527a6266SJeff Kirsher 	reg |= GM_RXCR_UCF_ENA;
3857527a6266SJeff Kirsher 
3858527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)	/* promiscuous */
3859527a6266SJeff Kirsher 		reg &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
3860527a6266SJeff Kirsher 	else if (dev->flags & IFF_ALLMULTI)
3861527a6266SJeff Kirsher 		memset(filter, 0xff, sizeof(filter));
3862527a6266SJeff Kirsher 	else if (netdev_mc_empty(dev) && !rx_pause)
3863527a6266SJeff Kirsher 		reg &= ~GM_RXCR_MCF_ENA;
3864527a6266SJeff Kirsher 	else {
3865527a6266SJeff Kirsher 		reg |= GM_RXCR_MCF_ENA;
3866527a6266SJeff Kirsher 
3867527a6266SJeff Kirsher 		if (rx_pause)
3868527a6266SJeff Kirsher 			sky2_add_filter(filter, pause_mc_addr);
3869527a6266SJeff Kirsher 
3870527a6266SJeff Kirsher 		netdev_for_each_mc_addr(ha, dev)
3871527a6266SJeff Kirsher 			sky2_add_filter(filter, ha->addr);
3872527a6266SJeff Kirsher 	}
3873527a6266SJeff Kirsher 
3874527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H1,
3875527a6266SJeff Kirsher 		    (u16) filter[0] | ((u16) filter[1] << 8));
3876527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H2,
3877527a6266SJeff Kirsher 		    (u16) filter[2] | ((u16) filter[3] << 8));
3878527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H3,
3879527a6266SJeff Kirsher 		    (u16) filter[4] | ((u16) filter[5] << 8));
3880527a6266SJeff Kirsher 	gma_write16(hw, port, GM_MC_ADDR_H4,
3881527a6266SJeff Kirsher 		    (u16) filter[6] | ((u16) filter[7] << 8));
3882527a6266SJeff Kirsher 
3883527a6266SJeff Kirsher 	gma_write16(hw, port, GM_RX_CTRL, reg);
3884527a6266SJeff Kirsher }
3885527a6266SJeff Kirsher 
sky2_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)3886bc1f4470Sstephen hemminger static void sky2_get_stats(struct net_device *dev,
3887527a6266SJeff Kirsher 			   struct rtnl_link_stats64 *stats)
3888527a6266SJeff Kirsher {
3889527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3890527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3891527a6266SJeff Kirsher 	unsigned port = sky2->port;
3892527a6266SJeff Kirsher 	unsigned int start;
3893527a6266SJeff Kirsher 	u64 _bytes, _packets;
3894527a6266SJeff Kirsher 
3895527a6266SJeff Kirsher 	do {
3896068c38adSThomas Gleixner 		start = u64_stats_fetch_begin(&sky2->rx_stats.syncp);
3897527a6266SJeff Kirsher 		_bytes = sky2->rx_stats.bytes;
3898527a6266SJeff Kirsher 		_packets = sky2->rx_stats.packets;
3899068c38adSThomas Gleixner 	} while (u64_stats_fetch_retry(&sky2->rx_stats.syncp, start));
3900527a6266SJeff Kirsher 
3901527a6266SJeff Kirsher 	stats->rx_packets = _packets;
3902527a6266SJeff Kirsher 	stats->rx_bytes = _bytes;
3903527a6266SJeff Kirsher 
3904527a6266SJeff Kirsher 	do {
3905068c38adSThomas Gleixner 		start = u64_stats_fetch_begin(&sky2->tx_stats.syncp);
3906527a6266SJeff Kirsher 		_bytes = sky2->tx_stats.bytes;
3907527a6266SJeff Kirsher 		_packets = sky2->tx_stats.packets;
3908068c38adSThomas Gleixner 	} while (u64_stats_fetch_retry(&sky2->tx_stats.syncp, start));
3909527a6266SJeff Kirsher 
3910527a6266SJeff Kirsher 	stats->tx_packets = _packets;
3911527a6266SJeff Kirsher 	stats->tx_bytes = _bytes;
3912527a6266SJeff Kirsher 
3913527a6266SJeff Kirsher 	stats->multicast = get_stats32(hw, port, GM_RXF_MC_OK)
3914527a6266SJeff Kirsher 		+ get_stats32(hw, port, GM_RXF_BC_OK);
3915527a6266SJeff Kirsher 
3916527a6266SJeff Kirsher 	stats->collisions = get_stats32(hw, port, GM_TXF_COL);
3917527a6266SJeff Kirsher 
3918527a6266SJeff Kirsher 	stats->rx_length_errors = get_stats32(hw, port, GM_RXF_LNG_ERR);
3919527a6266SJeff Kirsher 	stats->rx_crc_errors = get_stats32(hw, port, GM_RXF_FCS_ERR);
3920527a6266SJeff Kirsher 	stats->rx_frame_errors = get_stats32(hw, port, GM_RXF_SHT)
3921527a6266SJeff Kirsher 		+ get_stats32(hw, port, GM_RXE_FRAG);
3922527a6266SJeff Kirsher 	stats->rx_over_errors = get_stats32(hw, port, GM_RXE_FIFO_OV);
3923527a6266SJeff Kirsher 
3924527a6266SJeff Kirsher 	stats->rx_dropped = dev->stats.rx_dropped;
3925527a6266SJeff Kirsher 	stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
3926527a6266SJeff Kirsher 	stats->tx_fifo_errors = dev->stats.tx_fifo_errors;
3927527a6266SJeff Kirsher }
3928527a6266SJeff Kirsher 
3929527a6266SJeff Kirsher /* Can have one global because blinking is controlled by
3930527a6266SJeff Kirsher  * ethtool and that is always under RTNL mutex
3931527a6266SJeff Kirsher  */
sky2_led(struct sky2_port * sky2,enum led_mode mode)3932527a6266SJeff Kirsher static void sky2_led(struct sky2_port *sky2, enum led_mode mode)
3933527a6266SJeff Kirsher {
3934527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
3935527a6266SJeff Kirsher 	unsigned port = sky2->port;
3936527a6266SJeff Kirsher 
3937527a6266SJeff Kirsher 	spin_lock_bh(&sky2->phy_lock);
3938527a6266SJeff Kirsher 	if (hw->chip_id == CHIP_ID_YUKON_EC_U ||
3939527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_EX ||
3940527a6266SJeff Kirsher 	    hw->chip_id == CHIP_ID_YUKON_SUPR) {
3941527a6266SJeff Kirsher 		u16 pg;
3942527a6266SJeff Kirsher 		pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
3943527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
3944527a6266SJeff Kirsher 
3945527a6266SJeff Kirsher 		switch (mode) {
3946527a6266SJeff Kirsher 		case MO_LED_OFF:
3947527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
3948527a6266SJeff Kirsher 				     PHY_M_LEDC_LOS_CTRL(8) |
3949527a6266SJeff Kirsher 				     PHY_M_LEDC_INIT_CTRL(8) |
3950527a6266SJeff Kirsher 				     PHY_M_LEDC_STA1_CTRL(8) |
3951527a6266SJeff Kirsher 				     PHY_M_LEDC_STA0_CTRL(8));
3952527a6266SJeff Kirsher 			break;
3953527a6266SJeff Kirsher 		case MO_LED_ON:
3954527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
3955527a6266SJeff Kirsher 				     PHY_M_LEDC_LOS_CTRL(9) |
3956527a6266SJeff Kirsher 				     PHY_M_LEDC_INIT_CTRL(9) |
3957527a6266SJeff Kirsher 				     PHY_M_LEDC_STA1_CTRL(9) |
3958527a6266SJeff Kirsher 				     PHY_M_LEDC_STA0_CTRL(9));
3959527a6266SJeff Kirsher 			break;
3960527a6266SJeff Kirsher 		case MO_LED_BLINK:
3961527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
3962527a6266SJeff Kirsher 				     PHY_M_LEDC_LOS_CTRL(0xa) |
3963527a6266SJeff Kirsher 				     PHY_M_LEDC_INIT_CTRL(0xa) |
3964527a6266SJeff Kirsher 				     PHY_M_LEDC_STA1_CTRL(0xa) |
3965527a6266SJeff Kirsher 				     PHY_M_LEDC_STA0_CTRL(0xa));
3966527a6266SJeff Kirsher 			break;
3967527a6266SJeff Kirsher 		case MO_LED_NORM:
3968527a6266SJeff Kirsher 			gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
3969527a6266SJeff Kirsher 				     PHY_M_LEDC_LOS_CTRL(1) |
3970527a6266SJeff Kirsher 				     PHY_M_LEDC_INIT_CTRL(8) |
3971527a6266SJeff Kirsher 				     PHY_M_LEDC_STA1_CTRL(7) |
3972527a6266SJeff Kirsher 				     PHY_M_LEDC_STA0_CTRL(7));
3973527a6266SJeff Kirsher 		}
3974527a6266SJeff Kirsher 
3975527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
3976527a6266SJeff Kirsher 	} else
3977527a6266SJeff Kirsher 		gm_phy_write(hw, port, PHY_MARV_LED_OVER,
3978527a6266SJeff Kirsher 				     PHY_M_LED_MO_DUP(mode) |
3979527a6266SJeff Kirsher 				     PHY_M_LED_MO_10(mode) |
3980527a6266SJeff Kirsher 				     PHY_M_LED_MO_100(mode) |
3981527a6266SJeff Kirsher 				     PHY_M_LED_MO_1000(mode) |
3982527a6266SJeff Kirsher 				     PHY_M_LED_MO_RX(mode) |
3983527a6266SJeff Kirsher 				     PHY_M_LED_MO_TX(mode));
3984527a6266SJeff Kirsher 
3985527a6266SJeff Kirsher 	spin_unlock_bh(&sky2->phy_lock);
3986527a6266SJeff Kirsher }
3987527a6266SJeff Kirsher 
3988527a6266SJeff Kirsher /* blink LED's for finding board */
sky2_set_phys_id(struct net_device * dev,enum ethtool_phys_id_state state)3989527a6266SJeff Kirsher static int sky2_set_phys_id(struct net_device *dev,
3990527a6266SJeff Kirsher 			    enum ethtool_phys_id_state state)
3991527a6266SJeff Kirsher {
3992527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
3993527a6266SJeff Kirsher 
3994527a6266SJeff Kirsher 	switch (state) {
3995527a6266SJeff Kirsher 	case ETHTOOL_ID_ACTIVE:
3996527a6266SJeff Kirsher 		return 1;	/* cycle on/off once per second */
3997527a6266SJeff Kirsher 	case ETHTOOL_ID_INACTIVE:
3998527a6266SJeff Kirsher 		sky2_led(sky2, MO_LED_NORM);
3999527a6266SJeff Kirsher 		break;
4000527a6266SJeff Kirsher 	case ETHTOOL_ID_ON:
4001527a6266SJeff Kirsher 		sky2_led(sky2, MO_LED_ON);
4002527a6266SJeff Kirsher 		break;
4003527a6266SJeff Kirsher 	case ETHTOOL_ID_OFF:
4004527a6266SJeff Kirsher 		sky2_led(sky2, MO_LED_OFF);
4005527a6266SJeff Kirsher 		break;
4006527a6266SJeff Kirsher 	}
4007527a6266SJeff Kirsher 
4008527a6266SJeff Kirsher 	return 0;
4009527a6266SJeff Kirsher }
4010527a6266SJeff Kirsher 
sky2_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * ecmd)4011527a6266SJeff Kirsher static void sky2_get_pauseparam(struct net_device *dev,
4012527a6266SJeff Kirsher 				struct ethtool_pauseparam *ecmd)
4013527a6266SJeff Kirsher {
4014527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4015527a6266SJeff Kirsher 
4016527a6266SJeff Kirsher 	switch (sky2->flow_mode) {
4017527a6266SJeff Kirsher 	case FC_NONE:
4018527a6266SJeff Kirsher 		ecmd->tx_pause = ecmd->rx_pause = 0;
4019527a6266SJeff Kirsher 		break;
4020527a6266SJeff Kirsher 	case FC_TX:
4021527a6266SJeff Kirsher 		ecmd->tx_pause = 1, ecmd->rx_pause = 0;
4022527a6266SJeff Kirsher 		break;
4023527a6266SJeff Kirsher 	case FC_RX:
4024527a6266SJeff Kirsher 		ecmd->tx_pause = 0, ecmd->rx_pause = 1;
4025527a6266SJeff Kirsher 		break;
4026527a6266SJeff Kirsher 	case FC_BOTH:
4027527a6266SJeff Kirsher 		ecmd->tx_pause = ecmd->rx_pause = 1;
4028527a6266SJeff Kirsher 	}
4029527a6266SJeff Kirsher 
4030527a6266SJeff Kirsher 	ecmd->autoneg = (sky2->flags & SKY2_FLAG_AUTO_PAUSE)
4031527a6266SJeff Kirsher 		? AUTONEG_ENABLE : AUTONEG_DISABLE;
4032527a6266SJeff Kirsher }
4033527a6266SJeff Kirsher 
sky2_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * ecmd)4034527a6266SJeff Kirsher static int sky2_set_pauseparam(struct net_device *dev,
4035527a6266SJeff Kirsher 			       struct ethtool_pauseparam *ecmd)
4036527a6266SJeff Kirsher {
4037527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4038527a6266SJeff Kirsher 
4039527a6266SJeff Kirsher 	if (ecmd->autoneg == AUTONEG_ENABLE)
4040527a6266SJeff Kirsher 		sky2->flags |= SKY2_FLAG_AUTO_PAUSE;
4041527a6266SJeff Kirsher 	else
4042527a6266SJeff Kirsher 		sky2->flags &= ~SKY2_FLAG_AUTO_PAUSE;
4043527a6266SJeff Kirsher 
4044527a6266SJeff Kirsher 	sky2->flow_mode = sky2_flow(ecmd->rx_pause, ecmd->tx_pause);
4045527a6266SJeff Kirsher 
4046527a6266SJeff Kirsher 	if (netif_running(dev))
4047527a6266SJeff Kirsher 		sky2_phy_reinit(sky2);
4048527a6266SJeff Kirsher 
4049527a6266SJeff Kirsher 	return 0;
4050527a6266SJeff Kirsher }
4051527a6266SJeff Kirsher 
sky2_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ecmd,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)4052527a6266SJeff Kirsher static int sky2_get_coalesce(struct net_device *dev,
4053f3ccfda1SYufeng Mo 			     struct ethtool_coalesce *ecmd,
4054f3ccfda1SYufeng Mo 			     struct kernel_ethtool_coalesce *kernel_coal,
4055f3ccfda1SYufeng Mo 			     struct netlink_ext_ack *extack)
4056527a6266SJeff Kirsher {
4057527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4058527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
4059527a6266SJeff Kirsher 
4060527a6266SJeff Kirsher 	if (sky2_read8(hw, STAT_TX_TIMER_CTRL) == TIM_STOP)
4061527a6266SJeff Kirsher 		ecmd->tx_coalesce_usecs = 0;
4062527a6266SJeff Kirsher 	else {
4063527a6266SJeff Kirsher 		u32 clks = sky2_read32(hw, STAT_TX_TIMER_INI);
4064527a6266SJeff Kirsher 		ecmd->tx_coalesce_usecs = sky2_clk2us(hw, clks);
4065527a6266SJeff Kirsher 	}
4066527a6266SJeff Kirsher 	ecmd->tx_max_coalesced_frames = sky2_read16(hw, STAT_TX_IDX_TH);
4067527a6266SJeff Kirsher 
4068527a6266SJeff Kirsher 	if (sky2_read8(hw, STAT_LEV_TIMER_CTRL) == TIM_STOP)
4069527a6266SJeff Kirsher 		ecmd->rx_coalesce_usecs = 0;
4070527a6266SJeff Kirsher 	else {
4071527a6266SJeff Kirsher 		u32 clks = sky2_read32(hw, STAT_LEV_TIMER_INI);
4072527a6266SJeff Kirsher 		ecmd->rx_coalesce_usecs = sky2_clk2us(hw, clks);
4073527a6266SJeff Kirsher 	}
4074527a6266SJeff Kirsher 	ecmd->rx_max_coalesced_frames = sky2_read8(hw, STAT_FIFO_WM);
4075527a6266SJeff Kirsher 
4076527a6266SJeff Kirsher 	if (sky2_read8(hw, STAT_ISR_TIMER_CTRL) == TIM_STOP)
4077527a6266SJeff Kirsher 		ecmd->rx_coalesce_usecs_irq = 0;
4078527a6266SJeff Kirsher 	else {
4079527a6266SJeff Kirsher 		u32 clks = sky2_read32(hw, STAT_ISR_TIMER_INI);
4080527a6266SJeff Kirsher 		ecmd->rx_coalesce_usecs_irq = sky2_clk2us(hw, clks);
4081527a6266SJeff Kirsher 	}
4082527a6266SJeff Kirsher 
4083527a6266SJeff Kirsher 	ecmd->rx_max_coalesced_frames_irq = sky2_read8(hw, STAT_FIFO_ISR_WM);
4084527a6266SJeff Kirsher 
4085527a6266SJeff Kirsher 	return 0;
4086527a6266SJeff Kirsher }
4087527a6266SJeff Kirsher 
4088527a6266SJeff Kirsher /* Note: this affect both ports */
sky2_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ecmd,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)4089527a6266SJeff Kirsher static int sky2_set_coalesce(struct net_device *dev,
4090f3ccfda1SYufeng Mo 			     struct ethtool_coalesce *ecmd,
4091f3ccfda1SYufeng Mo 			     struct kernel_ethtool_coalesce *kernel_coal,
4092f3ccfda1SYufeng Mo 			     struct netlink_ext_ack *extack)
4093527a6266SJeff Kirsher {
4094527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4095527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
4096527a6266SJeff Kirsher 	const u32 tmax = sky2_clk2us(hw, 0x0ffffff);
4097527a6266SJeff Kirsher 
4098527a6266SJeff Kirsher 	if (ecmd->tx_coalesce_usecs > tmax ||
4099527a6266SJeff Kirsher 	    ecmd->rx_coalesce_usecs > tmax ||
4100527a6266SJeff Kirsher 	    ecmd->rx_coalesce_usecs_irq > tmax)
4101527a6266SJeff Kirsher 		return -EINVAL;
4102527a6266SJeff Kirsher 
4103527a6266SJeff Kirsher 	if (ecmd->tx_max_coalesced_frames >= sky2->tx_ring_size-1)
4104527a6266SJeff Kirsher 		return -EINVAL;
4105527a6266SJeff Kirsher 	if (ecmd->rx_max_coalesced_frames > RX_MAX_PENDING)
4106527a6266SJeff Kirsher 		return -EINVAL;
4107527a6266SJeff Kirsher 	if (ecmd->rx_max_coalesced_frames_irq > RX_MAX_PENDING)
4108527a6266SJeff Kirsher 		return -EINVAL;
4109527a6266SJeff Kirsher 
4110527a6266SJeff Kirsher 	if (ecmd->tx_coalesce_usecs == 0)
4111527a6266SJeff Kirsher 		sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_STOP);
4112527a6266SJeff Kirsher 	else {
4113527a6266SJeff Kirsher 		sky2_write32(hw, STAT_TX_TIMER_INI,
4114527a6266SJeff Kirsher 			     sky2_us2clk(hw, ecmd->tx_coalesce_usecs));
4115527a6266SJeff Kirsher 		sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
4116527a6266SJeff Kirsher 	}
4117527a6266SJeff Kirsher 	sky2_write16(hw, STAT_TX_IDX_TH, ecmd->tx_max_coalesced_frames);
4118527a6266SJeff Kirsher 
4119527a6266SJeff Kirsher 	if (ecmd->rx_coalesce_usecs == 0)
4120527a6266SJeff Kirsher 		sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_STOP);
4121527a6266SJeff Kirsher 	else {
4122527a6266SJeff Kirsher 		sky2_write32(hw, STAT_LEV_TIMER_INI,
4123527a6266SJeff Kirsher 			     sky2_us2clk(hw, ecmd->rx_coalesce_usecs));
4124527a6266SJeff Kirsher 		sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
4125527a6266SJeff Kirsher 	}
4126527a6266SJeff Kirsher 	sky2_write8(hw, STAT_FIFO_WM, ecmd->rx_max_coalesced_frames);
4127527a6266SJeff Kirsher 
4128527a6266SJeff Kirsher 	if (ecmd->rx_coalesce_usecs_irq == 0)
4129527a6266SJeff Kirsher 		sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_STOP);
4130527a6266SJeff Kirsher 	else {
4131527a6266SJeff Kirsher 		sky2_write32(hw, STAT_ISR_TIMER_INI,
4132527a6266SJeff Kirsher 			     sky2_us2clk(hw, ecmd->rx_coalesce_usecs_irq));
4133527a6266SJeff Kirsher 		sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
4134527a6266SJeff Kirsher 	}
4135527a6266SJeff Kirsher 	sky2_write8(hw, STAT_FIFO_ISR_WM, ecmd->rx_max_coalesced_frames_irq);
4136527a6266SJeff Kirsher 	return 0;
4137527a6266SJeff Kirsher }
4138527a6266SJeff Kirsher 
4139738a849cSstephen hemminger /*
4140738a849cSstephen hemminger  * Hardware is limited to min of 128 and max of 2048 for ring size
4141738a849cSstephen hemminger  * and  rounded up to next power of two
414265c7bc1bSBhaskar Chowdhury  * to avoid division in modulus calculation
4143738a849cSstephen hemminger  */
roundup_ring_size(unsigned long pending)4144738a849cSstephen hemminger static unsigned long roundup_ring_size(unsigned long pending)
4145738a849cSstephen hemminger {
4146738a849cSstephen hemminger 	return max(128ul, roundup_pow_of_two(pending+1));
4147738a849cSstephen hemminger }
4148738a849cSstephen hemminger 
sky2_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ering,struct kernel_ethtool_ringparam * kernel_ering,struct netlink_ext_ack * extack)4149527a6266SJeff Kirsher static void sky2_get_ringparam(struct net_device *dev,
415074624944SHao Chen 			       struct ethtool_ringparam *ering,
415174624944SHao Chen 			       struct kernel_ethtool_ringparam *kernel_ering,
415274624944SHao Chen 			       struct netlink_ext_ack *extack)
4153527a6266SJeff Kirsher {
4154527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4155527a6266SJeff Kirsher 
4156527a6266SJeff Kirsher 	ering->rx_max_pending = RX_MAX_PENDING;
4157527a6266SJeff Kirsher 	ering->tx_max_pending = TX_MAX_PENDING;
4158527a6266SJeff Kirsher 
4159527a6266SJeff Kirsher 	ering->rx_pending = sky2->rx_pending;
4160527a6266SJeff Kirsher 	ering->tx_pending = sky2->tx_pending;
4161527a6266SJeff Kirsher }
4162527a6266SJeff Kirsher 
sky2_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ering,struct kernel_ethtool_ringparam * kernel_ering,struct netlink_ext_ack * extack)4163527a6266SJeff Kirsher static int sky2_set_ringparam(struct net_device *dev,
416474624944SHao Chen 			      struct ethtool_ringparam *ering,
416574624944SHao Chen 			      struct kernel_ethtool_ringparam *kernel_ering,
416674624944SHao Chen 			      struct netlink_ext_ack *extack)
4167527a6266SJeff Kirsher {
4168527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4169527a6266SJeff Kirsher 
4170527a6266SJeff Kirsher 	if (ering->rx_pending > RX_MAX_PENDING ||
4171527a6266SJeff Kirsher 	    ering->rx_pending < 8 ||
4172527a6266SJeff Kirsher 	    ering->tx_pending < TX_MIN_PENDING ||
4173527a6266SJeff Kirsher 	    ering->tx_pending > TX_MAX_PENDING)
4174527a6266SJeff Kirsher 		return -EINVAL;
4175527a6266SJeff Kirsher 
4176527a6266SJeff Kirsher 	sky2_detach(dev);
4177527a6266SJeff Kirsher 
4178527a6266SJeff Kirsher 	sky2->rx_pending = ering->rx_pending;
4179527a6266SJeff Kirsher 	sky2->tx_pending = ering->tx_pending;
4180738a849cSstephen hemminger 	sky2->tx_ring_size = roundup_ring_size(sky2->tx_pending);
4181527a6266SJeff Kirsher 
4182527a6266SJeff Kirsher 	return sky2_reattach(dev);
4183527a6266SJeff Kirsher }
4184527a6266SJeff Kirsher 
sky2_get_regs_len(struct net_device * dev)4185527a6266SJeff Kirsher static int sky2_get_regs_len(struct net_device *dev)
4186527a6266SJeff Kirsher {
4187527a6266SJeff Kirsher 	return 0x4000;
4188527a6266SJeff Kirsher }
4189527a6266SJeff Kirsher 
sky2_reg_access_ok(struct sky2_hw * hw,unsigned int b)4190527a6266SJeff Kirsher static int sky2_reg_access_ok(struct sky2_hw *hw, unsigned int b)
4191527a6266SJeff Kirsher {
4192527a6266SJeff Kirsher 	/* This complicated switch statement is to make sure and
4193527a6266SJeff Kirsher 	 * only access regions that are unreserved.
4194527a6266SJeff Kirsher 	 * Some blocks are only valid on dual port cards.
4195527a6266SJeff Kirsher 	 */
4196527a6266SJeff Kirsher 	switch (b) {
4197527a6266SJeff Kirsher 	/* second port */
4198527a6266SJeff Kirsher 	case 5:		/* Tx Arbiter 2 */
4199527a6266SJeff Kirsher 	case 9:		/* RX2 */
4200527a6266SJeff Kirsher 	case 14 ... 15:	/* TX2 */
4201527a6266SJeff Kirsher 	case 17: case 19: /* Ram Buffer 2 */
4202527a6266SJeff Kirsher 	case 22 ... 23: /* Tx Ram Buffer 2 */
4203527a6266SJeff Kirsher 	case 25:	/* Rx MAC Fifo 1 */
4204527a6266SJeff Kirsher 	case 27:	/* Tx MAC Fifo 2 */
4205527a6266SJeff Kirsher 	case 31:	/* GPHY 2 */
4206527a6266SJeff Kirsher 	case 40 ... 47: /* Pattern Ram 2 */
4207527a6266SJeff Kirsher 	case 52: case 54: /* TCP Segmentation 2 */
4208527a6266SJeff Kirsher 	case 112 ... 116: /* GMAC 2 */
4209527a6266SJeff Kirsher 		return hw->ports > 1;
4210527a6266SJeff Kirsher 
4211527a6266SJeff Kirsher 	case 0:		/* Control */
4212527a6266SJeff Kirsher 	case 2:		/* Mac address */
4213527a6266SJeff Kirsher 	case 4:		/* Tx Arbiter 1 */
4214527a6266SJeff Kirsher 	case 7:		/* PCI express reg */
4215527a6266SJeff Kirsher 	case 8:		/* RX1 */
4216527a6266SJeff Kirsher 	case 12 ... 13: /* TX1 */
4217527a6266SJeff Kirsher 	case 16: case 18:/* Rx Ram Buffer 1 */
4218527a6266SJeff Kirsher 	case 20 ... 21: /* Tx Ram Buffer 1 */
4219527a6266SJeff Kirsher 	case 24:	/* Rx MAC Fifo 1 */
4220527a6266SJeff Kirsher 	case 26:	/* Tx MAC Fifo 1 */
4221527a6266SJeff Kirsher 	case 28 ... 29: /* Descriptor and status unit */
4222527a6266SJeff Kirsher 	case 30:	/* GPHY 1*/
4223527a6266SJeff Kirsher 	case 32 ... 39: /* Pattern Ram 1 */
4224527a6266SJeff Kirsher 	case 48: case 50: /* TCP Segmentation 1 */
4225527a6266SJeff Kirsher 	case 56 ... 60:	/* PCI space */
4226527a6266SJeff Kirsher 	case 80 ... 84:	/* GMAC 1 */
4227527a6266SJeff Kirsher 		return 1;
4228527a6266SJeff Kirsher 
4229527a6266SJeff Kirsher 	default:
4230527a6266SJeff Kirsher 		return 0;
4231527a6266SJeff Kirsher 	}
4232527a6266SJeff Kirsher }
4233527a6266SJeff Kirsher 
4234527a6266SJeff Kirsher /*
4235527a6266SJeff Kirsher  * Returns copy of control register region
4236527a6266SJeff Kirsher  * Note: ethtool_get_regs always provides full size (16k) buffer
4237527a6266SJeff Kirsher  */
sky2_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * p)4238527a6266SJeff Kirsher static void sky2_get_regs(struct net_device *dev, struct ethtool_regs *regs,
4239527a6266SJeff Kirsher 			  void *p)
4240527a6266SJeff Kirsher {
4241527a6266SJeff Kirsher 	const struct sky2_port *sky2 = netdev_priv(dev);
4242527a6266SJeff Kirsher 	const void __iomem *io = sky2->hw->regs;
4243527a6266SJeff Kirsher 	unsigned int b;
4244527a6266SJeff Kirsher 
4245527a6266SJeff Kirsher 	regs->version = 1;
4246527a6266SJeff Kirsher 
4247527a6266SJeff Kirsher 	for (b = 0; b < 128; b++) {
4248527a6266SJeff Kirsher 		/* skip poisonous diagnostic ram region in block 3 */
4249527a6266SJeff Kirsher 		if (b == 3)
4250527a6266SJeff Kirsher 			memcpy_fromio(p + 0x10, io + 0x10, 128 - 0x10);
4251527a6266SJeff Kirsher 		else if (sky2_reg_access_ok(sky2->hw, b))
4252527a6266SJeff Kirsher 			memcpy_fromio(p, io, 128);
4253527a6266SJeff Kirsher 		else
4254527a6266SJeff Kirsher 			memset(p, 0, 128);
4255527a6266SJeff Kirsher 
4256527a6266SJeff Kirsher 		p += 128;
4257527a6266SJeff Kirsher 		io += 128;
4258527a6266SJeff Kirsher 	}
4259527a6266SJeff Kirsher }
4260527a6266SJeff Kirsher 
sky2_get_eeprom_len(struct net_device * dev)4261527a6266SJeff Kirsher static int sky2_get_eeprom_len(struct net_device *dev)
4262527a6266SJeff Kirsher {
4263527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4264527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
4265527a6266SJeff Kirsher 	u16 reg2;
4266527a6266SJeff Kirsher 
4267527a6266SJeff Kirsher 	reg2 = sky2_pci_read16(hw, PCI_DEV_REG2);
4268527a6266SJeff Kirsher 	return 1 << ( ((reg2 & PCI_VPD_ROM_SZ) >> 14) + 8);
4269527a6266SJeff Kirsher }
4270527a6266SJeff Kirsher 
sky2_get_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)4271527a6266SJeff Kirsher static int sky2_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
4272527a6266SJeff Kirsher 			   u8 *data)
4273527a6266SJeff Kirsher {
4274527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
427592e888bcSHeiner Kallweit 	int rc;
4276527a6266SJeff Kirsher 
4277527a6266SJeff Kirsher 	eeprom->magic = SKY2_EEPROM_MAGIC;
427892e888bcSHeiner Kallweit 	rc = pci_read_vpd_any(sky2->hw->pdev, eeprom->offset, eeprom->len,
427992e888bcSHeiner Kallweit 			      data);
428092e888bcSHeiner Kallweit 	if (rc < 0)
428192e888bcSHeiner Kallweit 		return rc;
4282527a6266SJeff Kirsher 
428392e888bcSHeiner Kallweit 	eeprom->len = rc;
428492e888bcSHeiner Kallweit 
428592e888bcSHeiner Kallweit 	return 0;
4286527a6266SJeff Kirsher }
4287527a6266SJeff Kirsher 
sky2_set_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)4288527a6266SJeff Kirsher static int sky2_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
4289527a6266SJeff Kirsher 			   u8 *data)
4290527a6266SJeff Kirsher {
4291527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
429292e888bcSHeiner Kallweit 	int rc;
4293527a6266SJeff Kirsher 
4294527a6266SJeff Kirsher 	if (eeprom->magic != SKY2_EEPROM_MAGIC)
4295527a6266SJeff Kirsher 		return -EINVAL;
4296527a6266SJeff Kirsher 
429792e888bcSHeiner Kallweit 	rc = pci_write_vpd_any(sky2->hw->pdev, eeprom->offset, eeprom->len,
429892e888bcSHeiner Kallweit 			       data);
4299527a6266SJeff Kirsher 
430092e888bcSHeiner Kallweit 	return rc < 0 ? rc : 0;
4301527a6266SJeff Kirsher }
4302527a6266SJeff Kirsher 
sky2_fix_features(struct net_device * dev,netdev_features_t features)4303c8f44affSMichał Mirosław static netdev_features_t sky2_fix_features(struct net_device *dev,
4304c8f44affSMichał Mirosław 	netdev_features_t features)
4305527a6266SJeff Kirsher {
4306527a6266SJeff Kirsher 	const struct sky2_port *sky2 = netdev_priv(dev);
4307527a6266SJeff Kirsher 	const struct sky2_hw *hw = sky2->hw;
4308527a6266SJeff Kirsher 
4309527a6266SJeff Kirsher 	/* In order to do Jumbo packets on these chips, need to turn off the
4310527a6266SJeff Kirsher 	 * transmit store/forward. Therefore checksum offload won't work.
4311527a6266SJeff Kirsher 	 */
4312527a6266SJeff Kirsher 	if (dev->mtu > ETH_DATA_LEN && hw->chip_id == CHIP_ID_YUKON_EC_U) {
4313527a6266SJeff Kirsher 		netdev_info(dev, "checksum offload not possible with jumbo frames\n");
4314a188222bSTom Herbert 		features &= ~(NETIF_F_TSO | NETIF_F_SG | NETIF_F_CSUM_MASK);
4315527a6266SJeff Kirsher 	}
4316527a6266SJeff Kirsher 
4317527a6266SJeff Kirsher 	/* Some hardware requires receive checksum for RSS to work. */
4318527a6266SJeff Kirsher 	if ( (features & NETIF_F_RXHASH) &&
4319527a6266SJeff Kirsher 	     !(features & NETIF_F_RXCSUM) &&
4320527a6266SJeff Kirsher 	     (sky2->hw->flags & SKY2_HW_RSS_CHKSUM)) {
4321527a6266SJeff Kirsher 		netdev_info(dev, "receive hashing forces receive checksum\n");
4322527a6266SJeff Kirsher 		features |= NETIF_F_RXCSUM;
4323527a6266SJeff Kirsher 	}
4324527a6266SJeff Kirsher 
4325527a6266SJeff Kirsher 	return features;
4326527a6266SJeff Kirsher }
4327527a6266SJeff Kirsher 
sky2_set_features(struct net_device * dev,netdev_features_t features)4328c8f44affSMichał Mirosław static int sky2_set_features(struct net_device *dev, netdev_features_t features)
4329527a6266SJeff Kirsher {
4330527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4331c8f44affSMichał Mirosław 	netdev_features_t changed = dev->features ^ features;
4332527a6266SJeff Kirsher 
43335ff0feacSstephen hemminger 	if ((changed & NETIF_F_RXCSUM) &&
43345ff0feacSstephen hemminger 	    !(sky2->hw->flags & SKY2_HW_NEW_LE)) {
43355ff0feacSstephen hemminger 		sky2_write32(sky2->hw,
43365ff0feacSstephen hemminger 			     Q_ADDR(rxqaddr[sky2->port], Q_CSR),
43375ff0feacSstephen hemminger 			     (features & NETIF_F_RXCSUM)
43385ff0feacSstephen hemminger 			     ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
4339527a6266SJeff Kirsher 	}
4340527a6266SJeff Kirsher 
4341527a6266SJeff Kirsher 	if (changed & NETIF_F_RXHASH)
4342527a6266SJeff Kirsher 		rx_set_rss(dev, features);
4343527a6266SJeff Kirsher 
4344f646968fSPatrick McHardy 	if (changed & (NETIF_F_HW_VLAN_CTAG_TX|NETIF_F_HW_VLAN_CTAG_RX))
4345527a6266SJeff Kirsher 		sky2_vlan_mode(dev, features);
4346527a6266SJeff Kirsher 
4347527a6266SJeff Kirsher 	return 0;
4348527a6266SJeff Kirsher }
4349527a6266SJeff Kirsher 
4350527a6266SJeff Kirsher static const struct ethtool_ops sky2_ethtool_ops = {
4351a1edda36SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4352a1edda36SJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES |
4353a1edda36SJakub Kicinski 				     ETHTOOL_COALESCE_RX_USECS_IRQ |
4354a1edda36SJakub Kicinski 				     ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ,
4355527a6266SJeff Kirsher 	.get_drvinfo	= sky2_get_drvinfo,
4356527a6266SJeff Kirsher 	.get_wol	= sky2_get_wol,
4357527a6266SJeff Kirsher 	.set_wol	= sky2_set_wol,
4358527a6266SJeff Kirsher 	.get_msglevel	= sky2_get_msglevel,
4359527a6266SJeff Kirsher 	.set_msglevel	= sky2_set_msglevel,
4360527a6266SJeff Kirsher 	.nway_reset	= sky2_nway_reset,
4361527a6266SJeff Kirsher 	.get_regs_len	= sky2_get_regs_len,
4362527a6266SJeff Kirsher 	.get_regs	= sky2_get_regs,
4363527a6266SJeff Kirsher 	.get_link	= ethtool_op_get_link,
4364527a6266SJeff Kirsher 	.get_eeprom_len	= sky2_get_eeprom_len,
4365527a6266SJeff Kirsher 	.get_eeprom	= sky2_get_eeprom,
4366527a6266SJeff Kirsher 	.set_eeprom	= sky2_set_eeprom,
4367527a6266SJeff Kirsher 	.get_strings	= sky2_get_strings,
4368527a6266SJeff Kirsher 	.get_coalesce	= sky2_get_coalesce,
4369527a6266SJeff Kirsher 	.set_coalesce	= sky2_set_coalesce,
4370527a6266SJeff Kirsher 	.get_ringparam	= sky2_get_ringparam,
4371527a6266SJeff Kirsher 	.set_ringparam	= sky2_set_ringparam,
4372527a6266SJeff Kirsher 	.get_pauseparam = sky2_get_pauseparam,
4373527a6266SJeff Kirsher 	.set_pauseparam = sky2_set_pauseparam,
4374527a6266SJeff Kirsher 	.set_phys_id	= sky2_set_phys_id,
4375527a6266SJeff Kirsher 	.get_sset_count = sky2_get_sset_count,
4376527a6266SJeff Kirsher 	.get_ethtool_stats = sky2_get_ethtool_stats,
437755f78fcdSPhilippe Reynes 	.get_link_ksettings = sky2_get_link_ksettings,
437855f78fcdSPhilippe Reynes 	.set_link_ksettings = sky2_set_link_ksettings,
4379527a6266SJeff Kirsher };
4380527a6266SJeff Kirsher 
4381527a6266SJeff Kirsher #ifdef CONFIG_SKY2_DEBUG
4382527a6266SJeff Kirsher 
4383527a6266SJeff Kirsher static struct dentry *sky2_debug;
4384527a6266SJeff Kirsher 
sky2_debug_show(struct seq_file * seq,void * v)4385527a6266SJeff Kirsher static int sky2_debug_show(struct seq_file *seq, void *v)
4386527a6266SJeff Kirsher {
4387527a6266SJeff Kirsher 	struct net_device *dev = seq->private;
4388527a6266SJeff Kirsher 	const struct sky2_port *sky2 = netdev_priv(dev);
4389527a6266SJeff Kirsher 	struct sky2_hw *hw = sky2->hw;
4390527a6266SJeff Kirsher 	unsigned port = sky2->port;
4391527a6266SJeff Kirsher 	unsigned idx, last;
4392527a6266SJeff Kirsher 	int sop;
4393527a6266SJeff Kirsher 
43940efcc3f2SHeiner Kallweit 	seq_printf(seq, "IRQ src=%x mask=%x control=%x\n",
4395527a6266SJeff Kirsher 		   sky2_read32(hw, B0_ISRC),
4396527a6266SJeff Kirsher 		   sky2_read32(hw, B0_IMSK),
4397527a6266SJeff Kirsher 		   sky2_read32(hw, B0_Y2_SP_ICR));
4398527a6266SJeff Kirsher 
4399527a6266SJeff Kirsher 	if (!netif_running(dev)) {
4400a0c51cf1SMarkus Elfring 		seq_puts(seq, "network not running\n");
4401527a6266SJeff Kirsher 		return 0;
4402527a6266SJeff Kirsher 	}
4403527a6266SJeff Kirsher 
4404527a6266SJeff Kirsher 	napi_disable(&hw->napi);
4405527a6266SJeff Kirsher 	last = sky2_read16(hw, STAT_PUT_IDX);
4406527a6266SJeff Kirsher 
4407527a6266SJeff Kirsher 	seq_printf(seq, "Status ring %u\n", hw->st_size);
4408527a6266SJeff Kirsher 	if (hw->st_idx == last)
4409527a6266SJeff Kirsher 		seq_puts(seq, "Status ring (empty)\n");
4410527a6266SJeff Kirsher 	else {
4411527a6266SJeff Kirsher 		seq_puts(seq, "Status ring\n");
4412527a6266SJeff Kirsher 		for (idx = hw->st_idx; idx != last && idx < hw->st_size;
4413527a6266SJeff Kirsher 		     idx = RING_NEXT(idx, hw->st_size)) {
4414527a6266SJeff Kirsher 			const struct sky2_status_le *le = hw->st_le + idx;
4415527a6266SJeff Kirsher 			seq_printf(seq, "[%d] %#x %d %#x\n",
4416527a6266SJeff Kirsher 				   idx, le->opcode, le->length, le->status);
4417527a6266SJeff Kirsher 		}
4418527a6266SJeff Kirsher 		seq_puts(seq, "\n");
4419527a6266SJeff Kirsher 	}
4420527a6266SJeff Kirsher 
4421527a6266SJeff Kirsher 	seq_printf(seq, "Tx ring pending=%u...%u report=%d done=%d\n",
4422527a6266SJeff Kirsher 		   sky2->tx_cons, sky2->tx_prod,
4423527a6266SJeff Kirsher 		   sky2_read16(hw, port == 0 ? STAT_TXA1_RIDX : STAT_TXA2_RIDX),
4424527a6266SJeff Kirsher 		   sky2_read16(hw, Q_ADDR(txqaddr[port], Q_DONE)));
4425527a6266SJeff Kirsher 
4426527a6266SJeff Kirsher 	/* Dump contents of tx ring */
4427527a6266SJeff Kirsher 	sop = 1;
4428527a6266SJeff Kirsher 	for (idx = sky2->tx_next; idx != sky2->tx_prod && idx < sky2->tx_ring_size;
4429527a6266SJeff Kirsher 	     idx = RING_NEXT(idx, sky2->tx_ring_size)) {
4430527a6266SJeff Kirsher 		const struct sky2_tx_le *le = sky2->tx_le + idx;
4431527a6266SJeff Kirsher 		u32 a = le32_to_cpu(le->addr);
4432527a6266SJeff Kirsher 
4433527a6266SJeff Kirsher 		if (sop)
4434527a6266SJeff Kirsher 			seq_printf(seq, "%u:", idx);
4435527a6266SJeff Kirsher 		sop = 0;
4436527a6266SJeff Kirsher 
4437527a6266SJeff Kirsher 		switch (le->opcode & ~HW_OWNER) {
4438527a6266SJeff Kirsher 		case OP_ADDR64:
4439527a6266SJeff Kirsher 			seq_printf(seq, " %#x:", a);
4440527a6266SJeff Kirsher 			break;
4441527a6266SJeff Kirsher 		case OP_LRGLEN:
4442527a6266SJeff Kirsher 			seq_printf(seq, " mtu=%d", a);
4443527a6266SJeff Kirsher 			break;
4444527a6266SJeff Kirsher 		case OP_VLAN:
4445527a6266SJeff Kirsher 			seq_printf(seq, " vlan=%d", be16_to_cpu(le->length));
4446527a6266SJeff Kirsher 			break;
4447527a6266SJeff Kirsher 		case OP_TCPLISW:
4448527a6266SJeff Kirsher 			seq_printf(seq, " csum=%#x", a);
4449527a6266SJeff Kirsher 			break;
4450527a6266SJeff Kirsher 		case OP_LARGESEND:
4451527a6266SJeff Kirsher 			seq_printf(seq, " tso=%#x(%d)", a, le16_to_cpu(le->length));
4452527a6266SJeff Kirsher 			break;
4453527a6266SJeff Kirsher 		case OP_PACKET:
4454527a6266SJeff Kirsher 			seq_printf(seq, " %#x(%d)", a, le16_to_cpu(le->length));
4455527a6266SJeff Kirsher 			break;
4456527a6266SJeff Kirsher 		case OP_BUFFER:
4457527a6266SJeff Kirsher 			seq_printf(seq, " frag=%#x(%d)", a, le16_to_cpu(le->length));
4458527a6266SJeff Kirsher 			break;
4459527a6266SJeff Kirsher 		default:
4460527a6266SJeff Kirsher 			seq_printf(seq, " op=%#x,%#x(%d)", le->opcode,
4461527a6266SJeff Kirsher 				   a, le16_to_cpu(le->length));
4462527a6266SJeff Kirsher 		}
4463527a6266SJeff Kirsher 
4464527a6266SJeff Kirsher 		if (le->ctrl & EOP) {
4465527a6266SJeff Kirsher 			seq_putc(seq, '\n');
4466527a6266SJeff Kirsher 			sop = 1;
4467527a6266SJeff Kirsher 		}
4468527a6266SJeff Kirsher 	}
4469527a6266SJeff Kirsher 
4470527a6266SJeff Kirsher 	seq_printf(seq, "\nRx ring hw get=%d put=%d last=%d\n",
4471527a6266SJeff Kirsher 		   sky2_read16(hw, Y2_QADDR(rxqaddr[port], PREF_UNIT_GET_IDX)),
4472527a6266SJeff Kirsher 		   sky2_read16(hw, Y2_QADDR(rxqaddr[port], PREF_UNIT_PUT_IDX)),
4473527a6266SJeff Kirsher 		   sky2_read16(hw, Y2_QADDR(rxqaddr[port], PREF_UNIT_LAST_IDX)));
4474527a6266SJeff Kirsher 
4475527a6266SJeff Kirsher 	sky2_read32(hw, B0_Y2_SP_LISR);
4476527a6266SJeff Kirsher 	napi_enable(&hw->napi);
4477527a6266SJeff Kirsher 	return 0;
4478527a6266SJeff Kirsher }
4479d9bbd6a1SYangtao Li DEFINE_SHOW_ATTRIBUTE(sky2_debug);
4480527a6266SJeff Kirsher 
4481527a6266SJeff Kirsher /*
4482527a6266SJeff Kirsher  * Use network device events to create/remove/rename
4483527a6266SJeff Kirsher  * debugfs file entries
4484527a6266SJeff Kirsher  */
sky2_device_event(struct notifier_block * unused,unsigned long event,void * ptr)4485527a6266SJeff Kirsher static int sky2_device_event(struct notifier_block *unused,
4486527a6266SJeff Kirsher 			     unsigned long event, void *ptr)
4487527a6266SJeff Kirsher {
4488351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4489527a6266SJeff Kirsher 	struct sky2_port *sky2 = netdev_priv(dev);
4490527a6266SJeff Kirsher 
4491926d0977Sstephen hemminger 	if (dev->netdev_ops->ndo_open != sky2_open || !sky2_debug)
4492527a6266SJeff Kirsher 		return NOTIFY_DONE;
4493527a6266SJeff Kirsher 
4494527a6266SJeff Kirsher 	switch (event) {
4495527a6266SJeff Kirsher 	case NETDEV_CHANGENAME:
4496527a6266SJeff Kirsher 		if (sky2->debugfs) {
4497527a6266SJeff Kirsher 			sky2->debugfs = debugfs_rename(sky2_debug, sky2->debugfs,
4498527a6266SJeff Kirsher 						       sky2_debug, dev->name);
4499527a6266SJeff Kirsher 		}
4500527a6266SJeff Kirsher 		break;
4501527a6266SJeff Kirsher 
4502527a6266SJeff Kirsher 	case NETDEV_GOING_DOWN:
4503527a6266SJeff Kirsher 		if (sky2->debugfs) {
4504527a6266SJeff Kirsher 			netdev_printk(KERN_DEBUG, dev, "remove debugfs\n");
4505527a6266SJeff Kirsher 			debugfs_remove(sky2->debugfs);
4506527a6266SJeff Kirsher 			sky2->debugfs = NULL;
4507527a6266SJeff Kirsher 		}
4508527a6266SJeff Kirsher 		break;
4509527a6266SJeff Kirsher 
4510527a6266SJeff Kirsher 	case NETDEV_UP:
4511d3757ba4SJoe Perches 		sky2->debugfs = debugfs_create_file(dev->name, 0444,
4512527a6266SJeff Kirsher 						    sky2_debug, dev,
4513527a6266SJeff Kirsher 						    &sky2_debug_fops);
4514527a6266SJeff Kirsher 		if (IS_ERR(sky2->debugfs))
4515527a6266SJeff Kirsher 			sky2->debugfs = NULL;
4516527a6266SJeff Kirsher 	}
4517527a6266SJeff Kirsher 
4518527a6266SJeff Kirsher 	return NOTIFY_DONE;
4519527a6266SJeff Kirsher }
4520527a6266SJeff Kirsher 
4521527a6266SJeff Kirsher static struct notifier_block sky2_notifier = {
4522527a6266SJeff Kirsher 	.notifier_call = sky2_device_event,
4523527a6266SJeff Kirsher };
4524527a6266SJeff Kirsher 
4525527a6266SJeff Kirsher 
sky2_debug_init(void)4526527a6266SJeff Kirsher static __init void sky2_debug_init(void)
4527527a6266SJeff Kirsher {
4528527a6266SJeff Kirsher 	struct dentry *ent;
4529527a6266SJeff Kirsher 
4530527a6266SJeff Kirsher 	ent = debugfs_create_dir("sky2", NULL);
4531*ee09e9deSRuan Jinjie 	if (IS_ERR(ent))
4532527a6266SJeff Kirsher 		return;
4533527a6266SJeff Kirsher 
4534527a6266SJeff Kirsher 	sky2_debug = ent;
4535527a6266SJeff Kirsher 	register_netdevice_notifier(&sky2_notifier);
4536527a6266SJeff Kirsher }
4537527a6266SJeff Kirsher 
sky2_debug_cleanup(void)4538527a6266SJeff Kirsher static __exit void sky2_debug_cleanup(void)
4539527a6266SJeff Kirsher {
4540527a6266SJeff Kirsher 	if (sky2_debug) {
4541527a6266SJeff Kirsher 		unregister_netdevice_notifier(&sky2_notifier);
4542527a6266SJeff Kirsher 		debugfs_remove(sky2_debug);
4543527a6266SJeff Kirsher 		sky2_debug = NULL;
4544527a6266SJeff Kirsher 	}
4545527a6266SJeff Kirsher }
4546527a6266SJeff Kirsher 
4547527a6266SJeff Kirsher #else
4548527a6266SJeff Kirsher #define sky2_debug_init()
4549527a6266SJeff Kirsher #define sky2_debug_cleanup()
4550527a6266SJeff Kirsher #endif
4551527a6266SJeff Kirsher 
4552527a6266SJeff Kirsher /* Two copies of network device operations to handle special case of
4553df4a17a9SYangyang Li  * not allowing netpoll on second port
4554df4a17a9SYangyang Li  */
4555527a6266SJeff Kirsher static const struct net_device_ops sky2_netdev_ops[2] = {
4556527a6266SJeff Kirsher   {
4557926d0977Sstephen hemminger 	.ndo_open		= sky2_open,
4558926d0977Sstephen hemminger 	.ndo_stop		= sky2_close,
4559527a6266SJeff Kirsher 	.ndo_start_xmit		= sky2_xmit_frame,
4560a7605370SArnd Bergmann 	.ndo_eth_ioctl		= sky2_ioctl,
4561527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
4562527a6266SJeff Kirsher 	.ndo_set_mac_address	= sky2_set_mac_address,
4563afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= sky2_set_multicast,
4564527a6266SJeff Kirsher 	.ndo_change_mtu		= sky2_change_mtu,
4565527a6266SJeff Kirsher 	.ndo_fix_features	= sky2_fix_features,
4566527a6266SJeff Kirsher 	.ndo_set_features	= sky2_set_features,
4567527a6266SJeff Kirsher 	.ndo_tx_timeout		= sky2_tx_timeout,
4568527a6266SJeff Kirsher 	.ndo_get_stats64	= sky2_get_stats,
4569527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
4570527a6266SJeff Kirsher 	.ndo_poll_controller	= sky2_netpoll,
4571527a6266SJeff Kirsher #endif
4572527a6266SJeff Kirsher   },
4573527a6266SJeff Kirsher   {
4574926d0977Sstephen hemminger 	.ndo_open		= sky2_open,
4575926d0977Sstephen hemminger 	.ndo_stop		= sky2_close,
4576527a6266SJeff Kirsher 	.ndo_start_xmit		= sky2_xmit_frame,
4577a7605370SArnd Bergmann 	.ndo_eth_ioctl		= sky2_ioctl,
4578527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
4579527a6266SJeff Kirsher 	.ndo_set_mac_address	= sky2_set_mac_address,
4580afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= sky2_set_multicast,
4581527a6266SJeff Kirsher 	.ndo_change_mtu		= sky2_change_mtu,
4582527a6266SJeff Kirsher 	.ndo_fix_features	= sky2_fix_features,
4583527a6266SJeff Kirsher 	.ndo_set_features	= sky2_set_features,
4584527a6266SJeff Kirsher 	.ndo_tx_timeout		= sky2_tx_timeout,
4585527a6266SJeff Kirsher 	.ndo_get_stats64	= sky2_get_stats,
4586527a6266SJeff Kirsher   },
4587527a6266SJeff Kirsher };
4588527a6266SJeff Kirsher 
4589527a6266SJeff Kirsher /* Initialize network device */
sky2_init_netdev(struct sky2_hw * hw,unsigned port,int highmem,int wol)45901dd06ae8SGreg Kroah-Hartman static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
4591527a6266SJeff Kirsher 					   int highmem, int wol)
4592527a6266SJeff Kirsher {
4593527a6266SJeff Kirsher 	struct sky2_port *sky2;
4594527a6266SJeff Kirsher 	struct net_device *dev = alloc_etherdev(sizeof(*sky2));
459583216e39SMichael Walle 	int ret;
4596527a6266SJeff Kirsher 
459741de8d4cSJoe Perches 	if (!dev)
4598527a6266SJeff Kirsher 		return NULL;
4599527a6266SJeff Kirsher 
4600527a6266SJeff Kirsher 	SET_NETDEV_DEV(dev, &hw->pdev->dev);
4601527a6266SJeff Kirsher 	dev->irq = hw->pdev->irq;
46027ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &sky2_ethtool_ops;
4603527a6266SJeff Kirsher 	dev->watchdog_timeo = TX_WATCHDOG;
4604527a6266SJeff Kirsher 	dev->netdev_ops = &sky2_netdev_ops[port];
4605527a6266SJeff Kirsher 
4606527a6266SJeff Kirsher 	sky2 = netdev_priv(dev);
4607527a6266SJeff Kirsher 	sky2->netdev = dev;
4608527a6266SJeff Kirsher 	sky2->hw = hw;
4609527a6266SJeff Kirsher 	sky2->msg_enable = netif_msg_init(debug, default_msg);
4610527a6266SJeff Kirsher 
4611827da44cSJohn Stultz 	u64_stats_init(&sky2->tx_stats.syncp);
4612827da44cSJohn Stultz 	u64_stats_init(&sky2->rx_stats.syncp);
4613827da44cSJohn Stultz 
4614527a6266SJeff Kirsher 	/* Auto speed and flow control */
4615527a6266SJeff Kirsher 	sky2->flags = SKY2_FLAG_AUTO_SPEED | SKY2_FLAG_AUTO_PAUSE;
4616527a6266SJeff Kirsher 	if (hw->chip_id != CHIP_ID_YUKON_XL)
4617527a6266SJeff Kirsher 		dev->hw_features |= NETIF_F_RXCSUM;
4618527a6266SJeff Kirsher 
4619527a6266SJeff Kirsher 	sky2->flow_mode = FC_BOTH;
4620527a6266SJeff Kirsher 
4621527a6266SJeff Kirsher 	sky2->duplex = -1;
4622527a6266SJeff Kirsher 	sky2->speed = -1;
4623527a6266SJeff Kirsher 	sky2->advertising = sky2_supported_modes(hw);
4624527a6266SJeff Kirsher 	sky2->wol = wol;
4625527a6266SJeff Kirsher 
4626527a6266SJeff Kirsher 	spin_lock_init(&sky2->phy_lock);
4627527a6266SJeff Kirsher 
4628527a6266SJeff Kirsher 	sky2->tx_pending = TX_DEF_PENDING;
4629738a849cSstephen hemminger 	sky2->tx_ring_size = roundup_ring_size(TX_DEF_PENDING);
4630527a6266SJeff Kirsher 	sky2->rx_pending = RX_DEF_PENDING;
4631527a6266SJeff Kirsher 
4632527a6266SJeff Kirsher 	hw->dev[port] = dev;
4633527a6266SJeff Kirsher 
4634527a6266SJeff Kirsher 	sky2->port = port;
4635527a6266SJeff Kirsher 
4636527a6266SJeff Kirsher 	dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO;
4637527a6266SJeff Kirsher 
4638527a6266SJeff Kirsher 	if (highmem)
4639527a6266SJeff Kirsher 		dev->features |= NETIF_F_HIGHDMA;
4640527a6266SJeff Kirsher 
4641527a6266SJeff Kirsher 	/* Enable receive hashing unless hardware is known broken */
4642527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_RSS_BROKEN))
4643527a6266SJeff Kirsher 		dev->hw_features |= NETIF_F_RXHASH;
4644527a6266SJeff Kirsher 
4645527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_VLAN_BROKEN)) {
4646f646968fSPatrick McHardy 		dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
4647f646968fSPatrick McHardy 				    NETIF_F_HW_VLAN_CTAG_RX;
4648527a6266SJeff Kirsher 		dev->vlan_features |= SKY2_VLAN_OFFLOADS;
4649527a6266SJeff Kirsher 	}
4650527a6266SJeff Kirsher 
4651527a6266SJeff Kirsher 	dev->features |= dev->hw_features;
4652527a6266SJeff Kirsher 
46535777987eSJarod Wilson 	/* MTU range: 60 - 1500 or 9000 */
46545777987eSJarod Wilson 	dev->min_mtu = ETH_ZLEN;
46555777987eSJarod Wilson 	if (hw->chip_id == CHIP_ID_YUKON_FE ||
46565777987eSJarod Wilson 	    hw->chip_id == CHIP_ID_YUKON_FE_P)
46575777987eSJarod Wilson 		dev->max_mtu = ETH_DATA_LEN;
46585777987eSJarod Wilson 	else
46595777987eSJarod Wilson 		dev->max_mtu = ETH_JUMBO_MTU;
46605777987eSJarod Wilson 
46613ee2f8ceSTim Harvey 	/* try to get mac address in the following order:
46623ee2f8ceSTim Harvey 	 * 1) from device tree data
46633ee2f8ceSTim Harvey 	 * 2) from internal registers set by bootloader
46643ee2f8ceSTim Harvey 	 */
46659ca01b25SJakub Kicinski 	ret = of_get_ethdev_address(hw->pdev->dev.of_node, dev);
46664789b57aSJakub Kicinski 	if (ret) {
46674789b57aSJakub Kicinski 		u8 addr[ETH_ALEN];
46684789b57aSJakub Kicinski 
46694789b57aSJakub Kicinski 		memcpy_fromio(addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
46704789b57aSJakub Kicinski 		eth_hw_addr_set(dev, addr);
46714789b57aSJakub Kicinski 	}
4672527a6266SJeff Kirsher 
46730f50c10dSLiviu Dudau 	/* if the address is invalid, use a random value */
46740f50c10dSLiviu Dudau 	if (!is_valid_ether_addr(dev->dev_addr)) {
46750f50c10dSLiviu Dudau 		struct sockaddr sa = { AF_UNSPEC };
46760f50c10dSLiviu Dudau 
467718755e27SKrzysztof Halasa 		dev_warn(&hw->pdev->dev, "Invalid MAC address, defaulting to random\n");
46780f50c10dSLiviu Dudau 		eth_hw_addr_random(dev);
46790f50c10dSLiviu Dudau 		memcpy(sa.sa_data, dev->dev_addr, ETH_ALEN);
46800f50c10dSLiviu Dudau 		if (sky2_set_mac_address(dev, &sa))
468118755e27SKrzysztof Halasa 			dev_warn(&hw->pdev->dev, "Failed to set MAC address.\n");
46820f50c10dSLiviu Dudau 	}
46830f50c10dSLiviu Dudau 
4684527a6266SJeff Kirsher 	return dev;
4685527a6266SJeff Kirsher }
4686527a6266SJeff Kirsher 
sky2_show_addr(struct net_device * dev)4687853e3f4cSBill Pemberton static void sky2_show_addr(struct net_device *dev)
4688527a6266SJeff Kirsher {
4689527a6266SJeff Kirsher 	const struct sky2_port *sky2 = netdev_priv(dev);
4690527a6266SJeff Kirsher 
4691527a6266SJeff Kirsher 	netif_info(sky2, probe, dev, "addr %pM\n", dev->dev_addr);
4692527a6266SJeff Kirsher }
4693527a6266SJeff Kirsher 
4694527a6266SJeff Kirsher /* Handle software interrupt used during MSI test */
sky2_test_intr(int irq,void * dev_id)4695853e3f4cSBill Pemberton static irqreturn_t sky2_test_intr(int irq, void *dev_id)
4696527a6266SJeff Kirsher {
4697527a6266SJeff Kirsher 	struct sky2_hw *hw = dev_id;
4698527a6266SJeff Kirsher 	u32 status = sky2_read32(hw, B0_Y2_SP_ISRC2);
4699527a6266SJeff Kirsher 
4700527a6266SJeff Kirsher 	if (status == 0)
4701527a6266SJeff Kirsher 		return IRQ_NONE;
4702527a6266SJeff Kirsher 
4703527a6266SJeff Kirsher 	if (status & Y2_IS_IRQ_SW) {
4704527a6266SJeff Kirsher 		hw->flags |= SKY2_HW_USE_MSI;
4705527a6266SJeff Kirsher 		wake_up(&hw->msi_wait);
4706527a6266SJeff Kirsher 		sky2_write8(hw, B0_CTST, CS_CL_SW_IRQ);
4707527a6266SJeff Kirsher 	}
4708527a6266SJeff Kirsher 	sky2_write32(hw, B0_Y2_SP_ICR, 2);
4709527a6266SJeff Kirsher 
4710527a6266SJeff Kirsher 	return IRQ_HANDLED;
4711527a6266SJeff Kirsher }
4712527a6266SJeff Kirsher 
47131c3997b1SJilin Yuan /* Test interrupt path by forcing a software IRQ */
sky2_test_msi(struct sky2_hw * hw)4714853e3f4cSBill Pemberton static int sky2_test_msi(struct sky2_hw *hw)
4715527a6266SJeff Kirsher {
4716527a6266SJeff Kirsher 	struct pci_dev *pdev = hw->pdev;
4717527a6266SJeff Kirsher 	int err;
4718527a6266SJeff Kirsher 
4719527a6266SJeff Kirsher 	init_waitqueue_head(&hw->msi_wait);
4720527a6266SJeff Kirsher 
4721527a6266SJeff Kirsher 	err = request_irq(pdev->irq, sky2_test_intr, 0, DRV_NAME, hw);
4722527a6266SJeff Kirsher 	if (err) {
4723527a6266SJeff Kirsher 		dev_err(&pdev->dev, "cannot assign irq %d\n", pdev->irq);
4724527a6266SJeff Kirsher 		return err;
4725527a6266SJeff Kirsher 	}
4726527a6266SJeff Kirsher 
4727ede7193dSLino Sanfilippo 	sky2_write32(hw, B0_IMSK, Y2_IS_IRQ_SW);
4728ede7193dSLino Sanfilippo 
4729527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_ST_SW_IRQ);
4730527a6266SJeff Kirsher 	sky2_read8(hw, B0_CTST);
4731527a6266SJeff Kirsher 
4732527a6266SJeff Kirsher 	wait_event_timeout(hw->msi_wait, (hw->flags & SKY2_HW_USE_MSI), HZ/10);
4733527a6266SJeff Kirsher 
4734527a6266SJeff Kirsher 	if (!(hw->flags & SKY2_HW_USE_MSI)) {
4735527a6266SJeff Kirsher 		/* MSI test failed, go back to INTx mode */
4736527a6266SJeff Kirsher 		dev_info(&pdev->dev, "No interrupt generated using MSI, "
4737527a6266SJeff Kirsher 			 "switching to INTx mode.\n");
4738527a6266SJeff Kirsher 
4739527a6266SJeff Kirsher 		err = -EOPNOTSUPP;
4740527a6266SJeff Kirsher 		sky2_write8(hw, B0_CTST, CS_CL_SW_IRQ);
4741527a6266SJeff Kirsher 	}
4742527a6266SJeff Kirsher 
4743527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, 0);
4744527a6266SJeff Kirsher 	sky2_read32(hw, B0_IMSK);
4745527a6266SJeff Kirsher 
4746527a6266SJeff Kirsher 	free_irq(pdev->irq, hw);
4747527a6266SJeff Kirsher 
4748527a6266SJeff Kirsher 	return err;
4749527a6266SJeff Kirsher }
4750527a6266SJeff Kirsher 
4751527a6266SJeff Kirsher /* This driver supports yukon2 chipset only */
sky2_name(u8 chipid,char * buf,int sz)4752527a6266SJeff Kirsher static const char *sky2_name(u8 chipid, char *buf, int sz)
4753527a6266SJeff Kirsher {
4754628fe1ceSColin Ian King 	static const char *const name[] = {
4755527a6266SJeff Kirsher 		"XL",		/* 0xb3 */
4756527a6266SJeff Kirsher 		"EC Ultra", 	/* 0xb4 */
4757527a6266SJeff Kirsher 		"Extreme",	/* 0xb5 */
4758527a6266SJeff Kirsher 		"EC",		/* 0xb6 */
4759527a6266SJeff Kirsher 		"FE",		/* 0xb7 */
4760527a6266SJeff Kirsher 		"FE+",		/* 0xb8 */
4761527a6266SJeff Kirsher 		"Supreme",	/* 0xb9 */
4762527a6266SJeff Kirsher 		"UL 2",		/* 0xba */
4763527a6266SJeff Kirsher 		"Unknown",	/* 0xbb */
4764527a6266SJeff Kirsher 		"Optima",	/* 0xbc */
47650e767324SMirko Lindner 		"OptimaEEE",    /* 0xbd */
4766527a6266SJeff Kirsher 		"Optima 2",	/* 0xbe */
4767527a6266SJeff Kirsher 	};
4768527a6266SJeff Kirsher 
4769527a6266SJeff Kirsher 	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
47700575beddSAndrew Lunn 		snprintf(buf, sz, "%s", name[chipid - CHIP_ID_YUKON_XL]);
4771527a6266SJeff Kirsher 	else
4772527a6266SJeff Kirsher 		snprintf(buf, sz, "(chip %#x)", chipid);
4773527a6266SJeff Kirsher 	return buf;
4774527a6266SJeff Kirsher }
4775527a6266SJeff Kirsher 
4776b33b7cd6SKai-Heng Feng static const struct dmi_system_id msi_blacklist[] = {
4777b33b7cd6SKai-Heng Feng 	{
4778b33b7cd6SKai-Heng Feng 		.ident = "Dell Inspiron 1545",
4779b33b7cd6SKai-Heng Feng 		.matches = {
4780b33b7cd6SKai-Heng Feng 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4781b33b7cd6SKai-Heng Feng 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1545"),
4782b33b7cd6SKai-Heng Feng 		},
4783b33b7cd6SKai-Heng Feng 	},
4784b33b7cd6SKai-Heng Feng 	{
4785b33b7cd6SKai-Heng Feng 		.ident = "Gateway P-79",
4786b33b7cd6SKai-Heng Feng 		.matches = {
4787b33b7cd6SKai-Heng Feng 			DMI_MATCH(DMI_SYS_VENDOR, "Gateway"),
4788b33b7cd6SKai-Heng Feng 			DMI_MATCH(DMI_PRODUCT_NAME, "P-79"),
4789b33b7cd6SKai-Heng Feng 		},
4790b33b7cd6SKai-Heng Feng 	},
479176104862STasos Sahanidis 	{
479276104862STasos Sahanidis 		.ident = "ASUS P5W DH Deluxe",
479376104862STasos Sahanidis 		.matches = {
479476104862STasos Sahanidis 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTEK COMPUTER INC"),
479576104862STasos Sahanidis 			DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
479676104862STasos Sahanidis 		},
479776104862STasos Sahanidis 	},
4798a261e379STakashi Iwai 	{
4799a261e379STakashi Iwai 		.ident = "ASUS P6T",
4800a261e379STakashi Iwai 		.matches = {
4801a261e379STakashi Iwai 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
4802a261e379STakashi Iwai 			DMI_MATCH(DMI_BOARD_NAME, "P6T"),
4803a261e379STakashi Iwai 		},
4804a261e379STakashi Iwai 	},
4805189308d5STakashi Iwai 	{
4806189308d5STakashi Iwai 		.ident = "ASUS P6X",
4807189308d5STakashi Iwai 		.matches = {
4808189308d5STakashi Iwai 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
4809189308d5STakashi Iwai 			DMI_MATCH(DMI_BOARD_NAME, "P6X"),
4810189308d5STakashi Iwai 		},
4811189308d5STakashi Iwai 	},
4812b33b7cd6SKai-Heng Feng 	{}
4813b33b7cd6SKai-Heng Feng };
4814b33b7cd6SKai-Heng Feng 
sky2_probe(struct pci_dev * pdev,const struct pci_device_id * ent)48151dd06ae8SGreg Kroah-Hartman static int sky2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4816527a6266SJeff Kirsher {
48170bdb0bd0Sstephen hemminger 	struct net_device *dev, *dev1;
4818527a6266SJeff Kirsher 	struct sky2_hw *hw;
4819527a6266SJeff Kirsher 	int err, using_dac = 0, wol_default;
4820527a6266SJeff Kirsher 	u32 reg;
4821527a6266SJeff Kirsher 	char buf1[16];
4822527a6266SJeff Kirsher 
4823527a6266SJeff Kirsher 	err = pci_enable_device(pdev);
4824527a6266SJeff Kirsher 	if (err) {
4825527a6266SJeff Kirsher 		dev_err(&pdev->dev, "cannot enable PCI device\n");
4826527a6266SJeff Kirsher 		goto err_out;
4827527a6266SJeff Kirsher 	}
4828527a6266SJeff Kirsher 
4829527a6266SJeff Kirsher 	/* Get configuration information
4830527a6266SJeff Kirsher 	 * Note: only regular PCI config access once to test for HW issues
4831527a6266SJeff Kirsher 	 *       other PCI access through shared memory for speed and to
4832527a6266SJeff Kirsher 	 *	 avoid MMCONFIG problems.
4833527a6266SJeff Kirsher 	 */
4834527a6266SJeff Kirsher 	err = pci_read_config_dword(pdev, PCI_DEV_REG2, &reg);
4835527a6266SJeff Kirsher 	if (err) {
4836527a6266SJeff Kirsher 		dev_err(&pdev->dev, "PCI read config failed\n");
48371c85382eSLino Sanfilippo 		goto err_out_disable;
4838527a6266SJeff Kirsher 	}
4839527a6266SJeff Kirsher 
4840527a6266SJeff Kirsher 	if (~reg == 0) {
4841527a6266SJeff Kirsher 		dev_err(&pdev->dev, "PCI configuration read error\n");
48420bd8ba18SPeter Senna Tschudin 		err = -EIO;
48431c85382eSLino Sanfilippo 		goto err_out_disable;
4844527a6266SJeff Kirsher 	}
4845527a6266SJeff Kirsher 
4846527a6266SJeff Kirsher 	err = pci_request_regions(pdev, DRV_NAME);
4847527a6266SJeff Kirsher 	if (err) {
4848527a6266SJeff Kirsher 		dev_err(&pdev->dev, "cannot obtain PCI resources\n");
4849527a6266SJeff Kirsher 		goto err_out_disable;
4850527a6266SJeff Kirsher 	}
4851527a6266SJeff Kirsher 
4852527a6266SJeff Kirsher 	pci_set_master(pdev);
4853527a6266SJeff Kirsher 
4854527a6266SJeff Kirsher 	if (sizeof(dma_addr_t) > sizeof(u32) &&
48556a03bfbdSluo penghao 	    !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
4856527a6266SJeff Kirsher 		using_dac = 1;
4857c86768cfSChristophe JAILLET 		err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
4858527a6266SJeff Kirsher 		if (err < 0) {
4859527a6266SJeff Kirsher 			dev_err(&pdev->dev, "unable to obtain 64 bit DMA "
4860527a6266SJeff Kirsher 				"for consistent allocations\n");
4861527a6266SJeff Kirsher 			goto err_out_free_regions;
4862527a6266SJeff Kirsher 		}
4863527a6266SJeff Kirsher 	} else {
4864c86768cfSChristophe JAILLET 		err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
4865527a6266SJeff Kirsher 		if (err) {
4866527a6266SJeff Kirsher 			dev_err(&pdev->dev, "no usable DMA configuration\n");
4867527a6266SJeff Kirsher 			goto err_out_free_regions;
4868527a6266SJeff Kirsher 		}
4869527a6266SJeff Kirsher 	}
4870527a6266SJeff Kirsher 
4871527a6266SJeff Kirsher 
4872527a6266SJeff Kirsher #ifdef __BIG_ENDIAN
4873527a6266SJeff Kirsher 	/* The sk98lin vendor driver uses hardware byte swapping but
4874527a6266SJeff Kirsher 	 * this driver uses software swapping.
4875527a6266SJeff Kirsher 	 */
4876527a6266SJeff Kirsher 	reg &= ~PCI_REV_DESC;
4877527a6266SJeff Kirsher 	err = pci_write_config_dword(pdev, PCI_DEV_REG2, reg);
4878527a6266SJeff Kirsher 	if (err) {
4879527a6266SJeff Kirsher 		dev_err(&pdev->dev, "PCI write config failed\n");
4880527a6266SJeff Kirsher 		goto err_out_free_regions;
4881527a6266SJeff Kirsher 	}
4882527a6266SJeff Kirsher #endif
4883527a6266SJeff Kirsher 
4884527a6266SJeff Kirsher 	wol_default = device_may_wakeup(&pdev->dev) ? WAKE_MAGIC : 0;
4885527a6266SJeff Kirsher 
4886527a6266SJeff Kirsher 	err = -ENOMEM;
4887527a6266SJeff Kirsher 
4888527a6266SJeff Kirsher 	hw = kzalloc(sizeof(*hw) + strlen(DRV_NAME "@pci:")
4889527a6266SJeff Kirsher 		     + strlen(pci_name(pdev)) + 1, GFP_KERNEL);
4890b2adaca9SJoe Perches 	if (!hw)
4891527a6266SJeff Kirsher 		goto err_out_free_regions;
4892527a6266SJeff Kirsher 
4893527a6266SJeff Kirsher 	hw->pdev = pdev;
4894527a6266SJeff Kirsher 	sprintf(hw->irq_name, DRV_NAME "@pci:%s", pci_name(pdev));
4895527a6266SJeff Kirsher 
48964bdc0d67SChristoph Hellwig 	hw->regs = ioremap(pci_resource_start(pdev, 0), 0x4000);
4897527a6266SJeff Kirsher 	if (!hw->regs) {
4898527a6266SJeff Kirsher 		dev_err(&pdev->dev, "cannot map device registers\n");
4899527a6266SJeff Kirsher 		goto err_out_free_hw;
4900527a6266SJeff Kirsher 	}
4901527a6266SJeff Kirsher 
4902527a6266SJeff Kirsher 	err = sky2_init(hw);
4903527a6266SJeff Kirsher 	if (err)
4904527a6266SJeff Kirsher 		goto err_out_iounmap;
4905527a6266SJeff Kirsher 
4906527a6266SJeff Kirsher 	/* ring for status responses */
4907527a6266SJeff Kirsher 	hw->st_size = hw->ports * roundup_pow_of_two(3*RX_MAX_PENDING + TX_MAX_PENDING);
4908c86768cfSChristophe JAILLET 	hw->st_le = dma_alloc_coherent(&pdev->dev,
4909c86768cfSChristophe JAILLET 				       hw->st_size * sizeof(struct sky2_status_le),
4910c86768cfSChristophe JAILLET 				       &hw->st_dma, GFP_KERNEL);
49110bd8ba18SPeter Senna Tschudin 	if (!hw->st_le) {
49120bd8ba18SPeter Senna Tschudin 		err = -ENOMEM;
4913527a6266SJeff Kirsher 		goto err_out_reset;
49140bd8ba18SPeter Senna Tschudin 	}
4915527a6266SJeff Kirsher 
4916527a6266SJeff Kirsher 	dev_info(&pdev->dev, "Yukon-2 %s chip revision %d\n",
4917527a6266SJeff Kirsher 		 sky2_name(hw->chip_id, buf1, sizeof(buf1)), hw->chip_rev);
4918527a6266SJeff Kirsher 
4919527a6266SJeff Kirsher 	sky2_reset(hw);
4920527a6266SJeff Kirsher 
4921527a6266SJeff Kirsher 	dev = sky2_init_netdev(hw, 0, using_dac, wol_default);
4922527a6266SJeff Kirsher 	if (!dev) {
4923527a6266SJeff Kirsher 		err = -ENOMEM;
4924527a6266SJeff Kirsher 		goto err_out_free_pci;
4925527a6266SJeff Kirsher 	}
4926527a6266SJeff Kirsher 
4927b33b7cd6SKai-Heng Feng 	if (disable_msi == -1)
4928b33b7cd6SKai-Heng Feng 		disable_msi = !!dmi_check_system(msi_blacklist);
4929b33b7cd6SKai-Heng Feng 
4930527a6266SJeff Kirsher 	if (!disable_msi && pci_enable_msi(pdev) == 0) {
4931527a6266SJeff Kirsher 		err = sky2_test_msi(hw);
49321c85382eSLino Sanfilippo 		if (err) {
4933527a6266SJeff Kirsher 			pci_disable_msi(pdev);
49341c85382eSLino Sanfilippo 			if (err != -EOPNOTSUPP)
4935527a6266SJeff Kirsher 				goto err_out_free_netdev;
4936527a6266SJeff Kirsher 		}
49371c85382eSLino Sanfilippo 	}
4938527a6266SJeff Kirsher 
4939b48b89f9SJakub Kicinski 	netif_napi_add(dev, &hw->napi, sky2_poll);
4940731073b9SStanislaw Gruszka 
4941527a6266SJeff Kirsher 	err = register_netdev(dev);
4942527a6266SJeff Kirsher 	if (err) {
4943527a6266SJeff Kirsher 		dev_err(&pdev->dev, "cannot register net device\n");
4944527a6266SJeff Kirsher 		goto err_out_free_netdev;
4945527a6266SJeff Kirsher 	}
4946527a6266SJeff Kirsher 
4947527a6266SJeff Kirsher 	netif_carrier_off(dev);
4948527a6266SJeff Kirsher 
4949527a6266SJeff Kirsher 	sky2_show_addr(dev);
4950527a6266SJeff Kirsher 
4951527a6266SJeff Kirsher 	if (hw->ports > 1) {
4952527a6266SJeff Kirsher 		dev1 = sky2_init_netdev(hw, 1, using_dac, wol_default);
49530bdb0bd0Sstephen hemminger 		if (!dev1) {
49540bdb0bd0Sstephen hemminger 			err = -ENOMEM;
49550bdb0bd0Sstephen hemminger 			goto err_out_unregister;
4956527a6266SJeff Kirsher 		}
49570bdb0bd0Sstephen hemminger 
49580bdb0bd0Sstephen hemminger 		err = register_netdev(dev1);
49590bdb0bd0Sstephen hemminger 		if (err) {
49600bdb0bd0Sstephen hemminger 			dev_err(&pdev->dev, "cannot register second net device\n");
49610bdb0bd0Sstephen hemminger 			goto err_out_free_dev1;
49620bdb0bd0Sstephen hemminger 		}
49630bdb0bd0Sstephen hemminger 
49640bdb0bd0Sstephen hemminger 		err = sky2_setup_irq(hw, hw->irq_name);
49650bdb0bd0Sstephen hemminger 		if (err)
49660bdb0bd0Sstephen hemminger 			goto err_out_unregister_dev1;
49670bdb0bd0Sstephen hemminger 
49680bdb0bd0Sstephen hemminger 		sky2_show_addr(dev1);
4969527a6266SJeff Kirsher 	}
4970527a6266SJeff Kirsher 
4971e99e88a9SKees Cook 	timer_setup(&hw->watchdog_timer, sky2_watchdog, 0);
4972527a6266SJeff Kirsher 	INIT_WORK(&hw->restart_work, sky2_restart);
4973527a6266SJeff Kirsher 
4974527a6266SJeff Kirsher 	pci_set_drvdata(pdev, hw);
49753789af9aSKrzysztof Wilczyński 	pdev->d3hot_delay = 300;
4976527a6266SJeff Kirsher 
4977527a6266SJeff Kirsher 	return 0;
4978527a6266SJeff Kirsher 
49790bdb0bd0Sstephen hemminger err_out_unregister_dev1:
49800bdb0bd0Sstephen hemminger 	unregister_netdev(dev1);
49810bdb0bd0Sstephen hemminger err_out_free_dev1:
49820bdb0bd0Sstephen hemminger 	free_netdev(dev1);
4983527a6266SJeff Kirsher err_out_unregister:
4984527a6266SJeff Kirsher 	unregister_netdev(dev);
4985527a6266SJeff Kirsher err_out_free_netdev:
49861c85382eSLino Sanfilippo 	if (hw->flags & SKY2_HW_USE_MSI)
49871c85382eSLino Sanfilippo 		pci_disable_msi(pdev);
4988527a6266SJeff Kirsher 	free_netdev(dev);
4989527a6266SJeff Kirsher err_out_free_pci:
4990c86768cfSChristophe JAILLET 	dma_free_coherent(&pdev->dev,
4991c86768cfSChristophe JAILLET 			  hw->st_size * sizeof(struct sky2_status_le),
4992527a6266SJeff Kirsher 			  hw->st_le, hw->st_dma);
4993527a6266SJeff Kirsher err_out_reset:
4994527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_RST_SET);
4995527a6266SJeff Kirsher err_out_iounmap:
4996527a6266SJeff Kirsher 	iounmap(hw->regs);
4997527a6266SJeff Kirsher err_out_free_hw:
4998527a6266SJeff Kirsher 	kfree(hw);
4999527a6266SJeff Kirsher err_out_free_regions:
5000527a6266SJeff Kirsher 	pci_release_regions(pdev);
5001527a6266SJeff Kirsher err_out_disable:
5002527a6266SJeff Kirsher 	pci_disable_device(pdev);
5003527a6266SJeff Kirsher err_out:
5004527a6266SJeff Kirsher 	return err;
5005527a6266SJeff Kirsher }
5006527a6266SJeff Kirsher 
sky2_remove(struct pci_dev * pdev)5007853e3f4cSBill Pemberton static void sky2_remove(struct pci_dev *pdev)
5008527a6266SJeff Kirsher {
5009527a6266SJeff Kirsher 	struct sky2_hw *hw = pci_get_drvdata(pdev);
5010527a6266SJeff Kirsher 	int i;
5011527a6266SJeff Kirsher 
5012527a6266SJeff Kirsher 	if (!hw)
5013527a6266SJeff Kirsher 		return;
5014527a6266SJeff Kirsher 
5015292a089dSSteven Rostedt (Google) 	timer_shutdown_sync(&hw->watchdog_timer);
5016527a6266SJeff Kirsher 	cancel_work_sync(&hw->restart_work);
5017527a6266SJeff Kirsher 
5018527a6266SJeff Kirsher 	for (i = hw->ports-1; i >= 0; --i)
5019527a6266SJeff Kirsher 		unregister_netdev(hw->dev[i]);
5020527a6266SJeff Kirsher 
5021527a6266SJeff Kirsher 	sky2_write32(hw, B0_IMSK, 0);
50220bdb0bd0Sstephen hemminger 	sky2_read32(hw, B0_IMSK);
5023527a6266SJeff Kirsher 
5024527a6266SJeff Kirsher 	sky2_power_aux(hw);
5025527a6266SJeff Kirsher 
5026527a6266SJeff Kirsher 	sky2_write8(hw, B0_CTST, CS_RST_SET);
5027527a6266SJeff Kirsher 	sky2_read8(hw, B0_CTST);
5028527a6266SJeff Kirsher 
50290bdb0bd0Sstephen hemminger 	if (hw->ports > 1) {
50300bdb0bd0Sstephen hemminger 		napi_disable(&hw->napi);
5031527a6266SJeff Kirsher 		free_irq(pdev->irq, hw);
50320bdb0bd0Sstephen hemminger 	}
50330bdb0bd0Sstephen hemminger 
5034527a6266SJeff Kirsher 	if (hw->flags & SKY2_HW_USE_MSI)
5035527a6266SJeff Kirsher 		pci_disable_msi(pdev);
5036c86768cfSChristophe JAILLET 	dma_free_coherent(&pdev->dev,
5037c86768cfSChristophe JAILLET 			  hw->st_size * sizeof(struct sky2_status_le),
5038527a6266SJeff Kirsher 			  hw->st_le, hw->st_dma);
5039527a6266SJeff Kirsher 	pci_release_regions(pdev);
5040527a6266SJeff Kirsher 	pci_disable_device(pdev);
5041527a6266SJeff Kirsher 
5042527a6266SJeff Kirsher 	for (i = hw->ports-1; i >= 0; --i)
5043527a6266SJeff Kirsher 		free_netdev(hw->dev[i]);
5044527a6266SJeff Kirsher 
5045527a6266SJeff Kirsher 	iounmap(hw->regs);
5046527a6266SJeff Kirsher 	kfree(hw);
5047527a6266SJeff Kirsher }
5048527a6266SJeff Kirsher 
sky2_suspend(struct device * dev)5049527a6266SJeff Kirsher static int sky2_suspend(struct device *dev)
5050527a6266SJeff Kirsher {
50517bdb9234SChuhong Yuan 	struct sky2_hw *hw = dev_get_drvdata(dev);
5052527a6266SJeff Kirsher 	int i;
5053527a6266SJeff Kirsher 
5054527a6266SJeff Kirsher 	if (!hw)
5055527a6266SJeff Kirsher 		return 0;
5056527a6266SJeff Kirsher 
5057527a6266SJeff Kirsher 	del_timer_sync(&hw->watchdog_timer);
5058527a6266SJeff Kirsher 	cancel_work_sync(&hw->restart_work);
5059527a6266SJeff Kirsher 
5060527a6266SJeff Kirsher 	rtnl_lock();
5061527a6266SJeff Kirsher 
5062527a6266SJeff Kirsher 	sky2_all_down(hw);
5063527a6266SJeff Kirsher 	for (i = 0; i < hw->ports; i++) {
5064527a6266SJeff Kirsher 		struct net_device *dev = hw->dev[i];
5065527a6266SJeff Kirsher 		struct sky2_port *sky2 = netdev_priv(dev);
5066527a6266SJeff Kirsher 
5067527a6266SJeff Kirsher 		if (sky2->wol)
5068527a6266SJeff Kirsher 			sky2_wol_init(sky2);
5069527a6266SJeff Kirsher 	}
5070527a6266SJeff Kirsher 
5071527a6266SJeff Kirsher 	sky2_power_aux(hw);
5072527a6266SJeff Kirsher 	rtnl_unlock();
5073527a6266SJeff Kirsher 
5074527a6266SJeff Kirsher 	return 0;
5075527a6266SJeff Kirsher }
5076527a6266SJeff Kirsher 
5077527a6266SJeff Kirsher #ifdef CONFIG_PM_SLEEP
sky2_resume(struct device * dev)5078527a6266SJeff Kirsher static int sky2_resume(struct device *dev)
5079527a6266SJeff Kirsher {
5080527a6266SJeff Kirsher 	struct pci_dev *pdev = to_pci_dev(dev);
5081527a6266SJeff Kirsher 	struct sky2_hw *hw = pci_get_drvdata(pdev);
5082527a6266SJeff Kirsher 	int err;
5083527a6266SJeff Kirsher 
5084527a6266SJeff Kirsher 	if (!hw)
5085527a6266SJeff Kirsher 		return 0;
5086527a6266SJeff Kirsher 
5087527a6266SJeff Kirsher 	/* Re-enable all clocks */
5088527a6266SJeff Kirsher 	err = pci_write_config_dword(pdev, PCI_DEV_REG3, 0);
5089527a6266SJeff Kirsher 	if (err) {
5090527a6266SJeff Kirsher 		dev_err(&pdev->dev, "PCI write config failed\n");
5091527a6266SJeff Kirsher 		goto out;
5092527a6266SJeff Kirsher 	}
5093527a6266SJeff Kirsher 
5094527a6266SJeff Kirsher 	rtnl_lock();
5095527a6266SJeff Kirsher 	sky2_reset(hw);
5096527a6266SJeff Kirsher 	sky2_all_up(hw);
5097527a6266SJeff Kirsher 	rtnl_unlock();
5098527a6266SJeff Kirsher 
5099527a6266SJeff Kirsher 	return 0;
5100527a6266SJeff Kirsher out:
5101527a6266SJeff Kirsher 
5102527a6266SJeff Kirsher 	dev_err(&pdev->dev, "resume failed (%d)\n", err);
5103527a6266SJeff Kirsher 	pci_disable_device(pdev);
5104527a6266SJeff Kirsher 	return err;
5105527a6266SJeff Kirsher }
5106527a6266SJeff Kirsher 
5107527a6266SJeff Kirsher static SIMPLE_DEV_PM_OPS(sky2_pm_ops, sky2_suspend, sky2_resume);
5108527a6266SJeff Kirsher #define SKY2_PM_OPS (&sky2_pm_ops)
5109527a6266SJeff Kirsher 
5110527a6266SJeff Kirsher #else
5111527a6266SJeff Kirsher 
5112527a6266SJeff Kirsher #define SKY2_PM_OPS NULL
5113527a6266SJeff Kirsher #endif
5114527a6266SJeff Kirsher 
sky2_shutdown(struct pci_dev * pdev)5115527a6266SJeff Kirsher static void sky2_shutdown(struct pci_dev *pdev)
5116527a6266SJeff Kirsher {
511706ba3b21SJeremy Linton 	struct sky2_hw *hw = pci_get_drvdata(pdev);
511806ba3b21SJeremy Linton 	int port;
511906ba3b21SJeremy Linton 
512006ba3b21SJeremy Linton 	for (port = 0; port < hw->ports; port++) {
512106ba3b21SJeremy Linton 		struct net_device *ndev = hw->dev[port];
512206ba3b21SJeremy Linton 
512306ba3b21SJeremy Linton 		rtnl_lock();
512406ba3b21SJeremy Linton 		if (netif_running(ndev)) {
512506ba3b21SJeremy Linton 			dev_close(ndev);
512606ba3b21SJeremy Linton 			netif_device_detach(ndev);
512706ba3b21SJeremy Linton 		}
512806ba3b21SJeremy Linton 		rtnl_unlock();
512906ba3b21SJeremy Linton 	}
5130527a6266SJeff Kirsher 	sky2_suspend(&pdev->dev);
5131527a6266SJeff Kirsher 	pci_wake_from_d3(pdev, device_may_wakeup(&pdev->dev));
5132527a6266SJeff Kirsher 	pci_set_power_state(pdev, PCI_D3hot);
5133527a6266SJeff Kirsher }
5134527a6266SJeff Kirsher 
5135527a6266SJeff Kirsher static struct pci_driver sky2_driver = {
5136527a6266SJeff Kirsher 	.name = DRV_NAME,
5137527a6266SJeff Kirsher 	.id_table = sky2_id_table,
5138527a6266SJeff Kirsher 	.probe = sky2_probe,
5139853e3f4cSBill Pemberton 	.remove = sky2_remove,
5140527a6266SJeff Kirsher 	.shutdown = sky2_shutdown,
5141527a6266SJeff Kirsher 	.driver.pm = SKY2_PM_OPS,
5142527a6266SJeff Kirsher };
5143527a6266SJeff Kirsher 
sky2_init_module(void)5144527a6266SJeff Kirsher static int __init sky2_init_module(void)
5145527a6266SJeff Kirsher {
5146527a6266SJeff Kirsher 	pr_info("driver version " DRV_VERSION "\n");
5147527a6266SJeff Kirsher 
5148527a6266SJeff Kirsher 	sky2_debug_init();
5149527a6266SJeff Kirsher 	return pci_register_driver(&sky2_driver);
5150527a6266SJeff Kirsher }
5151527a6266SJeff Kirsher 
sky2_cleanup_module(void)5152527a6266SJeff Kirsher static void __exit sky2_cleanup_module(void)
5153527a6266SJeff Kirsher {
5154527a6266SJeff Kirsher 	pci_unregister_driver(&sky2_driver);
5155527a6266SJeff Kirsher 	sky2_debug_cleanup();
5156527a6266SJeff Kirsher }
5157527a6266SJeff Kirsher 
5158527a6266SJeff Kirsher module_init(sky2_init_module);
5159527a6266SJeff Kirsher module_exit(sky2_cleanup_module);
5160527a6266SJeff Kirsher 
5161527a6266SJeff Kirsher MODULE_DESCRIPTION("Marvell Yukon 2 Gigabit Ethernet driver");
5162527a6266SJeff Kirsher MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
5163527a6266SJeff Kirsher MODULE_LICENSE("GPL");
5164527a6266SJeff Kirsher MODULE_VERSION(DRV_VERSION);
5165