sky2: carrier management
[linux-2.6-block.git] / drivers / net / sky2.c
CommitLineData
cd28ab6a
SH
1/*
2 * New driver for Marvell Yukon 2 chipset.
3 * Based on earlier sk98lin, and skge driver.
4 *
5 * This driver intentionally does not support all the features
6 * of the original driver such as link fail-over and link management because
7 * those should be done at higher levels.
8 *
9 * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
798b6b19 13 * the Free Software Foundation; either version 2 of the License.
cd28ab6a
SH
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
793b883e 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cd28ab6a
SH
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
793b883e 25#include <linux/crc32.h>
cd28ab6a
SH
26#include <linux/kernel.h>
27#include <linux/version.h>
28#include <linux/module.h>
29#include <linux/netdevice.h>
d0bbccfa 30#include <linux/dma-mapping.h>
cd28ab6a
SH
31#include <linux/etherdevice.h>
32#include <linux/ethtool.h>
33#include <linux/pci.h>
34#include <linux/ip.h>
c9bdd4b5 35#include <net/ip.h>
cd28ab6a
SH
36#include <linux/tcp.h>
37#include <linux/in.h>
38#include <linux/delay.h>
91c86df5 39#include <linux/workqueue.h>
d1f13708 40#include <linux/if_vlan.h>
d70cd51a 41#include <linux/prefetch.h>
ef743d33 42#include <linux/mii.h>
cd28ab6a
SH
43
44#include <asm/irq.h>
45
d1f13708 46#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
47#define SKY2_VLAN_TAG_USED 1
48#endif
49
cd28ab6a
SH
50#include "sky2.h"
51
52#define DRV_NAME "sky2"
a73b629b 53#define DRV_VERSION "1.15"
cd28ab6a
SH
54#define PFX DRV_NAME " "
55
56/*
57 * The Yukon II chipset takes 64 bit command blocks (called list elements)
58 * that are organized into three (receive, transmit, status) different rings
14d0263f 59 * similar to Tigon3.
cd28ab6a
SH
60 */
61
14d0263f 62#define RX_LE_SIZE 1024
cd28ab6a 63#define RX_LE_BYTES (RX_LE_SIZE*sizeof(struct sky2_rx_le))
14d0263f 64#define RX_MAX_PENDING (RX_LE_SIZE/6 - 2)
13210ce5 65#define RX_DEF_PENDING RX_MAX_PENDING
82788c7a 66#define RX_SKB_ALIGN 8
22e11703 67#define RX_BUF_WRITE 16
793b883e
SH
68
69#define TX_RING_SIZE 512
70#define TX_DEF_PENDING (TX_RING_SIZE - 1)
71#define TX_MIN_PENDING 64
b19666d9 72#define MAX_SKB_TX_LE (4 + (sizeof(dma_addr_t)/sizeof(u32))*MAX_SKB_FRAGS)
cd28ab6a 73
793b883e 74#define STATUS_RING_SIZE 2048 /* 2 ports * (TX + 2*RX) */
cd28ab6a 75#define STATUS_LE_BYTES (STATUS_RING_SIZE*sizeof(struct sky2_status_le))
cd28ab6a
SH
76#define TX_WATCHDOG (5 * HZ)
77#define NAPI_WEIGHT 64
78#define PHY_RETRIES 1000
79
cb5d9547
SH
80#define RING_NEXT(x,s) (((x)+1) & ((s)-1))
81
cd28ab6a 82static const u32 default_msg =
793b883e
SH
83 NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
84 | NETIF_MSG_TIMER | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR
3be92a70 85 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
cd28ab6a 86
793b883e 87static int debug = -1; /* defaults above */
cd28ab6a
SH
88module_param(debug, int, 0);
89MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
90
14d0263f 91static int copybreak __read_mostly = 128;
bdb5c58e
SH
92module_param(copybreak, int, 0);
93MODULE_PARM_DESC(copybreak, "Receive copy threshold");
94
fb2690a9
SH
95static int disable_msi = 0;
96module_param(disable_msi, int, 0);
97MODULE_PARM_DESC(disable_msi, "Disable Message Signaled Interrupt (MSI)");
98
c59697e0 99static int idle_timeout = 100;
01bd7564 100module_param(idle_timeout, int, 0);
e561a83b 101MODULE_PARM_DESC(idle_timeout, "Watchdog timer for lost interrupts (ms)");
01bd7564 102
cd28ab6a 103static const struct pci_device_id sky2_id_table[] = {
e5b74c7d
SH
104 { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9000) }, /* SK-9Sxx */
105 { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9E00) }, /* SK-9Exx */
2d2a3871 106 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4b00) }, /* DGE-560T */
2f4a66ad 107 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4001) }, /* DGE-550SX */
508f89e7 108 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4B02) }, /* DGE-560SX */
f1a0b6f5 109 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4B03) }, /* DGE-550T */
e5b74c7d
SH
110 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4340) }, /* 88E8021 */
111 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4341) }, /* 88E8022 */
112 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4342) }, /* 88E8061 */
113 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4343) }, /* 88E8062 */
114 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4344) }, /* 88E8021 */
115 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4345) }, /* 88E8022 */
116 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4346) }, /* 88E8061 */
117 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4347) }, /* 88E8062 */
118 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4350) }, /* 88E8035 */
119 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4351) }, /* 88E8036 */
120 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4352) }, /* 88E8038 */
121 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4353) }, /* 88E8039 */
122 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4356) }, /* 88EC033 */
123 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4360) }, /* 88E8052 */
124 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4361) }, /* 88E8050 */
125 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4362) }, /* 88E8053 */
126 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4363) }, /* 88E8055 */
127 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4364) }, /* 88E8056 */
128 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4366) }, /* 88EC036 */
129 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4367) }, /* 88EC032 */
130 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4368) }, /* 88EC034 */
f1a0b6f5
SH
131 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4369) }, /* 88EC042 */
132 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436A) }, /* 88E8058 */
69161611 133 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x436B) }, /* 88E8071 */
cd28ab6a
SH
134 { 0 }
135};
793b883e 136
cd28ab6a
SH
137MODULE_DEVICE_TABLE(pci, sky2_id_table);
138
139/* Avoid conditionals by using array */
140static const unsigned txqaddr[] = { Q_XA1, Q_XA2 };
141static const unsigned rxqaddr[] = { Q_R1, Q_R2 };
f4ea431b 142static const u32 portirq_msk[] = { Y2_IS_PORT_1, Y2_IS_PORT_2 };
cd28ab6a 143
92f965e8
SH
144/* This driver supports yukon2 chipset only */
145static const char *yukon2_name[] = {
146 "XL", /* 0xb3 */
147 "EC Ultra", /* 0xb4 */
93745494 148 "Extreme", /* 0xb5 */
92f965e8
SH
149 "EC", /* 0xb6 */
150 "FE", /* 0xb7 */
793b883e
SH
151};
152
793b883e 153/* Access to external PHY */
ef743d33 154static int gm_phy_write(struct sky2_hw *hw, unsigned port, u16 reg, u16 val)
cd28ab6a
SH
155{
156 int i;
157
158 gma_write16(hw, port, GM_SMI_DATA, val);
159 gma_write16(hw, port, GM_SMI_CTRL,
160 GM_SMI_CT_PHY_AD(PHY_ADDR_MARV) | GM_SMI_CT_REG_AD(reg));
161
162 for (i = 0; i < PHY_RETRIES; i++) {
cd28ab6a 163 if (!(gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_BUSY))
ef743d33 164 return 0;
793b883e 165 udelay(1);
cd28ab6a 166 }
ef743d33 167
793b883e 168 printk(KERN_WARNING PFX "%s: phy write timeout\n", hw->dev[port]->name);
ef743d33 169 return -ETIMEDOUT;
cd28ab6a
SH
170}
171
ef743d33 172static int __gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg, u16 *val)
cd28ab6a
SH
173{
174 int i;
175
793b883e 176 gma_write16(hw, port, GM_SMI_CTRL, GM_SMI_CT_PHY_AD(PHY_ADDR_MARV)
cd28ab6a
SH
177 | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
178
179 for (i = 0; i < PHY_RETRIES; i++) {
ef743d33 180 if (gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_RD_VAL) {
181 *val = gma_read16(hw, port, GM_SMI_DATA);
182 return 0;
183 }
184
793b883e 185 udelay(1);
cd28ab6a
SH
186 }
187
ef743d33 188 return -ETIMEDOUT;
189}
190
191static u16 gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg)
192{
193 u16 v;
194
195 if (__gm_phy_read(hw, port, reg, &v) != 0)
196 printk(KERN_WARNING PFX "%s: phy read timeout\n", hw->dev[port]->name);
197 return v;
cd28ab6a
SH
198}
199
5afa0a9c 200
ae306cca
SH
201static void sky2_power_on(struct sky2_hw *hw)
202{
203 /* switch power to VCC (WA for VAUX problem) */
204 sky2_write8(hw, B0_POWER_CTRL,
205 PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
5afa0a9c 206
ae306cca
SH
207 /* disable Core Clock Division, */
208 sky2_write32(hw, B2_Y2_CLK_CTRL, Y2_CLK_DIV_DIS);
d3bcfbeb 209
ae306cca
SH
210 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
211 /* enable bits are inverted */
212 sky2_write8(hw, B2_Y2_CLK_GATE,
213 Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
214 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
215 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
216 else
217 sky2_write8(hw, B2_Y2_CLK_GATE, 0);
977bdf06 218
93745494 219 if (hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX) {
fc99fe06 220 u32 reg;
5afa0a9c 221
fc99fe06
SH
222 reg = sky2_pci_read32(hw, PCI_DEV_REG4);
223 /* set all bits to 0 except bits 15..12 and 8 */
224 reg &= P_ASPM_CONTROL_MSK;
225 sky2_pci_write32(hw, PCI_DEV_REG4, reg);
226
227 reg = sky2_pci_read32(hw, PCI_DEV_REG5);
228 /* set all bits to 0 except bits 28 & 27 */
229 reg &= P_CTL_TIM_VMAIN_AV_MSK;
230 sky2_pci_write32(hw, PCI_DEV_REG5, reg);
231
232 sky2_pci_write32(hw, PCI_CFG_REG_1, 0);
8f70920f
SH
233
234 /* Enable workaround for dev 4.107 on Yukon-Ultra & Extreme */
235 reg = sky2_read32(hw, B2_GP_IO);
236 reg |= GLB_GPIO_STAT_RACE_DIS;
237 sky2_write32(hw, B2_GP_IO, reg);
5afa0a9c 238 }
ae306cca 239}
5afa0a9c 240
ae306cca
SH
241static void sky2_power_aux(struct sky2_hw *hw)
242{
243 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
244 sky2_write8(hw, B2_Y2_CLK_GATE, 0);
245 else
246 /* enable bits are inverted */
247 sky2_write8(hw, B2_Y2_CLK_GATE,
248 Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
249 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
250 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
251
252 /* switch power to VAUX */
253 if (sky2_read16(hw, B0_CTST) & Y2_VAUX_AVAIL)
254 sky2_write8(hw, B0_POWER_CTRL,
255 (PC_VAUX_ENA | PC_VCC_ENA |
256 PC_VAUX_ON | PC_VCC_OFF));
5afa0a9c 257}
258
d3bcfbeb 259static void sky2_gmac_reset(struct sky2_hw *hw, unsigned port)
cd28ab6a
SH
260{
261 u16 reg;
262
263 /* disable all GMAC IRQ's */
264 sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
265 /* disable PHY IRQs */
266 gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);
793b883e 267
cd28ab6a
SH
268 gma_write16(hw, port, GM_MC_ADDR_H1, 0); /* clear MC hash */
269 gma_write16(hw, port, GM_MC_ADDR_H2, 0);
270 gma_write16(hw, port, GM_MC_ADDR_H3, 0);
271 gma_write16(hw, port, GM_MC_ADDR_H4, 0);
272
273 reg = gma_read16(hw, port, GM_RX_CTRL);
274 reg |= GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA;
275 gma_write16(hw, port, GM_RX_CTRL, reg);
276}
277
16ad91e1
SH
278/* flow control to advertise bits */
279static const u16 copper_fc_adv[] = {
280 [FC_NONE] = 0,
281 [FC_TX] = PHY_M_AN_ASP,
282 [FC_RX] = PHY_M_AN_PC,
283 [FC_BOTH] = PHY_M_AN_PC | PHY_M_AN_ASP,
284};
285
286/* flow control to advertise bits when using 1000BaseX */
287static const u16 fiber_fc_adv[] = {
288 [FC_BOTH] = PHY_M_P_BOTH_MD_X,
289 [FC_TX] = PHY_M_P_ASYM_MD_X,
290 [FC_RX] = PHY_M_P_SYM_MD_X,
291 [FC_NONE] = PHY_M_P_NO_PAUSE_X,
292};
293
294/* flow control to GMA disable bits */
295static const u16 gm_fc_disable[] = {
296 [FC_NONE] = GM_GPCR_FC_RX_DIS | GM_GPCR_FC_TX_DIS,
297 [FC_TX] = GM_GPCR_FC_RX_DIS,
298 [FC_RX] = GM_GPCR_FC_TX_DIS,
299 [FC_BOTH] = 0,
300};
301
302
cd28ab6a
SH
303static void sky2_phy_init(struct sky2_hw *hw, unsigned port)
304{
305 struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
2eaba1a2 306 u16 ctrl, ct1000, adv, pg, ledctrl, ledover, reg;
cd28ab6a 307
93745494
SH
308 if (sky2->autoneg == AUTONEG_ENABLE
309 && !(hw->chip_id == CHIP_ID_YUKON_XL
310 || hw->chip_id == CHIP_ID_YUKON_EC_U
311 || hw->chip_id == CHIP_ID_YUKON_EX)) {
cd28ab6a
SH
312 u16 ectrl = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
313
314 ectrl &= ~(PHY_M_EC_M_DSC_MSK | PHY_M_EC_S_DSC_MSK |
793b883e 315 PHY_M_EC_MAC_S_MSK);
cd28ab6a
SH
316 ectrl |= PHY_M_EC_MAC_S(MAC_TX_CLK_25_MHZ);
317
53419c68 318 /* on PHY 88E1040 Rev.D0 (and newer) downshift control changed */
cd28ab6a 319 if (hw->chip_id == CHIP_ID_YUKON_EC)
53419c68 320 /* set downshift counter to 3x and enable downshift */
cd28ab6a
SH
321 ectrl |= PHY_M_EC_DSC_2(2) | PHY_M_EC_DOWN_S_ENA;
322 else
53419c68
SH
323 /* set master & slave downshift counter to 1x */
324 ectrl |= PHY_M_EC_M_DSC(0) | PHY_M_EC_S_DSC(1);
cd28ab6a
SH
325
326 gm_phy_write(hw, port, PHY_MARV_EXT_CTRL, ectrl);
327 }
328
329 ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
b89165f2 330 if (sky2_is_copper(hw)) {
cd28ab6a
SH
331 if (hw->chip_id == CHIP_ID_YUKON_FE) {
332 /* enable automatic crossover */
333 ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO) >> 1;
334 } else {
335 /* disable energy detect */
336 ctrl &= ~PHY_M_PC_EN_DET_MSK;
337
338 /* enable automatic crossover */
339 ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO);
340
53419c68 341 /* downshift on PHY 88E1112 and 88E1149 is changed */
93745494
SH
342 if (sky2->autoneg == AUTONEG_ENABLE
343 && (hw->chip_id == CHIP_ID_YUKON_XL
344 || hw->chip_id == CHIP_ID_YUKON_EC_U
345 || hw->chip_id == CHIP_ID_YUKON_EX)) {
53419c68 346 /* set downshift counter to 3x and enable downshift */
cd28ab6a
SH
347 ctrl &= ~PHY_M_PC_DSC_MSK;
348 ctrl |= PHY_M_PC_DSC(2) | PHY_M_PC_DOWN_S_ENA;
349 }
350 }
cd28ab6a
SH
351 } else {
352 /* workaround for deviation #4.88 (CRC errors) */
353 /* disable Automatic Crossover */
354
355 ctrl &= ~PHY_M_PC_MDIX_MSK;
b89165f2 356 }
cd28ab6a 357
b89165f2
SH
358 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
359
360 /* special setup for PHY 88E1112 Fiber */
361 if (hw->chip_id == CHIP_ID_YUKON_XL && !sky2_is_copper(hw)) {
362 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
cd28ab6a 363
b89165f2
SH
364 /* Fiber: select 1000BASE-X only mode MAC Specific Ctrl Reg. */
365 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 2);
366 ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
367 ctrl &= ~PHY_M_MAC_MD_MSK;
368 ctrl |= PHY_M_MAC_MODE_SEL(PHY_M_MAC_MD_1000BX);
369 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
370
371 if (hw->pmd_type == 'P') {
cd28ab6a
SH
372 /* select page 1 to access Fiber registers */
373 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 1);
b89165f2
SH
374
375 /* for SFP-module set SIGDET polarity to low */
376 ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
377 ctrl |= PHY_M_FIB_SIGD_POL;
34dd962b 378 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
cd28ab6a 379 }
b89165f2
SH
380
381 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
cd28ab6a
SH
382 }
383
7800fddc 384 ctrl = PHY_CT_RESET;
cd28ab6a
SH
385 ct1000 = 0;
386 adv = PHY_AN_CSMA;
2eaba1a2 387 reg = 0;
cd28ab6a
SH
388
389 if (sky2->autoneg == AUTONEG_ENABLE) {
b89165f2 390 if (sky2_is_copper(hw)) {
cd28ab6a
SH
391 if (sky2->advertising & ADVERTISED_1000baseT_Full)
392 ct1000 |= PHY_M_1000C_AFD;
393 if (sky2->advertising & ADVERTISED_1000baseT_Half)
394 ct1000 |= PHY_M_1000C_AHD;
395 if (sky2->advertising & ADVERTISED_100baseT_Full)
396 adv |= PHY_M_AN_100_FD;
397 if (sky2->advertising & ADVERTISED_100baseT_Half)
398 adv |= PHY_M_AN_100_HD;
399 if (sky2->advertising & ADVERTISED_10baseT_Full)
400 adv |= PHY_M_AN_10_FD;
401 if (sky2->advertising & ADVERTISED_10baseT_Half)
402 adv |= PHY_M_AN_10_HD;
709c6e7b 403
16ad91e1 404 adv |= copper_fc_adv[sky2->flow_mode];
b89165f2
SH
405 } else { /* special defines for FIBER (88E1040S only) */
406 if (sky2->advertising & ADVERTISED_1000baseT_Full)
407 adv |= PHY_M_AN_1000X_AFD;
408 if (sky2->advertising & ADVERTISED_1000baseT_Half)
409 adv |= PHY_M_AN_1000X_AHD;
cd28ab6a 410
16ad91e1 411 adv |= fiber_fc_adv[sky2->flow_mode];
709c6e7b 412 }
cd28ab6a
SH
413
414 /* Restart Auto-negotiation */
415 ctrl |= PHY_CT_ANE | PHY_CT_RE_CFG;
416 } else {
417 /* forced speed/duplex settings */
418 ct1000 = PHY_M_1000C_MSE;
419
2eaba1a2
SH
420 /* Disable auto update for duplex flow control and speed */
421 reg |= GM_GPCR_AU_ALL_DIS;
cd28ab6a
SH
422
423 switch (sky2->speed) {
424 case SPEED_1000:
425 ctrl |= PHY_CT_SP1000;
2eaba1a2 426 reg |= GM_GPCR_SPEED_1000;
cd28ab6a
SH
427 break;
428 case SPEED_100:
429 ctrl |= PHY_CT_SP100;
2eaba1a2 430 reg |= GM_GPCR_SPEED_100;
cd28ab6a
SH
431 break;
432 }
433
2eaba1a2
SH
434 if (sky2->duplex == DUPLEX_FULL) {
435 reg |= GM_GPCR_DUP_FULL;
436 ctrl |= PHY_CT_DUP_MD;
16ad91e1
SH
437 } else if (sky2->speed < SPEED_1000)
438 sky2->flow_mode = FC_NONE;
2eaba1a2 439
2eaba1a2 440
16ad91e1 441 reg |= gm_fc_disable[sky2->flow_mode];
2eaba1a2
SH
442
443 /* Forward pause packets to GMAC? */
16ad91e1 444 if (sky2->flow_mode & FC_RX)
2eaba1a2
SH
445 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
446 else
447 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
cd28ab6a
SH
448 }
449
2eaba1a2
SH
450 gma_write16(hw, port, GM_GP_CTRL, reg);
451
cd28ab6a
SH
452 if (hw->chip_id != CHIP_ID_YUKON_FE)
453 gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, ct1000);
454
455 gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, adv);
456 gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
457
458 /* Setup Phy LED's */
459 ledctrl = PHY_M_LED_PULS_DUR(PULS_170MS);
460 ledover = 0;
461
462 switch (hw->chip_id) {
463 case CHIP_ID_YUKON_FE:
464 /* on 88E3082 these bits are at 11..9 (shifted left) */
465 ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) << 1;
466
467 ctrl = gm_phy_read(hw, port, PHY_MARV_FE_LED_PAR);
468
469 /* delete ACT LED control bits */
470 ctrl &= ~PHY_M_FELP_LED1_MSK;
471 /* change ACT LED control to blink mode */
472 ctrl |= PHY_M_FELP_LED1_CTRL(LED_PAR_CTRL_ACT_BL);
473 gm_phy_write(hw, port, PHY_MARV_FE_LED_PAR, ctrl);
474 break;
475
476 case CHIP_ID_YUKON_XL:
793b883e 477 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
cd28ab6a
SH
478
479 /* select page 3 to access LED control register */
480 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
481
482 /* set LED Function Control register */
ed6d32c7
SH
483 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
484 (PHY_M_LEDC_LOS_CTRL(1) | /* LINK/ACT */
485 PHY_M_LEDC_INIT_CTRL(7) | /* 10 Mbps */
486 PHY_M_LEDC_STA1_CTRL(7) | /* 100 Mbps */
487 PHY_M_LEDC_STA0_CTRL(7))); /* 1000 Mbps */
cd28ab6a
SH
488
489 /* set Polarity Control register */
490 gm_phy_write(hw, port, PHY_MARV_PHY_STAT,
793b883e
SH
491 (PHY_M_POLC_LS1_P_MIX(4) |
492 PHY_M_POLC_IS0_P_MIX(4) |
493 PHY_M_POLC_LOS_CTRL(2) |
494 PHY_M_POLC_INIT_CTRL(2) |
495 PHY_M_POLC_STA1_CTRL(2) |
496 PHY_M_POLC_STA0_CTRL(2)));
cd28ab6a
SH
497
498 /* restore page register */
793b883e 499 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
cd28ab6a 500 break;
93745494 501
ed6d32c7 502 case CHIP_ID_YUKON_EC_U:
93745494 503 case CHIP_ID_YUKON_EX:
ed6d32c7
SH
504 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
505
506 /* select page 3 to access LED control register */
507 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
508
509 /* set LED Function Control register */
510 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
511 (PHY_M_LEDC_LOS_CTRL(1) | /* LINK/ACT */
512 PHY_M_LEDC_INIT_CTRL(8) | /* 10 Mbps */
513 PHY_M_LEDC_STA1_CTRL(7) | /* 100 Mbps */
514 PHY_M_LEDC_STA0_CTRL(7)));/* 1000 Mbps */
515
516 /* set Blink Rate in LED Timer Control Register */
517 gm_phy_write(hw, port, PHY_MARV_INT_MASK,
518 ledctrl | PHY_M_LED_BLINK_RT(BLINK_84MS));
519 /* restore page register */
520 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
521 break;
cd28ab6a
SH
522
523 default:
524 /* set Tx LED (LED_TX) to blink mode on Rx OR Tx activity */
525 ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) | PHY_M_LEDC_TX_CTRL;
526 /* turn off the Rx LED (LED_RX) */
0efdf262 527 ledover &= ~PHY_M_LED_MO_RX;
cd28ab6a
SH
528 }
529
9467a8fc
SH
530 if (hw->chip_id == CHIP_ID_YUKON_EC_U &&
531 hw->chip_rev == CHIP_REV_YU_EC_U_A1) {
977bdf06 532 /* apply fixes in PHY AFE */
ed6d32c7
SH
533 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 255);
534
977bdf06 535 /* increase differential signal amplitude in 10BASE-T */
ed6d32c7
SH
536 gm_phy_write(hw, port, 0x18, 0xaa99);
537 gm_phy_write(hw, port, 0x17, 0x2011);
cd28ab6a 538
977bdf06 539 /* fix for IEEE A/B Symmetry failure in 1000BASE-T */
ed6d32c7
SH
540 gm_phy_write(hw, port, 0x18, 0xa204);
541 gm_phy_write(hw, port, 0x17, 0x2002);
977bdf06
SH
542
543 /* set page register to 0 */
9467a8fc 544 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 0);
93745494 545 } else if (hw->chip_id != CHIP_ID_YUKON_EX) {
977bdf06 546 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, ledctrl);
cd28ab6a 547
977bdf06
SH
548 if (sky2->autoneg == AUTONEG_DISABLE || sky2->speed == SPEED_100) {
549 /* turn on 100 Mbps LED (LED_LINK100) */
0efdf262 550 ledover |= PHY_M_LED_MO_100;
977bdf06 551 }
cd28ab6a 552
977bdf06
SH
553 if (ledover)
554 gm_phy_write(hw, port, PHY_MARV_LED_OVER, ledover);
555
556 }
2eaba1a2 557
d571b694 558 /* Enable phy interrupt on auto-negotiation complete (or link up) */
cd28ab6a
SH
559 if (sky2->autoneg == AUTONEG_ENABLE)
560 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_AN_COMPL);
561 else
562 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
563}
564
d3bcfbeb 565static void sky2_phy_power(struct sky2_hw *hw, unsigned port, int onoff)
566{
567 u32 reg1;
568 static const u32 phy_power[]
569 = { PCI_Y2_PHY1_POWD, PCI_Y2_PHY2_POWD };
570
571 /* looks like this XL is back asswards .. */
572 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
573 onoff = !onoff;
574
aed2cec4 575 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
d3bcfbeb 576 reg1 = sky2_pci_read32(hw, PCI_DEV_REG1);
d3bcfbeb 577 if (onoff)
578 /* Turn off phy power saving */
579 reg1 &= ~phy_power[port];
580 else
581 reg1 |= phy_power[port];
582
583 sky2_pci_write32(hw, PCI_DEV_REG1, reg1);
98232f85 584 sky2_pci_read32(hw, PCI_DEV_REG1);
aed2cec4 585 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
d3bcfbeb 586 udelay(100);
587}
588
1b537565
SH
589/* Force a renegotiation */
590static void sky2_phy_reinit(struct sky2_port *sky2)
591{
e07b1aa8 592 spin_lock_bh(&sky2->phy_lock);
1b537565 593 sky2_phy_init(sky2->hw, sky2->port);
e07b1aa8 594 spin_unlock_bh(&sky2->phy_lock);
1b537565
SH
595}
596
e3173832
SH
597/* Put device in state to listen for Wake On Lan */
598static void sky2_wol_init(struct sky2_port *sky2)
599{
600 struct sky2_hw *hw = sky2->hw;
601 unsigned port = sky2->port;
602 enum flow_control save_mode;
603 u16 ctrl;
604 u32 reg1;
605
606 /* Bring hardware out of reset */
607 sky2_write16(hw, B0_CTST, CS_RST_CLR);
608 sky2_write16(hw, SK_REG(port, GMAC_LINK_CTRL), GMLC_RST_CLR);
609
610 sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
611 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
612
613 /* Force to 10/100
614 * sky2_reset will re-enable on resume
615 */
616 save_mode = sky2->flow_mode;
617 ctrl = sky2->advertising;
618
619 sky2->advertising &= ~(ADVERTISED_1000baseT_Half|ADVERTISED_1000baseT_Full);
620 sky2->flow_mode = FC_NONE;
621 sky2_phy_power(hw, port, 1);
622 sky2_phy_reinit(sky2);
623
624 sky2->flow_mode = save_mode;
625 sky2->advertising = ctrl;
626
627 /* Set GMAC to no flow control and auto update for speed/duplex */
628 gma_write16(hw, port, GM_GP_CTRL,
629 GM_GPCR_FC_TX_DIS|GM_GPCR_TX_ENA|GM_GPCR_RX_ENA|
630 GM_GPCR_DUP_FULL|GM_GPCR_FC_RX_DIS|GM_GPCR_AU_FCT_DIS);
631
632 /* Set WOL address */
633 memcpy_toio(hw->regs + WOL_REGS(port, WOL_MAC_ADDR),
634 sky2->netdev->dev_addr, ETH_ALEN);
635
636 /* Turn on appropriate WOL control bits */
637 sky2_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), WOL_CTL_CLEAR_RESULT);
638 ctrl = 0;
639 if (sky2->wol & WAKE_PHY)
640 ctrl |= WOL_CTL_ENA_PME_ON_LINK_CHG|WOL_CTL_ENA_LINK_CHG_UNIT;
641 else
642 ctrl |= WOL_CTL_DIS_PME_ON_LINK_CHG|WOL_CTL_DIS_LINK_CHG_UNIT;
643
644 if (sky2->wol & WAKE_MAGIC)
645 ctrl |= WOL_CTL_ENA_PME_ON_MAGIC_PKT|WOL_CTL_ENA_MAGIC_PKT_UNIT;
646 else
647 ctrl |= WOL_CTL_DIS_PME_ON_MAGIC_PKT|WOL_CTL_DIS_MAGIC_PKT_UNIT;;
648
649 ctrl |= WOL_CTL_DIS_PME_ON_PATTERN|WOL_CTL_DIS_PATTERN_UNIT;
650 sky2_write16(hw, WOL_REGS(port, WOL_CTRL_STAT), ctrl);
651
652 /* Turn on legacy PCI-Express PME mode */
653 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
654 reg1 = sky2_pci_read32(hw, PCI_DEV_REG1);
655 reg1 |= PCI_Y2_PME_LEGACY;
656 sky2_pci_write32(hw, PCI_DEV_REG1, reg1);
657 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
658
659 /* block receiver */
660 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
661
662}
663
69161611
SH
664static void sky2_set_tx_stfwd(struct sky2_hw *hw, unsigned port)
665{
666 if (hw->chip_id == CHIP_ID_YUKON_EX && hw->chip_rev != CHIP_REV_YU_EX_A0) {
667 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
668 TX_STFW_ENA |
669 (hw->dev[port]->mtu > ETH_DATA_LEN) ? TX_JUMBO_ENA : TX_JUMBO_DIS);
670 } else {
671 if (hw->dev[port]->mtu > ETH_DATA_LEN) {
672 /* set Tx GMAC FIFO Almost Empty Threshold */
673 sky2_write32(hw, SK_REG(port, TX_GMF_AE_THR),
674 (ECU_JUMBO_WM << 16) | ECU_AE_THR);
675
676 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
677 TX_JUMBO_ENA | TX_STFW_DIS);
678
679 /* Can't do offload because of lack of store/forward */
680 hw->dev[port]->features &= ~(NETIF_F_TSO | NETIF_F_SG
681 | NETIF_F_ALL_CSUM);
682 } else
683 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
684 TX_JUMBO_DIS | TX_STFW_ENA);
685 }
686}
687
cd28ab6a
SH
688static void sky2_mac_init(struct sky2_hw *hw, unsigned port)
689{
690 struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
691 u16 reg;
692 int i;
693 const u8 *addr = hw->dev[port]->dev_addr;
694
42eeea01 695 sky2_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
b4ed372b 696 sky2_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR);
cd28ab6a
SH
697
698 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
699
793b883e 700 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0 && port == 1) {
cd28ab6a
SH
701 /* WA DEV_472 -- looks like crossed wires on port 2 */
702 /* clear GMAC 1 Control reset */
703 sky2_write8(hw, SK_REG(0, GMAC_CTRL), GMC_RST_CLR);
704 do {
705 sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_SET);
706 sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_CLR);
707 } while (gm_phy_read(hw, 1, PHY_MARV_ID0) != PHY_MARV_ID0_VAL ||
708 gm_phy_read(hw, 1, PHY_MARV_ID1) != PHY_MARV_ID1_Y2 ||
709 gm_phy_read(hw, 1, PHY_MARV_INT_MASK) != 0);
710 }
711
793b883e 712 sky2_read16(hw, SK_REG(port, GMAC_IRQ_SRC));
cd28ab6a 713
2eaba1a2
SH
714 /* Enable Transmit FIFO Underrun */
715 sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), GMAC_DEF_MSK);
716
e07b1aa8 717 spin_lock_bh(&sky2->phy_lock);
cd28ab6a 718 sky2_phy_init(hw, port);
e07b1aa8 719 spin_unlock_bh(&sky2->phy_lock);
cd28ab6a
SH
720
721 /* MIB clear */
722 reg = gma_read16(hw, port, GM_PHY_ADDR);
723 gma_write16(hw, port, GM_PHY_ADDR, reg | GM_PAR_MIB_CLR);
724
43f2f104
SH
725 for (i = GM_MIB_CNT_BASE; i <= GM_MIB_CNT_END; i += 4)
726 gma_read16(hw, port, i);
cd28ab6a
SH
727 gma_write16(hw, port, GM_PHY_ADDR, reg);
728
729 /* transmit control */
730 gma_write16(hw, port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
731
732 /* receive control reg: unicast + multicast + no FCS */
733 gma_write16(hw, port, GM_RX_CTRL,
793b883e 734 GM_RXCR_UCF_ENA | GM_RXCR_CRC_DIS | GM_RXCR_MCF_ENA);
cd28ab6a
SH
735
736 /* transmit flow control */
737 gma_write16(hw, port, GM_TX_FLOW_CTRL, 0xffff);
738
739 /* transmit parameter */
740 gma_write16(hw, port, GM_TX_PARAM,
741 TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) |
742 TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
743 TX_IPG_JAM_DATA(TX_IPG_JAM_DEF) |
744 TX_BACK_OFF_LIM(TX_BOF_LIM_DEF));
745
746 /* serial mode register */
747 reg = DATA_BLIND_VAL(DATA_BLIND_DEF) |
6b1a3aef 748 GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF);
cd28ab6a 749
6b1a3aef 750 if (hw->dev[port]->mtu > ETH_DATA_LEN)
cd28ab6a
SH
751 reg |= GM_SMOD_JUMBO_ENA;
752
753 gma_write16(hw, port, GM_SERIAL_MODE, reg);
754
cd28ab6a
SH
755 /* virtual address for data */
756 gma_set_addr(hw, port, GM_SRC_ADDR_2L, addr);
757
793b883e
SH
758 /* physical address: used for pause frames */
759 gma_set_addr(hw, port, GM_SRC_ADDR_1L, addr);
760
761 /* ignore counter overflows */
cd28ab6a
SH
762 gma_write16(hw, port, GM_TX_IRQ_MSK, 0);
763 gma_write16(hw, port, GM_RX_IRQ_MSK, 0);
764 gma_write16(hw, port, GM_TR_IRQ_MSK, 0);
765
766 /* Configure Rx MAC FIFO */
767 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_CLR);
69161611
SH
768 reg = GMF_OPER_ON | GMF_RX_F_FL_ON;
769 if (hw->chip_id == CHIP_ID_YUKON_EX)
770 reg |= GMF_RX_OVER_ON;
771
772 sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T), reg);
cd28ab6a 773
d571b694 774 /* Flush Rx MAC FIFO on any flow control or error */
42eeea01 775 sky2_write16(hw, SK_REG(port, RX_GMF_FL_MSK), GMR_FS_ANY_ERR);
cd28ab6a 776
8df9a876
SH
777 /* Set threshold to 0xa (64 bytes) + 1 to workaround pause bug */
778 sky2_write16(hw, SK_REG(port, RX_GMF_FL_THR), RX_GMF_FL_THR_DEF+1);
cd28ab6a
SH
779
780 /* Configure Tx MAC FIFO */
781 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_CLR);
782 sky2_write16(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_OPER_ON);
5a5b1ea0 783
93745494 784 if (hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX) {
8df9a876 785 sky2_write8(hw, SK_REG(port, RX_GMF_LP_THR), 768/8);
5a5b1ea0 786 sky2_write8(hw, SK_REG(port, RX_GMF_UP_THR), 1024/8);
b628ed98 787
69161611 788 sky2_set_tx_stfwd(hw, port);
5a5b1ea0 789 }
790
cd28ab6a
SH
791}
792
67712901
SH
793/* Assign Ram Buffer allocation to queue */
794static void sky2_ramset(struct sky2_hw *hw, u16 q, u32 start, u32 space)
cd28ab6a 795{
67712901
SH
796 u32 end;
797
798 /* convert from K bytes to qwords used for hw register */
799 start *= 1024/8;
800 space *= 1024/8;
801 end = start + space - 1;
793b883e 802
cd28ab6a
SH
803 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_RST_CLR);
804 sky2_write32(hw, RB_ADDR(q, RB_START), start);
805 sky2_write32(hw, RB_ADDR(q, RB_END), end);
806 sky2_write32(hw, RB_ADDR(q, RB_WP), start);
807 sky2_write32(hw, RB_ADDR(q, RB_RP), start);
808
809 if (q == Q_R1 || q == Q_R2) {
1c28f6ba 810 u32 tp = space - space/4;
793b883e 811
1c28f6ba
SH
812 /* On receive queue's set the thresholds
813 * give receiver priority when > 3/4 full
814 * send pause when down to 2K
815 */
816 sky2_write32(hw, RB_ADDR(q, RB_RX_UTHP), tp);
817 sky2_write32(hw, RB_ADDR(q, RB_RX_LTHP), space/2);
793b883e 818
1c28f6ba
SH
819 tp = space - 2048/8;
820 sky2_write32(hw, RB_ADDR(q, RB_RX_UTPP), tp);
821 sky2_write32(hw, RB_ADDR(q, RB_RX_LTPP), space/4);
cd28ab6a
SH
822 } else {
823 /* Enable store & forward on Tx queue's because
824 * Tx FIFO is only 1K on Yukon
825 */
826 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_STFWD);
827 }
828
829 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_OP_MD);
793b883e 830 sky2_read8(hw, RB_ADDR(q, RB_CTRL));
cd28ab6a
SH
831}
832
cd28ab6a 833/* Setup Bus Memory Interface */
af4ed7e6 834static void sky2_qset(struct sky2_hw *hw, u16 q)
cd28ab6a
SH
835{
836 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_CLR_RESET);
837 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_OPER_INIT);
838 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_FIFO_OP_ON);
af4ed7e6 839 sky2_write32(hw, Q_ADDR(q, Q_WM), BMU_WM_DEFAULT);
cd28ab6a
SH
840}
841
cd28ab6a
SH
842/* Setup prefetch unit registers. This is the interface between
843 * hardware and driver list elements
844 */
8cc048e3 845static void sky2_prefetch_init(struct sky2_hw *hw, u32 qaddr,
cd28ab6a
SH
846 u64 addr, u32 last)
847{
cd28ab6a
SH
848 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
849 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_CLR);
850 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_HI), addr >> 32);
851 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_LO), (u32) addr);
852 sky2_write16(hw, Y2_QADDR(qaddr, PREF_UNIT_LAST_IDX), last);
853 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_OP_ON);
793b883e
SH
854
855 sky2_read32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL));
cd28ab6a
SH
856}
857
793b883e
SH
858static inline struct sky2_tx_le *get_tx_le(struct sky2_port *sky2)
859{
860 struct sky2_tx_le *le = sky2->tx_le + sky2->tx_prod;
861
cb5d9547 862 sky2->tx_prod = RING_NEXT(sky2->tx_prod, TX_RING_SIZE);
291ea614 863 le->ctrl = 0;
793b883e
SH
864 return le;
865}
cd28ab6a 866
291ea614
SH
867static inline struct tx_ring_info *tx_le_re(struct sky2_port *sky2,
868 struct sky2_tx_le *le)
869{
870 return sky2->tx_ring + (le - sky2->tx_le);
871}
872
290d4de5
SH
873/* Update chip's next pointer */
874static inline void sky2_put_idx(struct sky2_hw *hw, unsigned q, u16 idx)
cd28ab6a 875{
50432cb5 876 /* Make sure write' to descriptors are complete before we tell hardware */
762c2de2 877 wmb();
50432cb5
SH
878 sky2_write16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX), idx);
879
880 /* Synchronize I/O on since next processor may write to tail */
881 mmiowb();
cd28ab6a
SH
882}
883
793b883e 884
cd28ab6a
SH
885static inline struct sky2_rx_le *sky2_next_rx(struct sky2_port *sky2)
886{
887 struct sky2_rx_le *le = sky2->rx_le + sky2->rx_put;
cb5d9547 888 sky2->rx_put = RING_NEXT(sky2->rx_put, RX_LE_SIZE);
291ea614 889 le->ctrl = 0;
cd28ab6a
SH
890 return le;
891}
892
a018e330 893/* Return high part of DMA address (could be 32 or 64 bit) */
894static inline u32 high32(dma_addr_t a)
895{
a036119f 896 return sizeof(a) > sizeof(u32) ? (a >> 16) >> 16 : 0;
a018e330 897}
898
14d0263f
SH
899/* Build description to hardware for one receive segment */
900static void sky2_rx_add(struct sky2_port *sky2, u8 op,
901 dma_addr_t map, unsigned len)
cd28ab6a
SH
902{
903 struct sky2_rx_le *le;
734d1868 904 u32 hi = high32(map);
cd28ab6a 905
793b883e 906 if (sky2->rx_addr64 != hi) {
cd28ab6a 907 le = sky2_next_rx(sky2);
793b883e 908 le->addr = cpu_to_le32(hi);
cd28ab6a 909 le->opcode = OP_ADDR64 | HW_OWNER;
734d1868 910 sky2->rx_addr64 = high32(map + len);
cd28ab6a 911 }
793b883e 912
cd28ab6a 913 le = sky2_next_rx(sky2);
734d1868
SH
914 le->addr = cpu_to_le32((u32) map);
915 le->length = cpu_to_le16(len);
14d0263f 916 le->opcode = op | HW_OWNER;
cd28ab6a
SH
917}
918
14d0263f
SH
919/* Build description to hardware for one possibly fragmented skb */
920static void sky2_rx_submit(struct sky2_port *sky2,
921 const struct rx_ring_info *re)
922{
923 int i;
924
925 sky2_rx_add(sky2, OP_PACKET, re->data_addr, sky2->rx_data_size);
926
927 for (i = 0; i < skb_shinfo(re->skb)->nr_frags; i++)
928 sky2_rx_add(sky2, OP_BUFFER, re->frag_addr[i], PAGE_SIZE);
929}
930
931
932static void sky2_rx_map_skb(struct pci_dev *pdev, struct rx_ring_info *re,
933 unsigned size)
934{
935 struct sk_buff *skb = re->skb;
936 int i;
937
938 re->data_addr = pci_map_single(pdev, skb->data, size, PCI_DMA_FROMDEVICE);
939 pci_unmap_len_set(re, data_size, size);
940
941 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
942 re->frag_addr[i] = pci_map_page(pdev,
943 skb_shinfo(skb)->frags[i].page,
944 skb_shinfo(skb)->frags[i].page_offset,
945 skb_shinfo(skb)->frags[i].size,
946 PCI_DMA_FROMDEVICE);
947}
948
949static void sky2_rx_unmap_skb(struct pci_dev *pdev, struct rx_ring_info *re)
950{
951 struct sk_buff *skb = re->skb;
952 int i;
953
954 pci_unmap_single(pdev, re->data_addr, pci_unmap_len(re, data_size),
955 PCI_DMA_FROMDEVICE);
956
957 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
958 pci_unmap_page(pdev, re->frag_addr[i],
959 skb_shinfo(skb)->frags[i].size,
960 PCI_DMA_FROMDEVICE);
961}
793b883e 962
cd28ab6a
SH
963/* Tell chip where to start receive checksum.
964 * Actually has two checksums, but set both same to avoid possible byte
965 * order problems.
966 */
793b883e 967static void rx_set_checksum(struct sky2_port *sky2)
cd28ab6a
SH
968{
969 struct sky2_rx_le *le;
970
69161611
SH
971 if (sky2->hw->chip_id != CHIP_ID_YUKON_EX) {
972 le = sky2_next_rx(sky2);
973 le->addr = cpu_to_le32((ETH_HLEN << 16) | ETH_HLEN);
974 le->ctrl = 0;
975 le->opcode = OP_TCPSTART | HW_OWNER;
793b883e 976
69161611
SH
977 sky2_write32(sky2->hw,
978 Q_ADDR(rxqaddr[sky2->port], Q_CSR),
979 sky2->rx_csum ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
980 }
cd28ab6a
SH
981
982}
983
6b1a3aef 984/*
985 * The RX Stop command will not work for Yukon-2 if the BMU does not
986 * reach the end of packet and since we can't make sure that we have
987 * incoming data, we must reset the BMU while it is not doing a DMA
988 * transfer. Since it is possible that the RX path is still active,
989 * the RX RAM buffer will be stopped first, so any possible incoming
990 * data will not trigger a DMA. After the RAM buffer is stopped, the
991 * BMU is polled until any DMA in progress is ended and only then it
992 * will be reset.
993 */
994static void sky2_rx_stop(struct sky2_port *sky2)
995{
996 struct sky2_hw *hw = sky2->hw;
997 unsigned rxq = rxqaddr[sky2->port];
998 int i;
999
1000 /* disable the RAM Buffer receive queue */
1001 sky2_write8(hw, RB_ADDR(rxq, RB_CTRL), RB_DIS_OP_MD);
1002
1003 for (i = 0; i < 0xffff; i++)
1004 if (sky2_read8(hw, RB_ADDR(rxq, Q_RSL))
1005 == sky2_read8(hw, RB_ADDR(rxq, Q_RL)))
1006 goto stopped;
1007
1008 printk(KERN_WARNING PFX "%s: receiver stop failed\n",
1009 sky2->netdev->name);
1010stopped:
1011 sky2_write32(hw, Q_ADDR(rxq, Q_CSR), BMU_RST_SET | BMU_FIFO_RST);
1012
1013 /* reset the Rx prefetch unit */
1014 sky2_write32(hw, Y2_QADDR(rxq, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
50432cb5 1015 mmiowb();
6b1a3aef 1016}
793b883e 1017
d571b694 1018/* Clean out receive buffer area, assumes receiver hardware stopped */
cd28ab6a
SH
1019static void sky2_rx_clean(struct sky2_port *sky2)
1020{
1021 unsigned i;
1022
1023 memset(sky2->rx_le, 0, RX_LE_BYTES);
793b883e 1024 for (i = 0; i < sky2->rx_pending; i++) {
291ea614 1025 struct rx_ring_info *re = sky2->rx_ring + i;
cd28ab6a
SH
1026
1027 if (re->skb) {
14d0263f 1028 sky2_rx_unmap_skb(sky2->hw->pdev, re);
cd28ab6a
SH
1029 kfree_skb(re->skb);
1030 re->skb = NULL;
1031 }
1032 }
1033}
1034
ef743d33 1035/* Basic MII support */
1036static int sky2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1037{
1038 struct mii_ioctl_data *data = if_mii(ifr);
1039 struct sky2_port *sky2 = netdev_priv(dev);
1040 struct sky2_hw *hw = sky2->hw;
1041 int err = -EOPNOTSUPP;
1042
1043 if (!netif_running(dev))
1044 return -ENODEV; /* Phy still in reset */
1045
d89e1343 1046 switch (cmd) {
ef743d33 1047 case SIOCGMIIPHY:
1048 data->phy_id = PHY_ADDR_MARV;
1049
1050 /* fallthru */
1051 case SIOCGMIIREG: {
1052 u16 val = 0;
91c86df5 1053
e07b1aa8 1054 spin_lock_bh(&sky2->phy_lock);
ef743d33 1055 err = __gm_phy_read(hw, sky2->port, data->reg_num & 0x1f, &val);
e07b1aa8 1056 spin_unlock_bh(&sky2->phy_lock);
91c86df5 1057
ef743d33 1058 data->val_out = val;
1059 break;
1060 }
1061
1062 case SIOCSMIIREG:
1063 if (!capable(CAP_NET_ADMIN))
1064 return -EPERM;
1065
e07b1aa8 1066 spin_lock_bh(&sky2->phy_lock);
ef743d33 1067 err = gm_phy_write(hw, sky2->port, data->reg_num & 0x1f,
1068 data->val_in);
e07b1aa8 1069 spin_unlock_bh(&sky2->phy_lock);
ef743d33 1070 break;
1071 }
1072 return err;
1073}
1074
d1f13708 1075#ifdef SKY2_VLAN_TAG_USED
1076static void sky2_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
1077{
1078 struct sky2_port *sky2 = netdev_priv(dev);
1079 struct sky2_hw *hw = sky2->hw;
1080 u16 port = sky2->port;
d1f13708 1081
2bb8c262 1082 netif_tx_lock_bh(dev);
3d4e66f5 1083 netif_poll_disable(sky2->hw->dev[0]);
d1f13708 1084
d1f13708 1085 sky2->vlgrp = grp;
3d4e66f5
SH
1086 if (grp) {
1087 sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T),
1088 RX_VLAN_STRIP_ON);
1089 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
1090 TX_VLAN_TAG_ON);
1091 } else {
1092 sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T),
1093 RX_VLAN_STRIP_OFF);
1094 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T),
1095 TX_VLAN_TAG_OFF);
1096 }
d1f13708 1097
3d4e66f5 1098 netif_poll_enable(sky2->hw->dev[0]);
2bb8c262 1099 netif_tx_unlock_bh(dev);
d1f13708 1100}
1101#endif
1102
82788c7a 1103/*
14d0263f
SH
1104 * Allocate an skb for receiving. If the MTU is large enough
1105 * make the skb non-linear with a fragment list of pages.
1106 *
82788c7a
SH
1107 * It appears the hardware has a bug in the FIFO logic that
1108 * cause it to hang if the FIFO gets overrun and the receive buffer
497d7c86 1109 * is not 64 byte aligned. The buffer returned from netdev_alloc_skb is
1110 * aligned except if slab debugging is enabled.
82788c7a 1111 */
14d0263f 1112static struct sk_buff *sky2_rx_alloc(struct sky2_port *sky2)
82788c7a
SH
1113{
1114 struct sk_buff *skb;
14d0263f
SH
1115 unsigned long p;
1116 int i;
82788c7a 1117
14d0263f
SH
1118 skb = netdev_alloc_skb(sky2->netdev, sky2->rx_data_size + RX_SKB_ALIGN);
1119 if (!skb)
1120 goto nomem;
1121
1122 p = (unsigned long) skb->data;
1123 skb_reserve(skb, ALIGN(p, RX_SKB_ALIGN) - p);
1124
1125 for (i = 0; i < sky2->rx_nfrags; i++) {
1126 struct page *page = alloc_page(GFP_ATOMIC);
1127
1128 if (!page)
1129 goto free_partial;
1130 skb_fill_page_desc(skb, i, page, 0, PAGE_SIZE);
82788c7a
SH
1131 }
1132
1133 return skb;
14d0263f
SH
1134free_partial:
1135 kfree_skb(skb);
1136nomem:
1137 return NULL;
82788c7a
SH
1138}
1139
cd28ab6a
SH
1140/*
1141 * Allocate and setup receiver buffer pool.
14d0263f
SH
1142 * Normal case this ends up creating one list element for skb
1143 * in the receive ring. Worst case if using large MTU and each
1144 * allocation falls on a different 64 bit region, that results
1145 * in 6 list elements per ring entry.
1146 * One element is used for checksum enable/disable, and one
1147 * extra to avoid wrap.
cd28ab6a 1148 */
6b1a3aef 1149static int sky2_rx_start(struct sky2_port *sky2)
cd28ab6a 1150{
6b1a3aef 1151 struct sky2_hw *hw = sky2->hw;
14d0263f 1152 struct rx_ring_info *re;
6b1a3aef 1153 unsigned rxq = rxqaddr[sky2->port];
14d0263f 1154 unsigned i, size, space, thresh;
cd28ab6a 1155
6b1a3aef 1156 sky2->rx_put = sky2->rx_next = 0;
af4ed7e6 1157 sky2_qset(hw, rxq);
977bdf06 1158
c3905bc4
SH
1159 /* On PCI express lowering the watermark gives better performance */
1160 if (pci_find_capability(hw->pdev, PCI_CAP_ID_EXP))
1161 sky2_write32(hw, Q_ADDR(rxq, Q_WM), BMU_WM_PEX);
1162
1163 /* These chips have no ram buffer?
1164 * MAC Rx RAM Read is controlled by hardware */
8df9a876 1165 if (hw->chip_id == CHIP_ID_YUKON_EC_U &&
c3905bc4
SH
1166 (hw->chip_rev == CHIP_REV_YU_EC_U_A1
1167 || hw->chip_rev == CHIP_REV_YU_EC_U_B0))
f449c7c1 1168 sky2_write32(hw, Q_ADDR(rxq, Q_TEST), F_M_RX_RAM_DIS);
977bdf06 1169
6b1a3aef 1170 sky2_prefetch_init(hw, rxq, sky2->rx_le_map, RX_LE_SIZE - 1);
1171
1172 rx_set_checksum(sky2);
14d0263f
SH
1173
1174 /* Space needed for frame data + headers rounded up */
1175 size = ALIGN(sky2->netdev->mtu + ETH_HLEN + VLAN_HLEN, 8)
1176 + 8;
1177
1178 /* Stopping point for hardware truncation */
1179 thresh = (size - 8) / sizeof(u32);
1180
1181 /* Account for overhead of skb - to avoid order > 0 allocation */
1182 space = SKB_DATA_ALIGN(size) + NET_SKB_PAD
1183 + sizeof(struct skb_shared_info);
1184
1185 sky2->rx_nfrags = space >> PAGE_SHIFT;
1186 BUG_ON(sky2->rx_nfrags > ARRAY_SIZE(re->frag_addr));
1187
1188 if (sky2->rx_nfrags != 0) {
1189 /* Compute residue after pages */
1190 space = sky2->rx_nfrags << PAGE_SHIFT;
1191
1192 if (space < size)
1193 size -= space;
1194 else
1195 size = 0;
1196
1197 /* Optimize to handle small packets and headers */
1198 if (size < copybreak)
1199 size = copybreak;
1200 if (size < ETH_HLEN)
1201 size = ETH_HLEN;
1202 }
1203 sky2->rx_data_size = size;
1204
1205 /* Fill Rx ring */
793b883e 1206 for (i = 0; i < sky2->rx_pending; i++) {
14d0263f 1207 re = sky2->rx_ring + i;
cd28ab6a 1208
14d0263f 1209 re->skb = sky2_rx_alloc(sky2);
cd28ab6a
SH
1210 if (!re->skb)
1211 goto nomem;
1212
14d0263f
SH
1213 sky2_rx_map_skb(hw->pdev, re, sky2->rx_data_size);
1214 sky2_rx_submit(sky2, re);
cd28ab6a
SH
1215 }
1216
a1433ac4
SH
1217 /*
1218 * The receiver hangs if it receives frames larger than the
1219 * packet buffer. As a workaround, truncate oversize frames, but
1220 * the register is limited to 9 bits, so if you do frames > 2052
1221 * you better get the MTU right!
1222 */
a1433ac4
SH
1223 if (thresh > 0x1ff)
1224 sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T), RX_TRUNC_OFF);
1225 else {
1226 sky2_write16(hw, SK_REG(sky2->port, RX_GMF_TR_THR), thresh);
1227 sky2_write32(hw, SK_REG(sky2->port, RX_GMF_CTRL_T), RX_TRUNC_ON);
1228 }
1229
6b1a3aef 1230 /* Tell chip about available buffers */
50432cb5 1231 sky2_put_idx(hw, rxq, sky2->rx_put);
cd28ab6a
SH
1232 return 0;
1233nomem:
1234 sky2_rx_clean(sky2);
1235 return -ENOMEM;
1236}
1237
1238/* Bring up network interface. */
1239static int sky2_up(struct net_device *dev)
1240{
1241 struct sky2_port *sky2 = netdev_priv(dev);
1242 struct sky2_hw *hw = sky2->hw;
1243 unsigned port = sky2->port;
67712901 1244 u32 ramsize, imask;
ee7abb04 1245 int cap, err = -ENOMEM;
843a46f4 1246 struct net_device *otherdev = hw->dev[sky2->port^1];
cd28ab6a 1247
ee7abb04
SH
1248 /*
1249 * On dual port PCI-X card, there is an problem where status
1250 * can be received out of order due to split transactions
843a46f4 1251 */
ee7abb04
SH
1252 if (otherdev && netif_running(otherdev) &&
1253 (cap = pci_find_capability(hw->pdev, PCI_CAP_ID_PCIX))) {
1254 struct sky2_port *osky2 = netdev_priv(otherdev);
1255 u16 cmd;
1256
1257 cmd = sky2_pci_read16(hw, cap + PCI_X_CMD);
1258 cmd &= ~PCI_X_CMD_MAX_SPLIT;
1259 sky2_pci_write16(hw, cap + PCI_X_CMD, cmd);
1260
1261 sky2->rx_csum = 0;
1262 osky2->rx_csum = 0;
1263 }
843a46f4 1264
cd28ab6a
SH
1265 if (netif_msg_ifup(sky2))
1266 printk(KERN_INFO PFX "%s: enabling interface\n", dev->name);
1267
55d7b4e6
SH
1268 netif_carrier_off(dev);
1269
cd28ab6a
SH
1270 /* must be power of 2 */
1271 sky2->tx_le = pci_alloc_consistent(hw->pdev,
793b883e
SH
1272 TX_RING_SIZE *
1273 sizeof(struct sky2_tx_le),
cd28ab6a
SH
1274 &sky2->tx_le_map);
1275 if (!sky2->tx_le)
1276 goto err_out;
1277
6cdbbdf3 1278 sky2->tx_ring = kcalloc(TX_RING_SIZE, sizeof(struct tx_ring_info),
cd28ab6a
SH
1279 GFP_KERNEL);
1280 if (!sky2->tx_ring)
1281 goto err_out;
1282 sky2->tx_prod = sky2->tx_cons = 0;
cd28ab6a
SH
1283
1284 sky2->rx_le = pci_alloc_consistent(hw->pdev, RX_LE_BYTES,
1285 &sky2->rx_le_map);
1286 if (!sky2->rx_le)
1287 goto err_out;
1288 memset(sky2->rx_le, 0, RX_LE_BYTES);
1289
291ea614 1290 sky2->rx_ring = kcalloc(sky2->rx_pending, sizeof(struct rx_ring_info),
cd28ab6a
SH
1291 GFP_KERNEL);
1292 if (!sky2->rx_ring)
1293 goto err_out;
1294
d3bcfbeb 1295 sky2_phy_power(hw, port, 1);
1296
cd28ab6a
SH
1297 sky2_mac_init(hw, port);
1298
67712901
SH
1299 /* Register is number of 4K blocks on internal RAM buffer. */
1300 ramsize = sky2_read8(hw, B2_E_0) * 4;
1301 printk(KERN_INFO PFX "%s: ram buffer %dK\n", dev->name, ramsize);
1c28f6ba 1302
67712901
SH
1303 if (ramsize > 0) {
1304 u32 rxspace;
cd28ab6a 1305
67712901
SH
1306 if (ramsize < 16)
1307 rxspace = ramsize / 2;
1308 else
1309 rxspace = 8 + (2*(ramsize - 16))/3;
cd28ab6a 1310
67712901
SH
1311 sky2_ramset(hw, rxqaddr[port], 0, rxspace);
1312 sky2_ramset(hw, txqaddr[port], rxspace, ramsize - rxspace);
1313
1314 /* Make sure SyncQ is disabled */
1315 sky2_write8(hw, RB_ADDR(port == 0 ? Q_XS1 : Q_XS2, RB_CTRL),
1316 RB_RST_SET);
1317 }
793b883e 1318
af4ed7e6 1319 sky2_qset(hw, txqaddr[port]);
5a5b1ea0 1320
69161611
SH
1321 /* This is copied from sk98lin 10.0.5.3; no one tells me about erratta's */
1322 if (hw->chip_id == CHIP_ID_YUKON_EX && hw->chip_rev == CHIP_REV_YU_EX_B0)
1323 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_TEST), F_TX_CHK_AUTO_OFF);
1324
977bdf06 1325 /* Set almost empty threshold */
c2716fb4
SH
1326 if (hw->chip_id == CHIP_ID_YUKON_EC_U
1327 && hw->chip_rev == CHIP_REV_YU_EC_U_A0)
b628ed98 1328 sky2_write16(hw, Q_ADDR(txqaddr[port], Q_AL), ECU_TXFF_LEV);
5a5b1ea0 1329
6b1a3aef 1330 sky2_prefetch_init(hw, txqaddr[port], sky2->tx_le_map,
1331 TX_RING_SIZE - 1);
cd28ab6a 1332
6b1a3aef 1333 err = sky2_rx_start(sky2);
cd28ab6a
SH
1334 if (err)
1335 goto err_out;
1336
cd28ab6a 1337 /* Enable interrupts from phy/mac for port */
e07b1aa8 1338 imask = sky2_read32(hw, B0_IMSK);
f4ea431b 1339 imask |= portirq_msk[port];
e07b1aa8
SH
1340 sky2_write32(hw, B0_IMSK, imask);
1341
cd28ab6a
SH
1342 return 0;
1343
1344err_out:
1b537565 1345 if (sky2->rx_le) {
cd28ab6a
SH
1346 pci_free_consistent(hw->pdev, RX_LE_BYTES,
1347 sky2->rx_le, sky2->rx_le_map);
1b537565
SH
1348 sky2->rx_le = NULL;
1349 }
1350 if (sky2->tx_le) {
cd28ab6a
SH
1351 pci_free_consistent(hw->pdev,
1352 TX_RING_SIZE * sizeof(struct sky2_tx_le),
1353 sky2->tx_le, sky2->tx_le_map);
1b537565
SH
1354 sky2->tx_le = NULL;
1355 }
1356 kfree(sky2->tx_ring);
1357 kfree(sky2->rx_ring);
cd28ab6a 1358
1b537565
SH
1359 sky2->tx_ring = NULL;
1360 sky2->rx_ring = NULL;
cd28ab6a
SH
1361 return err;
1362}
1363
793b883e
SH
1364/* Modular subtraction in ring */
1365static inline int tx_dist(unsigned tail, unsigned head)
1366{
cb5d9547 1367 return (head - tail) & (TX_RING_SIZE - 1);
793b883e 1368}
cd28ab6a 1369
793b883e
SH
1370/* Number of list elements available for next tx */
1371static inline int tx_avail(const struct sky2_port *sky2)
cd28ab6a 1372{
793b883e 1373 return sky2->tx_pending - tx_dist(sky2->tx_cons, sky2->tx_prod);
cd28ab6a
SH
1374}
1375
793b883e 1376/* Estimate of number of transmit list elements required */
28bd181a 1377static unsigned tx_le_req(const struct sk_buff *skb)
cd28ab6a 1378{
793b883e
SH
1379 unsigned count;
1380
1381 count = sizeof(dma_addr_t) / sizeof(u32);
1382 count += skb_shinfo(skb)->nr_frags * count;
1383
89114afd 1384 if (skb_is_gso(skb))
793b883e
SH
1385 ++count;
1386
84fa7933 1387 if (skb->ip_summed == CHECKSUM_PARTIAL)
793b883e
SH
1388 ++count;
1389
1390 return count;
cd28ab6a
SH
1391}
1392
793b883e
SH
1393/*
1394 * Put one packet in ring for transmit.
1395 * A single packet can generate multiple list elements, and
1396 * the number of ring elements will probably be less than the number
1397 * of list elements used.
1398 */
cd28ab6a
SH
1399static int sky2_xmit_frame(struct sk_buff *skb, struct net_device *dev)
1400{
1401 struct sky2_port *sky2 = netdev_priv(dev);
1402 struct sky2_hw *hw = sky2->hw;
d1f13708 1403 struct sky2_tx_le *le = NULL;
6cdbbdf3 1404 struct tx_ring_info *re;
cd28ab6a
SH
1405 unsigned i, len;
1406 dma_addr_t mapping;
1407 u32 addr64;
1408 u16 mss;
1409 u8 ctrl;
1410
2bb8c262
SH
1411 if (unlikely(tx_avail(sky2) < tx_le_req(skb)))
1412 return NETDEV_TX_BUSY;
cd28ab6a 1413
793b883e 1414 if (unlikely(netif_msg_tx_queued(sky2)))
cd28ab6a
SH
1415 printk(KERN_DEBUG "%s: tx queued, slot %u, len %d\n",
1416 dev->name, sky2->tx_prod, skb->len);
1417
cd28ab6a
SH
1418 len = skb_headlen(skb);
1419 mapping = pci_map_single(hw->pdev, skb->data, len, PCI_DMA_TODEVICE);
a018e330 1420 addr64 = high32(mapping);
793b883e 1421
a018e330 1422 /* Send high bits if changed or crosses boundary */
1423 if (addr64 != sky2->tx_addr64 || high32(mapping + len) != sky2->tx_addr64) {
793b883e 1424 le = get_tx_le(sky2);
f65b138c 1425 le->addr = cpu_to_le32(addr64);
793b883e 1426 le->opcode = OP_ADDR64 | HW_OWNER;
a018e330 1427 sky2->tx_addr64 = high32(mapping + len);
793b883e 1428 }
cd28ab6a
SH
1429
1430 /* Check for TCP Segmentation Offload */
7967168c 1431 mss = skb_shinfo(skb)->gso_size;
793b883e 1432 if (mss != 0) {
69161611
SH
1433 if (hw->chip_id != CHIP_ID_YUKON_EX)
1434 mss += ETH_HLEN + ip_hdrlen(skb) + tcp_hdrlen(skb);
1435
1436 if (mss != sky2->tx_last_mss) {
1437 le = get_tx_le(sky2);
1438 le->addr = cpu_to_le32(mss);
1439 if (hw->chip_id == CHIP_ID_YUKON_EX)
1440 le->opcode = OP_MSS | HW_OWNER;
1441 else
1442 le->opcode = OP_LRGLEN | HW_OWNER;
e07560cd 1443 sky2->tx_last_mss = mss;
1444 }
cd28ab6a
SH
1445 }
1446
cd28ab6a 1447 ctrl = 0;
d1f13708 1448#ifdef SKY2_VLAN_TAG_USED
1449 /* Add VLAN tag, can piggyback on LRGLEN or ADDR64 */
1450 if (sky2->vlgrp && vlan_tx_tag_present(skb)) {
1451 if (!le) {
1452 le = get_tx_le(sky2);
f65b138c 1453 le->addr = 0;
d1f13708 1454 le->opcode = OP_VLAN|HW_OWNER;
d1f13708 1455 } else
1456 le->opcode |= OP_VLAN;
1457 le->length = cpu_to_be16(vlan_tx_tag_get(skb));
1458 ctrl |= INS_VLAN;
1459 }
1460#endif
1461
1462 /* Handle TCP checksum offload */
84fa7933 1463 if (skb->ip_summed == CHECKSUM_PARTIAL) {
69161611
SH
1464 /* On Yukon EX (some versions) encoding change. */
1465 if (hw->chip_id == CHIP_ID_YUKON_EX
1466 && hw->chip_rev != CHIP_REV_YU_EX_B0)
1467 ctrl |= CALSUM; /* auto checksum */
1468 else {
1469 const unsigned offset = skb_transport_offset(skb);
1470 u32 tcpsum;
1471
1472 tcpsum = offset << 16; /* sum start */
1473 tcpsum |= offset + skb->csum_offset; /* sum write */
1474
1475 ctrl |= CALSUM | WR_SUM | INIT_SUM | LOCK_SUM;
1476 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1477 ctrl |= UDPTCP;
1478
1479 if (tcpsum != sky2->tx_tcpsum) {
1480 sky2->tx_tcpsum = tcpsum;
1481
1482 le = get_tx_le(sky2);
1483 le->addr = cpu_to_le32(tcpsum);
1484 le->length = 0; /* initial checksum value */
1485 le->ctrl = 1; /* one packet */
1486 le->opcode = OP_TCPLISW | HW_OWNER;
1487 }
1d179332 1488 }
cd28ab6a
SH
1489 }
1490
1491 le = get_tx_le(sky2);
f65b138c 1492 le->addr = cpu_to_le32((u32) mapping);
cd28ab6a
SH
1493 le->length = cpu_to_le16(len);
1494 le->ctrl = ctrl;
793b883e 1495 le->opcode = mss ? (OP_LARGESEND | HW_OWNER) : (OP_PACKET | HW_OWNER);
cd28ab6a 1496
291ea614 1497 re = tx_le_re(sky2, le);
cd28ab6a 1498 re->skb = skb;
6cdbbdf3 1499 pci_unmap_addr_set(re, mapaddr, mapping);
291ea614 1500 pci_unmap_len_set(re, maplen, len);
cd28ab6a
SH
1501
1502 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
291ea614 1503 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
cd28ab6a
SH
1504
1505 mapping = pci_map_page(hw->pdev, frag->page, frag->page_offset,
1506 frag->size, PCI_DMA_TODEVICE);
a036119f 1507 addr64 = high32(mapping);
793b883e
SH
1508 if (addr64 != sky2->tx_addr64) {
1509 le = get_tx_le(sky2);
f65b138c 1510 le->addr = cpu_to_le32(addr64);
793b883e
SH
1511 le->ctrl = 0;
1512 le->opcode = OP_ADDR64 | HW_OWNER;
1513 sky2->tx_addr64 = addr64;
cd28ab6a
SH
1514 }
1515
1516 le = get_tx_le(sky2);
f65b138c 1517 le->addr = cpu_to_le32((u32) mapping);
cd28ab6a
SH
1518 le->length = cpu_to_le16(frag->size);
1519 le->ctrl = ctrl;
793b883e 1520 le->opcode = OP_BUFFER | HW_OWNER;
cd28ab6a 1521
291ea614
SH
1522 re = tx_le_re(sky2, le);
1523 re->skb = skb;
1524 pci_unmap_addr_set(re, mapaddr, mapping);
1525 pci_unmap_len_set(re, maplen, frag->size);
cd28ab6a 1526 }
6cdbbdf3 1527
cd28ab6a
SH
1528 le->ctrl |= EOP;
1529
97bda706 1530 if (tx_avail(sky2) <= MAX_SKB_TX_LE)
1531 netif_stop_queue(dev);
b19666d9 1532
290d4de5 1533 sky2_put_idx(hw, txqaddr[sky2->port], sky2->tx_prod);
cd28ab6a 1534
cd28ab6a
SH
1535 dev->trans_start = jiffies;
1536 return NETDEV_TX_OK;
1537}
1538
cd28ab6a 1539/*
793b883e
SH
1540 * Free ring elements from starting at tx_cons until "done"
1541 *
1542 * NB: the hardware will tell us about partial completion of multi-part
291ea614 1543 * buffers so make sure not to free skb to early.
cd28ab6a 1544 */
d11c13e7 1545static void sky2_tx_complete(struct sky2_port *sky2, u16 done)
cd28ab6a 1546{
d11c13e7 1547 struct net_device *dev = sky2->netdev;
af2a58ac 1548 struct pci_dev *pdev = sky2->hw->pdev;
291ea614 1549 unsigned idx;
cd28ab6a 1550
0e3ff6aa 1551 BUG_ON(done >= TX_RING_SIZE);
2224795d 1552
291ea614
SH
1553 for (idx = sky2->tx_cons; idx != done;
1554 idx = RING_NEXT(idx, TX_RING_SIZE)) {
1555 struct sky2_tx_le *le = sky2->tx_le + idx;
1556 struct tx_ring_info *re = sky2->tx_ring + idx;
1557
1558 switch(le->opcode & ~HW_OWNER) {
1559 case OP_LARGESEND:
1560 case OP_PACKET:
1561 pci_unmap_single(pdev,
1562 pci_unmap_addr(re, mapaddr),
1563 pci_unmap_len(re, maplen),
1564 PCI_DMA_TODEVICE);
af2a58ac 1565 break;
291ea614
SH
1566 case OP_BUFFER:
1567 pci_unmap_page(pdev, pci_unmap_addr(re, mapaddr),
1568 pci_unmap_len(re, maplen),
734d1868 1569 PCI_DMA_TODEVICE);
291ea614
SH
1570 break;
1571 }
1572
1573 if (le->ctrl & EOP) {
1574 if (unlikely(netif_msg_tx_done(sky2)))
1575 printk(KERN_DEBUG "%s: tx done %u\n",
1576 dev->name, idx);
2bf56fe2 1577 sky2->net_stats.tx_packets++;
1578 sky2->net_stats.tx_bytes += re->skb->len;
1579
794b2bd2 1580 dev_kfree_skb_any(re->skb);
cd28ab6a
SH
1581 }
1582
291ea614 1583 le->opcode = 0; /* paranoia */
793b883e 1584 }
793b883e 1585
291ea614 1586 sky2->tx_cons = idx;
50432cb5
SH
1587 smp_mb();
1588
22e11703 1589 if (tx_avail(sky2) > MAX_SKB_TX_LE + 4)
cd28ab6a 1590 netif_wake_queue(dev);
cd28ab6a
SH
1591}
1592
1593/* Cleanup all untransmitted buffers, assume transmitter not running */
2bb8c262 1594static void sky2_tx_clean(struct net_device *dev)
cd28ab6a 1595{
2bb8c262
SH
1596 struct sky2_port *sky2 = netdev_priv(dev);
1597
1598 netif_tx_lock_bh(dev);
d11c13e7 1599 sky2_tx_complete(sky2, sky2->tx_prod);
2bb8c262 1600 netif_tx_unlock_bh(dev);
cd28ab6a
SH
1601}
1602
1603/* Network shutdown */
1604static int sky2_down(struct net_device *dev)
1605{
1606 struct sky2_port *sky2 = netdev_priv(dev);
1607 struct sky2_hw *hw = sky2->hw;
1608 unsigned port = sky2->port;
1609 u16 ctrl;
e07b1aa8 1610 u32 imask;
cd28ab6a 1611
1b537565
SH
1612 /* Never really got started! */
1613 if (!sky2->tx_le)
1614 return 0;
1615
cd28ab6a
SH
1616 if (netif_msg_ifdown(sky2))
1617 printk(KERN_INFO PFX "%s: disabling interface\n", dev->name);
1618
018d1c66 1619 /* Stop more packets from being queued */
cd28ab6a
SH
1620 netif_stop_queue(dev);
1621
ebc646f6
SH
1622 /* Disable port IRQ */
1623 imask = sky2_read32(hw, B0_IMSK);
1624 imask &= ~portirq_msk[port];
1625 sky2_write32(hw, B0_IMSK, imask);
1626
d3bcfbeb 1627 sky2_gmac_reset(hw, port);
793b883e 1628
cd28ab6a
SH
1629 /* Stop transmitter */
1630 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_STOP);
1631 sky2_read32(hw, Q_ADDR(txqaddr[port], Q_CSR));
1632
1633 sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL),
793b883e 1634 RB_RST_SET | RB_DIS_OP_MD);
cd28ab6a
SH
1635
1636 ctrl = gma_read16(hw, port, GM_GP_CTRL);
793b883e 1637 ctrl &= ~(GM_GPCR_TX_ENA | GM_GPCR_RX_ENA);
cd28ab6a
SH
1638 gma_write16(hw, port, GM_GP_CTRL, ctrl);
1639
1640 sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
1641
1642 /* Workaround shared GMAC reset */
793b883e
SH
1643 if (!(hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0
1644 && port == 0 && hw->dev[1] && netif_running(hw->dev[1])))
cd28ab6a
SH
1645 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
1646
1647 /* Disable Force Sync bit and Enable Alloc bit */
1648 sky2_write8(hw, SK_REG(port, TXA_CTRL),
1649 TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
1650
1651 /* Stop Interval Timer and Limit Counter of Tx Arbiter */
1652 sky2_write32(hw, SK_REG(port, TXA_ITI_INI), 0L);
1653 sky2_write32(hw, SK_REG(port, TXA_LIM_INI), 0L);
1654
1655 /* Reset the PCI FIFO of the async Tx queue */
793b883e
SH
1656 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR),
1657 BMU_RST_SET | BMU_FIFO_RST);
cd28ab6a
SH
1658
1659 /* Reset the Tx prefetch units */
1660 sky2_write32(hw, Y2_QADDR(txqaddr[port], PREF_UNIT_CTRL),
1661 PREF_UNIT_RST_SET);
1662
1663 sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL), RB_RST_SET);
1664
6b1a3aef 1665 sky2_rx_stop(sky2);
cd28ab6a
SH
1666
1667 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
1668 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_SET);
1669
d3bcfbeb 1670 sky2_phy_power(hw, port, 0);
1671
55d7b4e6
SH
1672 netif_carrier_off(dev);
1673
d571b694 1674 /* turn off LED's */
cd28ab6a
SH
1675 sky2_write16(hw, B0_Y2LED, LED_STAT_OFF);
1676
018d1c66 1677 synchronize_irq(hw->pdev->irq);
1678
2bb8c262 1679 sky2_tx_clean(dev);
cd28ab6a
SH
1680 sky2_rx_clean(sky2);
1681
1682 pci_free_consistent(hw->pdev, RX_LE_BYTES,
1683 sky2->rx_le, sky2->rx_le_map);
1684 kfree(sky2->rx_ring);
1685
1686 pci_free_consistent(hw->pdev,
1687 TX_RING_SIZE * sizeof(struct sky2_tx_le),
1688 sky2->tx_le, sky2->tx_le_map);
1689 kfree(sky2->tx_ring);
1690
1b537565
SH
1691 sky2->tx_le = NULL;
1692 sky2->rx_le = NULL;
1693
1694 sky2->rx_ring = NULL;
1695 sky2->tx_ring = NULL;
1696
cd28ab6a
SH
1697 return 0;
1698}
1699
1700static u16 sky2_phy_speed(const struct sky2_hw *hw, u16 aux)
1701{
b89165f2 1702 if (!sky2_is_copper(hw))
793b883e
SH
1703 return SPEED_1000;
1704
cd28ab6a
SH
1705 if (hw->chip_id == CHIP_ID_YUKON_FE)
1706 return (aux & PHY_M_PS_SPEED_100) ? SPEED_100 : SPEED_10;
1707
1708 switch (aux & PHY_M_PS_SPEED_MSK) {
1709 case PHY_M_PS_SPEED_1000:
1710 return SPEED_1000;
1711 case PHY_M_PS_SPEED_100:
1712 return SPEED_100;
1713 default:
1714 return SPEED_10;
1715 }
1716}
1717
1718static void sky2_link_up(struct sky2_port *sky2)
1719{
1720 struct sky2_hw *hw = sky2->hw;
1721 unsigned port = sky2->port;
1722 u16 reg;
16ad91e1
SH
1723 static const char *fc_name[] = {
1724 [FC_NONE] = "none",
1725 [FC_TX] = "tx",
1726 [FC_RX] = "rx",
1727 [FC_BOTH] = "both",
1728 };
cd28ab6a 1729
cd28ab6a 1730 /* enable Rx/Tx */
2eaba1a2 1731 reg = gma_read16(hw, port, GM_GP_CTRL);
cd28ab6a
SH
1732 reg |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
1733 gma_write16(hw, port, GM_GP_CTRL, reg);
cd28ab6a
SH
1734
1735 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
1736
1737 netif_carrier_on(sky2->netdev);
cd28ab6a
SH
1738
1739 /* Turn on link LED */
793b883e 1740 sky2_write8(hw, SK_REG(port, LNK_LED_REG),
cd28ab6a
SH
1741 LINKLED_ON | LINKLED_BLINK_OFF | LINKLED_LINKSYNC_OFF);
1742
93745494
SH
1743 if (hw->chip_id == CHIP_ID_YUKON_XL
1744 || hw->chip_id == CHIP_ID_YUKON_EC_U
1745 || hw->chip_id == CHIP_ID_YUKON_EX) {
793b883e 1746 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
ed6d32c7
SH
1747 u16 led = PHY_M_LEDC_LOS_CTRL(1); /* link active */
1748
1749 switch(sky2->speed) {
1750 case SPEED_10:
1751 led |= PHY_M_LEDC_INIT_CTRL(7);
1752 break;
1753
1754 case SPEED_100:
1755 led |= PHY_M_LEDC_STA1_CTRL(7);
1756 break;
1757
1758 case SPEED_1000:
1759 led |= PHY_M_LEDC_STA0_CTRL(7);
1760 break;
1761 }
793b883e
SH
1762
1763 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
ed6d32c7 1764 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, led);
793b883e
SH
1765 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
1766 }
1767
cd28ab6a
SH
1768 if (netif_msg_link(sky2))
1769 printk(KERN_INFO PFX
d571b694 1770 "%s: Link is up at %d Mbps, %s duplex, flow control %s\n",
cd28ab6a
SH
1771 sky2->netdev->name, sky2->speed,
1772 sky2->duplex == DUPLEX_FULL ? "full" : "half",
16ad91e1 1773 fc_name[sky2->flow_status]);
cd28ab6a
SH
1774}
1775
1776static void sky2_link_down(struct sky2_port *sky2)
1777{
1778 struct sky2_hw *hw = sky2->hw;
1779 unsigned port = sky2->port;
1780 u16 reg;
1781
1782 gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);
1783
1784 reg = gma_read16(hw, port, GM_GP_CTRL);
1785 reg &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
1786 gma_write16(hw, port, GM_GP_CTRL, reg);
cd28ab6a 1787
cd28ab6a 1788 netif_carrier_off(sky2->netdev);
cd28ab6a
SH
1789
1790 /* Turn on link LED */
1791 sky2_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_OFF);
1792
1793 if (netif_msg_link(sky2))
1794 printk(KERN_INFO PFX "%s: Link is down.\n", sky2->netdev->name);
2eaba1a2 1795
cd28ab6a
SH
1796 sky2_phy_init(hw, port);
1797}
1798
16ad91e1
SH
1799static enum flow_control sky2_flow(int rx, int tx)
1800{
1801 if (rx)
1802 return tx ? FC_BOTH : FC_RX;
1803 else
1804 return tx ? FC_TX : FC_NONE;
1805}
1806
793b883e
SH
1807static int sky2_autoneg_done(struct sky2_port *sky2, u16 aux)
1808{
1809 struct sky2_hw *hw = sky2->hw;
1810 unsigned port = sky2->port;
da4c1ff4 1811 u16 advert, lpa;
793b883e 1812
da4c1ff4 1813 advert = gm_phy_read(hw, port, PHY_MARV_AUNE_ADV);
793b883e 1814 lpa = gm_phy_read(hw, port, PHY_MARV_AUNE_LP);
793b883e
SH
1815 if (lpa & PHY_M_AN_RF) {
1816 printk(KERN_ERR PFX "%s: remote fault", sky2->netdev->name);
1817 return -1;
1818 }
1819
793b883e
SH
1820 if (!(aux & PHY_M_PS_SPDUP_RES)) {
1821 printk(KERN_ERR PFX "%s: speed/duplex mismatch",
1822 sky2->netdev->name);
1823 return -1;
1824 }
1825
793b883e 1826 sky2->speed = sky2_phy_speed(hw, aux);
7c74ac1c 1827 sky2->duplex = (aux & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
793b883e 1828
da4c1ff4
SH
1829 /* Since the pause result bits seem to in different positions on
1830 * different chips. look at registers.
1831 */
1832 if (!sky2_is_copper(hw)) {
1833 /* Shift for bits in fiber PHY */
1834 advert &= ~(ADVERTISE_PAUSE_CAP|ADVERTISE_PAUSE_ASYM);
1835 lpa &= ~(LPA_PAUSE_CAP|LPA_PAUSE_ASYM);
1836
1837 if (advert & ADVERTISE_1000XPAUSE)
1838 advert |= ADVERTISE_PAUSE_CAP;
1839 if (advert & ADVERTISE_1000XPSE_ASYM)
1840 advert |= ADVERTISE_PAUSE_ASYM;
1841 if (lpa & LPA_1000XPAUSE)
1842 lpa |= LPA_PAUSE_CAP;
1843 if (lpa & LPA_1000XPAUSE_ASYM)
1844 lpa |= LPA_PAUSE_ASYM;
1845 }
793b883e 1846
da4c1ff4
SH
1847 sky2->flow_status = FC_NONE;
1848 if (advert & ADVERTISE_PAUSE_CAP) {
1849 if (lpa & LPA_PAUSE_CAP)
1850 sky2->flow_status = FC_BOTH;
1851 else if (advert & ADVERTISE_PAUSE_ASYM)
1852 sky2->flow_status = FC_RX;
1853 } else if (advert & ADVERTISE_PAUSE_ASYM) {
1854 if ((lpa & LPA_PAUSE_CAP) && (lpa & LPA_PAUSE_ASYM))
1855 sky2->flow_status = FC_TX;
1856 }
793b883e 1857
16ad91e1 1858 if (sky2->duplex == DUPLEX_HALF && sky2->speed < SPEED_1000
93745494 1859 && !(hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX))
16ad91e1 1860 sky2->flow_status = FC_NONE;
2eaba1a2 1861
da4c1ff4 1862 if (sky2->flow_status & FC_TX)
793b883e
SH
1863 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
1864 else
1865 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
1866
1867 return 0;
1868}
cd28ab6a 1869
e07b1aa8
SH
1870/* Interrupt from PHY */
1871static void sky2_phy_intr(struct sky2_hw *hw, unsigned port)
cd28ab6a 1872{
e07b1aa8
SH
1873 struct net_device *dev = hw->dev[port];
1874 struct sky2_port *sky2 = netdev_priv(dev);
cd28ab6a
SH
1875 u16 istatus, phystat;
1876
ebc646f6
SH
1877 if (!netif_running(dev))
1878 return;
1879
e07b1aa8
SH
1880 spin_lock(&sky2->phy_lock);
1881 istatus = gm_phy_read(hw, port, PHY_MARV_INT_STAT);
1882 phystat = gm_phy_read(hw, port, PHY_MARV_PHY_STAT);
1883
cd28ab6a
SH
1884 if (netif_msg_intr(sky2))
1885 printk(KERN_INFO PFX "%s: phy interrupt status 0x%x 0x%x\n",
1886 sky2->netdev->name, istatus, phystat);
1887
2eaba1a2 1888 if (sky2->autoneg == AUTONEG_ENABLE && (istatus & PHY_M_IS_AN_COMPL)) {
793b883e
SH
1889 if (sky2_autoneg_done(sky2, phystat) == 0)
1890 sky2_link_up(sky2);
1891 goto out;
1892 }
cd28ab6a 1893
793b883e
SH
1894 if (istatus & PHY_M_IS_LSP_CHANGE)
1895 sky2->speed = sky2_phy_speed(hw, phystat);
cd28ab6a 1896
793b883e
SH
1897 if (istatus & PHY_M_IS_DUP_CHANGE)
1898 sky2->duplex =
1899 (phystat & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
cd28ab6a 1900
793b883e
SH
1901 if (istatus & PHY_M_IS_LST_CHANGE) {
1902 if (phystat & PHY_M_PS_LINK_UP)
cd28ab6a 1903 sky2_link_up(sky2);
793b883e
SH
1904 else
1905 sky2_link_down(sky2);
cd28ab6a 1906 }
793b883e 1907out:
e07b1aa8 1908 spin_unlock(&sky2->phy_lock);
cd28ab6a
SH
1909}
1910
62335ab0 1911/* Transmit timeout is only called if we are running, carrier is up
302d1252
SH
1912 * and tx queue is full (stopped).
1913 */
cd28ab6a
SH
1914static void sky2_tx_timeout(struct net_device *dev)
1915{
1916 struct sky2_port *sky2 = netdev_priv(dev);
8cc048e3 1917 struct sky2_hw *hw = sky2->hw;
cd28ab6a
SH
1918
1919 if (netif_msg_timer(sky2))
1920 printk(KERN_ERR PFX "%s: tx timeout\n", dev->name);
1921
8f24664d 1922 printk(KERN_DEBUG PFX "%s: transmit ring %u .. %u report=%u done=%u\n",
62335ab0
SH
1923 dev->name, sky2->tx_cons, sky2->tx_prod,
1924 sky2_read16(hw, sky2->port == 0 ? STAT_TXA1_RIDX : STAT_TXA2_RIDX),
1925 sky2_read16(hw, Q_ADDR(txqaddr[sky2->port], Q_DONE)));
8f24664d 1926
81906791
SH
1927 /* can't restart safely under softirq */
1928 schedule_work(&hw->restart_work);
cd28ab6a
SH
1929}
1930
1931static int sky2_change_mtu(struct net_device *dev, int new_mtu)
1932{
6b1a3aef 1933 struct sky2_port *sky2 = netdev_priv(dev);
1934 struct sky2_hw *hw = sky2->hw;
b628ed98 1935 unsigned port = sky2->port;
6b1a3aef 1936 int err;
1937 u16 ctl, mode;
e07b1aa8 1938 u32 imask;
cd28ab6a
SH
1939
1940 if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
1941 return -EINVAL;
1942
d2adf4f6
SH
1943 if (new_mtu > ETH_DATA_LEN && hw->chip_id == CHIP_ID_YUKON_FE)
1944 return -EINVAL;
1945
6b1a3aef 1946 if (!netif_running(dev)) {
1947 dev->mtu = new_mtu;
1948 return 0;
1949 }
1950
e07b1aa8 1951 imask = sky2_read32(hw, B0_IMSK);
6b1a3aef 1952 sky2_write32(hw, B0_IMSK, 0);
1953
018d1c66 1954 dev->trans_start = jiffies; /* prevent tx timeout */
1955 netif_stop_queue(dev);
1956 netif_poll_disable(hw->dev[0]);
1957
e07b1aa8
SH
1958 synchronize_irq(hw->pdev->irq);
1959
69161611
SH
1960 if (hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX)
1961 sky2_set_tx_stfwd(hw, port);
b628ed98
SH
1962
1963 ctl = gma_read16(hw, port, GM_GP_CTRL);
1964 gma_write16(hw, port, GM_GP_CTRL, ctl & ~GM_GPCR_RX_ENA);
6b1a3aef 1965 sky2_rx_stop(sky2);
1966 sky2_rx_clean(sky2);
cd28ab6a
SH
1967
1968 dev->mtu = new_mtu;
14d0263f 1969
6b1a3aef 1970 mode = DATA_BLIND_VAL(DATA_BLIND_DEF) |
1971 GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF);
1972
1973 if (dev->mtu > ETH_DATA_LEN)
1974 mode |= GM_SMOD_JUMBO_ENA;
1975
b628ed98 1976 gma_write16(hw, port, GM_SERIAL_MODE, mode);
cd28ab6a 1977
b628ed98 1978 sky2_write8(hw, RB_ADDR(rxqaddr[port], RB_CTRL), RB_ENA_OP_MD);
cd28ab6a 1979
6b1a3aef 1980 err = sky2_rx_start(sky2);
e07b1aa8 1981 sky2_write32(hw, B0_IMSK, imask);
018d1c66 1982
1b537565
SH
1983 if (err)
1984 dev_close(dev);
1985 else {
b628ed98 1986 gma_write16(hw, port, GM_GP_CTRL, ctl);
1b537565
SH
1987
1988 netif_poll_enable(hw->dev[0]);
1989 netif_wake_queue(dev);
1990 }
1991
cd28ab6a
SH
1992 return err;
1993}
1994
14d0263f
SH
1995/* For small just reuse existing skb for next receive */
1996static struct sk_buff *receive_copy(struct sky2_port *sky2,
1997 const struct rx_ring_info *re,
1998 unsigned length)
1999{
2000 struct sk_buff *skb;
2001
2002 skb = netdev_alloc_skb(sky2->netdev, length + 2);
2003 if (likely(skb)) {
2004 skb_reserve(skb, 2);
2005 pci_dma_sync_single_for_cpu(sky2->hw->pdev, re->data_addr,
2006 length, PCI_DMA_FROMDEVICE);
d626f62b 2007 skb_copy_from_linear_data(re->skb, skb->data, length);
14d0263f
SH
2008 skb->ip_summed = re->skb->ip_summed;
2009 skb->csum = re->skb->csum;
2010 pci_dma_sync_single_for_device(sky2->hw->pdev, re->data_addr,
2011 length, PCI_DMA_FROMDEVICE);
2012 re->skb->ip_summed = CHECKSUM_NONE;
489b10c1 2013 skb_put(skb, length);
14d0263f
SH
2014 }
2015 return skb;
2016}
2017
2018/* Adjust length of skb with fragments to match received data */
2019static void skb_put_frags(struct sk_buff *skb, unsigned int hdr_space,
2020 unsigned int length)
2021{
2022 int i, num_frags;
2023 unsigned int size;
2024
2025 /* put header into skb */
2026 size = min(length, hdr_space);
2027 skb->tail += size;
2028 skb->len += size;
2029 length -= size;
2030
2031 num_frags = skb_shinfo(skb)->nr_frags;
2032 for (i = 0; i < num_frags; i++) {
2033 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2034
2035 if (length == 0) {
2036 /* don't need this page */
2037 __free_page(frag->page);
2038 --skb_shinfo(skb)->nr_frags;
2039 } else {
2040 size = min(length, (unsigned) PAGE_SIZE);
2041
2042 frag->size = size;
2043 skb->data_len += size;
2044 skb->truesize += size;
2045 skb->len += size;
2046 length -= size;
2047 }
2048 }
2049}
2050
2051/* Normal packet - take skb from ring element and put in a new one */
2052static struct sk_buff *receive_new(struct sky2_port *sky2,
2053 struct rx_ring_info *re,
2054 unsigned int length)
2055{
2056 struct sk_buff *skb, *nskb;
2057 unsigned hdr_space = sky2->rx_data_size;
2058
2059 pr_debug(PFX "receive new length=%d\n", length);
2060
2061 /* Don't be tricky about reusing pages (yet) */
2062 nskb = sky2_rx_alloc(sky2);
2063 if (unlikely(!nskb))
2064 return NULL;
2065
2066 skb = re->skb;
2067 sky2_rx_unmap_skb(sky2->hw->pdev, re);
2068
2069 prefetch(skb->data);
2070 re->skb = nskb;
2071 sky2_rx_map_skb(sky2->hw->pdev, re, hdr_space);
2072
2073 if (skb_shinfo(skb)->nr_frags)
2074 skb_put_frags(skb, hdr_space, length);
2075 else
489b10c1 2076 skb_put(skb, length);
14d0263f
SH
2077 return skb;
2078}
2079
cd28ab6a
SH
2080/*
2081 * Receive one packet.
d571b694 2082 * For larger packets, get new buffer.
cd28ab6a 2083 */
497d7c86 2084static struct sk_buff *sky2_receive(struct net_device *dev,
cd28ab6a
SH
2085 u16 length, u32 status)
2086{
497d7c86 2087 struct sky2_port *sky2 = netdev_priv(dev);
291ea614 2088 struct rx_ring_info *re = sky2->rx_ring + sky2->rx_next;
79e57d32 2089 struct sk_buff *skb = NULL;
cd28ab6a
SH
2090
2091 if (unlikely(netif_msg_rx_status(sky2)))
2092 printk(KERN_DEBUG PFX "%s: rx slot %u status 0x%x len %d\n",
497d7c86 2093 dev->name, sky2->rx_next, status, length);
cd28ab6a 2094
793b883e 2095 sky2->rx_next = (sky2->rx_next + 1) % sky2->rx_pending;
d70cd51a 2096 prefetch(sky2->rx_ring + sky2->rx_next);
cd28ab6a 2097
42eeea01 2098 if (status & GMR_FS_ANY_ERR)
cd28ab6a
SH
2099 goto error;
2100
42eeea01 2101 if (!(status & GMR_FS_RX_OK))
2102 goto resubmit;
2103
14d0263f
SH
2104 if (length < copybreak)
2105 skb = receive_copy(sky2, re, length);
2106 else
2107 skb = receive_new(sky2, re, length);
793b883e 2108resubmit:
14d0263f 2109 sky2_rx_submit(sky2, re);
79e57d32 2110
cd28ab6a
SH
2111 return skb;
2112
2113error:
6e15b712 2114 ++sky2->net_stats.rx_errors;
b6d77734 2115 if (status & GMR_FS_RX_FF_OV) {
a79abdc6 2116 sky2->net_stats.rx_over_errors++;
b6d77734
SH
2117 goto resubmit;
2118 }
6e15b712 2119
3be92a70 2120 if (netif_msg_rx_err(sky2) && net_ratelimit())
cd28ab6a 2121 printk(KERN_INFO PFX "%s: rx error, status 0x%x length %d\n",
497d7c86 2122 dev->name, status, length);
793b883e
SH
2123
2124 if (status & (GMR_FS_LONG_ERR | GMR_FS_UN_SIZE))
cd28ab6a
SH
2125 sky2->net_stats.rx_length_errors++;
2126 if (status & GMR_FS_FRAGMENT)
2127 sky2->net_stats.rx_frame_errors++;
2128 if (status & GMR_FS_CRC_ERR)
2129 sky2->net_stats.rx_crc_errors++;
79e57d32 2130
793b883e 2131 goto resubmit;
cd28ab6a
SH
2132}
2133
e07b1aa8
SH
2134/* Transmit complete */
2135static inline void sky2_tx_done(struct net_device *dev, u16 last)
13b97b74 2136{
e07b1aa8 2137 struct sky2_port *sky2 = netdev_priv(dev);
302d1252 2138
e07b1aa8 2139 if (netif_running(dev)) {
2bb8c262 2140 netif_tx_lock(dev);
e07b1aa8 2141 sky2_tx_complete(sky2, last);
2bb8c262 2142 netif_tx_unlock(dev);
2224795d 2143 }
cd28ab6a
SH
2144}
2145
e07b1aa8
SH
2146/* Process status response ring */
2147static int sky2_status_intr(struct sky2_hw *hw, int to_do)
cd28ab6a 2148{
22e11703 2149 struct sky2_port *sky2;
e07b1aa8 2150 int work_done = 0;
22e11703 2151 unsigned buf_write[2] = { 0, 0 };
e71ebd73 2152 u16 hwidx = sky2_read16(hw, STAT_PUT_IDX);
a8fd6266 2153
af2a58ac 2154 rmb();
bea86103 2155
e71ebd73 2156 while (hw->st_idx != hwidx) {
13210ce5 2157 struct sky2_status_le *le = hw->st_le + hw->st_idx;
69161611 2158 unsigned port = le->css & CSS_LINK_BIT;
13210ce5 2159 struct net_device *dev;
cd28ab6a 2160 struct sk_buff *skb;
cd28ab6a
SH
2161 u32 status;
2162 u16 length;
2163
cb5d9547 2164 hw->st_idx = RING_NEXT(hw->st_idx, STATUS_RING_SIZE);
bea86103 2165
69161611 2166 dev = hw->dev[port];
13210ce5 2167 sky2 = netdev_priv(dev);
f65b138c
SH
2168 length = le16_to_cpu(le->length);
2169 status = le32_to_cpu(le->status);
cd28ab6a 2170
e71ebd73 2171 switch (le->opcode & ~HW_OWNER) {
cd28ab6a 2172 case OP_RXSTAT:
497d7c86 2173 skb = sky2_receive(dev, length, status);
3225b919
SH
2174 if (unlikely(!skb)) {
2175 sky2->net_stats.rx_dropped++;
5df79111 2176 goto force_update;
3225b919 2177 }
13210ce5 2178
69161611
SH
2179 /* This chip reports checksum status differently */
2180 if (hw->chip_id == CHIP_ID_YUKON_EX) {
2181 if (sky2->rx_csum &&
2182 (le->css & (CSS_ISIPV4 | CSS_ISIPV6)) &&
2183 (le->css & CSS_TCPUDPCSOK))
2184 skb->ip_summed = CHECKSUM_UNNECESSARY;
2185 else
2186 skb->ip_summed = CHECKSUM_NONE;
2187 }
2188
13210ce5 2189 skb->protocol = eth_type_trans(skb, dev);
2bf56fe2 2190 sky2->net_stats.rx_packets++;
2191 sky2->net_stats.rx_bytes += skb->len;
13210ce5 2192 dev->last_rx = jiffies;
2193
d1f13708 2194#ifdef SKY2_VLAN_TAG_USED
2195 if (sky2->vlgrp && (status & GMR_FS_VLAN)) {
2196 vlan_hwaccel_receive_skb(skb,
2197 sky2->vlgrp,
2198 be16_to_cpu(sky2->rx_tag));
2199 } else
2200#endif
cd28ab6a 2201 netif_receive_skb(skb);
13210ce5 2202
22e11703 2203 /* Update receiver after 16 frames */
69161611 2204 if (++buf_write[port] == RX_BUF_WRITE) {
5df79111 2205force_update:
69161611
SH
2206 sky2_put_idx(hw, rxqaddr[port], sky2->rx_put);
2207 buf_write[port] = 0;
22e11703
SH
2208 }
2209
2210 /* Stop after net poll weight */
13210ce5 2211 if (++work_done >= to_do)
2212 goto exit_loop;
cd28ab6a
SH
2213 break;
2214
d1f13708 2215#ifdef SKY2_VLAN_TAG_USED
2216 case OP_RXVLAN:
2217 sky2->rx_tag = length;
2218 break;
2219
2220 case OP_RXCHKSVLAN:
2221 sky2->rx_tag = length;
2222 /* fall through */
2223#endif
cd28ab6a 2224 case OP_RXCHKS:
87418307
SH
2225 if (!sky2->rx_csum)
2226 break;
2227
69161611
SH
2228 if (hw->chip_id == CHIP_ID_YUKON_EX)
2229 break;
2230
87418307
SH
2231 /* Both checksum counters are programmed to start at
2232 * the same offset, so unless there is a problem they
2233 * should match. This failure is an early indication that
2234 * hardware receive checksumming won't work.
2235 */
2236 if (likely(status >> 16 == (status & 0xffff))) {
2237 skb = sky2->rx_ring[sky2->rx_next].skb;
2238 skb->ip_summed = CHECKSUM_COMPLETE;
2239 skb->csum = status & 0xffff;
2240 } else {
2241 printk(KERN_NOTICE PFX "%s: hardware receive "
2242 "checksum problem (status = %#x)\n",
2243 dev->name, status);
2244 sky2->rx_csum = 0;
2245 sky2_write32(sky2->hw,
69161611 2246 Q_ADDR(rxqaddr[port], Q_CSR),
87418307
SH
2247 BMU_DIS_RX_CHKSUM);
2248 }
cd28ab6a
SH
2249 break;
2250
2251 case OP_TXINDEXLE:
13b97b74 2252 /* TX index reports status for both ports */
f55925d7
SH
2253 BUILD_BUG_ON(TX_RING_SIZE > 0x1000);
2254 sky2_tx_done(hw->dev[0], status & 0xfff);
e07b1aa8
SH
2255 if (hw->dev[1])
2256 sky2_tx_done(hw->dev[1],
2257 ((status >> 24) & 0xff)
2258 | (u16)(length & 0xf) << 8);
cd28ab6a
SH
2259 break;
2260
cd28ab6a
SH
2261 default:
2262 if (net_ratelimit())
793b883e 2263 printk(KERN_WARNING PFX
e71ebd73
SH
2264 "unknown status opcode 0x%x\n", le->opcode);
2265 goto exit_loop;
cd28ab6a 2266 }
13210ce5 2267 }
cd28ab6a 2268
fe2a24df
SH
2269 /* Fully processed status ring so clear irq */
2270 sky2_write32(hw, STAT_CTRL, SC_STAT_CLR_IRQ);
50432cb5 2271 mmiowb();
fe2a24df 2272
13210ce5 2273exit_loop:
22e11703
SH
2274 if (buf_write[0]) {
2275 sky2 = netdev_priv(hw->dev[0]);
2276 sky2_put_idx(hw, Q_R1, sky2->rx_put);
2277 }
2278
2279 if (buf_write[1]) {
2280 sky2 = netdev_priv(hw->dev[1]);
2281 sky2_put_idx(hw, Q_R2, sky2->rx_put);
2282 }
2283
e07b1aa8 2284 return work_done;
cd28ab6a
SH
2285}
2286
2287static void sky2_hw_error(struct sky2_hw *hw, unsigned port, u32 status)
2288{
2289 struct net_device *dev = hw->dev[port];
2290
3be92a70
SH
2291 if (net_ratelimit())
2292 printk(KERN_INFO PFX "%s: hw error interrupt status 0x%x\n",
2293 dev->name, status);
cd28ab6a
SH
2294
2295 if (status & Y2_IS_PAR_RD1) {
3be92a70
SH
2296 if (net_ratelimit())
2297 printk(KERN_ERR PFX "%s: ram data read parity error\n",
2298 dev->name);
cd28ab6a
SH
2299 /* Clear IRQ */
2300 sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_RD_PERR);
2301 }
2302
2303 if (status & Y2_IS_PAR_WR1) {
3be92a70
SH
2304 if (net_ratelimit())
2305 printk(KERN_ERR PFX "%s: ram data write parity error\n",
2306 dev->name);
cd28ab6a
SH
2307
2308 sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_WR_PERR);
2309 }
2310
2311 if (status & Y2_IS_PAR_MAC1) {
3be92a70
SH
2312 if (net_ratelimit())
2313 printk(KERN_ERR PFX "%s: MAC parity error\n", dev->name);
cd28ab6a
SH
2314 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_PE);
2315 }
2316
2317 if (status & Y2_IS_PAR_RX1) {
3be92a70
SH
2318 if (net_ratelimit())
2319 printk(KERN_ERR PFX "%s: RX parity error\n", dev->name);
cd28ab6a
SH
2320 sky2_write32(hw, Q_ADDR(rxqaddr[port], Q_CSR), BMU_CLR_IRQ_PAR);
2321 }
2322
2323 if (status & Y2_IS_TCP_TXA1) {
3be92a70
SH
2324 if (net_ratelimit())
2325 printk(KERN_ERR PFX "%s: TCP segmentation error\n",
2326 dev->name);
cd28ab6a
SH
2327 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_CLR_IRQ_TCP);
2328 }
2329}
2330
2331static void sky2_hw_intr(struct sky2_hw *hw)
2332{
2333 u32 status = sky2_read32(hw, B0_HWE_ISRC);
2334
793b883e 2335 if (status & Y2_IS_TIST_OV)
cd28ab6a 2336 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
cd28ab6a
SH
2337
2338 if (status & (Y2_IS_MST_ERR | Y2_IS_IRQ_STAT)) {
793b883e
SH
2339 u16 pci_err;
2340
56a645cc 2341 pci_err = sky2_pci_read16(hw, PCI_STATUS);
3be92a70 2342 if (net_ratelimit())
b02a9258
SH
2343 dev_err(&hw->pdev->dev, "PCI hardware error (0x%x)\n",
2344 pci_err);
cd28ab6a
SH
2345
2346 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
56a645cc 2347 sky2_pci_write16(hw, PCI_STATUS,
91aeb3ed 2348 pci_err | PCI_STATUS_ERROR_BITS);
cd28ab6a
SH
2349 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2350 }
2351
2352 if (status & Y2_IS_PCI_EXP) {
d571b694 2353 /* PCI-Express uncorrectable Error occurred */
793b883e
SH
2354 u32 pex_err;
2355
7bd656d1 2356 pex_err = sky2_pci_read32(hw, PEX_UNC_ERR_STAT);
cd28ab6a 2357
3be92a70 2358 if (net_ratelimit())
b02a9258
SH
2359 dev_err(&hw->pdev->dev, "PCI Express error (0x%x)\n",
2360 pex_err);
cd28ab6a
SH
2361
2362 /* clear the interrupt */
2363 sky2_write32(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
7bd656d1
SH
2364 sky2_pci_write32(hw, PEX_UNC_ERR_STAT,
2365 0xffffffffUL);
cd28ab6a
SH
2366 sky2_write32(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2367
7bd656d1 2368 if (pex_err & PEX_FATAL_ERRORS) {
cd28ab6a
SH
2369 u32 hwmsk = sky2_read32(hw, B0_HWE_IMSK);
2370 hwmsk &= ~Y2_IS_PCI_EXP;
2371 sky2_write32(hw, B0_HWE_IMSK, hwmsk);
2372 }
2373 }
2374
2375 if (status & Y2_HWE_L1_MASK)
2376 sky2_hw_error(hw, 0, status);
2377 status >>= 8;
2378 if (status & Y2_HWE_L1_MASK)
2379 sky2_hw_error(hw, 1, status);
2380}
2381
2382static void sky2_mac_intr(struct sky2_hw *hw, unsigned port)
2383{
2384 struct net_device *dev = hw->dev[port];
2385 struct sky2_port *sky2 = netdev_priv(dev);
2386 u8 status = sky2_read8(hw, SK_REG(port, GMAC_IRQ_SRC));
2387
2388 if (netif_msg_intr(sky2))
2389 printk(KERN_INFO PFX "%s: mac interrupt status 0x%x\n",
2390 dev->name, status);
2391
a3caeada
SH
2392 if (status & GM_IS_RX_CO_OV)
2393 gma_read16(hw, port, GM_RX_IRQ_SRC);
2394
2395 if (status & GM_IS_TX_CO_OV)
2396 gma_read16(hw, port, GM_TX_IRQ_SRC);
2397
cd28ab6a
SH
2398 if (status & GM_IS_RX_FF_OR) {
2399 ++sky2->net_stats.rx_fifo_errors;
2400 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_CLI_RX_FO);
2401 }
2402
2403 if (status & GM_IS_TX_FF_UR) {
2404 ++sky2->net_stats.tx_fifo_errors;
2405 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_FU);
2406 }
cd28ab6a
SH
2407}
2408
40b01727
SH
2409/* This should never happen it is a bug. */
2410static void sky2_le_error(struct sky2_hw *hw, unsigned port,
2411 u16 q, unsigned ring_size)
d257924e
SH
2412{
2413 struct net_device *dev = hw->dev[port];
2414 struct sky2_port *sky2 = netdev_priv(dev);
40b01727
SH
2415 unsigned idx;
2416 const u64 *le = (q == Q_R1 || q == Q_R2)
2417 ? (u64 *) sky2->rx_le : (u64 *) sky2->tx_le;
d257924e 2418
40b01727
SH
2419 idx = sky2_read16(hw, Y2_QADDR(q, PREF_UNIT_GET_IDX));
2420 printk(KERN_ERR PFX "%s: descriptor error q=%#x get=%u [%llx] put=%u\n",
2421 dev->name, (unsigned) q, idx, (unsigned long long) le[idx],
2422 (unsigned) sky2_read16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX)));
d257924e 2423
40b01727 2424 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_CLR_IRQ_CHK);
d257924e 2425}
cd28ab6a 2426
d27ed387
SH
2427/* If idle then force a fake soft NAPI poll once a second
2428 * to work around cases where sharing an edge triggered interrupt.
2429 */
eb35cf60
SH
2430static inline void sky2_idle_start(struct sky2_hw *hw)
2431{
2432 if (idle_timeout > 0)
2433 mod_timer(&hw->idle_timer,
2434 jiffies + msecs_to_jiffies(idle_timeout));
2435}
2436
d27ed387
SH
2437static void sky2_idle(unsigned long arg)
2438{
01bd7564
SH
2439 struct sky2_hw *hw = (struct sky2_hw *) arg;
2440 struct net_device *dev = hw->dev[0];
d27ed387 2441
d27ed387
SH
2442 if (__netif_rx_schedule_prep(dev))
2443 __netif_rx_schedule(dev);
01bd7564
SH
2444
2445 mod_timer(&hw->idle_timer, jiffies + msecs_to_jiffies(idle_timeout));
d27ed387
SH
2446}
2447
40b01727
SH
2448/* Hardware/software error handling */
2449static void sky2_err_intr(struct sky2_hw *hw, u32 status)
cd28ab6a 2450{
40b01727
SH
2451 if (net_ratelimit())
2452 dev_warn(&hw->pdev->dev, "error interrupt status=%#x\n", status);
cd28ab6a 2453
1e5f1283
SH
2454 if (status & Y2_IS_HW_ERR)
2455 sky2_hw_intr(hw);
d257924e 2456
1e5f1283
SH
2457 if (status & Y2_IS_IRQ_MAC1)
2458 sky2_mac_intr(hw, 0);
cd28ab6a 2459
1e5f1283
SH
2460 if (status & Y2_IS_IRQ_MAC2)
2461 sky2_mac_intr(hw, 1);
cd28ab6a 2462
1e5f1283 2463 if (status & Y2_IS_CHK_RX1)
40b01727 2464 sky2_le_error(hw, 0, Q_R1, RX_LE_SIZE);
d257924e 2465
1e5f1283 2466 if (status & Y2_IS_CHK_RX2)
40b01727 2467 sky2_le_error(hw, 1, Q_R2, RX_LE_SIZE);
d257924e 2468
1e5f1283 2469 if (status & Y2_IS_CHK_TXA1)
40b01727 2470 sky2_le_error(hw, 0, Q_XA1, TX_RING_SIZE);
d257924e 2471
1e5f1283 2472 if (status & Y2_IS_CHK_TXA2)
40b01727
SH
2473 sky2_le_error(hw, 1, Q_XA2, TX_RING_SIZE);
2474}
2475
2476static int sky2_poll(struct net_device *dev0, int *budget)
2477{
2478 struct sky2_hw *hw = ((struct sky2_port *) netdev_priv(dev0))->hw;
2479 int work_limit = min(dev0->quota, *budget);
2480 int work_done = 0;
2481 u32 status = sky2_read32(hw, B0_Y2_SP_EISR);
2482
2483 if (unlikely(status & Y2_IS_ERROR))
2484 sky2_err_intr(hw, status);
2485
2486 if (status & Y2_IS_IRQ_PHY1)
2487 sky2_phy_intr(hw, 0);
2488
2489 if (status & Y2_IS_IRQ_PHY2)
2490 sky2_phy_intr(hw, 1);
cd28ab6a 2491
1e5f1283 2492 work_done = sky2_status_intr(hw, work_limit);
fe2a24df 2493 if (work_done < work_limit) {
c59697e0
SH
2494 /* Bug/Errata workaround?
2495 * Need to kick the TX irq moderation timer.
2496 */
2497 if (sky2_read8(hw, STAT_TX_TIMER_CTRL) == TIM_START) {
2498 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_STOP);
2499 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
2500 }
fe2a24df 2501 netif_rx_complete(dev0);
86fba634 2502
50432cb5 2503 /* end of interrupt, re-enables also acts as I/O synchronization */
fe2a24df
SH
2504 sky2_read32(hw, B0_Y2_SP_LISR);
2505 return 0;
2506 } else {
2507 *budget -= work_done;
2508 dev0->quota -= work_done;
1e5f1283 2509 return 1;
fe2a24df 2510 }
e07b1aa8
SH
2511}
2512
7d12e780 2513static irqreturn_t sky2_intr(int irq, void *dev_id)
e07b1aa8
SH
2514{
2515 struct sky2_hw *hw = dev_id;
2516 struct net_device *dev0 = hw->dev[0];
2517 u32 status;
2518
2519 /* Reading this mask interrupts as side effect */
2520 status = sky2_read32(hw, B0_Y2_SP_ISRC2);
2521 if (status == 0 || status == ~0)
2522 return IRQ_NONE;
793b883e 2523
e07b1aa8
SH
2524 prefetch(&hw->st_le[hw->st_idx]);
2525 if (likely(__netif_rx_schedule_prep(dev0)))
2526 __netif_rx_schedule(dev0);
793b883e 2527
cd28ab6a
SH
2528 return IRQ_HANDLED;
2529}
2530
2531#ifdef CONFIG_NET_POLL_CONTROLLER
2532static void sky2_netpoll(struct net_device *dev)
2533{
2534 struct sky2_port *sky2 = netdev_priv(dev);
88d11360 2535 struct net_device *dev0 = sky2->hw->dev[0];
cd28ab6a 2536
88d11360
SH
2537 if (netif_running(dev) && __netif_rx_schedule_prep(dev0))
2538 __netif_rx_schedule(dev0);
cd28ab6a
SH
2539}
2540#endif
2541
2542/* Chip internal frequency for clock calculations */
fb17358f 2543static inline u32 sky2_mhz(const struct sky2_hw *hw)
cd28ab6a 2544{
793b883e 2545 switch (hw->chip_id) {
cd28ab6a 2546 case CHIP_ID_YUKON_EC:
5a5b1ea0 2547 case CHIP_ID_YUKON_EC_U:
93745494 2548 case CHIP_ID_YUKON_EX:
fb17358f 2549 return 125; /* 125 Mhz */
cd28ab6a 2550 case CHIP_ID_YUKON_FE:
fb17358f 2551 return 100; /* 100 Mhz */
793b883e 2552 default: /* YUKON_XL */
fb17358f 2553 return 156; /* 156 Mhz */
cd28ab6a
SH
2554 }
2555}
2556
fb17358f 2557static inline u32 sky2_us2clk(const struct sky2_hw *hw, u32 us)
cd28ab6a 2558{
fb17358f 2559 return sky2_mhz(hw) * us;
cd28ab6a
SH
2560}
2561
fb17358f 2562static inline u32 sky2_clk2us(const struct sky2_hw *hw, u32 clk)
cd28ab6a 2563{
fb17358f 2564 return clk / sky2_mhz(hw);
cd28ab6a
SH
2565}
2566
fb17358f 2567
e3173832 2568static int __devinit sky2_init(struct sky2_hw *hw)
cd28ab6a 2569{
b89165f2 2570 u8 t8;
cd28ab6a 2571
451af335
SH
2572 /* Enable all clocks */
2573 sky2_pci_write32(hw, PCI_DEV_REG3, 0);
2574
cd28ab6a 2575 sky2_write8(hw, B0_CTST, CS_RST_CLR);
08c06d8a 2576
cd28ab6a
SH
2577 hw->chip_id = sky2_read8(hw, B2_CHIP_ID);
2578 if (hw->chip_id < CHIP_ID_YUKON_XL || hw->chip_id > CHIP_ID_YUKON_FE) {
b02a9258
SH
2579 dev_err(&hw->pdev->dev, "unsupported chip type 0x%x\n",
2580 hw->chip_id);
cd28ab6a
SH
2581 return -EOPNOTSUPP;
2582 }
2583
290d4de5
SH
2584 hw->chip_rev = (sky2_read8(hw, B2_MAC_CFG) & CFG_CHIP_R_MSK) >> 4;
2585
2586 /* This rev is really old, and requires untested workarounds */
2587 if (hw->chip_id == CHIP_ID_YUKON_EC && hw->chip_rev == CHIP_REV_YU_EC_A1) {
b02a9258
SH
2588 dev_err(&hw->pdev->dev, "unsupported revision Yukon-%s (0x%x) rev %d\n",
2589 yukon2_name[hw->chip_id - CHIP_ID_YUKON_XL],
2590 hw->chip_id, hw->chip_rev);
290d4de5
SH
2591 return -EOPNOTSUPP;
2592 }
2593
e3173832
SH
2594 hw->pmd_type = sky2_read8(hw, B2_PMD_TYP);
2595 hw->ports = 1;
2596 t8 = sky2_read8(hw, B2_Y2_HW_RES);
2597 if ((t8 & CFG_DUAL_MAC_MSK) == CFG_DUAL_MAC_MSK) {
2598 if (!(sky2_read8(hw, B2_Y2_CLK_GATE) & Y2_STATUS_LNK2_INAC))
2599 ++hw->ports;
2600 }
2601
2602 return 0;
2603}
2604
2605static void sky2_reset(struct sky2_hw *hw)
2606{
2607 u16 status;
2608 int i;
2609
cd28ab6a 2610 /* disable ASF */
4f44d8ba
SH
2611 if (hw->chip_id == CHIP_ID_YUKON_EX) {
2612 status = sky2_read16(hw, HCU_CCSR);
2613 status &= ~(HCU_CCSR_AHB_RST | HCU_CCSR_CPU_RST_MODE |
2614 HCU_CCSR_UC_STATE_MSK);
2615 sky2_write16(hw, HCU_CCSR, status);
2616 } else
2617 sky2_write8(hw, B28_Y2_ASF_STAT_CMD, Y2_ASF_RESET);
2618 sky2_write16(hw, B0_CTST, Y2_ASF_DISABLE);
cd28ab6a
SH
2619
2620 /* do a SW reset */
2621 sky2_write8(hw, B0_CTST, CS_RST_SET);
2622 sky2_write8(hw, B0_CTST, CS_RST_CLR);
2623
2624 /* clear PCI errors, if any */
56a645cc 2625 status = sky2_pci_read16(hw, PCI_STATUS);
2d42d21f 2626
cd28ab6a 2627 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
56a645cc
SH
2628 sky2_pci_write16(hw, PCI_STATUS, status | PCI_STATUS_ERROR_BITS);
2629
cd28ab6a
SH
2630
2631 sky2_write8(hw, B0_CTST, CS_MRST_CLR);
2632
2633 /* clear any PEX errors */
7bd656d1
SH
2634 if (pci_find_capability(hw->pdev, PCI_CAP_ID_EXP))
2635 sky2_pci_write32(hw, PEX_UNC_ERR_STAT, 0xffffffffUL);
2636
cd28ab6a 2637
ae306cca 2638 sky2_power_on(hw);
cd28ab6a
SH
2639
2640 for (i = 0; i < hw->ports; i++) {
2641 sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_SET);
2642 sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_CLR);
69161611
SH
2643
2644 if (hw->chip_id == CHIP_ID_YUKON_EX)
2645 sky2_write16(hw, SK_REG(i, GMAC_CTRL),
2646 GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON
2647 | GMC_BYP_RETR_ON);
cd28ab6a
SH
2648 }
2649
2650 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2651
793b883e
SH
2652 /* Clear I2C IRQ noise */
2653 sky2_write32(hw, B2_I2C_IRQ, 1);
cd28ab6a
SH
2654
2655 /* turn off hardware timer (unused) */
2656 sky2_write8(hw, B2_TI_CTRL, TIM_STOP);
2657 sky2_write8(hw, B2_TI_CTRL, TIM_CLR_IRQ);
793b883e 2658
cd28ab6a
SH
2659 sky2_write8(hw, B0_Y2LED, LED_STAT_ON);
2660
69634ee7
SH
2661 /* Turn off descriptor polling */
2662 sky2_write32(hw, B28_DPT_CTRL, DPT_STOP);
cd28ab6a
SH
2663
2664 /* Turn off receive timestamp */
2665 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_STOP);
793b883e 2666 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
cd28ab6a
SH
2667
2668 /* enable the Tx Arbiters */
2669 for (i = 0; i < hw->ports; i++)
2670 sky2_write8(hw, SK_REG(i, TXA_CTRL), TXA_ENA_ARB);
2671
2672 /* Initialize ram interface */
2673 for (i = 0; i < hw->ports; i++) {
793b883e 2674 sky2_write8(hw, RAM_BUFFER(i, B3_RI_CTRL), RI_RST_CLR);
cd28ab6a
SH
2675
2676 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R1), SK_RI_TO_53);
2677 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA1), SK_RI_TO_53);
2678 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS1), SK_RI_TO_53);
2679 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R1), SK_RI_TO_53);
2680 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA1), SK_RI_TO_53);
2681 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS1), SK_RI_TO_53);
2682 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R2), SK_RI_TO_53);
2683 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA2), SK_RI_TO_53);
2684 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS2), SK_RI_TO_53);
2685 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R2), SK_RI_TO_53);
2686 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA2), SK_RI_TO_53);
2687 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS2), SK_RI_TO_53);
2688 }
2689
7bd656d1 2690 sky2_write32(hw, B0_HWE_IMSK, Y2_HWE_ALL_MASK);
cd28ab6a 2691
cd28ab6a 2692 for (i = 0; i < hw->ports; i++)
d3bcfbeb 2693 sky2_gmac_reset(hw, i);
cd28ab6a 2694
cd28ab6a
SH
2695 memset(hw->st_le, 0, STATUS_LE_BYTES);
2696 hw->st_idx = 0;
2697
2698 sky2_write32(hw, STAT_CTRL, SC_STAT_RST_SET);
2699 sky2_write32(hw, STAT_CTRL, SC_STAT_RST_CLR);
2700
2701 sky2_write32(hw, STAT_LIST_ADDR_LO, hw->st_dma);
793b883e 2702 sky2_write32(hw, STAT_LIST_ADDR_HI, (u64) hw->st_dma >> 32);
cd28ab6a
SH
2703
2704 /* Set the list last index */
793b883e 2705 sky2_write16(hw, STAT_LAST_IDX, STATUS_RING_SIZE - 1);
cd28ab6a 2706
290d4de5
SH
2707 sky2_write16(hw, STAT_TX_IDX_TH, 10);
2708 sky2_write8(hw, STAT_FIFO_WM, 16);
cd28ab6a 2709
290d4de5
SH
2710 /* set Status-FIFO ISR watermark */
2711 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0)
2712 sky2_write8(hw, STAT_FIFO_ISR_WM, 4);
2713 else
2714 sky2_write8(hw, STAT_FIFO_ISR_WM, 16);
cd28ab6a 2715
290d4de5 2716 sky2_write32(hw, STAT_TX_TIMER_INI, sky2_us2clk(hw, 1000));
77b3d6a2
SH
2717 sky2_write32(hw, STAT_ISR_TIMER_INI, sky2_us2clk(hw, 20));
2718 sky2_write32(hw, STAT_LEV_TIMER_INI, sky2_us2clk(hw, 100));
cd28ab6a 2719
793b883e 2720 /* enable status unit */
cd28ab6a
SH
2721 sky2_write32(hw, STAT_CTRL, SC_STAT_OP_ON);
2722
2723 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
2724 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
2725 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
e3173832
SH
2726}
2727
81906791
SH
2728static void sky2_restart(struct work_struct *work)
2729{
2730 struct sky2_hw *hw = container_of(work, struct sky2_hw, restart_work);
2731 struct net_device *dev;
2732 int i, err;
2733
2734 dev_dbg(&hw->pdev->dev, "restarting\n");
2735
2736 del_timer_sync(&hw->idle_timer);
2737
2738 rtnl_lock();
2739 sky2_write32(hw, B0_IMSK, 0);
2740 sky2_read32(hw, B0_IMSK);
2741
2742 netif_poll_disable(hw->dev[0]);
2743
2744 for (i = 0; i < hw->ports; i++) {
2745 dev = hw->dev[i];
2746 if (netif_running(dev))
2747 sky2_down(dev);
2748 }
2749
2750 sky2_reset(hw);
2751 sky2_write32(hw, B0_IMSK, Y2_IS_BASE);
2752 netif_poll_enable(hw->dev[0]);
2753
2754 for (i = 0; i < hw->ports; i++) {
2755 dev = hw->dev[i];
2756 if (netif_running(dev)) {
2757 err = sky2_up(dev);
2758 if (err) {
2759 printk(KERN_INFO PFX "%s: could not restart %d\n",
2760 dev->name, err);
2761 dev_close(dev);
2762 }
2763 }
2764 }
2765
2766 sky2_idle_start(hw);
2767
2768 rtnl_unlock();
2769}
2770
e3173832
SH
2771static inline u8 sky2_wol_supported(const struct sky2_hw *hw)
2772{
2773 return sky2_is_copper(hw) ? (WAKE_PHY | WAKE_MAGIC) : 0;
2774}
2775
2776static void sky2_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2777{
2778 const struct sky2_port *sky2 = netdev_priv(dev);
2779
2780 wol->supported = sky2_wol_supported(sky2->hw);
2781 wol->wolopts = sky2->wol;
2782}
2783
2784static int sky2_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2785{
2786 struct sky2_port *sky2 = netdev_priv(dev);
2787 struct sky2_hw *hw = sky2->hw;
cd28ab6a 2788
e3173832
SH
2789 if (wol->wolopts & ~sky2_wol_supported(sky2->hw))
2790 return -EOPNOTSUPP;
2791
2792 sky2->wol = wol->wolopts;
2793
69161611 2794 if (hw->chip_id == CHIP_ID_YUKON_EC_U || hw->chip_id == CHIP_ID_YUKON_EX)
e3173832
SH
2795 sky2_write32(hw, B0_CTST, sky2->wol
2796 ? Y2_HW_WOL_ON : Y2_HW_WOL_OFF);
2797
2798 if (!netif_running(dev))
2799 sky2_wol_init(sky2);
cd28ab6a
SH
2800 return 0;
2801}
2802
28bd181a 2803static u32 sky2_supported_modes(const struct sky2_hw *hw)
cd28ab6a 2804{
b89165f2
SH
2805 if (sky2_is_copper(hw)) {
2806 u32 modes = SUPPORTED_10baseT_Half
2807 | SUPPORTED_10baseT_Full
2808 | SUPPORTED_100baseT_Half
2809 | SUPPORTED_100baseT_Full
2810 | SUPPORTED_Autoneg | SUPPORTED_TP;
cd28ab6a
SH
2811
2812 if (hw->chip_id != CHIP_ID_YUKON_FE)
2813 modes |= SUPPORTED_1000baseT_Half
b89165f2
SH
2814 | SUPPORTED_1000baseT_Full;
2815 return modes;
cd28ab6a 2816 } else
b89165f2
SH
2817 return SUPPORTED_1000baseT_Half
2818 | SUPPORTED_1000baseT_Full
2819 | SUPPORTED_Autoneg
2820 | SUPPORTED_FIBRE;
cd28ab6a
SH
2821}
2822
793b883e 2823static int sky2_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
cd28ab6a
SH
2824{
2825 struct sky2_port *sky2 = netdev_priv(dev);
2826 struct sky2_hw *hw = sky2->hw;
2827
2828 ecmd->transceiver = XCVR_INTERNAL;
2829 ecmd->supported = sky2_supported_modes(hw);
2830 ecmd->phy_address = PHY_ADDR_MARV;
b89165f2 2831 if (sky2_is_copper(hw)) {
cd28ab6a 2832 ecmd->supported = SUPPORTED_10baseT_Half
793b883e
SH
2833 | SUPPORTED_10baseT_Full
2834 | SUPPORTED_100baseT_Half
2835 | SUPPORTED_100baseT_Full
2836 | SUPPORTED_1000baseT_Half
2837 | SUPPORTED_1000baseT_Full
2838 | SUPPORTED_Autoneg | SUPPORTED_TP;
cd28ab6a 2839 ecmd->port = PORT_TP;
b89165f2
SH
2840 ecmd->speed = sky2->speed;
2841 } else {
2842 ecmd->speed = SPEED_1000;
cd28ab6a 2843 ecmd->port = PORT_FIBRE;
b89165f2 2844 }
cd28ab6a
SH
2845
2846 ecmd->advertising = sky2->advertising;
2847 ecmd->autoneg = sky2->autoneg;
cd28ab6a
SH
2848 ecmd->duplex = sky2->duplex;
2849 return 0;
2850}
2851
2852static int sky2_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2853{
2854 struct sky2_port *sky2 = netdev_priv(dev);
2855 const struct sky2_hw *hw = sky2->hw;
2856 u32 supported = sky2_supported_modes(hw);
2857
2858 if (ecmd->autoneg == AUTONEG_ENABLE) {
2859 ecmd->advertising = supported;
2860 sky2->duplex = -1;
2861 sky2->speed = -1;
2862 } else {
2863 u32 setting;
2864
793b883e 2865 switch (ecmd->speed) {
cd28ab6a
SH
2866 case SPEED_1000:
2867 if (ecmd->duplex == DUPLEX_FULL)
2868 setting = SUPPORTED_1000baseT_Full;
2869 else if (ecmd->duplex == DUPLEX_HALF)
2870 setting = SUPPORTED_1000baseT_Half;
2871 else
2872 return -EINVAL;
2873 break;
2874 case SPEED_100:
2875 if (ecmd->duplex == DUPLEX_FULL)
2876 setting = SUPPORTED_100baseT_Full;
2877 else if (ecmd->duplex == DUPLEX_HALF)
2878 setting = SUPPORTED_100baseT_Half;
2879 else
2880 return -EINVAL;
2881 break;
2882
2883 case SPEED_10:
2884 if (ecmd->duplex == DUPLEX_FULL)
2885 setting = SUPPORTED_10baseT_Full;
2886 else if (ecmd->duplex == DUPLEX_HALF)
2887 setting = SUPPORTED_10baseT_Half;
2888 else
2889 return -EINVAL;
2890 break;
2891 default:
2892 return -EINVAL;
2893 }
2894
2895 if ((setting & supported) == 0)
2896 return -EINVAL;
2897
2898 sky2->speed = ecmd->speed;
2899 sky2->duplex = ecmd->duplex;
2900 }
2901
2902 sky2->autoneg = ecmd->autoneg;
2903 sky2->advertising = ecmd->advertising;
2904
1b537565
SH
2905 if (netif_running(dev))
2906 sky2_phy_reinit(sky2);
cd28ab6a
SH
2907
2908 return 0;
2909}
2910
2911static void sky2_get_drvinfo(struct net_device *dev,
2912 struct ethtool_drvinfo *info)
2913{
2914 struct sky2_port *sky2 = netdev_priv(dev);
2915
2916 strcpy(info->driver, DRV_NAME);
2917 strcpy(info->version, DRV_VERSION);
2918 strcpy(info->fw_version, "N/A");
2919 strcpy(info->bus_info, pci_name(sky2->hw->pdev));
2920}
2921
2922static const struct sky2_stat {
793b883e
SH
2923 char name[ETH_GSTRING_LEN];
2924 u16 offset;
cd28ab6a
SH
2925} sky2_stats[] = {
2926 { "tx_bytes", GM_TXO_OK_HI },
2927 { "rx_bytes", GM_RXO_OK_HI },
2928 { "tx_broadcast", GM_TXF_BC_OK },
2929 { "rx_broadcast", GM_RXF_BC_OK },
2930 { "tx_multicast", GM_TXF_MC_OK },
2931 { "rx_multicast", GM_RXF_MC_OK },
2932 { "tx_unicast", GM_TXF_UC_OK },
2933 { "rx_unicast", GM_RXF_UC_OK },
2934 { "tx_mac_pause", GM_TXF_MPAUSE },
2935 { "rx_mac_pause", GM_RXF_MPAUSE },
eadfa7dd 2936 { "collisions", GM_TXF_COL },
cd28ab6a
SH
2937 { "late_collision",GM_TXF_LAT_COL },
2938 { "aborted", GM_TXF_ABO_COL },
eadfa7dd 2939 { "single_collisions", GM_TXF_SNG_COL },
cd28ab6a 2940 { "multi_collisions", GM_TXF_MUL_COL },
eadfa7dd 2941
d2604540 2942 { "rx_short", GM_RXF_SHT },
cd28ab6a 2943 { "rx_runt", GM_RXE_FRAG },
eadfa7dd
SH
2944 { "rx_64_byte_packets", GM_RXF_64B },
2945 { "rx_65_to_127_byte_packets", GM_RXF_127B },
2946 { "rx_128_to_255_byte_packets", GM_RXF_255B },
2947 { "rx_256_to_511_byte_packets", GM_RXF_511B },
2948 { "rx_512_to_1023_byte_packets", GM_RXF_1023B },
2949 { "rx_1024_to_1518_byte_packets", GM_RXF_1518B },
2950 { "rx_1518_to_max_byte_packets", GM_RXF_MAX_SZ },
cd28ab6a 2951 { "rx_too_long", GM_RXF_LNG_ERR },
eadfa7dd
SH
2952 { "rx_fifo_overflow", GM_RXE_FIFO_OV },
2953 { "rx_jabber", GM_RXF_JAB_PKT },
cd28ab6a 2954 { "rx_fcs_error", GM_RXF_FCS_ERR },
eadfa7dd
SH
2955
2956 { "tx_64_byte_packets", GM_TXF_64B },
2957 { "tx_65_to_127_byte_packets", GM_TXF_127B },
2958 { "tx_128_to_255_byte_packets", GM_TXF_255B },
2959 { "tx_256_to_511_byte_packets", GM_TXF_511B },
2960 { "tx_512_to_1023_byte_packets", GM_TXF_1023B },
2961 { "tx_1024_to_1518_byte_packets", GM_TXF_1518B },
2962 { "tx_1519_to_max_byte_packets", GM_TXF_MAX_SZ },
2963 { "tx_fifo_underrun", GM_TXE_FIFO_UR },
cd28ab6a
SH
2964};
2965
cd28ab6a
SH
2966static u32 sky2_get_rx_csum(struct net_device *dev)
2967{
2968 struct sky2_port *sky2 = netdev_priv(dev);
2969
2970 return sky2->rx_csum;
2971}
2972
2973static int sky2_set_rx_csum(struct net_device *dev, u32 data)
2974{
2975 struct sky2_port *sky2 = netdev_priv(dev);
2976
2977 sky2->rx_csum = data;
793b883e 2978
cd28ab6a
SH
2979 sky2_write32(sky2->hw, Q_ADDR(rxqaddr[sky2->port], Q_CSR),
2980 data ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
2981
2982 return 0;
2983}
2984
2985static u32 sky2_get_msglevel(struct net_device *netdev)
2986{
2987 struct sky2_port *sky2 = netdev_priv(netdev);
2988 return sky2->msg_enable;
2989}
2990
9a7ae0a9
SH
2991static int sky2_nway_reset(struct net_device *dev)
2992{
2993 struct sky2_port *sky2 = netdev_priv(dev);
9a7ae0a9 2994
16ad91e1 2995 if (!netif_running(dev) || sky2->autoneg != AUTONEG_ENABLE)
9a7ae0a9
SH
2996 return -EINVAL;
2997
1b537565 2998 sky2_phy_reinit(sky2);
9a7ae0a9
SH
2999
3000 return 0;
3001}
3002
793b883e 3003static void sky2_phy_stats(struct sky2_port *sky2, u64 * data, unsigned count)
cd28ab6a
SH
3004{
3005 struct sky2_hw *hw = sky2->hw;
3006 unsigned port = sky2->port;
3007 int i;
3008
3009 data[0] = (u64) gma_read32(hw, port, GM_TXO_OK_HI) << 32
793b883e 3010 | (u64) gma_read32(hw, port, GM_TXO_OK_LO);
cd28ab6a 3011 data[1] = (u64) gma_read32(hw, port, GM_RXO_OK_HI) << 32
793b883e 3012 | (u64) gma_read32(hw, port, GM_RXO_OK_LO);
cd28ab6a 3013
793b883e 3014 for (i = 2; i < count; i++)
cd28ab6a
SH
3015 data[i] = (u64) gma_read32(hw, port, sky2_stats[i].offset);
3016}
3017
cd28ab6a
SH
3018static void sky2_set_msglevel(struct net_device *netdev, u32 value)
3019{
3020 struct sky2_port *sky2 = netdev_priv(netdev);
3021 sky2->msg_enable = value;
3022}
3023
3024static int sky2_get_stats_count(struct net_device *dev)
3025{
3026 return ARRAY_SIZE(sky2_stats);
3027}
3028
3029static void sky2_get_ethtool_stats(struct net_device *dev,
793b883e 3030 struct ethtool_stats *stats, u64 * data)
cd28ab6a
SH
3031{
3032 struct sky2_port *sky2 = netdev_priv(dev);
3033
793b883e 3034 sky2_phy_stats(sky2, data, ARRAY_SIZE(sky2_stats));
cd28ab6a
SH
3035}
3036
793b883e 3037static void sky2_get_strings(struct net_device *dev, u32 stringset, u8 * data)
cd28ab6a
SH
3038{
3039 int i;
3040
3041 switch (stringset) {
3042 case ETH_SS_STATS:
3043 for (i = 0; i < ARRAY_SIZE(sky2_stats); i++)
3044 memcpy(data + i * ETH_GSTRING_LEN,
3045 sky2_stats[i].name, ETH_GSTRING_LEN);
3046 break;
3047 }
3048}
3049
cd28ab6a
SH
3050static struct net_device_stats *sky2_get_stats(struct net_device *dev)
3051{
3052 struct sky2_port *sky2 = netdev_priv(dev);
cd28ab6a
SH
3053 return &sky2->net_stats;
3054}
3055
3056static int sky2_set_mac_address(struct net_device *dev, void *p)
3057{
3058 struct sky2_port *sky2 = netdev_priv(dev);
a8ab1ec0
SH
3059 struct sky2_hw *hw = sky2->hw;
3060 unsigned port = sky2->port;
3061 const struct sockaddr *addr = p;
cd28ab6a
SH
3062
3063 if (!is_valid_ether_addr(addr->sa_data))
3064 return -EADDRNOTAVAIL;
3065
cd28ab6a 3066 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
a8ab1ec0 3067 memcpy_toio(hw->regs + B2_MAC_1 + port * 8,
cd28ab6a 3068 dev->dev_addr, ETH_ALEN);
a8ab1ec0 3069 memcpy_toio(hw->regs + B2_MAC_2 + port * 8,
cd28ab6a 3070 dev->dev_addr, ETH_ALEN);
1b537565 3071
a8ab1ec0
SH
3072 /* virtual address for data */
3073 gma_set_addr(hw, port, GM_SRC_ADDR_2L, dev->dev_addr);
3074
3075 /* physical address: used for pause frames */
3076 gma_set_addr(hw, port, GM_SRC_ADDR_1L, dev->dev_addr);
1b537565
SH
3077
3078 return 0;
cd28ab6a
SH
3079}
3080
a052b52f
SH
3081static void inline sky2_add_filter(u8 filter[8], const u8 *addr)
3082{
3083 u32 bit;
3084
3085 bit = ether_crc(ETH_ALEN, addr) & 63;
3086 filter[bit >> 3] |= 1 << (bit & 7);
3087}
3088
cd28ab6a
SH
3089static void sky2_set_multicast(struct net_device *dev)
3090{
3091 struct sky2_port *sky2 = netdev_priv(dev);
3092 struct sky2_hw *hw = sky2->hw;
3093 unsigned port = sky2->port;
3094 struct dev_mc_list *list = dev->mc_list;
3095 u16 reg;
3096 u8 filter[8];
a052b52f
SH
3097 int rx_pause;
3098 static const u8 pause_mc_addr[ETH_ALEN] = { 0x1, 0x80, 0xc2, 0x0, 0x0, 0x1 };
cd28ab6a 3099
a052b52f 3100 rx_pause = (sky2->flow_status == FC_RX || sky2->flow_status == FC_BOTH);
cd28ab6a
SH
3101 memset(filter, 0, sizeof(filter));
3102
3103 reg = gma_read16(hw, port, GM_RX_CTRL);
3104 reg |= GM_RXCR_UCF_ENA;
3105
d571b694 3106 if (dev->flags & IFF_PROMISC) /* promiscuous */
cd28ab6a 3107 reg &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
a052b52f 3108 else if (dev->flags & IFF_ALLMULTI)
cd28ab6a 3109 memset(filter, 0xff, sizeof(filter));
a052b52f 3110 else if (dev->mc_count == 0 && !rx_pause)
cd28ab6a
SH
3111 reg &= ~GM_RXCR_MCF_ENA;
3112 else {
3113 int i;
3114 reg |= GM_RXCR_MCF_ENA;
3115
a052b52f
SH
3116 if (rx_pause)
3117 sky2_add_filter(filter, pause_mc_addr);
3118
3119 for (i = 0; list && i < dev->mc_count; i++, list = list->next)
3120 sky2_add_filter(filter, list->dmi_addr);
cd28ab6a
SH
3121 }
3122
cd28ab6a 3123 gma_write16(hw, port, GM_MC_ADDR_H1,
793b883e 3124 (u16) filter[0] | ((u16) filter[1] << 8));
cd28ab6a 3125 gma_write16(hw, port, GM_MC_ADDR_H2,
793b883e 3126 (u16) filter[2] | ((u16) filter[3] << 8));
cd28ab6a 3127 gma_write16(hw, port, GM_MC_ADDR_H3,
793b883e 3128 (u16) filter[4] | ((u16) filter[5] << 8));
cd28ab6a 3129 gma_write16(hw, port, GM_MC_ADDR_H4,
793b883e 3130 (u16) filter[6] | ((u16) filter[7] << 8));
cd28ab6a
SH
3131
3132 gma_write16(hw, port, GM_RX_CTRL, reg);
3133}
3134
3135/* Can have one global because blinking is controlled by
3136 * ethtool and that is always under RTNL mutex
3137 */
91c86df5 3138static void sky2_led(struct sky2_hw *hw, unsigned port, int on)
cd28ab6a 3139{
793b883e
SH
3140 u16 pg;
3141
793b883e
SH
3142 switch (hw->chip_id) {
3143 case CHIP_ID_YUKON_XL:
3144 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
3145 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
3146 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
3147 on ? (PHY_M_LEDC_LOS_CTRL(1) |
3148 PHY_M_LEDC_INIT_CTRL(7) |
3149 PHY_M_LEDC_STA1_CTRL(7) |
3150 PHY_M_LEDC_STA0_CTRL(7))
3151 : 0);
3152
3153 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
3154 break;
3155
3156 default:
3157 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
0efdf262
SH
3158 gm_phy_write(hw, port, PHY_MARV_LED_OVER,
3159 on ? PHY_M_LED_ALL : 0);
793b883e 3160 }
cd28ab6a
SH
3161}
3162
3163/* blink LED's for finding board */
3164static int sky2_phys_id(struct net_device *dev, u32 data)
3165{
3166 struct sky2_port *sky2 = netdev_priv(dev);
3167 struct sky2_hw *hw = sky2->hw;
3168 unsigned port = sky2->port;
793b883e 3169 u16 ledctrl, ledover = 0;
cd28ab6a 3170 long ms;
91c86df5 3171 int interrupted;
cd28ab6a
SH
3172 int onoff = 1;
3173
793b883e 3174 if (!data || data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ))
cd28ab6a
SH
3175 ms = jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT);
3176 else
3177 ms = data * 1000;
3178
3179 /* save initial values */
e07b1aa8 3180 spin_lock_bh(&sky2->phy_lock);
793b883e
SH
3181 if (hw->chip_id == CHIP_ID_YUKON_XL) {
3182 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
3183 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
3184 ledctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
3185 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
3186 } else {
3187 ledctrl = gm_phy_read(hw, port, PHY_MARV_LED_CTRL);
3188 ledover = gm_phy_read(hw, port, PHY_MARV_LED_OVER);
3189 }
cd28ab6a 3190
91c86df5
SH
3191 interrupted = 0;
3192 while (!interrupted && ms > 0) {
cd28ab6a
SH
3193 sky2_led(hw, port, onoff);
3194 onoff = !onoff;
3195
e07b1aa8 3196 spin_unlock_bh(&sky2->phy_lock);
91c86df5 3197 interrupted = msleep_interruptible(250);
e07b1aa8 3198 spin_lock_bh(&sky2->phy_lock);
91c86df5 3199
cd28ab6a
SH
3200 ms -= 250;
3201 }
3202
3203 /* resume regularly scheduled programming */
793b883e
SH
3204 if (hw->chip_id == CHIP_ID_YUKON_XL) {
3205 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
3206 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
3207 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ledctrl);
3208 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
3209 } else {
3210 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, ledctrl);
3211 gm_phy_write(hw, port, PHY_MARV_LED_OVER, ledover);
3212 }
e07b1aa8 3213 spin_unlock_bh(&sky2->phy_lock);
cd28ab6a
SH
3214
3215 return 0;
3216}
3217
3218static void sky2_get_pauseparam(struct net_device *dev,
3219 struct ethtool_pauseparam *ecmd)
3220{
3221 struct sky2_port *sky2 = netdev_priv(dev);
3222
16ad91e1
SH
3223 switch (sky2->flow_mode) {
3224 case FC_NONE:
3225 ecmd->tx_pause = ecmd->rx_pause = 0;
3226 break;
3227 case FC_TX:
3228 ecmd->tx_pause = 1, ecmd->rx_pause = 0;
3229 break;
3230 case FC_RX:
3231 ecmd->tx_pause = 0, ecmd->rx_pause = 1;
3232 break;
3233 case FC_BOTH:
3234 ecmd->tx_pause = ecmd->rx_pause = 1;
3235 }
3236
cd28ab6a
SH
3237 ecmd->autoneg = sky2->autoneg;
3238}
3239
3240static int sky2_set_pauseparam(struct net_device *dev,
3241 struct ethtool_pauseparam *ecmd)
3242{
3243 struct sky2_port *sky2 = netdev_priv(dev);
cd28ab6a
SH
3244
3245 sky2->autoneg = ecmd->autoneg;
16ad91e1 3246 sky2->flow_mode = sky2_flow(ecmd->rx_pause, ecmd->tx_pause);
cd28ab6a 3247
16ad91e1
SH
3248 if (netif_running(dev))
3249 sky2_phy_reinit(sky2);
cd28ab6a 3250
2eaba1a2 3251 return 0;
cd28ab6a
SH
3252}
3253
fb17358f
SH
3254static int sky2_get_coalesce(struct net_device *dev,
3255 struct ethtool_coalesce *ecmd)
3256{
3257 struct sky2_port *sky2 = netdev_priv(dev);
3258 struct sky2_hw *hw = sky2->hw;
3259
3260 if (sky2_read8(hw, STAT_TX_TIMER_CTRL) == TIM_STOP)
3261 ecmd->tx_coalesce_usecs = 0;
3262 else {
3263 u32 clks = sky2_read32(hw, STAT_TX_TIMER_INI);
3264 ecmd->tx_coalesce_usecs = sky2_clk2us(hw, clks);
3265 }
3266 ecmd->tx_max_coalesced_frames = sky2_read16(hw, STAT_TX_IDX_TH);
3267
3268 if (sky2_read8(hw, STAT_LEV_TIMER_CTRL) == TIM_STOP)
3269 ecmd->rx_coalesce_usecs = 0;
3270 else {
3271 u32 clks = sky2_read32(hw, STAT_LEV_TIMER_INI);
3272 ecmd->rx_coalesce_usecs = sky2_clk2us(hw, clks);
3273 }
3274 ecmd->rx_max_coalesced_frames = sky2_read8(hw, STAT_FIFO_WM);
3275
3276 if (sky2_read8(hw, STAT_ISR_TIMER_CTRL) == TIM_STOP)
3277 ecmd->rx_coalesce_usecs_irq = 0;
3278 else {
3279 u32 clks = sky2_read32(hw, STAT_ISR_TIMER_INI);
3280 ecmd->rx_coalesce_usecs_irq = sky2_clk2us(hw, clks);
3281 }
3282
3283 ecmd->rx_max_coalesced_frames_irq = sky2_read8(hw, STAT_FIFO_ISR_WM);
3284
3285 return 0;
3286}
3287
3288/* Note: this affect both ports */
3289static int sky2_set_coalesce(struct net_device *dev,
3290 struct ethtool_coalesce *ecmd)
3291{
3292 struct sky2_port *sky2 = netdev_priv(dev);
3293 struct sky2_hw *hw = sky2->hw;
77b3d6a2 3294 const u32 tmax = sky2_clk2us(hw, 0x0ffffff);
fb17358f 3295
77b3d6a2
SH
3296 if (ecmd->tx_coalesce_usecs > tmax ||
3297 ecmd->rx_coalesce_usecs > tmax ||
3298 ecmd->rx_coalesce_usecs_irq > tmax)
fb17358f
SH
3299 return -EINVAL;
3300
ff81fbbe 3301 if (ecmd->tx_max_coalesced_frames >= TX_RING_SIZE-1)
fb17358f 3302 return -EINVAL;
ff81fbbe 3303 if (ecmd->rx_max_coalesced_frames > RX_MAX_PENDING)
fb17358f 3304 return -EINVAL;
ff81fbbe 3305 if (ecmd->rx_max_coalesced_frames_irq >RX_MAX_PENDING)
fb17358f
SH
3306 return -EINVAL;
3307
3308 if (ecmd->tx_coalesce_usecs == 0)
3309 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_STOP);
3310 else {
3311 sky2_write32(hw, STAT_TX_TIMER_INI,
3312 sky2_us2clk(hw, ecmd->tx_coalesce_usecs));
3313 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
3314 }
3315 sky2_write16(hw, STAT_TX_IDX_TH, ecmd->tx_max_coalesced_frames);
3316
3317 if (ecmd->rx_coalesce_usecs == 0)
3318 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_STOP);
3319 else {
3320 sky2_write32(hw, STAT_LEV_TIMER_INI,
3321 sky2_us2clk(hw, ecmd->rx_coalesce_usecs));
3322 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
3323 }
3324 sky2_write8(hw, STAT_FIFO_WM, ecmd->rx_max_coalesced_frames);
3325
3326 if (ecmd->rx_coalesce_usecs_irq == 0)
3327 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_STOP);
3328 else {
d28d4870 3329 sky2_write32(hw, STAT_ISR_TIMER_INI,
fb17358f
SH
3330 sky2_us2clk(hw, ecmd->rx_coalesce_usecs_irq));
3331 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
3332 }
3333 sky2_write8(hw, STAT_FIFO_ISR_WM, ecmd->rx_max_coalesced_frames_irq);
3334 return 0;
3335}
3336
793b883e
SH
3337static void sky2_get_ringparam(struct net_device *dev,
3338 struct ethtool_ringparam *ering)
3339{
3340 struct sky2_port *sky2 = netdev_priv(dev);
3341
3342 ering->rx_max_pending = RX_MAX_PENDING;
3343 ering->rx_mini_max_pending = 0;
3344 ering->rx_jumbo_max_pending = 0;
3345 ering->tx_max_pending = TX_RING_SIZE - 1;
3346
3347 ering->rx_pending = sky2->rx_pending;
3348 ering->rx_mini_pending = 0;
3349 ering->rx_jumbo_pending = 0;
3350 ering->tx_pending = sky2->tx_pending;
3351}
3352
3353static int sky2_set_ringparam(struct net_device *dev,
3354 struct ethtool_ringparam *ering)
3355{
3356 struct sky2_port *sky2 = netdev_priv(dev);
3357 int err = 0;
3358
3359 if (ering->rx_pending > RX_MAX_PENDING ||
3360 ering->rx_pending < 8 ||
3361 ering->tx_pending < MAX_SKB_TX_LE ||
3362 ering->tx_pending > TX_RING_SIZE - 1)
3363 return -EINVAL;
3364
3365 if (netif_running(dev))
3366 sky2_down(dev);
3367
3368 sky2->rx_pending = ering->rx_pending;
3369 sky2->tx_pending = ering->tx_pending;
3370
1b537565 3371 if (netif_running(dev)) {
793b883e 3372 err = sky2_up(dev);
1b537565
SH
3373 if (err)
3374 dev_close(dev);
6ed995bb
SH
3375 else
3376 sky2_set_multicast(dev);
1b537565 3377 }
793b883e
SH
3378
3379 return err;
3380}
3381
793b883e
SH
3382static int sky2_get_regs_len(struct net_device *dev)
3383{
6e4cbb34 3384 return 0x4000;
793b883e
SH
3385}
3386
3387/*
3388 * Returns copy of control register region
3ead5db7 3389 * Note: ethtool_get_regs always provides full size (16k) buffer
793b883e
SH
3390 */
3391static void sky2_get_regs(struct net_device *dev, struct ethtool_regs *regs,
3392 void *p)
3393{
3394 const struct sky2_port *sky2 = netdev_priv(dev);
793b883e 3395 const void __iomem *io = sky2->hw->regs;
793b883e
SH
3396
3397 regs->version = 1;
6e4cbb34 3398 memset(p, 0, regs->len);
793b883e 3399
6e4cbb34
SH
3400 memcpy_fromio(p, io, B3_RAM_ADDR);
3401
3ead5db7
SH
3402 /* skip diagnostic ram region */
3403 memcpy_fromio(p + B3_RI_WTO_R1, io + B3_RI_WTO_R1, 0x2000 - B3_RI_WTO_R1);
3404
3405 /* copy GMAC registers */
3406 memcpy_fromio(p + BASE_GMAC_1, io + BASE_GMAC_1, 0x1000);
3407 if (sky2->hw->ports > 1)
3408 memcpy_fromio(p + BASE_GMAC_2, io + BASE_GMAC_2, 0x1000);
3409
793b883e 3410}
cd28ab6a 3411
b628ed98
SH
3412/* In order to do Jumbo packets on these chips, need to turn off the
3413 * transmit store/forward. Therefore checksum offload won't work.
3414 */
3415static int no_tx_offload(struct net_device *dev)
3416{
3417 const struct sky2_port *sky2 = netdev_priv(dev);
3418 const struct sky2_hw *hw = sky2->hw;
3419
69161611 3420 return dev->mtu > ETH_DATA_LEN && hw->chip_id == CHIP_ID_YUKON_EC_U;
b628ed98
SH
3421}
3422
3423static int sky2_set_tx_csum(struct net_device *dev, u32 data)
3424{
3425 if (data && no_tx_offload(dev))
3426 return -EINVAL;
3427
3428 return ethtool_op_set_tx_csum(dev, data);
3429}
3430
3431
3432static int sky2_set_tso(struct net_device *dev, u32 data)
3433{
3434 if (data && no_tx_offload(dev))
3435 return -EINVAL;
3436
3437 return ethtool_op_set_tso(dev, data);
3438}
3439
7282d491 3440static const struct ethtool_ops sky2_ethtool_ops = {
793b883e
SH
3441 .get_settings = sky2_get_settings,
3442 .set_settings = sky2_set_settings,
e3173832
SH
3443 .get_drvinfo = sky2_get_drvinfo,
3444 .get_wol = sky2_get_wol,
3445 .set_wol = sky2_set_wol,
793b883e
SH
3446 .get_msglevel = sky2_get_msglevel,
3447 .set_msglevel = sky2_set_msglevel,
9a7ae0a9 3448 .nway_reset = sky2_nway_reset,
793b883e
SH
3449 .get_regs_len = sky2_get_regs_len,
3450 .get_regs = sky2_get_regs,
3451 .get_link = ethtool_op_get_link,
3452 .get_sg = ethtool_op_get_sg,
3453 .set_sg = ethtool_op_set_sg,
3454 .get_tx_csum = ethtool_op_get_tx_csum,
b628ed98 3455 .set_tx_csum = sky2_set_tx_csum,
793b883e 3456 .get_tso = ethtool_op_get_tso,
b628ed98 3457 .set_tso = sky2_set_tso,
793b883e
SH
3458 .get_rx_csum = sky2_get_rx_csum,
3459 .set_rx_csum = sky2_set_rx_csum,
3460 .get_strings = sky2_get_strings,
fb17358f
SH
3461 .get_coalesce = sky2_get_coalesce,
3462 .set_coalesce = sky2_set_coalesce,
793b883e
SH
3463 .get_ringparam = sky2_get_ringparam,
3464 .set_ringparam = sky2_set_ringparam,
cd28ab6a
SH
3465 .get_pauseparam = sky2_get_pauseparam,
3466 .set_pauseparam = sky2_set_pauseparam,
793b883e 3467 .phys_id = sky2_phys_id,
cd28ab6a
SH
3468 .get_stats_count = sky2_get_stats_count,
3469 .get_ethtool_stats = sky2_get_ethtool_stats,
2995bfb7 3470 .get_perm_addr = ethtool_op_get_perm_addr,
cd28ab6a
SH
3471};
3472
3473/* Initialize network device */
3474static __devinit struct net_device *sky2_init_netdev(struct sky2_hw *hw,
e3173832
SH
3475 unsigned port,
3476 int highmem, int wol)
cd28ab6a
SH
3477{
3478 struct sky2_port *sky2;
3479 struct net_device *dev = alloc_etherdev(sizeof(*sky2));
3480
3481 if (!dev) {
b02a9258 3482 dev_err(&hw->pdev->dev, "etherdev alloc failed");
cd28ab6a
SH
3483 return NULL;
3484 }
3485
3486 SET_MODULE_OWNER(dev);
3487 SET_NETDEV_DEV(dev, &hw->pdev->dev);
ef743d33 3488 dev->irq = hw->pdev->irq;
cd28ab6a
SH
3489 dev->open = sky2_up;
3490 dev->stop = sky2_down;
ef743d33 3491 dev->do_ioctl = sky2_ioctl;
cd28ab6a
SH
3492 dev->hard_start_xmit = sky2_xmit_frame;
3493 dev->get_stats = sky2_get_stats;
3494 dev->set_multicast_list = sky2_set_multicast;
3495 dev->set_mac_address = sky2_set_mac_address;
3496 dev->change_mtu = sky2_change_mtu;
3497 SET_ETHTOOL_OPS(dev, &sky2_ethtool_ops);
3498 dev->tx_timeout = sky2_tx_timeout;
3499 dev->watchdog_timeo = TX_WATCHDOG;
3500 if (port == 0)
3501 dev->poll = sky2_poll;
3502 dev->weight = NAPI_WEIGHT;
3503#ifdef CONFIG_NET_POLL_CONTROLLER
0ca43235
SH
3504 /* Network console (only works on port 0)
3505 * because netpoll makes assumptions about NAPI
3506 */
3507 if (port == 0)
3508 dev->poll_controller = sky2_netpoll;
cd28ab6a 3509#endif
cd28ab6a
SH
3510
3511 sky2 = netdev_priv(dev);
3512 sky2->netdev = dev;
3513 sky2->hw = hw;
3514 sky2->msg_enable = netif_msg_init(debug, default_msg);
3515
cd28ab6a
SH
3516 /* Auto speed and flow control */
3517 sky2->autoneg = AUTONEG_ENABLE;
16ad91e1
SH
3518 sky2->flow_mode = FC_BOTH;
3519
cd28ab6a
SH
3520 sky2->duplex = -1;
3521 sky2->speed = -1;
3522 sky2->advertising = sky2_supported_modes(hw);
ee7abb04 3523 sky2->rx_csum = 1;
e3173832 3524 sky2->wol = wol;
75d070c5 3525
e07b1aa8 3526 spin_lock_init(&sky2->phy_lock);
793b883e 3527 sky2->tx_pending = TX_DEF_PENDING;
290d4de5 3528 sky2->rx_pending = RX_DEF_PENDING;
cd28ab6a
SH
3529
3530 hw->dev[port] = dev;
3531
3532 sky2->port = port;
3533
4a50a876 3534 dev->features |= NETIF_F_TSO | NETIF_F_IP_CSUM | NETIF_F_SG;
cd28ab6a
SH
3535 if (highmem)
3536 dev->features |= NETIF_F_HIGHDMA;
cd28ab6a 3537
d1f13708 3538#ifdef SKY2_VLAN_TAG_USED
3539 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3540 dev->vlan_rx_register = sky2_vlan_rx_register;
d1f13708 3541#endif
3542
cd28ab6a 3543 /* read the mac address */
793b883e 3544 memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
2995bfb7 3545 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
cd28ab6a 3546
cd28ab6a
SH
3547 return dev;
3548}
3549
28bd181a 3550static void __devinit sky2_show_addr(struct net_device *dev)
cd28ab6a
SH
3551{
3552 const struct sky2_port *sky2 = netdev_priv(dev);
3553
3554 if (netif_msg_probe(sky2))
3555 printk(KERN_INFO PFX "%s: addr %02x:%02x:%02x:%02x:%02x:%02x\n",
3556 dev->name,
3557 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
3558 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
3559}
3560
fb2690a9 3561/* Handle software interrupt used during MSI test */
7d12e780 3562static irqreturn_t __devinit sky2_test_intr(int irq, void *dev_id)
fb2690a9
SH
3563{
3564 struct sky2_hw *hw = dev_id;
3565 u32 status = sky2_read32(hw, B0_Y2_SP_ISRC2);
3566
3567 if (status == 0)
3568 return IRQ_NONE;
3569
3570 if (status & Y2_IS_IRQ_SW) {
b0a20ded 3571 hw->msi = 1;
fb2690a9
SH
3572 wake_up(&hw->msi_wait);
3573 sky2_write8(hw, B0_CTST, CS_CL_SW_IRQ);
3574 }
3575 sky2_write32(hw, B0_Y2_SP_ICR, 2);
3576
3577 return IRQ_HANDLED;
3578}
3579
3580/* Test interrupt path by forcing a a software IRQ */
3581static int __devinit sky2_test_msi(struct sky2_hw *hw)
3582{
3583 struct pci_dev *pdev = hw->pdev;
3584 int err;
3585
bb507fe1 3586 init_waitqueue_head (&hw->msi_wait);
3587
fb2690a9
SH
3588 sky2_write32(hw, B0_IMSK, Y2_IS_IRQ_SW);
3589
b0a20ded 3590 err = request_irq(pdev->irq, sky2_test_intr, 0, DRV_NAME, hw);
fb2690a9 3591 if (err) {
b02a9258 3592 dev_err(&pdev->dev, "cannot assign irq %d\n", pdev->irq);
fb2690a9
SH
3593 return err;
3594 }
3595
fb2690a9 3596 sky2_write8(hw, B0_CTST, CS_ST_SW_IRQ);
bb507fe1 3597 sky2_read8(hw, B0_CTST);
fb2690a9 3598
b0a20ded 3599 wait_event_timeout(hw->msi_wait, hw->msi, HZ/10);
fb2690a9 3600
b0a20ded 3601 if (!hw->msi) {
fb2690a9 3602 /* MSI test failed, go back to INTx mode */
b02a9258
SH
3603 dev_info(&pdev->dev, "No interrupt generated using MSI, "
3604 "switching to INTx mode.\n");
fb2690a9
SH
3605
3606 err = -EOPNOTSUPP;
3607 sky2_write8(hw, B0_CTST, CS_CL_SW_IRQ);
3608 }
3609
3610 sky2_write32(hw, B0_IMSK, 0);
2bffc23a 3611 sky2_read32(hw, B0_IMSK);
fb2690a9
SH
3612
3613 free_irq(pdev->irq, hw);
3614
3615 return err;
3616}
3617
e3173832
SH
3618static int __devinit pci_wake_enabled(struct pci_dev *dev)
3619{
3620 int pm = pci_find_capability(dev, PCI_CAP_ID_PM);
3621 u16 value;
3622
3623 if (!pm)
3624 return 0;
3625 if (pci_read_config_word(dev, pm + PCI_PM_CTRL, &value))
3626 return 0;
3627 return value & PCI_PM_CTRL_PME_ENABLE;
3628}
3629
cd28ab6a
SH
3630static int __devinit sky2_probe(struct pci_dev *pdev,
3631 const struct pci_device_id *ent)
3632{
7f60c64b 3633 struct net_device *dev;
cd28ab6a 3634 struct sky2_hw *hw;
e3173832 3635 int err, using_dac = 0, wol_default;
cd28ab6a 3636
793b883e
SH
3637 err = pci_enable_device(pdev);
3638 if (err) {
b02a9258 3639 dev_err(&pdev->dev, "cannot enable PCI device\n");
cd28ab6a
SH
3640 goto err_out;
3641 }
3642
793b883e
SH
3643 err = pci_request_regions(pdev, DRV_NAME);
3644 if (err) {
b02a9258 3645 dev_err(&pdev->dev, "cannot obtain PCI resources\n");
44a1d2e5 3646 goto err_out_disable;
cd28ab6a
SH
3647 }
3648
3649 pci_set_master(pdev);
3650
d1f3d4dd
SH
3651 if (sizeof(dma_addr_t) > sizeof(u32) &&
3652 !(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
3653 using_dac = 1;
3654 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3655 if (err < 0) {
b02a9258
SH
3656 dev_err(&pdev->dev, "unable to obtain 64 bit DMA "
3657 "for consistent allocations\n");
d1f3d4dd
SH
3658 goto err_out_free_regions;
3659 }
d1f3d4dd 3660 } else {
cd28ab6a
SH
3661 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3662 if (err) {
b02a9258 3663 dev_err(&pdev->dev, "no usable DMA configuration\n");
cd28ab6a
SH
3664 goto err_out_free_regions;
3665 }
3666 }
d1f3d4dd 3667
e3173832
SH
3668 wol_default = pci_wake_enabled(pdev) ? WAKE_MAGIC : 0;
3669
cd28ab6a 3670 err = -ENOMEM;
6aad85d6 3671 hw = kzalloc(sizeof(*hw), GFP_KERNEL);
cd28ab6a 3672 if (!hw) {
b02a9258 3673 dev_err(&pdev->dev, "cannot allocate hardware struct\n");
cd28ab6a
SH
3674 goto err_out_free_regions;
3675 }
3676
cd28ab6a 3677 hw->pdev = pdev;
cd28ab6a
SH
3678
3679 hw->regs = ioremap_nocache(pci_resource_start(pdev, 0), 0x4000);
3680 if (!hw->regs) {
b02a9258 3681 dev_err(&pdev->dev, "cannot map device registers\n");
cd28ab6a
SH
3682 goto err_out_free_hw;
3683 }
3684
56a645cc 3685#ifdef __BIG_ENDIAN
f65b138c
SH
3686 /* The sk98lin vendor driver uses hardware byte swapping but
3687 * this driver uses software swapping.
3688 */
56a645cc
SH
3689 {
3690 u32 reg;
56a645cc 3691 reg = sky2_pci_read32(hw, PCI_DEV_REG2);
f65b138c 3692 reg &= ~PCI_REV_DESC;
56a645cc
SH
3693 sky2_pci_write32(hw, PCI_DEV_REG2, reg);
3694 }
3695#endif
3696
08c06d8a
SH
3697 /* ring for status responses */
3698 hw->st_le = pci_alloc_consistent(hw->pdev, STATUS_LE_BYTES,
3699 &hw->st_dma);
3700 if (!hw->st_le)
3701 goto err_out_iounmap;
3702
e3173832 3703 err = sky2_init(hw);
cd28ab6a 3704 if (err)
793b883e 3705 goto err_out_iounmap;
cd28ab6a 3706
b02a9258 3707 dev_info(&pdev->dev, "v%s addr 0x%llx irq %d Yukon-%s (0x%x) rev %d\n",
7c7459d1
GKH
3708 DRV_VERSION, (unsigned long long)pci_resource_start(pdev, 0),
3709 pdev->irq, yukon2_name[hw->chip_id - CHIP_ID_YUKON_XL],
793b883e 3710 hw->chip_id, hw->chip_rev);
cd28ab6a 3711
e3173832
SH
3712 sky2_reset(hw);
3713
3714 dev = sky2_init_netdev(hw, 0, using_dac, wol_default);
7f60c64b 3715 if (!dev) {
3716 err = -ENOMEM;
cd28ab6a 3717 goto err_out_free_pci;
7f60c64b 3718 }
cd28ab6a 3719
9fa1b1f3
SH
3720 if (!disable_msi && pci_enable_msi(pdev) == 0) {
3721 err = sky2_test_msi(hw);
3722 if (err == -EOPNOTSUPP)
3723 pci_disable_msi(pdev);
3724 else if (err)
3725 goto err_out_free_netdev;
3726 }
3727
793b883e
SH
3728 err = register_netdev(dev);
3729 if (err) {
b02a9258 3730 dev_err(&pdev->dev, "cannot register net device\n");
cd28ab6a
SH
3731 goto err_out_free_netdev;
3732 }
3733
b0a20ded
SH
3734 err = request_irq(pdev->irq, sky2_intr, hw->msi ? 0 : IRQF_SHARED,
3735 dev->name, hw);
9fa1b1f3 3736 if (err) {
b02a9258 3737 dev_err(&pdev->dev, "cannot assign irq %d\n", pdev->irq);
9fa1b1f3
SH
3738 goto err_out_unregister;
3739 }
3740 sky2_write32(hw, B0_IMSK, Y2_IS_BASE);
3741
cd28ab6a
SH
3742 sky2_show_addr(dev);
3743
7f60c64b 3744 if (hw->ports > 1) {
3745 struct net_device *dev1;
3746
e3173832 3747 dev1 = sky2_init_netdev(hw, 1, using_dac, wol_default);
b02a9258
SH
3748 if (!dev1)
3749 dev_warn(&pdev->dev, "allocation for second device failed\n");
3750 else if ((err = register_netdev(dev1))) {
3751 dev_warn(&pdev->dev,
3752 "register of second port failed (%d)\n", err);
cd28ab6a
SH
3753 hw->dev[1] = NULL;
3754 free_netdev(dev1);
b02a9258
SH
3755 } else
3756 sky2_show_addr(dev1);
cd28ab6a
SH
3757 }
3758
01bd7564 3759 setup_timer(&hw->idle_timer, sky2_idle, (unsigned long) hw);
81906791
SH
3760 INIT_WORK(&hw->restart_work, sky2_restart);
3761
eb35cf60 3762 sky2_idle_start(hw);
d27ed387 3763
793b883e
SH
3764 pci_set_drvdata(pdev, hw);
3765
cd28ab6a
SH
3766 return 0;
3767
793b883e 3768err_out_unregister:
b0a20ded
SH
3769 if (hw->msi)
3770 pci_disable_msi(pdev);
793b883e 3771 unregister_netdev(dev);
cd28ab6a
SH
3772err_out_free_netdev:
3773 free_netdev(dev);
cd28ab6a 3774err_out_free_pci:
793b883e 3775 sky2_write8(hw, B0_CTST, CS_RST_SET);
cd28ab6a
SH
3776 pci_free_consistent(hw->pdev, STATUS_LE_BYTES, hw->st_le, hw->st_dma);
3777err_out_iounmap:
3778 iounmap(hw->regs);
3779err_out_free_hw:
3780 kfree(hw);
3781err_out_free_regions:
3782 pci_release_regions(pdev);
44a1d2e5 3783err_out_disable:
cd28ab6a 3784 pci_disable_device(pdev);
cd28ab6a 3785err_out:
549a68c3 3786 pci_set_drvdata(pdev, NULL);
cd28ab6a
SH
3787 return err;
3788}
3789
3790static void __devexit sky2_remove(struct pci_dev *pdev)
3791{
793b883e 3792 struct sky2_hw *hw = pci_get_drvdata(pdev);
cd28ab6a
SH
3793 struct net_device *dev0, *dev1;
3794
793b883e 3795 if (!hw)
cd28ab6a
SH
3796 return;
3797
d27ed387
SH
3798 del_timer_sync(&hw->idle_timer);
3799
81906791
SH
3800 flush_scheduled_work();
3801
d27ed387 3802 sky2_write32(hw, B0_IMSK, 0);
72cb8529
SH
3803 synchronize_irq(hw->pdev->irq);
3804
cd28ab6a 3805 dev0 = hw->dev[0];
793b883e
SH
3806 dev1 = hw->dev[1];
3807 if (dev1)
3808 unregister_netdev(dev1);
cd28ab6a
SH
3809 unregister_netdev(dev0);
3810
ae306cca
SH
3811 sky2_power_aux(hw);
3812
cd28ab6a 3813 sky2_write16(hw, B0_Y2LED, LED_STAT_OFF);
793b883e 3814 sky2_write8(hw, B0_CTST, CS_RST_SET);
5afa0a9c 3815 sky2_read8(hw, B0_CTST);
cd28ab6a
SH
3816
3817 free_irq(pdev->irq, hw);
b0a20ded
SH
3818 if (hw->msi)
3819 pci_disable_msi(pdev);
793b883e 3820 pci_free_consistent(pdev, STATUS_LE_BYTES, hw->st_le, hw->st_dma);
cd28ab6a
SH
3821 pci_release_regions(pdev);
3822 pci_disable_device(pdev);
793b883e 3823
cd28ab6a
SH
3824 if (dev1)
3825 free_netdev(dev1);
3826 free_netdev(dev0);
3827 iounmap(hw->regs);
3828 kfree(hw);
5afa0a9c 3829
cd28ab6a
SH
3830 pci_set_drvdata(pdev, NULL);
3831}
3832
3833#ifdef CONFIG_PM
3834static int sky2_suspend(struct pci_dev *pdev, pm_message_t state)
3835{
793b883e 3836 struct sky2_hw *hw = pci_get_drvdata(pdev);
e3173832 3837 int i, wol = 0;
cd28ab6a 3838
549a68c3
SH
3839 if (!hw)
3840 return 0;
3841
eb35cf60 3842 del_timer_sync(&hw->idle_timer);
6a5706b9 3843 netif_poll_disable(hw->dev[0]);
eb35cf60 3844
f05267e7 3845 for (i = 0; i < hw->ports; i++) {
cd28ab6a 3846 struct net_device *dev = hw->dev[i];
e3173832 3847 struct sky2_port *sky2 = netdev_priv(dev);
cd28ab6a 3848
e3173832 3849 if (netif_running(dev))
5afa0a9c 3850 sky2_down(dev);
e3173832
SH
3851
3852 if (sky2->wol)
3853 sky2_wol_init(sky2);
3854
3855 wol |= sky2->wol;
cd28ab6a
SH
3856 }
3857
8ab8fca2 3858 sky2_write32(hw, B0_IMSK, 0);
ae306cca 3859 sky2_power_aux(hw);
e3173832 3860
d374c1c1 3861 pci_save_state(pdev);
e3173832 3862 pci_enable_wake(pdev, pci_choose_state(pdev, state), wol);
ae306cca
SH
3863 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3864
2ccc99b7 3865 return 0;
cd28ab6a
SH
3866}
3867
3868static int sky2_resume(struct pci_dev *pdev)
3869{
793b883e 3870 struct sky2_hw *hw = pci_get_drvdata(pdev);
08c06d8a 3871 int i, err;
cd28ab6a 3872
549a68c3
SH
3873 if (!hw)
3874 return 0;
3875
ae306cca
SH
3876 err = pci_set_power_state(pdev, PCI_D0);
3877 if (err)
3878 goto out;
3879
3880 err = pci_restore_state(pdev);
3881 if (err)
3882 goto out;
3883
cd28ab6a 3884 pci_enable_wake(pdev, PCI_D0, 0);
1ad5b4a5
SH
3885
3886 /* Re-enable all clocks */
3887 if (hw->chip_id == CHIP_ID_YUKON_EX || hw->chip_id == CHIP_ID_YUKON_EC_U)
3888 sky2_pci_write32(hw, PCI_DEV_REG3, 0);
3889
e3173832 3890 sky2_reset(hw);
cd28ab6a 3891
8ab8fca2
SH
3892 sky2_write32(hw, B0_IMSK, Y2_IS_BASE);
3893
f05267e7 3894 for (i = 0; i < hw->ports; i++) {
cd28ab6a 3895 struct net_device *dev = hw->dev[i];
6a5706b9 3896 if (netif_running(dev)) {
08c06d8a
SH
3897 err = sky2_up(dev);
3898 if (err) {
3899 printk(KERN_ERR PFX "%s: could not up: %d\n",
3900 dev->name, err);
3901 dev_close(dev);
eb35cf60 3902 goto out;
5afa0a9c 3903 }
cd28ab6a
SH
3904 }
3905 }
eb35cf60 3906
6a5706b9 3907 netif_poll_enable(hw->dev[0]);
eb35cf60 3908 sky2_idle_start(hw);
ae306cca 3909 return 0;
08c06d8a 3910out:
b02a9258 3911 dev_err(&pdev->dev, "resume failed (%d)\n", err);
ae306cca 3912 pci_disable_device(pdev);
08c06d8a 3913 return err;
cd28ab6a
SH
3914}
3915#endif
3916
e3173832
SH
3917static void sky2_shutdown(struct pci_dev *pdev)
3918{
3919 struct sky2_hw *hw = pci_get_drvdata(pdev);
3920 int i, wol = 0;
3921
549a68c3
SH
3922 if (!hw)
3923 return;
3924
e3173832
SH
3925 del_timer_sync(&hw->idle_timer);
3926 netif_poll_disable(hw->dev[0]);
3927
3928 for (i = 0; i < hw->ports; i++) {
3929 struct net_device *dev = hw->dev[i];
3930 struct sky2_port *sky2 = netdev_priv(dev);
3931
3932 if (sky2->wol) {
3933 wol = 1;
3934 sky2_wol_init(sky2);
3935 }
3936 }
3937
3938 if (wol)
3939 sky2_power_aux(hw);
3940
3941 pci_enable_wake(pdev, PCI_D3hot, wol);
3942 pci_enable_wake(pdev, PCI_D3cold, wol);
3943
3944 pci_disable_device(pdev);
3945 pci_set_power_state(pdev, PCI_D3hot);
3946
3947}
3948
cd28ab6a 3949static struct pci_driver sky2_driver = {
793b883e
SH
3950 .name = DRV_NAME,
3951 .id_table = sky2_id_table,
3952 .probe = sky2_probe,
3953 .remove = __devexit_p(sky2_remove),
cd28ab6a 3954#ifdef CONFIG_PM
793b883e
SH
3955 .suspend = sky2_suspend,
3956 .resume = sky2_resume,
cd28ab6a 3957#endif
e3173832 3958 .shutdown = sky2_shutdown,
cd28ab6a
SH
3959};
3960
3961static int __init sky2_init_module(void)
3962{
50241c4c 3963 return pci_register_driver(&sky2_driver);
cd28ab6a
SH
3964}
3965
3966static void __exit sky2_cleanup_module(void)
3967{
3968 pci_unregister_driver(&sky2_driver);
3969}
3970
3971module_init(sky2_init_module);
3972module_exit(sky2_cleanup_module);
3973
3974MODULE_DESCRIPTION("Marvell Yukon 2 Gigabit Ethernet driver");
65ebe634 3975MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
cd28ab6a 3976MODULE_LICENSE("GPL");
5f4f9dc1 3977MODULE_VERSION(DRV_VERSION);