Save sram context after changing MPU, DSP or core clocks
[linux-ginger.git] / drivers / net / atlx / atl2.c
blobab688862093f41c7642c5a43b7f9b3262efbe7ce
1 /*
2 * Copyright(c) 2006 - 2007 Atheros Corporation. All rights reserved.
3 * Copyright(c) 2007 - 2008 Chris Snook <csnook@redhat.com>
5 * Derived from Intel e1000 driver
6 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59
20 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <asm/atomic.h>
24 #include <linux/crc32.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/hardirq.h>
29 #include <linux/if_vlan.h>
30 #include <linux/in.h>
31 #include <linux/interrupt.h>
32 #include <linux/ip.h>
33 #include <linux/irqflags.h>
34 #include <linux/irqreturn.h>
35 #include <linux/mii.h>
36 #include <linux/net.h>
37 #include <linux/netdevice.h>
38 #include <linux/pci.h>
39 #include <linux/pci_ids.h>
40 #include <linux/pm.h>
41 #include <linux/skbuff.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/tcp.h>
45 #include <linux/timer.h>
46 #include <linux/types.h>
47 #include <linux/workqueue.h>
49 #include "atl2.h"
51 #define ATL2_DRV_VERSION "2.2.3"
53 static char atl2_driver_name[] = "atl2";
54 static const char atl2_driver_string[] = "Atheros(R) L2 Ethernet Driver";
55 static char atl2_copyright[] = "Copyright (c) 2007 Atheros Corporation.";
56 static char atl2_driver_version[] = ATL2_DRV_VERSION;
58 MODULE_AUTHOR("Atheros Corporation <xiong.huang@atheros.com>, Chris Snook <csnook@redhat.com>");
59 MODULE_DESCRIPTION("Atheros Fast Ethernet Network Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(ATL2_DRV_VERSION);
64 * atl2_pci_tbl - PCI Device ID Table
66 static struct pci_device_id atl2_pci_tbl[] = {
67 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2)},
68 /* required last entry */
69 {0,}
71 MODULE_DEVICE_TABLE(pci, atl2_pci_tbl);
73 static void atl2_set_ethtool_ops(struct net_device *netdev);
75 static void atl2_check_options(struct atl2_adapter *adapter);
78 * atl2_sw_init - Initialize general software structures (struct atl2_adapter)
79 * @adapter: board private structure to initialize
81 * atl2_sw_init initializes the Adapter private data structure.
82 * Fields are initialized based on PCI device information and
83 * OS network device settings (MTU size).
85 static int __devinit atl2_sw_init(struct atl2_adapter *adapter)
87 struct atl2_hw *hw = &adapter->hw;
88 struct pci_dev *pdev = adapter->pdev;
90 /* PCI config space info */
91 hw->vendor_id = pdev->vendor;
92 hw->device_id = pdev->device;
93 hw->subsystem_vendor_id = pdev->subsystem_vendor;
94 hw->subsystem_id = pdev->subsystem_device;
96 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
97 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
99 adapter->wol = 0;
100 adapter->ict = 50000; /* ~100ms */
101 adapter->link_speed = SPEED_0; /* hardware init */
102 adapter->link_duplex = FULL_DUPLEX;
104 hw->phy_configured = false;
105 hw->preamble_len = 7;
106 hw->ipgt = 0x60;
107 hw->min_ifg = 0x50;
108 hw->ipgr1 = 0x40;
109 hw->ipgr2 = 0x60;
110 hw->retry_buf = 2;
111 hw->max_retry = 0xf;
112 hw->lcol = 0x37;
113 hw->jam_ipg = 7;
114 hw->fc_rxd_hi = 0;
115 hw->fc_rxd_lo = 0;
116 hw->max_frame_size = adapter->netdev->mtu;
118 spin_lock_init(&adapter->stats_lock);
120 set_bit(__ATL2_DOWN, &adapter->flags);
122 return 0;
126 * atl2_set_multi - Multicast and Promiscuous mode set
127 * @netdev: network interface device structure
129 * The set_multi entry point is called whenever the multicast address
130 * list or the network interface flags are updated. This routine is
131 * responsible for configuring the hardware for proper multicast,
132 * promiscuous mode, and all-multi behavior.
134 static void atl2_set_multi(struct net_device *netdev)
136 struct atl2_adapter *adapter = netdev_priv(netdev);
137 struct atl2_hw *hw = &adapter->hw;
138 struct dev_mc_list *mc_ptr;
139 u32 rctl;
140 u32 hash_value;
142 /* Check for Promiscuous and All Multicast modes */
143 rctl = ATL2_READ_REG(hw, REG_MAC_CTRL);
145 if (netdev->flags & IFF_PROMISC) {
146 rctl |= MAC_CTRL_PROMIS_EN;
147 } else if (netdev->flags & IFF_ALLMULTI) {
148 rctl |= MAC_CTRL_MC_ALL_EN;
149 rctl &= ~MAC_CTRL_PROMIS_EN;
150 } else
151 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
153 ATL2_WRITE_REG(hw, REG_MAC_CTRL, rctl);
155 /* clear the old settings from the multicast hash table */
156 ATL2_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
157 ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
159 /* comoute mc addresses' hash value ,and put it into hash table */
160 for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
161 hash_value = atl2_hash_mc_addr(hw, mc_ptr->dmi_addr);
162 atl2_hash_set(hw, hash_value);
166 static void init_ring_ptrs(struct atl2_adapter *adapter)
168 /* Read / Write Ptr Initialize: */
169 adapter->txd_write_ptr = 0;
170 atomic_set(&adapter->txd_read_ptr, 0);
172 adapter->rxd_read_ptr = 0;
173 adapter->rxd_write_ptr = 0;
175 atomic_set(&adapter->txs_write_ptr, 0);
176 adapter->txs_next_clear = 0;
180 * atl2_configure - Configure Transmit&Receive Unit after Reset
181 * @adapter: board private structure
183 * Configure the Tx /Rx unit of the MAC after a reset.
185 static int atl2_configure(struct atl2_adapter *adapter)
187 struct atl2_hw *hw = &adapter->hw;
188 u32 value;
190 /* clear interrupt status */
191 ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0xffffffff);
193 /* set MAC Address */
194 value = (((u32)hw->mac_addr[2]) << 24) |
195 (((u32)hw->mac_addr[3]) << 16) |
196 (((u32)hw->mac_addr[4]) << 8) |
197 (((u32)hw->mac_addr[5]));
198 ATL2_WRITE_REG(hw, REG_MAC_STA_ADDR, value);
199 value = (((u32)hw->mac_addr[0]) << 8) |
200 (((u32)hw->mac_addr[1]));
201 ATL2_WRITE_REG(hw, (REG_MAC_STA_ADDR+4), value);
203 /* HI base address */
204 ATL2_WRITE_REG(hw, REG_DESC_BASE_ADDR_HI,
205 (u32)((adapter->ring_dma & 0xffffffff00000000ULL) >> 32));
207 /* LO base address */
208 ATL2_WRITE_REG(hw, REG_TXD_BASE_ADDR_LO,
209 (u32)(adapter->txd_dma & 0x00000000ffffffffULL));
210 ATL2_WRITE_REG(hw, REG_TXS_BASE_ADDR_LO,
211 (u32)(adapter->txs_dma & 0x00000000ffffffffULL));
212 ATL2_WRITE_REG(hw, REG_RXD_BASE_ADDR_LO,
213 (u32)(adapter->rxd_dma & 0x00000000ffffffffULL));
215 /* element count */
216 ATL2_WRITE_REGW(hw, REG_TXD_MEM_SIZE, (u16)(adapter->txd_ring_size/4));
217 ATL2_WRITE_REGW(hw, REG_TXS_MEM_SIZE, (u16)adapter->txs_ring_size);
218 ATL2_WRITE_REGW(hw, REG_RXD_BUF_NUM, (u16)adapter->rxd_ring_size);
220 /* config Internal SRAM */
222 ATL2_WRITE_REGW(hw, REG_SRAM_TXRAM_END, sram_tx_end);
223 ATL2_WRITE_REGW(hw, REG_SRAM_TXRAM_END, sram_rx_end);
226 /* config IPG/IFG */
227 value = (((u32)hw->ipgt & MAC_IPG_IFG_IPGT_MASK) <<
228 MAC_IPG_IFG_IPGT_SHIFT) |
229 (((u32)hw->min_ifg & MAC_IPG_IFG_MIFG_MASK) <<
230 MAC_IPG_IFG_MIFG_SHIFT) |
231 (((u32)hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK) <<
232 MAC_IPG_IFG_IPGR1_SHIFT)|
233 (((u32)hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK) <<
234 MAC_IPG_IFG_IPGR2_SHIFT);
235 ATL2_WRITE_REG(hw, REG_MAC_IPG_IFG, value);
237 /* config Half-Duplex Control */
238 value = ((u32)hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
239 (((u32)hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK) <<
240 MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
241 MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
242 (0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
243 (((u32)hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK) <<
244 MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
245 ATL2_WRITE_REG(hw, REG_MAC_HALF_DUPLX_CTRL, value);
247 /* set Interrupt Moderator Timer */
248 ATL2_WRITE_REGW(hw, REG_IRQ_MODU_TIMER_INIT, adapter->imt);
249 ATL2_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_ITIMER_EN);
251 /* set Interrupt Clear Timer */
252 ATL2_WRITE_REGW(hw, REG_CMBDISDMA_TIMER, adapter->ict);
254 /* set MTU */
255 ATL2_WRITE_REG(hw, REG_MTU, adapter->netdev->mtu +
256 ENET_HEADER_SIZE + VLAN_SIZE + ETHERNET_FCS_SIZE);
258 /* 1590 */
259 ATL2_WRITE_REG(hw, REG_TX_CUT_THRESH, 0x177);
261 /* flow control */
262 ATL2_WRITE_REGW(hw, REG_PAUSE_ON_TH, hw->fc_rxd_hi);
263 ATL2_WRITE_REGW(hw, REG_PAUSE_OFF_TH, hw->fc_rxd_lo);
265 /* Init mailbox */
266 ATL2_WRITE_REGW(hw, REG_MB_TXD_WR_IDX, (u16)adapter->txd_write_ptr);
267 ATL2_WRITE_REGW(hw, REG_MB_RXD_RD_IDX, (u16)adapter->rxd_read_ptr);
269 /* enable DMA read/write */
270 ATL2_WRITE_REGB(hw, REG_DMAR, DMAR_EN);
271 ATL2_WRITE_REGB(hw, REG_DMAW, DMAW_EN);
273 value = ATL2_READ_REG(&adapter->hw, REG_ISR);
274 if ((value & ISR_PHY_LINKDOWN) != 0)
275 value = 1; /* config failed */
276 else
277 value = 0;
279 /* clear all interrupt status */
280 ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0x3fffffff);
281 ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0);
282 return value;
286 * atl2_setup_ring_resources - allocate Tx / RX descriptor resources
287 * @adapter: board private structure
289 * Return 0 on success, negative on failure
291 static s32 atl2_setup_ring_resources(struct atl2_adapter *adapter)
293 struct pci_dev *pdev = adapter->pdev;
294 int size;
295 u8 offset = 0;
297 /* real ring DMA buffer */
298 adapter->ring_size = size =
299 adapter->txd_ring_size * 1 + 7 + /* dword align */
300 adapter->txs_ring_size * 4 + 7 + /* dword align */
301 adapter->rxd_ring_size * 1536 + 127; /* 128bytes align */
303 adapter->ring_vir_addr = pci_alloc_consistent(pdev, size,
304 &adapter->ring_dma);
305 if (!adapter->ring_vir_addr)
306 return -ENOMEM;
307 memset(adapter->ring_vir_addr, 0, adapter->ring_size);
309 /* Init TXD Ring */
310 adapter->txd_dma = adapter->ring_dma ;
311 offset = (adapter->txd_dma & 0x7) ? (8 - (adapter->txd_dma & 0x7)) : 0;
312 adapter->txd_dma += offset;
313 adapter->txd_ring = (struct tx_pkt_header *) (adapter->ring_vir_addr +
314 offset);
316 /* Init TXS Ring */
317 adapter->txs_dma = adapter->txd_dma + adapter->txd_ring_size;
318 offset = (adapter->txs_dma & 0x7) ? (8 - (adapter->txs_dma & 0x7)) : 0;
319 adapter->txs_dma += offset;
320 adapter->txs_ring = (struct tx_pkt_status *)
321 (((u8 *)adapter->txd_ring) + (adapter->txd_ring_size + offset));
323 /* Init RXD Ring */
324 adapter->rxd_dma = adapter->txs_dma + adapter->txs_ring_size * 4;
325 offset = (adapter->rxd_dma & 127) ?
326 (128 - (adapter->rxd_dma & 127)) : 0;
327 if (offset > 7)
328 offset -= 8;
329 else
330 offset += (128 - 8);
332 adapter->rxd_dma += offset;
333 adapter->rxd_ring = (struct rx_desc *) (((u8 *)adapter->txs_ring) +
334 (adapter->txs_ring_size * 4 + offset));
337 * Read / Write Ptr Initialize:
338 * init_ring_ptrs(adapter);
340 return 0;
344 * atl2_irq_enable - Enable default interrupt generation settings
345 * @adapter: board private structure
347 static inline void atl2_irq_enable(struct atl2_adapter *adapter)
349 ATL2_WRITE_REG(&adapter->hw, REG_IMR, IMR_NORMAL_MASK);
350 ATL2_WRITE_FLUSH(&adapter->hw);
354 * atl2_irq_disable - Mask off interrupt generation on the NIC
355 * @adapter: board private structure
357 static inline void atl2_irq_disable(struct atl2_adapter *adapter)
359 ATL2_WRITE_REG(&adapter->hw, REG_IMR, 0);
360 ATL2_WRITE_FLUSH(&adapter->hw);
361 synchronize_irq(adapter->pdev->irq);
364 #ifdef NETIF_F_HW_VLAN_TX
365 static void atl2_vlan_rx_register(struct net_device *netdev,
366 struct vlan_group *grp)
368 struct atl2_adapter *adapter = netdev_priv(netdev);
369 u32 ctrl;
371 atl2_irq_disable(adapter);
372 adapter->vlgrp = grp;
374 if (grp) {
375 /* enable VLAN tag insert/strip */
376 ctrl = ATL2_READ_REG(&adapter->hw, REG_MAC_CTRL);
377 ctrl |= MAC_CTRL_RMV_VLAN;
378 ATL2_WRITE_REG(&adapter->hw, REG_MAC_CTRL, ctrl);
379 } else {
380 /* disable VLAN tag insert/strip */
381 ctrl = ATL2_READ_REG(&adapter->hw, REG_MAC_CTRL);
382 ctrl &= ~MAC_CTRL_RMV_VLAN;
383 ATL2_WRITE_REG(&adapter->hw, REG_MAC_CTRL, ctrl);
386 atl2_irq_enable(adapter);
389 static void atl2_restore_vlan(struct atl2_adapter *adapter)
391 atl2_vlan_rx_register(adapter->netdev, adapter->vlgrp);
393 #endif
395 static void atl2_intr_rx(struct atl2_adapter *adapter)
397 struct net_device *netdev = adapter->netdev;
398 struct rx_desc *rxd;
399 struct sk_buff *skb;
401 do {
402 rxd = adapter->rxd_ring+adapter->rxd_write_ptr;
403 if (!rxd->status.update)
404 break; /* end of tx */
406 /* clear this flag at once */
407 rxd->status.update = 0;
409 if (rxd->status.ok && rxd->status.pkt_size >= 60) {
410 int rx_size = (int)(rxd->status.pkt_size - 4);
411 /* alloc new buffer */
412 skb = netdev_alloc_skb(netdev, rx_size + NET_IP_ALIGN);
413 if (NULL == skb) {
414 printk(KERN_WARNING
415 "%s: Mem squeeze, deferring packet.\n",
416 netdev->name);
418 * Check that some rx space is free. If not,
419 * free one and mark stats->rx_dropped++.
421 netdev->stats.rx_dropped++;
422 break;
424 skb_reserve(skb, NET_IP_ALIGN);
425 skb->dev = netdev;
426 memcpy(skb->data, rxd->packet, rx_size);
427 skb_put(skb, rx_size);
428 skb->protocol = eth_type_trans(skb, netdev);
429 #ifdef NETIF_F_HW_VLAN_TX
430 if (adapter->vlgrp && (rxd->status.vlan)) {
431 u16 vlan_tag = (rxd->status.vtag>>4) |
432 ((rxd->status.vtag&7) << 13) |
433 ((rxd->status.vtag&8) << 9);
434 vlan_hwaccel_rx(skb, adapter->vlgrp, vlan_tag);
435 } else
436 #endif
437 netif_rx(skb);
438 netdev->stats.rx_bytes += rx_size;
439 netdev->stats.rx_packets++;
440 } else {
441 netdev->stats.rx_errors++;
443 if (rxd->status.ok && rxd->status.pkt_size <= 60)
444 netdev->stats.rx_length_errors++;
445 if (rxd->status.mcast)
446 netdev->stats.multicast++;
447 if (rxd->status.crc)
448 netdev->stats.rx_crc_errors++;
449 if (rxd->status.align)
450 netdev->stats.rx_frame_errors++;
453 /* advance write ptr */
454 if (++adapter->rxd_write_ptr == adapter->rxd_ring_size)
455 adapter->rxd_write_ptr = 0;
456 } while (1);
458 /* update mailbox? */
459 adapter->rxd_read_ptr = adapter->rxd_write_ptr;
460 ATL2_WRITE_REGW(&adapter->hw, REG_MB_RXD_RD_IDX, adapter->rxd_read_ptr);
463 static void atl2_intr_tx(struct atl2_adapter *adapter)
465 struct net_device *netdev = adapter->netdev;
466 u32 txd_read_ptr;
467 u32 txs_write_ptr;
468 struct tx_pkt_status *txs;
469 struct tx_pkt_header *txph;
470 int free_hole = 0;
472 do {
473 txs_write_ptr = (u32) atomic_read(&adapter->txs_write_ptr);
474 txs = adapter->txs_ring + txs_write_ptr;
475 if (!txs->update)
476 break; /* tx stop here */
478 free_hole = 1;
479 txs->update = 0;
481 if (++txs_write_ptr == adapter->txs_ring_size)
482 txs_write_ptr = 0;
483 atomic_set(&adapter->txs_write_ptr, (int)txs_write_ptr);
485 txd_read_ptr = (u32) atomic_read(&adapter->txd_read_ptr);
486 txph = (struct tx_pkt_header *)
487 (((u8 *)adapter->txd_ring) + txd_read_ptr);
489 if (txph->pkt_size != txs->pkt_size) {
490 struct tx_pkt_status *old_txs = txs;
491 printk(KERN_WARNING
492 "%s: txs packet size not consistent with txd"
493 " txd_:0x%08x, txs_:0x%08x!\n",
494 adapter->netdev->name,
495 *(u32 *)txph, *(u32 *)txs);
496 printk(KERN_WARNING
497 "txd read ptr: 0x%x\n",
498 txd_read_ptr);
499 txs = adapter->txs_ring + txs_write_ptr;
500 printk(KERN_WARNING
501 "txs-behind:0x%08x\n",
502 *(u32 *)txs);
503 if (txs_write_ptr < 2) {
504 txs = adapter->txs_ring +
505 (adapter->txs_ring_size +
506 txs_write_ptr - 2);
507 } else {
508 txs = adapter->txs_ring + (txs_write_ptr - 2);
510 printk(KERN_WARNING
511 "txs-before:0x%08x\n",
512 *(u32 *)txs);
513 txs = old_txs;
516 /* 4for TPH */
517 txd_read_ptr += (((u32)(txph->pkt_size) + 7) & ~3);
518 if (txd_read_ptr >= adapter->txd_ring_size)
519 txd_read_ptr -= adapter->txd_ring_size;
521 atomic_set(&adapter->txd_read_ptr, (int)txd_read_ptr);
523 /* tx statistics: */
524 if (txs->ok) {
525 netdev->stats.tx_bytes += txs->pkt_size;
526 netdev->stats.tx_packets++;
528 else
529 netdev->stats.tx_errors++;
531 if (txs->defer)
532 netdev->stats.collisions++;
533 if (txs->abort_col)
534 netdev->stats.tx_aborted_errors++;
535 if (txs->late_col)
536 netdev->stats.tx_window_errors++;
537 if (txs->underun)
538 netdev->stats.tx_fifo_errors++;
539 } while (1);
541 if (free_hole) {
542 if (netif_queue_stopped(adapter->netdev) &&
543 netif_carrier_ok(adapter->netdev))
544 netif_wake_queue(adapter->netdev);
548 static void atl2_check_for_link(struct atl2_adapter *adapter)
550 struct net_device *netdev = adapter->netdev;
551 u16 phy_data = 0;
553 spin_lock(&adapter->stats_lock);
554 atl2_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
555 atl2_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
556 spin_unlock(&adapter->stats_lock);
558 /* notify upper layer link down ASAP */
559 if (!(phy_data & BMSR_LSTATUS)) { /* Link Down */
560 if (netif_carrier_ok(netdev)) { /* old link state: Up */
561 printk(KERN_INFO "%s: %s NIC Link is Down\n",
562 atl2_driver_name, netdev->name);
563 adapter->link_speed = SPEED_0;
564 netif_carrier_off(netdev);
565 netif_stop_queue(netdev);
568 schedule_work(&adapter->link_chg_task);
571 static inline void atl2_clear_phy_int(struct atl2_adapter *adapter)
573 u16 phy_data;
574 spin_lock(&adapter->stats_lock);
575 atl2_read_phy_reg(&adapter->hw, 19, &phy_data);
576 spin_unlock(&adapter->stats_lock);
580 * atl2_intr - Interrupt Handler
581 * @irq: interrupt number
582 * @data: pointer to a network interface device structure
583 * @pt_regs: CPU registers structure
585 static irqreturn_t atl2_intr(int irq, void *data)
587 struct atl2_adapter *adapter = netdev_priv(data);
588 struct atl2_hw *hw = &adapter->hw;
589 u32 status;
591 status = ATL2_READ_REG(hw, REG_ISR);
592 if (0 == status)
593 return IRQ_NONE;
595 /* link event */
596 if (status & ISR_PHY)
597 atl2_clear_phy_int(adapter);
599 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
600 ATL2_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
602 /* check if PCIE PHY Link down */
603 if (status & ISR_PHY_LINKDOWN) {
604 if (netif_running(adapter->netdev)) { /* reset MAC */
605 ATL2_WRITE_REG(hw, REG_ISR, 0);
606 ATL2_WRITE_REG(hw, REG_IMR, 0);
607 ATL2_WRITE_FLUSH(hw);
608 schedule_work(&adapter->reset_task);
609 return IRQ_HANDLED;
613 /* check if DMA read/write error? */
614 if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
615 ATL2_WRITE_REG(hw, REG_ISR, 0);
616 ATL2_WRITE_REG(hw, REG_IMR, 0);
617 ATL2_WRITE_FLUSH(hw);
618 schedule_work(&adapter->reset_task);
619 return IRQ_HANDLED;
622 /* link event */
623 if (status & (ISR_PHY | ISR_MANUAL)) {
624 adapter->netdev->stats.tx_carrier_errors++;
625 atl2_check_for_link(adapter);
628 /* transmit event */
629 if (status & ISR_TX_EVENT)
630 atl2_intr_tx(adapter);
632 /* rx exception */
633 if (status & ISR_RX_EVENT)
634 atl2_intr_rx(adapter);
636 /* re-enable Interrupt */
637 ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0);
638 return IRQ_HANDLED;
641 static int atl2_request_irq(struct atl2_adapter *adapter)
643 struct net_device *netdev = adapter->netdev;
644 int flags, err = 0;
646 flags = IRQF_SHARED;
647 adapter->have_msi = true;
648 err = pci_enable_msi(adapter->pdev);
649 if (err)
650 adapter->have_msi = false;
652 if (adapter->have_msi)
653 flags &= ~IRQF_SHARED;
655 return request_irq(adapter->pdev->irq, &atl2_intr, flags, netdev->name,
656 netdev);
660 * atl2_free_ring_resources - Free Tx / RX descriptor Resources
661 * @adapter: board private structure
663 * Free all transmit software resources
665 static void atl2_free_ring_resources(struct atl2_adapter *adapter)
667 struct pci_dev *pdev = adapter->pdev;
668 pci_free_consistent(pdev, adapter->ring_size, adapter->ring_vir_addr,
669 adapter->ring_dma);
673 * atl2_open - Called when a network interface is made active
674 * @netdev: network interface device structure
676 * Returns 0 on success, negative value on failure
678 * The open entry point is called when a network interface is made
679 * active by the system (IFF_UP). At this point all resources needed
680 * for transmit and receive operations are allocated, the interrupt
681 * handler is registered with the OS, the watchdog timer is started,
682 * and the stack is notified that the interface is ready.
684 static int atl2_open(struct net_device *netdev)
686 struct atl2_adapter *adapter = netdev_priv(netdev);
687 int err;
688 u32 val;
690 /* disallow open during test */
691 if (test_bit(__ATL2_TESTING, &adapter->flags))
692 return -EBUSY;
694 /* allocate transmit descriptors */
695 err = atl2_setup_ring_resources(adapter);
696 if (err)
697 return err;
699 err = atl2_init_hw(&adapter->hw);
700 if (err) {
701 err = -EIO;
702 goto err_init_hw;
705 /* hardware has been reset, we need to reload some things */
706 atl2_set_multi(netdev);
707 init_ring_ptrs(adapter);
709 #ifdef NETIF_F_HW_VLAN_TX
710 atl2_restore_vlan(adapter);
711 #endif
713 if (atl2_configure(adapter)) {
714 err = -EIO;
715 goto err_config;
718 err = atl2_request_irq(adapter);
719 if (err)
720 goto err_req_irq;
722 clear_bit(__ATL2_DOWN, &adapter->flags);
724 mod_timer(&adapter->watchdog_timer, round_jiffies(jiffies + 4*HZ));
726 val = ATL2_READ_REG(&adapter->hw, REG_MASTER_CTRL);
727 ATL2_WRITE_REG(&adapter->hw, REG_MASTER_CTRL,
728 val | MASTER_CTRL_MANUAL_INT);
730 atl2_irq_enable(adapter);
732 return 0;
734 err_init_hw:
735 err_req_irq:
736 err_config:
737 atl2_free_ring_resources(adapter);
738 atl2_reset_hw(&adapter->hw);
740 return err;
743 static void atl2_down(struct atl2_adapter *adapter)
745 struct net_device *netdev = adapter->netdev;
747 /* signal that we're down so the interrupt handler does not
748 * reschedule our watchdog timer */
749 set_bit(__ATL2_DOWN, &adapter->flags);
751 netif_tx_disable(netdev);
753 /* reset MAC to disable all RX/TX */
754 atl2_reset_hw(&adapter->hw);
755 msleep(1);
757 atl2_irq_disable(adapter);
759 del_timer_sync(&adapter->watchdog_timer);
760 del_timer_sync(&adapter->phy_config_timer);
761 clear_bit(0, &adapter->cfg_phy);
763 netif_carrier_off(netdev);
764 adapter->link_speed = SPEED_0;
765 adapter->link_duplex = -1;
768 static void atl2_free_irq(struct atl2_adapter *adapter)
770 struct net_device *netdev = adapter->netdev;
772 free_irq(adapter->pdev->irq, netdev);
774 #ifdef CONFIG_PCI_MSI
775 if (adapter->have_msi)
776 pci_disable_msi(adapter->pdev);
777 #endif
781 * atl2_close - Disables a network interface
782 * @netdev: network interface device structure
784 * Returns 0, this is not allowed to fail
786 * The close entry point is called when an interface is de-activated
787 * by the OS. The hardware is still under the drivers control, but
788 * needs to be disabled. A global MAC reset is issued to stop the
789 * hardware, and all transmit and receive resources are freed.
791 static int atl2_close(struct net_device *netdev)
793 struct atl2_adapter *adapter = netdev_priv(netdev);
795 WARN_ON(test_bit(__ATL2_RESETTING, &adapter->flags));
797 atl2_down(adapter);
798 atl2_free_irq(adapter);
799 atl2_free_ring_resources(adapter);
801 return 0;
804 static inline int TxsFreeUnit(struct atl2_adapter *adapter)
806 u32 txs_write_ptr = (u32) atomic_read(&adapter->txs_write_ptr);
808 return (adapter->txs_next_clear >= txs_write_ptr) ?
809 (int) (adapter->txs_ring_size - adapter->txs_next_clear +
810 txs_write_ptr - 1) :
811 (int) (txs_write_ptr - adapter->txs_next_clear - 1);
814 static inline int TxdFreeBytes(struct atl2_adapter *adapter)
816 u32 txd_read_ptr = (u32)atomic_read(&adapter->txd_read_ptr);
818 return (adapter->txd_write_ptr >= txd_read_ptr) ?
819 (int) (adapter->txd_ring_size - adapter->txd_write_ptr +
820 txd_read_ptr - 1) :
821 (int) (txd_read_ptr - adapter->txd_write_ptr - 1);
824 static netdev_tx_t atl2_xmit_frame(struct sk_buff *skb,
825 struct net_device *netdev)
827 struct atl2_adapter *adapter = netdev_priv(netdev);
828 struct tx_pkt_header *txph;
829 u32 offset, copy_len;
830 int txs_unused;
831 int txbuf_unused;
833 if (test_bit(__ATL2_DOWN, &adapter->flags)) {
834 dev_kfree_skb_any(skb);
835 return NETDEV_TX_OK;
838 if (unlikely(skb->len <= 0)) {
839 dev_kfree_skb_any(skb);
840 return NETDEV_TX_OK;
843 txs_unused = TxsFreeUnit(adapter);
844 txbuf_unused = TxdFreeBytes(adapter);
846 if (skb->len + sizeof(struct tx_pkt_header) + 4 > txbuf_unused ||
847 txs_unused < 1) {
848 /* not enough resources */
849 netif_stop_queue(netdev);
850 return NETDEV_TX_BUSY;
853 offset = adapter->txd_write_ptr;
855 txph = (struct tx_pkt_header *) (((u8 *)adapter->txd_ring) + offset);
857 *(u32 *)txph = 0;
858 txph->pkt_size = skb->len;
860 offset += 4;
861 if (offset >= adapter->txd_ring_size)
862 offset -= adapter->txd_ring_size;
863 copy_len = adapter->txd_ring_size - offset;
864 if (copy_len >= skb->len) {
865 memcpy(((u8 *)adapter->txd_ring) + offset, skb->data, skb->len);
866 offset += ((u32)(skb->len + 3) & ~3);
867 } else {
868 memcpy(((u8 *)adapter->txd_ring)+offset, skb->data, copy_len);
869 memcpy((u8 *)adapter->txd_ring, skb->data+copy_len,
870 skb->len-copy_len);
871 offset = ((u32)(skb->len-copy_len + 3) & ~3);
873 #ifdef NETIF_F_HW_VLAN_TX
874 if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
875 u16 vlan_tag = vlan_tx_tag_get(skb);
876 vlan_tag = (vlan_tag << 4) |
877 (vlan_tag >> 13) |
878 ((vlan_tag >> 9) & 0x8);
879 txph->ins_vlan = 1;
880 txph->vlan = vlan_tag;
882 #endif
883 if (offset >= adapter->txd_ring_size)
884 offset -= adapter->txd_ring_size;
885 adapter->txd_write_ptr = offset;
887 /* clear txs before send */
888 adapter->txs_ring[adapter->txs_next_clear].update = 0;
889 if (++adapter->txs_next_clear == adapter->txs_ring_size)
890 adapter->txs_next_clear = 0;
892 ATL2_WRITE_REGW(&adapter->hw, REG_MB_TXD_WR_IDX,
893 (adapter->txd_write_ptr >> 2));
895 mmiowb();
896 netdev->trans_start = jiffies;
897 dev_kfree_skb_any(skb);
898 return NETDEV_TX_OK;
902 * atl2_change_mtu - Change the Maximum Transfer Unit
903 * @netdev: network interface device structure
904 * @new_mtu: new value for maximum frame size
906 * Returns 0 on success, negative on failure
908 static int atl2_change_mtu(struct net_device *netdev, int new_mtu)
910 struct atl2_adapter *adapter = netdev_priv(netdev);
911 struct atl2_hw *hw = &adapter->hw;
913 if ((new_mtu < 40) || (new_mtu > (ETH_DATA_LEN + VLAN_SIZE)))
914 return -EINVAL;
916 /* set MTU */
917 if (hw->max_frame_size != new_mtu) {
918 netdev->mtu = new_mtu;
919 ATL2_WRITE_REG(hw, REG_MTU, new_mtu + ENET_HEADER_SIZE +
920 VLAN_SIZE + ETHERNET_FCS_SIZE);
923 return 0;
927 * atl2_set_mac - Change the Ethernet Address of the NIC
928 * @netdev: network interface device structure
929 * @p: pointer to an address structure
931 * Returns 0 on success, negative on failure
933 static int atl2_set_mac(struct net_device *netdev, void *p)
935 struct atl2_adapter *adapter = netdev_priv(netdev);
936 struct sockaddr *addr = p;
938 if (!is_valid_ether_addr(addr->sa_data))
939 return -EADDRNOTAVAIL;
941 if (netif_running(netdev))
942 return -EBUSY;
944 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
945 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
947 atl2_set_mac_addr(&adapter->hw);
949 return 0;
953 * atl2_mii_ioctl -
954 * @netdev:
955 * @ifreq:
956 * @cmd:
958 static int atl2_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
960 struct atl2_adapter *adapter = netdev_priv(netdev);
961 struct mii_ioctl_data *data = if_mii(ifr);
962 unsigned long flags;
964 switch (cmd) {
965 case SIOCGMIIPHY:
966 data->phy_id = 0;
967 break;
968 case SIOCGMIIREG:
969 spin_lock_irqsave(&adapter->stats_lock, flags);
970 if (atl2_read_phy_reg(&adapter->hw,
971 data->reg_num & 0x1F, &data->val_out)) {
972 spin_unlock_irqrestore(&adapter->stats_lock, flags);
973 return -EIO;
975 spin_unlock_irqrestore(&adapter->stats_lock, flags);
976 break;
977 case SIOCSMIIREG:
978 if (data->reg_num & ~(0x1F))
979 return -EFAULT;
980 spin_lock_irqsave(&adapter->stats_lock, flags);
981 if (atl2_write_phy_reg(&adapter->hw, data->reg_num,
982 data->val_in)) {
983 spin_unlock_irqrestore(&adapter->stats_lock, flags);
984 return -EIO;
986 spin_unlock_irqrestore(&adapter->stats_lock, flags);
987 break;
988 default:
989 return -EOPNOTSUPP;
991 return 0;
995 * atl2_ioctl -
996 * @netdev:
997 * @ifreq:
998 * @cmd:
1000 static int atl2_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1002 switch (cmd) {
1003 case SIOCGMIIPHY:
1004 case SIOCGMIIREG:
1005 case SIOCSMIIREG:
1006 return atl2_mii_ioctl(netdev, ifr, cmd);
1007 #ifdef ETHTOOL_OPS_COMPAT
1008 case SIOCETHTOOL:
1009 return ethtool_ioctl(ifr);
1010 #endif
1011 default:
1012 return -EOPNOTSUPP;
1017 * atl2_tx_timeout - Respond to a Tx Hang
1018 * @netdev: network interface device structure
1020 static void atl2_tx_timeout(struct net_device *netdev)
1022 struct atl2_adapter *adapter = netdev_priv(netdev);
1024 /* Do the reset outside of interrupt context */
1025 schedule_work(&adapter->reset_task);
1029 * atl2_watchdog - Timer Call-back
1030 * @data: pointer to netdev cast into an unsigned long
1032 static void atl2_watchdog(unsigned long data)
1034 struct atl2_adapter *adapter = (struct atl2_adapter *) data;
1036 if (!test_bit(__ATL2_DOWN, &adapter->flags)) {
1037 u32 drop_rxd, drop_rxs;
1038 unsigned long flags;
1040 spin_lock_irqsave(&adapter->stats_lock, flags);
1041 drop_rxd = ATL2_READ_REG(&adapter->hw, REG_STS_RXD_OV);
1042 drop_rxs = ATL2_READ_REG(&adapter->hw, REG_STS_RXS_OV);
1043 spin_unlock_irqrestore(&adapter->stats_lock, flags);
1045 adapter->netdev->stats.rx_over_errors += drop_rxd + drop_rxs;
1047 /* Reset the timer */
1048 mod_timer(&adapter->watchdog_timer,
1049 round_jiffies(jiffies + 4 * HZ));
1054 * atl2_phy_config - Timer Call-back
1055 * @data: pointer to netdev cast into an unsigned long
1057 static void atl2_phy_config(unsigned long data)
1059 struct atl2_adapter *adapter = (struct atl2_adapter *) data;
1060 struct atl2_hw *hw = &adapter->hw;
1061 unsigned long flags;
1063 spin_lock_irqsave(&adapter->stats_lock, flags);
1064 atl2_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
1065 atl2_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN |
1066 MII_CR_RESTART_AUTO_NEG);
1067 spin_unlock_irqrestore(&adapter->stats_lock, flags);
1068 clear_bit(0, &adapter->cfg_phy);
1071 static int atl2_up(struct atl2_adapter *adapter)
1073 struct net_device *netdev = adapter->netdev;
1074 int err = 0;
1075 u32 val;
1077 /* hardware has been reset, we need to reload some things */
1079 err = atl2_init_hw(&adapter->hw);
1080 if (err) {
1081 err = -EIO;
1082 return err;
1085 atl2_set_multi(netdev);
1086 init_ring_ptrs(adapter);
1088 #ifdef NETIF_F_HW_VLAN_TX
1089 atl2_restore_vlan(adapter);
1090 #endif
1092 if (atl2_configure(adapter)) {
1093 err = -EIO;
1094 goto err_up;
1097 clear_bit(__ATL2_DOWN, &adapter->flags);
1099 val = ATL2_READ_REG(&adapter->hw, REG_MASTER_CTRL);
1100 ATL2_WRITE_REG(&adapter->hw, REG_MASTER_CTRL, val |
1101 MASTER_CTRL_MANUAL_INT);
1103 atl2_irq_enable(adapter);
1105 err_up:
1106 return err;
1109 static void atl2_reinit_locked(struct atl2_adapter *adapter)
1111 WARN_ON(in_interrupt());
1112 while (test_and_set_bit(__ATL2_RESETTING, &adapter->flags))
1113 msleep(1);
1114 atl2_down(adapter);
1115 atl2_up(adapter);
1116 clear_bit(__ATL2_RESETTING, &adapter->flags);
1119 static void atl2_reset_task(struct work_struct *work)
1121 struct atl2_adapter *adapter;
1122 adapter = container_of(work, struct atl2_adapter, reset_task);
1124 atl2_reinit_locked(adapter);
1127 static void atl2_setup_mac_ctrl(struct atl2_adapter *adapter)
1129 u32 value;
1130 struct atl2_hw *hw = &adapter->hw;
1131 struct net_device *netdev = adapter->netdev;
1133 /* Config MAC CTRL Register */
1134 value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN | MAC_CTRL_MACLP_CLK_PHY;
1136 /* duplex */
1137 if (FULL_DUPLEX == adapter->link_duplex)
1138 value |= MAC_CTRL_DUPLX;
1140 /* flow control */
1141 value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1143 /* PAD & CRC */
1144 value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1146 /* preamble length */
1147 value |= (((u32)adapter->hw.preamble_len & MAC_CTRL_PRMLEN_MASK) <<
1148 MAC_CTRL_PRMLEN_SHIFT);
1150 /* vlan */
1151 if (adapter->vlgrp)
1152 value |= MAC_CTRL_RMV_VLAN;
1154 /* filter mode */
1155 value |= MAC_CTRL_BC_EN;
1156 if (netdev->flags & IFF_PROMISC)
1157 value |= MAC_CTRL_PROMIS_EN;
1158 else if (netdev->flags & IFF_ALLMULTI)
1159 value |= MAC_CTRL_MC_ALL_EN;
1161 /* half retry buffer */
1162 value |= (((u32)(adapter->hw.retry_buf &
1163 MAC_CTRL_HALF_LEFT_BUF_MASK)) << MAC_CTRL_HALF_LEFT_BUF_SHIFT);
1165 ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1168 static int atl2_check_link(struct atl2_adapter *adapter)
1170 struct atl2_hw *hw = &adapter->hw;
1171 struct net_device *netdev = adapter->netdev;
1172 int ret_val;
1173 u16 speed, duplex, phy_data;
1174 int reconfig = 0;
1176 /* MII_BMSR must read twise */
1177 atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1178 atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1179 if (!(phy_data&BMSR_LSTATUS)) { /* link down */
1180 if (netif_carrier_ok(netdev)) { /* old link state: Up */
1181 u32 value;
1182 /* disable rx */
1183 value = ATL2_READ_REG(hw, REG_MAC_CTRL);
1184 value &= ~MAC_CTRL_RX_EN;
1185 ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1186 adapter->link_speed = SPEED_0;
1187 netif_carrier_off(netdev);
1188 netif_stop_queue(netdev);
1190 return 0;
1193 /* Link Up */
1194 ret_val = atl2_get_speed_and_duplex(hw, &speed, &duplex);
1195 if (ret_val)
1196 return ret_val;
1197 switch (hw->MediaType) {
1198 case MEDIA_TYPE_100M_FULL:
1199 if (speed != SPEED_100 || duplex != FULL_DUPLEX)
1200 reconfig = 1;
1201 break;
1202 case MEDIA_TYPE_100M_HALF:
1203 if (speed != SPEED_100 || duplex != HALF_DUPLEX)
1204 reconfig = 1;
1205 break;
1206 case MEDIA_TYPE_10M_FULL:
1207 if (speed != SPEED_10 || duplex != FULL_DUPLEX)
1208 reconfig = 1;
1209 break;
1210 case MEDIA_TYPE_10M_HALF:
1211 if (speed != SPEED_10 || duplex != HALF_DUPLEX)
1212 reconfig = 1;
1213 break;
1215 /* link result is our setting */
1216 if (reconfig == 0) {
1217 if (adapter->link_speed != speed ||
1218 adapter->link_duplex != duplex) {
1219 adapter->link_speed = speed;
1220 adapter->link_duplex = duplex;
1221 atl2_setup_mac_ctrl(adapter);
1222 printk(KERN_INFO "%s: %s NIC Link is Up<%d Mbps %s>\n",
1223 atl2_driver_name, netdev->name,
1224 adapter->link_speed,
1225 adapter->link_duplex == FULL_DUPLEX ?
1226 "Full Duplex" : "Half Duplex");
1229 if (!netif_carrier_ok(netdev)) { /* Link down -> Up */
1230 netif_carrier_on(netdev);
1231 netif_wake_queue(netdev);
1233 return 0;
1236 /* change original link status */
1237 if (netif_carrier_ok(netdev)) {
1238 u32 value;
1239 /* disable rx */
1240 value = ATL2_READ_REG(hw, REG_MAC_CTRL);
1241 value &= ~MAC_CTRL_RX_EN;
1242 ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1244 adapter->link_speed = SPEED_0;
1245 netif_carrier_off(netdev);
1246 netif_stop_queue(netdev);
1249 /* auto-neg, insert timer to re-config phy
1250 * (if interval smaller than 5 seconds, something strange) */
1251 if (!test_bit(__ATL2_DOWN, &adapter->flags)) {
1252 if (!test_and_set_bit(0, &adapter->cfg_phy))
1253 mod_timer(&adapter->phy_config_timer,
1254 round_jiffies(jiffies + 5 * HZ));
1257 return 0;
1261 * atl2_link_chg_task - deal with link change event Out of interrupt context
1262 * @netdev: network interface device structure
1264 static void atl2_link_chg_task(struct work_struct *work)
1266 struct atl2_adapter *adapter;
1267 unsigned long flags;
1269 adapter = container_of(work, struct atl2_adapter, link_chg_task);
1271 spin_lock_irqsave(&adapter->stats_lock, flags);
1272 atl2_check_link(adapter);
1273 spin_unlock_irqrestore(&adapter->stats_lock, flags);
1276 static void atl2_setup_pcicmd(struct pci_dev *pdev)
1278 u16 cmd;
1280 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1282 if (cmd & PCI_COMMAND_INTX_DISABLE)
1283 cmd &= ~PCI_COMMAND_INTX_DISABLE;
1284 if (cmd & PCI_COMMAND_IO)
1285 cmd &= ~PCI_COMMAND_IO;
1286 if (0 == (cmd & PCI_COMMAND_MEMORY))
1287 cmd |= PCI_COMMAND_MEMORY;
1288 if (0 == (cmd & PCI_COMMAND_MASTER))
1289 cmd |= PCI_COMMAND_MASTER;
1290 pci_write_config_word(pdev, PCI_COMMAND, cmd);
1293 * some motherboards BIOS(PXE/EFI) driver may set PME
1294 * while they transfer control to OS (Windows/Linux)
1295 * so we should clear this bit before NIC work normally
1297 pci_write_config_dword(pdev, REG_PM_CTRLSTAT, 0);
1300 #ifdef CONFIG_NET_POLL_CONTROLLER
1301 static void atl2_poll_controller(struct net_device *netdev)
1303 disable_irq(netdev->irq);
1304 atl2_intr(netdev->irq, netdev);
1305 enable_irq(netdev->irq);
1307 #endif
1310 static const struct net_device_ops atl2_netdev_ops = {
1311 .ndo_open = atl2_open,
1312 .ndo_stop = atl2_close,
1313 .ndo_start_xmit = atl2_xmit_frame,
1314 .ndo_set_multicast_list = atl2_set_multi,
1315 .ndo_validate_addr = eth_validate_addr,
1316 .ndo_set_mac_address = atl2_set_mac,
1317 .ndo_change_mtu = atl2_change_mtu,
1318 .ndo_do_ioctl = atl2_ioctl,
1319 .ndo_tx_timeout = atl2_tx_timeout,
1320 .ndo_vlan_rx_register = atl2_vlan_rx_register,
1321 #ifdef CONFIG_NET_POLL_CONTROLLER
1322 .ndo_poll_controller = atl2_poll_controller,
1323 #endif
1327 * atl2_probe - Device Initialization Routine
1328 * @pdev: PCI device information struct
1329 * @ent: entry in atl2_pci_tbl
1331 * Returns 0 on success, negative on failure
1333 * atl2_probe initializes an adapter identified by a pci_dev structure.
1334 * The OS initialization, configuring of the adapter private structure,
1335 * and a hardware reset occur.
1337 static int __devinit atl2_probe(struct pci_dev *pdev,
1338 const struct pci_device_id *ent)
1340 struct net_device *netdev;
1341 struct atl2_adapter *adapter;
1342 static int cards_found;
1343 unsigned long mmio_start;
1344 int mmio_len;
1345 int err;
1347 cards_found = 0;
1349 err = pci_enable_device(pdev);
1350 if (err)
1351 return err;
1354 * atl2 is a shared-high-32-bit device, so we're stuck with 32-bit DMA
1355 * until the kernel has the proper infrastructure to support 64-bit DMA
1356 * on these devices.
1358 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1359 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1360 printk(KERN_ERR "atl2: No usable DMA configuration, aborting\n");
1361 goto err_dma;
1364 /* Mark all PCI regions associated with PCI device
1365 * pdev as being reserved by owner atl2_driver_name */
1366 err = pci_request_regions(pdev, atl2_driver_name);
1367 if (err)
1368 goto err_pci_reg;
1370 /* Enables bus-mastering on the device and calls
1371 * pcibios_set_master to do the needed arch specific settings */
1372 pci_set_master(pdev);
1374 err = -ENOMEM;
1375 netdev = alloc_etherdev(sizeof(struct atl2_adapter));
1376 if (!netdev)
1377 goto err_alloc_etherdev;
1379 SET_NETDEV_DEV(netdev, &pdev->dev);
1381 pci_set_drvdata(pdev, netdev);
1382 adapter = netdev_priv(netdev);
1383 adapter->netdev = netdev;
1384 adapter->pdev = pdev;
1385 adapter->hw.back = adapter;
1387 mmio_start = pci_resource_start(pdev, 0x0);
1388 mmio_len = pci_resource_len(pdev, 0x0);
1390 adapter->hw.mem_rang = (u32)mmio_len;
1391 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
1392 if (!adapter->hw.hw_addr) {
1393 err = -EIO;
1394 goto err_ioremap;
1397 atl2_setup_pcicmd(pdev);
1399 netdev->netdev_ops = &atl2_netdev_ops;
1400 atl2_set_ethtool_ops(netdev);
1401 netdev->watchdog_timeo = 5 * HZ;
1402 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1404 netdev->mem_start = mmio_start;
1405 netdev->mem_end = mmio_start + mmio_len;
1406 adapter->bd_number = cards_found;
1407 adapter->pci_using_64 = false;
1409 /* setup the private structure */
1410 err = atl2_sw_init(adapter);
1411 if (err)
1412 goto err_sw_init;
1414 err = -EIO;
1416 #ifdef NETIF_F_HW_VLAN_TX
1417 netdev->features |= (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
1418 #endif
1420 /* Init PHY as early as possible due to power saving issue */
1421 atl2_phy_init(&adapter->hw);
1423 /* reset the controller to
1424 * put the device in a known good starting state */
1426 if (atl2_reset_hw(&adapter->hw)) {
1427 err = -EIO;
1428 goto err_reset;
1431 /* copy the MAC address out of the EEPROM */
1432 atl2_read_mac_addr(&adapter->hw);
1433 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
1434 /* FIXME: do we still need this? */
1435 #ifdef ETHTOOL_GPERMADDR
1436 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
1438 if (!is_valid_ether_addr(netdev->perm_addr)) {
1439 #else
1440 if (!is_valid_ether_addr(netdev->dev_addr)) {
1441 #endif
1442 err = -EIO;
1443 goto err_eeprom;
1446 atl2_check_options(adapter);
1448 init_timer(&adapter->watchdog_timer);
1449 adapter->watchdog_timer.function = &atl2_watchdog;
1450 adapter->watchdog_timer.data = (unsigned long) adapter;
1452 init_timer(&adapter->phy_config_timer);
1453 adapter->phy_config_timer.function = &atl2_phy_config;
1454 adapter->phy_config_timer.data = (unsigned long) adapter;
1456 INIT_WORK(&adapter->reset_task, atl2_reset_task);
1457 INIT_WORK(&adapter->link_chg_task, atl2_link_chg_task);
1459 strcpy(netdev->name, "eth%d"); /* ?? */
1460 err = register_netdev(netdev);
1461 if (err)
1462 goto err_register;
1464 /* assume we have no link for now */
1465 netif_carrier_off(netdev);
1466 netif_stop_queue(netdev);
1468 cards_found++;
1470 return 0;
1472 err_reset:
1473 err_register:
1474 err_sw_init:
1475 err_eeprom:
1476 iounmap(adapter->hw.hw_addr);
1477 err_ioremap:
1478 free_netdev(netdev);
1479 err_alloc_etherdev:
1480 pci_release_regions(pdev);
1481 err_pci_reg:
1482 err_dma:
1483 pci_disable_device(pdev);
1484 return err;
1488 * atl2_remove - Device Removal Routine
1489 * @pdev: PCI device information struct
1491 * atl2_remove is called by the PCI subsystem to alert the driver
1492 * that it should release a PCI device. The could be caused by a
1493 * Hot-Plug event, or because the driver is going to be removed from
1494 * memory.
1496 /* FIXME: write the original MAC address back in case it was changed from a
1497 * BIOS-set value, as in atl1 -- CHS */
1498 static void __devexit atl2_remove(struct pci_dev *pdev)
1500 struct net_device *netdev = pci_get_drvdata(pdev);
1501 struct atl2_adapter *adapter = netdev_priv(netdev);
1503 /* flush_scheduled work may reschedule our watchdog task, so
1504 * explicitly disable watchdog tasks from being rescheduled */
1505 set_bit(__ATL2_DOWN, &adapter->flags);
1507 del_timer_sync(&adapter->watchdog_timer);
1508 del_timer_sync(&adapter->phy_config_timer);
1510 flush_scheduled_work();
1512 unregister_netdev(netdev);
1514 atl2_force_ps(&adapter->hw);
1516 iounmap(adapter->hw.hw_addr);
1517 pci_release_regions(pdev);
1519 free_netdev(netdev);
1521 pci_disable_device(pdev);
1524 static int atl2_suspend(struct pci_dev *pdev, pm_message_t state)
1526 struct net_device *netdev = pci_get_drvdata(pdev);
1527 struct atl2_adapter *adapter = netdev_priv(netdev);
1528 struct atl2_hw *hw = &adapter->hw;
1529 u16 speed, duplex;
1530 u32 ctrl = 0;
1531 u32 wufc = adapter->wol;
1533 #ifdef CONFIG_PM
1534 int retval = 0;
1535 #endif
1537 netif_device_detach(netdev);
1539 if (netif_running(netdev)) {
1540 WARN_ON(test_bit(__ATL2_RESETTING, &adapter->flags));
1541 atl2_down(adapter);
1544 #ifdef CONFIG_PM
1545 retval = pci_save_state(pdev);
1546 if (retval)
1547 return retval;
1548 #endif
1550 atl2_read_phy_reg(hw, MII_BMSR, (u16 *)&ctrl);
1551 atl2_read_phy_reg(hw, MII_BMSR, (u16 *)&ctrl);
1552 if (ctrl & BMSR_LSTATUS)
1553 wufc &= ~ATLX_WUFC_LNKC;
1555 if (0 != (ctrl & BMSR_LSTATUS) && 0 != wufc) {
1556 u32 ret_val;
1557 /* get current link speed & duplex */
1558 ret_val = atl2_get_speed_and_duplex(hw, &speed, &duplex);
1559 if (ret_val) {
1560 printk(KERN_DEBUG
1561 "%s: get speed&duplex error while suspend\n",
1562 atl2_driver_name);
1563 goto wol_dis;
1566 ctrl = 0;
1568 /* turn on magic packet wol */
1569 if (wufc & ATLX_WUFC_MAG)
1570 ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
1572 /* ignore Link Chg event when Link is up */
1573 ATL2_WRITE_REG(hw, REG_WOL_CTRL, ctrl);
1575 /* Config MAC CTRL Register */
1576 ctrl = MAC_CTRL_RX_EN | MAC_CTRL_MACLP_CLK_PHY;
1577 if (FULL_DUPLEX == adapter->link_duplex)
1578 ctrl |= MAC_CTRL_DUPLX;
1579 ctrl |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1580 ctrl |= (((u32)adapter->hw.preamble_len &
1581 MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1582 ctrl |= (((u32)(adapter->hw.retry_buf &
1583 MAC_CTRL_HALF_LEFT_BUF_MASK)) <<
1584 MAC_CTRL_HALF_LEFT_BUF_SHIFT);
1585 if (wufc & ATLX_WUFC_MAG) {
1586 /* magic packet maybe Broadcast&multicast&Unicast */
1587 ctrl |= MAC_CTRL_BC_EN;
1590 ATL2_WRITE_REG(hw, REG_MAC_CTRL, ctrl);
1592 /* pcie patch */
1593 ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1594 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1595 ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1596 ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1597 ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1598 ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1600 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
1601 goto suspend_exit;
1604 if (0 == (ctrl&BMSR_LSTATUS) && 0 != (wufc&ATLX_WUFC_LNKC)) {
1605 /* link is down, so only LINK CHG WOL event enable */
1606 ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
1607 ATL2_WRITE_REG(hw, REG_WOL_CTRL, ctrl);
1608 ATL2_WRITE_REG(hw, REG_MAC_CTRL, 0);
1610 /* pcie patch */
1611 ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1612 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1613 ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1614 ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1615 ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1616 ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1618 hw->phy_configured = false; /* re-init PHY when resume */
1620 pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
1622 goto suspend_exit;
1625 wol_dis:
1626 /* WOL disabled */
1627 ATL2_WRITE_REG(hw, REG_WOL_CTRL, 0);
1629 /* pcie patch */
1630 ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1631 ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1632 ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1633 ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1634 ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1635 ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1637 atl2_force_ps(hw);
1638 hw->phy_configured = false; /* re-init PHY when resume */
1640 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1642 suspend_exit:
1643 if (netif_running(netdev))
1644 atl2_free_irq(adapter);
1646 pci_disable_device(pdev);
1648 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1650 return 0;
1653 #ifdef CONFIG_PM
1654 static int atl2_resume(struct pci_dev *pdev)
1656 struct net_device *netdev = pci_get_drvdata(pdev);
1657 struct atl2_adapter *adapter = netdev_priv(netdev);
1658 u32 err;
1660 pci_set_power_state(pdev, PCI_D0);
1661 pci_restore_state(pdev);
1663 err = pci_enable_device(pdev);
1664 if (err) {
1665 printk(KERN_ERR
1666 "atl2: Cannot enable PCI device from suspend\n");
1667 return err;
1670 pci_set_master(pdev);
1672 ATL2_READ_REG(&adapter->hw, REG_WOL_CTRL); /* clear WOL status */
1674 pci_enable_wake(pdev, PCI_D3hot, 0);
1675 pci_enable_wake(pdev, PCI_D3cold, 0);
1677 ATL2_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
1679 if (netif_running(netdev)) {
1680 err = atl2_request_irq(adapter);
1681 if (err)
1682 return err;
1685 atl2_reset_hw(&adapter->hw);
1687 if (netif_running(netdev))
1688 atl2_up(adapter);
1690 netif_device_attach(netdev);
1692 return 0;
1694 #endif
1696 static void atl2_shutdown(struct pci_dev *pdev)
1698 atl2_suspend(pdev, PMSG_SUSPEND);
1701 static struct pci_driver atl2_driver = {
1702 .name = atl2_driver_name,
1703 .id_table = atl2_pci_tbl,
1704 .probe = atl2_probe,
1705 .remove = __devexit_p(atl2_remove),
1706 /* Power Managment Hooks */
1707 .suspend = atl2_suspend,
1708 #ifdef CONFIG_PM
1709 .resume = atl2_resume,
1710 #endif
1711 .shutdown = atl2_shutdown,
1715 * atl2_init_module - Driver Registration Routine
1717 * atl2_init_module is the first routine called when the driver is
1718 * loaded. All it does is register with the PCI subsystem.
1720 static int __init atl2_init_module(void)
1722 printk(KERN_INFO "%s - version %s\n", atl2_driver_string,
1723 atl2_driver_version);
1724 printk(KERN_INFO "%s\n", atl2_copyright);
1725 return pci_register_driver(&atl2_driver);
1727 module_init(atl2_init_module);
1730 * atl2_exit_module - Driver Exit Cleanup Routine
1732 * atl2_exit_module is called just before the driver is removed
1733 * from memory.
1735 static void __exit atl2_exit_module(void)
1737 pci_unregister_driver(&atl2_driver);
1739 module_exit(atl2_exit_module);
1741 static void atl2_read_pci_cfg(struct atl2_hw *hw, u32 reg, u16 *value)
1743 struct atl2_adapter *adapter = hw->back;
1744 pci_read_config_word(adapter->pdev, reg, value);
1747 static void atl2_write_pci_cfg(struct atl2_hw *hw, u32 reg, u16 *value)
1749 struct atl2_adapter *adapter = hw->back;
1750 pci_write_config_word(adapter->pdev, reg, *value);
1753 static int atl2_get_settings(struct net_device *netdev,
1754 struct ethtool_cmd *ecmd)
1756 struct atl2_adapter *adapter = netdev_priv(netdev);
1757 struct atl2_hw *hw = &adapter->hw;
1759 ecmd->supported = (SUPPORTED_10baseT_Half |
1760 SUPPORTED_10baseT_Full |
1761 SUPPORTED_100baseT_Half |
1762 SUPPORTED_100baseT_Full |
1763 SUPPORTED_Autoneg |
1764 SUPPORTED_TP);
1765 ecmd->advertising = ADVERTISED_TP;
1767 ecmd->advertising |= ADVERTISED_Autoneg;
1768 ecmd->advertising |= hw->autoneg_advertised;
1770 ecmd->port = PORT_TP;
1771 ecmd->phy_address = 0;
1772 ecmd->transceiver = XCVR_INTERNAL;
1774 if (adapter->link_speed != SPEED_0) {
1775 ecmd->speed = adapter->link_speed;
1776 if (adapter->link_duplex == FULL_DUPLEX)
1777 ecmd->duplex = DUPLEX_FULL;
1778 else
1779 ecmd->duplex = DUPLEX_HALF;
1780 } else {
1781 ecmd->speed = -1;
1782 ecmd->duplex = -1;
1785 ecmd->autoneg = AUTONEG_ENABLE;
1786 return 0;
1789 static int atl2_set_settings(struct net_device *netdev,
1790 struct ethtool_cmd *ecmd)
1792 struct atl2_adapter *adapter = netdev_priv(netdev);
1793 struct atl2_hw *hw = &adapter->hw;
1795 while (test_and_set_bit(__ATL2_RESETTING, &adapter->flags))
1796 msleep(1);
1798 if (ecmd->autoneg == AUTONEG_ENABLE) {
1799 #define MY_ADV_MASK (ADVERTISE_10_HALF | \
1800 ADVERTISE_10_FULL | \
1801 ADVERTISE_100_HALF| \
1802 ADVERTISE_100_FULL)
1804 if ((ecmd->advertising & MY_ADV_MASK) == MY_ADV_MASK) {
1805 hw->MediaType = MEDIA_TYPE_AUTO_SENSOR;
1806 hw->autoneg_advertised = MY_ADV_MASK;
1807 } else if ((ecmd->advertising & MY_ADV_MASK) ==
1808 ADVERTISE_100_FULL) {
1809 hw->MediaType = MEDIA_TYPE_100M_FULL;
1810 hw->autoneg_advertised = ADVERTISE_100_FULL;
1811 } else if ((ecmd->advertising & MY_ADV_MASK) ==
1812 ADVERTISE_100_HALF) {
1813 hw->MediaType = MEDIA_TYPE_100M_HALF;
1814 hw->autoneg_advertised = ADVERTISE_100_HALF;
1815 } else if ((ecmd->advertising & MY_ADV_MASK) ==
1816 ADVERTISE_10_FULL) {
1817 hw->MediaType = MEDIA_TYPE_10M_FULL;
1818 hw->autoneg_advertised = ADVERTISE_10_FULL;
1819 } else if ((ecmd->advertising & MY_ADV_MASK) ==
1820 ADVERTISE_10_HALF) {
1821 hw->MediaType = MEDIA_TYPE_10M_HALF;
1822 hw->autoneg_advertised = ADVERTISE_10_HALF;
1823 } else {
1824 clear_bit(__ATL2_RESETTING, &adapter->flags);
1825 return -EINVAL;
1827 ecmd->advertising = hw->autoneg_advertised |
1828 ADVERTISED_TP | ADVERTISED_Autoneg;
1829 } else {
1830 clear_bit(__ATL2_RESETTING, &adapter->flags);
1831 return -EINVAL;
1834 /* reset the link */
1835 if (netif_running(adapter->netdev)) {
1836 atl2_down(adapter);
1837 atl2_up(adapter);
1838 } else
1839 atl2_reset_hw(&adapter->hw);
1841 clear_bit(__ATL2_RESETTING, &adapter->flags);
1842 return 0;
1845 static u32 atl2_get_tx_csum(struct net_device *netdev)
1847 return (netdev->features & NETIF_F_HW_CSUM) != 0;
1850 static u32 atl2_get_msglevel(struct net_device *netdev)
1852 return 0;
1856 * It's sane for this to be empty, but we might want to take advantage of this.
1858 static void atl2_set_msglevel(struct net_device *netdev, u32 data)
1862 static int atl2_get_regs_len(struct net_device *netdev)
1864 #define ATL2_REGS_LEN 42
1865 return sizeof(u32) * ATL2_REGS_LEN;
1868 static void atl2_get_regs(struct net_device *netdev,
1869 struct ethtool_regs *regs, void *p)
1871 struct atl2_adapter *adapter = netdev_priv(netdev);
1872 struct atl2_hw *hw = &adapter->hw;
1873 u32 *regs_buff = p;
1874 u16 phy_data;
1876 memset(p, 0, sizeof(u32) * ATL2_REGS_LEN);
1878 regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
1880 regs_buff[0] = ATL2_READ_REG(hw, REG_VPD_CAP);
1881 regs_buff[1] = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
1882 regs_buff[2] = ATL2_READ_REG(hw, REG_SPI_FLASH_CONFIG);
1883 regs_buff[3] = ATL2_READ_REG(hw, REG_TWSI_CTRL);
1884 regs_buff[4] = ATL2_READ_REG(hw, REG_PCIE_DEV_MISC_CTRL);
1885 regs_buff[5] = ATL2_READ_REG(hw, REG_MASTER_CTRL);
1886 regs_buff[6] = ATL2_READ_REG(hw, REG_MANUAL_TIMER_INIT);
1887 regs_buff[7] = ATL2_READ_REG(hw, REG_IRQ_MODU_TIMER_INIT);
1888 regs_buff[8] = ATL2_READ_REG(hw, REG_PHY_ENABLE);
1889 regs_buff[9] = ATL2_READ_REG(hw, REG_CMBDISDMA_TIMER);
1890 regs_buff[10] = ATL2_READ_REG(hw, REG_IDLE_STATUS);
1891 regs_buff[11] = ATL2_READ_REG(hw, REG_MDIO_CTRL);
1892 regs_buff[12] = ATL2_READ_REG(hw, REG_SERDES_LOCK);
1893 regs_buff[13] = ATL2_READ_REG(hw, REG_MAC_CTRL);
1894 regs_buff[14] = ATL2_READ_REG(hw, REG_MAC_IPG_IFG);
1895 regs_buff[15] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR);
1896 regs_buff[16] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR+4);
1897 regs_buff[17] = ATL2_READ_REG(hw, REG_RX_HASH_TABLE);
1898 regs_buff[18] = ATL2_READ_REG(hw, REG_RX_HASH_TABLE+4);
1899 regs_buff[19] = ATL2_READ_REG(hw, REG_MAC_HALF_DUPLX_CTRL);
1900 regs_buff[20] = ATL2_READ_REG(hw, REG_MTU);
1901 regs_buff[21] = ATL2_READ_REG(hw, REG_WOL_CTRL);
1902 regs_buff[22] = ATL2_READ_REG(hw, REG_SRAM_TXRAM_END);
1903 regs_buff[23] = ATL2_READ_REG(hw, REG_DESC_BASE_ADDR_HI);
1904 regs_buff[24] = ATL2_READ_REG(hw, REG_TXD_BASE_ADDR_LO);
1905 regs_buff[25] = ATL2_READ_REG(hw, REG_TXD_MEM_SIZE);
1906 regs_buff[26] = ATL2_READ_REG(hw, REG_TXS_BASE_ADDR_LO);
1907 regs_buff[27] = ATL2_READ_REG(hw, REG_TXS_MEM_SIZE);
1908 regs_buff[28] = ATL2_READ_REG(hw, REG_RXD_BASE_ADDR_LO);
1909 regs_buff[29] = ATL2_READ_REG(hw, REG_RXD_BUF_NUM);
1910 regs_buff[30] = ATL2_READ_REG(hw, REG_DMAR);
1911 regs_buff[31] = ATL2_READ_REG(hw, REG_TX_CUT_THRESH);
1912 regs_buff[32] = ATL2_READ_REG(hw, REG_DMAW);
1913 regs_buff[33] = ATL2_READ_REG(hw, REG_PAUSE_ON_TH);
1914 regs_buff[34] = ATL2_READ_REG(hw, REG_PAUSE_OFF_TH);
1915 regs_buff[35] = ATL2_READ_REG(hw, REG_MB_TXD_WR_IDX);
1916 regs_buff[36] = ATL2_READ_REG(hw, REG_MB_RXD_RD_IDX);
1917 regs_buff[38] = ATL2_READ_REG(hw, REG_ISR);
1918 regs_buff[39] = ATL2_READ_REG(hw, REG_IMR);
1920 atl2_read_phy_reg(hw, MII_BMCR, &phy_data);
1921 regs_buff[40] = (u32)phy_data;
1922 atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1923 regs_buff[41] = (u32)phy_data;
1926 static int atl2_get_eeprom_len(struct net_device *netdev)
1928 struct atl2_adapter *adapter = netdev_priv(netdev);
1930 if (!atl2_check_eeprom_exist(&adapter->hw))
1931 return 512;
1932 else
1933 return 0;
1936 static int atl2_get_eeprom(struct net_device *netdev,
1937 struct ethtool_eeprom *eeprom, u8 *bytes)
1939 struct atl2_adapter *adapter = netdev_priv(netdev);
1940 struct atl2_hw *hw = &adapter->hw;
1941 u32 *eeprom_buff;
1942 int first_dword, last_dword;
1943 int ret_val = 0;
1944 int i;
1946 if (eeprom->len == 0)
1947 return -EINVAL;
1949 if (atl2_check_eeprom_exist(hw))
1950 return -EINVAL;
1952 eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1954 first_dword = eeprom->offset >> 2;
1955 last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
1957 eeprom_buff = kmalloc(sizeof(u32) * (last_dword - first_dword + 1),
1958 GFP_KERNEL);
1959 if (!eeprom_buff)
1960 return -ENOMEM;
1962 for (i = first_dword; i < last_dword; i++) {
1963 if (!atl2_read_eeprom(hw, i*4, &(eeprom_buff[i-first_dword])))
1964 return -EIO;
1967 memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 3),
1968 eeprom->len);
1969 kfree(eeprom_buff);
1971 return ret_val;
1974 static int atl2_set_eeprom(struct net_device *netdev,
1975 struct ethtool_eeprom *eeprom, u8 *bytes)
1977 struct atl2_adapter *adapter = netdev_priv(netdev);
1978 struct atl2_hw *hw = &adapter->hw;
1979 u32 *eeprom_buff;
1980 u32 *ptr;
1981 int max_len, first_dword, last_dword, ret_val = 0;
1982 int i;
1984 if (eeprom->len == 0)
1985 return -EOPNOTSUPP;
1987 if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
1988 return -EFAULT;
1990 max_len = 512;
1992 first_dword = eeprom->offset >> 2;
1993 last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
1994 eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1995 if (!eeprom_buff)
1996 return -ENOMEM;
1998 ptr = (u32 *)eeprom_buff;
2000 if (eeprom->offset & 3) {
2001 /* need read/modify/write of first changed EEPROM word */
2002 /* only the second byte of the word is being modified */
2003 if (!atl2_read_eeprom(hw, first_dword*4, &(eeprom_buff[0])))
2004 return -EIO;
2005 ptr++;
2007 if (((eeprom->offset + eeprom->len) & 3)) {
2009 * need read/modify/write of last changed EEPROM word
2010 * only the first byte of the word is being modified
2012 if (!atl2_read_eeprom(hw, last_dword * 4,
2013 &(eeprom_buff[last_dword - first_dword])))
2014 return -EIO;
2017 /* Device's eeprom is always little-endian, word addressable */
2018 memcpy(ptr, bytes, eeprom->len);
2020 for (i = 0; i < last_dword - first_dword + 1; i++) {
2021 if (!atl2_write_eeprom(hw, ((first_dword+i)*4), eeprom_buff[i]))
2022 return -EIO;
2025 kfree(eeprom_buff);
2026 return ret_val;
2029 static void atl2_get_drvinfo(struct net_device *netdev,
2030 struct ethtool_drvinfo *drvinfo)
2032 struct atl2_adapter *adapter = netdev_priv(netdev);
2034 strncpy(drvinfo->driver, atl2_driver_name, 32);
2035 strncpy(drvinfo->version, atl2_driver_version, 32);
2036 strncpy(drvinfo->fw_version, "L2", 32);
2037 strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
2038 drvinfo->n_stats = 0;
2039 drvinfo->testinfo_len = 0;
2040 drvinfo->regdump_len = atl2_get_regs_len(netdev);
2041 drvinfo->eedump_len = atl2_get_eeprom_len(netdev);
2044 static void atl2_get_wol(struct net_device *netdev,
2045 struct ethtool_wolinfo *wol)
2047 struct atl2_adapter *adapter = netdev_priv(netdev);
2049 wol->supported = WAKE_MAGIC;
2050 wol->wolopts = 0;
2052 if (adapter->wol & ATLX_WUFC_EX)
2053 wol->wolopts |= WAKE_UCAST;
2054 if (adapter->wol & ATLX_WUFC_MC)
2055 wol->wolopts |= WAKE_MCAST;
2056 if (adapter->wol & ATLX_WUFC_BC)
2057 wol->wolopts |= WAKE_BCAST;
2058 if (adapter->wol & ATLX_WUFC_MAG)
2059 wol->wolopts |= WAKE_MAGIC;
2060 if (adapter->wol & ATLX_WUFC_LNKC)
2061 wol->wolopts |= WAKE_PHY;
2064 static int atl2_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2066 struct atl2_adapter *adapter = netdev_priv(netdev);
2068 if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE))
2069 return -EOPNOTSUPP;
2071 if (wol->wolopts & (WAKE_UCAST | WAKE_BCAST | WAKE_MCAST))
2072 return -EOPNOTSUPP;
2074 /* these settings will always override what we currently have */
2075 adapter->wol = 0;
2077 if (wol->wolopts & WAKE_MAGIC)
2078 adapter->wol |= ATLX_WUFC_MAG;
2079 if (wol->wolopts & WAKE_PHY)
2080 adapter->wol |= ATLX_WUFC_LNKC;
2082 return 0;
2085 static int atl2_nway_reset(struct net_device *netdev)
2087 struct atl2_adapter *adapter = netdev_priv(netdev);
2088 if (netif_running(netdev))
2089 atl2_reinit_locked(adapter);
2090 return 0;
2093 static const struct ethtool_ops atl2_ethtool_ops = {
2094 .get_settings = atl2_get_settings,
2095 .set_settings = atl2_set_settings,
2096 .get_drvinfo = atl2_get_drvinfo,
2097 .get_regs_len = atl2_get_regs_len,
2098 .get_regs = atl2_get_regs,
2099 .get_wol = atl2_get_wol,
2100 .set_wol = atl2_set_wol,
2101 .get_msglevel = atl2_get_msglevel,
2102 .set_msglevel = atl2_set_msglevel,
2103 .nway_reset = atl2_nway_reset,
2104 .get_link = ethtool_op_get_link,
2105 .get_eeprom_len = atl2_get_eeprom_len,
2106 .get_eeprom = atl2_get_eeprom,
2107 .set_eeprom = atl2_set_eeprom,
2108 .get_tx_csum = atl2_get_tx_csum,
2109 .get_sg = ethtool_op_get_sg,
2110 .set_sg = ethtool_op_set_sg,
2111 #ifdef NETIF_F_TSO
2112 .get_tso = ethtool_op_get_tso,
2113 #endif
2116 static void atl2_set_ethtool_ops(struct net_device *netdev)
2118 SET_ETHTOOL_OPS(netdev, &atl2_ethtool_ops);
2121 #define LBYTESWAP(a) ((((a) & 0x00ff00ff) << 8) | \
2122 (((a) & 0xff00ff00) >> 8))
2123 #define LONGSWAP(a) ((LBYTESWAP(a) << 16) | (LBYTESWAP(a) >> 16))
2124 #define SHORTSWAP(a) (((a) << 8) | ((a) >> 8))
2127 * Reset the transmit and receive units; mask and clear all interrupts.
2129 * hw - Struct containing variables accessed by shared code
2130 * return : 0 or idle status (if error)
2132 static s32 atl2_reset_hw(struct atl2_hw *hw)
2134 u32 icr;
2135 u16 pci_cfg_cmd_word;
2136 int i;
2138 /* Workaround for PCI problem when BIOS sets MMRBC incorrectly. */
2139 atl2_read_pci_cfg(hw, PCI_REG_COMMAND, &pci_cfg_cmd_word);
2140 if ((pci_cfg_cmd_word &
2141 (CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER)) !=
2142 (CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER)) {
2143 pci_cfg_cmd_word |=
2144 (CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER);
2145 atl2_write_pci_cfg(hw, PCI_REG_COMMAND, &pci_cfg_cmd_word);
2148 /* Clear Interrupt mask to stop board from generating
2149 * interrupts & Clear any pending interrupt events
2151 /* FIXME */
2152 /* ATL2_WRITE_REG(hw, REG_IMR, 0); */
2153 /* ATL2_WRITE_REG(hw, REG_ISR, 0xffffffff); */
2155 /* Issue Soft Reset to the MAC. This will reset the chip's
2156 * transmit, receive, DMA. It will not effect
2157 * the current PCI configuration. The global reset bit is self-
2158 * clearing, and should clear within a microsecond.
2160 ATL2_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_SOFT_RST);
2161 wmb();
2162 msleep(1); /* delay about 1ms */
2164 /* Wait at least 10ms for All module to be Idle */
2165 for (i = 0; i < 10; i++) {
2166 icr = ATL2_READ_REG(hw, REG_IDLE_STATUS);
2167 if (!icr)
2168 break;
2169 msleep(1); /* delay 1 ms */
2170 cpu_relax();
2173 if (icr)
2174 return icr;
2176 return 0;
2179 #define CUSTOM_SPI_CS_SETUP 2
2180 #define CUSTOM_SPI_CLK_HI 2
2181 #define CUSTOM_SPI_CLK_LO 2
2182 #define CUSTOM_SPI_CS_HOLD 2
2183 #define CUSTOM_SPI_CS_HI 3
2185 static struct atl2_spi_flash_dev flash_table[] =
2187 /* MFR WRSR READ PROGRAM WREN WRDI RDSR RDID SECTOR_ERASE CHIP_ERASE */
2188 {"Atmel", 0x0, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62 },
2189 {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60 },
2190 {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7 },
2193 static bool atl2_spi_read(struct atl2_hw *hw, u32 addr, u32 *buf)
2195 int i;
2196 u32 value;
2198 ATL2_WRITE_REG(hw, REG_SPI_DATA, 0);
2199 ATL2_WRITE_REG(hw, REG_SPI_ADDR, addr);
2201 value = SPI_FLASH_CTRL_WAIT_READY |
2202 (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
2203 SPI_FLASH_CTRL_CS_SETUP_SHIFT |
2204 (CUSTOM_SPI_CLK_HI & SPI_FLASH_CTRL_CLK_HI_MASK) <<
2205 SPI_FLASH_CTRL_CLK_HI_SHIFT |
2206 (CUSTOM_SPI_CLK_LO & SPI_FLASH_CTRL_CLK_LO_MASK) <<
2207 SPI_FLASH_CTRL_CLK_LO_SHIFT |
2208 (CUSTOM_SPI_CS_HOLD & SPI_FLASH_CTRL_CS_HOLD_MASK) <<
2209 SPI_FLASH_CTRL_CS_HOLD_SHIFT |
2210 (CUSTOM_SPI_CS_HI & SPI_FLASH_CTRL_CS_HI_MASK) <<
2211 SPI_FLASH_CTRL_CS_HI_SHIFT |
2212 (0x1 & SPI_FLASH_CTRL_INS_MASK) << SPI_FLASH_CTRL_INS_SHIFT;
2214 ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2216 value |= SPI_FLASH_CTRL_START;
2218 ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2220 for (i = 0; i < 10; i++) {
2221 msleep(1);
2222 value = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
2223 if (!(value & SPI_FLASH_CTRL_START))
2224 break;
2227 if (value & SPI_FLASH_CTRL_START)
2228 return false;
2230 *buf = ATL2_READ_REG(hw, REG_SPI_DATA);
2232 return true;
2236 * get_permanent_address
2237 * return 0 if get valid mac address,
2239 static int get_permanent_address(struct atl2_hw *hw)
2241 u32 Addr[2];
2242 u32 i, Control;
2243 u16 Register;
2244 u8 EthAddr[NODE_ADDRESS_SIZE];
2245 bool KeyValid;
2247 if (is_valid_ether_addr(hw->perm_mac_addr))
2248 return 0;
2250 Addr[0] = 0;
2251 Addr[1] = 0;
2253 if (!atl2_check_eeprom_exist(hw)) { /* eeprom exists */
2254 Register = 0;
2255 KeyValid = false;
2257 /* Read out all EEPROM content */
2258 i = 0;
2259 while (1) {
2260 if (atl2_read_eeprom(hw, i + 0x100, &Control)) {
2261 if (KeyValid) {
2262 if (Register == REG_MAC_STA_ADDR)
2263 Addr[0] = Control;
2264 else if (Register ==
2265 (REG_MAC_STA_ADDR + 4))
2266 Addr[1] = Control;
2267 KeyValid = false;
2268 } else if ((Control & 0xff) == 0x5A) {
2269 KeyValid = true;
2270 Register = (u16) (Control >> 16);
2271 } else {
2272 /* assume data end while encount an invalid KEYWORD */
2273 break;
2275 } else {
2276 break; /* read error */
2278 i += 4;
2281 *(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2282 *(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *) &Addr[1]);
2284 if (is_valid_ether_addr(EthAddr)) {
2285 memcpy(hw->perm_mac_addr, EthAddr, NODE_ADDRESS_SIZE);
2286 return 0;
2288 return 1;
2291 /* see if SPI flash exists? */
2292 Addr[0] = 0;
2293 Addr[1] = 0;
2294 Register = 0;
2295 KeyValid = false;
2296 i = 0;
2297 while (1) {
2298 if (atl2_spi_read(hw, i + 0x1f000, &Control)) {
2299 if (KeyValid) {
2300 if (Register == REG_MAC_STA_ADDR)
2301 Addr[0] = Control;
2302 else if (Register == (REG_MAC_STA_ADDR + 4))
2303 Addr[1] = Control;
2304 KeyValid = false;
2305 } else if ((Control & 0xff) == 0x5A) {
2306 KeyValid = true;
2307 Register = (u16) (Control >> 16);
2308 } else {
2309 break; /* data end */
2311 } else {
2312 break; /* read error */
2314 i += 4;
2317 *(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2318 *(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *)&Addr[1]);
2319 if (is_valid_ether_addr(EthAddr)) {
2320 memcpy(hw->perm_mac_addr, EthAddr, NODE_ADDRESS_SIZE);
2321 return 0;
2323 /* maybe MAC-address is from BIOS */
2324 Addr[0] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR);
2325 Addr[1] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR + 4);
2326 *(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2327 *(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *) &Addr[1]);
2329 if (is_valid_ether_addr(EthAddr)) {
2330 memcpy(hw->perm_mac_addr, EthAddr, NODE_ADDRESS_SIZE);
2331 return 0;
2334 return 1;
2338 * Reads the adapter's MAC address from the EEPROM
2340 * hw - Struct containing variables accessed by shared code
2342 static s32 atl2_read_mac_addr(struct atl2_hw *hw)
2344 u16 i;
2346 if (get_permanent_address(hw)) {
2347 /* for test */
2348 /* FIXME: shouldn't we use random_ether_addr() here? */
2349 hw->perm_mac_addr[0] = 0x00;
2350 hw->perm_mac_addr[1] = 0x13;
2351 hw->perm_mac_addr[2] = 0x74;
2352 hw->perm_mac_addr[3] = 0x00;
2353 hw->perm_mac_addr[4] = 0x5c;
2354 hw->perm_mac_addr[5] = 0x38;
2357 for (i = 0; i < NODE_ADDRESS_SIZE; i++)
2358 hw->mac_addr[i] = hw->perm_mac_addr[i];
2360 return 0;
2364 * Hashes an address to determine its location in the multicast table
2366 * hw - Struct containing variables accessed by shared code
2367 * mc_addr - the multicast address to hash
2369 * atl2_hash_mc_addr
2370 * purpose
2371 * set hash value for a multicast address
2372 * hash calcu processing :
2373 * 1. calcu 32bit CRC for multicast address
2374 * 2. reverse crc with MSB to LSB
2376 static u32 atl2_hash_mc_addr(struct atl2_hw *hw, u8 *mc_addr)
2378 u32 crc32, value;
2379 int i;
2381 value = 0;
2382 crc32 = ether_crc_le(6, mc_addr);
2384 for (i = 0; i < 32; i++)
2385 value |= (((crc32 >> i) & 1) << (31 - i));
2387 return value;
2391 * Sets the bit in the multicast table corresponding to the hash value.
2393 * hw - Struct containing variables accessed by shared code
2394 * hash_value - Multicast address hash value
2396 static void atl2_hash_set(struct atl2_hw *hw, u32 hash_value)
2398 u32 hash_bit, hash_reg;
2399 u32 mta;
2401 /* The HASH Table is a register array of 2 32-bit registers.
2402 * It is treated like an array of 64 bits. We want to set
2403 * bit BitArray[hash_value]. So we figure out what register
2404 * the bit is in, read it, OR in the new bit, then write
2405 * back the new value. The register is determined by the
2406 * upper 7 bits of the hash value and the bit within that
2407 * register are determined by the lower 5 bits of the value.
2409 hash_reg = (hash_value >> 31) & 0x1;
2410 hash_bit = (hash_value >> 26) & 0x1F;
2412 mta = ATL2_READ_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg);
2414 mta |= (1 << hash_bit);
2416 ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg, mta);
2420 * atl2_init_pcie - init PCIE module
2422 static void atl2_init_pcie(struct atl2_hw *hw)
2424 u32 value;
2425 value = LTSSM_TEST_MODE_DEF;
2426 ATL2_WRITE_REG(hw, REG_LTSSM_TEST_MODE, value);
2428 value = PCIE_DLL_TX_CTRL1_DEF;
2429 ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, value);
2432 static void atl2_init_flash_opcode(struct atl2_hw *hw)
2434 if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
2435 hw->flash_vendor = 0; /* ATMEL */
2437 /* Init OP table */
2438 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_PROGRAM,
2439 flash_table[hw->flash_vendor].cmdPROGRAM);
2440 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_SC_ERASE,
2441 flash_table[hw->flash_vendor].cmdSECTOR_ERASE);
2442 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_CHIP_ERASE,
2443 flash_table[hw->flash_vendor].cmdCHIP_ERASE);
2444 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_RDID,
2445 flash_table[hw->flash_vendor].cmdRDID);
2446 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_WREN,
2447 flash_table[hw->flash_vendor].cmdWREN);
2448 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_RDSR,
2449 flash_table[hw->flash_vendor].cmdRDSR);
2450 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_WRSR,
2451 flash_table[hw->flash_vendor].cmdWRSR);
2452 ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_READ,
2453 flash_table[hw->flash_vendor].cmdREAD);
2456 /********************************************************************
2457 * Performs basic configuration of the adapter.
2459 * hw - Struct containing variables accessed by shared code
2460 * Assumes that the controller has previously been reset and is in a
2461 * post-reset uninitialized state. Initializes multicast table,
2462 * and Calls routines to setup link
2463 * Leaves the transmit and receive units disabled and uninitialized.
2464 ********************************************************************/
2465 static s32 atl2_init_hw(struct atl2_hw *hw)
2467 u32 ret_val = 0;
2469 atl2_init_pcie(hw);
2471 /* Zero out the Multicast HASH table */
2472 /* clear the old settings from the multicast hash table */
2473 ATL2_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
2474 ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
2476 atl2_init_flash_opcode(hw);
2478 ret_val = atl2_phy_init(hw);
2480 return ret_val;
2484 * Detects the current speed and duplex settings of the hardware.
2486 * hw - Struct containing variables accessed by shared code
2487 * speed - Speed of the connection
2488 * duplex - Duplex setting of the connection
2490 static s32 atl2_get_speed_and_duplex(struct atl2_hw *hw, u16 *speed,
2491 u16 *duplex)
2493 s32 ret_val;
2494 u16 phy_data;
2496 /* Read PHY Specific Status Register (17) */
2497 ret_val = atl2_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
2498 if (ret_val)
2499 return ret_val;
2501 if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
2502 return ATLX_ERR_PHY_RES;
2504 switch (phy_data & MII_ATLX_PSSR_SPEED) {
2505 case MII_ATLX_PSSR_100MBS:
2506 *speed = SPEED_100;
2507 break;
2508 case MII_ATLX_PSSR_10MBS:
2509 *speed = SPEED_10;
2510 break;
2511 default:
2512 return ATLX_ERR_PHY_SPEED;
2513 break;
2516 if (phy_data & MII_ATLX_PSSR_DPLX)
2517 *duplex = FULL_DUPLEX;
2518 else
2519 *duplex = HALF_DUPLEX;
2521 return 0;
2525 * Reads the value from a PHY register
2526 * hw - Struct containing variables accessed by shared code
2527 * reg_addr - address of the PHY register to read
2529 static s32 atl2_read_phy_reg(struct atl2_hw *hw, u16 reg_addr, u16 *phy_data)
2531 u32 val;
2532 int i;
2534 val = ((u32)(reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
2535 MDIO_START |
2536 MDIO_SUP_PREAMBLE |
2537 MDIO_RW |
2538 MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2539 ATL2_WRITE_REG(hw, REG_MDIO_CTRL, val);
2541 wmb();
2543 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2544 udelay(2);
2545 val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2546 if (!(val & (MDIO_START | MDIO_BUSY)))
2547 break;
2548 wmb();
2550 if (!(val & (MDIO_START | MDIO_BUSY))) {
2551 *phy_data = (u16)val;
2552 return 0;
2555 return ATLX_ERR_PHY;
2559 * Writes a value to a PHY register
2560 * hw - Struct containing variables accessed by shared code
2561 * reg_addr - address of the PHY register to write
2562 * data - data to write to the PHY
2564 static s32 atl2_write_phy_reg(struct atl2_hw *hw, u32 reg_addr, u16 phy_data)
2566 int i;
2567 u32 val;
2569 val = ((u32)(phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
2570 (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
2571 MDIO_SUP_PREAMBLE |
2572 MDIO_START |
2573 MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2574 ATL2_WRITE_REG(hw, REG_MDIO_CTRL, val);
2576 wmb();
2578 for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2579 udelay(2);
2580 val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2581 if (!(val & (MDIO_START | MDIO_BUSY)))
2582 break;
2584 wmb();
2587 if (!(val & (MDIO_START | MDIO_BUSY)))
2588 return 0;
2590 return ATLX_ERR_PHY;
2594 * Configures PHY autoneg and flow control advertisement settings
2596 * hw - Struct containing variables accessed by shared code
2598 static s32 atl2_phy_setup_autoneg_adv(struct atl2_hw *hw)
2600 s32 ret_val;
2601 s16 mii_autoneg_adv_reg;
2603 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
2604 mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
2606 /* Need to parse autoneg_advertised and set up
2607 * the appropriate PHY registers. First we will parse for
2608 * autoneg_advertised software override. Since we can advertise
2609 * a plethora of combinations, we need to check each bit
2610 * individually.
2613 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
2614 * Advertisement Register (Address 4) and the 1000 mb speed bits in
2615 * the 1000Base-T Control Register (Address 9). */
2616 mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
2618 /* Need to parse MediaType and setup the
2619 * appropriate PHY registers. */
2620 switch (hw->MediaType) {
2621 case MEDIA_TYPE_AUTO_SENSOR:
2622 mii_autoneg_adv_reg |=
2623 (MII_AR_10T_HD_CAPS |
2624 MII_AR_10T_FD_CAPS |
2625 MII_AR_100TX_HD_CAPS|
2626 MII_AR_100TX_FD_CAPS);
2627 hw->autoneg_advertised =
2628 ADVERTISE_10_HALF |
2629 ADVERTISE_10_FULL |
2630 ADVERTISE_100_HALF|
2631 ADVERTISE_100_FULL;
2632 break;
2633 case MEDIA_TYPE_100M_FULL:
2634 mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
2635 hw->autoneg_advertised = ADVERTISE_100_FULL;
2636 break;
2637 case MEDIA_TYPE_100M_HALF:
2638 mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
2639 hw->autoneg_advertised = ADVERTISE_100_HALF;
2640 break;
2641 case MEDIA_TYPE_10M_FULL:
2642 mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
2643 hw->autoneg_advertised = ADVERTISE_10_FULL;
2644 break;
2645 default:
2646 mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
2647 hw->autoneg_advertised = ADVERTISE_10_HALF;
2648 break;
2651 /* flow control fixed to enable all */
2652 mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
2654 hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
2656 ret_val = atl2_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
2658 if (ret_val)
2659 return ret_val;
2661 return 0;
2665 * Resets the PHY and make all config validate
2667 * hw - Struct containing variables accessed by shared code
2669 * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
2671 static s32 atl2_phy_commit(struct atl2_hw *hw)
2673 s32 ret_val;
2674 u16 phy_data;
2676 phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG;
2677 ret_val = atl2_write_phy_reg(hw, MII_BMCR, phy_data);
2678 if (ret_val) {
2679 u32 val;
2680 int i;
2681 /* pcie serdes link may be down ! */
2682 for (i = 0; i < 25; i++) {
2683 msleep(1);
2684 val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2685 if (!(val & (MDIO_START | MDIO_BUSY)))
2686 break;
2689 if (0 != (val & (MDIO_START | MDIO_BUSY))) {
2690 printk(KERN_ERR "atl2: PCIe link down for at least 25ms !\n");
2691 return ret_val;
2694 return 0;
2697 static s32 atl2_phy_init(struct atl2_hw *hw)
2699 s32 ret_val;
2700 u16 phy_val;
2702 if (hw->phy_configured)
2703 return 0;
2705 /* Enable PHY */
2706 ATL2_WRITE_REGW(hw, REG_PHY_ENABLE, 1);
2707 ATL2_WRITE_FLUSH(hw);
2708 msleep(1);
2710 /* check if the PHY is in powersaving mode */
2711 atl2_write_phy_reg(hw, MII_DBG_ADDR, 0);
2712 atl2_read_phy_reg(hw, MII_DBG_DATA, &phy_val);
2714 /* 024E / 124E 0r 0274 / 1274 ? */
2715 if (phy_val & 0x1000) {
2716 phy_val &= ~0x1000;
2717 atl2_write_phy_reg(hw, MII_DBG_DATA, phy_val);
2720 msleep(1);
2722 /*Enable PHY LinkChange Interrupt */
2723 ret_val = atl2_write_phy_reg(hw, 18, 0xC00);
2724 if (ret_val)
2725 return ret_val;
2727 /* setup AutoNeg parameters */
2728 ret_val = atl2_phy_setup_autoneg_adv(hw);
2729 if (ret_val)
2730 return ret_val;
2732 /* SW.Reset & En-Auto-Neg to restart Auto-Neg */
2733 ret_val = atl2_phy_commit(hw);
2734 if (ret_val)
2735 return ret_val;
2737 hw->phy_configured = true;
2739 return ret_val;
2742 static void atl2_set_mac_addr(struct atl2_hw *hw)
2744 u32 value;
2745 /* 00-0B-6A-F6-00-DC
2746 * 0: 6AF600DC 1: 000B
2747 * low dword */
2748 value = (((u32)hw->mac_addr[2]) << 24) |
2749 (((u32)hw->mac_addr[3]) << 16) |
2750 (((u32)hw->mac_addr[4]) << 8) |
2751 (((u32)hw->mac_addr[5]));
2752 ATL2_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 0, value);
2753 /* hight dword */
2754 value = (((u32)hw->mac_addr[0]) << 8) |
2755 (((u32)hw->mac_addr[1]));
2756 ATL2_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 1, value);
2760 * check_eeprom_exist
2761 * return 0 if eeprom exist
2763 static int atl2_check_eeprom_exist(struct atl2_hw *hw)
2765 u32 value;
2767 value = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
2768 if (value & SPI_FLASH_CTRL_EN_VPD) {
2769 value &= ~SPI_FLASH_CTRL_EN_VPD;
2770 ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2772 value = ATL2_READ_REGW(hw, REG_PCIE_CAP_LIST);
2773 return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
2776 /* FIXME: This doesn't look right. -- CHS */
2777 static bool atl2_write_eeprom(struct atl2_hw *hw, u32 offset, u32 value)
2779 return true;
2782 static bool atl2_read_eeprom(struct atl2_hw *hw, u32 Offset, u32 *pValue)
2784 int i;
2785 u32 Control;
2787 if (Offset & 0x3)
2788 return false; /* address do not align */
2790 ATL2_WRITE_REG(hw, REG_VPD_DATA, 0);
2791 Control = (Offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
2792 ATL2_WRITE_REG(hw, REG_VPD_CAP, Control);
2794 for (i = 0; i < 10; i++) {
2795 msleep(2);
2796 Control = ATL2_READ_REG(hw, REG_VPD_CAP);
2797 if (Control & VPD_CAP_VPD_FLAG)
2798 break;
2801 if (Control & VPD_CAP_VPD_FLAG) {
2802 *pValue = ATL2_READ_REG(hw, REG_VPD_DATA);
2803 return true;
2805 return false; /* timeout */
2808 static void atl2_force_ps(struct atl2_hw *hw)
2810 u16 phy_val;
2812 atl2_write_phy_reg(hw, MII_DBG_ADDR, 0);
2813 atl2_read_phy_reg(hw, MII_DBG_DATA, &phy_val);
2814 atl2_write_phy_reg(hw, MII_DBG_DATA, phy_val | 0x1000);
2816 atl2_write_phy_reg(hw, MII_DBG_ADDR, 2);
2817 atl2_write_phy_reg(hw, MII_DBG_DATA, 0x3000);
2818 atl2_write_phy_reg(hw, MII_DBG_ADDR, 3);
2819 atl2_write_phy_reg(hw, MII_DBG_DATA, 0);
2822 /* This is the only thing that needs to be changed to adjust the
2823 * maximum number of ports that the driver can manage.
2825 #define ATL2_MAX_NIC 4
2827 #define OPTION_UNSET -1
2828 #define OPTION_DISABLED 0
2829 #define OPTION_ENABLED 1
2831 /* All parameters are treated the same, as an integer array of values.
2832 * This macro just reduces the need to repeat the same declaration code
2833 * over and over (plus this helps to avoid typo bugs).
2835 #define ATL2_PARAM_INIT {[0 ... ATL2_MAX_NIC] = OPTION_UNSET}
2836 #ifndef module_param_array
2837 /* Module Parameters are always initialized to -1, so that the driver
2838 * can tell the difference between no user specified value or the
2839 * user asking for the default value.
2840 * The true default values are loaded in when atl2_check_options is called.
2842 * This is a GCC extension to ANSI C.
2843 * See the item "Labeled Elements in Initializers" in the section
2844 * "Extensions to the C Language Family" of the GCC documentation.
2847 #define ATL2_PARAM(X, desc) \
2848 static const int __devinitdata X[ATL2_MAX_NIC + 1] = ATL2_PARAM_INIT; \
2849 MODULE_PARM(X, "1-" __MODULE_STRING(ATL2_MAX_NIC) "i"); \
2850 MODULE_PARM_DESC(X, desc);
2851 #else
2852 #define ATL2_PARAM(X, desc) \
2853 static int __devinitdata X[ATL2_MAX_NIC+1] = ATL2_PARAM_INIT; \
2854 static unsigned int num_##X; \
2855 module_param_array_named(X, X, int, &num_##X, 0); \
2856 MODULE_PARM_DESC(X, desc);
2857 #endif
2860 * Transmit Memory Size
2861 * Valid Range: 64-2048
2862 * Default Value: 128
2864 #define ATL2_MIN_TX_MEMSIZE 4 /* 4KB */
2865 #define ATL2_MAX_TX_MEMSIZE 64 /* 64KB */
2866 #define ATL2_DEFAULT_TX_MEMSIZE 8 /* 8KB */
2867 ATL2_PARAM(TxMemSize, "Bytes of Transmit Memory");
2870 * Receive Memory Block Count
2871 * Valid Range: 16-512
2872 * Default Value: 128
2874 #define ATL2_MIN_RXD_COUNT 16
2875 #define ATL2_MAX_RXD_COUNT 512
2876 #define ATL2_DEFAULT_RXD_COUNT 64
2877 ATL2_PARAM(RxMemBlock, "Number of receive memory block");
2880 * User Specified MediaType Override
2882 * Valid Range: 0-5
2883 * - 0 - auto-negotiate at all supported speeds
2884 * - 1 - only link at 1000Mbps Full Duplex
2885 * - 2 - only link at 100Mbps Full Duplex
2886 * - 3 - only link at 100Mbps Half Duplex
2887 * - 4 - only link at 10Mbps Full Duplex
2888 * - 5 - only link at 10Mbps Half Duplex
2889 * Default Value: 0
2891 ATL2_PARAM(MediaType, "MediaType Select");
2894 * Interrupt Moderate Timer in units of 2048 ns (~2 us)
2895 * Valid Range: 10-65535
2896 * Default Value: 45000(90ms)
2898 #define INT_MOD_DEFAULT_CNT 100 /* 200us */
2899 #define INT_MOD_MAX_CNT 65000
2900 #define INT_MOD_MIN_CNT 50
2901 ATL2_PARAM(IntModTimer, "Interrupt Moderator Timer");
2904 * FlashVendor
2905 * Valid Range: 0-2
2906 * 0 - Atmel
2907 * 1 - SST
2908 * 2 - ST
2910 ATL2_PARAM(FlashVendor, "SPI Flash Vendor");
2912 #define AUTONEG_ADV_DEFAULT 0x2F
2913 #define AUTONEG_ADV_MASK 0x2F
2914 #define FLOW_CONTROL_DEFAULT FLOW_CONTROL_FULL
2916 #define FLASH_VENDOR_DEFAULT 0
2917 #define FLASH_VENDOR_MIN 0
2918 #define FLASH_VENDOR_MAX 2
2920 struct atl2_option {
2921 enum { enable_option, range_option, list_option } type;
2922 char *name;
2923 char *err;
2924 int def;
2925 union {
2926 struct { /* range_option info */
2927 int min;
2928 int max;
2929 } r;
2930 struct { /* list_option info */
2931 int nr;
2932 struct atl2_opt_list { int i; char *str; } *p;
2933 } l;
2934 } arg;
2937 static int __devinit atl2_validate_option(int *value, struct atl2_option *opt)
2939 int i;
2940 struct atl2_opt_list *ent;
2942 if (*value == OPTION_UNSET) {
2943 *value = opt->def;
2944 return 0;
2947 switch (opt->type) {
2948 case enable_option:
2949 switch (*value) {
2950 case OPTION_ENABLED:
2951 printk(KERN_INFO "%s Enabled\n", opt->name);
2952 return 0;
2953 break;
2954 case OPTION_DISABLED:
2955 printk(KERN_INFO "%s Disabled\n", opt->name);
2956 return 0;
2957 break;
2959 break;
2960 case range_option:
2961 if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
2962 printk(KERN_INFO "%s set to %i\n", opt->name, *value);
2963 return 0;
2965 break;
2966 case list_option:
2967 for (i = 0; i < opt->arg.l.nr; i++) {
2968 ent = &opt->arg.l.p[i];
2969 if (*value == ent->i) {
2970 if (ent->str[0] != '\0')
2971 printk(KERN_INFO "%s\n", ent->str);
2972 return 0;
2975 break;
2976 default:
2977 BUG();
2980 printk(KERN_INFO "Invalid %s specified (%i) %s\n",
2981 opt->name, *value, opt->err);
2982 *value = opt->def;
2983 return -1;
2987 * atl2_check_options - Range Checking for Command Line Parameters
2988 * @adapter: board private structure
2990 * This routine checks all command line parameters for valid user
2991 * input. If an invalid value is given, or if no user specified
2992 * value exists, a default value is used. The final value is stored
2993 * in a variable in the adapter structure.
2995 static void __devinit atl2_check_options(struct atl2_adapter *adapter)
2997 int val;
2998 struct atl2_option opt;
2999 int bd = adapter->bd_number;
3000 if (bd >= ATL2_MAX_NIC) {
3001 printk(KERN_NOTICE "Warning: no configuration for board #%i\n",
3002 bd);
3003 printk(KERN_NOTICE "Using defaults for all values\n");
3004 #ifndef module_param_array
3005 bd = ATL2_MAX_NIC;
3006 #endif
3009 /* Bytes of Transmit Memory */
3010 opt.type = range_option;
3011 opt.name = "Bytes of Transmit Memory";
3012 opt.err = "using default of " __MODULE_STRING(ATL2_DEFAULT_TX_MEMSIZE);
3013 opt.def = ATL2_DEFAULT_TX_MEMSIZE;
3014 opt.arg.r.min = ATL2_MIN_TX_MEMSIZE;
3015 opt.arg.r.max = ATL2_MAX_TX_MEMSIZE;
3016 #ifdef module_param_array
3017 if (num_TxMemSize > bd) {
3018 #endif
3019 val = TxMemSize[bd];
3020 atl2_validate_option(&val, &opt);
3021 adapter->txd_ring_size = ((u32) val) * 1024;
3022 #ifdef module_param_array
3023 } else
3024 adapter->txd_ring_size = ((u32)opt.def) * 1024;
3025 #endif
3026 /* txs ring size: */
3027 adapter->txs_ring_size = adapter->txd_ring_size / 128;
3028 if (adapter->txs_ring_size > 160)
3029 adapter->txs_ring_size = 160;
3031 /* Receive Memory Block Count */
3032 opt.type = range_option;
3033 opt.name = "Number of receive memory block";
3034 opt.err = "using default of " __MODULE_STRING(ATL2_DEFAULT_RXD_COUNT);
3035 opt.def = ATL2_DEFAULT_RXD_COUNT;
3036 opt.arg.r.min = ATL2_MIN_RXD_COUNT;
3037 opt.arg.r.max = ATL2_MAX_RXD_COUNT;
3038 #ifdef module_param_array
3039 if (num_RxMemBlock > bd) {
3040 #endif
3041 val = RxMemBlock[bd];
3042 atl2_validate_option(&val, &opt);
3043 adapter->rxd_ring_size = (u32)val;
3044 /* FIXME */
3045 /* ((u16)val)&~1; */ /* even number */
3046 #ifdef module_param_array
3047 } else
3048 adapter->rxd_ring_size = (u32)opt.def;
3049 #endif
3050 /* init RXD Flow control value */
3051 adapter->hw.fc_rxd_hi = (adapter->rxd_ring_size / 8) * 7;
3052 adapter->hw.fc_rxd_lo = (ATL2_MIN_RXD_COUNT / 8) >
3053 (adapter->rxd_ring_size / 12) ? (ATL2_MIN_RXD_COUNT / 8) :
3054 (adapter->rxd_ring_size / 12);
3056 /* Interrupt Moderate Timer */
3057 opt.type = range_option;
3058 opt.name = "Interrupt Moderate Timer";
3059 opt.err = "using default of " __MODULE_STRING(INT_MOD_DEFAULT_CNT);
3060 opt.def = INT_MOD_DEFAULT_CNT;
3061 opt.arg.r.min = INT_MOD_MIN_CNT;
3062 opt.arg.r.max = INT_MOD_MAX_CNT;
3063 #ifdef module_param_array
3064 if (num_IntModTimer > bd) {
3065 #endif
3066 val = IntModTimer[bd];
3067 atl2_validate_option(&val, &opt);
3068 adapter->imt = (u16) val;
3069 #ifdef module_param_array
3070 } else
3071 adapter->imt = (u16)(opt.def);
3072 #endif
3073 /* Flash Vendor */
3074 opt.type = range_option;
3075 opt.name = "SPI Flash Vendor";
3076 opt.err = "using default of " __MODULE_STRING(FLASH_VENDOR_DEFAULT);
3077 opt.def = FLASH_VENDOR_DEFAULT;
3078 opt.arg.r.min = FLASH_VENDOR_MIN;
3079 opt.arg.r.max = FLASH_VENDOR_MAX;
3080 #ifdef module_param_array
3081 if (num_FlashVendor > bd) {
3082 #endif
3083 val = FlashVendor[bd];
3084 atl2_validate_option(&val, &opt);
3085 adapter->hw.flash_vendor = (u8) val;
3086 #ifdef module_param_array
3087 } else
3088 adapter->hw.flash_vendor = (u8)(opt.def);
3089 #endif
3090 /* MediaType */
3091 opt.type = range_option;
3092 opt.name = "Speed/Duplex Selection";
3093 opt.err = "using default of " __MODULE_STRING(MEDIA_TYPE_AUTO_SENSOR);
3094 opt.def = MEDIA_TYPE_AUTO_SENSOR;
3095 opt.arg.r.min = MEDIA_TYPE_AUTO_SENSOR;
3096 opt.arg.r.max = MEDIA_TYPE_10M_HALF;
3097 #ifdef module_param_array
3098 if (num_MediaType > bd) {
3099 #endif
3100 val = MediaType[bd];
3101 atl2_validate_option(&val, &opt);
3102 adapter->hw.MediaType = (u16) val;
3103 #ifdef module_param_array
3104 } else
3105 adapter->hw.MediaType = (u16)(opt.def);
3106 #endif