blob: d03112c3e4e974e5cfef261c1e3a21fc4b9daa2c [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
Jie Yang8f574b32010-06-01 00:28:12 -070024#define ATL1C_DRV_VERSION "1.0.1.0-NAPI"
Jie Yang43250dd2009-02-18 17:24:15 -080025char atl1c_driver_name[] = "atl1c";
26char atl1c_driver_version[] = ATL1C_DRV_VERSION;
Luis R. Rodriguez496c1852010-02-16 15:16:45 -080027
Jie Yang43250dd2009-02-18 17:24:15 -080028/*
29 * atl1c_pci_tbl - PCI Device ID Table
30 *
31 * Wildcard entries (PCI_ANY_ID) should come last
32 * Last entry must be all 0s
33 *
34 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
35 * Class, Class Mask, private data (not used) }
36 */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000037static DEFINE_PCI_DEVICE_TABLE(atl1c_pci_tbl) = {
Jie Yang43250dd2009-02-18 17:24:15 -080038 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1C)},
39 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2C)},
Luis R. Rodriguez496c1852010-02-16 15:16:45 -080040 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B)},
41 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B2)},
42 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D)},
Chuck Ebbert94dde7e2011-02-02 15:02:08 -080043 {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D_2_0)},
Jie Yang43250dd2009-02-18 17:24:15 -080044 /* required last entry */
45 { 0 }
46};
47MODULE_DEVICE_TABLE(pci, atl1c_pci_tbl);
48
Huang, Xiong70c9fbd2012-04-17 19:32:25 +000049MODULE_AUTHOR("Jie Yang");
50MODULE_AUTHOR("Qualcomm Atheros Inc., <nic-devel@qualcomm.com>");
51MODULE_DESCRIPTION("Qualcom Atheros 100/1000M Ethernet Network Driver");
Jie Yang43250dd2009-02-18 17:24:15 -080052MODULE_LICENSE("GPL");
53MODULE_VERSION(ATL1C_DRV_VERSION);
54
55static int atl1c_stop_mac(struct atl1c_hw *hw);
56static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw);
57static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw);
58static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
Huang, Xiong024e1e42012-04-18 22:01:28 +000059static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed);
Jie Yang43250dd2009-02-18 17:24:15 -080060static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter);
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +000061static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
Jie Yang43250dd2009-02-18 17:24:15 -080062 int *work_done, int work_to_do);
stephen hemminger0fb1e542010-10-21 07:50:49 +000063static int atl1c_up(struct atl1c_adapter *adapter);
64static void atl1c_down(struct atl1c_adapter *adapter);
Jie Yang43250dd2009-02-18 17:24:15 -080065
66static const u16 atl1c_pay_load_size[] = {
67 128, 256, 512, 1024, 2048, 4096,
68};
69
Jie Yang43250dd2009-02-18 17:24:15 -080070
71static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
72 NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
Jie Yang8f574b32010-06-01 00:28:12 -070073static void atl1c_pcie_patch(struct atl1c_hw *hw)
74{
Huang, Xiong5cbdcc22012-04-18 22:01:27 +000075 u32 mst_data, data;
76
77 /* pclk sel could switch to 25M */
78 AT_READ_REG(hw, REG_MASTER_CTRL, &mst_data);
79 mst_data &= ~MASTER_CTRL_CLK_SEL_DIS;
80 AT_WRITE_REG(hw, REG_MASTER_CTRL, mst_data);
Jie Yang43250dd2009-02-18 17:24:15 -080081
Huang, Xiongebe22ed2012-04-18 22:01:29 +000082 /* WoL/PCIE related settings */
83 if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
84 AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
85 data |= PCIE_PHYMISC_FORCE_RCV_DET;
86 AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
87 } else { /* new dev set bit5 of MASTER */
88 if (!(mst_data & MASTER_CTRL_WAKEN_25M))
89 AT_WRITE_REG(hw, REG_MASTER_CTRL,
90 mst_data | MASTER_CTRL_WAKEN_25M);
91 }
92 /* aspm/PCIE setting only for l2cb 1.0 */
Jie Yang8f574b32010-06-01 00:28:12 -070093 if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
94 AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
Huang, Xiongebe22ed2012-04-18 22:01:29 +000095 data = FIELD_SETX(data, PCIE_PHYMISC2_CDR_BW,
96 L2CB1_PCIE_PHYMISC2_CDR_BW);
97 data = FIELD_SETX(data, PCIE_PHYMISC2_L0S_TH,
98 L2CB1_PCIE_PHYMISC2_L0S_TH);
Jie Yang8f574b32010-06-01 00:28:12 -070099 AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
Huang, Xiongebe22ed2012-04-18 22:01:29 +0000100 /* extend L1 sync timer */
101 AT_READ_REG(hw, REG_LINK_CTRL, &data);
102 data |= LINK_CTRL_EXT_SYNC;
103 AT_WRITE_REG(hw, REG_LINK_CTRL, data);
104 }
105 /* l2cb 1.x & l1d 1.x */
106 if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d) {
107 AT_READ_REG(hw, REG_PM_CTRL, &data);
108 data |= PM_CTRL_L0S_BUFSRX_EN;
109 AT_WRITE_REG(hw, REG_PM_CTRL, data);
110 /* clear vendor msg */
111 AT_READ_REG(hw, REG_DMA_DBG, &data);
112 AT_WRITE_REG(hw, REG_DMA_DBG, data & ~DMA_DBG_VENDOR_MSG);
Jie Yang8f574b32010-06-01 00:28:12 -0700113 }
114}
115
116/* FIXME: no need any more ? */
Jie Yang43250dd2009-02-18 17:24:15 -0800117/*
118 * atl1c_init_pcie - init PCIE module
119 */
120static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
121{
122 u32 data;
123 u32 pci_cmd;
124 struct pci_dev *pdev = hw->adapter->pdev;
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000125 int pos;
Jie Yang43250dd2009-02-18 17:24:15 -0800126
127 AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
128 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
129 pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
130 PCI_COMMAND_IO);
131 AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
132
133 /*
134 * Clear any PowerSaveing Settings
135 */
136 pci_enable_wake(pdev, PCI_D3hot, 0);
137 pci_enable_wake(pdev, PCI_D3cold, 0);
138
139 /*
140 * Mask some pcie error bits
141 */
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000142 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
143 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &data);
144 data &= ~(PCI_ERR_UNC_DLP | PCI_ERR_UNC_FCP);
145 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, data);
146 /* clear error status */
147 pci_write_config_word(pdev, pci_pcie_cap(pdev) + PCI_EXP_DEVSTA,
148 PCI_EXP_DEVSTA_NFED |
149 PCI_EXP_DEVSTA_FED |
150 PCI_EXP_DEVSTA_CED |
151 PCI_EXP_DEVSTA_URD);
Jie Yang43250dd2009-02-18 17:24:15 -0800152
Jie Yang8f574b32010-06-01 00:28:12 -0700153 AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
154 data &= ~LTSSM_ID_EN_WRO;
155 AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
156
157 atl1c_pcie_patch(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800158 if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
159 atl1c_disable_l0s_l1(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800160
Jie Yang8f574b32010-06-01 00:28:12 -0700161 msleep(5);
Jie Yang43250dd2009-02-18 17:24:15 -0800162}
163
164/*
165 * atl1c_irq_enable - Enable default interrupt generation settings
166 * @adapter: board private structure
167 */
168static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
169{
170 if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
171 AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
172 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
173 AT_WRITE_FLUSH(&adapter->hw);
174 }
175}
176
177/*
178 * atl1c_irq_disable - Mask off interrupt generation on the NIC
179 * @adapter: board private structure
180 */
181static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
182{
183 atomic_inc(&adapter->irq_sem);
184 AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
Jie Yang8f574b32010-06-01 00:28:12 -0700185 AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
Jie Yang43250dd2009-02-18 17:24:15 -0800186 AT_WRITE_FLUSH(&adapter->hw);
187 synchronize_irq(adapter->pdev->irq);
188}
189
190/*
191 * atl1c_irq_reset - reset interrupt confiure on the NIC
192 * @adapter: board private structure
193 */
194static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
195{
196 atomic_set(&adapter->irq_sem, 1);
197 atl1c_irq_enable(adapter);
198}
199
200/*
Joe Perchesc930a662009-05-31 20:44:54 +0000201 * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
202 * of the idle status register until the device is actually idle
203 */
Huang, Xiong969a7ee2012-04-18 22:01:22 +0000204static u32 atl1c_wait_until_idle(struct atl1c_hw *hw, u32 modu_ctrl)
Joe Perchesc930a662009-05-31 20:44:54 +0000205{
206 int timeout;
207 u32 data;
208
209 for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
210 AT_READ_REG(hw, REG_IDLE_STATUS, &data);
Huang, Xiong969a7ee2012-04-18 22:01:22 +0000211 if ((data & modu_ctrl) == 0)
Joe Perchesc930a662009-05-31 20:44:54 +0000212 return 0;
213 msleep(1);
214 }
215 return data;
216}
217
218/*
Jie Yang43250dd2009-02-18 17:24:15 -0800219 * atl1c_phy_config - Timer Call-back
220 * @data: pointer to netdev cast into an unsigned long
221 */
222static void atl1c_phy_config(unsigned long data)
223{
224 struct atl1c_adapter *adapter = (struct atl1c_adapter *) data;
225 struct atl1c_hw *hw = &adapter->hw;
226 unsigned long flags;
227
228 spin_lock_irqsave(&adapter->mdio_lock, flags);
229 atl1c_restart_autoneg(hw);
230 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
231}
232
233void atl1c_reinit_locked(struct atl1c_adapter *adapter)
234{
Jie Yang43250dd2009-02-18 17:24:15 -0800235 WARN_ON(in_interrupt());
236 atl1c_down(adapter);
237 atl1c_up(adapter);
238 clear_bit(__AT_RESETTING, &adapter->flags);
239}
240
Jie Yang43250dd2009-02-18 17:24:15 -0800241static void atl1c_check_link_status(struct atl1c_adapter *adapter)
242{
243 struct atl1c_hw *hw = &adapter->hw;
244 struct net_device *netdev = adapter->netdev;
245 struct pci_dev *pdev = adapter->pdev;
246 int err;
247 unsigned long flags;
248 u16 speed, duplex, phy_data;
249
250 spin_lock_irqsave(&adapter->mdio_lock, flags);
251 /* MII_BMSR must read twise */
252 atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
253 atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
254 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
255
256 if ((phy_data & BMSR_LSTATUS) == 0) {
257 /* link down */
Jie Yang8f574b32010-06-01 00:28:12 -0700258 hw->hibernate = true;
259 if (atl1c_stop_mac(hw) != 0)
260 if (netif_msg_hw(adapter))
261 dev_warn(&pdev->dev, "stop mac failed\n");
Huang, Xiong024e1e42012-04-18 22:01:28 +0000262 atl1c_set_aspm(hw, SPEED_0);
Jie Yang43250dd2009-02-18 17:24:15 -0800263 netif_carrier_off(netdev);
Jie Yang8f574b32010-06-01 00:28:12 -0700264 netif_stop_queue(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -0800265 } else {
266 /* Link Up */
267 hw->hibernate = false;
268 spin_lock_irqsave(&adapter->mdio_lock, flags);
269 err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
270 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
271 if (unlikely(err))
272 return;
273 /* link result is our setting */
274 if (adapter->link_speed != speed ||
275 adapter->link_duplex != duplex) {
276 adapter->link_speed = speed;
277 adapter->link_duplex = duplex;
Huang, Xiong024e1e42012-04-18 22:01:28 +0000278 atl1c_set_aspm(hw, speed);
Jie Yang43250dd2009-02-18 17:24:15 -0800279 atl1c_enable_tx_ctrl(hw);
280 atl1c_enable_rx_ctrl(hw);
281 atl1c_setup_mac_ctrl(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -0800282 if (netif_msg_link(adapter))
283 dev_info(&pdev->dev,
284 "%s: %s NIC Link is Up<%d Mbps %s>\n",
285 atl1c_driver_name, netdev->name,
286 adapter->link_speed,
287 adapter->link_duplex == FULL_DUPLEX ?
288 "Full Duplex" : "Half Duplex");
289 }
290 if (!netif_carrier_ok(netdev))
291 netif_carrier_on(netdev);
292 }
293}
294
Jie Yang43250dd2009-02-18 17:24:15 -0800295static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
296{
297 struct net_device *netdev = adapter->netdev;
298 struct pci_dev *pdev = adapter->pdev;
299 u16 phy_data;
300 u16 link_up;
301
302 spin_lock(&adapter->mdio_lock);
303 atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
304 atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
305 spin_unlock(&adapter->mdio_lock);
306 link_up = phy_data & BMSR_LSTATUS;
307 /* notify upper layer link down ASAP */
308 if (!link_up) {
309 if (netif_carrier_ok(netdev)) {
310 /* old link state: Up */
311 netif_carrier_off(netdev);
312 if (netif_msg_link(adapter))
313 dev_info(&pdev->dev,
314 "%s: %s NIC Link is Down\n",
315 atl1c_driver_name, netdev->name);
316 adapter->link_speed = SPEED_0;
317 }
318 }
Jie Yangcb190542009-12-06 23:16:58 +0000319
Tim Gardnercb771832011-04-20 09:00:49 +0000320 set_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +0000321 schedule_work(&adapter->common_task);
Jie Yang43250dd2009-02-18 17:24:15 -0800322}
323
Jie Yangcb190542009-12-06 23:16:58 +0000324static void atl1c_common_task(struct work_struct *work)
325{
326 struct atl1c_adapter *adapter;
327 struct net_device *netdev;
328
329 adapter = container_of(work, struct atl1c_adapter, common_task);
330 netdev = adapter->netdev;
331
Tim Gardnercb771832011-04-20 09:00:49 +0000332 if (test_and_clear_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event)) {
Jie Yangcb190542009-12-06 23:16:58 +0000333 netif_device_detach(netdev);
334 atl1c_down(adapter);
335 atl1c_up(adapter);
336 netif_device_attach(netdev);
Jie Yangcb190542009-12-06 23:16:58 +0000337 }
338
Tim Gardnercb771832011-04-20 09:00:49 +0000339 if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE,
340 &adapter->work_event))
Jie Yangcb190542009-12-06 23:16:58 +0000341 atl1c_check_link_status(adapter);
Jie Yangcb190542009-12-06 23:16:58 +0000342}
343
344
Jie Yang43250dd2009-02-18 17:24:15 -0800345static void atl1c_del_timer(struct atl1c_adapter *adapter)
346{
347 del_timer_sync(&adapter->phy_config_timer);
348}
349
Jie Yang43250dd2009-02-18 17:24:15 -0800350
351/*
352 * atl1c_tx_timeout - Respond to a Tx Hang
353 * @netdev: network interface device structure
354 */
355static void atl1c_tx_timeout(struct net_device *netdev)
356{
357 struct atl1c_adapter *adapter = netdev_priv(netdev);
358
359 /* Do the reset outside of interrupt context */
Tim Gardnercb771832011-04-20 09:00:49 +0000360 set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +0000361 schedule_work(&adapter->common_task);
Jie Yang43250dd2009-02-18 17:24:15 -0800362}
363
364/*
365 * atl1c_set_multi - Multicast and Promiscuous mode set
366 * @netdev: network interface device structure
367 *
368 * The set_multi entry point is called whenever the multicast address
369 * list or the network interface flags are updated. This routine is
370 * responsible for configuring the hardware for proper multicast,
371 * promiscuous mode, and all-multi behavior.
372 */
373static void atl1c_set_multi(struct net_device *netdev)
374{
375 struct atl1c_adapter *adapter = netdev_priv(netdev);
376 struct atl1c_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000377 struct netdev_hw_addr *ha;
Jie Yang43250dd2009-02-18 17:24:15 -0800378 u32 mac_ctrl_data;
379 u32 hash_value;
380
381 /* Check for Promiscuous and All Multicast modes */
382 AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
383
384 if (netdev->flags & IFF_PROMISC) {
385 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
386 } else if (netdev->flags & IFF_ALLMULTI) {
387 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
388 mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
389 } else {
390 mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
391 }
392
393 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
394
395 /* clear the old settings from the multicast hash table */
396 AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
397 AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
398
399 /* comoute mc addresses' hash value ,and put it into hash table */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000400 netdev_for_each_mc_addr(ha, netdev) {
401 hash_value = atl1c_hash_mc_addr(hw, ha->addr);
Jie Yang43250dd2009-02-18 17:24:15 -0800402 atl1c_hash_set(hw, hash_value);
403 }
404}
405
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000406static void __atl1c_vlan_mode(netdev_features_t features, u32 *mac_ctrl_data)
Jiri Pirko46facce2011-07-20 04:54:12 +0000407{
408 if (features & NETIF_F_HW_VLAN_RX) {
409 /* enable VLAN tag insert/strip */
410 *mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
411 } else {
412 /* disable VLAN tag insert/strip */
413 *mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
414 }
415}
416
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000417static void atl1c_vlan_mode(struct net_device *netdev,
418 netdev_features_t features)
Jie Yang43250dd2009-02-18 17:24:15 -0800419{
420 struct atl1c_adapter *adapter = netdev_priv(netdev);
421 struct pci_dev *pdev = adapter->pdev;
422 u32 mac_ctrl_data = 0;
423
424 if (netif_msg_pktdata(adapter))
Jiri Pirko46facce2011-07-20 04:54:12 +0000425 dev_dbg(&pdev->dev, "atl1c_vlan_mode\n");
Jie Yang43250dd2009-02-18 17:24:15 -0800426
427 atl1c_irq_disable(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -0800428 AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
Jiri Pirko46facce2011-07-20 04:54:12 +0000429 __atl1c_vlan_mode(features, &mac_ctrl_data);
Jie Yang43250dd2009-02-18 17:24:15 -0800430 AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
431 atl1c_irq_enable(adapter);
432}
433
434static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
435{
436 struct pci_dev *pdev = adapter->pdev;
437
438 if (netif_msg_pktdata(adapter))
Jiri Pirko46facce2011-07-20 04:54:12 +0000439 dev_dbg(&pdev->dev, "atl1c_restore_vlan\n");
440 atl1c_vlan_mode(adapter->netdev, adapter->netdev->features);
Jie Yang43250dd2009-02-18 17:24:15 -0800441}
Jiri Pirko46facce2011-07-20 04:54:12 +0000442
Jie Yang43250dd2009-02-18 17:24:15 -0800443/*
444 * atl1c_set_mac - Change the Ethernet Address of the NIC
445 * @netdev: network interface device structure
446 * @p: pointer to an address structure
447 *
448 * Returns 0 on success, negative on failure
449 */
450static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
451{
452 struct atl1c_adapter *adapter = netdev_priv(netdev);
453 struct sockaddr *addr = p;
454
455 if (!is_valid_ether_addr(addr->sa_data))
456 return -EADDRNOTAVAIL;
457
458 if (netif_running(netdev))
459 return -EBUSY;
460
461 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
462 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
Danny Kukawka6a214fd2012-02-17 05:43:30 +0000463 netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
Jie Yang43250dd2009-02-18 17:24:15 -0800464
465 atl1c_hw_set_mac_addr(&adapter->hw);
466
467 return 0;
468}
469
470static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
471 struct net_device *dev)
472{
473 int mtu = dev->mtu;
474
475 adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
476 roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
477}
Michał Mirosław782d6402011-04-07 07:32:18 +0000478
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000479static netdev_features_t atl1c_fix_features(struct net_device *netdev,
480 netdev_features_t features)
Michał Mirosław782d6402011-04-07 07:32:18 +0000481{
Jiri Pirko46facce2011-07-20 04:54:12 +0000482 /*
483 * Since there is no support for separate rx/tx vlan accel
484 * enable/disable make sure tx flag is always in same state as rx.
485 */
486 if (features & NETIF_F_HW_VLAN_RX)
487 features |= NETIF_F_HW_VLAN_TX;
488 else
489 features &= ~NETIF_F_HW_VLAN_TX;
490
Michał Mirosław782d6402011-04-07 07:32:18 +0000491 if (netdev->mtu > MAX_TSO_FRAME_SIZE)
492 features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
493
494 return features;
495}
496
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000497static int atl1c_set_features(struct net_device *netdev,
498 netdev_features_t features)
Jiri Pirko46facce2011-07-20 04:54:12 +0000499{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000500 netdev_features_t changed = netdev->features ^ features;
Jiri Pirko46facce2011-07-20 04:54:12 +0000501
502 if (changed & NETIF_F_HW_VLAN_RX)
503 atl1c_vlan_mode(netdev, features);
504
505 return 0;
506}
507
Jie Yang43250dd2009-02-18 17:24:15 -0800508/*
509 * atl1c_change_mtu - Change the Maximum Transfer Unit
510 * @netdev: network interface device structure
511 * @new_mtu: new value for maximum frame size
512 *
513 * Returns 0 on success, negative on failure
514 */
515static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
516{
517 struct atl1c_adapter *adapter = netdev_priv(netdev);
Huang, Xiongc08b9b22012-04-18 22:01:23 +0000518 struct atl1c_hw *hw = &adapter->hw;
Jie Yang43250dd2009-02-18 17:24:15 -0800519 int old_mtu = netdev->mtu;
520 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
521
Huang, Xiongc08b9b22012-04-18 22:01:23 +0000522 /* Fast Ethernet controller doesn't support jumbo packet */
523 if (((hw->nic_type == athr_l2c ||
524 hw->nic_type == athr_l2c_b ||
525 hw->nic_type == athr_l2c_b2) && new_mtu > ETH_DATA_LEN) ||
526 max_frame < ETH_ZLEN + ETH_FCS_LEN ||
527 max_frame > MAX_JUMBO_FRAME_SIZE) {
Jie Yang43250dd2009-02-18 17:24:15 -0800528 if (netif_msg_link(adapter))
529 dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
530 return -EINVAL;
531 }
532 /* set MTU */
533 if (old_mtu != new_mtu && netif_running(netdev)) {
534 while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
535 msleep(1);
536 netdev->mtu = new_mtu;
537 adapter->hw.max_frame_size = new_mtu;
538 atl1c_set_rxbufsize(adapter, netdev);
539 atl1c_down(adapter);
Michał Mirosław782d6402011-04-07 07:32:18 +0000540 netdev_update_features(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -0800541 atl1c_up(adapter);
542 clear_bit(__AT_RESETTING, &adapter->flags);
543 if (adapter->hw.ctrl_flags & ATL1C_FPGA_VERSION) {
544 u32 phy_data;
545
546 AT_READ_REG(&adapter->hw, 0x1414, &phy_data);
547 phy_data |= 0x10000000;
548 AT_WRITE_REG(&adapter->hw, 0x1414, phy_data);
549 }
550
551 }
552 return 0;
553}
554
555/*
556 * caller should hold mdio_lock
557 */
558static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
559{
560 struct atl1c_adapter *adapter = netdev_priv(netdev);
561 u16 result;
562
Huang, Xiong2528a5d2012-04-18 22:01:31 +0000563 atl1c_read_phy_reg(&adapter->hw, reg_num, &result);
Jie Yang43250dd2009-02-18 17:24:15 -0800564 return result;
565}
566
567static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
568 int reg_num, int val)
569{
570 struct atl1c_adapter *adapter = netdev_priv(netdev);
571
Huang, Xiong2528a5d2012-04-18 22:01:31 +0000572 atl1c_write_phy_reg(&adapter->hw, reg_num, val);
Jie Yang43250dd2009-02-18 17:24:15 -0800573}
574
575/*
576 * atl1c_mii_ioctl -
577 * @netdev:
578 * @ifreq:
579 * @cmd:
580 */
581static int atl1c_mii_ioctl(struct net_device *netdev,
582 struct ifreq *ifr, int cmd)
583{
584 struct atl1c_adapter *adapter = netdev_priv(netdev);
585 struct pci_dev *pdev = adapter->pdev;
586 struct mii_ioctl_data *data = if_mii(ifr);
587 unsigned long flags;
588 int retval = 0;
589
590 if (!netif_running(netdev))
591 return -EINVAL;
592
593 spin_lock_irqsave(&adapter->mdio_lock, flags);
594 switch (cmd) {
595 case SIOCGMIIPHY:
596 data->phy_id = 0;
597 break;
598
599 case SIOCGMIIREG:
Jie Yang43250dd2009-02-18 17:24:15 -0800600 if (atl1c_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
601 &data->val_out)) {
602 retval = -EIO;
603 goto out;
604 }
605 break;
606
607 case SIOCSMIIREG:
Jie Yang43250dd2009-02-18 17:24:15 -0800608 if (data->reg_num & ~(0x1F)) {
609 retval = -EFAULT;
610 goto out;
611 }
612
613 dev_dbg(&pdev->dev, "<atl1c_mii_ioctl> write %x %x",
614 data->reg_num, data->val_in);
615 if (atl1c_write_phy_reg(&adapter->hw,
616 data->reg_num, data->val_in)) {
617 retval = -EIO;
618 goto out;
619 }
620 break;
621
622 default:
623 retval = -EOPNOTSUPP;
624 break;
625 }
626out:
627 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
628 return retval;
629}
630
631/*
632 * atl1c_ioctl -
633 * @netdev:
634 * @ifreq:
635 * @cmd:
636 */
637static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
638{
639 switch (cmd) {
640 case SIOCGMIIPHY:
641 case SIOCGMIIREG:
642 case SIOCSMIIREG:
643 return atl1c_mii_ioctl(netdev, ifr, cmd);
644 default:
645 return -EOPNOTSUPP;
646 }
647}
648
649/*
650 * atl1c_alloc_queues - Allocate memory for all rings
651 * @adapter: board private structure to initialize
652 *
653 */
654static int __devinit atl1c_alloc_queues(struct atl1c_adapter *adapter)
655{
656 return 0;
657}
658
659static void atl1c_set_mac_type(struct atl1c_hw *hw)
660{
661 switch (hw->device_id) {
662 case PCI_DEVICE_ID_ATTANSIC_L2C:
663 hw->nic_type = athr_l2c;
664 break;
Jie Yang43250dd2009-02-18 17:24:15 -0800665 case PCI_DEVICE_ID_ATTANSIC_L1C:
666 hw->nic_type = athr_l1c;
667 break;
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800668 case PCI_DEVICE_ID_ATHEROS_L2C_B:
669 hw->nic_type = athr_l2c_b;
670 break;
671 case PCI_DEVICE_ID_ATHEROS_L2C_B2:
672 hw->nic_type = athr_l2c_b2;
673 break;
674 case PCI_DEVICE_ID_ATHEROS_L1D:
675 hw->nic_type = athr_l1d;
676 break;
Jie Yang8f574b32010-06-01 00:28:12 -0700677 case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
678 hw->nic_type = athr_l1d_2;
679 break;
Jie Yang43250dd2009-02-18 17:24:15 -0800680 default:
681 break;
682 }
683}
684
685static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
686{
Jie Yang43250dd2009-02-18 17:24:15 -0800687 u32 link_ctrl_data;
688
689 atl1c_set_mac_type(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800690 AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
691
Jie Yang8f574b32010-06-01 00:28:12 -0700692 hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE |
Jie Yang43250dd2009-02-18 17:24:15 -0800693 ATL1C_TXQ_MODE_ENHANCE;
Huang, Xiong024e1e42012-04-18 22:01:28 +0000694 hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT |
695 ATL1C_ASPM_L1_SUPPORT;
Jie Yang8f574b32010-06-01 00:28:12 -0700696 hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
Jie Yang43250dd2009-02-18 17:24:15 -0800697
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800698 if (hw->nic_type == athr_l1c ||
Jie Yang8f574b32010-06-01 00:28:12 -0700699 hw->nic_type == athr_l1d ||
700 hw->nic_type == athr_l1d_2)
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800701 hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
Jie Yang43250dd2009-02-18 17:24:15 -0800702 return 0;
703}
704/*
705 * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
706 * @adapter: board private structure to initialize
707 *
708 * atl1c_sw_init initializes the Adapter private data structure.
709 * Fields are initialized based on PCI device information and
710 * OS network device settings (MTU size).
711 */
712static int __devinit atl1c_sw_init(struct atl1c_adapter *adapter)
713{
714 struct atl1c_hw *hw = &adapter->hw;
715 struct pci_dev *pdev = adapter->pdev;
Jie Yang8f574b32010-06-01 00:28:12 -0700716 u32 revision;
717
Jie Yang43250dd2009-02-18 17:24:15 -0800718
719 adapter->wol = 0;
Rafael J. Wysocki762e3022010-12-22 03:07:52 +0000720 device_set_wakeup_enable(&pdev->dev, false);
Jie Yang43250dd2009-02-18 17:24:15 -0800721 adapter->link_speed = SPEED_0;
722 adapter->link_duplex = FULL_DUPLEX;
Jie Yang43250dd2009-02-18 17:24:15 -0800723 adapter->tpd_ring[0].count = 1024;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000724 adapter->rfd_ring.count = 512;
Jie Yang43250dd2009-02-18 17:24:15 -0800725
726 hw->vendor_id = pdev->vendor;
727 hw->device_id = pdev->device;
728 hw->subsystem_vendor_id = pdev->subsystem_vendor;
729 hw->subsystem_id = pdev->subsystem_device;
Jie Yang8f574b32010-06-01 00:28:12 -0700730 AT_READ_REG(hw, PCI_CLASS_REVISION, &revision);
731 hw->revision_id = revision & 0xFF;
Jie Yang43250dd2009-02-18 17:24:15 -0800732 /* before link up, we assume hibernate is true */
733 hw->hibernate = true;
734 hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
735 if (atl1c_setup_mac_funcs(hw) != 0) {
736 dev_err(&pdev->dev, "set mac function pointers failed\n");
737 return -1;
738 }
739 hw->intr_mask = IMR_NORMAL_MASK;
740 hw->phy_configured = false;
741 hw->preamble_len = 7;
742 hw->max_frame_size = adapter->netdev->mtu;
Jie Yang43250dd2009-02-18 17:24:15 -0800743 hw->autoneg_advertised = ADVERTISED_Autoneg;
744 hw->indirect_tab = 0xE4E4E4E4;
745 hw->base_cpu = 0;
746
747 hw->ict = 50000; /* 100ms */
748 hw->smb_timer = 200000; /* 400ms */
Jie Yang43250dd2009-02-18 17:24:15 -0800749 hw->rx_imt = 200;
750 hw->tx_imt = 1000;
751
752 hw->tpd_burst = 5;
753 hw->rfd_burst = 8;
754 hw->dma_order = atl1c_dma_ord_out;
755 hw->dmar_block = atl1c_dma_req_1024;
Jie Yang43250dd2009-02-18 17:24:15 -0800756
757 if (atl1c_alloc_queues(adapter)) {
758 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
759 return -ENOMEM;
760 }
761 /* TODO */
762 atl1c_set_rxbufsize(adapter, adapter->netdev);
763 atomic_set(&adapter->irq_sem, 1);
764 spin_lock_init(&adapter->mdio_lock);
765 spin_lock_init(&adapter->tx_lock);
766 set_bit(__AT_DOWN, &adapter->flags);
767
768 return 0;
769}
770
Jie Yangc6060be2009-11-06 00:32:05 -0800771static inline void atl1c_clean_buffer(struct pci_dev *pdev,
772 struct atl1c_buffer *buffer_info, int in_irq)
773{
Jie Yang4b45e342009-12-06 22:56:59 +0000774 u16 pci_driection;
Jie Yangc6060be2009-11-06 00:32:05 -0800775 if (buffer_info->flags & ATL1C_BUFFER_FREE)
776 return;
777 if (buffer_info->dma) {
Jie Yang4b45e342009-12-06 22:56:59 +0000778 if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
779 pci_driection = PCI_DMA_FROMDEVICE;
780 else
781 pci_driection = PCI_DMA_TODEVICE;
782
Jie Yangc6060be2009-11-06 00:32:05 -0800783 if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
784 pci_unmap_single(pdev, buffer_info->dma,
Jie Yang4b45e342009-12-06 22:56:59 +0000785 buffer_info->length, pci_driection);
Jie Yangc6060be2009-11-06 00:32:05 -0800786 else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
787 pci_unmap_page(pdev, buffer_info->dma,
Jie Yang4b45e342009-12-06 22:56:59 +0000788 buffer_info->length, pci_driection);
Jie Yangc6060be2009-11-06 00:32:05 -0800789 }
790 if (buffer_info->skb) {
791 if (in_irq)
792 dev_kfree_skb_irq(buffer_info->skb);
793 else
794 dev_kfree_skb(buffer_info->skb);
795 }
796 buffer_info->dma = 0;
797 buffer_info->skb = NULL;
798 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
799}
Jie Yang43250dd2009-02-18 17:24:15 -0800800/*
801 * atl1c_clean_tx_ring - Free Tx-skb
802 * @adapter: board private structure
803 */
804static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
805 enum atl1c_trans_queue type)
806{
807 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
808 struct atl1c_buffer *buffer_info;
809 struct pci_dev *pdev = adapter->pdev;
810 u16 index, ring_count;
811
812 ring_count = tpd_ring->count;
813 for (index = 0; index < ring_count; index++) {
814 buffer_info = &tpd_ring->buffer_info[index];
Jie Yangc6060be2009-11-06 00:32:05 -0800815 atl1c_clean_buffer(pdev, buffer_info, 0);
Jie Yang43250dd2009-02-18 17:24:15 -0800816 }
817
818 /* Zero out Tx-buffers */
819 memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
Jie Yangc6060be2009-11-06 00:32:05 -0800820 ring_count);
Jie Yang43250dd2009-02-18 17:24:15 -0800821 atomic_set(&tpd_ring->next_to_clean, 0);
822 tpd_ring->next_to_use = 0;
823}
824
825/*
826 * atl1c_clean_rx_ring - Free rx-reservation skbs
827 * @adapter: board private structure
828 */
829static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
830{
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000831 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
832 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800833 struct atl1c_buffer *buffer_info;
834 struct pci_dev *pdev = adapter->pdev;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000835 int j;
Jie Yang43250dd2009-02-18 17:24:15 -0800836
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000837 for (j = 0; j < rfd_ring->count; j++) {
838 buffer_info = &rfd_ring->buffer_info[j];
839 atl1c_clean_buffer(pdev, buffer_info, 0);
Jie Yang43250dd2009-02-18 17:24:15 -0800840 }
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000841 /* zero out the descriptor ring */
842 memset(rfd_ring->desc, 0, rfd_ring->size);
843 rfd_ring->next_to_clean = 0;
844 rfd_ring->next_to_use = 0;
845 rrd_ring->next_to_use = 0;
846 rrd_ring->next_to_clean = 0;
Jie Yang43250dd2009-02-18 17:24:15 -0800847}
848
849/*
850 * Read / Write Ptr Initialize:
851 */
852static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
853{
854 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000855 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
856 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800857 struct atl1c_buffer *buffer_info;
858 int i, j;
859
860 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
861 tpd_ring[i].next_to_use = 0;
862 atomic_set(&tpd_ring[i].next_to_clean, 0);
863 buffer_info = tpd_ring[i].buffer_info;
864 for (j = 0; j < tpd_ring->count; j++)
Jie Yangc6060be2009-11-06 00:32:05 -0800865 ATL1C_SET_BUFFER_STATE(&buffer_info[i],
866 ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -0800867 }
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000868 rfd_ring->next_to_use = 0;
869 rfd_ring->next_to_clean = 0;
870 rrd_ring->next_to_use = 0;
871 rrd_ring->next_to_clean = 0;
872 for (j = 0; j < rfd_ring->count; j++) {
873 buffer_info = &rfd_ring->buffer_info[j];
874 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -0800875 }
876}
877
878/*
879 * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
880 * @adapter: board private structure
881 *
882 * Free all transmit software resources
883 */
884static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
885{
886 struct pci_dev *pdev = adapter->pdev;
887
888 pci_free_consistent(pdev, adapter->ring_header.size,
889 adapter->ring_header.desc,
890 adapter->ring_header.dma);
891 adapter->ring_header.desc = NULL;
892
893 /* Note: just free tdp_ring.buffer_info,
894 * it contain rfd_ring.buffer_info, do not double free */
895 if (adapter->tpd_ring[0].buffer_info) {
896 kfree(adapter->tpd_ring[0].buffer_info);
897 adapter->tpd_ring[0].buffer_info = NULL;
898 }
899}
900
901/*
902 * atl1c_setup_mem_resources - allocate Tx / RX descriptor resources
903 * @adapter: board private structure
904 *
905 * Return 0 on success, negative on failure
906 */
907static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
908{
909 struct pci_dev *pdev = adapter->pdev;
910 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000911 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
912 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800913 struct atl1c_ring_header *ring_header = &adapter->ring_header;
Jie Yang43250dd2009-02-18 17:24:15 -0800914 int size;
915 int i;
916 int count = 0;
917 int rx_desc_count = 0;
918 u32 offset = 0;
919
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000920 rrd_ring->count = rfd_ring->count;
Jie Yang43250dd2009-02-18 17:24:15 -0800921 for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
922 tpd_ring[i].count = tpd_ring[0].count;
923
Jie Yang43250dd2009-02-18 17:24:15 -0800924 /* 2 tpd queue, one high priority queue,
925 * another normal priority queue */
926 size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000927 rfd_ring->count);
Jie Yang43250dd2009-02-18 17:24:15 -0800928 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
929 if (unlikely(!tpd_ring->buffer_info)) {
930 dev_err(&pdev->dev, "kzalloc failed, size = %d\n",
931 size);
932 goto err_nomem;
933 }
934 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
935 tpd_ring[i].buffer_info =
936 (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
937 count += tpd_ring[i].count;
938 }
939
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000940 rfd_ring->buffer_info =
941 (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
942 count += rfd_ring->count;
943 rx_desc_count += rfd_ring->count;
944
Jie Yang43250dd2009-02-18 17:24:15 -0800945 /*
946 * real ring DMA buffer
947 * each ring/block may need up to 8 bytes for alignment, hence the
948 * additional bytes tacked onto the end.
949 */
950 ring_header->size = size =
951 sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
952 sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
953 sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
Huang, Xiong8d5c6832012-04-17 19:32:29 +0000954 8 * 4;
Jie Yang43250dd2009-02-18 17:24:15 -0800955
956 ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
957 &ring_header->dma);
958 if (unlikely(!ring_header->desc)) {
959 dev_err(&pdev->dev, "pci_alloc_consistend failed\n");
960 goto err_nomem;
961 }
962 memset(ring_header->desc, 0, ring_header->size);
963 /* init TPD ring */
964
965 tpd_ring[0].dma = roundup(ring_header->dma, 8);
966 offset = tpd_ring[0].dma - ring_header->dma;
967 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
968 tpd_ring[i].dma = ring_header->dma + offset;
969 tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
970 tpd_ring[i].size =
971 sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
972 offset += roundup(tpd_ring[i].size, 8);
973 }
974 /* init RFD ring */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000975 rfd_ring->dma = ring_header->dma + offset;
976 rfd_ring->desc = (u8 *) ring_header->desc + offset;
977 rfd_ring->size = sizeof(struct atl1c_rx_free_desc) * rfd_ring->count;
978 offset += roundup(rfd_ring->size, 8);
Jie Yang43250dd2009-02-18 17:24:15 -0800979
980 /* init RRD ring */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000981 rrd_ring->dma = ring_header->dma + offset;
982 rrd_ring->desc = (u8 *) ring_header->desc + offset;
983 rrd_ring->size = sizeof(struct atl1c_recv_ret_status) *
984 rrd_ring->count;
985 offset += roundup(rrd_ring->size, 8);
Jie Yang43250dd2009-02-18 17:24:15 -0800986
Jie Yang43250dd2009-02-18 17:24:15 -0800987 return 0;
988
989err_nomem:
990 kfree(tpd_ring->buffer_info);
991 return -ENOMEM;
992}
993
994static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
995{
996 struct atl1c_hw *hw = &adapter->hw;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000997 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
998 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800999 struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1000 adapter->tpd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001001
1002 /* TPD */
1003 AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
1004 (u32)((tpd_ring[atl1c_trans_normal].dma &
1005 AT_DMA_HI_ADDR_MASK) >> 32));
1006 /* just enable normal priority TX queue */
Huang, Xiong0af48332012-04-17 19:32:30 +00001007 AT_WRITE_REG(hw, REG_TPD_PRI0_ADDR_LO,
Jie Yang43250dd2009-02-18 17:24:15 -08001008 (u32)(tpd_ring[atl1c_trans_normal].dma &
1009 AT_DMA_LO_ADDR_MASK));
Huang, Xiong0af48332012-04-17 19:32:30 +00001010 AT_WRITE_REG(hw, REG_TPD_PRI1_ADDR_LO,
Jie Yang43250dd2009-02-18 17:24:15 -08001011 (u32)(tpd_ring[atl1c_trans_high].dma &
1012 AT_DMA_LO_ADDR_MASK));
1013 AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
1014 (u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
1015
1016
1017 /* RFD */
1018 AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001019 (u32)((rfd_ring->dma & AT_DMA_HI_ADDR_MASK) >> 32));
1020 AT_WRITE_REG(hw, REG_RFD0_HEAD_ADDR_LO,
1021 (u32)(rfd_ring->dma & AT_DMA_LO_ADDR_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001022
1023 AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001024 rfd_ring->count & RFD_RING_SIZE_MASK);
Jie Yang43250dd2009-02-18 17:24:15 -08001025 AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
1026 adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
1027
1028 /* RRD */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001029 AT_WRITE_REG(hw, REG_RRD0_HEAD_ADDR_LO,
1030 (u32)(rrd_ring->dma & AT_DMA_LO_ADDR_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001031 AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001032 (rrd_ring->count & RRD_RING_SIZE_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001033
Jie Yang8f574b32010-06-01 00:28:12 -07001034 if (hw->nic_type == athr_l2c_b) {
1035 AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
1036 AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
1037 AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
1038 AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
1039 AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
1040 AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
1041 AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0); /* TX watermark, to enter l1 state.*/
1042 AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0); /* RXD threshold.*/
1043 }
Jie Yang43250dd2009-02-18 17:24:15 -08001044 /* Load all of base address above */
1045 AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
1046}
1047
1048static void atl1c_configure_tx(struct atl1c_adapter *adapter)
1049{
1050 struct atl1c_hw *hw = &adapter->hw;
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001051 int max_pay_load;
Jie Yang43250dd2009-02-18 17:24:15 -08001052 u16 tx_offload_thresh;
1053 u32 txq_ctrl_data;
Jie Yang43250dd2009-02-18 17:24:15 -08001054
Huang, Xiongc08b9b22012-04-18 22:01:23 +00001055 tx_offload_thresh = MAX_TSO_FRAME_SIZE;
Jie Yang43250dd2009-02-18 17:24:15 -08001056 AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
1057 (tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001058 max_pay_load = pcie_get_readrq(adapter->pdev) >> 8;
stephen hemminger81b504b2011-02-23 09:06:49 +00001059 hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001060 /*
1061 * if BIOS had changed the dam-read-max-length to an invalid value,
1062 * restore it to default value
1063 */
1064 if (hw->dmar_block < DEVICE_CTRL_MAXRRS_MIN) {
1065 pcie_set_readrq(adapter->pdev, 128 << DEVICE_CTRL_MAXRRS_MIN);
1066 hw->dmar_block = DEVICE_CTRL_MAXRRS_MIN;
1067 }
Huang, Xiongc24588a2012-04-17 19:32:35 +00001068 txq_ctrl_data =
1069 hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2 ?
1070 L2CB_TXQ_CFGV : L1C_TXQ_CFGV;
Jie Yang43250dd2009-02-18 17:24:15 -08001071
1072 AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
1073}
1074
1075static void atl1c_configure_rx(struct atl1c_adapter *adapter)
1076{
1077 struct atl1c_hw *hw = &adapter->hw;
1078 u32 rxq_ctrl_data;
1079
1080 rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
1081 RXQ_RFD_BURST_NUM_SHIFT;
1082
1083 if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
1084 rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001085
Huang, Xiong9c528212012-04-18 22:01:21 +00001086 /* aspm for gigabit */
1087 if (hw->nic_type != athr_l1d_2 && (hw->device_id & 1) != 0)
1088 rxq_ctrl_data = FIELD_SETX(rxq_ctrl_data, ASPM_THRUPUT_LIMIT,
1089 ASPM_THRUPUT_LIMIT_100M);
Jie Yang43250dd2009-02-18 17:24:15 -08001090
1091 AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
1092}
1093
Jie Yang43250dd2009-02-18 17:24:15 -08001094static void atl1c_configure_dma(struct atl1c_adapter *adapter)
1095{
1096 struct atl1c_hw *hw = &adapter->hw;
1097 u32 dma_ctrl_data;
1098
Huang, Xiong37bfccb2012-04-17 19:32:33 +00001099 dma_ctrl_data = FIELDX(DMA_CTRL_RORDER_MODE, DMA_CTRL_RORDER_MODE_OUT) |
1100 DMA_CTRL_RREQ_PRI_DATA |
1101 FIELDX(DMA_CTRL_RREQ_BLEN, hw->dmar_block) |
1102 FIELDX(DMA_CTRL_WDLY_CNT, DMA_CTRL_WDLY_CNT_DEF) |
1103 FIELDX(DMA_CTRL_RDLY_CNT, DMA_CTRL_RDLY_CNT_DEF);
Jie Yang43250dd2009-02-18 17:24:15 -08001104
1105 AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
1106}
1107
1108/*
1109 * Stop the mac, transmit and receive units
1110 * hw - Struct containing variables accessed by shared code
1111 * return : 0 or idle status (if error)
1112 */
1113static int atl1c_stop_mac(struct atl1c_hw *hw)
1114{
1115 u32 data;
Jie Yang43250dd2009-02-18 17:24:15 -08001116
1117 AT_READ_REG(hw, REG_RXQ_CTRL, &data);
Huang, Xiong027392c2012-04-17 19:32:31 +00001118 data &= ~RXQ_CTRL_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001119 AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1120
1121 AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1122 data &= ~TXQ_CTRL_EN;
Huang, Xiong0cbec612012-04-17 19:32:32 +00001123 AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
Jie Yang43250dd2009-02-18 17:24:15 -08001124
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001125 atl1c_wait_until_idle(hw, IDLE_STATUS_RXQ_BUSY | IDLE_STATUS_TXQ_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001126
1127 AT_READ_REG(hw, REG_MAC_CTRL, &data);
1128 data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
1129 AT_WRITE_REG(hw, REG_MAC_CTRL, data);
1130
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001131 return (int)atl1c_wait_until_idle(hw,
1132 IDLE_STATUS_TXMAC_BUSY | IDLE_STATUS_RXMAC_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001133}
1134
1135static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw)
1136{
1137 u32 data;
1138
1139 AT_READ_REG(hw, REG_RXQ_CTRL, &data);
Jie Yang43250dd2009-02-18 17:24:15 -08001140 data |= RXQ_CTRL_EN;
1141 AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1142}
1143
1144static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw)
1145{
1146 u32 data;
1147
1148 AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1149 data |= TXQ_CTRL_EN;
1150 AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
1151}
1152
1153/*
1154 * Reset the transmit and receive units; mask and clear all interrupts.
1155 * hw - Struct containing variables accessed by shared code
1156 * return : 0 or idle status (if error)
1157 */
1158static int atl1c_reset_mac(struct atl1c_hw *hw)
1159{
1160 struct atl1c_adapter *adapter = (struct atl1c_adapter *)hw->adapter;
1161 struct pci_dev *pdev = adapter->pdev;
Huang, Xiong7737fd92012-04-25 20:27:12 +00001162 u32 ctrl_data = 0;
Jie Yang43250dd2009-02-18 17:24:15 -08001163
1164 AT_WRITE_REG(hw, REG_IMR, 0);
1165 AT_WRITE_REG(hw, REG_ISR, ISR_DIS_INT);
1166
Jie Yang8f574b32010-06-01 00:28:12 -07001167 atl1c_stop_mac(hw);
Jie Yang43250dd2009-02-18 17:24:15 -08001168 /*
1169 * Issue Soft Reset to the MAC. This will reset the chip's
1170 * transmit, receive, DMA. It will not effect
1171 * the current PCI configuration. The global reset bit is self-
1172 * clearing, and should clear within a microsecond.
1173 */
Huang, Xiong7737fd92012-04-25 20:27:12 +00001174 AT_READ_REG(hw, REG_MASTER_CTRL, &ctrl_data);
1175 ctrl_data |= MASTER_CTRL_OOB_DIS;
1176 AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data | MASTER_CTRL_SOFT_RST);
Jie Yang8f574b32010-06-01 00:28:12 -07001177
Jie Yang43250dd2009-02-18 17:24:15 -08001178 AT_WRITE_FLUSH(hw);
1179 msleep(10);
1180 /* Wait at least 10ms for All module to be Idle */
Joe Perchesc930a662009-05-31 20:44:54 +00001181
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001182 if (atl1c_wait_until_idle(hw, IDLE_STATUS_MASK)) {
Jie Yang43250dd2009-02-18 17:24:15 -08001183 dev_err(&pdev->dev,
Joe Perchesc930a662009-05-31 20:44:54 +00001184 "MAC state machine can't be idle since"
Jie Yang43250dd2009-02-18 17:24:15 -08001185 " disabled for 10ms second\n");
1186 return -1;
1187 }
Huang, Xiong7737fd92012-04-25 20:27:12 +00001188 AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data);
1189
1190 /* driver control speed/duplex */
1191 AT_READ_REG(hw, REG_MAC_CTRL, &ctrl_data);
1192 AT_WRITE_REG(hw, REG_MAC_CTRL, ctrl_data | MAC_CTRL_SPEED_MODE_SW);
1193
1194 /* clk switch setting */
1195 AT_READ_REG(hw, REG_SERDES, &ctrl_data);
1196 switch (hw->nic_type) {
1197 case athr_l2c_b:
1198 ctrl_data &= ~(SERDES_PHY_CLK_SLOWDOWN |
1199 SERDES_MAC_CLK_SLOWDOWN);
1200 AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1201 break;
1202 case athr_l2c_b2:
1203 case athr_l1d_2:
1204 ctrl_data |= SERDES_PHY_CLK_SLOWDOWN | SERDES_MAC_CLK_SLOWDOWN;
1205 AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1206 break;
1207 default:
1208 break;
1209 }
Huang, Xiongebe22ed2012-04-18 22:01:29 +00001210
Jie Yang43250dd2009-02-18 17:24:15 -08001211 return 0;
1212}
1213
1214static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
1215{
Huang, Xiong024e1e42012-04-18 22:01:28 +00001216 u16 ctrl_flags = hw->ctrl_flags;
Jie Yang43250dd2009-02-18 17:24:15 -08001217
Huang, Xiong024e1e42012-04-18 22:01:28 +00001218 hw->ctrl_flags &= ~(ATL1C_ASPM_L0S_SUPPORT | ATL1C_ASPM_L1_SUPPORT);
1219 atl1c_set_aspm(hw, SPEED_0);
1220 hw->ctrl_flags = ctrl_flags;
Jie Yang43250dd2009-02-18 17:24:15 -08001221}
1222
1223/*
1224 * Set ASPM state.
1225 * Enable/disable L0s/L1 depend on link state.
1226 */
Huang, Xiong024e1e42012-04-18 22:01:28 +00001227static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed)
Jie Yang43250dd2009-02-18 17:24:15 -08001228{
1229 u32 pm_ctrl_data;
Huang, Xiong024e1e42012-04-18 22:01:28 +00001230 u32 link_l1_timer;
Jie Yang43250dd2009-02-18 17:24:15 -08001231
1232 AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
Huang, Xiong024e1e42012-04-18 22:01:28 +00001233 pm_ctrl_data &= ~(PM_CTRL_ASPM_L1_EN |
1234 PM_CTRL_ASPM_L0S_EN |
1235 PM_CTRL_MAC_ASPM_CHK);
1236 /* L1 timer */
1237 if (hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1238 pm_ctrl_data &= ~PMCTRL_TXL1_AFTER_L0S;
1239 link_l1_timer =
1240 link_speed == SPEED_1000 || link_speed == SPEED_100 ?
1241 L1D_PMCTRL_L1_ENTRY_TM_16US : 1;
1242 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1243 L1D_PMCTRL_L1_ENTRY_TM, link_l1_timer);
Jie Yang43250dd2009-02-18 17:24:15 -08001244 } else {
Huang, Xiong024e1e42012-04-18 22:01:28 +00001245 link_l1_timer = hw->nic_type == athr_l2c_b ?
1246 L2CB1_PM_CTRL_L1_ENTRY_TM : L1C_PM_CTRL_L1_ENTRY_TM;
1247 if (link_speed != SPEED_1000 && link_speed != SPEED_100)
1248 link_l1_timer = 1;
1249 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1250 PM_CTRL_L1_ENTRY_TIMER, link_l1_timer);
1251 }
Jie Yang43250dd2009-02-18 17:24:15 -08001252
Huang, Xiong024e1e42012-04-18 22:01:28 +00001253 /* L0S/L1 enable */
1254 if (hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT)
1255 pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN | PM_CTRL_MAC_ASPM_CHK;
1256 if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
1257 pm_ctrl_data |= PM_CTRL_ASPM_L1_EN | PM_CTRL_MAC_ASPM_CHK;
1258
1259 /* l2cb & l1d & l2cb2 & l1d2 */
1260 if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
1261 hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1262 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1263 PM_CTRL_PM_REQ_TIMER, PM_CTRL_PM_REQ_TO_DEF);
1264 pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER |
1265 PM_CTRL_SERDES_PD_EX_L1 |
1266 PM_CTRL_CLK_SWH_L1;
1267 pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1268 PM_CTRL_SERDES_PLL_L1_EN |
1269 PM_CTRL_SERDES_BUFS_RX_L1_EN |
1270 PM_CTRL_SA_DLY_EN |
1271 PM_CTRL_HOTRST);
1272 /* disable l0s if link down or l2cb */
1273 if (link_speed == SPEED_0 || hw->nic_type == athr_l2c_b)
1274 pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1275 } else { /* l1c */
1276 pm_ctrl_data =
1277 FIELD_SETX(pm_ctrl_data, PM_CTRL_L1_ENTRY_TIMER, 0);
1278 if (link_speed != SPEED_0) {
1279 pm_ctrl_data |= PM_CTRL_SERDES_L1_EN |
1280 PM_CTRL_SERDES_PLL_L1_EN |
1281 PM_CTRL_SERDES_BUFS_RX_L1_EN;
1282 pm_ctrl_data &= ~(PM_CTRL_SERDES_PD_EX_L1 |
1283 PM_CTRL_CLK_SWH_L1 |
1284 PM_CTRL_ASPM_L0S_EN |
1285 PM_CTRL_ASPM_L1_EN);
1286 } else { /* link down */
1287 pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
1288 pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1289 PM_CTRL_SERDES_PLL_L1_EN |
1290 PM_CTRL_SERDES_BUFS_RX_L1_EN |
1291 PM_CTRL_ASPM_L0S_EN);
1292 }
Jie Yang43250dd2009-02-18 17:24:15 -08001293 }
Jie Yang43250dd2009-02-18 17:24:15 -08001294 AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
Jie Yang8f574b32010-06-01 00:28:12 -07001295
1296 return;
Jie Yang43250dd2009-02-18 17:24:15 -08001297}
1298
1299static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter)
1300{
1301 struct atl1c_hw *hw = &adapter->hw;
1302 struct net_device *netdev = adapter->netdev;
1303 u32 mac_ctrl_data;
1304
1305 mac_ctrl_data = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
1306 mac_ctrl_data |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
1307
1308 if (adapter->link_duplex == FULL_DUPLEX) {
1309 hw->mac_duplex = true;
1310 mac_ctrl_data |= MAC_CTRL_DUPLX;
1311 }
1312
1313 if (adapter->link_speed == SPEED_1000)
1314 hw->mac_speed = atl1c_mac_speed_1000;
1315 else
1316 hw->mac_speed = atl1c_mac_speed_10_100;
1317
1318 mac_ctrl_data |= (hw->mac_speed & MAC_CTRL_SPEED_MASK) <<
1319 MAC_CTRL_SPEED_SHIFT;
1320
1321 mac_ctrl_data |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
1322 mac_ctrl_data |= ((hw->preamble_len & MAC_CTRL_PRMLEN_MASK) <<
1323 MAC_CTRL_PRMLEN_SHIFT);
1324
Jiri Pirko46facce2011-07-20 04:54:12 +00001325 __atl1c_vlan_mode(netdev->features, &mac_ctrl_data);
Jie Yang43250dd2009-02-18 17:24:15 -08001326
1327 mac_ctrl_data |= MAC_CTRL_BC_EN;
1328 if (netdev->flags & IFF_PROMISC)
1329 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
1330 if (netdev->flags & IFF_ALLMULTI)
1331 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
1332
1333 mac_ctrl_data |= MAC_CTRL_SINGLE_PAUSE_EN;
Jie Yang8f574b32010-06-01 00:28:12 -07001334 if (hw->nic_type == athr_l1d || hw->nic_type == athr_l2c_b2 ||
1335 hw->nic_type == athr_l1d_2) {
Luis R. Rodriguez496c1852010-02-16 15:16:45 -08001336 mac_ctrl_data |= MAC_CTRL_SPEED_MODE_SW;
1337 mac_ctrl_data |= MAC_CTRL_HASH_ALG_CRC32;
1338 }
Jie Yang43250dd2009-02-18 17:24:15 -08001339 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
1340}
1341
1342/*
1343 * atl1c_configure - Configure Transmit&Receive Unit after Reset
1344 * @adapter: board private structure
1345 *
1346 * Configure the Tx /Rx unit of the MAC after a reset.
1347 */
1348static int atl1c_configure(struct atl1c_adapter *adapter)
1349{
1350 struct atl1c_hw *hw = &adapter->hw;
1351 u32 master_ctrl_data = 0;
1352 u32 intr_modrt_data;
Jie Yang8f574b32010-06-01 00:28:12 -07001353 u32 data;
Jie Yang43250dd2009-02-18 17:24:15 -08001354
Huang, Xiongebe22ed2012-04-18 22:01:29 +00001355 AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
1356 master_ctrl_data &= ~(MASTER_CTRL_TX_ITIMER_EN |
1357 MASTER_CTRL_RX_ITIMER_EN |
1358 MASTER_CTRL_INT_RDCLR);
Jie Yang43250dd2009-02-18 17:24:15 -08001359 /* clear interrupt status */
1360 AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
1361 /* Clear any WOL status */
1362 AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
1363 /* set Interrupt Clear Timer
1364 * HW will enable self to assert interrupt event to system after
1365 * waiting x-time for software to notify it accept interrupt.
1366 */
Jie Yang8f574b32010-06-01 00:28:12 -07001367
1368 data = CLK_GATING_EN_ALL;
1369 if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
1370 if (hw->nic_type == athr_l2c_b)
1371 data &= ~CLK_GATING_RXMAC_EN;
1372 } else
1373 data = 0;
1374 AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
1375
Jie Yang43250dd2009-02-18 17:24:15 -08001376 AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
1377 hw->ict & INT_RETRIG_TIMER_MASK);
1378
1379 atl1c_configure_des_ring(adapter);
1380
1381 if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
1382 intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
1383 IRQ_MODRT_TX_TIMER_SHIFT;
1384 intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
1385 IRQ_MODRT_RX_TIMER_SHIFT;
1386 AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
1387 master_ctrl_data |=
1388 MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
1389 }
1390
1391 if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
1392 master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
1393
Jie Yang8f574b32010-06-01 00:28:12 -07001394 master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001395 AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
1396
Huang, Xiong8d5c6832012-04-17 19:32:29 +00001397 AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
1398 hw->smb_timer & SMB_STAT_TIMER_MASK);
Jie Yang43250dd2009-02-18 17:24:15 -08001399
Jie Yang43250dd2009-02-18 17:24:15 -08001400 /* set MTU */
1401 AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
1402 VLAN_HLEN + ETH_FCS_LEN);
Jie Yang43250dd2009-02-18 17:24:15 -08001403
1404 atl1c_configure_tx(adapter);
1405 atl1c_configure_rx(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001406 atl1c_configure_dma(adapter);
1407
1408 return 0;
1409}
1410
1411static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
1412{
1413 u16 hw_reg_addr = 0;
1414 unsigned long *stats_item = NULL;
1415 u32 data;
1416
1417 /* update rx status */
1418 hw_reg_addr = REG_MAC_RX_STATUS_BIN;
1419 stats_item = &adapter->hw_stats.rx_ok;
1420 while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
1421 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1422 *stats_item += data;
1423 stats_item++;
1424 hw_reg_addr += 4;
1425 }
1426/* update tx status */
1427 hw_reg_addr = REG_MAC_TX_STATUS_BIN;
1428 stats_item = &adapter->hw_stats.tx_ok;
1429 while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
1430 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1431 *stats_item += data;
1432 stats_item++;
1433 hw_reg_addr += 4;
1434 }
1435}
1436
1437/*
1438 * atl1c_get_stats - Get System Network Statistics
1439 * @netdev: network interface device structure
1440 *
1441 * Returns the address of the device statistics structure.
1442 * The statistics are actually updated from the timer callback.
1443 */
1444static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
1445{
1446 struct atl1c_adapter *adapter = netdev_priv(netdev);
1447 struct atl1c_hw_stats *hw_stats = &adapter->hw_stats;
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001448 struct net_device_stats *net_stats = &netdev->stats;
Jie Yang43250dd2009-02-18 17:24:15 -08001449
1450 atl1c_update_hw_stats(adapter);
1451 net_stats->rx_packets = hw_stats->rx_ok;
1452 net_stats->tx_packets = hw_stats->tx_ok;
1453 net_stats->rx_bytes = hw_stats->rx_byte_cnt;
1454 net_stats->tx_bytes = hw_stats->tx_byte_cnt;
1455 net_stats->multicast = hw_stats->rx_mcast;
1456 net_stats->collisions = hw_stats->tx_1_col +
1457 hw_stats->tx_2_col * 2 +
1458 hw_stats->tx_late_col + hw_stats->tx_abort_col;
1459 net_stats->rx_errors = hw_stats->rx_frag + hw_stats->rx_fcs_err +
1460 hw_stats->rx_len_err + hw_stats->rx_sz_ov +
1461 hw_stats->rx_rrd_ov + hw_stats->rx_align_err;
1462 net_stats->rx_fifo_errors = hw_stats->rx_rxf_ov;
1463 net_stats->rx_length_errors = hw_stats->rx_len_err;
1464 net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
1465 net_stats->rx_frame_errors = hw_stats->rx_align_err;
1466 net_stats->rx_over_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1467
1468 net_stats->rx_missed_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
1469
1470 net_stats->tx_errors = hw_stats->tx_late_col + hw_stats->tx_abort_col +
1471 hw_stats->tx_underrun + hw_stats->tx_trunc;
1472 net_stats->tx_fifo_errors = hw_stats->tx_underrun;
1473 net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1474 net_stats->tx_window_errors = hw_stats->tx_late_col;
1475
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001476 return net_stats;
Jie Yang43250dd2009-02-18 17:24:15 -08001477}
1478
1479static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
1480{
1481 u16 phy_data;
1482
1483 spin_lock(&adapter->mdio_lock);
1484 atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
1485 spin_unlock(&adapter->mdio_lock);
1486}
1487
1488static bool atl1c_clean_tx_irq(struct atl1c_adapter *adapter,
1489 enum atl1c_trans_queue type)
1490{
1491 struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1492 &adapter->tpd_ring[type];
1493 struct atl1c_buffer *buffer_info;
Jie Yangc6060be2009-11-06 00:32:05 -08001494 struct pci_dev *pdev = adapter->pdev;
Jie Yang43250dd2009-02-18 17:24:15 -08001495 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1496 u16 hw_next_to_clean;
Huang, Xiong0af48332012-04-17 19:32:30 +00001497 u16 reg;
Jie Yang43250dd2009-02-18 17:24:15 -08001498
Huang, Xiong0af48332012-04-17 19:32:30 +00001499 reg = type == atl1c_trans_high ? REG_TPD_PRI1_CIDX : REG_TPD_PRI0_CIDX;
Jie Yang43250dd2009-02-18 17:24:15 -08001500
Huang, Xiong0af48332012-04-17 19:32:30 +00001501 AT_READ_REGW(&adapter->hw, reg, &hw_next_to_clean);
Jie Yang43250dd2009-02-18 17:24:15 -08001502
1503 while (next_to_clean != hw_next_to_clean) {
1504 buffer_info = &tpd_ring->buffer_info[next_to_clean];
Jie Yangc6060be2009-11-06 00:32:05 -08001505 atl1c_clean_buffer(pdev, buffer_info, 1);
Jie Yang43250dd2009-02-18 17:24:15 -08001506 if (++next_to_clean == tpd_ring->count)
1507 next_to_clean = 0;
1508 atomic_set(&tpd_ring->next_to_clean, next_to_clean);
1509 }
1510
1511 if (netif_queue_stopped(adapter->netdev) &&
1512 netif_carrier_ok(adapter->netdev)) {
1513 netif_wake_queue(adapter->netdev);
1514 }
1515
1516 return true;
1517}
1518
1519/*
1520 * atl1c_intr - Interrupt Handler
1521 * @irq: interrupt number
1522 * @data: pointer to a network interface device structure
1523 * @pt_regs: CPU registers structure
1524 */
1525static irqreturn_t atl1c_intr(int irq, void *data)
1526{
1527 struct net_device *netdev = data;
1528 struct atl1c_adapter *adapter = netdev_priv(netdev);
1529 struct pci_dev *pdev = adapter->pdev;
1530 struct atl1c_hw *hw = &adapter->hw;
1531 int max_ints = AT_MAX_INT_WORK;
1532 int handled = IRQ_NONE;
1533 u32 status;
1534 u32 reg_data;
1535
1536 do {
1537 AT_READ_REG(hw, REG_ISR, &reg_data);
1538 status = reg_data & hw->intr_mask;
1539
1540 if (status == 0 || (status & ISR_DIS_INT) != 0) {
1541 if (max_ints != AT_MAX_INT_WORK)
1542 handled = IRQ_HANDLED;
1543 break;
1544 }
1545 /* link event */
1546 if (status & ISR_GPHY)
1547 atl1c_clear_phy_int(adapter);
1548 /* Ack ISR */
1549 AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
1550 if (status & ISR_RX_PKT) {
1551 if (likely(napi_schedule_prep(&adapter->napi))) {
1552 hw->intr_mask &= ~ISR_RX_PKT;
1553 AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
1554 __napi_schedule(&adapter->napi);
1555 }
1556 }
1557 if (status & ISR_TX_PKT)
1558 atl1c_clean_tx_irq(adapter, atl1c_trans_normal);
1559
1560 handled = IRQ_HANDLED;
1561 /* check if PCIE PHY Link down */
1562 if (status & ISR_ERROR) {
1563 if (netif_msg_hw(adapter))
1564 dev_err(&pdev->dev,
1565 "atl1c hardware error (status = 0x%x)\n",
1566 status & ISR_ERROR);
1567 /* reset MAC */
Dan Carpenter78315452012-03-07 00:02:04 +00001568 set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +00001569 schedule_work(&adapter->common_task);
Jie Yang8f574b32010-06-01 00:28:12 -07001570 return IRQ_HANDLED;
Jie Yang43250dd2009-02-18 17:24:15 -08001571 }
1572
1573 if (status & ISR_OVER)
1574 if (netif_msg_intr(adapter))
1575 dev_warn(&pdev->dev,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001576 "TX/RX overflow (status = 0x%x)\n",
Jie Yang43250dd2009-02-18 17:24:15 -08001577 status & ISR_OVER);
1578
1579 /* link event */
1580 if (status & (ISR_GPHY | ISR_MANUAL)) {
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001581 netdev->stats.tx_carrier_errors++;
Jie Yang43250dd2009-02-18 17:24:15 -08001582 atl1c_link_chg_event(adapter);
1583 break;
1584 }
1585
1586 } while (--max_ints > 0);
1587 /* re-enable Interrupt*/
1588 AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
1589 return handled;
1590}
1591
1592static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
1593 struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
1594{
1595 /*
1596 * The pid field in RRS in not correct sometimes, so we
1597 * cannot figure out if the packet is fragmented or not,
1598 * so we tell the KERNEL CHECKSUM_NONE
1599 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001600 skb_checksum_none_assert(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08001601}
1602
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001603static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08001604{
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001605 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001606 struct pci_dev *pdev = adapter->pdev;
1607 struct atl1c_buffer *buffer_info, *next_info;
1608 struct sk_buff *skb;
1609 void *vir_addr = NULL;
1610 u16 num_alloc = 0;
1611 u16 rfd_next_to_use, next_next;
1612 struct atl1c_rx_free_desc *rfd_desc;
1613
1614 next_next = rfd_next_to_use = rfd_ring->next_to_use;
1615 if (++next_next == rfd_ring->count)
1616 next_next = 0;
1617 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1618 next_info = &rfd_ring->buffer_info[next_next];
1619
Jie Yangc6060be2009-11-06 00:32:05 -08001620 while (next_info->flags & ATL1C_BUFFER_FREE) {
Jie Yang43250dd2009-02-18 17:24:15 -08001621 rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
1622
Pradeep A Dalvi1d266432012-02-05 02:49:09 +00001623 skb = netdev_alloc_skb(adapter->netdev, adapter->rx_buffer_len);
Jie Yang43250dd2009-02-18 17:24:15 -08001624 if (unlikely(!skb)) {
1625 if (netif_msg_rx_err(adapter))
1626 dev_warn(&pdev->dev, "alloc rx buffer failed\n");
1627 break;
1628 }
1629
1630 /*
1631 * Make buffer alignment 2 beyond a 16 byte boundary
1632 * this will result in a 16 byte aligned IP header after
1633 * the 14 byte MAC header is removed
1634 */
1635 vir_addr = skb->data;
Jie Yangc6060be2009-11-06 00:32:05 -08001636 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001637 buffer_info->skb = skb;
1638 buffer_info->length = adapter->rx_buffer_len;
1639 buffer_info->dma = pci_map_single(pdev, vir_addr,
1640 buffer_info->length,
1641 PCI_DMA_FROMDEVICE);
Jie Yang4b45e342009-12-06 22:56:59 +00001642 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
1643 ATL1C_PCIMAP_FROMDEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08001644 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1645 rfd_next_to_use = next_next;
1646 if (++next_next == rfd_ring->count)
1647 next_next = 0;
1648 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1649 next_info = &rfd_ring->buffer_info[next_next];
1650 num_alloc++;
1651 }
1652
1653 if (num_alloc) {
1654 /* TODO: update mailbox here */
1655 wmb();
1656 rfd_ring->next_to_use = rfd_next_to_use;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001657 AT_WRITE_REG(&adapter->hw, REG_MB_RFD0_PROD_IDX,
Jie Yang43250dd2009-02-18 17:24:15 -08001658 rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
1659 }
1660
1661 return num_alloc;
1662}
1663
1664static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring,
1665 struct atl1c_recv_ret_status *rrs, u16 num)
1666{
1667 u16 i;
1668 /* the relationship between rrd and rfd is one map one */
1669 for (i = 0; i < num; i++, rrs = ATL1C_RRD_DESC(rrd_ring,
1670 rrd_ring->next_to_clean)) {
1671 rrs->word3 &= ~RRS_RXD_UPDATED;
1672 if (++rrd_ring->next_to_clean == rrd_ring->count)
1673 rrd_ring->next_to_clean = 0;
1674 }
1675}
1676
1677static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring,
1678 struct atl1c_recv_ret_status *rrs, u16 num)
1679{
1680 u16 i;
1681 u16 rfd_index;
1682 struct atl1c_buffer *buffer_info = rfd_ring->buffer_info;
1683
1684 rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1685 RRS_RX_RFD_INDEX_MASK;
1686 for (i = 0; i < num; i++) {
1687 buffer_info[rfd_index].skb = NULL;
Jie Yangc6060be2009-11-06 00:32:05 -08001688 ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index],
1689 ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -08001690 if (++rfd_index == rfd_ring->count)
1691 rfd_index = 0;
1692 }
1693 rfd_ring->next_to_clean = rfd_index;
1694}
1695
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001696static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
Jie Yang43250dd2009-02-18 17:24:15 -08001697 int *work_done, int work_to_do)
1698{
1699 u16 rfd_num, rfd_index;
1700 u16 count = 0;
1701 u16 length;
1702 struct pci_dev *pdev = adapter->pdev;
1703 struct net_device *netdev = adapter->netdev;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001704 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1705 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001706 struct sk_buff *skb;
1707 struct atl1c_recv_ret_status *rrs;
1708 struct atl1c_buffer *buffer_info;
1709
1710 while (1) {
1711 if (*work_done >= work_to_do)
1712 break;
1713 rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean);
1714 if (likely(RRS_RXD_IS_VALID(rrs->word3))) {
1715 rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) &
1716 RRS_RX_RFD_CNT_MASK;
roel kluin37b76c62009-07-12 12:57:38 +00001717 if (unlikely(rfd_num != 1))
Jie Yang43250dd2009-02-18 17:24:15 -08001718 /* TODO support mul rfd*/
1719 if (netif_msg_rx_err(adapter))
1720 dev_warn(&pdev->dev,
1721 "Multi rfd not support yet!\n");
1722 goto rrs_checked;
1723 } else {
1724 break;
1725 }
1726rrs_checked:
1727 atl1c_clean_rrd(rrd_ring, rrs, rfd_num);
1728 if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) {
1729 atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1730 if (netif_msg_rx_err(adapter))
1731 dev_warn(&pdev->dev,
1732 "wrong packet! rrs word3 is %x\n",
1733 rrs->word3);
1734 continue;
1735 }
1736
1737 length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) &
1738 RRS_PKT_SIZE_MASK);
1739 /* Good Receive */
1740 if (likely(rfd_num == 1)) {
1741 rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1742 RRS_RX_RFD_INDEX_MASK;
1743 buffer_info = &rfd_ring->buffer_info[rfd_index];
1744 pci_unmap_single(pdev, buffer_info->dma,
1745 buffer_info->length, PCI_DMA_FROMDEVICE);
1746 skb = buffer_info->skb;
1747 } else {
1748 /* TODO */
1749 if (netif_msg_rx_err(adapter))
1750 dev_warn(&pdev->dev,
1751 "Multi rfd not support yet!\n");
1752 break;
1753 }
1754 atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1755 skb_put(skb, length - ETH_FCS_LEN);
1756 skb->protocol = eth_type_trans(skb, netdev);
Jie Yang43250dd2009-02-18 17:24:15 -08001757 atl1c_rx_checksum(adapter, skb, rrs);
Jiri Pirko46facce2011-07-20 04:54:12 +00001758 if (rrs->word3 & RRS_VLAN_INS) {
Jie Yang43250dd2009-02-18 17:24:15 -08001759 u16 vlan;
1760
1761 AT_TAG_TO_VLAN(rrs->vlan_tag, vlan);
1762 vlan = le16_to_cpu(vlan);
Jiri Pirko46facce2011-07-20 04:54:12 +00001763 __vlan_hwaccel_put_tag(skb, vlan);
1764 }
1765 netif_receive_skb(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08001766
Jie Yang43250dd2009-02-18 17:24:15 -08001767 (*work_done)++;
1768 count++;
1769 }
1770 if (count)
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001771 atl1c_alloc_rx_buffer(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001772}
1773
1774/*
1775 * atl1c_clean - NAPI Rx polling callback
1776 * @adapter: board private structure
1777 */
1778static int atl1c_clean(struct napi_struct *napi, int budget)
1779{
1780 struct atl1c_adapter *adapter =
1781 container_of(napi, struct atl1c_adapter, napi);
1782 int work_done = 0;
1783
1784 /* Keep link state information with original netdev */
1785 if (!netif_carrier_ok(adapter->netdev))
1786 goto quit_polling;
1787 /* just enable one RXQ */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001788 atl1c_clean_rx_irq(adapter, &work_done, budget);
Jie Yang43250dd2009-02-18 17:24:15 -08001789
1790 if (work_done < budget) {
1791quit_polling:
1792 napi_complete(napi);
1793 adapter->hw.intr_mask |= ISR_RX_PKT;
1794 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
1795 }
1796 return work_done;
1797}
1798
1799#ifdef CONFIG_NET_POLL_CONTROLLER
1800
1801/*
1802 * Polling 'interrupt' - used by things like netconsole to send skbs
1803 * without having to re-enable interrupts. It's not called while
1804 * the interrupt routine is executing.
1805 */
1806static void atl1c_netpoll(struct net_device *netdev)
1807{
1808 struct atl1c_adapter *adapter = netdev_priv(netdev);
1809
1810 disable_irq(adapter->pdev->irq);
1811 atl1c_intr(adapter->pdev->irq, netdev);
1812 enable_irq(adapter->pdev->irq);
1813}
1814#endif
1815
1816static inline u16 atl1c_tpd_avail(struct atl1c_adapter *adapter, enum atl1c_trans_queue type)
1817{
1818 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1819 u16 next_to_use = 0;
1820 u16 next_to_clean = 0;
1821
1822 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1823 next_to_use = tpd_ring->next_to_use;
1824
1825 return (u16)(next_to_clean > next_to_use) ?
1826 (next_to_clean - next_to_use - 1) :
1827 (tpd_ring->count + next_to_clean - next_to_use - 1);
1828}
1829
1830/*
1831 * get next usable tpd
1832 * Note: should call atl1c_tdp_avail to make sure
1833 * there is enough tpd to use
1834 */
1835static struct atl1c_tpd_desc *atl1c_get_tpd(struct atl1c_adapter *adapter,
1836 enum atl1c_trans_queue type)
1837{
1838 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1839 struct atl1c_tpd_desc *tpd_desc;
1840 u16 next_to_use = 0;
1841
1842 next_to_use = tpd_ring->next_to_use;
1843 if (++tpd_ring->next_to_use == tpd_ring->count)
1844 tpd_ring->next_to_use = 0;
1845 tpd_desc = ATL1C_TPD_DESC(tpd_ring, next_to_use);
1846 memset(tpd_desc, 0, sizeof(struct atl1c_tpd_desc));
1847 return tpd_desc;
1848}
1849
1850static struct atl1c_buffer *
1851atl1c_get_tx_buffer(struct atl1c_adapter *adapter, struct atl1c_tpd_desc *tpd)
1852{
1853 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
1854
1855 return &tpd_ring->buffer_info[tpd -
1856 (struct atl1c_tpd_desc *)tpd_ring->desc];
1857}
1858
1859/* Calculate the transmit packet descript needed*/
1860static u16 atl1c_cal_tpd_req(const struct sk_buff *skb)
1861{
1862 u16 tpd_req;
1863 u16 proto_hdr_len = 0;
1864
1865 tpd_req = skb_shinfo(skb)->nr_frags + 1;
1866
1867 if (skb_is_gso(skb)) {
1868 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1869 if (proto_hdr_len < skb_headlen(skb))
1870 tpd_req++;
1871 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
1872 tpd_req++;
1873 }
1874 return tpd_req;
1875}
1876
1877static int atl1c_tso_csum(struct atl1c_adapter *adapter,
1878 struct sk_buff *skb,
1879 struct atl1c_tpd_desc **tpd,
1880 enum atl1c_trans_queue type)
1881{
1882 struct pci_dev *pdev = adapter->pdev;
1883 u8 hdr_len;
1884 u32 real_len;
1885 unsigned short offload_type;
1886 int err;
1887
1888 if (skb_is_gso(skb)) {
1889 if (skb_header_cloned(skb)) {
1890 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1891 if (unlikely(err))
1892 return -1;
1893 }
1894 offload_type = skb_shinfo(skb)->gso_type;
1895
1896 if (offload_type & SKB_GSO_TCPV4) {
1897 real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
1898 + ntohs(ip_hdr(skb)->tot_len));
1899
1900 if (real_len < skb->len)
1901 pskb_trim(skb, real_len);
1902
1903 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
1904 if (unlikely(skb->len == hdr_len)) {
1905 /* only xsum need */
1906 if (netif_msg_tx_queued(adapter))
1907 dev_warn(&pdev->dev,
1908 "IPV4 tso with zero data??\n");
1909 goto check_sum;
1910 } else {
1911 ip_hdr(skb)->check = 0;
1912 tcp_hdr(skb)->check = ~csum_tcpudp_magic(
1913 ip_hdr(skb)->saddr,
1914 ip_hdr(skb)->daddr,
1915 0, IPPROTO_TCP, 0);
1916 (*tpd)->word1 |= 1 << TPD_IPV4_PACKET_SHIFT;
1917 }
1918 }
1919
1920 if (offload_type & SKB_GSO_TCPV6) {
1921 struct atl1c_tpd_ext_desc *etpd =
1922 *(struct atl1c_tpd_ext_desc **)(tpd);
1923
1924 memset(etpd, 0, sizeof(struct atl1c_tpd_ext_desc));
1925 *tpd = atl1c_get_tpd(adapter, type);
1926 ipv6_hdr(skb)->payload_len = 0;
1927 /* check payload == 0 byte ? */
1928 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
1929 if (unlikely(skb->len == hdr_len)) {
1930 /* only xsum need */
1931 if (netif_msg_tx_queued(adapter))
1932 dev_warn(&pdev->dev,
1933 "IPV6 tso with zero data??\n");
1934 goto check_sum;
1935 } else
1936 tcp_hdr(skb)->check = ~csum_ipv6_magic(
1937 &ipv6_hdr(skb)->saddr,
1938 &ipv6_hdr(skb)->daddr,
1939 0, IPPROTO_TCP, 0);
1940 etpd->word1 |= 1 << TPD_LSO_EN_SHIFT;
1941 etpd->word1 |= 1 << TPD_LSO_VER_SHIFT;
1942 etpd->pkt_len = cpu_to_le32(skb->len);
1943 (*tpd)->word1 |= 1 << TPD_LSO_VER_SHIFT;
1944 }
1945
1946 (*tpd)->word1 |= 1 << TPD_LSO_EN_SHIFT;
1947 (*tpd)->word1 |= (skb_transport_offset(skb) & TPD_TCPHDR_OFFSET_MASK) <<
1948 TPD_TCPHDR_OFFSET_SHIFT;
1949 (*tpd)->word1 |= (skb_shinfo(skb)->gso_size & TPD_MSS_MASK) <<
1950 TPD_MSS_SHIFT;
1951 return 0;
1952 }
1953
1954check_sum:
1955 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1956 u8 css, cso;
Michał Mirosław0d0b1672010-12-14 15:24:08 +00001957 cso = skb_checksum_start_offset(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08001958
1959 if (unlikely(cso & 0x1)) {
1960 if (netif_msg_tx_err(adapter))
1961 dev_err(&adapter->pdev->dev,
1962 "payload offset should not an event number\n");
1963 return -1;
1964 } else {
1965 css = cso + skb->csum_offset;
1966
1967 (*tpd)->word1 |= ((cso >> 1) & TPD_PLOADOFFSET_MASK) <<
1968 TPD_PLOADOFFSET_SHIFT;
1969 (*tpd)->word1 |= ((css >> 1) & TPD_CCSUM_OFFSET_MASK) <<
1970 TPD_CCSUM_OFFSET_SHIFT;
1971 (*tpd)->word1 |= 1 << TPD_CCSUM_EN_SHIFT;
1972 }
1973 }
1974 return 0;
1975}
1976
1977static void atl1c_tx_map(struct atl1c_adapter *adapter,
1978 struct sk_buff *skb, struct atl1c_tpd_desc *tpd,
1979 enum atl1c_trans_queue type)
1980{
1981 struct atl1c_tpd_desc *use_tpd = NULL;
1982 struct atl1c_buffer *buffer_info = NULL;
1983 u16 buf_len = skb_headlen(skb);
1984 u16 map_len = 0;
1985 u16 mapped_len = 0;
1986 u16 hdr_len = 0;
1987 u16 nr_frags;
1988 u16 f;
1989 int tso;
1990
1991 nr_frags = skb_shinfo(skb)->nr_frags;
1992 tso = (tpd->word1 >> TPD_LSO_EN_SHIFT) & TPD_LSO_EN_MASK;
1993 if (tso) {
1994 /* TSO */
1995 map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1996 use_tpd = tpd;
1997
1998 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
1999 buffer_info->length = map_len;
2000 buffer_info->dma = pci_map_single(adapter->pdev,
2001 skb->data, hdr_len, PCI_DMA_TODEVICE);
Jie Yangc6060be2009-11-06 00:32:05 -08002002 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002003 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2004 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002005 mapped_len += map_len;
2006 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2007 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2008 }
2009
2010 if (mapped_len < buf_len) {
2011 /* mapped_len == 0, means we should use the first tpd,
2012 which is given by caller */
2013 if (mapped_len == 0)
2014 use_tpd = tpd;
2015 else {
2016 use_tpd = atl1c_get_tpd(adapter, type);
2017 memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
Jie Yang43250dd2009-02-18 17:24:15 -08002018 }
2019 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2020 buffer_info->length = buf_len - mapped_len;
2021 buffer_info->dma =
2022 pci_map_single(adapter->pdev, skb->data + mapped_len,
2023 buffer_info->length, PCI_DMA_TODEVICE);
Jie Yangc6060be2009-11-06 00:32:05 -08002024 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002025 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2026 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002027 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2028 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2029 }
2030
2031 for (f = 0; f < nr_frags; f++) {
2032 struct skb_frag_struct *frag;
2033
2034 frag = &skb_shinfo(skb)->frags[f];
2035
2036 use_tpd = atl1c_get_tpd(adapter, type);
2037 memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2038
2039 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002040 buffer_info->length = skb_frag_size(frag);
Ian Campbell8d1bb862011-08-29 23:18:20 +00002041 buffer_info->dma = skb_frag_dma_map(&adapter->pdev->dev,
2042 frag, 0,
2043 buffer_info->length,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002044 DMA_TO_DEVICE);
Jie Yangc6060be2009-11-06 00:32:05 -08002045 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002046 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_PAGE,
2047 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002048 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2049 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2050 }
2051
2052 /* The last tpd */
2053 use_tpd->word1 |= 1 << TPD_EOP_SHIFT;
2054 /* The last buffer info contain the skb address,
2055 so it will be free after unmap */
2056 buffer_info->skb = skb;
2057}
2058
2059static void atl1c_tx_queue(struct atl1c_adapter *adapter, struct sk_buff *skb,
2060 struct atl1c_tpd_desc *tpd, enum atl1c_trans_queue type)
2061{
2062 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
Huang, Xiong0af48332012-04-17 19:32:30 +00002063 u16 reg;
Jie Yang43250dd2009-02-18 17:24:15 -08002064
Huang, Xiong0af48332012-04-17 19:32:30 +00002065 reg = type == atl1c_trans_high ? REG_TPD_PRI1_PIDX : REG_TPD_PRI0_PIDX;
2066 AT_WRITE_REGW(&adapter->hw, reg, tpd_ring->next_to_use);
Jie Yang43250dd2009-02-18 17:24:15 -08002067}
2068
Stephen Hemminger613573252009-08-31 19:50:58 +00002069static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
2070 struct net_device *netdev)
Jie Yang43250dd2009-02-18 17:24:15 -08002071{
2072 struct atl1c_adapter *adapter = netdev_priv(netdev);
2073 unsigned long flags;
2074 u16 tpd_req = 1;
2075 struct atl1c_tpd_desc *tpd;
2076 enum atl1c_trans_queue type = atl1c_trans_normal;
2077
2078 if (test_bit(__AT_DOWN, &adapter->flags)) {
2079 dev_kfree_skb_any(skb);
2080 return NETDEV_TX_OK;
2081 }
2082
2083 tpd_req = atl1c_cal_tpd_req(skb);
2084 if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
2085 if (netif_msg_pktdata(adapter))
2086 dev_info(&adapter->pdev->dev, "tx locked\n");
2087 return NETDEV_TX_LOCKED;
2088 }
Jie Yang43250dd2009-02-18 17:24:15 -08002089
2090 if (atl1c_tpd_avail(adapter, type) < tpd_req) {
2091 /* no enough descriptor, just stop queue */
2092 netif_stop_queue(netdev);
2093 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2094 return NETDEV_TX_BUSY;
2095 }
2096
2097 tpd = atl1c_get_tpd(adapter, type);
2098
2099 /* do TSO and check sum */
2100 if (atl1c_tso_csum(adapter, skb, &tpd, type) != 0) {
2101 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2102 dev_kfree_skb_any(skb);
2103 return NETDEV_TX_OK;
2104 }
2105
Jesse Grosseab6d182010-10-20 13:56:03 +00002106 if (unlikely(vlan_tx_tag_present(skb))) {
Jie Yang43250dd2009-02-18 17:24:15 -08002107 u16 vlan = vlan_tx_tag_get(skb);
2108 __le16 tag;
2109
2110 vlan = cpu_to_le16(vlan);
2111 AT_VLAN_TO_TAG(vlan, tag);
2112 tpd->word1 |= 1 << TPD_INS_VTAG_SHIFT;
2113 tpd->vlan_tag = tag;
2114 }
2115
2116 if (skb_network_offset(skb) != ETH_HLEN)
2117 tpd->word1 |= 1 << TPD_ETH_TYPE_SHIFT; /* Ethernet frame */
2118
2119 atl1c_tx_map(adapter, skb, tpd, type);
2120 atl1c_tx_queue(adapter, skb, tpd, type);
2121
Jie Yang43250dd2009-02-18 17:24:15 -08002122 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2123 return NETDEV_TX_OK;
2124}
2125
2126static void atl1c_free_irq(struct atl1c_adapter *adapter)
2127{
2128 struct net_device *netdev = adapter->netdev;
2129
2130 free_irq(adapter->pdev->irq, netdev);
2131
2132 if (adapter->have_msi)
2133 pci_disable_msi(adapter->pdev);
2134}
2135
2136static int atl1c_request_irq(struct atl1c_adapter *adapter)
2137{
2138 struct pci_dev *pdev = adapter->pdev;
2139 struct net_device *netdev = adapter->netdev;
2140 int flags = 0;
2141 int err = 0;
2142
2143 adapter->have_msi = true;
2144 err = pci_enable_msi(adapter->pdev);
2145 if (err) {
2146 if (netif_msg_ifup(adapter))
2147 dev_err(&pdev->dev,
2148 "Unable to allocate MSI interrupt Error: %d\n",
2149 err);
2150 adapter->have_msi = false;
Francois Romieu93f7fab2012-03-09 19:22:31 +01002151 }
Jie Yang43250dd2009-02-18 17:24:15 -08002152
2153 if (!adapter->have_msi)
2154 flags |= IRQF_SHARED;
Julia Lawall9aff7e92009-11-18 10:47:03 -08002155 err = request_irq(adapter->pdev->irq, atl1c_intr, flags,
Jie Yang43250dd2009-02-18 17:24:15 -08002156 netdev->name, netdev);
2157 if (err) {
2158 if (netif_msg_ifup(adapter))
2159 dev_err(&pdev->dev,
2160 "Unable to allocate interrupt Error: %d\n",
2161 err);
2162 if (adapter->have_msi)
2163 pci_disable_msi(adapter->pdev);
2164 return err;
2165 }
2166 if (netif_msg_ifup(adapter))
2167 dev_dbg(&pdev->dev, "atl1c_request_irq OK\n");
2168 return err;
2169}
2170
stephen hemminger0fb1e542010-10-21 07:50:49 +00002171static int atl1c_up(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08002172{
2173 struct net_device *netdev = adapter->netdev;
2174 int num;
2175 int err;
Jie Yang43250dd2009-02-18 17:24:15 -08002176
2177 netif_carrier_off(netdev);
2178 atl1c_init_ring_ptrs(adapter);
2179 atl1c_set_multi(netdev);
2180 atl1c_restore_vlan(adapter);
2181
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00002182 num = atl1c_alloc_rx_buffer(adapter);
2183 if (unlikely(num == 0)) {
2184 err = -ENOMEM;
2185 goto err_alloc_rx;
Jie Yang43250dd2009-02-18 17:24:15 -08002186 }
2187
2188 if (atl1c_configure(adapter)) {
2189 err = -EIO;
2190 goto err_up;
2191 }
2192
2193 err = atl1c_request_irq(adapter);
2194 if (unlikely(err))
2195 goto err_up;
2196
2197 clear_bit(__AT_DOWN, &adapter->flags);
2198 napi_enable(&adapter->napi);
2199 atl1c_irq_enable(adapter);
2200 atl1c_check_link_status(adapter);
2201 netif_start_queue(netdev);
2202 return err;
2203
2204err_up:
2205err_alloc_rx:
2206 atl1c_clean_rx_ring(adapter);
2207 return err;
2208}
2209
stephen hemminger0fb1e542010-10-21 07:50:49 +00002210static void atl1c_down(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08002211{
2212 struct net_device *netdev = adapter->netdev;
2213
2214 atl1c_del_timer(adapter);
Jie Yangcb190542009-12-06 23:16:58 +00002215 adapter->work_event = 0; /* clear all event */
Jie Yang43250dd2009-02-18 17:24:15 -08002216 /* signal that we're down so the interrupt handler does not
2217 * reschedule our watchdog timer */
2218 set_bit(__AT_DOWN, &adapter->flags);
2219 netif_carrier_off(netdev);
2220 napi_disable(&adapter->napi);
2221 atl1c_irq_disable(adapter);
2222 atl1c_free_irq(adapter);
Huang, Xiong024e1e42012-04-18 22:01:28 +00002223 /* disable ASPM if device inactive */
2224 atl1c_disable_l0s_l1(&adapter->hw);
Jie Yang43250dd2009-02-18 17:24:15 -08002225 /* reset MAC to disable all RX/TX */
2226 atl1c_reset_mac(&adapter->hw);
2227 msleep(1);
2228
2229 adapter->link_speed = SPEED_0;
2230 adapter->link_duplex = -1;
2231 atl1c_clean_tx_ring(adapter, atl1c_trans_normal);
2232 atl1c_clean_tx_ring(adapter, atl1c_trans_high);
2233 atl1c_clean_rx_ring(adapter);
2234}
2235
2236/*
2237 * atl1c_open - Called when a network interface is made active
2238 * @netdev: network interface device structure
2239 *
2240 * Returns 0 on success, negative value on failure
2241 *
2242 * The open entry point is called when a network interface is made
2243 * active by the system (IFF_UP). At this point all resources needed
2244 * for transmit and receive operations are allocated, the interrupt
2245 * handler is registered with the OS, the watchdog timer is started,
2246 * and the stack is notified that the interface is ready.
2247 */
2248static int atl1c_open(struct net_device *netdev)
2249{
2250 struct atl1c_adapter *adapter = netdev_priv(netdev);
2251 int err;
2252
2253 /* disallow open during test */
2254 if (test_bit(__AT_TESTING, &adapter->flags))
2255 return -EBUSY;
2256
2257 /* allocate rx/tx dma buffer & descriptors */
2258 err = atl1c_setup_ring_resources(adapter);
2259 if (unlikely(err))
2260 return err;
2261
2262 err = atl1c_up(adapter);
2263 if (unlikely(err))
2264 goto err_up;
2265
Jie Yang43250dd2009-02-18 17:24:15 -08002266 return 0;
2267
2268err_up:
2269 atl1c_free_irq(adapter);
2270 atl1c_free_ring_resources(adapter);
2271 atl1c_reset_mac(&adapter->hw);
2272 return err;
2273}
2274
2275/*
2276 * atl1c_close - Disables a network interface
2277 * @netdev: network interface device structure
2278 *
2279 * Returns 0, this is not allowed to fail
2280 *
2281 * The close entry point is called when an interface is de-activated
2282 * by the OS. The hardware is still under the drivers control, but
2283 * needs to be disabled. A global MAC reset is issued to stop the
2284 * hardware, and all transmit and receive resources are freed.
2285 */
2286static int atl1c_close(struct net_device *netdev)
2287{
2288 struct atl1c_adapter *adapter = netdev_priv(netdev);
2289
2290 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2291 atl1c_down(adapter);
2292 atl1c_free_ring_resources(adapter);
2293 return 0;
2294}
2295
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002296static int atl1c_suspend(struct device *dev)
Jie Yang43250dd2009-02-18 17:24:15 -08002297{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002298 struct pci_dev *pdev = to_pci_dev(dev);
Jie Yang43250dd2009-02-18 17:24:15 -08002299 struct net_device *netdev = pci_get_drvdata(pdev);
2300 struct atl1c_adapter *adapter = netdev_priv(netdev);
2301 struct atl1c_hw *hw = &adapter->hw;
Jie Yang43250dd2009-02-18 17:24:15 -08002302 u32 wufc = adapter->wol;
Jie Yang43250dd2009-02-18 17:24:15 -08002303
Jie Yang8f574b32010-06-01 00:28:12 -07002304 atl1c_disable_l0s_l1(hw);
Jie Yang43250dd2009-02-18 17:24:15 -08002305 if (netif_running(netdev)) {
2306 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2307 atl1c_down(adapter);
2308 }
2309 netif_device_detach(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -08002310
Jie Yang8f574b32010-06-01 00:28:12 -07002311 if (wufc)
Huang, Xiong319d0132012-04-25 20:40:59 +00002312 if (atl1c_phy_to_ps_link(hw) != 0)
Jie Yang8f574b32010-06-01 00:28:12 -07002313 dev_dbg(&pdev->dev, "phy power saving failed");
2314
Huang, Xiong319d0132012-04-25 20:40:59 +00002315 atl1c_power_saving(hw, wufc);
Huang, Xiongce5b9722012-04-25 20:27:14 +00002316
Jie Yang43250dd2009-02-18 17:24:15 -08002317 return 0;
2318}
2319
David S. Millerd187c1a2011-05-19 18:44:41 -04002320#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002321static int atl1c_resume(struct device *dev)
Jie Yang43250dd2009-02-18 17:24:15 -08002322{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002323 struct pci_dev *pdev = to_pci_dev(dev);
Jie Yang43250dd2009-02-18 17:24:15 -08002324 struct net_device *netdev = pci_get_drvdata(pdev);
2325 struct atl1c_adapter *adapter = netdev_priv(netdev);
2326
Jie Yang43250dd2009-02-18 17:24:15 -08002327 AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
Huang, Xiongce3ba0c2012-04-25 20:27:11 +00002328 atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
Jie Yang43250dd2009-02-18 17:24:15 -08002329
2330 atl1c_phy_reset(&adapter->hw);
2331 atl1c_reset_mac(&adapter->hw);
Jie Yang8f574b32010-06-01 00:28:12 -07002332 atl1c_phy_init(&adapter->hw);
2333
2334#if 0
2335 AT_READ_REG(&adapter->hw, REG_PM_CTRLSTAT, &pm_data);
2336 pm_data &= ~PM_CTRLSTAT_PME_EN;
2337 AT_WRITE_REG(&adapter->hw, REG_PM_CTRLSTAT, pm_data);
2338#endif
2339
Jie Yang43250dd2009-02-18 17:24:15 -08002340 netif_device_attach(netdev);
2341 if (netif_running(netdev))
2342 atl1c_up(adapter);
2343
2344 return 0;
2345}
David S. Millerd187c1a2011-05-19 18:44:41 -04002346#endif
Jie Yang43250dd2009-02-18 17:24:15 -08002347
2348static void atl1c_shutdown(struct pci_dev *pdev)
2349{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002350 struct net_device *netdev = pci_get_drvdata(pdev);
2351 struct atl1c_adapter *adapter = netdev_priv(netdev);
2352
2353 atl1c_suspend(&pdev->dev);
2354 pci_wake_from_d3(pdev, adapter->wol);
2355 pci_set_power_state(pdev, PCI_D3hot);
Jie Yang43250dd2009-02-18 17:24:15 -08002356}
2357
2358static const struct net_device_ops atl1c_netdev_ops = {
2359 .ndo_open = atl1c_open,
2360 .ndo_stop = atl1c_close,
2361 .ndo_validate_addr = eth_validate_addr,
2362 .ndo_start_xmit = atl1c_xmit_frame,
Jiri Pirko46facce2011-07-20 04:54:12 +00002363 .ndo_set_mac_address = atl1c_set_mac_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002364 .ndo_set_rx_mode = atl1c_set_multi,
Jie Yang43250dd2009-02-18 17:24:15 -08002365 .ndo_change_mtu = atl1c_change_mtu,
Michał Mirosław782d6402011-04-07 07:32:18 +00002366 .ndo_fix_features = atl1c_fix_features,
Jiri Pirko46facce2011-07-20 04:54:12 +00002367 .ndo_set_features = atl1c_set_features,
Jie Yang43250dd2009-02-18 17:24:15 -08002368 .ndo_do_ioctl = atl1c_ioctl,
2369 .ndo_tx_timeout = atl1c_tx_timeout,
2370 .ndo_get_stats = atl1c_get_stats,
Jie Yang43250dd2009-02-18 17:24:15 -08002371#ifdef CONFIG_NET_POLL_CONTROLLER
2372 .ndo_poll_controller = atl1c_netpoll,
2373#endif
2374};
2375
2376static int atl1c_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
2377{
2378 SET_NETDEV_DEV(netdev, &pdev->dev);
2379 pci_set_drvdata(pdev, netdev);
2380
Jie Yang43250dd2009-02-18 17:24:15 -08002381 netdev->netdev_ops = &atl1c_netdev_ops;
2382 netdev->watchdog_timeo = AT_TX_WATCHDOG;
2383 atl1c_set_ethtool_ops(netdev);
2384
2385 /* TODO: add when ready */
Michał Mirosław782d6402011-04-07 07:32:18 +00002386 netdev->hw_features = NETIF_F_SG |
Jie Yang43250dd2009-02-18 17:24:15 -08002387 NETIF_F_HW_CSUM |
Jiri Pirko46facce2011-07-20 04:54:12 +00002388 NETIF_F_HW_VLAN_RX |
Jie Yang43250dd2009-02-18 17:24:15 -08002389 NETIF_F_TSO |
2390 NETIF_F_TSO6;
Michał Mirosław782d6402011-04-07 07:32:18 +00002391 netdev->features = netdev->hw_features |
Jiri Pirko46facce2011-07-20 04:54:12 +00002392 NETIF_F_HW_VLAN_TX;
Jie Yang43250dd2009-02-18 17:24:15 -08002393 return 0;
2394}
2395
2396/*
2397 * atl1c_probe - Device Initialization Routine
2398 * @pdev: PCI device information struct
2399 * @ent: entry in atl1c_pci_tbl
2400 *
2401 * Returns 0 on success, negative on failure
2402 *
2403 * atl1c_probe initializes an adapter identified by a pci_dev structure.
2404 * The OS initialization, configuring of the adapter private structure,
2405 * and a hardware reset occur.
2406 */
2407static int __devinit atl1c_probe(struct pci_dev *pdev,
2408 const struct pci_device_id *ent)
2409{
2410 struct net_device *netdev;
2411 struct atl1c_adapter *adapter;
2412 static int cards_found;
2413
2414 int err = 0;
2415
2416 /* enable device (incl. PCI PM wakeup and hotplug setup) */
2417 err = pci_enable_device_mem(pdev);
2418 if (err) {
2419 dev_err(&pdev->dev, "cannot enable PCI device\n");
2420 return err;
2421 }
2422
2423 /*
2424 * The atl1c chip can DMA to 64-bit addresses, but it uses a single
2425 * shared register for the high 32 bits, so only a single, aligned,
2426 * 4 GB physical address range can be used at a time.
2427 *
2428 * Supporting 64-bit DMA on this hardware is more trouble than it's
2429 * worth. It is far easier to limit to 32-bit DMA than update
2430 * various kernel subsystems to support the mechanics required by a
2431 * fixed-high-32-bit system.
2432 */
Yang Hongyange9304382009-04-13 14:40:14 -07002433 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
2434 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Jie Yang43250dd2009-02-18 17:24:15 -08002435 dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
2436 goto err_dma;
2437 }
2438
2439 err = pci_request_regions(pdev, atl1c_driver_name);
2440 if (err) {
2441 dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2442 goto err_pci_reg;
2443 }
2444
2445 pci_set_master(pdev);
2446
2447 netdev = alloc_etherdev(sizeof(struct atl1c_adapter));
2448 if (netdev == NULL) {
2449 err = -ENOMEM;
Jie Yang43250dd2009-02-18 17:24:15 -08002450 goto err_alloc_etherdev;
2451 }
2452
2453 err = atl1c_init_netdev(netdev, pdev);
2454 if (err) {
2455 dev_err(&pdev->dev, "init netdevice failed\n");
2456 goto err_init_netdev;
2457 }
2458 adapter = netdev_priv(netdev);
2459 adapter->bd_number = cards_found;
2460 adapter->netdev = netdev;
2461 adapter->pdev = pdev;
2462 adapter->hw.adapter = adapter;
2463 adapter->msg_enable = netif_msg_init(-1, atl1c_default_msg);
2464 adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
2465 if (!adapter->hw.hw_addr) {
2466 err = -EIO;
2467 dev_err(&pdev->dev, "cannot map device registers\n");
2468 goto err_ioremap;
2469 }
Jie Yang43250dd2009-02-18 17:24:15 -08002470
2471 /* init mii data */
2472 adapter->mii.dev = netdev;
2473 adapter->mii.mdio_read = atl1c_mdio_read;
2474 adapter->mii.mdio_write = atl1c_mdio_write;
2475 adapter->mii.phy_id_mask = 0x1f;
Huang, Xiong929a5e92012-04-25 20:27:10 +00002476 adapter->mii.reg_num_mask = MDIO_CTRL_REG_MASK;
Jie Yang43250dd2009-02-18 17:24:15 -08002477 netif_napi_add(netdev, &adapter->napi, atl1c_clean, 64);
2478 setup_timer(&adapter->phy_config_timer, atl1c_phy_config,
2479 (unsigned long)adapter);
2480 /* setup the private structure */
2481 err = atl1c_sw_init(adapter);
2482 if (err) {
2483 dev_err(&pdev->dev, "net device private data init failed\n");
2484 goto err_sw_init;
2485 }
Huang, Xiongce3ba0c2012-04-25 20:27:11 +00002486 atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
Jie Yang43250dd2009-02-18 17:24:15 -08002487
2488 /* Init GPHY as early as possible due to power saving issue */
2489 atl1c_phy_reset(&adapter->hw);
2490
2491 err = atl1c_reset_mac(&adapter->hw);
2492 if (err) {
2493 err = -EIO;
2494 goto err_reset;
2495 }
2496
Jie Yang43250dd2009-02-18 17:24:15 -08002497 /* reset the controller to
2498 * put the device in a known good starting state */
2499 err = atl1c_phy_init(&adapter->hw);
2500 if (err) {
2501 err = -EIO;
2502 goto err_reset;
2503 }
Danny Kukawka6a214fd2012-02-17 05:43:30 +00002504 if (atl1c_read_mac_addr(&adapter->hw)) {
2505 /* got a random MAC address, set NET_ADDR_RANDOM to netdev */
2506 netdev->addr_assign_type |= NET_ADDR_RANDOM;
Jie Yang43250dd2009-02-18 17:24:15 -08002507 }
2508 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
2509 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
2510 if (netif_msg_probe(adapter))
hartleys82991172010-01-05 06:54:01 +00002511 dev_dbg(&pdev->dev, "mac address : %pM\n",
2512 adapter->hw.mac_addr);
Jie Yang43250dd2009-02-18 17:24:15 -08002513
2514 atl1c_hw_set_mac_addr(&adapter->hw);
Jie Yangcb190542009-12-06 23:16:58 +00002515 INIT_WORK(&adapter->common_task, atl1c_common_task);
2516 adapter->work_event = 0;
Jie Yang43250dd2009-02-18 17:24:15 -08002517 err = register_netdev(netdev);
2518 if (err) {
2519 dev_err(&pdev->dev, "register netdevice failed\n");
2520 goto err_register;
2521 }
2522
2523 if (netif_msg_probe(adapter))
2524 dev_info(&pdev->dev, "version %s\n", ATL1C_DRV_VERSION);
2525 cards_found++;
2526 return 0;
2527
2528err_reset:
2529err_register:
2530err_sw_init:
Jie Yang43250dd2009-02-18 17:24:15 -08002531 iounmap(adapter->hw.hw_addr);
2532err_init_netdev:
2533err_ioremap:
2534 free_netdev(netdev);
2535err_alloc_etherdev:
2536 pci_release_regions(pdev);
2537err_pci_reg:
2538err_dma:
2539 pci_disable_device(pdev);
2540 return err;
2541}
2542
2543/*
2544 * atl1c_remove - Device Removal Routine
2545 * @pdev: PCI device information struct
2546 *
2547 * atl1c_remove is called by the PCI subsystem to alert the driver
2548 * that it should release a PCI device. The could be caused by a
2549 * Hot-Plug event, or because the driver is going to be removed from
2550 * memory.
2551 */
2552static void __devexit atl1c_remove(struct pci_dev *pdev)
2553{
2554 struct net_device *netdev = pci_get_drvdata(pdev);
2555 struct atl1c_adapter *adapter = netdev_priv(netdev);
2556
2557 unregister_netdev(netdev);
2558 atl1c_phy_disable(&adapter->hw);
2559
2560 iounmap(adapter->hw.hw_addr);
2561
2562 pci_release_regions(pdev);
2563 pci_disable_device(pdev);
2564 free_netdev(netdev);
2565}
2566
2567/*
2568 * atl1c_io_error_detected - called when PCI error is detected
2569 * @pdev: Pointer to PCI device
2570 * @state: The current pci connection state
2571 *
2572 * This function is called after a PCI bus error affecting
2573 * this device has been detected.
2574 */
2575static pci_ers_result_t atl1c_io_error_detected(struct pci_dev *pdev,
2576 pci_channel_state_t state)
2577{
2578 struct net_device *netdev = pci_get_drvdata(pdev);
2579 struct atl1c_adapter *adapter = netdev_priv(netdev);
2580
2581 netif_device_detach(netdev);
2582
Dean Nelson005fb4f2009-07-31 09:13:02 +00002583 if (state == pci_channel_io_perm_failure)
2584 return PCI_ERS_RESULT_DISCONNECT;
2585
Jie Yang43250dd2009-02-18 17:24:15 -08002586 if (netif_running(netdev))
2587 atl1c_down(adapter);
2588
2589 pci_disable_device(pdev);
2590
2591 /* Request a slot slot reset. */
2592 return PCI_ERS_RESULT_NEED_RESET;
2593}
2594
2595/*
2596 * atl1c_io_slot_reset - called after the pci bus has been reset.
2597 * @pdev: Pointer to PCI device
2598 *
2599 * Restart the card from scratch, as if from a cold-boot. Implementation
2600 * resembles the first-half of the e1000_resume routine.
2601 */
2602static pci_ers_result_t atl1c_io_slot_reset(struct pci_dev *pdev)
2603{
2604 struct net_device *netdev = pci_get_drvdata(pdev);
2605 struct atl1c_adapter *adapter = netdev_priv(netdev);
2606
2607 if (pci_enable_device(pdev)) {
2608 if (netif_msg_hw(adapter))
2609 dev_err(&pdev->dev,
2610 "Cannot re-enable PCI device after reset\n");
2611 return PCI_ERS_RESULT_DISCONNECT;
2612 }
2613 pci_set_master(pdev);
2614
2615 pci_enable_wake(pdev, PCI_D3hot, 0);
2616 pci_enable_wake(pdev, PCI_D3cold, 0);
2617
2618 atl1c_reset_mac(&adapter->hw);
2619
2620 return PCI_ERS_RESULT_RECOVERED;
2621}
2622
2623/*
2624 * atl1c_io_resume - called when traffic can start flowing again.
2625 * @pdev: Pointer to PCI device
2626 *
2627 * This callback is called when the error recovery driver tells us that
2628 * its OK to resume normal operation. Implementation resembles the
2629 * second-half of the atl1c_resume routine.
2630 */
2631static void atl1c_io_resume(struct pci_dev *pdev)
2632{
2633 struct net_device *netdev = pci_get_drvdata(pdev);
2634 struct atl1c_adapter *adapter = netdev_priv(netdev);
2635
2636 if (netif_running(netdev)) {
2637 if (atl1c_up(adapter)) {
2638 if (netif_msg_hw(adapter))
2639 dev_err(&pdev->dev,
2640 "Cannot bring device back up after reset\n");
2641 return;
2642 }
2643 }
2644
2645 netif_device_attach(netdev);
2646}
2647
2648static struct pci_error_handlers atl1c_err_handler = {
2649 .error_detected = atl1c_io_error_detected,
2650 .slot_reset = atl1c_io_slot_reset,
2651 .resume = atl1c_io_resume,
2652};
2653
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002654static SIMPLE_DEV_PM_OPS(atl1c_pm_ops, atl1c_suspend, atl1c_resume);
2655
Jie Yang43250dd2009-02-18 17:24:15 -08002656static struct pci_driver atl1c_driver = {
2657 .name = atl1c_driver_name,
2658 .id_table = atl1c_pci_tbl,
2659 .probe = atl1c_probe,
2660 .remove = __devexit_p(atl1c_remove),
Jie Yang43250dd2009-02-18 17:24:15 -08002661 .shutdown = atl1c_shutdown,
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002662 .err_handler = &atl1c_err_handler,
2663 .driver.pm = &atl1c_pm_ops,
Jie Yang43250dd2009-02-18 17:24:15 -08002664};
2665
2666/*
2667 * atl1c_init_module - Driver Registration Routine
2668 *
2669 * atl1c_init_module is the first routine called when the driver is
2670 * loaded. All it does is register with the PCI subsystem.
2671 */
2672static int __init atl1c_init_module(void)
2673{
2674 return pci_register_driver(&atl1c_driver);
2675}
2676
2677/*
2678 * atl1c_exit_module - Driver Exit Cleanup Routine
2679 *
2680 * atl1c_exit_module is called just before the driver is removed
2681 * from memory.
2682 */
2683static void __exit atl1c_exit_module(void)
2684{
2685 pci_unregister_driver(&atl1c_driver);
2686}
2687
2688module_init(atl1c_init_module);
2689module_exit(atl1c_exit_module);