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)
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
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 <linux/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>
31 #include <linux/interrupt.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>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/tcp.h>
46 #include <linux/timer.h>
47 #include <linux/types.h>
48 #include <linux/workqueue.h>
52 #define ATL2_DRV_VERSION "2.2.3"
54 static const char atl2_driver_name
[] = "atl2";
55 static const char atl2_driver_string
[] = "Atheros(R) L2 Ethernet Driver";
56 static const char atl2_copyright
[] = "Copyright (c) 2007 Atheros Corporation.";
57 static const char atl2_driver_version
[] = ATL2_DRV_VERSION
;
58 static const struct ethtool_ops atl2_ethtool_ops
;
60 MODULE_AUTHOR("Atheros Corporation <xiong.huang@atheros.com>, Chris Snook <csnook@redhat.com>");
61 MODULE_DESCRIPTION("Atheros Fast Ethernet Network Driver");
62 MODULE_LICENSE("GPL");
63 MODULE_VERSION(ATL2_DRV_VERSION
);
66 * atl2_pci_tbl - PCI Device ID Table
68 static const struct pci_device_id atl2_pci_tbl
[] = {
69 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC
, PCI_DEVICE_ID_ATTANSIC_L2
)},
70 /* required last entry */
73 MODULE_DEVICE_TABLE(pci
, atl2_pci_tbl
);
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 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
;
95 hw
->revision_id
= pdev
->revision
;
97 pci_read_config_word(pdev
, PCI_COMMAND
, &hw
->pci_cmd_word
);
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;
116 hw
->max_frame_size
= adapter
->netdev
->mtu
;
118 spin_lock_init(&adapter
->stats_lock
);
120 set_bit(__ATL2_DOWN
, &adapter
->flags
);
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 netdev_hw_addr
*ha
;
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
;
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 netdev_for_each_mc_addr(ha
, netdev
) {
161 hash_value
= atl2_hash_mc_addr(hw
, ha
->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
;
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
));
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);
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
);
255 ATL2_WRITE_REG(hw
, REG_MTU
, adapter
->netdev
->mtu
+
256 ENET_HEADER_SIZE
+ VLAN_SIZE
+ ETHERNET_FCS_SIZE
);
259 ATL2_WRITE_REG(hw
, REG_TX_CUT_THRESH
, 0x177);
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
);
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 */
279 /* clear all interrupt status */
280 ATL2_WRITE_REG(&adapter
->hw
, REG_ISR
, 0x3fffffff);
281 ATL2_WRITE_REG(&adapter
->hw
, REG_ISR
, 0);
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
;
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
,
305 if (!adapter
->ring_vir_addr
)
307 memset(adapter
->ring_vir_addr
, 0, adapter
->ring_size
);
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
= adapter
->ring_vir_addr
+ offset
;
316 adapter
->txs_dma
= adapter
->txd_dma
+ adapter
->txd_ring_size
;
317 offset
= (adapter
->txs_dma
& 0x7) ? (8 - (adapter
->txs_dma
& 0x7)) : 0;
318 adapter
->txs_dma
+= offset
;
319 adapter
->txs_ring
= (struct tx_pkt_status
*)
320 (((u8
*)adapter
->txd_ring
) + (adapter
->txd_ring_size
+ offset
));
323 adapter
->rxd_dma
= adapter
->txs_dma
+ adapter
->txs_ring_size
* 4;
324 offset
= (adapter
->rxd_dma
& 127) ?
325 (128 - (adapter
->rxd_dma
& 127)) : 0;
331 adapter
->rxd_dma
+= offset
;
332 adapter
->rxd_ring
= (struct rx_desc
*) (((u8
*)adapter
->txs_ring
) +
333 (adapter
->txs_ring_size
* 4 + offset
));
336 * Read / Write Ptr Initialize:
337 * init_ring_ptrs(adapter);
343 * atl2_irq_enable - Enable default interrupt generation settings
344 * @adapter: board private structure
346 static inline void atl2_irq_enable(struct atl2_adapter
*adapter
)
348 ATL2_WRITE_REG(&adapter
->hw
, REG_IMR
, IMR_NORMAL_MASK
);
349 ATL2_WRITE_FLUSH(&adapter
->hw
);
353 * atl2_irq_disable - Mask off interrupt generation on the NIC
354 * @adapter: board private structure
356 static inline void atl2_irq_disable(struct atl2_adapter
*adapter
)
358 ATL2_WRITE_REG(&adapter
->hw
, REG_IMR
, 0);
359 ATL2_WRITE_FLUSH(&adapter
->hw
);
360 synchronize_irq(adapter
->pdev
->irq
);
363 static void __atl2_vlan_mode(netdev_features_t features
, u32
*ctrl
)
365 if (features
& NETIF_F_HW_VLAN_CTAG_RX
) {
366 /* enable VLAN tag insert/strip */
367 *ctrl
|= MAC_CTRL_RMV_VLAN
;
369 /* disable VLAN tag insert/strip */
370 *ctrl
&= ~MAC_CTRL_RMV_VLAN
;
374 static void atl2_vlan_mode(struct net_device
*netdev
,
375 netdev_features_t features
)
377 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
380 atl2_irq_disable(adapter
);
382 ctrl
= ATL2_READ_REG(&adapter
->hw
, REG_MAC_CTRL
);
383 __atl2_vlan_mode(features
, &ctrl
);
384 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_mode(adapter
->netdev
, adapter
->netdev
->features
);
394 static netdev_features_t
atl2_fix_features(struct net_device
*netdev
,
395 netdev_features_t features
)
398 * Since there is no support for separate rx/tx vlan accel
399 * enable/disable make sure tx flag is always in same state as rx.
401 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
402 features
|= NETIF_F_HW_VLAN_CTAG_TX
;
404 features
&= ~NETIF_F_HW_VLAN_CTAG_TX
;
409 static int atl2_set_features(struct net_device
*netdev
,
410 netdev_features_t features
)
412 netdev_features_t changed
= netdev
->features
^ features
;
414 if (changed
& NETIF_F_HW_VLAN_CTAG_RX
)
415 atl2_vlan_mode(netdev
, features
);
420 static void atl2_intr_rx(struct atl2_adapter
*adapter
)
422 struct net_device
*netdev
= adapter
->netdev
;
427 rxd
= adapter
->rxd_ring
+adapter
->rxd_write_ptr
;
428 if (!rxd
->status
.update
)
429 break; /* end of tx */
431 /* clear this flag at once */
432 rxd
->status
.update
= 0;
434 if (rxd
->status
.ok
&& rxd
->status
.pkt_size
>= 60) {
435 int rx_size
= (int)(rxd
->status
.pkt_size
- 4);
436 /* alloc new buffer */
437 skb
= netdev_alloc_skb_ip_align(netdev
, rx_size
);
440 * Check that some rx space is free. If not,
441 * free one and mark stats->rx_dropped++.
443 netdev
->stats
.rx_dropped
++;
446 memcpy(skb
->data
, rxd
->packet
, rx_size
);
447 skb_put(skb
, rx_size
);
448 skb
->protocol
= eth_type_trans(skb
, netdev
);
449 if (rxd
->status
.vlan
) {
450 u16 vlan_tag
= (rxd
->status
.vtag
>>4) |
451 ((rxd
->status
.vtag
&7) << 13) |
452 ((rxd
->status
.vtag
&8) << 9);
454 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), vlan_tag
);
457 netdev
->stats
.rx_bytes
+= rx_size
;
458 netdev
->stats
.rx_packets
++;
460 netdev
->stats
.rx_errors
++;
462 if (rxd
->status
.ok
&& rxd
->status
.pkt_size
<= 60)
463 netdev
->stats
.rx_length_errors
++;
464 if (rxd
->status
.mcast
)
465 netdev
->stats
.multicast
++;
467 netdev
->stats
.rx_crc_errors
++;
468 if (rxd
->status
.align
)
469 netdev
->stats
.rx_frame_errors
++;
472 /* advance write ptr */
473 if (++adapter
->rxd_write_ptr
== adapter
->rxd_ring_size
)
474 adapter
->rxd_write_ptr
= 0;
477 /* update mailbox? */
478 adapter
->rxd_read_ptr
= adapter
->rxd_write_ptr
;
479 ATL2_WRITE_REGW(&adapter
->hw
, REG_MB_RXD_RD_IDX
, adapter
->rxd_read_ptr
);
482 static void atl2_intr_tx(struct atl2_adapter
*adapter
)
484 struct net_device
*netdev
= adapter
->netdev
;
487 struct tx_pkt_status
*txs
;
488 struct tx_pkt_header
*txph
;
492 txs_write_ptr
= (u32
) atomic_read(&adapter
->txs_write_ptr
);
493 txs
= adapter
->txs_ring
+ txs_write_ptr
;
495 break; /* tx stop here */
500 if (++txs_write_ptr
== adapter
->txs_ring_size
)
502 atomic_set(&adapter
->txs_write_ptr
, (int)txs_write_ptr
);
504 txd_read_ptr
= (u32
) atomic_read(&adapter
->txd_read_ptr
);
505 txph
= (struct tx_pkt_header
*)
506 (((u8
*)adapter
->txd_ring
) + txd_read_ptr
);
508 if (txph
->pkt_size
!= txs
->pkt_size
) {
509 struct tx_pkt_status
*old_txs
= txs
;
511 "%s: txs packet size not consistent with txd"
512 " txd_:0x%08x, txs_:0x%08x!\n",
513 adapter
->netdev
->name
,
514 *(u32
*)txph
, *(u32
*)txs
);
516 "txd read ptr: 0x%x\n",
518 txs
= adapter
->txs_ring
+ txs_write_ptr
;
520 "txs-behind:0x%08x\n",
522 if (txs_write_ptr
< 2) {
523 txs
= adapter
->txs_ring
+
524 (adapter
->txs_ring_size
+
527 txs
= adapter
->txs_ring
+ (txs_write_ptr
- 2);
530 "txs-before:0x%08x\n",
536 txd_read_ptr
+= (((u32
)(txph
->pkt_size
) + 7) & ~3);
537 if (txd_read_ptr
>= adapter
->txd_ring_size
)
538 txd_read_ptr
-= adapter
->txd_ring_size
;
540 atomic_set(&adapter
->txd_read_ptr
, (int)txd_read_ptr
);
544 netdev
->stats
.tx_bytes
+= txs
->pkt_size
;
545 netdev
->stats
.tx_packets
++;
548 netdev
->stats
.tx_errors
++;
551 netdev
->stats
.collisions
++;
553 netdev
->stats
.tx_aborted_errors
++;
555 netdev
->stats
.tx_window_errors
++;
557 netdev
->stats
.tx_fifo_errors
++;
561 if (netif_queue_stopped(adapter
->netdev
) &&
562 netif_carrier_ok(adapter
->netdev
))
563 netif_wake_queue(adapter
->netdev
);
567 static void atl2_check_for_link(struct atl2_adapter
*adapter
)
569 struct net_device
*netdev
= adapter
->netdev
;
572 spin_lock(&adapter
->stats_lock
);
573 atl2_read_phy_reg(&adapter
->hw
, MII_BMSR
, &phy_data
);
574 atl2_read_phy_reg(&adapter
->hw
, MII_BMSR
, &phy_data
);
575 spin_unlock(&adapter
->stats_lock
);
577 /* notify upper layer link down ASAP */
578 if (!(phy_data
& BMSR_LSTATUS
)) { /* Link Down */
579 if (netif_carrier_ok(netdev
)) { /* old link state: Up */
580 printk(KERN_INFO
"%s: %s NIC Link is Down\n",
581 atl2_driver_name
, netdev
->name
);
582 adapter
->link_speed
= SPEED_0
;
583 netif_carrier_off(netdev
);
584 netif_stop_queue(netdev
);
587 schedule_work(&adapter
->link_chg_task
);
590 static inline void atl2_clear_phy_int(struct atl2_adapter
*adapter
)
593 spin_lock(&adapter
->stats_lock
);
594 atl2_read_phy_reg(&adapter
->hw
, 19, &phy_data
);
595 spin_unlock(&adapter
->stats_lock
);
599 * atl2_intr - Interrupt Handler
600 * @irq: interrupt number
601 * @data: pointer to a network interface device structure
603 static irqreturn_t
atl2_intr(int irq
, void *data
)
605 struct atl2_adapter
*adapter
= netdev_priv(data
);
606 struct atl2_hw
*hw
= &adapter
->hw
;
609 status
= ATL2_READ_REG(hw
, REG_ISR
);
614 if (status
& ISR_PHY
)
615 atl2_clear_phy_int(adapter
);
617 /* clear ISR status, and Enable CMB DMA/Disable Interrupt */
618 ATL2_WRITE_REG(hw
, REG_ISR
, status
| ISR_DIS_INT
);
620 /* check if PCIE PHY Link down */
621 if (status
& ISR_PHY_LINKDOWN
) {
622 if (netif_running(adapter
->netdev
)) { /* reset MAC */
623 ATL2_WRITE_REG(hw
, REG_ISR
, 0);
624 ATL2_WRITE_REG(hw
, REG_IMR
, 0);
625 ATL2_WRITE_FLUSH(hw
);
626 schedule_work(&adapter
->reset_task
);
631 /* check if DMA read/write error? */
632 if (status
& (ISR_DMAR_TO_RST
| ISR_DMAW_TO_RST
)) {
633 ATL2_WRITE_REG(hw
, REG_ISR
, 0);
634 ATL2_WRITE_REG(hw
, REG_IMR
, 0);
635 ATL2_WRITE_FLUSH(hw
);
636 schedule_work(&adapter
->reset_task
);
641 if (status
& (ISR_PHY
| ISR_MANUAL
)) {
642 adapter
->netdev
->stats
.tx_carrier_errors
++;
643 atl2_check_for_link(adapter
);
647 if (status
& ISR_TX_EVENT
)
648 atl2_intr_tx(adapter
);
651 if (status
& ISR_RX_EVENT
)
652 atl2_intr_rx(adapter
);
654 /* re-enable Interrupt */
655 ATL2_WRITE_REG(&adapter
->hw
, REG_ISR
, 0);
659 static int atl2_request_irq(struct atl2_adapter
*adapter
)
661 struct net_device
*netdev
= adapter
->netdev
;
665 adapter
->have_msi
= true;
666 err
= pci_enable_msi(adapter
->pdev
);
668 adapter
->have_msi
= false;
670 if (adapter
->have_msi
)
671 flags
&= ~IRQF_SHARED
;
673 return request_irq(adapter
->pdev
->irq
, atl2_intr
, flags
, netdev
->name
,
678 * atl2_free_ring_resources - Free Tx / RX descriptor Resources
679 * @adapter: board private structure
681 * Free all transmit software resources
683 static void atl2_free_ring_resources(struct atl2_adapter
*adapter
)
685 struct pci_dev
*pdev
= adapter
->pdev
;
686 pci_free_consistent(pdev
, adapter
->ring_size
, adapter
->ring_vir_addr
,
691 * atl2_open - Called when a network interface is made active
692 * @netdev: network interface device structure
694 * Returns 0 on success, negative value on failure
696 * The open entry point is called when a network interface is made
697 * active by the system (IFF_UP). At this point all resources needed
698 * for transmit and receive operations are allocated, the interrupt
699 * handler is registered with the OS, the watchdog timer is started,
700 * and the stack is notified that the interface is ready.
702 static int atl2_open(struct net_device
*netdev
)
704 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
708 /* disallow open during test */
709 if (test_bit(__ATL2_TESTING
, &adapter
->flags
))
712 /* allocate transmit descriptors */
713 err
= atl2_setup_ring_resources(adapter
);
717 err
= atl2_init_hw(&adapter
->hw
);
723 /* hardware has been reset, we need to reload some things */
724 atl2_set_multi(netdev
);
725 init_ring_ptrs(adapter
);
727 atl2_restore_vlan(adapter
);
729 if (atl2_configure(adapter
)) {
734 err
= atl2_request_irq(adapter
);
738 clear_bit(__ATL2_DOWN
, &adapter
->flags
);
740 mod_timer(&adapter
->watchdog_timer
, round_jiffies(jiffies
+ 4*HZ
));
742 val
= ATL2_READ_REG(&adapter
->hw
, REG_MASTER_CTRL
);
743 ATL2_WRITE_REG(&adapter
->hw
, REG_MASTER_CTRL
,
744 val
| MASTER_CTRL_MANUAL_INT
);
746 atl2_irq_enable(adapter
);
753 atl2_free_ring_resources(adapter
);
754 atl2_reset_hw(&adapter
->hw
);
759 static void atl2_down(struct atl2_adapter
*adapter
)
761 struct net_device
*netdev
= adapter
->netdev
;
763 /* signal that we're down so the interrupt handler does not
764 * reschedule our watchdog timer */
765 set_bit(__ATL2_DOWN
, &adapter
->flags
);
767 netif_tx_disable(netdev
);
769 /* reset MAC to disable all RX/TX */
770 atl2_reset_hw(&adapter
->hw
);
773 atl2_irq_disable(adapter
);
775 del_timer_sync(&adapter
->watchdog_timer
);
776 del_timer_sync(&adapter
->phy_config_timer
);
777 clear_bit(0, &adapter
->cfg_phy
);
779 netif_carrier_off(netdev
);
780 adapter
->link_speed
= SPEED_0
;
781 adapter
->link_duplex
= -1;
784 static void atl2_free_irq(struct atl2_adapter
*adapter
)
786 struct net_device
*netdev
= adapter
->netdev
;
788 free_irq(adapter
->pdev
->irq
, netdev
);
790 #ifdef CONFIG_PCI_MSI
791 if (adapter
->have_msi
)
792 pci_disable_msi(adapter
->pdev
);
797 * atl2_close - Disables a network interface
798 * @netdev: network interface device structure
800 * Returns 0, this is not allowed to fail
802 * The close entry point is called when an interface is de-activated
803 * by the OS. The hardware is still under the drivers control, but
804 * needs to be disabled. A global MAC reset is issued to stop the
805 * hardware, and all transmit and receive resources are freed.
807 static int atl2_close(struct net_device
*netdev
)
809 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
811 WARN_ON(test_bit(__ATL2_RESETTING
, &adapter
->flags
));
814 atl2_free_irq(adapter
);
815 atl2_free_ring_resources(adapter
);
820 static inline int TxsFreeUnit(struct atl2_adapter
*adapter
)
822 u32 txs_write_ptr
= (u32
) atomic_read(&adapter
->txs_write_ptr
);
824 return (adapter
->txs_next_clear
>= txs_write_ptr
) ?
825 (int) (adapter
->txs_ring_size
- adapter
->txs_next_clear
+
827 (int) (txs_write_ptr
- adapter
->txs_next_clear
- 1);
830 static inline int TxdFreeBytes(struct atl2_adapter
*adapter
)
832 u32 txd_read_ptr
= (u32
)atomic_read(&adapter
->txd_read_ptr
);
834 return (adapter
->txd_write_ptr
>= txd_read_ptr
) ?
835 (int) (adapter
->txd_ring_size
- adapter
->txd_write_ptr
+
837 (int) (txd_read_ptr
- adapter
->txd_write_ptr
- 1);
840 static netdev_tx_t
atl2_xmit_frame(struct sk_buff
*skb
,
841 struct net_device
*netdev
)
843 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
844 struct tx_pkt_header
*txph
;
845 u32 offset
, copy_len
;
849 if (test_bit(__ATL2_DOWN
, &adapter
->flags
)) {
850 dev_kfree_skb_any(skb
);
854 if (unlikely(skb
->len
<= 0)) {
855 dev_kfree_skb_any(skb
);
859 txs_unused
= TxsFreeUnit(adapter
);
860 txbuf_unused
= TxdFreeBytes(adapter
);
862 if (skb
->len
+ sizeof(struct tx_pkt_header
) + 4 > txbuf_unused
||
864 /* not enough resources */
865 netif_stop_queue(netdev
);
866 return NETDEV_TX_BUSY
;
869 offset
= adapter
->txd_write_ptr
;
871 txph
= (struct tx_pkt_header
*) (((u8
*)adapter
->txd_ring
) + offset
);
874 txph
->pkt_size
= skb
->len
;
877 if (offset
>= adapter
->txd_ring_size
)
878 offset
-= adapter
->txd_ring_size
;
879 copy_len
= adapter
->txd_ring_size
- offset
;
880 if (copy_len
>= skb
->len
) {
881 memcpy(((u8
*)adapter
->txd_ring
) + offset
, skb
->data
, skb
->len
);
882 offset
+= ((u32
)(skb
->len
+ 3) & ~3);
884 memcpy(((u8
*)adapter
->txd_ring
)+offset
, skb
->data
, copy_len
);
885 memcpy((u8
*)adapter
->txd_ring
, skb
->data
+copy_len
,
887 offset
= ((u32
)(skb
->len
-copy_len
+ 3) & ~3);
889 #ifdef NETIF_F_HW_VLAN_CTAG_TX
890 if (skb_vlan_tag_present(skb
)) {
891 u16 vlan_tag
= skb_vlan_tag_get(skb
);
892 vlan_tag
= (vlan_tag
<< 4) |
894 ((vlan_tag
>> 9) & 0x8);
896 txph
->vlan
= vlan_tag
;
899 if (offset
>= adapter
->txd_ring_size
)
900 offset
-= adapter
->txd_ring_size
;
901 adapter
->txd_write_ptr
= offset
;
903 /* clear txs before send */
904 adapter
->txs_ring
[adapter
->txs_next_clear
].update
= 0;
905 if (++adapter
->txs_next_clear
== adapter
->txs_ring_size
)
906 adapter
->txs_next_clear
= 0;
908 ATL2_WRITE_REGW(&adapter
->hw
, REG_MB_TXD_WR_IDX
,
909 (adapter
->txd_write_ptr
>> 2));
912 dev_kfree_skb_any(skb
);
917 * atl2_change_mtu - Change the Maximum Transfer Unit
918 * @netdev: network interface device structure
919 * @new_mtu: new value for maximum frame size
921 * Returns 0 on success, negative on failure
923 static int atl2_change_mtu(struct net_device
*netdev
, int new_mtu
)
925 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
926 struct atl2_hw
*hw
= &adapter
->hw
;
928 if ((new_mtu
< 40) || (new_mtu
> (ETH_DATA_LEN
+ VLAN_SIZE
)))
932 if (hw
->max_frame_size
!= new_mtu
) {
933 netdev
->mtu
= new_mtu
;
934 ATL2_WRITE_REG(hw
, REG_MTU
, new_mtu
+ ENET_HEADER_SIZE
+
935 VLAN_SIZE
+ ETHERNET_FCS_SIZE
);
942 * atl2_set_mac - Change the Ethernet Address of the NIC
943 * @netdev: network interface device structure
944 * @p: pointer to an address structure
946 * Returns 0 on success, negative on failure
948 static int atl2_set_mac(struct net_device
*netdev
, void *p
)
950 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
951 struct sockaddr
*addr
= p
;
953 if (!is_valid_ether_addr(addr
->sa_data
))
954 return -EADDRNOTAVAIL
;
956 if (netif_running(netdev
))
959 memcpy(netdev
->dev_addr
, addr
->sa_data
, netdev
->addr_len
);
960 memcpy(adapter
->hw
.mac_addr
, addr
->sa_data
, netdev
->addr_len
);
962 atl2_set_mac_addr(&adapter
->hw
);
967 static int atl2_mii_ioctl(struct net_device
*netdev
, struct ifreq
*ifr
, int cmd
)
969 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
970 struct mii_ioctl_data
*data
= if_mii(ifr
);
978 spin_lock_irqsave(&adapter
->stats_lock
, flags
);
979 if (atl2_read_phy_reg(&adapter
->hw
,
980 data
->reg_num
& 0x1F, &data
->val_out
)) {
981 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
984 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
987 if (data
->reg_num
& ~(0x1F))
989 spin_lock_irqsave(&adapter
->stats_lock
, flags
);
990 if (atl2_write_phy_reg(&adapter
->hw
, data
->reg_num
,
992 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
995 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
1003 static int atl2_ioctl(struct net_device
*netdev
, struct ifreq
*ifr
, int cmd
)
1009 return atl2_mii_ioctl(netdev
, ifr
, cmd
);
1010 #ifdef ETHTOOL_OPS_COMPAT
1012 return ethtool_ioctl(ifr
);
1020 * atl2_tx_timeout - Respond to a Tx Hang
1021 * @netdev: network interface device structure
1023 static void atl2_tx_timeout(struct net_device
*netdev
)
1025 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1027 /* Do the reset outside of interrupt context */
1028 schedule_work(&adapter
->reset_task
);
1032 * atl2_watchdog - Timer Call-back
1033 * @data: pointer to netdev cast into an unsigned long
1035 static void atl2_watchdog(unsigned long data
)
1037 struct atl2_adapter
*adapter
= (struct atl2_adapter
*) data
;
1039 if (!test_bit(__ATL2_DOWN
, &adapter
->flags
)) {
1040 u32 drop_rxd
, drop_rxs
;
1041 unsigned long flags
;
1043 spin_lock_irqsave(&adapter
->stats_lock
, flags
);
1044 drop_rxd
= ATL2_READ_REG(&adapter
->hw
, REG_STS_RXD_OV
);
1045 drop_rxs
= ATL2_READ_REG(&adapter
->hw
, REG_STS_RXS_OV
);
1046 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
1048 adapter
->netdev
->stats
.rx_over_errors
+= drop_rxd
+ drop_rxs
;
1050 /* Reset the timer */
1051 mod_timer(&adapter
->watchdog_timer
,
1052 round_jiffies(jiffies
+ 4 * HZ
));
1057 * atl2_phy_config - Timer Call-back
1058 * @data: pointer to netdev cast into an unsigned long
1060 static void atl2_phy_config(unsigned long data
)
1062 struct atl2_adapter
*adapter
= (struct atl2_adapter
*) data
;
1063 struct atl2_hw
*hw
= &adapter
->hw
;
1064 unsigned long flags
;
1066 spin_lock_irqsave(&adapter
->stats_lock
, flags
);
1067 atl2_write_phy_reg(hw
, MII_ADVERTISE
, hw
->mii_autoneg_adv_reg
);
1068 atl2_write_phy_reg(hw
, MII_BMCR
, MII_CR_RESET
| MII_CR_AUTO_NEG_EN
|
1069 MII_CR_RESTART_AUTO_NEG
);
1070 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
1071 clear_bit(0, &adapter
->cfg_phy
);
1074 static int atl2_up(struct atl2_adapter
*adapter
)
1076 struct net_device
*netdev
= adapter
->netdev
;
1080 /* hardware has been reset, we need to reload some things */
1082 err
= atl2_init_hw(&adapter
->hw
);
1088 atl2_set_multi(netdev
);
1089 init_ring_ptrs(adapter
);
1091 atl2_restore_vlan(adapter
);
1093 if (atl2_configure(adapter
)) {
1098 clear_bit(__ATL2_DOWN
, &adapter
->flags
);
1100 val
= ATL2_READ_REG(&adapter
->hw
, REG_MASTER_CTRL
);
1101 ATL2_WRITE_REG(&adapter
->hw
, REG_MASTER_CTRL
, val
|
1102 MASTER_CTRL_MANUAL_INT
);
1104 atl2_irq_enable(adapter
);
1110 static void atl2_reinit_locked(struct atl2_adapter
*adapter
)
1112 WARN_ON(in_interrupt());
1113 while (test_and_set_bit(__ATL2_RESETTING
, &adapter
->flags
))
1117 clear_bit(__ATL2_RESETTING
, &adapter
->flags
);
1120 static void atl2_reset_task(struct work_struct
*work
)
1122 struct atl2_adapter
*adapter
;
1123 adapter
= container_of(work
, struct atl2_adapter
, reset_task
);
1125 atl2_reinit_locked(adapter
);
1128 static void atl2_setup_mac_ctrl(struct atl2_adapter
*adapter
)
1131 struct atl2_hw
*hw
= &adapter
->hw
;
1132 struct net_device
*netdev
= adapter
->netdev
;
1134 /* Config MAC CTRL Register */
1135 value
= MAC_CTRL_TX_EN
| MAC_CTRL_RX_EN
| MAC_CTRL_MACLP_CLK_PHY
;
1138 if (FULL_DUPLEX
== adapter
->link_duplex
)
1139 value
|= MAC_CTRL_DUPLX
;
1142 value
|= (MAC_CTRL_TX_FLOW
| MAC_CTRL_RX_FLOW
);
1145 value
|= (MAC_CTRL_ADD_CRC
| MAC_CTRL_PAD
);
1147 /* preamble length */
1148 value
|= (((u32
)adapter
->hw
.preamble_len
& MAC_CTRL_PRMLEN_MASK
) <<
1149 MAC_CTRL_PRMLEN_SHIFT
);
1152 __atl2_vlan_mode(netdev
->features
, &value
);
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
;
1173 u16 speed
, duplex
, phy_data
;
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 */
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
);
1194 ret_val
= atl2_get_speed_and_duplex(hw
, &speed
, &duplex
);
1197 switch (hw
->MediaType
) {
1198 case MEDIA_TYPE_100M_FULL
:
1199 if (speed
!= SPEED_100
|| duplex
!= FULL_DUPLEX
)
1202 case MEDIA_TYPE_100M_HALF
:
1203 if (speed
!= SPEED_100
|| duplex
!= HALF_DUPLEX
)
1206 case MEDIA_TYPE_10M_FULL
:
1207 if (speed
!= SPEED_10
|| duplex
!= FULL_DUPLEX
)
1210 case MEDIA_TYPE_10M_HALF
:
1211 if (speed
!= SPEED_10
|| duplex
!= HALF_DUPLEX
)
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
);
1236 /* change original link status */
1237 if (netif_carrier_ok(netdev
)) {
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
));
1261 * atl2_link_chg_task - deal with link change event Out of interrupt context
1263 static void atl2_link_chg_task(struct work_struct
*work
)
1265 struct atl2_adapter
*adapter
;
1266 unsigned long flags
;
1268 adapter
= container_of(work
, struct atl2_adapter
, link_chg_task
);
1270 spin_lock_irqsave(&adapter
->stats_lock
, flags
);
1271 atl2_check_link(adapter
);
1272 spin_unlock_irqrestore(&adapter
->stats_lock
, flags
);
1275 static void atl2_setup_pcicmd(struct pci_dev
*pdev
)
1279 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
1281 if (cmd
& PCI_COMMAND_INTX_DISABLE
)
1282 cmd
&= ~PCI_COMMAND_INTX_DISABLE
;
1283 if (cmd
& PCI_COMMAND_IO
)
1284 cmd
&= ~PCI_COMMAND_IO
;
1285 if (0 == (cmd
& PCI_COMMAND_MEMORY
))
1286 cmd
|= PCI_COMMAND_MEMORY
;
1287 if (0 == (cmd
& PCI_COMMAND_MASTER
))
1288 cmd
|= PCI_COMMAND_MASTER
;
1289 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
);
1292 * some motherboards BIOS(PXE/EFI) driver may set PME
1293 * while they transfer control to OS (Windows/Linux)
1294 * so we should clear this bit before NIC work normally
1296 pci_write_config_dword(pdev
, REG_PM_CTRLSTAT
, 0);
1299 #ifdef CONFIG_NET_POLL_CONTROLLER
1300 static void atl2_poll_controller(struct net_device
*netdev
)
1302 disable_irq(netdev
->irq
);
1303 atl2_intr(netdev
->irq
, netdev
);
1304 enable_irq(netdev
->irq
);
1309 static const struct net_device_ops atl2_netdev_ops
= {
1310 .ndo_open
= atl2_open
,
1311 .ndo_stop
= atl2_close
,
1312 .ndo_start_xmit
= atl2_xmit_frame
,
1313 .ndo_set_rx_mode
= atl2_set_multi
,
1314 .ndo_validate_addr
= eth_validate_addr
,
1315 .ndo_set_mac_address
= atl2_set_mac
,
1316 .ndo_change_mtu
= atl2_change_mtu
,
1317 .ndo_fix_features
= atl2_fix_features
,
1318 .ndo_set_features
= atl2_set_features
,
1319 .ndo_do_ioctl
= atl2_ioctl
,
1320 .ndo_tx_timeout
= atl2_tx_timeout
,
1321 #ifdef CONFIG_NET_POLL_CONTROLLER
1322 .ndo_poll_controller
= atl2_poll_controller
,
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 atl2_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1339 struct net_device
*netdev
;
1340 struct atl2_adapter
*adapter
;
1341 static int cards_found
;
1342 unsigned long mmio_start
;
1348 err
= pci_enable_device(pdev
);
1353 * atl2 is a shared-high-32-bit device, so we're stuck with 32-bit DMA
1354 * until the kernel has the proper infrastructure to support 64-bit DMA
1357 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) &&
1358 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32))) {
1359 printk(KERN_ERR
"atl2: No usable DMA configuration, aborting\n");
1363 /* Mark all PCI regions associated with PCI device
1364 * pdev as being reserved by owner atl2_driver_name */
1365 err
= pci_request_regions(pdev
, atl2_driver_name
);
1369 /* Enables bus-mastering on the device and calls
1370 * pcibios_set_master to do the needed arch specific settings */
1371 pci_set_master(pdev
);
1374 netdev
= alloc_etherdev(sizeof(struct atl2_adapter
));
1376 goto err_alloc_etherdev
;
1378 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
1380 pci_set_drvdata(pdev
, netdev
);
1381 adapter
= netdev_priv(netdev
);
1382 adapter
->netdev
= netdev
;
1383 adapter
->pdev
= pdev
;
1384 adapter
->hw
.back
= adapter
;
1386 mmio_start
= pci_resource_start(pdev
, 0x0);
1387 mmio_len
= pci_resource_len(pdev
, 0x0);
1389 adapter
->hw
.mem_rang
= (u32
)mmio_len
;
1390 adapter
->hw
.hw_addr
= ioremap(mmio_start
, mmio_len
);
1391 if (!adapter
->hw
.hw_addr
) {
1396 atl2_setup_pcicmd(pdev
);
1398 netdev
->netdev_ops
= &atl2_netdev_ops
;
1399 netdev
->ethtool_ops
= &atl2_ethtool_ops
;
1400 netdev
->watchdog_timeo
= 5 * HZ
;
1401 strncpy(netdev
->name
, pci_name(pdev
), sizeof(netdev
->name
) - 1);
1403 netdev
->mem_start
= mmio_start
;
1404 netdev
->mem_end
= mmio_start
+ mmio_len
;
1405 adapter
->bd_number
= cards_found
;
1406 adapter
->pci_using_64
= false;
1408 /* setup the private structure */
1409 err
= atl2_sw_init(adapter
);
1415 netdev
->hw_features
= NETIF_F_HW_VLAN_CTAG_RX
;
1416 netdev
->features
|= (NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
);
1418 /* Init PHY as early as possible due to power saving issue */
1419 atl2_phy_init(&adapter
->hw
);
1421 /* reset the controller to
1422 * put the device in a known good starting state */
1424 if (atl2_reset_hw(&adapter
->hw
)) {
1429 /* copy the MAC address out of the EEPROM */
1430 atl2_read_mac_addr(&adapter
->hw
);
1431 memcpy(netdev
->dev_addr
, adapter
->hw
.mac_addr
, netdev
->addr_len
);
1432 if (!is_valid_ether_addr(netdev
->dev_addr
)) {
1437 atl2_check_options(adapter
);
1439 setup_timer(&adapter
->watchdog_timer
, atl2_watchdog
,
1440 (unsigned long)adapter
);
1442 setup_timer(&adapter
->phy_config_timer
, atl2_phy_config
,
1443 (unsigned long)adapter
);
1445 INIT_WORK(&adapter
->reset_task
, atl2_reset_task
);
1446 INIT_WORK(&adapter
->link_chg_task
, atl2_link_chg_task
);
1448 strcpy(netdev
->name
, "eth%d"); /* ?? */
1449 err
= register_netdev(netdev
);
1453 /* assume we have no link for now */
1454 netif_carrier_off(netdev
);
1455 netif_stop_queue(netdev
);
1465 iounmap(adapter
->hw
.hw_addr
);
1467 free_netdev(netdev
);
1469 pci_release_regions(pdev
);
1472 pci_disable_device(pdev
);
1477 * atl2_remove - Device Removal Routine
1478 * @pdev: PCI device information struct
1480 * atl2_remove is called by the PCI subsystem to alert the driver
1481 * that it should release a PCI device. The could be caused by a
1482 * Hot-Plug event, or because the driver is going to be removed from
1485 /* FIXME: write the original MAC address back in case it was changed from a
1486 * BIOS-set value, as in atl1 -- CHS */
1487 static void atl2_remove(struct pci_dev
*pdev
)
1489 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1490 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1492 /* flush_scheduled work may reschedule our watchdog task, so
1493 * explicitly disable watchdog tasks from being rescheduled */
1494 set_bit(__ATL2_DOWN
, &adapter
->flags
);
1496 del_timer_sync(&adapter
->watchdog_timer
);
1497 del_timer_sync(&adapter
->phy_config_timer
);
1498 cancel_work_sync(&adapter
->reset_task
);
1499 cancel_work_sync(&adapter
->link_chg_task
);
1501 unregister_netdev(netdev
);
1503 atl2_force_ps(&adapter
->hw
);
1505 iounmap(adapter
->hw
.hw_addr
);
1506 pci_release_regions(pdev
);
1508 free_netdev(netdev
);
1510 pci_disable_device(pdev
);
1513 static int atl2_suspend(struct pci_dev
*pdev
, pm_message_t state
)
1515 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1516 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1517 struct atl2_hw
*hw
= &adapter
->hw
;
1520 u32 wufc
= adapter
->wol
;
1526 netif_device_detach(netdev
);
1528 if (netif_running(netdev
)) {
1529 WARN_ON(test_bit(__ATL2_RESETTING
, &adapter
->flags
));
1534 retval
= pci_save_state(pdev
);
1539 atl2_read_phy_reg(hw
, MII_BMSR
, (u16
*)&ctrl
);
1540 atl2_read_phy_reg(hw
, MII_BMSR
, (u16
*)&ctrl
);
1541 if (ctrl
& BMSR_LSTATUS
)
1542 wufc
&= ~ATLX_WUFC_LNKC
;
1544 if (0 != (ctrl
& BMSR_LSTATUS
) && 0 != wufc
) {
1546 /* get current link speed & duplex */
1547 ret_val
= atl2_get_speed_and_duplex(hw
, &speed
, &duplex
);
1550 "%s: get speed&duplex error while suspend\n",
1557 /* turn on magic packet wol */
1558 if (wufc
& ATLX_WUFC_MAG
)
1559 ctrl
|= (WOL_MAGIC_EN
| WOL_MAGIC_PME_EN
);
1561 /* ignore Link Chg event when Link is up */
1562 ATL2_WRITE_REG(hw
, REG_WOL_CTRL
, ctrl
);
1564 /* Config MAC CTRL Register */
1565 ctrl
= MAC_CTRL_RX_EN
| MAC_CTRL_MACLP_CLK_PHY
;
1566 if (FULL_DUPLEX
== adapter
->link_duplex
)
1567 ctrl
|= MAC_CTRL_DUPLX
;
1568 ctrl
|= (MAC_CTRL_ADD_CRC
| MAC_CTRL_PAD
);
1569 ctrl
|= (((u32
)adapter
->hw
.preamble_len
&
1570 MAC_CTRL_PRMLEN_MASK
) << MAC_CTRL_PRMLEN_SHIFT
);
1571 ctrl
|= (((u32
)(adapter
->hw
.retry_buf
&
1572 MAC_CTRL_HALF_LEFT_BUF_MASK
)) <<
1573 MAC_CTRL_HALF_LEFT_BUF_SHIFT
);
1574 if (wufc
& ATLX_WUFC_MAG
) {
1575 /* magic packet maybe Broadcast&multicast&Unicast */
1576 ctrl
|= MAC_CTRL_BC_EN
;
1579 ATL2_WRITE_REG(hw
, REG_MAC_CTRL
, ctrl
);
1582 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_PHYMISC
);
1583 ctrl
|= PCIE_PHYMISC_FORCE_RCV_DET
;
1584 ATL2_WRITE_REG(hw
, REG_PCIE_PHYMISC
, ctrl
);
1585 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_DLL_TX_CTRL1
);
1586 ctrl
|= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK
;
1587 ATL2_WRITE_REG(hw
, REG_PCIE_DLL_TX_CTRL1
, ctrl
);
1589 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 1);
1593 if (0 == (ctrl
&BMSR_LSTATUS
) && 0 != (wufc
&ATLX_WUFC_LNKC
)) {
1594 /* link is down, so only LINK CHG WOL event enable */
1595 ctrl
|= (WOL_LINK_CHG_EN
| WOL_LINK_CHG_PME_EN
);
1596 ATL2_WRITE_REG(hw
, REG_WOL_CTRL
, ctrl
);
1597 ATL2_WRITE_REG(hw
, REG_MAC_CTRL
, 0);
1600 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_PHYMISC
);
1601 ctrl
|= PCIE_PHYMISC_FORCE_RCV_DET
;
1602 ATL2_WRITE_REG(hw
, REG_PCIE_PHYMISC
, ctrl
);
1603 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_DLL_TX_CTRL1
);
1604 ctrl
|= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK
;
1605 ATL2_WRITE_REG(hw
, REG_PCIE_DLL_TX_CTRL1
, ctrl
);
1607 hw
->phy_configured
= false; /* re-init PHY when resume */
1609 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 1);
1616 ATL2_WRITE_REG(hw
, REG_WOL_CTRL
, 0);
1619 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_PHYMISC
);
1620 ctrl
|= PCIE_PHYMISC_FORCE_RCV_DET
;
1621 ATL2_WRITE_REG(hw
, REG_PCIE_PHYMISC
, ctrl
);
1622 ctrl
= ATL2_READ_REG(hw
, REG_PCIE_DLL_TX_CTRL1
);
1623 ctrl
|= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK
;
1624 ATL2_WRITE_REG(hw
, REG_PCIE_DLL_TX_CTRL1
, ctrl
);
1627 hw
->phy_configured
= false; /* re-init PHY when resume */
1629 pci_enable_wake(pdev
, pci_choose_state(pdev
, state
), 0);
1632 if (netif_running(netdev
))
1633 atl2_free_irq(adapter
);
1635 pci_disable_device(pdev
);
1637 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
1643 static int atl2_resume(struct pci_dev
*pdev
)
1645 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1646 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1649 pci_set_power_state(pdev
, PCI_D0
);
1650 pci_restore_state(pdev
);
1652 err
= pci_enable_device(pdev
);
1655 "atl2: Cannot enable PCI device from suspend\n");
1659 pci_set_master(pdev
);
1661 ATL2_READ_REG(&adapter
->hw
, REG_WOL_CTRL
); /* clear WOL status */
1663 pci_enable_wake(pdev
, PCI_D3hot
, 0);
1664 pci_enable_wake(pdev
, PCI_D3cold
, 0);
1666 ATL2_WRITE_REG(&adapter
->hw
, REG_WOL_CTRL
, 0);
1668 if (netif_running(netdev
)) {
1669 err
= atl2_request_irq(adapter
);
1674 atl2_reset_hw(&adapter
->hw
);
1676 if (netif_running(netdev
))
1679 netif_device_attach(netdev
);
1685 static void atl2_shutdown(struct pci_dev
*pdev
)
1687 atl2_suspend(pdev
, PMSG_SUSPEND
);
1690 static struct pci_driver atl2_driver
= {
1691 .name
= atl2_driver_name
,
1692 .id_table
= atl2_pci_tbl
,
1693 .probe
= atl2_probe
,
1694 .remove
= atl2_remove
,
1695 /* Power Management Hooks */
1696 .suspend
= atl2_suspend
,
1698 .resume
= atl2_resume
,
1700 .shutdown
= atl2_shutdown
,
1704 * atl2_init_module - Driver Registration Routine
1706 * atl2_init_module is the first routine called when the driver is
1707 * loaded. All it does is register with the PCI subsystem.
1709 static int __init
atl2_init_module(void)
1711 printk(KERN_INFO
"%s - version %s\n", atl2_driver_string
,
1712 atl2_driver_version
);
1713 printk(KERN_INFO
"%s\n", atl2_copyright
);
1714 return pci_register_driver(&atl2_driver
);
1716 module_init(atl2_init_module
);
1719 * atl2_exit_module - Driver Exit Cleanup Routine
1721 * atl2_exit_module is called just before the driver is removed
1724 static void __exit
atl2_exit_module(void)
1726 pci_unregister_driver(&atl2_driver
);
1728 module_exit(atl2_exit_module
);
1730 static void atl2_read_pci_cfg(struct atl2_hw
*hw
, u32 reg
, u16
*value
)
1732 struct atl2_adapter
*adapter
= hw
->back
;
1733 pci_read_config_word(adapter
->pdev
, reg
, value
);
1736 static void atl2_write_pci_cfg(struct atl2_hw
*hw
, u32 reg
, u16
*value
)
1738 struct atl2_adapter
*adapter
= hw
->back
;
1739 pci_write_config_word(adapter
->pdev
, reg
, *value
);
1742 static int atl2_get_settings(struct net_device
*netdev
,
1743 struct ethtool_cmd
*ecmd
)
1745 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1746 struct atl2_hw
*hw
= &adapter
->hw
;
1748 ecmd
->supported
= (SUPPORTED_10baseT_Half
|
1749 SUPPORTED_10baseT_Full
|
1750 SUPPORTED_100baseT_Half
|
1751 SUPPORTED_100baseT_Full
|
1754 ecmd
->advertising
= ADVERTISED_TP
;
1756 ecmd
->advertising
|= ADVERTISED_Autoneg
;
1757 ecmd
->advertising
|= hw
->autoneg_advertised
;
1759 ecmd
->port
= PORT_TP
;
1760 ecmd
->phy_address
= 0;
1761 ecmd
->transceiver
= XCVR_INTERNAL
;
1763 if (adapter
->link_speed
!= SPEED_0
) {
1764 ethtool_cmd_speed_set(ecmd
, adapter
->link_speed
);
1765 if (adapter
->link_duplex
== FULL_DUPLEX
)
1766 ecmd
->duplex
= DUPLEX_FULL
;
1768 ecmd
->duplex
= DUPLEX_HALF
;
1770 ethtool_cmd_speed_set(ecmd
, SPEED_UNKNOWN
);
1771 ecmd
->duplex
= DUPLEX_UNKNOWN
;
1774 ecmd
->autoneg
= AUTONEG_ENABLE
;
1778 static int atl2_set_settings(struct net_device
*netdev
,
1779 struct ethtool_cmd
*ecmd
)
1781 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1782 struct atl2_hw
*hw
= &adapter
->hw
;
1784 while (test_and_set_bit(__ATL2_RESETTING
, &adapter
->flags
))
1787 if (ecmd
->autoneg
== AUTONEG_ENABLE
) {
1788 #define MY_ADV_MASK (ADVERTISE_10_HALF | \
1789 ADVERTISE_10_FULL | \
1790 ADVERTISE_100_HALF| \
1793 if ((ecmd
->advertising
& MY_ADV_MASK
) == MY_ADV_MASK
) {
1794 hw
->MediaType
= MEDIA_TYPE_AUTO_SENSOR
;
1795 hw
->autoneg_advertised
= MY_ADV_MASK
;
1796 } else if ((ecmd
->advertising
& MY_ADV_MASK
) ==
1797 ADVERTISE_100_FULL
) {
1798 hw
->MediaType
= MEDIA_TYPE_100M_FULL
;
1799 hw
->autoneg_advertised
= ADVERTISE_100_FULL
;
1800 } else if ((ecmd
->advertising
& MY_ADV_MASK
) ==
1801 ADVERTISE_100_HALF
) {
1802 hw
->MediaType
= MEDIA_TYPE_100M_HALF
;
1803 hw
->autoneg_advertised
= ADVERTISE_100_HALF
;
1804 } else if ((ecmd
->advertising
& MY_ADV_MASK
) ==
1805 ADVERTISE_10_FULL
) {
1806 hw
->MediaType
= MEDIA_TYPE_10M_FULL
;
1807 hw
->autoneg_advertised
= ADVERTISE_10_FULL
;
1808 } else if ((ecmd
->advertising
& MY_ADV_MASK
) ==
1809 ADVERTISE_10_HALF
) {
1810 hw
->MediaType
= MEDIA_TYPE_10M_HALF
;
1811 hw
->autoneg_advertised
= ADVERTISE_10_HALF
;
1813 clear_bit(__ATL2_RESETTING
, &adapter
->flags
);
1816 ecmd
->advertising
= hw
->autoneg_advertised
|
1817 ADVERTISED_TP
| ADVERTISED_Autoneg
;
1819 clear_bit(__ATL2_RESETTING
, &adapter
->flags
);
1823 /* reset the link */
1824 if (netif_running(adapter
->netdev
)) {
1828 atl2_reset_hw(&adapter
->hw
);
1830 clear_bit(__ATL2_RESETTING
, &adapter
->flags
);
1834 static u32
atl2_get_msglevel(struct net_device
*netdev
)
1840 * It's sane for this to be empty, but we might want to take advantage of this.
1842 static void atl2_set_msglevel(struct net_device
*netdev
, u32 data
)
1846 static int atl2_get_regs_len(struct net_device
*netdev
)
1848 #define ATL2_REGS_LEN 42
1849 return sizeof(u32
) * ATL2_REGS_LEN
;
1852 static void atl2_get_regs(struct net_device
*netdev
,
1853 struct ethtool_regs
*regs
, void *p
)
1855 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1856 struct atl2_hw
*hw
= &adapter
->hw
;
1860 memset(p
, 0, sizeof(u32
) * ATL2_REGS_LEN
);
1862 regs
->version
= (1 << 24) | (hw
->revision_id
<< 16) | hw
->device_id
;
1864 regs_buff
[0] = ATL2_READ_REG(hw
, REG_VPD_CAP
);
1865 regs_buff
[1] = ATL2_READ_REG(hw
, REG_SPI_FLASH_CTRL
);
1866 regs_buff
[2] = ATL2_READ_REG(hw
, REG_SPI_FLASH_CONFIG
);
1867 regs_buff
[3] = ATL2_READ_REG(hw
, REG_TWSI_CTRL
);
1868 regs_buff
[4] = ATL2_READ_REG(hw
, REG_PCIE_DEV_MISC_CTRL
);
1869 regs_buff
[5] = ATL2_READ_REG(hw
, REG_MASTER_CTRL
);
1870 regs_buff
[6] = ATL2_READ_REG(hw
, REG_MANUAL_TIMER_INIT
);
1871 regs_buff
[7] = ATL2_READ_REG(hw
, REG_IRQ_MODU_TIMER_INIT
);
1872 regs_buff
[8] = ATL2_READ_REG(hw
, REG_PHY_ENABLE
);
1873 regs_buff
[9] = ATL2_READ_REG(hw
, REG_CMBDISDMA_TIMER
);
1874 regs_buff
[10] = ATL2_READ_REG(hw
, REG_IDLE_STATUS
);
1875 regs_buff
[11] = ATL2_READ_REG(hw
, REG_MDIO_CTRL
);
1876 regs_buff
[12] = ATL2_READ_REG(hw
, REG_SERDES_LOCK
);
1877 regs_buff
[13] = ATL2_READ_REG(hw
, REG_MAC_CTRL
);
1878 regs_buff
[14] = ATL2_READ_REG(hw
, REG_MAC_IPG_IFG
);
1879 regs_buff
[15] = ATL2_READ_REG(hw
, REG_MAC_STA_ADDR
);
1880 regs_buff
[16] = ATL2_READ_REG(hw
, REG_MAC_STA_ADDR
+4);
1881 regs_buff
[17] = ATL2_READ_REG(hw
, REG_RX_HASH_TABLE
);
1882 regs_buff
[18] = ATL2_READ_REG(hw
, REG_RX_HASH_TABLE
+4);
1883 regs_buff
[19] = ATL2_READ_REG(hw
, REG_MAC_HALF_DUPLX_CTRL
);
1884 regs_buff
[20] = ATL2_READ_REG(hw
, REG_MTU
);
1885 regs_buff
[21] = ATL2_READ_REG(hw
, REG_WOL_CTRL
);
1886 regs_buff
[22] = ATL2_READ_REG(hw
, REG_SRAM_TXRAM_END
);
1887 regs_buff
[23] = ATL2_READ_REG(hw
, REG_DESC_BASE_ADDR_HI
);
1888 regs_buff
[24] = ATL2_READ_REG(hw
, REG_TXD_BASE_ADDR_LO
);
1889 regs_buff
[25] = ATL2_READ_REG(hw
, REG_TXD_MEM_SIZE
);
1890 regs_buff
[26] = ATL2_READ_REG(hw
, REG_TXS_BASE_ADDR_LO
);
1891 regs_buff
[27] = ATL2_READ_REG(hw
, REG_TXS_MEM_SIZE
);
1892 regs_buff
[28] = ATL2_READ_REG(hw
, REG_RXD_BASE_ADDR_LO
);
1893 regs_buff
[29] = ATL2_READ_REG(hw
, REG_RXD_BUF_NUM
);
1894 regs_buff
[30] = ATL2_READ_REG(hw
, REG_DMAR
);
1895 regs_buff
[31] = ATL2_READ_REG(hw
, REG_TX_CUT_THRESH
);
1896 regs_buff
[32] = ATL2_READ_REG(hw
, REG_DMAW
);
1897 regs_buff
[33] = ATL2_READ_REG(hw
, REG_PAUSE_ON_TH
);
1898 regs_buff
[34] = ATL2_READ_REG(hw
, REG_PAUSE_OFF_TH
);
1899 regs_buff
[35] = ATL2_READ_REG(hw
, REG_MB_TXD_WR_IDX
);
1900 regs_buff
[36] = ATL2_READ_REG(hw
, REG_MB_RXD_RD_IDX
);
1901 regs_buff
[38] = ATL2_READ_REG(hw
, REG_ISR
);
1902 regs_buff
[39] = ATL2_READ_REG(hw
, REG_IMR
);
1904 atl2_read_phy_reg(hw
, MII_BMCR
, &phy_data
);
1905 regs_buff
[40] = (u32
)phy_data
;
1906 atl2_read_phy_reg(hw
, MII_BMSR
, &phy_data
);
1907 regs_buff
[41] = (u32
)phy_data
;
1910 static int atl2_get_eeprom_len(struct net_device
*netdev
)
1912 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1914 if (!atl2_check_eeprom_exist(&adapter
->hw
))
1920 static int atl2_get_eeprom(struct net_device
*netdev
,
1921 struct ethtool_eeprom
*eeprom
, u8
*bytes
)
1923 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1924 struct atl2_hw
*hw
= &adapter
->hw
;
1926 int first_dword
, last_dword
;
1930 if (eeprom
->len
== 0)
1933 if (atl2_check_eeprom_exist(hw
))
1936 eeprom
->magic
= hw
->vendor_id
| (hw
->device_id
<< 16);
1938 first_dword
= eeprom
->offset
>> 2;
1939 last_dword
= (eeprom
->offset
+ eeprom
->len
- 1) >> 2;
1941 eeprom_buff
= kmalloc(sizeof(u32
) * (last_dword
- first_dword
+ 1),
1946 for (i
= first_dword
; i
< last_dword
; i
++) {
1947 if (!atl2_read_eeprom(hw
, i
*4, &(eeprom_buff
[i
-first_dword
]))) {
1953 memcpy(bytes
, (u8
*)eeprom_buff
+ (eeprom
->offset
& 3),
1961 static int atl2_set_eeprom(struct net_device
*netdev
,
1962 struct ethtool_eeprom
*eeprom
, u8
*bytes
)
1964 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
1965 struct atl2_hw
*hw
= &adapter
->hw
;
1968 int max_len
, first_dword
, last_dword
, ret_val
= 0;
1971 if (eeprom
->len
== 0)
1974 if (eeprom
->magic
!= (hw
->vendor_id
| (hw
->device_id
<< 16)))
1979 first_dword
= eeprom
->offset
>> 2;
1980 last_dword
= (eeprom
->offset
+ eeprom
->len
- 1) >> 2;
1981 eeprom_buff
= kmalloc(max_len
, GFP_KERNEL
);
1987 if (eeprom
->offset
& 3) {
1988 /* need read/modify/write of first changed EEPROM word */
1989 /* only the second byte of the word is being modified */
1990 if (!atl2_read_eeprom(hw
, first_dword
*4, &(eeprom_buff
[0]))) {
1996 if (((eeprom
->offset
+ eeprom
->len
) & 3)) {
1998 * need read/modify/write of last changed EEPROM word
1999 * only the first byte of the word is being modified
2001 if (!atl2_read_eeprom(hw
, last_dword
* 4,
2002 &(eeprom_buff
[last_dword
- first_dword
]))) {
2008 /* Device's eeprom is always little-endian, word addressable */
2009 memcpy(ptr
, bytes
, eeprom
->len
);
2011 for (i
= 0; i
< last_dword
- first_dword
+ 1; i
++) {
2012 if (!atl2_write_eeprom(hw
, ((first_dword
+i
)*4), eeprom_buff
[i
])) {
2022 static void atl2_get_drvinfo(struct net_device
*netdev
,
2023 struct ethtool_drvinfo
*drvinfo
)
2025 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
2027 strlcpy(drvinfo
->driver
, atl2_driver_name
, sizeof(drvinfo
->driver
));
2028 strlcpy(drvinfo
->version
, atl2_driver_version
,
2029 sizeof(drvinfo
->version
));
2030 strlcpy(drvinfo
->fw_version
, "L2", sizeof(drvinfo
->fw_version
));
2031 strlcpy(drvinfo
->bus_info
, pci_name(adapter
->pdev
),
2032 sizeof(drvinfo
->bus_info
));
2035 static void atl2_get_wol(struct net_device
*netdev
,
2036 struct ethtool_wolinfo
*wol
)
2038 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
2040 wol
->supported
= WAKE_MAGIC
;
2043 if (adapter
->wol
& ATLX_WUFC_EX
)
2044 wol
->wolopts
|= WAKE_UCAST
;
2045 if (adapter
->wol
& ATLX_WUFC_MC
)
2046 wol
->wolopts
|= WAKE_MCAST
;
2047 if (adapter
->wol
& ATLX_WUFC_BC
)
2048 wol
->wolopts
|= WAKE_BCAST
;
2049 if (adapter
->wol
& ATLX_WUFC_MAG
)
2050 wol
->wolopts
|= WAKE_MAGIC
;
2051 if (adapter
->wol
& ATLX_WUFC_LNKC
)
2052 wol
->wolopts
|= WAKE_PHY
;
2055 static int atl2_set_wol(struct net_device
*netdev
, struct ethtool_wolinfo
*wol
)
2057 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
2059 if (wol
->wolopts
& (WAKE_ARP
| WAKE_MAGICSECURE
))
2062 if (wol
->wolopts
& (WAKE_UCAST
| WAKE_BCAST
| WAKE_MCAST
))
2065 /* these settings will always override what we currently have */
2068 if (wol
->wolopts
& WAKE_MAGIC
)
2069 adapter
->wol
|= ATLX_WUFC_MAG
;
2070 if (wol
->wolopts
& WAKE_PHY
)
2071 adapter
->wol
|= ATLX_WUFC_LNKC
;
2076 static int atl2_nway_reset(struct net_device
*netdev
)
2078 struct atl2_adapter
*adapter
= netdev_priv(netdev
);
2079 if (netif_running(netdev
))
2080 atl2_reinit_locked(adapter
);
2084 static const struct ethtool_ops atl2_ethtool_ops
= {
2085 .get_settings
= atl2_get_settings
,
2086 .set_settings
= atl2_set_settings
,
2087 .get_drvinfo
= atl2_get_drvinfo
,
2088 .get_regs_len
= atl2_get_regs_len
,
2089 .get_regs
= atl2_get_regs
,
2090 .get_wol
= atl2_get_wol
,
2091 .set_wol
= atl2_set_wol
,
2092 .get_msglevel
= atl2_get_msglevel
,
2093 .set_msglevel
= atl2_set_msglevel
,
2094 .nway_reset
= atl2_nway_reset
,
2095 .get_link
= ethtool_op_get_link
,
2096 .get_eeprom_len
= atl2_get_eeprom_len
,
2097 .get_eeprom
= atl2_get_eeprom
,
2098 .set_eeprom
= atl2_set_eeprom
,
2101 #define LBYTESWAP(a) ((((a) & 0x00ff00ff) << 8) | \
2102 (((a) & 0xff00ff00) >> 8))
2103 #define LONGSWAP(a) ((LBYTESWAP(a) << 16) | (LBYTESWAP(a) >> 16))
2104 #define SHORTSWAP(a) (((a) << 8) | ((a) >> 8))
2107 * Reset the transmit and receive units; mask and clear all interrupts.
2109 * hw - Struct containing variables accessed by shared code
2110 * return : 0 or idle status (if error)
2112 static s32
atl2_reset_hw(struct atl2_hw
*hw
)
2115 u16 pci_cfg_cmd_word
;
2118 /* Workaround for PCI problem when BIOS sets MMRBC incorrectly. */
2119 atl2_read_pci_cfg(hw
, PCI_REG_COMMAND
, &pci_cfg_cmd_word
);
2120 if ((pci_cfg_cmd_word
&
2121 (CMD_IO_SPACE
|CMD_MEMORY_SPACE
|CMD_BUS_MASTER
)) !=
2122 (CMD_IO_SPACE
|CMD_MEMORY_SPACE
|CMD_BUS_MASTER
)) {
2124 (CMD_IO_SPACE
|CMD_MEMORY_SPACE
|CMD_BUS_MASTER
);
2125 atl2_write_pci_cfg(hw
, PCI_REG_COMMAND
, &pci_cfg_cmd_word
);
2128 /* Clear Interrupt mask to stop board from generating
2129 * interrupts & Clear any pending interrupt events
2132 /* ATL2_WRITE_REG(hw, REG_IMR, 0); */
2133 /* ATL2_WRITE_REG(hw, REG_ISR, 0xffffffff); */
2135 /* Issue Soft Reset to the MAC. This will reset the chip's
2136 * transmit, receive, DMA. It will not effect
2137 * the current PCI configuration. The global reset bit is self-
2138 * clearing, and should clear within a microsecond.
2140 ATL2_WRITE_REG(hw
, REG_MASTER_CTRL
, MASTER_CTRL_SOFT_RST
);
2142 msleep(1); /* delay about 1ms */
2144 /* Wait at least 10ms for All module to be Idle */
2145 for (i
= 0; i
< 10; i
++) {
2146 icr
= ATL2_READ_REG(hw
, REG_IDLE_STATUS
);
2149 msleep(1); /* delay 1 ms */
2159 #define CUSTOM_SPI_CS_SETUP 2
2160 #define CUSTOM_SPI_CLK_HI 2
2161 #define CUSTOM_SPI_CLK_LO 2
2162 #define CUSTOM_SPI_CS_HOLD 2
2163 #define CUSTOM_SPI_CS_HI 3
2165 static struct atl2_spi_flash_dev flash_table
[] =
2167 /* MFR WRSR READ PROGRAM WREN WRDI RDSR RDID SECTOR_ERASE CHIP_ERASE */
2168 {"Atmel", 0x0, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62 },
2169 {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60 },
2170 {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7 },
2173 static bool atl2_spi_read(struct atl2_hw
*hw
, u32 addr
, u32
*buf
)
2178 ATL2_WRITE_REG(hw
, REG_SPI_DATA
, 0);
2179 ATL2_WRITE_REG(hw
, REG_SPI_ADDR
, addr
);
2181 value
= SPI_FLASH_CTRL_WAIT_READY
|
2182 (CUSTOM_SPI_CS_SETUP
& SPI_FLASH_CTRL_CS_SETUP_MASK
) <<
2183 SPI_FLASH_CTRL_CS_SETUP_SHIFT
|
2184 (CUSTOM_SPI_CLK_HI
& SPI_FLASH_CTRL_CLK_HI_MASK
) <<
2185 SPI_FLASH_CTRL_CLK_HI_SHIFT
|
2186 (CUSTOM_SPI_CLK_LO
& SPI_FLASH_CTRL_CLK_LO_MASK
) <<
2187 SPI_FLASH_CTRL_CLK_LO_SHIFT
|
2188 (CUSTOM_SPI_CS_HOLD
& SPI_FLASH_CTRL_CS_HOLD_MASK
) <<
2189 SPI_FLASH_CTRL_CS_HOLD_SHIFT
|
2190 (CUSTOM_SPI_CS_HI
& SPI_FLASH_CTRL_CS_HI_MASK
) <<
2191 SPI_FLASH_CTRL_CS_HI_SHIFT
|
2192 (0x1 & SPI_FLASH_CTRL_INS_MASK
) << SPI_FLASH_CTRL_INS_SHIFT
;
2194 ATL2_WRITE_REG(hw
, REG_SPI_FLASH_CTRL
, value
);
2196 value
|= SPI_FLASH_CTRL_START
;
2198 ATL2_WRITE_REG(hw
, REG_SPI_FLASH_CTRL
, value
);
2200 for (i
= 0; i
< 10; i
++) {
2202 value
= ATL2_READ_REG(hw
, REG_SPI_FLASH_CTRL
);
2203 if (!(value
& SPI_FLASH_CTRL_START
))
2207 if (value
& SPI_FLASH_CTRL_START
)
2210 *buf
= ATL2_READ_REG(hw
, REG_SPI_DATA
);
2216 * get_permanent_address
2217 * return 0 if get valid mac address,
2219 static int get_permanent_address(struct atl2_hw
*hw
)
2224 u8 EthAddr
[ETH_ALEN
];
2227 if (is_valid_ether_addr(hw
->perm_mac_addr
))
2233 if (!atl2_check_eeprom_exist(hw
)) { /* eeprom exists */
2237 /* Read out all EEPROM content */
2240 if (atl2_read_eeprom(hw
, i
+ 0x100, &Control
)) {
2242 if (Register
== REG_MAC_STA_ADDR
)
2244 else if (Register
==
2245 (REG_MAC_STA_ADDR
+ 4))
2248 } else if ((Control
& 0xff) == 0x5A) {
2250 Register
= (u16
) (Control
>> 16);
2252 /* assume data end while encount an invalid KEYWORD */
2256 break; /* read error */
2261 *(u32
*) &EthAddr
[2] = LONGSWAP(Addr
[0]);
2262 *(u16
*) &EthAddr
[0] = SHORTSWAP(*(u16
*) &Addr
[1]);
2264 if (is_valid_ether_addr(EthAddr
)) {
2265 memcpy(hw
->perm_mac_addr
, EthAddr
, ETH_ALEN
);
2271 /* see if SPI flash exists? */
2278 if (atl2_spi_read(hw
, i
+ 0x1f000, &Control
)) {
2280 if (Register
== REG_MAC_STA_ADDR
)
2282 else if (Register
== (REG_MAC_STA_ADDR
+ 4))
2285 } else if ((Control
& 0xff) == 0x5A) {
2287 Register
= (u16
) (Control
>> 16);
2289 break; /* data end */
2292 break; /* read error */
2297 *(u32
*) &EthAddr
[2] = LONGSWAP(Addr
[0]);
2298 *(u16
*) &EthAddr
[0] = SHORTSWAP(*(u16
*)&Addr
[1]);
2299 if (is_valid_ether_addr(EthAddr
)) {
2300 memcpy(hw
->perm_mac_addr
, EthAddr
, ETH_ALEN
);
2303 /* maybe MAC-address is from BIOS */
2304 Addr
[0] = ATL2_READ_REG(hw
, REG_MAC_STA_ADDR
);
2305 Addr
[1] = ATL2_READ_REG(hw
, REG_MAC_STA_ADDR
+ 4);
2306 *(u32
*) &EthAddr
[2] = LONGSWAP(Addr
[0]);
2307 *(u16
*) &EthAddr
[0] = SHORTSWAP(*(u16
*) &Addr
[1]);
2309 if (is_valid_ether_addr(EthAddr
)) {
2310 memcpy(hw
->perm_mac_addr
, EthAddr
, ETH_ALEN
);
2318 * Reads the adapter's MAC address from the EEPROM
2320 * hw - Struct containing variables accessed by shared code
2322 static s32
atl2_read_mac_addr(struct atl2_hw
*hw
)
2324 if (get_permanent_address(hw
)) {
2326 /* FIXME: shouldn't we use eth_random_addr() here? */
2327 hw
->perm_mac_addr
[0] = 0x00;
2328 hw
->perm_mac_addr
[1] = 0x13;
2329 hw
->perm_mac_addr
[2] = 0x74;
2330 hw
->perm_mac_addr
[3] = 0x00;
2331 hw
->perm_mac_addr
[4] = 0x5c;
2332 hw
->perm_mac_addr
[5] = 0x38;
2335 memcpy(hw
->mac_addr
, hw
->perm_mac_addr
, ETH_ALEN
);
2341 * Hashes an address to determine its location in the multicast table
2343 * hw - Struct containing variables accessed by shared code
2344 * mc_addr - the multicast address to hash
2348 * set hash value for a multicast address
2349 * hash calcu processing :
2350 * 1. calcu 32bit CRC for multicast address
2351 * 2. reverse crc with MSB to LSB
2353 static u32
atl2_hash_mc_addr(struct atl2_hw
*hw
, u8
*mc_addr
)
2359 crc32
= ether_crc_le(6, mc_addr
);
2361 for (i
= 0; i
< 32; i
++)
2362 value
|= (((crc32
>> i
) & 1) << (31 - i
));
2368 * Sets the bit in the multicast table corresponding to the hash value.
2370 * hw - Struct containing variables accessed by shared code
2371 * hash_value - Multicast address hash value
2373 static void atl2_hash_set(struct atl2_hw
*hw
, u32 hash_value
)
2375 u32 hash_bit
, hash_reg
;
2378 /* The HASH Table is a register array of 2 32-bit registers.
2379 * It is treated like an array of 64 bits. We want to set
2380 * bit BitArray[hash_value]. So we figure out what register
2381 * the bit is in, read it, OR in the new bit, then write
2382 * back the new value. The register is determined by the
2383 * upper 7 bits of the hash value and the bit within that
2384 * register are determined by the lower 5 bits of the value.
2386 hash_reg
= (hash_value
>> 31) & 0x1;
2387 hash_bit
= (hash_value
>> 26) & 0x1F;
2389 mta
= ATL2_READ_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
);
2391 mta
|= (1 << hash_bit
);
2393 ATL2_WRITE_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
, mta
);
2397 * atl2_init_pcie - init PCIE module
2399 static void atl2_init_pcie(struct atl2_hw
*hw
)
2402 value
= LTSSM_TEST_MODE_DEF
;
2403 ATL2_WRITE_REG(hw
, REG_LTSSM_TEST_MODE
, value
);
2405 value
= PCIE_DLL_TX_CTRL1_DEF
;
2406 ATL2_WRITE_REG(hw
, REG_PCIE_DLL_TX_CTRL1
, value
);
2409 static void atl2_init_flash_opcode(struct atl2_hw
*hw
)
2411 if (hw
->flash_vendor
>= ARRAY_SIZE(flash_table
))
2412 hw
->flash_vendor
= 0; /* ATMEL */
2415 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_PROGRAM
,
2416 flash_table
[hw
->flash_vendor
].cmdPROGRAM
);
2417 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_SC_ERASE
,
2418 flash_table
[hw
->flash_vendor
].cmdSECTOR_ERASE
);
2419 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_CHIP_ERASE
,
2420 flash_table
[hw
->flash_vendor
].cmdCHIP_ERASE
);
2421 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_RDID
,
2422 flash_table
[hw
->flash_vendor
].cmdRDID
);
2423 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_WREN
,
2424 flash_table
[hw
->flash_vendor
].cmdWREN
);
2425 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_RDSR
,
2426 flash_table
[hw
->flash_vendor
].cmdRDSR
);
2427 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_WRSR
,
2428 flash_table
[hw
->flash_vendor
].cmdWRSR
);
2429 ATL2_WRITE_REGB(hw
, REG_SPI_FLASH_OP_READ
,
2430 flash_table
[hw
->flash_vendor
].cmdREAD
);
2433 /********************************************************************
2434 * Performs basic configuration of the adapter.
2436 * hw - Struct containing variables accessed by shared code
2437 * Assumes that the controller has previously been reset and is in a
2438 * post-reset uninitialized state. Initializes multicast table,
2439 * and Calls routines to setup link
2440 * Leaves the transmit and receive units disabled and uninitialized.
2441 ********************************************************************/
2442 static s32
atl2_init_hw(struct atl2_hw
*hw
)
2448 /* Zero out the Multicast HASH table */
2449 /* clear the old settings from the multicast hash table */
2450 ATL2_WRITE_REG(hw
, REG_RX_HASH_TABLE
, 0);
2451 ATL2_WRITE_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, 1, 0);
2453 atl2_init_flash_opcode(hw
);
2455 ret_val
= atl2_phy_init(hw
);
2461 * Detects the current speed and duplex settings of the hardware.
2463 * hw - Struct containing variables accessed by shared code
2464 * speed - Speed of the connection
2465 * duplex - Duplex setting of the connection
2467 static s32
atl2_get_speed_and_duplex(struct atl2_hw
*hw
, u16
*speed
,
2473 /* Read PHY Specific Status Register (17) */
2474 ret_val
= atl2_read_phy_reg(hw
, MII_ATLX_PSSR
, &phy_data
);
2478 if (!(phy_data
& MII_ATLX_PSSR_SPD_DPLX_RESOLVED
))
2479 return ATLX_ERR_PHY_RES
;
2481 switch (phy_data
& MII_ATLX_PSSR_SPEED
) {
2482 case MII_ATLX_PSSR_100MBS
:
2485 case MII_ATLX_PSSR_10MBS
:
2489 return ATLX_ERR_PHY_SPEED
;
2492 if (phy_data
& MII_ATLX_PSSR_DPLX
)
2493 *duplex
= FULL_DUPLEX
;
2495 *duplex
= HALF_DUPLEX
;
2501 * Reads the value from a PHY register
2502 * hw - Struct containing variables accessed by shared code
2503 * reg_addr - address of the PHY register to read
2505 static s32
atl2_read_phy_reg(struct atl2_hw
*hw
, u16 reg_addr
, u16
*phy_data
)
2510 val
= ((u32
)(reg_addr
& MDIO_REG_ADDR_MASK
)) << MDIO_REG_ADDR_SHIFT
|
2514 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
2515 ATL2_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
2519 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
2521 val
= ATL2_READ_REG(hw
, REG_MDIO_CTRL
);
2522 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
2526 if (!(val
& (MDIO_START
| MDIO_BUSY
))) {
2527 *phy_data
= (u16
)val
;
2531 return ATLX_ERR_PHY
;
2535 * Writes a value to a PHY register
2536 * hw - Struct containing variables accessed by shared code
2537 * reg_addr - address of the PHY register to write
2538 * data - data to write to the PHY
2540 static s32
atl2_write_phy_reg(struct atl2_hw
*hw
, u32 reg_addr
, u16 phy_data
)
2545 val
= ((u32
)(phy_data
& MDIO_DATA_MASK
)) << MDIO_DATA_SHIFT
|
2546 (reg_addr
& MDIO_REG_ADDR_MASK
) << MDIO_REG_ADDR_SHIFT
|
2549 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
2550 ATL2_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
2554 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
2556 val
= ATL2_READ_REG(hw
, REG_MDIO_CTRL
);
2557 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
2563 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
2566 return ATLX_ERR_PHY
;
2570 * Configures PHY autoneg and flow control advertisement settings
2572 * hw - Struct containing variables accessed by shared code
2574 static s32
atl2_phy_setup_autoneg_adv(struct atl2_hw
*hw
)
2577 s16 mii_autoneg_adv_reg
;
2579 /* Read the MII Auto-Neg Advertisement Register (Address 4). */
2580 mii_autoneg_adv_reg
= MII_AR_DEFAULT_CAP_MASK
;
2582 /* Need to parse autoneg_advertised and set up
2583 * the appropriate PHY registers. First we will parse for
2584 * autoneg_advertised software override. Since we can advertise
2585 * a plethora of combinations, we need to check each bit
2589 /* First we clear all the 10/100 mb speed bits in the Auto-Neg
2590 * Advertisement Register (Address 4) and the 1000 mb speed bits in
2591 * the 1000Base-T Control Register (Address 9). */
2592 mii_autoneg_adv_reg
&= ~MII_AR_SPEED_MASK
;
2594 /* Need to parse MediaType and setup the
2595 * appropriate PHY registers. */
2596 switch (hw
->MediaType
) {
2597 case MEDIA_TYPE_AUTO_SENSOR
:
2598 mii_autoneg_adv_reg
|=
2599 (MII_AR_10T_HD_CAPS
|
2600 MII_AR_10T_FD_CAPS
|
2601 MII_AR_100TX_HD_CAPS
|
2602 MII_AR_100TX_FD_CAPS
);
2603 hw
->autoneg_advertised
=
2609 case MEDIA_TYPE_100M_FULL
:
2610 mii_autoneg_adv_reg
|= MII_AR_100TX_FD_CAPS
;
2611 hw
->autoneg_advertised
= ADVERTISE_100_FULL
;
2613 case MEDIA_TYPE_100M_HALF
:
2614 mii_autoneg_adv_reg
|= MII_AR_100TX_HD_CAPS
;
2615 hw
->autoneg_advertised
= ADVERTISE_100_HALF
;
2617 case MEDIA_TYPE_10M_FULL
:
2618 mii_autoneg_adv_reg
|= MII_AR_10T_FD_CAPS
;
2619 hw
->autoneg_advertised
= ADVERTISE_10_FULL
;
2622 mii_autoneg_adv_reg
|= MII_AR_10T_HD_CAPS
;
2623 hw
->autoneg_advertised
= ADVERTISE_10_HALF
;
2627 /* flow control fixed to enable all */
2628 mii_autoneg_adv_reg
|= (MII_AR_ASM_DIR
| MII_AR_PAUSE
);
2630 hw
->mii_autoneg_adv_reg
= mii_autoneg_adv_reg
;
2632 ret_val
= atl2_write_phy_reg(hw
, MII_ADVERTISE
, mii_autoneg_adv_reg
);
2641 * Resets the PHY and make all config validate
2643 * hw - Struct containing variables accessed by shared code
2645 * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
2647 static s32
atl2_phy_commit(struct atl2_hw
*hw
)
2652 phy_data
= MII_CR_RESET
| MII_CR_AUTO_NEG_EN
| MII_CR_RESTART_AUTO_NEG
;
2653 ret_val
= atl2_write_phy_reg(hw
, MII_BMCR
, phy_data
);
2657 /* pcie serdes link may be down ! */
2658 for (i
= 0; i
< 25; i
++) {
2660 val
= ATL2_READ_REG(hw
, REG_MDIO_CTRL
);
2661 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
2665 if (0 != (val
& (MDIO_START
| MDIO_BUSY
))) {
2666 printk(KERN_ERR
"atl2: PCIe link down for at least 25ms !\n");
2673 static s32
atl2_phy_init(struct atl2_hw
*hw
)
2678 if (hw
->phy_configured
)
2682 ATL2_WRITE_REGW(hw
, REG_PHY_ENABLE
, 1);
2683 ATL2_WRITE_FLUSH(hw
);
2686 /* check if the PHY is in powersaving mode */
2687 atl2_write_phy_reg(hw
, MII_DBG_ADDR
, 0);
2688 atl2_read_phy_reg(hw
, MII_DBG_DATA
, &phy_val
);
2690 /* 024E / 124E 0r 0274 / 1274 ? */
2691 if (phy_val
& 0x1000) {
2693 atl2_write_phy_reg(hw
, MII_DBG_DATA
, phy_val
);
2698 /*Enable PHY LinkChange Interrupt */
2699 ret_val
= atl2_write_phy_reg(hw
, 18, 0xC00);
2703 /* setup AutoNeg parameters */
2704 ret_val
= atl2_phy_setup_autoneg_adv(hw
);
2708 /* SW.Reset & En-Auto-Neg to restart Auto-Neg */
2709 ret_val
= atl2_phy_commit(hw
);
2713 hw
->phy_configured
= true;
2718 static void atl2_set_mac_addr(struct atl2_hw
*hw
)
2721 /* 00-0B-6A-F6-00-DC
2722 * 0: 6AF600DC 1: 000B
2724 value
= (((u32
)hw
->mac_addr
[2]) << 24) |
2725 (((u32
)hw
->mac_addr
[3]) << 16) |
2726 (((u32
)hw
->mac_addr
[4]) << 8) |
2727 (((u32
)hw
->mac_addr
[5]));
2728 ATL2_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 0, value
);
2730 value
= (((u32
)hw
->mac_addr
[0]) << 8) |
2731 (((u32
)hw
->mac_addr
[1]));
2732 ATL2_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 1, value
);
2736 * check_eeprom_exist
2737 * return 0 if eeprom exist
2739 static int atl2_check_eeprom_exist(struct atl2_hw
*hw
)
2743 value
= ATL2_READ_REG(hw
, REG_SPI_FLASH_CTRL
);
2744 if (value
& SPI_FLASH_CTRL_EN_VPD
) {
2745 value
&= ~SPI_FLASH_CTRL_EN_VPD
;
2746 ATL2_WRITE_REG(hw
, REG_SPI_FLASH_CTRL
, value
);
2748 value
= ATL2_READ_REGW(hw
, REG_PCIE_CAP_LIST
);
2749 return ((value
& 0xFF00) == 0x6C00) ? 0 : 1;
2752 /* FIXME: This doesn't look right. -- CHS */
2753 static bool atl2_write_eeprom(struct atl2_hw
*hw
, u32 offset
, u32 value
)
2758 static bool atl2_read_eeprom(struct atl2_hw
*hw
, u32 Offset
, u32
*pValue
)
2764 return false; /* address do not align */
2766 ATL2_WRITE_REG(hw
, REG_VPD_DATA
, 0);
2767 Control
= (Offset
& VPD_CAP_VPD_ADDR_MASK
) << VPD_CAP_VPD_ADDR_SHIFT
;
2768 ATL2_WRITE_REG(hw
, REG_VPD_CAP
, Control
);
2770 for (i
= 0; i
< 10; i
++) {
2772 Control
= ATL2_READ_REG(hw
, REG_VPD_CAP
);
2773 if (Control
& VPD_CAP_VPD_FLAG
)
2777 if (Control
& VPD_CAP_VPD_FLAG
) {
2778 *pValue
= ATL2_READ_REG(hw
, REG_VPD_DATA
);
2781 return false; /* timeout */
2784 static void atl2_force_ps(struct atl2_hw
*hw
)
2788 atl2_write_phy_reg(hw
, MII_DBG_ADDR
, 0);
2789 atl2_read_phy_reg(hw
, MII_DBG_DATA
, &phy_val
);
2790 atl2_write_phy_reg(hw
, MII_DBG_DATA
, phy_val
| 0x1000);
2792 atl2_write_phy_reg(hw
, MII_DBG_ADDR
, 2);
2793 atl2_write_phy_reg(hw
, MII_DBG_DATA
, 0x3000);
2794 atl2_write_phy_reg(hw
, MII_DBG_ADDR
, 3);
2795 atl2_write_phy_reg(hw
, MII_DBG_DATA
, 0);
2798 /* This is the only thing that needs to be changed to adjust the
2799 * maximum number of ports that the driver can manage.
2801 #define ATL2_MAX_NIC 4
2803 #define OPTION_UNSET -1
2804 #define OPTION_DISABLED 0
2805 #define OPTION_ENABLED 1
2807 /* All parameters are treated the same, as an integer array of values.
2808 * This macro just reduces the need to repeat the same declaration code
2809 * over and over (plus this helps to avoid typo bugs).
2811 #define ATL2_PARAM_INIT {[0 ... ATL2_MAX_NIC] = OPTION_UNSET}
2812 #ifndef module_param_array
2813 /* Module Parameters are always initialized to -1, so that the driver
2814 * can tell the difference between no user specified value or the
2815 * user asking for the default value.
2816 * The true default values are loaded in when atl2_check_options is called.
2818 * This is a GCC extension to ANSI C.
2819 * See the item "Labeled Elements in Initializers" in the section
2820 * "Extensions to the C Language Family" of the GCC documentation.
2823 #define ATL2_PARAM(X, desc) \
2824 static const int X[ATL2_MAX_NIC + 1] = ATL2_PARAM_INIT; \
2825 MODULE_PARM(X, "1-" __MODULE_STRING(ATL2_MAX_NIC) "i"); \
2826 MODULE_PARM_DESC(X, desc);
2828 #define ATL2_PARAM(X, desc) \
2829 static int X[ATL2_MAX_NIC+1] = ATL2_PARAM_INIT; \
2830 static unsigned int num_##X; \
2831 module_param_array_named(X, X, int, &num_##X, 0); \
2832 MODULE_PARM_DESC(X, desc);
2836 * Transmit Memory Size
2837 * Valid Range: 64-2048
2838 * Default Value: 128
2840 #define ATL2_MIN_TX_MEMSIZE 4 /* 4KB */
2841 #define ATL2_MAX_TX_MEMSIZE 64 /* 64KB */
2842 #define ATL2_DEFAULT_TX_MEMSIZE 8 /* 8KB */
2843 ATL2_PARAM(TxMemSize
, "Bytes of Transmit Memory");
2846 * Receive Memory Block Count
2847 * Valid Range: 16-512
2848 * Default Value: 128
2850 #define ATL2_MIN_RXD_COUNT 16
2851 #define ATL2_MAX_RXD_COUNT 512
2852 #define ATL2_DEFAULT_RXD_COUNT 64
2853 ATL2_PARAM(RxMemBlock
, "Number of receive memory block");
2856 * User Specified MediaType Override
2859 * - 0 - auto-negotiate at all supported speeds
2860 * - 1 - only link at 1000Mbps Full Duplex
2861 * - 2 - only link at 100Mbps Full Duplex
2862 * - 3 - only link at 100Mbps Half Duplex
2863 * - 4 - only link at 10Mbps Full Duplex
2864 * - 5 - only link at 10Mbps Half Duplex
2867 ATL2_PARAM(MediaType
, "MediaType Select");
2870 * Interrupt Moderate Timer in units of 2048 ns (~2 us)
2871 * Valid Range: 10-65535
2872 * Default Value: 45000(90ms)
2874 #define INT_MOD_DEFAULT_CNT 100 /* 200us */
2875 #define INT_MOD_MAX_CNT 65000
2876 #define INT_MOD_MIN_CNT 50
2877 ATL2_PARAM(IntModTimer
, "Interrupt Moderator Timer");
2886 ATL2_PARAM(FlashVendor
, "SPI Flash Vendor");
2888 #define AUTONEG_ADV_DEFAULT 0x2F
2889 #define AUTONEG_ADV_MASK 0x2F
2890 #define FLOW_CONTROL_DEFAULT FLOW_CONTROL_FULL
2892 #define FLASH_VENDOR_DEFAULT 0
2893 #define FLASH_VENDOR_MIN 0
2894 #define FLASH_VENDOR_MAX 2
2896 struct atl2_option
{
2897 enum { enable_option
, range_option
, list_option
} type
;
2902 struct { /* range_option info */
2906 struct { /* list_option info */
2908 struct atl2_opt_list
{ int i
; char *str
; } *p
;
2913 static int atl2_validate_option(int *value
, struct atl2_option
*opt
)
2916 struct atl2_opt_list
*ent
;
2918 if (*value
== OPTION_UNSET
) {
2923 switch (opt
->type
) {
2926 case OPTION_ENABLED
:
2927 printk(KERN_INFO
"%s Enabled\n", opt
->name
);
2929 case OPTION_DISABLED
:
2930 printk(KERN_INFO
"%s Disabled\n", opt
->name
);
2935 if (*value
>= opt
->arg
.r
.min
&& *value
<= opt
->arg
.r
.max
) {
2936 printk(KERN_INFO
"%s set to %i\n", opt
->name
, *value
);
2941 for (i
= 0; i
< opt
->arg
.l
.nr
; i
++) {
2942 ent
= &opt
->arg
.l
.p
[i
];
2943 if (*value
== ent
->i
) {
2944 if (ent
->str
[0] != '\0')
2945 printk(KERN_INFO
"%s\n", ent
->str
);
2954 printk(KERN_INFO
"Invalid %s specified (%i) %s\n",
2955 opt
->name
, *value
, opt
->err
);
2961 * atl2_check_options - Range Checking for Command Line Parameters
2962 * @adapter: board private structure
2964 * This routine checks all command line parameters for valid user
2965 * input. If an invalid value is given, or if no user specified
2966 * value exists, a default value is used. The final value is stored
2967 * in a variable in the adapter structure.
2969 static void atl2_check_options(struct atl2_adapter
*adapter
)
2972 struct atl2_option opt
;
2973 int bd
= adapter
->bd_number
;
2974 if (bd
>= ATL2_MAX_NIC
) {
2975 printk(KERN_NOTICE
"Warning: no configuration for board #%i\n",
2977 printk(KERN_NOTICE
"Using defaults for all values\n");
2978 #ifndef module_param_array
2983 /* Bytes of Transmit Memory */
2984 opt
.type
= range_option
;
2985 opt
.name
= "Bytes of Transmit Memory";
2986 opt
.err
= "using default of " __MODULE_STRING(ATL2_DEFAULT_TX_MEMSIZE
);
2987 opt
.def
= ATL2_DEFAULT_TX_MEMSIZE
;
2988 opt
.arg
.r
.min
= ATL2_MIN_TX_MEMSIZE
;
2989 opt
.arg
.r
.max
= ATL2_MAX_TX_MEMSIZE
;
2990 #ifdef module_param_array
2991 if (num_TxMemSize
> bd
) {
2993 val
= TxMemSize
[bd
];
2994 atl2_validate_option(&val
, &opt
);
2995 adapter
->txd_ring_size
= ((u32
) val
) * 1024;
2996 #ifdef module_param_array
2998 adapter
->txd_ring_size
= ((u32
)opt
.def
) * 1024;
3000 /* txs ring size: */
3001 adapter
->txs_ring_size
= adapter
->txd_ring_size
/ 128;
3002 if (adapter
->txs_ring_size
> 160)
3003 adapter
->txs_ring_size
= 160;
3005 /* Receive Memory Block Count */
3006 opt
.type
= range_option
;
3007 opt
.name
= "Number of receive memory block";
3008 opt
.err
= "using default of " __MODULE_STRING(ATL2_DEFAULT_RXD_COUNT
);
3009 opt
.def
= ATL2_DEFAULT_RXD_COUNT
;
3010 opt
.arg
.r
.min
= ATL2_MIN_RXD_COUNT
;
3011 opt
.arg
.r
.max
= ATL2_MAX_RXD_COUNT
;
3012 #ifdef module_param_array
3013 if (num_RxMemBlock
> bd
) {
3015 val
= RxMemBlock
[bd
];
3016 atl2_validate_option(&val
, &opt
);
3017 adapter
->rxd_ring_size
= (u32
)val
;
3019 /* ((u16)val)&~1; */ /* even number */
3020 #ifdef module_param_array
3022 adapter
->rxd_ring_size
= (u32
)opt
.def
;
3024 /* init RXD Flow control value */
3025 adapter
->hw
.fc_rxd_hi
= (adapter
->rxd_ring_size
/ 8) * 7;
3026 adapter
->hw
.fc_rxd_lo
= (ATL2_MIN_RXD_COUNT
/ 8) >
3027 (adapter
->rxd_ring_size
/ 12) ? (ATL2_MIN_RXD_COUNT
/ 8) :
3028 (adapter
->rxd_ring_size
/ 12);
3030 /* Interrupt Moderate Timer */
3031 opt
.type
= range_option
;
3032 opt
.name
= "Interrupt Moderate Timer";
3033 opt
.err
= "using default of " __MODULE_STRING(INT_MOD_DEFAULT_CNT
);
3034 opt
.def
= INT_MOD_DEFAULT_CNT
;
3035 opt
.arg
.r
.min
= INT_MOD_MIN_CNT
;
3036 opt
.arg
.r
.max
= INT_MOD_MAX_CNT
;
3037 #ifdef module_param_array
3038 if (num_IntModTimer
> bd
) {
3040 val
= IntModTimer
[bd
];
3041 atl2_validate_option(&val
, &opt
);
3042 adapter
->imt
= (u16
) val
;
3043 #ifdef module_param_array
3045 adapter
->imt
= (u16
)(opt
.def
);
3048 opt
.type
= range_option
;
3049 opt
.name
= "SPI Flash Vendor";
3050 opt
.err
= "using default of " __MODULE_STRING(FLASH_VENDOR_DEFAULT
);
3051 opt
.def
= FLASH_VENDOR_DEFAULT
;
3052 opt
.arg
.r
.min
= FLASH_VENDOR_MIN
;
3053 opt
.arg
.r
.max
= FLASH_VENDOR_MAX
;
3054 #ifdef module_param_array
3055 if (num_FlashVendor
> bd
) {
3057 val
= FlashVendor
[bd
];
3058 atl2_validate_option(&val
, &opt
);
3059 adapter
->hw
.flash_vendor
= (u8
) val
;
3060 #ifdef module_param_array
3062 adapter
->hw
.flash_vendor
= (u8
)(opt
.def
);
3065 opt
.type
= range_option
;
3066 opt
.name
= "Speed/Duplex Selection";
3067 opt
.err
= "using default of " __MODULE_STRING(MEDIA_TYPE_AUTO_SENSOR
);
3068 opt
.def
= MEDIA_TYPE_AUTO_SENSOR
;
3069 opt
.arg
.r
.min
= MEDIA_TYPE_AUTO_SENSOR
;
3070 opt
.arg
.r
.max
= MEDIA_TYPE_10M_HALF
;
3071 #ifdef module_param_array
3072 if (num_MediaType
> bd
) {
3074 val
= MediaType
[bd
];
3075 atl2_validate_option(&val
, &opt
);
3076 adapter
->hw
.MediaType
= (u16
) val
;
3077 #ifdef module_param_array
3079 adapter
->hw
.MediaType
= (u16
)(opt
.def
);