1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright(c) 2006 - 2007 Atheros Corporation. All rights reserved.
4  * Copyright(c) 2007 - 2008 Chris Snook <csnook@redhat.com>
5  *
6  * Derived from Intel e1000 driver
7  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
8  */
9 
10 #include <linux/atomic.h>
11 #include <linux/crc32.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/hardirq.h>
16 #include <linux/if_vlan.h>
17 #include <linux/in.h>
18 #include <linux/interrupt.h>
19 #include <linux/ip.h>
20 #include <linux/irqflags.h>
21 #include <linux/irqreturn.h>
22 #include <linux/mii.h>
23 #include <linux/net.h>
24 #include <linux/netdevice.h>
25 #include <linux/pci.h>
26 #include <linux/pci_ids.h>
27 #include <linux/pm.h>
28 #include <linux/skbuff.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/string.h>
32 #include <linux/tcp.h>
33 #include <linux/timer.h>
34 #include <linux/types.h>
35 #include <linux/workqueue.h>
36 
37 #include "atl2.h"
38 
39 static const char atl2_driver_name[] = "atl2";
40 static const struct ethtool_ops atl2_ethtool_ops;
41 
42 MODULE_AUTHOR("Atheros Corporation <xiong.huang@atheros.com>, Chris Snook <csnook@redhat.com>");
43 MODULE_DESCRIPTION("Atheros Fast Ethernet Network Driver");
44 MODULE_LICENSE("GPL");
45 
46 /*
47  * atl2_pci_tbl - PCI Device ID Table
48  */
49 static const struct pci_device_id atl2_pci_tbl[] = {
50 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2)},
51 	/* required last entry */
52 	{0,}
53 };
54 MODULE_DEVICE_TABLE(pci, atl2_pci_tbl);
55 
56 static void atl2_check_options(struct atl2_adapter *adapter);
57 
58 /**
59  * atl2_sw_init - Initialize general software structures (struct atl2_adapter)
60  * @adapter: board private structure to initialize
61  *
62  * atl2_sw_init initializes the Adapter private data structure.
63  * Fields are initialized based on PCI device information and
64  * OS network device settings (MTU size).
65  */
66 static int atl2_sw_init(struct atl2_adapter *adapter)
67 {
68 	struct atl2_hw *hw = &adapter->hw;
69 	struct pci_dev *pdev = adapter->pdev;
70 
71 	/* PCI config space info */
72 	hw->vendor_id = pdev->vendor;
73 	hw->device_id = pdev->device;
74 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
75 	hw->subsystem_id = pdev->subsystem_device;
76 	hw->revision_id  = pdev->revision;
77 
78 	pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
79 
80 	adapter->wol = 0;
81 	adapter->ict = 50000;  /* ~100ms */
82 	adapter->link_speed = SPEED_0;   /* hardware init */
83 	adapter->link_duplex = FULL_DUPLEX;
84 
85 	hw->phy_configured = false;
86 	hw->preamble_len = 7;
87 	hw->ipgt = 0x60;
88 	hw->min_ifg = 0x50;
89 	hw->ipgr1 = 0x40;
90 	hw->ipgr2 = 0x60;
91 	hw->retry_buf = 2;
92 	hw->max_retry = 0xf;
93 	hw->lcol = 0x37;
94 	hw->jam_ipg = 7;
95 	hw->fc_rxd_hi = 0;
96 	hw->fc_rxd_lo = 0;
97 	hw->max_frame_size = adapter->netdev->mtu;
98 
99 	spin_lock_init(&adapter->stats_lock);
100 
101 	set_bit(__ATL2_DOWN, &adapter->flags);
102 
103 	return 0;
104 }
105 
106 /**
107  * atl2_set_multi - Multicast and Promiscuous mode set
108  * @netdev: network interface device structure
109  *
110  * The set_multi entry point is called whenever the multicast address
111  * list or the network interface flags are updated.  This routine is
112  * responsible for configuring the hardware for proper multicast,
113  * promiscuous mode, and all-multi behavior.
114  */
115 static void atl2_set_multi(struct net_device *netdev)
116 {
117 	struct atl2_adapter *adapter = netdev_priv(netdev);
118 	struct atl2_hw *hw = &adapter->hw;
119 	struct netdev_hw_addr *ha;
120 	u32 rctl;
121 	u32 hash_value;
122 
123 	/* Check for Promiscuous and All Multicast modes */
124 	rctl = ATL2_READ_REG(hw, REG_MAC_CTRL);
125 
126 	if (netdev->flags & IFF_PROMISC) {
127 		rctl |= MAC_CTRL_PROMIS_EN;
128 	} else if (netdev->flags & IFF_ALLMULTI) {
129 		rctl |= MAC_CTRL_MC_ALL_EN;
130 		rctl &= ~MAC_CTRL_PROMIS_EN;
131 	} else
132 		rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
133 
134 	ATL2_WRITE_REG(hw, REG_MAC_CTRL, rctl);
135 
136 	/* clear the old settings from the multicast hash table */
137 	ATL2_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
138 	ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
139 
140 	/* comoute mc addresses' hash value ,and put it into hash table */
141 	netdev_for_each_mc_addr(ha, netdev) {
142 		hash_value = atl2_hash_mc_addr(hw, ha->addr);
143 		atl2_hash_set(hw, hash_value);
144 	}
145 }
146 
147 static void init_ring_ptrs(struct atl2_adapter *adapter)
148 {
149 	/* Read / Write Ptr Initialize: */
150 	adapter->txd_write_ptr = 0;
151 	atomic_set(&adapter->txd_read_ptr, 0);
152 
153 	adapter->rxd_read_ptr = 0;
154 	adapter->rxd_write_ptr = 0;
155 
156 	atomic_set(&adapter->txs_write_ptr, 0);
157 	adapter->txs_next_clear = 0;
158 }
159 
160 /**
161  * atl2_configure - Configure Transmit&Receive Unit after Reset
162  * @adapter: board private structure
163  *
164  * Configure the Tx /Rx unit of the MAC after a reset.
165  */
166 static int atl2_configure(struct atl2_adapter *adapter)
167 {
168 	struct atl2_hw *hw = &adapter->hw;
169 	u32 value;
170 
171 	/* clear interrupt status */
172 	ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0xffffffff);
173 
174 	/* set MAC Address */
175 	value = (((u32)hw->mac_addr[2]) << 24) |
176 		(((u32)hw->mac_addr[3]) << 16) |
177 		(((u32)hw->mac_addr[4]) << 8) |
178 		(((u32)hw->mac_addr[5]));
179 	ATL2_WRITE_REG(hw, REG_MAC_STA_ADDR, value);
180 	value = (((u32)hw->mac_addr[0]) << 8) |
181 		(((u32)hw->mac_addr[1]));
182 	ATL2_WRITE_REG(hw, (REG_MAC_STA_ADDR+4), value);
183 
184 	/* HI base address */
185 	ATL2_WRITE_REG(hw, REG_DESC_BASE_ADDR_HI,
186 		(u32)((adapter->ring_dma & 0xffffffff00000000ULL) >> 32));
187 
188 	/* LO base address */
189 	ATL2_WRITE_REG(hw, REG_TXD_BASE_ADDR_LO,
190 		(u32)(adapter->txd_dma & 0x00000000ffffffffULL));
191 	ATL2_WRITE_REG(hw, REG_TXS_BASE_ADDR_LO,
192 		(u32)(adapter->txs_dma & 0x00000000ffffffffULL));
193 	ATL2_WRITE_REG(hw, REG_RXD_BASE_ADDR_LO,
194 		(u32)(adapter->rxd_dma & 0x00000000ffffffffULL));
195 
196 	/* element count */
197 	ATL2_WRITE_REGW(hw, REG_TXD_MEM_SIZE, (u16)(adapter->txd_ring_size/4));
198 	ATL2_WRITE_REGW(hw, REG_TXS_MEM_SIZE, (u16)adapter->txs_ring_size);
199 	ATL2_WRITE_REGW(hw, REG_RXD_BUF_NUM,  (u16)adapter->rxd_ring_size);
200 
201 	/* config Internal SRAM */
202 /*
203     ATL2_WRITE_REGW(hw, REG_SRAM_TXRAM_END, sram_tx_end);
204     ATL2_WRITE_REGW(hw, REG_SRAM_TXRAM_END, sram_rx_end);
205 */
206 
207 	/* config IPG/IFG */
208 	value = (((u32)hw->ipgt & MAC_IPG_IFG_IPGT_MASK) <<
209 		MAC_IPG_IFG_IPGT_SHIFT) |
210 		(((u32)hw->min_ifg & MAC_IPG_IFG_MIFG_MASK) <<
211 		MAC_IPG_IFG_MIFG_SHIFT) |
212 		(((u32)hw->ipgr1 & MAC_IPG_IFG_IPGR1_MASK) <<
213 		MAC_IPG_IFG_IPGR1_SHIFT)|
214 		(((u32)hw->ipgr2 & MAC_IPG_IFG_IPGR2_MASK) <<
215 		MAC_IPG_IFG_IPGR2_SHIFT);
216 	ATL2_WRITE_REG(hw, REG_MAC_IPG_IFG, value);
217 
218 	/* config  Half-Duplex Control */
219 	value = ((u32)hw->lcol & MAC_HALF_DUPLX_CTRL_LCOL_MASK) |
220 		(((u32)hw->max_retry & MAC_HALF_DUPLX_CTRL_RETRY_MASK) <<
221 		MAC_HALF_DUPLX_CTRL_RETRY_SHIFT) |
222 		MAC_HALF_DUPLX_CTRL_EXC_DEF_EN |
223 		(0xa << MAC_HALF_DUPLX_CTRL_ABEBT_SHIFT) |
224 		(((u32)hw->jam_ipg & MAC_HALF_DUPLX_CTRL_JAMIPG_MASK) <<
225 		MAC_HALF_DUPLX_CTRL_JAMIPG_SHIFT);
226 	ATL2_WRITE_REG(hw, REG_MAC_HALF_DUPLX_CTRL, value);
227 
228 	/* set Interrupt Moderator Timer */
229 	ATL2_WRITE_REGW(hw, REG_IRQ_MODU_TIMER_INIT, adapter->imt);
230 	ATL2_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_ITIMER_EN);
231 
232 	/* set Interrupt Clear Timer */
233 	ATL2_WRITE_REGW(hw, REG_CMBDISDMA_TIMER, adapter->ict);
234 
235 	/* set MTU */
236 	ATL2_WRITE_REG(hw, REG_MTU, adapter->netdev->mtu +
237 		ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
238 
239 	/* 1590 */
240 	ATL2_WRITE_REG(hw, REG_TX_CUT_THRESH, 0x177);
241 
242 	/* flow control */
243 	ATL2_WRITE_REGW(hw, REG_PAUSE_ON_TH, hw->fc_rxd_hi);
244 	ATL2_WRITE_REGW(hw, REG_PAUSE_OFF_TH, hw->fc_rxd_lo);
245 
246 	/* Init mailbox */
247 	ATL2_WRITE_REGW(hw, REG_MB_TXD_WR_IDX, (u16)adapter->txd_write_ptr);
248 	ATL2_WRITE_REGW(hw, REG_MB_RXD_RD_IDX, (u16)adapter->rxd_read_ptr);
249 
250 	/* enable DMA read/write */
251 	ATL2_WRITE_REGB(hw, REG_DMAR, DMAR_EN);
252 	ATL2_WRITE_REGB(hw, REG_DMAW, DMAW_EN);
253 
254 	value = ATL2_READ_REG(&adapter->hw, REG_ISR);
255 	if ((value & ISR_PHY_LINKDOWN) != 0)
256 		value = 1; /* config failed */
257 	else
258 		value = 0;
259 
260 	/* clear all interrupt status */
261 	ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0x3fffffff);
262 	ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0);
263 	return value;
264 }
265 
266 /**
267  * atl2_setup_ring_resources - allocate Tx / RX descriptor resources
268  * @adapter: board private structure
269  *
270  * Return 0 on success, negative on failure
271  */
272 static s32 atl2_setup_ring_resources(struct atl2_adapter *adapter)
273 {
274 	struct pci_dev *pdev = adapter->pdev;
275 	int size;
276 	u8 offset = 0;
277 
278 	/* real ring DMA buffer */
279 	adapter->ring_size = size =
280 		adapter->txd_ring_size * 1 + 7 +	/* dword align */
281 		adapter->txs_ring_size * 4 + 7 +	/* dword align */
282 		adapter->rxd_ring_size * 1536 + 127;	/* 128bytes align */
283 
284 	adapter->ring_vir_addr = dma_alloc_coherent(&pdev->dev, size,
285 						    &adapter->ring_dma, GFP_KERNEL);
286 	if (!adapter->ring_vir_addr)
287 		return -ENOMEM;
288 
289 	/* Init TXD Ring */
290 	adapter->txd_dma = adapter->ring_dma ;
291 	offset = (adapter->txd_dma & 0x7) ? (8 - (adapter->txd_dma & 0x7)) : 0;
292 	adapter->txd_dma += offset;
293 	adapter->txd_ring = adapter->ring_vir_addr + offset;
294 
295 	/* Init TXS Ring */
296 	adapter->txs_dma = adapter->txd_dma + adapter->txd_ring_size;
297 	offset = (adapter->txs_dma & 0x7) ? (8 - (adapter->txs_dma & 0x7)) : 0;
298 	adapter->txs_dma += offset;
299 	adapter->txs_ring = (struct tx_pkt_status *)
300 		(((u8 *)adapter->txd_ring) + (adapter->txd_ring_size + offset));
301 
302 	/* Init RXD Ring */
303 	adapter->rxd_dma = adapter->txs_dma + adapter->txs_ring_size * 4;
304 	offset = (adapter->rxd_dma & 127) ?
305 		(128 - (adapter->rxd_dma & 127)) : 0;
306 	if (offset > 7)
307 		offset -= 8;
308 	else
309 		offset += (128 - 8);
310 
311 	adapter->rxd_dma += offset;
312 	adapter->rxd_ring = (struct rx_desc *) (((u8 *)adapter->txs_ring) +
313 		(adapter->txs_ring_size * 4 + offset));
314 
315 /*
316  * Read / Write Ptr Initialize:
317  *      init_ring_ptrs(adapter);
318  */
319 	return 0;
320 }
321 
322 /**
323  * atl2_irq_enable - Enable default interrupt generation settings
324  * @adapter: board private structure
325  */
326 static inline void atl2_irq_enable(struct atl2_adapter *adapter)
327 {
328 	ATL2_WRITE_REG(&adapter->hw, REG_IMR, IMR_NORMAL_MASK);
329 	ATL2_WRITE_FLUSH(&adapter->hw);
330 }
331 
332 /**
333  * atl2_irq_disable - Mask off interrupt generation on the NIC
334  * @adapter: board private structure
335  */
336 static inline void atl2_irq_disable(struct atl2_adapter *adapter)
337 {
338     ATL2_WRITE_REG(&adapter->hw, REG_IMR, 0);
339     ATL2_WRITE_FLUSH(&adapter->hw);
340     synchronize_irq(adapter->pdev->irq);
341 }
342 
343 static void __atl2_vlan_mode(netdev_features_t features, u32 *ctrl)
344 {
345 	if (features & NETIF_F_HW_VLAN_CTAG_RX) {
346 		/* enable VLAN tag insert/strip */
347 		*ctrl |= MAC_CTRL_RMV_VLAN;
348 	} else {
349 		/* disable VLAN tag insert/strip */
350 		*ctrl &= ~MAC_CTRL_RMV_VLAN;
351 	}
352 }
353 
354 static void atl2_vlan_mode(struct net_device *netdev,
355 	netdev_features_t features)
356 {
357 	struct atl2_adapter *adapter = netdev_priv(netdev);
358 	u32 ctrl;
359 
360 	atl2_irq_disable(adapter);
361 
362 	ctrl = ATL2_READ_REG(&adapter->hw, REG_MAC_CTRL);
363 	__atl2_vlan_mode(features, &ctrl);
364 	ATL2_WRITE_REG(&adapter->hw, REG_MAC_CTRL, ctrl);
365 
366 	atl2_irq_enable(adapter);
367 }
368 
369 static void atl2_restore_vlan(struct atl2_adapter *adapter)
370 {
371 	atl2_vlan_mode(adapter->netdev, adapter->netdev->features);
372 }
373 
374 static netdev_features_t atl2_fix_features(struct net_device *netdev,
375 	netdev_features_t features)
376 {
377 	/*
378 	 * Since there is no support for separate rx/tx vlan accel
379 	 * enable/disable make sure tx flag is always in same state as rx.
380 	 */
381 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
382 		features |= NETIF_F_HW_VLAN_CTAG_TX;
383 	else
384 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
385 
386 	return features;
387 }
388 
389 static int atl2_set_features(struct net_device *netdev,
390 	netdev_features_t features)
391 {
392 	netdev_features_t changed = netdev->features ^ features;
393 
394 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
395 		atl2_vlan_mode(netdev, features);
396 
397 	return 0;
398 }
399 
400 static void atl2_intr_rx(struct atl2_adapter *adapter)
401 {
402 	struct net_device *netdev = adapter->netdev;
403 	struct rx_desc *rxd;
404 	struct sk_buff *skb;
405 
406 	do {
407 		rxd = adapter->rxd_ring+adapter->rxd_write_ptr;
408 		if (!rxd->status.update)
409 			break; /* end of tx */
410 
411 		/* clear this flag at once */
412 		rxd->status.update = 0;
413 
414 		if (rxd->status.ok && rxd->status.pkt_size >= 60) {
415 			int rx_size = (int)(rxd->status.pkt_size - 4);
416 			/* alloc new buffer */
417 			skb = netdev_alloc_skb_ip_align(netdev, rx_size);
418 			if (NULL == skb) {
419 				/*
420 				 * Check that some rx space is free. If not,
421 				 * free one and mark stats->rx_dropped++.
422 				 */
423 				netdev->stats.rx_dropped++;
424 				break;
425 			}
426 			memcpy(skb->data, rxd->packet, rx_size);
427 			skb_put(skb, rx_size);
428 			skb->protocol = eth_type_trans(skb, netdev);
429 			if (rxd->status.vlan) {
430 				u16 vlan_tag = (rxd->status.vtag>>4) |
431 					((rxd->status.vtag&7) << 13) |
432 					((rxd->status.vtag&8) << 9);
433 
434 				__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
435 			}
436 			netif_rx(skb);
437 			netdev->stats.rx_bytes += rx_size;
438 			netdev->stats.rx_packets++;
439 		} else {
440 			netdev->stats.rx_errors++;
441 
442 			if (rxd->status.ok && rxd->status.pkt_size <= 60)
443 				netdev->stats.rx_length_errors++;
444 			if (rxd->status.mcast)
445 				netdev->stats.multicast++;
446 			if (rxd->status.crc)
447 				netdev->stats.rx_crc_errors++;
448 			if (rxd->status.align)
449 				netdev->stats.rx_frame_errors++;
450 		}
451 
452 		/* advance write ptr */
453 		if (++adapter->rxd_write_ptr == adapter->rxd_ring_size)
454 			adapter->rxd_write_ptr = 0;
455 	} while (1);
456 
457 	/* update mailbox? */
458 	adapter->rxd_read_ptr = adapter->rxd_write_ptr;
459 	ATL2_WRITE_REGW(&adapter->hw, REG_MB_RXD_RD_IDX, adapter->rxd_read_ptr);
460 }
461 
462 static void atl2_intr_tx(struct atl2_adapter *adapter)
463 {
464 	struct net_device *netdev = adapter->netdev;
465 	u32 txd_read_ptr;
466 	u32 txs_write_ptr;
467 	struct tx_pkt_status *txs;
468 	struct tx_pkt_header *txph;
469 	int free_hole = 0;
470 
471 	do {
472 		txs_write_ptr = (u32) atomic_read(&adapter->txs_write_ptr);
473 		txs = adapter->txs_ring + txs_write_ptr;
474 		if (!txs->update)
475 			break; /* tx stop here */
476 
477 		free_hole = 1;
478 		txs->update = 0;
479 
480 		if (++txs_write_ptr == adapter->txs_ring_size)
481 			txs_write_ptr = 0;
482 		atomic_set(&adapter->txs_write_ptr, (int)txs_write_ptr);
483 
484 		txd_read_ptr = (u32) atomic_read(&adapter->txd_read_ptr);
485 		txph = (struct tx_pkt_header *)
486 			(((u8 *)adapter->txd_ring) + txd_read_ptr);
487 
488 		if (txph->pkt_size != txs->pkt_size) {
489 			struct tx_pkt_status *old_txs = txs;
490 			printk(KERN_WARNING
491 				"%s: txs packet size not consistent with txd"
492 				" txd_:0x%08x, txs_:0x%08x!\n",
493 				adapter->netdev->name,
494 				*(u32 *)txph, *(u32 *)txs);
495 			printk(KERN_WARNING
496 				"txd read ptr: 0x%x\n",
497 				txd_read_ptr);
498 			txs = adapter->txs_ring + txs_write_ptr;
499 			printk(KERN_WARNING
500 				"txs-behind:0x%08x\n",
501 				*(u32 *)txs);
502 			if (txs_write_ptr < 2) {
503 				txs = adapter->txs_ring +
504 					(adapter->txs_ring_size +
505 					txs_write_ptr - 2);
506 			} else {
507 				txs = adapter->txs_ring + (txs_write_ptr - 2);
508 			}
509 			printk(KERN_WARNING
510 				"txs-before:0x%08x\n",
511 				*(u32 *)txs);
512 			txs = old_txs;
513 		}
514 
515 		 /* 4for TPH */
516 		txd_read_ptr += (((u32)(txph->pkt_size) + 7) & ~3);
517 		if (txd_read_ptr >= adapter->txd_ring_size)
518 			txd_read_ptr -= adapter->txd_ring_size;
519 
520 		atomic_set(&adapter->txd_read_ptr, (int)txd_read_ptr);
521 
522 		/* tx statistics: */
523 		if (txs->ok) {
524 			netdev->stats.tx_bytes += txs->pkt_size;
525 			netdev->stats.tx_packets++;
526 		}
527 		else
528 			netdev->stats.tx_errors++;
529 
530 		if (txs->defer)
531 			netdev->stats.collisions++;
532 		if (txs->abort_col)
533 			netdev->stats.tx_aborted_errors++;
534 		if (txs->late_col)
535 			netdev->stats.tx_window_errors++;
536 		if (txs->underrun)
537 			netdev->stats.tx_fifo_errors++;
538 	} while (1);
539 
540 	if (free_hole) {
541 		if (netif_queue_stopped(adapter->netdev) &&
542 			netif_carrier_ok(adapter->netdev))
543 			netif_wake_queue(adapter->netdev);
544 	}
545 }
546 
547 static void atl2_check_for_link(struct atl2_adapter *adapter)
548 {
549 	struct net_device *netdev = adapter->netdev;
550 	u16 phy_data = 0;
551 
552 	spin_lock(&adapter->stats_lock);
553 	atl2_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
554 	atl2_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
555 	spin_unlock(&adapter->stats_lock);
556 
557 	/* notify upper layer link down ASAP */
558 	if (!(phy_data & BMSR_LSTATUS)) { /* Link Down */
559 		if (netif_carrier_ok(netdev)) { /* old link state: Up */
560 		printk(KERN_INFO "%s: %s NIC Link is Down\n",
561 			atl2_driver_name, netdev->name);
562 		adapter->link_speed = SPEED_0;
563 		netif_carrier_off(netdev);
564 		netif_stop_queue(netdev);
565 		}
566 	}
567 	schedule_work(&adapter->link_chg_task);
568 }
569 
570 static inline void atl2_clear_phy_int(struct atl2_adapter *adapter)
571 {
572 	u16 phy_data;
573 	spin_lock(&adapter->stats_lock);
574 	atl2_read_phy_reg(&adapter->hw, 19, &phy_data);
575 	spin_unlock(&adapter->stats_lock);
576 }
577 
578 /**
579  * atl2_intr - Interrupt Handler
580  * @irq: interrupt number
581  * @data: pointer to a network interface device structure
582  */
583 static irqreturn_t atl2_intr(int irq, void *data)
584 {
585 	struct atl2_adapter *adapter = netdev_priv(data);
586 	struct atl2_hw *hw = &adapter->hw;
587 	u32 status;
588 
589 	status = ATL2_READ_REG(hw, REG_ISR);
590 	if (0 == status)
591 		return IRQ_NONE;
592 
593 	/* link event */
594 	if (status & ISR_PHY)
595 		atl2_clear_phy_int(adapter);
596 
597 	/* clear ISR status, and Enable CMB DMA/Disable Interrupt */
598 	ATL2_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
599 
600 	/* check if PCIE PHY Link down */
601 	if (status & ISR_PHY_LINKDOWN) {
602 		if (netif_running(adapter->netdev)) { /* reset MAC */
603 			ATL2_WRITE_REG(hw, REG_ISR, 0);
604 			ATL2_WRITE_REG(hw, REG_IMR, 0);
605 			ATL2_WRITE_FLUSH(hw);
606 			schedule_work(&adapter->reset_task);
607 			return IRQ_HANDLED;
608 		}
609 	}
610 
611 	/* check if DMA read/write error? */
612 	if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
613 		ATL2_WRITE_REG(hw, REG_ISR, 0);
614 		ATL2_WRITE_REG(hw, REG_IMR, 0);
615 		ATL2_WRITE_FLUSH(hw);
616 		schedule_work(&adapter->reset_task);
617 		return IRQ_HANDLED;
618 	}
619 
620 	/* link event */
621 	if (status & (ISR_PHY | ISR_MANUAL)) {
622 		adapter->netdev->stats.tx_carrier_errors++;
623 		atl2_check_for_link(adapter);
624 	}
625 
626 	/* transmit event */
627 	if (status & ISR_TX_EVENT)
628 		atl2_intr_tx(adapter);
629 
630 	/* rx exception */
631 	if (status & ISR_RX_EVENT)
632 		atl2_intr_rx(adapter);
633 
634 	/* re-enable Interrupt */
635 	ATL2_WRITE_REG(&adapter->hw, REG_ISR, 0);
636 	return IRQ_HANDLED;
637 }
638 
639 static int atl2_request_irq(struct atl2_adapter *adapter)
640 {
641 	struct net_device *netdev = adapter->netdev;
642 	int flags, err = 0;
643 
644 	flags = IRQF_SHARED;
645 	adapter->have_msi = true;
646 	err = pci_enable_msi(adapter->pdev);
647 	if (err)
648 		adapter->have_msi = false;
649 
650 	if (adapter->have_msi)
651 		flags &= ~IRQF_SHARED;
652 
653 	return request_irq(adapter->pdev->irq, atl2_intr, flags, netdev->name,
654 		netdev);
655 }
656 
657 /**
658  * atl2_free_ring_resources - Free Tx / RX descriptor Resources
659  * @adapter: board private structure
660  *
661  * Free all transmit software resources
662  */
663 static void atl2_free_ring_resources(struct atl2_adapter *adapter)
664 {
665 	struct pci_dev *pdev = adapter->pdev;
666 	dma_free_coherent(&pdev->dev, adapter->ring_size,
667 			  adapter->ring_vir_addr, adapter->ring_dma);
668 }
669 
670 /**
671  * atl2_open - Called when a network interface is made active
672  * @netdev: network interface device structure
673  *
674  * Returns 0 on success, negative value on failure
675  *
676  * The open entry point is called when a network interface is made
677  * active by the system (IFF_UP).  At this point all resources needed
678  * for transmit and receive operations are allocated, the interrupt
679  * handler is registered with the OS, the watchdog timer is started,
680  * and the stack is notified that the interface is ready.
681  */
682 static int atl2_open(struct net_device *netdev)
683 {
684 	struct atl2_adapter *adapter = netdev_priv(netdev);
685 	int err;
686 	u32 val;
687 
688 	/* disallow open during test */
689 	if (test_bit(__ATL2_TESTING, &adapter->flags))
690 		return -EBUSY;
691 
692 	/* allocate transmit descriptors */
693 	err = atl2_setup_ring_resources(adapter);
694 	if (err)
695 		return err;
696 
697 	err = atl2_init_hw(&adapter->hw);
698 	if (err) {
699 		err = -EIO;
700 		goto err_init_hw;
701 	}
702 
703 	/* hardware has been reset, we need to reload some things */
704 	atl2_set_multi(netdev);
705 	init_ring_ptrs(adapter);
706 
707 	atl2_restore_vlan(adapter);
708 
709 	if (atl2_configure(adapter)) {
710 		err = -EIO;
711 		goto err_config;
712 	}
713 
714 	err = atl2_request_irq(adapter);
715 	if (err)
716 		goto err_req_irq;
717 
718 	clear_bit(__ATL2_DOWN, &adapter->flags);
719 
720 	mod_timer(&adapter->watchdog_timer, round_jiffies(jiffies + 4*HZ));
721 
722 	val = ATL2_READ_REG(&adapter->hw, REG_MASTER_CTRL);
723 	ATL2_WRITE_REG(&adapter->hw, REG_MASTER_CTRL,
724 		val | MASTER_CTRL_MANUAL_INT);
725 
726 	atl2_irq_enable(adapter);
727 
728 	return 0;
729 
730 err_init_hw:
731 err_req_irq:
732 err_config:
733 	atl2_free_ring_resources(adapter);
734 	atl2_reset_hw(&adapter->hw);
735 
736 	return err;
737 }
738 
739 static void atl2_down(struct atl2_adapter *adapter)
740 {
741 	struct net_device *netdev = adapter->netdev;
742 
743 	/* signal that we're down so the interrupt handler does not
744 	 * reschedule our watchdog timer */
745 	set_bit(__ATL2_DOWN, &adapter->flags);
746 
747 	netif_tx_disable(netdev);
748 
749 	/* reset MAC to disable all RX/TX */
750 	atl2_reset_hw(&adapter->hw);
751 	msleep(1);
752 
753 	atl2_irq_disable(adapter);
754 
755 	del_timer_sync(&adapter->watchdog_timer);
756 	del_timer_sync(&adapter->phy_config_timer);
757 	clear_bit(0, &adapter->cfg_phy);
758 
759 	netif_carrier_off(netdev);
760 	adapter->link_speed = SPEED_0;
761 	adapter->link_duplex = -1;
762 }
763 
764 static void atl2_free_irq(struct atl2_adapter *adapter)
765 {
766 	struct net_device *netdev = adapter->netdev;
767 
768 	free_irq(adapter->pdev->irq, netdev);
769 
770 #ifdef CONFIG_PCI_MSI
771 	if (adapter->have_msi)
772 		pci_disable_msi(adapter->pdev);
773 #endif
774 }
775 
776 /**
777  * atl2_close - Disables a network interface
778  * @netdev: network interface device structure
779  *
780  * Returns 0, this is not allowed to fail
781  *
782  * The close entry point is called when an interface is de-activated
783  * by the OS.  The hardware is still under the drivers control, but
784  * needs to be disabled.  A global MAC reset is issued to stop the
785  * hardware, and all transmit and receive resources are freed.
786  */
787 static int atl2_close(struct net_device *netdev)
788 {
789 	struct atl2_adapter *adapter = netdev_priv(netdev);
790 
791 	WARN_ON(test_bit(__ATL2_RESETTING, &adapter->flags));
792 
793 	atl2_down(adapter);
794 	atl2_free_irq(adapter);
795 	atl2_free_ring_resources(adapter);
796 
797 	return 0;
798 }
799 
800 static inline int TxsFreeUnit(struct atl2_adapter *adapter)
801 {
802 	u32 txs_write_ptr = (u32) atomic_read(&adapter->txs_write_ptr);
803 
804 	return (adapter->txs_next_clear >= txs_write_ptr) ?
805 		(int) (adapter->txs_ring_size - adapter->txs_next_clear +
806 		txs_write_ptr - 1) :
807 		(int) (txs_write_ptr - adapter->txs_next_clear - 1);
808 }
809 
810 static inline int TxdFreeBytes(struct atl2_adapter *adapter)
811 {
812 	u32 txd_read_ptr = (u32)atomic_read(&adapter->txd_read_ptr);
813 
814 	return (adapter->txd_write_ptr >= txd_read_ptr) ?
815 		(int) (adapter->txd_ring_size - adapter->txd_write_ptr +
816 		txd_read_ptr - 1) :
817 		(int) (txd_read_ptr - adapter->txd_write_ptr - 1);
818 }
819 
820 static netdev_tx_t atl2_xmit_frame(struct sk_buff *skb,
821 					 struct net_device *netdev)
822 {
823 	struct atl2_adapter *adapter = netdev_priv(netdev);
824 	struct tx_pkt_header *txph;
825 	u32 offset, copy_len;
826 	int txs_unused;
827 	int txbuf_unused;
828 
829 	if (test_bit(__ATL2_DOWN, &adapter->flags)) {
830 		dev_kfree_skb_any(skb);
831 		return NETDEV_TX_OK;
832 	}
833 
834 	if (unlikely(skb->len <= 0)) {
835 		dev_kfree_skb_any(skb);
836 		return NETDEV_TX_OK;
837 	}
838 
839 	txs_unused = TxsFreeUnit(adapter);
840 	txbuf_unused = TxdFreeBytes(adapter);
841 
842 	if (skb->len + sizeof(struct tx_pkt_header) + 4  > txbuf_unused ||
843 		txs_unused < 1) {
844 		/* not enough resources */
845 		netif_stop_queue(netdev);
846 		return NETDEV_TX_BUSY;
847 	}
848 
849 	offset = adapter->txd_write_ptr;
850 
851 	txph = (struct tx_pkt_header *) (((u8 *)adapter->txd_ring) + offset);
852 
853 	*(u32 *)txph = 0;
854 	txph->pkt_size = skb->len;
855 
856 	offset += 4;
857 	if (offset >= adapter->txd_ring_size)
858 		offset -= adapter->txd_ring_size;
859 	copy_len = adapter->txd_ring_size - offset;
860 	if (copy_len >= skb->len) {
861 		memcpy(((u8 *)adapter->txd_ring) + offset, skb->data, skb->len);
862 		offset += ((u32)(skb->len + 3) & ~3);
863 	} else {
864 		memcpy(((u8 *)adapter->txd_ring)+offset, skb->data, copy_len);
865 		memcpy((u8 *)adapter->txd_ring, skb->data+copy_len,
866 			skb->len-copy_len);
867 		offset = ((u32)(skb->len-copy_len + 3) & ~3);
868 	}
869 #ifdef NETIF_F_HW_VLAN_CTAG_TX
870 	if (skb_vlan_tag_present(skb)) {
871 		u16 vlan_tag = skb_vlan_tag_get(skb);
872 		vlan_tag = (vlan_tag << 4) |
873 			(vlan_tag >> 13) |
874 			((vlan_tag >> 9) & 0x8);
875 		txph->ins_vlan = 1;
876 		txph->vlan = vlan_tag;
877 	}
878 #endif
879 	if (offset >= adapter->txd_ring_size)
880 		offset -= adapter->txd_ring_size;
881 	adapter->txd_write_ptr = offset;
882 
883 	/* clear txs before send */
884 	adapter->txs_ring[adapter->txs_next_clear].update = 0;
885 	if (++adapter->txs_next_clear == adapter->txs_ring_size)
886 		adapter->txs_next_clear = 0;
887 
888 	ATL2_WRITE_REGW(&adapter->hw, REG_MB_TXD_WR_IDX,
889 		(adapter->txd_write_ptr >> 2));
890 
891 	dev_consume_skb_any(skb);
892 	return NETDEV_TX_OK;
893 }
894 
895 /**
896  * atl2_change_mtu - Change the Maximum Transfer Unit
897  * @netdev: network interface device structure
898  * @new_mtu: new value for maximum frame size
899  *
900  * Returns 0 on success, negative on failure
901  */
902 static int atl2_change_mtu(struct net_device *netdev, int new_mtu)
903 {
904 	struct atl2_adapter *adapter = netdev_priv(netdev);
905 	struct atl2_hw *hw = &adapter->hw;
906 
907 	/* set MTU */
908 	netdev->mtu = new_mtu;
909 	hw->max_frame_size = new_mtu;
910 	ATL2_WRITE_REG(hw, REG_MTU, new_mtu + ETH_HLEN +
911 		       VLAN_HLEN + ETH_FCS_LEN);
912 
913 	return 0;
914 }
915 
916 /**
917  * atl2_set_mac - Change the Ethernet Address of the NIC
918  * @netdev: network interface device structure
919  * @p: pointer to an address structure
920  *
921  * Returns 0 on success, negative on failure
922  */
923 static int atl2_set_mac(struct net_device *netdev, void *p)
924 {
925 	struct atl2_adapter *adapter = netdev_priv(netdev);
926 	struct sockaddr *addr = p;
927 
928 	if (!is_valid_ether_addr(addr->sa_data))
929 		return -EADDRNOTAVAIL;
930 
931 	if (netif_running(netdev))
932 		return -EBUSY;
933 
934 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
935 	memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
936 
937 	atl2_set_mac_addr(&adapter->hw);
938 
939 	return 0;
940 }
941 
942 static int atl2_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
943 {
944 	struct atl2_adapter *adapter = netdev_priv(netdev);
945 	struct mii_ioctl_data *data = if_mii(ifr);
946 	unsigned long flags;
947 
948 	switch (cmd) {
949 	case SIOCGMIIPHY:
950 		data->phy_id = 0;
951 		break;
952 	case SIOCGMIIREG:
953 		spin_lock_irqsave(&adapter->stats_lock, flags);
954 		if (atl2_read_phy_reg(&adapter->hw,
955 			data->reg_num & 0x1F, &data->val_out)) {
956 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
957 			return -EIO;
958 		}
959 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
960 		break;
961 	case SIOCSMIIREG:
962 		if (data->reg_num & ~(0x1F))
963 			return -EFAULT;
964 		spin_lock_irqsave(&adapter->stats_lock, flags);
965 		if (atl2_write_phy_reg(&adapter->hw, data->reg_num,
966 			data->val_in)) {
967 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
968 			return -EIO;
969 		}
970 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
971 		break;
972 	default:
973 		return -EOPNOTSUPP;
974 	}
975 	return 0;
976 }
977 
978 static int atl2_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
979 {
980 	switch (cmd) {
981 	case SIOCGMIIPHY:
982 	case SIOCGMIIREG:
983 	case SIOCSMIIREG:
984 		return atl2_mii_ioctl(netdev, ifr, cmd);
985 #ifdef ETHTOOL_OPS_COMPAT
986 	case SIOCETHTOOL:
987 		return ethtool_ioctl(ifr);
988 #endif
989 	default:
990 		return -EOPNOTSUPP;
991 	}
992 }
993 
994 /**
995  * atl2_tx_timeout - Respond to a Tx Hang
996  * @netdev: network interface device structure
997  * @txqueue: index of the hanging transmit queue
998  */
999 static void atl2_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1000 {
1001 	struct atl2_adapter *adapter = netdev_priv(netdev);
1002 
1003 	/* Do the reset outside of interrupt context */
1004 	schedule_work(&adapter->reset_task);
1005 }
1006 
1007 /**
1008  * atl2_watchdog - Timer Call-back
1009  * @t: timer list containing a pointer to netdev cast into an unsigned long
1010  */
1011 static void atl2_watchdog(struct timer_list *t)
1012 {
1013 	struct atl2_adapter *adapter = from_timer(adapter, t, watchdog_timer);
1014 
1015 	if (!test_bit(__ATL2_DOWN, &adapter->flags)) {
1016 		u32 drop_rxd, drop_rxs;
1017 		unsigned long flags;
1018 
1019 		spin_lock_irqsave(&adapter->stats_lock, flags);
1020 		drop_rxd = ATL2_READ_REG(&adapter->hw, REG_STS_RXD_OV);
1021 		drop_rxs = ATL2_READ_REG(&adapter->hw, REG_STS_RXS_OV);
1022 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
1023 
1024 		adapter->netdev->stats.rx_over_errors += drop_rxd + drop_rxs;
1025 
1026 		/* Reset the timer */
1027 		mod_timer(&adapter->watchdog_timer,
1028 			  round_jiffies(jiffies + 4 * HZ));
1029 	}
1030 }
1031 
1032 /**
1033  * atl2_phy_config - Timer Call-back
1034  * @t: timer list containing a pointer to netdev cast into an unsigned long
1035  */
1036 static void atl2_phy_config(struct timer_list *t)
1037 {
1038 	struct atl2_adapter *adapter = from_timer(adapter, t,
1039 						  phy_config_timer);
1040 	struct atl2_hw *hw = &adapter->hw;
1041 	unsigned long flags;
1042 
1043 	spin_lock_irqsave(&adapter->stats_lock, flags);
1044 	atl2_write_phy_reg(hw, MII_ADVERTISE, hw->mii_autoneg_adv_reg);
1045 	atl2_write_phy_reg(hw, MII_BMCR, MII_CR_RESET | MII_CR_AUTO_NEG_EN |
1046 		MII_CR_RESTART_AUTO_NEG);
1047 	spin_unlock_irqrestore(&adapter->stats_lock, flags);
1048 	clear_bit(0, &adapter->cfg_phy);
1049 }
1050 
1051 static int atl2_up(struct atl2_adapter *adapter)
1052 {
1053 	struct net_device *netdev = adapter->netdev;
1054 	int err = 0;
1055 	u32 val;
1056 
1057 	/* hardware has been reset, we need to reload some things */
1058 
1059 	err = atl2_init_hw(&adapter->hw);
1060 	if (err) {
1061 		err = -EIO;
1062 		return err;
1063 	}
1064 
1065 	atl2_set_multi(netdev);
1066 	init_ring_ptrs(adapter);
1067 
1068 	atl2_restore_vlan(adapter);
1069 
1070 	if (atl2_configure(adapter)) {
1071 		err = -EIO;
1072 		goto err_up;
1073 	}
1074 
1075 	clear_bit(__ATL2_DOWN, &adapter->flags);
1076 
1077 	val = ATL2_READ_REG(&adapter->hw, REG_MASTER_CTRL);
1078 	ATL2_WRITE_REG(&adapter->hw, REG_MASTER_CTRL, val |
1079 		MASTER_CTRL_MANUAL_INT);
1080 
1081 	atl2_irq_enable(adapter);
1082 
1083 err_up:
1084 	return err;
1085 }
1086 
1087 static void atl2_reinit_locked(struct atl2_adapter *adapter)
1088 {
1089 	while (test_and_set_bit(__ATL2_RESETTING, &adapter->flags))
1090 		msleep(1);
1091 	atl2_down(adapter);
1092 	atl2_up(adapter);
1093 	clear_bit(__ATL2_RESETTING, &adapter->flags);
1094 }
1095 
1096 static void atl2_reset_task(struct work_struct *work)
1097 {
1098 	struct atl2_adapter *adapter;
1099 	adapter = container_of(work, struct atl2_adapter, reset_task);
1100 
1101 	atl2_reinit_locked(adapter);
1102 }
1103 
1104 static void atl2_setup_mac_ctrl(struct atl2_adapter *adapter)
1105 {
1106 	u32 value;
1107 	struct atl2_hw *hw = &adapter->hw;
1108 	struct net_device *netdev = adapter->netdev;
1109 
1110 	/* Config MAC CTRL Register */
1111 	value = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN | MAC_CTRL_MACLP_CLK_PHY;
1112 
1113 	/* duplex */
1114 	if (FULL_DUPLEX == adapter->link_duplex)
1115 		value |= MAC_CTRL_DUPLX;
1116 
1117 	/* flow control */
1118 	value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1119 
1120 	/* PAD & CRC */
1121 	value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1122 
1123 	/* preamble length */
1124 	value |= (((u32)adapter->hw.preamble_len & MAC_CTRL_PRMLEN_MASK) <<
1125 		MAC_CTRL_PRMLEN_SHIFT);
1126 
1127 	/* vlan */
1128 	__atl2_vlan_mode(netdev->features, &value);
1129 
1130 	/* filter mode */
1131 	value |= MAC_CTRL_BC_EN;
1132 	if (netdev->flags & IFF_PROMISC)
1133 		value |= MAC_CTRL_PROMIS_EN;
1134 	else if (netdev->flags & IFF_ALLMULTI)
1135 		value |= MAC_CTRL_MC_ALL_EN;
1136 
1137 	/* half retry buffer */
1138 	value |= (((u32)(adapter->hw.retry_buf &
1139 		MAC_CTRL_HALF_LEFT_BUF_MASK)) << MAC_CTRL_HALF_LEFT_BUF_SHIFT);
1140 
1141 	ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1142 }
1143 
1144 static int atl2_check_link(struct atl2_adapter *adapter)
1145 {
1146 	struct atl2_hw *hw = &adapter->hw;
1147 	struct net_device *netdev = adapter->netdev;
1148 	int ret_val;
1149 	u16 speed, duplex, phy_data;
1150 	int reconfig = 0;
1151 
1152 	/* MII_BMSR must read twise */
1153 	atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1154 	atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1155 	if (!(phy_data&BMSR_LSTATUS)) { /* link down */
1156 		if (netif_carrier_ok(netdev)) { /* old link state: Up */
1157 			u32 value;
1158 			/* disable rx */
1159 			value = ATL2_READ_REG(hw, REG_MAC_CTRL);
1160 			value &= ~MAC_CTRL_RX_EN;
1161 			ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1162 			adapter->link_speed = SPEED_0;
1163 			netif_carrier_off(netdev);
1164 			netif_stop_queue(netdev);
1165 		}
1166 		return 0;
1167 	}
1168 
1169 	/* Link Up */
1170 	ret_val = atl2_get_speed_and_duplex(hw, &speed, &duplex);
1171 	if (ret_val)
1172 		return ret_val;
1173 	switch (hw->MediaType) {
1174 	case MEDIA_TYPE_100M_FULL:
1175 		if (speed  != SPEED_100 || duplex != FULL_DUPLEX)
1176 			reconfig = 1;
1177 		break;
1178 	case MEDIA_TYPE_100M_HALF:
1179 		if (speed  != SPEED_100 || duplex != HALF_DUPLEX)
1180 			reconfig = 1;
1181 		break;
1182 	case MEDIA_TYPE_10M_FULL:
1183 		if (speed != SPEED_10 || duplex != FULL_DUPLEX)
1184 			reconfig = 1;
1185 		break;
1186 	case MEDIA_TYPE_10M_HALF:
1187 		if (speed  != SPEED_10 || duplex != HALF_DUPLEX)
1188 			reconfig = 1;
1189 		break;
1190 	}
1191 	/* link result is our setting */
1192 	if (reconfig == 0) {
1193 		if (adapter->link_speed != speed ||
1194 			adapter->link_duplex != duplex) {
1195 			adapter->link_speed = speed;
1196 			adapter->link_duplex = duplex;
1197 			atl2_setup_mac_ctrl(adapter);
1198 			printk(KERN_INFO "%s: %s NIC Link is Up<%d Mbps %s>\n",
1199 				atl2_driver_name, netdev->name,
1200 				adapter->link_speed,
1201 				adapter->link_duplex == FULL_DUPLEX ?
1202 					"Full Duplex" : "Half Duplex");
1203 		}
1204 
1205 		if (!netif_carrier_ok(netdev)) { /* Link down -> Up */
1206 			netif_carrier_on(netdev);
1207 			netif_wake_queue(netdev);
1208 		}
1209 		return 0;
1210 	}
1211 
1212 	/* change original link status */
1213 	if (netif_carrier_ok(netdev)) {
1214 		u32 value;
1215 		/* disable rx */
1216 		value = ATL2_READ_REG(hw, REG_MAC_CTRL);
1217 		value &= ~MAC_CTRL_RX_EN;
1218 		ATL2_WRITE_REG(hw, REG_MAC_CTRL, value);
1219 
1220 		adapter->link_speed = SPEED_0;
1221 		netif_carrier_off(netdev);
1222 		netif_stop_queue(netdev);
1223 	}
1224 
1225 	/* auto-neg, insert timer to re-config phy
1226 	 * (if interval smaller than 5 seconds, something strange) */
1227 	if (!test_bit(__ATL2_DOWN, &adapter->flags)) {
1228 		if (!test_and_set_bit(0, &adapter->cfg_phy))
1229 			mod_timer(&adapter->phy_config_timer,
1230 				  round_jiffies(jiffies + 5 * HZ));
1231 	}
1232 
1233 	return 0;
1234 }
1235 
1236 /**
1237  * atl2_link_chg_task - deal with link change event Out of interrupt context
1238  * @work: pointer to work struct with private info
1239  */
1240 static void atl2_link_chg_task(struct work_struct *work)
1241 {
1242 	struct atl2_adapter *adapter;
1243 	unsigned long flags;
1244 
1245 	adapter = container_of(work, struct atl2_adapter, link_chg_task);
1246 
1247 	spin_lock_irqsave(&adapter->stats_lock, flags);
1248 	atl2_check_link(adapter);
1249 	spin_unlock_irqrestore(&adapter->stats_lock, flags);
1250 }
1251 
1252 static void atl2_setup_pcicmd(struct pci_dev *pdev)
1253 {
1254 	u16 cmd;
1255 
1256 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1257 
1258 	if (cmd & PCI_COMMAND_INTX_DISABLE)
1259 		cmd &= ~PCI_COMMAND_INTX_DISABLE;
1260 	if (cmd & PCI_COMMAND_IO)
1261 		cmd &= ~PCI_COMMAND_IO;
1262 	if (0 == (cmd & PCI_COMMAND_MEMORY))
1263 		cmd |= PCI_COMMAND_MEMORY;
1264 	if (0 == (cmd & PCI_COMMAND_MASTER))
1265 		cmd |= PCI_COMMAND_MASTER;
1266 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
1267 
1268 	/*
1269 	 * some motherboards BIOS(PXE/EFI) driver may set PME
1270 	 * while they transfer control to OS (Windows/Linux)
1271 	 * so we should clear this bit before NIC work normally
1272 	 */
1273 	pci_write_config_dword(pdev, REG_PM_CTRLSTAT, 0);
1274 }
1275 
1276 #ifdef CONFIG_NET_POLL_CONTROLLER
1277 static void atl2_poll_controller(struct net_device *netdev)
1278 {
1279 	disable_irq(netdev->irq);
1280 	atl2_intr(netdev->irq, netdev);
1281 	enable_irq(netdev->irq);
1282 }
1283 #endif
1284 
1285 
1286 static const struct net_device_ops atl2_netdev_ops = {
1287 	.ndo_open		= atl2_open,
1288 	.ndo_stop		= atl2_close,
1289 	.ndo_start_xmit		= atl2_xmit_frame,
1290 	.ndo_set_rx_mode	= atl2_set_multi,
1291 	.ndo_validate_addr	= eth_validate_addr,
1292 	.ndo_set_mac_address	= atl2_set_mac,
1293 	.ndo_change_mtu		= atl2_change_mtu,
1294 	.ndo_fix_features	= atl2_fix_features,
1295 	.ndo_set_features	= atl2_set_features,
1296 	.ndo_do_ioctl		= atl2_ioctl,
1297 	.ndo_tx_timeout		= atl2_tx_timeout,
1298 #ifdef CONFIG_NET_POLL_CONTROLLER
1299 	.ndo_poll_controller	= atl2_poll_controller,
1300 #endif
1301 };
1302 
1303 /**
1304  * atl2_probe - Device Initialization Routine
1305  * @pdev: PCI device information struct
1306  * @ent: entry in atl2_pci_tbl
1307  *
1308  * Returns 0 on success, negative on failure
1309  *
1310  * atl2_probe initializes an adapter identified by a pci_dev structure.
1311  * The OS initialization, configuring of the adapter private structure,
1312  * and a hardware reset occur.
1313  */
1314 static int atl2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1315 {
1316 	struct net_device *netdev;
1317 	struct atl2_adapter *adapter;
1318 	static int cards_found = 0;
1319 	unsigned long mmio_start;
1320 	int mmio_len;
1321 	int err;
1322 
1323 	err = pci_enable_device(pdev);
1324 	if (err)
1325 		return err;
1326 
1327 	/*
1328 	 * atl2 is a shared-high-32-bit device, so we're stuck with 32-bit DMA
1329 	 * until the kernel has the proper infrastructure to support 64-bit DMA
1330 	 * on these devices.
1331 	 */
1332 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)) &&
1333 	    dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1334 		printk(KERN_ERR "atl2: No usable DMA configuration, aborting\n");
1335 		err = -EIO;
1336 		goto err_dma;
1337 	}
1338 
1339 	/* Mark all PCI regions associated with PCI device
1340 	 * pdev as being reserved by owner atl2_driver_name */
1341 	err = pci_request_regions(pdev, atl2_driver_name);
1342 	if (err)
1343 		goto err_pci_reg;
1344 
1345 	/* Enables bus-mastering on the device and calls
1346 	 * pcibios_set_master to do the needed arch specific settings */
1347 	pci_set_master(pdev);
1348 
1349 	netdev = alloc_etherdev(sizeof(struct atl2_adapter));
1350 	if (!netdev) {
1351 		err = -ENOMEM;
1352 		goto err_alloc_etherdev;
1353 	}
1354 
1355 	SET_NETDEV_DEV(netdev, &pdev->dev);
1356 
1357 	pci_set_drvdata(pdev, netdev);
1358 	adapter = netdev_priv(netdev);
1359 	adapter->netdev = netdev;
1360 	adapter->pdev = pdev;
1361 	adapter->hw.back = adapter;
1362 
1363 	mmio_start = pci_resource_start(pdev, 0x0);
1364 	mmio_len = pci_resource_len(pdev, 0x0);
1365 
1366 	adapter->hw.mem_rang = (u32)mmio_len;
1367 	adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
1368 	if (!adapter->hw.hw_addr) {
1369 		err = -EIO;
1370 		goto err_ioremap;
1371 	}
1372 
1373 	atl2_setup_pcicmd(pdev);
1374 
1375 	netdev->netdev_ops = &atl2_netdev_ops;
1376 	netdev->ethtool_ops = &atl2_ethtool_ops;
1377 	netdev->watchdog_timeo = 5 * HZ;
1378 	netdev->min_mtu = 40;
1379 	netdev->max_mtu = ETH_DATA_LEN + VLAN_HLEN;
1380 	strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1381 
1382 	netdev->mem_start = mmio_start;
1383 	netdev->mem_end = mmio_start + mmio_len;
1384 	adapter->bd_number = cards_found;
1385 	adapter->pci_using_64 = false;
1386 
1387 	/* setup the private structure */
1388 	err = atl2_sw_init(adapter);
1389 	if (err)
1390 		goto err_sw_init;
1391 
1392 	netdev->hw_features = NETIF_F_HW_VLAN_CTAG_RX;
1393 	netdev->features |= (NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
1394 
1395 	/* Init PHY as early as possible due to power saving issue  */
1396 	atl2_phy_init(&adapter->hw);
1397 
1398 	/* reset the controller to
1399 	 * put the device in a known good starting state */
1400 
1401 	if (atl2_reset_hw(&adapter->hw)) {
1402 		err = -EIO;
1403 		goto err_reset;
1404 	}
1405 
1406 	/* copy the MAC address out of the EEPROM */
1407 	atl2_read_mac_addr(&adapter->hw);
1408 	memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
1409 	if (!is_valid_ether_addr(netdev->dev_addr)) {
1410 		err = -EIO;
1411 		goto err_eeprom;
1412 	}
1413 
1414 	atl2_check_options(adapter);
1415 
1416 	timer_setup(&adapter->watchdog_timer, atl2_watchdog, 0);
1417 
1418 	timer_setup(&adapter->phy_config_timer, atl2_phy_config, 0);
1419 
1420 	INIT_WORK(&adapter->reset_task, atl2_reset_task);
1421 	INIT_WORK(&adapter->link_chg_task, atl2_link_chg_task);
1422 
1423 	strcpy(netdev->name, "eth%d"); /* ?? */
1424 	err = register_netdev(netdev);
1425 	if (err)
1426 		goto err_register;
1427 
1428 	/* assume we have no link for now */
1429 	netif_carrier_off(netdev);
1430 	netif_stop_queue(netdev);
1431 
1432 	cards_found++;
1433 
1434 	return 0;
1435 
1436 err_reset:
1437 err_register:
1438 err_sw_init:
1439 err_eeprom:
1440 	iounmap(adapter->hw.hw_addr);
1441 err_ioremap:
1442 	free_netdev(netdev);
1443 err_alloc_etherdev:
1444 	pci_release_regions(pdev);
1445 err_pci_reg:
1446 err_dma:
1447 	pci_disable_device(pdev);
1448 	return err;
1449 }
1450 
1451 /**
1452  * atl2_remove - Device Removal Routine
1453  * @pdev: PCI device information struct
1454  *
1455  * atl2_remove is called by the PCI subsystem to alert the driver
1456  * that it should release a PCI device.  The could be caused by a
1457  * Hot-Plug event, or because the driver is going to be removed from
1458  * memory.
1459  */
1460 /* FIXME: write the original MAC address back in case it was changed from a
1461  * BIOS-set value, as in atl1 -- CHS */
1462 static void atl2_remove(struct pci_dev *pdev)
1463 {
1464 	struct net_device *netdev = pci_get_drvdata(pdev);
1465 	struct atl2_adapter *adapter = netdev_priv(netdev);
1466 
1467 	/* flush_scheduled work may reschedule our watchdog task, so
1468 	 * explicitly disable watchdog tasks from being rescheduled  */
1469 	set_bit(__ATL2_DOWN, &adapter->flags);
1470 
1471 	del_timer_sync(&adapter->watchdog_timer);
1472 	del_timer_sync(&adapter->phy_config_timer);
1473 	cancel_work_sync(&adapter->reset_task);
1474 	cancel_work_sync(&adapter->link_chg_task);
1475 
1476 	unregister_netdev(netdev);
1477 
1478 	atl2_force_ps(&adapter->hw);
1479 
1480 	iounmap(adapter->hw.hw_addr);
1481 	pci_release_regions(pdev);
1482 
1483 	free_netdev(netdev);
1484 
1485 	pci_disable_device(pdev);
1486 }
1487 
1488 static int atl2_suspend(struct pci_dev *pdev, pm_message_t state)
1489 {
1490 	struct net_device *netdev = pci_get_drvdata(pdev);
1491 	struct atl2_adapter *adapter = netdev_priv(netdev);
1492 	struct atl2_hw *hw = &adapter->hw;
1493 	u16 speed, duplex;
1494 	u32 ctrl = 0;
1495 	u32 wufc = adapter->wol;
1496 
1497 #ifdef CONFIG_PM
1498 	int retval = 0;
1499 #endif
1500 
1501 	netif_device_detach(netdev);
1502 
1503 	if (netif_running(netdev)) {
1504 		WARN_ON(test_bit(__ATL2_RESETTING, &adapter->flags));
1505 		atl2_down(adapter);
1506 	}
1507 
1508 #ifdef CONFIG_PM
1509 	retval = pci_save_state(pdev);
1510 	if (retval)
1511 		return retval;
1512 #endif
1513 
1514 	atl2_read_phy_reg(hw, MII_BMSR, (u16 *)&ctrl);
1515 	atl2_read_phy_reg(hw, MII_BMSR, (u16 *)&ctrl);
1516 	if (ctrl & BMSR_LSTATUS)
1517 		wufc &= ~ATLX_WUFC_LNKC;
1518 
1519 	if (0 != (ctrl & BMSR_LSTATUS) && 0 != wufc) {
1520 		u32 ret_val;
1521 		/* get current link speed & duplex */
1522 		ret_val = atl2_get_speed_and_duplex(hw, &speed, &duplex);
1523 		if (ret_val) {
1524 			printk(KERN_DEBUG
1525 				"%s: get speed&duplex error while suspend\n",
1526 				atl2_driver_name);
1527 			goto wol_dis;
1528 		}
1529 
1530 		ctrl = 0;
1531 
1532 		/* turn on magic packet wol */
1533 		if (wufc & ATLX_WUFC_MAG)
1534 			ctrl |= (WOL_MAGIC_EN | WOL_MAGIC_PME_EN);
1535 
1536 		/* ignore Link Chg event when Link is up */
1537 		ATL2_WRITE_REG(hw, REG_WOL_CTRL, ctrl);
1538 
1539 		/* Config MAC CTRL Register */
1540 		ctrl = MAC_CTRL_RX_EN | MAC_CTRL_MACLP_CLK_PHY;
1541 		if (FULL_DUPLEX == adapter->link_duplex)
1542 			ctrl |= MAC_CTRL_DUPLX;
1543 		ctrl |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1544 		ctrl |= (((u32)adapter->hw.preamble_len &
1545 			MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
1546 		ctrl |= (((u32)(adapter->hw.retry_buf &
1547 			MAC_CTRL_HALF_LEFT_BUF_MASK)) <<
1548 			MAC_CTRL_HALF_LEFT_BUF_SHIFT);
1549 		if (wufc & ATLX_WUFC_MAG) {
1550 			/* magic packet maybe Broadcast&multicast&Unicast */
1551 			ctrl |= MAC_CTRL_BC_EN;
1552 		}
1553 
1554 		ATL2_WRITE_REG(hw, REG_MAC_CTRL, ctrl);
1555 
1556 		/* pcie patch */
1557 		ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1558 		ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1559 		ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1560 		ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1561 		ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1562 		ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1563 
1564 		pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
1565 		goto suspend_exit;
1566 	}
1567 
1568 	if (0 == (ctrl&BMSR_LSTATUS) && 0 != (wufc&ATLX_WUFC_LNKC)) {
1569 		/* link is down, so only LINK CHG WOL event enable */
1570 		ctrl |= (WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN);
1571 		ATL2_WRITE_REG(hw, REG_WOL_CTRL, ctrl);
1572 		ATL2_WRITE_REG(hw, REG_MAC_CTRL, 0);
1573 
1574 		/* pcie patch */
1575 		ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1576 		ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1577 		ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1578 		ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1579 		ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1580 		ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1581 
1582 		hw->phy_configured = false; /* re-init PHY when resume */
1583 
1584 		pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
1585 
1586 		goto suspend_exit;
1587 	}
1588 
1589 wol_dis:
1590 	/* WOL disabled */
1591 	ATL2_WRITE_REG(hw, REG_WOL_CTRL, 0);
1592 
1593 	/* pcie patch */
1594 	ctrl = ATL2_READ_REG(hw, REG_PCIE_PHYMISC);
1595 	ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
1596 	ATL2_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
1597 	ctrl = ATL2_READ_REG(hw, REG_PCIE_DLL_TX_CTRL1);
1598 	ctrl |= PCIE_DLL_TX_CTRL1_SEL_NOR_CLK;
1599 	ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, ctrl);
1600 
1601 	atl2_force_ps(hw);
1602 	hw->phy_configured = false; /* re-init PHY when resume */
1603 
1604 	pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1605 
1606 suspend_exit:
1607 	if (netif_running(netdev))
1608 		atl2_free_irq(adapter);
1609 
1610 	pci_disable_device(pdev);
1611 
1612 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
1613 
1614 	return 0;
1615 }
1616 
1617 #ifdef CONFIG_PM
1618 static int atl2_resume(struct pci_dev *pdev)
1619 {
1620 	struct net_device *netdev = pci_get_drvdata(pdev);
1621 	struct atl2_adapter *adapter = netdev_priv(netdev);
1622 	u32 err;
1623 
1624 	pci_set_power_state(pdev, PCI_D0);
1625 	pci_restore_state(pdev);
1626 
1627 	err = pci_enable_device(pdev);
1628 	if (err) {
1629 		printk(KERN_ERR
1630 			"atl2: Cannot enable PCI device from suspend\n");
1631 		return err;
1632 	}
1633 
1634 	pci_set_master(pdev);
1635 
1636 	ATL2_READ_REG(&adapter->hw, REG_WOL_CTRL); /* clear WOL status */
1637 
1638 	pci_enable_wake(pdev, PCI_D3hot, 0);
1639 	pci_enable_wake(pdev, PCI_D3cold, 0);
1640 
1641 	ATL2_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
1642 
1643 	if (netif_running(netdev)) {
1644 		err = atl2_request_irq(adapter);
1645 		if (err)
1646 			return err;
1647 	}
1648 
1649 	atl2_reset_hw(&adapter->hw);
1650 
1651 	if (netif_running(netdev))
1652 		atl2_up(adapter);
1653 
1654 	netif_device_attach(netdev);
1655 
1656 	return 0;
1657 }
1658 #endif
1659 
1660 static void atl2_shutdown(struct pci_dev *pdev)
1661 {
1662 	atl2_suspend(pdev, PMSG_SUSPEND);
1663 }
1664 
1665 static struct pci_driver atl2_driver = {
1666 	.name     = atl2_driver_name,
1667 	.id_table = atl2_pci_tbl,
1668 	.probe    = atl2_probe,
1669 	.remove   = atl2_remove,
1670 	/* Power Management Hooks */
1671 	.suspend  = atl2_suspend,
1672 #ifdef CONFIG_PM
1673 	.resume   = atl2_resume,
1674 #endif
1675 	.shutdown = atl2_shutdown,
1676 };
1677 
1678 /**
1679  * atl2_init_module - Driver Registration Routine
1680  *
1681  * atl2_init_module is the first routine called when the driver is
1682  * loaded. All it does is register with the PCI subsystem.
1683  */
1684 static int __init atl2_init_module(void)
1685 {
1686 	return pci_register_driver(&atl2_driver);
1687 }
1688 module_init(atl2_init_module);
1689 
1690 /**
1691  * atl2_exit_module - Driver Exit Cleanup Routine
1692  *
1693  * atl2_exit_module is called just before the driver is removed
1694  * from memory.
1695  */
1696 static void __exit atl2_exit_module(void)
1697 {
1698 	pci_unregister_driver(&atl2_driver);
1699 }
1700 module_exit(atl2_exit_module);
1701 
1702 static void atl2_read_pci_cfg(struct atl2_hw *hw, u32 reg, u16 *value)
1703 {
1704 	struct atl2_adapter *adapter = hw->back;
1705 	pci_read_config_word(adapter->pdev, reg, value);
1706 }
1707 
1708 static void atl2_write_pci_cfg(struct atl2_hw *hw, u32 reg, u16 *value)
1709 {
1710 	struct atl2_adapter *adapter = hw->back;
1711 	pci_write_config_word(adapter->pdev, reg, *value);
1712 }
1713 
1714 static int atl2_get_link_ksettings(struct net_device *netdev,
1715 				   struct ethtool_link_ksettings *cmd)
1716 {
1717 	struct atl2_adapter *adapter = netdev_priv(netdev);
1718 	struct atl2_hw *hw = &adapter->hw;
1719 	u32 supported, advertising;
1720 
1721 	supported = (SUPPORTED_10baseT_Half |
1722 		SUPPORTED_10baseT_Full |
1723 		SUPPORTED_100baseT_Half |
1724 		SUPPORTED_100baseT_Full |
1725 		SUPPORTED_Autoneg |
1726 		SUPPORTED_TP);
1727 	advertising = ADVERTISED_TP;
1728 
1729 	advertising |= ADVERTISED_Autoneg;
1730 	advertising |= hw->autoneg_advertised;
1731 
1732 	cmd->base.port = PORT_TP;
1733 	cmd->base.phy_address = 0;
1734 
1735 	if (adapter->link_speed != SPEED_0) {
1736 		cmd->base.speed = adapter->link_speed;
1737 		if (adapter->link_duplex == FULL_DUPLEX)
1738 			cmd->base.duplex = DUPLEX_FULL;
1739 		else
1740 			cmd->base.duplex = DUPLEX_HALF;
1741 	} else {
1742 		cmd->base.speed = SPEED_UNKNOWN;
1743 		cmd->base.duplex = DUPLEX_UNKNOWN;
1744 	}
1745 
1746 	cmd->base.autoneg = AUTONEG_ENABLE;
1747 
1748 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1749 						supported);
1750 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1751 						advertising);
1752 
1753 	return 0;
1754 }
1755 
1756 static int atl2_set_link_ksettings(struct net_device *netdev,
1757 				   const struct ethtool_link_ksettings *cmd)
1758 {
1759 	struct atl2_adapter *adapter = netdev_priv(netdev);
1760 	struct atl2_hw *hw = &adapter->hw;
1761 	u32 advertising;
1762 
1763 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
1764 						cmd->link_modes.advertising);
1765 
1766 	while (test_and_set_bit(__ATL2_RESETTING, &adapter->flags))
1767 		msleep(1);
1768 
1769 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1770 #define MY_ADV_MASK	(ADVERTISE_10_HALF | \
1771 			 ADVERTISE_10_FULL | \
1772 			 ADVERTISE_100_HALF| \
1773 			 ADVERTISE_100_FULL)
1774 
1775 		if ((advertising & MY_ADV_MASK) == MY_ADV_MASK) {
1776 			hw->MediaType = MEDIA_TYPE_AUTO_SENSOR;
1777 			hw->autoneg_advertised =  MY_ADV_MASK;
1778 		} else if ((advertising & MY_ADV_MASK) == ADVERTISE_100_FULL) {
1779 			hw->MediaType = MEDIA_TYPE_100M_FULL;
1780 			hw->autoneg_advertised = ADVERTISE_100_FULL;
1781 		} else if ((advertising & MY_ADV_MASK) == ADVERTISE_100_HALF) {
1782 			hw->MediaType = MEDIA_TYPE_100M_HALF;
1783 			hw->autoneg_advertised = ADVERTISE_100_HALF;
1784 		} else if ((advertising & MY_ADV_MASK) == ADVERTISE_10_FULL) {
1785 			hw->MediaType = MEDIA_TYPE_10M_FULL;
1786 			hw->autoneg_advertised = ADVERTISE_10_FULL;
1787 		}  else if ((advertising & MY_ADV_MASK) == ADVERTISE_10_HALF) {
1788 			hw->MediaType = MEDIA_TYPE_10M_HALF;
1789 			hw->autoneg_advertised = ADVERTISE_10_HALF;
1790 		} else {
1791 			clear_bit(__ATL2_RESETTING, &adapter->flags);
1792 			return -EINVAL;
1793 		}
1794 		advertising = hw->autoneg_advertised |
1795 			ADVERTISED_TP | ADVERTISED_Autoneg;
1796 	} else {
1797 		clear_bit(__ATL2_RESETTING, &adapter->flags);
1798 		return -EINVAL;
1799 	}
1800 
1801 	/* reset the link */
1802 	if (netif_running(adapter->netdev)) {
1803 		atl2_down(adapter);
1804 		atl2_up(adapter);
1805 	} else
1806 		atl2_reset_hw(&adapter->hw);
1807 
1808 	clear_bit(__ATL2_RESETTING, &adapter->flags);
1809 	return 0;
1810 }
1811 
1812 static u32 atl2_get_msglevel(struct net_device *netdev)
1813 {
1814 	return 0;
1815 }
1816 
1817 /*
1818  * It's sane for this to be empty, but we might want to take advantage of this.
1819  */
1820 static void atl2_set_msglevel(struct net_device *netdev, u32 data)
1821 {
1822 }
1823 
1824 static int atl2_get_regs_len(struct net_device *netdev)
1825 {
1826 #define ATL2_REGS_LEN 42
1827 	return sizeof(u32) * ATL2_REGS_LEN;
1828 }
1829 
1830 static void atl2_get_regs(struct net_device *netdev,
1831 	struct ethtool_regs *regs, void *p)
1832 {
1833 	struct atl2_adapter *adapter = netdev_priv(netdev);
1834 	struct atl2_hw *hw = &adapter->hw;
1835 	u32 *regs_buff = p;
1836 	u16 phy_data;
1837 
1838 	memset(p, 0, sizeof(u32) * ATL2_REGS_LEN);
1839 
1840 	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
1841 
1842 	regs_buff[0]  = ATL2_READ_REG(hw, REG_VPD_CAP);
1843 	regs_buff[1]  = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
1844 	regs_buff[2]  = ATL2_READ_REG(hw, REG_SPI_FLASH_CONFIG);
1845 	regs_buff[3]  = ATL2_READ_REG(hw, REG_TWSI_CTRL);
1846 	regs_buff[4]  = ATL2_READ_REG(hw, REG_PCIE_DEV_MISC_CTRL);
1847 	regs_buff[5]  = ATL2_READ_REG(hw, REG_MASTER_CTRL);
1848 	regs_buff[6]  = ATL2_READ_REG(hw, REG_MANUAL_TIMER_INIT);
1849 	regs_buff[7]  = ATL2_READ_REG(hw, REG_IRQ_MODU_TIMER_INIT);
1850 	regs_buff[8]  = ATL2_READ_REG(hw, REG_PHY_ENABLE);
1851 	regs_buff[9]  = ATL2_READ_REG(hw, REG_CMBDISDMA_TIMER);
1852 	regs_buff[10] = ATL2_READ_REG(hw, REG_IDLE_STATUS);
1853 	regs_buff[11] = ATL2_READ_REG(hw, REG_MDIO_CTRL);
1854 	regs_buff[12] = ATL2_READ_REG(hw, REG_SERDES_LOCK);
1855 	regs_buff[13] = ATL2_READ_REG(hw, REG_MAC_CTRL);
1856 	regs_buff[14] = ATL2_READ_REG(hw, REG_MAC_IPG_IFG);
1857 	regs_buff[15] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR);
1858 	regs_buff[16] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR+4);
1859 	regs_buff[17] = ATL2_READ_REG(hw, REG_RX_HASH_TABLE);
1860 	regs_buff[18] = ATL2_READ_REG(hw, REG_RX_HASH_TABLE+4);
1861 	regs_buff[19] = ATL2_READ_REG(hw, REG_MAC_HALF_DUPLX_CTRL);
1862 	regs_buff[20] = ATL2_READ_REG(hw, REG_MTU);
1863 	regs_buff[21] = ATL2_READ_REG(hw, REG_WOL_CTRL);
1864 	regs_buff[22] = ATL2_READ_REG(hw, REG_SRAM_TXRAM_END);
1865 	regs_buff[23] = ATL2_READ_REG(hw, REG_DESC_BASE_ADDR_HI);
1866 	regs_buff[24] = ATL2_READ_REG(hw, REG_TXD_BASE_ADDR_LO);
1867 	regs_buff[25] = ATL2_READ_REG(hw, REG_TXD_MEM_SIZE);
1868 	regs_buff[26] = ATL2_READ_REG(hw, REG_TXS_BASE_ADDR_LO);
1869 	regs_buff[27] = ATL2_READ_REG(hw, REG_TXS_MEM_SIZE);
1870 	regs_buff[28] = ATL2_READ_REG(hw, REG_RXD_BASE_ADDR_LO);
1871 	regs_buff[29] = ATL2_READ_REG(hw, REG_RXD_BUF_NUM);
1872 	regs_buff[30] = ATL2_READ_REG(hw, REG_DMAR);
1873 	regs_buff[31] = ATL2_READ_REG(hw, REG_TX_CUT_THRESH);
1874 	regs_buff[32] = ATL2_READ_REG(hw, REG_DMAW);
1875 	regs_buff[33] = ATL2_READ_REG(hw, REG_PAUSE_ON_TH);
1876 	regs_buff[34] = ATL2_READ_REG(hw, REG_PAUSE_OFF_TH);
1877 	regs_buff[35] = ATL2_READ_REG(hw, REG_MB_TXD_WR_IDX);
1878 	regs_buff[36] = ATL2_READ_REG(hw, REG_MB_RXD_RD_IDX);
1879 	regs_buff[38] = ATL2_READ_REG(hw, REG_ISR);
1880 	regs_buff[39] = ATL2_READ_REG(hw, REG_IMR);
1881 
1882 	atl2_read_phy_reg(hw, MII_BMCR, &phy_data);
1883 	regs_buff[40] = (u32)phy_data;
1884 	atl2_read_phy_reg(hw, MII_BMSR, &phy_data);
1885 	regs_buff[41] = (u32)phy_data;
1886 }
1887 
1888 static int atl2_get_eeprom_len(struct net_device *netdev)
1889 {
1890 	struct atl2_adapter *adapter = netdev_priv(netdev);
1891 
1892 	if (!atl2_check_eeprom_exist(&adapter->hw))
1893 		return 512;
1894 	else
1895 		return 0;
1896 }
1897 
1898 static int atl2_get_eeprom(struct net_device *netdev,
1899 	struct ethtool_eeprom *eeprom, u8 *bytes)
1900 {
1901 	struct atl2_adapter *adapter = netdev_priv(netdev);
1902 	struct atl2_hw *hw = &adapter->hw;
1903 	u32 *eeprom_buff;
1904 	int first_dword, last_dword;
1905 	int ret_val = 0;
1906 	int i;
1907 
1908 	if (eeprom->len == 0)
1909 		return -EINVAL;
1910 
1911 	if (atl2_check_eeprom_exist(hw))
1912 		return -EINVAL;
1913 
1914 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1915 
1916 	first_dword = eeprom->offset >> 2;
1917 	last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
1918 
1919 	eeprom_buff = kmalloc_array(last_dword - first_dword + 1, sizeof(u32),
1920 				    GFP_KERNEL);
1921 	if (!eeprom_buff)
1922 		return -ENOMEM;
1923 
1924 	for (i = first_dword; i < last_dword; i++) {
1925 		if (!atl2_read_eeprom(hw, i*4, &(eeprom_buff[i-first_dword]))) {
1926 			ret_val = -EIO;
1927 			goto free;
1928 		}
1929 	}
1930 
1931 	memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 3),
1932 		eeprom->len);
1933 free:
1934 	kfree(eeprom_buff);
1935 
1936 	return ret_val;
1937 }
1938 
1939 static int atl2_set_eeprom(struct net_device *netdev,
1940 	struct ethtool_eeprom *eeprom, u8 *bytes)
1941 {
1942 	struct atl2_adapter *adapter = netdev_priv(netdev);
1943 	struct atl2_hw *hw = &adapter->hw;
1944 	u32 *eeprom_buff;
1945 	u32 *ptr;
1946 	int max_len, first_dword, last_dword, ret_val = 0;
1947 	int i;
1948 
1949 	if (eeprom->len == 0)
1950 		return -EOPNOTSUPP;
1951 
1952 	if (eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
1953 		return -EFAULT;
1954 
1955 	max_len = 512;
1956 
1957 	first_dword = eeprom->offset >> 2;
1958 	last_dword = (eeprom->offset + eeprom->len - 1) >> 2;
1959 	eeprom_buff = kmalloc(max_len, GFP_KERNEL);
1960 	if (!eeprom_buff)
1961 		return -ENOMEM;
1962 
1963 	ptr = eeprom_buff;
1964 
1965 	if (eeprom->offset & 3) {
1966 		/* need read/modify/write of first changed EEPROM word */
1967 		/* only the second byte of the word is being modified */
1968 		if (!atl2_read_eeprom(hw, first_dword*4, &(eeprom_buff[0]))) {
1969 			ret_val = -EIO;
1970 			goto out;
1971 		}
1972 		ptr++;
1973 	}
1974 	if (((eeprom->offset + eeprom->len) & 3)) {
1975 		/*
1976 		 * need read/modify/write of last changed EEPROM word
1977 		 * only the first byte of the word is being modified
1978 		 */
1979 		if (!atl2_read_eeprom(hw, last_dword * 4,
1980 					&(eeprom_buff[last_dword - first_dword]))) {
1981 			ret_val = -EIO;
1982 			goto out;
1983 		}
1984 	}
1985 
1986 	/* Device's eeprom is always little-endian, word addressable */
1987 	memcpy(ptr, bytes, eeprom->len);
1988 
1989 	for (i = 0; i < last_dword - first_dword + 1; i++) {
1990 		if (!atl2_write_eeprom(hw, ((first_dword+i)*4), eeprom_buff[i])) {
1991 			ret_val = -EIO;
1992 			goto out;
1993 		}
1994 	}
1995  out:
1996 	kfree(eeprom_buff);
1997 	return ret_val;
1998 }
1999 
2000 static void atl2_get_drvinfo(struct net_device *netdev,
2001 	struct ethtool_drvinfo *drvinfo)
2002 {
2003 	struct atl2_adapter *adapter = netdev_priv(netdev);
2004 
2005 	strlcpy(drvinfo->driver,  atl2_driver_name, sizeof(drvinfo->driver));
2006 	strlcpy(drvinfo->fw_version, "L2", sizeof(drvinfo->fw_version));
2007 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
2008 		sizeof(drvinfo->bus_info));
2009 }
2010 
2011 static void atl2_get_wol(struct net_device *netdev,
2012 	struct ethtool_wolinfo *wol)
2013 {
2014 	struct atl2_adapter *adapter = netdev_priv(netdev);
2015 
2016 	wol->supported = WAKE_MAGIC;
2017 	wol->wolopts = 0;
2018 
2019 	if (adapter->wol & ATLX_WUFC_EX)
2020 		wol->wolopts |= WAKE_UCAST;
2021 	if (adapter->wol & ATLX_WUFC_MC)
2022 		wol->wolopts |= WAKE_MCAST;
2023 	if (adapter->wol & ATLX_WUFC_BC)
2024 		wol->wolopts |= WAKE_BCAST;
2025 	if (adapter->wol & ATLX_WUFC_MAG)
2026 		wol->wolopts |= WAKE_MAGIC;
2027 	if (adapter->wol & ATLX_WUFC_LNKC)
2028 		wol->wolopts |= WAKE_PHY;
2029 }
2030 
2031 static int atl2_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2032 {
2033 	struct atl2_adapter *adapter = netdev_priv(netdev);
2034 
2035 	if (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE))
2036 		return -EOPNOTSUPP;
2037 
2038 	if (wol->wolopts & (WAKE_UCAST | WAKE_BCAST | WAKE_MCAST))
2039 		return -EOPNOTSUPP;
2040 
2041 	/* these settings will always override what we currently have */
2042 	adapter->wol = 0;
2043 
2044 	if (wol->wolopts & WAKE_MAGIC)
2045 		adapter->wol |= ATLX_WUFC_MAG;
2046 	if (wol->wolopts & WAKE_PHY)
2047 		adapter->wol |= ATLX_WUFC_LNKC;
2048 
2049 	return 0;
2050 }
2051 
2052 static int atl2_nway_reset(struct net_device *netdev)
2053 {
2054 	struct atl2_adapter *adapter = netdev_priv(netdev);
2055 	if (netif_running(netdev))
2056 		atl2_reinit_locked(adapter);
2057 	return 0;
2058 }
2059 
2060 static const struct ethtool_ops atl2_ethtool_ops = {
2061 	.get_drvinfo		= atl2_get_drvinfo,
2062 	.get_regs_len		= atl2_get_regs_len,
2063 	.get_regs		= atl2_get_regs,
2064 	.get_wol		= atl2_get_wol,
2065 	.set_wol		= atl2_set_wol,
2066 	.get_msglevel		= atl2_get_msglevel,
2067 	.set_msglevel		= atl2_set_msglevel,
2068 	.nway_reset		= atl2_nway_reset,
2069 	.get_link		= ethtool_op_get_link,
2070 	.get_eeprom_len		= atl2_get_eeprom_len,
2071 	.get_eeprom		= atl2_get_eeprom,
2072 	.set_eeprom		= atl2_set_eeprom,
2073 	.get_link_ksettings	= atl2_get_link_ksettings,
2074 	.set_link_ksettings	= atl2_set_link_ksettings,
2075 };
2076 
2077 #define LBYTESWAP(a)  ((((a) & 0x00ff00ff) << 8) | \
2078 	(((a) & 0xff00ff00) >> 8))
2079 #define LONGSWAP(a)   ((LBYTESWAP(a) << 16) | (LBYTESWAP(a) >> 16))
2080 #define SHORTSWAP(a)  (((a) << 8) | ((a) >> 8))
2081 
2082 /*
2083  * Reset the transmit and receive units; mask and clear all interrupts.
2084  *
2085  * hw - Struct containing variables accessed by shared code
2086  * return : 0  or  idle status (if error)
2087  */
2088 static s32 atl2_reset_hw(struct atl2_hw *hw)
2089 {
2090 	u32 icr;
2091 	u16 pci_cfg_cmd_word;
2092 	int i;
2093 
2094 	/* Workaround for PCI problem when BIOS sets MMRBC incorrectly. */
2095 	atl2_read_pci_cfg(hw, PCI_REG_COMMAND, &pci_cfg_cmd_word);
2096 	if ((pci_cfg_cmd_word &
2097 		(CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER)) !=
2098 		(CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER)) {
2099 		pci_cfg_cmd_word |=
2100 			(CMD_IO_SPACE|CMD_MEMORY_SPACE|CMD_BUS_MASTER);
2101 		atl2_write_pci_cfg(hw, PCI_REG_COMMAND, &pci_cfg_cmd_word);
2102 	}
2103 
2104 	/* Clear Interrupt mask to stop board from generating
2105 	 * interrupts & Clear any pending interrupt events
2106 	 */
2107 	/* FIXME */
2108 	/* ATL2_WRITE_REG(hw, REG_IMR, 0); */
2109 	/* ATL2_WRITE_REG(hw, REG_ISR, 0xffffffff); */
2110 
2111 	/* Issue Soft Reset to the MAC.  This will reset the chip's
2112 	 * transmit, receive, DMA.  It will not effect
2113 	 * the current PCI configuration.  The global reset bit is self-
2114 	 * clearing, and should clear within a microsecond.
2115 	 */
2116 	ATL2_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_SOFT_RST);
2117 	wmb();
2118 	msleep(1); /* delay about 1ms */
2119 
2120 	/* Wait at least 10ms for All module to be Idle */
2121 	for (i = 0; i < 10; i++) {
2122 		icr = ATL2_READ_REG(hw, REG_IDLE_STATUS);
2123 		if (!icr)
2124 			break;
2125 		msleep(1); /* delay 1 ms */
2126 		cpu_relax();
2127 	}
2128 
2129 	if (icr)
2130 		return icr;
2131 
2132 	return 0;
2133 }
2134 
2135 #define CUSTOM_SPI_CS_SETUP        2
2136 #define CUSTOM_SPI_CLK_HI          2
2137 #define CUSTOM_SPI_CLK_LO          2
2138 #define CUSTOM_SPI_CS_HOLD         2
2139 #define CUSTOM_SPI_CS_HI           3
2140 
2141 static struct atl2_spi_flash_dev flash_table[] =
2142 {
2143 /* MFR    WRSR  READ  PROGRAM WREN  WRDI  RDSR  RDID  SECTOR_ERASE CHIP_ERASE */
2144 {"Atmel", 0x0,  0x03, 0x02,   0x06, 0x04, 0x05, 0x15, 0x52,        0x62 },
2145 {"SST",   0x01, 0x03, 0x02,   0x06, 0x04, 0x05, 0x90, 0x20,        0x60 },
2146 {"ST",    0x01, 0x03, 0x02,   0x06, 0x04, 0x05, 0xAB, 0xD8,        0xC7 },
2147 };
2148 
2149 static bool atl2_spi_read(struct atl2_hw *hw, u32 addr, u32 *buf)
2150 {
2151 	int i;
2152 	u32 value;
2153 
2154 	ATL2_WRITE_REG(hw, REG_SPI_DATA, 0);
2155 	ATL2_WRITE_REG(hw, REG_SPI_ADDR, addr);
2156 
2157 	value = SPI_FLASH_CTRL_WAIT_READY |
2158 		(CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
2159 			SPI_FLASH_CTRL_CS_SETUP_SHIFT |
2160 		(CUSTOM_SPI_CLK_HI & SPI_FLASH_CTRL_CLK_HI_MASK) <<
2161 			SPI_FLASH_CTRL_CLK_HI_SHIFT |
2162 		(CUSTOM_SPI_CLK_LO & SPI_FLASH_CTRL_CLK_LO_MASK) <<
2163 			SPI_FLASH_CTRL_CLK_LO_SHIFT |
2164 		(CUSTOM_SPI_CS_HOLD & SPI_FLASH_CTRL_CS_HOLD_MASK) <<
2165 			SPI_FLASH_CTRL_CS_HOLD_SHIFT |
2166 		(CUSTOM_SPI_CS_HI & SPI_FLASH_CTRL_CS_HI_MASK) <<
2167 			SPI_FLASH_CTRL_CS_HI_SHIFT |
2168 		(0x1 & SPI_FLASH_CTRL_INS_MASK) << SPI_FLASH_CTRL_INS_SHIFT;
2169 
2170 	ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2171 
2172 	value |= SPI_FLASH_CTRL_START;
2173 
2174 	ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2175 
2176 	for (i = 0; i < 10; i++) {
2177 		msleep(1);
2178 		value = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
2179 		if (!(value & SPI_FLASH_CTRL_START))
2180 			break;
2181 	}
2182 
2183 	if (value & SPI_FLASH_CTRL_START)
2184 		return false;
2185 
2186 	*buf = ATL2_READ_REG(hw, REG_SPI_DATA);
2187 
2188 	return true;
2189 }
2190 
2191 /*
2192  * get_permanent_address
2193  * return 0 if get valid mac address,
2194  */
2195 static int get_permanent_address(struct atl2_hw *hw)
2196 {
2197 	u32 Addr[2];
2198 	u32 i, Control;
2199 	u16 Register;
2200 	u8  EthAddr[ETH_ALEN];
2201 	bool KeyValid;
2202 
2203 	if (is_valid_ether_addr(hw->perm_mac_addr))
2204 		return 0;
2205 
2206 	Addr[0] = 0;
2207 	Addr[1] = 0;
2208 
2209 	if (!atl2_check_eeprom_exist(hw)) { /* eeprom exists */
2210 		Register = 0;
2211 		KeyValid = false;
2212 
2213 		/* Read out all EEPROM content */
2214 		i = 0;
2215 		while (1) {
2216 			if (atl2_read_eeprom(hw, i + 0x100, &Control)) {
2217 				if (KeyValid) {
2218 					if (Register == REG_MAC_STA_ADDR)
2219 						Addr[0] = Control;
2220 					else if (Register ==
2221 						(REG_MAC_STA_ADDR + 4))
2222 						Addr[1] = Control;
2223 					KeyValid = false;
2224 				} else if ((Control & 0xff) == 0x5A) {
2225 					KeyValid = true;
2226 					Register = (u16) (Control >> 16);
2227 				} else {
2228 			/* assume data end while encount an invalid KEYWORD */
2229 					break;
2230 				}
2231 			} else {
2232 				break; /* read error */
2233 			}
2234 			i += 4;
2235 		}
2236 
2237 		*(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2238 		*(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *) &Addr[1]);
2239 
2240 		if (is_valid_ether_addr(EthAddr)) {
2241 			memcpy(hw->perm_mac_addr, EthAddr, ETH_ALEN);
2242 			return 0;
2243 		}
2244 		return 1;
2245 	}
2246 
2247 	/* see if SPI flash exists? */
2248 	Addr[0] = 0;
2249 	Addr[1] = 0;
2250 	Register = 0;
2251 	KeyValid = false;
2252 	i = 0;
2253 	while (1) {
2254 		if (atl2_spi_read(hw, i + 0x1f000, &Control)) {
2255 			if (KeyValid) {
2256 				if (Register == REG_MAC_STA_ADDR)
2257 					Addr[0] = Control;
2258 				else if (Register == (REG_MAC_STA_ADDR + 4))
2259 					Addr[1] = Control;
2260 				KeyValid = false;
2261 			} else if ((Control & 0xff) == 0x5A) {
2262 				KeyValid = true;
2263 				Register = (u16) (Control >> 16);
2264 			} else {
2265 				break; /* data end */
2266 			}
2267 		} else {
2268 			break; /* read error */
2269 		}
2270 		i += 4;
2271 	}
2272 
2273 	*(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2274 	*(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *)&Addr[1]);
2275 	if (is_valid_ether_addr(EthAddr)) {
2276 		memcpy(hw->perm_mac_addr, EthAddr, ETH_ALEN);
2277 		return 0;
2278 	}
2279 	/* maybe MAC-address is from BIOS */
2280 	Addr[0] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR);
2281 	Addr[1] = ATL2_READ_REG(hw, REG_MAC_STA_ADDR + 4);
2282 	*(u32 *) &EthAddr[2] = LONGSWAP(Addr[0]);
2283 	*(u16 *) &EthAddr[0] = SHORTSWAP(*(u16 *) &Addr[1]);
2284 
2285 	if (is_valid_ether_addr(EthAddr)) {
2286 		memcpy(hw->perm_mac_addr, EthAddr, ETH_ALEN);
2287 		return 0;
2288 	}
2289 
2290 	return 1;
2291 }
2292 
2293 /*
2294  * Reads the adapter's MAC address from the EEPROM
2295  *
2296  * hw - Struct containing variables accessed by shared code
2297  */
2298 static s32 atl2_read_mac_addr(struct atl2_hw *hw)
2299 {
2300 	if (get_permanent_address(hw)) {
2301 		/* for test */
2302 		/* FIXME: shouldn't we use eth_random_addr() here? */
2303 		hw->perm_mac_addr[0] = 0x00;
2304 		hw->perm_mac_addr[1] = 0x13;
2305 		hw->perm_mac_addr[2] = 0x74;
2306 		hw->perm_mac_addr[3] = 0x00;
2307 		hw->perm_mac_addr[4] = 0x5c;
2308 		hw->perm_mac_addr[5] = 0x38;
2309 	}
2310 
2311 	memcpy(hw->mac_addr, hw->perm_mac_addr, ETH_ALEN);
2312 
2313 	return 0;
2314 }
2315 
2316 /*
2317  * Hashes an address to determine its location in the multicast table
2318  *
2319  * hw - Struct containing variables accessed by shared code
2320  * mc_addr - the multicast address to hash
2321  *
2322  * atl2_hash_mc_addr
2323  *  purpose
2324  *      set hash value for a multicast address
2325  *      hash calcu processing :
2326  *          1. calcu 32bit CRC for multicast address
2327  *          2. reverse crc with MSB to LSB
2328  */
2329 static u32 atl2_hash_mc_addr(struct atl2_hw *hw, u8 *mc_addr)
2330 {
2331 	u32 crc32, value;
2332 	int i;
2333 
2334 	value = 0;
2335 	crc32 = ether_crc_le(6, mc_addr);
2336 
2337 	for (i = 0; i < 32; i++)
2338 		value |= (((crc32 >> i) & 1) << (31 - i));
2339 
2340 	return value;
2341 }
2342 
2343 /*
2344  * Sets the bit in the multicast table corresponding to the hash value.
2345  *
2346  * hw - Struct containing variables accessed by shared code
2347  * hash_value - Multicast address hash value
2348  */
2349 static void atl2_hash_set(struct atl2_hw *hw, u32 hash_value)
2350 {
2351 	u32 hash_bit, hash_reg;
2352 	u32 mta;
2353 
2354 	/* The HASH Table  is a register array of 2 32-bit registers.
2355 	 * It is treated like an array of 64 bits.  We want to set
2356 	 * bit BitArray[hash_value]. So we figure out what register
2357 	 * the bit is in, read it, OR in the new bit, then write
2358 	 * back the new value.  The register is determined by the
2359 	 * upper 7 bits of the hash value and the bit within that
2360 	 * register are determined by the lower 5 bits of the value.
2361 	 */
2362 	hash_reg = (hash_value >> 31) & 0x1;
2363 	hash_bit = (hash_value >> 26) & 0x1F;
2364 
2365 	mta = ATL2_READ_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg);
2366 
2367 	mta |= (1 << hash_bit);
2368 
2369 	ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg, mta);
2370 }
2371 
2372 /*
2373  * atl2_init_pcie - init PCIE module
2374  */
2375 static void atl2_init_pcie(struct atl2_hw *hw)
2376 {
2377     u32 value;
2378     value = LTSSM_TEST_MODE_DEF;
2379     ATL2_WRITE_REG(hw, REG_LTSSM_TEST_MODE, value);
2380 
2381     value = PCIE_DLL_TX_CTRL1_DEF;
2382     ATL2_WRITE_REG(hw, REG_PCIE_DLL_TX_CTRL1, value);
2383 }
2384 
2385 static void atl2_init_flash_opcode(struct atl2_hw *hw)
2386 {
2387 	if (hw->flash_vendor >= ARRAY_SIZE(flash_table))
2388 		hw->flash_vendor = 0; /* ATMEL */
2389 
2390 	/* Init OP table */
2391 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_PROGRAM,
2392 		flash_table[hw->flash_vendor].cmdPROGRAM);
2393 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_SC_ERASE,
2394 		flash_table[hw->flash_vendor].cmdSECTOR_ERASE);
2395 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_CHIP_ERASE,
2396 		flash_table[hw->flash_vendor].cmdCHIP_ERASE);
2397 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_RDID,
2398 		flash_table[hw->flash_vendor].cmdRDID);
2399 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_WREN,
2400 		flash_table[hw->flash_vendor].cmdWREN);
2401 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_RDSR,
2402 		flash_table[hw->flash_vendor].cmdRDSR);
2403 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_WRSR,
2404 		flash_table[hw->flash_vendor].cmdWRSR);
2405 	ATL2_WRITE_REGB(hw, REG_SPI_FLASH_OP_READ,
2406 		flash_table[hw->flash_vendor].cmdREAD);
2407 }
2408 
2409 /********************************************************************
2410 * Performs basic configuration of the adapter.
2411 *
2412 * hw - Struct containing variables accessed by shared code
2413 * Assumes that the controller has previously been reset and is in a
2414 * post-reset uninitialized state. Initializes multicast table,
2415 * and  Calls routines to setup link
2416 * Leaves the transmit and receive units disabled and uninitialized.
2417 ********************************************************************/
2418 static s32 atl2_init_hw(struct atl2_hw *hw)
2419 {
2420 	u32 ret_val = 0;
2421 
2422 	atl2_init_pcie(hw);
2423 
2424 	/* Zero out the Multicast HASH table */
2425 	/* clear the old settings from the multicast hash table */
2426 	ATL2_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
2427 	ATL2_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
2428 
2429 	atl2_init_flash_opcode(hw);
2430 
2431 	ret_val = atl2_phy_init(hw);
2432 
2433 	return ret_val;
2434 }
2435 
2436 /*
2437  * Detects the current speed and duplex settings of the hardware.
2438  *
2439  * hw - Struct containing variables accessed by shared code
2440  * speed - Speed of the connection
2441  * duplex - Duplex setting of the connection
2442  */
2443 static s32 atl2_get_speed_and_duplex(struct atl2_hw *hw, u16 *speed,
2444 	u16 *duplex)
2445 {
2446 	s32 ret_val;
2447 	u16 phy_data;
2448 
2449 	/* Read PHY Specific Status Register (17) */
2450 	ret_val = atl2_read_phy_reg(hw, MII_ATLX_PSSR, &phy_data);
2451 	if (ret_val)
2452 		return ret_val;
2453 
2454 	if (!(phy_data & MII_ATLX_PSSR_SPD_DPLX_RESOLVED))
2455 		return ATLX_ERR_PHY_RES;
2456 
2457 	switch (phy_data & MII_ATLX_PSSR_SPEED) {
2458 	case MII_ATLX_PSSR_100MBS:
2459 		*speed = SPEED_100;
2460 		break;
2461 	case MII_ATLX_PSSR_10MBS:
2462 		*speed = SPEED_10;
2463 		break;
2464 	default:
2465 		return ATLX_ERR_PHY_SPEED;
2466 	}
2467 
2468 	if (phy_data & MII_ATLX_PSSR_DPLX)
2469 		*duplex = FULL_DUPLEX;
2470 	else
2471 		*duplex = HALF_DUPLEX;
2472 
2473 	return 0;
2474 }
2475 
2476 /*
2477  * Reads the value from a PHY register
2478  * hw - Struct containing variables accessed by shared code
2479  * reg_addr - address of the PHY register to read
2480  */
2481 static s32 atl2_read_phy_reg(struct atl2_hw *hw, u16 reg_addr, u16 *phy_data)
2482 {
2483 	u32 val;
2484 	int i;
2485 
2486 	val = ((u32)(reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
2487 		MDIO_START |
2488 		MDIO_SUP_PREAMBLE |
2489 		MDIO_RW |
2490 		MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2491 	ATL2_WRITE_REG(hw, REG_MDIO_CTRL, val);
2492 
2493 	wmb();
2494 
2495 	for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2496 		udelay(2);
2497 		val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2498 		if (!(val & (MDIO_START | MDIO_BUSY)))
2499 			break;
2500 		wmb();
2501 	}
2502 	if (!(val & (MDIO_START | MDIO_BUSY))) {
2503 		*phy_data = (u16)val;
2504 		return 0;
2505 	}
2506 
2507 	return ATLX_ERR_PHY;
2508 }
2509 
2510 /*
2511  * Writes a value to a PHY register
2512  * hw - Struct containing variables accessed by shared code
2513  * reg_addr - address of the PHY register to write
2514  * data - data to write to the PHY
2515  */
2516 static s32 atl2_write_phy_reg(struct atl2_hw *hw, u32 reg_addr, u16 phy_data)
2517 {
2518 	int i;
2519 	u32 val;
2520 
2521 	val = ((u32)(phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
2522 		(reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
2523 		MDIO_SUP_PREAMBLE |
2524 		MDIO_START |
2525 		MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
2526 	ATL2_WRITE_REG(hw, REG_MDIO_CTRL, val);
2527 
2528 	wmb();
2529 
2530 	for (i = 0; i < MDIO_WAIT_TIMES; i++) {
2531 		udelay(2);
2532 		val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2533 		if (!(val & (MDIO_START | MDIO_BUSY)))
2534 			break;
2535 
2536 		wmb();
2537 	}
2538 
2539 	if (!(val & (MDIO_START | MDIO_BUSY)))
2540 		return 0;
2541 
2542 	return ATLX_ERR_PHY;
2543 }
2544 
2545 /*
2546  * Configures PHY autoneg and flow control advertisement settings
2547  *
2548  * hw - Struct containing variables accessed by shared code
2549  */
2550 static s32 atl2_phy_setup_autoneg_adv(struct atl2_hw *hw)
2551 {
2552 	s16 mii_autoneg_adv_reg;
2553 
2554 	/* Read the MII Auto-Neg Advertisement Register (Address 4). */
2555 	mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
2556 
2557 	/* Need to parse autoneg_advertised  and set up
2558 	 * the appropriate PHY registers.  First we will parse for
2559 	 * autoneg_advertised software override.  Since we can advertise
2560 	 * a plethora of combinations, we need to check each bit
2561 	 * individually.
2562 	 */
2563 
2564 	/* First we clear all the 10/100 mb speed bits in the Auto-Neg
2565 	 * Advertisement Register (Address 4) and the 1000 mb speed bits in
2566 	 * the  1000Base-T Control Register (Address 9). */
2567 	mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
2568 
2569 	/* Need to parse MediaType and setup the
2570 	 * appropriate PHY registers. */
2571 	switch (hw->MediaType) {
2572 	case MEDIA_TYPE_AUTO_SENSOR:
2573 		mii_autoneg_adv_reg |=
2574 			(MII_AR_10T_HD_CAPS |
2575 			MII_AR_10T_FD_CAPS  |
2576 			MII_AR_100TX_HD_CAPS|
2577 			MII_AR_100TX_FD_CAPS);
2578 		hw->autoneg_advertised =
2579 			ADVERTISE_10_HALF |
2580 			ADVERTISE_10_FULL |
2581 			ADVERTISE_100_HALF|
2582 			ADVERTISE_100_FULL;
2583 		break;
2584 	case MEDIA_TYPE_100M_FULL:
2585 		mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
2586 		hw->autoneg_advertised = ADVERTISE_100_FULL;
2587 		break;
2588 	case MEDIA_TYPE_100M_HALF:
2589 		mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
2590 		hw->autoneg_advertised = ADVERTISE_100_HALF;
2591 		break;
2592 	case MEDIA_TYPE_10M_FULL:
2593 		mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
2594 		hw->autoneg_advertised = ADVERTISE_10_FULL;
2595 		break;
2596 	default:
2597 		mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
2598 		hw->autoneg_advertised = ADVERTISE_10_HALF;
2599 		break;
2600 	}
2601 
2602 	/* flow control fixed to enable all */
2603 	mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
2604 
2605 	hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
2606 
2607 	return atl2_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
2608 }
2609 
2610 /*
2611  * Resets the PHY and make all config validate
2612  *
2613  * hw - Struct containing variables accessed by shared code
2614  *
2615  * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
2616  */
2617 static s32 atl2_phy_commit(struct atl2_hw *hw)
2618 {
2619 	s32 ret_val;
2620 	u16 phy_data;
2621 
2622 	phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG;
2623 	ret_val = atl2_write_phy_reg(hw, MII_BMCR, phy_data);
2624 	if (ret_val) {
2625 		u32 val;
2626 		int i;
2627 		/* pcie serdes link may be down ! */
2628 		for (i = 0; i < 25; i++) {
2629 			msleep(1);
2630 			val = ATL2_READ_REG(hw, REG_MDIO_CTRL);
2631 			if (!(val & (MDIO_START | MDIO_BUSY)))
2632 				break;
2633 		}
2634 
2635 		if (0 != (val & (MDIO_START | MDIO_BUSY))) {
2636 			printk(KERN_ERR "atl2: PCIe link down for at least 25ms !\n");
2637 			return ret_val;
2638 		}
2639 	}
2640 	return 0;
2641 }
2642 
2643 static s32 atl2_phy_init(struct atl2_hw *hw)
2644 {
2645 	s32 ret_val;
2646 	u16 phy_val;
2647 
2648 	if (hw->phy_configured)
2649 		return 0;
2650 
2651 	/* Enable PHY */
2652 	ATL2_WRITE_REGW(hw, REG_PHY_ENABLE, 1);
2653 	ATL2_WRITE_FLUSH(hw);
2654 	msleep(1);
2655 
2656 	/* check if the PHY is in powersaving mode */
2657 	atl2_write_phy_reg(hw, MII_DBG_ADDR, 0);
2658 	atl2_read_phy_reg(hw, MII_DBG_DATA, &phy_val);
2659 
2660 	/* 024E / 124E 0r 0274 / 1274 ? */
2661 	if (phy_val & 0x1000) {
2662 		phy_val &= ~0x1000;
2663 		atl2_write_phy_reg(hw, MII_DBG_DATA, phy_val);
2664 	}
2665 
2666 	msleep(1);
2667 
2668 	/*Enable PHY LinkChange Interrupt */
2669 	ret_val = atl2_write_phy_reg(hw, 18, 0xC00);
2670 	if (ret_val)
2671 		return ret_val;
2672 
2673 	/* setup AutoNeg parameters */
2674 	ret_val = atl2_phy_setup_autoneg_adv(hw);
2675 	if (ret_val)
2676 		return ret_val;
2677 
2678 	/* SW.Reset & En-Auto-Neg to restart Auto-Neg */
2679 	ret_val = atl2_phy_commit(hw);
2680 	if (ret_val)
2681 		return ret_val;
2682 
2683 	hw->phy_configured = true;
2684 
2685 	return ret_val;
2686 }
2687 
2688 static void atl2_set_mac_addr(struct atl2_hw *hw)
2689 {
2690 	u32 value;
2691 	/* 00-0B-6A-F6-00-DC
2692 	 * 0:  6AF600DC   1: 000B
2693 	 * low dword */
2694 	value = (((u32)hw->mac_addr[2]) << 24) |
2695 		(((u32)hw->mac_addr[3]) << 16) |
2696 		(((u32)hw->mac_addr[4]) << 8)  |
2697 		(((u32)hw->mac_addr[5]));
2698 	ATL2_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 0, value);
2699 	/* hight dword */
2700 	value = (((u32)hw->mac_addr[0]) << 8) |
2701 		(((u32)hw->mac_addr[1]));
2702 	ATL2_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 1, value);
2703 }
2704 
2705 /*
2706  * check_eeprom_exist
2707  * return 0 if eeprom exist
2708  */
2709 static int atl2_check_eeprom_exist(struct atl2_hw *hw)
2710 {
2711 	u32 value;
2712 
2713 	value = ATL2_READ_REG(hw, REG_SPI_FLASH_CTRL);
2714 	if (value & SPI_FLASH_CTRL_EN_VPD) {
2715 		value &= ~SPI_FLASH_CTRL_EN_VPD;
2716 		ATL2_WRITE_REG(hw, REG_SPI_FLASH_CTRL, value);
2717 	}
2718 	value = ATL2_READ_REGW(hw, REG_PCIE_CAP_LIST);
2719 	return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
2720 }
2721 
2722 /* FIXME: This doesn't look right. -- CHS */
2723 static bool atl2_write_eeprom(struct atl2_hw *hw, u32 offset, u32 value)
2724 {
2725 	return true;
2726 }
2727 
2728 static bool atl2_read_eeprom(struct atl2_hw *hw, u32 Offset, u32 *pValue)
2729 {
2730 	int i;
2731 	u32    Control;
2732 
2733 	if (Offset & 0x3)
2734 		return false; /* address do not align */
2735 
2736 	ATL2_WRITE_REG(hw, REG_VPD_DATA, 0);
2737 	Control = (Offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
2738 	ATL2_WRITE_REG(hw, REG_VPD_CAP, Control);
2739 
2740 	for (i = 0; i < 10; i++) {
2741 		msleep(2);
2742 		Control = ATL2_READ_REG(hw, REG_VPD_CAP);
2743 		if (Control & VPD_CAP_VPD_FLAG)
2744 			break;
2745 	}
2746 
2747 	if (Control & VPD_CAP_VPD_FLAG) {
2748 		*pValue = ATL2_READ_REG(hw, REG_VPD_DATA);
2749 		return true;
2750 	}
2751 	return false; /* timeout */
2752 }
2753 
2754 static void atl2_force_ps(struct atl2_hw *hw)
2755 {
2756 	u16 phy_val;
2757 
2758 	atl2_write_phy_reg(hw, MII_DBG_ADDR, 0);
2759 	atl2_read_phy_reg(hw, MII_DBG_DATA, &phy_val);
2760 	atl2_write_phy_reg(hw, MII_DBG_DATA, phy_val | 0x1000);
2761 
2762 	atl2_write_phy_reg(hw, MII_DBG_ADDR, 2);
2763 	atl2_write_phy_reg(hw, MII_DBG_DATA, 0x3000);
2764 	atl2_write_phy_reg(hw, MII_DBG_ADDR, 3);
2765 	atl2_write_phy_reg(hw, MII_DBG_DATA, 0);
2766 }
2767 
2768 /* This is the only thing that needs to be changed to adjust the
2769  * maximum number of ports that the driver can manage.
2770  */
2771 #define ATL2_MAX_NIC 4
2772 
2773 #define OPTION_UNSET    -1
2774 #define OPTION_DISABLED 0
2775 #define OPTION_ENABLED  1
2776 
2777 /* All parameters are treated the same, as an integer array of values.
2778  * This macro just reduces the need to repeat the same declaration code
2779  * over and over (plus this helps to avoid typo bugs).
2780  */
2781 #define ATL2_PARAM_INIT {[0 ... ATL2_MAX_NIC] = OPTION_UNSET}
2782 #ifndef module_param_array
2783 /* Module Parameters are always initialized to -1, so that the driver
2784  * can tell the difference between no user specified value or the
2785  * user asking for the default value.
2786  * The true default values are loaded in when atl2_check_options is called.
2787  *
2788  * This is a GCC extension to ANSI C.
2789  * See the item "Labeled Elements in Initializers" in the section
2790  * "Extensions to the C Language Family" of the GCC documentation.
2791  */
2792 
2793 #define ATL2_PARAM(X, desc) \
2794     static const int X[ATL2_MAX_NIC + 1] = ATL2_PARAM_INIT; \
2795     MODULE_PARM(X, "1-" __MODULE_STRING(ATL2_MAX_NIC) "i"); \
2796     MODULE_PARM_DESC(X, desc);
2797 #else
2798 #define ATL2_PARAM(X, desc) \
2799     static int X[ATL2_MAX_NIC+1] = ATL2_PARAM_INIT; \
2800     static unsigned int num_##X; \
2801     module_param_array_named(X, X, int, &num_##X, 0); \
2802     MODULE_PARM_DESC(X, desc);
2803 #endif
2804 
2805 /*
2806  * Transmit Memory Size
2807  * Valid Range: 64-2048
2808  * Default Value: 128
2809  */
2810 #define ATL2_MIN_TX_MEMSIZE		4	/* 4KB */
2811 #define ATL2_MAX_TX_MEMSIZE		64	/* 64KB */
2812 #define ATL2_DEFAULT_TX_MEMSIZE		8	/* 8KB */
2813 ATL2_PARAM(TxMemSize, "Bytes of Transmit Memory");
2814 
2815 /*
2816  * Receive Memory Block Count
2817  * Valid Range: 16-512
2818  * Default Value: 128
2819  */
2820 #define ATL2_MIN_RXD_COUNT		16
2821 #define ATL2_MAX_RXD_COUNT		512
2822 #define ATL2_DEFAULT_RXD_COUNT		64
2823 ATL2_PARAM(RxMemBlock, "Number of receive memory block");
2824 
2825 /*
2826  * User Specified MediaType Override
2827  *
2828  * Valid Range: 0-5
2829  *  - 0    - auto-negotiate at all supported speeds
2830  *  - 1    - only link at 1000Mbps Full Duplex
2831  *  - 2    - only link at 100Mbps Full Duplex
2832  *  - 3    - only link at 100Mbps Half Duplex
2833  *  - 4    - only link at 10Mbps Full Duplex
2834  *  - 5    - only link at 10Mbps Half Duplex
2835  * Default Value: 0
2836  */
2837 ATL2_PARAM(MediaType, "MediaType Select");
2838 
2839 /*
2840  * Interrupt Moderate Timer in units of 2048 ns (~2 us)
2841  * Valid Range: 10-65535
2842  * Default Value: 45000(90ms)
2843  */
2844 #define INT_MOD_DEFAULT_CNT	100 /* 200us */
2845 #define INT_MOD_MAX_CNT		65000
2846 #define INT_MOD_MIN_CNT		50
2847 ATL2_PARAM(IntModTimer, "Interrupt Moderator Timer");
2848 
2849 /*
2850  * FlashVendor
2851  * Valid Range: 0-2
2852  * 0 - Atmel
2853  * 1 - SST
2854  * 2 - ST
2855  */
2856 ATL2_PARAM(FlashVendor, "SPI Flash Vendor");
2857 
2858 #define AUTONEG_ADV_DEFAULT	0x2F
2859 #define AUTONEG_ADV_MASK	0x2F
2860 #define FLOW_CONTROL_DEFAULT	FLOW_CONTROL_FULL
2861 
2862 #define FLASH_VENDOR_DEFAULT	0
2863 #define FLASH_VENDOR_MIN	0
2864 #define FLASH_VENDOR_MAX	2
2865 
2866 struct atl2_option {
2867 	enum { enable_option, range_option, list_option } type;
2868 	char *name;
2869 	char *err;
2870 	int  def;
2871 	union {
2872 		struct { /* range_option info */
2873 			int min;
2874 			int max;
2875 		} r;
2876 		struct { /* list_option info */
2877 			int nr;
2878 			struct atl2_opt_list { int i; char *str; } *p;
2879 		} l;
2880 	} arg;
2881 };
2882 
2883 static int atl2_validate_option(int *value, struct atl2_option *opt)
2884 {
2885 	int i;
2886 	struct atl2_opt_list *ent;
2887 
2888 	if (*value == OPTION_UNSET) {
2889 		*value = opt->def;
2890 		return 0;
2891 	}
2892 
2893 	switch (opt->type) {
2894 	case enable_option:
2895 		switch (*value) {
2896 		case OPTION_ENABLED:
2897 			printk(KERN_INFO "%s Enabled\n", opt->name);
2898 			return 0;
2899 		case OPTION_DISABLED:
2900 			printk(KERN_INFO "%s Disabled\n", opt->name);
2901 			return 0;
2902 		}
2903 		break;
2904 	case range_option:
2905 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
2906 			printk(KERN_INFO "%s set to %i\n", opt->name, *value);
2907 			return 0;
2908 		}
2909 		break;
2910 	case list_option:
2911 		for (i = 0; i < opt->arg.l.nr; i++) {
2912 			ent = &opt->arg.l.p[i];
2913 			if (*value == ent->i) {
2914 				if (ent->str[0] != '\0')
2915 					printk(KERN_INFO "%s\n", ent->str);
2916 				return 0;
2917 			}
2918 		}
2919 		break;
2920 	default:
2921 		BUG();
2922 	}
2923 
2924 	printk(KERN_INFO "Invalid %s specified (%i) %s\n",
2925 		opt->name, *value, opt->err);
2926 	*value = opt->def;
2927 	return -1;
2928 }
2929 
2930 /**
2931  * atl2_check_options - Range Checking for Command Line Parameters
2932  * @adapter: board private structure
2933  *
2934  * This routine checks all command line parameters for valid user
2935  * input.  If an invalid value is given, or if no user specified
2936  * value exists, a default value is used.  The final value is stored
2937  * in a variable in the adapter structure.
2938  */
2939 static void atl2_check_options(struct atl2_adapter *adapter)
2940 {
2941 	int val;
2942 	struct atl2_option opt;
2943 	int bd = adapter->bd_number;
2944 	if (bd >= ATL2_MAX_NIC) {
2945 		printk(KERN_NOTICE "Warning: no configuration for board #%i\n",
2946 			bd);
2947 		printk(KERN_NOTICE "Using defaults for all values\n");
2948 #ifndef module_param_array
2949 		bd = ATL2_MAX_NIC;
2950 #endif
2951 	}
2952 
2953 	/* Bytes of Transmit Memory */
2954 	opt.type = range_option;
2955 	opt.name = "Bytes of Transmit Memory";
2956 	opt.err = "using default of " __MODULE_STRING(ATL2_DEFAULT_TX_MEMSIZE);
2957 	opt.def = ATL2_DEFAULT_TX_MEMSIZE;
2958 	opt.arg.r.min = ATL2_MIN_TX_MEMSIZE;
2959 	opt.arg.r.max = ATL2_MAX_TX_MEMSIZE;
2960 #ifdef module_param_array
2961 	if (num_TxMemSize > bd) {
2962 #endif
2963 		val = TxMemSize[bd];
2964 		atl2_validate_option(&val, &opt);
2965 		adapter->txd_ring_size = ((u32) val) * 1024;
2966 #ifdef module_param_array
2967 	} else
2968 		adapter->txd_ring_size = ((u32)opt.def) * 1024;
2969 #endif
2970 	/* txs ring size: */
2971 	adapter->txs_ring_size = adapter->txd_ring_size / 128;
2972 	if (adapter->txs_ring_size > 160)
2973 		adapter->txs_ring_size = 160;
2974 
2975 	/* Receive Memory Block Count */
2976 	opt.type = range_option;
2977 	opt.name = "Number of receive memory block";
2978 	opt.err = "using default of " __MODULE_STRING(ATL2_DEFAULT_RXD_COUNT);
2979 	opt.def = ATL2_DEFAULT_RXD_COUNT;
2980 	opt.arg.r.min = ATL2_MIN_RXD_COUNT;
2981 	opt.arg.r.max = ATL2_MAX_RXD_COUNT;
2982 #ifdef module_param_array
2983 	if (num_RxMemBlock > bd) {
2984 #endif
2985 		val = RxMemBlock[bd];
2986 		atl2_validate_option(&val, &opt);
2987 		adapter->rxd_ring_size = (u32)val;
2988 		/* FIXME */
2989 		/* ((u16)val)&~1; */	/* even number */
2990 #ifdef module_param_array
2991 	} else
2992 		adapter->rxd_ring_size = (u32)opt.def;
2993 #endif
2994 	/* init RXD Flow control value */
2995 	adapter->hw.fc_rxd_hi = (adapter->rxd_ring_size / 8) * 7;
2996 	adapter->hw.fc_rxd_lo = (ATL2_MIN_RXD_COUNT / 8) >
2997 		(adapter->rxd_ring_size / 12) ? (ATL2_MIN_RXD_COUNT / 8) :
2998 		(adapter->rxd_ring_size / 12);
2999 
3000 	/* Interrupt Moderate Timer */
3001 	opt.type = range_option;
3002 	opt.name = "Interrupt Moderate Timer";
3003 	opt.err = "using default of " __MODULE_STRING(INT_MOD_DEFAULT_CNT);
3004 	opt.def = INT_MOD_DEFAULT_CNT;
3005 	opt.arg.r.min = INT_MOD_MIN_CNT;
3006 	opt.arg.r.max = INT_MOD_MAX_CNT;
3007 #ifdef module_param_array
3008 	if (num_IntModTimer > bd) {
3009 #endif
3010 		val = IntModTimer[bd];
3011 		atl2_validate_option(&val, &opt);
3012 		adapter->imt = (u16) val;
3013 #ifdef module_param_array
3014 	} else
3015 		adapter->imt = (u16)(opt.def);
3016 #endif
3017 	/* Flash Vendor */
3018 	opt.type = range_option;
3019 	opt.name = "SPI Flash Vendor";
3020 	opt.err = "using default of " __MODULE_STRING(FLASH_VENDOR_DEFAULT);
3021 	opt.def = FLASH_VENDOR_DEFAULT;
3022 	opt.arg.r.min = FLASH_VENDOR_MIN;
3023 	opt.arg.r.max = FLASH_VENDOR_MAX;
3024 #ifdef module_param_array
3025 	if (num_FlashVendor > bd) {
3026 #endif
3027 		val = FlashVendor[bd];
3028 		atl2_validate_option(&val, &opt);
3029 		adapter->hw.flash_vendor = (u8) val;
3030 #ifdef module_param_array
3031 	} else
3032 		adapter->hw.flash_vendor = (u8)(opt.def);
3033 #endif
3034 	/* MediaType */
3035 	opt.type = range_option;
3036 	opt.name = "Speed/Duplex Selection";
3037 	opt.err = "using default of " __MODULE_STRING(MEDIA_TYPE_AUTO_SENSOR);
3038 	opt.def = MEDIA_TYPE_AUTO_SENSOR;
3039 	opt.arg.r.min = MEDIA_TYPE_AUTO_SENSOR;
3040 	opt.arg.r.max = MEDIA_TYPE_10M_HALF;
3041 #ifdef module_param_array
3042 	if (num_MediaType > bd) {
3043 #endif
3044 		val = MediaType[bd];
3045 		atl2_validate_option(&val, &opt);
3046 		adapter->hw.MediaType = (u16) val;
3047 #ifdef module_param_array
3048 	} else
3049 		adapter->hw.MediaType = (u16)(opt.def);
3050 #endif
3051 }
3052