blob: d0084d4d1a9b718773ca2e6ecae238d285392999 [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
Huang, Xiongac574802013-02-11 14:44:40 +000024#define ATL1C_DRV_VERSION "1.0.1.1-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 */
Benoit Taine9baa3c32014-08-08 15:56:03 +020037static const struct pci_device_id 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>");
Masanari Iidafe4e4372014-10-17 00:09:24 +090051MODULE_DESCRIPTION("Qualcomm 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);
Jie Yang43250dd2009-02-18 17:24:15 -080056static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
Huang, Xiong024e1e42012-04-18 22:01:28 +000057static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed);
Huang, Xiong25456e52012-04-25 20:41:00 +000058static void atl1c_start_mac(struct atl1c_adapter *adapter);
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +000059static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
Jie Yang43250dd2009-02-18 17:24:15 -080060 int *work_done, int work_to_do);
stephen hemminger0fb1e542010-10-21 07:50:49 +000061static int atl1c_up(struct atl1c_adapter *adapter);
62static void atl1c_down(struct atl1c_adapter *adapter);
Huang, Xiong5e5c0962012-04-30 15:38:56 +000063static int atl1c_reset_mac(struct atl1c_hw *hw);
64static void atl1c_reset_dma_ring(struct atl1c_adapter *adapter);
65static int atl1c_configure(struct atl1c_adapter *adapter);
66static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter);
Jie Yang43250dd2009-02-18 17:24:15 -080067
Jie Yang43250dd2009-02-18 17:24:15 -080068
69static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
70 NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
Jie Yang8f574b32010-06-01 00:28:12 -070071static void atl1c_pcie_patch(struct atl1c_hw *hw)
72{
Huang, Xiong5cbdcc22012-04-18 22:01:27 +000073 u32 mst_data, data;
74
75 /* pclk sel could switch to 25M */
76 AT_READ_REG(hw, REG_MASTER_CTRL, &mst_data);
77 mst_data &= ~MASTER_CTRL_CLK_SEL_DIS;
78 AT_WRITE_REG(hw, REG_MASTER_CTRL, mst_data);
Jie Yang43250dd2009-02-18 17:24:15 -080079
Huang, Xiongebe22ed2012-04-18 22:01:29 +000080 /* WoL/PCIE related settings */
81 if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
82 AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
83 data |= PCIE_PHYMISC_FORCE_RCV_DET;
84 AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
85 } else { /* new dev set bit5 of MASTER */
86 if (!(mst_data & MASTER_CTRL_WAKEN_25M))
87 AT_WRITE_REG(hw, REG_MASTER_CTRL,
88 mst_data | MASTER_CTRL_WAKEN_25M);
89 }
90 /* aspm/PCIE setting only for l2cb 1.0 */
Jie Yang8f574b32010-06-01 00:28:12 -070091 if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
92 AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
Huang, Xiongebe22ed2012-04-18 22:01:29 +000093 data = FIELD_SETX(data, PCIE_PHYMISC2_CDR_BW,
94 L2CB1_PCIE_PHYMISC2_CDR_BW);
95 data = FIELD_SETX(data, PCIE_PHYMISC2_L0S_TH,
96 L2CB1_PCIE_PHYMISC2_L0S_TH);
Jie Yang8f574b32010-06-01 00:28:12 -070097 AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
Huang, Xiongebe22ed2012-04-18 22:01:29 +000098 /* extend L1 sync timer */
99 AT_READ_REG(hw, REG_LINK_CTRL, &data);
100 data |= LINK_CTRL_EXT_SYNC;
101 AT_WRITE_REG(hw, REG_LINK_CTRL, data);
102 }
103 /* l2cb 1.x & l1d 1.x */
104 if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d) {
105 AT_READ_REG(hw, REG_PM_CTRL, &data);
106 data |= PM_CTRL_L0S_BUFSRX_EN;
107 AT_WRITE_REG(hw, REG_PM_CTRL, data);
108 /* clear vendor msg */
109 AT_READ_REG(hw, REG_DMA_DBG, &data);
110 AT_WRITE_REG(hw, REG_DMA_DBG, data & ~DMA_DBG_VENDOR_MSG);
Jie Yang8f574b32010-06-01 00:28:12 -0700111 }
112}
113
114/* FIXME: no need any more ? */
Jie Yang43250dd2009-02-18 17:24:15 -0800115/*
116 * atl1c_init_pcie - init PCIE module
117 */
118static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
119{
120 u32 data;
121 u32 pci_cmd;
122 struct pci_dev *pdev = hw->adapter->pdev;
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000123 int pos;
Jie Yang43250dd2009-02-18 17:24:15 -0800124
125 AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
126 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
127 pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
128 PCI_COMMAND_IO);
129 AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
130
131 /*
132 * Clear any PowerSaveing Settings
133 */
134 pci_enable_wake(pdev, PCI_D3hot, 0);
135 pci_enable_wake(pdev, PCI_D3cold, 0);
Huang, Xiong87eabe62012-04-30 15:38:51 +0000136 /* wol sts read-clear */
137 AT_READ_REG(hw, REG_WOL_CTRL, &data);
138 AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
Jie Yang43250dd2009-02-18 17:24:15 -0800139
140 /*
141 * Mask some pcie error bits
142 */
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000143 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
Betty Dalla4f636342013-12-19 10:59:09 -0700144 if (pos) {
145 pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &data);
146 data &= ~(PCI_ERR_UNC_DLP | PCI_ERR_UNC_FCP);
147 pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, data);
148 }
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000149 /* clear error status */
Jiang Liueb0ff562012-07-24 17:20:24 +0800150 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA,
Huang, Xiong95f9aea2012-04-18 22:01:25 +0000151 PCI_EXP_DEVSTA_NFED |
152 PCI_EXP_DEVSTA_FED |
153 PCI_EXP_DEVSTA_CED |
154 PCI_EXP_DEVSTA_URD);
Jie Yang43250dd2009-02-18 17:24:15 -0800155
Jie Yang8f574b32010-06-01 00:28:12 -0700156 AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
157 data &= ~LTSSM_ID_EN_WRO;
158 AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
159
160 atl1c_pcie_patch(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800161 if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
162 atl1c_disable_l0s_l1(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800163
Jie Yang8f574b32010-06-01 00:28:12 -0700164 msleep(5);
Jie Yang43250dd2009-02-18 17:24:15 -0800165}
166
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000167/**
Jie Yang43250dd2009-02-18 17:24:15 -0800168 * atl1c_irq_enable - Enable default interrupt generation settings
169 * @adapter: board private structure
170 */
171static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
172{
173 if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
174 AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
175 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
176 AT_WRITE_FLUSH(&adapter->hw);
177 }
178}
179
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000180/**
Jie Yang43250dd2009-02-18 17:24:15 -0800181 * atl1c_irq_disable - Mask off interrupt generation on the NIC
182 * @adapter: board private structure
183 */
184static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
185{
186 atomic_inc(&adapter->irq_sem);
187 AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
Jie Yang8f574b32010-06-01 00:28:12 -0700188 AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
Jie Yang43250dd2009-02-18 17:24:15 -0800189 AT_WRITE_FLUSH(&adapter->hw);
190 synchronize_irq(adapter->pdev->irq);
191}
192
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000193/**
Jie Yang43250dd2009-02-18 17:24:15 -0800194 * atl1c_irq_reset - reset interrupt confiure on the NIC
195 * @adapter: board private structure
196 */
197static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
198{
199 atomic_set(&adapter->irq_sem, 1);
200 atl1c_irq_enable(adapter);
201}
202
203/*
Joe Perchesc930a662009-05-31 20:44:54 +0000204 * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
205 * of the idle status register until the device is actually idle
206 */
Huang, Xiong969a7ee2012-04-18 22:01:22 +0000207static u32 atl1c_wait_until_idle(struct atl1c_hw *hw, u32 modu_ctrl)
Joe Perchesc930a662009-05-31 20:44:54 +0000208{
209 int timeout;
210 u32 data;
211
212 for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
213 AT_READ_REG(hw, REG_IDLE_STATUS, &data);
Huang, Xiong969a7ee2012-04-18 22:01:22 +0000214 if ((data & modu_ctrl) == 0)
Joe Perchesc930a662009-05-31 20:44:54 +0000215 return 0;
216 msleep(1);
217 }
218 return data;
219}
220
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000221/**
Jie Yang43250dd2009-02-18 17:24:15 -0800222 * atl1c_phy_config - Timer Call-back
223 * @data: pointer to netdev cast into an unsigned long
224 */
225static void atl1c_phy_config(unsigned long data)
226{
227 struct atl1c_adapter *adapter = (struct atl1c_adapter *) data;
228 struct atl1c_hw *hw = &adapter->hw;
229 unsigned long flags;
230
231 spin_lock_irqsave(&adapter->mdio_lock, flags);
232 atl1c_restart_autoneg(hw);
233 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
234}
235
236void atl1c_reinit_locked(struct atl1c_adapter *adapter)
237{
Jie Yang43250dd2009-02-18 17:24:15 -0800238 WARN_ON(in_interrupt());
239 atl1c_down(adapter);
240 atl1c_up(adapter);
241 clear_bit(__AT_RESETTING, &adapter->flags);
242}
243
Jie Yang43250dd2009-02-18 17:24:15 -0800244static void atl1c_check_link_status(struct atl1c_adapter *adapter)
245{
246 struct atl1c_hw *hw = &adapter->hw;
247 struct net_device *netdev = adapter->netdev;
248 struct pci_dev *pdev = adapter->pdev;
249 int err;
250 unsigned long flags;
251 u16 speed, duplex, phy_data;
252
253 spin_lock_irqsave(&adapter->mdio_lock, flags);
254 /* MII_BMSR must read twise */
255 atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
256 atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
257 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
258
259 if ((phy_data & BMSR_LSTATUS) == 0) {
260 /* link down */
Jie Yang43250dd2009-02-18 17:24:15 -0800261 netif_carrier_off(netdev);
Huang, Xiong5e5c0962012-04-30 15:38:56 +0000262 hw->hibernate = true;
263 if (atl1c_reset_mac(hw) != 0)
264 if (netif_msg_hw(adapter))
265 dev_warn(&pdev->dev, "reset mac failed\n");
266 atl1c_set_aspm(hw, SPEED_0);
267 atl1c_post_phy_linkchg(hw, SPEED_0);
268 atl1c_reset_dma_ring(adapter);
269 atl1c_configure(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -0800270 } else {
271 /* Link Up */
272 hw->hibernate = false;
273 spin_lock_irqsave(&adapter->mdio_lock, flags);
274 err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
275 spin_unlock_irqrestore(&adapter->mdio_lock, flags);
276 if (unlikely(err))
277 return;
278 /* link result is our setting */
279 if (adapter->link_speed != speed ||
280 adapter->link_duplex != duplex) {
281 adapter->link_speed = speed;
282 adapter->link_duplex = duplex;
Huang, Xiong024e1e42012-04-18 22:01:28 +0000283 atl1c_set_aspm(hw, speed);
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000284 atl1c_post_phy_linkchg(hw, speed);
Huang, Xiong25456e52012-04-25 20:41:00 +0000285 atl1c_start_mac(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -0800286 if (netif_msg_link(adapter))
287 dev_info(&pdev->dev,
288 "%s: %s NIC Link is Up<%d Mbps %s>\n",
289 atl1c_driver_name, netdev->name,
290 adapter->link_speed,
291 adapter->link_duplex == FULL_DUPLEX ?
292 "Full Duplex" : "Half Duplex");
293 }
294 if (!netif_carrier_ok(netdev))
295 netif_carrier_on(netdev);
296 }
297}
298
Jie Yang43250dd2009-02-18 17:24:15 -0800299static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
300{
301 struct net_device *netdev = adapter->netdev;
302 struct pci_dev *pdev = adapter->pdev;
303 u16 phy_data;
304 u16 link_up;
305
306 spin_lock(&adapter->mdio_lock);
307 atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
308 atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
309 spin_unlock(&adapter->mdio_lock);
310 link_up = phy_data & BMSR_LSTATUS;
311 /* notify upper layer link down ASAP */
312 if (!link_up) {
313 if (netif_carrier_ok(netdev)) {
314 /* old link state: Up */
315 netif_carrier_off(netdev);
316 if (netif_msg_link(adapter))
317 dev_info(&pdev->dev,
318 "%s: %s NIC Link is Down\n",
319 atl1c_driver_name, netdev->name);
320 adapter->link_speed = SPEED_0;
321 }
322 }
Jie Yangcb190542009-12-06 23:16:58 +0000323
Tim Gardnercb771832011-04-20 09:00:49 +0000324 set_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +0000325 schedule_work(&adapter->common_task);
Jie Yang43250dd2009-02-18 17:24:15 -0800326}
327
Jie Yangcb190542009-12-06 23:16:58 +0000328static void atl1c_common_task(struct work_struct *work)
329{
330 struct atl1c_adapter *adapter;
331 struct net_device *netdev;
332
333 adapter = container_of(work, struct atl1c_adapter, common_task);
334 netdev = adapter->netdev;
335
Huang, Xiong0aa76ce2012-04-30 15:38:55 +0000336 if (test_bit(__AT_DOWN, &adapter->flags))
337 return;
338
Tim Gardnercb771832011-04-20 09:00:49 +0000339 if (test_and_clear_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event)) {
Jie Yangcb190542009-12-06 23:16:58 +0000340 netif_device_detach(netdev);
341 atl1c_down(adapter);
342 atl1c_up(adapter);
343 netif_device_attach(netdev);
Jie Yangcb190542009-12-06 23:16:58 +0000344 }
345
Tim Gardnercb771832011-04-20 09:00:49 +0000346 if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE,
Huang, Xiong5e5c0962012-04-30 15:38:56 +0000347 &adapter->work_event)) {
348 atl1c_irq_disable(adapter);
Jie Yangcb190542009-12-06 23:16:58 +0000349 atl1c_check_link_status(adapter);
Huang, Xiong5e5c0962012-04-30 15:38:56 +0000350 atl1c_irq_enable(adapter);
351 }
Jie Yangcb190542009-12-06 23:16:58 +0000352}
353
354
Jie Yang43250dd2009-02-18 17:24:15 -0800355static void atl1c_del_timer(struct atl1c_adapter *adapter)
356{
357 del_timer_sync(&adapter->phy_config_timer);
358}
359
Jie Yang43250dd2009-02-18 17:24:15 -0800360
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000361/**
Jie Yang43250dd2009-02-18 17:24:15 -0800362 * atl1c_tx_timeout - Respond to a Tx Hang
363 * @netdev: network interface device structure
364 */
365static void atl1c_tx_timeout(struct net_device *netdev)
366{
367 struct atl1c_adapter *adapter = netdev_priv(netdev);
368
369 /* Do the reset outside of interrupt context */
Tim Gardnercb771832011-04-20 09:00:49 +0000370 set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +0000371 schedule_work(&adapter->common_task);
Jie Yang43250dd2009-02-18 17:24:15 -0800372}
373
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000374/**
Jie Yang43250dd2009-02-18 17:24:15 -0800375 * atl1c_set_multi - Multicast and Promiscuous mode set
376 * @netdev: network interface device structure
377 *
378 * The set_multi entry point is called whenever the multicast address
379 * list or the network interface flags are updated. This routine is
380 * responsible for configuring the hardware for proper multicast,
381 * promiscuous mode, and all-multi behavior.
382 */
383static void atl1c_set_multi(struct net_device *netdev)
384{
385 struct atl1c_adapter *adapter = netdev_priv(netdev);
386 struct atl1c_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000387 struct netdev_hw_addr *ha;
Jie Yang43250dd2009-02-18 17:24:15 -0800388 u32 mac_ctrl_data;
389 u32 hash_value;
390
391 /* Check for Promiscuous and All Multicast modes */
392 AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
393
394 if (netdev->flags & IFF_PROMISC) {
395 mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
396 } else if (netdev->flags & IFF_ALLMULTI) {
397 mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
398 mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
399 } else {
400 mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
401 }
402
403 AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
404
405 /* clear the old settings from the multicast hash table */
406 AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
407 AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
408
409 /* comoute mc addresses' hash value ,and put it into hash table */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000410 netdev_for_each_mc_addr(ha, netdev) {
411 hash_value = atl1c_hash_mc_addr(hw, ha->addr);
Jie Yang43250dd2009-02-18 17:24:15 -0800412 atl1c_hash_set(hw, hash_value);
413 }
414}
415
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000416static void __atl1c_vlan_mode(netdev_features_t features, u32 *mac_ctrl_data)
Jiri Pirko46facce2011-07-20 04:54:12 +0000417{
Patrick McHardyf6469682013-04-19 02:04:27 +0000418 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
Jiri Pirko46facce2011-07-20 04:54:12 +0000419 /* enable VLAN tag insert/strip */
420 *mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
421 } else {
422 /* disable VLAN tag insert/strip */
423 *mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
424 }
425}
426
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000427static void atl1c_vlan_mode(struct net_device *netdev,
428 netdev_features_t features)
Jie Yang43250dd2009-02-18 17:24:15 -0800429{
430 struct atl1c_adapter *adapter = netdev_priv(netdev);
431 struct pci_dev *pdev = adapter->pdev;
432 u32 mac_ctrl_data = 0;
433
434 if (netif_msg_pktdata(adapter))
Jiri Pirko46facce2011-07-20 04:54:12 +0000435 dev_dbg(&pdev->dev, "atl1c_vlan_mode\n");
Jie Yang43250dd2009-02-18 17:24:15 -0800436
437 atl1c_irq_disable(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -0800438 AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
Jiri Pirko46facce2011-07-20 04:54:12 +0000439 __atl1c_vlan_mode(features, &mac_ctrl_data);
Jie Yang43250dd2009-02-18 17:24:15 -0800440 AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
441 atl1c_irq_enable(adapter);
442}
443
444static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
445{
446 struct pci_dev *pdev = adapter->pdev;
447
448 if (netif_msg_pktdata(adapter))
Jiri Pirko46facce2011-07-20 04:54:12 +0000449 dev_dbg(&pdev->dev, "atl1c_restore_vlan\n");
450 atl1c_vlan_mode(adapter->netdev, adapter->netdev->features);
Jie Yang43250dd2009-02-18 17:24:15 -0800451}
Jiri Pirko46facce2011-07-20 04:54:12 +0000452
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000453/**
Jie Yang43250dd2009-02-18 17:24:15 -0800454 * atl1c_set_mac - Change the Ethernet Address of the NIC
455 * @netdev: network interface device structure
456 * @p: pointer to an address structure
457 *
458 * Returns 0 on success, negative on failure
459 */
460static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
461{
462 struct atl1c_adapter *adapter = netdev_priv(netdev);
463 struct sockaddr *addr = p;
464
465 if (!is_valid_ether_addr(addr->sa_data))
466 return -EADDRNOTAVAIL;
467
468 if (netif_running(netdev))
469 return -EBUSY;
470
471 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
472 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
473
Huang, Xiong229e6b62012-04-30 15:38:53 +0000474 atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.mac_addr);
Jie Yang43250dd2009-02-18 17:24:15 -0800475
476 return 0;
477}
478
479static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
480 struct net_device *dev)
481{
Eric Dumazet7b701762013-07-29 10:24:04 -0700482 unsigned int head_size;
Jie Yang43250dd2009-02-18 17:24:15 -0800483 int mtu = dev->mtu;
484
485 adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
486 roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
Eric Dumazet7b701762013-07-29 10:24:04 -0700487
488 head_size = SKB_DATA_ALIGN(adapter->rx_buffer_len + NET_SKB_PAD) +
489 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
490 adapter->rx_frag_size = roundup_pow_of_two(head_size);
Jie Yang43250dd2009-02-18 17:24:15 -0800491}
Michał Mirosław782d6402011-04-07 07:32:18 +0000492
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000493static netdev_features_t atl1c_fix_features(struct net_device *netdev,
494 netdev_features_t features)
Michał Mirosław782d6402011-04-07 07:32:18 +0000495{
Jiri Pirko46facce2011-07-20 04:54:12 +0000496 /*
497 * Since there is no support for separate rx/tx vlan accel
498 * enable/disable make sure tx flag is always in same state as rx.
499 */
Patrick McHardyf6469682013-04-19 02:04:27 +0000500 if (features & NETIF_F_HW_VLAN_CTAG_RX)
501 features |= NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko46facce2011-07-20 04:54:12 +0000502 else
Patrick McHardyf6469682013-04-19 02:04:27 +0000503 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko46facce2011-07-20 04:54:12 +0000504
Michał Mirosław782d6402011-04-07 07:32:18 +0000505 if (netdev->mtu > MAX_TSO_FRAME_SIZE)
506 features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
507
508 return features;
509}
510
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000511static int atl1c_set_features(struct net_device *netdev,
512 netdev_features_t features)
Jiri Pirko46facce2011-07-20 04:54:12 +0000513{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000514 netdev_features_t changed = netdev->features ^ features;
Jiri Pirko46facce2011-07-20 04:54:12 +0000515
Patrick McHardyf6469682013-04-19 02:04:27 +0000516 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
Jiri Pirko46facce2011-07-20 04:54:12 +0000517 atl1c_vlan_mode(netdev, features);
518
519 return 0;
520}
521
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000522/**
Jie Yang43250dd2009-02-18 17:24:15 -0800523 * atl1c_change_mtu - Change the Maximum Transfer Unit
524 * @netdev: network interface device structure
525 * @new_mtu: new value for maximum frame size
526 *
527 * Returns 0 on success, negative on failure
528 */
529static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
530{
531 struct atl1c_adapter *adapter = netdev_priv(netdev);
Huang, Xiongc08b9b22012-04-18 22:01:23 +0000532 struct atl1c_hw *hw = &adapter->hw;
Jie Yang43250dd2009-02-18 17:24:15 -0800533 int old_mtu = netdev->mtu;
534 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
535
Huang, Xiongc08b9b22012-04-18 22:01:23 +0000536 /* Fast Ethernet controller doesn't support jumbo packet */
537 if (((hw->nic_type == athr_l2c ||
538 hw->nic_type == athr_l2c_b ||
539 hw->nic_type == athr_l2c_b2) && new_mtu > ETH_DATA_LEN) ||
540 max_frame < ETH_ZLEN + ETH_FCS_LEN ||
541 max_frame > MAX_JUMBO_FRAME_SIZE) {
Jie Yang43250dd2009-02-18 17:24:15 -0800542 if (netif_msg_link(adapter))
543 dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
544 return -EINVAL;
545 }
546 /* set MTU */
547 if (old_mtu != new_mtu && netif_running(netdev)) {
548 while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
549 msleep(1);
550 netdev->mtu = new_mtu;
551 adapter->hw.max_frame_size = new_mtu;
552 atl1c_set_rxbufsize(adapter, netdev);
553 atl1c_down(adapter);
Michał Mirosław782d6402011-04-07 07:32:18 +0000554 netdev_update_features(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -0800555 atl1c_up(adapter);
556 clear_bit(__AT_RESETTING, &adapter->flags);
Jie Yang43250dd2009-02-18 17:24:15 -0800557 }
558 return 0;
559}
560
561/*
562 * caller should hold mdio_lock
563 */
564static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
565{
566 struct atl1c_adapter *adapter = netdev_priv(netdev);
567 u16 result;
568
Huang, Xiong2528a5d2012-04-18 22:01:31 +0000569 atl1c_read_phy_reg(&adapter->hw, reg_num, &result);
Jie Yang43250dd2009-02-18 17:24:15 -0800570 return result;
571}
572
573static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
574 int reg_num, int val)
575{
576 struct atl1c_adapter *adapter = netdev_priv(netdev);
577
Huang, Xiong2528a5d2012-04-18 22:01:31 +0000578 atl1c_write_phy_reg(&adapter->hw, reg_num, val);
Jie Yang43250dd2009-02-18 17:24:15 -0800579}
580
Jie Yang43250dd2009-02-18 17:24:15 -0800581static 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
Jie Yang43250dd2009-02-18 17:24:15 -0800631static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
632{
633 switch (cmd) {
634 case SIOCGMIIPHY:
635 case SIOCGMIIREG:
636 case SIOCSMIIREG:
637 return atl1c_mii_ioctl(netdev, ifr, cmd);
638 default:
639 return -EOPNOTSUPP;
640 }
641}
642
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000643/**
Jie Yang43250dd2009-02-18 17:24:15 -0800644 * atl1c_alloc_queues - Allocate memory for all rings
645 * @adapter: board private structure to initialize
646 *
647 */
Bill Pemberton093d3692012-12-03 09:23:56 -0500648static int atl1c_alloc_queues(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -0800649{
650 return 0;
651}
652
653static void atl1c_set_mac_type(struct atl1c_hw *hw)
654{
655 switch (hw->device_id) {
656 case PCI_DEVICE_ID_ATTANSIC_L2C:
657 hw->nic_type = athr_l2c;
658 break;
Jie Yang43250dd2009-02-18 17:24:15 -0800659 case PCI_DEVICE_ID_ATTANSIC_L1C:
660 hw->nic_type = athr_l1c;
661 break;
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800662 case PCI_DEVICE_ID_ATHEROS_L2C_B:
663 hw->nic_type = athr_l2c_b;
664 break;
665 case PCI_DEVICE_ID_ATHEROS_L2C_B2:
666 hw->nic_type = athr_l2c_b2;
667 break;
668 case PCI_DEVICE_ID_ATHEROS_L1D:
669 hw->nic_type = athr_l1d;
670 break;
Jie Yang8f574b32010-06-01 00:28:12 -0700671 case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
672 hw->nic_type = athr_l1d_2;
673 break;
Jie Yang43250dd2009-02-18 17:24:15 -0800674 default:
675 break;
676 }
677}
678
679static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
680{
Jie Yang43250dd2009-02-18 17:24:15 -0800681 u32 link_ctrl_data;
682
683 atl1c_set_mac_type(hw);
Jie Yang43250dd2009-02-18 17:24:15 -0800684 AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
685
Jie Yang8f574b32010-06-01 00:28:12 -0700686 hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE |
Jie Yang43250dd2009-02-18 17:24:15 -0800687 ATL1C_TXQ_MODE_ENHANCE;
Huang, Xiong024e1e42012-04-18 22:01:28 +0000688 hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT |
689 ATL1C_ASPM_L1_SUPPORT;
Jie Yang8f574b32010-06-01 00:28:12 -0700690 hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
Jie Yang43250dd2009-02-18 17:24:15 -0800691
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800692 if (hw->nic_type == athr_l1c ||
Jie Yang8f574b32010-06-01 00:28:12 -0700693 hw->nic_type == athr_l1d ||
694 hw->nic_type == athr_l1d_2)
Luis R. Rodriguez496c1852010-02-16 15:16:45 -0800695 hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
Jie Yang43250dd2009-02-18 17:24:15 -0800696 return 0;
697}
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000698
699struct atl1c_platform_patch {
700 u16 pci_did;
701 u8 pci_revid;
702 u16 subsystem_vid;
703 u16 subsystem_did;
704 u32 patch_flag;
705#define ATL1C_LINK_PATCH 0x1
706};
Bill Pemberton093d3692012-12-03 09:23:56 -0500707static const struct atl1c_platform_patch plats[] = {
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000708{0x2060, 0xC1, 0x1019, 0x8152, 0x1},
709{0x2060, 0xC1, 0x1019, 0x2060, 0x1},
710{0x2060, 0xC1, 0x1019, 0xE000, 0x1},
711{0x2062, 0xC0, 0x1019, 0x8152, 0x1},
712{0x2062, 0xC0, 0x1019, 0x2062, 0x1},
713{0x2062, 0xC0, 0x1458, 0xE000, 0x1},
714{0x2062, 0xC1, 0x1019, 0x8152, 0x1},
715{0x2062, 0xC1, 0x1019, 0x2062, 0x1},
716{0x2062, 0xC1, 0x1458, 0xE000, 0x1},
717{0x2062, 0xC1, 0x1565, 0x2802, 0x1},
718{0x2062, 0xC1, 0x1565, 0x2801, 0x1},
719{0x1073, 0xC0, 0x1019, 0x8151, 0x1},
720{0x1073, 0xC0, 0x1019, 0x1073, 0x1},
721{0x1073, 0xC0, 0x1458, 0xE000, 0x1},
722{0x1083, 0xC0, 0x1458, 0xE000, 0x1},
723{0x1083, 0xC0, 0x1019, 0x8151, 0x1},
724{0x1083, 0xC0, 0x1019, 0x1083, 0x1},
725{0x1083, 0xC0, 0x1462, 0x7680, 0x1},
726{0x1083, 0xC0, 0x1565, 0x2803, 0x1},
727{0},
728};
729
Bill Pemberton093d3692012-12-03 09:23:56 -0500730static void atl1c_patch_assign(struct atl1c_hw *hw)
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000731{
Cloud Renfa0afcd2012-07-19 17:01:58 +0000732 struct pci_dev *pdev = hw->adapter->pdev;
733 u32 misc_ctrl;
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000734 int i = 0;
735
736 hw->msi_lnkpatch = false;
737
738 while (plats[i].pci_did != 0) {
739 if (plats[i].pci_did == hw->device_id &&
740 plats[i].pci_revid == hw->revision_id &&
741 plats[i].subsystem_vid == hw->subsystem_vendor_id &&
742 plats[i].subsystem_did == hw->subsystem_id) {
743 if (plats[i].patch_flag & ATL1C_LINK_PATCH)
744 hw->msi_lnkpatch = true;
745 }
746 i++;
747 }
Cloud Renfa0afcd2012-07-19 17:01:58 +0000748
749 if (hw->device_id == PCI_DEVICE_ID_ATHEROS_L2C_B2 &&
750 hw->revision_id == L2CB_V21) {
Joe Perchesdbedd442015-03-06 20:49:12 -0800751 /* config access mode */
Cloud Renfa0afcd2012-07-19 17:01:58 +0000752 pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
753 REG_PCIE_DEV_MISC_CTRL);
754 pci_read_config_dword(pdev, REG_PCIE_IND_ACC_DATA, &misc_ctrl);
755 misc_ctrl &= ~0x100;
756 pci_write_config_dword(pdev, REG_PCIE_IND_ACC_ADDR,
757 REG_PCIE_DEV_MISC_CTRL);
758 pci_write_config_dword(pdev, REG_PCIE_IND_ACC_DATA, misc_ctrl);
759 }
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000760}
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000761/**
Jie Yang43250dd2009-02-18 17:24:15 -0800762 * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
763 * @adapter: board private structure to initialize
764 *
765 * atl1c_sw_init initializes the Adapter private data structure.
766 * Fields are initialized based on PCI device information and
767 * OS network device settings (MTU size).
768 */
Bill Pemberton093d3692012-12-03 09:23:56 -0500769static int atl1c_sw_init(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -0800770{
771 struct atl1c_hw *hw = &adapter->hw;
772 struct pci_dev *pdev = adapter->pdev;
Jie Yang8f574b32010-06-01 00:28:12 -0700773 u32 revision;
774
Jie Yang43250dd2009-02-18 17:24:15 -0800775
776 adapter->wol = 0;
Rafael J. Wysocki762e3022010-12-22 03:07:52 +0000777 device_set_wakeup_enable(&pdev->dev, false);
Jie Yang43250dd2009-02-18 17:24:15 -0800778 adapter->link_speed = SPEED_0;
779 adapter->link_duplex = FULL_DUPLEX;
Jie Yang43250dd2009-02-18 17:24:15 -0800780 adapter->tpd_ring[0].count = 1024;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000781 adapter->rfd_ring.count = 512;
Jie Yang43250dd2009-02-18 17:24:15 -0800782
783 hw->vendor_id = pdev->vendor;
784 hw->device_id = pdev->device;
785 hw->subsystem_vendor_id = pdev->subsystem_vendor;
786 hw->subsystem_id = pdev->subsystem_device;
Cloud Renfa0afcd2012-07-19 17:01:58 +0000787 pci_read_config_dword(pdev, PCI_CLASS_REVISION, &revision);
Jie Yang8f574b32010-06-01 00:28:12 -0700788 hw->revision_id = revision & 0xFF;
Jie Yang43250dd2009-02-18 17:24:15 -0800789 /* before link up, we assume hibernate is true */
790 hw->hibernate = true;
791 hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
792 if (atl1c_setup_mac_funcs(hw) != 0) {
793 dev_err(&pdev->dev, "set mac function pointers failed\n");
794 return -1;
795 }
Huang, Xiong903d7ce2012-04-30 15:38:50 +0000796 atl1c_patch_assign(hw);
797
Jie Yang43250dd2009-02-18 17:24:15 -0800798 hw->intr_mask = IMR_NORMAL_MASK;
799 hw->phy_configured = false;
800 hw->preamble_len = 7;
801 hw->max_frame_size = adapter->netdev->mtu;
Jie Yang43250dd2009-02-18 17:24:15 -0800802 hw->autoneg_advertised = ADVERTISED_Autoneg;
803 hw->indirect_tab = 0xE4E4E4E4;
804 hw->base_cpu = 0;
805
806 hw->ict = 50000; /* 100ms */
807 hw->smb_timer = 200000; /* 400ms */
Jie Yang43250dd2009-02-18 17:24:15 -0800808 hw->rx_imt = 200;
809 hw->tx_imt = 1000;
810
811 hw->tpd_burst = 5;
812 hw->rfd_burst = 8;
813 hw->dma_order = atl1c_dma_ord_out;
814 hw->dmar_block = atl1c_dma_req_1024;
Jie Yang43250dd2009-02-18 17:24:15 -0800815
816 if (atl1c_alloc_queues(adapter)) {
817 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
818 return -ENOMEM;
819 }
820 /* TODO */
821 atl1c_set_rxbufsize(adapter, adapter->netdev);
822 atomic_set(&adapter->irq_sem, 1);
823 spin_lock_init(&adapter->mdio_lock);
824 spin_lock_init(&adapter->tx_lock);
825 set_bit(__AT_DOWN, &adapter->flags);
826
827 return 0;
828}
829
Jie Yangc6060be2009-11-06 00:32:05 -0800830static inline void atl1c_clean_buffer(struct pci_dev *pdev,
Eric W. Biederman07641c82014-03-15 15:58:10 -0700831 struct atl1c_buffer *buffer_info)
Jie Yangc6060be2009-11-06 00:32:05 -0800832{
Jie Yang4b45e342009-12-06 22:56:59 +0000833 u16 pci_driection;
Jie Yangc6060be2009-11-06 00:32:05 -0800834 if (buffer_info->flags & ATL1C_BUFFER_FREE)
835 return;
836 if (buffer_info->dma) {
Jie Yang4b45e342009-12-06 22:56:59 +0000837 if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
838 pci_driection = PCI_DMA_FROMDEVICE;
839 else
840 pci_driection = PCI_DMA_TODEVICE;
841
Jie Yangc6060be2009-11-06 00:32:05 -0800842 if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
843 pci_unmap_single(pdev, buffer_info->dma,
Jie Yang4b45e342009-12-06 22:56:59 +0000844 buffer_info->length, pci_driection);
Jie Yangc6060be2009-11-06 00:32:05 -0800845 else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
846 pci_unmap_page(pdev, buffer_info->dma,
Jie Yang4b45e342009-12-06 22:56:59 +0000847 buffer_info->length, pci_driection);
Jie Yangc6060be2009-11-06 00:32:05 -0800848 }
Eric W. Biederman07641c82014-03-15 15:58:10 -0700849 if (buffer_info->skb)
850 dev_consume_skb_any(buffer_info->skb);
Jie Yangc6060be2009-11-06 00:32:05 -0800851 buffer_info->dma = 0;
852 buffer_info->skb = NULL;
853 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
854}
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000855/**
Jie Yang43250dd2009-02-18 17:24:15 -0800856 * atl1c_clean_tx_ring - Free Tx-skb
857 * @adapter: board private structure
858 */
859static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
860 enum atl1c_trans_queue type)
861{
862 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
863 struct atl1c_buffer *buffer_info;
864 struct pci_dev *pdev = adapter->pdev;
865 u16 index, ring_count;
866
867 ring_count = tpd_ring->count;
868 for (index = 0; index < ring_count; index++) {
869 buffer_info = &tpd_ring->buffer_info[index];
Eric W. Biederman07641c82014-03-15 15:58:10 -0700870 atl1c_clean_buffer(pdev, buffer_info);
Jie Yang43250dd2009-02-18 17:24:15 -0800871 }
872
Ron Angeles47b344b2015-08-11 23:01:20 -0700873 netdev_reset_queue(adapter->netdev);
874
Jie Yang43250dd2009-02-18 17:24:15 -0800875 /* Zero out Tx-buffers */
876 memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
Jie Yangc6060be2009-11-06 00:32:05 -0800877 ring_count);
Jie Yang43250dd2009-02-18 17:24:15 -0800878 atomic_set(&tpd_ring->next_to_clean, 0);
879 tpd_ring->next_to_use = 0;
880}
881
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000882/**
Jie Yang43250dd2009-02-18 17:24:15 -0800883 * atl1c_clean_rx_ring - Free rx-reservation skbs
884 * @adapter: board private structure
885 */
886static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
887{
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000888 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
889 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800890 struct atl1c_buffer *buffer_info;
891 struct pci_dev *pdev = adapter->pdev;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000892 int j;
Jie Yang43250dd2009-02-18 17:24:15 -0800893
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000894 for (j = 0; j < rfd_ring->count; j++) {
895 buffer_info = &rfd_ring->buffer_info[j];
Eric W. Biederman07641c82014-03-15 15:58:10 -0700896 atl1c_clean_buffer(pdev, buffer_info);
Jie Yang43250dd2009-02-18 17:24:15 -0800897 }
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000898 /* zero out the descriptor ring */
899 memset(rfd_ring->desc, 0, rfd_ring->size);
900 rfd_ring->next_to_clean = 0;
901 rfd_ring->next_to_use = 0;
902 rrd_ring->next_to_use = 0;
903 rrd_ring->next_to_clean = 0;
Jie Yang43250dd2009-02-18 17:24:15 -0800904}
905
906/*
907 * Read / Write Ptr Initialize:
908 */
909static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
910{
911 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000912 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
913 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800914 struct atl1c_buffer *buffer_info;
915 int i, j;
916
917 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
918 tpd_ring[i].next_to_use = 0;
919 atomic_set(&tpd_ring[i].next_to_clean, 0);
920 buffer_info = tpd_ring[i].buffer_info;
921 for (j = 0; j < tpd_ring->count; j++)
Jie Yangc6060be2009-11-06 00:32:05 -0800922 ATL1C_SET_BUFFER_STATE(&buffer_info[i],
923 ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -0800924 }
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000925 rfd_ring->next_to_use = 0;
926 rfd_ring->next_to_clean = 0;
927 rrd_ring->next_to_use = 0;
928 rrd_ring->next_to_clean = 0;
929 for (j = 0; j < rfd_ring->count; j++) {
930 buffer_info = &rfd_ring->buffer_info[j];
931 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -0800932 }
933}
934
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000935/**
Jie Yang43250dd2009-02-18 17:24:15 -0800936 * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
937 * @adapter: board private structure
938 *
939 * Free all transmit software resources
940 */
941static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
942{
943 struct pci_dev *pdev = adapter->pdev;
944
945 pci_free_consistent(pdev, adapter->ring_header.size,
946 adapter->ring_header.desc,
947 adapter->ring_header.dma);
948 adapter->ring_header.desc = NULL;
949
950 /* Note: just free tdp_ring.buffer_info,
951 * it contain rfd_ring.buffer_info, do not double free */
952 if (adapter->tpd_ring[0].buffer_info) {
953 kfree(adapter->tpd_ring[0].buffer_info);
954 adapter->tpd_ring[0].buffer_info = NULL;
955 }
Eric Dumazet7b701762013-07-29 10:24:04 -0700956 if (adapter->rx_page) {
957 put_page(adapter->rx_page);
958 adapter->rx_page = NULL;
959 }
Jie Yang43250dd2009-02-18 17:24:15 -0800960}
961
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000962/**
Jie Yang43250dd2009-02-18 17:24:15 -0800963 * atl1c_setup_mem_resources - allocate Tx / RX descriptor resources
964 * @adapter: board private structure
965 *
966 * Return 0 on success, negative on failure
967 */
968static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
969{
970 struct pci_dev *pdev = adapter->pdev;
971 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000972 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
973 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -0800974 struct atl1c_ring_header *ring_header = &adapter->ring_header;
Jie Yang43250dd2009-02-18 17:24:15 -0800975 int size;
976 int i;
977 int count = 0;
978 int rx_desc_count = 0;
979 u32 offset = 0;
980
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000981 rrd_ring->count = rfd_ring->count;
Jie Yang43250dd2009-02-18 17:24:15 -0800982 for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
983 tpd_ring[i].count = tpd_ring[0].count;
984
Jie Yang43250dd2009-02-18 17:24:15 -0800985 /* 2 tpd queue, one high priority queue,
986 * another normal priority queue */
987 size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000988 rfd_ring->count);
Jie Yang43250dd2009-02-18 17:24:15 -0800989 tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +0000990 if (unlikely(!tpd_ring->buffer_info))
Jie Yang43250dd2009-02-18 17:24:15 -0800991 goto err_nomem;
Joe Perchesb2adaca2013-02-03 17:43:58 +0000992
Jie Yang43250dd2009-02-18 17:24:15 -0800993 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
994 tpd_ring[i].buffer_info =
Joe Perches64699332012-06-04 12:44:16 +0000995 (tpd_ring->buffer_info + count);
Jie Yang43250dd2009-02-18 17:24:15 -0800996 count += tpd_ring[i].count;
997 }
998
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +0000999 rfd_ring->buffer_info =
Joe Perches64699332012-06-04 12:44:16 +00001000 (tpd_ring->buffer_info + count);
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001001 count += rfd_ring->count;
1002 rx_desc_count += rfd_ring->count;
1003
Jie Yang43250dd2009-02-18 17:24:15 -08001004 /*
1005 * real ring DMA buffer
1006 * each ring/block may need up to 8 bytes for alignment, hence the
1007 * additional bytes tacked onto the end.
1008 */
1009 ring_header->size = size =
1010 sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
1011 sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
1012 sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
Huang, Xiong8d5c6832012-04-17 19:32:29 +00001013 8 * 4;
Jie Yang43250dd2009-02-18 17:24:15 -08001014
Pavel Machekf2a37712015-12-04 09:50:00 +01001015 ring_header->desc = dma_zalloc_coherent(&pdev->dev, ring_header->size,
1016 &ring_header->dma, GFP_KERNEL);
Jie Yang43250dd2009-02-18 17:24:15 -08001017 if (unlikely(!ring_header->desc)) {
Pavel Machekf2a37712015-12-04 09:50:00 +01001018 dev_err(&pdev->dev, "could not get memory for DMA buffer\n");
Jie Yang43250dd2009-02-18 17:24:15 -08001019 goto err_nomem;
1020 }
Jie Yang43250dd2009-02-18 17:24:15 -08001021 /* init TPD ring */
1022
1023 tpd_ring[0].dma = roundup(ring_header->dma, 8);
1024 offset = tpd_ring[0].dma - ring_header->dma;
1025 for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
1026 tpd_ring[i].dma = ring_header->dma + offset;
1027 tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
1028 tpd_ring[i].size =
1029 sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
1030 offset += roundup(tpd_ring[i].size, 8);
1031 }
1032 /* init RFD ring */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001033 rfd_ring->dma = ring_header->dma + offset;
1034 rfd_ring->desc = (u8 *) ring_header->desc + offset;
1035 rfd_ring->size = sizeof(struct atl1c_rx_free_desc) * rfd_ring->count;
1036 offset += roundup(rfd_ring->size, 8);
Jie Yang43250dd2009-02-18 17:24:15 -08001037
1038 /* init RRD ring */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001039 rrd_ring->dma = ring_header->dma + offset;
1040 rrd_ring->desc = (u8 *) ring_header->desc + offset;
1041 rrd_ring->size = sizeof(struct atl1c_recv_ret_status) *
1042 rrd_ring->count;
1043 offset += roundup(rrd_ring->size, 8);
Jie Yang43250dd2009-02-18 17:24:15 -08001044
Jie Yang43250dd2009-02-18 17:24:15 -08001045 return 0;
1046
1047err_nomem:
1048 kfree(tpd_ring->buffer_info);
1049 return -ENOMEM;
1050}
1051
1052static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
1053{
1054 struct atl1c_hw *hw = &adapter->hw;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001055 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1056 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001057 struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
1058 adapter->tpd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001059
1060 /* TPD */
1061 AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
1062 (u32)((tpd_ring[atl1c_trans_normal].dma &
1063 AT_DMA_HI_ADDR_MASK) >> 32));
1064 /* just enable normal priority TX queue */
Huang, Xiong0af48332012-04-17 19:32:30 +00001065 AT_WRITE_REG(hw, REG_TPD_PRI0_ADDR_LO,
Jie Yang43250dd2009-02-18 17:24:15 -08001066 (u32)(tpd_ring[atl1c_trans_normal].dma &
1067 AT_DMA_LO_ADDR_MASK));
Huang, Xiong0af48332012-04-17 19:32:30 +00001068 AT_WRITE_REG(hw, REG_TPD_PRI1_ADDR_LO,
Jie Yang43250dd2009-02-18 17:24:15 -08001069 (u32)(tpd_ring[atl1c_trans_high].dma &
1070 AT_DMA_LO_ADDR_MASK));
1071 AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
1072 (u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
1073
1074
1075 /* RFD */
1076 AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001077 (u32)((rfd_ring->dma & AT_DMA_HI_ADDR_MASK) >> 32));
1078 AT_WRITE_REG(hw, REG_RFD0_HEAD_ADDR_LO,
1079 (u32)(rfd_ring->dma & AT_DMA_LO_ADDR_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001080
1081 AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001082 rfd_ring->count & RFD_RING_SIZE_MASK);
Jie Yang43250dd2009-02-18 17:24:15 -08001083 AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
1084 adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
1085
1086 /* RRD */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001087 AT_WRITE_REG(hw, REG_RRD0_HEAD_ADDR_LO,
1088 (u32)(rrd_ring->dma & AT_DMA_LO_ADDR_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001089 AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001090 (rrd_ring->count & RRD_RING_SIZE_MASK));
Jie Yang43250dd2009-02-18 17:24:15 -08001091
Jie Yang8f574b32010-06-01 00:28:12 -07001092 if (hw->nic_type == athr_l2c_b) {
1093 AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
1094 AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
1095 AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
1096 AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
1097 AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
1098 AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
1099 AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0); /* TX watermark, to enter l1 state.*/
1100 AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0); /* RXD threshold.*/
1101 }
Jie Yang43250dd2009-02-18 17:24:15 -08001102 /* Load all of base address above */
1103 AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
1104}
1105
1106static void atl1c_configure_tx(struct atl1c_adapter *adapter)
1107{
1108 struct atl1c_hw *hw = &adapter->hw;
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001109 int max_pay_load;
Jie Yang43250dd2009-02-18 17:24:15 -08001110 u16 tx_offload_thresh;
1111 u32 txq_ctrl_data;
Jie Yang43250dd2009-02-18 17:24:15 -08001112
Huang, Xiongc08b9b22012-04-18 22:01:23 +00001113 tx_offload_thresh = MAX_TSO_FRAME_SIZE;
Jie Yang43250dd2009-02-18 17:24:15 -08001114 AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
1115 (tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001116 max_pay_load = pcie_get_readrq(adapter->pdev) >> 8;
stephen hemminger81b504b2011-02-23 09:06:49 +00001117 hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
Huang, Xiong59e26ef2012-04-17 19:32:36 +00001118 /*
1119 * if BIOS had changed the dam-read-max-length to an invalid value,
1120 * restore it to default value
1121 */
1122 if (hw->dmar_block < DEVICE_CTRL_MAXRRS_MIN) {
1123 pcie_set_readrq(adapter->pdev, 128 << DEVICE_CTRL_MAXRRS_MIN);
1124 hw->dmar_block = DEVICE_CTRL_MAXRRS_MIN;
1125 }
Huang, Xiongc24588a2012-04-17 19:32:35 +00001126 txq_ctrl_data =
1127 hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2 ?
1128 L2CB_TXQ_CFGV : L1C_TXQ_CFGV;
Jie Yang43250dd2009-02-18 17:24:15 -08001129
1130 AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
1131}
1132
1133static void atl1c_configure_rx(struct atl1c_adapter *adapter)
1134{
1135 struct atl1c_hw *hw = &adapter->hw;
1136 u32 rxq_ctrl_data;
1137
1138 rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
1139 RXQ_RFD_BURST_NUM_SHIFT;
1140
1141 if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
1142 rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001143
Huang, Xiong9c528212012-04-18 22:01:21 +00001144 /* aspm for gigabit */
1145 if (hw->nic_type != athr_l1d_2 && (hw->device_id & 1) != 0)
1146 rxq_ctrl_data = FIELD_SETX(rxq_ctrl_data, ASPM_THRUPUT_LIMIT,
1147 ASPM_THRUPUT_LIMIT_100M);
Jie Yang43250dd2009-02-18 17:24:15 -08001148
1149 AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
1150}
1151
Jie Yang43250dd2009-02-18 17:24:15 -08001152static void atl1c_configure_dma(struct atl1c_adapter *adapter)
1153{
1154 struct atl1c_hw *hw = &adapter->hw;
1155 u32 dma_ctrl_data;
1156
Huang, Xiong37bfccb2012-04-17 19:32:33 +00001157 dma_ctrl_data = FIELDX(DMA_CTRL_RORDER_MODE, DMA_CTRL_RORDER_MODE_OUT) |
1158 DMA_CTRL_RREQ_PRI_DATA |
1159 FIELDX(DMA_CTRL_RREQ_BLEN, hw->dmar_block) |
1160 FIELDX(DMA_CTRL_WDLY_CNT, DMA_CTRL_WDLY_CNT_DEF) |
1161 FIELDX(DMA_CTRL_RDLY_CNT, DMA_CTRL_RDLY_CNT_DEF);
Jie Yang43250dd2009-02-18 17:24:15 -08001162
1163 AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
1164}
1165
1166/*
1167 * Stop the mac, transmit and receive units
1168 * hw - Struct containing variables accessed by shared code
1169 * return : 0 or idle status (if error)
1170 */
1171static int atl1c_stop_mac(struct atl1c_hw *hw)
1172{
1173 u32 data;
Jie Yang43250dd2009-02-18 17:24:15 -08001174
1175 AT_READ_REG(hw, REG_RXQ_CTRL, &data);
Huang, Xiong027392c2012-04-17 19:32:31 +00001176 data &= ~RXQ_CTRL_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001177 AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
1178
1179 AT_READ_REG(hw, REG_TXQ_CTRL, &data);
1180 data &= ~TXQ_CTRL_EN;
Huang, Xiong0cbec612012-04-17 19:32:32 +00001181 AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
Jie Yang43250dd2009-02-18 17:24:15 -08001182
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001183 atl1c_wait_until_idle(hw, IDLE_STATUS_RXQ_BUSY | IDLE_STATUS_TXQ_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001184
1185 AT_READ_REG(hw, REG_MAC_CTRL, &data);
1186 data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
1187 AT_WRITE_REG(hw, REG_MAC_CTRL, data);
1188
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001189 return (int)atl1c_wait_until_idle(hw,
1190 IDLE_STATUS_TXMAC_BUSY | IDLE_STATUS_RXMAC_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001191}
1192
Huang, Xiong25456e52012-04-25 20:41:00 +00001193static void atl1c_start_mac(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08001194{
Huang, Xiong25456e52012-04-25 20:41:00 +00001195 struct atl1c_hw *hw = &adapter->hw;
1196 u32 mac, txq, rxq;
Jie Yang43250dd2009-02-18 17:24:15 -08001197
Huang, Xiong25456e52012-04-25 20:41:00 +00001198 hw->mac_duplex = adapter->link_duplex == FULL_DUPLEX ? true : false;
1199 hw->mac_speed = adapter->link_speed == SPEED_1000 ?
1200 atl1c_mac_speed_1000 : atl1c_mac_speed_10_100;
Jie Yang43250dd2009-02-18 17:24:15 -08001201
Huang, Xiong25456e52012-04-25 20:41:00 +00001202 AT_READ_REG(hw, REG_TXQ_CTRL, &txq);
1203 AT_READ_REG(hw, REG_RXQ_CTRL, &rxq);
1204 AT_READ_REG(hw, REG_MAC_CTRL, &mac);
Jie Yang43250dd2009-02-18 17:24:15 -08001205
Huang, Xiong25456e52012-04-25 20:41:00 +00001206 txq |= TXQ_CTRL_EN;
1207 rxq |= RXQ_CTRL_EN;
1208 mac |= MAC_CTRL_TX_EN | MAC_CTRL_TX_FLOW |
1209 MAC_CTRL_RX_EN | MAC_CTRL_RX_FLOW |
1210 MAC_CTRL_ADD_CRC | MAC_CTRL_PAD |
1211 MAC_CTRL_BC_EN | MAC_CTRL_SINGLE_PAUSE_EN |
1212 MAC_CTRL_HASH_ALG_CRC32;
1213 if (hw->mac_duplex)
1214 mac |= MAC_CTRL_DUPLX;
1215 else
1216 mac &= ~MAC_CTRL_DUPLX;
1217 mac = FIELD_SETX(mac, MAC_CTRL_SPEED, hw->mac_speed);
1218 mac = FIELD_SETX(mac, MAC_CTRL_PRMLEN, hw->preamble_len);
1219
1220 AT_WRITE_REG(hw, REG_TXQ_CTRL, txq);
1221 AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq);
1222 AT_WRITE_REG(hw, REG_MAC_CTRL, mac);
Jie Yang43250dd2009-02-18 17:24:15 -08001223}
1224
1225/*
1226 * Reset the transmit and receive units; mask and clear all interrupts.
1227 * hw - Struct containing variables accessed by shared code
1228 * return : 0 or idle status (if error)
1229 */
1230static int atl1c_reset_mac(struct atl1c_hw *hw)
1231{
Joe Perches64699332012-06-04 12:44:16 +00001232 struct atl1c_adapter *adapter = hw->adapter;
Jie Yang43250dd2009-02-18 17:24:15 -08001233 struct pci_dev *pdev = adapter->pdev;
Huang, Xiong7737fd92012-04-25 20:27:12 +00001234 u32 ctrl_data = 0;
Jie Yang43250dd2009-02-18 17:24:15 -08001235
Jie Yang8f574b32010-06-01 00:28:12 -07001236 atl1c_stop_mac(hw);
Jie Yang43250dd2009-02-18 17:24:15 -08001237 /*
1238 * Issue Soft Reset to the MAC. This will reset the chip's
1239 * transmit, receive, DMA. It will not effect
1240 * the current PCI configuration. The global reset bit is self-
1241 * clearing, and should clear within a microsecond.
1242 */
Huang, Xiong7737fd92012-04-25 20:27:12 +00001243 AT_READ_REG(hw, REG_MASTER_CTRL, &ctrl_data);
1244 ctrl_data |= MASTER_CTRL_OOB_DIS;
1245 AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data | MASTER_CTRL_SOFT_RST);
Jie Yang8f574b32010-06-01 00:28:12 -07001246
Jie Yang43250dd2009-02-18 17:24:15 -08001247 AT_WRITE_FLUSH(hw);
1248 msleep(10);
1249 /* Wait at least 10ms for All module to be Idle */
Joe Perchesc930a662009-05-31 20:44:54 +00001250
Huang, Xiong969a7ee2012-04-18 22:01:22 +00001251 if (atl1c_wait_until_idle(hw, IDLE_STATUS_MASK)) {
Jie Yang43250dd2009-02-18 17:24:15 -08001252 dev_err(&pdev->dev,
Joe Perchesc930a662009-05-31 20:44:54 +00001253 "MAC state machine can't be idle since"
Jie Yang43250dd2009-02-18 17:24:15 -08001254 " disabled for 10ms second\n");
1255 return -1;
1256 }
Huang, Xiong7737fd92012-04-25 20:27:12 +00001257 AT_WRITE_REG(hw, REG_MASTER_CTRL, ctrl_data);
1258
1259 /* driver control speed/duplex */
1260 AT_READ_REG(hw, REG_MAC_CTRL, &ctrl_data);
1261 AT_WRITE_REG(hw, REG_MAC_CTRL, ctrl_data | MAC_CTRL_SPEED_MODE_SW);
1262
1263 /* clk switch setting */
1264 AT_READ_REG(hw, REG_SERDES, &ctrl_data);
1265 switch (hw->nic_type) {
1266 case athr_l2c_b:
1267 ctrl_data &= ~(SERDES_PHY_CLK_SLOWDOWN |
1268 SERDES_MAC_CLK_SLOWDOWN);
1269 AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1270 break;
1271 case athr_l2c_b2:
1272 case athr_l1d_2:
1273 ctrl_data |= SERDES_PHY_CLK_SLOWDOWN | SERDES_MAC_CLK_SLOWDOWN;
1274 AT_WRITE_REG(hw, REG_SERDES, ctrl_data);
1275 break;
1276 default:
1277 break;
1278 }
Huang, Xiongebe22ed2012-04-18 22:01:29 +00001279
Jie Yang43250dd2009-02-18 17:24:15 -08001280 return 0;
1281}
1282
1283static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
1284{
Huang, Xiong024e1e42012-04-18 22:01:28 +00001285 u16 ctrl_flags = hw->ctrl_flags;
Jie Yang43250dd2009-02-18 17:24:15 -08001286
Huang, Xiong024e1e42012-04-18 22:01:28 +00001287 hw->ctrl_flags &= ~(ATL1C_ASPM_L0S_SUPPORT | ATL1C_ASPM_L1_SUPPORT);
1288 atl1c_set_aspm(hw, SPEED_0);
1289 hw->ctrl_flags = ctrl_flags;
Jie Yang43250dd2009-02-18 17:24:15 -08001290}
1291
1292/*
1293 * Set ASPM state.
1294 * Enable/disable L0s/L1 depend on link state.
1295 */
Huang, Xiong024e1e42012-04-18 22:01:28 +00001296static void atl1c_set_aspm(struct atl1c_hw *hw, u16 link_speed)
Jie Yang43250dd2009-02-18 17:24:15 -08001297{
1298 u32 pm_ctrl_data;
Huang, Xiong024e1e42012-04-18 22:01:28 +00001299 u32 link_l1_timer;
Jie Yang43250dd2009-02-18 17:24:15 -08001300
1301 AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
Huang, Xiong024e1e42012-04-18 22:01:28 +00001302 pm_ctrl_data &= ~(PM_CTRL_ASPM_L1_EN |
1303 PM_CTRL_ASPM_L0S_EN |
1304 PM_CTRL_MAC_ASPM_CHK);
1305 /* L1 timer */
1306 if (hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1307 pm_ctrl_data &= ~PMCTRL_TXL1_AFTER_L0S;
1308 link_l1_timer =
1309 link_speed == SPEED_1000 || link_speed == SPEED_100 ?
1310 L1D_PMCTRL_L1_ENTRY_TM_16US : 1;
1311 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1312 L1D_PMCTRL_L1_ENTRY_TM, link_l1_timer);
Jie Yang43250dd2009-02-18 17:24:15 -08001313 } else {
Huang, Xiong024e1e42012-04-18 22:01:28 +00001314 link_l1_timer = hw->nic_type == athr_l2c_b ?
1315 L2CB1_PM_CTRL_L1_ENTRY_TM : L1C_PM_CTRL_L1_ENTRY_TM;
1316 if (link_speed != SPEED_1000 && link_speed != SPEED_100)
1317 link_l1_timer = 1;
1318 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1319 PM_CTRL_L1_ENTRY_TIMER, link_l1_timer);
1320 }
Jie Yang43250dd2009-02-18 17:24:15 -08001321
Huang, Xiong024e1e42012-04-18 22:01:28 +00001322 /* L0S/L1 enable */
Huang, Xiong4fc36352012-04-30 15:38:57 +00001323 if ((hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT) && link_speed != SPEED_0)
Huang, Xiong024e1e42012-04-18 22:01:28 +00001324 pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN | PM_CTRL_MAC_ASPM_CHK;
1325 if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
1326 pm_ctrl_data |= PM_CTRL_ASPM_L1_EN | PM_CTRL_MAC_ASPM_CHK;
1327
1328 /* l2cb & l1d & l2cb2 & l1d2 */
1329 if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
1330 hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
1331 pm_ctrl_data = FIELD_SETX(pm_ctrl_data,
1332 PM_CTRL_PM_REQ_TIMER, PM_CTRL_PM_REQ_TO_DEF);
1333 pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER |
1334 PM_CTRL_SERDES_PD_EX_L1 |
1335 PM_CTRL_CLK_SWH_L1;
1336 pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1337 PM_CTRL_SERDES_PLL_L1_EN |
1338 PM_CTRL_SERDES_BUFS_RX_L1_EN |
1339 PM_CTRL_SA_DLY_EN |
1340 PM_CTRL_HOTRST);
1341 /* disable l0s if link down or l2cb */
1342 if (link_speed == SPEED_0 || hw->nic_type == athr_l2c_b)
1343 pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
1344 } else { /* l1c */
1345 pm_ctrl_data =
1346 FIELD_SETX(pm_ctrl_data, PM_CTRL_L1_ENTRY_TIMER, 0);
1347 if (link_speed != SPEED_0) {
1348 pm_ctrl_data |= PM_CTRL_SERDES_L1_EN |
1349 PM_CTRL_SERDES_PLL_L1_EN |
1350 PM_CTRL_SERDES_BUFS_RX_L1_EN;
1351 pm_ctrl_data &= ~(PM_CTRL_SERDES_PD_EX_L1 |
1352 PM_CTRL_CLK_SWH_L1 |
1353 PM_CTRL_ASPM_L0S_EN |
1354 PM_CTRL_ASPM_L1_EN);
1355 } else { /* link down */
1356 pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
1357 pm_ctrl_data &= ~(PM_CTRL_SERDES_L1_EN |
1358 PM_CTRL_SERDES_PLL_L1_EN |
1359 PM_CTRL_SERDES_BUFS_RX_L1_EN |
1360 PM_CTRL_ASPM_L0S_EN);
1361 }
Jie Yang43250dd2009-02-18 17:24:15 -08001362 }
Jie Yang43250dd2009-02-18 17:24:15 -08001363 AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
Jie Yang8f574b32010-06-01 00:28:12 -07001364
1365 return;
Jie Yang43250dd2009-02-18 17:24:15 -08001366}
1367
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001368/**
Jie Yang43250dd2009-02-18 17:24:15 -08001369 * atl1c_configure - Configure Transmit&Receive Unit after Reset
1370 * @adapter: board private structure
1371 *
1372 * Configure the Tx /Rx unit of the MAC after a reset.
1373 */
Huang, Xiong5e5c0962012-04-30 15:38:56 +00001374static int atl1c_configure_mac(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08001375{
1376 struct atl1c_hw *hw = &adapter->hw;
1377 u32 master_ctrl_data = 0;
1378 u32 intr_modrt_data;
Jie Yang8f574b32010-06-01 00:28:12 -07001379 u32 data;
Jie Yang43250dd2009-02-18 17:24:15 -08001380
Huang, Xiongebe22ed2012-04-18 22:01:29 +00001381 AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
1382 master_ctrl_data &= ~(MASTER_CTRL_TX_ITIMER_EN |
1383 MASTER_CTRL_RX_ITIMER_EN |
1384 MASTER_CTRL_INT_RDCLR);
Jie Yang43250dd2009-02-18 17:24:15 -08001385 /* clear interrupt status */
1386 AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
1387 /* Clear any WOL status */
1388 AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
1389 /* set Interrupt Clear Timer
1390 * HW will enable self to assert interrupt event to system after
1391 * waiting x-time for software to notify it accept interrupt.
1392 */
Jie Yang8f574b32010-06-01 00:28:12 -07001393
1394 data = CLK_GATING_EN_ALL;
1395 if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
1396 if (hw->nic_type == athr_l2c_b)
1397 data &= ~CLK_GATING_RXMAC_EN;
1398 } else
1399 data = 0;
1400 AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
1401
Jie Yang43250dd2009-02-18 17:24:15 -08001402 AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
1403 hw->ict & INT_RETRIG_TIMER_MASK);
1404
1405 atl1c_configure_des_ring(adapter);
1406
1407 if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
1408 intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
1409 IRQ_MODRT_TX_TIMER_SHIFT;
1410 intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
1411 IRQ_MODRT_RX_TIMER_SHIFT;
1412 AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
1413 master_ctrl_data |=
1414 MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
1415 }
1416
1417 if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
1418 master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
1419
Jie Yang8f574b32010-06-01 00:28:12 -07001420 master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
Jie Yang43250dd2009-02-18 17:24:15 -08001421 AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
1422
Huang, Xiong8d5c6832012-04-17 19:32:29 +00001423 AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
1424 hw->smb_timer & SMB_STAT_TIMER_MASK);
Jie Yang43250dd2009-02-18 17:24:15 -08001425
Jie Yang43250dd2009-02-18 17:24:15 -08001426 /* set MTU */
1427 AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
1428 VLAN_HLEN + ETH_FCS_LEN);
Jie Yang43250dd2009-02-18 17:24:15 -08001429
1430 atl1c_configure_tx(adapter);
1431 atl1c_configure_rx(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001432 atl1c_configure_dma(adapter);
1433
1434 return 0;
1435}
1436
Huang, Xiong5e5c0962012-04-30 15:38:56 +00001437static int atl1c_configure(struct atl1c_adapter *adapter)
1438{
1439 struct net_device *netdev = adapter->netdev;
1440 int num;
1441
1442 atl1c_init_ring_ptrs(adapter);
1443 atl1c_set_multi(netdev);
1444 atl1c_restore_vlan(adapter);
1445
1446 num = atl1c_alloc_rx_buffer(adapter);
1447 if (unlikely(num == 0))
1448 return -ENOMEM;
1449
1450 if (atl1c_configure_mac(adapter))
1451 return -EIO;
1452
1453 return 0;
1454}
1455
Jie Yang43250dd2009-02-18 17:24:15 -08001456static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
1457{
1458 u16 hw_reg_addr = 0;
1459 unsigned long *stats_item = NULL;
1460 u32 data;
1461
1462 /* update rx status */
1463 hw_reg_addr = REG_MAC_RX_STATUS_BIN;
1464 stats_item = &adapter->hw_stats.rx_ok;
1465 while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
1466 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1467 *stats_item += data;
1468 stats_item++;
1469 hw_reg_addr += 4;
1470 }
1471/* update tx status */
1472 hw_reg_addr = REG_MAC_TX_STATUS_BIN;
1473 stats_item = &adapter->hw_stats.tx_ok;
1474 while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
1475 AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
1476 *stats_item += data;
1477 stats_item++;
1478 hw_reg_addr += 4;
1479 }
1480}
1481
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001482/**
Jie Yang43250dd2009-02-18 17:24:15 -08001483 * atl1c_get_stats - Get System Network Statistics
1484 * @netdev: network interface device structure
1485 *
1486 * Returns the address of the device statistics structure.
1487 * The statistics are actually updated from the timer callback.
1488 */
1489static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
1490{
1491 struct atl1c_adapter *adapter = netdev_priv(netdev);
1492 struct atl1c_hw_stats *hw_stats = &adapter->hw_stats;
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001493 struct net_device_stats *net_stats = &netdev->stats;
Jie Yang43250dd2009-02-18 17:24:15 -08001494
1495 atl1c_update_hw_stats(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001496 net_stats->rx_bytes = hw_stats->rx_byte_cnt;
1497 net_stats->tx_bytes = hw_stats->tx_byte_cnt;
1498 net_stats->multicast = hw_stats->rx_mcast;
1499 net_stats->collisions = hw_stats->tx_1_col +
Sabrina Dubroca85602582014-01-12 18:50:38 +01001500 hw_stats->tx_2_col +
1501 hw_stats->tx_late_col +
1502 hw_stats->tx_abort_col;
1503
1504 net_stats->rx_errors = hw_stats->rx_frag +
1505 hw_stats->rx_fcs_err +
1506 hw_stats->rx_len_err +
1507 hw_stats->rx_sz_ov +
1508 hw_stats->rx_rrd_ov +
1509 hw_stats->rx_align_err +
1510 hw_stats->rx_rxf_ov;
1511
Jie Yang43250dd2009-02-18 17:24:15 -08001512 net_stats->rx_fifo_errors = hw_stats->rx_rxf_ov;
1513 net_stats->rx_length_errors = hw_stats->rx_len_err;
1514 net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
1515 net_stats->rx_frame_errors = hw_stats->rx_align_err;
Sabrina Dubroca85602582014-01-12 18:50:38 +01001516 net_stats->rx_dropped = hw_stats->rx_rrd_ov;
Jie Yang43250dd2009-02-18 17:24:15 -08001517
Sabrina Dubroca85602582014-01-12 18:50:38 +01001518 net_stats->tx_errors = hw_stats->tx_late_col +
1519 hw_stats->tx_abort_col +
1520 hw_stats->tx_underrun +
1521 hw_stats->tx_trunc;
Jie Yang43250dd2009-02-18 17:24:15 -08001522
Jie Yang43250dd2009-02-18 17:24:15 -08001523 net_stats->tx_fifo_errors = hw_stats->tx_underrun;
1524 net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1525 net_stats->tx_window_errors = hw_stats->tx_late_col;
1526
Sabrina Dubroca85602582014-01-12 18:50:38 +01001527 net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
1528 net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
1529
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001530 return net_stats;
Jie Yang43250dd2009-02-18 17:24:15 -08001531}
1532
1533static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
1534{
1535 u16 phy_data;
1536
1537 spin_lock(&adapter->mdio_lock);
1538 atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
1539 spin_unlock(&adapter->mdio_lock);
1540}
1541
1542static bool atl1c_clean_tx_irq(struct atl1c_adapter *adapter,
1543 enum atl1c_trans_queue type)
1544{
Joe Perches64699332012-06-04 12:44:16 +00001545 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
Jie Yang43250dd2009-02-18 17:24:15 -08001546 struct atl1c_buffer *buffer_info;
Jie Yangc6060be2009-11-06 00:32:05 -08001547 struct pci_dev *pdev = adapter->pdev;
Jie Yang43250dd2009-02-18 17:24:15 -08001548 u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1549 u16 hw_next_to_clean;
Huang, Xiong0af48332012-04-17 19:32:30 +00001550 u16 reg;
Ron Angeles47b344b2015-08-11 23:01:20 -07001551 unsigned int total_bytes = 0, total_packets = 0;
Jie Yang43250dd2009-02-18 17:24:15 -08001552
Huang, Xiong0af48332012-04-17 19:32:30 +00001553 reg = type == atl1c_trans_high ? REG_TPD_PRI1_CIDX : REG_TPD_PRI0_CIDX;
Jie Yang43250dd2009-02-18 17:24:15 -08001554
Huang, Xiong0af48332012-04-17 19:32:30 +00001555 AT_READ_REGW(&adapter->hw, reg, &hw_next_to_clean);
Jie Yang43250dd2009-02-18 17:24:15 -08001556
1557 while (next_to_clean != hw_next_to_clean) {
1558 buffer_info = &tpd_ring->buffer_info[next_to_clean];
Ron Angeles47b344b2015-08-11 23:01:20 -07001559 if (buffer_info->skb) {
1560 total_bytes += buffer_info->skb->len;
1561 total_packets++;
1562 }
Eric W. Biederman07641c82014-03-15 15:58:10 -07001563 atl1c_clean_buffer(pdev, buffer_info);
Jie Yang43250dd2009-02-18 17:24:15 -08001564 if (++next_to_clean == tpd_ring->count)
1565 next_to_clean = 0;
1566 atomic_set(&tpd_ring->next_to_clean, next_to_clean);
1567 }
1568
Ron Angeles47b344b2015-08-11 23:01:20 -07001569 netdev_completed_queue(adapter->netdev, total_packets, total_bytes);
1570
Jie Yang43250dd2009-02-18 17:24:15 -08001571 if (netif_queue_stopped(adapter->netdev) &&
1572 netif_carrier_ok(adapter->netdev)) {
1573 netif_wake_queue(adapter->netdev);
1574 }
1575
1576 return true;
1577}
1578
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001579/**
Jie Yang43250dd2009-02-18 17:24:15 -08001580 * atl1c_intr - Interrupt Handler
1581 * @irq: interrupt number
1582 * @data: pointer to a network interface device structure
Jie Yang43250dd2009-02-18 17:24:15 -08001583 */
1584static irqreturn_t atl1c_intr(int irq, void *data)
1585{
1586 struct net_device *netdev = data;
1587 struct atl1c_adapter *adapter = netdev_priv(netdev);
1588 struct pci_dev *pdev = adapter->pdev;
1589 struct atl1c_hw *hw = &adapter->hw;
1590 int max_ints = AT_MAX_INT_WORK;
1591 int handled = IRQ_NONE;
1592 u32 status;
1593 u32 reg_data;
1594
1595 do {
1596 AT_READ_REG(hw, REG_ISR, &reg_data);
1597 status = reg_data & hw->intr_mask;
1598
1599 if (status == 0 || (status & ISR_DIS_INT) != 0) {
1600 if (max_ints != AT_MAX_INT_WORK)
1601 handled = IRQ_HANDLED;
1602 break;
1603 }
1604 /* link event */
1605 if (status & ISR_GPHY)
1606 atl1c_clear_phy_int(adapter);
1607 /* Ack ISR */
1608 AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
1609 if (status & ISR_RX_PKT) {
1610 if (likely(napi_schedule_prep(&adapter->napi))) {
1611 hw->intr_mask &= ~ISR_RX_PKT;
1612 AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
1613 __napi_schedule(&adapter->napi);
1614 }
1615 }
1616 if (status & ISR_TX_PKT)
1617 atl1c_clean_tx_irq(adapter, atl1c_trans_normal);
1618
1619 handled = IRQ_HANDLED;
1620 /* check if PCIE PHY Link down */
1621 if (status & ISR_ERROR) {
1622 if (netif_msg_hw(adapter))
1623 dev_err(&pdev->dev,
1624 "atl1c hardware error (status = 0x%x)\n",
1625 status & ISR_ERROR);
1626 /* reset MAC */
Dan Carpenter78315452012-03-07 00:02:04 +00001627 set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
Jie Yangcb190542009-12-06 23:16:58 +00001628 schedule_work(&adapter->common_task);
Jie Yang8f574b32010-06-01 00:28:12 -07001629 return IRQ_HANDLED;
Jie Yang43250dd2009-02-18 17:24:15 -08001630 }
1631
1632 if (status & ISR_OVER)
1633 if (netif_msg_intr(adapter))
1634 dev_warn(&pdev->dev,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001635 "TX/RX overflow (status = 0x%x)\n",
Jie Yang43250dd2009-02-18 17:24:15 -08001636 status & ISR_OVER);
1637
1638 /* link event */
1639 if (status & (ISR_GPHY | ISR_MANUAL)) {
Eric Dumazeta2c483a2010-08-20 02:55:16 +00001640 netdev->stats.tx_carrier_errors++;
Jie Yang43250dd2009-02-18 17:24:15 -08001641 atl1c_link_chg_event(adapter);
1642 break;
1643 }
1644
1645 } while (--max_ints > 0);
1646 /* re-enable Interrupt*/
1647 AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
1648 return handled;
1649}
1650
1651static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
1652 struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
1653{
1654 /*
1655 * The pid field in RRS in not correct sometimes, so we
1656 * cannot figure out if the packet is fragmented or not,
1657 * so we tell the KERNEL CHECKSUM_NONE
1658 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001659 skb_checksum_none_assert(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08001660}
1661
Eric Dumazet7b701762013-07-29 10:24:04 -07001662static struct sk_buff *atl1c_alloc_skb(struct atl1c_adapter *adapter)
1663{
1664 struct sk_buff *skb;
1665 struct page *page;
1666
1667 if (adapter->rx_frag_size > PAGE_SIZE)
1668 return netdev_alloc_skb(adapter->netdev,
1669 adapter->rx_buffer_len);
1670
1671 page = adapter->rx_page;
1672 if (!page) {
1673 adapter->rx_page = page = alloc_page(GFP_ATOMIC);
1674 if (unlikely(!page))
1675 return NULL;
1676 adapter->rx_page_offset = 0;
1677 }
1678
1679 skb = build_skb(page_address(page) + adapter->rx_page_offset,
1680 adapter->rx_frag_size);
1681 if (likely(skb)) {
1682 adapter->rx_page_offset += adapter->rx_frag_size;
1683 if (adapter->rx_page_offset >= PAGE_SIZE)
1684 adapter->rx_page = NULL;
1685 else
1686 get_page(page);
1687 }
1688 return skb;
1689}
1690
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001691static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08001692{
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001693 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001694 struct pci_dev *pdev = adapter->pdev;
1695 struct atl1c_buffer *buffer_info, *next_info;
1696 struct sk_buff *skb;
1697 void *vir_addr = NULL;
1698 u16 num_alloc = 0;
1699 u16 rfd_next_to_use, next_next;
1700 struct atl1c_rx_free_desc *rfd_desc;
Huang, Xiongac574802013-02-11 14:44:40 +00001701 dma_addr_t mapping;
Jie Yang43250dd2009-02-18 17:24:15 -08001702
1703 next_next = rfd_next_to_use = rfd_ring->next_to_use;
1704 if (++next_next == rfd_ring->count)
1705 next_next = 0;
1706 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1707 next_info = &rfd_ring->buffer_info[next_next];
1708
Jie Yangc6060be2009-11-06 00:32:05 -08001709 while (next_info->flags & ATL1C_BUFFER_FREE) {
Jie Yang43250dd2009-02-18 17:24:15 -08001710 rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
1711
Eric Dumazet7b701762013-07-29 10:24:04 -07001712 skb = atl1c_alloc_skb(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001713 if (unlikely(!skb)) {
1714 if (netif_msg_rx_err(adapter))
1715 dev_warn(&pdev->dev, "alloc rx buffer failed\n");
1716 break;
1717 }
1718
1719 /*
1720 * Make buffer alignment 2 beyond a 16 byte boundary
1721 * this will result in a 16 byte aligned IP header after
1722 * the 14 byte MAC header is removed
1723 */
1724 vir_addr = skb->data;
Jie Yangc6060be2009-11-06 00:32:05 -08001725 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang43250dd2009-02-18 17:24:15 -08001726 buffer_info->skb = skb;
1727 buffer_info->length = adapter->rx_buffer_len;
Huang, Xiongac574802013-02-11 14:44:40 +00001728 mapping = pci_map_single(pdev, vir_addr,
Jie Yang43250dd2009-02-18 17:24:15 -08001729 buffer_info->length,
1730 PCI_DMA_FROMDEVICE);
Huang, Xiongac574802013-02-11 14:44:40 +00001731 if (unlikely(pci_dma_mapping_error(pdev, mapping))) {
1732 dev_kfree_skb(skb);
1733 buffer_info->skb = NULL;
1734 buffer_info->length = 0;
1735 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
1736 netif_warn(adapter, rx_err, adapter->netdev, "RX pci_map_single failed");
1737 break;
1738 }
1739 buffer_info->dma = mapping;
Jie Yang4b45e342009-12-06 22:56:59 +00001740 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
1741 ATL1C_PCIMAP_FROMDEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08001742 rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
1743 rfd_next_to_use = next_next;
1744 if (++next_next == rfd_ring->count)
1745 next_next = 0;
1746 buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
1747 next_info = &rfd_ring->buffer_info[next_next];
1748 num_alloc++;
1749 }
1750
1751 if (num_alloc) {
1752 /* TODO: update mailbox here */
1753 wmb();
1754 rfd_ring->next_to_use = rfd_next_to_use;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001755 AT_WRITE_REG(&adapter->hw, REG_MB_RFD0_PROD_IDX,
Jie Yang43250dd2009-02-18 17:24:15 -08001756 rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
1757 }
1758
1759 return num_alloc;
1760}
1761
1762static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring,
1763 struct atl1c_recv_ret_status *rrs, u16 num)
1764{
1765 u16 i;
1766 /* the relationship between rrd and rfd is one map one */
1767 for (i = 0; i < num; i++, rrs = ATL1C_RRD_DESC(rrd_ring,
1768 rrd_ring->next_to_clean)) {
1769 rrs->word3 &= ~RRS_RXD_UPDATED;
1770 if (++rrd_ring->next_to_clean == rrd_ring->count)
1771 rrd_ring->next_to_clean = 0;
1772 }
1773}
1774
1775static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring,
1776 struct atl1c_recv_ret_status *rrs, u16 num)
1777{
1778 u16 i;
1779 u16 rfd_index;
1780 struct atl1c_buffer *buffer_info = rfd_ring->buffer_info;
1781
1782 rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1783 RRS_RX_RFD_INDEX_MASK;
1784 for (i = 0; i < num; i++) {
1785 buffer_info[rfd_index].skb = NULL;
Jie Yangc6060be2009-11-06 00:32:05 -08001786 ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index],
1787 ATL1C_BUFFER_FREE);
Jie Yang43250dd2009-02-18 17:24:15 -08001788 if (++rfd_index == rfd_ring->count)
1789 rfd_index = 0;
1790 }
1791 rfd_ring->next_to_clean = rfd_index;
1792}
1793
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001794static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter,
Jie Yang43250dd2009-02-18 17:24:15 -08001795 int *work_done, int work_to_do)
1796{
1797 u16 rfd_num, rfd_index;
1798 u16 count = 0;
1799 u16 length;
1800 struct pci_dev *pdev = adapter->pdev;
1801 struct net_device *netdev = adapter->netdev;
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001802 struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring;
1803 struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring;
Jie Yang43250dd2009-02-18 17:24:15 -08001804 struct sk_buff *skb;
1805 struct atl1c_recv_ret_status *rrs;
1806 struct atl1c_buffer *buffer_info;
1807
1808 while (1) {
1809 if (*work_done >= work_to_do)
1810 break;
1811 rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean);
1812 if (likely(RRS_RXD_IS_VALID(rrs->word3))) {
1813 rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) &
1814 RRS_RX_RFD_CNT_MASK;
roel kluin37b76c62009-07-12 12:57:38 +00001815 if (unlikely(rfd_num != 1))
Jie Yang43250dd2009-02-18 17:24:15 -08001816 /* TODO support mul rfd*/
1817 if (netif_msg_rx_err(adapter))
1818 dev_warn(&pdev->dev,
1819 "Multi rfd not support yet!\n");
1820 goto rrs_checked;
1821 } else {
1822 break;
1823 }
1824rrs_checked:
1825 atl1c_clean_rrd(rrd_ring, rrs, rfd_num);
1826 if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) {
1827 atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1828 if (netif_msg_rx_err(adapter))
1829 dev_warn(&pdev->dev,
1830 "wrong packet! rrs word3 is %x\n",
1831 rrs->word3);
1832 continue;
1833 }
1834
1835 length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) &
1836 RRS_PKT_SIZE_MASK);
1837 /* Good Receive */
1838 if (likely(rfd_num == 1)) {
1839 rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
1840 RRS_RX_RFD_INDEX_MASK;
1841 buffer_info = &rfd_ring->buffer_info[rfd_index];
1842 pci_unmap_single(pdev, buffer_info->dma,
1843 buffer_info->length, PCI_DMA_FROMDEVICE);
1844 skb = buffer_info->skb;
1845 } else {
1846 /* TODO */
1847 if (netif_msg_rx_err(adapter))
1848 dev_warn(&pdev->dev,
1849 "Multi rfd not support yet!\n");
1850 break;
1851 }
1852 atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
1853 skb_put(skb, length - ETH_FCS_LEN);
1854 skb->protocol = eth_type_trans(skb, netdev);
Jie Yang43250dd2009-02-18 17:24:15 -08001855 atl1c_rx_checksum(adapter, skb, rrs);
Jiri Pirko46facce2011-07-20 04:54:12 +00001856 if (rrs->word3 & RRS_VLAN_INS) {
Jie Yang43250dd2009-02-18 17:24:15 -08001857 u16 vlan;
1858
1859 AT_TAG_TO_VLAN(rrs->vlan_tag, vlan);
1860 vlan = le16_to_cpu(vlan);
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001861 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan);
Jiri Pirko46facce2011-07-20 04:54:12 +00001862 }
1863 netif_receive_skb(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08001864
Jie Yang43250dd2009-02-18 17:24:15 -08001865 (*work_done)++;
1866 count++;
1867 }
1868 if (count)
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001869 atl1c_alloc_rx_buffer(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08001870}
1871
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001872/**
Jie Yang43250dd2009-02-18 17:24:15 -08001873 * atl1c_clean - NAPI Rx polling callback
Jie Yang43250dd2009-02-18 17:24:15 -08001874 */
1875static int atl1c_clean(struct napi_struct *napi, int budget)
1876{
1877 struct atl1c_adapter *adapter =
1878 container_of(napi, struct atl1c_adapter, napi);
1879 int work_done = 0;
1880
1881 /* Keep link state information with original netdev */
1882 if (!netif_carrier_ok(adapter->netdev))
1883 goto quit_polling;
1884 /* just enable one RXQ */
Huang, Xiong9f1fd0e2012-04-17 19:32:26 +00001885 atl1c_clean_rx_irq(adapter, &work_done, budget);
Jie Yang43250dd2009-02-18 17:24:15 -08001886
1887 if (work_done < budget) {
1888quit_polling:
1889 napi_complete(napi);
1890 adapter->hw.intr_mask |= ISR_RX_PKT;
1891 AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
1892 }
1893 return work_done;
1894}
1895
1896#ifdef CONFIG_NET_POLL_CONTROLLER
1897
1898/*
1899 * Polling 'interrupt' - used by things like netconsole to send skbs
1900 * without having to re-enable interrupts. It's not called while
1901 * the interrupt routine is executing.
1902 */
1903static void atl1c_netpoll(struct net_device *netdev)
1904{
1905 struct atl1c_adapter *adapter = netdev_priv(netdev);
1906
1907 disable_irq(adapter->pdev->irq);
1908 atl1c_intr(adapter->pdev->irq, netdev);
1909 enable_irq(adapter->pdev->irq);
1910}
1911#endif
1912
1913static inline u16 atl1c_tpd_avail(struct atl1c_adapter *adapter, enum atl1c_trans_queue type)
1914{
1915 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1916 u16 next_to_use = 0;
1917 u16 next_to_clean = 0;
1918
1919 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
1920 next_to_use = tpd_ring->next_to_use;
1921
1922 return (u16)(next_to_clean > next_to_use) ?
1923 (next_to_clean - next_to_use - 1) :
1924 (tpd_ring->count + next_to_clean - next_to_use - 1);
1925}
1926
1927/*
1928 * get next usable tpd
1929 * Note: should call atl1c_tdp_avail to make sure
1930 * there is enough tpd to use
1931 */
1932static struct atl1c_tpd_desc *atl1c_get_tpd(struct atl1c_adapter *adapter,
1933 enum atl1c_trans_queue type)
1934{
1935 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
1936 struct atl1c_tpd_desc *tpd_desc;
1937 u16 next_to_use = 0;
1938
1939 next_to_use = tpd_ring->next_to_use;
1940 if (++tpd_ring->next_to_use == tpd_ring->count)
1941 tpd_ring->next_to_use = 0;
1942 tpd_desc = ATL1C_TPD_DESC(tpd_ring, next_to_use);
1943 memset(tpd_desc, 0, sizeof(struct atl1c_tpd_desc));
1944 return tpd_desc;
1945}
1946
1947static struct atl1c_buffer *
1948atl1c_get_tx_buffer(struct atl1c_adapter *adapter, struct atl1c_tpd_desc *tpd)
1949{
1950 struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
1951
1952 return &tpd_ring->buffer_info[tpd -
1953 (struct atl1c_tpd_desc *)tpd_ring->desc];
1954}
1955
1956/* Calculate the transmit packet descript needed*/
1957static u16 atl1c_cal_tpd_req(const struct sk_buff *skb)
1958{
1959 u16 tpd_req;
1960 u16 proto_hdr_len = 0;
1961
1962 tpd_req = skb_shinfo(skb)->nr_frags + 1;
1963
1964 if (skb_is_gso(skb)) {
1965 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1966 if (proto_hdr_len < skb_headlen(skb))
1967 tpd_req++;
1968 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
1969 tpd_req++;
1970 }
1971 return tpd_req;
1972}
1973
1974static int atl1c_tso_csum(struct atl1c_adapter *adapter,
1975 struct sk_buff *skb,
1976 struct atl1c_tpd_desc **tpd,
1977 enum atl1c_trans_queue type)
1978{
1979 struct pci_dev *pdev = adapter->pdev;
françois romieu0f5c1132014-03-29 12:26:15 +01001980 unsigned short offload_type;
Jie Yang43250dd2009-02-18 17:24:15 -08001981 u8 hdr_len;
1982 u32 real_len;
Jie Yang43250dd2009-02-18 17:24:15 -08001983
1984 if (skb_is_gso(skb)) {
françois romieu0f5c1132014-03-29 12:26:15 +01001985 int err;
1986
1987 err = skb_cow_head(skb, 0);
1988 if (err < 0)
1989 return err;
1990
Jie Yang43250dd2009-02-18 17:24:15 -08001991 offload_type = skb_shinfo(skb)->gso_type;
1992
1993 if (offload_type & SKB_GSO_TCPV4) {
1994 real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
1995 + ntohs(ip_hdr(skb)->tot_len));
1996
1997 if (real_len < skb->len)
1998 pskb_trim(skb, real_len);
1999
2000 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2001 if (unlikely(skb->len == hdr_len)) {
2002 /* only xsum need */
2003 if (netif_msg_tx_queued(adapter))
2004 dev_warn(&pdev->dev,
2005 "IPV4 tso with zero data??\n");
2006 goto check_sum;
2007 } else {
2008 ip_hdr(skb)->check = 0;
2009 tcp_hdr(skb)->check = ~csum_tcpudp_magic(
2010 ip_hdr(skb)->saddr,
2011 ip_hdr(skb)->daddr,
2012 0, IPPROTO_TCP, 0);
2013 (*tpd)->word1 |= 1 << TPD_IPV4_PACKET_SHIFT;
2014 }
2015 }
2016
2017 if (offload_type & SKB_GSO_TCPV6) {
2018 struct atl1c_tpd_ext_desc *etpd =
2019 *(struct atl1c_tpd_ext_desc **)(tpd);
2020
2021 memset(etpd, 0, sizeof(struct atl1c_tpd_ext_desc));
2022 *tpd = atl1c_get_tpd(adapter, type);
2023 ipv6_hdr(skb)->payload_len = 0;
2024 /* check payload == 0 byte ? */
2025 hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
2026 if (unlikely(skb->len == hdr_len)) {
2027 /* only xsum need */
2028 if (netif_msg_tx_queued(adapter))
2029 dev_warn(&pdev->dev,
2030 "IPV6 tso with zero data??\n");
2031 goto check_sum;
2032 } else
2033 tcp_hdr(skb)->check = ~csum_ipv6_magic(
2034 &ipv6_hdr(skb)->saddr,
2035 &ipv6_hdr(skb)->daddr,
2036 0, IPPROTO_TCP, 0);
2037 etpd->word1 |= 1 << TPD_LSO_EN_SHIFT;
2038 etpd->word1 |= 1 << TPD_LSO_VER_SHIFT;
2039 etpd->pkt_len = cpu_to_le32(skb->len);
2040 (*tpd)->word1 |= 1 << TPD_LSO_VER_SHIFT;
2041 }
2042
2043 (*tpd)->word1 |= 1 << TPD_LSO_EN_SHIFT;
2044 (*tpd)->word1 |= (skb_transport_offset(skb) & TPD_TCPHDR_OFFSET_MASK) <<
2045 TPD_TCPHDR_OFFSET_SHIFT;
2046 (*tpd)->word1 |= (skb_shinfo(skb)->gso_size & TPD_MSS_MASK) <<
2047 TPD_MSS_SHIFT;
2048 return 0;
2049 }
2050
2051check_sum:
2052 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2053 u8 css, cso;
Michał Mirosław0d0b1672010-12-14 15:24:08 +00002054 cso = skb_checksum_start_offset(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08002055
2056 if (unlikely(cso & 0x1)) {
2057 if (netif_msg_tx_err(adapter))
2058 dev_err(&adapter->pdev->dev,
2059 "payload offset should not an event number\n");
2060 return -1;
2061 } else {
2062 css = cso + skb->csum_offset;
2063
2064 (*tpd)->word1 |= ((cso >> 1) & TPD_PLOADOFFSET_MASK) <<
2065 TPD_PLOADOFFSET_SHIFT;
2066 (*tpd)->word1 |= ((css >> 1) & TPD_CCSUM_OFFSET_MASK) <<
2067 TPD_CCSUM_OFFSET_SHIFT;
2068 (*tpd)->word1 |= 1 << TPD_CCSUM_EN_SHIFT;
2069 }
2070 }
2071 return 0;
2072}
2073
Huang, Xiongac574802013-02-11 14:44:40 +00002074static void atl1c_tx_rollback(struct atl1c_adapter *adpt,
2075 struct atl1c_tpd_desc *first_tpd,
2076 enum atl1c_trans_queue type)
2077{
2078 struct atl1c_tpd_ring *tpd_ring = &adpt->tpd_ring[type];
2079 struct atl1c_buffer *buffer_info;
2080 struct atl1c_tpd_desc *tpd;
2081 u16 first_index, index;
2082
2083 first_index = first_tpd - (struct atl1c_tpd_desc *)tpd_ring->desc;
2084 index = first_index;
2085 while (index != tpd_ring->next_to_use) {
2086 tpd = ATL1C_TPD_DESC(tpd_ring, index);
2087 buffer_info = &tpd_ring->buffer_info[index];
Eric W. Biederman07641c82014-03-15 15:58:10 -07002088 atl1c_clean_buffer(adpt->pdev, buffer_info);
Huang, Xiongac574802013-02-11 14:44:40 +00002089 memset(tpd, 0, sizeof(struct atl1c_tpd_desc));
2090 if (++index == tpd_ring->count)
2091 index = 0;
2092 }
2093 tpd_ring->next_to_use = first_index;
2094}
2095
2096static int atl1c_tx_map(struct atl1c_adapter *adapter,
Jie Yang43250dd2009-02-18 17:24:15 -08002097 struct sk_buff *skb, struct atl1c_tpd_desc *tpd,
2098 enum atl1c_trans_queue type)
2099{
2100 struct atl1c_tpd_desc *use_tpd = NULL;
2101 struct atl1c_buffer *buffer_info = NULL;
2102 u16 buf_len = skb_headlen(skb);
2103 u16 map_len = 0;
2104 u16 mapped_len = 0;
2105 u16 hdr_len = 0;
2106 u16 nr_frags;
2107 u16 f;
2108 int tso;
2109
2110 nr_frags = skb_shinfo(skb)->nr_frags;
2111 tso = (tpd->word1 >> TPD_LSO_EN_SHIFT) & TPD_LSO_EN_MASK;
2112 if (tso) {
2113 /* TSO */
2114 map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2115 use_tpd = tpd;
2116
2117 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2118 buffer_info->length = map_len;
2119 buffer_info->dma = pci_map_single(adapter->pdev,
2120 skb->data, hdr_len, PCI_DMA_TODEVICE);
Huang, Xiongac574802013-02-11 14:44:40 +00002121 if (unlikely(pci_dma_mapping_error(adapter->pdev,
2122 buffer_info->dma)))
2123 goto err_dma;
Huang, Xiong7cb08d72013-02-19 07:23:09 +00002124 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002125 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2126 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002127 mapped_len += map_len;
2128 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2129 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2130 }
2131
2132 if (mapped_len < buf_len) {
2133 /* mapped_len == 0, means we should use the first tpd,
2134 which is given by caller */
2135 if (mapped_len == 0)
2136 use_tpd = tpd;
2137 else {
2138 use_tpd = atl1c_get_tpd(adapter, type);
2139 memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
Jie Yang43250dd2009-02-18 17:24:15 -08002140 }
2141 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
2142 buffer_info->length = buf_len - mapped_len;
2143 buffer_info->dma =
2144 pci_map_single(adapter->pdev, skb->data + mapped_len,
2145 buffer_info->length, PCI_DMA_TODEVICE);
Huang, Xiongac574802013-02-11 14:44:40 +00002146 if (unlikely(pci_dma_mapping_error(adapter->pdev,
2147 buffer_info->dma)))
2148 goto err_dma;
2149
Jie Yangc6060be2009-11-06 00:32:05 -08002150 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002151 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
2152 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002153 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2154 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2155 }
2156
2157 for (f = 0; f < nr_frags; f++) {
2158 struct skb_frag_struct *frag;
2159
2160 frag = &skb_shinfo(skb)->frags[f];
2161
2162 use_tpd = atl1c_get_tpd(adapter, type);
2163 memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
2164
2165 buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002166 buffer_info->length = skb_frag_size(frag);
Ian Campbell8d1bb862011-08-29 23:18:20 +00002167 buffer_info->dma = skb_frag_dma_map(&adapter->pdev->dev,
2168 frag, 0,
2169 buffer_info->length,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002170 DMA_TO_DEVICE);
Huang, Xiongac574802013-02-11 14:44:40 +00002171 if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma))
2172 goto err_dma;
2173
Jie Yangc6060be2009-11-06 00:32:05 -08002174 ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
Jie Yang4b45e342009-12-06 22:56:59 +00002175 ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_PAGE,
2176 ATL1C_PCIMAP_TODEVICE);
Jie Yang43250dd2009-02-18 17:24:15 -08002177 use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
2178 use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
2179 }
2180
2181 /* The last tpd */
2182 use_tpd->word1 |= 1 << TPD_EOP_SHIFT;
2183 /* The last buffer info contain the skb address,
2184 so it will be free after unmap */
2185 buffer_info->skb = skb;
Huang, Xiongac574802013-02-11 14:44:40 +00002186
2187 return 0;
2188
2189err_dma:
2190 buffer_info->dma = 0;
2191 buffer_info->length = 0;
2192 return -1;
Jie Yang43250dd2009-02-18 17:24:15 -08002193}
2194
2195static void atl1c_tx_queue(struct atl1c_adapter *adapter, struct sk_buff *skb,
2196 struct atl1c_tpd_desc *tpd, enum atl1c_trans_queue type)
2197{
2198 struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
Huang, Xiong0af48332012-04-17 19:32:30 +00002199 u16 reg;
Jie Yang43250dd2009-02-18 17:24:15 -08002200
Huang, Xiong0af48332012-04-17 19:32:30 +00002201 reg = type == atl1c_trans_high ? REG_TPD_PRI1_PIDX : REG_TPD_PRI0_PIDX;
2202 AT_WRITE_REGW(&adapter->hw, reg, tpd_ring->next_to_use);
Jie Yang43250dd2009-02-18 17:24:15 -08002203}
2204
Stephen Hemminger613573252009-08-31 19:50:58 +00002205static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
2206 struct net_device *netdev)
Jie Yang43250dd2009-02-18 17:24:15 -08002207{
2208 struct atl1c_adapter *adapter = netdev_priv(netdev);
2209 unsigned long flags;
2210 u16 tpd_req = 1;
2211 struct atl1c_tpd_desc *tpd;
2212 enum atl1c_trans_queue type = atl1c_trans_normal;
2213
2214 if (test_bit(__AT_DOWN, &adapter->flags)) {
2215 dev_kfree_skb_any(skb);
2216 return NETDEV_TX_OK;
2217 }
2218
2219 tpd_req = atl1c_cal_tpd_req(skb);
2220 if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
2221 if (netif_msg_pktdata(adapter))
2222 dev_info(&adapter->pdev->dev, "tx locked\n");
2223 return NETDEV_TX_LOCKED;
2224 }
Jie Yang43250dd2009-02-18 17:24:15 -08002225
2226 if (atl1c_tpd_avail(adapter, type) < tpd_req) {
2227 /* no enough descriptor, just stop queue */
2228 netif_stop_queue(netdev);
2229 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2230 return NETDEV_TX_BUSY;
2231 }
2232
2233 tpd = atl1c_get_tpd(adapter, type);
2234
2235 /* do TSO and check sum */
2236 if (atl1c_tso_csum(adapter, skb, &tpd, type) != 0) {
2237 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2238 dev_kfree_skb_any(skb);
2239 return NETDEV_TX_OK;
2240 }
2241
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01002242 if (unlikely(skb_vlan_tag_present(skb))) {
2243 u16 vlan = skb_vlan_tag_get(skb);
Jie Yang43250dd2009-02-18 17:24:15 -08002244 __le16 tag;
2245
2246 vlan = cpu_to_le16(vlan);
2247 AT_VLAN_TO_TAG(vlan, tag);
2248 tpd->word1 |= 1 << TPD_INS_VTAG_SHIFT;
2249 tpd->vlan_tag = tag;
2250 }
2251
2252 if (skb_network_offset(skb) != ETH_HLEN)
2253 tpd->word1 |= 1 << TPD_ETH_TYPE_SHIFT; /* Ethernet frame */
2254
Huang, Xiongac574802013-02-11 14:44:40 +00002255 if (atl1c_tx_map(adapter, skb, tpd, type) < 0) {
2256 netif_info(adapter, tx_done, adapter->netdev,
2257 "tx-skb droppted due to dma error\n");
2258 /* roll back tpd/buffer */
2259 atl1c_tx_rollback(adapter, tpd, type);
2260 spin_unlock_irqrestore(&adapter->tx_lock, flags);
Eric W. Biederman07641c82014-03-15 15:58:10 -07002261 dev_kfree_skb_any(skb);
Huang, Xiongac574802013-02-11 14:44:40 +00002262 } else {
Ron Angeles47b344b2015-08-11 23:01:20 -07002263 netdev_sent_queue(adapter->netdev, skb->len);
Huang, Xiongac574802013-02-11 14:44:40 +00002264 atl1c_tx_queue(adapter, skb, tpd, type);
2265 spin_unlock_irqrestore(&adapter->tx_lock, flags);
2266 }
Jie Yang43250dd2009-02-18 17:24:15 -08002267
Jie Yang43250dd2009-02-18 17:24:15 -08002268 return NETDEV_TX_OK;
2269}
2270
2271static void atl1c_free_irq(struct atl1c_adapter *adapter)
2272{
2273 struct net_device *netdev = adapter->netdev;
2274
2275 free_irq(adapter->pdev->irq, netdev);
2276
2277 if (adapter->have_msi)
2278 pci_disable_msi(adapter->pdev);
2279}
2280
2281static int atl1c_request_irq(struct atl1c_adapter *adapter)
2282{
2283 struct pci_dev *pdev = adapter->pdev;
2284 struct net_device *netdev = adapter->netdev;
2285 int flags = 0;
2286 int err = 0;
2287
2288 adapter->have_msi = true;
2289 err = pci_enable_msi(adapter->pdev);
2290 if (err) {
2291 if (netif_msg_ifup(adapter))
2292 dev_err(&pdev->dev,
2293 "Unable to allocate MSI interrupt Error: %d\n",
2294 err);
2295 adapter->have_msi = false;
Francois Romieu93f7fab2012-03-09 19:22:31 +01002296 }
Jie Yang43250dd2009-02-18 17:24:15 -08002297
2298 if (!adapter->have_msi)
2299 flags |= IRQF_SHARED;
Julia Lawall9aff7e92009-11-18 10:47:03 -08002300 err = request_irq(adapter->pdev->irq, atl1c_intr, flags,
Jie Yang43250dd2009-02-18 17:24:15 -08002301 netdev->name, netdev);
2302 if (err) {
2303 if (netif_msg_ifup(adapter))
2304 dev_err(&pdev->dev,
2305 "Unable to allocate interrupt Error: %d\n",
2306 err);
2307 if (adapter->have_msi)
2308 pci_disable_msi(adapter->pdev);
2309 return err;
2310 }
2311 if (netif_msg_ifup(adapter))
2312 dev_dbg(&pdev->dev, "atl1c_request_irq OK\n");
2313 return err;
2314}
2315
Huang, Xiong5e5c0962012-04-30 15:38:56 +00002316
2317static void atl1c_reset_dma_ring(struct atl1c_adapter *adapter)
2318{
2319 /* release tx-pending skbs and reset tx/rx ring index */
2320 atl1c_clean_tx_ring(adapter, atl1c_trans_normal);
2321 atl1c_clean_tx_ring(adapter, atl1c_trans_high);
2322 atl1c_clean_rx_ring(adapter);
2323}
2324
stephen hemminger0fb1e542010-10-21 07:50:49 +00002325static int atl1c_up(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08002326{
2327 struct net_device *netdev = adapter->netdev;
Jie Yang43250dd2009-02-18 17:24:15 -08002328 int err;
Jie Yang43250dd2009-02-18 17:24:15 -08002329
2330 netif_carrier_off(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -08002331
Huang, Xiong5e5c0962012-04-30 15:38:56 +00002332 err = atl1c_configure(adapter);
2333 if (unlikely(err))
Jie Yang43250dd2009-02-18 17:24:15 -08002334 goto err_up;
Jie Yang43250dd2009-02-18 17:24:15 -08002335
2336 err = atl1c_request_irq(adapter);
2337 if (unlikely(err))
2338 goto err_up;
2339
Huang, Xiong5e5c0962012-04-30 15:38:56 +00002340 atl1c_check_link_status(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08002341 clear_bit(__AT_DOWN, &adapter->flags);
2342 napi_enable(&adapter->napi);
2343 atl1c_irq_enable(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08002344 netif_start_queue(netdev);
2345 return err;
2346
2347err_up:
Jie Yang43250dd2009-02-18 17:24:15 -08002348 atl1c_clean_rx_ring(adapter);
2349 return err;
2350}
2351
stephen hemminger0fb1e542010-10-21 07:50:49 +00002352static void atl1c_down(struct atl1c_adapter *adapter)
Jie Yang43250dd2009-02-18 17:24:15 -08002353{
2354 struct net_device *netdev = adapter->netdev;
2355
2356 atl1c_del_timer(adapter);
Jie Yangcb190542009-12-06 23:16:58 +00002357 adapter->work_event = 0; /* clear all event */
Jie Yang43250dd2009-02-18 17:24:15 -08002358 /* signal that we're down so the interrupt handler does not
2359 * reschedule our watchdog timer */
2360 set_bit(__AT_DOWN, &adapter->flags);
2361 netif_carrier_off(netdev);
2362 napi_disable(&adapter->napi);
2363 atl1c_irq_disable(adapter);
2364 atl1c_free_irq(adapter);
Huang, Xiong024e1e42012-04-18 22:01:28 +00002365 /* disable ASPM if device inactive */
2366 atl1c_disable_l0s_l1(&adapter->hw);
Jie Yang43250dd2009-02-18 17:24:15 -08002367 /* reset MAC to disable all RX/TX */
2368 atl1c_reset_mac(&adapter->hw);
2369 msleep(1);
2370
2371 adapter->link_speed = SPEED_0;
2372 adapter->link_duplex = -1;
Huang, Xiong5e5c0962012-04-30 15:38:56 +00002373 atl1c_reset_dma_ring(adapter);
Jie Yang43250dd2009-02-18 17:24:15 -08002374}
2375
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002376/**
Jie Yang43250dd2009-02-18 17:24:15 -08002377 * atl1c_open - Called when a network interface is made active
2378 * @netdev: network interface device structure
2379 *
2380 * Returns 0 on success, negative value on failure
2381 *
2382 * The open entry point is called when a network interface is made
2383 * active by the system (IFF_UP). At this point all resources needed
2384 * for transmit and receive operations are allocated, the interrupt
2385 * handler is registered with the OS, the watchdog timer is started,
2386 * and the stack is notified that the interface is ready.
2387 */
2388static int atl1c_open(struct net_device *netdev)
2389{
2390 struct atl1c_adapter *adapter = netdev_priv(netdev);
2391 int err;
2392
2393 /* disallow open during test */
2394 if (test_bit(__AT_TESTING, &adapter->flags))
2395 return -EBUSY;
2396
2397 /* allocate rx/tx dma buffer & descriptors */
2398 err = atl1c_setup_ring_resources(adapter);
2399 if (unlikely(err))
2400 return err;
2401
2402 err = atl1c_up(adapter);
2403 if (unlikely(err))
2404 goto err_up;
2405
Jie Yang43250dd2009-02-18 17:24:15 -08002406 return 0;
2407
2408err_up:
2409 atl1c_free_irq(adapter);
2410 atl1c_free_ring_resources(adapter);
2411 atl1c_reset_mac(&adapter->hw);
2412 return err;
2413}
2414
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002415/**
Jie Yang43250dd2009-02-18 17:24:15 -08002416 * atl1c_close - Disables a network interface
2417 * @netdev: network interface device structure
2418 *
2419 * Returns 0, this is not allowed to fail
2420 *
2421 * The close entry point is called when an interface is de-activated
2422 * by the OS. The hardware is still under the drivers control, but
2423 * needs to be disabled. A global MAC reset is issued to stop the
2424 * hardware, and all transmit and receive resources are freed.
2425 */
2426static int atl1c_close(struct net_device *netdev)
2427{
2428 struct atl1c_adapter *adapter = netdev_priv(netdev);
2429
2430 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
Huang, Xiong0aa76ce2012-04-30 15:38:55 +00002431 set_bit(__AT_DOWN, &adapter->flags);
2432 cancel_work_sync(&adapter->common_task);
Jie Yang43250dd2009-02-18 17:24:15 -08002433 atl1c_down(adapter);
2434 atl1c_free_ring_resources(adapter);
2435 return 0;
2436}
2437
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002438static int atl1c_suspend(struct device *dev)
Jie Yang43250dd2009-02-18 17:24:15 -08002439{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002440 struct pci_dev *pdev = to_pci_dev(dev);
Jie Yang43250dd2009-02-18 17:24:15 -08002441 struct net_device *netdev = pci_get_drvdata(pdev);
2442 struct atl1c_adapter *adapter = netdev_priv(netdev);
2443 struct atl1c_hw *hw = &adapter->hw;
Jie Yang43250dd2009-02-18 17:24:15 -08002444 u32 wufc = adapter->wol;
Jie Yang43250dd2009-02-18 17:24:15 -08002445
Jie Yang8f574b32010-06-01 00:28:12 -07002446 atl1c_disable_l0s_l1(hw);
Jie Yang43250dd2009-02-18 17:24:15 -08002447 if (netif_running(netdev)) {
2448 WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
2449 atl1c_down(adapter);
2450 }
2451 netif_device_detach(netdev);
Jie Yang43250dd2009-02-18 17:24:15 -08002452
Jie Yang8f574b32010-06-01 00:28:12 -07002453 if (wufc)
Huang, Xiong319d0132012-04-25 20:40:59 +00002454 if (atl1c_phy_to_ps_link(hw) != 0)
Jie Yang8f574b32010-06-01 00:28:12 -07002455 dev_dbg(&pdev->dev, "phy power saving failed");
2456
Huang, Xiong319d0132012-04-25 20:40:59 +00002457 atl1c_power_saving(hw, wufc);
Huang, Xiongce5b9722012-04-25 20:27:14 +00002458
Jie Yang43250dd2009-02-18 17:24:15 -08002459 return 0;
2460}
2461
David S. Millerd187c1a2011-05-19 18:44:41 -04002462#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002463static int atl1c_resume(struct device *dev)
Jie Yang43250dd2009-02-18 17:24:15 -08002464{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002465 struct pci_dev *pdev = to_pci_dev(dev);
Jie Yang43250dd2009-02-18 17:24:15 -08002466 struct net_device *netdev = pci_get_drvdata(pdev);
2467 struct atl1c_adapter *adapter = netdev_priv(netdev);
2468
Jie Yang43250dd2009-02-18 17:24:15 -08002469 AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
Huang, Xiongce3ba0c2012-04-25 20:27:11 +00002470 atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
Jie Yang43250dd2009-02-18 17:24:15 -08002471
2472 atl1c_phy_reset(&adapter->hw);
2473 atl1c_reset_mac(&adapter->hw);
Jie Yang8f574b32010-06-01 00:28:12 -07002474 atl1c_phy_init(&adapter->hw);
2475
2476#if 0
2477 AT_READ_REG(&adapter->hw, REG_PM_CTRLSTAT, &pm_data);
2478 pm_data &= ~PM_CTRLSTAT_PME_EN;
2479 AT_WRITE_REG(&adapter->hw, REG_PM_CTRLSTAT, pm_data);
2480#endif
2481
Jie Yang43250dd2009-02-18 17:24:15 -08002482 netif_device_attach(netdev);
2483 if (netif_running(netdev))
2484 atl1c_up(adapter);
2485
2486 return 0;
2487}
David S. Millerd187c1a2011-05-19 18:44:41 -04002488#endif
Jie Yang43250dd2009-02-18 17:24:15 -08002489
2490static void atl1c_shutdown(struct pci_dev *pdev)
2491{
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002492 struct net_device *netdev = pci_get_drvdata(pdev);
2493 struct atl1c_adapter *adapter = netdev_priv(netdev);
2494
2495 atl1c_suspend(&pdev->dev);
2496 pci_wake_from_d3(pdev, adapter->wol);
2497 pci_set_power_state(pdev, PCI_D3hot);
Jie Yang43250dd2009-02-18 17:24:15 -08002498}
2499
2500static const struct net_device_ops atl1c_netdev_ops = {
2501 .ndo_open = atl1c_open,
2502 .ndo_stop = atl1c_close,
2503 .ndo_validate_addr = eth_validate_addr,
2504 .ndo_start_xmit = atl1c_xmit_frame,
Jiri Pirko46facce2011-07-20 04:54:12 +00002505 .ndo_set_mac_address = atl1c_set_mac_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002506 .ndo_set_rx_mode = atl1c_set_multi,
Jie Yang43250dd2009-02-18 17:24:15 -08002507 .ndo_change_mtu = atl1c_change_mtu,
Michał Mirosław782d6402011-04-07 07:32:18 +00002508 .ndo_fix_features = atl1c_fix_features,
Jiri Pirko46facce2011-07-20 04:54:12 +00002509 .ndo_set_features = atl1c_set_features,
Jie Yang43250dd2009-02-18 17:24:15 -08002510 .ndo_do_ioctl = atl1c_ioctl,
2511 .ndo_tx_timeout = atl1c_tx_timeout,
2512 .ndo_get_stats = atl1c_get_stats,
Jie Yang43250dd2009-02-18 17:24:15 -08002513#ifdef CONFIG_NET_POLL_CONTROLLER
2514 .ndo_poll_controller = atl1c_netpoll,
2515#endif
2516};
2517
2518static int atl1c_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
2519{
2520 SET_NETDEV_DEV(netdev, &pdev->dev);
2521 pci_set_drvdata(pdev, netdev);
2522
Jie Yang43250dd2009-02-18 17:24:15 -08002523 netdev->netdev_ops = &atl1c_netdev_ops;
2524 netdev->watchdog_timeo = AT_TX_WATCHDOG;
2525 atl1c_set_ethtool_ops(netdev);
2526
2527 /* TODO: add when ready */
Patrick McHardyf6469682013-04-19 02:04:27 +00002528 netdev->hw_features = NETIF_F_SG |
2529 NETIF_F_HW_CSUM |
2530 NETIF_F_HW_VLAN_CTAG_RX |
2531 NETIF_F_TSO |
Jie Yang43250dd2009-02-18 17:24:15 -08002532 NETIF_F_TSO6;
Patrick McHardyf6469682013-04-19 02:04:27 +00002533 netdev->features = netdev->hw_features |
2534 NETIF_F_HW_VLAN_CTAG_TX;
Jie Yang43250dd2009-02-18 17:24:15 -08002535 return 0;
2536}
2537
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002538/**
Jie Yang43250dd2009-02-18 17:24:15 -08002539 * atl1c_probe - Device Initialization Routine
2540 * @pdev: PCI device information struct
2541 * @ent: entry in atl1c_pci_tbl
2542 *
2543 * Returns 0 on success, negative on failure
2544 *
2545 * atl1c_probe initializes an adapter identified by a pci_dev structure.
2546 * The OS initialization, configuring of the adapter private structure,
2547 * and a hardware reset occur.
2548 */
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002549static int atl1c_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Jie Yang43250dd2009-02-18 17:24:15 -08002550{
2551 struct net_device *netdev;
2552 struct atl1c_adapter *adapter;
2553 static int cards_found;
2554
2555 int err = 0;
2556
2557 /* enable device (incl. PCI PM wakeup and hotplug setup) */
2558 err = pci_enable_device_mem(pdev);
2559 if (err) {
2560 dev_err(&pdev->dev, "cannot enable PCI device\n");
2561 return err;
2562 }
2563
2564 /*
2565 * The atl1c chip can DMA to 64-bit addresses, but it uses a single
2566 * shared register for the high 32 bits, so only a single, aligned,
2567 * 4 GB physical address range can be used at a time.
2568 *
2569 * Supporting 64-bit DMA on this hardware is more trouble than it's
2570 * worth. It is far easier to limit to 32-bit DMA than update
2571 * various kernel subsystems to support the mechanics required by a
2572 * fixed-high-32-bit system.
2573 */
Yang Hongyange9304382009-04-13 14:40:14 -07002574 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
2575 (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
Jie Yang43250dd2009-02-18 17:24:15 -08002576 dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
2577 goto err_dma;
2578 }
2579
2580 err = pci_request_regions(pdev, atl1c_driver_name);
2581 if (err) {
2582 dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2583 goto err_pci_reg;
2584 }
2585
2586 pci_set_master(pdev);
2587
2588 netdev = alloc_etherdev(sizeof(struct atl1c_adapter));
2589 if (netdev == NULL) {
2590 err = -ENOMEM;
Jie Yang43250dd2009-02-18 17:24:15 -08002591 goto err_alloc_etherdev;
2592 }
2593
2594 err = atl1c_init_netdev(netdev, pdev);
2595 if (err) {
2596 dev_err(&pdev->dev, "init netdevice failed\n");
2597 goto err_init_netdev;
2598 }
2599 adapter = netdev_priv(netdev);
2600 adapter->bd_number = cards_found;
2601 adapter->netdev = netdev;
2602 adapter->pdev = pdev;
2603 adapter->hw.adapter = adapter;
2604 adapter->msg_enable = netif_msg_init(-1, atl1c_default_msg);
2605 adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
2606 if (!adapter->hw.hw_addr) {
2607 err = -EIO;
2608 dev_err(&pdev->dev, "cannot map device registers\n");
2609 goto err_ioremap;
2610 }
Jie Yang43250dd2009-02-18 17:24:15 -08002611
2612 /* init mii data */
2613 adapter->mii.dev = netdev;
2614 adapter->mii.mdio_read = atl1c_mdio_read;
2615 adapter->mii.mdio_write = atl1c_mdio_write;
2616 adapter->mii.phy_id_mask = 0x1f;
Huang, Xiong929a5e92012-04-25 20:27:10 +00002617 adapter->mii.reg_num_mask = MDIO_CTRL_REG_MASK;
Jie Yang43250dd2009-02-18 17:24:15 -08002618 netif_napi_add(netdev, &adapter->napi, atl1c_clean, 64);
2619 setup_timer(&adapter->phy_config_timer, atl1c_phy_config,
2620 (unsigned long)adapter);
2621 /* setup the private structure */
2622 err = atl1c_sw_init(adapter);
2623 if (err) {
2624 dev_err(&pdev->dev, "net device private data init failed\n");
2625 goto err_sw_init;
2626 }
Huang, Xiongce3ba0c2012-04-25 20:27:11 +00002627 atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE);
Jie Yang43250dd2009-02-18 17:24:15 -08002628
2629 /* Init GPHY as early as possible due to power saving issue */
2630 atl1c_phy_reset(&adapter->hw);
2631
2632 err = atl1c_reset_mac(&adapter->hw);
2633 if (err) {
2634 err = -EIO;
2635 goto err_reset;
2636 }
2637
Jie Yang43250dd2009-02-18 17:24:15 -08002638 /* reset the controller to
2639 * put the device in a known good starting state */
2640 err = atl1c_phy_init(&adapter->hw);
2641 if (err) {
2642 err = -EIO;
2643 goto err_reset;
2644 }
Danny Kukawka6a214fd2012-02-17 05:43:30 +00002645 if (atl1c_read_mac_addr(&adapter->hw)) {
2646 /* got a random MAC address, set NET_ADDR_RANDOM to netdev */
Jiri Pirkoe41b2d72013-01-01 03:30:15 +00002647 netdev->addr_assign_type = NET_ADDR_RANDOM;
Jie Yang43250dd2009-02-18 17:24:15 -08002648 }
2649 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
Jie Yang43250dd2009-02-18 17:24:15 -08002650 if (netif_msg_probe(adapter))
hartleys82991172010-01-05 06:54:01 +00002651 dev_dbg(&pdev->dev, "mac address : %pM\n",
2652 adapter->hw.mac_addr);
Jie Yang43250dd2009-02-18 17:24:15 -08002653
Huang, Xiong229e6b62012-04-30 15:38:53 +00002654 atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.mac_addr);
Jie Yangcb190542009-12-06 23:16:58 +00002655 INIT_WORK(&adapter->common_task, atl1c_common_task);
2656 adapter->work_event = 0;
Jie Yang43250dd2009-02-18 17:24:15 -08002657 err = register_netdev(netdev);
2658 if (err) {
2659 dev_err(&pdev->dev, "register netdevice failed\n");
2660 goto err_register;
2661 }
2662
2663 if (netif_msg_probe(adapter))
2664 dev_info(&pdev->dev, "version %s\n", ATL1C_DRV_VERSION);
2665 cards_found++;
2666 return 0;
2667
2668err_reset:
2669err_register:
2670err_sw_init:
Jie Yang43250dd2009-02-18 17:24:15 -08002671 iounmap(adapter->hw.hw_addr);
2672err_init_netdev:
2673err_ioremap:
2674 free_netdev(netdev);
2675err_alloc_etherdev:
2676 pci_release_regions(pdev);
2677err_pci_reg:
2678err_dma:
2679 pci_disable_device(pdev);
2680 return err;
2681}
2682
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002683/**
Jie Yang43250dd2009-02-18 17:24:15 -08002684 * atl1c_remove - Device Removal Routine
2685 * @pdev: PCI device information struct
2686 *
2687 * atl1c_remove is called by the PCI subsystem to alert the driver
2688 * that it should release a PCI device. The could be caused by a
2689 * Hot-Plug event, or because the driver is going to be removed from
2690 * memory.
2691 */
Bill Pemberton093d3692012-12-03 09:23:56 -05002692static void atl1c_remove(struct pci_dev *pdev)
Jie Yang43250dd2009-02-18 17:24:15 -08002693{
2694 struct net_device *netdev = pci_get_drvdata(pdev);
2695 struct atl1c_adapter *adapter = netdev_priv(netdev);
2696
2697 unregister_netdev(netdev);
Huang, Xiong229e6b62012-04-30 15:38:53 +00002698 /* restore permanent address */
2699 atl1c_hw_set_mac_addr(&adapter->hw, adapter->hw.perm_mac_addr);
Jie Yang43250dd2009-02-18 17:24:15 -08002700 atl1c_phy_disable(&adapter->hw);
2701
2702 iounmap(adapter->hw.hw_addr);
2703
2704 pci_release_regions(pdev);
2705 pci_disable_device(pdev);
2706 free_netdev(netdev);
2707}
2708
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002709/**
Jie Yang43250dd2009-02-18 17:24:15 -08002710 * atl1c_io_error_detected - called when PCI error is detected
2711 * @pdev: Pointer to PCI device
2712 * @state: The current pci connection state
2713 *
2714 * This function is called after a PCI bus error affecting
2715 * this device has been detected.
2716 */
2717static pci_ers_result_t atl1c_io_error_detected(struct pci_dev *pdev,
2718 pci_channel_state_t state)
2719{
2720 struct net_device *netdev = pci_get_drvdata(pdev);
2721 struct atl1c_adapter *adapter = netdev_priv(netdev);
2722
2723 netif_device_detach(netdev);
2724
Dean Nelson005fb4f2009-07-31 09:13:02 +00002725 if (state == pci_channel_io_perm_failure)
2726 return PCI_ERS_RESULT_DISCONNECT;
2727
Jie Yang43250dd2009-02-18 17:24:15 -08002728 if (netif_running(netdev))
2729 atl1c_down(adapter);
2730
2731 pci_disable_device(pdev);
2732
2733 /* Request a slot slot reset. */
2734 return PCI_ERS_RESULT_NEED_RESET;
2735}
2736
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002737/**
Jie Yang43250dd2009-02-18 17:24:15 -08002738 * atl1c_io_slot_reset - called after the pci bus has been reset.
2739 * @pdev: Pointer to PCI device
2740 *
2741 * Restart the card from scratch, as if from a cold-boot. Implementation
2742 * resembles the first-half of the e1000_resume routine.
2743 */
2744static pci_ers_result_t atl1c_io_slot_reset(struct pci_dev *pdev)
2745{
2746 struct net_device *netdev = pci_get_drvdata(pdev);
2747 struct atl1c_adapter *adapter = netdev_priv(netdev);
2748
2749 if (pci_enable_device(pdev)) {
2750 if (netif_msg_hw(adapter))
2751 dev_err(&pdev->dev,
2752 "Cannot re-enable PCI device after reset\n");
2753 return PCI_ERS_RESULT_DISCONNECT;
2754 }
2755 pci_set_master(pdev);
2756
2757 pci_enable_wake(pdev, PCI_D3hot, 0);
2758 pci_enable_wake(pdev, PCI_D3cold, 0);
2759
2760 atl1c_reset_mac(&adapter->hw);
2761
2762 return PCI_ERS_RESULT_RECOVERED;
2763}
2764
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002765/**
Jie Yang43250dd2009-02-18 17:24:15 -08002766 * atl1c_io_resume - called when traffic can start flowing again.
2767 * @pdev: Pointer to PCI device
2768 *
2769 * This callback is called when the error recovery driver tells us that
2770 * its OK to resume normal operation. Implementation resembles the
2771 * second-half of the atl1c_resume routine.
2772 */
2773static void atl1c_io_resume(struct pci_dev *pdev)
2774{
2775 struct net_device *netdev = pci_get_drvdata(pdev);
2776 struct atl1c_adapter *adapter = netdev_priv(netdev);
2777
2778 if (netif_running(netdev)) {
2779 if (atl1c_up(adapter)) {
2780 if (netif_msg_hw(adapter))
2781 dev_err(&pdev->dev,
2782 "Cannot bring device back up after reset\n");
2783 return;
2784 }
2785 }
2786
2787 netif_device_attach(netdev);
2788}
2789
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07002790static const struct pci_error_handlers atl1c_err_handler = {
Jie Yang43250dd2009-02-18 17:24:15 -08002791 .error_detected = atl1c_io_error_detected,
2792 .slot_reset = atl1c_io_slot_reset,
2793 .resume = atl1c_io_resume,
2794};
2795
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002796static SIMPLE_DEV_PM_OPS(atl1c_pm_ops, atl1c_suspend, atl1c_resume);
2797
Jie Yang43250dd2009-02-18 17:24:15 -08002798static struct pci_driver atl1c_driver = {
2799 .name = atl1c_driver_name,
2800 .id_table = atl1c_pci_tbl,
2801 .probe = atl1c_probe,
Bill Pemberton093d3692012-12-03 09:23:56 -05002802 .remove = atl1c_remove,
Jie Yang43250dd2009-02-18 17:24:15 -08002803 .shutdown = atl1c_shutdown,
Rafael J. Wysocki762e3022010-12-22 03:07:52 +00002804 .err_handler = &atl1c_err_handler,
2805 .driver.pm = &atl1c_pm_ops,
Jie Yang43250dd2009-02-18 17:24:15 -08002806};
2807
Peter Hüwe2c21d6c2013-05-21 12:42:07 +00002808module_pci_driver(atl1c_driver);