32 #include <linux/kernel.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
46 #define DEBUG_PKT_BYTES 0
50 #define DRV_NAME "ixp4xx_eth"
56 #define TXDONE_QUEUE_LEN 64
58 #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
59 #define REGS_SIZE 0x1000
61 #define RX_BUFF_SIZE ALIGN((NET_IP_ALIGN) + MAX_MRU, 4)
63 #define NAPI_WEIGHT 16
64 #define MDIO_INTERVAL (3 * HZ)
65 #define MAX_MDIO_RETRIES 100
66 #define MAX_CLOSE_WAIT 1000
68 #define NPE_ID(port_id) ((port_id) >> 4)
69 #define PHYSICAL_ID(port_id) ((NPE_ID(port_id) + 2) % 3)
70 #define TX_QUEUE(port_id) (NPE_ID(port_id) + 23)
71 #define RXFREE_QUEUE(port_id) (NPE_ID(port_id) + 26)
72 #define TXDONE_QUEUE 31
74 #define PTP_SLAVE_MODE 1
75 #define PTP_MASTER_MODE 2
76 #define PORT2CHANNEL(p) NPE_ID(p->id)
79 #define TX_CNTRL0_TX_EN 0x01
80 #define TX_CNTRL0_HALFDUPLEX 0x02
81 #define TX_CNTRL0_RETRY 0x04
82 #define TX_CNTRL0_PAD_EN 0x08
83 #define TX_CNTRL0_APPEND_FCS 0x10
84 #define TX_CNTRL0_2DEFER 0x20
85 #define TX_CNTRL0_RMII 0x40
86 #define TX_CNTRL1_RETRIES 0x0F
89 #define RX_CNTRL0_RX_EN 0x01
90 #define RX_CNTRL0_PADSTRIP_EN 0x02
91 #define RX_CNTRL0_SEND_FCS 0x04
92 #define RX_CNTRL0_PAUSE_EN 0x08
93 #define RX_CNTRL0_LOOP_EN 0x10
94 #define RX_CNTRL0_ADDR_FLTR_EN 0x20
95 #define RX_CNTRL0_RX_RUNT_EN 0x40
96 #define RX_CNTRL0_BCAST_DIS 0x80
97 #define RX_CNTRL1_DEFER_EN 0x01
100 #define CORE_RESET 0x01
101 #define CORE_RX_FIFO_FLUSH 0x02
102 #define CORE_TX_FIFO_FLUSH 0x04
103 #define CORE_SEND_JAM 0x08
104 #define CORE_MDC_EN 0x10
106 #define DEFAULT_TX_CNTRL0 (TX_CNTRL0_TX_EN | TX_CNTRL0_RETRY | \
107 TX_CNTRL0_PAD_EN | TX_CNTRL0_APPEND_FCS | \
109 #define DEFAULT_RX_CNTRL0 RX_CNTRL0_RX_EN
110 #define DEFAULT_CORE_CNTRL CORE_MDC_EN
114 #define NPE_GETSTATUS 0x00
115 #define NPE_EDB_SETPORTADDRESS 0x01
116 #define NPE_EDB_GETMACADDRESSDATABASE 0x02
117 #define NPE_EDB_SETMACADDRESSSDATABASE 0x03
118 #define NPE_GETSTATS 0x04
119 #define NPE_RESETSTATS 0x05
120 #define NPE_SETMAXFRAMELENGTHS 0x06
121 #define NPE_VLAN_SETRXTAGMODE 0x07
122 #define NPE_VLAN_SETDEFAULTRXVID 0x08
123 #define NPE_VLAN_SETPORTVLANTABLEENTRY 0x09
124 #define NPE_VLAN_SETPORTVLANTABLERANGE 0x0A
125 #define NPE_VLAN_SETRXQOSENTRY 0x0B
126 #define NPE_VLAN_SETPORTIDEXTRACTIONMODE 0x0C
127 #define NPE_STP_SETBLOCKINGSTATE 0x0D
128 #define NPE_FW_SETFIREWALLMODE 0x0E
129 #define NPE_PC_SETFRAMECONTROLDURATIONID 0x0F
130 #define NPE_PC_SETAPMACTABLE 0x11
131 #define NPE_SETLOOPBACK_MODE 0x12
132 #define NPE_PC_SETBSSIDTABLE 0x13
133 #define NPE_ADDRESS_FILTER_CONFIG 0x14
134 #define NPE_APPENDFCSCONFIG 0x15
135 #define NPE_NOTIFY_MAC_RECOVERY_DONE 0x16
136 #define NPE_MAC_RECOVERY_START 0x17
141 #define free_buffer dev_kfree_skb
142 #define free_buffer_irq dev_kfree_skb_irq
145 #define free_buffer kfree
146 #define free_buffer_irq kfree
235 #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
236 (n) * sizeof(struct desc))
237 #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
239 #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
240 ((n) + RX_DESCS) * sizeof(struct desc))
241 #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
247 for (i = 0; i <
cnt; i++)
254 static struct mii_bus *mdio_bus;
255 static int ports_open;
281 memcpy(&lo, &hi[1],
sizeof(lo));
283 return (uid_hi ==
ntohs(*hi) &&
284 uid_lo ==
ntohl(lo) &&
285 seqid ==
ntohs(*
id));
312 seq = (hi >> 16) & 0xffff;
319 ns = ((
u64) hi) << 32;
323 shhwtstamps = skb_hwtstamps(skb);
324 memset(shhwtstamps, 0,
sizeof(*shhwtstamps));
325 shhwtstamps->
hwtstamp = ns_to_ktime(ns);
330 static void ixp_tx_timestamp(
struct port *port,
struct sk_buff *skb)
338 shtx = skb_shinfo(skb);
352 for (cnt = 0; cnt < 100; cnt++) {
365 ns = ((
u64) hi) << 32;
369 memset(&shhwtstamps, 0,
sizeof(shhwtstamps));
370 shhwtstamps.
hwtstamp = ns_to_ktime(ns);
380 struct port *port = netdev_priv(netdev);
392 switch (
cfg.tx_type) {
403 switch (
cfg.rx_filter) {
459 phy_id, write ?
"write" :
"read", cycles);
477 static int ixp4xx_mdio_read(
struct mii_bus *bus,
int phy_id,
int location)
483 ret = ixp4xx_mdio_cmd(bus, phy_id, location, 0, 0);
484 spin_unlock_irqrestore(&mdio_lock, flags);
487 phy_id, location, ret);
492 static int ixp4xx_mdio_write(
struct mii_bus *bus,
int phy_id,
int location,
499 ret = ixp4xx_mdio_cmd(bus, phy_id, location, 1, val);
500 spin_unlock_irqrestore(&mdio_lock, flags);
503 bus->
name, phy_id, location, val, ret);
508 static int ixp4xx_mdio_register(
void)
512 if (!(mdio_bus = mdiobus_alloc()))
529 mdio_bus->
name =
"IXP4xx MII Bus";
530 mdio_bus->
read = &ixp4xx_mdio_read;
531 mdio_bus->
write = &ixp4xx_mdio_write;
539 static void ixp4xx_mdio_remove(
void)
548 struct port *port = netdev_priv(dev);
567 &port->
regs->tx_control[0]);
570 &port->
regs->tx_control[0]);
577 static inline void debug_pkt(
struct net_device *dev,
const char *
func,
584 for (i = 0; i < len; i++) {
588 ((i == 6) || (i == 12) || (i >= 14)) ?
" " :
"",
600 " %X %X %02X%02X%02X%02X%02X%02X < %02X%02X%02X%02X%02X%02X\n",
611 static inline int queue_get_desc(
unsigned int queue,
struct port *port,
617 if (!(phys = qmgr_get_entry(queue)))
623 n_desc = (phys - tab_phys) /
sizeof(
struct desc);
625 debug_desc(phys, &tab[n_desc]);
630 static inline void queue_put_desc(
unsigned int queue,
u32 phys,
633 debug_desc(phys, desc);
635 qmgr_put_entry(queue, phys);
641 static inline void dma_unmap_tx(
struct port *port,
struct desc *
desc)
654 static void eth_rx_irq(
void *pdev)
657 struct port *port = netdev_priv(dev);
663 napi_schedule(&port->
napi);
668 struct port *port =
container_of(napi,
struct port, napi);
677 while (received < budget) {
686 if ((n = queue_get_desc(rxq, port, 0)) < 0) {
693 if (!qmgr_stat_below_low_watermark(rxq) &&
694 napi_reschedule(napi)) {
697 " napi_reschedule successed\n",
722 skb = netdev_alloc_skb(dev,
727 dev->
stats.rx_dropped++;
750 debug_pkt(dev,
"eth_poll", skb->
data, skb->
len);
752 ixp_rx_timestamp(port, skb);
754 dev->
stats.rx_packets++;
776 static void eth_txdone_irq(
void *
unused)
791 port = npe_port_tab[npe_id];
794 n_desc = (phys -
tx_desc_phys(port, 0)) /
sizeof(
struct desc);
797 debug_desc(phys, desc);
800 port->
netdev->stats.tx_packets++;
803 dma_unmap_tx(port, desc);
812 start = qmgr_stat_below_low_watermark(port->
plat->txreadyq);
813 queue_put_desc(port->
plat->txreadyq, phys, desc);
819 netif_wake_queue(port->
netdev);
826 struct port *port = netdev_priv(dev);
827 unsigned int txreadyq = port->
plat->txreadyq;
839 dev->
stats.tx_errors++;
843 debug_pkt(dev,
"eth_xmit", skb->
data, skb->
len);
852 bytes =
ALIGN(offset + len, 4);
855 dev->
stats.tx_dropped++;
858 memcpy_swab32(mem, (
u32 *)((
int)skb->
data & ~3), bytes / 4);
867 dev->
stats.tx_dropped++;
871 n = queue_get_desc(txreadyq, port, 1);
885 queue_put_desc(
TX_QUEUE(port->
id), tx_desc_phys(port, n), desc);
887 if (qmgr_stat_below_low_watermark(txreadyq)) {
891 netif_stop_queue(dev);
894 if (!qmgr_stat_below_low_watermark(txreadyq)) {
899 netif_wake_queue(dev);
907 ixp_tx_timestamp(port, skb);
908 skb_tx_timestamp(skb);
917 static void eth_set_mcast_list(
struct net_device *dev)
919 struct port *port = netdev_priv(dev);
923 static const u8 allmulti[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
931 &port->
regs->rx_control[0]);
937 &port->
regs->rx_control[0]);
948 diffs[i] |= addr[i] ^ ha->
addr[i];
957 &port->
regs->rx_control[0]);
963 struct port *port = netdev_priv(dev);
965 if (!netif_running(dev))
969 return hwtstamp_ioctl(dev, req, cmd);
976 static void ixp4xx_get_drvinfo(
struct net_device *dev,
979 struct port *port = netdev_priv(dev);
989 struct port *port = netdev_priv(dev);
995 struct port *port = netdev_priv(dev);
999 static int ixp4xx_nway_reset(
struct net_device *dev)
1001 struct port *port = netdev_priv(dev);
1008 static int ixp4xx_get_ts_info(
struct net_device *dev,
1034 static const struct ethtool_ops ixp4xx_ethtool_ops = {
1035 .get_drvinfo = ixp4xx_get_drvinfo,
1036 .get_settings = ixp4xx_get_settings,
1037 .set_settings = ixp4xx_set_settings,
1038 .nway_reset = ixp4xx_nway_reset,
1040 .get_ts_info = ixp4xx_get_ts_info,
1044 static int request_queues(
struct port *port)
1049 "%s:RX-free", port->
netdev->name);
1054 "%s:RX", port->
netdev->name);
1059 "%s:TX", port->
netdev->name);
1064 "%s:TX-ready", port->
netdev->name);
1090 static void release_queues(
struct port *port)
1101 static int init_queues(
struct port *port)
1147 static void destroy_queues(
struct port *port)
1166 dma_unmap_tx(port, desc);
1174 if (!ports_open && dma_pool) {
1182 struct port *port = netdev_priv(dev);
1208 for (i = 0; i < 8; i++) {
1231 if ((err = request_queues(port)) != 0)
1234 if ((err = init_queues(port)) != 0) {
1235 destroy_queues(port);
1236 release_queues(port);
1257 queue_put_desc(port->
plat->txreadyq,
1269 napi_enable(&port->
napi);
1270 eth_set_mcast_list(dev);
1271 netif_start_queue(dev);
1277 eth_txdone_irq,
NULL);
1282 napi_schedule(&port->
napi);
1288 struct port *port = netdev_priv(dev);
1295 napi_disable(&port->
napi);
1296 netif_stop_queue(dev);
1310 while (queue_get_desc(port->
plat->rxq, port, 0) >= 0)
1318 int n = queue_get_desc(port->
plat->txreadyq, port, 1);
1324 queue_put_desc(
TX_QUEUE(port->
id), phys, desc);
1331 " left in NPE\n", dev->
name, buffs);
1338 while (queue_get_desc(
TX_QUEUE(port->
id), port, 1) >= 0)
1343 while (queue_get_desc(port->
plat->txreadyq, port, 1) >= 0)
1351 "left in NPE\n", dev->
name, buffs);
1366 destroy_queues(port);
1367 release_queues(port);
1372 .ndo_open = eth_open,
1373 .ndo_stop = eth_close,
1374 .ndo_start_xmit = eth_xmit,
1375 .ndo_set_rx_mode = eth_set_mcast_list,
1376 .ndo_do_ioctl = eth_ioctl,
1391 if (ptp_filter_init(ptp_filter,
ARRAY_SIZE(ptp_filter))) {
1392 pr_err(
"ixp4xx_eth: bad ptp filter\n");
1396 if (!(dev = alloc_etherdev(
sizeof(
struct port))))
1400 port = netdev_priv(dev);
1402 port->
id = pdev->
id;
1443 platform_set_drvdata(pdev, dev);
1446 &port->
regs->core_control);
1452 mdio_bus->
id, plat->
phy);
1455 if (IS_ERR(port->
phydev)) {
1456 err = PTR_ERR(port->
phydev);
1466 npe_name(port->
npe));
1474 platform_set_drvdata(pdev,
NULL);
1485 struct net_device *dev = platform_get_drvdata(pdev);
1486 struct port *port = netdev_priv(dev);
1491 platform_set_drvdata(pdev,
NULL);
1500 .probe = eth_init_one,
1501 .remove = eth_remove_one,
1504 static int __init eth_init_module(
void)
1507 if ((err = ixp4xx_mdio_register()))
1512 static void __exit eth_cleanup_module(
void)
1515 ixp4xx_mdio_remove();