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