1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright(c) 2008 - 2009 Atheros Corporation. All rights reserved.
4  *
5  * Derived from Intel e1000 driver
6  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
7  */
8 
9 #include "atl1c.h"
10 
11 char atl1c_driver_name[] = "atl1c";
12 
13 /*
14  * atl1c_pci_tbl - PCI Device ID Table
15  *
16  * Wildcard entries (PCI_ANY_ID) should come last
17  * Last entry must be all 0s
18  *
19  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
20  *   Class, Class Mask, private data (not used) }
21  */
22 static const struct pci_device_id atl1c_pci_tbl[] = {
23 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1C)},
24 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2C)},
25 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B)},
26 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B2)},
27 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D)},
28 	{PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D_2_0)},
29 	/* required last entry */
30 	{ 0 }
31 };
32 MODULE_DEVICE_TABLE(pci, atl1c_pci_tbl);
33 
34 MODULE_AUTHOR("Jie Yang");
35 MODULE_AUTHOR("Qualcomm Atheros Inc.");
36 MODULE_DESCRIPTION("Qualcomm Atheros 100/1000M Ethernet Network Driver");
37 MODULE_LICENSE("GPL");
38 
39 static int atl1c_stop_mac(struct atl1c_hw *hw);
40 static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
41 static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed);
42 static void atl1c_start_mac(struct atl1c_adapter *adapter);
43 static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
44 		   int *work_done, int work_to_do);
45 static int atl1c_up(struct atl1c_adapter *adapter);
46 static void atl1c_down(struct atl1c_adapter *adapter);
47 static int atl1c_reset_mac(struct atl1c_hw *hw);
48 static void atl1c_reset_dma_ring(struct atl1c_adapter *adapter);
49 static int atl1c_configure(struct atl1c_adapter *adapter);
50 static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter, bool napi_mode);
51 
52 
53 static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
54 	NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
55 static void atl1c_pcie_patch(struct atl1c_hw *hw)
56 {
57 	u32 mst_data, data;
58 
59 	/* pclk sel could switch to 25M */
60 	AT_READ_REG(hw, REG_MASTER_CTRL, &mst_data);
61 	mst_data &= ~MASTER_CTRL_CLK_SEL_DIS;
62 	AT_WRITE_REG(hw, REG_MASTER_CTRL, mst_data);
63 
64 	/* WoL/PCIE related settings */
65 	if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
66 		AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
67 		data |= PCIE_PHYMISC_FORCE_RCV_DET;
68 		AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
69 	} else { /* new dev set bit5 of MASTER */
70 		if (!(mst_data & MASTER_CTRL_WAKEN_25M))
71 			AT_WRITE_REG(hw, REG_MASTER_CTRL,
72 				mst_data | MASTER_CTRL_WAKEN_25M);
73 	}
74 	/* aspm/PCIE setting only for l2cb 1.0 */
75 	if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
76 		AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
77 		data = FIELD_SETX(data, PCIE_PHYMISC2_CDR_BW,
78 			L2CB1_PCIE_PHYMISC2_CDR_BW);
79 		data = FIELD_SETX(data, PCIE_PHYMISC2_L0S_TH,
80 			L2CB1_PCIE_PHYMISC2_L0S_TH);
81 		AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
82 		/* extend L1 sync timer */
83 		AT_READ_REG(hw, REG_LINK_CTRL, &data);
84 		data |= LINK_CTRL_EXT_SYNC;
85 		AT_WRITE_REG(hw, REG_LINK_CTRL, data);
86 	}
87 	/* l2cb 1.x & l1d 1.x */
88 	if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d) {
89 		AT_READ_REG(hw, REG_PM_CTRL, &data);
90 		data |= PM_CTRL_L0S_BUFSRX_EN;
91 		AT_WRITE_REG(hw, REG_PM_CTRL, data);
92 		/* clear vendor msg */
93 		AT_READ_REG(hw, REG_DMA_DBG, &data);
94 		AT_WRITE_REG(hw, REG_DMA_DBG, data & ~DMA_DBG_VENDOR_MSG);
95 	}
96 }
97 
98 /* FIXME: no need any more ? */
99 /*
100  * atl1c_init_pcie - init PCIE module
101  */
102 static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
103 {
104 	u32 data;
105 	u32 pci_cmd;
106 	struct pci_dev *pdev = hw->adapter->pdev;
107 	int pos;
108 
109 	AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
110 	pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
111 	pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
112 		PCI_COMMAND_IO);
113 	AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
114 
115 	/*
116 	 * Clear any PowerSaveing Settings
117 	 */
118 	pci_enable_wake(pdev, PCI_D3hot, 0);
119 	pci_enable_wake(pdev, PCI_D3cold, 0);
120 	/* wol sts read-clear */
121 	AT_READ_REG(hw, REG_WOL_CTRL, &data);
122 	AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
123 
124 	/*
125 	 * Mask some pcie error bits
126 	 */
127 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
128 	if (pos) {
129 		pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &data);
130 		data &= ~(PCI_ERR_UNC_DLP | PCI_ERR_UNC_FCP);
131 		pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, data);
132 	}
133 	/* clear error status */
134 	pcie_capability_write_word(pdev, PCI_EXP_DEVSTA,
135 			PCI_EXP_DEVSTA_NFED |
136 			PCI_EXP_DEVSTA_FED |
137 			PCI_EXP_DEVSTA_CED |
138 			PCI_EXP_DEVSTA_URD);
139 
140 	AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
141 	data &= ~LTSSM_ID_EN_WRO;
142 	AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
143 
144 	atl1c_pcie_patch(hw);
145 	if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
146 		atl1c_disable_l0s_l1(hw);
147 
148 	msleep(5);
149 }
150 
151 /**
152  * atl1c_irq_enable - Enable default interrupt generation settings
153  * @adapter: board private structure
154  */
155 static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
156 {
157 	if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
158 		AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
159 		AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
160 		AT_WRITE_FLUSH(&adapter->hw);
161 	}
162 }
163 
164 /**
165  * atl1c_irq_disable - Mask off interrupt generation on the NIC
166  * @adapter: board private structure
167  */
168 static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
169 {
170 	atomic_inc(&adapter->irq_sem);
171 	AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
172 	AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
173 	AT_WRITE_FLUSH(&adapter->hw);
174 	synchronize_irq(adapter->pdev->irq);
175 }
176 
177 /**
178  * atl1c_irq_reset - reset interrupt confiure on the NIC
179  * @adapter: board private structure
180  */
181 static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
182 {
183 	atomic_set(&adapter->irq_sem, 1);
184 	atl1c_irq_enable(adapter);
185 }
186 
187 /*
188  * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
189  * of the idle status register until the device is actually idle
190  */
191 static u32 atl1c_wait_until_idle(struct atl1c_hw *hw, u32 modu_ctrl)
192 {
193 	int timeout;
194 	u32 data;
195 
196 	for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
197 		AT_READ_REG(hw, REG_IDLE_STATUS, &data);
198 		if ((data & modu_ctrl) == 0)
199 			return 0;
200 		msleep(1);
201 	}
202 	return data;
203 }
204 
205 /**
206  * atl1c_phy_config - Timer Call-back
207  * @t: timer list containing pointer to netdev cast into an unsigned long
208  */
209 static void atl1c_phy_config(struct timer_list *t)
210 {
211 	struct atl1c_adapter *adapter = from_timer(adapter, t,
212 						   phy_config_timer);
213 	struct atl1c_hw *hw = &adapter->hw;
214 	unsigned long flags;
215 
216 	spin_lock_irqsave(&adapter->mdio_lock, flags);
217 	atl1c_restart_autoneg(hw);
218 	spin_unlock_irqrestore(&adapter->mdio_lock, flags);
219 }
220 
221 void atl1c_reinit_locked(struct atl1c_adapter *adapter)
222 {
223 	atl1c_down(adapter);
224 	atl1c_up(adapter);
225 	clear_bit(__AT_RESETTING, &adapter->flags);
226 }
227 
228 static void atl1c_check_link_status(struct atl1c_adapter *adapter)
229 {
230 	struct atl1c_hw *hw = &adapter->hw;
231 	struct net_device *netdev = adapter->netdev;
232 	struct pci_dev    *pdev   = adapter->pdev;
233 	int err;
234 	unsigned long flags;
235 	u16 speed, duplex;
236 	bool link;
237 
238 	spin_lock_irqsave(&adapter->mdio_lock, flags);
239 	link = atl1c_get_link_status(hw);
240 	spin_unlock_irqrestore(&adapter->mdio_lock, flags);
241 
242 	if (!link) {
243 		/* link down */
244 		netif_carrier_off(netdev);
245 		hw->hibernate = true;
246 		if (atl1c_reset_mac(hw) != 0)
247 			if (netif_msg_hw(adapter))
248 				dev_warn(&pdev->dev, "reset mac failed\n");
249 		atl1c_set_aspm(hw, SPEED_0);
250 		atl1c_post_phy_linkchg(hw, SPEED_0);
251 		atl1c_reset_dma_ring(adapter);
252 		atl1c_configure(adapter);
253 	} else {
254 		/* Link Up */
255 		hw->hibernate = false;
256 		spin_lock_irqsave(&adapter->mdio_lock, flags);
257 		err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
258 		spin_unlock_irqrestore(&adapter->mdio_lock, flags);
259 		if (unlikely(err))
260 			return;
261 		/* link result is our setting */
262 		if (adapter->link_speed != speed ||
263 		    adapter->link_duplex != duplex) {
264 			adapter->link_speed  = speed;
265 			adapter->link_duplex = duplex;
266 			atl1c_set_aspm(hw, speed);
267 			atl1c_post_phy_linkchg(hw, speed);
268 			atl1c_start_mac(adapter);
269 			if (netif_msg_link(adapter))
270 				dev_info(&pdev->dev,
271 					"%s: %s NIC Link is Up<%d Mbps %s>\n",
272 					atl1c_driver_name, netdev->name,
273 					adapter->link_speed,
274 					adapter->link_duplex == FULL_DUPLEX ?
275 					"Full Duplex" : "Half Duplex");
276 		}
277 		if (!netif_carrier_ok(netdev))
278 			netif_carrier_on(netdev);
279 	}
280 }
281 
282 static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
283 {
284 	struct net_device *netdev = adapter->netdev;
285 	struct pci_dev    *pdev   = adapter->pdev;
286 	bool link;
287 
288 	spin_lock(&adapter->mdio_lock);
289 	link = atl1c_get_link_status(&adapter->hw);
290 	spin_unlock(&adapter->mdio_lock);
291 	/* notify upper layer link down ASAP */
292 	if (!link) {
293 		if (netif_carrier_ok(netdev)) {
294 			/* old link state: Up */
295 			netif_carrier_off(netdev);
296 			if (netif_msg_link(adapter))
297 				dev_info(&pdev->dev,
298 					"%s: %s NIC Link is Down\n",
299 					atl1c_driver_name, netdev->name);
300 			adapter->link_speed = SPEED_0;
301 		}
302 	}
303 
304 	set_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event);
305 	schedule_work(&adapter->common_task);
306 }
307 
308 static void atl1c_common_task(struct work_struct *work)
309 {
310 	struct atl1c_adapter *adapter;
311 	struct net_device *netdev;
312 
313 	adapter = container_of(work, struct atl1c_adapter, common_task);
314 	netdev = adapter->netdev;
315 
316 	if (test_bit(__AT_DOWN, &adapter->flags))
317 		return;
318 
319 	if (test_and_clear_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event)) {
320 		netif_device_detach(netdev);
321 		atl1c_down(adapter);
322 		atl1c_up(adapter);
323 		netif_device_attach(netdev);
324 	}
325 
326 	if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE,
327 		&adapter->work_event)) {
328 		atl1c_irq_disable(adapter);
329 		atl1c_check_link_status(adapter);
330 		atl1c_irq_enable(adapter);
331 	}
332 }
333 
334 
335 static void atl1c_del_timer(struct atl1c_adapter *adapter)
336 {
337 	del_timer_sync(&adapter->phy_config_timer);
338 }
339 
340 
341 /**
342  * atl1c_tx_timeout - Respond to a Tx Hang
343  * @netdev: network interface device structure
344  * @txqueue: index of hanging tx queue
345  */
346 static void atl1c_tx_timeout(struct net_device *netdev, unsigned int txqueue)
347 {
348 	struct atl1c_adapter *adapter = netdev_priv(netdev);
349 
350 	/* Do the reset outside of interrupt context */
351 	set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
352 	schedule_work(&adapter->common_task);
353 }
354 
355 /**
356  * atl1c_set_multi - Multicast and Promiscuous mode set
357  * @netdev: network interface device structure
358  *
359  * The set_multi entry point is called whenever the multicast address
360  * list or the network interface flags are updated.  This routine is
361  * responsible for configuring the hardware for proper multicast,
362  * promiscuous mode, and all-multi behavior.
363  */
364 static void atl1c_set_multi(struct net_device *netdev)
365 {
366 	struct atl1c_adapter *adapter = netdev_priv(netdev);
367 	struct atl1c_hw *hw = &adapter->hw;
368 	struct netdev_hw_addr *ha;
369 	u32 mac_ctrl_data;
370 	u32 hash_value;
371 
372 	/* Check for Promiscuous and All Multicast modes */
373 	AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
374 
375 	if (netdev->flags & IFF_PROMISC) {
376 		mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
377 	} else if (netdev->flags & IFF_ALLMULTI) {
378 		mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
379 		mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
380 	} else {
381 		mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
382 	}
383 
384 	AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
385 
386 	/* clear the old settings from the multicast hash table */
387 	AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
388 	AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
389 
390 	/* comoute mc addresses' hash value ,and put it into hash table */
391 	netdev_for_each_mc_addr(ha, netdev) {
392 		hash_value = atl1c_hash_mc_addr(hw, ha->addr);
393 		atl1c_hash_set(hw, hash_value);
394 	}
395 }
396 
397 static void __atl1c_vlan_mode(netdev_features_t features, u32 *mac_ctrl_data)
398 {
399 	if (features & NETIF_F_HW_VLAN_CTAG_RX) {
400 		/* enable VLAN tag insert/strip */
401 		*mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
402 	} else {
403 		/* disable VLAN tag insert/strip */
404 		*mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
405 	}
406 }
407 
408 static void atl1c_vlan_mode(struct net_device *netdev,
409 	netdev_features_t features)
410 {
411 	struct atl1c_adapter *adapter = netdev_priv(netdev);
412 	struct pci_dev *pdev = adapter->pdev;
413 	u32 mac_ctrl_data = 0;
414 
415 	if (netif_msg_pktdata(adapter))
416 		dev_dbg(&pdev->dev, "atl1c_vlan_mode\n");
417 
418 	atl1c_irq_disable(adapter);
419 	AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
420 	__atl1c_vlan_mode(features, &mac_ctrl_data);
421 	AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
422 	atl1c_irq_enable(adapter);
423 }
424 
425 static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
426 {
427 	struct pci_dev *pdev = adapter->pdev;
428 
429 	if (netif_msg_pktdata(adapter))
430 		dev_dbg(&pdev->dev, "atl1c_restore_vlan\n");
431 	atl1c_vlan_mode(adapter->netdev, adapter->netdev->features);
432 }
433 
434 /**
435  * atl1c_set_mac_addr - Change the Ethernet Address of the NIC
436  * @netdev: network interface device structure
437  * @p: pointer to an address structure
438  *
439  * Returns 0 on success, negative on failure
440  */
441 static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
442 {
443 	struct atl1c_adapter *adapter = netdev_priv(netdev);
444 	struct sockaddr *addr = p;
445 
446 	if (!is_valid_ether_addr(addr->sa_data))
447 		return -EADDRNOTAVAIL;
448 
449 	if (netif_running(netdev))
450 		return -EBUSY;
451 
452 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
453 	memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
454 
455 	atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.mac_addr);
456 
457 	return 0;
458 }
459 
460 static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
461 				struct net_device *dev)
462 {
463 	unsigned int head_size;
464 	int mtu = dev->mtu;
465 
466 	adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
467 		roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
468 
469 	head_size = SKB_DATA_ALIGN(adapter->rx_buffer_len + NET_SKB_PAD + NET_IP_ALIGN) +
470 		    SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
471 	adapter->rx_frag_size = roundup_pow_of_two(head_size);
472 }
473 
474 static netdev_features_t atl1c_fix_features(struct net_device *netdev,
475 	netdev_features_t features)
476 {
477 	struct atl1c_adapter *adapter = netdev_priv(netdev);
478 	struct atl1c_hw *hw = &adapter->hw;
479 
480 	/*
481 	 * Since there is no support for separate rx/tx vlan accel
482 	 * enable/disable make sure tx flag is always in same state as rx.
483 	 */
484 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
485 		features |= NETIF_F_HW_VLAN_CTAG_TX;
486 	else
487 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
488 
489 	if (hw->nic_type != athr_mt) {
490 		if (netdev->mtu > MAX_TSO_FRAME_SIZE)
491 			features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
492 	}
493 
494 	return features;
495 }
496 
497 static int atl1c_set_features(struct net_device *netdev,
498 	netdev_features_t features)
499 {
500 	netdev_features_t changed = netdev->features ^ features;
501 
502 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
503 		atl1c_vlan_mode(netdev, features);
504 
505 	return 0;
506 }
507 
508 static void atl1c_set_max_mtu(struct net_device *netdev)
509 {
510 	struct atl1c_adapter *adapter = netdev_priv(netdev);
511 	struct atl1c_hw *hw = &adapter->hw;
512 
513 	switch (hw->nic_type) {
514 	/* These (GbE) devices support jumbo packets, max_mtu 6122 */
515 	case athr_l1c:
516 	case athr_l1d:
517 	case athr_l1d_2:
518 		netdev->max_mtu = MAX_JUMBO_FRAME_SIZE -
519 			(ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
520 		break;
521 	case athr_mt:
522 		netdev->max_mtu = 9500;
523 		break;
524 		/* The 10/100 devices don't support jumbo packets, max_mtu 1500 */
525 	default:
526 		netdev->max_mtu = ETH_DATA_LEN;
527 		break;
528 	}
529 }
530 
531 /**
532  * atl1c_change_mtu - Change the Maximum Transfer Unit
533  * @netdev: network interface device structure
534  * @new_mtu: new value for maximum frame size
535  *
536  * Returns 0 on success, negative on failure
537  */
538 static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
539 {
540 	struct atl1c_adapter *adapter = netdev_priv(netdev);
541 
542 	/* set MTU */
543 	if (netif_running(netdev)) {
544 		while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
545 			msleep(1);
546 		netdev->mtu = new_mtu;
547 		adapter->hw.max_frame_size = new_mtu;
548 		atl1c_set_rxbufsize(adapter, netdev);
549 		atl1c_down(adapter);
550 		netdev_update_features(netdev);
551 		atl1c_up(adapter);
552 		clear_bit(__AT_RESETTING, &adapter->flags);
553 	}
554 	return 0;
555 }
556 
557 /*
558  *  caller should hold mdio_lock
559  */
560 static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
561 {
562 	struct atl1c_adapter *adapter = netdev_priv(netdev);
563 	u16 result;
564 
565 	atl1c_read_phy_reg(&adapter->hw, reg_num, &result);
566 	return result;
567 }
568 
569 static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
570 			     int reg_num, int val)
571 {
572 	struct atl1c_adapter *adapter = netdev_priv(netdev);
573 
574 	atl1c_write_phy_reg(&adapter->hw, reg_num, val);
575 }
576 
577 static int atl1c_mii_ioctl(struct net_device *netdev,
578 			   struct ifreq *ifr, int cmd)
579 {
580 	struct atl1c_adapter *adapter = netdev_priv(netdev);
581 	struct pci_dev *pdev = adapter->pdev;
582 	struct mii_ioctl_data *data = if_mii(ifr);
583 	unsigned long flags;
584 	int retval = 0;
585 
586 	if (!netif_running(netdev))
587 		return -EINVAL;
588 
589 	spin_lock_irqsave(&adapter->mdio_lock, flags);
590 	switch (cmd) {
591 	case SIOCGMIIPHY:
592 		data->phy_id = 0;
593 		break;
594 
595 	case SIOCGMIIREG:
596 		if (atl1c_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
597 				    &data->val_out)) {
598 			retval = -EIO;
599 			goto out;
600 		}
601 		break;
602 
603 	case SIOCSMIIREG:
604 		if (data->reg_num & ~(0x1F)) {
605 			retval = -EFAULT;
606 			goto out;
607 		}
608 
609 		dev_dbg(&pdev->dev, "<atl1c_mii_ioctl> write %x %x",
610 				data->reg_num, data->val_in);
611 		if (atl1c_write_phy_reg(&adapter->hw,
612 				     data->reg_num, data->val_in)) {
613 			retval = -EIO;
614 			goto out;
615 		}
616 		break;
617 
618 	default:
619 		retval = -EOPNOTSUPP;
620 		break;
621 	}
622 out:
623 	spin_unlock_irqrestore(&adapter->mdio_lock, flags);
624 	return retval;
625 }
626 
627 static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
628 {
629 	switch (cmd) {
630 	case SIOCGMIIPHY:
631 	case SIOCGMIIREG:
632 	case SIOCSMIIREG:
633 		return atl1c_mii_ioctl(netdev, ifr, cmd);
634 	default:
635 		return -EOPNOTSUPP;
636 	}
637 }
638 
639 /**
640  * atl1c_alloc_queues - Allocate memory for all rings
641  * @adapter: board private structure to initialize
642  *
643  */
644 static int atl1c_alloc_queues(struct atl1c_adapter *adapter)
645 {
646 	return 0;
647 }
648 
649 static void atl1c_set_mac_type(struct atl1c_hw *hw)
650 {
651 	u32 magic;
652 	switch (hw->device_id) {
653 	case PCI_DEVICE_ID_ATTANSIC_L2C:
654 		hw->nic_type = athr_l2c;
655 		break;
656 	case PCI_DEVICE_ID_ATTANSIC_L1C:
657 		hw->nic_type = athr_l1c;
658 		break;
659 	case PCI_DEVICE_ID_ATHEROS_L2C_B:
660 		hw->nic_type = athr_l2c_b;
661 		break;
662 	case PCI_DEVICE_ID_ATHEROS_L2C_B2:
663 		hw->nic_type = athr_l2c_b2;
664 		break;
665 	case PCI_DEVICE_ID_ATHEROS_L1D:
666 		hw->nic_type = athr_l1d;
667 		break;
668 	case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
669 		hw->nic_type = athr_l1d_2;
670 		AT_READ_REG(hw, REG_MT_MAGIC, &magic);
671 		if (magic == MT_MAGIC)
672 			hw->nic_type = athr_mt;
673 		break;
674 	default:
675 		break;
676 	}
677 }
678 
679 static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
680 {
681 	u32 link_ctrl_data;
682 
683 	atl1c_set_mac_type(hw);
684 	AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
685 
686 	hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE  |
687 			 ATL1C_TXQ_MODE_ENHANCE;
688 	hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT |
689 			  ATL1C_ASPM_L1_SUPPORT;
690 	hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
691 
692 	if (hw->nic_type == athr_l1c ||
693 	    hw->nic_type == athr_l1d ||
694 	    hw->nic_type == athr_l1d_2)
695 		hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
696 	return 0;
697 }
698 
699 struct atl1c_platform_patch {
700 	u16 pci_did;
701 	u8  pci_revid;
702 	u16 subsystem_vid;
703 	u16 subsystem_did;
704 	u32 patch_flag;
705 #define ATL1C_LINK_PATCH	0x1
706 };
707 static const struct atl1c_platform_patch plats[] = {
708 {0x2060, 0xC1, 0x1019, 0x8152, 0x1},
709 {0x2060, 0xC1, 0x1019, 0x2060, 0x1},
710 {0x2060, 0xC1, 0x1019, 0xE000, 0x1},
711 {0x2062, 0xC0, 0x1019, 0x8152, 0x1},
712 {0x2062, 0xC0, 0x1019, 0x2062, 0x1},
713 {0x2062, 0xC0, 0x1458, 0xE000, 0x1},
714 {0x2062, 0xC1, 0x1019, 0x8152, 0x1},
715 {0x2062, 0xC1, 0x1019, 0x2062, 0x1},
716 {0x2062, 0xC1, 0x1458, 0xE000, 0x1},
717 {0x2062, 0xC1, 0x1565, 0x2802, 0x1},
718 {0x2062, 0xC1, 0x1565, 0x2801, 0x1},
719 {0x1073, 0xC0, 0x1019, 0x8151, 0x1},
720 {0x1073, 0xC0, 0x1019, 0x1073, 0x1},
721 {0x1073, 0xC0, 0x1458, 0xE000, 0x1},
722 {0x1083, 0xC0, 0x1458, 0xE000, 0x1},
723 {0x1083, 0xC0, 0x1019, 0x8151, 0x1},
724 {0x1083, 0xC0, 0x1019, 0x1083, 0x1},
725 {0x1083, 0xC0, 0x1462, 0x7680, 0x1},
726 {0x1083, 0xC0, 0x1565, 0x2803, 0x1},
727 {0},
728 };
729 
730 static void atl1c_patch_assign(struct atl1c_hw *hw)
731 {
732 	struct pci_dev	*pdev = hw->adapter->pdev;
733 	u32 misc_ctrl;
734 	int i = 0;
735 
736 	hw->msi_lnkpatch = false;
737 
738 	while (plats[i].pci_did != 0) {
739 		if (plats[i].pci_did == hw->device_id &&
740 		    plats[i].pci_revid == hw->revision_id &&
741 		    plats[i].subsystem_vid == hw->subsystem_vendor_id &&
742 		    plats[i].subsystem_did == hw->subsystem_id) {
743 			if (plats[i].patch_flag & ATL1C_LINK_PATCH)
744 				hw->msi_lnkpatch = true;
745 		}
746 		i++;
747 	}
748 
749 	if (hw->device_id == PCI_DEVICE_ID_ATHEROS_L2C_B2 &&
750 	    hw->revision_id == L2CB_V21) {
751 		/* config access mode */
752 		pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
753 				       REG_PCIE_DEV_MISC_CTRL);
754 		pci_read_config_dword(pdev, REG_PCIE_IND_ACC_DATA, &misc_ctrl);
755 		misc_ctrl &= ~0x100;
756 		pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
757 				       REG_PCIE_DEV_MISC_CTRL);
758 		pci_write_config_dword(pdev, REG_PCIE_IND_ACC_DATA, misc_ctrl);
759 	}
760 }
761 /**
762  * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
763  * @adapter: board private structure to initialize
764  *
765  * atl1c_sw_init initializes the Adapter private data structure.
766  * Fields are initialized based on PCI device information and
767  * OS network device settings (MTU size).
768  */
769 static int atl1c_sw_init(struct atl1c_adapter *adapter)
770 {
771 	struct atl1c_hw *hw   = &adapter->hw;
772 	struct pci_dev	*pdev = adapter->pdev;
773 	u32 revision;
774 
775 
776 	adapter->wol = 0;
777 	device_set_wakeup_enable(&pdev->dev, false);
778 	adapter->link_speed = SPEED_0;
779 	adapter->link_duplex = FULL_DUPLEX;
780 	adapter->tpd_ring[0].count = 1024;
781 	adapter->rfd_ring.count = 512;
782 
783 	hw->vendor_id = pdev->vendor;
784 	hw->device_id = pdev->device;
785 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
786 	hw->subsystem_id = pdev->subsystem_device;
787 	pci_read_config_dword(pdev, PCI_CLASS_REVISION, &revision);
788 	hw->revision_id = revision & 0xFF;
789 	/* before link up, we assume hibernate is true */
790 	hw->hibernate = true;
791 	hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
792 	if (atl1c_setup_mac_funcs(hw) != 0) {
793 		dev_err(&pdev->dev, "set mac function pointers failed\n");
794 		return -1;
795 	}
796 	atl1c_patch_assign(hw);
797 
798 	hw->intr_mask = IMR_NORMAL_MASK;
799 	hw->phy_configured = false;
800 	hw->preamble_len = 7;
801 	hw->max_frame_size = adapter->netdev->mtu;
802 	hw->autoneg_advertised = ADVERTISED_Autoneg;
803 	hw->indirect_tab = 0xE4E4E4E4;
804 	hw->base_cpu = 0;
805 
806 	hw->ict = 50000;		/* 100ms */
807 	hw->smb_timer = 200000;	  	/* 400ms */
808 	hw->rx_imt = 200;
809 	hw->tx_imt = 1000;
810 
811 	hw->tpd_burst = 5;
812 	hw->rfd_burst = 8;
813 	hw->dma_order = atl1c_dma_ord_out;
814 	hw->dmar_block = atl1c_dma_req_1024;
815 
816 	if (atl1c_alloc_queues(adapter)) {
817 		dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
818 		return -ENOMEM;
819 	}
820 	/* TODO */
821 	atl1c_set_rxbufsize(adapter, adapter->netdev);
822 	atomic_set(&adapter->irq_sem, 1);
823 	spin_lock_init(&adapter->mdio_lock);
824 	spin_lock_init(&adapter->hw.intr_mask_lock);
825 	set_bit(__AT_DOWN, &adapter->flags);
826 
827 	return 0;
828 }
829 
830 static inline void atl1c_clean_buffer(struct pci_dev *pdev,
831 				struct atl1c_buffer *buffer_info)
832 {
833 	u16 pci_driection;
834 	if (buffer_info->flags & ATL1C_BUFFER_FREE)
835 		return;
836 	if (buffer_info->dma) {
837 		if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
838 			pci_driection = DMA_FROM_DEVICE;
839 		else
840 			pci_driection = DMA_TO_DEVICE;
841 
842 		if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
843 			dma_unmap_single(&pdev->dev, buffer_info->dma,
844 					 buffer_info->length, pci_driection);
845 		else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
846 			dma_unmap_page(&pdev->dev, buffer_info->dma,
847 				       buffer_info->length, pci_driection);
848 	}
849 	if (buffer_info->skb)
850 		dev_consume_skb_any(buffer_info->skb);
851 	buffer_info->dma = 0;
852 	buffer_info->skb = NULL;
853 	ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
854 }
855 /**
856  * atl1c_clean_tx_ring - Free Tx-skb
857  * @adapter: board private structure
858  * @type: type of transmit queue
859  */
860 static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
861 				enum atl1c_trans_queue type)
862 {
863 	struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
864 	struct atl1c_buffer *buffer_info;
865 	struct pci_dev *pdev = adapter->pdev;
866 	u16 index, ring_count;
867 
868 	ring_count = tpd_ring->count;
869 	for (index = 0; index < ring_count; index++) {
870 		buffer_info = &tpd_ring->buffer_info[index];
871 		atl1c_clean_buffer(pdev, buffer_info);
872 	}
873 
874 	netdev_reset_queue(adapter->netdev);
875 
876 	/* Zero out Tx-buffers */
877 	memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
878 		ring_count);
879 	atomic_set(&tpd_ring->next_to_clean, 0);
880 	tpd_ring->next_to_use = 0;
881 }
882 
883 /**
884  * atl1c_clean_rx_ring - Free rx-reservation skbs
885  * @adapter: board private structure
886  */
887 static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
888 {
889 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
890 	struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
891 	struct atl1c_buffer *buffer_info;
892 	struct pci_dev *pdev = adapter->pdev;
893 	int j;
894 
895 	for (j = 0; j < rfd_ring->count; j++) {
896 		buffer_info = &rfd_ring->buffer_info[j];
897 		atl1c_clean_buffer(pdev, buffer_info);
898 	}
899 	/* zero out the descriptor ring */
900 	memset(rfd_ring->desc, 0, rfd_ring->size);
901 	rfd_ring->next_to_clean = 0;
902 	rfd_ring->next_to_use = 0;
903 	rrd_ring->next_to_use = 0;
904 	rrd_ring->next_to_clean = 0;
905 }
906 
907 /*
908  * Read / Write Ptr Initialize:
909  */
910 static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
911 {
912 	struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
913 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
914 	struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
915 	struct atl1c_buffer *buffer_info;
916 	int i, j;
917 
918 	for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
919 		tpd_ring[i].next_to_use = 0;
920 		atomic_set(&tpd_ring[i].next_to_clean, 0);
921 		buffer_info = tpd_ring[i].buffer_info;
922 		for (j = 0; j < tpd_ring->count; j++)
923 			ATL1C_SET_BUFFER_STATE(&buffer_info[i],
924 					ATL1C_BUFFER_FREE);
925 	}
926 	rfd_ring->next_to_use = 0;
927 	rfd_ring->next_to_clean = 0;
928 	rrd_ring->next_to_use = 0;
929 	rrd_ring->next_to_clean = 0;
930 	for (j = 0; j < rfd_ring->count; j++) {
931 		buffer_info = &rfd_ring->buffer_info[j];
932 		ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
933 	}
934 }
935 
936 /**
937  * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
938  * @adapter: board private structure
939  *
940  * Free all transmit software resources
941  */
942 static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
943 {
944 	struct pci_dev *pdev = adapter->pdev;
945 
946 	dma_free_coherent(&pdev->dev, adapter->ring_header.size,
947 			  adapter->ring_header.desc, adapter->ring_header.dma);
948 	adapter->ring_header.desc = NULL;
949 
950 	/* Note: just free tdp_ring.buffer_info,
951 	*  it contain rfd_ring.buffer_info, do not double free */
952 	if (adapter->tpd_ring[0].buffer_info) {
953 		kfree(adapter->tpd_ring[0].buffer_info);
954 		adapter->tpd_ring[0].buffer_info = NULL;
955 	}
956 	if (adapter->rx_page) {
957 		put_page(adapter->rx_page);
958 		adapter->rx_page = NULL;
959 	}
960 }
961 
962 /**
963  * atl1c_setup_ring_resources - allocate Tx / RX descriptor resources
964  * @adapter: board private structure
965  *
966  * Return 0 on success, negative on failure
967  */
968 static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
969 {
970 	struct pci_dev *pdev = adapter->pdev;
971 	struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
972 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
973 	struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
974 	struct atl1c_ring_header *ring_header = &adapter->ring_header;
975 	int size;
976 	int i;
977 	int count = 0;
978 	int rx_desc_count = 0;
979 	u32 offset = 0;
980 
981 	rrd_ring->count = rfd_ring->count;
982 	for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
983 		tpd_ring[i].count = tpd_ring[0].count;
984 
985 	/* 2 tpd queue, one high priority queue,
986 	 * another normal priority queue */
987 	size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
988 		rfd_ring->count);
989 	tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
990 	if (unlikely(!tpd_ring->buffer_info))
991 		goto err_nomem;
992 
993 	for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
994 		tpd_ring[i].buffer_info =
995 			(tpd_ring->buffer_info + count);
996 		count += tpd_ring[i].count;
997 	}
998 
999 	rfd_ring->buffer_info =
1000 		(tpd_ring->buffer_info + count);
1001 	count += rfd_ring->count;
1002 	rx_desc_count += rfd_ring->count;
1003 
1004 	/*
1005 	 * real ring DMA buffer
1006 	 * each ring/block may need up to 8 bytes for alignment, hence the
1007 	 * additional bytes tacked onto the end.
1008 	 */
1009 	ring_header->size = size =
1010 		sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
1011 		sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
1012 		sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
1013 		8 * 4;
1014 
1015 	ring_header->desc = dma_alloc_coherent(&pdev->dev, ring_header->size,
1016 					       &ring_header->dma, GFP_KERNEL);
1017 	if (unlikely(!ring_header->desc)) {
1018 		dev_err(&pdev->dev, "could not get memory for DMA buffer\n");
1019 		goto err_nomem;
1020 	}
1021 	/* init TPD ring */
1022 
1023 	tpd_ring[0].dma = roundup(ring_header->dma, 8);
1024 	offset = tpd_ring[0].dma - ring_header->dma;
1025 	for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
1026 		tpd_ring[i].dma = ring_header->dma + offset;
1027 		tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
1028 		tpd_ring[i].size =
1029 			sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
1030 		offset += roundup(tpd_ring[i].size, 8);
1031 	}
1032 	/* init RFD ring */
1033 	rfd_ring->dma = ring_header->dma + offset;
1034 	rfd_ring->desc = (u8 *) ring_header->desc + offset;
1035 	rfd_ring->size = sizeof(struct atl1c_rx_free_desc) * rfd_ring->count;
1036 	offset += roundup(rfd_ring->size, 8);
1037 
1038 	/* init RRD ring */
1039 	rrd_ring->dma = ring_header->dma + offset;
1040 	rrd_ring->desc = (u8 *) ring_header->desc + offset;
1041 	rrd_ring->size = sizeof(struct atl1c_recv_ret_status) *
1042 		rrd_ring->count;
1043 	offset += roundup(rrd_ring->size, 8);
1044 
1045 	return 0;
1046 
1047 err_nomem:
1048 	kfree(tpd_ring->buffer_info);
1049 	return -ENOMEM;
1050 }
1051 
1052 static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
1053 {
1054 	struct atl1c_hw *hw = &adapter->hw;
1055 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1056 	struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
1057 	struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1058 				adapter->tpd_ring;
1059 
1060 	/* TPD */
1061 	AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
1062 			(u32)((tpd_ring[atl1c_trans_normal].dma &
1063 				AT_DMA_HI_ADDR_MASK) >> 32));
1064 	/* just enable normal priority TX queue */
1065 	AT_WRITE_REG(hw, REG_TPD_PRI0_ADDR_LO,
1066 			(u32)(tpd_ring[atl1c_trans_normal].dma &
1067 				AT_DMA_LO_ADDR_MASK));
1068 	AT_WRITE_REG(hw, REG_TPD_PRI1_ADDR_LO,
1069 			(u32)(tpd_ring[atl1c_trans_high].dma &
1070 				AT_DMA_LO_ADDR_MASK));
1071 	AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
1072 			(u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
1073 
1074 
1075 	/* RFD */
1076 	AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
1077 			(u32)((rfd_ring->dma & AT_DMA_HI_ADDR_MASK) >> 32));
1078 	AT_WRITE_REG(hw, REG_RFD0_HEAD_ADDR_LO,
1079 			(u32)(rfd_ring->dma & AT_DMA_LO_ADDR_MASK));
1080 
1081 	AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
1082 			rfd_ring->count & RFD_RING_SIZE_MASK);
1083 	AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
1084 			adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
1085 
1086 	/* RRD */
1087 	AT_WRITE_REG(hw, REG_RRD0_HEAD_ADDR_LO,
1088 			(u32)(rrd_ring->dma & AT_DMA_LO_ADDR_MASK));
1089 	AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
1090 			(rrd_ring->count & RRD_RING_SIZE_MASK));
1091 
1092 	if (hw->nic_type == athr_l2c_b) {
1093 		AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
1094 		AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
1095 		AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
1096 		AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
1097 		AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
1098 		AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
1099 		AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0);	/* TX watermark, to enter l1 state.*/
1100 		AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0);		/* RXD threshold.*/
1101 	}
1102 	/* Load all of base address above */
1103 	AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
1104 }
1105 
1106 static void atl1c_configure_tx(struct atl1c_adapter *adapter)
1107 {
1108 	struct atl1c_hw *hw = &adapter->hw;
1109 	int max_pay_load;
1110 	u16 tx_offload_thresh;
1111 	u32 txq_ctrl_data;
1112 
1113 	tx_offload_thresh = MAX_TSO_FRAME_SIZE;
1114 	AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
1115 		(tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
1116 	max_pay_load = pcie_get_readrq(adapter->pdev) >> 8;
1117 	hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
1118 	/*
1119 	 * if BIOS had changed the dam-read-max-length to an invalid value,
1120 	 * restore it to default value
1121 	 */
1122 	if (hw->dmar_block < DEVICE_CTRL_MAXRRS_MIN) {
1123 		pcie_set_readrq(adapter->pdev, 128 << DEVICE_CTRL_MAXRRS_MIN);
1124 		hw->dmar_block = DEVICE_CTRL_MAXRRS_MIN;
1125 	}
1126 	txq_ctrl_data =
1127 		hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2 ?
1128 		L2CB_TXQ_CFGV : L1C_TXQ_CFGV;
1129 
1130 	AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
1131 }
1132 
1133 static void atl1c_configure_rx(struct atl1c_adapter *adapter)
1134 {
1135 	struct atl1c_hw *hw = &adapter->hw;
1136 	u32 rxq_ctrl_data;
1137 
1138 	rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
1139 			RXQ_RFD_BURST_NUM_SHIFT;
1140 
1141 	if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
1142 		rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
1143 
1144 	/* aspm for gigabit */
1145 	if (hw->nic_type != athr_l1d_2 && (hw->device_id & 1) != 0)
1146 		rxq_ctrl_data = FIELD_SETX(rxq_ctrl_data, ASPM_THRUPUT_LIMIT,
1147 			ASPM_THRUPUT_LIMIT_100M);
1148 
1149 	AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
1150 }
1151 
1152 static void atl1c_configure_dma(struct atl1c_adapter *adapter)
1153 {
1154 	struct atl1c_hw *hw = &adapter->hw;
1155 	u32 dma_ctrl_data;
1156 
1157 	dma_ctrl_data = FIELDX(DMA_CTRL_RORDER_MODE, DMA_CTRL_RORDER_MODE_OUT) |
1158 		DMA_CTRL_RREQ_PRI_DATA |
1159 		FIELDX(DMA_CTRL_RREQ_BLEN, hw->dmar_block) |
1160 		FIELDX(DMA_CTRL_WDLY_CNT, DMA_CTRL_WDLY_CNT_DEF) |
1161 		FIELDX(DMA_CTRL_RDLY_CNT, DMA_CTRL_RDLY_CNT_DEF);
1162 
1163 	AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
1164 }
1165 
1166 /*
1167  * Stop the mac, transmit and receive units
1168  * hw - Struct containing variables accessed by shared code
1169  * return : 0  or  idle status (if error)
1170  */
1171 static int atl1c_stop_mac(struct atl1c_hw *hw)
1172 {
1173 	u32 data;
1174 
1175 	AT_READ_REG(hw, REG_RXQ_CTRL, &data);
1176 	data &= ~RXQ_CTRL_EN;
1177 	AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1178 
1179 	AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1180 	data &= ~TXQ_CTRL_EN;
1181 	AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
1182 
1183 	atl1c_wait_until_idle(hw, IDLE_STATUS_RXQ_BUSY | IDLE_STATUS_TXQ_BUSY);
1184 
1185 	AT_READ_REG(hw, REG_MAC_CTRL, &data);
1186 	data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
1187 	AT_WRITE_REG(hw, REG_MAC_CTRL, data);
1188 
1189 	return (int)atl1c_wait_until_idle(hw,
1190 		IDLE_STATUS_TXMAC_BUSY | IDLE_STATUS_RXMAC_BUSY);
1191 }
1192 
1193 static void atl1c_start_mac(struct atl1c_adapter *adapter)
1194 {
1195 	struct atl1c_hw *hw = &adapter->hw;
1196 	u32 mac, txq, rxq;
1197 
1198 	hw->mac_duplex = adapter->link_duplex == FULL_DUPLEX;
1199 	hw->mac_speed = adapter->link_speed == SPEED_1000 ?
1200 		atl1c_mac_speed_1000 : atl1c_mac_speed_10_100;
1201 
1202 	AT_READ_REG(hw, REG_TXQ_CTRL, &txq);
1203 	AT_READ_REG(hw, REG_RXQ_CTRL, &rxq);
1204 	AT_READ_REG(hw, REG_MAC_CTRL, &mac);
1205 
1206 	txq |= TXQ_CTRL_EN;
1207 	rxq |= RXQ_CTRL_EN;
1208 	mac |= MAC_CTRL_TX_EN | MAC_CTRL_TX_FLOW |
1209 	       MAC_CTRL_RX_EN | MAC_CTRL_RX_FLOW |
1210 	       MAC_CTRL_ADD_CRC | MAC_CTRL_PAD |
1211 	       MAC_CTRL_BC_EN | MAC_CTRL_SINGLE_PAUSE_EN |
1212 	       MAC_CTRL_HASH_ALG_CRC32;
1213 	if (hw->mac_duplex)
1214 		mac |= MAC_CTRL_DUPLX;
1215 	else
1216 		mac &= ~MAC_CTRL_DUPLX;
1217 	mac = FIELD_SETX(mac, MAC_CTRL_SPEED, hw->mac_speed);
1218 	mac = FIELD_SETX(mac, MAC_CTRL_PRMLEN, hw->preamble_len);
1219 
1220 	AT_WRITE_REG(hw, REG_TXQ_CTRL, txq);
1221 	AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq);
1222 	AT_WRITE_REG(hw, REG_MAC_CTRL, mac);
1223 }
1224 
1225 /*
1226  * Reset the transmit and receive units; mask and clear all interrupts.
1227  * hw - Struct containing variables accessed by shared code
1228  * return : 0  or  idle status (if error)
1229  */
1230 static int atl1c_reset_mac(struct atl1c_hw *hw)
1231 {
1232 	struct atl1c_adapter *adapter = hw->adapter;
1233 	struct pci_dev *pdev = adapter->pdev;
1234 	u32 ctrl_data = 0;
1235 
1236 	atl1c_stop_mac(hw);
1237 	/*
1238 	 * Issue Soft Reset to the MAC.  This will reset the chip's
1239 	 * transmit, receive, DMA.  It will not effect
1240 	 * the current PCI configuration.  The global reset bit is self-
1241 	 * clearing, and should clear within a microsecond.
1242 	 */
1243 	AT_READ_REG(hw, REG_MASTER_CTRL, &ctrl_data);
1244 	ctrl_data |= MASTER_CTRL_OOB_DIS;
1245 	AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data | MASTER_CTRL_SOFT_RST);
1246 
1247 	AT_WRITE_FLUSH(hw);
1248 	msleep(10);
1249 	/* Wait at least 10ms for All module to be Idle */
1250 
1251 	if (atl1c_wait_until_idle(hw, IDLE_STATUS_MASK)) {
1252 		dev_err(&pdev->dev,
1253 			"MAC state machine can't be idle since"
1254 			" disabled for 10ms second\n");
1255 		return -1;
1256 	}
1257 	AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data);
1258 
1259 	/* driver control speed/duplex */
1260 	AT_READ_REG(hw, REG_MAC_CTRL, &ctrl_data);
1261 	AT_WRITE_REG(hw, REG_MAC_CTRL, ctrl_data | MAC_CTRL_SPEED_MODE_SW);
1262 
1263 	/* clk switch setting */
1264 	AT_READ_REG(hw, REG_SERDES, &ctrl_data);
1265 	switch (hw->nic_type) {
1266 	case athr_l2c_b:
1267 		ctrl_data &= ~(SERDES_PHY_CLK_SLOWDOWN |
1268 				SERDES_MAC_CLK_SLOWDOWN);
1269 		AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1270 		break;
1271 	case athr_l2c_b2:
1272 	case athr_l1d_2:
1273 		ctrl_data |= SERDES_PHY_CLK_SLOWDOWN | SERDES_MAC_CLK_SLOWDOWN;
1274 		AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1275 		break;
1276 	default:
1277 		break;
1278 	}
1279 
1280 	return 0;
1281 }
1282 
1283 static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
1284 {
1285 	u16 ctrl_flags = hw->ctrl_flags;
1286 
1287 	hw->ctrl_flags &= ~(ATL1C_ASPM_L0S_SUPPORT | ATL1C_ASPM_L1_SUPPORT);
1288 	atl1c_set_aspm(hw, SPEED_0);
1289 	hw->ctrl_flags = ctrl_flags;
1290 }
1291 
1292 /*
1293  * Set ASPM state.
1294  * Enable/disable L0s/L1 depend on link state.
1295  */
1296 static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed)
1297 {
1298 	u32 pm_ctrl_data;
1299 	u32 link_l1_timer;
1300 
1301 	AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
1302 	pm_ctrl_data &= ~(PM_CTRL_ASPM_L1_EN |
1303 			  PM_CTRL_ASPM_L0S_EN |
1304 			  PM_CTRL_MAC_ASPM_CHK);
1305 	/* L1 timer */
1306 	if (hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1307 		pm_ctrl_data &= ~PMCTRL_TXL1_AFTER_L0S;
1308 		link_l1_timer =
1309 			link_speed == SPEED_1000 || link_speed == SPEED_100 ?
1310 			L1D_PMCTRL_L1_ENTRY_TM_16US : 1;
1311 		pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1312 			L1D_PMCTRL_L1_ENTRY_TM, link_l1_timer);
1313 	} else {
1314 		link_l1_timer = hw->nic_type == athr_l2c_b ?
1315 			L2CB1_PM_CTRL_L1_ENTRY_TM : L1C_PM_CTRL_L1_ENTRY_TM;
1316 		if (link_speed != SPEED_1000 && link_speed != SPEED_100)
1317 			link_l1_timer = 1;
1318 		pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1319 			PM_CTRL_L1_ENTRY_TIMER, link_l1_timer);
1320 	}
1321 
1322 	/* L0S/L1 enable */
1323 	if ((hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT) && link_speed != SPEED_0)
1324 		pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN | PM_CTRL_MAC_ASPM_CHK;
1325 	if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
1326 		pm_ctrl_data |= PM_CTRL_ASPM_L1_EN | PM_CTRL_MAC_ASPM_CHK;
1327 
1328 	/* l2cb & l1d & l2cb2 & l1d2 */
1329 	if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
1330 	    hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1331 		pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1332 			PM_CTRL_PM_REQ_TIMER, PM_CTRL_PM_REQ_TO_DEF);
1333 		pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER |
1334 				PM_CTRL_SERDES_PD_EX_L1 |
1335 				PM_CTRL_CLK_SWH_L1;
1336 		pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1337 				  PM_CTRL_SERDES_PLL_L1_EN |
1338 				  PM_CTRL_SERDES_BUFS_RX_L1_EN |
1339 				  PM_CTRL_SA_DLY_EN |
1340 				  PM_CTRL_HOTRST);
1341 		/* disable l0s if link down or l2cb */
1342 		if (link_speed == SPEED_0 || hw->nic_type == athr_l2c_b)
1343 			pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1344 	} else { /* l1c */
1345 		pm_ctrl_data =
1346 			FIELD_SETX(pm_ctrl_data, PM_CTRL_L1_ENTRY_TIMER, 0);
1347 		if (link_speed != SPEED_0) {
1348 			pm_ctrl_data |= PM_CTRL_SERDES_L1_EN |
1349 					PM_CTRL_SERDES_PLL_L1_EN |
1350 					PM_CTRL_SERDES_BUFS_RX_L1_EN;
1351 			pm_ctrl_data &= ~(PM_CTRL_SERDES_PD_EX_L1 |
1352 					  PM_CTRL_CLK_SWH_L1 |
1353 					  PM_CTRL_ASPM_L0S_EN |
1354 					  PM_CTRL_ASPM_L1_EN);
1355 		} else { /* link down */
1356 			pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
1357 			pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1358 					  PM_CTRL_SERDES_PLL_L1_EN |
1359 					  PM_CTRL_SERDES_BUFS_RX_L1_EN |
1360 					  PM_CTRL_ASPM_L0S_EN);
1361 		}
1362 	}
1363 	AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
1364 
1365 	return;
1366 }
1367 
1368 /**
1369  * atl1c_configure_mac - Configure Transmit&Receive Unit after Reset
1370  * @adapter: board private structure
1371  *
1372  * Configure the Tx /Rx unit of the MAC after a reset.
1373  */
1374 static int atl1c_configure_mac(struct atl1c_adapter *adapter)
1375 {
1376 	struct atl1c_hw *hw = &adapter->hw;
1377 	u32 master_ctrl_data = 0;
1378 	u32 intr_modrt_data;
1379 	u32 data;
1380 
1381 	AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
1382 	master_ctrl_data &= ~(MASTER_CTRL_TX_ITIMER_EN |
1383 			      MASTER_CTRL_RX_ITIMER_EN |
1384 			      MASTER_CTRL_INT_RDCLR);
1385 	/* clear interrupt status */
1386 	AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
1387 	/*  Clear any WOL status */
1388 	AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
1389 	/* set Interrupt Clear Timer
1390 	 * HW will enable self to assert interrupt event to system after
1391 	 * waiting x-time for software to notify it accept interrupt.
1392 	 */
1393 
1394 	data = CLK_GATING_EN_ALL;
1395 	if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
1396 		if (hw->nic_type == athr_l2c_b)
1397 			data &= ~CLK_GATING_RXMAC_EN;
1398 	} else
1399 		data = 0;
1400 	AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
1401 
1402 	AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
1403 		hw->ict & INT_RETRIG_TIMER_MASK);
1404 
1405 	atl1c_configure_des_ring(adapter);
1406 
1407 	if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
1408 		intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
1409 					IRQ_MODRT_TX_TIMER_SHIFT;
1410 		intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
1411 					IRQ_MODRT_RX_TIMER_SHIFT;
1412 		AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
1413 		master_ctrl_data |=
1414 			MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
1415 	}
1416 
1417 	if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
1418 		master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
1419 
1420 	master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
1421 	AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
1422 
1423 	AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
1424 		hw->smb_timer & SMB_STAT_TIMER_MASK);
1425 
1426 	/* set MTU */
1427 	AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
1428 			VLAN_HLEN + ETH_FCS_LEN);
1429 
1430 	atl1c_configure_tx(adapter);
1431 	atl1c_configure_rx(adapter);
1432 	atl1c_configure_dma(adapter);
1433 
1434 	return 0;
1435 }
1436 
1437 static int atl1c_configure(struct atl1c_adapter *adapter)
1438 {
1439 	struct net_device *netdev = adapter->netdev;
1440 	int num;
1441 
1442 	atl1c_init_ring_ptrs(adapter);
1443 	atl1c_set_multi(netdev);
1444 	atl1c_restore_vlan(adapter);
1445 
1446 	num = atl1c_alloc_rx_buffer(adapter, false);
1447 	if (unlikely(num == 0))
1448 		return -ENOMEM;
1449 
1450 	if (atl1c_configure_mac(adapter))
1451 		return -EIO;
1452 
1453 	return 0;
1454 }
1455 
1456 static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
1457 {
1458 	u16 hw_reg_addr = 0;
1459 	unsigned long *stats_item = NULL;
1460 	u32 data;
1461 
1462 	/* update rx status */
1463 	hw_reg_addr = REG_MAC_RX_STATUS_BIN;
1464 	stats_item  = &adapter->hw_stats.rx_ok;
1465 	while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
1466 		AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1467 		*stats_item += data;
1468 		stats_item++;
1469 		hw_reg_addr += 4;
1470 	}
1471 /* update tx status */
1472 	hw_reg_addr = REG_MAC_TX_STATUS_BIN;
1473 	stats_item  = &adapter->hw_stats.tx_ok;
1474 	while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
1475 		AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1476 		*stats_item += data;
1477 		stats_item++;
1478 		hw_reg_addr += 4;
1479 	}
1480 }
1481 
1482 /**
1483  * atl1c_get_stats - Get System Network Statistics
1484  * @netdev: network interface device structure
1485  *
1486  * Returns the address of the device statistics structure.
1487  * The statistics are actually updated from the timer callback.
1488  */
1489 static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
1490 {
1491 	struct atl1c_adapter *adapter = netdev_priv(netdev);
1492 	struct atl1c_hw_stats  *hw_stats = &adapter->hw_stats;
1493 	struct net_device_stats *net_stats = &netdev->stats;
1494 
1495 	atl1c_update_hw_stats(adapter);
1496 	net_stats->rx_bytes   = hw_stats->rx_byte_cnt;
1497 	net_stats->tx_bytes   = hw_stats->tx_byte_cnt;
1498 	net_stats->multicast  = hw_stats->rx_mcast;
1499 	net_stats->collisions = hw_stats->tx_1_col +
1500 				hw_stats->tx_2_col +
1501 				hw_stats->tx_late_col +
1502 				hw_stats->tx_abort_col;
1503 
1504 	net_stats->rx_errors  = hw_stats->rx_frag +
1505 				hw_stats->rx_fcs_err +
1506 				hw_stats->rx_len_err +
1507 				hw_stats->rx_sz_ov +
1508 				hw_stats->rx_rrd_ov +
1509 				hw_stats->rx_align_err +
1510 				hw_stats->rx_rxf_ov;
1511 
1512 	net_stats->rx_fifo_errors   = hw_stats->rx_rxf_ov;
1513 	net_stats->rx_length_errors = hw_stats->rx_len_err;
1514 	net_stats->rx_crc_errors    = hw_stats->rx_fcs_err;
1515 	net_stats->rx_frame_errors  = hw_stats->rx_align_err;
1516 	net_stats->rx_dropped       = hw_stats->rx_rrd_ov;
1517 
1518 	net_stats->tx_errors = hw_stats->tx_late_col +
1519 			       hw_stats->tx_abort_col +
1520 			       hw_stats->tx_underrun +
1521 			       hw_stats->tx_trunc;
1522 
1523 	net_stats->tx_fifo_errors    = hw_stats->tx_underrun;
1524 	net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1525 	net_stats->tx_window_errors  = hw_stats->tx_late_col;
1526 
1527 	net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
1528 	net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
1529 
1530 	return net_stats;
1531 }
1532 
1533 static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
1534 {
1535 	u16 phy_data;
1536 
1537 	spin_lock(&adapter->mdio_lock);
1538 	atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
1539 	spin_unlock(&adapter->mdio_lock);
1540 }
1541 
1542 static int atl1c_clean_tx(struct napi_struct *napi, int budget)
1543 {
1544 	struct atl1c_adapter *adapter =
1545 		container_of(napi, struct atl1c_adapter, tx_napi);
1546 	struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[atl1c_trans_normal];
1547 	struct atl1c_buffer *buffer_info;
1548 	struct pci_dev *pdev = adapter->pdev;
1549 	u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1550 	u16 hw_next_to_clean;
1551 	unsigned int total_bytes = 0, total_packets = 0;
1552 	unsigned long flags;
1553 
1554 	AT_READ_REGW(&adapter->hw, REG_TPD_PRI0_CIDX, &hw_next_to_clean);
1555 
1556 	while (next_to_clean != hw_next_to_clean) {
1557 		buffer_info = &tpd_ring->buffer_info[next_to_clean];
1558 		if (buffer_info->skb) {
1559 			total_bytes += buffer_info->skb->len;
1560 			total_packets++;
1561 		}
1562 		atl1c_clean_buffer(pdev, buffer_info);
1563 		if (++next_to_clean == tpd_ring->count)
1564 			next_to_clean = 0;
1565 		atomic_set(&tpd_ring->next_to_clean, next_to_clean);
1566 	}
1567 
1568 	netdev_completed_queue(adapter->netdev, total_packets, total_bytes);
1569 
1570 	if (netif_queue_stopped(adapter->netdev) &&
1571 			netif_carrier_ok(adapter->netdev)) {
1572 		netif_wake_queue(adapter->netdev);
1573 	}
1574 
1575 	if (total_packets < budget) {
1576 		napi_complete_done(napi, total_packets);
1577 		spin_lock_irqsave(&adapter->hw.intr_mask_lock, flags);
1578 		adapter->hw.intr_mask |= ISR_TX_PKT;
1579 		AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
1580 		spin_unlock_irqrestore(&adapter->hw.intr_mask_lock, flags);
1581 		return total_packets;
1582 	}
1583 	return budget;
1584 }
1585 
1586 /**
1587  * atl1c_intr - Interrupt Handler
1588  * @irq: interrupt number
1589  * @data: pointer to a network interface device structure
1590  */
1591 static irqreturn_t atl1c_intr(int irq, void *data)
1592 {
1593 	struct net_device *netdev  = data;
1594 	struct atl1c_adapter *adapter = netdev_priv(netdev);
1595 	struct pci_dev *pdev = adapter->pdev;
1596 	struct atl1c_hw *hw = &adapter->hw;
1597 	int max_ints = AT_MAX_INT_WORK;
1598 	int handled = IRQ_NONE;
1599 	u32 status;
1600 	u32 reg_data;
1601 
1602 	do {
1603 		AT_READ_REG(hw, REG_ISR, &reg_data);
1604 		status = reg_data & hw->intr_mask;
1605 
1606 		if (status == 0 || (status & ISR_DIS_INT) != 0) {
1607 			if (max_ints != AT_MAX_INT_WORK)
1608 				handled = IRQ_HANDLED;
1609 			break;
1610 		}
1611 		/* link event */
1612 		if (status & ISR_GPHY)
1613 			atl1c_clear_phy_int(adapter);
1614 		/* Ack ISR */
1615 		AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
1616 		if (status & ISR_RX_PKT) {
1617 			if (likely(napi_schedule_prep(&adapter->napi))) {
1618 				spin_lock(&hw->intr_mask_lock);
1619 				hw->intr_mask &= ~ISR_RX_PKT;
1620 				AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
1621 				spin_unlock(&hw->intr_mask_lock);
1622 				__napi_schedule(&adapter->napi);
1623 			}
1624 		}
1625 		if (status & ISR_TX_PKT) {
1626 			if (napi_schedule_prep(&adapter->tx_napi)) {
1627 				spin_lock(&hw->intr_mask_lock);
1628 				hw->intr_mask &= ~ISR_TX_PKT;
1629 				AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
1630 				spin_unlock(&hw->intr_mask_lock);
1631 				__napi_schedule(&adapter->tx_napi);
1632 			}
1633 		}
1634 
1635 		handled = IRQ_HANDLED;
1636 		/* check if PCIE PHY Link down */
1637 		if (status & ISR_ERROR) {
1638 			if (netif_msg_hw(adapter))
1639 				dev_err(&pdev->dev,
1640 					"atl1c hardware error (status = 0x%x)\n",
1641 					status & ISR_ERROR);
1642 			/* reset MAC */
1643 			set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
1644 			schedule_work(&adapter->common_task);
1645 			return IRQ_HANDLED;
1646 		}
1647 
1648 		if (status & ISR_OVER)
1649 			if (netif_msg_intr(adapter))
1650 				dev_warn(&pdev->dev,
1651 					"TX/RX overflow (status = 0x%x)\n",
1652 					status & ISR_OVER);
1653 
1654 		/* link event */
1655 		if (status & (ISR_GPHY | ISR_MANUAL)) {
1656 			netdev->stats.tx_carrier_errors++;
1657 			atl1c_link_chg_event(adapter);
1658 			break;
1659 		}
1660 
1661 	} while (--max_ints > 0);
1662 	/* re-enable Interrupt*/
1663 	AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
1664 	return handled;
1665 }
1666 
1667 static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
1668 		  struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
1669 {
1670 	if (adapter->hw.nic_type == athr_mt) {
1671 		if (prrs->word3 & RRS_MT_PROT_ID_TCPUDP)
1672 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1673 		return;
1674 	}
1675 	/*
1676 	 * The pid field in RRS in not correct sometimes, so we
1677 	 * cannot figure out if the packet is fragmented or not,
1678 	 * so we tell the KERNEL CHECKSUM_NONE
1679 	 */
1680 	skb_checksum_none_assert(skb);
1681 }
1682 
1683 static struct sk_buff *atl1c_alloc_skb(struct atl1c_adapter *adapter,
1684 				       bool napi_mode)
1685 {
1686 	struct sk_buff *skb;
1687 	struct page *page;
1688 
1689 	if (adapter->rx_frag_size > PAGE_SIZE) {
1690 		if (likely(napi_mode))
1691 			return napi_alloc_skb(&adapter->napi,
1692 					      adapter->rx_buffer_len);
1693 		else
1694 			return netdev_alloc_skb_ip_align(adapter->netdev,
1695 							 adapter->rx_buffer_len);
1696 	}
1697 
1698 	page = adapter->rx_page;
1699 	if (!page) {
1700 		adapter->rx_page = page = alloc_page(GFP_ATOMIC);
1701 		if (unlikely(!page))
1702 			return NULL;
1703 		adapter->rx_page_offset = 0;
1704 	}
1705 
1706 	skb = build_skb(page_address(page) + adapter->rx_page_offset,
1707 			adapter->rx_frag_size);
1708 	if (likely(skb)) {
1709 		skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1710 		adapter->rx_page_offset += adapter->rx_frag_size;
1711 		if (adapter->rx_page_offset >= PAGE_SIZE)
1712 			adapter->rx_page = NULL;
1713 		else
1714 			get_page(page);
1715 	}
1716 	return skb;
1717 }
1718 
1719 static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter, bool napi_mode)
1720 {
1721 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1722 	struct pci_dev *pdev = adapter->pdev;
1723 	struct atl1c_buffer *buffer_info, *next_info;
1724 	struct sk_buff *skb;
1725 	void *vir_addr = NULL;
1726 	u16 num_alloc = 0;
1727 	u16 rfd_next_to_use, next_next;
1728 	struct atl1c_rx_free_desc *rfd_desc;
1729 	dma_addr_t mapping;
1730 
1731 	next_next = rfd_next_to_use = rfd_ring->next_to_use;
1732 	if (++next_next == rfd_ring->count)
1733 		next_next = 0;
1734 	buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1735 	next_info = &rfd_ring->buffer_info[next_next];
1736 
1737 	while (next_info->flags & ATL1C_BUFFER_FREE) {
1738 		rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
1739 
1740 		skb = atl1c_alloc_skb(adapter, napi_mode);
1741 		if (unlikely(!skb)) {
1742 			if (netif_msg_rx_err(adapter))
1743 				dev_warn(&pdev->dev, "alloc rx buffer failed\n");
1744 			break;
1745 		}
1746 
1747 		/*
1748 		 * Make buffer alignment 2 beyond a 16 byte boundary
1749 		 * this will result in a 16 byte aligned IP header after
1750 		 * the 14 byte MAC header is removed
1751 		 */
1752 		vir_addr = skb->data;
1753 		ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
1754 		buffer_info->skb = skb;
1755 		buffer_info->length = adapter->rx_buffer_len;
1756 		mapping = dma_map_single(&pdev->dev, vir_addr,
1757 					 buffer_info->length, DMA_FROM_DEVICE);
1758 		if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
1759 			dev_kfree_skb(skb);
1760 			buffer_info->skb = NULL;
1761 			buffer_info->length = 0;
1762 			ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
1763 			netif_warn(adapter, rx_err, adapter->netdev, "RX pci_map_single failed");
1764 			break;
1765 		}
1766 		buffer_info->dma = mapping;
1767 		ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
1768 			ATL1C_PCIMAP_FROMDEVICE);
1769 		rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1770 		rfd_next_to_use = next_next;
1771 		if (++next_next == rfd_ring->count)
1772 			next_next = 0;
1773 		buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1774 		next_info = &rfd_ring->buffer_info[next_next];
1775 		num_alloc++;
1776 	}
1777 
1778 	if (num_alloc) {
1779 		/* TODO: update mailbox here */
1780 		wmb();
1781 		rfd_ring->next_to_use = rfd_next_to_use;
1782 		AT_WRITE_REG(&adapter->hw, REG_MB_RFD0_PROD_IDX,
1783 			rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
1784 	}
1785 
1786 	return num_alloc;
1787 }
1788 
1789 static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring,
1790 			struct	atl1c_recv_ret_status *rrs, u16 num)
1791 {
1792 	u16 i;
1793 	/* the relationship between rrd and rfd is one map one */
1794 	for (i = 0; i < num; i++, rrs = ATL1C_RRD_DESC(rrd_ring,
1795 					rrd_ring->next_to_clean)) {
1796 		rrs->word3 &= ~RRS_RXD_UPDATED;
1797 		if (++rrd_ring->next_to_clean == rrd_ring->count)
1798 			rrd_ring->next_to_clean = 0;
1799 	}
1800 }
1801 
1802 static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring,
1803 	struct atl1c_recv_ret_status *rrs, u16 num)
1804 {
1805 	u16 i;
1806 	u16 rfd_index;
1807 	struct atl1c_buffer *buffer_info = rfd_ring->buffer_info;
1808 
1809 	rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1810 			RRS_RX_RFD_INDEX_MASK;
1811 	for (i = 0; i < num; i++) {
1812 		buffer_info[rfd_index].skb = NULL;
1813 		ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index],
1814 					ATL1C_BUFFER_FREE);
1815 		if (++rfd_index == rfd_ring->count)
1816 			rfd_index = 0;
1817 	}
1818 	rfd_ring->next_to_clean = rfd_index;
1819 }
1820 
1821 static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
1822 		   int *work_done, int work_to_do)
1823 {
1824 	u16 rfd_num, rfd_index;
1825 	u16 count = 0;
1826 	u16 length;
1827 	struct pci_dev *pdev = adapter->pdev;
1828 	struct net_device *netdev  = adapter->netdev;
1829 	struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1830 	struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
1831 	struct sk_buff *skb;
1832 	struct atl1c_recv_ret_status *rrs;
1833 	struct atl1c_buffer *buffer_info;
1834 
1835 	while (1) {
1836 		if (*work_done >= work_to_do)
1837 			break;
1838 		rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean);
1839 		if (likely(RRS_RXD_IS_VALID(rrs->word3))) {
1840 			rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) &
1841 				RRS_RX_RFD_CNT_MASK;
1842 			if (unlikely(rfd_num != 1))
1843 				/* TODO support mul rfd*/
1844 				if (netif_msg_rx_err(adapter))
1845 					dev_warn(&pdev->dev,
1846 						"Multi rfd not support yet!\n");
1847 			goto rrs_checked;
1848 		} else {
1849 			break;
1850 		}
1851 rrs_checked:
1852 		atl1c_clean_rrd(rrd_ring, rrs, rfd_num);
1853 		if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) {
1854 			atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1855 			if (netif_msg_rx_err(adapter))
1856 				dev_warn(&pdev->dev,
1857 					 "wrong packet! rrs word3 is %x\n",
1858 					 rrs->word3);
1859 			continue;
1860 		}
1861 
1862 		length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) &
1863 				RRS_PKT_SIZE_MASK);
1864 		/* Good Receive */
1865 		if (likely(rfd_num == 1)) {
1866 			rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1867 					RRS_RX_RFD_INDEX_MASK;
1868 			buffer_info = &rfd_ring->buffer_info[rfd_index];
1869 			dma_unmap_single(&pdev->dev, buffer_info->dma,
1870 					 buffer_info->length, DMA_FROM_DEVICE);
1871 			skb = buffer_info->skb;
1872 		} else {
1873 			/* TODO */
1874 			if (netif_msg_rx_err(adapter))
1875 				dev_warn(&pdev->dev,
1876 					"Multi rfd not support yet!\n");
1877 			break;
1878 		}
1879 		atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1880 		skb_put(skb, length - ETH_FCS_LEN);
1881 		skb->protocol = eth_type_trans(skb, netdev);
1882 		atl1c_rx_checksum(adapter, skb, rrs);
1883 		if (rrs->word3 & RRS_VLAN_INS) {
1884 			u16 vlan;
1885 
1886 			AT_TAG_TO_VLAN(rrs->vlan_tag, vlan);
1887 			vlan = le16_to_cpu(vlan);
1888 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
1889 		}
1890 		napi_gro_receive(&adapter->napi, skb);
1891 
1892 		(*work_done)++;
1893 		count++;
1894 	}
1895 	if (count)
1896 		atl1c_alloc_rx_buffer(adapter, true);
1897 }
1898 
1899 /**
1900  * atl1c_clean - NAPI Rx polling callback
1901  * @napi: napi info
1902  * @budget: limit of packets to clean
1903  */
1904 static int atl1c_clean(struct napi_struct *napi, int budget)
1905 {
1906 	struct atl1c_adapter *adapter =
1907 			container_of(napi, struct atl1c_adapter, napi);
1908 	int work_done = 0;
1909 	unsigned long flags;
1910 
1911 	/* Keep link state information with original netdev */
1912 	if (!netif_carrier_ok(adapter->netdev))
1913 		goto quit_polling;
1914 	/* just enable one RXQ */
1915 	atl1c_clean_rx_irq(adapter, &work_done, budget);
1916 
1917 	if (work_done < budget) {
1918 quit_polling:
1919 		napi_complete_done(napi, work_done);
1920 		spin_lock_irqsave(&adapter->hw.intr_mask_lock, flags);
1921 		adapter->hw.intr_mask |= ISR_RX_PKT;
1922 		AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
1923 		spin_unlock_irqrestore(&adapter->hw.intr_mask_lock, flags);
1924 	}
1925 	return work_done;
1926 }
1927 
1928 #ifdef CONFIG_NET_POLL_CONTROLLER
1929 
1930 /*
1931  * Polling 'interrupt' - used by things like netconsole to send skbs
1932  * without having to re-enable interrupts. It's not called while
1933  * the interrupt routine is executing.
1934  */
1935 static void atl1c_netpoll(struct net_device *netdev)
1936 {
1937 	struct atl1c_adapter *adapter = netdev_priv(netdev);
1938 
1939 	disable_irq(adapter->pdev->irq);
1940 	atl1c_intr(adapter->pdev->irq, netdev);
1941 	enable_irq(adapter->pdev->irq);
1942 }
1943 #endif
1944 
1945 static inline u16 atl1c_tpd_avail(struct atl1c_adapter *adapter, enum atl1c_trans_queue type)
1946 {
1947 	struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1948 	u16 next_to_use = 0;
1949 	u16 next_to_clean = 0;
1950 
1951 	next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1952 	next_to_use   = tpd_ring->next_to_use;
1953 
1954 	return (u16)(next_to_clean > next_to_use) ?
1955 		(next_to_clean - next_to_use - 1) :
1956 		(tpd_ring->count + next_to_clean - next_to_use - 1);
1957 }
1958 
1959 /*
1960  * get next usable tpd
1961  * Note: should call atl1c_tdp_avail to make sure
1962  * there is enough tpd to use
1963  */
1964 static struct atl1c_tpd_desc *atl1c_get_tpd(struct atl1c_adapter *adapter,
1965 	enum atl1c_trans_queue type)
1966 {
1967 	struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1968 	struct atl1c_tpd_desc *tpd_desc;
1969 	u16 next_to_use = 0;
1970 
1971 	next_to_use = tpd_ring->next_to_use;
1972 	if (++tpd_ring->next_to_use == tpd_ring->count)
1973 		tpd_ring->next_to_use = 0;
1974 	tpd_desc = ATL1C_TPD_DESC(tpd_ring, next_to_use);
1975 	memset(tpd_desc, 0, sizeof(struct atl1c_tpd_desc));
1976 	return	tpd_desc;
1977 }
1978 
1979 static struct atl1c_buffer *
1980 atl1c_get_tx_buffer(struct atl1c_adapter *adapter, struct atl1c_tpd_desc *tpd)
1981 {
1982 	struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
1983 
1984 	return &tpd_ring->buffer_info[tpd -
1985 			(struct atl1c_tpd_desc *)tpd_ring->desc];
1986 }
1987 
1988 /* Calculate the transmit packet descript needed*/
1989 static u16 atl1c_cal_tpd_req(const struct sk_buff *skb)
1990 {
1991 	u16 tpd_req;
1992 	u16 proto_hdr_len = 0;
1993 
1994 	tpd_req = skb_shinfo(skb)->nr_frags + 1;
1995 
1996 	if (skb_is_gso(skb)) {
1997 		proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1998 		if (proto_hdr_len < skb_headlen(skb))
1999 			tpd_req++;
2000 		if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
2001 			tpd_req++;
2002 	}
2003 	return tpd_req;
2004 }
2005 
2006 static int atl1c_tso_csum(struct atl1c_adapter *adapter,
2007 			  struct sk_buff *skb,
2008 			  struct atl1c_tpd_desc **tpd,
2009 			  enum atl1c_trans_queue type)
2010 {
2011 	struct pci_dev *pdev = adapter->pdev;
2012 	unsigned short offload_type;
2013 	u8 hdr_len;
2014 	u32 real_len;
2015 
2016 	if (skb_is_gso(skb)) {
2017 		int err;
2018 
2019 		err = skb_cow_head(skb, 0);
2020 		if (err < 0)
2021 			return err;
2022 
2023 		offload_type = skb_shinfo(skb)->gso_type;
2024 
2025 		if (offload_type & SKB_GSO_TCPV4) {
2026 			real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
2027 					+ ntohs(ip_hdr(skb)->tot_len));
2028 
2029 			if (real_len < skb->len)
2030 				pskb_trim(skb, real_len);
2031 
2032 			hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2033 			if (unlikely(skb->len == hdr_len)) {
2034 				/* only xsum need */
2035 				if (netif_msg_tx_queued(adapter))
2036 					dev_warn(&pdev->dev,
2037 						"IPV4 tso with zero data??\n");
2038 				goto check_sum;
2039 			} else {
2040 				ip_hdr(skb)->check = 0;
2041 				tcp_hdr(skb)->check = ~csum_tcpudp_magic(
2042 							ip_hdr(skb)->saddr,
2043 							ip_hdr(skb)->daddr,
2044 							0, IPPROTO_TCP, 0);
2045 				(*tpd)->word1 |= 1 << TPD_IPV4_PACKET_SHIFT;
2046 			}
2047 		}
2048 
2049 		if (offload_type & SKB_GSO_TCPV6) {
2050 			struct atl1c_tpd_ext_desc *etpd =
2051 				*(struct atl1c_tpd_ext_desc **)(tpd);
2052 
2053 			memset(etpd, 0, sizeof(struct atl1c_tpd_ext_desc));
2054 			*tpd = atl1c_get_tpd(adapter, type);
2055 			ipv6_hdr(skb)->payload_len = 0;
2056 			/* check payload == 0 byte ? */
2057 			hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2058 			if (unlikely(skb->len == hdr_len)) {
2059 				/* only xsum need */
2060 				if (netif_msg_tx_queued(adapter))
2061 					dev_warn(&pdev->dev,
2062 						"IPV6 tso with zero data??\n");
2063 				goto check_sum;
2064 			} else
2065 				tcp_v6_gso_csum_prep(skb);
2066 
2067 			etpd->word1 |= 1 << TPD_LSO_EN_SHIFT;
2068 			etpd->word1 |= 1 << TPD_LSO_VER_SHIFT;
2069 			etpd->pkt_len = cpu_to_le32(skb->len);
2070 			(*tpd)->word1 |= 1 << TPD_LSO_VER_SHIFT;
2071 		}
2072 
2073 		(*tpd)->word1 |= 1 << TPD_LSO_EN_SHIFT;
2074 		(*tpd)->word1 |= (skb_transport_offset(skb) & TPD_TCPHDR_OFFSET_MASK) <<
2075 				TPD_TCPHDR_OFFSET_SHIFT;
2076 		(*tpd)->word1 |= (skb_shinfo(skb)->gso_size & TPD_MSS_MASK) <<
2077 				TPD_MSS_SHIFT;
2078 		return 0;
2079 	}
2080 
2081 check_sum:
2082 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2083 		u8 css, cso;
2084 		cso = skb_checksum_start_offset(skb);
2085 
2086 		if (unlikely(cso & 0x1)) {
2087 			if (netif_msg_tx_err(adapter))
2088 				dev_err(&adapter->pdev->dev,
2089 					"payload offset should not an event number\n");
2090 			return -1;
2091 		} else {
2092 			css = cso + skb->csum_offset;
2093 
2094 			(*tpd)->word1 |= ((cso >> 1) & TPD_PLOADOFFSET_MASK) <<
2095 					TPD_PLOADOFFSET_SHIFT;
2096 			(*tpd)->word1 |= ((css >> 1) & TPD_CCSUM_OFFSET_MASK) <<
2097 					TPD_CCSUM_OFFSET_SHIFT;
2098 			(*tpd)->word1 |= 1 << TPD_CCSUM_EN_SHIFT;
2099 		}
2100 	}
2101 	return 0;
2102 }
2103 
2104 static void atl1c_tx_rollback(struct atl1c_adapter *adpt,
2105 			      struct atl1c_tpd_desc *first_tpd,
2106 			      enum atl1c_trans_queue type)
2107 {
2108 	struct atl1c_tpd_ring *tpd_ring = &adpt->tpd_ring[type];
2109 	struct atl1c_buffer *buffer_info;
2110 	struct atl1c_tpd_desc *tpd;
2111 	u16 first_index, index;
2112 
2113 	first_index = first_tpd - (struct atl1c_tpd_desc *)tpd_ring->desc;
2114 	index = first_index;
2115 	while (index != tpd_ring->next_to_use) {
2116 		tpd = ATL1C_TPD_DESC(tpd_ring, index);
2117 		buffer_info = &tpd_ring->buffer_info[index];
2118 		atl1c_clean_buffer(adpt->pdev, buffer_info);
2119 		memset(tpd, 0, sizeof(struct atl1c_tpd_desc));
2120 		if (++index == tpd_ring->count)
2121 			index = 0;
2122 	}
2123 	tpd_ring->next_to_use = first_index;
2124 }
2125 
2126 static int atl1c_tx_map(struct atl1c_adapter *adapter,
2127 		      struct sk_buff *skb, struct atl1c_tpd_desc *tpd,
2128 			enum atl1c_trans_queue type)
2129 {
2130 	struct atl1c_tpd_desc *use_tpd = NULL;
2131 	struct atl1c_buffer *buffer_info = NULL;
2132 	u16 buf_len = skb_headlen(skb);
2133 	u16 map_len = 0;
2134 	u16 mapped_len = 0;
2135 	u16 hdr_len = 0;
2136 	u16 nr_frags;
2137 	u16 f;
2138 	int tso;
2139 
2140 	nr_frags = skb_shinfo(skb)->nr_frags;
2141 	tso = (tpd->word1 >> TPD_LSO_EN_SHIFT) & TPD_LSO_EN_MASK;
2142 	if (tso) {
2143 		/* TSO */
2144 		map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2145 		use_tpd = tpd;
2146 
2147 		buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2148 		buffer_info->length = map_len;
2149 		buffer_info->dma = dma_map_single(&adapter->pdev->dev,
2150 						  skb->data, hdr_len,
2151 						  DMA_TO_DEVICE);
2152 		if (unlikely(dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)))
2153 			goto err_dma;
2154 		ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2155 		ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2156 			ATL1C_PCIMAP_TODEVICE);
2157 		mapped_len += map_len;
2158 		use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2159 		use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2160 	}
2161 
2162 	if (mapped_len < buf_len) {
2163 		/* mapped_len == 0, means we should use the first tpd,
2164 		   which is given by caller  */
2165 		if (mapped_len == 0)
2166 			use_tpd = tpd;
2167 		else {
2168 			use_tpd = atl1c_get_tpd(adapter, type);
2169 			memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2170 		}
2171 		buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2172 		buffer_info->length = buf_len - mapped_len;
2173 		buffer_info->dma =
2174 			dma_map_single(&adapter->pdev->dev,
2175 				       skb->data + mapped_len,
2176 				       buffer_info->length, DMA_TO_DEVICE);
2177 		if (unlikely(dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)))
2178 			goto err_dma;
2179 
2180 		ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2181 		ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2182 			ATL1C_PCIMAP_TODEVICE);
2183 		use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2184 		use_tpd->buffer_len  = cpu_to_le16(buffer_info->length);
2185 	}
2186 
2187 	for (f = 0; f < nr_frags; f++) {
2188 		skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2189 
2190 		use_tpd = atl1c_get_tpd(adapter, type);
2191 		memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2192 
2193 		buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2194 		buffer_info->length = skb_frag_size(frag);
2195 		buffer_info->dma = skb_frag_dma_map(&adapter->pdev->dev,
2196 						    frag, 0,
2197 						    buffer_info->length,
2198 						    DMA_TO_DEVICE);
2199 		if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma))
2200 			goto err_dma;
2201 
2202 		ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
2203 		ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_PAGE,
2204 			ATL1C_PCIMAP_TODEVICE);
2205 		use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2206 		use_tpd->buffer_len  = cpu_to_le16(buffer_info->length);
2207 	}
2208 
2209 	/* The last tpd */
2210 	use_tpd->word1 |= 1 << TPD_EOP_SHIFT;
2211 	/* The last buffer info contain the skb address,
2212 	   so it will be free after unmap */
2213 	buffer_info->skb = skb;
2214 
2215 	return 0;
2216 
2217 err_dma:
2218 	buffer_info->dma = 0;
2219 	buffer_info->length = 0;
2220 	return -1;
2221 }
2222 
2223 static void atl1c_tx_queue(struct atl1c_adapter *adapter,
2224 			   enum atl1c_trans_queue type)
2225 {
2226 	struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
2227 	u16 reg;
2228 
2229 	reg = type == atl1c_trans_high ? REG_TPD_PRI1_PIDX : REG_TPD_PRI0_PIDX;
2230 	AT_WRITE_REGW(&adapter->hw, reg, tpd_ring->next_to_use);
2231 }
2232 
2233 static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
2234 					  struct net_device *netdev)
2235 {
2236 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2237 	u16 tpd_req;
2238 	struct atl1c_tpd_desc *tpd;
2239 	enum atl1c_trans_queue type = atl1c_trans_normal;
2240 
2241 	if (test_bit(__AT_DOWN, &adapter->flags)) {
2242 		dev_kfree_skb_any(skb);
2243 		return NETDEV_TX_OK;
2244 	}
2245 
2246 	tpd_req = atl1c_cal_tpd_req(skb);
2247 
2248 	if (atl1c_tpd_avail(adapter, type) < tpd_req) {
2249 		/* no enough descriptor, just stop queue */
2250 		atl1c_tx_queue(adapter, type);
2251 		netif_stop_queue(netdev);
2252 		return NETDEV_TX_BUSY;
2253 	}
2254 
2255 	tpd = atl1c_get_tpd(adapter, type);
2256 
2257 	/* do TSO and check sum */
2258 	if (atl1c_tso_csum(adapter, skb, &tpd, type) != 0) {
2259 		atl1c_tx_queue(adapter, type);
2260 		dev_kfree_skb_any(skb);
2261 		return NETDEV_TX_OK;
2262 	}
2263 
2264 	if (unlikely(skb_vlan_tag_present(skb))) {
2265 		u16 vlan = skb_vlan_tag_get(skb);
2266 		__le16 tag;
2267 
2268 		vlan = cpu_to_le16(vlan);
2269 		AT_VLAN_TO_TAG(vlan, tag);
2270 		tpd->word1 |= 1 << TPD_INS_VTAG_SHIFT;
2271 		tpd->vlan_tag = tag;
2272 	}
2273 
2274 	if (skb_network_offset(skb) != ETH_HLEN)
2275 		tpd->word1 |= 1 << TPD_ETH_TYPE_SHIFT; /* Ethernet frame */
2276 
2277 	if (atl1c_tx_map(adapter, skb, tpd, type) < 0) {
2278 		netif_info(adapter, tx_done, adapter->netdev,
2279 			   "tx-skb dropped due to dma error\n");
2280 		/* roll back tpd/buffer */
2281 		atl1c_tx_rollback(adapter, tpd, type);
2282 		dev_kfree_skb_any(skb);
2283 	} else {
2284 		bool more = netdev_xmit_more();
2285 
2286 		if (__netdev_sent_queue(adapter->netdev, skb->len, more))
2287 			atl1c_tx_queue(adapter, type);
2288 	}
2289 
2290 	return NETDEV_TX_OK;
2291 }
2292 
2293 static void atl1c_free_irq(struct atl1c_adapter *adapter)
2294 {
2295 	struct net_device *netdev = adapter->netdev;
2296 
2297 	free_irq(adapter->pdev->irq, netdev);
2298 
2299 	if (adapter->have_msi)
2300 		pci_disable_msi(adapter->pdev);
2301 }
2302 
2303 static int atl1c_request_irq(struct atl1c_adapter *adapter)
2304 {
2305 	struct pci_dev    *pdev   = adapter->pdev;
2306 	struct net_device *netdev = adapter->netdev;
2307 	int flags = 0;
2308 	int err = 0;
2309 
2310 	adapter->have_msi = true;
2311 	err = pci_enable_msi(adapter->pdev);
2312 	if (err) {
2313 		if (netif_msg_ifup(adapter))
2314 			dev_err(&pdev->dev,
2315 				"Unable to allocate MSI interrupt Error: %d\n",
2316 				err);
2317 		adapter->have_msi = false;
2318 	}
2319 
2320 	if (!adapter->have_msi)
2321 		flags |= IRQF_SHARED;
2322 	err = request_irq(adapter->pdev->irq, atl1c_intr, flags,
2323 			netdev->name, netdev);
2324 	if (err) {
2325 		if (netif_msg_ifup(adapter))
2326 			dev_err(&pdev->dev,
2327 				"Unable to allocate interrupt Error: %d\n",
2328 				err);
2329 		if (adapter->have_msi)
2330 			pci_disable_msi(adapter->pdev);
2331 		return err;
2332 	}
2333 	if (netif_msg_ifup(adapter))
2334 		dev_dbg(&pdev->dev, "atl1c_request_irq OK\n");
2335 	return err;
2336 }
2337 
2338 
2339 static void atl1c_reset_dma_ring(struct atl1c_adapter *adapter)
2340 {
2341 	/* release tx-pending skbs and reset tx/rx ring index */
2342 	atl1c_clean_tx_ring(adapter, atl1c_trans_normal);
2343 	atl1c_clean_tx_ring(adapter, atl1c_trans_high);
2344 	atl1c_clean_rx_ring(adapter);
2345 }
2346 
2347 static int atl1c_up(struct atl1c_adapter *adapter)
2348 {
2349 	struct net_device *netdev = adapter->netdev;
2350 	int err;
2351 
2352 	netif_carrier_off(netdev);
2353 
2354 	err = atl1c_configure(adapter);
2355 	if (unlikely(err))
2356 		goto err_up;
2357 
2358 	err = atl1c_request_irq(adapter);
2359 	if (unlikely(err))
2360 		goto err_up;
2361 
2362 	atl1c_check_link_status(adapter);
2363 	clear_bit(__AT_DOWN, &adapter->flags);
2364 	napi_enable(&adapter->napi);
2365 	napi_enable(&adapter->tx_napi);
2366 	atl1c_irq_enable(adapter);
2367 	netif_start_queue(netdev);
2368 	return err;
2369 
2370 err_up:
2371 	atl1c_clean_rx_ring(adapter);
2372 	return err;
2373 }
2374 
2375 static void atl1c_down(struct atl1c_adapter *adapter)
2376 {
2377 	struct net_device *netdev = adapter->netdev;
2378 
2379 	atl1c_del_timer(adapter);
2380 	adapter->work_event = 0; /* clear all event */
2381 	/* signal that we're down so the interrupt handler does not
2382 	 * reschedule our watchdog timer */
2383 	set_bit(__AT_DOWN, &adapter->flags);
2384 	netif_carrier_off(netdev);
2385 	napi_disable(&adapter->napi);
2386 	napi_disable(&adapter->tx_napi);
2387 	atl1c_irq_disable(adapter);
2388 	atl1c_free_irq(adapter);
2389 	/* disable ASPM if device inactive */
2390 	atl1c_disable_l0s_l1(&adapter->hw);
2391 	/* reset MAC to disable all RX/TX */
2392 	atl1c_reset_mac(&adapter->hw);
2393 	msleep(1);
2394 
2395 	adapter->link_speed = SPEED_0;
2396 	adapter->link_duplex = -1;
2397 	atl1c_reset_dma_ring(adapter);
2398 }
2399 
2400 /**
2401  * atl1c_open - Called when a network interface is made active
2402  * @netdev: network interface device structure
2403  *
2404  * Returns 0 on success, negative value on failure
2405  *
2406  * The open entry point is called when a network interface is made
2407  * active by the system (IFF_UP).  At this point all resources needed
2408  * for transmit and receive operations are allocated, the interrupt
2409  * handler is registered with the OS, the watchdog timer is started,
2410  * and the stack is notified that the interface is ready.
2411  */
2412 static int atl1c_open(struct net_device *netdev)
2413 {
2414 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2415 	int err;
2416 
2417 	/* disallow open during test */
2418 	if (test_bit(__AT_TESTING, &adapter->flags))
2419 		return -EBUSY;
2420 
2421 	/* allocate rx/tx dma buffer & descriptors */
2422 	err = atl1c_setup_ring_resources(adapter);
2423 	if (unlikely(err))
2424 		return err;
2425 
2426 	err = atl1c_up(adapter);
2427 	if (unlikely(err))
2428 		goto err_up;
2429 
2430 	return 0;
2431 
2432 err_up:
2433 	atl1c_free_irq(adapter);
2434 	atl1c_free_ring_resources(adapter);
2435 	atl1c_reset_mac(&adapter->hw);
2436 	return err;
2437 }
2438 
2439 /**
2440  * atl1c_close - Disables a network interface
2441  * @netdev: network interface device structure
2442  *
2443  * Returns 0, this is not allowed to fail
2444  *
2445  * The close entry point is called when an interface is de-activated
2446  * by the OS.  The hardware is still under the drivers control, but
2447  * needs to be disabled.  A global MAC reset is issued to stop the
2448  * hardware, and all transmit and receive resources are freed.
2449  */
2450 static int atl1c_close(struct net_device *netdev)
2451 {
2452 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2453 
2454 	WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2455 	set_bit(__AT_DOWN, &adapter->flags);
2456 	cancel_work_sync(&adapter->common_task);
2457 	atl1c_down(adapter);
2458 	atl1c_free_ring_resources(adapter);
2459 	return 0;
2460 }
2461 
2462 static int atl1c_suspend(struct device *dev)
2463 {
2464 	struct net_device *netdev = dev_get_drvdata(dev);
2465 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2466 	struct atl1c_hw *hw = &adapter->hw;
2467 	u32 wufc = adapter->wol;
2468 
2469 	atl1c_disable_l0s_l1(hw);
2470 	if (netif_running(netdev)) {
2471 		WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2472 		atl1c_down(adapter);
2473 	}
2474 	netif_device_detach(netdev);
2475 
2476 	if (wufc)
2477 		if (atl1c_phy_to_ps_link(hw) != 0)
2478 			dev_dbg(dev, "phy power saving failed");
2479 
2480 	atl1c_power_saving(hw, wufc);
2481 
2482 	return 0;
2483 }
2484 
2485 #ifdef CONFIG_PM_SLEEP
2486 static int atl1c_resume(struct device *dev)
2487 {
2488 	struct net_device *netdev = dev_get_drvdata(dev);
2489 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2490 
2491 	AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
2492 	atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
2493 
2494 	atl1c_phy_reset(&adapter->hw);
2495 	atl1c_reset_mac(&adapter->hw);
2496 	atl1c_phy_init(&adapter->hw);
2497 
2498 	netif_device_attach(netdev);
2499 	if (netif_running(netdev))
2500 		atl1c_up(adapter);
2501 
2502 	return 0;
2503 }
2504 #endif
2505 
2506 static void atl1c_shutdown(struct pci_dev *pdev)
2507 {
2508 	struct net_device *netdev = pci_get_drvdata(pdev);
2509 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2510 
2511 	atl1c_suspend(&pdev->dev);
2512 	pci_wake_from_d3(pdev, adapter->wol);
2513 	pci_set_power_state(pdev, PCI_D3hot);
2514 }
2515 
2516 static const struct net_device_ops atl1c_netdev_ops = {
2517 	.ndo_open		= atl1c_open,
2518 	.ndo_stop		= atl1c_close,
2519 	.ndo_validate_addr	= eth_validate_addr,
2520 	.ndo_start_xmit		= atl1c_xmit_frame,
2521 	.ndo_set_mac_address	= atl1c_set_mac_addr,
2522 	.ndo_set_rx_mode	= atl1c_set_multi,
2523 	.ndo_change_mtu		= atl1c_change_mtu,
2524 	.ndo_fix_features	= atl1c_fix_features,
2525 	.ndo_set_features	= atl1c_set_features,
2526 	.ndo_do_ioctl		= atl1c_ioctl,
2527 	.ndo_tx_timeout		= atl1c_tx_timeout,
2528 	.ndo_get_stats		= atl1c_get_stats,
2529 #ifdef CONFIG_NET_POLL_CONTROLLER
2530 	.ndo_poll_controller	= atl1c_netpoll,
2531 #endif
2532 };
2533 
2534 static int atl1c_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
2535 {
2536 	SET_NETDEV_DEV(netdev, &pdev->dev);
2537 	pci_set_drvdata(pdev, netdev);
2538 
2539 	netdev->netdev_ops = &atl1c_netdev_ops;
2540 	netdev->watchdog_timeo = AT_TX_WATCHDOG;
2541 	netdev->min_mtu = ETH_ZLEN - (ETH_HLEN + VLAN_HLEN);
2542 	atl1c_set_ethtool_ops(netdev);
2543 
2544 	/* TODO: add when ready */
2545 	netdev->hw_features =	NETIF_F_SG		|
2546 				NETIF_F_HW_CSUM		|
2547 				NETIF_F_HW_VLAN_CTAG_RX	|
2548 				NETIF_F_TSO		|
2549 				NETIF_F_TSO6;
2550 	netdev->features =	netdev->hw_features	|
2551 				NETIF_F_HW_VLAN_CTAG_TX;
2552 	return 0;
2553 }
2554 
2555 /**
2556  * atl1c_probe - Device Initialization Routine
2557  * @pdev: PCI device information struct
2558  * @ent: entry in atl1c_pci_tbl
2559  *
2560  * Returns 0 on success, negative on failure
2561  *
2562  * atl1c_probe initializes an adapter identified by a pci_dev structure.
2563  * The OS initialization, configuring of the adapter private structure,
2564  * and a hardware reset occur.
2565  */
2566 static int atl1c_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2567 {
2568 	struct net_device *netdev;
2569 	struct atl1c_adapter *adapter;
2570 	static int cards_found;
2571 
2572 	int err = 0;
2573 
2574 	/* enable device (incl. PCI PM wakeup and hotplug setup) */
2575 	err = pci_enable_device_mem(pdev);
2576 	if (err) {
2577 		dev_err(&pdev->dev, "cannot enable PCI device\n");
2578 		return err;
2579 	}
2580 
2581 	/*
2582 	 * The atl1c chip can DMA to 64-bit addresses, but it uses a single
2583 	 * shared register for the high 32 bits, so only a single, aligned,
2584 	 * 4 GB physical address range can be used at a time.
2585 	 *
2586 	 * Supporting 64-bit DMA on this hardware is more trouble than it's
2587 	 * worth.  It is far easier to limit to 32-bit DMA than update
2588 	 * various kernel subsystems to support the mechanics required by a
2589 	 * fixed-high-32-bit system.
2590 	 */
2591 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2592 	if (err) {
2593 		dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
2594 		goto err_dma;
2595 	}
2596 
2597 	err = pci_request_regions(pdev, atl1c_driver_name);
2598 	if (err) {
2599 		dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2600 		goto err_pci_reg;
2601 	}
2602 
2603 	pci_set_master(pdev);
2604 
2605 	netdev = alloc_etherdev(sizeof(struct atl1c_adapter));
2606 	if (netdev == NULL) {
2607 		err = -ENOMEM;
2608 		goto err_alloc_etherdev;
2609 	}
2610 
2611 	err = atl1c_init_netdev(netdev, pdev);
2612 	if (err) {
2613 		dev_err(&pdev->dev, "init netdevice failed\n");
2614 		goto err_init_netdev;
2615 	}
2616 	adapter = netdev_priv(netdev);
2617 	adapter->bd_number = cards_found;
2618 	adapter->netdev = netdev;
2619 	adapter->pdev = pdev;
2620 	adapter->hw.adapter = adapter;
2621 	adapter->msg_enable = netif_msg_init(-1, atl1c_default_msg);
2622 	adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
2623 	if (!adapter->hw.hw_addr) {
2624 		err = -EIO;
2625 		dev_err(&pdev->dev, "cannot map device registers\n");
2626 		goto err_ioremap;
2627 	}
2628 
2629 	/* init mii data */
2630 	adapter->mii.dev = netdev;
2631 	adapter->mii.mdio_read  = atl1c_mdio_read;
2632 	adapter->mii.mdio_write = atl1c_mdio_write;
2633 	adapter->mii.phy_id_mask = 0x1f;
2634 	adapter->mii.reg_num_mask = MDIO_CTRL_REG_MASK;
2635 	dev_set_threaded(netdev, true);
2636 	netif_napi_add(netdev, &adapter->napi, atl1c_clean, 64);
2637 	netif_napi_add(netdev, &adapter->tx_napi, atl1c_clean_tx, 64);
2638 	timer_setup(&adapter->phy_config_timer, atl1c_phy_config, 0);
2639 	/* setup the private structure */
2640 	err = atl1c_sw_init(adapter);
2641 	if (err) {
2642 		dev_err(&pdev->dev, "net device private data init failed\n");
2643 		goto err_sw_init;
2644 	}
2645 	/* set max MTU */
2646 	atl1c_set_max_mtu(netdev);
2647 
2648 	atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
2649 
2650 	/* Init GPHY as early as possible due to power saving issue  */
2651 	atl1c_phy_reset(&adapter->hw);
2652 
2653 	err = atl1c_reset_mac(&adapter->hw);
2654 	if (err) {
2655 		err = -EIO;
2656 		goto err_reset;
2657 	}
2658 
2659 	/* reset the controller to
2660 	 * put the device in a known good starting state */
2661 	err = atl1c_phy_init(&adapter->hw);
2662 	if (err) {
2663 		err = -EIO;
2664 		goto err_reset;
2665 	}
2666 	if (atl1c_read_mac_addr(&adapter->hw)) {
2667 		/* got a random MAC address, set NET_ADDR_RANDOM to netdev */
2668 		netdev->addr_assign_type = NET_ADDR_RANDOM;
2669 	}
2670 	memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2671 	if (netif_msg_probe(adapter))
2672 		dev_dbg(&pdev->dev, "mac address : %pM\n",
2673 			adapter->hw.mac_addr);
2674 
2675 	atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.mac_addr);
2676 	INIT_WORK(&adapter->common_task, atl1c_common_task);
2677 	adapter->work_event = 0;
2678 	err = register_netdev(netdev);
2679 	if (err) {
2680 		dev_err(&pdev->dev, "register netdevice failed\n");
2681 		goto err_register;
2682 	}
2683 
2684 	cards_found++;
2685 	return 0;
2686 
2687 err_reset:
2688 err_register:
2689 err_sw_init:
2690 	iounmap(adapter->hw.hw_addr);
2691 err_init_netdev:
2692 err_ioremap:
2693 	free_netdev(netdev);
2694 err_alloc_etherdev:
2695 	pci_release_regions(pdev);
2696 err_pci_reg:
2697 err_dma:
2698 	pci_disable_device(pdev);
2699 	return err;
2700 }
2701 
2702 /**
2703  * atl1c_remove - Device Removal Routine
2704  * @pdev: PCI device information struct
2705  *
2706  * atl1c_remove is called by the PCI subsystem to alert the driver
2707  * that it should release a PCI device.  The could be caused by a
2708  * Hot-Plug event, or because the driver is going to be removed from
2709  * memory.
2710  */
2711 static void atl1c_remove(struct pci_dev *pdev)
2712 {
2713 	struct net_device *netdev = pci_get_drvdata(pdev);
2714 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2715 
2716 	unregister_netdev(netdev);
2717 	/* restore permanent address */
2718 	atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.perm_mac_addr);
2719 	atl1c_phy_disable(&adapter->hw);
2720 
2721 	iounmap(adapter->hw.hw_addr);
2722 
2723 	pci_release_regions(pdev);
2724 	pci_disable_device(pdev);
2725 	free_netdev(netdev);
2726 }
2727 
2728 /**
2729  * atl1c_io_error_detected - called when PCI error is detected
2730  * @pdev: Pointer to PCI device
2731  * @state: The current pci connection state
2732  *
2733  * This function is called after a PCI bus error affecting
2734  * this device has been detected.
2735  */
2736 static pci_ers_result_t atl1c_io_error_detected(struct pci_dev *pdev,
2737 						pci_channel_state_t state)
2738 {
2739 	struct net_device *netdev = pci_get_drvdata(pdev);
2740 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2741 
2742 	netif_device_detach(netdev);
2743 
2744 	if (state == pci_channel_io_perm_failure)
2745 		return PCI_ERS_RESULT_DISCONNECT;
2746 
2747 	if (netif_running(netdev))
2748 		atl1c_down(adapter);
2749 
2750 	pci_disable_device(pdev);
2751 
2752 	/* Request a slot slot reset. */
2753 	return PCI_ERS_RESULT_NEED_RESET;
2754 }
2755 
2756 /**
2757  * atl1c_io_slot_reset - called after the pci bus has been reset.
2758  * @pdev: Pointer to PCI device
2759  *
2760  * Restart the card from scratch, as if from a cold-boot. Implementation
2761  * resembles the first-half of the e1000_resume routine.
2762  */
2763 static pci_ers_result_t atl1c_io_slot_reset(struct pci_dev *pdev)
2764 {
2765 	struct net_device *netdev = pci_get_drvdata(pdev);
2766 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2767 
2768 	if (pci_enable_device(pdev)) {
2769 		if (netif_msg_hw(adapter))
2770 			dev_err(&pdev->dev,
2771 				"Cannot re-enable PCI device after reset\n");
2772 		return PCI_ERS_RESULT_DISCONNECT;
2773 	}
2774 	pci_set_master(pdev);
2775 
2776 	pci_enable_wake(pdev, PCI_D3hot, 0);
2777 	pci_enable_wake(pdev, PCI_D3cold, 0);
2778 
2779 	atl1c_reset_mac(&adapter->hw);
2780 
2781 	return PCI_ERS_RESULT_RECOVERED;
2782 }
2783 
2784 /**
2785  * atl1c_io_resume - called when traffic can start flowing again.
2786  * @pdev: Pointer to PCI device
2787  *
2788  * This callback is called when the error recovery driver tells us that
2789  * its OK to resume normal operation. Implementation resembles the
2790  * second-half of the atl1c_resume routine.
2791  */
2792 static void atl1c_io_resume(struct pci_dev *pdev)
2793 {
2794 	struct net_device *netdev = pci_get_drvdata(pdev);
2795 	struct atl1c_adapter *adapter = netdev_priv(netdev);
2796 
2797 	if (netif_running(netdev)) {
2798 		if (atl1c_up(adapter)) {
2799 			if (netif_msg_hw(adapter))
2800 				dev_err(&pdev->dev,
2801 					"Cannot bring device back up after reset\n");
2802 			return;
2803 		}
2804 	}
2805 
2806 	netif_device_attach(netdev);
2807 }
2808 
2809 static const struct pci_error_handlers atl1c_err_handler = {
2810 	.error_detected = atl1c_io_error_detected,
2811 	.slot_reset = atl1c_io_slot_reset,
2812 	.resume = atl1c_io_resume,
2813 };
2814 
2815 static SIMPLE_DEV_PM_OPS(atl1c_pm_ops, atl1c_suspend, atl1c_resume);
2816 
2817 static struct pci_driver atl1c_driver = {
2818 	.name     = atl1c_driver_name,
2819 	.id_table = atl1c_pci_tbl,
2820 	.probe    = atl1c_probe,
2821 	.remove   = atl1c_remove,
2822 	.shutdown = atl1c_shutdown,
2823 	.err_handler = &atl1c_err_handler,
2824 	.driver.pm = &atl1c_pm_ops,
2825 };
2826 
2827 module_pci_driver(atl1c_driver);
2828