23 #include <linux/kernel.h>
24 #include <linux/netdevice.h>
26 #include <linux/pci.h>
27 #include <linux/if_vlan.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <asm/unaligned.h>
35 #define DRV_NAME "smsc9420"
36 #define PFX DRV_NAME ": "
37 #define DRV_MDIONAME "smsc9420-mdio"
38 #define DRV_DESCRIPTION "SMSC LAN9420 driver"
39 #define DRV_VERSION "1.01"
93 #define SMSC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
95 static uint smsc_debug;
100 #define smsc_dbg(TYPE, f, a...) \
101 do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
102 printk(KERN_DEBUG PFX f "\n", ## a); \
105 #define smsc_info(TYPE, f, a...) \
106 do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
107 printk(KERN_INFO PFX f "\n", ## a); \
110 #define smsc_warn(TYPE, f, a...) \
111 do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
112 printk(KERN_WARNING PFX f "\n", ## a); \
126 static inline void smsc9420_pci_flush_write(
struct smsc9420_pdata *pd)
129 smsc9420_reg_read(pd,
ID_REV);
148 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
153 for (i = 0; i < 5; i++) {
155 MII_ACCESS_MII_BUSY_)) {
169 static int smsc9420_mii_write(
struct mii_bus *bus,
int phyaddr,
int regidx,
180 if ((smsc9420_reg_read(pd,
MII_ACCESS) & MII_ACCESS_MII_BUSY_)) {
189 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
194 for (i = 0; i < 5; i++) {
196 MII_ACCESS_MII_BUSY_)) {
225 smsc_dbg(DRV,
"smsc9420_eeprom_reload: Eeprom busy");
229 smsc9420_reg_write(pd,
E2P_CMD,
234 if (!(smsc9420_reg_read(pd,
E2P_CMD) & E2P_CMD_EPC_BUSY_))
238 smsc_warn(DRV,
"smsc9420_eeprom_reload: Eeprom timed out");
247 if (!netif_running(dev) || !pd->
phy_dev)
253 static int smsc9420_ethtool_get_settings(
struct net_device *dev,
266 static int smsc9420_ethtool_set_settings(
struct net_device *dev,
277 static void smsc9420_ethtool_get_drvinfo(
struct net_device *netdev,
288 static u32 smsc9420_ethtool_get_msglevel(
struct net_device *netdev)
300 static int smsc9420_ethtool_nway_reset(
struct net_device *netdev)
310 static int smsc9420_ethtool_getregslen(
struct net_device *dev)
313 return 0x100 + (32 *
sizeof(
u32));
322 unsigned int i,
j = 0;
326 for (i = 0; i < 0x100; i += (
sizeof(
u32)))
327 data[j++] = smsc9420_reg_read(pd, i);
333 for (i = 0; i <= 31; i++)
334 data[j++] = smsc9420_mii_read(phy_dev->
bus, phy_dev->
addr, i);
337 static void smsc9420_eeprom_enable_access(
struct smsc9420_pdata *pd)
341 smsc9420_reg_write(pd,
GPIO_CFG, temp);
351 if (smsc9420_reg_read(pd,
E2P_CMD) & E2P_CMD_EPC_BUSY_) {
357 smsc9420_reg_write(pd,
E2P_CMD, e2cmd);
361 e2cmd = smsc9420_reg_read(pd,
E2P_CMD);
362 }
while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
370 smsc_info(HW,
"Error occurred during eeprom operation");
377 static int smsc9420_eeprom_read_location(
struct smsc9420_pdata *pd,
383 smsc_dbg(HW,
"address 0x%x", address);
384 ret = smsc9420_eeprom_send_cmd(pd, op);
392 static int smsc9420_eeprom_write_location(
struct smsc9420_pdata *pd,
398 smsc_dbg(HW,
"address 0x%x, data 0x%x", address, data);
399 ret = smsc9420_eeprom_send_cmd(pd, op);
404 ret = smsc9420_eeprom_send_cmd(pd, op);
410 static int smsc9420_ethtool_get_eeprom_len(
struct net_device *dev)
415 static int smsc9420_ethtool_get_eeprom(
struct net_device *dev,
422 smsc9420_eeprom_enable_access(pd);
425 for (i = 0; i < len; i++) {
426 int ret = smsc9420_eeprom_read_location(pd, i, eeprom_data);
439 static int smsc9420_ethtool_set_eeprom(
struct net_device *dev,
448 smsc9420_eeprom_enable_access(pd);
450 ret = smsc9420_eeprom_write_location(pd, eeprom->
offset, *data);
459 static const struct ethtool_ops smsc9420_ethtool_ops = {
460 .get_settings = smsc9420_ethtool_get_settings,
461 .set_settings = smsc9420_ethtool_set_settings,
462 .get_drvinfo = smsc9420_ethtool_get_drvinfo,
463 .get_msglevel = smsc9420_ethtool_get_msglevel,
464 .set_msglevel = smsc9420_ethtool_set_msglevel,
465 .nway_reset = smsc9420_ethtool_nway_reset,
467 .get_eeprom_len = smsc9420_ethtool_get_eeprom_len,
468 .get_eeprom = smsc9420_ethtool_get_eeprom,
469 .set_eeprom = smsc9420_ethtool_set_eeprom,
470 .get_regs_len = smsc9420_ethtool_getregslen,
471 .get_regs = smsc9420_ethtool_getregs,
476 static void smsc9420_set_mac_address(
struct net_device *dev)
480 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
481 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
482 (dev_addr[1] << 8) | dev_addr[0];
484 smsc9420_reg_write(pd,
ADDRH, mac_high16);
485 smsc9420_reg_write(pd,
ADDRL, mac_low32);
488 static void smsc9420_check_mac_address(
struct net_device *dev)
493 if (is_valid_ether_addr(dev->
dev_addr)) {
494 smsc9420_set_mac_address(dev);
495 smsc_dbg(PROBE,
"MAC Address is specified by configuration");
499 u32 mac_high16 = smsc9420_reg_read(pd,
ADDRH);
500 u32 mac_low32 = smsc9420_reg_read(pd,
ADDRL);
508 if (is_valid_ether_addr(dev->
dev_addr)) {
510 smsc_dbg(PROBE,
"Mac Address is read from EEPROM");
513 eth_hw_addr_random(dev);
514 smsc9420_set_mac_address(dev);
515 smsc_dbg(PROBE,
"MAC Address is set to random");
522 u32 dmac_control, mac_cr, dma_intr_ena;
538 smsc_warn(IFDOWN,
"TX DMAC failed to stop");
547 smsc9420_pci_flush_write(pd);
551 smsc9420_reg_write(pd,
MAC_CR, mac_cr);
552 smsc9420_pci_flush_write(pd);
622 u32 mac_cr, dmac_control, dma_intr_ena;
628 smsc9420_pci_flush_write(pd);
632 smsc9420_reg_write(pd,
MAC_CR, mac_cr);
633 smsc9420_pci_flush_write(pd);
639 smsc9420_pci_flush_write(pd);
649 smsc_warn(IFDOWN,
"RX DMAC did not stop! timeout.");
658 u32 int_cfg, int_sts, int_ctl;
665 int_cfg = smsc9420_reg_read(pd,
INT_CFG);
672 int_sts = smsc9420_reg_read(pd,
INT_STAT);
676 u32 ints_to_clear = 0;
680 netif_wake_queue(pd->
dev);
688 smsc9420_pci_flush_write(pd);
691 napi_schedule(&pd->
napi);
695 smsc9420_reg_write(pd,
DMAC_STATUS, ints_to_clear);
703 int_ctl = smsc9420_reg_read(pd,
INT_CTL);
705 smsc9420_reg_write(pd,
INT_CTL, int_ctl);
706 spin_unlock_irqrestore(&pd->
int_lock, flags);
716 smsc9420_pci_flush_write(pd);
721 #ifdef CONFIG_NET_POLL_CONTROLLER
722 static void smsc9420_poll_controller(
struct net_device *dev)
725 const int irq = pd->
pdev->irq;
728 smsc9420_isr(0, dev);
739 smsc_warn(DRV,
"Software reset not cleared");
742 static int smsc9420_stop(
struct net_device *dev)
754 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
755 spin_unlock_irqrestore(&pd->
int_lock, flags);
757 netif_tx_disable(dev);
758 napi_disable(&pd->
napi);
760 smsc9420_stop_tx(pd);
761 smsc9420_free_tx_ring(pd);
763 smsc9420_stop_rx(pd);
764 smsc9420_free_rx_ring(pd);
768 smsc9420_dmac_soft_reset(pd);
783 dev->
stats.rx_errors++;
785 dev->
stats.rx_over_errors++;
788 dev->
stats.rx_frame_errors++;
790 dev->
stats.rx_crc_errors++;
794 dev->
stats.rx_length_errors++;
798 dev->
stats.rx_length_errors++;
801 dev->
stats.multicast++;
818 dev->
stats.rx_packets++;
819 dev->
stats.rx_bytes += packet_length;
829 u16 hw_csum = get_unaligned_le16(skb_tail_pointer(skb) +
831 put_unaligned_le16(hw_csum, &skb->
csum);
843 static int smsc9420_alloc_rx_buffer(
struct smsc9420_pdata *pd,
int index)
856 mapping = pci_map_single(pd->
pdev, skb_tail_pointer(skb),
858 if (pci_dma_mapping_error(pd->
pdev, mapping)) {
873 static void smsc9420_alloc_new_rx_buffers(
struct smsc9420_pdata *pd)
888 u32 drop_frame_cnt, dma_intr_ena,
status;
891 for (work_done = 0; work_done < budget; work_done++) {
899 smsc9420_rx_count_stats(dev, status);
902 smsc9420_alloc_new_rx_buffers(pd);
906 dev->
stats.rx_dropped +=
907 (drop_frame_cnt & 0xFFFF) + ((drop_frame_cnt >> 17) & 0x3FF);
911 smsc9420_pci_flush_write(pd);
913 if (work_done < budget) {
920 smsc9420_pci_flush_write(pd);
929 dev->
stats.tx_errors++;
932 dev->
stats.tx_aborted_errors++;
935 dev->
stats.tx_carrier_errors++;
937 dev->
stats.tx_packets++;
938 dev->
stats.tx_bytes += (length & 0x7FF);
942 dev->
stats.collisions += 16;
944 dev->
stats.collisions +=
950 dev->
stats.tx_heartbeat_errors++;
954 static void smsc9420_complete_tx(
struct net_device *dev)
970 smsc9420_tx_update_stats(dev, status, length);
996 bool about_to_take_last_desc =
999 smsc9420_complete_tx(dev);
1006 mapping = pci_map_single(pd->
pdev, skb->
data,
1008 if (pci_dma_mapping_error(pd->
pdev, mapping)) {
1009 smsc_warn(TX_ERR,
"pci_map_single failed, dropping packet");
1017 if (
unlikely(about_to_take_last_desc)) {
1019 netif_stop_queue(pd->
dev);
1023 if (
unlikely(index == (TX_RING_SIZE - 1)))
1037 skb_tx_timestamp(skb);
1041 smsc9420_pci_flush_write(pd);
1050 dev->
stats.rx_dropped +=
1051 (counter & 0x0000FFFF) + ((counter >> 17) & 0x000003FF);
1055 static void smsc9420_set_multicast_list(
struct net_device *dev)
1058 u32 mac_cr = smsc9420_reg_read(pd,
MAC_CR);
1061 smsc_dbg(HW,
"Promiscuous Mode Enabled");
1066 smsc_dbg(HW,
"Receive all Multicast Enabled");
1072 u32 hash_lo = 0, hash_hi = 0;
1074 smsc_dbg(HW,
"Multicast filter enabled");
1076 u32 bit_num = smsc9420_hash(ha->
addr);
1077 u32 mask = 1 << (bit_num & 0x1F);
1085 smsc9420_reg_write(pd,
HASHH, hash_hi);
1086 smsc9420_reg_write(pd,
HASHL, hash_lo);
1092 smsc_dbg(HW,
"Receive own packets only.");
1093 smsc9420_reg_write(pd,
HASHH, 0);
1094 smsc9420_reg_write(pd,
HASHL, 0);
1101 smsc9420_reg_write(pd,
MAC_CR, mac_cr);
1102 smsc9420_pci_flush_write(pd);
1105 static void smsc9420_phy_update_flowcontrol(
struct smsc9420_pdata *pd)
1113 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1121 (cap & FLOW_CTRL_RX ?
"enabled" :
"disabled"),
1128 smsc9420_reg_write(pd,
FLOW, flow);
1133 static void smsc9420_phy_adjust_link(
struct net_device *dev)
1140 u32 mac_cr = smsc9420_reg_read(pd,
MAC_CR);
1148 smsc9420_reg_write(pd,
MAC_CR, mac_cr);
1150 smsc9420_phy_update_flowcontrol(pd);
1154 carrier = netif_carrier_ok(dev);
1164 static int smsc9420_mii_probe(
struct net_device *dev)
1172 if (!pd->
mii_bus->phy_map[1]) {
1173 pr_err(
"%s: no PHY found at address 1\n", dev->
name);
1177 phydev = pd->
mii_bus->phy_map[1];
1178 smsc_info(PROBE,
"PHY addr %d, phy_id 0x%08X", phydev->
addr,
1184 if (IS_ERR(phydev)) {
1185 pr_err(
"%s: Could not attach to PHY\n", dev->
name);
1186 return PTR_ERR(phydev);
1189 pr_info(
"%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1190 dev->
name, phydev->
drv->name, dev_name(&phydev->
dev), phydev->
irq);
1204 static int smsc9420_mii_init(
struct net_device *dev)
1209 pd->
mii_bus = mdiobus_alloc();
1216 (pd->
pdev->bus->number << 8) | pd->
pdev->devfn);
1218 pd->
mii_bus->read = smsc9420_mii_read;
1219 pd->
mii_bus->write = smsc9420_mii_write;
1225 pd->
mii_bus->phy_mask = ~(1 << 1);
1228 smsc_warn(PROBE,
"Error registering mii bus");
1229 goto err_out_free_bus_2;
1232 if (smsc9420_mii_probe(dev) < 0) {
1233 smsc_warn(PROBE,
"Error probing mii bus");
1234 goto err_out_unregister_bus_3;
1239 err_out_unregister_bus_3:
1256 smsc_warn(IFUP,
"Failed to allocated tx_buffers");
1276 smsc9420_pci_flush_write(pd);
1290 smsc_warn(IFUP,
"Failed to allocated rx_buffers");
1306 if (smsc9420_alloc_rx_buffer(pd, i)) {
1307 smsc_warn(IFUP,
"failed to allocate rx skb %d", i);
1308 goto out_free_rx_skbs;
1316 smsc_dbg(IFUP,
"VLAN1 = 0x%08x", smsc9420_reg_read(pd,
VLAN1));
1321 smsc9420_reg_write(pd,
COE_CR, coe);
1322 smsc_dbg(IFUP,
"COE_CR = 0x%08x", coe);
1326 smsc9420_pci_flush_write(pd);
1331 smsc9420_free_rx_ring(pd);
1336 static int smsc9420_open(
struct net_device *dev)
1339 u32 bus_mode, mac_cr, dmac_control, int_cfg, dma_intr_ena, int_ctl;
1340 const int irq = pd->
pdev->irq;
1341 unsigned long flags;
1344 if (!is_valid_ether_addr(dev->
dev_addr)) {
1345 smsc_warn(IFUP,
"dev_addr is not a valid MAC address");
1355 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
1356 smsc9420_reg_write(pd,
INT_CTL, 0);
1357 spin_unlock_irqrestore(&pd->
int_lock, flags);
1359 smsc9420_reg_write(pd,
INT_STAT, 0xFFFFFFFF);
1360 smsc9420_pci_flush_write(pd);
1365 smsc_warn(IFUP,
"Unable to use IRQ = %d", irq);
1370 smsc9420_dmac_soft_reset(pd);
1373 smsc9420_reg_write(pd,
MAC_CR, 0);
1375 smsc9420_set_mac_address(dev);
1387 smsc9420_reg_write(pd,
BUS_MODE, bus_mode);
1389 smsc9420_pci_flush_write(pd);
1397 smsc9420_pci_flush_write(pd);
1400 smsc_dbg(IFUP,
"Testing ISR using IRQ %d", irq);
1408 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
1412 smsc9420_reg_write(pd,
INT_CTL, int_ctl);
1413 spin_unlock_irqrestore(&pd->
int_lock, flags);
1414 smsc9420_pci_flush_write(pd);
1426 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
1427 spin_unlock_irqrestore(&pd->
int_lock, flags);
1430 smsc_warn(IFUP,
"ISR failed signaling test");
1432 goto out_free_irq_1;
1435 smsc_dbg(IFUP,
"ISR passed test using IRQ %d", irq);
1437 result = smsc9420_alloc_tx_ring(pd);
1439 smsc_warn(IFUP,
"Failed to Initialize tx dma ring");
1441 goto out_free_irq_1;
1444 result = smsc9420_alloc_rx_ring(pd);
1446 smsc_warn(IFUP,
"Failed to Initialize rx dma ring");
1448 goto out_free_tx_ring_2;
1451 result = smsc9420_mii_init(dev);
1453 smsc_warn(IFUP,
"Failed to initialize Phy");
1455 goto out_free_rx_ring_3;
1461 napi_enable(&pd->
napi);
1465 smsc9420_reg_write(pd,
MAC_CR, mac_cr);
1470 smsc9420_pci_flush_write(pd);
1476 smsc9420_pci_flush_write(pd);
1478 netif_wake_queue(dev);
1485 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
1486 spin_unlock_irqrestore(&pd->
int_lock, flags);
1491 smsc9420_free_rx_ring(pd);
1493 smsc9420_free_tx_ring(pd);
1504 struct net_device *dev = pci_get_drvdata(pdev);
1512 smsc9420_reg_write(pd,
INT_CFG, int_cfg);
1513 spin_unlock_irqrestore(&pd->
int_lock, flags);
1515 if (netif_running(dev)) {
1516 netif_tx_disable(dev);
1517 smsc9420_stop_tx(pd);
1518 smsc9420_free_tx_ring(pd);
1520 napi_disable(&pd->
napi);
1521 smsc9420_stop_rx(pd);
1522 smsc9420_free_rx_ring(pd);
1537 static int smsc9420_resume(
struct pci_dev *pdev)
1539 struct net_device *dev = pci_get_drvdata(pdev);
1552 err = pci_enable_wake(pdev, 0, 0);
1554 smsc_warn(IFUP,
"pci_enable_wake failed: %d", err);
1556 if (netif_running(dev)) {
1558 err = smsc9420_open(dev);
1567 .ndo_open = smsc9420_open,
1568 .ndo_stop = smsc9420_stop,
1569 .ndo_start_xmit = smsc9420_hard_start_xmit,
1570 .ndo_get_stats = smsc9420_get_stats,
1571 .ndo_set_rx_mode = smsc9420_set_multicast_list,
1572 .ndo_do_ioctl = smsc9420_do_ioctl,
1575 #ifdef CONFIG_NET_POLL_CONTROLLER
1576 .ndo_poll_controller = smsc9420_poll_controller,
1600 dev = alloc_etherdev(
sizeof(*pd));
1602 goto out_disable_pci_device_1;
1608 goto out_free_netdev_2;
1613 goto out_free_netdev_2;
1618 goto out_free_regions_3;
1625 goto out_free_regions_3;
1631 pd = netdev_priv(dev);
1655 id_rev = smsc9420_reg_read(pd,
ID_REV);
1656 switch (id_rev & 0xFFFF0000) {
1658 smsc_info(PROBE,
"LAN9420 identified, ID_REV=0x%08X", id_rev);
1661 smsc_warn(PROBE,
"LAN9420 NOT identified");
1662 smsc_warn(PROBE,
"ID_REV=0x%08X", id_rev);
1663 goto out_free_dmadesc_5;
1666 smsc9420_dmac_soft_reset(pd);
1667 smsc9420_eeprom_reload(pd);
1668 smsc9420_check_mac_address(dev);
1677 smsc_warn(PROBE,
"error %i registering device", result);
1678 goto out_free_dmadesc_5;
1681 pci_set_drvdata(pdev, dev);
1699 out_disable_pci_device_1:
1710 dev = pci_get_drvdata(pdev);
1714 pci_set_drvdata(pdev,
NULL);
1716 pd = netdev_priv(dev);
1737 .id_table = smsc9420_id_table,
1738 .probe = smsc9420_probe,
1741 .suspend = smsc9420_suspend,
1742 .resume = smsc9420_resume,
1746 static int __init smsc9420_init_module(
void)
1750 return pci_register_driver(&smsc9420_driver);
1753 static void __exit smsc9420_exit_module(
void)