25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/string.h>
30 #include <linux/errno.h>
33 #include <linux/pci.h>
34 #include <linux/netdevice.h>
39 #include <linux/mii.h>
40 #include <linux/ethtool.h>
43 #include <linux/bitops.h>
49 #include <asm/processor.h>
51 #define DRV_NAME "r6040"
52 #define DRV_VERSION "0.28"
53 #define DRV_RELDATE "07Oct2011"
56 #define TX_TIMEOUT (6000 * HZ / 1000)
59 #define R6040_IO_SIZE 256
66 #define MCR0_RCVEN 0x0002
67 #define MCR0_PROMISC 0x0020
68 #define MCR0_HASH_EN 0x0100
69 #define MCR0_XMTEN 0x1000
70 #define MCR0_FD 0x8000
72 #define MAC_RST 0x0001
81 #define TX_FIFO_UNDR 0x0200
82 #define TX_EXCEEDC 0x2000
83 #define TX_LATEC 0x4000
85 #define MDIO_WRITE 0x4000
86 #define MDIO_READ 0x2000
95 #define MSK_INT 0x0000
96 #define RX_FINISH 0x0001
97 #define RX_NO_DESC 0x0002
98 #define RX_FIFO_FULL 0x0004
99 #define RX_EARLY 0x0008
100 #define TX_FINISH 0x0010
101 #define TX_EARLY 0x0080
102 #define EVENT_OVRFL 0x0100
103 #define LINK_CHANGED 0x0200
132 #define PHYAD_SHIFT 8
133 #define TMRDIV_SHIFT 0
136 #define MAC_SM_RST 0x0002
141 #define MAX_BUF_SIZE 0x600
142 #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
143 #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
144 #define MBCR_DEFAULT 0x012A
147 #define MAC_DEF_TIMEOUT 2048
150 #define DSC_OWNER_MAC 0x8000
151 #define DSC_RX_OK 0x4000
152 #define DSC_RX_ERR 0x0800
153 #define DSC_RX_ERR_DRI 0x0400
154 #define DSC_RX_ERR_BUF 0x0200
155 #define DSC_RX_ERR_LONG 0x0100
156 #define DSC_RX_ERR_RUNT 0x0080
157 #define DSC_RX_ERR_CRC 0x0040
158 #define DSC_RX_BCAST 0x0020
159 #define DSC_RX_MCAST 0x0010
160 #define DSC_RX_MCH_HIT 0x0008
161 #define DSC_RX_MIDH_HIT 0x0004
162 #define DSC_RX_IDX_MID_MASK 3
172 #define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH)
173 #define TX_INTS (TX_FINISH)
174 #define INT_MASK (RX_INTS | TX_INTS)
210 ": RDC R6040 NAPI net driver,"
214 static int r6040_phy_read(
void __iomem *ioaddr,
int phy_addr,
int reg)
231 static void r6040_phy_write(
void __iomem *ioaddr,
248 static int r6040_mdiobus_read(
struct mii_bus *
bus,
int phy_addr,
int reg)
254 return r6040_phy_read(ioaddr, phy_addr, reg);
257 static int r6040_mdiobus_write(
struct mii_bus *
bus,
int phy_addr,
264 r6040_phy_write(ioaddr, phy_addr, reg, value);
269 static int r6040_mdiobus_reset(
struct mii_bus *
bus)
279 for (i = 0; i <
TX_DCNT; i++) {
281 pci_unmap_single(lp->
pdev,
291 static void r6040_free_rxbufs(
struct net_device *dev)
296 for (i = 0; i <
RX_DCNT; i++) {
298 pci_unmap_single(lp->
pdev,
315 mapping +=
sizeof(*desc);
325 static void r6040_init_txbufs(
struct net_device *dev)
335 static int r6040_alloc_rxbufs(
struct net_device *dev)
350 netdev_err(dev,
"failed to alloc skb for rx\n");
366 r6040_free_rxbufs(dev);
389 static void r6040_init_mac_regs(
struct net_device *dev)
430 static void r6040_tx_timeout(
struct net_device *dev)
435 netdev_warn(dev,
"transmit timed out, int enable %4.4x "
440 dev->
stats.tx_errors++;
443 r6040_init_mac_regs(dev);
455 spin_unlock_irqrestore(&priv->
lock, flags);
461 static void r6040_down(
struct net_device *dev)
482 static int r6040_close(
struct net_device *dev)
487 spin_lock_irq(&lp->
lock);
488 napi_disable(&lp->
napi);
489 netif_stop_queue(dev);
495 r6040_free_rxbufs(dev);
498 r6040_free_txbufs(dev);
500 spin_unlock_irq(&lp->
lock);
528 static int r6040_rx(
struct net_device *dev,
int limit)
544 dev->
stats.rx_frame_errors++;
547 dev->
stats.rx_length_errors++;
550 dev->
stats.rx_length_errors++;
553 dev->
stats.rx_length_errors++;
556 spin_lock(&priv->
lock);
557 dev->
stats.rx_crc_errors++;
558 spin_unlock(&priv->
lock);
566 dev->
stats.rx_dropped++;
580 dev->
stats.rx_packets++;
581 dev->
stats.rx_bytes += descptr->
len - 4;
608 spin_lock(&priv->
lock);
615 dev->
stats.tx_fifo_errors++;
617 dev->
stats.tx_carrier_errors++;
634 netif_wake_queue(dev);
635 spin_unlock(&priv->
lock);
646 work_done = r6040_rx(dev, budget);
648 if (work_done < budget) {
671 if (status == 0x0000 || status == 0xffff) {
681 dev->
stats.rx_dropped++;
682 dev->
stats.rx_missed_errors++;
685 dev->
stats.rx_fifo_errors++;
704 #ifdef CONFIG_NET_POLL_CONTROLLER
705 static void r6040_poll_controller(
struct net_device *dev)
708 r6040_interrupt(dev->
irq, dev);
721 r6040_init_txbufs(dev);
722 ret = r6040_alloc_rxbufs(dev);
727 r6040_phy_write(ioaddr, 30, 17,
728 (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
729 r6040_phy_write(ioaddr, 30, 17,
730 ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
731 r6040_phy_write(ioaddr, 0, 19, 0x0000);
732 r6040_phy_write(ioaddr, 0, 30, 0x01F0);
735 r6040_init_mac_regs(dev);
744 static void r6040_mac_address(
struct net_device *dev)
775 r6040_mac_address(dev);
789 goto err_free_rx_ring;
794 goto err_free_tx_ring;
796 napi_enable(&lp->
napi);
797 netif_start_queue(dev);
826 spin_unlock_irqrestore(&lp->
lock, flags);
827 netif_stop_queue(dev);
828 netdev_err(dev,
": no tx descriptor\n");
833 dev->
stats.tx_packets++;
848 skb_tx_timestamp(skb);
856 netif_stop_queue(dev);
858 spin_unlock_irqrestore(&lp->
lock, flags);
863 static void r6040_multicast_list(
struct net_device *dev)
871 u16 hash_table[4] = { 0 };
899 for (i = 0; i < 4; i++)
900 hash_table[i] = 0xffff;
913 while (i < MCAST_MAX) {
938 hash_table[crc >> 4] |= 1 << (crc & 0xf);
952 spin_unlock_irqrestore(&lp->
lock, flags);
955 static void netdev_get_drvinfo(
struct net_device *dev,
979 static const struct ethtool_ops netdev_ethtool_ops = {
980 .get_drvinfo = netdev_get_drvinfo,
981 .get_settings = netdev_get_settings,
982 .set_settings = netdev_set_settings,
988 .ndo_open = r6040_open,
989 .ndo_stop = r6040_close,
990 .ndo_start_xmit = r6040_start_xmit,
991 .ndo_get_stats = r6040_get_stats,
992 .ndo_set_rx_mode = r6040_multicast_list,
996 .ndo_do_ioctl = r6040_ioctl,
997 .ndo_tx_timeout = r6040_tx_timeout,
998 #ifdef CONFIG_NET_POLL_CONTROLLER
999 .ndo_poll_controller = r6040_poll_controller,
1003 static void r6040_adjust_link(
struct net_device *dev)
1007 int status_changed = 0;
1026 if (status_changed) {
1036 static int r6040_mii_probe(
struct net_device *dev)
1047 phydev =
phy_connect(dev, dev_name(&phydev->
dev), &r6040_adjust_link,
1050 if (IS_ERR(phydev)) {
1051 dev_err(&lp->
pdev->dev,
"could not attach to PHY\n");
1052 return PTR_ERR(phydev);
1070 "(mii_bus:phy_addr=%s)\n",
1071 phydev->
drv->name, dev_name(&phydev->
dev));
1083 static int card_idx = -1;
1097 dev_err(&pdev->
dev,
"32-bit PCI DMA addresses"
1098 "not supported by the card\n");
1099 goto err_out_disable_dev;
1101 err = pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(32));
1103 dev_err(&pdev->
dev,
"32-bit PCI DMA addresses"
1104 "not supported by the card\n");
1105 goto err_out_disable_dev;
1110 dev_err(&pdev->
dev,
"Insufficient PCI resources, aborting\n");
1112 goto err_out_disable_dev;
1120 goto err_out_disable_dev;
1123 lp = netdev_priv(dev);
1128 dev_err(&pdev->
dev,
"Failed to request PCI regions\n");
1129 goto err_out_free_dev;
1132 ioaddr = pci_iomap(pdev, bar, io_size);
1134 dev_err(&pdev->
dev,
"ioremap failed for device\n");
1136 goto err_out_free_res;
1153 pci_set_drvdata(pdev, dev);
1165 if (!(adrp[0] || adrp[1] || adrp[2])) {
1166 netdev_warn(dev,
"MAC address not initialized, "
1167 "generating random\n");
1168 eth_hw_addr_random(dev);
1185 lp->
mii_bus = mdiobus_alloc();
1187 dev_err(&pdev->
dev,
"mdiobus_alloc() failed\n");
1193 lp->
mii_bus->read = r6040_mdiobus_read;
1194 lp->
mii_bus->write = r6040_mdiobus_write;
1195 lp->
mii_bus->reset = r6040_mdiobus_reset;
1196 lp->
mii_bus->name =
"r6040_eth_mii";
1198 dev_name(&pdev->
dev), card_idx);
1201 dev_err(&pdev->
dev,
"mii_bus irq allocation failed\n");
1211 dev_err(&pdev->
dev,
"failed to register MII bus\n");
1212 goto err_out_mdio_irq;
1215 err = r6040_mii_probe(dev);
1217 dev_err(&pdev->
dev,
"failed to probe MII bus\n");
1218 goto err_out_mdio_unregister;
1224 dev_err(&pdev->
dev,
"Failed to register net device\n");
1225 goto err_out_mdio_unregister;
1229 err_out_mdio_unregister:
1237 pci_set_drvdata(pdev,
NULL);
1243 err_out_disable_dev:
1251 struct net_device *dev = pci_get_drvdata(pdev);
1263 pci_set_drvdata(pdev,
NULL);
1275 .id_table = r6040_pci_tbl,
1276 .probe = r6040_init_one,