blob: 0ebd8208f606afdaf0f387ad3353ccb7b3d958e8 [file] [log] [blame]
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001/*
2 * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
Jay Cliburn305282b2008-02-02 19:50:04 -06003 * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
Jay Cliburne8f720f2008-05-09 22:12:09 -05004 * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05005 *
6 * Derived from Intel e1000 driver
7 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
25 *
26 * Contact Information:
Jay Cliburnc8f2d9b2008-09-27 04:17:23 +000027 * Xiong Huang <xiong.huang@atheros.com>
28 * Jie Yang <jie.yang@atheros.com>
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050029 * Chris Snook <csnook@redhat.com>
30 * Jay Cliburn <jcliburn@gmail.com>
31 *
Jay Cliburnc8f2d9b2008-09-27 04:17:23 +000032 * This version is adapted from the Attansic reference driver.
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050033 *
34 * TODO:
Jay Cliburn53ffb422007-07-15 11:03:27 -050035 * Add more ethtool functions.
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050036 * Fix abstruse irq enable/disable condition described here:
37 * http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
38 *
39 * NEEDS TESTING:
40 * VLAN
41 * multicast
42 * promiscuous mode
43 * interrupt coalescing
44 * SMP torture testing
45 */
46
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050047#include <asm/atomic.h>
48#include <asm/byteorder.h>
49
Jay Cliburn305282b2008-02-02 19:50:04 -060050#include <linux/compiler.h>
51#include <linux/crc32.h>
52#include <linux/delay.h>
53#include <linux/dma-mapping.h>
54#include <linux/etherdevice.h>
55#include <linux/hardirq.h>
56#include <linux/if_ether.h>
57#include <linux/if_vlan.h>
58#include <linux/in.h>
59#include <linux/interrupt.h>
60#include <linux/ip.h>
61#include <linux/irqflags.h>
62#include <linux/irqreturn.h>
63#include <linux/jiffies.h>
64#include <linux/mii.h>
65#include <linux/module.h>
66#include <linux/moduleparam.h>
67#include <linux/net.h>
68#include <linux/netdevice.h>
69#include <linux/pci.h>
70#include <linux/pci_ids.h>
71#include <linux/pm.h>
72#include <linux/skbuff.h>
73#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/string.h>
76#include <linux/tcp.h>
77#include <linux/timer.h>
78#include <linux/types.h>
79#include <linux/workqueue.h>
80
81#include <net/checksum.h>
82
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050083#include "atl1.h"
84
Alex Chiang5ad18902009-05-26 20:50:12 -070085#define ATLX_DRIVER_VERSION "2.1.3"
86MODULE_AUTHOR("Xiong Huang <xiong.huang@atheros.com>, \
Joe Perches44ebb952010-03-26 16:27:55 +000087Chris Snook <csnook@redhat.com>, Jay Cliburn <jcliburn@gmail.com>");
Alex Chiang5ad18902009-05-26 20:50:12 -070088MODULE_LICENSE("GPL");
89MODULE_VERSION(ATLX_DRIVER_VERSION);
90
Jay Cliburn305282b2008-02-02 19:50:04 -060091/* Temporary hack for merging atl1 and atl2 */
92#include "atlx.c"
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050093
94/*
Chris Snook8ec72262008-04-18 21:51:53 -040095 * This is the only thing that needs to be changed to adjust the
96 * maximum number of ports that the driver can manage.
97 */
98#define ATL1_MAX_NIC 4
99
100#define OPTION_UNSET -1
101#define OPTION_DISABLED 0
102#define OPTION_ENABLED 1
103
104#define ATL1_PARAM_INIT { [0 ... ATL1_MAX_NIC] = OPTION_UNSET }
105
106/*
107 * Interrupt Moderate Timer in units of 2 us
108 *
109 * Valid Range: 10-65535
110 *
111 * Default Value: 100 (200us)
112 */
113static int __devinitdata int_mod_timer[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
Hannes Ederb79d8ff2009-02-14 11:15:17 +0000114static unsigned int num_int_mod_timer;
Chris Snook8ec72262008-04-18 21:51:53 -0400115module_param_array_named(int_mod_timer, int_mod_timer, int,
116 &num_int_mod_timer, 0);
117MODULE_PARM_DESC(int_mod_timer, "Interrupt moderator timer");
118
119#define DEFAULT_INT_MOD_CNT 100 /* 200us */
120#define MAX_INT_MOD_CNT 65000
121#define MIN_INT_MOD_CNT 50
122
123struct atl1_option {
124 enum { enable_option, range_option, list_option } type;
125 char *name;
126 char *err;
127 int def;
128 union {
129 struct { /* range_option info */
130 int min;
131 int max;
132 } r;
133 struct { /* list_option info */
134 int nr;
135 struct atl1_opt_list {
136 int i;
137 char *str;
138 } *p;
139 } l;
140 } arg;
141};
142
143static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
144 struct pci_dev *pdev)
145{
146 if (*value == OPTION_UNSET) {
147 *value = opt->def;
148 return 0;
149 }
150
151 switch (opt->type) {
152 case enable_option:
153 switch (*value) {
154 case OPTION_ENABLED:
155 dev_info(&pdev->dev, "%s enabled\n", opt->name);
156 return 0;
157 case OPTION_DISABLED:
158 dev_info(&pdev->dev, "%s disabled\n", opt->name);
159 return 0;
160 }
161 break;
162 case range_option:
163 if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
164 dev_info(&pdev->dev, "%s set to %i\n", opt->name,
165 *value);
166 return 0;
167 }
168 break;
169 case list_option:{
170 int i;
171 struct atl1_opt_list *ent;
172
173 for (i = 0; i < opt->arg.l.nr; i++) {
174 ent = &opt->arg.l.p[i];
175 if (*value == ent->i) {
176 if (ent->str[0] != '\0')
177 dev_info(&pdev->dev, "%s\n",
178 ent->str);
179 return 0;
180 }
181 }
182 }
183 break;
184
185 default:
186 break;
187 }
188
189 dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
190 opt->name, *value, opt->err);
191 *value = opt->def;
192 return -1;
193}
194
195/*
196 * atl1_check_options - Range Checking for Command Line Parameters
197 * @adapter: board private structure
198 *
199 * This routine checks all command line parameters for valid user
200 * input. If an invalid value is given, or if no user specified
201 * value exists, a default value is used. The final value is stored
202 * in a variable in the adapter structure.
203 */
Hannes Eder9dc20f52008-12-25 23:58:35 -0800204static void __devinit atl1_check_options(struct atl1_adapter *adapter)
Chris Snook8ec72262008-04-18 21:51:53 -0400205{
206 struct pci_dev *pdev = adapter->pdev;
207 int bd = adapter->bd_number;
208 if (bd >= ATL1_MAX_NIC) {
209 dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
210 dev_notice(&pdev->dev, "using defaults for all values\n");
211 }
212 { /* Interrupt Moderate Timer */
213 struct atl1_option opt = {
214 .type = range_option,
215 .name = "Interrupt Moderator Timer",
216 .err = "using default of "
217 __MODULE_STRING(DEFAULT_INT_MOD_CNT),
218 .def = DEFAULT_INT_MOD_CNT,
219 .arg = {.r = {.min = MIN_INT_MOD_CNT,
220 .max = MAX_INT_MOD_CNT} }
221 };
222 int val;
223 if (num_int_mod_timer > bd) {
224 val = int_mod_timer[bd];
225 atl1_validate_option(&val, &opt, pdev);
226 adapter->imt = (u16) val;
227 } else
228 adapter->imt = (u16) (opt.def);
229 }
230}
231
232/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500233 * atl1_pci_tbl - PCI Device ID Table
234 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(atl1_pci_tbl) = {
Chris Snooke81e5572007-02-14 20:17:01 -0600236 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500237 /* required last entry */
238 {0,}
239};
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500240MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
241
Jay Cliburn460578b2008-02-02 19:50:09 -0600242static const u32 atl1_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
243 NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
244
245static int debug = -1;
246module_param(debug, int, 0);
247MODULE_PARM_DESC(debug, "Message level (0=none,...,16=all)");
248
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500249/*
Jay Cliburn6446a862008-02-02 19:50:12 -0600250 * Reset the transmit and receive units; mask and clear all interrupts.
251 * hw - Struct containing variables accessed by shared code
252 * return : 0 or idle status (if error)
253 */
254static s32 atl1_reset_hw(struct atl1_hw *hw)
255{
256 struct pci_dev *pdev = hw->back->pdev;
257 struct atl1_adapter *adapter = hw->back;
258 u32 icr;
259 int i;
260
261 /*
262 * Clear Interrupt mask to stop board from generating
263 * interrupts & Clear any pending interrupt events
264 */
265 /*
266 * iowrite32(0, hw->hw_addr + REG_IMR);
267 * iowrite32(0xffffffff, hw->hw_addr + REG_ISR);
268 */
269
270 /*
271 * Issue Soft Reset to the MAC. This will reset the chip's
272 * transmit, receive, DMA. It will not effect
273 * the current PCI configuration. The global reset bit is self-
274 * clearing, and should clear within a microsecond.
275 */
276 iowrite32(MASTER_CTRL_SOFT_RST, hw->hw_addr + REG_MASTER_CTRL);
277 ioread32(hw->hw_addr + REG_MASTER_CTRL);
278
279 iowrite16(1, hw->hw_addr + REG_PHY_ENABLE);
280 ioread16(hw->hw_addr + REG_PHY_ENABLE);
281
282 /* delay about 1ms */
283 msleep(1);
284
285 /* Wait at least 10ms for All module to be Idle */
286 for (i = 0; i < 10; i++) {
287 icr = ioread32(hw->hw_addr + REG_IDLE_STATUS);
288 if (!icr)
289 break;
290 /* delay 1 ms */
291 msleep(1);
292 /* FIXME: still the right way to do this? */
293 cpu_relax();
294 }
295
296 if (icr) {
297 if (netif_msg_hw(adapter))
298 dev_dbg(&pdev->dev, "ICR = 0x%x\n", icr);
299 return icr;
300 }
301
302 return 0;
303}
304
305/* function about EEPROM
306 *
307 * check_eeprom_exist
308 * return 0 if eeprom exist
309 */
310static int atl1_check_eeprom_exist(struct atl1_hw *hw)
311{
312 u32 value;
313 value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
314 if (value & SPI_FLASH_CTRL_EN_VPD) {
315 value &= ~SPI_FLASH_CTRL_EN_VPD;
316 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
317 }
318
319 value = ioread16(hw->hw_addr + REG_PCIE_CAP_LIST);
320 return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
321}
322
323static bool atl1_read_eeprom(struct atl1_hw *hw, u32 offset, u32 *p_value)
324{
325 int i;
326 u32 control;
327
328 if (offset & 3)
329 /* address do not align */
330 return false;
331
332 iowrite32(0, hw->hw_addr + REG_VPD_DATA);
333 control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
334 iowrite32(control, hw->hw_addr + REG_VPD_CAP);
335 ioread32(hw->hw_addr + REG_VPD_CAP);
336
337 for (i = 0; i < 10; i++) {
338 msleep(2);
339 control = ioread32(hw->hw_addr + REG_VPD_CAP);
340 if (control & VPD_CAP_VPD_FLAG)
341 break;
342 }
343 if (control & VPD_CAP_VPD_FLAG) {
344 *p_value = ioread32(hw->hw_addr + REG_VPD_DATA);
345 return true;
346 }
347 /* timeout */
348 return false;
349}
350
351/*
352 * Reads the value from a PHY register
353 * hw - Struct containing variables accessed by shared code
354 * reg_addr - address of the PHY register to read
355 */
356s32 atl1_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data)
357{
358 u32 val;
359 int i;
360
361 val = ((u32) (reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
362 MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW | MDIO_CLK_25_4 <<
363 MDIO_CLK_SEL_SHIFT;
364 iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
365 ioread32(hw->hw_addr + REG_MDIO_CTRL);
366
367 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
368 udelay(2);
369 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
370 if (!(val & (MDIO_START | MDIO_BUSY)))
371 break;
372 }
373 if (!(val & (MDIO_START | MDIO_BUSY))) {
374 *phy_data = (u16) val;
375 return 0;
376 }
377 return ATLX_ERR_PHY;
378}
379
380#define CUSTOM_SPI_CS_SETUP 2
381#define CUSTOM_SPI_CLK_HI 2
382#define CUSTOM_SPI_CLK_LO 2
383#define CUSTOM_SPI_CS_HOLD 2
384#define CUSTOM_SPI_CS_HI 3
385
386static bool atl1_spi_read(struct atl1_hw *hw, u32 addr, u32 *buf)
387{
388 int i;
389 u32 value;
390
391 iowrite32(0, hw->hw_addr + REG_SPI_DATA);
392 iowrite32(addr, hw->hw_addr + REG_SPI_ADDR);
393
394 value = SPI_FLASH_CTRL_WAIT_READY |
395 (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
396 SPI_FLASH_CTRL_CS_SETUP_SHIFT | (CUSTOM_SPI_CLK_HI &
397 SPI_FLASH_CTRL_CLK_HI_MASK) <<
398 SPI_FLASH_CTRL_CLK_HI_SHIFT | (CUSTOM_SPI_CLK_LO &
399 SPI_FLASH_CTRL_CLK_LO_MASK) <<
400 SPI_FLASH_CTRL_CLK_LO_SHIFT | (CUSTOM_SPI_CS_HOLD &
401 SPI_FLASH_CTRL_CS_HOLD_MASK) <<
402 SPI_FLASH_CTRL_CS_HOLD_SHIFT | (CUSTOM_SPI_CS_HI &
403 SPI_FLASH_CTRL_CS_HI_MASK) <<
404 SPI_FLASH_CTRL_CS_HI_SHIFT | (1 & SPI_FLASH_CTRL_INS_MASK) <<
405 SPI_FLASH_CTRL_INS_SHIFT;
406
407 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
408
409 value |= SPI_FLASH_CTRL_START;
410 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
411 ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
412
413 for (i = 0; i < 10; i++) {
414 msleep(1);
415 value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
416 if (!(value & SPI_FLASH_CTRL_START))
417 break;
418 }
419
420 if (value & SPI_FLASH_CTRL_START)
421 return false;
422
423 *buf = ioread32(hw->hw_addr + REG_SPI_DATA);
424
425 return true;
426}
427
428/*
429 * get_permanent_address
430 * return 0 if get valid mac address,
431 */
432static int atl1_get_permanent_address(struct atl1_hw *hw)
433{
434 u32 addr[2];
435 u32 i, control;
436 u16 reg;
437 u8 eth_addr[ETH_ALEN];
438 bool key_valid;
439
440 if (is_valid_ether_addr(hw->perm_mac_addr))
441 return 0;
442
443 /* init */
444 addr[0] = addr[1] = 0;
445
446 if (!atl1_check_eeprom_exist(hw)) {
447 reg = 0;
448 key_valid = false;
449 /* Read out all EEPROM content */
450 i = 0;
451 while (1) {
452 if (atl1_read_eeprom(hw, i + 0x100, &control)) {
453 if (key_valid) {
454 if (reg == REG_MAC_STA_ADDR)
455 addr[0] = control;
456 else if (reg == (REG_MAC_STA_ADDR + 4))
457 addr[1] = control;
458 key_valid = false;
459 } else if ((control & 0xff) == 0x5A) {
460 key_valid = true;
461 reg = (u16) (control >> 16);
462 } else
463 break;
464 } else
465 /* read error */
466 break;
467 i += 4;
468 }
469
470 *(u32 *) &eth_addr[2] = swab32(addr[0]);
471 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
472 if (is_valid_ether_addr(eth_addr)) {
473 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
474 return 0;
475 }
Jay Cliburn6446a862008-02-02 19:50:12 -0600476 }
477
478 /* see if SPI FLAGS exist ? */
479 addr[0] = addr[1] = 0;
480 reg = 0;
481 key_valid = false;
482 i = 0;
483 while (1) {
484 if (atl1_spi_read(hw, i + 0x1f000, &control)) {
485 if (key_valid) {
486 if (reg == REG_MAC_STA_ADDR)
487 addr[0] = control;
488 else if (reg == (REG_MAC_STA_ADDR + 4))
489 addr[1] = control;
490 key_valid = false;
491 } else if ((control & 0xff) == 0x5A) {
492 key_valid = true;
493 reg = (u16) (control >> 16);
494 } else
495 /* data end */
496 break;
497 } else
498 /* read error */
499 break;
500 i += 4;
501 }
502
503 *(u32 *) &eth_addr[2] = swab32(addr[0]);
504 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
505 if (is_valid_ether_addr(eth_addr)) {
506 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
507 return 0;
508 }
509
510 /*
511 * On some motherboards, the MAC address is written by the
512 * BIOS directly to the MAC register during POST, and is
513 * not stored in eeprom. If all else thus far has failed
514 * to fetch the permanent MAC address, try reading it directly.
515 */
516 addr[0] = ioread32(hw->hw_addr + REG_MAC_STA_ADDR);
517 addr[1] = ioread16(hw->hw_addr + (REG_MAC_STA_ADDR + 4));
518 *(u32 *) &eth_addr[2] = swab32(addr[0]);
519 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
520 if (is_valid_ether_addr(eth_addr)) {
521 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
522 return 0;
523 }
524
525 return 1;
526}
527
528/*
529 * Reads the adapter's MAC address from the EEPROM
530 * hw - Struct containing variables accessed by shared code
531 */
Hannes Eder9dc20f52008-12-25 23:58:35 -0800532static s32 atl1_read_mac_addr(struct atl1_hw *hw)
Jay Cliburn6446a862008-02-02 19:50:12 -0600533{
534 u16 i;
535
536 if (atl1_get_permanent_address(hw))
537 random_ether_addr(hw->perm_mac_addr);
538
539 for (i = 0; i < ETH_ALEN; i++)
540 hw->mac_addr[i] = hw->perm_mac_addr[i];
541 return 0;
542}
543
544/*
545 * Hashes an address to determine its location in the multicast table
546 * hw - Struct containing variables accessed by shared code
547 * mc_addr - the multicast address to hash
548 *
549 * atl1_hash_mc_addr
550 * purpose
551 * set hash value for a multicast address
552 * hash calcu processing :
553 * 1. calcu 32bit CRC for multicast address
554 * 2. reverse crc with MSB to LSB
555 */
556u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
557{
558 u32 crc32, value = 0;
559 int i;
560
561 crc32 = ether_crc_le(6, mc_addr);
562 for (i = 0; i < 32; i++)
563 value |= (((crc32 >> i) & 1) << (31 - i));
564
565 return value;
566}
567
568/*
569 * Sets the bit in the multicast table corresponding to the hash value.
570 * hw - Struct containing variables accessed by shared code
571 * hash_value - Multicast address hash value
572 */
573void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
574{
575 u32 hash_bit, hash_reg;
576 u32 mta;
577
578 /*
579 * The HASH Table is a register array of 2 32-bit registers.
580 * It is treated like an array of 64 bits. We want to set
581 * bit BitArray[hash_value]. So we figure out what register
582 * the bit is in, read it, OR in the new bit, then write
583 * back the new value. The register is determined by the
584 * upper 7 bits of the hash value and the bit within that
585 * register are determined by the lower 5 bits of the value.
586 */
587 hash_reg = (hash_value >> 31) & 0x1;
588 hash_bit = (hash_value >> 26) & 0x1F;
589 mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
590 mta |= (1 << hash_bit);
591 iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
592}
593
594/*
595 * Writes a value to a PHY register
596 * hw - Struct containing variables accessed by shared code
597 * reg_addr - address of the PHY register to write
598 * data - data to write to the PHY
599 */
600static s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
601{
602 int i;
603 u32 val;
604
605 val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
606 (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
607 MDIO_SUP_PREAMBLE |
608 MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
609 iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
610 ioread32(hw->hw_addr + REG_MDIO_CTRL);
611
612 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
613 udelay(2);
614 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
615 if (!(val & (MDIO_START | MDIO_BUSY)))
616 break;
617 }
618
619 if (!(val & (MDIO_START | MDIO_BUSY)))
620 return 0;
621
622 return ATLX_ERR_PHY;
623}
624
625/*
626 * Make L001's PHY out of Power Saving State (bug)
627 * hw - Struct containing variables accessed by shared code
628 * when power on, L001's PHY always on Power saving State
629 * (Gigabit Link forbidden)
630 */
631static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
632{
633 s32 ret;
634 ret = atl1_write_phy_reg(hw, 29, 0x0029);
635 if (ret)
636 return ret;
637 return atl1_write_phy_reg(hw, 30, 0);
638}
639
640/*
Jay Cliburn6446a862008-02-02 19:50:12 -0600641 * Resets the PHY and make all config validate
642 * hw - Struct containing variables accessed by shared code
643 *
644 * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
645 */
646static s32 atl1_phy_reset(struct atl1_hw *hw)
647{
648 struct pci_dev *pdev = hw->back->pdev;
649 struct atl1_adapter *adapter = hw->back;
650 s32 ret_val;
651 u16 phy_data;
652
653 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
654 hw->media_type == MEDIA_TYPE_1000M_FULL)
655 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
656 else {
657 switch (hw->media_type) {
658 case MEDIA_TYPE_100M_FULL:
659 phy_data =
660 MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
661 MII_CR_RESET;
662 break;
663 case MEDIA_TYPE_100M_HALF:
664 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
665 break;
666 case MEDIA_TYPE_10M_FULL:
667 phy_data =
668 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
669 break;
670 default:
671 /* MEDIA_TYPE_10M_HALF: */
672 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
673 break;
674 }
675 }
676
677 ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
678 if (ret_val) {
679 u32 val;
680 int i;
681 /* pcie serdes link may be down! */
682 if (netif_msg_hw(adapter))
683 dev_dbg(&pdev->dev, "pcie phy link down\n");
684
685 for (i = 0; i < 25; i++) {
686 msleep(1);
687 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
688 if (!(val & (MDIO_START | MDIO_BUSY)))
689 break;
690 }
691
692 if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
693 if (netif_msg_hw(adapter))
694 dev_warn(&pdev->dev,
695 "pcie link down at least 25ms\n");
696 return ret_val;
697 }
698 }
699 return 0;
700}
701
702/*
703 * Configures PHY autoneg and flow control advertisement settings
704 * hw - Struct containing variables accessed by shared code
705 */
706static s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
707{
708 s32 ret_val;
709 s16 mii_autoneg_adv_reg;
710 s16 mii_1000t_ctrl_reg;
711
712 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
713 mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
714
715 /* Read the MII 1000Base-T Control Register (Address 9). */
716 mii_1000t_ctrl_reg = MII_ATLX_CR_1000T_DEFAULT_CAP_MASK;
717
718 /*
719 * First we clear all the 10/100 mb speed bits in the Auto-Neg
720 * Advertisement Register (Address 4) and the 1000 mb speed bits in
721 * the 1000Base-T Control Register (Address 9).
722 */
723 mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
724 mii_1000t_ctrl_reg &= ~MII_ATLX_CR_1000T_SPEED_MASK;
725
726 /*
727 * Need to parse media_type and set up
728 * the appropriate PHY registers.
729 */
730 switch (hw->media_type) {
731 case MEDIA_TYPE_AUTO_SENSOR:
732 mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
733 MII_AR_10T_FD_CAPS |
734 MII_AR_100TX_HD_CAPS |
735 MII_AR_100TX_FD_CAPS);
736 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
737 break;
738
739 case MEDIA_TYPE_1000M_FULL:
740 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
741 break;
742
743 case MEDIA_TYPE_100M_FULL:
744 mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
745 break;
746
747 case MEDIA_TYPE_100M_HALF:
748 mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
749 break;
750
751 case MEDIA_TYPE_10M_FULL:
752 mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
753 break;
754
755 default:
756 mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
757 break;
758 }
759
760 /* flow control fixed to enable all */
761 mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
762
763 hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
764 hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
765
766 ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
767 if (ret_val)
768 return ret_val;
769
770 ret_val = atl1_write_phy_reg(hw, MII_ATLX_CR, mii_1000t_ctrl_reg);
771 if (ret_val)
772 return ret_val;
773
774 return 0;
775}
776
777/*
778 * Configures link settings.
779 * hw - Struct containing variables accessed by shared code
780 * Assumes the hardware has previously been reset and the
781 * transmitter and receiver are not enabled.
782 */
783static s32 atl1_setup_link(struct atl1_hw *hw)
784{
785 struct pci_dev *pdev = hw->back->pdev;
786 struct atl1_adapter *adapter = hw->back;
787 s32 ret_val;
788
789 /*
790 * Options:
791 * PHY will advertise value(s) parsed from
792 * autoneg_advertised and fc
793 * no matter what autoneg is , We will not wait link result.
794 */
795 ret_val = atl1_phy_setup_autoneg_adv(hw);
796 if (ret_val) {
797 if (netif_msg_link(adapter))
798 dev_dbg(&pdev->dev,
799 "error setting up autonegotiation\n");
800 return ret_val;
801 }
802 /* SW.Reset , En-Auto-Neg if needed */
803 ret_val = atl1_phy_reset(hw);
804 if (ret_val) {
805 if (netif_msg_link(adapter))
806 dev_dbg(&pdev->dev, "error resetting phy\n");
807 return ret_val;
808 }
809 hw->phy_configured = true;
810 return ret_val;
811}
812
813static void atl1_init_flash_opcode(struct atl1_hw *hw)
814{
815 if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
816 /* Atmel */
817 hw->flash_vendor = 0;
818
819 /* Init OP table */
820 iowrite8(flash_table[hw->flash_vendor].cmd_program,
821 hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
822 iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
823 hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
824 iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
825 hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
826 iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
827 hw->hw_addr + REG_SPI_FLASH_OP_RDID);
828 iowrite8(flash_table[hw->flash_vendor].cmd_wren,
829 hw->hw_addr + REG_SPI_FLASH_OP_WREN);
830 iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
831 hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
832 iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
833 hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
834 iowrite8(flash_table[hw->flash_vendor].cmd_read,
835 hw->hw_addr + REG_SPI_FLASH_OP_READ);
836}
837
838/*
839 * Performs basic configuration of the adapter.
840 * hw - Struct containing variables accessed by shared code
841 * Assumes that the controller has previously been reset and is in a
842 * post-reset uninitialized state. Initializes multicast table,
843 * and Calls routines to setup link
844 * Leaves the transmit and receive units disabled and uninitialized.
845 */
846static s32 atl1_init_hw(struct atl1_hw *hw)
847{
848 u32 ret_val = 0;
849
850 /* Zero out the Multicast HASH table */
851 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
852 /* clear the old settings from the multicast hash table */
853 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
854
855 atl1_init_flash_opcode(hw);
856
857 if (!hw->phy_configured) {
858 /* enable GPHY LinkChange Interrrupt */
859 ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
860 if (ret_val)
861 return ret_val;
862 /* make PHY out of power-saving state */
863 ret_val = atl1_phy_leave_power_saving(hw);
864 if (ret_val)
865 return ret_val;
866 /* Call a subroutine to configure the link */
867 ret_val = atl1_setup_link(hw);
868 }
869 return ret_val;
870}
871
872/*
873 * Detects the current speed and duplex settings of the hardware.
874 * hw - Struct containing variables accessed by shared code
875 * speed - Speed of the connection
876 * duplex - Duplex setting of the connection
877 */
878static s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
879{
880 struct pci_dev *pdev = hw->back->pdev;
881 struct atl1_adapter *adapter = hw->back;
882 s32 ret_val;
883 u16 phy_data;
884
885 /* ; --- Read PHY Specific Status Register (17) */
886 ret_val = atl1_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
887 if (ret_val)
888 return ret_val;
889
890 if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
891 return ATLX_ERR_PHY_RES;
892
893 switch (phy_data & MII_ATLX_PSSR_SPEED) {
894 case MII_ATLX_PSSR_1000MBS:
895 *speed = SPEED_1000;
896 break;
897 case MII_ATLX_PSSR_100MBS:
898 *speed = SPEED_100;
899 break;
900 case MII_ATLX_PSSR_10MBS:
901 *speed = SPEED_10;
902 break;
903 default:
904 if (netif_msg_hw(adapter))
905 dev_dbg(&pdev->dev, "error getting speed\n");
906 return ATLX_ERR_PHY_SPEED;
907 break;
908 }
909 if (phy_data & MII_ATLX_PSSR_DPLX)
910 *duplex = FULL_DUPLEX;
911 else
912 *duplex = HALF_DUPLEX;
913
914 return 0;
915}
916
917void atl1_set_mac_addr(struct atl1_hw *hw)
918{
919 u32 value;
920 /*
921 * 00-0B-6A-F6-00-DC
922 * 0: 6AF600DC 1: 000B
923 * low dword
924 */
925 value = (((u32) hw->mac_addr[2]) << 24) |
926 (((u32) hw->mac_addr[3]) << 16) |
927 (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
928 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
929 /* high dword */
930 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
931 iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
932}
933
934/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500935 * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
936 * @adapter: board private structure to initialize
937 *
938 * atl1_sw_init initializes the Adapter private data structure.
939 * Fields are initialized based on PCI device information and
940 * OS network device settings (MTU size).
941 */
942static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
943{
944 struct atl1_hw *hw = &adapter->hw;
945 struct net_device *netdev = adapter->netdev;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500946
Jay Cliburn2a491282008-01-14 19:56:41 -0600947 hw->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jay Cliburna3093d92007-07-19 18:45:14 -0500948 hw->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500949
950 adapter->wol = 0;
951 adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
Jay Cliburn305282b2008-02-02 19:50:04 -0600952 adapter->ict = 50000; /* 100ms */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500953 adapter->link_speed = SPEED_0; /* hardware init */
954 adapter->link_duplex = FULL_DUPLEX;
955
956 hw->phy_configured = false;
957 hw->preamble_len = 7;
958 hw->ipgt = 0x60;
959 hw->min_ifg = 0x50;
960 hw->ipgr1 = 0x40;
961 hw->ipgr2 = 0x60;
962 hw->max_retry = 0xf;
963 hw->lcol = 0x37;
964 hw->jam_ipg = 7;
965 hw->rfd_burst = 8;
966 hw->rrd_burst = 8;
967 hw->rfd_fetch_gap = 1;
968 hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
969 hw->rx_jumbo_lkah = 1;
970 hw->rrd_ret_timer = 16;
971 hw->tpd_burst = 4;
972 hw->tpd_fetch_th = 16;
973 hw->txf_burst = 0x100;
974 hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
975 hw->tpd_fetch_gap = 1;
976 hw->rcb_value = atl1_rcb_64;
977 hw->dma_ord = atl1_dma_ord_enh;
978 hw->dmar_block = atl1_dma_req_256;
979 hw->dmaw_block = atl1_dma_req_256;
980 hw->cmb_rrd = 4;
981 hw->cmb_tpd = 4;
982 hw->cmb_rx_timer = 1; /* about 2us */
983 hw->cmb_tx_timer = 1; /* about 2us */
984 hw->smb_timer = 100000; /* about 200ms */
985
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500986 spin_lock_init(&adapter->lock);
987 spin_lock_init(&adapter->mb_lock);
988
989 return 0;
990}
991
Jay Cliburn05ffdd72007-07-15 11:03:29 -0500992static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
993{
994 struct atl1_adapter *adapter = netdev_priv(netdev);
995 u16 result;
996
997 atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
998
999 return result;
1000}
1001
1002static void mdio_write(struct net_device *netdev, int phy_id, int reg_num,
1003 int val)
1004{
1005 struct atl1_adapter *adapter = netdev_priv(netdev);
1006
1007 atl1_write_phy_reg(&adapter->hw, reg_num, val);
1008}
1009
1010/*
1011 * atl1_mii_ioctl -
1012 * @netdev:
1013 * @ifreq:
1014 * @cmd:
1015 */
1016static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1017{
1018 struct atl1_adapter *adapter = netdev_priv(netdev);
1019 unsigned long flags;
1020 int retval;
1021
1022 if (!netif_running(netdev))
1023 return -EINVAL;
1024
1025 spin_lock_irqsave(&adapter->lock, flags);
1026 retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1027 spin_unlock_irqrestore(&adapter->lock, flags);
1028
1029 return retval;
1030}
1031
1032/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001033 * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
1034 * @adapter: board private structure
1035 *
1036 * Return 0 on success, negative on failure
1037 */
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06001038static s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001039{
1040 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1041 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1042 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1043 struct atl1_ring_header *ring_header = &adapter->ring_header;
1044 struct pci_dev *pdev = adapter->pdev;
1045 int size;
1046 u8 offset = 0;
1047
1048 size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
1049 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
1050 if (unlikely(!tpd_ring->buffer_info)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06001051 if (netif_msg_drv(adapter))
1052 dev_err(&pdev->dev, "kzalloc failed , size = D%d\n",
1053 size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001054 goto err_nomem;
1055 }
1056 rfd_ring->buffer_info =
Jay Cliburn53ffb422007-07-15 11:03:27 -05001057 (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001058
Jay Cliburn305282b2008-02-02 19:50:04 -06001059 /*
1060 * real ring DMA buffer
Jay Cliburn53ffb422007-07-15 11:03:27 -05001061 * each ring/block may need up to 8 bytes for alignment, hence the
1062 * additional 40 bytes tacked onto the end.
1063 */
1064 ring_header->size = size =
1065 sizeof(struct tx_packet_desc) * tpd_ring->count
1066 + sizeof(struct rx_free_desc) * rfd_ring->count
1067 + sizeof(struct rx_return_desc) * rrd_ring->count
1068 + sizeof(struct coals_msg_block)
1069 + sizeof(struct stats_msg_block)
1070 + 40;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001071
1072 ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
Jay Cliburn53ffb422007-07-15 11:03:27 -05001073 &ring_header->dma);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001074 if (unlikely(!ring_header->desc)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06001075 if (netif_msg_drv(adapter))
1076 dev_err(&pdev->dev, "pci_alloc_consistent failed\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001077 goto err_nomem;
1078 }
1079
1080 memset(ring_header->desc, 0, ring_header->size);
1081
1082 /* init TPD ring */
1083 tpd_ring->dma = ring_header->dma;
1084 offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
1085 tpd_ring->dma += offset;
1086 tpd_ring->desc = (u8 *) ring_header->desc + offset;
1087 tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001088
1089 /* init RFD ring */
1090 rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
1091 offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
1092 rfd_ring->dma += offset;
1093 rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
1094 rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001095
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001096
1097 /* init RRD ring */
1098 rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
1099 offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
1100 rrd_ring->dma += offset;
1101 rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
1102 rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001103
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001104
1105 /* init CMB */
1106 adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
1107 offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
1108 adapter->cmb.dma += offset;
Jay Cliburn53ffb422007-07-15 11:03:27 -05001109 adapter->cmb.cmb = (struct coals_msg_block *)
1110 ((u8 *) rrd_ring->desc + (rrd_ring->size + offset));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001111
1112 /* init SMB */
1113 adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
1114 offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
1115 adapter->smb.dma += offset;
1116 adapter->smb.smb = (struct stats_msg_block *)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001117 ((u8 *) adapter->cmb.cmb +
1118 (sizeof(struct coals_msg_block) + offset));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001119
Jay Cliburn305282b2008-02-02 19:50:04 -06001120 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001121
1122err_nomem:
1123 kfree(tpd_ring->buffer_info);
1124 return -ENOMEM;
1125}
1126
Chris Snook3d2557f2007-07-23 16:38:39 -04001127static void atl1_init_ring_ptrs(struct atl1_adapter *adapter)
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001128{
1129 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1130 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1131 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1132
1133 atomic_set(&tpd_ring->next_to_use, 0);
1134 atomic_set(&tpd_ring->next_to_clean, 0);
1135
1136 rfd_ring->next_to_clean = 0;
1137 atomic_set(&rfd_ring->next_to_use, 0);
1138
1139 rrd_ring->next_to_use = 0;
1140 atomic_set(&rrd_ring->next_to_clean, 0);
1141}
1142
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001143/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001144 * atl1_clean_rx_ring - Free RFD Buffers
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001145 * @adapter: board private structure
1146 */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001147static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001148{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001149 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1150 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1151 struct atl1_buffer *buffer_info;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001152 struct pci_dev *pdev = adapter->pdev;
1153 unsigned long size;
1154 unsigned int i;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001155
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001156 /* Free all the Rx ring sk_buffs */
1157 for (i = 0; i < rfd_ring->count; i++) {
1158 buffer_info = &rfd_ring->buffer_info[i];
1159 if (buffer_info->dma) {
1160 pci_unmap_page(pdev, buffer_info->dma,
1161 buffer_info->length, PCI_DMA_FROMDEVICE);
1162 buffer_info->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001163 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001164 if (buffer_info->skb) {
1165 dev_kfree_skb(buffer_info->skb);
1166 buffer_info->skb = NULL;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001167 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001168 }
1169
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001170 size = sizeof(struct atl1_buffer) * rfd_ring->count;
1171 memset(rfd_ring->buffer_info, 0, size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001172
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001173 /* Zero out the descriptor ring */
1174 memset(rfd_ring->desc, 0, rfd_ring->size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001175
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001176 rfd_ring->next_to_clean = 0;
1177 atomic_set(&rfd_ring->next_to_use, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001178
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001179 rrd_ring->next_to_use = 0;
1180 atomic_set(&rrd_ring->next_to_clean, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001181}
1182
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001183/*
1184 * atl1_clean_tx_ring - Free Tx Buffers
1185 * @adapter: board private structure
1186 */
1187static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001188{
1189 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1190 struct atl1_buffer *buffer_info;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001191 struct pci_dev *pdev = adapter->pdev;
1192 unsigned long size;
1193 unsigned int i;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001194
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001195 /* Free all the Tx ring sk_buffs */
1196 for (i = 0; i < tpd_ring->count; i++) {
1197 buffer_info = &tpd_ring->buffer_info[i];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001198 if (buffer_info->dma) {
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001199 pci_unmap_page(pdev, buffer_info->dma,
1200 buffer_info->length, PCI_DMA_TODEVICE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001201 buffer_info->dma = 0;
1202 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001203 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001204
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001205 for (i = 0; i < tpd_ring->count; i++) {
1206 buffer_info = &tpd_ring->buffer_info[i];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001207 if (buffer_info->skb) {
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001208 dev_kfree_skb_any(buffer_info->skb);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001209 buffer_info->skb = NULL;
1210 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001211 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001212
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001213 size = sizeof(struct atl1_buffer) * tpd_ring->count;
1214 memset(tpd_ring->buffer_info, 0, size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001215
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001216 /* Zero out the descriptor ring */
1217 memset(tpd_ring->desc, 0, tpd_ring->size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001218
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001219 atomic_set(&tpd_ring->next_to_use, 0);
1220 atomic_set(&tpd_ring->next_to_clean, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001221}
1222
1223/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001224 * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1225 * @adapter: board private structure
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001226 *
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001227 * Free all transmit software resources
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001228 */
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06001229static void atl1_free_ring_resources(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001230{
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001231 struct pci_dev *pdev = adapter->pdev;
1232 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1233 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1234 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1235 struct atl1_ring_header *ring_header = &adapter->ring_header;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001236
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001237 atl1_clean_tx_ring(adapter);
1238 atl1_clean_rx_ring(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001239
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001240 kfree(tpd_ring->buffer_info);
1241 pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1242 ring_header->dma);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001243
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001244 tpd_ring->buffer_info = NULL;
1245 tpd_ring->desc = NULL;
1246 tpd_ring->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001247
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001248 rfd_ring->buffer_info = NULL;
1249 rfd_ring->desc = NULL;
1250 rfd_ring->dma = 0;
1251
1252 rrd_ring->desc = NULL;
1253 rrd_ring->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001254}
1255
1256static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
1257{
1258 u32 value;
1259 struct atl1_hw *hw = &adapter->hw;
1260 struct net_device *netdev = adapter->netdev;
1261 /* Config MAC CTRL Register */
1262 value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
1263 /* duplex */
1264 if (FULL_DUPLEX == adapter->link_duplex)
1265 value |= MAC_CTRL_DUPLX;
1266 /* speed */
1267 value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
1268 MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
1269 MAC_CTRL_SPEED_SHIFT);
1270 /* flow control */
1271 value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1272 /* PAD & CRC */
1273 value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1274 /* preamble length */
1275 value |= (((u32) adapter->hw.preamble_len
1276 & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1277 /* vlan */
1278 if (adapter->vlgrp)
1279 value |= MAC_CTRL_RMV_VLAN;
1280 /* rx checksum
1281 if (adapter->rx_csum)
1282 value |= MAC_CTRL_RX_CHKSUM_EN;
1283 */
1284 /* filter mode */
1285 value |= MAC_CTRL_BC_EN;
1286 if (netdev->flags & IFF_PROMISC)
1287 value |= MAC_CTRL_PROMIS_EN;
1288 else if (netdev->flags & IFF_ALLMULTI)
1289 value |= MAC_CTRL_MC_ALL_EN;
1290 /* value |= MAC_CTRL_LOOPBACK; */
1291 iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
1292}
1293
1294static u32 atl1_check_link(struct atl1_adapter *adapter)
1295{
1296 struct atl1_hw *hw = &adapter->hw;
1297 struct net_device *netdev = adapter->netdev;
1298 u32 ret_val;
1299 u16 speed, duplex, phy_data;
1300 int reconfig = 0;
1301
1302 /* MII_BMSR must read twice */
1303 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
1304 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
Jay Cliburn305282b2008-02-02 19:50:04 -06001305 if (!(phy_data & BMSR_LSTATUS)) {
1306 /* link down */
1307 if (netif_carrier_ok(netdev)) {
1308 /* old link state: Up */
Jay Cliburn460578b2008-02-02 19:50:09 -06001309 if (netif_msg_link(adapter))
1310 dev_info(&adapter->pdev->dev, "link is down\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001311 adapter->link_speed = SPEED_0;
1312 netif_carrier_off(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001313 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001314 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001315 }
1316
1317 /* Link Up */
1318 ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
1319 if (ret_val)
1320 return ret_val;
1321
1322 switch (hw->media_type) {
1323 case MEDIA_TYPE_1000M_FULL:
1324 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
1325 reconfig = 1;
1326 break;
1327 case MEDIA_TYPE_100M_FULL:
1328 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
1329 reconfig = 1;
1330 break;
1331 case MEDIA_TYPE_100M_HALF:
1332 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
1333 reconfig = 1;
1334 break;
1335 case MEDIA_TYPE_10M_FULL:
1336 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
1337 reconfig = 1;
1338 break;
1339 case MEDIA_TYPE_10M_HALF:
1340 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
1341 reconfig = 1;
1342 break;
1343 }
1344
1345 /* link result is our setting */
1346 if (!reconfig) {
Joe Perches8e95a202009-12-03 07:58:21 +00001347 if (adapter->link_speed != speed ||
1348 adapter->link_duplex != duplex) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001349 adapter->link_speed = speed;
1350 adapter->link_duplex = duplex;
1351 atl1_setup_mac_ctrl(adapter);
Jay Cliburn460578b2008-02-02 19:50:09 -06001352 if (netif_msg_link(adapter))
1353 dev_info(&adapter->pdev->dev,
1354 "%s link is up %d Mbps %s\n",
1355 netdev->name, adapter->link_speed,
1356 adapter->link_duplex == FULL_DUPLEX ?
1357 "full duplex" : "half duplex");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001358 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001359 if (!netif_carrier_ok(netdev)) {
1360 /* Link down -> Up */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001361 netif_carrier_on(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001362 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001363 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001364 }
1365
Jay Cliburn305282b2008-02-02 19:50:04 -06001366 /* change original link status */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001367 if (netif_carrier_ok(netdev)) {
1368 adapter->link_speed = SPEED_0;
1369 netif_carrier_off(netdev);
1370 netif_stop_queue(netdev);
1371 }
1372
1373 if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
1374 hw->media_type != MEDIA_TYPE_1000M_FULL) {
1375 switch (hw->media_type) {
1376 case MEDIA_TYPE_100M_FULL:
1377 phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
1378 MII_CR_RESET;
1379 break;
1380 case MEDIA_TYPE_100M_HALF:
1381 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
1382 break;
1383 case MEDIA_TYPE_10M_FULL:
1384 phy_data =
1385 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
1386 break;
Jay Cliburn305282b2008-02-02 19:50:04 -06001387 default:
1388 /* MEDIA_TYPE_10M_HALF: */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001389 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
1390 break;
1391 }
1392 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
Jay Cliburn305282b2008-02-02 19:50:04 -06001393 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001394 }
1395
1396 /* auto-neg, insert timer to re-config phy */
1397 if (!adapter->phy_timer_pending) {
1398 adapter->phy_timer_pending = true;
Stephen Hemmingere053b622008-10-31 16:52:04 -07001399 mod_timer(&adapter->phy_config_timer,
1400 round_jiffies(jiffies + 3 * HZ));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001401 }
1402
Jay Cliburn305282b2008-02-02 19:50:04 -06001403 return 0;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001404}
1405
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001406static void set_flow_ctrl_old(struct atl1_adapter *adapter)
1407{
1408 u32 hi, lo, value;
1409
1410 /* RFD Flow Control */
1411 value = adapter->rfd_ring.count;
1412 hi = value / 16;
1413 if (hi < 2)
1414 hi = 2;
1415 lo = value * 7 / 8;
1416
1417 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001418 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001419 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1420
1421 /* RRD Flow Control */
1422 value = adapter->rrd_ring.count;
1423 lo = value / 16;
1424 hi = value * 7 / 8;
1425 if (lo < 2)
1426 lo = 2;
1427 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001428 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001429 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1430}
1431
1432static void set_flow_ctrl_new(struct atl1_hw *hw)
1433{
1434 u32 hi, lo, value;
1435
1436 /* RXF Flow Control */
1437 value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1438 lo = value / 16;
1439 if (lo < 192)
1440 lo = 192;
1441 hi = value * 7 / 8;
1442 if (hi < lo)
1443 hi = lo + 16;
1444 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001445 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001446 iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1447
1448 /* RRD Flow Control */
1449 value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1450 lo = value / 8;
1451 hi = value * 7 / 8;
1452 if (lo < 2)
1453 lo = 2;
1454 if (hi < lo)
1455 hi = lo + 3;
1456 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001457 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001458 iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1459}
1460
1461/*
1462 * atl1_configure - Configure Transmit&Receive Unit after Reset
1463 * @adapter: board private structure
1464 *
1465 * Configure the Tx /Rx unit of the MAC after a reset.
1466 */
1467static u32 atl1_configure(struct atl1_adapter *adapter)
1468{
1469 struct atl1_hw *hw = &adapter->hw;
1470 u32 value;
1471
1472 /* clear interrupt status */
1473 iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1474
1475 /* set MAC Address */
1476 value = (((u32) hw->mac_addr[2]) << 24) |
1477 (((u32) hw->mac_addr[3]) << 16) |
1478 (((u32) hw->mac_addr[4]) << 8) |
1479 (((u32) hw->mac_addr[5]));
1480 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1481 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1482 iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1483
1484 /* tx / rx ring */
1485
1486 /* HI base address */
1487 iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1488 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1489 /* LO base address */
1490 iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1491 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1492 iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1493 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1494 iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1495 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1496 iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1497 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1498 iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1499 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1500
1501 /* element count */
1502 value = adapter->rrd_ring.count;
1503 value <<= 16;
1504 value += adapter->rfd_ring.count;
1505 iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001506 iowrite32(adapter->tpd_ring.count, hw->hw_addr +
1507 REG_DESC_TPD_RING_SIZE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001508
1509 /* Load Ptr */
1510 iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1511
1512 /* config Mailbox */
1513 value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1514 & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001515 ((atomic_read(&adapter->rrd_ring.next_to_clean)
1516 & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1517 ((atomic_read(&adapter->rfd_ring.next_to_use)
1518 & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001519 iowrite32(value, hw->hw_addr + REG_MAILBOX);
1520
1521 /* config IPG/IFG */
1522 value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1523 << MAC_IPG_IFG_IPGT_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001524 (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1525 << MAC_IPG_IFG_MIFG_SHIFT) |
1526 (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1527 << MAC_IPG_IFG_IPGR1_SHIFT) |
1528 (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1529 << MAC_IPG_IFG_IPGR2_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001530 iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1531
1532 /* config Half-Duplex Control */
1533 value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001534 (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1535 << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1536 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1537 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1538 (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1539 << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001540 iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1541
1542 /* set Interrupt Moderator Timer */
1543 iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1544 iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1545
1546 /* set Interrupt Clear Timer */
1547 iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1548
Jay Cliburn2a491282008-01-14 19:56:41 -06001549 /* set max frame size hw will accept */
1550 iowrite32(hw->max_frame_size, hw->hw_addr + REG_MTU);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001551
1552 /* jumbo size & rrd retirement timer */
1553 value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1554 << RXQ_JMBOSZ_TH_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001555 (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1556 << RXQ_JMBO_LKAH_SHIFT) |
1557 (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1558 << RXQ_RRD_TIMER_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001559 iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1560
1561 /* Flow Control */
1562 switch (hw->dev_rev) {
1563 case 0x8001:
1564 case 0x9001:
1565 case 0x9002:
1566 case 0x9003:
1567 set_flow_ctrl_old(adapter);
1568 break;
1569 default:
1570 set_flow_ctrl_new(hw);
1571 break;
1572 }
1573
1574 /* config TXQ */
1575 value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1576 << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001577 (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1578 << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1579 (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1580 << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE |
1581 TXQ_CTRL_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001582 iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1583
1584 /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1585 value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001586 << TX_JUMBO_TASK_TH_SHIFT) |
1587 (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1588 << TX_TPD_MIN_IPG_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001589 iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1590
1591 /* config RXQ */
1592 value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001593 << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1594 (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1595 << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1596 (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1597 << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) | RXQ_CTRL_CUT_THRU_EN |
1598 RXQ_CTRL_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001599 iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1600
1601 /* config DMA Engine */
1602 value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001603 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
Jay Cliburn3f516c02007-07-19 18:45:11 -05001604 ((((u32) hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
1605 << DMA_CTRL_DMAW_BURST_LEN_SHIFT) | DMA_CTRL_DMAR_EN |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001606 DMA_CTRL_DMAW_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001607 value |= (u32) hw->dma_ord;
1608 if (atl1_rcb_128 == hw->rcb_value)
1609 value |= DMA_CTRL_RCB_VALUE;
1610 iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1611
1612 /* config CMB / SMB */
Jay Cliburn91a500a2007-07-19 18:45:12 -05001613 value = (hw->cmb_tpd > adapter->tpd_ring.count) ?
1614 hw->cmb_tpd : adapter->tpd_ring.count;
1615 value <<= 16;
1616 value |= hw->cmb_rrd;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001617 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1618 value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1619 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1620 iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1621
1622 /* --- enable CMB / SMB */
1623 value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1624 iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1625
1626 value = ioread32(adapter->hw.hw_addr + REG_ISR);
1627 if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1628 value = 1; /* config failed */
1629 else
1630 value = 0;
1631
1632 /* clear all interrupt status */
1633 iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1634 iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1635 return value;
1636}
1637
1638/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001639 * atl1_pcie_patch - Patch for PCIE module
1640 */
1641static void atl1_pcie_patch(struct atl1_adapter *adapter)
1642{
1643 u32 value;
1644
1645 /* much vendor magic here */
1646 value = 0x6500;
1647 iowrite32(value, adapter->hw.hw_addr + 0x12FC);
1648 /* pcie flow control mode change */
1649 value = ioread32(adapter->hw.hw_addr + 0x1008);
1650 value |= 0x8000;
1651 iowrite32(value, adapter->hw.hw_addr + 0x1008);
1652}
1653
1654/*
1655 * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
1656 * on PCI Command register is disable.
1657 * The function enable this bit.
1658 * Brackett, 2006/03/15
1659 */
1660static void atl1_via_workaround(struct atl1_adapter *adapter)
1661{
1662 unsigned long value;
1663
1664 value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
1665 if (value & PCI_COMMAND_INTX_DISABLE)
1666 value &= ~PCI_COMMAND_INTX_DISABLE;
1667 iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
1668}
1669
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001670static void atl1_inc_smb(struct atl1_adapter *adapter)
1671{
Stephen Hemminger02e71732008-10-31 16:52:03 -07001672 struct net_device *netdev = adapter->netdev;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001673 struct stats_msg_block *smb = adapter->smb.smb;
1674
1675 /* Fill out the OS statistics structure */
1676 adapter->soft_stats.rx_packets += smb->rx_ok;
1677 adapter->soft_stats.tx_packets += smb->tx_ok;
1678 adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
1679 adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
1680 adapter->soft_stats.multicast += smb->rx_mcast;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001681 adapter->soft_stats.collisions += (smb->tx_1_col + smb->tx_2_col * 2 +
1682 smb->tx_late_col + smb->tx_abort_col * adapter->hw.max_retry);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001683
1684 /* Rx Errors */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001685 adapter->soft_stats.rx_errors += (smb->rx_frag + smb->rx_fcs_err +
1686 smb->rx_len_err + smb->rx_sz_ov + smb->rx_rxf_ov +
1687 smb->rx_rrd_ov + smb->rx_align_err);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001688 adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
1689 adapter->soft_stats.rx_length_errors += smb->rx_len_err;
1690 adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
1691 adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
1692 adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001693 smb->rx_rxf_ov);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001694
1695 adapter->soft_stats.rx_pause += smb->rx_pause;
1696 adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
1697 adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
1698
1699 /* Tx Errors */
1700 adapter->soft_stats.tx_errors += (smb->tx_late_col +
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001701 smb->tx_abort_col + smb->tx_underrun + smb->tx_trunc);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001702 adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
1703 adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
1704 adapter->soft_stats.tx_window_errors += smb->tx_late_col;
1705
1706 adapter->soft_stats.excecol += smb->tx_abort_col;
1707 adapter->soft_stats.deffer += smb->tx_defer;
1708 adapter->soft_stats.scc += smb->tx_1_col;
1709 adapter->soft_stats.mcc += smb->tx_2_col;
1710 adapter->soft_stats.latecol += smb->tx_late_col;
1711 adapter->soft_stats.tx_underun += smb->tx_underrun;
1712 adapter->soft_stats.tx_trunc += smb->tx_trunc;
1713 adapter->soft_stats.tx_pause += smb->tx_pause;
1714
Stephen Hemminger02e71732008-10-31 16:52:03 -07001715 netdev->stats.rx_packets = adapter->soft_stats.rx_packets;
1716 netdev->stats.tx_packets = adapter->soft_stats.tx_packets;
1717 netdev->stats.rx_bytes = adapter->soft_stats.rx_bytes;
1718 netdev->stats.tx_bytes = adapter->soft_stats.tx_bytes;
1719 netdev->stats.multicast = adapter->soft_stats.multicast;
1720 netdev->stats.collisions = adapter->soft_stats.collisions;
1721 netdev->stats.rx_errors = adapter->soft_stats.rx_errors;
1722 netdev->stats.rx_over_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001723 adapter->soft_stats.rx_missed_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001724 netdev->stats.rx_length_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001725 adapter->soft_stats.rx_length_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001726 netdev->stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
1727 netdev->stats.rx_frame_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001728 adapter->soft_stats.rx_frame_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001729 netdev->stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
1730 netdev->stats.rx_missed_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001731 adapter->soft_stats.rx_missed_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001732 netdev->stats.tx_errors = adapter->soft_stats.tx_errors;
1733 netdev->stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
1734 netdev->stats.tx_aborted_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001735 adapter->soft_stats.tx_aborted_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001736 netdev->stats.tx_window_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001737 adapter->soft_stats.tx_window_errors;
Stephen Hemminger02e71732008-10-31 16:52:03 -07001738 netdev->stats.tx_carrier_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001739 adapter->soft_stats.tx_carrier_errors;
1740}
1741
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001742static void atl1_update_mailbox(struct atl1_adapter *adapter)
1743{
1744 unsigned long flags;
1745 u32 tpd_next_to_use;
1746 u32 rfd_next_to_use;
1747 u32 rrd_next_to_clean;
1748 u32 value;
1749
1750 spin_lock_irqsave(&adapter->mb_lock, flags);
1751
1752 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1753 rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1754 rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1755
1756 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1757 MB_RFD_PROD_INDX_SHIFT) |
1758 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1759 MB_RRD_CONS_INDX_SHIFT) |
1760 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1761 MB_TPD_PROD_INDX_SHIFT);
1762 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1763
1764 spin_unlock_irqrestore(&adapter->mb_lock, flags);
1765}
1766
1767static void atl1_clean_alloc_flag(struct atl1_adapter *adapter,
1768 struct rx_return_desc *rrd, u16 offset)
1769{
1770 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1771
1772 while (rfd_ring->next_to_clean != (rrd->buf_indx + offset)) {
1773 rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced = 0;
1774 if (++rfd_ring->next_to_clean == rfd_ring->count) {
1775 rfd_ring->next_to_clean = 0;
1776 }
1777 }
1778}
1779
1780static void atl1_update_rfd_index(struct atl1_adapter *adapter,
1781 struct rx_return_desc *rrd)
1782{
1783 u16 num_buf;
1784
1785 num_buf = (rrd->xsz.xsum_sz.pkt_size + adapter->rx_buffer_len - 1) /
1786 adapter->rx_buffer_len;
1787 if (rrd->num_buf == num_buf)
1788 /* clean alloc flag for bad rrd */
1789 atl1_clean_alloc_flag(adapter, rrd, num_buf);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001790}
1791
1792static void atl1_rx_checksum(struct atl1_adapter *adapter,
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001793 struct rx_return_desc *rrd, struct sk_buff *skb)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001794{
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001795 struct pci_dev *pdev = adapter->pdev;
1796
Jay Cliburnc2ac3ef2008-08-04 19:05:10 -05001797 /*
1798 * The L1 hardware contains a bug that erroneously sets the
1799 * PACKET_FLAG_ERR and ERR_FLAG_L4_CHKSUM bits whenever a
1800 * fragmented IP packet is received, even though the packet
1801 * is perfectly valid and its checksum is correct. There's
1802 * no way to distinguish between one of these good packets
1803 * and a packet that actually contains a TCP/UDP checksum
1804 * error, so all we can do is allow it to be handed up to
1805 * the higher layers and let it be sorted out there.
1806 */
1807
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001808 skb->ip_summed = CHECKSUM_NONE;
1809
1810 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1811 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
1812 ERR_FLAG_CODE | ERR_FLAG_OV)) {
1813 adapter->hw_csum_err++;
Jay Cliburn460578b2008-02-02 19:50:09 -06001814 if (netif_msg_rx_err(adapter))
1815 dev_printk(KERN_DEBUG, &pdev->dev,
1816 "rx checksum error\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001817 return;
1818 }
1819 }
1820
1821 /* not IPv4 */
1822 if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
1823 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
1824 return;
1825
1826 /* IPv4 packet */
1827 if (likely(!(rrd->err_flg &
1828 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
1829 skb->ip_summed = CHECKSUM_UNNECESSARY;
1830 adapter->hw_csum_good++;
1831 return;
1832 }
1833
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001834 return;
1835}
1836
1837/*
1838 * atl1_alloc_rx_buffers - Replace used receive buffers
1839 * @adapter: address of board private structure
1840 */
1841static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
1842{
1843 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1844 struct pci_dev *pdev = adapter->pdev;
1845 struct page *page;
1846 unsigned long offset;
1847 struct atl1_buffer *buffer_info, *next_info;
1848 struct sk_buff *skb;
1849 u16 num_alloc = 0;
1850 u16 rfd_next_to_use, next_next;
1851 struct rx_free_desc *rfd_desc;
1852
1853 next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
1854 if (++next_next == rfd_ring->count)
1855 next_next = 0;
1856 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1857 next_info = &rfd_ring->buffer_info[next_next];
1858
1859 while (!buffer_info->alloced && !next_info->alloced) {
1860 if (buffer_info->skb) {
1861 buffer_info->alloced = 1;
1862 goto next;
1863 }
1864
1865 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
1866
Eric Dumazet89d71a62009-10-13 05:34:20 +00001867 skb = netdev_alloc_skb_ip_align(adapter->netdev,
1868 adapter->rx_buffer_len);
Jay Cliburn305282b2008-02-02 19:50:04 -06001869 if (unlikely(!skb)) {
1870 /* Better luck next round */
Stephen Hemminger02e71732008-10-31 16:52:03 -07001871 adapter->netdev->stats.rx_dropped++;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001872 break;
1873 }
1874
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001875 buffer_info->alloced = 1;
1876 buffer_info->skb = skb;
1877 buffer_info->length = (u16) adapter->rx_buffer_len;
1878 page = virt_to_page(skb->data);
1879 offset = (unsigned long)skb->data & ~PAGE_MASK;
1880 buffer_info->dma = pci_map_page(pdev, page, offset,
1881 adapter->rx_buffer_len,
1882 PCI_DMA_FROMDEVICE);
1883 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1884 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
1885 rfd_desc->coalese = 0;
1886
1887next:
1888 rfd_next_to_use = next_next;
1889 if (unlikely(++next_next == rfd_ring->count))
1890 next_next = 0;
1891
1892 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1893 next_info = &rfd_ring->buffer_info[next_next];
1894 num_alloc++;
1895 }
1896
1897 if (num_alloc) {
1898 /*
1899 * Force memory writes to complete before letting h/w
1900 * know there are new descriptors to fetch. (Only
1901 * applicable for weak-ordered memory model archs,
1902 * such as IA-64).
1903 */
1904 wmb();
1905 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
1906 }
1907 return num_alloc;
1908}
1909
1910static void atl1_intr_rx(struct atl1_adapter *adapter)
1911{
1912 int i, count;
1913 u16 length;
1914 u16 rrd_next_to_clean;
1915 u32 value;
1916 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1917 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1918 struct atl1_buffer *buffer_info;
1919 struct rx_return_desc *rrd;
1920 struct sk_buff *skb;
1921
1922 count = 0;
1923
1924 rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
1925
1926 while (1) {
1927 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
1928 i = 1;
1929 if (likely(rrd->xsz.valid)) { /* packet valid */
1930chk_rrd:
1931 /* check rrd status */
1932 if (likely(rrd->num_buf == 1))
1933 goto rrd_ok;
Jay Cliburn235ffa12008-02-02 19:50:10 -06001934 else if (netif_msg_rx_err(adapter)) {
1935 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1936 "unexpected RRD buffer count\n");
1937 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1938 "rx_buf_len = %d\n",
1939 adapter->rx_buffer_len);
1940 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1941 "RRD num_buf = %d\n",
1942 rrd->num_buf);
1943 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1944 "RRD pkt_len = %d\n",
1945 rrd->xsz.xsum_sz.pkt_size);
1946 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1947 "RRD pkt_flg = 0x%08X\n",
1948 rrd->pkt_flg);
1949 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1950 "RRD err_flg = 0x%08X\n",
1951 rrd->err_flg);
1952 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1953 "RRD vlan_tag = 0x%08X\n",
1954 rrd->vlan_tag);
1955 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001956
1957 /* rrd seems to be bad */
1958 if (unlikely(i-- > 0)) {
1959 /* rrd may not be DMAed completely */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001960 udelay(1);
1961 goto chk_rrd;
1962 }
1963 /* bad rrd */
Jay Cliburn460578b2008-02-02 19:50:09 -06001964 if (netif_msg_rx_err(adapter))
1965 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1966 "bad RRD\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001967 /* see if update RFD index */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001968 if (rrd->num_buf > 1)
1969 atl1_update_rfd_index(adapter, rrd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001970
1971 /* update rrd */
1972 rrd->xsz.valid = 0;
1973 if (++rrd_next_to_clean == rrd_ring->count)
1974 rrd_next_to_clean = 0;
1975 count++;
1976 continue;
1977 } else { /* current rrd still not be updated */
1978
1979 break;
1980 }
1981rrd_ok:
1982 /* clean alloc flag for bad rrd */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001983 atl1_clean_alloc_flag(adapter, rrd, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001984
1985 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
1986 if (++rfd_ring->next_to_clean == rfd_ring->count)
1987 rfd_ring->next_to_clean = 0;
1988
1989 /* update rrd next to clean */
1990 if (++rrd_next_to_clean == rrd_ring->count)
1991 rrd_next_to_clean = 0;
1992 count++;
1993
1994 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1995 if (!(rrd->err_flg &
1996 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
1997 | ERR_FLAG_LEN))) {
1998 /* packet error, don't need upstream */
1999 buffer_info->alloced = 0;
2000 rrd->xsz.valid = 0;
2001 continue;
2002 }
2003 }
2004
2005 /* Good Receive */
2006 pci_unmap_page(adapter->pdev, buffer_info->dma,
2007 buffer_info->length, PCI_DMA_FROMDEVICE);
Alexey Dobriyanaefdbf12008-05-23 02:00:25 +04002008 buffer_info->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002009 skb = buffer_info->skb;
2010 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
2011
Jay Cliburna3093d92007-07-19 18:45:14 -05002012 skb_put(skb, length - ETH_FCS_LEN);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002013
2014 /* Receive Checksum Offload */
2015 atl1_rx_checksum(adapter, rrd, skb);
2016 skb->protocol = eth_type_trans(skb, adapter->netdev);
2017
2018 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
2019 u16 vlan_tag = (rrd->vlan_tag >> 4) |
2020 ((rrd->vlan_tag & 7) << 13) |
2021 ((rrd->vlan_tag & 8) << 9);
2022 vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
2023 } else
2024 netif_rx(skb);
2025
2026 /* let protocol layer free skb */
2027 buffer_info->skb = NULL;
2028 buffer_info->alloced = 0;
2029 rrd->xsz.valid = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002030 }
2031
2032 atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
2033
2034 atl1_alloc_rx_buffers(adapter);
2035
2036 /* update mailbox ? */
2037 if (count) {
2038 u32 tpd_next_to_use;
2039 u32 rfd_next_to_use;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002040
2041 spin_lock(&adapter->mb_lock);
2042
2043 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
2044 rfd_next_to_use =
2045 atomic_read(&adapter->rfd_ring.next_to_use);
2046 rrd_next_to_clean =
2047 atomic_read(&adapter->rrd_ring.next_to_clean);
2048 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
2049 MB_RFD_PROD_INDX_SHIFT) |
2050 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
2051 MB_RRD_CONS_INDX_SHIFT) |
2052 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
2053 MB_TPD_PROD_INDX_SHIFT);
2054 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
2055 spin_unlock(&adapter->mb_lock);
2056 }
2057}
2058
2059static void atl1_intr_tx(struct atl1_adapter *adapter)
2060{
2061 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2062 struct atl1_buffer *buffer_info;
2063 u16 sw_tpd_next_to_clean;
2064 u16 cmb_tpd_next_to_clean;
2065
2066 sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2067 cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
2068
2069 while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
2070 struct tx_packet_desc *tpd;
2071
2072 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
2073 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
2074 if (buffer_info->dma) {
2075 pci_unmap_page(adapter->pdev, buffer_info->dma,
2076 buffer_info->length, PCI_DMA_TODEVICE);
2077 buffer_info->dma = 0;
2078 }
2079
2080 if (buffer_info->skb) {
2081 dev_kfree_skb_irq(buffer_info->skb);
2082 buffer_info->skb = NULL;
2083 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002084
2085 if (++sw_tpd_next_to_clean == tpd_ring->count)
2086 sw_tpd_next_to_clean = 0;
2087 }
2088 atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
2089
Joe Perches8e95a202009-12-03 07:58:21 +00002090 if (netif_queue_stopped(adapter->netdev) &&
2091 netif_carrier_ok(adapter->netdev))
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002092 netif_wake_queue(adapter->netdev);
2093}
2094
Jay Cliburne6a7ff42007-07-19 18:45:10 -05002095static u16 atl1_tpd_avail(struct atl1_tpd_ring *tpd_ring)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002096{
2097 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2098 u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
Jay Cliburn53ffb422007-07-15 11:03:27 -05002099 return ((next_to_clean > next_to_use) ?
2100 next_to_clean - next_to_use - 1 :
2101 tpd_ring->count + next_to_clean - next_to_use - 1);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002102}
2103
2104static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002105 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002106{
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002107 u8 hdr_len, ip_off;
2108 u32 real_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002109 int err;
2110
2111 if (skb_shinfo(skb)->gso_size) {
2112 if (skb_header_cloned(skb)) {
2113 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2114 if (unlikely(err))
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002115 return -1;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002116 }
2117
Al Virod63ddce2008-05-21 01:34:30 +01002118 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002119 struct iphdr *iph = ip_hdr(skb);
2120
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002121 real_len = (((unsigned char *)iph - skb->data) +
2122 ntohs(iph->tot_len));
2123 if (real_len < skb->len)
2124 pskb_trim(skb, real_len);
2125 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2126 if (skb->len == hdr_len) {
2127 iph->check = 0;
2128 tcp_hdr(skb)->check =
2129 ~csum_tcpudp_magic(iph->saddr,
2130 iph->daddr, tcp_hdrlen(skb),
2131 IPPROTO_TCP, 0);
2132 ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2133 TPD_IPHL_SHIFT;
2134 ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2135 TPD_TCPHDRLEN_MASK) <<
2136 TPD_TCPHDRLEN_SHIFT;
2137 ptpd->word3 |= 1 << TPD_IP_CSUM_SHIFT;
2138 ptpd->word3 |= 1 << TPD_TCP_CSUM_SHIFT;
2139 return 1;
2140 }
2141
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002142 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002143 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002144 iph->daddr, 0, IPPROTO_TCP, 0);
2145 ip_off = (unsigned char *)iph -
2146 (unsigned char *) skb_network_header(skb);
2147 if (ip_off == 8) /* 802.3-SNAP frame */
2148 ptpd->word3 |= 1 << TPD_ETHTYPE_SHIFT;
2149 else if (ip_off != 0)
2150 return -2;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002151
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002152 ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2153 TPD_IPHL_SHIFT;
2154 ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2155 TPD_TCPHDRLEN_MASK) << TPD_TCPHDRLEN_SHIFT;
2156 ptpd->word3 |= (skb_shinfo(skb)->gso_size &
2157 TPD_MSS_MASK) << TPD_MSS_SHIFT;
2158 ptpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
2159 return 3;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002160 }
2161 }
2162 return false;
2163}
2164
2165static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002166 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002167{
2168 u8 css, cso;
2169
2170 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002171 css = (u8) (skb->csum_start - skb_headroom(skb));
2172 cso = css + (u8) skb->csum_offset;
2173 if (unlikely(css & 0x1)) {
2174 /* L1 hardware requires an even number here */
Jay Cliburn460578b2008-02-02 19:50:09 -06002175 if (netif_msg_tx_err(adapter))
2176 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2177 "payload offset not an even number\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002178 return -1;
2179 }
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002180 ptpd->word3 |= (css & TPD_PLOADOFFSET_MASK) <<
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002181 TPD_PLOADOFFSET_SHIFT;
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002182 ptpd->word3 |= (cso & TPD_CCSUMOFFSET_MASK) <<
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002183 TPD_CCSUMOFFSET_SHIFT;
2184 ptpd->word3 |= 1 << TPD_CUST_CSUM_EN_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002185 return true;
2186 }
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002187 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002188}
2189
Jay Cliburn53ffb422007-07-15 11:03:27 -05002190static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002191 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002192{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002193 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2194 struct atl1_buffer *buffer_info;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002195 u16 buf_len = skb->len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002196 struct page *page;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002197 unsigned long offset;
2198 unsigned int nr_frags;
2199 unsigned int f;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002200 int retval;
2201 u16 next_to_use;
2202 u16 data_len;
2203 u8 hdr_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002204
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002205 buf_len -= skb->data_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002206 nr_frags = skb_shinfo(skb)->nr_frags;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002207 next_to_use = atomic_read(&tpd_ring->next_to_use);
2208 buffer_info = &tpd_ring->buffer_info[next_to_use];
Alexander Beregalov0ee904c2009-04-11 14:50:23 +00002209 BUG_ON(buffer_info->skb);
Jay Cliburn305282b2008-02-02 19:50:04 -06002210 /* put skb in last TPD */
2211 buffer_info->skb = NULL;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002212
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002213 retval = (ptpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
2214 if (retval) {
2215 /* TSO */
2216 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2217 buffer_info->length = hdr_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002218 page = virt_to_page(skb->data);
2219 offset = (unsigned long)skb->data & ~PAGE_MASK;
2220 buffer_info->dma = pci_map_page(adapter->pdev, page,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002221 offset, hdr_len,
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002222 PCI_DMA_TODEVICE);
2223
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002224 if (++next_to_use == tpd_ring->count)
2225 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002226
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002227 if (buf_len > hdr_len) {
2228 int i, nseg;
Stephen Hemmingerddfce6b2007-10-05 17:19:47 -07002229
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002230 data_len = buf_len - hdr_len;
2231 nseg = (data_len + ATL1_MAX_TX_BUF_LEN - 1) /
Jay Cliburn53ffb422007-07-15 11:03:27 -05002232 ATL1_MAX_TX_BUF_LEN;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002233 for (i = 0; i < nseg; i++) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002234 buffer_info =
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002235 &tpd_ring->buffer_info[next_to_use];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002236 buffer_info->skb = NULL;
2237 buffer_info->length =
Jay Cliburn2b116142007-07-15 11:03:26 -05002238 (ATL1_MAX_TX_BUF_LEN >=
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002239 data_len) ? ATL1_MAX_TX_BUF_LEN : data_len;
2240 data_len -= buffer_info->length;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002241 page = virt_to_page(skb->data +
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002242 (hdr_len + i * ATL1_MAX_TX_BUF_LEN));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002243 offset = (unsigned long)(skb->data +
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002244 (hdr_len + i * ATL1_MAX_TX_BUF_LEN)) &
2245 ~PAGE_MASK;
Jay Cliburn53ffb422007-07-15 11:03:27 -05002246 buffer_info->dma = pci_map_page(adapter->pdev,
2247 page, offset, buffer_info->length,
2248 PCI_DMA_TODEVICE);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002249 if (++next_to_use == tpd_ring->count)
2250 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002251 }
2252 }
2253 } else {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002254 /* not TSO */
2255 buffer_info->length = buf_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002256 page = virt_to_page(skb->data);
2257 offset = (unsigned long)skb->data & ~PAGE_MASK;
2258 buffer_info->dma = pci_map_page(adapter->pdev, page,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002259 offset, buf_len, PCI_DMA_TODEVICE);
2260 if (++next_to_use == tpd_ring->count)
2261 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002262 }
2263
2264 for (f = 0; f < nr_frags; f++) {
2265 struct skb_frag_struct *frag;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002266 u16 i, nseg;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002267
2268 frag = &skb_shinfo(skb)->frags[f];
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002269 buf_len = frag->size;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002270
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002271 nseg = (buf_len + ATL1_MAX_TX_BUF_LEN - 1) /
2272 ATL1_MAX_TX_BUF_LEN;
2273 for (i = 0; i < nseg; i++) {
2274 buffer_info = &tpd_ring->buffer_info[next_to_use];
Alexander Beregalov0ee904c2009-04-11 14:50:23 +00002275 BUG_ON(buffer_info->skb);
2276
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002277 buffer_info->skb = NULL;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002278 buffer_info->length = (buf_len > ATL1_MAX_TX_BUF_LEN) ?
2279 ATL1_MAX_TX_BUF_LEN : buf_len;
2280 buf_len -= buffer_info->length;
Jay Cliburn53ffb422007-07-15 11:03:27 -05002281 buffer_info->dma = pci_map_page(adapter->pdev,
2282 frag->page,
2283 frag->page_offset + (i * ATL1_MAX_TX_BUF_LEN),
2284 buffer_info->length, PCI_DMA_TODEVICE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002285
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002286 if (++next_to_use == tpd_ring->count)
2287 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002288 }
2289 }
2290
2291 /* last tpd's buffer-info */
2292 buffer_info->skb = skb;
2293}
2294
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002295static void atl1_tx_queue(struct atl1_adapter *adapter, u16 count,
2296 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002297{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002298 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002299 struct atl1_buffer *buffer_info;
2300 struct tx_packet_desc *tpd;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002301 u16 j;
2302 u32 val;
2303 u16 next_to_use = (u16) atomic_read(&tpd_ring->next_to_use);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002304
2305 for (j = 0; j < count; j++) {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002306 buffer_info = &tpd_ring->buffer_info[next_to_use];
2307 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, next_to_use);
2308 if (tpd != ptpd)
2309 memcpy(tpd, ptpd, sizeof(struct tx_packet_desc));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002310 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
Jay Cliburndc5596d2008-10-29 11:01:36 -05002311 tpd->word2 &= ~(TPD_BUFLEN_MASK << TPD_BUFLEN_SHIFT);
2312 tpd->word2 |= (cpu_to_le16(buffer_info->length) &
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002313 TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002314
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002315 /*
2316 * if this is the first packet in a TSO chain, set
2317 * TPD_HDRFLAG, otherwise, clear it.
2318 */
2319 val = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) &
2320 TPD_SEGMENT_EN_MASK;
2321 if (val) {
2322 if (!j)
2323 tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
2324 else
2325 tpd->word3 &= ~(1 << TPD_HDRFLAG_SHIFT);
2326 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002327
2328 if (j == (count - 1))
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002329 tpd->word3 |= 1 << TPD_EOP_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002330
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002331 if (++next_to_use == tpd_ring->count)
2332 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002333 }
2334 /*
2335 * Force memory writes to complete before letting h/w
2336 * know there are new descriptors to fetch. (Only
2337 * applicable for weak-ordered memory model archs,
2338 * such as IA-64).
2339 */
2340 wmb();
2341
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002342 atomic_set(&tpd_ring->next_to_use, next_to_use);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002343}
2344
Stephen Hemminger613573252009-08-31 19:50:58 +00002345static netdev_tx_t atl1_xmit_frame(struct sk_buff *skb,
2346 struct net_device *netdev)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002347{
2348 struct atl1_adapter *adapter = netdev_priv(netdev);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002349 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002350 int len = skb->len;
2351 int tso;
2352 int count = 1;
2353 int ret_val;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002354 struct tx_packet_desc *ptpd;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002355 u16 frag_size;
2356 u16 vlan_tag;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002357 unsigned int nr_frags = 0;
2358 unsigned int mss = 0;
2359 unsigned int f;
2360 unsigned int proto_hdr_len;
2361
2362 len -= skb->data_len;
2363
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002364 if (unlikely(skb->len <= 0)) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002365 dev_kfree_skb_any(skb);
2366 return NETDEV_TX_OK;
2367 }
2368
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002369 nr_frags = skb_shinfo(skb)->nr_frags;
2370 for (f = 0; f < nr_frags; f++) {
2371 frag_size = skb_shinfo(skb)->frags[f].size;
2372 if (frag_size)
Jay Cliburn53ffb422007-07-15 11:03:27 -05002373 count += (frag_size + ATL1_MAX_TX_BUF_LEN - 1) /
2374 ATL1_MAX_TX_BUF_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002375 }
2376
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002377 mss = skb_shinfo(skb)->gso_size;
2378 if (mss) {
Eric Dumazet17d0cdf2009-06-11 17:23:24 -07002379 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002380 proto_hdr_len = (skb_transport_offset(skb) +
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002381 tcp_hdrlen(skb));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002382 if (unlikely(proto_hdr_len > len)) {
2383 dev_kfree_skb_any(skb);
2384 return NETDEV_TX_OK;
2385 }
2386 /* need additional TPD ? */
2387 if (proto_hdr_len != len)
2388 count += (len - proto_hdr_len +
Jay Cliburn53ffb422007-07-15 11:03:27 -05002389 ATL1_MAX_TX_BUF_LEN - 1) /
2390 ATL1_MAX_TX_BUF_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002391 }
2392 }
2393
Jay Cliburne6a7ff42007-07-19 18:45:10 -05002394 if (atl1_tpd_avail(&adapter->tpd_ring) < count) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002395 /* not enough descriptors */
2396 netif_stop_queue(netdev);
Jay Cliburn460578b2008-02-02 19:50:09 -06002397 if (netif_msg_tx_queued(adapter))
2398 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2399 "tx busy\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002400 return NETDEV_TX_BUSY;
2401 }
2402
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002403 ptpd = ATL1_TPD_DESC(tpd_ring,
2404 (u16) atomic_read(&tpd_ring->next_to_use));
2405 memset(ptpd, 0, sizeof(struct tx_packet_desc));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002406
2407 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2408 vlan_tag = vlan_tx_tag_get(skb);
2409 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
2410 ((vlan_tag >> 9) & 0x8);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002411 ptpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
Jay Cliburndc5596d2008-10-29 11:01:36 -05002412 ptpd->word2 |= (vlan_tag & TPD_VLANTAG_MASK) <<
2413 TPD_VLANTAG_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002414 }
2415
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002416 tso = atl1_tso(adapter, skb, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002417 if (tso < 0) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002418 dev_kfree_skb_any(skb);
2419 return NETDEV_TX_OK;
2420 }
2421
2422 if (!tso) {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002423 ret_val = atl1_tx_csum(adapter, skb, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002424 if (ret_val < 0) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002425 dev_kfree_skb_any(skb);
2426 return NETDEV_TX_OK;
2427 }
2428 }
2429
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002430 atl1_tx_map(adapter, skb, ptpd);
2431 atl1_tx_queue(adapter, count, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002432 atl1_update_mailbox(adapter);
Jay Cliburne1098322008-09-27 04:17:21 +00002433 mmiowb();
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002434 return NETDEV_TX_OK;
2435}
2436
2437/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002438 * atl1_intr - Interrupt Handler
2439 * @irq: interrupt number
2440 * @data: pointer to a network interface device structure
2441 * @pt_regs: CPU registers structure
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002442 */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002443static irqreturn_t atl1_intr(int irq, void *data)
2444{
2445 struct atl1_adapter *adapter = netdev_priv(data);
2446 u32 status;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002447 int max_ints = 10;
2448
2449 status = adapter->cmb.cmb->int_stats;
2450 if (!status)
2451 return IRQ_NONE;
2452
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002453 do {
2454 /* clear CMB interrupt status at once */
2455 adapter->cmb.cmb->int_stats = 0;
2456
2457 if (status & ISR_GPHY) /* clear phy status */
Jay Cliburn305282b2008-02-02 19:50:04 -06002458 atlx_clear_phy_int(adapter);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002459
2460 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
2461 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
2462
2463 /* check if SMB intr */
2464 if (status & ISR_SMB)
2465 atl1_inc_smb(adapter);
2466
2467 /* check if PCIE PHY Link down */
2468 if (status & ISR_PHY_LINKDOWN) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002469 if (netif_msg_intr(adapter))
2470 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2471 "pcie phy link down %x\n", status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002472 if (netif_running(adapter->netdev)) { /* reset MAC */
2473 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2474 schedule_work(&adapter->pcie_dma_to_rst_task);
2475 return IRQ_HANDLED;
2476 }
2477 }
2478
2479 /* check if DMA read/write error ? */
2480 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002481 if (netif_msg_intr(adapter))
2482 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2483 "pcie DMA r/w error (status = 0x%x)\n",
2484 status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002485 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2486 schedule_work(&adapter->pcie_dma_to_rst_task);
2487 return IRQ_HANDLED;
2488 }
2489
2490 /* link event */
2491 if (status & ISR_GPHY) {
2492 adapter->soft_stats.tx_carrier_errors++;
2493 atl1_check_for_link(adapter);
2494 }
2495
2496 /* transmit event */
2497 if (status & ISR_CMB_TX)
2498 atl1_intr_tx(adapter);
2499
2500 /* rx exception */
2501 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2502 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2503 ISR_HOST_RRD_OV | ISR_CMB_RX))) {
2504 if (status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2505 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2506 ISR_HOST_RRD_OV))
Jay Cliburn460578b2008-02-02 19:50:09 -06002507 if (netif_msg_intr(adapter))
2508 dev_printk(KERN_DEBUG,
2509 &adapter->pdev->dev,
2510 "rx exception, ISR = 0x%x\n",
2511 status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002512 atl1_intr_rx(adapter);
2513 }
2514
2515 if (--max_ints < 0)
2516 break;
2517
2518 } while ((status = adapter->cmb.cmb->int_stats));
2519
2520 /* re-enable Interrupt */
2521 iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
2522 return IRQ_HANDLED;
2523}
2524
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002525
2526/*
2527 * atl1_phy_config - Timer Call-back
2528 * @data: pointer to netdev cast into an unsigned long
2529 */
2530static void atl1_phy_config(unsigned long data)
2531{
2532 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
2533 struct atl1_hw *hw = &adapter->hw;
2534 unsigned long flags;
2535
2536 spin_lock_irqsave(&adapter->lock, flags);
2537 adapter->phy_timer_pending = false;
2538 atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
Jay Cliburn305282b2008-02-02 19:50:04 -06002539 atl1_write_phy_reg(hw, MII_ATLX_CR, hw->mii_1000t_ctrl_reg);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002540 atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
2541 spin_unlock_irqrestore(&adapter->lock, flags);
2542}
2543
2544/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002545 * Orphaned vendor comment left intact here:
2546 * <vendor comment>
2547 * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2548 * will assert. We do soft reset <0x1400=1> according
2549 * with the SPEC. BUT, it seemes that PCIE or DMA
2550 * state-machine will not be reset. DMAR_TO_INT will
2551 * assert again and again.
2552 * </vendor comment>
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002553 */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002554
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06002555static int atl1_reset(struct atl1_adapter *adapter)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002556{
2557 int ret;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002558 ret = atl1_reset_hw(&adapter->hw);
Jay Cliburn305282b2008-02-02 19:50:04 -06002559 if (ret)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002560 return ret;
2561 return atl1_init_hw(&adapter->hw);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002562}
2563
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06002564static s32 atl1_up(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002565{
2566 struct net_device *netdev = adapter->netdev;
2567 int err;
2568 int irq_flags = IRQF_SAMPLE_RANDOM;
2569
2570 /* hardware has been reset, we need to reload some things */
Jay Cliburn305282b2008-02-02 19:50:04 -06002571 atlx_set_multi(netdev);
Jay Cliburn2ca13da2007-07-15 11:03:28 -05002572 atl1_init_ring_ptrs(adapter);
Jay Cliburn305282b2008-02-02 19:50:04 -06002573 atlx_restore_vlan(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002574 err = atl1_alloc_rx_buffers(adapter);
Jay Cliburn305282b2008-02-02 19:50:04 -06002575 if (unlikely(!err))
2576 /* no RX BUFFER allocated */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002577 return -ENOMEM;
2578
2579 if (unlikely(atl1_configure(adapter))) {
2580 err = -EIO;
2581 goto err_up;
2582 }
2583
2584 err = pci_enable_msi(adapter->pdev);
2585 if (err) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002586 if (netif_msg_ifup(adapter))
2587 dev_info(&adapter->pdev->dev,
2588 "Unable to enable MSI: %d\n", err);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002589 irq_flags |= IRQF_SHARED;
2590 }
2591
Joe Perchesa0607fd2009-11-18 23:29:17 -08002592 err = request_irq(adapter->pdev->irq, atl1_intr, irq_flags,
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002593 netdev->name, netdev);
2594 if (unlikely(err))
2595 goto err_up;
2596
Jay Cliburn305282b2008-02-02 19:50:04 -06002597 atlx_irq_enable(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002598 atl1_check_link(adapter);
David S. Miller39d48152008-07-21 08:28:37 -07002599 netif_start_queue(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002600 return 0;
2601
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002602err_up:
2603 pci_disable_msi(adapter->pdev);
2604 /* free rx_buffers */
2605 atl1_clean_rx_ring(adapter);
2606 return err;
2607}
2608
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06002609static void atl1_down(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002610{
2611 struct net_device *netdev = adapter->netdev;
2612
Jay Cliburnb29be6d2008-09-27 04:17:20 +00002613 netif_stop_queue(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002614 del_timer_sync(&adapter->phy_config_timer);
2615 adapter->phy_timer_pending = false;
2616
Jay Cliburn305282b2008-02-02 19:50:04 -06002617 atlx_irq_disable(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002618 free_irq(adapter->pdev->irq, netdev);
2619 pci_disable_msi(adapter->pdev);
2620 atl1_reset_hw(&adapter->hw);
2621 adapter->cmb.cmb->int_stats = 0;
2622
2623 adapter->link_speed = SPEED_0;
2624 adapter->link_duplex = -1;
2625 netif_carrier_off(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002626
2627 atl1_clean_tx_ring(adapter);
2628 atl1_clean_rx_ring(adapter);
2629}
2630
Jay Cliburn0dde4ef2008-02-02 19:50:11 -06002631static void atl1_tx_timeout_task(struct work_struct *work)
2632{
2633 struct atl1_adapter *adapter =
2634 container_of(work, struct atl1_adapter, tx_timeout_task);
2635 struct net_device *netdev = adapter->netdev;
2636
2637 netif_device_detach(netdev);
2638 atl1_down(adapter);
2639 atl1_up(adapter);
2640 netif_device_attach(netdev);
2641}
2642
2643/*
2644 * atl1_change_mtu - Change the Maximum Transfer Unit
2645 * @netdev: network interface device structure
2646 * @new_mtu: new value for maximum frame size
2647 *
2648 * Returns 0 on success, negative on failure
2649 */
2650static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
2651{
2652 struct atl1_adapter *adapter = netdev_priv(netdev);
2653 int old_mtu = netdev->mtu;
2654 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2655
2656 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
2657 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2658 if (netif_msg_link(adapter))
2659 dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
2660 return -EINVAL;
2661 }
2662
2663 adapter->hw.max_frame_size = max_frame;
2664 adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
2665 adapter->rx_buffer_len = (max_frame + 7) & ~7;
2666 adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
2667
2668 netdev->mtu = new_mtu;
2669 if ((old_mtu != new_mtu) && netif_running(netdev)) {
2670 atl1_down(adapter);
2671 atl1_up(adapter);
2672 }
2673
2674 return 0;
2675}
2676
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002677/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002678 * atl1_open - Called when a network interface is made active
2679 * @netdev: network interface device structure
2680 *
2681 * Returns 0 on success, negative value on failure
2682 *
2683 * The open entry point is called when a network interface is made
2684 * active by the system (IFF_UP). At this point all resources needed
2685 * for transmit and receive operations are allocated, the interrupt
2686 * handler is registered with the OS, the watchdog timer is started,
2687 * and the stack is notified that the interface is ready.
2688 */
2689static int atl1_open(struct net_device *netdev)
2690{
2691 struct atl1_adapter *adapter = netdev_priv(netdev);
2692 int err;
2693
Jay Cliburnb29be6d2008-09-27 04:17:20 +00002694 netif_carrier_off(netdev);
2695
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002696 /* allocate transmit descriptors */
2697 err = atl1_setup_ring_resources(adapter);
2698 if (err)
2699 return err;
2700
2701 err = atl1_up(adapter);
2702 if (err)
2703 goto err_up;
2704
2705 return 0;
2706
2707err_up:
2708 atl1_reset(adapter);
2709 return err;
2710}
2711
2712/*
2713 * atl1_close - Disables a network interface
2714 * @netdev: network interface device structure
2715 *
2716 * Returns 0, this is not allowed to fail
2717 *
2718 * The close entry point is called when an interface is de-activated
2719 * by the OS. The hardware is still under the drivers control, but
2720 * needs to be disabled. A global MAC reset is issued to stop the
2721 * hardware, and all transmit and receive resources are freed.
2722 */
2723static int atl1_close(struct net_device *netdev)
2724{
2725 struct atl1_adapter *adapter = netdev_priv(netdev);
2726 atl1_down(adapter);
2727 atl1_free_ring_resources(adapter);
2728 return 0;
2729}
2730
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002731#ifdef CONFIG_PM
2732static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2733{
2734 struct net_device *netdev = pci_get_drvdata(pdev);
2735 struct atl1_adapter *adapter = netdev_priv(netdev);
2736 struct atl1_hw *hw = &adapter->hw;
2737 u32 ctrl = 0;
2738 u32 wufc = adapter->wol;
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002739 u32 val;
2740 int retval;
2741 u16 speed;
2742 u16 duplex;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002743
2744 netif_device_detach(netdev);
2745 if (netif_running(netdev))
2746 atl1_down(adapter);
2747
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002748 retval = pci_save_state(pdev);
2749 if (retval)
2750 return retval;
2751
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002752 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2753 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002754 val = ctrl & BMSR_LSTATUS;
2755 if (val)
Jay Cliburn305282b2008-02-02 19:50:04 -06002756 wufc &= ~ATLX_WUFC_LNKC;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002757
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002758 if (val && wufc) {
2759 val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
2760 if (val) {
2761 if (netif_msg_ifdown(adapter))
2762 dev_printk(KERN_DEBUG, &pdev->dev,
2763 "error getting speed/duplex\n");
2764 goto disable_wol;
2765 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002766
2767 ctrl = 0;
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002768
2769 /* enable magic packet WOL */
Jay Cliburn305282b2008-02-02 19:50:04 -06002770 if (wufc & ATLX_WUFC_MAG)
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002771 ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002772 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002773 ioread32(hw->hw_addr + REG_WOL_CTRL);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002774
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002775 /* configure the mac */
2776 ctrl = MAC_CTRL_RX_EN;
2777 ctrl |= ((u32)((speed == SPEED_1000) ? MAC_CTRL_SPEED_1000 :
2778 MAC_CTRL_SPEED_10_100) << MAC_CTRL_SPEED_SHIFT);
2779 if (duplex == FULL_DUPLEX)
2780 ctrl |= MAC_CTRL_DUPLX;
2781 ctrl |= (((u32)adapter->hw.preamble_len &
2782 MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
2783 if (adapter->vlgrp)
2784 ctrl |= MAC_CTRL_RMV_VLAN;
2785 if (wufc & ATLX_WUFC_MAG)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002786 ctrl |= MAC_CTRL_BC_EN;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002787 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002788 ioread32(hw->hw_addr + REG_MAC_CTRL);
2789
2790 /* poke the PHY */
2791 ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2792 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2793 iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2794 ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2795
2796 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2797 goto exit;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002798 }
2799
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002800 if (!val && wufc) {
2801 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2802 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2803 ioread32(hw->hw_addr + REG_WOL_CTRL);
2804 iowrite32(0, hw->hw_addr + REG_MAC_CTRL);
2805 ioread32(hw->hw_addr + REG_MAC_CTRL);
2806 hw->phy_configured = false;
2807 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2808 goto exit;
2809 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002810
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002811disable_wol:
2812 iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2813 ioread32(hw->hw_addr + REG_WOL_CTRL);
2814 ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2815 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2816 iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2817 ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002818 hw->phy_configured = false;
2819 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
2820exit:
2821 if (netif_running(netdev))
2822 pci_disable_msi(adapter->pdev);
2823 pci_disable_device(pdev);
2824 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002825
2826 return 0;
2827}
2828
2829static int atl1_resume(struct pci_dev *pdev)
2830{
2831 struct net_device *netdev = pci_get_drvdata(pdev);
2832 struct atl1_adapter *adapter = netdev_priv(netdev);
Jay Cliburn305282b2008-02-02 19:50:04 -06002833 u32 err;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002834
Jay Cliburn305282b2008-02-02 19:50:04 -06002835 pci_set_power_state(pdev, PCI_D0);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002836 pci_restore_state(pdev);
2837
Jay Cliburn305282b2008-02-02 19:50:04 -06002838 err = pci_enable_device(pdev);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002839 if (err) {
2840 if (netif_msg_ifup(adapter))
2841 dev_printk(KERN_DEBUG, &pdev->dev,
2842 "error enabling pci device\n");
2843 return err;
2844 }
2845
2846 pci_set_master(pdev);
2847 iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002848 pci_enable_wake(pdev, PCI_D3hot, 0);
2849 pci_enable_wake(pdev, PCI_D3cold, 0);
2850
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002851 atl1_reset_hw(&adapter->hw);
2852 adapter->cmb.cmb->int_stats = 0;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002853
2854 if (netif_running(netdev))
2855 atl1_up(adapter);
2856 netif_device_attach(netdev);
2857
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002858 return 0;
2859}
2860#else
2861#define atl1_suspend NULL
2862#define atl1_resume NULL
2863#endif
2864
Jay Cliburnbf455a22008-05-09 22:12:08 -05002865static void atl1_shutdown(struct pci_dev *pdev)
2866{
2867#ifdef CONFIG_PM
2868 atl1_suspend(pdev, PMSG_SUSPEND);
2869#endif
2870}
2871
Alexey Dobriyan497f0502007-05-09 18:52:35 +04002872#ifdef CONFIG_NET_POLL_CONTROLLER
2873static void atl1_poll_controller(struct net_device *netdev)
2874{
2875 disable_irq(netdev->irq);
2876 atl1_intr(netdev->irq, netdev);
2877 enable_irq(netdev->irq);
2878}
2879#endif
2880
Stephen Hemminger825a84d2008-11-19 22:14:17 -08002881static const struct net_device_ops atl1_netdev_ops = {
2882 .ndo_open = atl1_open,
2883 .ndo_stop = atl1_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08002884 .ndo_start_xmit = atl1_xmit_frame,
Stephen Hemminger825a84d2008-11-19 22:14:17 -08002885 .ndo_set_multicast_list = atlx_set_multi,
2886 .ndo_validate_addr = eth_validate_addr,
2887 .ndo_set_mac_address = atl1_set_mac,
2888 .ndo_change_mtu = atl1_change_mtu,
2889 .ndo_do_ioctl = atlx_ioctl,
Stephen Hemminger00829822008-11-20 20:14:53 -08002890 .ndo_tx_timeout = atlx_tx_timeout,
Stephen Hemminger825a84d2008-11-19 22:14:17 -08002891 .ndo_vlan_rx_register = atlx_vlan_rx_register,
2892#ifdef CONFIG_NET_POLL_CONTROLLER
2893 .ndo_poll_controller = atl1_poll_controller,
2894#endif
2895};
2896
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002897/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002898 * atl1_probe - Device Initialization Routine
2899 * @pdev: PCI device information struct
2900 * @ent: entry in atl1_pci_tbl
2901 *
2902 * Returns 0 on success, negative on failure
2903 *
2904 * atl1_probe initializes an adapter identified by a pci_dev structure.
2905 * The OS initialization, configuring of the adapter private structure,
2906 * and a hardware reset occur.
2907 */
2908static int __devinit atl1_probe(struct pci_dev *pdev,
Jay Cliburn53ffb422007-07-15 11:03:27 -05002909 const struct pci_device_id *ent)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002910{
2911 struct net_device *netdev;
2912 struct atl1_adapter *adapter;
2913 static int cards_found = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002914 int err;
2915
2916 err = pci_enable_device(pdev);
2917 if (err)
2918 return err;
2919
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002920 /*
Chris Snookcdcc5202007-09-20 15:57:15 -04002921 * The atl1 chip can DMA to 64-bit addresses, but it uses a single
2922 * shared register for the high 32 bits, so only a single, aligned,
2923 * 4 GB physical address range can be used at a time.
2924 *
2925 * Supporting 64-bit DMA on this hardware is more trouble than it's
2926 * worth. It is far easier to limit to 32-bit DMA than update
2927 * various kernel subsystems to support the mechanics required by a
2928 * fixed-high-32-bit system.
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002929 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002930 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002931 if (err) {
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002932 dev_err(&pdev->dev, "no usable DMA configuration\n");
2933 goto err_dma;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002934 }
Jay Cliburn305282b2008-02-02 19:50:04 -06002935 /*
2936 * Mark all PCI regions associated with PCI device
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002937 * pdev as being reserved by owner atl1_driver_name
2938 */
Jay Cliburn305282b2008-02-02 19:50:04 -06002939 err = pci_request_regions(pdev, ATLX_DRIVER_NAME);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002940 if (err)
2941 goto err_request_regions;
2942
Jay Cliburn305282b2008-02-02 19:50:04 -06002943 /*
2944 * Enables bus-mastering on the device and calls
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002945 * pcibios_set_master to do the needed arch specific settings
2946 */
2947 pci_set_master(pdev);
2948
2949 netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2950 if (!netdev) {
2951 err = -ENOMEM;
2952 goto err_alloc_etherdev;
2953 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002954 SET_NETDEV_DEV(netdev, &pdev->dev);
2955
2956 pci_set_drvdata(pdev, netdev);
2957 adapter = netdev_priv(netdev);
2958 adapter->netdev = netdev;
2959 adapter->pdev = pdev;
2960 adapter->hw.back = adapter;
Jay Cliburn460578b2008-02-02 19:50:09 -06002961 adapter->msg_enable = netif_msg_init(debug, atl1_default_msg);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002962
2963 adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2964 if (!adapter->hw.hw_addr) {
2965 err = -EIO;
2966 goto err_pci_iomap;
2967 }
2968 /* get device revision number */
Jay Cliburn1e006362007-04-29 21:42:10 -05002969 adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr +
Jay Cliburn53ffb422007-07-15 11:03:27 -05002970 (REG_MASTER_CTRL + 2));
Jay Cliburn460578b2008-02-02 19:50:09 -06002971 if (netif_msg_probe(adapter))
2972 dev_info(&pdev->dev, "version %s\n", ATLX_DRIVER_VERSION);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002973
2974 /* set default ring resource counts */
2975 adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2976 adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2977
2978 adapter->mii.dev = netdev;
2979 adapter->mii.mdio_read = mdio_read;
2980 adapter->mii.mdio_write = mdio_write;
2981 adapter->mii.phy_id_mask = 0x1f;
2982 adapter->mii.reg_num_mask = 0x1f;
2983
Stephen Hemminger825a84d2008-11-19 22:14:17 -08002984 netdev->netdev_ops = &atl1_netdev_ops;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002985 netdev->watchdog_timeo = 5 * HZ;
Stephen Hemmingercb434e32007-06-01 09:44:00 -07002986
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002987 netdev->ethtool_ops = &atl1_ethtool_ops;
2988 adapter->bd_number = cards_found;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002989
2990 /* setup the private structure */
2991 err = atl1_sw_init(adapter);
2992 if (err)
2993 goto err_common;
2994
2995 netdev->features = NETIF_F_HW_CSUM;
2996 netdev->features |= NETIF_F_SG;
2997 netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002998
2999 /*
3000 * patch for some L1 of old version,
3001 * the final version of L1 may not need these
3002 * patches
3003 */
3004 /* atl1_pcie_patch(adapter); */
3005
3006 /* really reset GPHY core */
Jay Cliburn305282b2008-02-02 19:50:04 -06003007 iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003008
3009 /*
3010 * reset the controller to
3011 * put the device in a known good starting state
3012 */
3013 if (atl1_reset_hw(&adapter->hw)) {
3014 err = -EIO;
3015 goto err_common;
3016 }
3017
3018 /* copy the MAC address out of the EEPROM */
3019 atl1_read_mac_addr(&adapter->hw);
3020 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
3021
3022 if (!is_valid_ether_addr(netdev->dev_addr)) {
3023 err = -EIO;
3024 goto err_common;
3025 }
3026
3027 atl1_check_options(adapter);
3028
3029 /* pre-init the MAC, and setup link */
3030 err = atl1_init_hw(&adapter->hw);
3031 if (err) {
3032 err = -EIO;
3033 goto err_common;
3034 }
3035
3036 atl1_pcie_patch(adapter);
3037 /* assume we have no link for now */
3038 netif_carrier_off(netdev);
3039 netif_stop_queue(netdev);
3040
Stephen Hemmingere053b622008-10-31 16:52:04 -07003041 setup_timer(&adapter->phy_config_timer, &atl1_phy_config,
3042 (unsigned long)adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003043 adapter->phy_timer_pending = false;
3044
3045 INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
3046
Jay Cliburn305282b2008-02-02 19:50:04 -06003047 INIT_WORK(&adapter->link_chg_task, atlx_link_chg_task);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003048
3049 INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
3050
3051 err = register_netdev(netdev);
3052 if (err)
3053 goto err_common;
3054
3055 cards_found++;
3056 atl1_via_workaround(adapter);
3057 return 0;
3058
3059err_common:
3060 pci_iounmap(pdev, adapter->hw.hw_addr);
3061err_pci_iomap:
3062 free_netdev(netdev);
3063err_alloc_etherdev:
3064 pci_release_regions(pdev);
3065err_dma:
3066err_request_regions:
3067 pci_disable_device(pdev);
3068 return err;
3069}
3070
3071/*
3072 * atl1_remove - Device Removal Routine
3073 * @pdev: PCI device information struct
3074 *
3075 * atl1_remove is called by the PCI subsystem to alert the driver
3076 * that it should release a PCI device. The could be caused by a
3077 * Hot-Plug event, or because the driver is going to be removed from
3078 * memory.
3079 */
3080static void __devexit atl1_remove(struct pci_dev *pdev)
3081{
3082 struct net_device *netdev = pci_get_drvdata(pdev);
3083 struct atl1_adapter *adapter;
3084 /* Device not available. Return. */
3085 if (!netdev)
3086 return;
3087
3088 adapter = netdev_priv(netdev);
Chris Snook8c754a02007-03-28 20:51:51 -04003089
Jay Cliburn305282b2008-02-02 19:50:04 -06003090 /*
3091 * Some atl1 boards lack persistent storage for their MAC, and get it
Chris Snook8c754a02007-03-28 20:51:51 -04003092 * from the BIOS during POST. If we've been messing with the MAC
3093 * address, we need to save the permanent one.
3094 */
3095 if (memcmp(adapter->hw.mac_addr, adapter->hw.perm_mac_addr, ETH_ALEN)) {
Jay Cliburn53ffb422007-07-15 11:03:27 -05003096 memcpy(adapter->hw.mac_addr, adapter->hw.perm_mac_addr,
3097 ETH_ALEN);
Chris Snook8c754a02007-03-28 20:51:51 -04003098 atl1_set_mac_addr(&adapter->hw);
3099 }
3100
Jay Cliburn305282b2008-02-02 19:50:04 -06003101 iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003102 unregister_netdev(netdev);
3103 pci_iounmap(pdev, adapter->hw.hw_addr);
3104 pci_release_regions(pdev);
3105 free_netdev(netdev);
3106 pci_disable_device(pdev);
3107}
3108
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003109static struct pci_driver atl1_driver = {
Jay Cliburn305282b2008-02-02 19:50:04 -06003110 .name = ATLX_DRIVER_NAME,
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003111 .id_table = atl1_pci_tbl,
3112 .probe = atl1_probe,
3113 .remove = __devexit_p(atl1_remove),
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003114 .suspend = atl1_suspend,
Jay Cliburnbf455a22008-05-09 22:12:08 -05003115 .resume = atl1_resume,
3116 .shutdown = atl1_shutdown
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003117};
3118
3119/*
3120 * atl1_exit_module - Driver Exit Cleanup Routine
3121 *
3122 * atl1_exit_module is called just before the driver is removed
3123 * from memory.
3124 */
3125static void __exit atl1_exit_module(void)
3126{
3127 pci_unregister_driver(&atl1_driver);
3128}
3129
3130/*
3131 * atl1_init_module - Driver Registration Routine
3132 *
3133 * atl1_init_module is the first routine called when the driver is
3134 * loaded. All it does is register with the PCI subsystem.
3135 */
3136static int __init atl1_init_module(void)
3137{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003138 return pci_register_driver(&atl1_driver);
3139}
3140
3141module_init(atl1_init_module);
3142module_exit(atl1_exit_module);
Jay Cliburn305282b2008-02-02 19:50:04 -06003143
3144struct atl1_stats {
3145 char stat_string[ETH_GSTRING_LEN];
3146 int sizeof_stat;
3147 int stat_offset;
3148};
3149
3150#define ATL1_STAT(m) \
3151 sizeof(((struct atl1_adapter *)0)->m), offsetof(struct atl1_adapter, m)
3152
3153static struct atl1_stats atl1_gstrings_stats[] = {
3154 {"rx_packets", ATL1_STAT(soft_stats.rx_packets)},
3155 {"tx_packets", ATL1_STAT(soft_stats.tx_packets)},
3156 {"rx_bytes", ATL1_STAT(soft_stats.rx_bytes)},
3157 {"tx_bytes", ATL1_STAT(soft_stats.tx_bytes)},
3158 {"rx_errors", ATL1_STAT(soft_stats.rx_errors)},
3159 {"tx_errors", ATL1_STAT(soft_stats.tx_errors)},
Jay Cliburn305282b2008-02-02 19:50:04 -06003160 {"multicast", ATL1_STAT(soft_stats.multicast)},
3161 {"collisions", ATL1_STAT(soft_stats.collisions)},
3162 {"rx_length_errors", ATL1_STAT(soft_stats.rx_length_errors)},
3163 {"rx_over_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3164 {"rx_crc_errors", ATL1_STAT(soft_stats.rx_crc_errors)},
3165 {"rx_frame_errors", ATL1_STAT(soft_stats.rx_frame_errors)},
3166 {"rx_fifo_errors", ATL1_STAT(soft_stats.rx_fifo_errors)},
3167 {"rx_missed_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3168 {"tx_aborted_errors", ATL1_STAT(soft_stats.tx_aborted_errors)},
3169 {"tx_carrier_errors", ATL1_STAT(soft_stats.tx_carrier_errors)},
3170 {"tx_fifo_errors", ATL1_STAT(soft_stats.tx_fifo_errors)},
3171 {"tx_window_errors", ATL1_STAT(soft_stats.tx_window_errors)},
3172 {"tx_abort_exce_coll", ATL1_STAT(soft_stats.excecol)},
3173 {"tx_abort_late_coll", ATL1_STAT(soft_stats.latecol)},
3174 {"tx_deferred_ok", ATL1_STAT(soft_stats.deffer)},
3175 {"tx_single_coll_ok", ATL1_STAT(soft_stats.scc)},
3176 {"tx_multi_coll_ok", ATL1_STAT(soft_stats.mcc)},
3177 {"tx_underun", ATL1_STAT(soft_stats.tx_underun)},
3178 {"tx_trunc", ATL1_STAT(soft_stats.tx_trunc)},
3179 {"tx_pause", ATL1_STAT(soft_stats.tx_pause)},
3180 {"rx_pause", ATL1_STAT(soft_stats.rx_pause)},
3181 {"rx_rrd_ov", ATL1_STAT(soft_stats.rx_rrd_ov)},
3182 {"rx_trunc", ATL1_STAT(soft_stats.rx_trunc)}
3183};
3184
3185static void atl1_get_ethtool_stats(struct net_device *netdev,
3186 struct ethtool_stats *stats, u64 *data)
3187{
3188 struct atl1_adapter *adapter = netdev_priv(netdev);
3189 int i;
3190 char *p;
3191
3192 for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3193 p = (char *)adapter+atl1_gstrings_stats[i].stat_offset;
3194 data[i] = (atl1_gstrings_stats[i].sizeof_stat ==
3195 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
3196 }
3197
3198}
3199
3200static int atl1_get_sset_count(struct net_device *netdev, int sset)
3201{
3202 switch (sset) {
3203 case ETH_SS_STATS:
3204 return ARRAY_SIZE(atl1_gstrings_stats);
3205 default:
3206 return -EOPNOTSUPP;
3207 }
3208}
3209
3210static int atl1_get_settings(struct net_device *netdev,
3211 struct ethtool_cmd *ecmd)
3212{
3213 struct atl1_adapter *adapter = netdev_priv(netdev);
3214 struct atl1_hw *hw = &adapter->hw;
3215
3216 ecmd->supported = (SUPPORTED_10baseT_Half |
3217 SUPPORTED_10baseT_Full |
3218 SUPPORTED_100baseT_Half |
3219 SUPPORTED_100baseT_Full |
3220 SUPPORTED_1000baseT_Full |
3221 SUPPORTED_Autoneg | SUPPORTED_TP);
3222 ecmd->advertising = ADVERTISED_TP;
3223 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3224 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3225 ecmd->advertising |= ADVERTISED_Autoneg;
3226 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR) {
3227 ecmd->advertising |= ADVERTISED_Autoneg;
3228 ecmd->advertising |=
3229 (ADVERTISED_10baseT_Half |
3230 ADVERTISED_10baseT_Full |
3231 ADVERTISED_100baseT_Half |
3232 ADVERTISED_100baseT_Full |
3233 ADVERTISED_1000baseT_Full);
3234 } else
3235 ecmd->advertising |= (ADVERTISED_1000baseT_Full);
3236 }
3237 ecmd->port = PORT_TP;
3238 ecmd->phy_address = 0;
3239 ecmd->transceiver = XCVR_INTERNAL;
3240
3241 if (netif_carrier_ok(adapter->netdev)) {
3242 u16 link_speed, link_duplex;
3243 atl1_get_speed_and_duplex(hw, &link_speed, &link_duplex);
3244 ecmd->speed = link_speed;
3245 if (link_duplex == FULL_DUPLEX)
3246 ecmd->duplex = DUPLEX_FULL;
3247 else
3248 ecmd->duplex = DUPLEX_HALF;
3249 } else {
3250 ecmd->speed = -1;
3251 ecmd->duplex = -1;
3252 }
3253 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3254 hw->media_type == MEDIA_TYPE_1000M_FULL)
3255 ecmd->autoneg = AUTONEG_ENABLE;
3256 else
3257 ecmd->autoneg = AUTONEG_DISABLE;
3258
3259 return 0;
3260}
3261
3262static int atl1_set_settings(struct net_device *netdev,
3263 struct ethtool_cmd *ecmd)
3264{
3265 struct atl1_adapter *adapter = netdev_priv(netdev);
3266 struct atl1_hw *hw = &adapter->hw;
3267 u16 phy_data;
3268 int ret_val = 0;
3269 u16 old_media_type = hw->media_type;
3270
3271 if (netif_running(adapter->netdev)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003272 if (netif_msg_link(adapter))
3273 dev_dbg(&adapter->pdev->dev,
3274 "ethtool shutting down adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003275 atl1_down(adapter);
3276 }
3277
3278 if (ecmd->autoneg == AUTONEG_ENABLE)
3279 hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
3280 else {
3281 if (ecmd->speed == SPEED_1000) {
3282 if (ecmd->duplex != DUPLEX_FULL) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003283 if (netif_msg_link(adapter))
3284 dev_warn(&adapter->pdev->dev,
3285 "1000M half is invalid\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003286 ret_val = -EINVAL;
3287 goto exit_sset;
3288 }
3289 hw->media_type = MEDIA_TYPE_1000M_FULL;
3290 } else if (ecmd->speed == SPEED_100) {
3291 if (ecmd->duplex == DUPLEX_FULL)
3292 hw->media_type = MEDIA_TYPE_100M_FULL;
3293 else
3294 hw->media_type = MEDIA_TYPE_100M_HALF;
3295 } else {
3296 if (ecmd->duplex == DUPLEX_FULL)
3297 hw->media_type = MEDIA_TYPE_10M_FULL;
3298 else
3299 hw->media_type = MEDIA_TYPE_10M_HALF;
3300 }
3301 }
3302 switch (hw->media_type) {
3303 case MEDIA_TYPE_AUTO_SENSOR:
3304 ecmd->advertising =
3305 ADVERTISED_10baseT_Half |
3306 ADVERTISED_10baseT_Full |
3307 ADVERTISED_100baseT_Half |
3308 ADVERTISED_100baseT_Full |
3309 ADVERTISED_1000baseT_Full |
3310 ADVERTISED_Autoneg | ADVERTISED_TP;
3311 break;
3312 case MEDIA_TYPE_1000M_FULL:
3313 ecmd->advertising =
3314 ADVERTISED_1000baseT_Full |
3315 ADVERTISED_Autoneg | ADVERTISED_TP;
3316 break;
3317 default:
3318 ecmd->advertising = 0;
3319 break;
3320 }
3321 if (atl1_phy_setup_autoneg_adv(hw)) {
3322 ret_val = -EINVAL;
Jay Cliburn460578b2008-02-02 19:50:09 -06003323 if (netif_msg_link(adapter))
3324 dev_warn(&adapter->pdev->dev,
3325 "invalid ethtool speed/duplex setting\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003326 goto exit_sset;
3327 }
3328 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3329 hw->media_type == MEDIA_TYPE_1000M_FULL)
3330 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3331 else {
3332 switch (hw->media_type) {
3333 case MEDIA_TYPE_100M_FULL:
3334 phy_data =
3335 MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
3336 MII_CR_RESET;
3337 break;
3338 case MEDIA_TYPE_100M_HALF:
3339 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3340 break;
3341 case MEDIA_TYPE_10M_FULL:
3342 phy_data =
3343 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
3344 break;
3345 default:
3346 /* MEDIA_TYPE_10M_HALF: */
3347 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3348 break;
3349 }
3350 }
3351 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3352exit_sset:
3353 if (ret_val)
3354 hw->media_type = old_media_type;
3355
3356 if (netif_running(adapter->netdev)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003357 if (netif_msg_link(adapter))
3358 dev_dbg(&adapter->pdev->dev,
3359 "ethtool starting adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003360 atl1_up(adapter);
3361 } else if (!ret_val) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003362 if (netif_msg_link(adapter))
3363 dev_dbg(&adapter->pdev->dev,
3364 "ethtool resetting adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003365 atl1_reset(adapter);
3366 }
3367 return ret_val;
3368}
3369
3370static void atl1_get_drvinfo(struct net_device *netdev,
3371 struct ethtool_drvinfo *drvinfo)
3372{
3373 struct atl1_adapter *adapter = netdev_priv(netdev);
3374
Roel Kluin082ba882009-08-06 13:06:56 +00003375 strlcpy(drvinfo->driver, ATLX_DRIVER_NAME, sizeof(drvinfo->driver));
3376 strlcpy(drvinfo->version, ATLX_DRIVER_VERSION,
Jay Cliburn305282b2008-02-02 19:50:04 -06003377 sizeof(drvinfo->version));
Roel Kluin082ba882009-08-06 13:06:56 +00003378 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
3379 strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
Jay Cliburn305282b2008-02-02 19:50:04 -06003380 sizeof(drvinfo->bus_info));
3381 drvinfo->eedump_len = ATL1_EEDUMP_LEN;
3382}
3383
3384static void atl1_get_wol(struct net_device *netdev,
3385 struct ethtool_wolinfo *wol)
3386{
3387 struct atl1_adapter *adapter = netdev_priv(netdev);
3388
J. K. Cliburn3b259e32008-11-09 15:05:30 -06003389 wol->supported = WAKE_MAGIC;
Jay Cliburn305282b2008-02-02 19:50:04 -06003390 wol->wolopts = 0;
Jay Cliburn305282b2008-02-02 19:50:04 -06003391 if (adapter->wol & ATLX_WUFC_MAG)
3392 wol->wolopts |= WAKE_MAGIC;
3393 return;
3394}
3395
3396static int atl1_set_wol(struct net_device *netdev,
3397 struct ethtool_wolinfo *wol)
3398{
3399 struct atl1_adapter *adapter = netdev_priv(netdev);
3400
J. K. Cliburn3b259e32008-11-09 15:05:30 -06003401 if (wol->wolopts & (WAKE_PHY | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST |
3402 WAKE_ARP | WAKE_MAGICSECURE))
Jay Cliburn305282b2008-02-02 19:50:04 -06003403 return -EOPNOTSUPP;
3404 adapter->wol = 0;
Jay Cliburn305282b2008-02-02 19:50:04 -06003405 if (wol->wolopts & WAKE_MAGIC)
3406 adapter->wol |= ATLX_WUFC_MAG;
3407 return 0;
3408}
3409
Jay Cliburn460578b2008-02-02 19:50:09 -06003410static u32 atl1_get_msglevel(struct net_device *netdev)
3411{
3412 struct atl1_adapter *adapter = netdev_priv(netdev);
3413 return adapter->msg_enable;
3414}
3415
3416static void atl1_set_msglevel(struct net_device *netdev, u32 value)
3417{
3418 struct atl1_adapter *adapter = netdev_priv(netdev);
3419 adapter->msg_enable = value;
3420}
3421
Jay Cliburnc67c9a22008-02-02 19:50:06 -06003422static int atl1_get_regs_len(struct net_device *netdev)
3423{
3424 return ATL1_REG_COUNT * sizeof(u32);
3425}
3426
3427static void atl1_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
3428 void *p)
3429{
3430 struct atl1_adapter *adapter = netdev_priv(netdev);
3431 struct atl1_hw *hw = &adapter->hw;
3432 unsigned int i;
3433 u32 *regbuf = p;
3434
3435 for (i = 0; i < ATL1_REG_COUNT; i++) {
3436 /*
3437 * This switch statement avoids reserved regions
3438 * of register space.
3439 */
3440 switch (i) {
3441 case 6 ... 9:
3442 case 14:
3443 case 29 ... 31:
3444 case 34 ... 63:
3445 case 75 ... 127:
3446 case 136 ... 1023:
3447 case 1027 ... 1087:
3448 case 1091 ... 1151:
3449 case 1194 ... 1195:
3450 case 1200 ... 1201:
3451 case 1206 ... 1213:
3452 case 1216 ... 1279:
3453 case 1290 ... 1311:
3454 case 1323 ... 1343:
3455 case 1358 ... 1359:
3456 case 1368 ... 1375:
3457 case 1378 ... 1383:
3458 case 1388 ... 1391:
3459 case 1393 ... 1395:
3460 case 1402 ... 1403:
3461 case 1410 ... 1471:
3462 case 1522 ... 1535:
3463 /* reserved region; don't read it */
3464 regbuf[i] = 0;
3465 break;
3466 default:
3467 /* unreserved region */
3468 regbuf[i] = ioread32(hw->hw_addr + (i * sizeof(u32)));
3469 }
3470 }
3471}
3472
Jay Cliburn305282b2008-02-02 19:50:04 -06003473static void atl1_get_ringparam(struct net_device *netdev,
3474 struct ethtool_ringparam *ring)
3475{
3476 struct atl1_adapter *adapter = netdev_priv(netdev);
3477 struct atl1_tpd_ring *txdr = &adapter->tpd_ring;
3478 struct atl1_rfd_ring *rxdr = &adapter->rfd_ring;
3479
3480 ring->rx_max_pending = ATL1_MAX_RFD;
3481 ring->tx_max_pending = ATL1_MAX_TPD;
3482 ring->rx_mini_max_pending = 0;
3483 ring->rx_jumbo_max_pending = 0;
3484 ring->rx_pending = rxdr->count;
3485 ring->tx_pending = txdr->count;
3486 ring->rx_mini_pending = 0;
3487 ring->rx_jumbo_pending = 0;
3488}
3489
3490static int atl1_set_ringparam(struct net_device *netdev,
3491 struct ethtool_ringparam *ring)
3492{
3493 struct atl1_adapter *adapter = netdev_priv(netdev);
3494 struct atl1_tpd_ring *tpdr = &adapter->tpd_ring;
3495 struct atl1_rrd_ring *rrdr = &adapter->rrd_ring;
3496 struct atl1_rfd_ring *rfdr = &adapter->rfd_ring;
3497
3498 struct atl1_tpd_ring tpd_old, tpd_new;
3499 struct atl1_rfd_ring rfd_old, rfd_new;
3500 struct atl1_rrd_ring rrd_old, rrd_new;
3501 struct atl1_ring_header rhdr_old, rhdr_new;
3502 int err;
3503
3504 tpd_old = adapter->tpd_ring;
3505 rfd_old = adapter->rfd_ring;
3506 rrd_old = adapter->rrd_ring;
3507 rhdr_old = adapter->ring_header;
3508
3509 if (netif_running(adapter->netdev))
3510 atl1_down(adapter);
3511
3512 rfdr->count = (u16) max(ring->rx_pending, (u32) ATL1_MIN_RFD);
3513 rfdr->count = rfdr->count > ATL1_MAX_RFD ? ATL1_MAX_RFD :
3514 rfdr->count;
3515 rfdr->count = (rfdr->count + 3) & ~3;
3516 rrdr->count = rfdr->count;
3517
3518 tpdr->count = (u16) max(ring->tx_pending, (u32) ATL1_MIN_TPD);
3519 tpdr->count = tpdr->count > ATL1_MAX_TPD ? ATL1_MAX_TPD :
3520 tpdr->count;
3521 tpdr->count = (tpdr->count + 3) & ~3;
3522
3523 if (netif_running(adapter->netdev)) {
3524 /* try to get new resources before deleting old */
3525 err = atl1_setup_ring_resources(adapter);
3526 if (err)
3527 goto err_setup_ring;
3528
3529 /*
3530 * save the new, restore the old in order to free it,
3531 * then restore the new back again
3532 */
3533
3534 rfd_new = adapter->rfd_ring;
3535 rrd_new = adapter->rrd_ring;
3536 tpd_new = adapter->tpd_ring;
3537 rhdr_new = adapter->ring_header;
3538 adapter->rfd_ring = rfd_old;
3539 adapter->rrd_ring = rrd_old;
3540 adapter->tpd_ring = tpd_old;
3541 adapter->ring_header = rhdr_old;
3542 atl1_free_ring_resources(adapter);
3543 adapter->rfd_ring = rfd_new;
3544 adapter->rrd_ring = rrd_new;
3545 adapter->tpd_ring = tpd_new;
3546 adapter->ring_header = rhdr_new;
3547
3548 err = atl1_up(adapter);
3549 if (err)
3550 return err;
3551 }
3552 return 0;
3553
3554err_setup_ring:
3555 adapter->rfd_ring = rfd_old;
3556 adapter->rrd_ring = rrd_old;
3557 adapter->tpd_ring = tpd_old;
3558 adapter->ring_header = rhdr_old;
3559 atl1_up(adapter);
3560 return err;
3561}
3562
3563static void atl1_get_pauseparam(struct net_device *netdev,
3564 struct ethtool_pauseparam *epause)
3565{
3566 struct atl1_adapter *adapter = netdev_priv(netdev);
3567 struct atl1_hw *hw = &adapter->hw;
3568
3569 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3570 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3571 epause->autoneg = AUTONEG_ENABLE;
3572 } else {
3573 epause->autoneg = AUTONEG_DISABLE;
3574 }
3575 epause->rx_pause = 1;
3576 epause->tx_pause = 1;
3577}
3578
3579static int atl1_set_pauseparam(struct net_device *netdev,
3580 struct ethtool_pauseparam *epause)
3581{
3582 struct atl1_adapter *adapter = netdev_priv(netdev);
3583 struct atl1_hw *hw = &adapter->hw;
3584
3585 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3586 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3587 epause->autoneg = AUTONEG_ENABLE;
3588 } else {
3589 epause->autoneg = AUTONEG_DISABLE;
3590 }
3591
3592 epause->rx_pause = 1;
3593 epause->tx_pause = 1;
3594
3595 return 0;
3596}
3597
3598/* FIXME: is this right? -- CHS */
3599static u32 atl1_get_rx_csum(struct net_device *netdev)
3600{
3601 return 1;
3602}
3603
3604static void atl1_get_strings(struct net_device *netdev, u32 stringset,
3605 u8 *data)
3606{
3607 u8 *p = data;
3608 int i;
3609
3610 switch (stringset) {
3611 case ETH_SS_STATS:
3612 for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3613 memcpy(p, atl1_gstrings_stats[i].stat_string,
3614 ETH_GSTRING_LEN);
3615 p += ETH_GSTRING_LEN;
3616 }
3617 break;
3618 }
3619}
3620
3621static int atl1_nway_reset(struct net_device *netdev)
3622{
3623 struct atl1_adapter *adapter = netdev_priv(netdev);
3624 struct atl1_hw *hw = &adapter->hw;
3625
3626 if (netif_running(netdev)) {
3627 u16 phy_data;
3628 atl1_down(adapter);
3629
3630 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3631 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3632 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3633 } else {
3634 switch (hw->media_type) {
3635 case MEDIA_TYPE_100M_FULL:
3636 phy_data = MII_CR_FULL_DUPLEX |
3637 MII_CR_SPEED_100 | MII_CR_RESET;
3638 break;
3639 case MEDIA_TYPE_100M_HALF:
3640 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3641 break;
3642 case MEDIA_TYPE_10M_FULL:
3643 phy_data = MII_CR_FULL_DUPLEX |
3644 MII_CR_SPEED_10 | MII_CR_RESET;
3645 break;
3646 default:
3647 /* MEDIA_TYPE_10M_HALF */
3648 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3649 }
3650 }
3651 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3652 atl1_up(adapter);
3653 }
3654 return 0;
3655}
3656
3657const struct ethtool_ops atl1_ethtool_ops = {
3658 .get_settings = atl1_get_settings,
3659 .set_settings = atl1_set_settings,
3660 .get_drvinfo = atl1_get_drvinfo,
3661 .get_wol = atl1_get_wol,
3662 .set_wol = atl1_set_wol,
Jay Cliburn460578b2008-02-02 19:50:09 -06003663 .get_msglevel = atl1_get_msglevel,
3664 .set_msglevel = atl1_set_msglevel,
Jay Cliburnc67c9a22008-02-02 19:50:06 -06003665 .get_regs_len = atl1_get_regs_len,
3666 .get_regs = atl1_get_regs,
Jay Cliburn305282b2008-02-02 19:50:04 -06003667 .get_ringparam = atl1_get_ringparam,
3668 .set_ringparam = atl1_set_ringparam,
3669 .get_pauseparam = atl1_get_pauseparam,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06003670 .set_pauseparam = atl1_set_pauseparam,
Jay Cliburn305282b2008-02-02 19:50:04 -06003671 .get_rx_csum = atl1_get_rx_csum,
3672 .set_tx_csum = ethtool_op_set_tx_hw_csum,
3673 .get_link = ethtool_op_get_link,
3674 .set_sg = ethtool_op_set_sg,
3675 .get_strings = atl1_get_strings,
3676 .nway_reset = atl1_nway_reset,
3677 .get_ethtool_stats = atl1_get_ethtool_stats,
3678 .get_sset_count = atl1_get_sset_count,
3679 .set_tso = ethtool_op_set_tso,
3680};