]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/sky2.c
[PATCH] e100: e100 whitespace fixes
[mirror_ubuntu-bionic-kernel.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
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
793b883e 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cd28ab6a
SH
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26/*
cd28ab6a
SH
27 * TOTEST
28 * - speed setting
724bca3c 29 * - suspend/resume
cd28ab6a
SH
30 */
31
32#include <linux/config.h>
793b883e 33#include <linux/crc32.h>
cd28ab6a
SH
34#include <linux/kernel.h>
35#include <linux/version.h>
36#include <linux/module.h>
37#include <linux/netdevice.h>
d0bbccfa 38#include <linux/dma-mapping.h>
cd28ab6a
SH
39#include <linux/etherdevice.h>
40#include <linux/ethtool.h>
41#include <linux/pci.h>
42#include <linux/ip.h>
43#include <linux/tcp.h>
44#include <linux/in.h>
45#include <linux/delay.h>
91c86df5 46#include <linux/workqueue.h>
d1f13708 47#include <linux/if_vlan.h>
d70cd51a 48#include <linux/prefetch.h>
ef743d33 49#include <linux/mii.h>
cd28ab6a
SH
50
51#include <asm/irq.h>
52
d1f13708
SH
53#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
54#define SKY2_VLAN_TAG_USED 1
55#endif
56
cd28ab6a
SH
57#include "sky2.h"
58
59#define DRV_NAME "sky2"
c4c48d83 60#define DRV_VERSION "0.11"
cd28ab6a
SH
61#define PFX DRV_NAME " "
62
63/*
64 * The Yukon II chipset takes 64 bit command blocks (called list elements)
65 * that are organized into three (receive, transmit, status) different rings
66 * similar to Tigon3. A transmit can require several elements;
67 * a receive requires one (or two if using 64 bit dma).
68 */
69
cd28ab6a 70#define is_ec_a1(hw) \
21437643
SH
71 unlikely((hw)->chip_id == CHIP_ID_YUKON_EC && \
72 (hw)->chip_rev == CHIP_REV_YU_EC_A1)
cd28ab6a 73
13210ce5 74#define RX_LE_SIZE 512
cd28ab6a 75#define RX_LE_BYTES (RX_LE_SIZE*sizeof(struct sky2_rx_le))
bea86103 76#define RX_MAX_PENDING (RX_LE_SIZE/2 - 2)
13210ce5 77#define RX_DEF_PENDING RX_MAX_PENDING
793b883e
SH
78
79#define TX_RING_SIZE 512
80#define TX_DEF_PENDING (TX_RING_SIZE - 1)
81#define TX_MIN_PENDING 64
82#define MAX_SKB_TX_LE (4 + 2*MAX_SKB_FRAGS)
cd28ab6a 83
793b883e 84#define STATUS_RING_SIZE 2048 /* 2 ports * (TX + 2*RX) */
cd28ab6a
SH
85#define STATUS_LE_BYTES (STATUS_RING_SIZE*sizeof(struct sky2_status_le))
86#define ETH_JUMBO_MTU 9000
87#define TX_WATCHDOG (5 * HZ)
88#define NAPI_WEIGHT 64
89#define PHY_RETRIES 1000
90
91static const u32 default_msg =
793b883e
SH
92 NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
93 | NETIF_MSG_TIMER | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR
94 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_INTR;
cd28ab6a 95
793b883e 96static int debug = -1; /* defaults above */
cd28ab6a
SH
97module_param(debug, int, 0);
98MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
99
bdb5c58e
SH
100static int copybreak __read_mostly = 256;
101module_param(copybreak, int, 0);
102MODULE_PARM_DESC(copybreak, "Receive copy threshold");
103
cd28ab6a 104static const struct pci_device_id sky2_id_table[] = {
793b883e 105 { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9000) },
cd28ab6a
SH
106 { PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, 0x9E00) },
107 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4b00) },
108 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4b01) },
109 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4340) },
110 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4341) },
111 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4342) },
112 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4343) },
113 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4344) },
114 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4345) },
115 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4346) },
116 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4347) },
117 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4350) },
118 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4351) },
5a5b1ea0 119 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4352) },
cd28ab6a
SH
120 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4360) },
121 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4361) },
122 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4362) },
5a5b1ea0 123 { PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x4363) },
cd28ab6a
SH
124 { 0 }
125};
793b883e 126
cd28ab6a
SH
127MODULE_DEVICE_TABLE(pci, sky2_id_table);
128
129/* Avoid conditionals by using array */
130static const unsigned txqaddr[] = { Q_XA1, Q_XA2 };
131static const unsigned rxqaddr[] = { Q_R1, Q_R2 };
132
92f965e8
SH
133/* This driver supports yukon2 chipset only */
134static const char *yukon2_name[] = {
135 "XL", /* 0xb3 */
136 "EC Ultra", /* 0xb4 */
137 "UNKNOWN", /* 0xb5 */
138 "EC", /* 0xb6 */
139 "FE", /* 0xb7 */
793b883e
SH
140};
141
793b883e 142/* Access to external PHY */
ef743d33 143static int gm_phy_write(struct sky2_hw *hw, unsigned port, u16 reg, u16 val)
cd28ab6a
SH
144{
145 int i;
146
147 gma_write16(hw, port, GM_SMI_DATA, val);
148 gma_write16(hw, port, GM_SMI_CTRL,
149 GM_SMI_CT_PHY_AD(PHY_ADDR_MARV) | GM_SMI_CT_REG_AD(reg));
150
151 for (i = 0; i < PHY_RETRIES; i++) {
cd28ab6a 152 if (!(gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_BUSY))
ef743d33 153 return 0;
793b883e 154 udelay(1);
cd28ab6a 155 }
ef743d33 156
793b883e 157 printk(KERN_WARNING PFX "%s: phy write timeout\n", hw->dev[port]->name);
ef743d33 158 return -ETIMEDOUT;
cd28ab6a
SH
159}
160
ef743d33 161static int __gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg, u16 *val)
cd28ab6a
SH
162{
163 int i;
164
793b883e 165 gma_write16(hw, port, GM_SMI_CTRL, GM_SMI_CT_PHY_AD(PHY_ADDR_MARV)
cd28ab6a
SH
166 | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
167
168 for (i = 0; i < PHY_RETRIES; i++) {
ef743d33
SH
169 if (gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_RD_VAL) {
170 *val = gma_read16(hw, port, GM_SMI_DATA);
171 return 0;
172 }
173
793b883e 174 udelay(1);
cd28ab6a
SH
175 }
176
ef743d33
SH
177 return -ETIMEDOUT;
178}
179
180static u16 gm_phy_read(struct sky2_hw *hw, unsigned port, u16 reg)
181{
182 u16 v;
183
184 if (__gm_phy_read(hw, port, reg, &v) != 0)
185 printk(KERN_WARNING PFX "%s: phy read timeout\n", hw->dev[port]->name);
186 return v;
cd28ab6a
SH
187}
188
5afa0a9c
SH
189static int sky2_set_power_state(struct sky2_hw *hw, pci_power_t state)
190{
191 u16 power_control;
192 u32 reg1;
193 int vaux;
194 int ret = 0;
195
196 pr_debug("sky2_set_power_state %d\n", state);
197 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
198
199 pci_read_config_word(hw->pdev, hw->pm_cap + PCI_PM_PMC, &power_control);
200 vaux = (sky2_read8(hw, B0_CTST) & Y2_VAUX_AVAIL) &&
201 (power_control & PCI_PM_CAP_PME_D3cold);
202
203 pci_read_config_word(hw->pdev, hw->pm_cap + PCI_PM_CTRL, &power_control);
204
205 power_control |= PCI_PM_CTRL_PME_STATUS;
206 power_control &= ~(PCI_PM_CTRL_STATE_MASK);
207
208 switch (state) {
209 case PCI_D0:
210 /* switch power to VCC (WA for VAUX problem) */
211 sky2_write8(hw, B0_POWER_CTRL,
212 PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
213
214 /* disable Core Clock Division, */
215 sky2_write32(hw, B2_Y2_CLK_CTRL, Y2_CLK_DIV_DIS);
216
217 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
218 /* enable bits are inverted */
219 sky2_write8(hw, B2_Y2_CLK_GATE,
220 Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
221 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
222 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
223 else
224 sky2_write8(hw, B2_Y2_CLK_GATE, 0);
225
226 /* Turn off phy power saving */
227 pci_read_config_dword(hw->pdev, PCI_DEV_REG1, &reg1);
228 reg1 &= ~(PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD);
229
d571b694 230 /* looks like this XL is back asswards .. */
5afa0a9c
SH
231 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1) {
232 reg1 |= PCI_Y2_PHY1_COMA;
233 if (hw->ports > 1)
234 reg1 |= PCI_Y2_PHY2_COMA;
235 }
236 pci_write_config_dword(hw->pdev, PCI_DEV_REG1, reg1);
237 break;
238
239 case PCI_D3hot:
240 case PCI_D3cold:
241 /* Turn on phy power saving */
242 pci_read_config_dword(hw->pdev, PCI_DEV_REG1, &reg1);
243 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
244 reg1 &= ~(PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD);
245 else
246 reg1 |= (PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD);
247 pci_write_config_dword(hw->pdev, PCI_DEV_REG1, reg1);
248
249 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev > 1)
250 sky2_write8(hw, B2_Y2_CLK_GATE, 0);
251 else
252 /* enable bits are inverted */
253 sky2_write8(hw, B2_Y2_CLK_GATE,
254 Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS |
255 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS |
256 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS);
257
258 /* switch power to VAUX */
259 if (vaux && state != PCI_D3cold)
260 sky2_write8(hw, B0_POWER_CTRL,
261 (PC_VAUX_ENA | PC_VCC_ENA |
262 PC_VAUX_ON | PC_VCC_OFF));
263 break;
264 default:
265 printk(KERN_ERR PFX "Unknown power state %d\n", state);
266 ret = -1;
267 }
268
269 pci_write_config_byte(hw->pdev, hw->pm_cap + PCI_PM_CTRL, power_control);
270 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
271 return ret;
272}
273
cd28ab6a
SH
274static void sky2_phy_reset(struct sky2_hw *hw, unsigned port)
275{
276 u16 reg;
277
278 /* disable all GMAC IRQ's */
279 sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
280 /* disable PHY IRQs */
281 gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);
793b883e 282
cd28ab6a
SH
283 gma_write16(hw, port, GM_MC_ADDR_H1, 0); /* clear MC hash */
284 gma_write16(hw, port, GM_MC_ADDR_H2, 0);
285 gma_write16(hw, port, GM_MC_ADDR_H3, 0);
286 gma_write16(hw, port, GM_MC_ADDR_H4, 0);
287
288 reg = gma_read16(hw, port, GM_RX_CTRL);
289 reg |= GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA;
290 gma_write16(hw, port, GM_RX_CTRL, reg);
291}
292
293static void sky2_phy_init(struct sky2_hw *hw, unsigned port)
294{
295 struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
793b883e 296 u16 ctrl, ct1000, adv, pg, ledctrl, ledover;
cd28ab6a 297
793b883e 298 if (sky2->autoneg == AUTONEG_ENABLE && hw->chip_id != CHIP_ID_YUKON_XL) {
cd28ab6a
SH
299 u16 ectrl = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
300
301 ectrl &= ~(PHY_M_EC_M_DSC_MSK | PHY_M_EC_S_DSC_MSK |
793b883e 302 PHY_M_EC_MAC_S_MSK);
cd28ab6a
SH
303 ectrl |= PHY_M_EC_MAC_S(MAC_TX_CLK_25_MHZ);
304
305 if (hw->chip_id == CHIP_ID_YUKON_EC)
306 ectrl |= PHY_M_EC_DSC_2(2) | PHY_M_EC_DOWN_S_ENA;
307 else
308 ectrl |= PHY_M_EC_M_DSC(2) | PHY_M_EC_S_DSC(3);
309
310 gm_phy_write(hw, port, PHY_MARV_EXT_CTRL, ectrl);
311 }
312
313 ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
314 if (hw->copper) {
315 if (hw->chip_id == CHIP_ID_YUKON_FE) {
316 /* enable automatic crossover */
317 ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO) >> 1;
318 } else {
319 /* disable energy detect */
320 ctrl &= ~PHY_M_PC_EN_DET_MSK;
321
322 /* enable automatic crossover */
323 ctrl |= PHY_M_PC_MDI_XMODE(PHY_M_PC_ENA_AUTO);
324
325 if (sky2->autoneg == AUTONEG_ENABLE &&
326 hw->chip_id == CHIP_ID_YUKON_XL) {
327 ctrl &= ~PHY_M_PC_DSC_MSK;
328 ctrl |= PHY_M_PC_DSC(2) | PHY_M_PC_DOWN_S_ENA;
329 }
330 }
331 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
332 } else {
333 /* workaround for deviation #4.88 (CRC errors) */
334 /* disable Automatic Crossover */
335
336 ctrl &= ~PHY_M_PC_MDIX_MSK;
337 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
338
339 if (hw->chip_id == CHIP_ID_YUKON_XL) {
340 /* Fiber: select 1000BASE-X only mode MAC Specific Ctrl Reg. */
341 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 2);
342 ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
343 ctrl &= ~PHY_M_MAC_MD_MSK;
344 ctrl |= PHY_M_MAC_MODE_SEL(PHY_M_MAC_MD_1000BX);
345 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
346
347 /* select page 1 to access Fiber registers */
348 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 1);
349 }
cd28ab6a
SH
350 }
351
352 ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
353 if (sky2->autoneg == AUTONEG_DISABLE)
354 ctrl &= ~PHY_CT_ANE;
355 else
356 ctrl |= PHY_CT_ANE;
357
358 ctrl |= PHY_CT_RESET;
359 gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
360
361 ctrl = 0;
362 ct1000 = 0;
363 adv = PHY_AN_CSMA;
364
365 if (sky2->autoneg == AUTONEG_ENABLE) {
366 if (hw->copper) {
367 if (sky2->advertising & ADVERTISED_1000baseT_Full)
368 ct1000 |= PHY_M_1000C_AFD;
369 if (sky2->advertising & ADVERTISED_1000baseT_Half)
370 ct1000 |= PHY_M_1000C_AHD;
371 if (sky2->advertising & ADVERTISED_100baseT_Full)
372 adv |= PHY_M_AN_100_FD;
373 if (sky2->advertising & ADVERTISED_100baseT_Half)
374 adv |= PHY_M_AN_100_HD;
375 if (sky2->advertising & ADVERTISED_10baseT_Full)
376 adv |= PHY_M_AN_10_FD;
377 if (sky2->advertising & ADVERTISED_10baseT_Half)
378 adv |= PHY_M_AN_10_HD;
793b883e 379 } else /* special defines for FIBER (88E1011S only) */
cd28ab6a
SH
380 adv |= PHY_M_AN_1000X_AHD | PHY_M_AN_1000X_AFD;
381
382 /* Set Flow-control capabilities */
383 if (sky2->tx_pause && sky2->rx_pause)
793b883e 384 adv |= PHY_AN_PAUSE_CAP; /* symmetric */
cd28ab6a 385 else if (sky2->rx_pause && !sky2->tx_pause)
793b883e 386 adv |= PHY_AN_PAUSE_ASYM | PHY_AN_PAUSE_CAP;
cd28ab6a
SH
387 else if (!sky2->rx_pause && sky2->tx_pause)
388 adv |= PHY_AN_PAUSE_ASYM; /* local */
389
390 /* Restart Auto-negotiation */
391 ctrl |= PHY_CT_ANE | PHY_CT_RE_CFG;
392 } else {
393 /* forced speed/duplex settings */
394 ct1000 = PHY_M_1000C_MSE;
395
396 if (sky2->duplex == DUPLEX_FULL)
397 ctrl |= PHY_CT_DUP_MD;
398
399 switch (sky2->speed) {
400 case SPEED_1000:
401 ctrl |= PHY_CT_SP1000;
402 break;
403 case SPEED_100:
404 ctrl |= PHY_CT_SP100;
405 break;
406 }
407
408 ctrl |= PHY_CT_RESET;
409 }
410
411 if (hw->chip_id != CHIP_ID_YUKON_FE)
412 gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, ct1000);
413
414 gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, adv);
415 gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
416
417 /* Setup Phy LED's */
418 ledctrl = PHY_M_LED_PULS_DUR(PULS_170MS);
419 ledover = 0;
420
421 switch (hw->chip_id) {
422 case CHIP_ID_YUKON_FE:
423 /* on 88E3082 these bits are at 11..9 (shifted left) */
424 ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) << 1;
425
426 ctrl = gm_phy_read(hw, port, PHY_MARV_FE_LED_PAR);
427
428 /* delete ACT LED control bits */
429 ctrl &= ~PHY_M_FELP_LED1_MSK;
430 /* change ACT LED control to blink mode */
431 ctrl |= PHY_M_FELP_LED1_CTRL(LED_PAR_CTRL_ACT_BL);
432 gm_phy_write(hw, port, PHY_MARV_FE_LED_PAR, ctrl);
433 break;
434
435 case CHIP_ID_YUKON_XL:
793b883e 436 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
cd28ab6a
SH
437
438 /* select page 3 to access LED control register */
439 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
440
441 /* set LED Function Control register */
793b883e
SH
442 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, (PHY_M_LEDC_LOS_CTRL(1) | /* LINK/ACT */
443 PHY_M_LEDC_INIT_CTRL(7) | /* 10 Mbps */
444 PHY_M_LEDC_STA1_CTRL(7) | /* 100 Mbps */
445 PHY_M_LEDC_STA0_CTRL(7))); /* 1000 Mbps */
cd28ab6a
SH
446
447 /* set Polarity Control register */
448 gm_phy_write(hw, port, PHY_MARV_PHY_STAT,
793b883e
SH
449 (PHY_M_POLC_LS1_P_MIX(4) |
450 PHY_M_POLC_IS0_P_MIX(4) |
451 PHY_M_POLC_LOS_CTRL(2) |
452 PHY_M_POLC_INIT_CTRL(2) |
453 PHY_M_POLC_STA1_CTRL(2) |
454 PHY_M_POLC_STA0_CTRL(2)));
cd28ab6a
SH
455
456 /* restore page register */
793b883e 457 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
cd28ab6a
SH
458 break;
459
460 default:
461 /* set Tx LED (LED_TX) to blink mode on Rx OR Tx activity */
462 ledctrl |= PHY_M_LED_BLINK_RT(BLINK_84MS) | PHY_M_LEDC_TX_CTRL;
463 /* turn off the Rx LED (LED_RX) */
464 ledover |= PHY_M_LED_MO_RX(MO_LED_OFF);
465 }
466
467 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, ledctrl);
468
469 if (sky2->autoneg == AUTONEG_DISABLE || sky2->speed == SPEED_100) {
470 /* turn on 100 Mbps LED (LED_LINK100) */
471 ledover |= PHY_M_LED_MO_100(MO_LED_ON);
472 }
473
474 if (ledover)
475 gm_phy_write(hw, port, PHY_MARV_LED_OVER, ledover);
476
d571b694 477 /* Enable phy interrupt on auto-negotiation complete (or link up) */
cd28ab6a
SH
478 if (sky2->autoneg == AUTONEG_ENABLE)
479 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_AN_COMPL);
480 else
481 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
482}
483
1b537565
SH
484/* Force a renegotiation */
485static void sky2_phy_reinit(struct sky2_port *sky2)
486{
487 down(&sky2->phy_sema);
488 sky2_phy_init(sky2->hw, sky2->port);
489 up(&sky2->phy_sema);
490}
491
cd28ab6a
SH
492static void sky2_mac_init(struct sky2_hw *hw, unsigned port)
493{
494 struct sky2_port *sky2 = netdev_priv(hw->dev[port]);
495 u16 reg;
496 int i;
497 const u8 *addr = hw->dev[port]->dev_addr;
498
42eeea01
SH
499 sky2_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
500 sky2_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_CLR|GPC_ENA_PAUSE);
cd28ab6a
SH
501
502 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_CLR);
503
793b883e 504 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0 && port == 1) {
cd28ab6a
SH
505 /* WA DEV_472 -- looks like crossed wires on port 2 */
506 /* clear GMAC 1 Control reset */
507 sky2_write8(hw, SK_REG(0, GMAC_CTRL), GMC_RST_CLR);
508 do {
509 sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_SET);
510 sky2_write8(hw, SK_REG(1, GMAC_CTRL), GMC_RST_CLR);
511 } while (gm_phy_read(hw, 1, PHY_MARV_ID0) != PHY_MARV_ID0_VAL ||
512 gm_phy_read(hw, 1, PHY_MARV_ID1) != PHY_MARV_ID1_Y2 ||
513 gm_phy_read(hw, 1, PHY_MARV_INT_MASK) != 0);
514 }
515
cd28ab6a
SH
516 if (sky2->autoneg == AUTONEG_DISABLE) {
517 reg = gma_read16(hw, port, GM_GP_CTRL);
518 reg |= GM_GPCR_AU_ALL_DIS;
519 gma_write16(hw, port, GM_GP_CTRL, reg);
520 gma_read16(hw, port, GM_GP_CTRL);
521
cd28ab6a
SH
522 switch (sky2->speed) {
523 case SPEED_1000:
524 reg |= GM_GPCR_SPEED_1000;
525 /* fallthru */
526 case SPEED_100:
527 reg |= GM_GPCR_SPEED_100;
528 }
529
530 if (sky2->duplex == DUPLEX_FULL)
531 reg |= GM_GPCR_DUP_FULL;
532 } else
533 reg = GM_GPCR_SPEED_1000 | GM_GPCR_SPEED_100 | GM_GPCR_DUP_FULL;
534
535 if (!sky2->tx_pause && !sky2->rx_pause) {
536 sky2_write32(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
793b883e
SH
537 reg |=
538 GM_GPCR_FC_TX_DIS | GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
539 } else if (sky2->tx_pause && !sky2->rx_pause) {
cd28ab6a
SH
540 /* disable Rx flow-control */
541 reg |= GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
542 }
543
544 gma_write16(hw, port, GM_GP_CTRL, reg);
545
793b883e 546 sky2_read16(hw, SK_REG(port, GMAC_IRQ_SRC));
cd28ab6a 547
91c86df5 548 down(&sky2->phy_sema);
cd28ab6a 549 sky2_phy_init(hw, port);
91c86df5 550 up(&sky2->phy_sema);
cd28ab6a
SH
551
552 /* MIB clear */
553 reg = gma_read16(hw, port, GM_PHY_ADDR);
554 gma_write16(hw, port, GM_PHY_ADDR, reg | GM_PAR_MIB_CLR);
555
556 for (i = 0; i < GM_MIB_CNT_SIZE; i++)
793b883e 557 gma_read16(hw, port, GM_MIB_CNT_BASE + 8 * i);
cd28ab6a
SH
558 gma_write16(hw, port, GM_PHY_ADDR, reg);
559
560 /* transmit control */
561 gma_write16(hw, port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
562
563 /* receive control reg: unicast + multicast + no FCS */
564 gma_write16(hw, port, GM_RX_CTRL,
793b883e 565 GM_RXCR_UCF_ENA | GM_RXCR_CRC_DIS | GM_RXCR_MCF_ENA);
cd28ab6a
SH
566
567 /* transmit flow control */
568 gma_write16(hw, port, GM_TX_FLOW_CTRL, 0xffff);
569
570 /* transmit parameter */
571 gma_write16(hw, port, GM_TX_PARAM,
572 TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) |
573 TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
574 TX_IPG_JAM_DATA(TX_IPG_JAM_DEF) |
575 TX_BACK_OFF_LIM(TX_BOF_LIM_DEF));
576
577 /* serial mode register */
578 reg = DATA_BLIND_VAL(DATA_BLIND_DEF) |
6b1a3aef 579 GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF);
cd28ab6a 580
6b1a3aef 581 if (hw->dev[port]->mtu > ETH_DATA_LEN)
cd28ab6a
SH
582 reg |= GM_SMOD_JUMBO_ENA;
583
584 gma_write16(hw, port, GM_SERIAL_MODE, reg);
585
cd28ab6a
SH
586 /* virtual address for data */
587 gma_set_addr(hw, port, GM_SRC_ADDR_2L, addr);
588
793b883e
SH
589 /* physical address: used for pause frames */
590 gma_set_addr(hw, port, GM_SRC_ADDR_1L, addr);
591
592 /* ignore counter overflows */
cd28ab6a
SH
593 gma_write16(hw, port, GM_TX_IRQ_MSK, 0);
594 gma_write16(hw, port, GM_RX_IRQ_MSK, 0);
595 gma_write16(hw, port, GM_TR_IRQ_MSK, 0);
596
597 /* Configure Rx MAC FIFO */
598 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_CLR);
793b883e 599 sky2_write16(hw, SK_REG(port, RX_GMF_CTRL_T),
d1f13708 600 GMF_RX_CTRL_DEF);
cd28ab6a 601
d571b694 602 /* Flush Rx MAC FIFO on any flow control or error */
42eeea01 603 sky2_write16(hw, SK_REG(port, RX_GMF_FL_MSK), GMR_FS_ANY_ERR);
cd28ab6a 604
793b883e
SH
605 /* Set threshold to 0xa (64 bytes)
606 * ASF disabled so no need to do WA dev #4.30
cd28ab6a
SH
607 */
608 sky2_write16(hw, SK_REG(port, RX_GMF_FL_THR), RX_GMF_FL_THR_DEF);
609
610 /* Configure Tx MAC FIFO */
611 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_CLR);
612 sky2_write16(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_OPER_ON);
5a5b1ea0
SH
613
614 if (hw->chip_id == CHIP_ID_YUKON_EC_U) {
615 sky2_write8(hw, SK_REG(port, RX_GMF_LP_THR), 768/8);
616 sky2_write8(hw, SK_REG(port, RX_GMF_UP_THR), 1024/8);
617 if (hw->dev[port]->mtu > ETH_DATA_LEN) {
618 /* set Tx GMAC FIFO Almost Empty Threshold */
619 sky2_write32(hw, SK_REG(port, TX_GMF_AE_THR), 0x180);
620 /* Disable Store & Forward mode for TX */
621 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_STFW_DIS);
622 }
623 }
624
cd28ab6a
SH
625}
626
627static void sky2_ramset(struct sky2_hw *hw, u16 q, u32 start, size_t len)
628{
629 u32 end;
630
631 start /= 8;
632 len /= 8;
633 end = start + len - 1;
793b883e 634
cd28ab6a
SH
635 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_RST_CLR);
636 sky2_write32(hw, RB_ADDR(q, RB_START), start);
637 sky2_write32(hw, RB_ADDR(q, RB_END), end);
638 sky2_write32(hw, RB_ADDR(q, RB_WP), start);
639 sky2_write32(hw, RB_ADDR(q, RB_RP), start);
640
641 if (q == Q_R1 || q == Q_R2) {
793b883e
SH
642 u32 rxup, rxlo;
643
644 rxlo = len/2;
645 rxup = rxlo + len/4;
793b883e 646
cd28ab6a 647 /* Set thresholds on receive queue's */
793b883e
SH
648 sky2_write32(hw, RB_ADDR(q, RB_RX_UTPP), rxup);
649 sky2_write32(hw, RB_ADDR(q, RB_RX_LTPP), rxlo);
cd28ab6a
SH
650 } else {
651 /* Enable store & forward on Tx queue's because
652 * Tx FIFO is only 1K on Yukon
653 */
654 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_STFWD);
655 }
656
657 sky2_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_OP_MD);
793b883e 658 sky2_read8(hw, RB_ADDR(q, RB_CTRL));
cd28ab6a
SH
659}
660
cd28ab6a 661/* Setup Bus Memory Interface */
af4ed7e6 662static void sky2_qset(struct sky2_hw *hw, u16 q)
cd28ab6a
SH
663{
664 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_CLR_RESET);
665 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_OPER_INIT);
666 sky2_write32(hw, Q_ADDR(q, Q_CSR), BMU_FIFO_OP_ON);
af4ed7e6 667 sky2_write32(hw, Q_ADDR(q, Q_WM), BMU_WM_DEFAULT);
cd28ab6a
SH
668}
669
cd28ab6a
SH
670/* Setup prefetch unit registers. This is the interface between
671 * hardware and driver list elements
672 */
8cc048e3 673static void sky2_prefetch_init(struct sky2_hw *hw, u32 qaddr,
cd28ab6a
SH
674 u64 addr, u32 last)
675{
cd28ab6a
SH
676 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
677 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_RST_CLR);
678 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_HI), addr >> 32);
679 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_ADDR_LO), (u32) addr);
680 sky2_write16(hw, Y2_QADDR(qaddr, PREF_UNIT_LAST_IDX), last);
681 sky2_write32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL), PREF_UNIT_OP_ON);
793b883e
SH
682
683 sky2_read32(hw, Y2_QADDR(qaddr, PREF_UNIT_CTRL));
cd28ab6a
SH
684}
685
793b883e
SH
686static inline struct sky2_tx_le *get_tx_le(struct sky2_port *sky2)
687{
688 struct sky2_tx_le *le = sky2->tx_le + sky2->tx_prod;
689
690 sky2->tx_prod = (sky2->tx_prod + 1) % TX_RING_SIZE;
691 return le;
692}
cd28ab6a
SH
693
694/*
d571b694 695 * This is a workaround code taken from SysKonnect sk98lin driver
793b883e 696 * to deal with chip bug on Yukon EC rev 0 in the wraparound case.
cd28ab6a
SH
697 */
698static inline void sky2_put_idx(struct sky2_hw *hw, unsigned q,
699 u16 idx, u16 *last, u16 size)
cd28ab6a 700{
cd28ab6a
SH
701 if (is_ec_a1(hw) && idx < *last) {
702 u16 hwget = sky2_read16(hw, Y2_QADDR(q, PREF_UNIT_GET_IDX));
703
704 if (hwget == 0) {
705 /* Start prefetching again */
793b883e 706 sky2_write8(hw, Y2_QADDR(q, PREF_UNIT_FIFO_WM), 0xe0);
cd28ab6a
SH
707 goto setnew;
708 }
709
793b883e 710 if (hwget == size - 1) {
cd28ab6a
SH
711 /* set watermark to one list element */
712 sky2_write8(hw, Y2_QADDR(q, PREF_UNIT_FIFO_WM), 8);
713
714 /* set put index to first list element */
715 sky2_write16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX), 0);
793b883e
SH
716 } else /* have hardware go to end of list */
717 sky2_write16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX),
718 size - 1);
cd28ab6a 719 } else {
793b883e 720setnew:
cd28ab6a 721 sky2_write16(hw, Y2_QADDR(q, PREF_UNIT_PUT_IDX), idx);
cd28ab6a 722 }
bea86103 723 *last = idx;
cd28ab6a
SH
724}
725
793b883e 726
cd28ab6a
SH
727static inline struct sky2_rx_le *sky2_next_rx(struct sky2_port *sky2)
728{
729 struct sky2_rx_le *le = sky2->rx_le + sky2->rx_put;
730 sky2->rx_put = (sky2->rx_put + 1) % RX_LE_SIZE;
731 return le;
732}
733
a018e330
SH
734/* Return high part of DMA address (could be 32 or 64 bit) */
735static inline u32 high32(dma_addr_t a)
736{
737 return (a >> 16) >> 16;
738}
739
793b883e 740/* Build description to hardware about buffer */
734d1868 741static inline void sky2_rx_add(struct sky2_port *sky2, dma_addr_t map)
cd28ab6a
SH
742{
743 struct sky2_rx_le *le;
734d1868
SH
744 u32 hi = high32(map);
745 u16 len = sky2->rx_bufsize;
cd28ab6a 746
793b883e 747 if (sky2->rx_addr64 != hi) {
cd28ab6a 748 le = sky2_next_rx(sky2);
793b883e 749 le->addr = cpu_to_le32(hi);
cd28ab6a
SH
750 le->ctrl = 0;
751 le->opcode = OP_ADDR64 | HW_OWNER;
734d1868 752 sky2->rx_addr64 = high32(map + len);
cd28ab6a 753 }
793b883e 754
cd28ab6a 755 le = sky2_next_rx(sky2);
734d1868
SH
756 le->addr = cpu_to_le32((u32) map);
757 le->length = cpu_to_le16(len);
cd28ab6a
SH
758 le->ctrl = 0;
759 le->opcode = OP_PACKET | HW_OWNER;
760}
761
793b883e 762
cd28ab6a
SH
763/* Tell chip where to start receive checksum.
764 * Actually has two checksums, but set both same to avoid possible byte
765 * order problems.
766 */
793b883e 767static void rx_set_checksum(struct sky2_port *sky2)
cd28ab6a
SH
768{
769 struct sky2_rx_le *le;
770
cd28ab6a 771 le = sky2_next_rx(sky2);
793b883e 772 le->addr = (ETH_HLEN << 16) | ETH_HLEN;
cd28ab6a
SH
773 le->ctrl = 0;
774 le->opcode = OP_TCPSTART | HW_OWNER;
793b883e 775
793b883e
SH
776 sky2_write32(sky2->hw,
777 Q_ADDR(rxqaddr[sky2->port], Q_CSR),
778 sky2->rx_csum ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
cd28ab6a
SH
779
780}
781
6b1a3aef
SH
782/*
783 * The RX Stop command will not work for Yukon-2 if the BMU does not
784 * reach the end of packet and since we can't make sure that we have
785 * incoming data, we must reset the BMU while it is not doing a DMA
786 * transfer. Since it is possible that the RX path is still active,
787 * the RX RAM buffer will be stopped first, so any possible incoming
788 * data will not trigger a DMA. After the RAM buffer is stopped, the
789 * BMU is polled until any DMA in progress is ended and only then it
790 * will be reset.
791 */
792static void sky2_rx_stop(struct sky2_port *sky2)
793{
794 struct sky2_hw *hw = sky2->hw;
795 unsigned rxq = rxqaddr[sky2->port];
796 int i;
797
798 /* disable the RAM Buffer receive queue */
799 sky2_write8(hw, RB_ADDR(rxq, RB_CTRL), RB_DIS_OP_MD);
800
801 for (i = 0; i < 0xffff; i++)
802 if (sky2_read8(hw, RB_ADDR(rxq, Q_RSL))
803 == sky2_read8(hw, RB_ADDR(rxq, Q_RL)))
804 goto stopped;
805
806 printk(KERN_WARNING PFX "%s: receiver stop failed\n",
807 sky2->netdev->name);
808stopped:
809 sky2_write32(hw, Q_ADDR(rxq, Q_CSR), BMU_RST_SET | BMU_FIFO_RST);
810
811 /* reset the Rx prefetch unit */
812 sky2_write32(hw, Y2_QADDR(rxq, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
813}
793b883e 814
d571b694 815/* Clean out receive buffer area, assumes receiver hardware stopped */
cd28ab6a
SH
816static void sky2_rx_clean(struct sky2_port *sky2)
817{
818 unsigned i;
819
820 memset(sky2->rx_le, 0, RX_LE_BYTES);
793b883e 821 for (i = 0; i < sky2->rx_pending; i++) {
cd28ab6a
SH
822 struct ring_info *re = sky2->rx_ring + i;
823
824 if (re->skb) {
793b883e 825 pci_unmap_single(sky2->hw->pdev,
734d1868 826 re->mapaddr, sky2->rx_bufsize,
cd28ab6a
SH
827 PCI_DMA_FROMDEVICE);
828 kfree_skb(re->skb);
829 re->skb = NULL;
830 }
831 }
832}
833
ef743d33
SH
834/* Basic MII support */
835static int sky2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
836{
837 struct mii_ioctl_data *data = if_mii(ifr);
838 struct sky2_port *sky2 = netdev_priv(dev);
839 struct sky2_hw *hw = sky2->hw;
840 int err = -EOPNOTSUPP;
841
842 if (!netif_running(dev))
843 return -ENODEV; /* Phy still in reset */
844
845 switch(cmd) {
846 case SIOCGMIIPHY:
847 data->phy_id = PHY_ADDR_MARV;
848
849 /* fallthru */
850 case SIOCGMIIREG: {
851 u16 val = 0;
91c86df5
SH
852
853 down(&sky2->phy_sema);
ef743d33 854 err = __gm_phy_read(hw, sky2->port, data->reg_num & 0x1f, &val);
91c86df5
SH
855 up(&sky2->phy_sema);
856
ef743d33
SH
857 data->val_out = val;
858 break;
859 }
860
861 case SIOCSMIIREG:
862 if (!capable(CAP_NET_ADMIN))
863 return -EPERM;
864
91c86df5 865 down(&sky2->phy_sema);
ef743d33
SH
866 err = gm_phy_write(hw, sky2->port, data->reg_num & 0x1f,
867 data->val_in);
91c86df5 868 up(&sky2->phy_sema);
ef743d33
SH
869 break;
870 }
871 return err;
872}
873
d1f13708
SH
874#ifdef SKY2_VLAN_TAG_USED
875static void sky2_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
876{
877 struct sky2_port *sky2 = netdev_priv(dev);
878 struct sky2_hw *hw = sky2->hw;
879 u16 port = sky2->port;
d1f13708 880
f2e46561 881 spin_lock(&sky2->tx_lock);
d1f13708
SH
882
883 sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T), RX_VLAN_STRIP_ON);
884 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_VLAN_TAG_ON);
885 sky2->vlgrp = grp;
886
f2e46561 887 spin_unlock(&sky2->tx_lock);
d1f13708
SH
888}
889
890static void sky2_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
891{
892 struct sky2_port *sky2 = netdev_priv(dev);
893 struct sky2_hw *hw = sky2->hw;
894 u16 port = sky2->port;
d1f13708 895
f2e46561 896 spin_lock(&sky2->tx_lock);
d1f13708
SH
897
898 sky2_write32(hw, SK_REG(port, RX_GMF_CTRL_T), RX_VLAN_STRIP_OFF);
899 sky2_write32(hw, SK_REG(port, TX_GMF_CTRL_T), TX_VLAN_TAG_OFF);
900 if (sky2->vlgrp)
901 sky2->vlgrp->vlan_devices[vid] = NULL;
902
f2e46561 903 spin_unlock(&sky2->tx_lock);
d1f13708
SH
904}
905#endif
906
cd28ab6a
SH
907/*
908 * Allocate and setup receiver buffer pool.
909 * In case of 64 bit dma, there are 2X as many list elements
910 * available as ring entries
911 * and need to reserve one list element so we don't wrap around.
79e57d32
SH
912 *
913 * It appears the hardware has a bug in the FIFO logic that
914 * cause it to hang if the FIFO gets overrun and the receive buffer
915 * is not aligned. This means we can't use skb_reserve to align
916 * the IP header.
cd28ab6a 917 */
6b1a3aef 918static int sky2_rx_start(struct sky2_port *sky2)
cd28ab6a 919{
6b1a3aef 920 struct sky2_hw *hw = sky2->hw;
6b1a3aef
SH
921 unsigned rxq = rxqaddr[sky2->port];
922 int i;
cd28ab6a 923
6b1a3aef 924 sky2->rx_put = sky2->rx_next = 0;
af4ed7e6 925 sky2_qset(hw, rxq);
6b1a3aef
SH
926 sky2_prefetch_init(hw, rxq, sky2->rx_le_map, RX_LE_SIZE - 1);
927
928 rx_set_checksum(sky2);
793b883e 929 for (i = 0; i < sky2->rx_pending; i++) {
cd28ab6a 930 struct ring_info *re = sky2->rx_ring + i;
cd28ab6a 931
734d1868 932 re->skb = dev_alloc_skb(sky2->rx_bufsize);
cd28ab6a
SH
933 if (!re->skb)
934 goto nomem;
935
6b1a3aef 936 re->mapaddr = pci_map_single(hw->pdev, re->skb->data,
734d1868
SH
937 sky2->rx_bufsize, PCI_DMA_FROMDEVICE);
938 sky2_rx_add(sky2, re->mapaddr);
cd28ab6a
SH
939 }
940
6b1a3aef
SH
941 /* Tell chip about available buffers */
942 sky2_write16(hw, Y2_QADDR(rxq, PREF_UNIT_PUT_IDX), sky2->rx_put);
943 sky2->rx_last_put = sky2_read16(hw, Y2_QADDR(rxq, PREF_UNIT_PUT_IDX));
cd28ab6a
SH
944 return 0;
945nomem:
946 sky2_rx_clean(sky2);
947 return -ENOMEM;
948}
949
950/* Bring up network interface. */
951static int sky2_up(struct net_device *dev)
952{
953 struct sky2_port *sky2 = netdev_priv(dev);
954 struct sky2_hw *hw = sky2->hw;
955 unsigned port = sky2->port;
956 u32 ramsize, rxspace;
957 int err = -ENOMEM;
958
959 if (netif_msg_ifup(sky2))
960 printk(KERN_INFO PFX "%s: enabling interface\n", dev->name);
961
962 /* must be power of 2 */
963 sky2->tx_le = pci_alloc_consistent(hw->pdev,
793b883e
SH
964 TX_RING_SIZE *
965 sizeof(struct sky2_tx_le),
cd28ab6a
SH
966 &sky2->tx_le_map);
967 if (!sky2->tx_le)
968 goto err_out;
969
6cdbbdf3 970 sky2->tx_ring = kcalloc(TX_RING_SIZE, sizeof(struct tx_ring_info),
cd28ab6a
SH
971 GFP_KERNEL);
972 if (!sky2->tx_ring)
973 goto err_out;
974 sky2->tx_prod = sky2->tx_cons = 0;
cd28ab6a
SH
975
976 sky2->rx_le = pci_alloc_consistent(hw->pdev, RX_LE_BYTES,
977 &sky2->rx_le_map);
978 if (!sky2->rx_le)
979 goto err_out;
980 memset(sky2->rx_le, 0, RX_LE_BYTES);
981
6cdbbdf3 982 sky2->rx_ring = kcalloc(sky2->rx_pending, sizeof(struct ring_info),
cd28ab6a
SH
983 GFP_KERNEL);
984 if (!sky2->rx_ring)
985 goto err_out;
986
987 sky2_mac_init(hw, port);
988
989 /* Configure RAM buffers */
990 if (hw->chip_id == CHIP_ID_YUKON_FE ||
991 (hw->chip_id == CHIP_ID_YUKON_EC && hw->chip_rev == 2))
992 ramsize = 4096;
993 else {
793b883e
SH
994 u8 e0 = sky2_read8(hw, B2_E_0);
995 ramsize = (e0 == 0) ? (128 * 1024) : (e0 * 4096);
cd28ab6a
SH
996 }
997
998 /* 2/3 for Rx */
999 rxspace = (2 * ramsize) / 3;
1000 sky2_ramset(hw, rxqaddr[port], 0, rxspace);
1001 sky2_ramset(hw, txqaddr[port], rxspace, ramsize - rxspace);
1002
793b883e
SH
1003 /* Make sure SyncQ is disabled */
1004 sky2_write8(hw, RB_ADDR(port == 0 ? Q_XS1 : Q_XS2, RB_CTRL),
1005 RB_RST_SET);
1006
af4ed7e6 1007 sky2_qset(hw, txqaddr[port]);
5a5b1ea0
SH
1008 if (hw->chip_id == CHIP_ID_YUKON_EC_U)
1009 sky2_write16(hw, Q_ADDR(txqaddr[port], Q_AL), 0x1a0);
1010
1011
6b1a3aef
SH
1012 sky2_prefetch_init(hw, txqaddr[port], sky2->tx_le_map,
1013 TX_RING_SIZE - 1);
cd28ab6a 1014
6b1a3aef 1015 err = sky2_rx_start(sky2);
cd28ab6a
SH
1016 if (err)
1017 goto err_out;
1018
cd28ab6a
SH
1019 /* Enable interrupts from phy/mac for port */
1020 hw->intr_mask |= (port == 0) ? Y2_IS_PORT_1 : Y2_IS_PORT_2;
1021 sky2_write32(hw, B0_IMSK, hw->intr_mask);
1022 return 0;
1023
1024err_out:
1b537565 1025 if (sky2->rx_le) {
cd28ab6a
SH
1026 pci_free_consistent(hw->pdev, RX_LE_BYTES,
1027 sky2->rx_le, sky2->rx_le_map);
1b537565
SH
1028 sky2->rx_le = NULL;
1029 }
1030 if (sky2->tx_le) {
cd28ab6a
SH
1031 pci_free_consistent(hw->pdev,
1032 TX_RING_SIZE * sizeof(struct sky2_tx_le),
1033 sky2->tx_le, sky2->tx_le_map);
1b537565
SH
1034 sky2->tx_le = NULL;
1035 }
1036 kfree(sky2->tx_ring);
1037 kfree(sky2->rx_ring);
cd28ab6a 1038
1b537565
SH
1039 sky2->tx_ring = NULL;
1040 sky2->rx_ring = NULL;
cd28ab6a
SH
1041 return err;
1042}
1043
793b883e
SH
1044/* Modular subtraction in ring */
1045static inline int tx_dist(unsigned tail, unsigned head)
1046{
129372d0 1047 return (head - tail) % TX_RING_SIZE;
793b883e 1048}
cd28ab6a 1049
793b883e
SH
1050/* Number of list elements available for next tx */
1051static inline int tx_avail(const struct sky2_port *sky2)
cd28ab6a 1052{
793b883e 1053 return sky2->tx_pending - tx_dist(sky2->tx_cons, sky2->tx_prod);
cd28ab6a
SH
1054}
1055
793b883e
SH
1056/* Estimate of number of transmit list elements required */
1057static inline unsigned tx_le_req(const struct sk_buff *skb)
cd28ab6a 1058{
793b883e
SH
1059 unsigned count;
1060
1061 count = sizeof(dma_addr_t) / sizeof(u32);
1062 count += skb_shinfo(skb)->nr_frags * count;
1063
1064 if (skb_shinfo(skb)->tso_size)
1065 ++count;
1066
0e3ff6aa 1067 if (skb->ip_summed == CHECKSUM_HW)
793b883e
SH
1068 ++count;
1069
1070 return count;
cd28ab6a
SH
1071}
1072
793b883e
SH
1073/*
1074 * Put one packet in ring for transmit.
1075 * A single packet can generate multiple list elements, and
1076 * the number of ring elements will probably be less than the number
1077 * of list elements used.
f2e46561
SH
1078 *
1079 * No BH disabling for tx_lock here (like tg3)
793b883e 1080 */
cd28ab6a
SH
1081static int sky2_xmit_frame(struct sk_buff *skb, struct net_device *dev)
1082{
1083 struct sky2_port *sky2 = netdev_priv(dev);
1084 struct sky2_hw *hw = sky2->hw;
d1f13708 1085 struct sky2_tx_le *le = NULL;
6cdbbdf3 1086 struct tx_ring_info *re;
cd28ab6a
SH
1087 unsigned i, len;
1088 dma_addr_t mapping;
1089 u32 addr64;
1090 u16 mss;
1091 u8 ctrl;
1092
f2e46561 1093 if (!spin_trylock(&sky2->tx_lock))
cd28ab6a
SH
1094 return NETDEV_TX_LOCKED;
1095
793b883e 1096 if (unlikely(tx_avail(sky2) < tx_le_req(skb))) {
8c463ef7
SH
1097 /* There is a known but harmless race with lockless tx
1098 * and netif_stop_queue.
1099 */
1100 if (!netif_queue_stopped(dev)) {
1101 netif_stop_queue(dev);
1102 printk(KERN_WARNING PFX "%s: ring full when queue awake!\n",
1103 dev->name);
1104 }
f2e46561 1105 spin_unlock(&sky2->tx_lock);
cd28ab6a 1106
cd28ab6a
SH
1107 return NETDEV_TX_BUSY;
1108 }
1109
793b883e 1110 if (unlikely(netif_msg_tx_queued(sky2)))
cd28ab6a
SH
1111 printk(KERN_DEBUG "%s: tx queued, slot %u, len %d\n",
1112 dev->name, sky2->tx_prod, skb->len);
1113
cd28ab6a
SH
1114 len = skb_headlen(skb);
1115 mapping = pci_map_single(hw->pdev, skb->data, len, PCI_DMA_TODEVICE);
a018e330 1116 addr64 = high32(mapping);
793b883e
SH
1117
1118 re = sky2->tx_ring + sky2->tx_prod;
1119
a018e330
SH
1120 /* Send high bits if changed or crosses boundary */
1121 if (addr64 != sky2->tx_addr64 || high32(mapping + len) != sky2->tx_addr64) {
793b883e
SH
1122 le = get_tx_le(sky2);
1123 le->tx.addr = cpu_to_le32(addr64);
1124 le->ctrl = 0;
1125 le->opcode = OP_ADDR64 | HW_OWNER;
a018e330 1126 sky2->tx_addr64 = high32(mapping + len);
793b883e 1127 }
cd28ab6a
SH
1128
1129 /* Check for TCP Segmentation Offload */
1130 mss = skb_shinfo(skb)->tso_size;
793b883e 1131 if (mss != 0) {
cd28ab6a
SH
1132 /* just drop the packet if non-linear expansion fails */
1133 if (skb_header_cloned(skb) &&
1134 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
793b883e
SH
1135 dev_kfree_skb_any(skb);
1136 goto out_unlock;
cd28ab6a
SH
1137 }
1138
1139 mss += ((skb->h.th->doff - 5) * 4); /* TCP options */
1140 mss += (skb->nh.iph->ihl * 4) + sizeof(struct tcphdr);
1141 mss += ETH_HLEN;
793b883e 1142 }
cd28ab6a 1143
793b883e 1144 if (mss != sky2->tx_last_mss) {
cd28ab6a
SH
1145 le = get_tx_le(sky2);
1146 le->tx.tso.size = cpu_to_le16(mss);
793b883e 1147 le->tx.tso.rsvd = 0;
cd28ab6a 1148 le->opcode = OP_LRGLEN | HW_OWNER;
cd28ab6a 1149 le->ctrl = 0;
793b883e 1150 sky2->tx_last_mss = mss;
cd28ab6a
SH
1151 }
1152
cd28ab6a 1153 ctrl = 0;
d1f13708
SH
1154#ifdef SKY2_VLAN_TAG_USED
1155 /* Add VLAN tag, can piggyback on LRGLEN or ADDR64 */
1156 if (sky2->vlgrp && vlan_tx_tag_present(skb)) {
1157 if (!le) {
1158 le = get_tx_le(sky2);
1159 le->tx.addr = 0;
1160 le->opcode = OP_VLAN|HW_OWNER;
1161 le->ctrl = 0;
1162 } else
1163 le->opcode |= OP_VLAN;
1164 le->length = cpu_to_be16(vlan_tx_tag_get(skb));
1165 ctrl |= INS_VLAN;
1166 }
1167#endif
1168
1169 /* Handle TCP checksum offload */
cd28ab6a 1170 if (skb->ip_summed == CHECKSUM_HW) {
793b883e
SH
1171 u16 hdr = skb->h.raw - skb->data;
1172 u16 offset = hdr + skb->csum;
cd28ab6a
SH
1173
1174 ctrl = CALSUM | WR_SUM | INIT_SUM | LOCK_SUM;
1175 if (skb->nh.iph->protocol == IPPROTO_UDP)
1176 ctrl |= UDPTCP;
1177
1178 le = get_tx_le(sky2);
1179 le->tx.csum.start = cpu_to_le16(hdr);
793b883e
SH
1180 le->tx.csum.offset = cpu_to_le16(offset);
1181 le->length = 0; /* initial checksum value */
cd28ab6a 1182 le->ctrl = 1; /* one packet */
793b883e 1183 le->opcode = OP_TCPLISW | HW_OWNER;
cd28ab6a
SH
1184 }
1185
1186 le = get_tx_le(sky2);
1187 le->tx.addr = cpu_to_le32((u32) mapping);
1188 le->length = cpu_to_le16(len);
1189 le->ctrl = ctrl;
793b883e 1190 le->opcode = mss ? (OP_LARGESEND | HW_OWNER) : (OP_PACKET | HW_OWNER);
cd28ab6a 1191
793b883e 1192 /* Record the transmit mapping info */
cd28ab6a 1193 re->skb = skb;
6cdbbdf3 1194 pci_unmap_addr_set(re, mapaddr, mapping);
cd28ab6a
SH
1195
1196 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1197 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6cdbbdf3 1198 struct tx_ring_info *fre;
cd28ab6a
SH
1199
1200 mapping = pci_map_page(hw->pdev, frag->page, frag->page_offset,
1201 frag->size, PCI_DMA_TODEVICE);
793b883e
SH
1202 addr64 = (mapping >> 16) >> 16;
1203 if (addr64 != sky2->tx_addr64) {
1204 le = get_tx_le(sky2);
1205 le->tx.addr = cpu_to_le32(addr64);
1206 le->ctrl = 0;
1207 le->opcode = OP_ADDR64 | HW_OWNER;
1208 sky2->tx_addr64 = addr64;
cd28ab6a
SH
1209 }
1210
1211 le = get_tx_le(sky2);
1212 le->tx.addr = cpu_to_le32((u32) mapping);
1213 le->length = cpu_to_le16(frag->size);
1214 le->ctrl = ctrl;
793b883e 1215 le->opcode = OP_BUFFER | HW_OWNER;
cd28ab6a 1216
793b883e
SH
1217 fre = sky2->tx_ring
1218 + ((re - sky2->tx_ring) + i + 1) % TX_RING_SIZE;
6cdbbdf3 1219 pci_unmap_addr_set(fre, mapaddr, mapping);
cd28ab6a 1220 }
6cdbbdf3 1221
793b883e 1222 re->idx = sky2->tx_prod;
cd28ab6a
SH
1223 le->ctrl |= EOP;
1224
724bca3c 1225 sky2_put_idx(hw, txqaddr[sky2->port], sky2->tx_prod,
cd28ab6a
SH
1226 &sky2->tx_last_put, TX_RING_SIZE);
1227
0e3ff6aa 1228 if (tx_avail(sky2) <= MAX_SKB_TX_LE)
cd28ab6a 1229 netif_stop_queue(dev);
793b883e
SH
1230
1231out_unlock:
1232 mmiowb();
f2e46561 1233 spin_unlock(&sky2->tx_lock);
cd28ab6a
SH
1234
1235 dev->trans_start = jiffies;
1236 return NETDEV_TX_OK;
1237}
1238
cd28ab6a 1239/*
793b883e
SH
1240 * Free ring elements from starting at tx_cons until "done"
1241 *
1242 * NB: the hardware will tell us about partial completion of multi-part
d571b694 1243 * buffers; these are deferred until completion.
cd28ab6a 1244 */
d11c13e7 1245static void sky2_tx_complete(struct sky2_port *sky2, u16 done)
cd28ab6a 1246{
d11c13e7 1247 struct net_device *dev = sky2->netdev;
af2a58ac
SH
1248 struct pci_dev *pdev = sky2->hw->pdev;
1249 u16 nxt, put;
793b883e 1250 unsigned i;
cd28ab6a 1251
0e3ff6aa 1252 BUG_ON(done >= TX_RING_SIZE);
2224795d 1253
d11c13e7 1254 if (unlikely(netif_msg_tx_done(sky2)))
d571b694 1255 printk(KERN_DEBUG "%s: tx done, up to %u\n",
d11c13e7 1256 dev->name, done);
cd28ab6a 1257
af2a58ac
SH
1258 for (put = sky2->tx_cons; put != done; put = nxt) {
1259 struct tx_ring_info *re = sky2->tx_ring + put;
1260 struct sk_buff *skb = re->skb;
cd28ab6a 1261
af2a58ac
SH
1262 nxt = re->idx;
1263 BUG_ON(nxt >= TX_RING_SIZE);
d70cd51a 1264 prefetch(sky2->tx_ring + nxt);
cd28ab6a 1265
793b883e 1266 /* Check for partial status */
af2a58ac
SH
1267 if (tx_dist(put, done) < tx_dist(put, nxt))
1268 break;
793b883e
SH
1269
1270 skb = re->skb;
af2a58ac 1271 pci_unmap_single(pdev, pci_unmap_addr(re, mapaddr),
734d1868 1272 skb_headlen(skb), PCI_DMA_TODEVICE);
793b883e
SH
1273
1274 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6cdbbdf3 1275 struct tx_ring_info *fre;
af2a58ac
SH
1276 fre = sky2->tx_ring + (put + i + 1) % TX_RING_SIZE;
1277 pci_unmap_page(pdev, pci_unmap_addr(fre, mapaddr),
1278 skb_shinfo(skb)->frags[i].size,
734d1868 1279 PCI_DMA_TODEVICE);
cd28ab6a
SH
1280 }
1281
cd28ab6a 1282 dev_kfree_skb_any(skb);
793b883e 1283 }
793b883e 1284
af2a58ac
SH
1285 spin_lock(&sky2->tx_lock);
1286 sky2->tx_cons = put;
793b883e 1287 if (netif_queue_stopped(dev) && tx_avail(sky2) > MAX_SKB_TX_LE)
cd28ab6a
SH
1288 netif_wake_queue(dev);
1289 spin_unlock(&sky2->tx_lock);
1290}
1291
1292/* Cleanup all untransmitted buffers, assume transmitter not running */
13b97b74 1293static void sky2_tx_clean(struct sky2_port *sky2)
cd28ab6a 1294{
d11c13e7 1295 sky2_tx_complete(sky2, sky2->tx_prod);
cd28ab6a
SH
1296}
1297
1298/* Network shutdown */
1299static int sky2_down(struct net_device *dev)
1300{
1301 struct sky2_port *sky2 = netdev_priv(dev);
1302 struct sky2_hw *hw = sky2->hw;
1303 unsigned port = sky2->port;
1304 u16 ctrl;
cd28ab6a 1305
1b537565
SH
1306 /* Never really got started! */
1307 if (!sky2->tx_le)
1308 return 0;
1309
cd28ab6a
SH
1310 if (netif_msg_ifdown(sky2))
1311 printk(KERN_INFO PFX "%s: disabling interface\n", dev->name);
1312
018d1c66 1313 /* Stop more packets from being queued */
cd28ab6a
SH
1314 netif_stop_queue(dev);
1315
018d1c66
SH
1316 /* Disable port IRQ */
1317 local_irq_disable();
1318 hw->intr_mask &= ~((sky2->port == 0) ? Y2_IS_IRQ_PHY1 : Y2_IS_IRQ_PHY2);
1319 sky2_write32(hw, B0_IMSK, hw->intr_mask);
1320 local_irq_enable();
1321
91c86df5 1322 flush_scheduled_work();
018d1c66 1323
793b883e
SH
1324 sky2_phy_reset(hw, port);
1325
cd28ab6a
SH
1326 /* Stop transmitter */
1327 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_STOP);
1328 sky2_read32(hw, Q_ADDR(txqaddr[port], Q_CSR));
1329
1330 sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL),
793b883e 1331 RB_RST_SET | RB_DIS_OP_MD);
cd28ab6a
SH
1332
1333 ctrl = gma_read16(hw, port, GM_GP_CTRL);
793b883e 1334 ctrl &= ~(GM_GPCR_TX_ENA | GM_GPCR_RX_ENA);
cd28ab6a
SH
1335 gma_write16(hw, port, GM_GP_CTRL, ctrl);
1336
1337 sky2_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
1338
1339 /* Workaround shared GMAC reset */
793b883e
SH
1340 if (!(hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0
1341 && port == 0 && hw->dev[1] && netif_running(hw->dev[1])))
cd28ab6a
SH
1342 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
1343
1344 /* Disable Force Sync bit and Enable Alloc bit */
1345 sky2_write8(hw, SK_REG(port, TXA_CTRL),
1346 TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
1347
1348 /* Stop Interval Timer and Limit Counter of Tx Arbiter */
1349 sky2_write32(hw, SK_REG(port, TXA_ITI_INI), 0L);
1350 sky2_write32(hw, SK_REG(port, TXA_LIM_INI), 0L);
1351
1352 /* Reset the PCI FIFO of the async Tx queue */
793b883e
SH
1353 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR),
1354 BMU_RST_SET | BMU_FIFO_RST);
cd28ab6a
SH
1355
1356 /* Reset the Tx prefetch units */
1357 sky2_write32(hw, Y2_QADDR(txqaddr[port], PREF_UNIT_CTRL),
1358 PREF_UNIT_RST_SET);
1359
1360 sky2_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL), RB_RST_SET);
1361
6b1a3aef 1362 sky2_rx_stop(sky2);
cd28ab6a
SH
1363
1364 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
1365 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_SET);
1366
d571b694 1367 /* turn off LED's */
cd28ab6a
SH
1368 sky2_write16(hw, B0_Y2LED, LED_STAT_OFF);
1369
018d1c66
SH
1370 synchronize_irq(hw->pdev->irq);
1371
cd28ab6a
SH
1372 sky2_tx_clean(sky2);
1373 sky2_rx_clean(sky2);
1374
1375 pci_free_consistent(hw->pdev, RX_LE_BYTES,
1376 sky2->rx_le, sky2->rx_le_map);
1377 kfree(sky2->rx_ring);
1378
1379 pci_free_consistent(hw->pdev,
1380 TX_RING_SIZE * sizeof(struct sky2_tx_le),
1381 sky2->tx_le, sky2->tx_le_map);
1382 kfree(sky2->tx_ring);
1383
1b537565
SH
1384 sky2->tx_le = NULL;
1385 sky2->rx_le = NULL;
1386
1387 sky2->rx_ring = NULL;
1388 sky2->tx_ring = NULL;
1389
cd28ab6a
SH
1390 return 0;
1391}
1392
1393static u16 sky2_phy_speed(const struct sky2_hw *hw, u16 aux)
1394{
793b883e
SH
1395 if (!hw->copper)
1396 return SPEED_1000;
1397
cd28ab6a
SH
1398 if (hw->chip_id == CHIP_ID_YUKON_FE)
1399 return (aux & PHY_M_PS_SPEED_100) ? SPEED_100 : SPEED_10;
1400
1401 switch (aux & PHY_M_PS_SPEED_MSK) {
1402 case PHY_M_PS_SPEED_1000:
1403 return SPEED_1000;
1404 case PHY_M_PS_SPEED_100:
1405 return SPEED_100;
1406 default:
1407 return SPEED_10;
1408 }
1409}
1410
1411static void sky2_link_up(struct sky2_port *sky2)
1412{
1413 struct sky2_hw *hw = sky2->hw;
1414 unsigned port = sky2->port;
1415 u16 reg;
1416
1417 /* Enable Transmit FIFO Underrun */
793b883e 1418 sky2_write8(hw, SK_REG(port, GMAC_IRQ_MSK), GMAC_DEF_MSK);
cd28ab6a
SH
1419
1420 reg = gma_read16(hw, port, GM_GP_CTRL);
1421 if (sky2->duplex == DUPLEX_FULL || sky2->autoneg == AUTONEG_ENABLE)
1422 reg |= GM_GPCR_DUP_FULL;
1423
cd28ab6a
SH
1424 /* enable Rx/Tx */
1425 reg |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
1426 gma_write16(hw, port, GM_GP_CTRL, reg);
1427 gma_read16(hw, port, GM_GP_CTRL);
1428
1429 gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_DEF_MSK);
1430
1431 netif_carrier_on(sky2->netdev);
1432 netif_wake_queue(sky2->netdev);
1433
1434 /* Turn on link LED */
793b883e 1435 sky2_write8(hw, SK_REG(port, LNK_LED_REG),
cd28ab6a
SH
1436 LINKLED_ON | LINKLED_BLINK_OFF | LINKLED_LINKSYNC_OFF);
1437
793b883e
SH
1438 if (hw->chip_id == CHIP_ID_YUKON_XL) {
1439 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
1440
1441 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
1442 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, PHY_M_LEDC_LOS_CTRL(1) | /* LINK/ACT */
1443 PHY_M_LEDC_INIT_CTRL(sky2->speed ==
1444 SPEED_10 ? 7 : 0) |
1445 PHY_M_LEDC_STA1_CTRL(sky2->speed ==
1446 SPEED_100 ? 7 : 0) |
1447 PHY_M_LEDC_STA0_CTRL(sky2->speed ==
1448 SPEED_1000 ? 7 : 0));
1449 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
1450 }
1451
cd28ab6a
SH
1452 if (netif_msg_link(sky2))
1453 printk(KERN_INFO PFX
d571b694 1454 "%s: Link is up at %d Mbps, %s duplex, flow control %s\n",
cd28ab6a
SH
1455 sky2->netdev->name, sky2->speed,
1456 sky2->duplex == DUPLEX_FULL ? "full" : "half",
1457 (sky2->tx_pause && sky2->rx_pause) ? "both" :
793b883e 1458 sky2->tx_pause ? "tx" : sky2->rx_pause ? "rx" : "none");
cd28ab6a
SH
1459}
1460
1461static void sky2_link_down(struct sky2_port *sky2)
1462{
1463 struct sky2_hw *hw = sky2->hw;
1464 unsigned port = sky2->port;
1465 u16 reg;
1466
1467 gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);
1468
1469 reg = gma_read16(hw, port, GM_GP_CTRL);
1470 reg &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
1471 gma_write16(hw, port, GM_GP_CTRL, reg);
1472 gma_read16(hw, port, GM_GP_CTRL); /* PCI post */
1473
1474 if (sky2->rx_pause && !sky2->tx_pause) {
1475 /* restore Asymmetric Pause bit */
1476 gm_phy_write(hw, port, PHY_MARV_AUNE_ADV,
793b883e
SH
1477 gm_phy_read(hw, port, PHY_MARV_AUNE_ADV)
1478 | PHY_M_AN_ASP);
cd28ab6a
SH
1479 }
1480
cd28ab6a
SH
1481 netif_carrier_off(sky2->netdev);
1482 netif_stop_queue(sky2->netdev);
1483
1484 /* Turn on link LED */
1485 sky2_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_OFF);
1486
1487 if (netif_msg_link(sky2))
1488 printk(KERN_INFO PFX "%s: Link is down.\n", sky2->netdev->name);
1489 sky2_phy_init(hw, port);
1490}
1491
793b883e
SH
1492static int sky2_autoneg_done(struct sky2_port *sky2, u16 aux)
1493{
1494 struct sky2_hw *hw = sky2->hw;
1495 unsigned port = sky2->port;
1496 u16 lpa;
1497
1498 lpa = gm_phy_read(hw, port, PHY_MARV_AUNE_LP);
1499
1500 if (lpa & PHY_M_AN_RF) {
1501 printk(KERN_ERR PFX "%s: remote fault", sky2->netdev->name);
1502 return -1;
1503 }
1504
1505 if (hw->chip_id != CHIP_ID_YUKON_FE &&
1506 gm_phy_read(hw, port, PHY_MARV_1000T_STAT) & PHY_B_1000S_MSF) {
1507 printk(KERN_ERR PFX "%s: master/slave fault",
1508 sky2->netdev->name);
1509 return -1;
1510 }
1511
1512 if (!(aux & PHY_M_PS_SPDUP_RES)) {
1513 printk(KERN_ERR PFX "%s: speed/duplex mismatch",
1514 sky2->netdev->name);
1515 return -1;
1516 }
1517
1518 sky2->duplex = (aux & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
1519
1520 sky2->speed = sky2_phy_speed(hw, aux);
1521
1522 /* Pause bits are offset (9..8) */
1523 if (hw->chip_id == CHIP_ID_YUKON_XL)
1524 aux >>= 6;
1525
1526 sky2->rx_pause = (aux & PHY_M_PS_RX_P_EN) != 0;
1527 sky2->tx_pause = (aux & PHY_M_PS_TX_P_EN) != 0;
1528
1529 if ((sky2->tx_pause || sky2->rx_pause)
1530 && !(sky2->speed < SPEED_1000 && sky2->duplex == DUPLEX_HALF))
1531 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
1532 else
1533 sky2_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
1534
1535 return 0;
1536}
cd28ab6a
SH
1537
1538/*
91c86df5 1539 * Interrupt from PHY are handled outside of interrupt context
cd28ab6a
SH
1540 * because accessing phy registers requires spin wait which might
1541 * cause excess interrupt latency.
1542 */
91c86df5 1543static void sky2_phy_task(void *arg)
cd28ab6a 1544{
91c86df5 1545 struct sky2_port *sky2 = arg;
cd28ab6a 1546 struct sky2_hw *hw = sky2->hw;
cd28ab6a
SH
1547 u16 istatus, phystat;
1548
91c86df5 1549 down(&sky2->phy_sema);
793b883e
SH
1550 istatus = gm_phy_read(hw, sky2->port, PHY_MARV_INT_STAT);
1551 phystat = gm_phy_read(hw, sky2->port, PHY_MARV_PHY_STAT);
cd28ab6a
SH
1552
1553 if (netif_msg_intr(sky2))
1554 printk(KERN_INFO PFX "%s: phy interrupt status 0x%x 0x%x\n",
1555 sky2->netdev->name, istatus, phystat);
1556
1557 if (istatus & PHY_M_IS_AN_COMPL) {
793b883e
SH
1558 if (sky2_autoneg_done(sky2, phystat) == 0)
1559 sky2_link_up(sky2);
1560 goto out;
1561 }
cd28ab6a 1562
793b883e
SH
1563 if (istatus & PHY_M_IS_LSP_CHANGE)
1564 sky2->speed = sky2_phy_speed(hw, phystat);
cd28ab6a 1565
793b883e
SH
1566 if (istatus & PHY_M_IS_DUP_CHANGE)
1567 sky2->duplex =
1568 (phystat & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
cd28ab6a 1569
793b883e
SH
1570 if (istatus & PHY_M_IS_LST_CHANGE) {
1571 if (phystat & PHY_M_PS_LINK_UP)
cd28ab6a 1572 sky2_link_up(sky2);
793b883e
SH
1573 else
1574 sky2_link_down(sky2);
cd28ab6a 1575 }
793b883e 1576out:
91c86df5 1577 up(&sky2->phy_sema);
cd28ab6a
SH
1578
1579 local_irq_disable();
793b883e 1580 hw->intr_mask |= (sky2->port == 0) ? Y2_IS_IRQ_PHY1 : Y2_IS_IRQ_PHY2;
cd28ab6a
SH
1581 sky2_write32(hw, B0_IMSK, hw->intr_mask);
1582 local_irq_enable();
1583}
1584
1585static void sky2_tx_timeout(struct net_device *dev)
1586{
1587 struct sky2_port *sky2 = netdev_priv(dev);
8cc048e3
SH
1588 struct sky2_hw *hw = sky2->hw;
1589 unsigned txq = txqaddr[sky2->port];
cd28ab6a
SH
1590
1591 if (netif_msg_timer(sky2))
1592 printk(KERN_ERR PFX "%s: tx timeout\n", dev->name);
1593
8cc048e3
SH
1594 netif_stop_queue(dev);
1595
1596 sky2_write32(hw, Q_ADDR(txq, Q_CSR), BMU_STOP);
1597 sky2_read32(hw, Q_ADDR(txq, Q_CSR));
1598
1599 sky2_write32(hw, Y2_QADDR(txq, PREF_UNIT_CTRL), PREF_UNIT_RST_SET);
cd28ab6a
SH
1600
1601 sky2_tx_clean(sky2);
8cc048e3
SH
1602
1603 sky2_qset(hw, txq);
1604 sky2_prefetch_init(hw, txq, sky2->tx_le_map, TX_RING_SIZE - 1);
1605
1606 netif_wake_queue(dev);
cd28ab6a
SH
1607}
1608
734d1868
SH
1609
1610#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
1611/* Want receive buffer size to be multiple of 64 bits, and incl room for vlan */
1612static inline unsigned sky2_buf_size(int mtu)
1613{
1614 return roundup(mtu + ETH_HLEN + 4, 8);
1615}
1616
cd28ab6a
SH
1617static int sky2_change_mtu(struct net_device *dev, int new_mtu)
1618{
6b1a3aef
SH
1619 struct sky2_port *sky2 = netdev_priv(dev);
1620 struct sky2_hw *hw = sky2->hw;
1621 int err;
1622 u16 ctl, mode;
cd28ab6a
SH
1623
1624 if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
1625 return -EINVAL;
1626
5a5b1ea0
SH
1627 if (hw->chip_id == CHIP_ID_YUKON_EC_U && new_mtu > ETH_DATA_LEN)
1628 return -EINVAL;
1629
6b1a3aef
SH
1630 if (!netif_running(dev)) {
1631 dev->mtu = new_mtu;
1632 return 0;
1633 }
1634
6b1a3aef
SH
1635 sky2_write32(hw, B0_IMSK, 0);
1636
018d1c66
SH
1637 dev->trans_start = jiffies; /* prevent tx timeout */
1638 netif_stop_queue(dev);
1639 netif_poll_disable(hw->dev[0]);
1640
6b1a3aef
SH
1641 ctl = gma_read16(hw, sky2->port, GM_GP_CTRL);
1642 gma_write16(hw, sky2->port, GM_GP_CTRL, ctl & ~GM_GPCR_RX_ENA);
1643 sky2_rx_stop(sky2);
1644 sky2_rx_clean(sky2);
cd28ab6a
SH
1645
1646 dev->mtu = new_mtu;
734d1868 1647 sky2->rx_bufsize = sky2_buf_size(new_mtu);
6b1a3aef
SH
1648 mode = DATA_BLIND_VAL(DATA_BLIND_DEF) |
1649 GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF);
1650
1651 if (dev->mtu > ETH_DATA_LEN)
1652 mode |= GM_SMOD_JUMBO_ENA;
1653
1654 gma_write16(hw, sky2->port, GM_SERIAL_MODE, mode);
cd28ab6a 1655
6b1a3aef 1656 sky2_write8(hw, RB_ADDR(rxqaddr[sky2->port], RB_CTRL), RB_ENA_OP_MD);
cd28ab6a 1657
6b1a3aef 1658 err = sky2_rx_start(sky2);
6b1a3aef 1659 sky2_write32(hw, B0_IMSK, hw->intr_mask);
018d1c66 1660
1b537565
SH
1661 if (err)
1662 dev_close(dev);
1663 else {
1664 gma_write16(hw, sky2->port, GM_GP_CTRL, ctl);
1665
1666 netif_poll_enable(hw->dev[0]);
1667 netif_wake_queue(dev);
1668 }
1669
cd28ab6a
SH
1670 return err;
1671}
1672
1673/*
1674 * Receive one packet.
1675 * For small packets or errors, just reuse existing skb.
d571b694 1676 * For larger packets, get new buffer.
cd28ab6a 1677 */
d11c13e7 1678static struct sk_buff *sky2_receive(struct sky2_port *sky2,
cd28ab6a
SH
1679 u16 length, u32 status)
1680{
cd28ab6a 1681 struct ring_info *re = sky2->rx_ring + sky2->rx_next;
79e57d32 1682 struct sk_buff *skb = NULL;
cd28ab6a
SH
1683
1684 if (unlikely(netif_msg_rx_status(sky2)))
1685 printk(KERN_DEBUG PFX "%s: rx slot %u status 0x%x len %d\n",
d11c13e7 1686 sky2->netdev->name, sky2->rx_next, status, length);
cd28ab6a 1687
793b883e 1688 sky2->rx_next = (sky2->rx_next + 1) % sky2->rx_pending;
d70cd51a 1689 prefetch(sky2->rx_ring + sky2->rx_next);
cd28ab6a 1690
42eeea01 1691 if (status & GMR_FS_ANY_ERR)
cd28ab6a
SH
1692 goto error;
1693
42eeea01
SH
1694 if (!(status & GMR_FS_RX_OK))
1695 goto resubmit;
1696
6e15b712
SH
1697 if ((status >> 16) != length || length > sky2->rx_bufsize)
1698 goto oversize;
1699
bdb5c58e 1700 if (length < copybreak) {
79e57d32
SH
1701 skb = alloc_skb(length + 2, GFP_ATOMIC);
1702 if (!skb)
793b883e
SH
1703 goto resubmit;
1704
79e57d32 1705 skb_reserve(skb, 2);
793b883e
SH
1706 pci_dma_sync_single_for_cpu(sky2->hw->pdev, re->mapaddr,
1707 length, PCI_DMA_FROMDEVICE);
79e57d32 1708 memcpy(skb->data, re->skb->data, length);
d11c13e7
SH
1709 skb->ip_summed = re->skb->ip_summed;
1710 skb->csum = re->skb->csum;
793b883e
SH
1711 pci_dma_sync_single_for_device(sky2->hw->pdev, re->mapaddr,
1712 length, PCI_DMA_FROMDEVICE);
793b883e 1713 } else {
79e57d32
SH
1714 struct sk_buff *nskb;
1715
734d1868 1716 nskb = dev_alloc_skb(sky2->rx_bufsize);
793b883e
SH
1717 if (!nskb)
1718 goto resubmit;
cd28ab6a 1719
793b883e 1720 skb = re->skb;
79e57d32 1721 re->skb = nskb;
793b883e 1722 pci_unmap_single(sky2->hw->pdev, re->mapaddr,
734d1868 1723 sky2->rx_bufsize, PCI_DMA_FROMDEVICE);
793b883e 1724 prefetch(skb->data);
cd28ab6a 1725
793b883e 1726 re->mapaddr = pci_map_single(sky2->hw->pdev, nskb->data,
734d1868 1727 sky2->rx_bufsize, PCI_DMA_FROMDEVICE);
793b883e 1728 }
cd28ab6a 1729
79e57d32 1730 skb_put(skb, length);
793b883e 1731resubmit:
d11c13e7 1732 re->skb->ip_summed = CHECKSUM_NONE;
734d1868 1733 sky2_rx_add(sky2, re->mapaddr);
79e57d32 1734
bea86103
SH
1735 /* Tell receiver about new buffers. */
1736 sky2_put_idx(sky2->hw, rxqaddr[sky2->port], sky2->rx_put,
1737 &sky2->rx_last_put, RX_LE_SIZE);
1738
cd28ab6a
SH
1739 return skb;
1740
6e15b712
SH
1741oversize:
1742 ++sky2->net_stats.rx_over_errors;
1743 goto resubmit;
1744
cd28ab6a 1745error:
6e15b712
SH
1746 ++sky2->net_stats.rx_errors;
1747
cd28ab6a
SH
1748 if (netif_msg_rx_err(sky2))
1749 printk(KERN_INFO PFX "%s: rx error, status 0x%x length %d\n",
1750 sky2->netdev->name, status, length);
793b883e
SH
1751
1752 if (status & (GMR_FS_LONG_ERR | GMR_FS_UN_SIZE))
cd28ab6a
SH
1753 sky2->net_stats.rx_length_errors++;
1754 if (status & GMR_FS_FRAGMENT)
1755 sky2->net_stats.rx_frame_errors++;
1756 if (status & GMR_FS_CRC_ERR)
1757 sky2->net_stats.rx_crc_errors++;
793b883e
SH
1758 if (status & GMR_FS_RX_FF_OV)
1759 sky2->net_stats.rx_fifo_errors++;
79e57d32 1760
793b883e 1761 goto resubmit;
cd28ab6a
SH
1762}
1763
2224795d
SH
1764/*
1765 * Check for transmit complete
793b883e 1766 */
13b97b74 1767#define TX_NO_STATUS 0xffff
2224795d 1768
13b97b74
SH
1769static inline void sky2_tx_check(struct sky2_hw *hw, int port, u16 last)
1770{
1771 if (last != TX_NO_STATUS) {
1772 struct net_device *dev = hw->dev[port];
1773 if (dev && netif_running(dev)) {
1774 struct sky2_port *sky2 = netdev_priv(dev);
1775 sky2_tx_complete(sky2, last);
1776 }
2224795d 1777 }
cd28ab6a
SH
1778}
1779
1780/*
cd28ab6a
SH
1781 * Both ports share the same status interrupt, therefore there is only
1782 * one poll routine.
cd28ab6a 1783 */
d11c13e7 1784static int sky2_poll(struct net_device *dev0, int *budget)
cd28ab6a 1785{
d11c13e7
SH
1786 struct sky2_hw *hw = ((struct sky2_port *) netdev_priv(dev0))->hw;
1787 unsigned int to_do = min(dev0->quota, *budget);
cd28ab6a 1788 unsigned int work_done = 0;
793b883e 1789 u16 hwidx;
13b97b74 1790 u16 tx_done[2] = { TX_NO_STATUS, TX_NO_STATUS };
cd28ab6a 1791
793b883e 1792 hwidx = sky2_read16(hw, STAT_PUT_IDX);
79e57d32 1793 BUG_ON(hwidx >= STATUS_RING_SIZE);
af2a58ac 1794 rmb();
bea86103 1795
13210ce5
SH
1796 while (hwidx != hw->st_idx) {
1797 struct sky2_status_le *le = hw->st_le + hw->st_idx;
1798 struct net_device *dev;
d11c13e7 1799 struct sky2_port *sky2;
cd28ab6a 1800 struct sk_buff *skb;
cd28ab6a
SH
1801 u32 status;
1802 u16 length;
13210ce5 1803 u8 op;
cd28ab6a 1804
13210ce5 1805 le = hw->st_le + hw->st_idx;
bea86103 1806 hw->st_idx = (hw->st_idx + 1) % STATUS_RING_SIZE;
13210ce5 1807 prefetch(hw->st_le + hw->st_idx);
bea86103 1808
13210ce5
SH
1809 BUG_ON(le->link >= 2);
1810 dev = hw->dev[le->link];
1811 if (dev == NULL || !netif_running(dev))
1812 continue;
1813
1814 sky2 = netdev_priv(dev);
cd28ab6a
SH
1815 status = le32_to_cpu(le->status);
1816 length = le16_to_cpu(le->length);
13210ce5
SH
1817 op = le->opcode & ~HW_OWNER;
1818 le->opcode = 0;
cd28ab6a 1819
13210ce5 1820 switch (op) {
cd28ab6a 1821 case OP_RXSTAT:
d11c13e7 1822 skb = sky2_receive(sky2, length, status);
d1f13708
SH
1823 if (!skb)
1824 break;
13210ce5
SH
1825
1826 skb->dev = dev;
1827 skb->protocol = eth_type_trans(skb, dev);
1828 dev->last_rx = jiffies;
1829
d1f13708
SH
1830#ifdef SKY2_VLAN_TAG_USED
1831 if (sky2->vlgrp && (status & GMR_FS_VLAN)) {
1832 vlan_hwaccel_receive_skb(skb,
1833 sky2->vlgrp,
1834 be16_to_cpu(sky2->rx_tag));
1835 } else
1836#endif
cd28ab6a 1837 netif_receive_skb(skb);
13210ce5
SH
1838
1839 if (++work_done >= to_do)
1840 goto exit_loop;
cd28ab6a
SH
1841 break;
1842
d1f13708
SH
1843#ifdef SKY2_VLAN_TAG_USED
1844 case OP_RXVLAN:
1845 sky2->rx_tag = length;
1846 break;
1847
1848 case OP_RXCHKSVLAN:
1849 sky2->rx_tag = length;
1850 /* fall through */
1851#endif
cd28ab6a 1852 case OP_RXCHKS:
d11c13e7
SH
1853 skb = sky2->rx_ring[sky2->rx_next].skb;
1854 skb->ip_summed = CHECKSUM_HW;
1855 skb->csum = le16_to_cpu(status);
cd28ab6a
SH
1856 break;
1857
1858 case OP_TXINDEXLE:
13b97b74
SH
1859 /* TX index reports status for both ports */
1860 tx_done[0] = status & 0xffff;
1861 tx_done[1] = ((status >> 24) & 0xff)
1862 | (u16)(length & 0xf) << 8;
cd28ab6a
SH
1863 break;
1864
cd28ab6a
SH
1865 default:
1866 if (net_ratelimit())
793b883e 1867 printk(KERN_WARNING PFX
13210ce5 1868 "unknown status opcode 0x%x\n", op);
cd28ab6a
SH
1869 break;
1870 }
13210ce5 1871 }
cd28ab6a 1872
13210ce5 1873exit_loop:
3e4b32e1 1874 sky2_write32(hw, STAT_CTRL, SC_STAT_CLR_IRQ);
793b883e
SH
1875 mmiowb();
1876
13b97b74
SH
1877 sky2_tx_check(hw, 0, tx_done[0]);
1878 sky2_tx_check(hw, 1, tx_done[1]);
1879
3e4b32e1 1880 if (sky2_read16(hw, STAT_PUT_IDX) == hw->st_idx) {
13b97b74 1881 /* need to restart TX timer */
cd28ab6a
SH
1882 if (is_ec_a1(hw)) {
1883 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_STOP);
1884 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
1885 }
1886
bea86103 1887 netif_rx_complete(dev0);
cd28ab6a
SH
1888 hw->intr_mask |= Y2_IS_STAT_BMU;
1889 sky2_write32(hw, B0_IMSK, hw->intr_mask);
13210ce5
SH
1890 mmiowb();
1891 return 0;
1892 } else {
1893 *budget -= work_done;
1894 dev0->quota -= work_done;
1895 return 1;
cd28ab6a 1896 }
cd28ab6a
SH
1897}
1898
1899static void sky2_hw_error(struct sky2_hw *hw, unsigned port, u32 status)
1900{
1901 struct net_device *dev = hw->dev[port];
1902
1903 printk(KERN_INFO PFX "%s: hw error interrupt status 0x%x\n",
1904 dev->name, status);
1905
1906 if (status & Y2_IS_PAR_RD1) {
1907 printk(KERN_ERR PFX "%s: ram data read parity error\n",
1908 dev->name);
1909 /* Clear IRQ */
1910 sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_RD_PERR);
1911 }
1912
1913 if (status & Y2_IS_PAR_WR1) {
1914 printk(KERN_ERR PFX "%s: ram data write parity error\n",
1915 dev->name);
1916
1917 sky2_write16(hw, RAM_BUFFER(port, B3_RI_CTRL), RI_CLR_WR_PERR);
1918 }
1919
1920 if (status & Y2_IS_PAR_MAC1) {
1921 printk(KERN_ERR PFX "%s: MAC parity error\n", dev->name);
1922 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_PE);
1923 }
1924
1925 if (status & Y2_IS_PAR_RX1) {
1926 printk(KERN_ERR PFX "%s: RX parity error\n", dev->name);
1927 sky2_write32(hw, Q_ADDR(rxqaddr[port], Q_CSR), BMU_CLR_IRQ_PAR);
1928 }
1929
1930 if (status & Y2_IS_TCP_TXA1) {
1931 printk(KERN_ERR PFX "%s: TCP segmentation error\n", dev->name);
1932 sky2_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), BMU_CLR_IRQ_TCP);
1933 }
1934}
1935
1936static void sky2_hw_intr(struct sky2_hw *hw)
1937{
1938 u32 status = sky2_read32(hw, B0_HWE_ISRC);
1939
793b883e 1940 if (status & Y2_IS_TIST_OV)
cd28ab6a 1941 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
cd28ab6a
SH
1942
1943 if (status & (Y2_IS_MST_ERR | Y2_IS_IRQ_STAT)) {
793b883e
SH
1944 u16 pci_err;
1945
1946 pci_read_config_word(hw->pdev, PCI_STATUS, &pci_err);
cd28ab6a
SH
1947 printk(KERN_ERR PFX "%s: pci hw error (0x%x)\n",
1948 pci_name(hw->pdev), pci_err);
1949
1950 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
793b883e
SH
1951 pci_write_config_word(hw->pdev, PCI_STATUS,
1952 pci_err | PCI_STATUS_ERROR_BITS);
cd28ab6a
SH
1953 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
1954 }
1955
1956 if (status & Y2_IS_PCI_EXP) {
d571b694 1957 /* PCI-Express uncorrectable Error occurred */
793b883e
SH
1958 u32 pex_err;
1959
1960 pci_read_config_dword(hw->pdev, PEX_UNC_ERR_STAT, &pex_err);
cd28ab6a 1961
cd28ab6a
SH
1962 printk(KERN_ERR PFX "%s: pci express error (0x%x)\n",
1963 pci_name(hw->pdev), pex_err);
1964
1965 /* clear the interrupt */
1966 sky2_write32(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
793b883e
SH
1967 pci_write_config_dword(hw->pdev, PEX_UNC_ERR_STAT,
1968 0xffffffffUL);
cd28ab6a
SH
1969 sky2_write32(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
1970
1971 if (pex_err & PEX_FATAL_ERRORS) {
1972 u32 hwmsk = sky2_read32(hw, B0_HWE_IMSK);
1973 hwmsk &= ~Y2_IS_PCI_EXP;
1974 sky2_write32(hw, B0_HWE_IMSK, hwmsk);
1975 }
1976 }
1977
1978 if (status & Y2_HWE_L1_MASK)
1979 sky2_hw_error(hw, 0, status);
1980 status >>= 8;
1981 if (status & Y2_HWE_L1_MASK)
1982 sky2_hw_error(hw, 1, status);
1983}
1984
1985static void sky2_mac_intr(struct sky2_hw *hw, unsigned port)
1986{
1987 struct net_device *dev = hw->dev[port];
1988 struct sky2_port *sky2 = netdev_priv(dev);
1989 u8 status = sky2_read8(hw, SK_REG(port, GMAC_IRQ_SRC));
1990
1991 if (netif_msg_intr(sky2))
1992 printk(KERN_INFO PFX "%s: mac interrupt status 0x%x\n",
1993 dev->name, status);
1994
1995 if (status & GM_IS_RX_FF_OR) {
1996 ++sky2->net_stats.rx_fifo_errors;
1997 sky2_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_CLI_RX_FO);
1998 }
1999
2000 if (status & GM_IS_TX_FF_UR) {
2001 ++sky2->net_stats.tx_fifo_errors;
2002 sky2_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_CLI_TX_FU);
2003 }
cd28ab6a
SH
2004}
2005
2006static void sky2_phy_intr(struct sky2_hw *hw, unsigned port)
2007{
2008 struct net_device *dev = hw->dev[port];
2009 struct sky2_port *sky2 = netdev_priv(dev);
2010
2011 hw->intr_mask &= ~(port == 0 ? Y2_IS_IRQ_PHY1 : Y2_IS_IRQ_PHY2);
2012 sky2_write32(hw, B0_IMSK, hw->intr_mask);
91c86df5 2013 schedule_work(&sky2->phy_task);
cd28ab6a
SH
2014}
2015
2016static irqreturn_t sky2_intr(int irq, void *dev_id, struct pt_regs *regs)
2017{
2018 struct sky2_hw *hw = dev_id;
bea86103 2019 struct net_device *dev0 = hw->dev[0];
cd28ab6a
SH
2020 u32 status;
2021
2022 status = sky2_read32(hw, B0_Y2_SP_ISRC2);
793b883e 2023 if (status == 0 || status == ~0)
cd28ab6a
SH
2024 return IRQ_NONE;
2025
2026 if (status & Y2_IS_HW_ERR)
2027 sky2_hw_intr(hw);
2028
793b883e 2029 /* Do NAPI for Rx and Tx status */
bea86103 2030 if (status & Y2_IS_STAT_BMU) {
cd28ab6a
SH
2031 hw->intr_mask &= ~Y2_IS_STAT_BMU;
2032 sky2_write32(hw, B0_IMSK, hw->intr_mask);
bea86103 2033
0a122576
SH
2034 if (likely(__netif_rx_schedule_prep(dev0))) {
2035 prefetch(&hw->st_le[hw->st_idx]);
bea86103 2036 __netif_rx_schedule(dev0);
0a122576 2037 }
cd28ab6a
SH
2038 }
2039
793b883e 2040 if (status & Y2_IS_IRQ_PHY1)
cd28ab6a
SH
2041 sky2_phy_intr(hw, 0);
2042
2043 if (status & Y2_IS_IRQ_PHY2)
2044 sky2_phy_intr(hw, 1);
2045
2046 if (status & Y2_IS_IRQ_MAC1)
2047 sky2_mac_intr(hw, 0);
2048
2049 if (status & Y2_IS_IRQ_MAC2)
2050 sky2_mac_intr(hw, 1);
2051
cd28ab6a 2052 sky2_write32(hw, B0_Y2_SP_ICR, 2);
793b883e
SH
2053
2054 sky2_read32(hw, B0_IMSK);
2055
cd28ab6a
SH
2056 return IRQ_HANDLED;
2057}
2058
2059#ifdef CONFIG_NET_POLL_CONTROLLER
2060static void sky2_netpoll(struct net_device *dev)
2061{
2062 struct sky2_port *sky2 = netdev_priv(dev);
2063
793b883e 2064 sky2_intr(sky2->hw->pdev->irq, sky2->hw, NULL);
cd28ab6a
SH
2065}
2066#endif
2067
2068/* Chip internal frequency for clock calculations */
fb17358f 2069static inline u32 sky2_mhz(const struct sky2_hw *hw)
cd28ab6a 2070{
793b883e 2071 switch (hw->chip_id) {
cd28ab6a 2072 case CHIP_ID_YUKON_EC:
5a5b1ea0 2073 case CHIP_ID_YUKON_EC_U:
fb17358f 2074 return 125; /* 125 Mhz */
cd28ab6a 2075 case CHIP_ID_YUKON_FE:
fb17358f 2076 return 100; /* 100 Mhz */
793b883e 2077 default: /* YUKON_XL */
fb17358f 2078 return 156; /* 156 Mhz */
cd28ab6a
SH
2079 }
2080}
2081
fb17358f 2082static inline u32 sky2_us2clk(const struct sky2_hw *hw, u32 us)
cd28ab6a 2083{
fb17358f 2084 return sky2_mhz(hw) * us;
cd28ab6a
SH
2085}
2086
fb17358f 2087static inline u32 sky2_clk2us(const struct sky2_hw *hw, u32 clk)
cd28ab6a 2088{
fb17358f 2089 return clk / sky2_mhz(hw);
cd28ab6a
SH
2090}
2091
fb17358f 2092
cd28ab6a
SH
2093static int sky2_reset(struct sky2_hw *hw)
2094{
5afa0a9c 2095 u32 ctst;
cd28ab6a
SH
2096 u16 status;
2097 u8 t8, pmd_type;
2098 int i;
2099
2100 ctst = sky2_read32(hw, B0_CTST);
2101
2102 sky2_write8(hw, B0_CTST, CS_RST_CLR);
2103 hw->chip_id = sky2_read8(hw, B2_CHIP_ID);
2104 if (hw->chip_id < CHIP_ID_YUKON_XL || hw->chip_id > CHIP_ID_YUKON_FE) {
2105 printk(KERN_ERR PFX "%s: unsupported chip type 0x%x\n",
2106 pci_name(hw->pdev), hw->chip_id);
2107 return -EOPNOTSUPP;
2108 }
2109
793b883e
SH
2110 /* ring for status responses */
2111 hw->st_le = pci_alloc_consistent(hw->pdev, STATUS_LE_BYTES,
2112 &hw->st_dma);
2113 if (!hw->st_le)
2114 return -ENOMEM;
2115
cd28ab6a
SH
2116 /* disable ASF */
2117 if (hw->chip_id <= CHIP_ID_YUKON_EC) {
2118 sky2_write8(hw, B28_Y2_ASF_STAT_CMD, Y2_ASF_RESET);
2119 sky2_write16(hw, B0_CTST, Y2_ASF_DISABLE);
2120 }
2121
2122 /* do a SW reset */
2123 sky2_write8(hw, B0_CTST, CS_RST_SET);
2124 sky2_write8(hw, B0_CTST, CS_RST_CLR);
2125
2126 /* clear PCI errors, if any */
793b883e 2127 pci_read_config_word(hw->pdev, PCI_STATUS, &status);
cd28ab6a 2128 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
793b883e
SH
2129 pci_write_config_word(hw->pdev, PCI_STATUS,
2130 status | PCI_STATUS_ERROR_BITS);
cd28ab6a
SH
2131
2132 sky2_write8(hw, B0_CTST, CS_MRST_CLR);
2133
2134 /* clear any PEX errors */
2135 if (is_pciex(hw)) {
793b883e
SH
2136 u16 lstat;
2137 pci_write_config_dword(hw->pdev, PEX_UNC_ERR_STAT,
2138 0xffffffffUL);
2139 pci_read_config_word(hw->pdev, PEX_LNK_STAT, &lstat);
cd28ab6a
SH
2140 }
2141
2142 pmd_type = sky2_read8(hw, B2_PMD_TYP);
2143 hw->copper = !(pmd_type == 'L' || pmd_type == 'S');
2144
2145 hw->ports = 1;
2146 t8 = sky2_read8(hw, B2_Y2_HW_RES);
2147 if ((t8 & CFG_DUAL_MAC_MSK) == CFG_DUAL_MAC_MSK) {
2148 if (!(sky2_read8(hw, B2_Y2_CLK_GATE) & Y2_STATUS_LNK2_INAC))
2149 ++hw->ports;
2150 }
2151 hw->chip_rev = (sky2_read8(hw, B2_MAC_CFG) & CFG_CHIP_R_MSK) >> 4;
2152
5afa0a9c 2153 sky2_set_power_state(hw, PCI_D0);
cd28ab6a
SH
2154
2155 for (i = 0; i < hw->ports; i++) {
2156 sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_SET);
2157 sky2_write8(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_CLR);
2158 }
2159
2160 sky2_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
2161
793b883e
SH
2162 /* Clear I2C IRQ noise */
2163 sky2_write32(hw, B2_I2C_IRQ, 1);
cd28ab6a
SH
2164
2165 /* turn off hardware timer (unused) */
2166 sky2_write8(hw, B2_TI_CTRL, TIM_STOP);
2167 sky2_write8(hw, B2_TI_CTRL, TIM_CLR_IRQ);
793b883e 2168
cd28ab6a
SH
2169 sky2_write8(hw, B0_Y2LED, LED_STAT_ON);
2170
69634ee7
SH
2171 /* Turn off descriptor polling */
2172 sky2_write32(hw, B28_DPT_CTRL, DPT_STOP);
cd28ab6a
SH
2173
2174 /* Turn off receive timestamp */
2175 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_STOP);
793b883e 2176 sky2_write8(hw, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ);
cd28ab6a
SH
2177
2178 /* enable the Tx Arbiters */
2179 for (i = 0; i < hw->ports; i++)
2180 sky2_write8(hw, SK_REG(i, TXA_CTRL), TXA_ENA_ARB);
2181
2182 /* Initialize ram interface */
2183 for (i = 0; i < hw->ports; i++) {
793b883e 2184 sky2_write8(hw, RAM_BUFFER(i, B3_RI_CTRL), RI_RST_CLR);
cd28ab6a
SH
2185
2186 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R1), SK_RI_TO_53);
2187 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA1), SK_RI_TO_53);
2188 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS1), SK_RI_TO_53);
2189 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R1), SK_RI_TO_53);
2190 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA1), SK_RI_TO_53);
2191 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS1), SK_RI_TO_53);
2192 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_R2), SK_RI_TO_53);
2193 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XA2), SK_RI_TO_53);
2194 sky2_write8(hw, RAM_BUFFER(i, B3_RI_WTO_XS2), SK_RI_TO_53);
2195 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_R2), SK_RI_TO_53);
2196 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XA2), SK_RI_TO_53);
2197 sky2_write8(hw, RAM_BUFFER(i, B3_RI_RTO_XS2), SK_RI_TO_53);
2198 }
2199
cd28ab6a
SH
2200 sky2_write32(hw, B0_HWE_IMSK, Y2_HWE_ALL_MASK);
2201
cd28ab6a
SH
2202 for (i = 0; i < hw->ports; i++)
2203 sky2_phy_reset(hw, i);
cd28ab6a 2204
cd28ab6a
SH
2205 memset(hw->st_le, 0, STATUS_LE_BYTES);
2206 hw->st_idx = 0;
2207
2208 sky2_write32(hw, STAT_CTRL, SC_STAT_RST_SET);
2209 sky2_write32(hw, STAT_CTRL, SC_STAT_RST_CLR);
2210
2211 sky2_write32(hw, STAT_LIST_ADDR_LO, hw->st_dma);
793b883e 2212 sky2_write32(hw, STAT_LIST_ADDR_HI, (u64) hw->st_dma >> 32);
cd28ab6a
SH
2213
2214 /* Set the list last index */
793b883e 2215 sky2_write16(hw, STAT_LAST_IDX, STATUS_RING_SIZE - 1);
cd28ab6a 2216
793b883e 2217 /* These status setup values are copied from SysKonnect's driver */
cd28ab6a
SH
2218 if (is_ec_a1(hw)) {
2219 /* WA for dev. #4.3 */
793b883e 2220 sky2_write16(hw, STAT_TX_IDX_TH, 0xfff); /* Tx Threshold */
cd28ab6a
SH
2221
2222 /* set Status-FIFO watermark */
2223 sky2_write8(hw, STAT_FIFO_WM, 0x21); /* WA for dev. #4.18 */
2224
2225 /* set Status-FIFO ISR watermark */
793b883e 2226 sky2_write8(hw, STAT_FIFO_ISR_WM, 0x07); /* WA for dev. #4.18 */
69634ee7 2227 sky2_write32(hw, STAT_TX_TIMER_INI, sky2_us2clk(hw, 10000));
cd28ab6a 2228 } else {
69634ee7
SH
2229 sky2_write16(hw, STAT_TX_IDX_TH, 10);
2230 sky2_write8(hw, STAT_FIFO_WM, 16);
cd28ab6a
SH
2231
2232 /* set Status-FIFO ISR watermark */
2233 if (hw->chip_id == CHIP_ID_YUKON_XL && hw->chip_rev == 0)
69634ee7
SH
2234 sky2_write8(hw, STAT_FIFO_ISR_WM, 4);
2235 else
2236 sky2_write8(hw, STAT_FIFO_ISR_WM, 16);
cd28ab6a 2237
69634ee7
SH
2238 sky2_write32(hw, STAT_TX_TIMER_INI, sky2_us2clk(hw, 1000));
2239 sky2_write32(hw, STAT_LEV_TIMER_INI, sky2_us2clk(hw, 100));
2240 sky2_write32(hw, STAT_ISR_TIMER_INI, sky2_us2clk(hw, 20));
cd28ab6a
SH
2241 }
2242
793b883e 2243 /* enable status unit */
cd28ab6a
SH
2244 sky2_write32(hw, STAT_CTRL, SC_STAT_OP_ON);
2245
2246 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
2247 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
2248 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
2249
2250 return 0;
2251}
2252
2253static inline u32 sky2_supported_modes(const struct sky2_hw *hw)
2254{
2255 u32 modes;
2256 if (hw->copper) {
793b883e
SH
2257 modes = SUPPORTED_10baseT_Half
2258 | SUPPORTED_10baseT_Full
2259 | SUPPORTED_100baseT_Half
2260 | SUPPORTED_100baseT_Full
2261 | SUPPORTED_Autoneg | SUPPORTED_TP;
cd28ab6a
SH
2262
2263 if (hw->chip_id != CHIP_ID_YUKON_FE)
2264 modes |= SUPPORTED_1000baseT_Half
793b883e 2265 | SUPPORTED_1000baseT_Full;
cd28ab6a
SH
2266 } else
2267 modes = SUPPORTED_1000baseT_Full | SUPPORTED_FIBRE
793b883e 2268 | SUPPORTED_Autoneg;
cd28ab6a
SH
2269 return modes;
2270}
2271
793b883e 2272static int sky2_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
cd28ab6a
SH
2273{
2274 struct sky2_port *sky2 = netdev_priv(dev);
2275 struct sky2_hw *hw = sky2->hw;
2276
2277 ecmd->transceiver = XCVR_INTERNAL;
2278 ecmd->supported = sky2_supported_modes(hw);
2279 ecmd->phy_address = PHY_ADDR_MARV;
2280 if (hw->copper) {
2281 ecmd->supported = SUPPORTED_10baseT_Half
793b883e
SH
2282 | SUPPORTED_10baseT_Full
2283 | SUPPORTED_100baseT_Half
2284 | SUPPORTED_100baseT_Full
2285 | SUPPORTED_1000baseT_Half
2286 | SUPPORTED_1000baseT_Full
2287 | SUPPORTED_Autoneg | SUPPORTED_TP;
cd28ab6a
SH
2288 ecmd->port = PORT_TP;
2289 } else
2290 ecmd->port = PORT_FIBRE;
2291
2292 ecmd->advertising = sky2->advertising;
2293 ecmd->autoneg = sky2->autoneg;
2294 ecmd->speed = sky2->speed;
2295 ecmd->duplex = sky2->duplex;
2296 return 0;
2297}
2298
2299static int sky2_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
2300{
2301 struct sky2_port *sky2 = netdev_priv(dev);
2302 const struct sky2_hw *hw = sky2->hw;
2303 u32 supported = sky2_supported_modes(hw);
2304
2305 if (ecmd->autoneg == AUTONEG_ENABLE) {
2306 ecmd->advertising = supported;
2307 sky2->duplex = -1;
2308 sky2->speed = -1;
2309 } else {
2310 u32 setting;
2311
793b883e 2312 switch (ecmd->speed) {
cd28ab6a
SH
2313 case SPEED_1000:
2314 if (ecmd->duplex == DUPLEX_FULL)
2315 setting = SUPPORTED_1000baseT_Full;
2316 else if (ecmd->duplex == DUPLEX_HALF)
2317 setting = SUPPORTED_1000baseT_Half;
2318 else
2319 return -EINVAL;
2320 break;
2321 case SPEED_100:
2322 if (ecmd->duplex == DUPLEX_FULL)
2323 setting = SUPPORTED_100baseT_Full;
2324 else if (ecmd->duplex == DUPLEX_HALF)
2325 setting = SUPPORTED_100baseT_Half;
2326 else
2327 return -EINVAL;
2328 break;
2329
2330 case SPEED_10:
2331 if (ecmd->duplex == DUPLEX_FULL)
2332 setting = SUPPORTED_10baseT_Full;
2333 else if (ecmd->duplex == DUPLEX_HALF)
2334 setting = SUPPORTED_10baseT_Half;
2335 else
2336 return -EINVAL;
2337 break;
2338 default:
2339 return -EINVAL;
2340 }
2341
2342 if ((setting & supported) == 0)
2343 return -EINVAL;
2344
2345 sky2->speed = ecmd->speed;
2346 sky2->duplex = ecmd->duplex;
2347 }
2348
2349 sky2->autoneg = ecmd->autoneg;
2350 sky2->advertising = ecmd->advertising;
2351
1b537565
SH
2352 if (netif_running(dev))
2353 sky2_phy_reinit(sky2);
cd28ab6a
SH
2354
2355 return 0;
2356}
2357
2358static void sky2_get_drvinfo(struct net_device *dev,
2359 struct ethtool_drvinfo *info)
2360{
2361 struct sky2_port *sky2 = netdev_priv(dev);
2362
2363 strcpy(info->driver, DRV_NAME);
2364 strcpy(info->version, DRV_VERSION);
2365 strcpy(info->fw_version, "N/A");
2366 strcpy(info->bus_info, pci_name(sky2->hw->pdev));
2367}
2368
2369static const struct sky2_stat {
793b883e
SH
2370 char name[ETH_GSTRING_LEN];
2371 u16 offset;
cd28ab6a
SH
2372} sky2_stats[] = {
2373 { "tx_bytes", GM_TXO_OK_HI },
2374 { "rx_bytes", GM_RXO_OK_HI },
2375 { "tx_broadcast", GM_TXF_BC_OK },
2376 { "rx_broadcast", GM_RXF_BC_OK },
2377 { "tx_multicast", GM_TXF_MC_OK },
2378 { "rx_multicast", GM_RXF_MC_OK },
2379 { "tx_unicast", GM_TXF_UC_OK },
2380 { "rx_unicast", GM_RXF_UC_OK },
2381 { "tx_mac_pause", GM_TXF_MPAUSE },
2382 { "rx_mac_pause", GM_RXF_MPAUSE },
2383 { "collisions", GM_TXF_SNG_COL },
2384 { "late_collision",GM_TXF_LAT_COL },
2385 { "aborted", GM_TXF_ABO_COL },
2386 { "multi_collisions", GM_TXF_MUL_COL },
2387 { "fifo_underrun", GM_TXE_FIFO_UR },
2388 { "fifo_overflow", GM_RXE_FIFO_OV },
2389 { "rx_toolong", GM_RXF_LNG_ERR },
2390 { "rx_jabber", GM_RXF_JAB_PKT },
2391 { "rx_runt", GM_RXE_FRAG },
2392 { "rx_too_long", GM_RXF_LNG_ERR },
2393 { "rx_fcs_error", GM_RXF_FCS_ERR },
2394};
2395
cd28ab6a
SH
2396static u32 sky2_get_rx_csum(struct net_device *dev)
2397{
2398 struct sky2_port *sky2 = netdev_priv(dev);
2399
2400 return sky2->rx_csum;
2401}
2402
2403static int sky2_set_rx_csum(struct net_device *dev, u32 data)
2404{
2405 struct sky2_port *sky2 = netdev_priv(dev);
2406
2407 sky2->rx_csum = data;
793b883e 2408
cd28ab6a
SH
2409 sky2_write32(sky2->hw, Q_ADDR(rxqaddr[sky2->port], Q_CSR),
2410 data ? BMU_ENA_RX_CHKSUM : BMU_DIS_RX_CHKSUM);
2411
2412 return 0;
2413}
2414
2415static u32 sky2_get_msglevel(struct net_device *netdev)
2416{
2417 struct sky2_port *sky2 = netdev_priv(netdev);
2418 return sky2->msg_enable;
2419}
2420
9a7ae0a9
SH
2421static int sky2_nway_reset(struct net_device *dev)
2422{
2423 struct sky2_port *sky2 = netdev_priv(dev);
9a7ae0a9
SH
2424
2425 if (sky2->autoneg != AUTONEG_ENABLE)
2426 return -EINVAL;
2427
1b537565 2428 sky2_phy_reinit(sky2);
9a7ae0a9
SH
2429
2430 return 0;
2431}
2432
793b883e 2433static void sky2_phy_stats(struct sky2_port *sky2, u64 * data, unsigned count)
cd28ab6a
SH
2434{
2435 struct sky2_hw *hw = sky2->hw;
2436 unsigned port = sky2->port;
2437 int i;
2438
2439 data[0] = (u64) gma_read32(hw, port, GM_TXO_OK_HI) << 32
793b883e 2440 | (u64) gma_read32(hw, port, GM_TXO_OK_LO);
cd28ab6a 2441 data[1] = (u64) gma_read32(hw, port, GM_RXO_OK_HI) << 32
793b883e 2442 | (u64) gma_read32(hw, port, GM_RXO_OK_LO);
cd28ab6a 2443
793b883e 2444 for (i = 2; i < count; i++)
cd28ab6a
SH
2445 data[i] = (u64) gma_read32(hw, port, sky2_stats[i].offset);
2446}
2447
cd28ab6a
SH
2448static void sky2_set_msglevel(struct net_device *netdev, u32 value)
2449{
2450 struct sky2_port *sky2 = netdev_priv(netdev);
2451 sky2->msg_enable = value;
2452}
2453
2454static int sky2_get_stats_count(struct net_device *dev)
2455{
2456 return ARRAY_SIZE(sky2_stats);
2457}
2458
2459static void sky2_get_ethtool_stats(struct net_device *dev,
793b883e 2460 struct ethtool_stats *stats, u64 * data)
cd28ab6a
SH
2461{
2462 struct sky2_port *sky2 = netdev_priv(dev);
2463
793b883e 2464 sky2_phy_stats(sky2, data, ARRAY_SIZE(sky2_stats));
cd28ab6a
SH
2465}
2466
793b883e 2467static void sky2_get_strings(struct net_device *dev, u32 stringset, u8 * data)
cd28ab6a
SH
2468{
2469 int i;
2470
2471 switch (stringset) {
2472 case ETH_SS_STATS:
2473 for (i = 0; i < ARRAY_SIZE(sky2_stats); i++)
2474 memcpy(data + i * ETH_GSTRING_LEN,
2475 sky2_stats[i].name, ETH_GSTRING_LEN);
2476 break;
2477 }
2478}
2479
2480/* Use hardware MIB variables for critical path statistics and
2481 * transmit feedback not reported at interrupt.
2482 * Other errors are accounted for in interrupt handler.
2483 */
2484static struct net_device_stats *sky2_get_stats(struct net_device *dev)
2485{
2486 struct sky2_port *sky2 = netdev_priv(dev);
793b883e 2487 u64 data[13];
cd28ab6a 2488
793b883e 2489 sky2_phy_stats(sky2, data, ARRAY_SIZE(data));
cd28ab6a
SH
2490
2491 sky2->net_stats.tx_bytes = data[0];
2492 sky2->net_stats.rx_bytes = data[1];
2493 sky2->net_stats.tx_packets = data[2] + data[4] + data[6];
2494 sky2->net_stats.rx_packets = data[3] + data[5] + data[7];
2495 sky2->net_stats.multicast = data[5] + data[7];
2496 sky2->net_stats.collisions = data[10];
2497 sky2->net_stats.tx_aborted_errors = data[12];
2498
2499 return &sky2->net_stats;
2500}
2501
2502static int sky2_set_mac_address(struct net_device *dev, void *p)
2503{
2504 struct sky2_port *sky2 = netdev_priv(dev);
2505 struct sockaddr *addr = p;
cd28ab6a
SH
2506
2507 if (!is_valid_ether_addr(addr->sa_data))
2508 return -EADDRNOTAVAIL;
2509
cd28ab6a 2510 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
793b883e 2511 memcpy_toio(sky2->hw->regs + B2_MAC_1 + sky2->port * 8,
cd28ab6a 2512 dev->dev_addr, ETH_ALEN);
793b883e 2513 memcpy_toio(sky2->hw->regs + B2_MAC_2 + sky2->port * 8,
cd28ab6a 2514 dev->dev_addr, ETH_ALEN);
1b537565
SH
2515
2516 if (netif_running(dev))
2517 sky2_phy_reinit(sky2);
2518
2519 return 0;
cd28ab6a
SH
2520}
2521
2522static void sky2_set_multicast(struct net_device *dev)
2523{
2524 struct sky2_port *sky2 = netdev_priv(dev);
2525 struct sky2_hw *hw = sky2->hw;
2526 unsigned port = sky2->port;
2527 struct dev_mc_list *list = dev->mc_list;
2528 u16 reg;
2529 u8 filter[8];
2530
2531 memset(filter, 0, sizeof(filter));
2532
2533 reg = gma_read16(hw, port, GM_RX_CTRL);
2534 reg |= GM_RXCR_UCF_ENA;
2535
d571b694 2536 if (dev->flags & IFF_PROMISC) /* promiscuous */
cd28ab6a 2537 reg &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
793b883e 2538 else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count > 16) /* all multicast */
cd28ab6a 2539 memset(filter, 0xff, sizeof(filter));
793b883e 2540 else if (dev->mc_count == 0) /* no multicast */
cd28ab6a
SH
2541 reg &= ~GM_RXCR_MCF_ENA;
2542 else {
2543 int i;
2544 reg |= GM_RXCR_MCF_ENA;
2545
2546 for (i = 0; list && i < dev->mc_count; i++, list = list->next) {
2547 u32 bit = ether_crc(ETH_ALEN, list->dmi_addr) & 0x3f;
793b883e 2548 filter[bit / 8] |= 1 << (bit % 8);
cd28ab6a
SH
2549 }
2550 }
2551
cd28ab6a 2552 gma_write16(hw, port, GM_MC_ADDR_H1,
793b883e 2553 (u16) filter[0] | ((u16) filter[1] << 8));
cd28ab6a 2554 gma_write16(hw, port, GM_MC_ADDR_H2,
793b883e 2555 (u16) filter[2] | ((u16) filter[3] << 8));
cd28ab6a 2556 gma_write16(hw, port, GM_MC_ADDR_H3,
793b883e 2557 (u16) filter[4] | ((u16) filter[5] << 8));
cd28ab6a 2558 gma_write16(hw, port, GM_MC_ADDR_H4,
793b883e 2559 (u16) filter[6] | ((u16) filter[7] << 8));
cd28ab6a
SH
2560
2561 gma_write16(hw, port, GM_RX_CTRL, reg);
2562}
2563
2564/* Can have one global because blinking is controlled by
2565 * ethtool and that is always under RTNL mutex
2566 */
91c86df5 2567static void sky2_led(struct sky2_hw *hw, unsigned port, int on)
cd28ab6a 2568{
793b883e
SH
2569 u16 pg;
2570
793b883e
SH
2571 switch (hw->chip_id) {
2572 case CHIP_ID_YUKON_XL:
2573 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
2574 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
2575 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL,
2576 on ? (PHY_M_LEDC_LOS_CTRL(1) |
2577 PHY_M_LEDC_INIT_CTRL(7) |
2578 PHY_M_LEDC_STA1_CTRL(7) |
2579 PHY_M_LEDC_STA0_CTRL(7))
2580 : 0);
2581
2582 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
2583 break;
2584
2585 default:
2586 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
cd28ab6a 2587 gm_phy_write(hw, port, PHY_MARV_LED_OVER,
793b883e
SH
2588 on ? PHY_M_LED_MO_DUP(MO_LED_ON) |
2589 PHY_M_LED_MO_10(MO_LED_ON) |
2590 PHY_M_LED_MO_100(MO_LED_ON) |
cd28ab6a 2591 PHY_M_LED_MO_1000(MO_LED_ON) |
793b883e
SH
2592 PHY_M_LED_MO_RX(MO_LED_ON)
2593 : PHY_M_LED_MO_DUP(MO_LED_OFF) |
2594 PHY_M_LED_MO_10(MO_LED_OFF) |
2595 PHY_M_LED_MO_100(MO_LED_OFF) |
cd28ab6a
SH
2596 PHY_M_LED_MO_1000(MO_LED_OFF) |
2597 PHY_M_LED_MO_RX(MO_LED_OFF));
2598
793b883e 2599 }
cd28ab6a
SH
2600}
2601
2602/* blink LED's for finding board */
2603static int sky2_phys_id(struct net_device *dev, u32 data)
2604{
2605 struct sky2_port *sky2 = netdev_priv(dev);
2606 struct sky2_hw *hw = sky2->hw;
2607 unsigned port = sky2->port;
793b883e 2608 u16 ledctrl, ledover = 0;
cd28ab6a 2609 long ms;
91c86df5 2610 int interrupted;
cd28ab6a
SH
2611 int onoff = 1;
2612
793b883e 2613 if (!data || data > (u32) (MAX_SCHEDULE_TIMEOUT / HZ))
cd28ab6a
SH
2614 ms = jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT);
2615 else
2616 ms = data * 1000;
2617
2618 /* save initial values */
91c86df5 2619 down(&sky2->phy_sema);
793b883e
SH
2620 if (hw->chip_id == CHIP_ID_YUKON_XL) {
2621 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
2622 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
2623 ledctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
2624 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
2625 } else {
2626 ledctrl = gm_phy_read(hw, port, PHY_MARV_LED_CTRL);
2627 ledover = gm_phy_read(hw, port, PHY_MARV_LED_OVER);
2628 }
cd28ab6a 2629
91c86df5
SH
2630 interrupted = 0;
2631 while (!interrupted && ms > 0) {
cd28ab6a
SH
2632 sky2_led(hw, port, onoff);
2633 onoff = !onoff;
2634
91c86df5
SH
2635 up(&sky2->phy_sema);
2636 interrupted = msleep_interruptible(250);
2637 down(&sky2->phy_sema);
2638
cd28ab6a
SH
2639 ms -= 250;
2640 }
2641
2642 /* resume regularly scheduled programming */
793b883e
SH
2643 if (hw->chip_id == CHIP_ID_YUKON_XL) {
2644 u16 pg = gm_phy_read(hw, port, PHY_MARV_EXT_ADR);
2645 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, 3);
2646 gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ledctrl);
2647 gm_phy_write(hw, port, PHY_MARV_EXT_ADR, pg);
2648 } else {
2649 gm_phy_write(hw, port, PHY_MARV_LED_CTRL, ledctrl);
2650 gm_phy_write(hw, port, PHY_MARV_LED_OVER, ledover);
2651 }
91c86df5 2652 up(&sky2->phy_sema);
cd28ab6a
SH
2653
2654 return 0;
2655}
2656
2657static void sky2_get_pauseparam(struct net_device *dev,
2658 struct ethtool_pauseparam *ecmd)
2659{
2660 struct sky2_port *sky2 = netdev_priv(dev);
2661
2662 ecmd->tx_pause = sky2->tx_pause;
2663 ecmd->rx_pause = sky2->rx_pause;
2664 ecmd->autoneg = sky2->autoneg;
2665}
2666
2667static int sky2_set_pauseparam(struct net_device *dev,
2668 struct ethtool_pauseparam *ecmd)
2669{
2670 struct sky2_port *sky2 = netdev_priv(dev);
2671 int err = 0;
2672
2673 sky2->autoneg = ecmd->autoneg;
2674 sky2->tx_pause = ecmd->tx_pause != 0;
2675 sky2->rx_pause = ecmd->rx_pause != 0;
2676
1b537565 2677 sky2_phy_reinit(sky2);
cd28ab6a
SH
2678
2679 return err;
2680}
2681
2682#ifdef CONFIG_PM
2683static void sky2_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2684{
2685 struct sky2_port *sky2 = netdev_priv(dev);
2686
2687 wol->supported = WAKE_MAGIC;
2688 wol->wolopts = sky2->wol ? WAKE_MAGIC : 0;
2689}
2690
2691static int sky2_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2692{
2693 struct sky2_port *sky2 = netdev_priv(dev);
2694 struct sky2_hw *hw = sky2->hw;
2695
2696 if (wol->wolopts != WAKE_MAGIC && wol->wolopts != 0)
2697 return -EOPNOTSUPP;
2698
2699 sky2->wol = wol->wolopts == WAKE_MAGIC;
2700
2701 if (sky2->wol) {
2702 memcpy_toio(hw->regs + WOL_MAC_ADDR, dev->dev_addr, ETH_ALEN);
2703
2704 sky2_write16(hw, WOL_CTRL_STAT,
2705 WOL_CTL_ENA_PME_ON_MAGIC_PKT |
2706 WOL_CTL_ENA_MAGIC_PKT_UNIT);
2707 } else
2708 sky2_write16(hw, WOL_CTRL_STAT, WOL_CTL_DEFAULT);
2709
2710 return 0;
2711}
2712#endif
2713
fb17358f
SH
2714static int sky2_get_coalesce(struct net_device *dev,
2715 struct ethtool_coalesce *ecmd)
2716{
2717 struct sky2_port *sky2 = netdev_priv(dev);
2718 struct sky2_hw *hw = sky2->hw;
2719
2720 if (sky2_read8(hw, STAT_TX_TIMER_CTRL) == TIM_STOP)
2721 ecmd->tx_coalesce_usecs = 0;
2722 else {
2723 u32 clks = sky2_read32(hw, STAT_TX_TIMER_INI);
2724 ecmd->tx_coalesce_usecs = sky2_clk2us(hw, clks);
2725 }
2726 ecmd->tx_max_coalesced_frames = sky2_read16(hw, STAT_TX_IDX_TH);
2727
2728 if (sky2_read8(hw, STAT_LEV_TIMER_CTRL) == TIM_STOP)
2729 ecmd->rx_coalesce_usecs = 0;
2730 else {
2731 u32 clks = sky2_read32(hw, STAT_LEV_TIMER_INI);
2732 ecmd->rx_coalesce_usecs = sky2_clk2us(hw, clks);
2733 }
2734 ecmd->rx_max_coalesced_frames = sky2_read8(hw, STAT_FIFO_WM);
2735
2736 if (sky2_read8(hw, STAT_ISR_TIMER_CTRL) == TIM_STOP)
2737 ecmd->rx_coalesce_usecs_irq = 0;
2738 else {
2739 u32 clks = sky2_read32(hw, STAT_ISR_TIMER_INI);
2740 ecmd->rx_coalesce_usecs_irq = sky2_clk2us(hw, clks);
2741 }
2742
2743 ecmd->rx_max_coalesced_frames_irq = sky2_read8(hw, STAT_FIFO_ISR_WM);
2744
2745 return 0;
2746}
2747
2748/* Note: this affect both ports */
2749static int sky2_set_coalesce(struct net_device *dev,
2750 struct ethtool_coalesce *ecmd)
2751{
2752 struct sky2_port *sky2 = netdev_priv(dev);
2753 struct sky2_hw *hw = sky2->hw;
2754 const u32 tmin = sky2_clk2us(hw, 1);
2755 const u32 tmax = 5000;
2756
2757 if (ecmd->tx_coalesce_usecs != 0 &&
2758 (ecmd->tx_coalesce_usecs < tmin || ecmd->tx_coalesce_usecs > tmax))
2759 return -EINVAL;
2760
2761 if (ecmd->rx_coalesce_usecs != 0 &&
2762 (ecmd->rx_coalesce_usecs < tmin || ecmd->rx_coalesce_usecs > tmax))
2763 return -EINVAL;
2764
2765 if (ecmd->rx_coalesce_usecs_irq != 0 &&
2766 (ecmd->rx_coalesce_usecs_irq < tmin || ecmd->rx_coalesce_usecs_irq > tmax))
2767 return -EINVAL;
2768
2769 if (ecmd->tx_max_coalesced_frames > 0xffff)
2770 return -EINVAL;
2771 if (ecmd->rx_max_coalesced_frames > 0xff)
2772 return -EINVAL;
2773 if (ecmd->rx_max_coalesced_frames_irq > 0xff)
2774 return -EINVAL;
2775
2776 if (ecmd->tx_coalesce_usecs == 0)
2777 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_STOP);
2778 else {
2779 sky2_write32(hw, STAT_TX_TIMER_INI,
2780 sky2_us2clk(hw, ecmd->tx_coalesce_usecs));
2781 sky2_write8(hw, STAT_TX_TIMER_CTRL, TIM_START);
2782 }
2783 sky2_write16(hw, STAT_TX_IDX_TH, ecmd->tx_max_coalesced_frames);
2784
2785 if (ecmd->rx_coalesce_usecs == 0)
2786 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_STOP);
2787 else {
2788 sky2_write32(hw, STAT_LEV_TIMER_INI,
2789 sky2_us2clk(hw, ecmd->rx_coalesce_usecs));
2790 sky2_write8(hw, STAT_LEV_TIMER_CTRL, TIM_START);
2791 }
2792 sky2_write8(hw, STAT_FIFO_WM, ecmd->rx_max_coalesced_frames);
2793
2794 if (ecmd->rx_coalesce_usecs_irq == 0)
2795 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_STOP);
2796 else {
2797 sky2_write32(hw, STAT_TX_TIMER_INI,
2798 sky2_us2clk(hw, ecmd->rx_coalesce_usecs_irq));
2799 sky2_write8(hw, STAT_ISR_TIMER_CTRL, TIM_START);
2800 }
2801 sky2_write8(hw, STAT_FIFO_ISR_WM, ecmd->rx_max_coalesced_frames_irq);
2802 return 0;
2803}
2804
793b883e
SH
2805static void sky2_get_ringparam(struct net_device *dev,
2806 struct ethtool_ringparam *ering)
2807{
2808 struct sky2_port *sky2 = netdev_priv(dev);
2809
2810 ering->rx_max_pending = RX_MAX_PENDING;
2811 ering->rx_mini_max_pending = 0;
2812 ering->rx_jumbo_max_pending = 0;
2813 ering->tx_max_pending = TX_RING_SIZE - 1;
2814
2815 ering->rx_pending = sky2->rx_pending;
2816 ering->rx_mini_pending = 0;
2817 ering->rx_jumbo_pending = 0;
2818 ering->tx_pending = sky2->tx_pending;
2819}
2820
2821static int sky2_set_ringparam(struct net_device *dev,
2822 struct ethtool_ringparam *ering)
2823{
2824 struct sky2_port *sky2 = netdev_priv(dev);
2825 int err = 0;
2826
2827 if (ering->rx_pending > RX_MAX_PENDING ||
2828 ering->rx_pending < 8 ||
2829 ering->tx_pending < MAX_SKB_TX_LE ||
2830 ering->tx_pending > TX_RING_SIZE - 1)
2831 return -EINVAL;
2832
2833 if (netif_running(dev))
2834 sky2_down(dev);
2835
2836 sky2->rx_pending = ering->rx_pending;
2837 sky2->tx_pending = ering->tx_pending;
2838
1b537565 2839 if (netif_running(dev)) {
793b883e 2840 err = sky2_up(dev);
1b537565
SH
2841 if (err)
2842 dev_close(dev);
6ed995bb
SH
2843 else
2844 sky2_set_multicast(dev);
1b537565 2845 }
793b883e
SH
2846
2847 return err;
2848}
2849
793b883e
SH
2850static int sky2_get_regs_len(struct net_device *dev)
2851{
6e4cbb34 2852 return 0x4000;
793b883e
SH
2853}
2854
2855/*
2856 * Returns copy of control register region
6e4cbb34 2857 * Note: access to the RAM address register set will cause timeouts.
793b883e
SH
2858 */
2859static void sky2_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2860 void *p)
2861{
2862 const struct sky2_port *sky2 = netdev_priv(dev);
793b883e 2863 const void __iomem *io = sky2->hw->regs;
793b883e 2864
6e4cbb34 2865 BUG_ON(regs->len < B3_RI_WTO_R1);
793b883e 2866 regs->version = 1;
6e4cbb34 2867 memset(p, 0, regs->len);
793b883e 2868
6e4cbb34
SH
2869 memcpy_fromio(p, io, B3_RAM_ADDR);
2870
2871 memcpy_fromio(p + B3_RI_WTO_R1,
2872 io + B3_RI_WTO_R1,
2873 regs->len - B3_RI_WTO_R1);
793b883e 2874}
cd28ab6a
SH
2875
2876static struct ethtool_ops sky2_ethtool_ops = {
793b883e
SH
2877 .get_settings = sky2_get_settings,
2878 .set_settings = sky2_set_settings,
2879 .get_drvinfo = sky2_get_drvinfo,
2880 .get_msglevel = sky2_get_msglevel,
2881 .set_msglevel = sky2_set_msglevel,
9a7ae0a9 2882 .nway_reset = sky2_nway_reset,
793b883e
SH
2883 .get_regs_len = sky2_get_regs_len,
2884 .get_regs = sky2_get_regs,
2885 .get_link = ethtool_op_get_link,
2886 .get_sg = ethtool_op_get_sg,
2887 .set_sg = ethtool_op_set_sg,
2888 .get_tx_csum = ethtool_op_get_tx_csum,
2889 .set_tx_csum = ethtool_op_set_tx_csum,
2890 .get_tso = ethtool_op_get_tso,
2891 .set_tso = ethtool_op_set_tso,
2892 .get_rx_csum = sky2_get_rx_csum,
2893 .set_rx_csum = sky2_set_rx_csum,
2894 .get_strings = sky2_get_strings,
fb17358f
SH
2895 .get_coalesce = sky2_get_coalesce,
2896 .set_coalesce = sky2_set_coalesce,
793b883e
SH
2897 .get_ringparam = sky2_get_ringparam,
2898 .set_ringparam = sky2_set_ringparam,
cd28ab6a
SH
2899 .get_pauseparam = sky2_get_pauseparam,
2900 .set_pauseparam = sky2_set_pauseparam,
2901#ifdef CONFIG_PM
793b883e
SH
2902 .get_wol = sky2_get_wol,
2903 .set_wol = sky2_set_wol,
cd28ab6a 2904#endif
793b883e 2905 .phys_id = sky2_phys_id,
cd28ab6a
SH
2906 .get_stats_count = sky2_get_stats_count,
2907 .get_ethtool_stats = sky2_get_ethtool_stats,
2995bfb7 2908 .get_perm_addr = ethtool_op_get_perm_addr,
cd28ab6a
SH
2909};
2910
2911/* Initialize network device */
2912static __devinit struct net_device *sky2_init_netdev(struct sky2_hw *hw,
2913 unsigned port, int highmem)
2914{
2915 struct sky2_port *sky2;
2916 struct net_device *dev = alloc_etherdev(sizeof(*sky2));
2917
2918 if (!dev) {
2919 printk(KERN_ERR "sky2 etherdev alloc failed");
2920 return NULL;
2921 }
2922
2923 SET_MODULE_OWNER(dev);
2924 SET_NETDEV_DEV(dev, &hw->pdev->dev);
ef743d33 2925 dev->irq = hw->pdev->irq;
cd28ab6a
SH
2926 dev->open = sky2_up;
2927 dev->stop = sky2_down;
ef743d33 2928 dev->do_ioctl = sky2_ioctl;
cd28ab6a
SH
2929 dev->hard_start_xmit = sky2_xmit_frame;
2930 dev->get_stats = sky2_get_stats;
2931 dev->set_multicast_list = sky2_set_multicast;
2932 dev->set_mac_address = sky2_set_mac_address;
2933 dev->change_mtu = sky2_change_mtu;
2934 SET_ETHTOOL_OPS(dev, &sky2_ethtool_ops);
2935 dev->tx_timeout = sky2_tx_timeout;
2936 dev->watchdog_timeo = TX_WATCHDOG;
2937 if (port == 0)
2938 dev->poll = sky2_poll;
2939 dev->weight = NAPI_WEIGHT;
2940#ifdef CONFIG_NET_POLL_CONTROLLER
2941 dev->poll_controller = sky2_netpoll;
2942#endif
cd28ab6a
SH
2943
2944 sky2 = netdev_priv(dev);
2945 sky2->netdev = dev;
2946 sky2->hw = hw;
2947 sky2->msg_enable = netif_msg_init(debug, default_msg);
2948
2949 spin_lock_init(&sky2->tx_lock);
2950 /* Auto speed and flow control */
2951 sky2->autoneg = AUTONEG_ENABLE;
585b5601 2952 sky2->tx_pause = 1;
cd28ab6a
SH
2953 sky2->rx_pause = 1;
2954 sky2->duplex = -1;
2955 sky2->speed = -1;
2956 sky2->advertising = sky2_supported_modes(hw);
75d070c5
SH
2957
2958 /* Receive checksum disabled for Yukon XL
2959 * because of observed problems with incorrect
2960 * values when multiple packets are received in one interrupt
2961 */
2962 sky2->rx_csum = (hw->chip_id != CHIP_ID_YUKON_XL);
2963
91c86df5
SH
2964 INIT_WORK(&sky2->phy_task, sky2_phy_task, sky2);
2965 init_MUTEX(&sky2->phy_sema);
793b883e
SH
2966 sky2->tx_pending = TX_DEF_PENDING;
2967 sky2->rx_pending = is_ec_a1(hw) ? 8 : RX_DEF_PENDING;
734d1868 2968 sky2->rx_bufsize = sky2_buf_size(ETH_DATA_LEN);
cd28ab6a
SH
2969
2970 hw->dev[port] = dev;
2971
2972 sky2->port = port;
2973
5a5b1ea0
SH
2974 dev->features |= NETIF_F_LLTX;
2975 if (hw->chip_id != CHIP_ID_YUKON_EC_U)
2976 dev->features |= NETIF_F_TSO;
cd28ab6a
SH
2977 if (highmem)
2978 dev->features |= NETIF_F_HIGHDMA;
793b883e 2979 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
cd28ab6a 2980
d1f13708
SH
2981#ifdef SKY2_VLAN_TAG_USED
2982 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2983 dev->vlan_rx_register = sky2_vlan_rx_register;
2984 dev->vlan_rx_kill_vid = sky2_vlan_rx_kill_vid;
2985#endif
2986
cd28ab6a 2987 /* read the mac address */
793b883e 2988 memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
2995bfb7 2989 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
cd28ab6a
SH
2990
2991 /* device is off until link detection */
2992 netif_carrier_off(dev);
2993 netif_stop_queue(dev);
2994
2995 return dev;
2996}
2997
2998static inline void sky2_show_addr(struct net_device *dev)
2999{
3000 const struct sky2_port *sky2 = netdev_priv(dev);
3001
3002 if (netif_msg_probe(sky2))
3003 printk(KERN_INFO PFX "%s: addr %02x:%02x:%02x:%02x:%02x:%02x\n",
3004 dev->name,
3005 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
3006 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
3007}
3008
3009static int __devinit sky2_probe(struct pci_dev *pdev,
3010 const struct pci_device_id *ent)
3011{
793b883e 3012 struct net_device *dev, *dev1 = NULL;
cd28ab6a 3013 struct sky2_hw *hw;
5afa0a9c 3014 int err, pm_cap, using_dac = 0;
cd28ab6a 3015
793b883e
SH
3016 err = pci_enable_device(pdev);
3017 if (err) {
cd28ab6a
SH
3018 printk(KERN_ERR PFX "%s cannot enable PCI device\n",
3019 pci_name(pdev));
3020 goto err_out;
3021 }
3022
793b883e
SH
3023 err = pci_request_regions(pdev, DRV_NAME);
3024 if (err) {
cd28ab6a
SH
3025 printk(KERN_ERR PFX "%s cannot obtain PCI resources\n",
3026 pci_name(pdev));
793b883e 3027 goto err_out;
cd28ab6a
SH
3028 }
3029
3030 pci_set_master(pdev);
3031
5afa0a9c
SH
3032 /* Find power-management capability. */
3033 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
3034 if (pm_cap == 0) {
3035 printk(KERN_ERR PFX "Cannot find PowerManagement capability, "
3036 "aborting.\n");
3037 err = -EIO;
3038 goto err_out_free_regions;
3039 }
3040
cd28ab6a
SH
3041 if (sizeof(dma_addr_t) > sizeof(u32)) {
3042 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
3043 if (!err)
3044 using_dac = 1;
3045 }
3046
3047 if (!using_dac) {
3048 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3049 if (err) {
3050 printk(KERN_ERR PFX "%s no usable DMA configuration\n",
3051 pci_name(pdev));
3052 goto err_out_free_regions;
3053 }
3054 }
cd28ab6a 3055#ifdef __BIG_ENDIAN
d571b694 3056 /* byte swap descriptors in hardware */
cd28ab6a
SH
3057 {
3058 u32 reg;
3059
3060 pci_read_config_dword(pdev, PCI_DEV_REG2, &reg);
3061 reg |= PCI_REV_DESC;
3062 pci_write_config_dword(pdev, PCI_DEV_REG2, reg);
3063 }
3064#endif
3065
3066 err = -ENOMEM;
3067 hw = kmalloc(sizeof(*hw), GFP_KERNEL);
3068 if (!hw) {
3069 printk(KERN_ERR PFX "%s: cannot allocate hardware struct\n",
3070 pci_name(pdev));
3071 goto err_out_free_regions;
3072 }
3073
3074 memset(hw, 0, sizeof(*hw));
3075 hw->pdev = pdev;
cd28ab6a
SH
3076
3077 hw->regs = ioremap_nocache(pci_resource_start(pdev, 0), 0x4000);
3078 if (!hw->regs) {
3079 printk(KERN_ERR PFX "%s: cannot map device registers\n",
3080 pci_name(pdev));
3081 goto err_out_free_hw;
3082 }
5afa0a9c 3083 hw->pm_cap = pm_cap;
cd28ab6a 3084
cd28ab6a
SH
3085 err = sky2_reset(hw);
3086 if (err)
793b883e 3087 goto err_out_iounmap;
cd28ab6a 3088
5f4f9dc1
SH
3089 printk(KERN_INFO PFX "v%s addr 0x%lx irq %d Yukon-%s (0x%x) rev %d\n",
3090 DRV_VERSION, pci_resource_start(pdev, 0), pdev->irq,
92f965e8 3091 yukon2_name[hw->chip_id - CHIP_ID_YUKON_XL],
793b883e 3092 hw->chip_id, hw->chip_rev);
cd28ab6a 3093
793b883e
SH
3094 dev = sky2_init_netdev(hw, 0, using_dac);
3095 if (!dev)
cd28ab6a
SH
3096 goto err_out_free_pci;
3097
793b883e
SH
3098 err = register_netdev(dev);
3099 if (err) {
cd28ab6a
SH
3100 printk(KERN_ERR PFX "%s: cannot register net device\n",
3101 pci_name(pdev));
3102 goto err_out_free_netdev;
3103 }
3104
3105 sky2_show_addr(dev);
3106
3107 if (hw->ports > 1 && (dev1 = sky2_init_netdev(hw, 1, using_dac))) {
3108 if (register_netdev(dev1) == 0)
3109 sky2_show_addr(dev1);
3110 else {
3111 /* Failure to register second port need not be fatal */
793b883e
SH
3112 printk(KERN_WARNING PFX
3113 "register of second port failed\n");
cd28ab6a
SH
3114 hw->dev[1] = NULL;
3115 free_netdev(dev1);
3116 }
3117 }
3118
793b883e
SH
3119 err = request_irq(pdev->irq, sky2_intr, SA_SHIRQ, DRV_NAME, hw);
3120 if (err) {
3121 printk(KERN_ERR PFX "%s: cannot assign irq %d\n",
3122 pci_name(pdev), pdev->irq);
3123 goto err_out_unregister;
3124 }
3125
3126 hw->intr_mask = Y2_IS_BASE;
3127 sky2_write32(hw, B0_IMSK, hw->intr_mask);
3128
3129 pci_set_drvdata(pdev, hw);
3130
cd28ab6a
SH
3131 return 0;
3132
793b883e
SH
3133err_out_unregister:
3134 if (dev1) {
3135 unregister_netdev(dev1);
3136 free_netdev(dev1);
3137 }
3138 unregister_netdev(dev);
cd28ab6a
SH
3139err_out_free_netdev:
3140 free_netdev(dev);
cd28ab6a 3141err_out_free_pci:
793b883e 3142 sky2_write8(hw, B0_CTST, CS_RST_SET);
cd28ab6a
SH
3143 pci_free_consistent(hw->pdev, STATUS_LE_BYTES, hw->st_le, hw->st_dma);
3144err_out_iounmap:
3145 iounmap(hw->regs);
3146err_out_free_hw:
3147 kfree(hw);
3148err_out_free_regions:
3149 pci_release_regions(pdev);
cd28ab6a 3150 pci_disable_device(pdev);
cd28ab6a
SH
3151err_out:
3152 return err;
3153}
3154
3155static void __devexit sky2_remove(struct pci_dev *pdev)
3156{
793b883e 3157 struct sky2_hw *hw = pci_get_drvdata(pdev);
cd28ab6a
SH
3158 struct net_device *dev0, *dev1;
3159
793b883e 3160 if (!hw)
cd28ab6a
SH
3161 return;
3162
cd28ab6a 3163 dev0 = hw->dev[0];
793b883e
SH
3164 dev1 = hw->dev[1];
3165 if (dev1)
3166 unregister_netdev(dev1);
cd28ab6a
SH
3167 unregister_netdev(dev0);
3168
793b883e 3169 sky2_write32(hw, B0_IMSK, 0);
5afa0a9c 3170 sky2_set_power_state(hw, PCI_D3hot);
cd28ab6a 3171 sky2_write16(hw, B0_Y2LED, LED_STAT_OFF);
793b883e 3172 sky2_write8(hw, B0_CTST, CS_RST_SET);
5afa0a9c 3173 sky2_read8(hw, B0_CTST);
cd28ab6a
SH
3174
3175 free_irq(pdev->irq, hw);
793b883e 3176 pci_free_consistent(pdev, STATUS_LE_BYTES, hw->st_le, hw->st_dma);
cd28ab6a
SH
3177 pci_release_regions(pdev);
3178 pci_disable_device(pdev);
793b883e 3179
cd28ab6a
SH
3180 if (dev1)
3181 free_netdev(dev1);
3182 free_netdev(dev0);
3183 iounmap(hw->regs);
3184 kfree(hw);
5afa0a9c 3185
cd28ab6a
SH
3186 pci_set_drvdata(pdev, NULL);
3187}
3188
3189#ifdef CONFIG_PM
3190static int sky2_suspend(struct pci_dev *pdev, pm_message_t state)
3191{
793b883e 3192 struct sky2_hw *hw = pci_get_drvdata(pdev);
5afa0a9c 3193 int i;
cd28ab6a
SH
3194
3195 for (i = 0; i < 2; i++) {
3196 struct net_device *dev = hw->dev[i];
3197
3198 if (dev) {
5afa0a9c
SH
3199 if (!netif_running(dev))
3200 continue;
3201
3202 sky2_down(dev);
cd28ab6a 3203 netif_device_detach(dev);
cd28ab6a
SH
3204 }
3205 }
3206
5afa0a9c 3207 return sky2_set_power_state(hw, pci_choose_state(pdev, state));
cd28ab6a
SH
3208}
3209
3210static int sky2_resume(struct pci_dev *pdev)
3211{
793b883e 3212 struct sky2_hw *hw = pci_get_drvdata(pdev);
cd28ab6a
SH
3213 int i;
3214
cd28ab6a
SH
3215 pci_restore_state(pdev);
3216 pci_enable_wake(pdev, PCI_D0, 0);
5afa0a9c 3217 sky2_set_power_state(hw, PCI_D0);
cd28ab6a
SH
3218
3219 sky2_reset(hw);
3220
3221 for (i = 0; i < 2; i++) {
3222 struct net_device *dev = hw->dev[i];
3223 if (dev) {
5afa0a9c
SH
3224 if (netif_running(dev)) {
3225 netif_device_attach(dev);
1b537565
SH
3226 if (sky2_up(dev))
3227 dev_close(dev);
5afa0a9c 3228 }
cd28ab6a
SH
3229 }
3230 }
3231 return 0;
3232}
3233#endif
3234
3235static struct pci_driver sky2_driver = {
793b883e
SH
3236 .name = DRV_NAME,
3237 .id_table = sky2_id_table,
3238 .probe = sky2_probe,
3239 .remove = __devexit_p(sky2_remove),
cd28ab6a 3240#ifdef CONFIG_PM
793b883e
SH
3241 .suspend = sky2_suspend,
3242 .resume = sky2_resume,
cd28ab6a
SH
3243#endif
3244};
3245
3246static int __init sky2_init_module(void)
3247{
50241c4c 3248 return pci_register_driver(&sky2_driver);
cd28ab6a
SH
3249}
3250
3251static void __exit sky2_cleanup_module(void)
3252{
3253 pci_unregister_driver(&sky2_driver);
3254}
3255
3256module_init(sky2_init_module);
3257module_exit(sky2_cleanup_module);
3258
3259MODULE_DESCRIPTION("Marvell Yukon 2 Gigabit Ethernet driver");
3260MODULE_AUTHOR("Stephen Hemminger <shemminger@osdl.org>");
3261MODULE_LICENSE("GPL");
5f4f9dc1 3262MODULE_VERSION(DRV_VERSION);