blob: 3e5f533df1fa2b2ab910a4967760b45d5f1c2a2b [file] [log] [blame]
Steve Glendinning2cb37722008-12-11 20:54:30 -08001 /***************************************************************************
2 *
3 * Copyright (C) 2007,2008 SMSC
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Jeff Kirsher0ab75ae2013-12-06 06:28:43 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Steve Glendinning2cb37722008-12-11 20:54:30 -080017 *
18 ***************************************************************************
19 */
20
Joe Perchesacec6d72013-11-05 10:34:21 -080021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Steve Glendinning2cb37722008-12-11 20:54:30 -080024#include <linux/kernel.h>
25#include <linux/netdevice.h>
26#include <linux/phy.h>
27#include <linux/pci.h>
28#include <linux/if_vlan.h>
29#include <linux/dma-mapping.h>
30#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040032#include <linux/module.h>
Steve Glendinning2cb37722008-12-11 20:54:30 -080033#include <asm/unaligned.h>
34#include "smsc9420.h"
35
36#define DRV_NAME "smsc9420"
Steve Glendinning2cb37722008-12-11 20:54:30 -080037#define DRV_MDIONAME "smsc9420-mdio"
38#define DRV_DESCRIPTION "SMSC LAN9420 driver"
39#define DRV_VERSION "1.01"
40
41MODULE_LICENSE("GPL");
42MODULE_VERSION(DRV_VERSION);
43
44struct smsc9420_dma_desc {
45 u32 status;
46 u32 length;
47 u32 buffer1;
48 u32 buffer2;
49};
50
51struct smsc9420_ring_info {
52 struct sk_buff *skb;
53 dma_addr_t mapping;
54};
55
56struct smsc9420_pdata {
Francois Romieub5a80832012-03-09 18:28:59 +010057 void __iomem *ioaddr;
Steve Glendinning2cb37722008-12-11 20:54:30 -080058 struct pci_dev *pdev;
59 struct net_device *dev;
60
61 struct smsc9420_dma_desc *rx_ring;
62 struct smsc9420_dma_desc *tx_ring;
63 struct smsc9420_ring_info *tx_buffers;
64 struct smsc9420_ring_info *rx_buffers;
65 dma_addr_t rx_dma_addr;
66 dma_addr_t tx_dma_addr;
67 int tx_ring_head, tx_ring_tail;
68 int rx_ring_head, rx_ring_tail;
69
70 spinlock_t int_lock;
71 spinlock_t phy_lock;
72
73 struct napi_struct napi;
74
75 bool software_irq_signal;
76 bool rx_csum;
77 u32 msg_enable;
78
Steve Glendinning2cb37722008-12-11 20:54:30 -080079 struct mii_bus *mii_bus;
Steve Glendinning2cb37722008-12-11 20:54:30 -080080 int last_duplex;
81 int last_carrier;
82};
83
Benoit Taine9baa3c32014-08-08 15:56:03 +020084static const struct pci_device_id smsc9420_id_table[] = {
Steve Glendinning2cb37722008-12-11 20:54:30 -080085 { PCI_VENDOR_ID_9420, PCI_DEVICE_ID_9420, PCI_ANY_ID, PCI_ANY_ID, },
86 { 0, }
87};
88
89MODULE_DEVICE_TABLE(pci, smsc9420_id_table);
90
91#define SMSC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
92
93static uint smsc_debug;
94static uint debug = -1;
95module_param(debug, uint, 0);
96MODULE_PARM_DESC(debug, "debug level");
97
Steve Glendinning2cb37722008-12-11 20:54:30 -080098static inline u32 smsc9420_reg_read(struct smsc9420_pdata *pd, u32 offset)
99{
Francois Romieub5a80832012-03-09 18:28:59 +0100100 return ioread32(pd->ioaddr + offset);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800101}
102
103static inline void
104smsc9420_reg_write(struct smsc9420_pdata *pd, u32 offset, u32 value)
105{
Francois Romieub5a80832012-03-09 18:28:59 +0100106 iowrite32(value, pd->ioaddr + offset);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800107}
108
109static inline void smsc9420_pci_flush_write(struct smsc9420_pdata *pd)
110{
111 /* to ensure PCI write completion, we must perform a PCI read */
112 smsc9420_reg_read(pd, ID_REV);
113}
114
115static int smsc9420_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
116{
117 struct smsc9420_pdata *pd = (struct smsc9420_pdata *)bus->priv;
118 unsigned long flags;
119 u32 addr;
120 int i, reg = -EIO;
121
122 spin_lock_irqsave(&pd->phy_lock, flags);
123
124 /* confirm MII not busy */
125 if ((smsc9420_reg_read(pd, MII_ACCESS) & MII_ACCESS_MII_BUSY_)) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800126 netif_warn(pd, drv, pd->dev, "MII is busy???\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800127 goto out;
128 }
129
130 /* set the address, index & direction (read from PHY) */
131 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
132 MII_ACCESS_MII_READ_;
133 smsc9420_reg_write(pd, MII_ACCESS, addr);
134
135 /* wait for read to complete with 50us timeout */
136 for (i = 0; i < 5; i++) {
137 if (!(smsc9420_reg_read(pd, MII_ACCESS) &
138 MII_ACCESS_MII_BUSY_)) {
139 reg = (u16)smsc9420_reg_read(pd, MII_DATA);
140 goto out;
141 }
142 udelay(10);
143 }
144
Joe Perchesacec6d72013-11-05 10:34:21 -0800145 netif_warn(pd, drv, pd->dev, "MII busy timeout!\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800146
147out:
148 spin_unlock_irqrestore(&pd->phy_lock, flags);
149 return reg;
150}
151
152static int smsc9420_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
153 u16 val)
154{
155 struct smsc9420_pdata *pd = (struct smsc9420_pdata *)bus->priv;
156 unsigned long flags;
157 u32 addr;
158 int i, reg = -EIO;
159
160 spin_lock_irqsave(&pd->phy_lock, flags);
161
162 /* confirm MII not busy */
163 if ((smsc9420_reg_read(pd, MII_ACCESS) & MII_ACCESS_MII_BUSY_)) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800164 netif_warn(pd, drv, pd->dev, "MII is busy???\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800165 goto out;
166 }
167
168 /* put the data to write in the MAC */
169 smsc9420_reg_write(pd, MII_DATA, (u32)val);
170
171 /* set the address, index & direction (write to PHY) */
172 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
173 MII_ACCESS_MII_WRITE_;
174 smsc9420_reg_write(pd, MII_ACCESS, addr);
175
176 /* wait for write to complete with 50us timeout */
177 for (i = 0; i < 5; i++) {
178 if (!(smsc9420_reg_read(pd, MII_ACCESS) &
179 MII_ACCESS_MII_BUSY_)) {
180 reg = 0;
181 goto out;
182 }
183 udelay(10);
184 }
185
Joe Perchesacec6d72013-11-05 10:34:21 -0800186 netif_warn(pd, drv, pd->dev, "MII busy timeout!\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800187
188out:
189 spin_unlock_irqrestore(&pd->phy_lock, flags);
190 return reg;
191}
192
193/* Returns hash bit number for given MAC address
194 * Example:
195 * 01 00 5E 00 00 01 -> returns bit number 31 */
196static u32 smsc9420_hash(u8 addr[ETH_ALEN])
197{
198 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
199}
200
201static int smsc9420_eeprom_reload(struct smsc9420_pdata *pd)
202{
203 int timeout = 100000;
204
205 BUG_ON(!pd);
206
207 if (smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800208 netif_dbg(pd, drv, pd->dev, "%s: Eeprom busy\n", __func__);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800209 return -EIO;
210 }
211
212 smsc9420_reg_write(pd, E2P_CMD,
213 (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_RELOAD_));
214
215 do {
216 udelay(10);
217 if (!(smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_))
218 return 0;
219 } while (timeout--);
220
Joe Perchesacec6d72013-11-05 10:34:21 -0800221 netif_warn(pd, drv, pd->dev, "%s: Eeprom timed out\n", __func__);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800222 return -EIO;
223}
224
225/* Standard ioctls for mii-tool */
226static int smsc9420_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
227{
Philippe Reynes5d872c52016-07-15 10:36:20 +0200228 if (!netif_running(dev) || !dev->phydev)
Steve Glendinning2cb37722008-12-11 20:54:30 -0800229 return -EINVAL;
230
Philippe Reynes5d872c52016-07-15 10:36:20 +0200231 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800232}
233
234static int smsc9420_ethtool_get_settings(struct net_device *dev,
235 struct ethtool_cmd *cmd)
236{
Philippe Reynes5d872c52016-07-15 10:36:20 +0200237 if (!dev->phydev)
Steve Glendinning6c53b1b2009-11-29 23:14:45 -0800238 return -ENODEV;
239
Steve Glendinning2cb37722008-12-11 20:54:30 -0800240 cmd->maxtxpkt = 1;
241 cmd->maxrxpkt = 1;
Philippe Reynes5d872c52016-07-15 10:36:20 +0200242 return phy_ethtool_gset(dev->phydev, cmd);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800243}
244
245static int smsc9420_ethtool_set_settings(struct net_device *dev,
246 struct ethtool_cmd *cmd)
247{
Philippe Reynes5d872c52016-07-15 10:36:20 +0200248 if (!dev->phydev)
Steve Glendinning6c53b1b2009-11-29 23:14:45 -0800249 return -ENODEV;
250
Philippe Reynes5d872c52016-07-15 10:36:20 +0200251 return phy_ethtool_sset(dev->phydev, cmd);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800252}
253
254static void smsc9420_ethtool_get_drvinfo(struct net_device *netdev,
255 struct ethtool_drvinfo *drvinfo)
256{
257 struct smsc9420_pdata *pd = netdev_priv(netdev);
258
Rick Jones68aad782011-11-07 13:29:27 +0000259 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
260 strlcpy(drvinfo->bus_info, pci_name(pd->pdev),
261 sizeof(drvinfo->bus_info));
262 strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
Steve Glendinning2cb37722008-12-11 20:54:30 -0800263}
264
265static u32 smsc9420_ethtool_get_msglevel(struct net_device *netdev)
266{
267 struct smsc9420_pdata *pd = netdev_priv(netdev);
268 return pd->msg_enable;
269}
270
271static void smsc9420_ethtool_set_msglevel(struct net_device *netdev, u32 data)
272{
273 struct smsc9420_pdata *pd = netdev_priv(netdev);
274 pd->msg_enable = data;
275}
276
277static int smsc9420_ethtool_nway_reset(struct net_device *netdev)
278{
Philippe Reynes5d872c52016-07-15 10:36:20 +0200279 if (!netdev->phydev)
Steve Glendinning6c53b1b2009-11-29 23:14:45 -0800280 return -ENODEV;
281
Philippe Reynes5d872c52016-07-15 10:36:20 +0200282 return phy_start_aneg(netdev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800283}
284
Steve Glendinninga7276db2008-12-15 00:59:47 -0800285static int smsc9420_ethtool_getregslen(struct net_device *dev)
286{
287 /* all smsc9420 registers plus all phy registers */
288 return 0x100 + (32 * sizeof(u32));
289}
290
291static void
292smsc9420_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
293 void *buf)
294{
295 struct smsc9420_pdata *pd = netdev_priv(dev);
Philippe Reynes5d872c52016-07-15 10:36:20 +0200296 struct phy_device *phy_dev = dev->phydev;
Steve Glendinninga7276db2008-12-15 00:59:47 -0800297 unsigned int i, j = 0;
298 u32 *data = buf;
299
300 regs->version = smsc9420_reg_read(pd, ID_REV);
301 for (i = 0; i < 0x100; i += (sizeof(u32)))
302 data[j++] = smsc9420_reg_read(pd, i);
303
Steve Glendinning6c53b1b2009-11-29 23:14:45 -0800304 // cannot read phy registers if the net device is down
305 if (!phy_dev)
306 return;
307
Steve Glendinninga7276db2008-12-15 00:59:47 -0800308 for (i = 0; i <= 31; i++)
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100309 data[j++] = smsc9420_mii_read(phy_dev->mdio.bus,
310 phy_dev->mdio.addr, i);
Steve Glendinninga7276db2008-12-15 00:59:47 -0800311}
312
Steve Glendinning012b2152008-12-12 22:32:22 -0800313static void smsc9420_eeprom_enable_access(struct smsc9420_pdata *pd)
314{
315 unsigned int temp = smsc9420_reg_read(pd, GPIO_CFG);
316 temp &= ~GPIO_CFG_EEPR_EN_;
317 smsc9420_reg_write(pd, GPIO_CFG, temp);
318 msleep(1);
319}
320
321static int smsc9420_eeprom_send_cmd(struct smsc9420_pdata *pd, u32 op)
322{
323 int timeout = 100;
324 u32 e2cmd;
325
Joe Perchesacec6d72013-11-05 10:34:21 -0800326 netif_dbg(pd, hw, pd->dev, "op 0x%08x\n", op);
Steve Glendinning012b2152008-12-12 22:32:22 -0800327 if (smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800328 netif_warn(pd, hw, pd->dev, "Busy at start\n");
Steve Glendinning012b2152008-12-12 22:32:22 -0800329 return -EBUSY;
330 }
331
332 e2cmd = op | E2P_CMD_EPC_BUSY_;
333 smsc9420_reg_write(pd, E2P_CMD, e2cmd);
334
335 do {
336 msleep(1);
337 e2cmd = smsc9420_reg_read(pd, E2P_CMD);
Steve Glendinning9df8f4e2009-02-16 07:46:06 +0000338 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
Steve Glendinning012b2152008-12-12 22:32:22 -0800339
340 if (!timeout) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800341 netif_info(pd, hw, pd->dev, "TIMED OUT\n");
Steve Glendinning012b2152008-12-12 22:32:22 -0800342 return -EAGAIN;
343 }
344
345 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800346 netif_info(pd, hw, pd->dev,
347 "Error occurred during eeprom operation\n");
Steve Glendinning012b2152008-12-12 22:32:22 -0800348 return -EINVAL;
349 }
350
351 return 0;
352}
353
354static int smsc9420_eeprom_read_location(struct smsc9420_pdata *pd,
355 u8 address, u8 *data)
356{
357 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
358 int ret;
359
Joe Perchesacec6d72013-11-05 10:34:21 -0800360 netif_dbg(pd, hw, pd->dev, "address 0x%x\n", address);
Steve Glendinning012b2152008-12-12 22:32:22 -0800361 ret = smsc9420_eeprom_send_cmd(pd, op);
362
363 if (!ret)
364 data[address] = smsc9420_reg_read(pd, E2P_DATA);
365
366 return ret;
367}
368
369static int smsc9420_eeprom_write_location(struct smsc9420_pdata *pd,
370 u8 address, u8 data)
371{
372 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
373 int ret;
374
Joe Perchesacec6d72013-11-05 10:34:21 -0800375 netif_dbg(pd, hw, pd->dev, "address 0x%x, data 0x%x\n", address, data);
Steve Glendinning012b2152008-12-12 22:32:22 -0800376 ret = smsc9420_eeprom_send_cmd(pd, op);
377
378 if (!ret) {
379 op = E2P_CMD_EPC_CMD_WRITE_ | address;
380 smsc9420_reg_write(pd, E2P_DATA, (u32)data);
381 ret = smsc9420_eeprom_send_cmd(pd, op);
382 }
383
384 return ret;
385}
386
387static int smsc9420_ethtool_get_eeprom_len(struct net_device *dev)
388{
389 return SMSC9420_EEPROM_SIZE;
390}
391
392static int smsc9420_ethtool_get_eeprom(struct net_device *dev,
393 struct ethtool_eeprom *eeprom, u8 *data)
394{
395 struct smsc9420_pdata *pd = netdev_priv(dev);
396 u8 eeprom_data[SMSC9420_EEPROM_SIZE];
397 int len, i;
398
399 smsc9420_eeprom_enable_access(pd);
400
401 len = min(eeprom->len, SMSC9420_EEPROM_SIZE);
402 for (i = 0; i < len; i++) {
403 int ret = smsc9420_eeprom_read_location(pd, i, eeprom_data);
404 if (ret < 0) {
405 eeprom->len = 0;
406 return ret;
407 }
408 }
409
410 memcpy(data, &eeprom_data[eeprom->offset], len);
Steve Glendinning196b7e12009-02-15 22:55:01 +0000411 eeprom->magic = SMSC9420_EEPROM_MAGIC;
Steve Glendinning012b2152008-12-12 22:32:22 -0800412 eeprom->len = len;
413 return 0;
414}
415
416static int smsc9420_ethtool_set_eeprom(struct net_device *dev,
417 struct ethtool_eeprom *eeprom, u8 *data)
418{
419 struct smsc9420_pdata *pd = netdev_priv(dev);
420 int ret;
421
Steve Glendinning196b7e12009-02-15 22:55:01 +0000422 if (eeprom->magic != SMSC9420_EEPROM_MAGIC)
423 return -EINVAL;
424
Steve Glendinning012b2152008-12-12 22:32:22 -0800425 smsc9420_eeprom_enable_access(pd);
426 smsc9420_eeprom_send_cmd(pd, E2P_CMD_EPC_CMD_EWEN_);
427 ret = smsc9420_eeprom_write_location(pd, eeprom->offset, *data);
428 smsc9420_eeprom_send_cmd(pd, E2P_CMD_EPC_CMD_EWDS_);
429
430 /* Single byte write, according to man page */
431 eeprom->len = 1;
432
433 return ret;
434}
435
Steve Glendinning2cb37722008-12-11 20:54:30 -0800436static const struct ethtool_ops smsc9420_ethtool_ops = {
437 .get_settings = smsc9420_ethtool_get_settings,
438 .set_settings = smsc9420_ethtool_set_settings,
439 .get_drvinfo = smsc9420_ethtool_get_drvinfo,
440 .get_msglevel = smsc9420_ethtool_get_msglevel,
441 .set_msglevel = smsc9420_ethtool_set_msglevel,
442 .nway_reset = smsc9420_ethtool_nway_reset,
443 .get_link = ethtool_op_get_link,
Steve Glendinning012b2152008-12-12 22:32:22 -0800444 .get_eeprom_len = smsc9420_ethtool_get_eeprom_len,
445 .get_eeprom = smsc9420_ethtool_get_eeprom,
446 .set_eeprom = smsc9420_ethtool_set_eeprom,
Steve Glendinninga7276db2008-12-15 00:59:47 -0800447 .get_regs_len = smsc9420_ethtool_getregslen,
448 .get_regs = smsc9420_ethtool_getregs,
Richard Cochran50c0c112012-04-03 22:59:37 +0000449 .get_ts_info = ethtool_op_get_ts_info,
Steve Glendinning2cb37722008-12-11 20:54:30 -0800450};
451
452/* Sets the device MAC address to dev_addr */
453static void smsc9420_set_mac_address(struct net_device *dev)
454{
455 struct smsc9420_pdata *pd = netdev_priv(dev);
456 u8 *dev_addr = dev->dev_addr;
457 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
458 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
459 (dev_addr[1] << 8) | dev_addr[0];
460
461 smsc9420_reg_write(pd, ADDRH, mac_high16);
462 smsc9420_reg_write(pd, ADDRL, mac_low32);
463}
464
465static void smsc9420_check_mac_address(struct net_device *dev)
466{
467 struct smsc9420_pdata *pd = netdev_priv(dev);
468
469 /* Check if mac address has been specified when bringing interface up */
470 if (is_valid_ether_addr(dev->dev_addr)) {
471 smsc9420_set_mac_address(dev);
Joe Perchesacec6d72013-11-05 10:34:21 -0800472 netif_dbg(pd, probe, pd->dev,
473 "MAC Address is specified by configuration\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800474 } else {
475 /* Try reading mac address from device. if EEPROM is present
476 * it will already have been set */
477 u32 mac_high16 = smsc9420_reg_read(pd, ADDRH);
478 u32 mac_low32 = smsc9420_reg_read(pd, ADDRL);
479 dev->dev_addr[0] = (u8)(mac_low32);
480 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
481 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
482 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
483 dev->dev_addr[4] = (u8)(mac_high16);
484 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
485
486 if (is_valid_ether_addr(dev->dev_addr)) {
487 /* eeprom values are valid so use them */
Joe Perchesacec6d72013-11-05 10:34:21 -0800488 netif_dbg(pd, probe, pd->dev,
489 "Mac Address is read from EEPROM\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800490 } else {
491 /* eeprom values are invalid, generate random MAC */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000492 eth_hw_addr_random(dev);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800493 smsc9420_set_mac_address(dev);
Joe Perchesacec6d72013-11-05 10:34:21 -0800494 netif_dbg(pd, probe, pd->dev,
495 "MAC Address is set to random\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800496 }
497 }
498}
499
500static void smsc9420_stop_tx(struct smsc9420_pdata *pd)
501{
502 u32 dmac_control, mac_cr, dma_intr_ena;
Roel Kluin46578a692009-02-02 21:39:02 -0800503 int timeout = 1000;
Steve Glendinning2cb37722008-12-11 20:54:30 -0800504
505 /* disable TX DMAC */
506 dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
507 dmac_control &= (~DMAC_CONTROL_ST_);
508 smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
509
510 /* Wait max 10ms for transmit process to stop */
Roel Kluin46578a692009-02-02 21:39:02 -0800511 while (--timeout) {
Steve Glendinning2cb37722008-12-11 20:54:30 -0800512 if (smsc9420_reg_read(pd, DMAC_STATUS) & DMAC_STS_TS_)
513 break;
514 udelay(10);
515 }
516
Roel Kluin46578a692009-02-02 21:39:02 -0800517 if (!timeout)
Joe Perchesacec6d72013-11-05 10:34:21 -0800518 netif_warn(pd, ifdown, pd->dev, "TX DMAC failed to stop\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800519
520 /* ACK Tx DMAC stop bit */
521 smsc9420_reg_write(pd, DMAC_STATUS, DMAC_STS_TXPS_);
522
523 /* mask TX DMAC interrupts */
524 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
525 dma_intr_ena &= ~(DMAC_INTR_ENA_TX_);
526 smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
527 smsc9420_pci_flush_write(pd);
528
529 /* stop MAC TX */
530 mac_cr = smsc9420_reg_read(pd, MAC_CR) & (~MAC_CR_TXEN_);
531 smsc9420_reg_write(pd, MAC_CR, mac_cr);
532 smsc9420_pci_flush_write(pd);
533}
534
535static void smsc9420_free_tx_ring(struct smsc9420_pdata *pd)
536{
537 int i;
538
539 BUG_ON(!pd->tx_ring);
540
541 if (!pd->tx_buffers)
542 return;
543
544 for (i = 0; i < TX_RING_SIZE; i++) {
545 struct sk_buff *skb = pd->tx_buffers[i].skb;
546
547 if (skb) {
548 BUG_ON(!pd->tx_buffers[i].mapping);
549 pci_unmap_single(pd->pdev, pd->tx_buffers[i].mapping,
550 skb->len, PCI_DMA_TODEVICE);
551 dev_kfree_skb_any(skb);
552 }
553
554 pd->tx_ring[i].status = 0;
555 pd->tx_ring[i].length = 0;
556 pd->tx_ring[i].buffer1 = 0;
557 pd->tx_ring[i].buffer2 = 0;
558 }
559 wmb();
560
561 kfree(pd->tx_buffers);
562 pd->tx_buffers = NULL;
563
564 pd->tx_ring_head = 0;
565 pd->tx_ring_tail = 0;
566}
567
568static void smsc9420_free_rx_ring(struct smsc9420_pdata *pd)
569{
570 int i;
571
572 BUG_ON(!pd->rx_ring);
573
574 if (!pd->rx_buffers)
575 return;
576
577 for (i = 0; i < RX_RING_SIZE; i++) {
578 if (pd->rx_buffers[i].skb)
579 dev_kfree_skb_any(pd->rx_buffers[i].skb);
580
581 if (pd->rx_buffers[i].mapping)
582 pci_unmap_single(pd->pdev, pd->rx_buffers[i].mapping,
583 PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
584
585 pd->rx_ring[i].status = 0;
586 pd->rx_ring[i].length = 0;
587 pd->rx_ring[i].buffer1 = 0;
588 pd->rx_ring[i].buffer2 = 0;
589 }
590 wmb();
591
592 kfree(pd->rx_buffers);
593 pd->rx_buffers = NULL;
594
595 pd->rx_ring_head = 0;
596 pd->rx_ring_tail = 0;
597}
598
599static void smsc9420_stop_rx(struct smsc9420_pdata *pd)
600{
Roel Kluin46578a692009-02-02 21:39:02 -0800601 int timeout = 1000;
Steve Glendinning2cb37722008-12-11 20:54:30 -0800602 u32 mac_cr, dmac_control, dma_intr_ena;
603
604 /* mask RX DMAC interrupts */
605 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
606 dma_intr_ena &= (~DMAC_INTR_ENA_RX_);
607 smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
608 smsc9420_pci_flush_write(pd);
609
610 /* stop RX MAC prior to stoping DMA */
611 mac_cr = smsc9420_reg_read(pd, MAC_CR) & (~MAC_CR_RXEN_);
612 smsc9420_reg_write(pd, MAC_CR, mac_cr);
613 smsc9420_pci_flush_write(pd);
614
615 /* stop RX DMAC */
616 dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
617 dmac_control &= (~DMAC_CONTROL_SR_);
618 smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
619 smsc9420_pci_flush_write(pd);
620
621 /* wait up to 10ms for receive to stop */
Roel Kluin46578a692009-02-02 21:39:02 -0800622 while (--timeout) {
Steve Glendinning2cb37722008-12-11 20:54:30 -0800623 if (smsc9420_reg_read(pd, DMAC_STATUS) & DMAC_STS_RS_)
624 break;
625 udelay(10);
626 }
627
Roel Kluin46578a692009-02-02 21:39:02 -0800628 if (!timeout)
Joe Perchesacec6d72013-11-05 10:34:21 -0800629 netif_warn(pd, ifdown, pd->dev,
630 "RX DMAC did not stop! timeout\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800631
632 /* ACK the Rx DMAC stop bit */
633 smsc9420_reg_write(pd, DMAC_STATUS, DMAC_STS_RXPS_);
634}
635
636static irqreturn_t smsc9420_isr(int irq, void *dev_id)
637{
638 struct smsc9420_pdata *pd = dev_id;
639 u32 int_cfg, int_sts, int_ctl;
640 irqreturn_t ret = IRQ_NONE;
641 ulong flags;
642
643 BUG_ON(!pd);
Francois Romieub5a80832012-03-09 18:28:59 +0100644 BUG_ON(!pd->ioaddr);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800645
646 int_cfg = smsc9420_reg_read(pd, INT_CFG);
647
648 /* check if it's our interrupt */
649 if ((int_cfg & (INT_CFG_IRQ_EN_ | INT_CFG_IRQ_INT_)) !=
650 (INT_CFG_IRQ_EN_ | INT_CFG_IRQ_INT_))
651 return IRQ_NONE;
652
653 int_sts = smsc9420_reg_read(pd, INT_STAT);
654
655 if (likely(INT_STAT_DMAC_INT_ & int_sts)) {
656 u32 status = smsc9420_reg_read(pd, DMAC_STATUS);
657 u32 ints_to_clear = 0;
658
659 if (status & DMAC_STS_TX_) {
660 ints_to_clear |= (DMAC_STS_TX_ | DMAC_STS_NIS_);
661 netif_wake_queue(pd->dev);
662 }
663
664 if (status & DMAC_STS_RX_) {
665 /* mask RX DMAC interrupts */
666 u32 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
667 dma_intr_ena &= (~DMAC_INTR_ENA_RX_);
668 smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
669 smsc9420_pci_flush_write(pd);
670
671 ints_to_clear |= (DMAC_STS_RX_ | DMAC_STS_NIS_);
Ben Hutchings288379f2009-01-19 16:43:59 -0800672 napi_schedule(&pd->napi);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800673 }
674
675 if (ints_to_clear)
676 smsc9420_reg_write(pd, DMAC_STATUS, ints_to_clear);
677
678 ret = IRQ_HANDLED;
679 }
680
681 if (unlikely(INT_STAT_SW_INT_ & int_sts)) {
682 /* mask software interrupt */
683 spin_lock_irqsave(&pd->int_lock, flags);
684 int_ctl = smsc9420_reg_read(pd, INT_CTL);
685 int_ctl &= (~INT_CTL_SW_INT_EN_);
686 smsc9420_reg_write(pd, INT_CTL, int_ctl);
687 spin_unlock_irqrestore(&pd->int_lock, flags);
688
689 smsc9420_reg_write(pd, INT_STAT, INT_STAT_SW_INT_);
690 pd->software_irq_signal = true;
691 smp_wmb();
692
693 ret = IRQ_HANDLED;
694 }
695
696 /* to ensure PCI write completion, we must perform a PCI read */
697 smsc9420_pci_flush_write(pd);
698
699 return ret;
700}
701
Steve Glendinninge3126742008-12-12 22:31:50 -0800702#ifdef CONFIG_NET_POLL_CONTROLLER
703static void smsc9420_poll_controller(struct net_device *dev)
704{
Francois Romieub5a80832012-03-09 18:28:59 +0100705 struct smsc9420_pdata *pd = netdev_priv(dev);
706 const int irq = pd->pdev->irq;
707
708 disable_irq(irq);
Steve Glendinninge3126742008-12-12 22:31:50 -0800709 smsc9420_isr(0, dev);
Francois Romieub5a80832012-03-09 18:28:59 +0100710 enable_irq(irq);
Steve Glendinninge3126742008-12-12 22:31:50 -0800711}
712#endif /* CONFIG_NET_POLL_CONTROLLER */
713
Steve Glendinning2cb37722008-12-11 20:54:30 -0800714static void smsc9420_dmac_soft_reset(struct smsc9420_pdata *pd)
715{
716 smsc9420_reg_write(pd, BUS_MODE, BUS_MODE_SWR_);
717 smsc9420_reg_read(pd, BUS_MODE);
718 udelay(2);
719 if (smsc9420_reg_read(pd, BUS_MODE) & BUS_MODE_SWR_)
Joe Perchesacec6d72013-11-05 10:34:21 -0800720 netif_warn(pd, drv, pd->dev, "Software reset not cleared\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800721}
722
723static int smsc9420_stop(struct net_device *dev)
724{
725 struct smsc9420_pdata *pd = netdev_priv(dev);
726 u32 int_cfg;
727 ulong flags;
728
729 BUG_ON(!pd);
Philippe Reynes5d872c52016-07-15 10:36:20 +0200730 BUG_ON(!dev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800731
732 /* disable master interrupt */
733 spin_lock_irqsave(&pd->int_lock, flags);
734 int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
735 smsc9420_reg_write(pd, INT_CFG, int_cfg);
736 spin_unlock_irqrestore(&pd->int_lock, flags);
737
738 netif_tx_disable(dev);
739 napi_disable(&pd->napi);
740
741 smsc9420_stop_tx(pd);
742 smsc9420_free_tx_ring(pd);
743
744 smsc9420_stop_rx(pd);
745 smsc9420_free_rx_ring(pd);
746
Francois Romieub5a80832012-03-09 18:28:59 +0100747 free_irq(pd->pdev->irq, pd);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800748
749 smsc9420_dmac_soft_reset(pd);
750
Philippe Reynes5d872c52016-07-15 10:36:20 +0200751 phy_stop(dev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800752
Philippe Reynes5d872c52016-07-15 10:36:20 +0200753 phy_disconnect(dev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800754 mdiobus_unregister(pd->mii_bus);
755 mdiobus_free(pd->mii_bus);
756
757 return 0;
758}
759
760static void smsc9420_rx_count_stats(struct net_device *dev, u32 desc_status)
761{
762 if (unlikely(desc_status & RDES0_ERROR_SUMMARY_)) {
763 dev->stats.rx_errors++;
764 if (desc_status & RDES0_DESCRIPTOR_ERROR_)
765 dev->stats.rx_over_errors++;
766 else if (desc_status & (RDES0_FRAME_TOO_LONG_ |
767 RDES0_RUNT_FRAME_ | RDES0_COLLISION_SEEN_))
768 dev->stats.rx_frame_errors++;
769 else if (desc_status & RDES0_CRC_ERROR_)
770 dev->stats.rx_crc_errors++;
771 }
772
773 if (unlikely(desc_status & RDES0_LENGTH_ERROR_))
774 dev->stats.rx_length_errors++;
775
776 if (unlikely(!((desc_status & RDES0_LAST_DESCRIPTOR_) &&
777 (desc_status & RDES0_FIRST_DESCRIPTOR_))))
778 dev->stats.rx_length_errors++;
779
780 if (desc_status & RDES0_MULTICAST_FRAME_)
781 dev->stats.multicast++;
782}
783
784static void smsc9420_rx_handoff(struct smsc9420_pdata *pd, const int index,
785 const u32 status)
786{
787 struct net_device *dev = pd->dev;
788 struct sk_buff *skb;
789 u16 packet_length = (status & RDES0_FRAME_LENGTH_MASK_)
790 >> RDES0_FRAME_LENGTH_SHFT_;
791
792 /* remove crc from packet lendth */
793 packet_length -= 4;
794
795 if (pd->rx_csum)
796 packet_length -= 2;
797
798 dev->stats.rx_packets++;
799 dev->stats.rx_bytes += packet_length;
800
801 pci_unmap_single(pd->pdev, pd->rx_buffers[index].mapping,
802 PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
803 pd->rx_buffers[index].mapping = 0;
804
805 skb = pd->rx_buffers[index].skb;
806 pd->rx_buffers[index].skb = NULL;
807
808 if (pd->rx_csum) {
809 u16 hw_csum = get_unaligned_le16(skb_tail_pointer(skb) +
810 NET_IP_ALIGN + packet_length + 4);
Steve Glendinningcd7a3b72009-03-20 01:14:53 -0700811 put_unaligned_le16(hw_csum, &skb->csum);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800812 skb->ip_summed = CHECKSUM_COMPLETE;
813 }
814
815 skb_reserve(skb, NET_IP_ALIGN);
816 skb_put(skb, packet_length);
817
818 skb->protocol = eth_type_trans(skb, dev);
819
820 netif_receive_skb(skb);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800821}
822
823static int smsc9420_alloc_rx_buffer(struct smsc9420_pdata *pd, int index)
824{
825 struct sk_buff *skb = netdev_alloc_skb(pd->dev, PKT_BUF_SZ);
826 dma_addr_t mapping;
827
828 BUG_ON(pd->rx_buffers[index].skb);
829 BUG_ON(pd->rx_buffers[index].mapping);
830
Joe Perches720a43e2013-03-08 15:03:25 +0000831 if (unlikely(!skb))
Steve Glendinning2cb37722008-12-11 20:54:30 -0800832 return -ENOMEM;
Steve Glendinning2cb37722008-12-11 20:54:30 -0800833
Steve Glendinning2cb37722008-12-11 20:54:30 -0800834 mapping = pci_map_single(pd->pdev, skb_tail_pointer(skb),
835 PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
836 if (pci_dma_mapping_error(pd->pdev, mapping)) {
837 dev_kfree_skb_any(skb);
Joe Perchesacec6d72013-11-05 10:34:21 -0800838 netif_warn(pd, rx_err, pd->dev, "pci_map_single failed!\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800839 return -ENOMEM;
840 }
841
842 pd->rx_buffers[index].skb = skb;
843 pd->rx_buffers[index].mapping = mapping;
844 pd->rx_ring[index].buffer1 = mapping + NET_IP_ALIGN;
845 pd->rx_ring[index].status = RDES0_OWN_;
846 wmb();
847
848 return 0;
849}
850
851static void smsc9420_alloc_new_rx_buffers(struct smsc9420_pdata *pd)
852{
853 while (pd->rx_ring_tail != pd->rx_ring_head) {
854 if (smsc9420_alloc_rx_buffer(pd, pd->rx_ring_tail))
855 break;
856
857 pd->rx_ring_tail = (pd->rx_ring_tail + 1) % RX_RING_SIZE;
858 }
859}
860
861static int smsc9420_rx_poll(struct napi_struct *napi, int budget)
862{
863 struct smsc9420_pdata *pd =
864 container_of(napi, struct smsc9420_pdata, napi);
865 struct net_device *dev = pd->dev;
866 u32 drop_frame_cnt, dma_intr_ena, status;
867 int work_done;
868
869 for (work_done = 0; work_done < budget; work_done++) {
870 rmb();
871 status = pd->rx_ring[pd->rx_ring_head].status;
872
873 /* stop if DMAC owns this dma descriptor */
874 if (status & RDES0_OWN_)
875 break;
876
877 smsc9420_rx_count_stats(dev, status);
878 smsc9420_rx_handoff(pd, pd->rx_ring_head, status);
879 pd->rx_ring_head = (pd->rx_ring_head + 1) % RX_RING_SIZE;
880 smsc9420_alloc_new_rx_buffers(pd);
881 }
882
883 drop_frame_cnt = smsc9420_reg_read(pd, MISS_FRAME_CNTR);
884 dev->stats.rx_dropped +=
885 (drop_frame_cnt & 0xFFFF) + ((drop_frame_cnt >> 17) & 0x3FF);
886
887 /* Kick RXDMA */
888 smsc9420_reg_write(pd, RX_POLL_DEMAND, 1);
889 smsc9420_pci_flush_write(pd);
890
891 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800892 napi_complete(&pd->napi);
Steve Glendinning2cb37722008-12-11 20:54:30 -0800893
894 /* re-enable RX DMA interrupts */
895 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
896 dma_intr_ena |= (DMAC_INTR_ENA_RX_ | DMAC_INTR_ENA_NIS_);
897 smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
898 smsc9420_pci_flush_write(pd);
899 }
900 return work_done;
901}
902
903static void
904smsc9420_tx_update_stats(struct net_device *dev, u32 status, u32 length)
905{
906 if (unlikely(status & TDES0_ERROR_SUMMARY_)) {
907 dev->stats.tx_errors++;
908 if (status & (TDES0_EXCESSIVE_DEFERRAL_ |
909 TDES0_EXCESSIVE_COLLISIONS_))
910 dev->stats.tx_aborted_errors++;
911
912 if (status & (TDES0_LOSS_OF_CARRIER_ | TDES0_NO_CARRIER_))
913 dev->stats.tx_carrier_errors++;
914 } else {
915 dev->stats.tx_packets++;
916 dev->stats.tx_bytes += (length & 0x7FF);
917 }
918
919 if (unlikely(status & TDES0_EXCESSIVE_COLLISIONS_)) {
920 dev->stats.collisions += 16;
921 } else {
922 dev->stats.collisions +=
923 (status & TDES0_COLLISION_COUNT_MASK_) >>
924 TDES0_COLLISION_COUNT_SHFT_;
925 }
926
927 if (unlikely(status & TDES0_HEARTBEAT_FAIL_))
928 dev->stats.tx_heartbeat_errors++;
929}
930
931/* Check for completed dma transfers, update stats and free skbs */
932static void smsc9420_complete_tx(struct net_device *dev)
933{
934 struct smsc9420_pdata *pd = netdev_priv(dev);
935
936 while (pd->tx_ring_tail != pd->tx_ring_head) {
937 int index = pd->tx_ring_tail;
938 u32 status, length;
939
940 rmb();
941 status = pd->tx_ring[index].status;
942 length = pd->tx_ring[index].length;
943
944 /* Check if DMA still owns this descriptor */
945 if (unlikely(TDES0_OWN_ & status))
946 break;
947
948 smsc9420_tx_update_stats(dev, status, length);
949
950 BUG_ON(!pd->tx_buffers[index].skb);
951 BUG_ON(!pd->tx_buffers[index].mapping);
952
953 pci_unmap_single(pd->pdev, pd->tx_buffers[index].mapping,
954 pd->tx_buffers[index].skb->len, PCI_DMA_TODEVICE);
955 pd->tx_buffers[index].mapping = 0;
956
957 dev_kfree_skb_any(pd->tx_buffers[index].skb);
958 pd->tx_buffers[index].skb = NULL;
959
960 pd->tx_ring[index].buffer1 = 0;
961 wmb();
962
963 pd->tx_ring_tail = (pd->tx_ring_tail + 1) % TX_RING_SIZE;
964 }
965}
966
Stephen Hemminger613573252009-08-31 19:50:58 +0000967static netdev_tx_t smsc9420_hard_start_xmit(struct sk_buff *skb,
968 struct net_device *dev)
Steve Glendinning2cb37722008-12-11 20:54:30 -0800969{
970 struct smsc9420_pdata *pd = netdev_priv(dev);
971 dma_addr_t mapping;
972 int index = pd->tx_ring_head;
973 u32 tmp_desc1;
974 bool about_to_take_last_desc =
975 (((pd->tx_ring_head + 2) % TX_RING_SIZE) == pd->tx_ring_tail);
976
977 smsc9420_complete_tx(dev);
978
979 rmb();
980 BUG_ON(pd->tx_ring[index].status & TDES0_OWN_);
981 BUG_ON(pd->tx_buffers[index].skb);
982 BUG_ON(pd->tx_buffers[index].mapping);
983
984 mapping = pci_map_single(pd->pdev, skb->data,
985 skb->len, PCI_DMA_TODEVICE);
986 if (pci_dma_mapping_error(pd->pdev, mapping)) {
Joe Perchesacec6d72013-11-05 10:34:21 -0800987 netif_warn(pd, tx_err, pd->dev,
988 "pci_map_single failed, dropping packet\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -0800989 return NETDEV_TX_BUSY;
990 }
991
992 pd->tx_buffers[index].skb = skb;
993 pd->tx_buffers[index].mapping = mapping;
994
995 tmp_desc1 = (TDES1_LS_ | ((u32)skb->len & 0x7FF));
996 if (unlikely(about_to_take_last_desc)) {
997 tmp_desc1 |= TDES1_IC_;
998 netif_stop_queue(pd->dev);
999 }
1000
1001 /* check if we are at the last descriptor and need to set EOR */
1002 if (unlikely(index == (TX_RING_SIZE - 1)))
1003 tmp_desc1 |= TDES1_TER_;
1004
1005 pd->tx_ring[index].buffer1 = mapping;
1006 pd->tx_ring[index].length = tmp_desc1;
1007 wmb();
1008
1009 /* increment head */
1010 pd->tx_ring_head = (pd->tx_ring_head + 1) % TX_RING_SIZE;
1011
1012 /* assign ownership to DMAC */
1013 pd->tx_ring[index].status = TDES0_OWN_;
1014 wmb();
1015
Richard Cochran62412072011-06-19 03:31:44 +00001016 skb_tx_timestamp(skb);
1017
Steve Glendinning2cb37722008-12-11 20:54:30 -08001018 /* kick the DMA */
1019 smsc9420_reg_write(pd, TX_POLL_DEMAND, 1);
1020 smsc9420_pci_flush_write(pd);
1021
Steve Glendinning2cb37722008-12-11 20:54:30 -08001022 return NETDEV_TX_OK;
1023}
1024
1025static struct net_device_stats *smsc9420_get_stats(struct net_device *dev)
1026{
1027 struct smsc9420_pdata *pd = netdev_priv(dev);
1028 u32 counter = smsc9420_reg_read(pd, MISS_FRAME_CNTR);
1029 dev->stats.rx_dropped +=
1030 (counter & 0x0000FFFF) + ((counter >> 17) & 0x000003FF);
1031 return &dev->stats;
1032}
1033
1034static void smsc9420_set_multicast_list(struct net_device *dev)
1035{
1036 struct smsc9420_pdata *pd = netdev_priv(dev);
1037 u32 mac_cr = smsc9420_reg_read(pd, MAC_CR);
1038
1039 if (dev->flags & IFF_PROMISC) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001040 netif_dbg(pd, hw, pd->dev, "Promiscuous Mode Enabled\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001041 mac_cr |= MAC_CR_PRMS_;
1042 mac_cr &= (~MAC_CR_MCPAS_);
1043 mac_cr &= (~MAC_CR_HPFILT_);
1044 } else if (dev->flags & IFF_ALLMULTI) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001045 netif_dbg(pd, hw, pd->dev, "Receive all Multicast Enabled\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001046 mac_cr &= (~MAC_CR_PRMS_);
1047 mac_cr |= MAC_CR_MCPAS_;
1048 mac_cr &= (~MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001049 } else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001050 struct netdev_hw_addr *ha;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001051 u32 hash_lo = 0, hash_hi = 0;
1052
Joe Perchesacec6d72013-11-05 10:34:21 -08001053 netif_dbg(pd, hw, pd->dev, "Multicast filter enabled\n");
Jiri Pirko22bedad32010-04-01 21:22:57 +00001054 netdev_for_each_mc_addr(ha, dev) {
1055 u32 bit_num = smsc9420_hash(ha->addr);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001056 u32 mask = 1 << (bit_num & 0x1F);
1057
1058 if (bit_num & 0x20)
1059 hash_hi |= mask;
1060 else
1061 hash_lo |= mask;
1062
Steve Glendinning2cb37722008-12-11 20:54:30 -08001063 }
1064 smsc9420_reg_write(pd, HASHH, hash_hi);
1065 smsc9420_reg_write(pd, HASHL, hash_lo);
1066
1067 mac_cr &= (~MAC_CR_PRMS_);
1068 mac_cr &= (~MAC_CR_MCPAS_);
1069 mac_cr |= MAC_CR_HPFILT_;
1070 } else {
Joe Perchesacec6d72013-11-05 10:34:21 -08001071 netif_dbg(pd, hw, pd->dev, "Receive own packets only\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001072 smsc9420_reg_write(pd, HASHH, 0);
1073 smsc9420_reg_write(pd, HASHL, 0);
1074
1075 mac_cr &= (~MAC_CR_PRMS_);
1076 mac_cr &= (~MAC_CR_MCPAS_);
1077 mac_cr &= (~MAC_CR_HPFILT_);
1078 }
1079
1080 smsc9420_reg_write(pd, MAC_CR, mac_cr);
1081 smsc9420_pci_flush_write(pd);
1082}
1083
Steve Glendinning2cb37722008-12-11 20:54:30 -08001084static void smsc9420_phy_update_flowcontrol(struct smsc9420_pdata *pd)
1085{
Philippe Reynes5d872c52016-07-15 10:36:20 +02001086 struct net_device *dev = pd->dev;
1087 struct phy_device *phy_dev = dev->phydev;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001088 u32 flow;
1089
1090 if (phy_dev->duplex == DUPLEX_FULL) {
1091 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
1092 u16 rmtadv = phy_read(phy_dev, MII_LPA);
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001093 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001094
1095 if (cap & FLOW_CTRL_RX)
1096 flow = 0xFFFF0002;
1097 else
1098 flow = 0;
1099
Joe Perchesacec6d72013-11-05 10:34:21 -08001100 netif_info(pd, link, pd->dev, "rx pause %s, tx pause %s\n",
1101 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
1102 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001103 } else {
Joe Perchesacec6d72013-11-05 10:34:21 -08001104 netif_info(pd, link, pd->dev, "half duplex\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001105 flow = 0;
1106 }
1107
1108 smsc9420_reg_write(pd, FLOW, flow);
1109}
1110
1111/* Update link mode if anything has changed. Called periodically when the
1112 * PHY is in polling mode, even if nothing has changed. */
1113static void smsc9420_phy_adjust_link(struct net_device *dev)
1114{
1115 struct smsc9420_pdata *pd = netdev_priv(dev);
Philippe Reynes5d872c52016-07-15 10:36:20 +02001116 struct phy_device *phy_dev = dev->phydev;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001117 int carrier;
1118
1119 if (phy_dev->duplex != pd->last_duplex) {
1120 u32 mac_cr = smsc9420_reg_read(pd, MAC_CR);
1121 if (phy_dev->duplex) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001122 netif_dbg(pd, link, pd->dev, "full duplex mode\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001123 mac_cr |= MAC_CR_FDPX_;
1124 } else {
Joe Perchesacec6d72013-11-05 10:34:21 -08001125 netif_dbg(pd, link, pd->dev, "half duplex mode\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001126 mac_cr &= ~MAC_CR_FDPX_;
1127 }
1128 smsc9420_reg_write(pd, MAC_CR, mac_cr);
1129
1130 smsc9420_phy_update_flowcontrol(pd);
1131 pd->last_duplex = phy_dev->duplex;
1132 }
1133
1134 carrier = netif_carrier_ok(dev);
1135 if (carrier != pd->last_carrier) {
1136 if (carrier)
Joe Perchesacec6d72013-11-05 10:34:21 -08001137 netif_dbg(pd, link, pd->dev, "carrier OK\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001138 else
Joe Perchesacec6d72013-11-05 10:34:21 -08001139 netif_dbg(pd, link, pd->dev, "no carrier\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001140 pd->last_carrier = carrier;
1141 }
1142}
1143
1144static int smsc9420_mii_probe(struct net_device *dev)
1145{
1146 struct smsc9420_pdata *pd = netdev_priv(dev);
1147 struct phy_device *phydev = NULL;
1148
Philippe Reynes5d872c52016-07-15 10:36:20 +02001149 BUG_ON(dev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001150
1151 /* Device only supports internal PHY at address 1 */
Andrew Lunn7f854422016-01-06 20:11:18 +01001152 phydev = mdiobus_get_phy(pd->mii_bus, 1);
1153 if (!phydev) {
Ben Boeckel48005992013-11-01 08:53:36 -04001154 netdev_err(dev, "no PHY found at address 1\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001155 return -ENODEV;
1156 }
1157
Andrew Lunn84eff6d2016-01-06 20:11:10 +01001158 phydev = phy_connect(dev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +00001159 smsc9420_phy_adjust_link, PHY_INTERFACE_MODE_MII);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001160
1161 if (IS_ERR(phydev)) {
Ben Boeckel48005992013-11-01 08:53:36 -04001162 netdev_err(dev, "Could not attach to PHY\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001163 return PTR_ERR(phydev);
1164 }
1165
Steve Glendinning2cb37722008-12-11 20:54:30 -08001166 /* mask with MAC supported features */
1167 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
1168 SUPPORTED_Asym_Pause);
1169 phydev->advertising = phydev->supported;
1170
Andrew Lunn22209432016-01-06 20:11:13 +01001171 phy_attached_info(phydev);
1172
Steve Glendinning2cb37722008-12-11 20:54:30 -08001173 pd->last_duplex = -1;
1174 pd->last_carrier = -1;
1175
1176 return 0;
1177}
1178
1179static int smsc9420_mii_init(struct net_device *dev)
1180{
1181 struct smsc9420_pdata *pd = netdev_priv(dev);
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001182 int err = -ENXIO;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001183
1184 pd->mii_bus = mdiobus_alloc();
1185 if (!pd->mii_bus) {
1186 err = -ENOMEM;
1187 goto err_out_1;
1188 }
1189 pd->mii_bus->name = DRV_MDIONAME;
1190 snprintf(pd->mii_bus->id, MII_BUS_ID_SIZE, "%x",
1191 (pd->pdev->bus->number << 8) | pd->pdev->devfn);
1192 pd->mii_bus->priv = pd;
1193 pd->mii_bus->read = smsc9420_mii_read;
1194 pd->mii_bus->write = smsc9420_mii_write;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001195
1196 /* Mask all PHYs except ID 1 (internal) */
1197 pd->mii_bus->phy_mask = ~(1 << 1);
1198
1199 if (mdiobus_register(pd->mii_bus)) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001200 netif_warn(pd, probe, pd->dev, "Error registering mii bus\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001201 goto err_out_free_bus_2;
1202 }
1203
1204 if (smsc9420_mii_probe(dev) < 0) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001205 netif_warn(pd, probe, pd->dev, "Error probing mii bus\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001206 goto err_out_unregister_bus_3;
1207 }
1208
1209 return 0;
1210
1211err_out_unregister_bus_3:
1212 mdiobus_unregister(pd->mii_bus);
1213err_out_free_bus_2:
1214 mdiobus_free(pd->mii_bus);
1215err_out_1:
1216 return err;
1217}
1218
1219static int smsc9420_alloc_tx_ring(struct smsc9420_pdata *pd)
1220{
1221 int i;
1222
1223 BUG_ON(!pd->tx_ring);
1224
Joe Perches14f8dc42013-02-07 11:46:27 +00001225 pd->tx_buffers = kmalloc_array(TX_RING_SIZE,
1226 sizeof(struct smsc9420_ring_info),
1227 GFP_KERNEL);
1228 if (!pd->tx_buffers)
Steve Glendinning2cb37722008-12-11 20:54:30 -08001229 return -ENOMEM;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001230
1231 /* Initialize the TX Ring */
1232 for (i = 0; i < TX_RING_SIZE; i++) {
1233 pd->tx_buffers[i].skb = NULL;
1234 pd->tx_buffers[i].mapping = 0;
1235 pd->tx_ring[i].status = 0;
1236 pd->tx_ring[i].length = 0;
1237 pd->tx_ring[i].buffer1 = 0;
1238 pd->tx_ring[i].buffer2 = 0;
1239 }
1240 pd->tx_ring[TX_RING_SIZE - 1].length = TDES1_TER_;
1241 wmb();
1242
1243 pd->tx_ring_head = 0;
1244 pd->tx_ring_tail = 0;
1245
1246 smsc9420_reg_write(pd, TX_BASE_ADDR, pd->tx_dma_addr);
1247 smsc9420_pci_flush_write(pd);
1248
1249 return 0;
1250}
1251
1252static int smsc9420_alloc_rx_ring(struct smsc9420_pdata *pd)
1253{
1254 int i;
1255
1256 BUG_ON(!pd->rx_ring);
1257
Joe Perchesacec6d72013-11-05 10:34:21 -08001258 pd->rx_buffers = kmalloc_array(RX_RING_SIZE,
1259 sizeof(struct smsc9420_ring_info),
1260 GFP_KERNEL);
1261 if (pd->rx_buffers == NULL)
Steve Glendinning2cb37722008-12-11 20:54:30 -08001262 goto out;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001263
1264 /* initialize the rx ring */
1265 for (i = 0; i < RX_RING_SIZE; i++) {
1266 pd->rx_ring[i].status = 0;
1267 pd->rx_ring[i].length = PKT_BUF_SZ;
1268 pd->rx_ring[i].buffer2 = 0;
1269 pd->rx_buffers[i].skb = NULL;
1270 pd->rx_buffers[i].mapping = 0;
1271 }
1272 pd->rx_ring[RX_RING_SIZE - 1].length = (PKT_BUF_SZ | RDES1_RER_);
1273
1274 /* now allocate the entire ring of skbs */
1275 for (i = 0; i < RX_RING_SIZE; i++) {
1276 if (smsc9420_alloc_rx_buffer(pd, i)) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001277 netif_warn(pd, ifup, pd->dev,
1278 "failed to allocate rx skb %d\n", i);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001279 goto out_free_rx_skbs;
1280 }
1281 }
1282
1283 pd->rx_ring_head = 0;
1284 pd->rx_ring_tail = 0;
1285
1286 smsc9420_reg_write(pd, VLAN1, ETH_P_8021Q);
Joe Perchesacec6d72013-11-05 10:34:21 -08001287 netif_dbg(pd, ifup, pd->dev, "VLAN1 = 0x%08x\n",
1288 smsc9420_reg_read(pd, VLAN1));
Steve Glendinning2cb37722008-12-11 20:54:30 -08001289
1290 if (pd->rx_csum) {
1291 /* Enable RX COE */
1292 u32 coe = smsc9420_reg_read(pd, COE_CR) | RX_COE_EN;
1293 smsc9420_reg_write(pd, COE_CR, coe);
Joe Perchesacec6d72013-11-05 10:34:21 -08001294 netif_dbg(pd, ifup, pd->dev, "COE_CR = 0x%08x\n", coe);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001295 }
1296
1297 smsc9420_reg_write(pd, RX_BASE_ADDR, pd->rx_dma_addr);
1298 smsc9420_pci_flush_write(pd);
1299
1300 return 0;
1301
1302out_free_rx_skbs:
1303 smsc9420_free_rx_ring(pd);
1304out:
1305 return -ENOMEM;
1306}
1307
1308static int smsc9420_open(struct net_device *dev)
1309{
Francois Romieub5a80832012-03-09 18:28:59 +01001310 struct smsc9420_pdata *pd = netdev_priv(dev);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001311 u32 bus_mode, mac_cr, dmac_control, int_cfg, dma_intr_ena, int_ctl;
Francois Romieub5a80832012-03-09 18:28:59 +01001312 const int irq = pd->pdev->irq;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001313 unsigned long flags;
1314 int result = 0, timeout;
1315
Steve Glendinning2cb37722008-12-11 20:54:30 -08001316 if (!is_valid_ether_addr(dev->dev_addr)) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001317 netif_warn(pd, ifup, pd->dev,
1318 "dev_addr is not a valid MAC address\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001319 result = -EADDRNOTAVAIL;
1320 goto out_0;
1321 }
1322
1323 netif_carrier_off(dev);
1324
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001325 /* disable, mask and acknowledge all interrupts */
Steve Glendinning2cb37722008-12-11 20:54:30 -08001326 spin_lock_irqsave(&pd->int_lock, flags);
1327 int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
1328 smsc9420_reg_write(pd, INT_CFG, int_cfg);
1329 smsc9420_reg_write(pd, INT_CTL, 0);
1330 spin_unlock_irqrestore(&pd->int_lock, flags);
1331 smsc9420_reg_write(pd, DMAC_INTR_ENA, 0);
1332 smsc9420_reg_write(pd, INT_STAT, 0xFFFFFFFF);
1333 smsc9420_pci_flush_write(pd);
1334
Michael Opdenackercf68ca12013-09-13 06:27:47 +02001335 result = request_irq(irq, smsc9420_isr, IRQF_SHARED, DRV_NAME, pd);
Francois Romieub5a80832012-03-09 18:28:59 +01001336 if (result) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001337 netif_warn(pd, ifup, pd->dev, "Unable to use IRQ = %d\n", irq);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001338 result = -ENODEV;
1339 goto out_0;
1340 }
1341
1342 smsc9420_dmac_soft_reset(pd);
1343
1344 /* make sure MAC_CR is sane */
1345 smsc9420_reg_write(pd, MAC_CR, 0);
1346
1347 smsc9420_set_mac_address(dev);
1348
1349 /* Configure GPIO pins to drive LEDs */
1350 smsc9420_reg_write(pd, GPIO_CFG,
1351 (GPIO_CFG_LED_3_ | GPIO_CFG_LED_2_ | GPIO_CFG_LED_1_));
1352
1353 bus_mode = BUS_MODE_DMA_BURST_LENGTH_16;
1354
1355#ifdef __BIG_ENDIAN
1356 bus_mode |= BUS_MODE_DBO_;
1357#endif
1358
1359 smsc9420_reg_write(pd, BUS_MODE, bus_mode);
1360
1361 smsc9420_pci_flush_write(pd);
1362
1363 /* set bus master bridge arbitration priority for Rx and TX DMA */
1364 smsc9420_reg_write(pd, BUS_CFG, BUS_CFG_RXTXWEIGHT_4_1);
1365
1366 smsc9420_reg_write(pd, DMAC_CONTROL,
1367 (DMAC_CONTROL_SF_ | DMAC_CONTROL_OSF_));
1368
1369 smsc9420_pci_flush_write(pd);
1370
1371 /* test the IRQ connection to the ISR */
Joe Perchesacec6d72013-11-05 10:34:21 -08001372 netif_dbg(pd, ifup, pd->dev, "Testing ISR using IRQ %d\n", irq);
Steve Glendinning16095592009-01-29 17:29:15 -08001373 pd->software_irq_signal = false;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001374
1375 spin_lock_irqsave(&pd->int_lock, flags);
1376 /* configure interrupt deassertion timer and enable interrupts */
1377 int_cfg = smsc9420_reg_read(pd, INT_CFG) | INT_CFG_IRQ_EN_;
1378 int_cfg &= ~(INT_CFG_INT_DEAS_MASK);
1379 int_cfg |= (INT_DEAS_TIME & INT_CFG_INT_DEAS_MASK);
1380 smsc9420_reg_write(pd, INT_CFG, int_cfg);
1381
1382 /* unmask software interrupt */
1383 int_ctl = smsc9420_reg_read(pd, INT_CTL) | INT_CTL_SW_INT_EN_;
1384 smsc9420_reg_write(pd, INT_CTL, int_ctl);
1385 spin_unlock_irqrestore(&pd->int_lock, flags);
1386 smsc9420_pci_flush_write(pd);
1387
1388 timeout = 1000;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001389 while (timeout--) {
1390 if (pd->software_irq_signal)
1391 break;
1392 msleep(1);
1393 }
1394
1395 /* disable interrupts */
1396 spin_lock_irqsave(&pd->int_lock, flags);
1397 int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
1398 smsc9420_reg_write(pd, INT_CFG, int_cfg);
1399 spin_unlock_irqrestore(&pd->int_lock, flags);
1400
1401 if (!pd->software_irq_signal) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001402 netif_warn(pd, ifup, pd->dev, "ISR failed signaling test\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001403 result = -ENODEV;
1404 goto out_free_irq_1;
1405 }
1406
Joe Perchesacec6d72013-11-05 10:34:21 -08001407 netif_dbg(pd, ifup, pd->dev, "ISR passed test using IRQ %d\n", irq);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001408
1409 result = smsc9420_alloc_tx_ring(pd);
1410 if (result) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001411 netif_warn(pd, ifup, pd->dev,
1412 "Failed to Initialize tx dma ring\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001413 result = -ENOMEM;
1414 goto out_free_irq_1;
1415 }
1416
1417 result = smsc9420_alloc_rx_ring(pd);
1418 if (result) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001419 netif_warn(pd, ifup, pd->dev,
1420 "Failed to Initialize rx dma ring\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001421 result = -ENOMEM;
1422 goto out_free_tx_ring_2;
1423 }
1424
1425 result = smsc9420_mii_init(dev);
1426 if (result) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001427 netif_warn(pd, ifup, pd->dev, "Failed to initialize Phy\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001428 result = -ENODEV;
1429 goto out_free_rx_ring_3;
1430 }
1431
1432 /* Bring the PHY up */
Philippe Reynes5d872c52016-07-15 10:36:20 +02001433 phy_start(dev->phydev);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001434
1435 napi_enable(&pd->napi);
1436
1437 /* start tx and rx */
1438 mac_cr = smsc9420_reg_read(pd, MAC_CR) | MAC_CR_TXEN_ | MAC_CR_RXEN_;
1439 smsc9420_reg_write(pd, MAC_CR, mac_cr);
1440
1441 dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
1442 dmac_control |= DMAC_CONTROL_ST_ | DMAC_CONTROL_SR_;
1443 smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
1444 smsc9420_pci_flush_write(pd);
1445
1446 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
1447 dma_intr_ena |=
1448 (DMAC_INTR_ENA_TX_ | DMAC_INTR_ENA_RX_ | DMAC_INTR_ENA_NIS_);
1449 smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
1450 smsc9420_pci_flush_write(pd);
1451
1452 netif_wake_queue(dev);
1453
1454 smsc9420_reg_write(pd, RX_POLL_DEMAND, 1);
1455
1456 /* enable interrupts */
1457 spin_lock_irqsave(&pd->int_lock, flags);
1458 int_cfg = smsc9420_reg_read(pd, INT_CFG) | INT_CFG_IRQ_EN_;
1459 smsc9420_reg_write(pd, INT_CFG, int_cfg);
1460 spin_unlock_irqrestore(&pd->int_lock, flags);
1461
1462 return 0;
1463
1464out_free_rx_ring_3:
1465 smsc9420_free_rx_ring(pd);
1466out_free_tx_ring_2:
1467 smsc9420_free_tx_ring(pd);
1468out_free_irq_1:
Francois Romieub5a80832012-03-09 18:28:59 +01001469 free_irq(irq, pd);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001470out_0:
1471 return result;
1472}
1473
1474#ifdef CONFIG_PM
1475
1476static int smsc9420_suspend(struct pci_dev *pdev, pm_message_t state)
1477{
1478 struct net_device *dev = pci_get_drvdata(pdev);
1479 struct smsc9420_pdata *pd = netdev_priv(dev);
1480 u32 int_cfg;
1481 ulong flags;
1482
1483 /* disable interrupts */
1484 spin_lock_irqsave(&pd->int_lock, flags);
1485 int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
1486 smsc9420_reg_write(pd, INT_CFG, int_cfg);
1487 spin_unlock_irqrestore(&pd->int_lock, flags);
1488
1489 if (netif_running(dev)) {
1490 netif_tx_disable(dev);
1491 smsc9420_stop_tx(pd);
1492 smsc9420_free_tx_ring(pd);
1493
1494 napi_disable(&pd->napi);
1495 smsc9420_stop_rx(pd);
1496 smsc9420_free_rx_ring(pd);
1497
Francois Romieub5a80832012-03-09 18:28:59 +01001498 free_irq(pd->pdev->irq, pd);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001499
1500 netif_device_detach(dev);
1501 }
1502
1503 pci_save_state(pdev);
1504 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1505 pci_disable_device(pdev);
1506 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1507
1508 return 0;
1509}
1510
1511static int smsc9420_resume(struct pci_dev *pdev)
1512{
1513 struct net_device *dev = pci_get_drvdata(pdev);
1514 struct smsc9420_pdata *pd = netdev_priv(dev);
1515 int err;
1516
1517 pci_set_power_state(pdev, PCI_D0);
1518 pci_restore_state(pdev);
1519
1520 err = pci_enable_device(pdev);
1521 if (err)
1522 return err;
1523
1524 pci_set_master(pdev);
1525
Julia Lawallc018b7a2014-01-03 00:40:31 +01001526 err = pci_enable_wake(pdev, PCI_D0, 0);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001527 if (err)
Joe Perchesacec6d72013-11-05 10:34:21 -08001528 netif_warn(pd, ifup, pd->dev, "pci_enable_wake failed: %d\n",
1529 err);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001530
1531 if (netif_running(dev)) {
Francois Romieub5a80832012-03-09 18:28:59 +01001532 /* FIXME: gross. It looks like ancient PM relic.*/
Steve Glendinning2cb37722008-12-11 20:54:30 -08001533 err = smsc9420_open(dev);
1534 netif_device_attach(dev);
1535 }
1536 return err;
1537}
1538
1539#endif /* CONFIG_PM */
1540
1541static const struct net_device_ops smsc9420_netdev_ops = {
1542 .ndo_open = smsc9420_open,
1543 .ndo_stop = smsc9420_stop,
1544 .ndo_start_xmit = smsc9420_hard_start_xmit,
1545 .ndo_get_stats = smsc9420_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001546 .ndo_set_rx_mode = smsc9420_set_multicast_list,
Steve Glendinning2cb37722008-12-11 20:54:30 -08001547 .ndo_do_ioctl = smsc9420_do_ioctl,
1548 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerfe96aaa2009-01-09 11:13:14 +00001549 .ndo_set_mac_address = eth_mac_addr,
Steve Glendinninge3126742008-12-12 22:31:50 -08001550#ifdef CONFIG_NET_POLL_CONTROLLER
1551 .ndo_poll_controller = smsc9420_poll_controller,
1552#endif /* CONFIG_NET_POLL_CONTROLLER */
Steve Glendinning2cb37722008-12-11 20:54:30 -08001553};
1554
Bill Pembertonf3f9e502012-12-03 09:23:40 -05001555static int
Steve Glendinning2cb37722008-12-11 20:54:30 -08001556smsc9420_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1557{
1558 struct net_device *dev;
1559 struct smsc9420_pdata *pd;
1560 void __iomem *virt_addr;
1561 int result = 0;
1562 u32 id_rev;
1563
Joe Perchesacec6d72013-11-05 10:34:21 -08001564 pr_info("%s version %s\n", DRV_DESCRIPTION, DRV_VERSION);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001565
1566 /* First do the PCI initialisation */
1567 result = pci_enable_device(pdev);
1568 if (unlikely(result)) {
Ben Boeckel48005992013-11-01 08:53:36 -04001569 pr_err("Cannot enable smsc9420\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001570 goto out_0;
1571 }
1572
1573 pci_set_master(pdev);
1574
1575 dev = alloc_etherdev(sizeof(*pd));
Joe Perches41de8d42012-01-29 13:47:52 +00001576 if (!dev)
Steve Glendinning2cb37722008-12-11 20:54:30 -08001577 goto out_disable_pci_device_1;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001578
1579 SET_NETDEV_DEV(dev, &pdev->dev);
1580
1581 if (!(pci_resource_flags(pdev, SMSC_BAR) & IORESOURCE_MEM)) {
Ben Boeckel48005992013-11-01 08:53:36 -04001582 netdev_err(dev, "Cannot find PCI device base address\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001583 goto out_free_netdev_2;
1584 }
1585
1586 if ((pci_request_regions(pdev, DRV_NAME))) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001587 netdev_err(dev, "Cannot obtain PCI resources, aborting\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001588 goto out_free_netdev_2;
1589 }
1590
Yang Hongyang284901a2009-04-06 19:01:15 -07001591 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001592 netdev_err(dev, "No usable DMA configuration, aborting\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001593 goto out_free_regions_3;
1594 }
1595
1596 virt_addr = ioremap(pci_resource_start(pdev, SMSC_BAR),
1597 pci_resource_len(pdev, SMSC_BAR));
1598 if (!virt_addr) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001599 netdev_err(dev, "Cannot map device registers, aborting\n");
Steve Glendinning2cb37722008-12-11 20:54:30 -08001600 goto out_free_regions_3;
1601 }
1602
1603 /* registers are double mapped with 0 offset for LE and 0x200 for BE */
1604 virt_addr += LAN9420_CPSR_ENDIAN_OFFSET;
1605
Steve Glendinning2cb37722008-12-11 20:54:30 -08001606 pd = netdev_priv(dev);
1607
1608 /* pci descriptors are created in the PCI consistent area */
1609 pd->rx_ring = pci_alloc_consistent(pdev,
1610 sizeof(struct smsc9420_dma_desc) * RX_RING_SIZE +
1611 sizeof(struct smsc9420_dma_desc) * TX_RING_SIZE,
1612 &pd->rx_dma_addr);
1613
1614 if (!pd->rx_ring)
1615 goto out_free_io_4;
1616
1617 /* descriptors are aligned due to the nature of pci_alloc_consistent */
Joe Perches64699332012-06-04 12:44:16 +00001618 pd->tx_ring = (pd->rx_ring + RX_RING_SIZE);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001619 pd->tx_dma_addr = pd->rx_dma_addr +
1620 sizeof(struct smsc9420_dma_desc) * RX_RING_SIZE;
1621
1622 pd->pdev = pdev;
1623 pd->dev = dev;
Francois Romieub5a80832012-03-09 18:28:59 +01001624 pd->ioaddr = virt_addr;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001625 pd->msg_enable = smsc_debug;
1626 pd->rx_csum = true;
1627
Joe Perchesacec6d72013-11-05 10:34:21 -08001628 netif_dbg(pd, probe, pd->dev, "lan_base=0x%08lx\n", (ulong)virt_addr);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001629
1630 id_rev = smsc9420_reg_read(pd, ID_REV);
1631 switch (id_rev & 0xFFFF0000) {
1632 case 0x94200000:
Joe Perchesacec6d72013-11-05 10:34:21 -08001633 netif_info(pd, probe, pd->dev,
1634 "LAN9420 identified, ID_REV=0x%08X\n", id_rev);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001635 break;
1636 default:
Joe Perchesacec6d72013-11-05 10:34:21 -08001637 netif_warn(pd, probe, pd->dev, "LAN9420 NOT identified\n");
1638 netif_warn(pd, probe, pd->dev, "ID_REV=0x%08X\n", id_rev);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001639 goto out_free_dmadesc_5;
1640 }
1641
1642 smsc9420_dmac_soft_reset(pd);
1643 smsc9420_eeprom_reload(pd);
1644 smsc9420_check_mac_address(dev);
1645
1646 dev->netdev_ops = &smsc9420_netdev_ops;
1647 dev->ethtool_ops = &smsc9420_ethtool_ops;
Steve Glendinning2cb37722008-12-11 20:54:30 -08001648
1649 netif_napi_add(dev, &pd->napi, smsc9420_rx_poll, NAPI_WEIGHT);
1650
1651 result = register_netdev(dev);
1652 if (result) {
Joe Perchesacec6d72013-11-05 10:34:21 -08001653 netif_warn(pd, probe, pd->dev, "error %i registering device\n",
1654 result);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001655 goto out_free_dmadesc_5;
1656 }
1657
1658 pci_set_drvdata(pdev, dev);
1659
1660 spin_lock_init(&pd->int_lock);
1661 spin_lock_init(&pd->phy_lock);
1662
1663 dev_info(&dev->dev, "MAC Address: %pM\n", dev->dev_addr);
1664
1665 return 0;
1666
1667out_free_dmadesc_5:
1668 pci_free_consistent(pdev, sizeof(struct smsc9420_dma_desc) *
1669 (RX_RING_SIZE + TX_RING_SIZE), pd->rx_ring, pd->rx_dma_addr);
1670out_free_io_4:
1671 iounmap(virt_addr - LAN9420_CPSR_ENDIAN_OFFSET);
1672out_free_regions_3:
1673 pci_release_regions(pdev);
1674out_free_netdev_2:
1675 free_netdev(dev);
1676out_disable_pci_device_1:
1677 pci_disable_device(pdev);
1678out_0:
1679 return -ENODEV;
1680}
1681
Bill Pembertonf3f9e502012-12-03 09:23:40 -05001682static void smsc9420_remove(struct pci_dev *pdev)
Steve Glendinning2cb37722008-12-11 20:54:30 -08001683{
1684 struct net_device *dev;
1685 struct smsc9420_pdata *pd;
1686
1687 dev = pci_get_drvdata(pdev);
1688 if (!dev)
1689 return;
1690
Steve Glendinning2cb37722008-12-11 20:54:30 -08001691 pd = netdev_priv(dev);
1692 unregister_netdev(dev);
1693
1694 /* tx_buffers and rx_buffers are freed in stop */
1695 BUG_ON(pd->tx_buffers);
1696 BUG_ON(pd->rx_buffers);
1697
1698 BUG_ON(!pd->tx_ring);
1699 BUG_ON(!pd->rx_ring);
1700
1701 pci_free_consistent(pdev, sizeof(struct smsc9420_dma_desc) *
1702 (RX_RING_SIZE + TX_RING_SIZE), pd->rx_ring, pd->rx_dma_addr);
1703
Francois Romieub5a80832012-03-09 18:28:59 +01001704 iounmap(pd->ioaddr - LAN9420_CPSR_ENDIAN_OFFSET);
Steve Glendinning2cb37722008-12-11 20:54:30 -08001705 pci_release_regions(pdev);
1706 free_netdev(dev);
1707 pci_disable_device(pdev);
1708}
1709
1710static struct pci_driver smsc9420_driver = {
1711 .name = DRV_NAME,
1712 .id_table = smsc9420_id_table,
1713 .probe = smsc9420_probe,
Bill Pembertonf3f9e502012-12-03 09:23:40 -05001714 .remove = smsc9420_remove,
Steve Glendinning2cb37722008-12-11 20:54:30 -08001715#ifdef CONFIG_PM
1716 .suspend = smsc9420_suspend,
1717 .resume = smsc9420_resume,
1718#endif /* CONFIG_PM */
1719};
1720
1721static int __init smsc9420_init_module(void)
1722{
1723 smsc_debug = netif_msg_init(debug, SMSC_MSG_DEFAULT);
1724
1725 return pci_register_driver(&smsc9420_driver);
1726}
1727
1728static void __exit smsc9420_exit_module(void)
1729{
1730 pci_unregister_driver(&smsc9420_driver);
1731}
1732
1733module_init(smsc9420_init_module);
1734module_exit(smsc9420_exit_module);