blob: 5f157e09e94dea20ddd7df06dac004c2726b9729 [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:
27 * Xiong Huang <xiong_huang@attansic.com>
28 * Attansic Technology Corp. 3F 147, Xianzheng 9th Road, Zhubei,
29 * Xinzhu 302, TAIWAN, REPUBLIC OF CHINA
30 *
31 * Chris Snook <csnook@redhat.com>
32 * Jay Cliburn <jcliburn@gmail.com>
33 *
34 * This version is adapted from the Attansic reference driver for
35 * inclusion in the Linux kernel. It is currently under heavy development.
36 * A very incomplete list of things that need to be dealt with:
37 *
38 * TODO:
Jay Cliburn53ffb422007-07-15 11:03:27 -050039 * Add more ethtool functions.
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050040 * Fix abstruse irq enable/disable condition described here:
41 * http://marc.theaimsgroup.com/?l=linux-netdev&m=116398508500553&w=2
42 *
43 * NEEDS TESTING:
44 * VLAN
45 * multicast
46 * promiscuous mode
47 * interrupt coalescing
48 * SMP torture testing
49 */
50
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050051#include <asm/atomic.h>
52#include <asm/byteorder.h>
53
Jay Cliburn305282b2008-02-02 19:50:04 -060054#include <linux/compiler.h>
55#include <linux/crc32.h>
56#include <linux/delay.h>
57#include <linux/dma-mapping.h>
58#include <linux/etherdevice.h>
59#include <linux/hardirq.h>
60#include <linux/if_ether.h>
61#include <linux/if_vlan.h>
62#include <linux/in.h>
63#include <linux/interrupt.h>
64#include <linux/ip.h>
65#include <linux/irqflags.h>
66#include <linux/irqreturn.h>
67#include <linux/jiffies.h>
68#include <linux/mii.h>
69#include <linux/module.h>
70#include <linux/moduleparam.h>
71#include <linux/net.h>
72#include <linux/netdevice.h>
73#include <linux/pci.h>
74#include <linux/pci_ids.h>
75#include <linux/pm.h>
76#include <linux/skbuff.h>
77#include <linux/slab.h>
78#include <linux/spinlock.h>
79#include <linux/string.h>
80#include <linux/tcp.h>
81#include <linux/timer.h>
82#include <linux/types.h>
83#include <linux/workqueue.h>
84
85#include <net/checksum.h>
86
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050087#include "atl1.h"
88
Jay Cliburn305282b2008-02-02 19:50:04 -060089/* Temporary hack for merging atl1 and atl2 */
90#include "atlx.c"
Jay Cliburnf3cc28c2007-02-08 10:42:37 -050091
92/*
Chris Snook8ec72262008-04-18 21:51:53 -040093 * This is the only thing that needs to be changed to adjust the
94 * maximum number of ports that the driver can manage.
95 */
96#define ATL1_MAX_NIC 4
97
98#define OPTION_UNSET -1
99#define OPTION_DISABLED 0
100#define OPTION_ENABLED 1
101
102#define ATL1_PARAM_INIT { [0 ... ATL1_MAX_NIC] = OPTION_UNSET }
103
104/*
105 * Interrupt Moderate Timer in units of 2 us
106 *
107 * Valid Range: 10-65535
108 *
109 * Default Value: 100 (200us)
110 */
111static int __devinitdata int_mod_timer[ATL1_MAX_NIC+1] = ATL1_PARAM_INIT;
112static int num_int_mod_timer;
113module_param_array_named(int_mod_timer, int_mod_timer, int,
114 &num_int_mod_timer, 0);
115MODULE_PARM_DESC(int_mod_timer, "Interrupt moderator timer");
116
117#define DEFAULT_INT_MOD_CNT 100 /* 200us */
118#define MAX_INT_MOD_CNT 65000
119#define MIN_INT_MOD_CNT 50
120
121struct atl1_option {
122 enum { enable_option, range_option, list_option } type;
123 char *name;
124 char *err;
125 int def;
126 union {
127 struct { /* range_option info */
128 int min;
129 int max;
130 } r;
131 struct { /* list_option info */
132 int nr;
133 struct atl1_opt_list {
134 int i;
135 char *str;
136 } *p;
137 } l;
138 } arg;
139};
140
141static int __devinit atl1_validate_option(int *value, struct atl1_option *opt,
142 struct pci_dev *pdev)
143{
144 if (*value == OPTION_UNSET) {
145 *value = opt->def;
146 return 0;
147 }
148
149 switch (opt->type) {
150 case enable_option:
151 switch (*value) {
152 case OPTION_ENABLED:
153 dev_info(&pdev->dev, "%s enabled\n", opt->name);
154 return 0;
155 case OPTION_DISABLED:
156 dev_info(&pdev->dev, "%s disabled\n", opt->name);
157 return 0;
158 }
159 break;
160 case range_option:
161 if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
162 dev_info(&pdev->dev, "%s set to %i\n", opt->name,
163 *value);
164 return 0;
165 }
166 break;
167 case list_option:{
168 int i;
169 struct atl1_opt_list *ent;
170
171 for (i = 0; i < opt->arg.l.nr; i++) {
172 ent = &opt->arg.l.p[i];
173 if (*value == ent->i) {
174 if (ent->str[0] != '\0')
175 dev_info(&pdev->dev, "%s\n",
176 ent->str);
177 return 0;
178 }
179 }
180 }
181 break;
182
183 default:
184 break;
185 }
186
187 dev_info(&pdev->dev, "invalid %s specified (%i) %s\n",
188 opt->name, *value, opt->err);
189 *value = opt->def;
190 return -1;
191}
192
193/*
194 * atl1_check_options - Range Checking for Command Line Parameters
195 * @adapter: board private structure
196 *
197 * This routine checks all command line parameters for valid user
198 * input. If an invalid value is given, or if no user specified
199 * value exists, a default value is used. The final value is stored
200 * in a variable in the adapter structure.
201 */
202void __devinit atl1_check_options(struct atl1_adapter *adapter)
203{
204 struct pci_dev *pdev = adapter->pdev;
205 int bd = adapter->bd_number;
206 if (bd >= ATL1_MAX_NIC) {
207 dev_notice(&pdev->dev, "no configuration for board#%i\n", bd);
208 dev_notice(&pdev->dev, "using defaults for all values\n");
209 }
210 { /* Interrupt Moderate Timer */
211 struct atl1_option opt = {
212 .type = range_option,
213 .name = "Interrupt Moderator Timer",
214 .err = "using default of "
215 __MODULE_STRING(DEFAULT_INT_MOD_CNT),
216 .def = DEFAULT_INT_MOD_CNT,
217 .arg = {.r = {.min = MIN_INT_MOD_CNT,
218 .max = MAX_INT_MOD_CNT} }
219 };
220 int val;
221 if (num_int_mod_timer > bd) {
222 val = int_mod_timer[bd];
223 atl1_validate_option(&val, &opt, pdev);
224 adapter->imt = (u16) val;
225 } else
226 adapter->imt = (u16) (opt.def);
227 }
228}
229
230/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500231 * atl1_pci_tbl - PCI Device ID Table
232 */
233static const struct pci_device_id atl1_pci_tbl[] = {
Chris Snooke81e5572007-02-14 20:17:01 -0600234 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1)},
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500235 /* required last entry */
236 {0,}
237};
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500238MODULE_DEVICE_TABLE(pci, atl1_pci_tbl);
239
Jay Cliburn460578b2008-02-02 19:50:09 -0600240static const u32 atl1_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
241 NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
242
243static int debug = -1;
244module_param(debug, int, 0);
245MODULE_PARM_DESC(debug, "Message level (0=none,...,16=all)");
246
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500247/*
Jay Cliburn6446a862008-02-02 19:50:12 -0600248 * Reset the transmit and receive units; mask and clear all interrupts.
249 * hw - Struct containing variables accessed by shared code
250 * return : 0 or idle status (if error)
251 */
252static s32 atl1_reset_hw(struct atl1_hw *hw)
253{
254 struct pci_dev *pdev = hw->back->pdev;
255 struct atl1_adapter *adapter = hw->back;
256 u32 icr;
257 int i;
258
259 /*
260 * Clear Interrupt mask to stop board from generating
261 * interrupts & Clear any pending interrupt events
262 */
263 /*
264 * iowrite32(0, hw->hw_addr + REG_IMR);
265 * iowrite32(0xffffffff, hw->hw_addr + REG_ISR);
266 */
267
268 /*
269 * Issue Soft Reset to the MAC. This will reset the chip's
270 * transmit, receive, DMA. It will not effect
271 * the current PCI configuration. The global reset bit is self-
272 * clearing, and should clear within a microsecond.
273 */
274 iowrite32(MASTER_CTRL_SOFT_RST, hw->hw_addr + REG_MASTER_CTRL);
275 ioread32(hw->hw_addr + REG_MASTER_CTRL);
276
277 iowrite16(1, hw->hw_addr + REG_PHY_ENABLE);
278 ioread16(hw->hw_addr + REG_PHY_ENABLE);
279
280 /* delay about 1ms */
281 msleep(1);
282
283 /* Wait at least 10ms for All module to be Idle */
284 for (i = 0; i < 10; i++) {
285 icr = ioread32(hw->hw_addr + REG_IDLE_STATUS);
286 if (!icr)
287 break;
288 /* delay 1 ms */
289 msleep(1);
290 /* FIXME: still the right way to do this? */
291 cpu_relax();
292 }
293
294 if (icr) {
295 if (netif_msg_hw(adapter))
296 dev_dbg(&pdev->dev, "ICR = 0x%x\n", icr);
297 return icr;
298 }
299
300 return 0;
301}
302
303/* function about EEPROM
304 *
305 * check_eeprom_exist
306 * return 0 if eeprom exist
307 */
308static int atl1_check_eeprom_exist(struct atl1_hw *hw)
309{
310 u32 value;
311 value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
312 if (value & SPI_FLASH_CTRL_EN_VPD) {
313 value &= ~SPI_FLASH_CTRL_EN_VPD;
314 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
315 }
316
317 value = ioread16(hw->hw_addr + REG_PCIE_CAP_LIST);
318 return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
319}
320
321static bool atl1_read_eeprom(struct atl1_hw *hw, u32 offset, u32 *p_value)
322{
323 int i;
324 u32 control;
325
326 if (offset & 3)
327 /* address do not align */
328 return false;
329
330 iowrite32(0, hw->hw_addr + REG_VPD_DATA);
331 control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
332 iowrite32(control, hw->hw_addr + REG_VPD_CAP);
333 ioread32(hw->hw_addr + REG_VPD_CAP);
334
335 for (i = 0; i < 10; i++) {
336 msleep(2);
337 control = ioread32(hw->hw_addr + REG_VPD_CAP);
338 if (control & VPD_CAP_VPD_FLAG)
339 break;
340 }
341 if (control & VPD_CAP_VPD_FLAG) {
342 *p_value = ioread32(hw->hw_addr + REG_VPD_DATA);
343 return true;
344 }
345 /* timeout */
346 return false;
347}
348
349/*
350 * Reads the value from a PHY register
351 * hw - Struct containing variables accessed by shared code
352 * reg_addr - address of the PHY register to read
353 */
354s32 atl1_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data)
355{
356 u32 val;
357 int i;
358
359 val = ((u32) (reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
360 MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW | MDIO_CLK_25_4 <<
361 MDIO_CLK_SEL_SHIFT;
362 iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
363 ioread32(hw->hw_addr + REG_MDIO_CTRL);
364
365 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
366 udelay(2);
367 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
368 if (!(val & (MDIO_START | MDIO_BUSY)))
369 break;
370 }
371 if (!(val & (MDIO_START | MDIO_BUSY))) {
372 *phy_data = (u16) val;
373 return 0;
374 }
375 return ATLX_ERR_PHY;
376}
377
378#define CUSTOM_SPI_CS_SETUP 2
379#define CUSTOM_SPI_CLK_HI 2
380#define CUSTOM_SPI_CLK_LO 2
381#define CUSTOM_SPI_CS_HOLD 2
382#define CUSTOM_SPI_CS_HI 3
383
384static bool atl1_spi_read(struct atl1_hw *hw, u32 addr, u32 *buf)
385{
386 int i;
387 u32 value;
388
389 iowrite32(0, hw->hw_addr + REG_SPI_DATA);
390 iowrite32(addr, hw->hw_addr + REG_SPI_ADDR);
391
392 value = SPI_FLASH_CTRL_WAIT_READY |
393 (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
394 SPI_FLASH_CTRL_CS_SETUP_SHIFT | (CUSTOM_SPI_CLK_HI &
395 SPI_FLASH_CTRL_CLK_HI_MASK) <<
396 SPI_FLASH_CTRL_CLK_HI_SHIFT | (CUSTOM_SPI_CLK_LO &
397 SPI_FLASH_CTRL_CLK_LO_MASK) <<
398 SPI_FLASH_CTRL_CLK_LO_SHIFT | (CUSTOM_SPI_CS_HOLD &
399 SPI_FLASH_CTRL_CS_HOLD_MASK) <<
400 SPI_FLASH_CTRL_CS_HOLD_SHIFT | (CUSTOM_SPI_CS_HI &
401 SPI_FLASH_CTRL_CS_HI_MASK) <<
402 SPI_FLASH_CTRL_CS_HI_SHIFT | (1 & SPI_FLASH_CTRL_INS_MASK) <<
403 SPI_FLASH_CTRL_INS_SHIFT;
404
405 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
406
407 value |= SPI_FLASH_CTRL_START;
408 iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
409 ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
410
411 for (i = 0; i < 10; i++) {
412 msleep(1);
413 value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
414 if (!(value & SPI_FLASH_CTRL_START))
415 break;
416 }
417
418 if (value & SPI_FLASH_CTRL_START)
419 return false;
420
421 *buf = ioread32(hw->hw_addr + REG_SPI_DATA);
422
423 return true;
424}
425
426/*
427 * get_permanent_address
428 * return 0 if get valid mac address,
429 */
430static int atl1_get_permanent_address(struct atl1_hw *hw)
431{
432 u32 addr[2];
433 u32 i, control;
434 u16 reg;
435 u8 eth_addr[ETH_ALEN];
436 bool key_valid;
437
438 if (is_valid_ether_addr(hw->perm_mac_addr))
439 return 0;
440
441 /* init */
442 addr[0] = addr[1] = 0;
443
444 if (!atl1_check_eeprom_exist(hw)) {
445 reg = 0;
446 key_valid = false;
447 /* Read out all EEPROM content */
448 i = 0;
449 while (1) {
450 if (atl1_read_eeprom(hw, i + 0x100, &control)) {
451 if (key_valid) {
452 if (reg == REG_MAC_STA_ADDR)
453 addr[0] = control;
454 else if (reg == (REG_MAC_STA_ADDR + 4))
455 addr[1] = control;
456 key_valid = false;
457 } else if ((control & 0xff) == 0x5A) {
458 key_valid = true;
459 reg = (u16) (control >> 16);
460 } else
461 break;
462 } else
463 /* read error */
464 break;
465 i += 4;
466 }
467
468 *(u32 *) &eth_addr[2] = swab32(addr[0]);
469 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
470 if (is_valid_ether_addr(eth_addr)) {
471 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
472 return 0;
473 }
Jay Cliburn6446a862008-02-02 19:50:12 -0600474 }
475
476 /* see if SPI FLAGS exist ? */
477 addr[0] = addr[1] = 0;
478 reg = 0;
479 key_valid = false;
480 i = 0;
481 while (1) {
482 if (atl1_spi_read(hw, i + 0x1f000, &control)) {
483 if (key_valid) {
484 if (reg == REG_MAC_STA_ADDR)
485 addr[0] = control;
486 else if (reg == (REG_MAC_STA_ADDR + 4))
487 addr[1] = control;
488 key_valid = false;
489 } else if ((control & 0xff) == 0x5A) {
490 key_valid = true;
491 reg = (u16) (control >> 16);
492 } else
493 /* data end */
494 break;
495 } else
496 /* read error */
497 break;
498 i += 4;
499 }
500
501 *(u32 *) &eth_addr[2] = swab32(addr[0]);
502 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
503 if (is_valid_ether_addr(eth_addr)) {
504 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
505 return 0;
506 }
507
508 /*
509 * On some motherboards, the MAC address is written by the
510 * BIOS directly to the MAC register during POST, and is
511 * not stored in eeprom. If all else thus far has failed
512 * to fetch the permanent MAC address, try reading it directly.
513 */
514 addr[0] = ioread32(hw->hw_addr + REG_MAC_STA_ADDR);
515 addr[1] = ioread16(hw->hw_addr + (REG_MAC_STA_ADDR + 4));
516 *(u32 *) &eth_addr[2] = swab32(addr[0]);
517 *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
518 if (is_valid_ether_addr(eth_addr)) {
519 memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
520 return 0;
521 }
522
523 return 1;
524}
525
526/*
527 * Reads the adapter's MAC address from the EEPROM
528 * hw - Struct containing variables accessed by shared code
529 */
530s32 atl1_read_mac_addr(struct atl1_hw *hw)
531{
532 u16 i;
533
534 if (atl1_get_permanent_address(hw))
535 random_ether_addr(hw->perm_mac_addr);
536
537 for (i = 0; i < ETH_ALEN; i++)
538 hw->mac_addr[i] = hw->perm_mac_addr[i];
539 return 0;
540}
541
542/*
543 * Hashes an address to determine its location in the multicast table
544 * hw - Struct containing variables accessed by shared code
545 * mc_addr - the multicast address to hash
546 *
547 * atl1_hash_mc_addr
548 * purpose
549 * set hash value for a multicast address
550 * hash calcu processing :
551 * 1. calcu 32bit CRC for multicast address
552 * 2. reverse crc with MSB to LSB
553 */
554u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
555{
556 u32 crc32, value = 0;
557 int i;
558
559 crc32 = ether_crc_le(6, mc_addr);
560 for (i = 0; i < 32; i++)
561 value |= (((crc32 >> i) & 1) << (31 - i));
562
563 return value;
564}
565
566/*
567 * Sets the bit in the multicast table corresponding to the hash value.
568 * hw - Struct containing variables accessed by shared code
569 * hash_value - Multicast address hash value
570 */
571void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
572{
573 u32 hash_bit, hash_reg;
574 u32 mta;
575
576 /*
577 * The HASH Table is a register array of 2 32-bit registers.
578 * It is treated like an array of 64 bits. We want to set
579 * bit BitArray[hash_value]. So we figure out what register
580 * the bit is in, read it, OR in the new bit, then write
581 * back the new value. The register is determined by the
582 * upper 7 bits of the hash value and the bit within that
583 * register are determined by the lower 5 bits of the value.
584 */
585 hash_reg = (hash_value >> 31) & 0x1;
586 hash_bit = (hash_value >> 26) & 0x1F;
587 mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
588 mta |= (1 << hash_bit);
589 iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
590}
591
592/*
593 * Writes a value to a PHY register
594 * hw - Struct containing variables accessed by shared code
595 * reg_addr - address of the PHY register to write
596 * data - data to write to the PHY
597 */
598static s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
599{
600 int i;
601 u32 val;
602
603 val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
604 (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
605 MDIO_SUP_PREAMBLE |
606 MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
607 iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
608 ioread32(hw->hw_addr + REG_MDIO_CTRL);
609
610 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
611 udelay(2);
612 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
613 if (!(val & (MDIO_START | MDIO_BUSY)))
614 break;
615 }
616
617 if (!(val & (MDIO_START | MDIO_BUSY)))
618 return 0;
619
620 return ATLX_ERR_PHY;
621}
622
623/*
624 * Make L001's PHY out of Power Saving State (bug)
625 * hw - Struct containing variables accessed by shared code
626 * when power on, L001's PHY always on Power saving State
627 * (Gigabit Link forbidden)
628 */
629static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
630{
631 s32 ret;
632 ret = atl1_write_phy_reg(hw, 29, 0x0029);
633 if (ret)
634 return ret;
635 return atl1_write_phy_reg(hw, 30, 0);
636}
637
638/*
Jay Cliburn6446a862008-02-02 19:50:12 -0600639 * Resets the PHY and make all config validate
640 * hw - Struct containing variables accessed by shared code
641 *
642 * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
643 */
644static s32 atl1_phy_reset(struct atl1_hw *hw)
645{
646 struct pci_dev *pdev = hw->back->pdev;
647 struct atl1_adapter *adapter = hw->back;
648 s32 ret_val;
649 u16 phy_data;
650
651 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
652 hw->media_type == MEDIA_TYPE_1000M_FULL)
653 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
654 else {
655 switch (hw->media_type) {
656 case MEDIA_TYPE_100M_FULL:
657 phy_data =
658 MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
659 MII_CR_RESET;
660 break;
661 case MEDIA_TYPE_100M_HALF:
662 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
663 break;
664 case MEDIA_TYPE_10M_FULL:
665 phy_data =
666 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
667 break;
668 default:
669 /* MEDIA_TYPE_10M_HALF: */
670 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
671 break;
672 }
673 }
674
675 ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
676 if (ret_val) {
677 u32 val;
678 int i;
679 /* pcie serdes link may be down! */
680 if (netif_msg_hw(adapter))
681 dev_dbg(&pdev->dev, "pcie phy link down\n");
682
683 for (i = 0; i < 25; i++) {
684 msleep(1);
685 val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
686 if (!(val & (MDIO_START | MDIO_BUSY)))
687 break;
688 }
689
690 if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
691 if (netif_msg_hw(adapter))
692 dev_warn(&pdev->dev,
693 "pcie link down at least 25ms\n");
694 return ret_val;
695 }
696 }
697 return 0;
698}
699
700/*
701 * Configures PHY autoneg and flow control advertisement settings
702 * hw - Struct containing variables accessed by shared code
703 */
704static s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
705{
706 s32 ret_val;
707 s16 mii_autoneg_adv_reg;
708 s16 mii_1000t_ctrl_reg;
709
710 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
711 mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
712
713 /* Read the MII 1000Base-T Control Register (Address 9). */
714 mii_1000t_ctrl_reg = MII_ATLX_CR_1000T_DEFAULT_CAP_MASK;
715
716 /*
717 * First we clear all the 10/100 mb speed bits in the Auto-Neg
718 * Advertisement Register (Address 4) and the 1000 mb speed bits in
719 * the 1000Base-T Control Register (Address 9).
720 */
721 mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
722 mii_1000t_ctrl_reg &= ~MII_ATLX_CR_1000T_SPEED_MASK;
723
724 /*
725 * Need to parse media_type and set up
726 * the appropriate PHY registers.
727 */
728 switch (hw->media_type) {
729 case MEDIA_TYPE_AUTO_SENSOR:
730 mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
731 MII_AR_10T_FD_CAPS |
732 MII_AR_100TX_HD_CAPS |
733 MII_AR_100TX_FD_CAPS);
734 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
735 break;
736
737 case MEDIA_TYPE_1000M_FULL:
738 mii_1000t_ctrl_reg |= MII_ATLX_CR_1000T_FD_CAPS;
739 break;
740
741 case MEDIA_TYPE_100M_FULL:
742 mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
743 break;
744
745 case MEDIA_TYPE_100M_HALF:
746 mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
747 break;
748
749 case MEDIA_TYPE_10M_FULL:
750 mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
751 break;
752
753 default:
754 mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
755 break;
756 }
757
758 /* flow control fixed to enable all */
759 mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
760
761 hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
762 hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
763
764 ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
765 if (ret_val)
766 return ret_val;
767
768 ret_val = atl1_write_phy_reg(hw, MII_ATLX_CR, mii_1000t_ctrl_reg);
769 if (ret_val)
770 return ret_val;
771
772 return 0;
773}
774
775/*
776 * Configures link settings.
777 * hw - Struct containing variables accessed by shared code
778 * Assumes the hardware has previously been reset and the
779 * transmitter and receiver are not enabled.
780 */
781static s32 atl1_setup_link(struct atl1_hw *hw)
782{
783 struct pci_dev *pdev = hw->back->pdev;
784 struct atl1_adapter *adapter = hw->back;
785 s32 ret_val;
786
787 /*
788 * Options:
789 * PHY will advertise value(s) parsed from
790 * autoneg_advertised and fc
791 * no matter what autoneg is , We will not wait link result.
792 */
793 ret_val = atl1_phy_setup_autoneg_adv(hw);
794 if (ret_val) {
795 if (netif_msg_link(adapter))
796 dev_dbg(&pdev->dev,
797 "error setting up autonegotiation\n");
798 return ret_val;
799 }
800 /* SW.Reset , En-Auto-Neg if needed */
801 ret_val = atl1_phy_reset(hw);
802 if (ret_val) {
803 if (netif_msg_link(adapter))
804 dev_dbg(&pdev->dev, "error resetting phy\n");
805 return ret_val;
806 }
807 hw->phy_configured = true;
808 return ret_val;
809}
810
811static void atl1_init_flash_opcode(struct atl1_hw *hw)
812{
813 if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
814 /* Atmel */
815 hw->flash_vendor = 0;
816
817 /* Init OP table */
818 iowrite8(flash_table[hw->flash_vendor].cmd_program,
819 hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
820 iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
821 hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
822 iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
823 hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
824 iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
825 hw->hw_addr + REG_SPI_FLASH_OP_RDID);
826 iowrite8(flash_table[hw->flash_vendor].cmd_wren,
827 hw->hw_addr + REG_SPI_FLASH_OP_WREN);
828 iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
829 hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
830 iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
831 hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
832 iowrite8(flash_table[hw->flash_vendor].cmd_read,
833 hw->hw_addr + REG_SPI_FLASH_OP_READ);
834}
835
836/*
837 * Performs basic configuration of the adapter.
838 * hw - Struct containing variables accessed by shared code
839 * Assumes that the controller has previously been reset and is in a
840 * post-reset uninitialized state. Initializes multicast table,
841 * and Calls routines to setup link
842 * Leaves the transmit and receive units disabled and uninitialized.
843 */
844static s32 atl1_init_hw(struct atl1_hw *hw)
845{
846 u32 ret_val = 0;
847
848 /* Zero out the Multicast HASH table */
849 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
850 /* clear the old settings from the multicast hash table */
851 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
852
853 atl1_init_flash_opcode(hw);
854
855 if (!hw->phy_configured) {
856 /* enable GPHY LinkChange Interrrupt */
857 ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
858 if (ret_val)
859 return ret_val;
860 /* make PHY out of power-saving state */
861 ret_val = atl1_phy_leave_power_saving(hw);
862 if (ret_val)
863 return ret_val;
864 /* Call a subroutine to configure the link */
865 ret_val = atl1_setup_link(hw);
866 }
867 return ret_val;
868}
869
870/*
871 * Detects the current speed and duplex settings of the hardware.
872 * hw - Struct containing variables accessed by shared code
873 * speed - Speed of the connection
874 * duplex - Duplex setting of the connection
875 */
876static s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
877{
878 struct pci_dev *pdev = hw->back->pdev;
879 struct atl1_adapter *adapter = hw->back;
880 s32 ret_val;
881 u16 phy_data;
882
883 /* ; --- Read PHY Specific Status Register (17) */
884 ret_val = atl1_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
885 if (ret_val)
886 return ret_val;
887
888 if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
889 return ATLX_ERR_PHY_RES;
890
891 switch (phy_data & MII_ATLX_PSSR_SPEED) {
892 case MII_ATLX_PSSR_1000MBS:
893 *speed = SPEED_1000;
894 break;
895 case MII_ATLX_PSSR_100MBS:
896 *speed = SPEED_100;
897 break;
898 case MII_ATLX_PSSR_10MBS:
899 *speed = SPEED_10;
900 break;
901 default:
902 if (netif_msg_hw(adapter))
903 dev_dbg(&pdev->dev, "error getting speed\n");
904 return ATLX_ERR_PHY_SPEED;
905 break;
906 }
907 if (phy_data & MII_ATLX_PSSR_DPLX)
908 *duplex = FULL_DUPLEX;
909 else
910 *duplex = HALF_DUPLEX;
911
912 return 0;
913}
914
915void atl1_set_mac_addr(struct atl1_hw *hw)
916{
917 u32 value;
918 /*
919 * 00-0B-6A-F6-00-DC
920 * 0: 6AF600DC 1: 000B
921 * low dword
922 */
923 value = (((u32) hw->mac_addr[2]) << 24) |
924 (((u32) hw->mac_addr[3]) << 16) |
925 (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
926 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
927 /* high dword */
928 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
929 iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
930}
931
932/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500933 * atl1_sw_init - Initialize general software structures (struct atl1_adapter)
934 * @adapter: board private structure to initialize
935 *
936 * atl1_sw_init initializes the Adapter private data structure.
937 * Fields are initialized based on PCI device information and
938 * OS network device settings (MTU size).
939 */
940static int __devinit atl1_sw_init(struct atl1_adapter *adapter)
941{
942 struct atl1_hw *hw = &adapter->hw;
943 struct net_device *netdev = adapter->netdev;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500944
Jay Cliburn2a491282008-01-14 19:56:41 -0600945 hw->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Jay Cliburna3093d92007-07-19 18:45:14 -0500946 hw->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500947
948 adapter->wol = 0;
949 adapter->rx_buffer_len = (hw->max_frame_size + 7) & ~7;
Jay Cliburn305282b2008-02-02 19:50:04 -0600950 adapter->ict = 50000; /* 100ms */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500951 adapter->link_speed = SPEED_0; /* hardware init */
952 adapter->link_duplex = FULL_DUPLEX;
953
954 hw->phy_configured = false;
955 hw->preamble_len = 7;
956 hw->ipgt = 0x60;
957 hw->min_ifg = 0x50;
958 hw->ipgr1 = 0x40;
959 hw->ipgr2 = 0x60;
960 hw->max_retry = 0xf;
961 hw->lcol = 0x37;
962 hw->jam_ipg = 7;
963 hw->rfd_burst = 8;
964 hw->rrd_burst = 8;
965 hw->rfd_fetch_gap = 1;
966 hw->rx_jumbo_th = adapter->rx_buffer_len / 8;
967 hw->rx_jumbo_lkah = 1;
968 hw->rrd_ret_timer = 16;
969 hw->tpd_burst = 4;
970 hw->tpd_fetch_th = 16;
971 hw->txf_burst = 0x100;
972 hw->tx_jumbo_task_th = (hw->max_frame_size + 7) >> 3;
973 hw->tpd_fetch_gap = 1;
974 hw->rcb_value = atl1_rcb_64;
975 hw->dma_ord = atl1_dma_ord_enh;
976 hw->dmar_block = atl1_dma_req_256;
977 hw->dmaw_block = atl1_dma_req_256;
978 hw->cmb_rrd = 4;
979 hw->cmb_tpd = 4;
980 hw->cmb_rx_timer = 1; /* about 2us */
981 hw->cmb_tx_timer = 1; /* about 2us */
982 hw->smb_timer = 100000; /* about 200ms */
983
Jay Cliburnf3cc28c2007-02-08 10:42:37 -0500984 spin_lock_init(&adapter->lock);
985 spin_lock_init(&adapter->mb_lock);
986
987 return 0;
988}
989
Jay Cliburn05ffdd72007-07-15 11:03:29 -0500990static int mdio_read(struct net_device *netdev, int phy_id, int reg_num)
991{
992 struct atl1_adapter *adapter = netdev_priv(netdev);
993 u16 result;
994
995 atl1_read_phy_reg(&adapter->hw, reg_num & 0x1f, &result);
996
997 return result;
998}
999
1000static void mdio_write(struct net_device *netdev, int phy_id, int reg_num,
1001 int val)
1002{
1003 struct atl1_adapter *adapter = netdev_priv(netdev);
1004
1005 atl1_write_phy_reg(&adapter->hw, reg_num, val);
1006}
1007
1008/*
1009 * atl1_mii_ioctl -
1010 * @netdev:
1011 * @ifreq:
1012 * @cmd:
1013 */
1014static int atl1_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1015{
1016 struct atl1_adapter *adapter = netdev_priv(netdev);
1017 unsigned long flags;
1018 int retval;
1019
1020 if (!netif_running(netdev))
1021 return -EINVAL;
1022
1023 spin_lock_irqsave(&adapter->lock, flags);
1024 retval = generic_mii_ioctl(&adapter->mii, if_mii(ifr), cmd, NULL);
1025 spin_unlock_irqrestore(&adapter->lock, flags);
1026
1027 return retval;
1028}
1029
1030/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001031 * atl1_setup_mem_resources - allocate Tx / RX descriptor resources
1032 * @adapter: board private structure
1033 *
1034 * Return 0 on success, negative on failure
1035 */
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06001036static s32 atl1_setup_ring_resources(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001037{
1038 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1039 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1040 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1041 struct atl1_ring_header *ring_header = &adapter->ring_header;
1042 struct pci_dev *pdev = adapter->pdev;
1043 int size;
1044 u8 offset = 0;
1045
1046 size = sizeof(struct atl1_buffer) * (tpd_ring->count + rfd_ring->count);
1047 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
1048 if (unlikely(!tpd_ring->buffer_info)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06001049 if (netif_msg_drv(adapter))
1050 dev_err(&pdev->dev, "kzalloc failed , size = D%d\n",
1051 size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001052 goto err_nomem;
1053 }
1054 rfd_ring->buffer_info =
Jay Cliburn53ffb422007-07-15 11:03:27 -05001055 (struct atl1_buffer *)(tpd_ring->buffer_info + tpd_ring->count);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001056
Jay Cliburn305282b2008-02-02 19:50:04 -06001057 /*
1058 * real ring DMA buffer
Jay Cliburn53ffb422007-07-15 11:03:27 -05001059 * each ring/block may need up to 8 bytes for alignment, hence the
1060 * additional 40 bytes tacked onto the end.
1061 */
1062 ring_header->size = size =
1063 sizeof(struct tx_packet_desc) * tpd_ring->count
1064 + sizeof(struct rx_free_desc) * rfd_ring->count
1065 + sizeof(struct rx_return_desc) * rrd_ring->count
1066 + sizeof(struct coals_msg_block)
1067 + sizeof(struct stats_msg_block)
1068 + 40;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001069
1070 ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
Jay Cliburn53ffb422007-07-15 11:03:27 -05001071 &ring_header->dma);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001072 if (unlikely(!ring_header->desc)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06001073 if (netif_msg_drv(adapter))
1074 dev_err(&pdev->dev, "pci_alloc_consistent failed\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001075 goto err_nomem;
1076 }
1077
1078 memset(ring_header->desc, 0, ring_header->size);
1079
1080 /* init TPD ring */
1081 tpd_ring->dma = ring_header->dma;
1082 offset = (tpd_ring->dma & 0x7) ? (8 - (ring_header->dma & 0x7)) : 0;
1083 tpd_ring->dma += offset;
1084 tpd_ring->desc = (u8 *) ring_header->desc + offset;
1085 tpd_ring->size = sizeof(struct tx_packet_desc) * tpd_ring->count;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001086
1087 /* init RFD ring */
1088 rfd_ring->dma = tpd_ring->dma + tpd_ring->size;
1089 offset = (rfd_ring->dma & 0x7) ? (8 - (rfd_ring->dma & 0x7)) : 0;
1090 rfd_ring->dma += offset;
1091 rfd_ring->desc = (u8 *) tpd_ring->desc + (tpd_ring->size + offset);
1092 rfd_ring->size = sizeof(struct rx_free_desc) * rfd_ring->count;
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001093
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001094
1095 /* init RRD ring */
1096 rrd_ring->dma = rfd_ring->dma + rfd_ring->size;
1097 offset = (rrd_ring->dma & 0x7) ? (8 - (rrd_ring->dma & 0x7)) : 0;
1098 rrd_ring->dma += offset;
1099 rrd_ring->desc = (u8 *) rfd_ring->desc + (rfd_ring->size + offset);
1100 rrd_ring->size = sizeof(struct rx_return_desc) * rrd_ring->count;
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001101
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001102
1103 /* init CMB */
1104 adapter->cmb.dma = rrd_ring->dma + rrd_ring->size;
1105 offset = (adapter->cmb.dma & 0x7) ? (8 - (adapter->cmb.dma & 0x7)) : 0;
1106 adapter->cmb.dma += offset;
Jay Cliburn53ffb422007-07-15 11:03:27 -05001107 adapter->cmb.cmb = (struct coals_msg_block *)
1108 ((u8 *) rrd_ring->desc + (rrd_ring->size + offset));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001109
1110 /* init SMB */
1111 adapter->smb.dma = adapter->cmb.dma + sizeof(struct coals_msg_block);
1112 offset = (adapter->smb.dma & 0x7) ? (8 - (adapter->smb.dma & 0x7)) : 0;
1113 adapter->smb.dma += offset;
1114 adapter->smb.smb = (struct stats_msg_block *)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001115 ((u8 *) adapter->cmb.cmb +
1116 (sizeof(struct coals_msg_block) + offset));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001117
Jay Cliburn305282b2008-02-02 19:50:04 -06001118 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001119
1120err_nomem:
1121 kfree(tpd_ring->buffer_info);
1122 return -ENOMEM;
1123}
1124
Chris Snook3d2557f2007-07-23 16:38:39 -04001125static void atl1_init_ring_ptrs(struct atl1_adapter *adapter)
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001126{
1127 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1128 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1129 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1130
1131 atomic_set(&tpd_ring->next_to_use, 0);
1132 atomic_set(&tpd_ring->next_to_clean, 0);
1133
1134 rfd_ring->next_to_clean = 0;
1135 atomic_set(&rfd_ring->next_to_use, 0);
1136
1137 rrd_ring->next_to_use = 0;
1138 atomic_set(&rrd_ring->next_to_clean, 0);
1139}
1140
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001141/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001142 * atl1_clean_rx_ring - Free RFD Buffers
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001143 * @adapter: board private structure
1144 */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001145static void atl1_clean_rx_ring(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001146{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001147 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1148 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1149 struct atl1_buffer *buffer_info;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001150 struct pci_dev *pdev = adapter->pdev;
1151 unsigned long size;
1152 unsigned int i;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001153
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001154 /* Free all the Rx ring sk_buffs */
1155 for (i = 0; i < rfd_ring->count; i++) {
1156 buffer_info = &rfd_ring->buffer_info[i];
1157 if (buffer_info->dma) {
1158 pci_unmap_page(pdev, buffer_info->dma,
1159 buffer_info->length, PCI_DMA_FROMDEVICE);
1160 buffer_info->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001161 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001162 if (buffer_info->skb) {
1163 dev_kfree_skb(buffer_info->skb);
1164 buffer_info->skb = NULL;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001165 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001166 }
1167
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001168 size = sizeof(struct atl1_buffer) * rfd_ring->count;
1169 memset(rfd_ring->buffer_info, 0, size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001170
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001171 /* Zero out the descriptor ring */
1172 memset(rfd_ring->desc, 0, rfd_ring->size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001173
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001174 rfd_ring->next_to_clean = 0;
1175 atomic_set(&rfd_ring->next_to_use, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001176
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001177 rrd_ring->next_to_use = 0;
1178 atomic_set(&rrd_ring->next_to_clean, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001179}
1180
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001181/*
1182 * atl1_clean_tx_ring - Free Tx Buffers
1183 * @adapter: board private structure
1184 */
1185static void atl1_clean_tx_ring(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001186{
1187 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1188 struct atl1_buffer *buffer_info;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001189 struct pci_dev *pdev = adapter->pdev;
1190 unsigned long size;
1191 unsigned int i;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001192
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001193 /* Free all the Tx ring sk_buffs */
1194 for (i = 0; i < tpd_ring->count; i++) {
1195 buffer_info = &tpd_ring->buffer_info[i];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001196 if (buffer_info->dma) {
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001197 pci_unmap_page(pdev, buffer_info->dma,
1198 buffer_info->length, PCI_DMA_TODEVICE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001199 buffer_info->dma = 0;
1200 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001201 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001202
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001203 for (i = 0; i < tpd_ring->count; i++) {
1204 buffer_info = &tpd_ring->buffer_info[i];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001205 if (buffer_info->skb) {
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001206 dev_kfree_skb_any(buffer_info->skb);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001207 buffer_info->skb = NULL;
1208 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001209 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001210
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001211 size = sizeof(struct atl1_buffer) * tpd_ring->count;
1212 memset(tpd_ring->buffer_info, 0, size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001213
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001214 /* Zero out the descriptor ring */
1215 memset(tpd_ring->desc, 0, tpd_ring->size);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001216
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001217 atomic_set(&tpd_ring->next_to_use, 0);
1218 atomic_set(&tpd_ring->next_to_clean, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001219}
1220
1221/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001222 * atl1_free_ring_resources - Free Tx / RX descriptor Resources
1223 * @adapter: board private structure
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001224 *
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001225 * Free all transmit software resources
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001226 */
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06001227static void atl1_free_ring_resources(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001228{
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001229 struct pci_dev *pdev = adapter->pdev;
1230 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
1231 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1232 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1233 struct atl1_ring_header *ring_header = &adapter->ring_header;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001234
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001235 atl1_clean_tx_ring(adapter);
1236 atl1_clean_rx_ring(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001237
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001238 kfree(tpd_ring->buffer_info);
1239 pci_free_consistent(pdev, ring_header->size, ring_header->desc,
1240 ring_header->dma);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001241
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001242 tpd_ring->buffer_info = NULL;
1243 tpd_ring->desc = NULL;
1244 tpd_ring->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001245
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001246 rfd_ring->buffer_info = NULL;
1247 rfd_ring->desc = NULL;
1248 rfd_ring->dma = 0;
1249
1250 rrd_ring->desc = NULL;
1251 rrd_ring->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001252}
1253
1254static void atl1_setup_mac_ctrl(struct atl1_adapter *adapter)
1255{
1256 u32 value;
1257 struct atl1_hw *hw = &adapter->hw;
1258 struct net_device *netdev = adapter->netdev;
1259 /* Config MAC CTRL Register */
1260 value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
1261 /* duplex */
1262 if (FULL_DUPLEX == adapter->link_duplex)
1263 value |= MAC_CTRL_DUPLX;
1264 /* speed */
1265 value |= ((u32) ((SPEED_1000 == adapter->link_speed) ?
1266 MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
1267 MAC_CTRL_SPEED_SHIFT);
1268 /* flow control */
1269 value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1270 /* PAD & CRC */
1271 value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1272 /* preamble length */
1273 value |= (((u32) adapter->hw.preamble_len
1274 & MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1275 /* vlan */
1276 if (adapter->vlgrp)
1277 value |= MAC_CTRL_RMV_VLAN;
1278 /* rx checksum
1279 if (adapter->rx_csum)
1280 value |= MAC_CTRL_RX_CHKSUM_EN;
1281 */
1282 /* filter mode */
1283 value |= MAC_CTRL_BC_EN;
1284 if (netdev->flags & IFF_PROMISC)
1285 value |= MAC_CTRL_PROMIS_EN;
1286 else if (netdev->flags & IFF_ALLMULTI)
1287 value |= MAC_CTRL_MC_ALL_EN;
1288 /* value |= MAC_CTRL_LOOPBACK; */
1289 iowrite32(value, hw->hw_addr + REG_MAC_CTRL);
1290}
1291
1292static u32 atl1_check_link(struct atl1_adapter *adapter)
1293{
1294 struct atl1_hw *hw = &adapter->hw;
1295 struct net_device *netdev = adapter->netdev;
1296 u32 ret_val;
1297 u16 speed, duplex, phy_data;
1298 int reconfig = 0;
1299
1300 /* MII_BMSR must read twice */
1301 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
1302 atl1_read_phy_reg(hw, MII_BMSR, &phy_data);
Jay Cliburn305282b2008-02-02 19:50:04 -06001303 if (!(phy_data & BMSR_LSTATUS)) {
1304 /* link down */
1305 if (netif_carrier_ok(netdev)) {
1306 /* old link state: Up */
Jay Cliburn460578b2008-02-02 19:50:09 -06001307 if (netif_msg_link(adapter))
1308 dev_info(&adapter->pdev->dev, "link is down\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001309 adapter->link_speed = SPEED_0;
1310 netif_carrier_off(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001311 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001312 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001313 }
1314
1315 /* Link Up */
1316 ret_val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
1317 if (ret_val)
1318 return ret_val;
1319
1320 switch (hw->media_type) {
1321 case MEDIA_TYPE_1000M_FULL:
1322 if (speed != SPEED_1000 || duplex != FULL_DUPLEX)
1323 reconfig = 1;
1324 break;
1325 case MEDIA_TYPE_100M_FULL:
1326 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
1327 reconfig = 1;
1328 break;
1329 case MEDIA_TYPE_100M_HALF:
1330 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
1331 reconfig = 1;
1332 break;
1333 case MEDIA_TYPE_10M_FULL:
1334 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
1335 reconfig = 1;
1336 break;
1337 case MEDIA_TYPE_10M_HALF:
1338 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
1339 reconfig = 1;
1340 break;
1341 }
1342
1343 /* link result is our setting */
1344 if (!reconfig) {
1345 if (adapter->link_speed != speed
1346 || adapter->link_duplex != duplex) {
1347 adapter->link_speed = speed;
1348 adapter->link_duplex = duplex;
1349 atl1_setup_mac_ctrl(adapter);
Jay Cliburn460578b2008-02-02 19:50:09 -06001350 if (netif_msg_link(adapter))
1351 dev_info(&adapter->pdev->dev,
1352 "%s link is up %d Mbps %s\n",
1353 netdev->name, adapter->link_speed,
1354 adapter->link_duplex == FULL_DUPLEX ?
1355 "full duplex" : "half duplex");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001356 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001357 if (!netif_carrier_ok(netdev)) {
1358 /* Link down -> Up */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001359 netif_carrier_on(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001360 }
Jay Cliburn305282b2008-02-02 19:50:04 -06001361 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001362 }
1363
Jay Cliburn305282b2008-02-02 19:50:04 -06001364 /* change original link status */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001365 if (netif_carrier_ok(netdev)) {
1366 adapter->link_speed = SPEED_0;
1367 netif_carrier_off(netdev);
1368 netif_stop_queue(netdev);
1369 }
1370
1371 if (hw->media_type != MEDIA_TYPE_AUTO_SENSOR &&
1372 hw->media_type != MEDIA_TYPE_1000M_FULL) {
1373 switch (hw->media_type) {
1374 case MEDIA_TYPE_100M_FULL:
1375 phy_data = MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
1376 MII_CR_RESET;
1377 break;
1378 case MEDIA_TYPE_100M_HALF:
1379 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
1380 break;
1381 case MEDIA_TYPE_10M_FULL:
1382 phy_data =
1383 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
1384 break;
Jay Cliburn305282b2008-02-02 19:50:04 -06001385 default:
1386 /* MEDIA_TYPE_10M_HALF: */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001387 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
1388 break;
1389 }
1390 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
Jay Cliburn305282b2008-02-02 19:50:04 -06001391 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001392 }
1393
1394 /* auto-neg, insert timer to re-config phy */
1395 if (!adapter->phy_timer_pending) {
1396 adapter->phy_timer_pending = true;
1397 mod_timer(&adapter->phy_config_timer, jiffies + 3 * HZ);
1398 }
1399
Jay Cliburn305282b2008-02-02 19:50:04 -06001400 return 0;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001401}
1402
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001403static void set_flow_ctrl_old(struct atl1_adapter *adapter)
1404{
1405 u32 hi, lo, value;
1406
1407 /* RFD Flow Control */
1408 value = adapter->rfd_ring.count;
1409 hi = value / 16;
1410 if (hi < 2)
1411 hi = 2;
1412 lo = value * 7 / 8;
1413
1414 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001415 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001416 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1417
1418 /* RRD Flow Control */
1419 value = adapter->rrd_ring.count;
1420 lo = value / 16;
1421 hi = value * 7 / 8;
1422 if (lo < 2)
1423 lo = 2;
1424 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001425 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001426 iowrite32(value, adapter->hw.hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1427}
1428
1429static void set_flow_ctrl_new(struct atl1_hw *hw)
1430{
1431 u32 hi, lo, value;
1432
1433 /* RXF Flow Control */
1434 value = ioread32(hw->hw_addr + REG_SRAM_RXF_LEN);
1435 lo = value / 16;
1436 if (lo < 192)
1437 lo = 192;
1438 hi = value * 7 / 8;
1439 if (hi < lo)
1440 hi = lo + 16;
1441 value = ((hi & RXQ_RXF_PAUSE_TH_HI_MASK) << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001442 ((lo & RXQ_RXF_PAUSE_TH_LO_MASK) << RXQ_RXF_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001443 iowrite32(value, hw->hw_addr + REG_RXQ_RXF_PAUSE_THRESH);
1444
1445 /* RRD Flow Control */
1446 value = ioread32(hw->hw_addr + REG_SRAM_RRD_LEN);
1447 lo = value / 8;
1448 hi = value * 7 / 8;
1449 if (lo < 2)
1450 lo = 2;
1451 if (hi < lo)
1452 hi = lo + 3;
1453 value = ((hi & RXQ_RRD_PAUSE_TH_HI_MASK) << RXQ_RRD_PAUSE_TH_HI_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001454 ((lo & RXQ_RRD_PAUSE_TH_LO_MASK) << RXQ_RRD_PAUSE_TH_LO_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001455 iowrite32(value, hw->hw_addr + REG_RXQ_RRD_PAUSE_THRESH);
1456}
1457
1458/*
1459 * atl1_configure - Configure Transmit&Receive Unit after Reset
1460 * @adapter: board private structure
1461 *
1462 * Configure the Tx /Rx unit of the MAC after a reset.
1463 */
1464static u32 atl1_configure(struct atl1_adapter *adapter)
1465{
1466 struct atl1_hw *hw = &adapter->hw;
1467 u32 value;
1468
1469 /* clear interrupt status */
1470 iowrite32(0xffffffff, adapter->hw.hw_addr + REG_ISR);
1471
1472 /* set MAC Address */
1473 value = (((u32) hw->mac_addr[2]) << 24) |
1474 (((u32) hw->mac_addr[3]) << 16) |
1475 (((u32) hw->mac_addr[4]) << 8) |
1476 (((u32) hw->mac_addr[5]));
1477 iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
1478 value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
1479 iowrite32(value, hw->hw_addr + (REG_MAC_STA_ADDR + 4));
1480
1481 /* tx / rx ring */
1482
1483 /* HI base address */
1484 iowrite32((u32) ((adapter->tpd_ring.dma & 0xffffffff00000000ULL) >> 32),
1485 hw->hw_addr + REG_DESC_BASE_ADDR_HI);
1486 /* LO base address */
1487 iowrite32((u32) (adapter->rfd_ring.dma & 0x00000000ffffffffULL),
1488 hw->hw_addr + REG_DESC_RFD_ADDR_LO);
1489 iowrite32((u32) (adapter->rrd_ring.dma & 0x00000000ffffffffULL),
1490 hw->hw_addr + REG_DESC_RRD_ADDR_LO);
1491 iowrite32((u32) (adapter->tpd_ring.dma & 0x00000000ffffffffULL),
1492 hw->hw_addr + REG_DESC_TPD_ADDR_LO);
1493 iowrite32((u32) (adapter->cmb.dma & 0x00000000ffffffffULL),
1494 hw->hw_addr + REG_DESC_CMB_ADDR_LO);
1495 iowrite32((u32) (adapter->smb.dma & 0x00000000ffffffffULL),
1496 hw->hw_addr + REG_DESC_SMB_ADDR_LO);
1497
1498 /* element count */
1499 value = adapter->rrd_ring.count;
1500 value <<= 16;
1501 value += adapter->rfd_ring.count;
1502 iowrite32(value, hw->hw_addr + REG_DESC_RFD_RRD_RING_SIZE);
Jay Cliburn2ca13da2007-07-15 11:03:28 -05001503 iowrite32(adapter->tpd_ring.count, hw->hw_addr +
1504 REG_DESC_TPD_RING_SIZE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001505
1506 /* Load Ptr */
1507 iowrite32(1, hw->hw_addr + REG_LOAD_PTR);
1508
1509 /* config Mailbox */
1510 value = ((atomic_read(&adapter->tpd_ring.next_to_use)
1511 & MB_TPD_PROD_INDX_MASK) << MB_TPD_PROD_INDX_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001512 ((atomic_read(&adapter->rrd_ring.next_to_clean)
1513 & MB_RRD_CONS_INDX_MASK) << MB_RRD_CONS_INDX_SHIFT) |
1514 ((atomic_read(&adapter->rfd_ring.next_to_use)
1515 & MB_RFD_PROD_INDX_MASK) << MB_RFD_PROD_INDX_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001516 iowrite32(value, hw->hw_addr + REG_MAILBOX);
1517
1518 /* config IPG/IFG */
1519 value = (((u32) hw->ipgt & MAC_IPG_IFG_IPGT_MASK)
1520 << MAC_IPG_IFG_IPGT_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001521 (((u32) hw->min_ifg & MAC_IPG_IFG_MIFG_MASK)
1522 << MAC_IPG_IFG_MIFG_SHIFT) |
1523 (((u32) hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK)
1524 << MAC_IPG_IFG_IPGR1_SHIFT) |
1525 (((u32) hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK)
1526 << MAC_IPG_IFG_IPGR2_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001527 iowrite32(value, hw->hw_addr + REG_MAC_IPG_IFG);
1528
1529 /* config Half-Duplex Control */
1530 value = ((u32) hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001531 (((u32) hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK)
1532 << MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
1533 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
1534 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
1535 (((u32) hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK)
1536 << MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001537 iowrite32(value, hw->hw_addr + REG_MAC_HALF_DUPLX_CTRL);
1538
1539 /* set Interrupt Moderator Timer */
1540 iowrite16(adapter->imt, hw->hw_addr + REG_IRQ_MODU_TIMER_INIT);
1541 iowrite32(MASTER_CTRL_ITIMER_EN, hw->hw_addr + REG_MASTER_CTRL);
1542
1543 /* set Interrupt Clear Timer */
1544 iowrite16(adapter->ict, hw->hw_addr + REG_CMBDISDMA_TIMER);
1545
Jay Cliburn2a491282008-01-14 19:56:41 -06001546 /* set max frame size hw will accept */
1547 iowrite32(hw->max_frame_size, hw->hw_addr + REG_MTU);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001548
1549 /* jumbo size & rrd retirement timer */
1550 value = (((u32) hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK)
1551 << RXQ_JMBOSZ_TH_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001552 (((u32) hw->rx_jumbo_lkah & RXQ_JMBO_LKAH_MASK)
1553 << RXQ_JMBO_LKAH_SHIFT) |
1554 (((u32) hw->rrd_ret_timer & RXQ_RRD_TIMER_MASK)
1555 << RXQ_RRD_TIMER_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001556 iowrite32(value, hw->hw_addr + REG_RXQ_JMBOSZ_RRDTIM);
1557
1558 /* Flow Control */
1559 switch (hw->dev_rev) {
1560 case 0x8001:
1561 case 0x9001:
1562 case 0x9002:
1563 case 0x9003:
1564 set_flow_ctrl_old(adapter);
1565 break;
1566 default:
1567 set_flow_ctrl_new(hw);
1568 break;
1569 }
1570
1571 /* config TXQ */
1572 value = (((u32) hw->tpd_burst & TXQ_CTRL_TPD_BURST_NUM_MASK)
1573 << TXQ_CTRL_TPD_BURST_NUM_SHIFT) |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001574 (((u32) hw->txf_burst & TXQ_CTRL_TXF_BURST_NUM_MASK)
1575 << TXQ_CTRL_TXF_BURST_NUM_SHIFT) |
1576 (((u32) hw->tpd_fetch_th & TXQ_CTRL_TPD_FETCH_TH_MASK)
1577 << TXQ_CTRL_TPD_FETCH_TH_SHIFT) | TXQ_CTRL_ENH_MODE |
1578 TXQ_CTRL_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001579 iowrite32(value, hw->hw_addr + REG_TXQ_CTRL);
1580
1581 /* min tpd fetch gap & tx jumbo packet size threshold for taskoffload */
1582 value = (((u32) hw->tx_jumbo_task_th & TX_JUMBO_TASK_TH_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001583 << TX_JUMBO_TASK_TH_SHIFT) |
1584 (((u32) hw->tpd_fetch_gap & TX_TPD_MIN_IPG_MASK)
1585 << TX_TPD_MIN_IPG_SHIFT);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001586 iowrite32(value, hw->hw_addr + REG_TX_JUMBO_TASK_TH_TPD_IPG);
1587
1588 /* config RXQ */
1589 value = (((u32) hw->rfd_burst & RXQ_CTRL_RFD_BURST_NUM_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001590 << RXQ_CTRL_RFD_BURST_NUM_SHIFT) |
1591 (((u32) hw->rrd_burst & RXQ_CTRL_RRD_BURST_THRESH_MASK)
1592 << RXQ_CTRL_RRD_BURST_THRESH_SHIFT) |
1593 (((u32) hw->rfd_fetch_gap & RXQ_CTRL_RFD_PREF_MIN_IPG_MASK)
1594 << RXQ_CTRL_RFD_PREF_MIN_IPG_SHIFT) | RXQ_CTRL_CUT_THRU_EN |
1595 RXQ_CTRL_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001596 iowrite32(value, hw->hw_addr + REG_RXQ_CTRL);
1597
1598 /* config DMA Engine */
1599 value = ((((u32) hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
Jay Cliburn53ffb422007-07-15 11:03:27 -05001600 << DMA_CTRL_DMAR_BURST_LEN_SHIFT) |
Jay Cliburn3f516c02007-07-19 18:45:11 -05001601 ((((u32) hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
1602 << DMA_CTRL_DMAW_BURST_LEN_SHIFT) | DMA_CTRL_DMAR_EN |
Jay Cliburn53ffb422007-07-15 11:03:27 -05001603 DMA_CTRL_DMAW_EN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001604 value |= (u32) hw->dma_ord;
1605 if (atl1_rcb_128 == hw->rcb_value)
1606 value |= DMA_CTRL_RCB_VALUE;
1607 iowrite32(value, hw->hw_addr + REG_DMA_CTRL);
1608
1609 /* config CMB / SMB */
Jay Cliburn91a500a2007-07-19 18:45:12 -05001610 value = (hw->cmb_tpd > adapter->tpd_ring.count) ?
1611 hw->cmb_tpd : adapter->tpd_ring.count;
1612 value <<= 16;
1613 value |= hw->cmb_rrd;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001614 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TH);
1615 value = hw->cmb_rx_timer | ((u32) hw->cmb_tx_timer << 16);
1616 iowrite32(value, hw->hw_addr + REG_CMB_WRITE_TIMER);
1617 iowrite32(hw->smb_timer, hw->hw_addr + REG_SMB_TIMER);
1618
1619 /* --- enable CMB / SMB */
1620 value = CSMB_CTRL_CMB_EN | CSMB_CTRL_SMB_EN;
1621 iowrite32(value, hw->hw_addr + REG_CSMB_CTRL);
1622
1623 value = ioread32(adapter->hw.hw_addr + REG_ISR);
1624 if (unlikely((value & ISR_PHY_LINKDOWN) != 0))
1625 value = 1; /* config failed */
1626 else
1627 value = 0;
1628
1629 /* clear all interrupt status */
1630 iowrite32(0x3fffffff, adapter->hw.hw_addr + REG_ISR);
1631 iowrite32(0, adapter->hw.hw_addr + REG_ISR);
1632 return value;
1633}
1634
1635/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001636 * atl1_pcie_patch - Patch for PCIE module
1637 */
1638static void atl1_pcie_patch(struct atl1_adapter *adapter)
1639{
1640 u32 value;
1641
1642 /* much vendor magic here */
1643 value = 0x6500;
1644 iowrite32(value, adapter->hw.hw_addr + 0x12FC);
1645 /* pcie flow control mode change */
1646 value = ioread32(adapter->hw.hw_addr + 0x1008);
1647 value |= 0x8000;
1648 iowrite32(value, adapter->hw.hw_addr + 0x1008);
1649}
1650
1651/*
1652 * When ACPI resume on some VIA MotherBoard, the Interrupt Disable bit/0x400
1653 * on PCI Command register is disable.
1654 * The function enable this bit.
1655 * Brackett, 2006/03/15
1656 */
1657static void atl1_via_workaround(struct atl1_adapter *adapter)
1658{
1659 unsigned long value;
1660
1661 value = ioread16(adapter->hw.hw_addr + PCI_COMMAND);
1662 if (value & PCI_COMMAND_INTX_DISABLE)
1663 value &= ~PCI_COMMAND_INTX_DISABLE;
1664 iowrite32(value, adapter->hw.hw_addr + PCI_COMMAND);
1665}
1666
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001667static void atl1_inc_smb(struct atl1_adapter *adapter)
1668{
1669 struct stats_msg_block *smb = adapter->smb.smb;
1670
1671 /* Fill out the OS statistics structure */
1672 adapter->soft_stats.rx_packets += smb->rx_ok;
1673 adapter->soft_stats.tx_packets += smb->tx_ok;
1674 adapter->soft_stats.rx_bytes += smb->rx_byte_cnt;
1675 adapter->soft_stats.tx_bytes += smb->tx_byte_cnt;
1676 adapter->soft_stats.multicast += smb->rx_mcast;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001677 adapter->soft_stats.collisions += (smb->tx_1_col + smb->tx_2_col * 2 +
1678 smb->tx_late_col + smb->tx_abort_col * adapter->hw.max_retry);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001679
1680 /* Rx Errors */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001681 adapter->soft_stats.rx_errors += (smb->rx_frag + smb->rx_fcs_err +
1682 smb->rx_len_err + smb->rx_sz_ov + smb->rx_rxf_ov +
1683 smb->rx_rrd_ov + smb->rx_align_err);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001684 adapter->soft_stats.rx_fifo_errors += smb->rx_rxf_ov;
1685 adapter->soft_stats.rx_length_errors += smb->rx_len_err;
1686 adapter->soft_stats.rx_crc_errors += smb->rx_fcs_err;
1687 adapter->soft_stats.rx_frame_errors += smb->rx_align_err;
1688 adapter->soft_stats.rx_missed_errors += (smb->rx_rrd_ov +
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001689 smb->rx_rxf_ov);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001690
1691 adapter->soft_stats.rx_pause += smb->rx_pause;
1692 adapter->soft_stats.rx_rrd_ov += smb->rx_rrd_ov;
1693 adapter->soft_stats.rx_trunc += smb->rx_sz_ov;
1694
1695 /* Tx Errors */
1696 adapter->soft_stats.tx_errors += (smb->tx_late_col +
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001697 smb->tx_abort_col + smb->tx_underrun + smb->tx_trunc);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001698 adapter->soft_stats.tx_fifo_errors += smb->tx_underrun;
1699 adapter->soft_stats.tx_aborted_errors += smb->tx_abort_col;
1700 adapter->soft_stats.tx_window_errors += smb->tx_late_col;
1701
1702 adapter->soft_stats.excecol += smb->tx_abort_col;
1703 adapter->soft_stats.deffer += smb->tx_defer;
1704 adapter->soft_stats.scc += smb->tx_1_col;
1705 adapter->soft_stats.mcc += smb->tx_2_col;
1706 adapter->soft_stats.latecol += smb->tx_late_col;
1707 adapter->soft_stats.tx_underun += smb->tx_underrun;
1708 adapter->soft_stats.tx_trunc += smb->tx_trunc;
1709 adapter->soft_stats.tx_pause += smb->tx_pause;
1710
1711 adapter->net_stats.rx_packets = adapter->soft_stats.rx_packets;
1712 adapter->net_stats.tx_packets = adapter->soft_stats.tx_packets;
1713 adapter->net_stats.rx_bytes = adapter->soft_stats.rx_bytes;
1714 adapter->net_stats.tx_bytes = adapter->soft_stats.tx_bytes;
1715 adapter->net_stats.multicast = adapter->soft_stats.multicast;
1716 adapter->net_stats.collisions = adapter->soft_stats.collisions;
1717 adapter->net_stats.rx_errors = adapter->soft_stats.rx_errors;
1718 adapter->net_stats.rx_over_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001719 adapter->soft_stats.rx_missed_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001720 adapter->net_stats.rx_length_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001721 adapter->soft_stats.rx_length_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001722 adapter->net_stats.rx_crc_errors = adapter->soft_stats.rx_crc_errors;
1723 adapter->net_stats.rx_frame_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001724 adapter->soft_stats.rx_frame_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001725 adapter->net_stats.rx_fifo_errors = adapter->soft_stats.rx_fifo_errors;
1726 adapter->net_stats.rx_missed_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001727 adapter->soft_stats.rx_missed_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001728 adapter->net_stats.tx_errors = adapter->soft_stats.tx_errors;
1729 adapter->net_stats.tx_fifo_errors = adapter->soft_stats.tx_fifo_errors;
1730 adapter->net_stats.tx_aborted_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001731 adapter->soft_stats.tx_aborted_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001732 adapter->net_stats.tx_window_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001733 adapter->soft_stats.tx_window_errors;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001734 adapter->net_stats.tx_carrier_errors =
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001735 adapter->soft_stats.tx_carrier_errors;
1736}
1737
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001738static void atl1_update_mailbox(struct atl1_adapter *adapter)
1739{
1740 unsigned long flags;
1741 u32 tpd_next_to_use;
1742 u32 rfd_next_to_use;
1743 u32 rrd_next_to_clean;
1744 u32 value;
1745
1746 spin_lock_irqsave(&adapter->mb_lock, flags);
1747
1748 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
1749 rfd_next_to_use = atomic_read(&adapter->rfd_ring.next_to_use);
1750 rrd_next_to_clean = atomic_read(&adapter->rrd_ring.next_to_clean);
1751
1752 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
1753 MB_RFD_PROD_INDX_SHIFT) |
1754 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
1755 MB_RRD_CONS_INDX_SHIFT) |
1756 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
1757 MB_TPD_PROD_INDX_SHIFT);
1758 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
1759
1760 spin_unlock_irqrestore(&adapter->mb_lock, flags);
1761}
1762
1763static void atl1_clean_alloc_flag(struct atl1_adapter *adapter,
1764 struct rx_return_desc *rrd, u16 offset)
1765{
1766 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1767
1768 while (rfd_ring->next_to_clean != (rrd->buf_indx + offset)) {
1769 rfd_ring->buffer_info[rfd_ring->next_to_clean].alloced = 0;
1770 if (++rfd_ring->next_to_clean == rfd_ring->count) {
1771 rfd_ring->next_to_clean = 0;
1772 }
1773 }
1774}
1775
1776static void atl1_update_rfd_index(struct atl1_adapter *adapter,
1777 struct rx_return_desc *rrd)
1778{
1779 u16 num_buf;
1780
1781 num_buf = (rrd->xsz.xsum_sz.pkt_size + adapter->rx_buffer_len - 1) /
1782 adapter->rx_buffer_len;
1783 if (rrd->num_buf == num_buf)
1784 /* clean alloc flag for bad rrd */
1785 atl1_clean_alloc_flag(adapter, rrd, num_buf);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001786}
1787
1788static void atl1_rx_checksum(struct atl1_adapter *adapter,
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001789 struct rx_return_desc *rrd, struct sk_buff *skb)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001790{
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001791 struct pci_dev *pdev = adapter->pdev;
1792
Jay Cliburnc2ac3ef2008-08-04 19:05:10 -05001793 /*
1794 * The L1 hardware contains a bug that erroneously sets the
1795 * PACKET_FLAG_ERR and ERR_FLAG_L4_CHKSUM bits whenever a
1796 * fragmented IP packet is received, even though the packet
1797 * is perfectly valid and its checksum is correct. There's
1798 * no way to distinguish between one of these good packets
1799 * and a packet that actually contains a TCP/UDP checksum
1800 * error, so all we can do is allow it to be handed up to
1801 * the higher layers and let it be sorted out there.
1802 */
1803
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001804 skb->ip_summed = CHECKSUM_NONE;
1805
1806 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1807 if (rrd->err_flg & (ERR_FLAG_CRC | ERR_FLAG_TRUNC |
1808 ERR_FLAG_CODE | ERR_FLAG_OV)) {
1809 adapter->hw_csum_err++;
Jay Cliburn460578b2008-02-02 19:50:09 -06001810 if (netif_msg_rx_err(adapter))
1811 dev_printk(KERN_DEBUG, &pdev->dev,
1812 "rx checksum error\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001813 return;
1814 }
1815 }
1816
1817 /* not IPv4 */
1818 if (!(rrd->pkt_flg & PACKET_FLAG_IPV4))
1819 /* checksum is invalid, but it's not an IPv4 pkt, so ok */
1820 return;
1821
1822 /* IPv4 packet */
1823 if (likely(!(rrd->err_flg &
1824 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM)))) {
1825 skb->ip_summed = CHECKSUM_UNNECESSARY;
1826 adapter->hw_csum_good++;
1827 return;
1828 }
1829
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001830 return;
1831}
1832
1833/*
1834 * atl1_alloc_rx_buffers - Replace used receive buffers
1835 * @adapter: address of board private structure
1836 */
1837static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
1838{
1839 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1840 struct pci_dev *pdev = adapter->pdev;
1841 struct page *page;
1842 unsigned long offset;
1843 struct atl1_buffer *buffer_info, *next_info;
1844 struct sk_buff *skb;
1845 u16 num_alloc = 0;
1846 u16 rfd_next_to_use, next_next;
1847 struct rx_free_desc *rfd_desc;
1848
1849 next_next = rfd_next_to_use = atomic_read(&rfd_ring->next_to_use);
1850 if (++next_next == rfd_ring->count)
1851 next_next = 0;
1852 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1853 next_info = &rfd_ring->buffer_info[next_next];
1854
1855 while (!buffer_info->alloced && !next_info->alloced) {
1856 if (buffer_info->skb) {
1857 buffer_info->alloced = 1;
1858 goto next;
1859 }
1860
1861 rfd_desc = ATL1_RFD_DESC(rfd_ring, rfd_next_to_use);
1862
Stephen Hemmingerb102df12008-04-16 16:37:29 -07001863 skb = netdev_alloc_skb(adapter->netdev,
1864 adapter->rx_buffer_len + NET_IP_ALIGN);
Jay Cliburn305282b2008-02-02 19:50:04 -06001865 if (unlikely(!skb)) {
1866 /* Better luck next round */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001867 adapter->net_stats.rx_dropped++;
1868 break;
1869 }
1870
1871 /*
1872 * Make buffer alignment 2 beyond a 16 byte boundary
1873 * this will result in a 16 byte aligned IP header after
1874 * the 14 byte MAC header is removed
1875 */
1876 skb_reserve(skb, NET_IP_ALIGN);
1877
1878 buffer_info->alloced = 1;
1879 buffer_info->skb = skb;
1880 buffer_info->length = (u16) adapter->rx_buffer_len;
1881 page = virt_to_page(skb->data);
1882 offset = (unsigned long)skb->data & ~PAGE_MASK;
1883 buffer_info->dma = pci_map_page(pdev, page, offset,
1884 adapter->rx_buffer_len,
1885 PCI_DMA_FROMDEVICE);
1886 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1887 rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
1888 rfd_desc->coalese = 0;
1889
1890next:
1891 rfd_next_to_use = next_next;
1892 if (unlikely(++next_next == rfd_ring->count))
1893 next_next = 0;
1894
1895 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1896 next_info = &rfd_ring->buffer_info[next_next];
1897 num_alloc++;
1898 }
1899
1900 if (num_alloc) {
1901 /*
1902 * Force memory writes to complete before letting h/w
1903 * know there are new descriptors to fetch. (Only
1904 * applicable for weak-ordered memory model archs,
1905 * such as IA-64).
1906 */
1907 wmb();
1908 atomic_set(&rfd_ring->next_to_use, (int)rfd_next_to_use);
1909 }
1910 return num_alloc;
1911}
1912
1913static void atl1_intr_rx(struct atl1_adapter *adapter)
1914{
1915 int i, count;
1916 u16 length;
1917 u16 rrd_next_to_clean;
1918 u32 value;
1919 struct atl1_rfd_ring *rfd_ring = &adapter->rfd_ring;
1920 struct atl1_rrd_ring *rrd_ring = &adapter->rrd_ring;
1921 struct atl1_buffer *buffer_info;
1922 struct rx_return_desc *rrd;
1923 struct sk_buff *skb;
1924
1925 count = 0;
1926
1927 rrd_next_to_clean = atomic_read(&rrd_ring->next_to_clean);
1928
1929 while (1) {
1930 rrd = ATL1_RRD_DESC(rrd_ring, rrd_next_to_clean);
1931 i = 1;
1932 if (likely(rrd->xsz.valid)) { /* packet valid */
1933chk_rrd:
1934 /* check rrd status */
1935 if (likely(rrd->num_buf == 1))
1936 goto rrd_ok;
Jay Cliburn235ffa12008-02-02 19:50:10 -06001937 else if (netif_msg_rx_err(adapter)) {
1938 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1939 "unexpected RRD buffer count\n");
1940 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1941 "rx_buf_len = %d\n",
1942 adapter->rx_buffer_len);
1943 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1944 "RRD num_buf = %d\n",
1945 rrd->num_buf);
1946 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1947 "RRD pkt_len = %d\n",
1948 rrd->xsz.xsum_sz.pkt_size);
1949 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1950 "RRD pkt_flg = 0x%08X\n",
1951 rrd->pkt_flg);
1952 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1953 "RRD err_flg = 0x%08X\n",
1954 rrd->err_flg);
1955 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1956 "RRD vlan_tag = 0x%08X\n",
1957 rrd->vlan_tag);
1958 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001959
1960 /* rrd seems to be bad */
1961 if (unlikely(i-- > 0)) {
1962 /* rrd may not be DMAed completely */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001963 udelay(1);
1964 goto chk_rrd;
1965 }
1966 /* bad rrd */
Jay Cliburn460578b2008-02-02 19:50:09 -06001967 if (netif_msg_rx_err(adapter))
1968 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
1969 "bad RRD\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001970 /* see if update RFD index */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001971 if (rrd->num_buf > 1)
1972 atl1_update_rfd_index(adapter, rrd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001973
1974 /* update rrd */
1975 rrd->xsz.valid = 0;
1976 if (++rrd_next_to_clean == rrd_ring->count)
1977 rrd_next_to_clean = 0;
1978 count++;
1979 continue;
1980 } else { /* current rrd still not be updated */
1981
1982 break;
1983 }
1984rrd_ok:
1985 /* clean alloc flag for bad rrd */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05001986 atl1_clean_alloc_flag(adapter, rrd, 0);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05001987
1988 buffer_info = &rfd_ring->buffer_info[rrd->buf_indx];
1989 if (++rfd_ring->next_to_clean == rfd_ring->count)
1990 rfd_ring->next_to_clean = 0;
1991
1992 /* update rrd next to clean */
1993 if (++rrd_next_to_clean == rrd_ring->count)
1994 rrd_next_to_clean = 0;
1995 count++;
1996
1997 if (unlikely(rrd->pkt_flg & PACKET_FLAG_ERR)) {
1998 if (!(rrd->err_flg &
1999 (ERR_FLAG_IP_CHKSUM | ERR_FLAG_L4_CHKSUM
2000 | ERR_FLAG_LEN))) {
2001 /* packet error, don't need upstream */
2002 buffer_info->alloced = 0;
2003 rrd->xsz.valid = 0;
2004 continue;
2005 }
2006 }
2007
2008 /* Good Receive */
2009 pci_unmap_page(adapter->pdev, buffer_info->dma,
2010 buffer_info->length, PCI_DMA_FROMDEVICE);
Alexey Dobriyanaefdbf12008-05-23 02:00:25 +04002011 buffer_info->dma = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002012 skb = buffer_info->skb;
2013 length = le16_to_cpu(rrd->xsz.xsum_sz.pkt_size);
2014
Jay Cliburna3093d92007-07-19 18:45:14 -05002015 skb_put(skb, length - ETH_FCS_LEN);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002016
2017 /* Receive Checksum Offload */
2018 atl1_rx_checksum(adapter, rrd, skb);
2019 skb->protocol = eth_type_trans(skb, adapter->netdev);
2020
2021 if (adapter->vlgrp && (rrd->pkt_flg & PACKET_FLAG_VLAN_INS)) {
2022 u16 vlan_tag = (rrd->vlan_tag >> 4) |
2023 ((rrd->vlan_tag & 7) << 13) |
2024 ((rrd->vlan_tag & 8) << 9);
2025 vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
2026 } else
2027 netif_rx(skb);
2028
2029 /* let protocol layer free skb */
2030 buffer_info->skb = NULL;
2031 buffer_info->alloced = 0;
2032 rrd->xsz.valid = 0;
2033
2034 adapter->netdev->last_rx = jiffies;
2035 }
2036
2037 atomic_set(&rrd_ring->next_to_clean, rrd_next_to_clean);
2038
2039 atl1_alloc_rx_buffers(adapter);
2040
2041 /* update mailbox ? */
2042 if (count) {
2043 u32 tpd_next_to_use;
2044 u32 rfd_next_to_use;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002045
2046 spin_lock(&adapter->mb_lock);
2047
2048 tpd_next_to_use = atomic_read(&adapter->tpd_ring.next_to_use);
2049 rfd_next_to_use =
2050 atomic_read(&adapter->rfd_ring.next_to_use);
2051 rrd_next_to_clean =
2052 atomic_read(&adapter->rrd_ring.next_to_clean);
2053 value = ((rfd_next_to_use & MB_RFD_PROD_INDX_MASK) <<
2054 MB_RFD_PROD_INDX_SHIFT) |
2055 ((rrd_next_to_clean & MB_RRD_CONS_INDX_MASK) <<
2056 MB_RRD_CONS_INDX_SHIFT) |
2057 ((tpd_next_to_use & MB_TPD_PROD_INDX_MASK) <<
2058 MB_TPD_PROD_INDX_SHIFT);
2059 iowrite32(value, adapter->hw.hw_addr + REG_MAILBOX);
2060 spin_unlock(&adapter->mb_lock);
2061 }
2062}
2063
2064static void atl1_intr_tx(struct atl1_adapter *adapter)
2065{
2066 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2067 struct atl1_buffer *buffer_info;
2068 u16 sw_tpd_next_to_clean;
2069 u16 cmb_tpd_next_to_clean;
2070
2071 sw_tpd_next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2072 cmb_tpd_next_to_clean = le16_to_cpu(adapter->cmb.cmb->tpd_cons_idx);
2073
2074 while (cmb_tpd_next_to_clean != sw_tpd_next_to_clean) {
2075 struct tx_packet_desc *tpd;
2076
2077 tpd = ATL1_TPD_DESC(tpd_ring, sw_tpd_next_to_clean);
2078 buffer_info = &tpd_ring->buffer_info[sw_tpd_next_to_clean];
2079 if (buffer_info->dma) {
2080 pci_unmap_page(adapter->pdev, buffer_info->dma,
2081 buffer_info->length, PCI_DMA_TODEVICE);
2082 buffer_info->dma = 0;
2083 }
2084
2085 if (buffer_info->skb) {
2086 dev_kfree_skb_irq(buffer_info->skb);
2087 buffer_info->skb = NULL;
2088 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002089
2090 if (++sw_tpd_next_to_clean == tpd_ring->count)
2091 sw_tpd_next_to_clean = 0;
2092 }
2093 atomic_set(&tpd_ring->next_to_clean, sw_tpd_next_to_clean);
2094
2095 if (netif_queue_stopped(adapter->netdev)
2096 && netif_carrier_ok(adapter->netdev))
2097 netif_wake_queue(adapter->netdev);
2098}
2099
Jay Cliburne6a7ff42007-07-19 18:45:10 -05002100static u16 atl1_tpd_avail(struct atl1_tpd_ring *tpd_ring)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002101{
2102 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
2103 u16 next_to_use = atomic_read(&tpd_ring->next_to_use);
Jay Cliburn53ffb422007-07-15 11:03:27 -05002104 return ((next_to_clean > next_to_use) ?
2105 next_to_clean - next_to_use - 1 :
2106 tpd_ring->count + next_to_clean - next_to_use - 1);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002107}
2108
2109static int atl1_tso(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002110 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002111{
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002112 u8 hdr_len, ip_off;
2113 u32 real_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002114 int err;
2115
2116 if (skb_shinfo(skb)->gso_size) {
2117 if (skb_header_cloned(skb)) {
2118 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2119 if (unlikely(err))
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002120 return -1;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002121 }
2122
Al Virod63ddce2008-05-21 01:34:30 +01002123 if (skb->protocol == htons(ETH_P_IP)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002124 struct iphdr *iph = ip_hdr(skb);
2125
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002126 real_len = (((unsigned char *)iph - skb->data) +
2127 ntohs(iph->tot_len));
2128 if (real_len < skb->len)
2129 pskb_trim(skb, real_len);
2130 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2131 if (skb->len == hdr_len) {
2132 iph->check = 0;
2133 tcp_hdr(skb)->check =
2134 ~csum_tcpudp_magic(iph->saddr,
2135 iph->daddr, tcp_hdrlen(skb),
2136 IPPROTO_TCP, 0);
2137 ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2138 TPD_IPHL_SHIFT;
2139 ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2140 TPD_TCPHDRLEN_MASK) <<
2141 TPD_TCPHDRLEN_SHIFT;
2142 ptpd->word3 |= 1 << TPD_IP_CSUM_SHIFT;
2143 ptpd->word3 |= 1 << TPD_TCP_CSUM_SHIFT;
2144 return 1;
2145 }
2146
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002147 iph->check = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07002148 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002149 iph->daddr, 0, IPPROTO_TCP, 0);
2150 ip_off = (unsigned char *)iph -
2151 (unsigned char *) skb_network_header(skb);
2152 if (ip_off == 8) /* 802.3-SNAP frame */
2153 ptpd->word3 |= 1 << TPD_ETHTYPE_SHIFT;
2154 else if (ip_off != 0)
2155 return -2;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002156
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002157 ptpd->word3 |= (iph->ihl & TPD_IPHL_MASK) <<
2158 TPD_IPHL_SHIFT;
2159 ptpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
2160 TPD_TCPHDRLEN_MASK) << TPD_TCPHDRLEN_SHIFT;
2161 ptpd->word3 |= (skb_shinfo(skb)->gso_size &
2162 TPD_MSS_MASK) << TPD_MSS_SHIFT;
2163 ptpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
2164 return 3;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002165 }
2166 }
2167 return false;
2168}
2169
2170static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002171 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002172{
2173 u8 css, cso;
2174
2175 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002176 css = (u8) (skb->csum_start - skb_headroom(skb));
2177 cso = css + (u8) skb->csum_offset;
2178 if (unlikely(css & 0x1)) {
2179 /* L1 hardware requires an even number here */
Jay Cliburn460578b2008-02-02 19:50:09 -06002180 if (netif_msg_tx_err(adapter))
2181 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2182 "payload offset not an even number\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002183 return -1;
2184 }
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002185 ptpd->word3 |= (css & TPD_PLOADOFFSET_MASK) <<
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002186 TPD_PLOADOFFSET_SHIFT;
Jay Cliburn5ca3bc32008-02-02 19:50:08 -06002187 ptpd->word3 |= (cso & TPD_CCSUMOFFSET_MASK) <<
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002188 TPD_CCSUMOFFSET_SHIFT;
2189 ptpd->word3 |= 1 << TPD_CUST_CSUM_EN_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002190 return true;
2191 }
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002192 return 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002193}
2194
Jay Cliburn53ffb422007-07-15 11:03:27 -05002195static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002196 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002197{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002198 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
2199 struct atl1_buffer *buffer_info;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002200 u16 buf_len = skb->len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002201 struct page *page;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002202 unsigned long offset;
2203 unsigned int nr_frags;
2204 unsigned int f;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002205 int retval;
2206 u16 next_to_use;
2207 u16 data_len;
2208 u8 hdr_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002209
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002210 buf_len -= skb->data_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002211 nr_frags = skb_shinfo(skb)->nr_frags;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002212 next_to_use = atomic_read(&tpd_ring->next_to_use);
2213 buffer_info = &tpd_ring->buffer_info[next_to_use];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002214 if (unlikely(buffer_info->skb))
2215 BUG();
Jay Cliburn305282b2008-02-02 19:50:04 -06002216 /* put skb in last TPD */
2217 buffer_info->skb = NULL;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002218
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002219 retval = (ptpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
2220 if (retval) {
2221 /* TSO */
2222 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2223 buffer_info->length = hdr_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002224 page = virt_to_page(skb->data);
2225 offset = (unsigned long)skb->data & ~PAGE_MASK;
2226 buffer_info->dma = pci_map_page(adapter->pdev, page,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002227 offset, hdr_len,
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002228 PCI_DMA_TODEVICE);
2229
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002230 if (++next_to_use == tpd_ring->count)
2231 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002232
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002233 if (buf_len > hdr_len) {
2234 int i, nseg;
Stephen Hemmingerddfce6b2007-10-05 17:19:47 -07002235
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002236 data_len = buf_len - hdr_len;
2237 nseg = (data_len + ATL1_MAX_TX_BUF_LEN - 1) /
Jay Cliburn53ffb422007-07-15 11:03:27 -05002238 ATL1_MAX_TX_BUF_LEN;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002239 for (i = 0; i < nseg; i++) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002240 buffer_info =
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002241 &tpd_ring->buffer_info[next_to_use];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002242 buffer_info->skb = NULL;
2243 buffer_info->length =
Jay Cliburn2b116142007-07-15 11:03:26 -05002244 (ATL1_MAX_TX_BUF_LEN >=
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002245 data_len) ? ATL1_MAX_TX_BUF_LEN : data_len;
2246 data_len -= buffer_info->length;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002247 page = virt_to_page(skb->data +
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002248 (hdr_len + i * ATL1_MAX_TX_BUF_LEN));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002249 offset = (unsigned long)(skb->data +
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002250 (hdr_len + i * ATL1_MAX_TX_BUF_LEN)) &
2251 ~PAGE_MASK;
Jay Cliburn53ffb422007-07-15 11:03:27 -05002252 buffer_info->dma = pci_map_page(adapter->pdev,
2253 page, offset, buffer_info->length,
2254 PCI_DMA_TODEVICE);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002255 if (++next_to_use == tpd_ring->count)
2256 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002257 }
2258 }
2259 } else {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002260 /* not TSO */
2261 buffer_info->length = buf_len;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002262 page = virt_to_page(skb->data);
2263 offset = (unsigned long)skb->data & ~PAGE_MASK;
2264 buffer_info->dma = pci_map_page(adapter->pdev, page,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002265 offset, buf_len, PCI_DMA_TODEVICE);
2266 if (++next_to_use == tpd_ring->count)
2267 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002268 }
2269
2270 for (f = 0; f < nr_frags; f++) {
2271 struct skb_frag_struct *frag;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002272 u16 i, nseg;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002273
2274 frag = &skb_shinfo(skb)->frags[f];
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002275 buf_len = frag->size;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002276
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002277 nseg = (buf_len + ATL1_MAX_TX_BUF_LEN - 1) /
2278 ATL1_MAX_TX_BUF_LEN;
2279 for (i = 0; i < nseg; i++) {
2280 buffer_info = &tpd_ring->buffer_info[next_to_use];
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002281 if (unlikely(buffer_info->skb))
2282 BUG();
2283 buffer_info->skb = NULL;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002284 buffer_info->length = (buf_len > ATL1_MAX_TX_BUF_LEN) ?
2285 ATL1_MAX_TX_BUF_LEN : buf_len;
2286 buf_len -= buffer_info->length;
Jay Cliburn53ffb422007-07-15 11:03:27 -05002287 buffer_info->dma = pci_map_page(adapter->pdev,
2288 frag->page,
2289 frag->page_offset + (i * ATL1_MAX_TX_BUF_LEN),
2290 buffer_info->length, PCI_DMA_TODEVICE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002291
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002292 if (++next_to_use == tpd_ring->count)
2293 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002294 }
2295 }
2296
2297 /* last tpd's buffer-info */
2298 buffer_info->skb = skb;
2299}
2300
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002301static void atl1_tx_queue(struct atl1_adapter *adapter, u16 count,
2302 struct tx_packet_desc *ptpd)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002303{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002304 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002305 struct atl1_buffer *buffer_info;
2306 struct tx_packet_desc *tpd;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002307 u16 j;
2308 u32 val;
2309 u16 next_to_use = (u16) atomic_read(&tpd_ring->next_to_use);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002310
2311 for (j = 0; j < count; j++) {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002312 buffer_info = &tpd_ring->buffer_info[next_to_use];
2313 tpd = ATL1_TPD_DESC(&adapter->tpd_ring, next_to_use);
2314 if (tpd != ptpd)
2315 memcpy(tpd, ptpd, sizeof(struct tx_packet_desc));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002316 tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002317 tpd->word2 = (cpu_to_le16(buffer_info->length) &
2318 TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002319
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002320 /*
2321 * if this is the first packet in a TSO chain, set
2322 * TPD_HDRFLAG, otherwise, clear it.
2323 */
2324 val = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) &
2325 TPD_SEGMENT_EN_MASK;
2326 if (val) {
2327 if (!j)
2328 tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
2329 else
2330 tpd->word3 &= ~(1 << TPD_HDRFLAG_SHIFT);
2331 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002332
2333 if (j == (count - 1))
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002334 tpd->word3 |= 1 << TPD_EOP_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002335
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002336 if (++next_to_use == tpd_ring->count)
2337 next_to_use = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002338 }
2339 /*
2340 * Force memory writes to complete before letting h/w
2341 * know there are new descriptors to fetch. (Only
2342 * applicable for weak-ordered memory model archs,
2343 * such as IA-64).
2344 */
2345 wmb();
2346
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002347 atomic_set(&tpd_ring->next_to_use, next_to_use);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002348}
2349
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002350static int atl1_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2351{
2352 struct atl1_adapter *adapter = netdev_priv(netdev);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002353 struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002354 int len = skb->len;
2355 int tso;
2356 int count = 1;
2357 int ret_val;
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002358 struct tx_packet_desc *ptpd;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002359 u16 frag_size;
2360 u16 vlan_tag;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002361 unsigned int nr_frags = 0;
2362 unsigned int mss = 0;
2363 unsigned int f;
2364 unsigned int proto_hdr_len;
2365
2366 len -= skb->data_len;
2367
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002368 if (unlikely(skb->len <= 0)) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002369 dev_kfree_skb_any(skb);
2370 return NETDEV_TX_OK;
2371 }
2372
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002373 nr_frags = skb_shinfo(skb)->nr_frags;
2374 for (f = 0; f < nr_frags; f++) {
2375 frag_size = skb_shinfo(skb)->frags[f].size;
2376 if (frag_size)
Jay Cliburn53ffb422007-07-15 11:03:27 -05002377 count += (frag_size + ATL1_MAX_TX_BUF_LEN - 1) /
2378 ATL1_MAX_TX_BUF_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002379 }
2380
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002381 mss = skb_shinfo(skb)->gso_size;
2382 if (mss) {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002383 if (skb->protocol == ntohs(ETH_P_IP)) {
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002384 proto_hdr_len = (skb_transport_offset(skb) +
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002385 tcp_hdrlen(skb));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002386 if (unlikely(proto_hdr_len > len)) {
2387 dev_kfree_skb_any(skb);
2388 return NETDEV_TX_OK;
2389 }
2390 /* need additional TPD ? */
2391 if (proto_hdr_len != len)
2392 count += (len - proto_hdr_len +
Jay Cliburn53ffb422007-07-15 11:03:27 -05002393 ATL1_MAX_TX_BUF_LEN - 1) /
2394 ATL1_MAX_TX_BUF_LEN;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002395 }
2396 }
2397
Jay Cliburne6a7ff42007-07-19 18:45:10 -05002398 if (atl1_tpd_avail(&adapter->tpd_ring) < count) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002399 /* not enough descriptors */
2400 netif_stop_queue(netdev);
Jay Cliburn460578b2008-02-02 19:50:09 -06002401 if (netif_msg_tx_queued(adapter))
2402 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2403 "tx busy\n");
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002404 return NETDEV_TX_BUSY;
2405 }
2406
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002407 ptpd = ATL1_TPD_DESC(tpd_ring,
2408 (u16) atomic_read(&tpd_ring->next_to_use));
2409 memset(ptpd, 0, sizeof(struct tx_packet_desc));
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002410
2411 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
2412 vlan_tag = vlan_tx_tag_get(skb);
2413 vlan_tag = (vlan_tag << 4) | (vlan_tag >> 13) |
2414 ((vlan_tag >> 9) & 0x8);
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002415 ptpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
2416 ptpd->word3 |= (vlan_tag & TPD_VL_TAGGED_MASK) <<
2417 TPD_VL_TAGGED_SHIFT;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002418 }
2419
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002420 tso = atl1_tso(adapter, skb, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002421 if (tso < 0) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002422 dev_kfree_skb_any(skb);
2423 return NETDEV_TX_OK;
2424 }
2425
2426 if (!tso) {
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002427 ret_val = atl1_tx_csum(adapter, skb, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002428 if (ret_val < 0) {
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002429 dev_kfree_skb_any(skb);
2430 return NETDEV_TX_OK;
2431 }
2432 }
2433
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002434 atl1_tx_map(adapter, skb, ptpd);
2435 atl1_tx_queue(adapter, count, ptpd);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002436 atl1_update_mailbox(adapter);
Jay Cliburne1098322008-09-27 04:17:21 +00002437 mmiowb();
Jay Cliburn401c0aa2008-02-02 19:50:07 -06002438 netdev->trans_start = jiffies;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002439 return NETDEV_TX_OK;
2440}
2441
2442/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002443 * atl1_intr - Interrupt Handler
2444 * @irq: interrupt number
2445 * @data: pointer to a network interface device structure
2446 * @pt_regs: CPU registers structure
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002447 */
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002448static irqreturn_t atl1_intr(int irq, void *data)
2449{
2450 struct atl1_adapter *adapter = netdev_priv(data);
2451 u32 status;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002452 int max_ints = 10;
2453
2454 status = adapter->cmb.cmb->int_stats;
2455 if (!status)
2456 return IRQ_NONE;
2457
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002458 do {
2459 /* clear CMB interrupt status at once */
2460 adapter->cmb.cmb->int_stats = 0;
2461
2462 if (status & ISR_GPHY) /* clear phy status */
Jay Cliburn305282b2008-02-02 19:50:04 -06002463 atlx_clear_phy_int(adapter);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002464
2465 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
2466 iowrite32(status | ISR_DIS_INT, adapter->hw.hw_addr + REG_ISR);
2467
2468 /* check if SMB intr */
2469 if (status & ISR_SMB)
2470 atl1_inc_smb(adapter);
2471
2472 /* check if PCIE PHY Link down */
2473 if (status & ISR_PHY_LINKDOWN) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002474 if (netif_msg_intr(adapter))
2475 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2476 "pcie phy link down %x\n", status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002477 if (netif_running(adapter->netdev)) { /* reset MAC */
2478 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2479 schedule_work(&adapter->pcie_dma_to_rst_task);
2480 return IRQ_HANDLED;
2481 }
2482 }
2483
2484 /* check if DMA read/write error ? */
2485 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002486 if (netif_msg_intr(adapter))
2487 dev_printk(KERN_DEBUG, &adapter->pdev->dev,
2488 "pcie DMA r/w error (status = 0x%x)\n",
2489 status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002490 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
2491 schedule_work(&adapter->pcie_dma_to_rst_task);
2492 return IRQ_HANDLED;
2493 }
2494
2495 /* link event */
2496 if (status & ISR_GPHY) {
2497 adapter->soft_stats.tx_carrier_errors++;
2498 atl1_check_for_link(adapter);
2499 }
2500
2501 /* transmit event */
2502 if (status & ISR_CMB_TX)
2503 atl1_intr_tx(adapter);
2504
2505 /* rx exception */
2506 if (unlikely(status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2507 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2508 ISR_HOST_RRD_OV | ISR_CMB_RX))) {
2509 if (status & (ISR_RXF_OV | ISR_RFD_UNRUN |
2510 ISR_RRD_OV | ISR_HOST_RFD_UNRUN |
2511 ISR_HOST_RRD_OV))
Jay Cliburn460578b2008-02-02 19:50:09 -06002512 if (netif_msg_intr(adapter))
2513 dev_printk(KERN_DEBUG,
2514 &adapter->pdev->dev,
2515 "rx exception, ISR = 0x%x\n",
2516 status);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002517 atl1_intr_rx(adapter);
2518 }
2519
2520 if (--max_ints < 0)
2521 break;
2522
2523 } while ((status = adapter->cmb.cmb->int_stats));
2524
2525 /* re-enable Interrupt */
2526 iowrite32(ISR_DIS_SMB | ISR_DIS_DMA, adapter->hw.hw_addr + REG_ISR);
2527 return IRQ_HANDLED;
2528}
2529
2530/*
2531 * atl1_watchdog - Timer Call-back
2532 * @data: pointer to netdev cast into an unsigned long
2533 */
2534static void atl1_watchdog(unsigned long data)
2535{
2536 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
2537
2538 /* Reset the timer */
2539 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2540}
2541
2542/*
2543 * atl1_phy_config - Timer Call-back
2544 * @data: pointer to netdev cast into an unsigned long
2545 */
2546static void atl1_phy_config(unsigned long data)
2547{
2548 struct atl1_adapter *adapter = (struct atl1_adapter *)data;
2549 struct atl1_hw *hw = &adapter->hw;
2550 unsigned long flags;
2551
2552 spin_lock_irqsave(&adapter->lock, flags);
2553 adapter->phy_timer_pending = false;
2554 atl1_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
Jay Cliburn305282b2008-02-02 19:50:04 -06002555 atl1_write_phy_reg(hw, MII_ATLX_CR, hw->mii_1000t_ctrl_reg);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002556 atl1_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN);
2557 spin_unlock_irqrestore(&adapter->lock, flags);
2558}
2559
2560/*
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002561 * Orphaned vendor comment left intact here:
2562 * <vendor comment>
2563 * If TPD Buffer size equal to 0, PCIE DMAR_TO_INT
2564 * will assert. We do soft reset <0x1400=1> according
2565 * with the SPEC. BUT, it seemes that PCIE or DMA
2566 * state-machine will not be reset. DMAR_TO_INT will
2567 * assert again and again.
2568 * </vendor comment>
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002569 */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002570
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06002571static int atl1_reset(struct atl1_adapter *adapter)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002572{
2573 int ret;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002574 ret = atl1_reset_hw(&adapter->hw);
Jay Cliburn305282b2008-02-02 19:50:04 -06002575 if (ret)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002576 return ret;
2577 return atl1_init_hw(&adapter->hw);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002578}
2579
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06002580static s32 atl1_up(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002581{
2582 struct net_device *netdev = adapter->netdev;
2583 int err;
2584 int irq_flags = IRQF_SAMPLE_RANDOM;
2585
2586 /* hardware has been reset, we need to reload some things */
Jay Cliburn305282b2008-02-02 19:50:04 -06002587 atlx_set_multi(netdev);
Jay Cliburn2ca13da2007-07-15 11:03:28 -05002588 atl1_init_ring_ptrs(adapter);
Jay Cliburn305282b2008-02-02 19:50:04 -06002589 atlx_restore_vlan(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002590 err = atl1_alloc_rx_buffers(adapter);
Jay Cliburn305282b2008-02-02 19:50:04 -06002591 if (unlikely(!err))
2592 /* no RX BUFFER allocated */
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002593 return -ENOMEM;
2594
2595 if (unlikely(atl1_configure(adapter))) {
2596 err = -EIO;
2597 goto err_up;
2598 }
2599
2600 err = pci_enable_msi(adapter->pdev);
2601 if (err) {
Jay Cliburn460578b2008-02-02 19:50:09 -06002602 if (netif_msg_ifup(adapter))
2603 dev_info(&adapter->pdev->dev,
2604 "Unable to enable MSI: %d\n", err);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002605 irq_flags |= IRQF_SHARED;
2606 }
2607
2608 err = request_irq(adapter->pdev->irq, &atl1_intr, irq_flags,
2609 netdev->name, netdev);
2610 if (unlikely(err))
2611 goto err_up;
2612
2613 mod_timer(&adapter->watchdog_timer, jiffies);
Jay Cliburn305282b2008-02-02 19:50:04 -06002614 atlx_irq_enable(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002615 atl1_check_link(adapter);
David S. Miller39d48152008-07-21 08:28:37 -07002616 netif_start_queue(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002617 return 0;
2618
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002619err_up:
2620 pci_disable_msi(adapter->pdev);
2621 /* free rx_buffers */
2622 atl1_clean_rx_ring(adapter);
2623 return err;
2624}
2625
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06002626static void atl1_down(struct atl1_adapter *adapter)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002627{
2628 struct net_device *netdev = adapter->netdev;
2629
Jay Cliburnb29be6d2008-09-27 04:17:20 +00002630 netif_stop_queue(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002631 del_timer_sync(&adapter->watchdog_timer);
2632 del_timer_sync(&adapter->phy_config_timer);
2633 adapter->phy_timer_pending = false;
2634
Jay Cliburn305282b2008-02-02 19:50:04 -06002635 atlx_irq_disable(adapter);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002636 free_irq(adapter->pdev->irq, netdev);
2637 pci_disable_msi(adapter->pdev);
2638 atl1_reset_hw(&adapter->hw);
2639 adapter->cmb.cmb->int_stats = 0;
2640
2641 adapter->link_speed = SPEED_0;
2642 adapter->link_duplex = -1;
2643 netif_carrier_off(netdev);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002644
2645 atl1_clean_tx_ring(adapter);
2646 atl1_clean_rx_ring(adapter);
2647}
2648
Jay Cliburn0dde4ef92008-02-02 19:50:11 -06002649static void atl1_tx_timeout_task(struct work_struct *work)
2650{
2651 struct atl1_adapter *adapter =
2652 container_of(work, struct atl1_adapter, tx_timeout_task);
2653 struct net_device *netdev = adapter->netdev;
2654
2655 netif_device_detach(netdev);
2656 atl1_down(adapter);
2657 atl1_up(adapter);
2658 netif_device_attach(netdev);
2659}
2660
2661/*
2662 * atl1_change_mtu - Change the Maximum Transfer Unit
2663 * @netdev: network interface device structure
2664 * @new_mtu: new value for maximum frame size
2665 *
2666 * Returns 0 on success, negative on failure
2667 */
2668static int atl1_change_mtu(struct net_device *netdev, int new_mtu)
2669{
2670 struct atl1_adapter *adapter = netdev_priv(netdev);
2671 int old_mtu = netdev->mtu;
2672 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2673
2674 if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
2675 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2676 if (netif_msg_link(adapter))
2677 dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
2678 return -EINVAL;
2679 }
2680
2681 adapter->hw.max_frame_size = max_frame;
2682 adapter->hw.tx_jumbo_task_th = (max_frame + 7) >> 3;
2683 adapter->rx_buffer_len = (max_frame + 7) & ~7;
2684 adapter->hw.rx_jumbo_th = adapter->rx_buffer_len / 8;
2685
2686 netdev->mtu = new_mtu;
2687 if ((old_mtu != new_mtu) && netif_running(netdev)) {
2688 atl1_down(adapter);
2689 atl1_up(adapter);
2690 }
2691
2692 return 0;
2693}
2694
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002695/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002696 * atl1_open - Called when a network interface is made active
2697 * @netdev: network interface device structure
2698 *
2699 * Returns 0 on success, negative value on failure
2700 *
2701 * The open entry point is called when a network interface is made
2702 * active by the system (IFF_UP). At this point all resources needed
2703 * for transmit and receive operations are allocated, the interrupt
2704 * handler is registered with the OS, the watchdog timer is started,
2705 * and the stack is notified that the interface is ready.
2706 */
2707static int atl1_open(struct net_device *netdev)
2708{
2709 struct atl1_adapter *adapter = netdev_priv(netdev);
2710 int err;
2711
Jay Cliburnb29be6d2008-09-27 04:17:20 +00002712 netif_carrier_off(netdev);
2713
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002714 /* allocate transmit descriptors */
2715 err = atl1_setup_ring_resources(adapter);
2716 if (err)
2717 return err;
2718
2719 err = atl1_up(adapter);
2720 if (err)
2721 goto err_up;
2722
2723 return 0;
2724
2725err_up:
2726 atl1_reset(adapter);
2727 return err;
2728}
2729
2730/*
2731 * atl1_close - Disables a network interface
2732 * @netdev: network interface device structure
2733 *
2734 * Returns 0, this is not allowed to fail
2735 *
2736 * The close entry point is called when an interface is de-activated
2737 * by the OS. The hardware is still under the drivers control, but
2738 * needs to be disabled. A global MAC reset is issued to stop the
2739 * hardware, and all transmit and receive resources are freed.
2740 */
2741static int atl1_close(struct net_device *netdev)
2742{
2743 struct atl1_adapter *adapter = netdev_priv(netdev);
2744 atl1_down(adapter);
2745 atl1_free_ring_resources(adapter);
2746 return 0;
2747}
2748
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002749#ifdef CONFIG_PM
2750static int atl1_suspend(struct pci_dev *pdev, pm_message_t state)
2751{
2752 struct net_device *netdev = pci_get_drvdata(pdev);
2753 struct atl1_adapter *adapter = netdev_priv(netdev);
2754 struct atl1_hw *hw = &adapter->hw;
2755 u32 ctrl = 0;
2756 u32 wufc = adapter->wol;
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002757 u32 val;
2758 int retval;
2759 u16 speed;
2760 u16 duplex;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002761
2762 netif_device_detach(netdev);
2763 if (netif_running(netdev))
2764 atl1_down(adapter);
2765
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002766 retval = pci_save_state(pdev);
2767 if (retval)
2768 return retval;
2769
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002770 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
2771 atl1_read_phy_reg(hw, MII_BMSR, (u16 *) & ctrl);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002772 val = ctrl & BMSR_LSTATUS;
2773 if (val)
Jay Cliburn305282b2008-02-02 19:50:04 -06002774 wufc &= ~ATLX_WUFC_LNKC;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002775
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002776 if (val && wufc) {
2777 val = atl1_get_speed_and_duplex(hw, &speed, &duplex);
2778 if (val) {
2779 if (netif_msg_ifdown(adapter))
2780 dev_printk(KERN_DEBUG, &pdev->dev,
2781 "error getting speed/duplex\n");
2782 goto disable_wol;
2783 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002784
2785 ctrl = 0;
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002786
2787 /* enable magic packet WOL */
Jay Cliburn305282b2008-02-02 19:50:04 -06002788 if (wufc & ATLX_WUFC_MAG)
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002789 ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002790 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002791 ioread32(hw->hw_addr + REG_WOL_CTRL);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002792
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002793 /* configure the mac */
2794 ctrl = MAC_CTRL_RX_EN;
2795 ctrl |= ((u32)((speed == SPEED_1000) ? MAC_CTRL_SPEED_1000 :
2796 MAC_CTRL_SPEED_10_100) << MAC_CTRL_SPEED_SHIFT);
2797 if (duplex == FULL_DUPLEX)
2798 ctrl |= MAC_CTRL_DUPLX;
2799 ctrl |= (((u32)adapter->hw.preamble_len &
2800 MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
2801 if (adapter->vlgrp)
2802 ctrl |= MAC_CTRL_RMV_VLAN;
2803 if (wufc & ATLX_WUFC_MAG)
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002804 ctrl |= MAC_CTRL_BC_EN;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002805 iowrite32(ctrl, hw->hw_addr + REG_MAC_CTRL);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002806 ioread32(hw->hw_addr + REG_MAC_CTRL);
2807
2808 /* poke the PHY */
2809 ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2810 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2811 iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2812 ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2813
2814 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2815 goto exit;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002816 }
2817
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002818 if (!val && wufc) {
2819 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
2820 iowrite32(ctrl, hw->hw_addr + REG_WOL_CTRL);
2821 ioread32(hw->hw_addr + REG_WOL_CTRL);
2822 iowrite32(0, hw->hw_addr + REG_MAC_CTRL);
2823 ioread32(hw->hw_addr + REG_MAC_CTRL);
2824 hw->phy_configured = false;
2825 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
2826 goto exit;
2827 }
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002828
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002829disable_wol:
2830 iowrite32(0, hw->hw_addr + REG_WOL_CTRL);
2831 ioread32(hw->hw_addr + REG_WOL_CTRL);
2832 ctrl = ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
2833 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
2834 iowrite32(ctrl, hw->hw_addr + REG_PCIE_PHYMISC);
2835 ioread32(hw->hw_addr + REG_PCIE_PHYMISC);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002836 hw->phy_configured = false;
2837 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
2838exit:
2839 if (netif_running(netdev))
2840 pci_disable_msi(adapter->pdev);
2841 pci_disable_device(pdev);
2842 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002843
2844 return 0;
2845}
2846
2847static int atl1_resume(struct pci_dev *pdev)
2848{
2849 struct net_device *netdev = pci_get_drvdata(pdev);
2850 struct atl1_adapter *adapter = netdev_priv(netdev);
Jay Cliburn305282b2008-02-02 19:50:04 -06002851 u32 err;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002852
Jay Cliburn305282b2008-02-02 19:50:04 -06002853 pci_set_power_state(pdev, PCI_D0);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002854 pci_restore_state(pdev);
2855
Jay Cliburn305282b2008-02-02 19:50:04 -06002856 err = pci_enable_device(pdev);
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002857 if (err) {
2858 if (netif_msg_ifup(adapter))
2859 dev_printk(KERN_DEBUG, &pdev->dev,
2860 "error enabling pci device\n");
2861 return err;
2862 }
2863
2864 pci_set_master(pdev);
2865 iowrite32(0, adapter->hw.hw_addr + REG_WOL_CTRL);
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002866 pci_enable_wake(pdev, PCI_D3hot, 0);
2867 pci_enable_wake(pdev, PCI_D3cold, 0);
2868
Jay Cliburn08e0f1d2008-05-09 22:12:07 -05002869 atl1_reset_hw(&adapter->hw);
2870 adapter->cmb.cmb->int_stats = 0;
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002871
2872 if (netif_running(netdev))
2873 atl1_up(adapter);
2874 netif_device_attach(netdev);
2875
Jay Cliburn05ffdd72007-07-15 11:03:29 -05002876 return 0;
2877}
2878#else
2879#define atl1_suspend NULL
2880#define atl1_resume NULL
2881#endif
2882
Jay Cliburnbf455a22008-05-09 22:12:08 -05002883static void atl1_shutdown(struct pci_dev *pdev)
2884{
2885#ifdef CONFIG_PM
2886 atl1_suspend(pdev, PMSG_SUSPEND);
2887#endif
2888}
2889
Alexey Dobriyan497f0502007-05-09 18:52:35 +04002890#ifdef CONFIG_NET_POLL_CONTROLLER
2891static void atl1_poll_controller(struct net_device *netdev)
2892{
2893 disable_irq(netdev->irq);
2894 atl1_intr(netdev->irq, netdev);
2895 enable_irq(netdev->irq);
2896}
2897#endif
2898
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002899/*
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002900 * atl1_probe - Device Initialization Routine
2901 * @pdev: PCI device information struct
2902 * @ent: entry in atl1_pci_tbl
2903 *
2904 * Returns 0 on success, negative on failure
2905 *
2906 * atl1_probe initializes an adapter identified by a pci_dev structure.
2907 * The OS initialization, configuring of the adapter private structure,
2908 * and a hardware reset occur.
2909 */
2910static int __devinit atl1_probe(struct pci_dev *pdev,
Jay Cliburn53ffb422007-07-15 11:03:27 -05002911 const struct pci_device_id *ent)
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002912{
2913 struct net_device *netdev;
2914 struct atl1_adapter *adapter;
2915 static int cards_found = 0;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002916 int err;
2917
2918 err = pci_enable_device(pdev);
2919 if (err)
2920 return err;
2921
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002922 /*
Chris Snookcdcc5202007-09-20 15:57:15 -04002923 * The atl1 chip can DMA to 64-bit addresses, but it uses a single
2924 * shared register for the high 32 bits, so only a single, aligned,
2925 * 4 GB physical address range can be used at a time.
2926 *
2927 * Supporting 64-bit DMA on this hardware is more trouble than it's
2928 * worth. It is far easier to limit to 32-bit DMA than update
2929 * various kernel subsystems to support the mechanics required by a
2930 * fixed-high-32-bit system.
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002931 */
2932 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002933 if (err) {
Luca Tettamanti5f08e462007-09-07 20:25:01 -04002934 dev_err(&pdev->dev, "no usable DMA configuration\n");
2935 goto err_dma;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002936 }
Jay Cliburn305282b2008-02-02 19:50:04 -06002937 /*
2938 * Mark all PCI regions associated with PCI device
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002939 * pdev as being reserved by owner atl1_driver_name
2940 */
Jay Cliburn305282b2008-02-02 19:50:04 -06002941 err = pci_request_regions(pdev, ATLX_DRIVER_NAME);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002942 if (err)
2943 goto err_request_regions;
2944
Jay Cliburn305282b2008-02-02 19:50:04 -06002945 /*
2946 * Enables bus-mastering on the device and calls
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002947 * pcibios_set_master to do the needed arch specific settings
2948 */
2949 pci_set_master(pdev);
2950
2951 netdev = alloc_etherdev(sizeof(struct atl1_adapter));
2952 if (!netdev) {
2953 err = -ENOMEM;
2954 goto err_alloc_etherdev;
2955 }
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002956 SET_NETDEV_DEV(netdev, &pdev->dev);
2957
2958 pci_set_drvdata(pdev, netdev);
2959 adapter = netdev_priv(netdev);
2960 adapter->netdev = netdev;
2961 adapter->pdev = pdev;
2962 adapter->hw.back = adapter;
Jay Cliburn460578b2008-02-02 19:50:09 -06002963 adapter->msg_enable = netif_msg_init(debug, atl1_default_msg);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002964
2965 adapter->hw.hw_addr = pci_iomap(pdev, 0, 0);
2966 if (!adapter->hw.hw_addr) {
2967 err = -EIO;
2968 goto err_pci_iomap;
2969 }
2970 /* get device revision number */
Jay Cliburn1e006362007-04-29 21:42:10 -05002971 adapter->hw.dev_rev = ioread16(adapter->hw.hw_addr +
Jay Cliburn53ffb422007-07-15 11:03:27 -05002972 (REG_MASTER_CTRL + 2));
Jay Cliburn460578b2008-02-02 19:50:09 -06002973 if (netif_msg_probe(adapter))
2974 dev_info(&pdev->dev, "version %s\n", ATLX_DRIVER_VERSION);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002975
2976 /* set default ring resource counts */
2977 adapter->rfd_ring.count = adapter->rrd_ring.count = ATL1_DEFAULT_RFD;
2978 adapter->tpd_ring.count = ATL1_DEFAULT_TPD;
2979
2980 adapter->mii.dev = netdev;
2981 adapter->mii.mdio_read = mdio_read;
2982 adapter->mii.mdio_write = mdio_write;
2983 adapter->mii.phy_id_mask = 0x1f;
2984 adapter->mii.reg_num_mask = 0x1f;
2985
2986 netdev->open = &atl1_open;
2987 netdev->stop = &atl1_close;
2988 netdev->hard_start_xmit = &atl1_xmit_frame;
Jay Cliburn305282b2008-02-02 19:50:04 -06002989 netdev->get_stats = &atlx_get_stats;
2990 netdev->set_multicast_list = &atlx_set_multi;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002991 netdev->set_mac_address = &atl1_set_mac;
2992 netdev->change_mtu = &atl1_change_mtu;
Jay Cliburn305282b2008-02-02 19:50:04 -06002993 netdev->do_ioctl = &atlx_ioctl;
2994 netdev->tx_timeout = &atlx_tx_timeout;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05002995 netdev->watchdog_timeo = 5 * HZ;
Alexey Dobriyan497f0502007-05-09 18:52:35 +04002996#ifdef CONFIG_NET_POLL_CONTROLLER
2997 netdev->poll_controller = atl1_poll_controller;
2998#endif
Jay Cliburn305282b2008-02-02 19:50:04 -06002999 netdev->vlan_rx_register = atlx_vlan_rx_register;
Stephen Hemmingercb434e32007-06-01 09:44:00 -07003000
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003001 netdev->ethtool_ops = &atl1_ethtool_ops;
3002 adapter->bd_number = cards_found;
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003003
3004 /* setup the private structure */
3005 err = atl1_sw_init(adapter);
3006 if (err)
3007 goto err_common;
3008
3009 netdev->features = NETIF_F_HW_CSUM;
3010 netdev->features |= NETIF_F_SG;
3011 netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003012
3013 /*
3014 * patch for some L1 of old version,
3015 * the final version of L1 may not need these
3016 * patches
3017 */
3018 /* atl1_pcie_patch(adapter); */
3019
3020 /* really reset GPHY core */
Jay Cliburn305282b2008-02-02 19:50:04 -06003021 iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003022
3023 /*
3024 * reset the controller to
3025 * put the device in a known good starting state
3026 */
3027 if (atl1_reset_hw(&adapter->hw)) {
3028 err = -EIO;
3029 goto err_common;
3030 }
3031
3032 /* copy the MAC address out of the EEPROM */
3033 atl1_read_mac_addr(&adapter->hw);
3034 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
3035
3036 if (!is_valid_ether_addr(netdev->dev_addr)) {
3037 err = -EIO;
3038 goto err_common;
3039 }
3040
3041 atl1_check_options(adapter);
3042
3043 /* pre-init the MAC, and setup link */
3044 err = atl1_init_hw(&adapter->hw);
3045 if (err) {
3046 err = -EIO;
3047 goto err_common;
3048 }
3049
3050 atl1_pcie_patch(adapter);
3051 /* assume we have no link for now */
3052 netif_carrier_off(netdev);
3053 netif_stop_queue(netdev);
3054
3055 init_timer(&adapter->watchdog_timer);
3056 adapter->watchdog_timer.function = &atl1_watchdog;
3057 adapter->watchdog_timer.data = (unsigned long)adapter;
3058
3059 init_timer(&adapter->phy_config_timer);
3060 adapter->phy_config_timer.function = &atl1_phy_config;
3061 adapter->phy_config_timer.data = (unsigned long)adapter;
3062 adapter->phy_timer_pending = false;
3063
3064 INIT_WORK(&adapter->tx_timeout_task, atl1_tx_timeout_task);
3065
Jay Cliburn305282b2008-02-02 19:50:04 -06003066 INIT_WORK(&adapter->link_chg_task, atlx_link_chg_task);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003067
3068 INIT_WORK(&adapter->pcie_dma_to_rst_task, atl1_tx_timeout_task);
3069
3070 err = register_netdev(netdev);
3071 if (err)
3072 goto err_common;
3073
3074 cards_found++;
3075 atl1_via_workaround(adapter);
3076 return 0;
3077
3078err_common:
3079 pci_iounmap(pdev, adapter->hw.hw_addr);
3080err_pci_iomap:
3081 free_netdev(netdev);
3082err_alloc_etherdev:
3083 pci_release_regions(pdev);
3084err_dma:
3085err_request_regions:
3086 pci_disable_device(pdev);
3087 return err;
3088}
3089
3090/*
3091 * atl1_remove - Device Removal Routine
3092 * @pdev: PCI device information struct
3093 *
3094 * atl1_remove is called by the PCI subsystem to alert the driver
3095 * that it should release a PCI device. The could be caused by a
3096 * Hot-Plug event, or because the driver is going to be removed from
3097 * memory.
3098 */
3099static void __devexit atl1_remove(struct pci_dev *pdev)
3100{
3101 struct net_device *netdev = pci_get_drvdata(pdev);
3102 struct atl1_adapter *adapter;
3103 /* Device not available. Return. */
3104 if (!netdev)
3105 return;
3106
3107 adapter = netdev_priv(netdev);
Chris Snook8c754a02007-03-28 20:51:51 -04003108
Jay Cliburn305282b2008-02-02 19:50:04 -06003109 /*
3110 * Some atl1 boards lack persistent storage for their MAC, and get it
Chris Snook8c754a02007-03-28 20:51:51 -04003111 * from the BIOS during POST. If we've been messing with the MAC
3112 * address, we need to save the permanent one.
3113 */
3114 if (memcmp(adapter->hw.mac_addr, adapter->hw.perm_mac_addr, ETH_ALEN)) {
Jay Cliburn53ffb422007-07-15 11:03:27 -05003115 memcpy(adapter->hw.mac_addr, adapter->hw.perm_mac_addr,
3116 ETH_ALEN);
Chris Snook8c754a02007-03-28 20:51:51 -04003117 atl1_set_mac_addr(&adapter->hw);
3118 }
3119
Jay Cliburn305282b2008-02-02 19:50:04 -06003120 iowrite16(0, adapter->hw.hw_addr + REG_PHY_ENABLE);
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003121 unregister_netdev(netdev);
3122 pci_iounmap(pdev, adapter->hw.hw_addr);
3123 pci_release_regions(pdev);
3124 free_netdev(netdev);
3125 pci_disable_device(pdev);
3126}
3127
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003128static struct pci_driver atl1_driver = {
Jay Cliburn305282b2008-02-02 19:50:04 -06003129 .name = ATLX_DRIVER_NAME,
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003130 .id_table = atl1_pci_tbl,
3131 .probe = atl1_probe,
3132 .remove = __devexit_p(atl1_remove),
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003133 .suspend = atl1_suspend,
Jay Cliburnbf455a22008-05-09 22:12:08 -05003134 .resume = atl1_resume,
3135 .shutdown = atl1_shutdown
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003136};
3137
3138/*
3139 * atl1_exit_module - Driver Exit Cleanup Routine
3140 *
3141 * atl1_exit_module is called just before the driver is removed
3142 * from memory.
3143 */
3144static void __exit atl1_exit_module(void)
3145{
3146 pci_unregister_driver(&atl1_driver);
3147}
3148
3149/*
3150 * atl1_init_module - Driver Registration Routine
3151 *
3152 * atl1_init_module is the first routine called when the driver is
3153 * loaded. All it does is register with the PCI subsystem.
3154 */
3155static int __init atl1_init_module(void)
3156{
Jay Cliburnf3cc28c2007-02-08 10:42:37 -05003157 return pci_register_driver(&atl1_driver);
3158}
3159
3160module_init(atl1_init_module);
3161module_exit(atl1_exit_module);
Jay Cliburn305282b2008-02-02 19:50:04 -06003162
3163struct atl1_stats {
3164 char stat_string[ETH_GSTRING_LEN];
3165 int sizeof_stat;
3166 int stat_offset;
3167};
3168
3169#define ATL1_STAT(m) \
3170 sizeof(((struct atl1_adapter *)0)->m), offsetof(struct atl1_adapter, m)
3171
3172static struct atl1_stats atl1_gstrings_stats[] = {
3173 {"rx_packets", ATL1_STAT(soft_stats.rx_packets)},
3174 {"tx_packets", ATL1_STAT(soft_stats.tx_packets)},
3175 {"rx_bytes", ATL1_STAT(soft_stats.rx_bytes)},
3176 {"tx_bytes", ATL1_STAT(soft_stats.tx_bytes)},
3177 {"rx_errors", ATL1_STAT(soft_stats.rx_errors)},
3178 {"tx_errors", ATL1_STAT(soft_stats.tx_errors)},
3179 {"rx_dropped", ATL1_STAT(net_stats.rx_dropped)},
3180 {"tx_dropped", ATL1_STAT(net_stats.tx_dropped)},
3181 {"multicast", ATL1_STAT(soft_stats.multicast)},
3182 {"collisions", ATL1_STAT(soft_stats.collisions)},
3183 {"rx_length_errors", ATL1_STAT(soft_stats.rx_length_errors)},
3184 {"rx_over_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3185 {"rx_crc_errors", ATL1_STAT(soft_stats.rx_crc_errors)},
3186 {"rx_frame_errors", ATL1_STAT(soft_stats.rx_frame_errors)},
3187 {"rx_fifo_errors", ATL1_STAT(soft_stats.rx_fifo_errors)},
3188 {"rx_missed_errors", ATL1_STAT(soft_stats.rx_missed_errors)},
3189 {"tx_aborted_errors", ATL1_STAT(soft_stats.tx_aborted_errors)},
3190 {"tx_carrier_errors", ATL1_STAT(soft_stats.tx_carrier_errors)},
3191 {"tx_fifo_errors", ATL1_STAT(soft_stats.tx_fifo_errors)},
3192 {"tx_window_errors", ATL1_STAT(soft_stats.tx_window_errors)},
3193 {"tx_abort_exce_coll", ATL1_STAT(soft_stats.excecol)},
3194 {"tx_abort_late_coll", ATL1_STAT(soft_stats.latecol)},
3195 {"tx_deferred_ok", ATL1_STAT(soft_stats.deffer)},
3196 {"tx_single_coll_ok", ATL1_STAT(soft_stats.scc)},
3197 {"tx_multi_coll_ok", ATL1_STAT(soft_stats.mcc)},
3198 {"tx_underun", ATL1_STAT(soft_stats.tx_underun)},
3199 {"tx_trunc", ATL1_STAT(soft_stats.tx_trunc)},
3200 {"tx_pause", ATL1_STAT(soft_stats.tx_pause)},
3201 {"rx_pause", ATL1_STAT(soft_stats.rx_pause)},
3202 {"rx_rrd_ov", ATL1_STAT(soft_stats.rx_rrd_ov)},
3203 {"rx_trunc", ATL1_STAT(soft_stats.rx_trunc)}
3204};
3205
3206static void atl1_get_ethtool_stats(struct net_device *netdev,
3207 struct ethtool_stats *stats, u64 *data)
3208{
3209 struct atl1_adapter *adapter = netdev_priv(netdev);
3210 int i;
3211 char *p;
3212
3213 for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3214 p = (char *)adapter+atl1_gstrings_stats[i].stat_offset;
3215 data[i] = (atl1_gstrings_stats[i].sizeof_stat ==
3216 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
3217 }
3218
3219}
3220
3221static int atl1_get_sset_count(struct net_device *netdev, int sset)
3222{
3223 switch (sset) {
3224 case ETH_SS_STATS:
3225 return ARRAY_SIZE(atl1_gstrings_stats);
3226 default:
3227 return -EOPNOTSUPP;
3228 }
3229}
3230
3231static int atl1_get_settings(struct net_device *netdev,
3232 struct ethtool_cmd *ecmd)
3233{
3234 struct atl1_adapter *adapter = netdev_priv(netdev);
3235 struct atl1_hw *hw = &adapter->hw;
3236
3237 ecmd->supported = (SUPPORTED_10baseT_Half |
3238 SUPPORTED_10baseT_Full |
3239 SUPPORTED_100baseT_Half |
3240 SUPPORTED_100baseT_Full |
3241 SUPPORTED_1000baseT_Full |
3242 SUPPORTED_Autoneg | SUPPORTED_TP);
3243 ecmd->advertising = ADVERTISED_TP;
3244 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3245 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3246 ecmd->advertising |= ADVERTISED_Autoneg;
3247 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR) {
3248 ecmd->advertising |= ADVERTISED_Autoneg;
3249 ecmd->advertising |=
3250 (ADVERTISED_10baseT_Half |
3251 ADVERTISED_10baseT_Full |
3252 ADVERTISED_100baseT_Half |
3253 ADVERTISED_100baseT_Full |
3254 ADVERTISED_1000baseT_Full);
3255 } else
3256 ecmd->advertising |= (ADVERTISED_1000baseT_Full);
3257 }
3258 ecmd->port = PORT_TP;
3259 ecmd->phy_address = 0;
3260 ecmd->transceiver = XCVR_INTERNAL;
3261
3262 if (netif_carrier_ok(adapter->netdev)) {
3263 u16 link_speed, link_duplex;
3264 atl1_get_speed_and_duplex(hw, &link_speed, &link_duplex);
3265 ecmd->speed = link_speed;
3266 if (link_duplex == FULL_DUPLEX)
3267 ecmd->duplex = DUPLEX_FULL;
3268 else
3269 ecmd->duplex = DUPLEX_HALF;
3270 } else {
3271 ecmd->speed = -1;
3272 ecmd->duplex = -1;
3273 }
3274 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3275 hw->media_type == MEDIA_TYPE_1000M_FULL)
3276 ecmd->autoneg = AUTONEG_ENABLE;
3277 else
3278 ecmd->autoneg = AUTONEG_DISABLE;
3279
3280 return 0;
3281}
3282
3283static int atl1_set_settings(struct net_device *netdev,
3284 struct ethtool_cmd *ecmd)
3285{
3286 struct atl1_adapter *adapter = netdev_priv(netdev);
3287 struct atl1_hw *hw = &adapter->hw;
3288 u16 phy_data;
3289 int ret_val = 0;
3290 u16 old_media_type = hw->media_type;
3291
3292 if (netif_running(adapter->netdev)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003293 if (netif_msg_link(adapter))
3294 dev_dbg(&adapter->pdev->dev,
3295 "ethtool shutting down adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003296 atl1_down(adapter);
3297 }
3298
3299 if (ecmd->autoneg == AUTONEG_ENABLE)
3300 hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
3301 else {
3302 if (ecmd->speed == SPEED_1000) {
3303 if (ecmd->duplex != DUPLEX_FULL) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003304 if (netif_msg_link(adapter))
3305 dev_warn(&adapter->pdev->dev,
3306 "1000M half is invalid\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003307 ret_val = -EINVAL;
3308 goto exit_sset;
3309 }
3310 hw->media_type = MEDIA_TYPE_1000M_FULL;
3311 } else if (ecmd->speed == SPEED_100) {
3312 if (ecmd->duplex == DUPLEX_FULL)
3313 hw->media_type = MEDIA_TYPE_100M_FULL;
3314 else
3315 hw->media_type = MEDIA_TYPE_100M_HALF;
3316 } else {
3317 if (ecmd->duplex == DUPLEX_FULL)
3318 hw->media_type = MEDIA_TYPE_10M_FULL;
3319 else
3320 hw->media_type = MEDIA_TYPE_10M_HALF;
3321 }
3322 }
3323 switch (hw->media_type) {
3324 case MEDIA_TYPE_AUTO_SENSOR:
3325 ecmd->advertising =
3326 ADVERTISED_10baseT_Half |
3327 ADVERTISED_10baseT_Full |
3328 ADVERTISED_100baseT_Half |
3329 ADVERTISED_100baseT_Full |
3330 ADVERTISED_1000baseT_Full |
3331 ADVERTISED_Autoneg | ADVERTISED_TP;
3332 break;
3333 case MEDIA_TYPE_1000M_FULL:
3334 ecmd->advertising =
3335 ADVERTISED_1000baseT_Full |
3336 ADVERTISED_Autoneg | ADVERTISED_TP;
3337 break;
3338 default:
3339 ecmd->advertising = 0;
3340 break;
3341 }
3342 if (atl1_phy_setup_autoneg_adv(hw)) {
3343 ret_val = -EINVAL;
Jay Cliburn460578b2008-02-02 19:50:09 -06003344 if (netif_msg_link(adapter))
3345 dev_warn(&adapter->pdev->dev,
3346 "invalid ethtool speed/duplex setting\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003347 goto exit_sset;
3348 }
3349 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3350 hw->media_type == MEDIA_TYPE_1000M_FULL)
3351 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3352 else {
3353 switch (hw->media_type) {
3354 case MEDIA_TYPE_100M_FULL:
3355 phy_data =
3356 MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
3357 MII_CR_RESET;
3358 break;
3359 case MEDIA_TYPE_100M_HALF:
3360 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3361 break;
3362 case MEDIA_TYPE_10M_FULL:
3363 phy_data =
3364 MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
3365 break;
3366 default:
3367 /* MEDIA_TYPE_10M_HALF: */
3368 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3369 break;
3370 }
3371 }
3372 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3373exit_sset:
3374 if (ret_val)
3375 hw->media_type = old_media_type;
3376
3377 if (netif_running(adapter->netdev)) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003378 if (netif_msg_link(adapter))
3379 dev_dbg(&adapter->pdev->dev,
3380 "ethtool starting adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003381 atl1_up(adapter);
3382 } else if (!ret_val) {
Jay Cliburn460578b2008-02-02 19:50:09 -06003383 if (netif_msg_link(adapter))
3384 dev_dbg(&adapter->pdev->dev,
3385 "ethtool resetting adapter\n");
Jay Cliburn305282b2008-02-02 19:50:04 -06003386 atl1_reset(adapter);
3387 }
3388 return ret_val;
3389}
3390
3391static void atl1_get_drvinfo(struct net_device *netdev,
3392 struct ethtool_drvinfo *drvinfo)
3393{
3394 struct atl1_adapter *adapter = netdev_priv(netdev);
3395
3396 strncpy(drvinfo->driver, ATLX_DRIVER_NAME, sizeof(drvinfo->driver));
3397 strncpy(drvinfo->version, ATLX_DRIVER_VERSION,
3398 sizeof(drvinfo->version));
3399 strncpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
3400 strncpy(drvinfo->bus_info, pci_name(adapter->pdev),
3401 sizeof(drvinfo->bus_info));
3402 drvinfo->eedump_len = ATL1_EEDUMP_LEN;
3403}
3404
3405static void atl1_get_wol(struct net_device *netdev,
3406 struct ethtool_wolinfo *wol)
3407{
3408 struct atl1_adapter *adapter = netdev_priv(netdev);
3409
3410 wol->supported = WAKE_UCAST | WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
3411 wol->wolopts = 0;
3412 if (adapter->wol & ATLX_WUFC_EX)
3413 wol->wolopts |= WAKE_UCAST;
3414 if (adapter->wol & ATLX_WUFC_MC)
3415 wol->wolopts |= WAKE_MCAST;
3416 if (adapter->wol & ATLX_WUFC_BC)
3417 wol->wolopts |= WAKE_BCAST;
3418 if (adapter->wol & ATLX_WUFC_MAG)
3419 wol->wolopts |= WAKE_MAGIC;
3420 return;
3421}
3422
3423static int atl1_set_wol(struct net_device *netdev,
3424 struct ethtool_wolinfo *wol)
3425{
3426 struct atl1_adapter *adapter = netdev_priv(netdev);
3427
3428 if (wol->wolopts & (WAKE_PHY | WAKE_ARP | WAKE_MAGICSECURE))
3429 return -EOPNOTSUPP;
3430 adapter->wol = 0;
3431 if (wol->wolopts & WAKE_UCAST)
3432 adapter->wol |= ATLX_WUFC_EX;
3433 if (wol->wolopts & WAKE_MCAST)
3434 adapter->wol |= ATLX_WUFC_MC;
3435 if (wol->wolopts & WAKE_BCAST)
3436 adapter->wol |= ATLX_WUFC_BC;
3437 if (wol->wolopts & WAKE_MAGIC)
3438 adapter->wol |= ATLX_WUFC_MAG;
3439 return 0;
3440}
3441
Jay Cliburn460578b2008-02-02 19:50:09 -06003442static u32 atl1_get_msglevel(struct net_device *netdev)
3443{
3444 struct atl1_adapter *adapter = netdev_priv(netdev);
3445 return adapter->msg_enable;
3446}
3447
3448static void atl1_set_msglevel(struct net_device *netdev, u32 value)
3449{
3450 struct atl1_adapter *adapter = netdev_priv(netdev);
3451 adapter->msg_enable = value;
3452}
3453
Jay Cliburnc67c9a22008-02-02 19:50:06 -06003454static int atl1_get_regs_len(struct net_device *netdev)
3455{
3456 return ATL1_REG_COUNT * sizeof(u32);
3457}
3458
3459static void atl1_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
3460 void *p)
3461{
3462 struct atl1_adapter *adapter = netdev_priv(netdev);
3463 struct atl1_hw *hw = &adapter->hw;
3464 unsigned int i;
3465 u32 *regbuf = p;
3466
3467 for (i = 0; i < ATL1_REG_COUNT; i++) {
3468 /*
3469 * This switch statement avoids reserved regions
3470 * of register space.
3471 */
3472 switch (i) {
3473 case 6 ... 9:
3474 case 14:
3475 case 29 ... 31:
3476 case 34 ... 63:
3477 case 75 ... 127:
3478 case 136 ... 1023:
3479 case 1027 ... 1087:
3480 case 1091 ... 1151:
3481 case 1194 ... 1195:
3482 case 1200 ... 1201:
3483 case 1206 ... 1213:
3484 case 1216 ... 1279:
3485 case 1290 ... 1311:
3486 case 1323 ... 1343:
3487 case 1358 ... 1359:
3488 case 1368 ... 1375:
3489 case 1378 ... 1383:
3490 case 1388 ... 1391:
3491 case 1393 ... 1395:
3492 case 1402 ... 1403:
3493 case 1410 ... 1471:
3494 case 1522 ... 1535:
3495 /* reserved region; don't read it */
3496 regbuf[i] = 0;
3497 break;
3498 default:
3499 /* unreserved region */
3500 regbuf[i] = ioread32(hw->hw_addr + (i * sizeof(u32)));
3501 }
3502 }
3503}
3504
Jay Cliburn305282b2008-02-02 19:50:04 -06003505static void atl1_get_ringparam(struct net_device *netdev,
3506 struct ethtool_ringparam *ring)
3507{
3508 struct atl1_adapter *adapter = netdev_priv(netdev);
3509 struct atl1_tpd_ring *txdr = &adapter->tpd_ring;
3510 struct atl1_rfd_ring *rxdr = &adapter->rfd_ring;
3511
3512 ring->rx_max_pending = ATL1_MAX_RFD;
3513 ring->tx_max_pending = ATL1_MAX_TPD;
3514 ring->rx_mini_max_pending = 0;
3515 ring->rx_jumbo_max_pending = 0;
3516 ring->rx_pending = rxdr->count;
3517 ring->tx_pending = txdr->count;
3518 ring->rx_mini_pending = 0;
3519 ring->rx_jumbo_pending = 0;
3520}
3521
3522static int atl1_set_ringparam(struct net_device *netdev,
3523 struct ethtool_ringparam *ring)
3524{
3525 struct atl1_adapter *adapter = netdev_priv(netdev);
3526 struct atl1_tpd_ring *tpdr = &adapter->tpd_ring;
3527 struct atl1_rrd_ring *rrdr = &adapter->rrd_ring;
3528 struct atl1_rfd_ring *rfdr = &adapter->rfd_ring;
3529
3530 struct atl1_tpd_ring tpd_old, tpd_new;
3531 struct atl1_rfd_ring rfd_old, rfd_new;
3532 struct atl1_rrd_ring rrd_old, rrd_new;
3533 struct atl1_ring_header rhdr_old, rhdr_new;
3534 int err;
3535
3536 tpd_old = adapter->tpd_ring;
3537 rfd_old = adapter->rfd_ring;
3538 rrd_old = adapter->rrd_ring;
3539 rhdr_old = adapter->ring_header;
3540
3541 if (netif_running(adapter->netdev))
3542 atl1_down(adapter);
3543
3544 rfdr->count = (u16) max(ring->rx_pending, (u32) ATL1_MIN_RFD);
3545 rfdr->count = rfdr->count > ATL1_MAX_RFD ? ATL1_MAX_RFD :
3546 rfdr->count;
3547 rfdr->count = (rfdr->count + 3) & ~3;
3548 rrdr->count = rfdr->count;
3549
3550 tpdr->count = (u16) max(ring->tx_pending, (u32) ATL1_MIN_TPD);
3551 tpdr->count = tpdr->count > ATL1_MAX_TPD ? ATL1_MAX_TPD :
3552 tpdr->count;
3553 tpdr->count = (tpdr->count + 3) & ~3;
3554
3555 if (netif_running(adapter->netdev)) {
3556 /* try to get new resources before deleting old */
3557 err = atl1_setup_ring_resources(adapter);
3558 if (err)
3559 goto err_setup_ring;
3560
3561 /*
3562 * save the new, restore the old in order to free it,
3563 * then restore the new back again
3564 */
3565
3566 rfd_new = adapter->rfd_ring;
3567 rrd_new = adapter->rrd_ring;
3568 tpd_new = adapter->tpd_ring;
3569 rhdr_new = adapter->ring_header;
3570 adapter->rfd_ring = rfd_old;
3571 adapter->rrd_ring = rrd_old;
3572 adapter->tpd_ring = tpd_old;
3573 adapter->ring_header = rhdr_old;
3574 atl1_free_ring_resources(adapter);
3575 adapter->rfd_ring = rfd_new;
3576 adapter->rrd_ring = rrd_new;
3577 adapter->tpd_ring = tpd_new;
3578 adapter->ring_header = rhdr_new;
3579
3580 err = atl1_up(adapter);
3581 if (err)
3582 return err;
3583 }
3584 return 0;
3585
3586err_setup_ring:
3587 adapter->rfd_ring = rfd_old;
3588 adapter->rrd_ring = rrd_old;
3589 adapter->tpd_ring = tpd_old;
3590 adapter->ring_header = rhdr_old;
3591 atl1_up(adapter);
3592 return err;
3593}
3594
3595static void atl1_get_pauseparam(struct net_device *netdev,
3596 struct ethtool_pauseparam *epause)
3597{
3598 struct atl1_adapter *adapter = netdev_priv(netdev);
3599 struct atl1_hw *hw = &adapter->hw;
3600
3601 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3602 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3603 epause->autoneg = AUTONEG_ENABLE;
3604 } else {
3605 epause->autoneg = AUTONEG_DISABLE;
3606 }
3607 epause->rx_pause = 1;
3608 epause->tx_pause = 1;
3609}
3610
3611static int atl1_set_pauseparam(struct net_device *netdev,
3612 struct ethtool_pauseparam *epause)
3613{
3614 struct atl1_adapter *adapter = netdev_priv(netdev);
3615 struct atl1_hw *hw = &adapter->hw;
3616
3617 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3618 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3619 epause->autoneg = AUTONEG_ENABLE;
3620 } else {
3621 epause->autoneg = AUTONEG_DISABLE;
3622 }
3623
3624 epause->rx_pause = 1;
3625 epause->tx_pause = 1;
3626
3627 return 0;
3628}
3629
3630/* FIXME: is this right? -- CHS */
3631static u32 atl1_get_rx_csum(struct net_device *netdev)
3632{
3633 return 1;
3634}
3635
3636static void atl1_get_strings(struct net_device *netdev, u32 stringset,
3637 u8 *data)
3638{
3639 u8 *p = data;
3640 int i;
3641
3642 switch (stringset) {
3643 case ETH_SS_STATS:
3644 for (i = 0; i < ARRAY_SIZE(atl1_gstrings_stats); i++) {
3645 memcpy(p, atl1_gstrings_stats[i].stat_string,
3646 ETH_GSTRING_LEN);
3647 p += ETH_GSTRING_LEN;
3648 }
3649 break;
3650 }
3651}
3652
3653static int atl1_nway_reset(struct net_device *netdev)
3654{
3655 struct atl1_adapter *adapter = netdev_priv(netdev);
3656 struct atl1_hw *hw = &adapter->hw;
3657
3658 if (netif_running(netdev)) {
3659 u16 phy_data;
3660 atl1_down(adapter);
3661
3662 if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
3663 hw->media_type == MEDIA_TYPE_1000M_FULL) {
3664 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
3665 } else {
3666 switch (hw->media_type) {
3667 case MEDIA_TYPE_100M_FULL:
3668 phy_data = MII_CR_FULL_DUPLEX |
3669 MII_CR_SPEED_100 | MII_CR_RESET;
3670 break;
3671 case MEDIA_TYPE_100M_HALF:
3672 phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
3673 break;
3674 case MEDIA_TYPE_10M_FULL:
3675 phy_data = MII_CR_FULL_DUPLEX |
3676 MII_CR_SPEED_10 | MII_CR_RESET;
3677 break;
3678 default:
3679 /* MEDIA_TYPE_10M_HALF */
3680 phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
3681 }
3682 }
3683 atl1_write_phy_reg(hw, MII_BMCR, phy_data);
3684 atl1_up(adapter);
3685 }
3686 return 0;
3687}
3688
3689const struct ethtool_ops atl1_ethtool_ops = {
3690 .get_settings = atl1_get_settings,
3691 .set_settings = atl1_set_settings,
3692 .get_drvinfo = atl1_get_drvinfo,
3693 .get_wol = atl1_get_wol,
3694 .set_wol = atl1_set_wol,
Jay Cliburn460578b2008-02-02 19:50:09 -06003695 .get_msglevel = atl1_get_msglevel,
3696 .set_msglevel = atl1_set_msglevel,
Jay Cliburnc67c9a22008-02-02 19:50:06 -06003697 .get_regs_len = atl1_get_regs_len,
3698 .get_regs = atl1_get_regs,
Jay Cliburn305282b2008-02-02 19:50:04 -06003699 .get_ringparam = atl1_get_ringparam,
3700 .set_ringparam = atl1_set_ringparam,
3701 .get_pauseparam = atl1_get_pauseparam,
Jay Cliburn401c0aa2008-02-02 19:50:07 -06003702 .set_pauseparam = atl1_set_pauseparam,
Jay Cliburn305282b2008-02-02 19:50:04 -06003703 .get_rx_csum = atl1_get_rx_csum,
3704 .set_tx_csum = ethtool_op_set_tx_hw_csum,
3705 .get_link = ethtool_op_get_link,
3706 .set_sg = ethtool_op_set_sg,
3707 .get_strings = atl1_get_strings,
3708 .nway_reset = atl1_nway_reset,
3709 .get_ethtool_stats = atl1_get_ethtool_stats,
3710 .get_sset_count = atl1_get_sset_count,
3711 .set_tso = ethtool_op_set_tso,
3712};