22 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include <linux/ethtool.h>
32 #include <linux/if_vlan.h>
37 static char bcm_enet_driver_name[] =
"bcm63xx_enet";
38 static char bcm_enet_driver_version[] =
"1.0";
45 static void __iomem *bcm_enet_shared_base;
66 return bcm_readl(bcm_enet_shared_base + off);
95 }
while (limit-- > 0);
97 return (limit < 0) ? 1 : 0;
103 static int bcm_enet_mdio_read(
struct bcm_enet_priv *priv,
int mii_id,
113 if (do_mdio_op(priv, tmp))
124 static int bcm_enet_mdio_write(
struct bcm_enet_priv *priv,
int mii_id,
135 (
void)do_mdio_op(priv, tmp);
142 static int bcm_enet_mdio_read_phylib(
struct mii_bus *
bus,
int mii_id,
145 return bcm_enet_mdio_read(bus->
priv, mii_id, regnum);
151 static int bcm_enet_mdio_write_phylib(
struct mii_bus *
bus,
int mii_id,
152 int regnum,
u16 value)
154 return bcm_enet_mdio_write(bus->
priv, mii_id, regnum, value);
160 static int bcm_enet_mdio_read_mii(
struct net_device *
dev,
int mii_id,
163 return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
169 static void bcm_enet_mdio_write_mii(
struct net_device *dev,
int mii_id,
170 int regnum,
int value)
172 bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
178 static int bcm_enet_refill_rx(
struct net_device *dev)
182 priv = netdev_priv(dev);
194 if (!priv->
rx_skb[desc_idx]) {
226 dev_warn(&priv->
pdev->dev,
"unable to refill rx ring\n");
237 static void bcm_enet_refill_rx_timer(
unsigned long data)
243 priv = netdev_priv(dev);
246 bcm_enet_refill_rx((
struct net_device *)data);
259 priv = netdev_priv(dev);
260 kdev = &priv->
pdev->dev;
297 dev->
stats.rx_dropped++;
303 dev->
stats.rx_errors++;
306 dev->
stats.rx_length_errors++;
308 dev->
stats.rx_crc_errors++;
310 dev->
stats.rx_frame_errors++;
312 dev->
stats.rx_fifo_errors++;
317 skb = priv->
rx_skb[desc_idx];
325 nskb = netdev_alloc_skb_ip_align(dev, len);
328 dev->
stats.rx_dropped++;
346 dev->
stats.rx_packets++;
350 }
while (--budget > 0);
353 bcm_enet_refill_rx(dev);
372 priv = netdev_priv(dev);
385 if (!force && (desc->
len_stat & DMADESC_OWNER_MASK)) {
406 if (desc->
len_stat & DMADESC_UNDER_MASK)
407 dev->
stats.tx_errors++;
413 if (netif_queue_stopped(dev) && released)
414 netif_wake_queue(dev);
426 int tx_work_done, rx_work_done;
438 tx_work_done = bcm_enet_tx_reclaim(dev, 0);
441 rx_work_done = bcm_enet_receive_queue(dev, budget);
444 if (rx_work_done >= budget || tx_work_done > 0) {
472 priv = netdev_priv(dev);
491 static irqreturn_t bcm_enet_isr_dma(
int irq,
void *dev_id)
497 priv = netdev_priv(dev);
503 napi_schedule(&priv->
napi);
518 priv = netdev_priv(dev);
526 netif_stop_queue(dev);
527 dev_err(&priv->
pdev->dev,
"xmit called with no tx desc "
542 len_stat |= DMADESC_ESOP_MASK |
565 netif_stop_queue(dev);
568 dev->
stats.tx_packets++;
579 static int bcm_enet_set_mac_address(
struct net_device *dev,
void *p)
585 priv = netdev_priv(dev);
603 static void bcm_enet_set_multicast_list(
struct net_device *dev)
610 priv = netdev_priv(dev);
642 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
643 (dmi_addr[4] << 8) | dmi_addr[5];
646 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
662 static void bcm_enet_set_duplex(
struct bcm_enet_priv *priv,
int fullduplex)
677 static void bcm_enet_set_flow(
struct bcm_enet_priv *priv,
int rx_en,
int tx_en)
701 static void bcm_enet_adjust_phy_link(
struct net_device *dev)
707 priv = netdev_priv(dev);
718 bcm_enet_set_duplex(priv,
727 int rx_pause_en, tx_pause_en;
742 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
747 if (status_changed) {
753 phydev->
pause == 1 ?
"rx&tx" :
"off");
762 static void bcm_enet_adjust_link(
struct net_device *dev)
766 priv = netdev_priv(dev);
771 pr_info(
"%s: link forced UP - %d/%s - flow control %s/%s\n",
782 static int bcm_enet_open(
struct net_device *dev)
794 priv = netdev_priv(dev);
795 kdev = &priv->
pdev->dev;
802 phydev =
phy_connect(dev, phy_id, bcm_enet_adjust_phy_link, 0,
805 if (IS_ERR(phydev)) {
806 dev_err(kdev,
"could not attach to PHY\n");
807 return PTR_ERR(phydev);
825 dev_info(kdev,
"attached PHY at address %d [%s]\n",
826 phydev->
addr, phydev->
drv->name);
841 goto out_phy_disconnect;
854 for (i = 0; i < 4; i++) {
861 bcm_enet_set_mac_address(dev, &addr);
867 dev_err(kdev,
"cannot allocate rx ring %u\n", size);
880 dev_err(kdev,
"cannot allocate tx ring\n");
882 goto out_free_rx_ring;
892 dev_err(kdev,
"cannot allocate rx skb queue\n");
894 goto out_free_tx_ring;
906 dev_err(kdev,
"cannot allocate rx skb queue\n");
908 goto out_free_tx_skb;
919 if (bcm_enet_refill_rx(dev)) {
920 dev_err(kdev,
"cannot allocate rx skb queue\n");
979 napi_enable(&priv->
napi);
989 bcm_enet_adjust_link(dev);
991 netif_start_queue(dev);
1037 static void bcm_enet_disable_mac(
struct bcm_enet_priv *priv)
1080 static int bcm_enet_stop(
struct net_device *dev)
1086 priv = netdev_priv(dev);
1087 kdev = &priv->
pdev->dev;
1089 netif_stop_queue(dev);
1090 napi_disable(&priv->
napi);
1104 bcm_enet_disable_dma(priv, priv->
tx_chan);
1105 bcm_enet_disable_dma(priv, priv->
rx_chan);
1106 bcm_enet_disable_mac(priv);
1109 bcm_enet_tx_reclaim(dev, 1);
1154 #define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1155 offsetof(struct bcm_enet_priv, m)
1156 #define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
1157 offsetof(struct net_device_stats, m)
1216 #define BCM_ENET_STATS_LEN \
1217 (sizeof(bcm_enet_gstrings_stats) / sizeof(struct bcm_enet_stats))
1219 static const u32 unused_mib_regs[] = {
1227 static void bcm_enet_get_drvinfo(
struct net_device *netdev,
1237 static int bcm_enet_get_sset_count(
struct net_device *netdev,
1240 switch (string_set) {
1248 static void bcm_enet_get_strings(
struct net_device *netdev,
1249 u32 stringset,
u8 *data)
1253 switch (stringset) {
1273 s = &bcm_enet_gstrings_stats[
i];
1288 for (i = 0; i <
ARRAY_SIZE(unused_mib_regs); i++)
1289 (
void)enet_readl(priv,
ENET_MIB_REG(unused_mib_regs[i]));
1292 static void bcm_enet_update_mib_counters_defer(
struct work_struct *
t)
1298 update_mib_counters(priv);
1302 if (netif_running(priv->
net_dev))
1306 static void bcm_enet_get_ethtool_stats(
struct net_device *netdev,
1313 priv = netdev_priv(netdev);
1316 update_mib_counters(priv);
1322 s = &bcm_enet_gstrings_stats[
i];
1324 p = (
char *)&netdev->
stats;
1334 static int bcm_enet_get_settings(
struct net_device *dev,
1339 priv = netdev_priv(dev);
1365 static int bcm_enet_set_settings(
struct net_device *dev,
1370 priv = netdev_priv(dev);
1385 if (netif_running(dev))
1386 bcm_enet_adjust_link(dev);
1391 static void bcm_enet_get_ringparam(
struct net_device *dev,
1396 priv = netdev_priv(dev);
1405 static int bcm_enet_set_ringparam(
struct net_device *dev,
1411 priv = netdev_priv(dev);
1414 if (netif_running(dev)) {
1425 err = bcm_enet_open(dev);
1429 bcm_enet_set_multicast_list(dev);
1434 static void bcm_enet_get_pauseparam(
struct net_device *dev,
1439 priv = netdev_priv(dev);
1445 static int bcm_enet_set_pauseparam(
struct net_device *dev,
1450 priv = netdev_priv(dev);
1472 static const struct ethtool_ops bcm_enet_ethtool_ops = {
1473 .get_strings = bcm_enet_get_strings,
1474 .get_sset_count = bcm_enet_get_sset_count,
1475 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
1476 .get_settings = bcm_enet_get_settings,
1477 .set_settings = bcm_enet_set_settings,
1478 .get_drvinfo = bcm_enet_get_drvinfo,
1480 .get_ringparam = bcm_enet_get_ringparam,
1481 .set_ringparam = bcm_enet_set_ringparam,
1482 .get_pauseparam = bcm_enet_get_pauseparam,
1483 .set_pauseparam = bcm_enet_set_pauseparam,
1490 priv = netdev_priv(dev);
1499 mii.mdio_read = bcm_enet_mdio_read_mii;
1500 mii.mdio_write = bcm_enet_mdio_write_mii;
1502 mii.phy_id_mask = 0x3f;
1503 mii.reg_num_mask = 0x1f;
1511 static int compute_hw_mtu(
struct bcm_enet_priv *priv,
int mtu)
1529 priv->
hw_mtu = actual_mtu;
1543 static int bcm_enet_change_mtu(
struct net_device *dev,
int new_mtu)
1547 if (netif_running(dev))
1550 ret = compute_hw_mtu(netdev_priv(dev), new_mtu);
1566 bcm_enet_disable_mac(priv);
1600 .ndo_open = bcm_enet_open,
1601 .ndo_stop = bcm_enet_stop,
1602 .ndo_start_xmit = bcm_enet_start_xmit,
1603 .ndo_set_mac_address = bcm_enet_set_mac_address,
1604 .ndo_set_rx_mode = bcm_enet_set_multicast_list,
1605 .ndo_do_ioctl = bcm_enet_ioctl,
1606 .ndo_change_mtu = bcm_enet_change_mtu,
1607 #ifdef CONFIG_NET_POLL_CONTROLLER
1608 .ndo_poll_controller = bcm_enet_netpoll,
1620 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1622 const char *clk_name;
1628 if (!bcm_enet_shared_base)
1635 if (!res_mem || !res_irq || !res_irq_rx || !res_irq_tx)
1639 dev = alloc_etherdev(
sizeof(*priv));
1642 priv = netdev_priv(dev);
1644 ret = compute_hw_mtu(priv, dev->
mtu);
1648 iomem_size = resource_size(res_mem);
1657 goto out_release_mem;
1686 pd = pdev->
dev.platform_data;
1707 goto out_put_clk_mac;
1713 bcm_enet_hw_preinit(priv);
1718 priv->
mii_bus = mdiobus_alloc();
1725 bus->
name =
"bcm63xx_enet MII bus";
1728 bus->
read = bcm_enet_mdio_read_phylib;
1729 bus->
write = bcm_enet_mdio_write_phylib;
1750 dev_err(&pdev->
dev,
"unable to register mdio bus\n");
1757 pd->
mii_config(dev, 1, bcm_enet_mdio_read_mii,
1758 bcm_enet_mdio_write_mii)) {
1759 dev_err(&pdev->
dev,
"unable to configure mdio bus\n");
1768 priv->
rx_timeout.function = bcm_enet_refill_rx_timer;
1788 goto out_unregister_mdio;
1791 platform_set_drvdata(pdev, dev);
1797 out_unregister_mdio:
1840 dev = platform_get_drvdata(pdev);
1841 priv = netdev_priv(dev);
1854 pd = pdev->
dev.platform_data;
1856 pd->
mii_config(dev, 0, bcm_enet_mdio_read_mii,
1857 bcm_enet_mdio_write_mii);
1873 platform_set_drvdata(pdev,
NULL);
1879 .probe = bcm_enet_probe,
1882 .name =
"bcm63xx_enet",
1899 iomem_size = resource_size(res);
1903 bcm_enet_shared_base =
ioremap(res->
start, iomem_size);
1904 if (!bcm_enet_shared_base) {
1915 iounmap(bcm_enet_shared_base);
1926 .probe = bcm_enet_shared_probe,
1929 .name =
"bcm63xx_enet_shared",
1937 static int __init bcm_enet_init(
void)
1952 static void __exit bcm_enet_exit(
void)