2 * Intel IXP4xx Ethernet driver for Linux
4 * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License
8 * as published by the Free Software Foundation.
10 * Ethernet port config (0x00 is not present on IXP42X):
12 * logical port 0x00 0x10 0x20
13 * NPE 0 (NPE-A) 1 (NPE-B) 2 (NPE-C)
14 * physical PortId 2 0 1
16 * RX-free queue 26 27 28
17 * TX-done queue is always 31, per-port RX and TX-ready queues are configurable
21 * bits 0 -> 1 - NPE ID (RX and TX-done)
22 * bits 0 -> 2 - priority (TX, per 802.1D)
23 * bits 3 -> 4 - port ID (user-set?)
24 * bits 5 -> 31 - physical descriptor address
27 #include <linux/delay.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/dmapool.h>
30 #include <linux/etherdevice.h>
32 #include <linux/kernel.h>
33 #include <linux/phy.h>
34 #include <linux/platform_device.h>
36 #include <mach/qmgr.h>
41 #define DEBUG_PKT_BYTES 0
45 #define DRV_NAME "ixp4xx_eth"
49 #define RX_DESCS 64 /* also length of all RX queues */
50 #define TX_DESCS 16 /* also length of all TX queues */
51 #define TXDONE_QUEUE_LEN 64 /* dwords */
53 #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
54 #define REGS_SIZE 0x1000
55 #define MAX_MRU 1536 /* 0x600 */
56 #define RX_BUFF_SIZE ALIGN((NET_IP_ALIGN) + MAX_MRU, 4)
58 #define NAPI_WEIGHT 16
59 #define MDIO_INTERVAL (3 * HZ)
60 #define MAX_MDIO_RETRIES 100 /* microseconds, typically 30 cycles */
61 #define MAX_CLOSE_WAIT 1000 /* microseconds, typically 2-3 cycles */
63 #define NPE_ID(port_id) ((port_id) >> 4)
64 #define PHYSICAL_ID(port_id) ((NPE_ID(port_id) + 2) % 3)
65 #define TX_QUEUE(port_id) (NPE_ID(port_id) + 23)
66 #define RXFREE_QUEUE(port_id) (NPE_ID(port_id) + 26)
67 #define TXDONE_QUEUE 31
69 /* TX Control Registers */
70 #define TX_CNTRL0_TX_EN 0x01
71 #define TX_CNTRL0_HALFDUPLEX 0x02
72 #define TX_CNTRL0_RETRY 0x04
73 #define TX_CNTRL0_PAD_EN 0x08
74 #define TX_CNTRL0_APPEND_FCS 0x10
75 #define TX_CNTRL0_2DEFER 0x20
76 #define TX_CNTRL0_RMII 0x40 /* reduced MII */
77 #define TX_CNTRL1_RETRIES 0x0F /* 4 bits */
79 /* RX Control Registers */
80 #define RX_CNTRL0_RX_EN 0x01
81 #define RX_CNTRL0_PADSTRIP_EN 0x02
82 #define RX_CNTRL0_SEND_FCS 0x04
83 #define RX_CNTRL0_PAUSE_EN 0x08
84 #define RX_CNTRL0_LOOP_EN 0x10
85 #define RX_CNTRL0_ADDR_FLTR_EN 0x20
86 #define RX_CNTRL0_RX_RUNT_EN 0x40
87 #define RX_CNTRL0_BCAST_DIS 0x80
88 #define RX_CNTRL1_DEFER_EN 0x01
90 /* Core Control Register */
91 #define CORE_RESET 0x01
92 #define CORE_RX_FIFO_FLUSH 0x02
93 #define CORE_TX_FIFO_FLUSH 0x04
94 #define CORE_SEND_JAM 0x08
95 #define CORE_MDC_EN 0x10 /* MDIO using NPE-B ETH-0 only */
97 #define DEFAULT_TX_CNTRL0 (TX_CNTRL0_TX_EN | TX_CNTRL0_RETRY | \
98 TX_CNTRL0_PAD_EN | TX_CNTRL0_APPEND_FCS | \
100 #define DEFAULT_RX_CNTRL0 RX_CNTRL0_RX_EN
101 #define DEFAULT_CORE_CNTRL CORE_MDC_EN
104 /* NPE message codes */
105 #define NPE_GETSTATUS 0x00
106 #define NPE_EDB_SETPORTADDRESS 0x01
107 #define NPE_EDB_GETMACADDRESSDATABASE 0x02
108 #define NPE_EDB_SETMACADDRESSSDATABASE 0x03
109 #define NPE_GETSTATS 0x04
110 #define NPE_RESETSTATS 0x05
111 #define NPE_SETMAXFRAMELENGTHS 0x06
112 #define NPE_VLAN_SETRXTAGMODE 0x07
113 #define NPE_VLAN_SETDEFAULTRXVID 0x08
114 #define NPE_VLAN_SETPORTVLANTABLEENTRY 0x09
115 #define NPE_VLAN_SETPORTVLANTABLERANGE 0x0A
116 #define NPE_VLAN_SETRXQOSENTRY 0x0B
117 #define NPE_VLAN_SETPORTIDEXTRACTIONMODE 0x0C
118 #define NPE_STP_SETBLOCKINGSTATE 0x0D
119 #define NPE_FW_SETFIREWALLMODE 0x0E
120 #define NPE_PC_SETFRAMECONTROLDURATIONID 0x0F
121 #define NPE_PC_SETAPMACTABLE 0x11
122 #define NPE_SETLOOPBACK_MODE 0x12
123 #define NPE_PC_SETBSSIDTABLE 0x13
124 #define NPE_ADDRESS_FILTER_CONFIG 0x14
125 #define NPE_APPENDFCSCONFIG 0x15
126 #define NPE_NOTIFY_MAC_RECOVERY_DONE 0x16
127 #define NPE_MAC_RECOVERY_START 0x17
131 typedef struct sk_buff buffer_t;
132 #define free_buffer dev_kfree_skb
133 #define free_buffer_irq dev_kfree_skb_irq
135 typedef void buffer_t;
136 #define free_buffer kfree
137 #define free_buffer_irq kfree
141 u32 tx_control[2], __res1[2]; /* 000 */
142 u32 rx_control[2], __res2[2]; /* 010 */
143 u32 random_seed, __res3[3]; /* 020 */
144 u32 partial_empty_threshold, __res4; /* 030 */
145 u32 partial_full_threshold, __res5; /* 038 */
146 u32 tx_start_bytes, __res6[3]; /* 040 */
147 u32 tx_deferral, rx_deferral, __res7[2];/* 050 */
148 u32 tx_2part_deferral[2], __res8[2]; /* 060 */
149 u32 slot_time, __res9[3]; /* 070 */
150 u32 mdio_command[4]; /* 080 */
151 u32 mdio_status[4]; /* 090 */
152 u32 mcast_mask[6], __res10[2]; /* 0A0 */
153 u32 mcast_addr[6], __res11[2]; /* 0C0 */
154 u32 int_clock_threshold, __res12[3]; /* 0E0 */
155 u32 hw_addr[6], __res13[61]; /* 0F0 */
156 u32 core_control; /* 1FC */
160 struct resource *mem_res;
161 struct eth_regs __iomem *regs;
163 struct net_device *netdev;
164 struct napi_struct napi;
165 struct phy_device *phydev;
166 struct eth_plat_info *plat;
167 buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
168 struct desc *desc_tab; /* coherent */
170 int id; /* logical port ID */
175 /* NPE message structure */
178 u8 cmd, eth_id, byte2, byte3;
179 u8 byte4, byte5, byte6, byte7;
181 u8 byte3, byte2, eth_id, cmd;
182 u8 byte7, byte6, byte5, byte4;
186 /* Ethernet packet descriptor */
188 u32 next; /* pointer to next buffer, unused */
191 u16 buf_len; /* buffer length */
192 u16 pkt_len; /* packet length */
193 u32 data; /* pointer to data buffer in RAM */
201 u16 pkt_len; /* packet length */
202 u16 buf_len; /* buffer length */
203 u32 data; /* pointer to data buffer in RAM */
213 u8 dst_mac_0, dst_mac_1, dst_mac_2, dst_mac_3;
214 u8 dst_mac_4, dst_mac_5, src_mac_0, src_mac_1;
215 u8 src_mac_2, src_mac_3, src_mac_4, src_mac_5;
217 u8 dst_mac_3, dst_mac_2, dst_mac_1, dst_mac_0;
218 u8 src_mac_1, src_mac_0, dst_mac_5, dst_mac_4;
219 u8 src_mac_5, src_mac_4, src_mac_3, src_mac_2;
224 #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
225 (n) * sizeof(struct desc))
226 #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
228 #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
229 ((n) + RX_DESCS) * sizeof(struct desc))
230 #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
233 static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
236 for (i = 0; i < cnt; i++)
237 dest[i] = swab32(src[i]);
241 static spinlock_t mdio_lock;
242 static struct eth_regs __iomem *mdio_regs; /* mdio command and status only */
243 struct mii_bus *mdio_bus;
244 static int ports_open;
245 static struct port *npe_port_tab[MAX_NPES];
246 static struct dma_pool *dma_pool;
249 static int ixp4xx_mdio_cmd(struct mii_bus *bus, int phy_id, int location,
254 if (__raw_readl(&mdio_regs->mdio_command[3]) & 0x80) {
255 printk(KERN_ERR "%s: MII not ready to transmit\n", bus->name);
260 __raw_writel(cmd & 0xFF, &mdio_regs->mdio_command[0]);
261 __raw_writel(cmd >> 8, &mdio_regs->mdio_command[1]);
263 __raw_writel(((phy_id << 5) | location) & 0xFF,
264 &mdio_regs->mdio_command[2]);
265 __raw_writel((phy_id >> 3) | (write << 2) | 0x80 /* GO */,
266 &mdio_regs->mdio_command[3]);
268 while ((cycles < MAX_MDIO_RETRIES) &&
269 (__raw_readl(&mdio_regs->mdio_command[3]) & 0x80)) {
274 if (cycles == MAX_MDIO_RETRIES) {
275 printk(KERN_ERR "%s #%i: MII write failed\n", bus->name,
281 printk(KERN_DEBUG "%s #%i: mdio_%s() took %i cycles\n", bus->name,
282 phy_id, write ? "write" : "read", cycles);
288 if (__raw_readl(&mdio_regs->mdio_status[3]) & 0x80) {
290 printk(KERN_DEBUG "%s #%i: MII read failed\n", bus->name,
293 return 0xFFFF; /* don't return error */
296 return (__raw_readl(&mdio_regs->mdio_status[0]) & 0xFF) |
297 ((__raw_readl(&mdio_regs->mdio_status[1]) & 0xFF) << 8);
300 static int ixp4xx_mdio_read(struct mii_bus *bus, int phy_id, int location)
305 spin_lock_irqsave(&mdio_lock, flags);
306 ret = ixp4xx_mdio_cmd(bus, phy_id, location, 0, 0);
307 spin_unlock_irqrestore(&mdio_lock, flags);
309 printk(KERN_DEBUG "%s #%i: MII read [%i] -> 0x%X\n", bus->name,
310 phy_id, location, ret);
315 static int ixp4xx_mdio_write(struct mii_bus *bus, int phy_id, int location,
321 spin_lock_irqsave(&mdio_lock, flags);
322 ret = ixp4xx_mdio_cmd(bus, phy_id, location, 1, val);
323 spin_unlock_irqrestore(&mdio_lock, flags);
325 printk(KERN_DEBUG "%s #%i: MII read [%i] <- 0x%X, err = %i\n",
326 bus->name, phy_id, location, val, ret);
331 static int ixp4xx_mdio_register(void)
335 if (!(mdio_bus = mdiobus_alloc()))
338 if (cpu_is_ixp43x()) {
339 /* IXP43x lacks NPE-B and uses NPE-C for MII PHY access */
340 if (!(ixp4xx_read_feature_bits() & IXP4XX_FEATURE_NPEC_ETH))
342 mdio_regs = (struct eth_regs __iomem *)IXP4XX_EthC_BASE_VIRT;
344 /* All MII PHY accesses use NPE-B Ethernet registers */
345 if (!(ixp4xx_read_feature_bits() & IXP4XX_FEATURE_NPEB_ETH0))
347 mdio_regs = (struct eth_regs __iomem *)IXP4XX_EthB_BASE_VIRT;
350 __raw_writel(DEFAULT_CORE_CNTRL, &mdio_regs->core_control);
351 spin_lock_init(&mdio_lock);
352 mdio_bus->name = "IXP4xx MII Bus";
353 mdio_bus->read = &ixp4xx_mdio_read;
354 mdio_bus->write = &ixp4xx_mdio_write;
355 strcpy(mdio_bus->id, "0");
357 if ((err = mdiobus_register(mdio_bus)))
358 mdiobus_free(mdio_bus);
362 static void ixp4xx_mdio_remove(void)
364 mdiobus_unregister(mdio_bus);
365 mdiobus_free(mdio_bus);
369 static void ixp4xx_adjust_link(struct net_device *dev)
371 struct port *port = netdev_priv(dev);
372 struct phy_device *phydev = port->phydev;
377 printk(KERN_INFO "%s: link down\n", dev->name);
382 if (port->speed == phydev->speed && port->duplex == phydev->duplex)
385 port->speed = phydev->speed;
386 port->duplex = phydev->duplex;
389 __raw_writel(DEFAULT_TX_CNTRL0 & ~TX_CNTRL0_HALFDUPLEX,
390 &port->regs->tx_control[0]);
392 __raw_writel(DEFAULT_TX_CNTRL0 | TX_CNTRL0_HALFDUPLEX,
393 &port->regs->tx_control[0]);
395 printk(KERN_INFO "%s: link up, speed %u Mb/s, %s duplex\n",
396 dev->name, port->speed, port->duplex ? "full" : "half");
400 static inline void debug_pkt(struct net_device *dev, const char *func,
406 printk(KERN_DEBUG "%s: %s(%i) ", dev->name, func, len);
407 for (i = 0; i < len; i++) {
408 if (i >= DEBUG_PKT_BYTES)
411 ((i == 6) || (i == 12) || (i >= 14)) ? " " : "",
419 static inline void debug_desc(u32 phys, struct desc *desc)
422 printk(KERN_DEBUG "%X: %X %3X %3X %08X %2X < %2X %4X %X"
423 " %X %X %02X%02X%02X%02X%02X%02X < %02X%02X%02X%02X%02X%02X\n",
424 phys, desc->next, desc->buf_len, desc->pkt_len,
425 desc->data, desc->dest_id, desc->src_id, desc->flags,
426 desc->qos, desc->padlen, desc->vlan_tci,
427 desc->dst_mac_0, desc->dst_mac_1, desc->dst_mac_2,
428 desc->dst_mac_3, desc->dst_mac_4, desc->dst_mac_5,
429 desc->src_mac_0, desc->src_mac_1, desc->src_mac_2,
430 desc->src_mac_3, desc->src_mac_4, desc->src_mac_5);
434 static inline int queue_get_desc(unsigned int queue, struct port *port,
437 u32 phys, tab_phys, n_desc;
440 if (!(phys = qmgr_get_entry(queue)))
443 phys &= ~0x1F; /* mask out non-address bits */
444 tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
445 tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
446 n_desc = (phys - tab_phys) / sizeof(struct desc);
447 BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
448 debug_desc(phys, &tab[n_desc]);
449 BUG_ON(tab[n_desc].next);
453 static inline void queue_put_desc(unsigned int queue, u32 phys,
456 debug_desc(phys, desc);
458 qmgr_put_entry(queue, phys);
459 /* Don't check for queue overflow here, we've allocated sufficient
460 length and queues >= 32 don't support this check anyway. */
464 static inline void dma_unmap_tx(struct port *port, struct desc *desc)
467 dma_unmap_single(&port->netdev->dev, desc->data,
468 desc->buf_len, DMA_TO_DEVICE);
470 dma_unmap_single(&port->netdev->dev, desc->data & ~3,
471 ALIGN((desc->data & 3) + desc->buf_len, 4),
477 static void eth_rx_irq(void *pdev)
479 struct net_device *dev = pdev;
480 struct port *port = netdev_priv(dev);
483 printk(KERN_DEBUG "%s: eth_rx_irq\n", dev->name);
485 qmgr_disable_irq(port->plat->rxq);
486 napi_schedule(&port->napi);
489 static int eth_poll(struct napi_struct *napi, int budget)
491 struct port *port = container_of(napi, struct port, napi);
492 struct net_device *dev = port->netdev;
493 unsigned int rxq = port->plat->rxq, rxfreeq = RXFREE_QUEUE(port->id);
497 printk(KERN_DEBUG "%s: eth_poll\n", dev->name);
500 while (received < budget) {
505 struct sk_buff *temp;
509 if ((n = queue_get_desc(rxq, port, 0)) < 0) {
511 printk(KERN_DEBUG "%s: eth_poll napi_complete\n",
515 qmgr_enable_irq(rxq);
516 if (!qmgr_stat_below_low_watermark(rxq) &&
517 napi_reschedule(napi)) { /* not empty again */
519 printk(KERN_DEBUG "%s: eth_poll"
520 " napi_reschedule successed\n",
523 qmgr_disable_irq(rxq);
527 printk(KERN_DEBUG "%s: eth_poll all done\n",
530 return received; /* all work done */
533 desc = rx_desc_ptr(port, n);
536 if ((skb = netdev_alloc_skb(dev, RX_BUFF_SIZE))) {
537 phys = dma_map_single(&dev->dev, skb->data,
538 RX_BUFF_SIZE, DMA_FROM_DEVICE);
539 if (dma_mapping_error(&dev->dev, phys)) {
545 skb = netdev_alloc_skb(dev,
546 ALIGN(NET_IP_ALIGN + desc->pkt_len, 4));
550 dev->stats.rx_dropped++;
551 /* put the desc back on RX-ready queue */
552 desc->buf_len = MAX_MRU;
554 queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
558 /* process received frame */
561 skb = port->rx_buff_tab[n];
562 dma_unmap_single(&dev->dev, desc->data - NET_IP_ALIGN,
563 RX_BUFF_SIZE, DMA_FROM_DEVICE);
565 dma_sync_single_for_cpu(&dev->dev, desc->data - NET_IP_ALIGN,
566 RX_BUFF_SIZE, DMA_FROM_DEVICE);
567 memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
568 ALIGN(NET_IP_ALIGN + desc->pkt_len, 4) / 4);
570 skb_reserve(skb, NET_IP_ALIGN);
571 skb_put(skb, desc->pkt_len);
573 debug_pkt(dev, "eth_poll", skb->data, skb->len);
575 skb->protocol = eth_type_trans(skb, dev);
576 dev->stats.rx_packets++;
577 dev->stats.rx_bytes += skb->len;
578 netif_receive_skb(skb);
580 /* put the new buffer on RX-free queue */
582 port->rx_buff_tab[n] = temp;
583 desc->data = phys + NET_IP_ALIGN;
585 desc->buf_len = MAX_MRU;
587 queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
592 printk(KERN_DEBUG "eth_poll(): end, not all work done\n");
594 return received; /* not all work done */
598 static void eth_txdone_irq(void *unused)
603 printk(KERN_DEBUG DRV_NAME ": eth_txdone_irq\n");
605 while ((phys = qmgr_get_entry(TXDONE_QUEUE)) != 0) {
612 BUG_ON(npe_id >= MAX_NPES);
613 port = npe_port_tab[npe_id];
615 phys &= ~0x1F; /* mask out non-address bits */
616 n_desc = (phys - tx_desc_phys(port, 0)) / sizeof(struct desc);
617 BUG_ON(n_desc >= TX_DESCS);
618 desc = tx_desc_ptr(port, n_desc);
619 debug_desc(phys, desc);
621 if (port->tx_buff_tab[n_desc]) { /* not the draining packet */
622 port->netdev->stats.tx_packets++;
623 port->netdev->stats.tx_bytes += desc->pkt_len;
625 dma_unmap_tx(port, desc);
627 printk(KERN_DEBUG "%s: eth_txdone_irq free %p\n",
628 port->netdev->name, port->tx_buff_tab[n_desc]);
630 free_buffer_irq(port->tx_buff_tab[n_desc]);
631 port->tx_buff_tab[n_desc] = NULL;
634 start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
635 queue_put_desc(port->plat->txreadyq, phys, desc);
636 if (start) { /* TX-ready queue was empty */
638 printk(KERN_DEBUG "%s: eth_txdone_irq xmit ready\n",
641 netif_wake_queue(port->netdev);
646 static int eth_xmit(struct sk_buff *skb, struct net_device *dev)
648 struct port *port = netdev_priv(dev);
649 unsigned int txreadyq = port->plat->txreadyq;
650 int len, offset, bytes, n;
656 printk(KERN_DEBUG "%s: eth_xmit\n", dev->name);
659 if (unlikely(skb->len > MAX_MRU)) {
661 dev->stats.tx_errors++;
665 debug_pkt(dev, "eth_xmit", skb->data, skb->len);
669 offset = 0; /* no need to keep alignment */
673 offset = (int)skb->data & 3; /* keep 32-bit alignment */
674 bytes = ALIGN(offset + len, 4);
675 if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
677 dev->stats.tx_dropped++;
680 memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
684 phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
685 if (dma_mapping_error(&dev->dev, phys)) {
691 dev->stats.tx_dropped++;
695 n = queue_get_desc(txreadyq, port, 1);
697 desc = tx_desc_ptr(port, n);
700 port->tx_buff_tab[n] = skb;
702 port->tx_buff_tab[n] = mem;
704 desc->data = phys + offset;
705 desc->buf_len = desc->pkt_len = len;
707 /* NPE firmware pads short frames with zeros internally */
709 queue_put_desc(TX_QUEUE(port->id), tx_desc_phys(port, n), desc);
710 dev->trans_start = jiffies;
712 if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
714 printk(KERN_DEBUG "%s: eth_xmit queue full\n", dev->name);
716 netif_stop_queue(dev);
717 /* we could miss TX ready interrupt */
718 /* really empty in fact */
719 if (!qmgr_stat_below_low_watermark(txreadyq)) {
721 printk(KERN_DEBUG "%s: eth_xmit ready again\n",
724 netif_wake_queue(dev);
729 printk(KERN_DEBUG "%s: eth_xmit end\n", dev->name);
735 static void eth_set_mcast_list(struct net_device *dev)
737 struct port *port = netdev_priv(dev);
738 struct dev_mc_list *mclist = dev->mc_list;
739 u8 diffs[ETH_ALEN], *addr;
740 int cnt = dev->mc_count, i;
742 if ((dev->flags & IFF_PROMISC) || !mclist || !cnt) {
743 __raw_writel(DEFAULT_RX_CNTRL0 & ~RX_CNTRL0_ADDR_FLTR_EN,
744 &port->regs->rx_control[0]);
748 memset(diffs, 0, ETH_ALEN);
749 addr = mclist->dmi_addr; /* first MAC address */
751 while (--cnt && (mclist = mclist->next))
752 for (i = 0; i < ETH_ALEN; i++)
753 diffs[i] |= addr[i] ^ mclist->dmi_addr[i];
755 for (i = 0; i < ETH_ALEN; i++) {
756 __raw_writel(addr[i], &port->regs->mcast_addr[i]);
757 __raw_writel(~diffs[i], &port->regs->mcast_mask[i]);
760 __raw_writel(DEFAULT_RX_CNTRL0 | RX_CNTRL0_ADDR_FLTR_EN,
761 &port->regs->rx_control[0]);
765 static int eth_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
767 struct port *port = netdev_priv(dev);
769 if (!netif_running(dev))
771 return phy_mii_ioctl(port->phydev, if_mii(req), cmd);
774 /* ethtool support */
776 static void ixp4xx_get_drvinfo(struct net_device *dev,
777 struct ethtool_drvinfo *info)
779 struct port *port = netdev_priv(dev);
780 strcpy(info->driver, DRV_NAME);
781 snprintf(info->fw_version, sizeof(info->fw_version), "%u:%u:%u:%u",
782 port->firmware[0], port->firmware[1],
783 port->firmware[2], port->firmware[3]);
784 strcpy(info->bus_info, "internal");
787 static int ixp4xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
789 struct port *port = netdev_priv(dev);
790 return phy_ethtool_gset(port->phydev, cmd);
793 static int ixp4xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
795 struct port *port = netdev_priv(dev);
796 return phy_ethtool_sset(port->phydev, cmd);
799 static int ixp4xx_nway_reset(struct net_device *dev)
801 struct port *port = netdev_priv(dev);
802 return phy_start_aneg(port->phydev);
805 static struct ethtool_ops ixp4xx_ethtool_ops = {
806 .get_drvinfo = ixp4xx_get_drvinfo,
807 .get_settings = ixp4xx_get_settings,
808 .set_settings = ixp4xx_set_settings,
809 .nway_reset = ixp4xx_nway_reset,
810 .get_link = ethtool_op_get_link,
814 static int request_queues(struct port *port)
818 err = qmgr_request_queue(RXFREE_QUEUE(port->id), RX_DESCS, 0, 0,
819 "%s:RX-free", port->netdev->name);
823 err = qmgr_request_queue(port->plat->rxq, RX_DESCS, 0, 0,
824 "%s:RX", port->netdev->name);
828 err = qmgr_request_queue(TX_QUEUE(port->id), TX_DESCS, 0, 0,
829 "%s:TX", port->netdev->name);
833 err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
834 "%s:TX-ready", port->netdev->name);
838 /* TX-done queue handles skbs sent out by the NPEs */
840 err = qmgr_request_queue(TXDONE_QUEUE, TXDONE_QUEUE_LEN, 0, 0,
841 "%s:TX-done", DRV_NAME);
848 qmgr_release_queue(port->plat->txreadyq);
850 qmgr_release_queue(TX_QUEUE(port->id));
852 qmgr_release_queue(port->plat->rxq);
854 qmgr_release_queue(RXFREE_QUEUE(port->id));
855 printk(KERN_DEBUG "%s: unable to request hardware queues\n",
860 static void release_queues(struct port *port)
862 qmgr_release_queue(RXFREE_QUEUE(port->id));
863 qmgr_release_queue(port->plat->rxq);
864 qmgr_release_queue(TX_QUEUE(port->id));
865 qmgr_release_queue(port->plat->txreadyq);
868 qmgr_release_queue(TXDONE_QUEUE);
871 static int init_queues(struct port *port)
876 if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
877 POOL_ALLOC_SIZE, 32, 0)))
880 if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
881 &port->desc_tab_phys)))
883 memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
884 memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
885 memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
887 /* Setup RX buffers */
888 for (i = 0; i < RX_DESCS; i++) {
889 struct desc *desc = rx_desc_ptr(port, i);
890 buffer_t *buff; /* skb or kmalloc()ated memory */
893 if (!(buff = netdev_alloc_skb(port->netdev, RX_BUFF_SIZE)))
897 if (!(buff = kmalloc(RX_BUFF_SIZE, GFP_KERNEL)))
901 desc->buf_len = MAX_MRU;
902 desc->data = dma_map_single(&port->netdev->dev, data,
903 RX_BUFF_SIZE, DMA_FROM_DEVICE);
904 if (dma_mapping_error(&port->netdev->dev, desc->data)) {
908 desc->data += NET_IP_ALIGN;
909 port->rx_buff_tab[i] = buff;
915 static void destroy_queues(struct port *port)
919 if (port->desc_tab) {
920 for (i = 0; i < RX_DESCS; i++) {
921 struct desc *desc = rx_desc_ptr(port, i);
922 buffer_t *buff = port->rx_buff_tab[i];
924 dma_unmap_single(&port->netdev->dev,
925 desc->data - NET_IP_ALIGN,
926 RX_BUFF_SIZE, DMA_FROM_DEVICE);
930 for (i = 0; i < TX_DESCS; i++) {
931 struct desc *desc = tx_desc_ptr(port, i);
932 buffer_t *buff = port->tx_buff_tab[i];
934 dma_unmap_tx(port, desc);
938 dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
939 port->desc_tab = NULL;
942 if (!ports_open && dma_pool) {
943 dma_pool_destroy(dma_pool);
948 static int eth_open(struct net_device *dev)
950 struct port *port = netdev_priv(dev);
951 struct npe *npe = port->npe;
955 if (!npe_running(npe)) {
956 err = npe_load_firmware(npe, npe_name(npe), &dev->dev);
960 if (npe_recv_message(npe, &msg, "ETH_GET_STATUS")) {
961 printk(KERN_ERR "%s: %s not responding\n", dev->name,
965 port->firmware[0] = msg.byte4;
966 port->firmware[1] = msg.byte5;
967 port->firmware[2] = msg.byte6;
968 port->firmware[3] = msg.byte7;
971 memset(&msg, 0, sizeof(msg));
972 msg.cmd = NPE_VLAN_SETRXQOSENTRY;
973 msg.eth_id = port->id;
974 msg.byte5 = port->plat->rxq | 0x80;
975 msg.byte7 = port->plat->rxq << 4;
976 for (i = 0; i < 8; i++) {
978 if (npe_send_recv_message(port->npe, &msg, "ETH_SET_RXQ"))
982 msg.cmd = NPE_EDB_SETPORTADDRESS;
983 msg.eth_id = PHYSICAL_ID(port->id);
984 msg.byte2 = dev->dev_addr[0];
985 msg.byte3 = dev->dev_addr[1];
986 msg.byte4 = dev->dev_addr[2];
987 msg.byte5 = dev->dev_addr[3];
988 msg.byte6 = dev->dev_addr[4];
989 msg.byte7 = dev->dev_addr[5];
990 if (npe_send_recv_message(port->npe, &msg, "ETH_SET_MAC"))
993 memset(&msg, 0, sizeof(msg));
994 msg.cmd = NPE_FW_SETFIREWALLMODE;
995 msg.eth_id = port->id;
996 if (npe_send_recv_message(port->npe, &msg, "ETH_SET_FIREWALL_MODE"))
999 if ((err = request_queues(port)) != 0)
1002 if ((err = init_queues(port)) != 0) {
1003 destroy_queues(port);
1004 release_queues(port);
1008 port->speed = 0; /* force "link up" message */
1009 phy_start(port->phydev);
1011 for (i = 0; i < ETH_ALEN; i++)
1012 __raw_writel(dev->dev_addr[i], &port->regs->hw_addr[i]);
1013 __raw_writel(0x08, &port->regs->random_seed);
1014 __raw_writel(0x12, &port->regs->partial_empty_threshold);
1015 __raw_writel(0x30, &port->regs->partial_full_threshold);
1016 __raw_writel(0x08, &port->regs->tx_start_bytes);
1017 __raw_writel(0x15, &port->regs->tx_deferral);
1018 __raw_writel(0x08, &port->regs->tx_2part_deferral[0]);
1019 __raw_writel(0x07, &port->regs->tx_2part_deferral[1]);
1020 __raw_writel(0x80, &port->regs->slot_time);
1021 __raw_writel(0x01, &port->regs->int_clock_threshold);
1023 /* Populate queues with buffers, no failure after this point */
1024 for (i = 0; i < TX_DESCS; i++)
1025 queue_put_desc(port->plat->txreadyq,
1026 tx_desc_phys(port, i), tx_desc_ptr(port, i));
1028 for (i = 0; i < RX_DESCS; i++)
1029 queue_put_desc(RXFREE_QUEUE(port->id),
1030 rx_desc_phys(port, i), rx_desc_ptr(port, i));
1032 __raw_writel(TX_CNTRL1_RETRIES, &port->regs->tx_control[1]);
1033 __raw_writel(DEFAULT_TX_CNTRL0, &port->regs->tx_control[0]);
1034 __raw_writel(0, &port->regs->rx_control[1]);
1035 __raw_writel(DEFAULT_RX_CNTRL0, &port->regs->rx_control[0]);
1037 napi_enable(&port->napi);
1038 eth_set_mcast_list(dev);
1039 netif_start_queue(dev);
1041 qmgr_set_irq(port->plat->rxq, QUEUE_IRQ_SRC_NOT_EMPTY,
1044 qmgr_set_irq(TXDONE_QUEUE, QUEUE_IRQ_SRC_NOT_EMPTY,
1045 eth_txdone_irq, NULL);
1046 qmgr_enable_irq(TXDONE_QUEUE);
1049 /* we may already have RX data, enables IRQ */
1050 napi_schedule(&port->napi);
1054 static int eth_close(struct net_device *dev)
1056 struct port *port = netdev_priv(dev);
1058 int buffs = RX_DESCS; /* allocated RX buffers */
1062 qmgr_disable_irq(port->plat->rxq);
1063 napi_disable(&port->napi);
1064 netif_stop_queue(dev);
1066 while (queue_get_desc(RXFREE_QUEUE(port->id), port, 0) >= 0)
1069 memset(&msg, 0, sizeof(msg));
1070 msg.cmd = NPE_SETLOOPBACK_MODE;
1071 msg.eth_id = port->id;
1073 if (npe_send_recv_message(port->npe, &msg, "ETH_ENABLE_LOOPBACK"))
1074 printk(KERN_CRIT "%s: unable to enable loopback\n", dev->name);
1077 do { /* drain RX buffers */
1078 while (queue_get_desc(port->plat->rxq, port, 0) >= 0)
1082 if (qmgr_stat_empty(TX_QUEUE(port->id))) {
1083 /* we have to inject some packet */
1086 int n = queue_get_desc(port->plat->txreadyq, port, 1);
1088 desc = tx_desc_ptr(port, n);
1089 phys = tx_desc_phys(port, n);
1090 desc->buf_len = desc->pkt_len = 1;
1092 queue_put_desc(TX_QUEUE(port->id), phys, desc);
1095 } while (++i < MAX_CLOSE_WAIT);
1098 printk(KERN_CRIT "%s: unable to drain RX queue, %i buffer(s)"
1099 " left in NPE\n", dev->name, buffs);
1102 printk(KERN_DEBUG "Draining RX queue took %i cycles\n", i);
1106 while (queue_get_desc(TX_QUEUE(port->id), port, 1) >= 0)
1107 buffs--; /* cancel TX */
1111 while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
1115 } while (++i < MAX_CLOSE_WAIT);
1118 printk(KERN_CRIT "%s: unable to drain TX queue, %i buffer(s) "
1119 "left in NPE\n", dev->name, buffs);
1122 printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
1126 if (npe_send_recv_message(port->npe, &msg, "ETH_DISABLE_LOOPBACK"))
1127 printk(KERN_CRIT "%s: unable to disable loopback\n",
1130 phy_stop(port->phydev);
1133 qmgr_disable_irq(TXDONE_QUEUE);
1134 destroy_queues(port);
1135 release_queues(port);
1139 static const struct net_device_ops ixp4xx_netdev_ops = {
1140 .ndo_open = eth_open,
1141 .ndo_stop = eth_close,
1142 .ndo_start_xmit = eth_xmit,
1143 .ndo_set_multicast_list = eth_set_mcast_list,
1144 .ndo_do_ioctl = eth_ioctl,
1148 static int __devinit eth_init_one(struct platform_device *pdev)
1151 struct net_device *dev;
1152 struct eth_plat_info *plat = pdev->dev.platform_data;
1154 char phy_id[MII_BUS_ID_SIZE + 3];
1157 if (!(dev = alloc_etherdev(sizeof(struct port))))
1160 SET_NETDEV_DEV(dev, &pdev->dev);
1161 port = netdev_priv(dev);
1163 port->id = pdev->id;
1166 case IXP4XX_ETH_NPEA:
1167 port->regs = (struct eth_regs __iomem *)IXP4XX_EthA_BASE_VIRT;
1168 regs_phys = IXP4XX_EthA_BASE_PHYS;
1170 case IXP4XX_ETH_NPEB:
1171 port->regs = (struct eth_regs __iomem *)IXP4XX_EthB_BASE_VIRT;
1172 regs_phys = IXP4XX_EthB_BASE_PHYS;
1174 case IXP4XX_ETH_NPEC:
1175 port->regs = (struct eth_regs __iomem *)IXP4XX_EthC_BASE_VIRT;
1176 regs_phys = IXP4XX_EthC_BASE_PHYS;
1183 dev->netdev_ops = &ixp4xx_netdev_ops;
1184 dev->ethtool_ops = &ixp4xx_ethtool_ops;
1185 dev->tx_queue_len = 100;
1187 netif_napi_add(dev, &port->napi, eth_poll, NAPI_WEIGHT);
1189 if (!(port->npe = npe_request(NPE_ID(port->id)))) {
1194 port->mem_res = request_mem_region(regs_phys, REGS_SIZE, dev->name);
1195 if (!port->mem_res) {
1201 npe_port_tab[NPE_ID(port->id)] = port;
1202 memcpy(dev->dev_addr, plat->hwaddr, ETH_ALEN);
1204 platform_set_drvdata(pdev, dev);
1206 __raw_writel(DEFAULT_CORE_CNTRL | CORE_RESET,
1207 &port->regs->core_control);
1209 __raw_writel(DEFAULT_CORE_CNTRL, &port->regs->core_control);
1212 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, "0", plat->phy);
1213 port->phydev = phy_connect(dev, phy_id, &ixp4xx_adjust_link, 0,
1214 PHY_INTERFACE_MODE_MII);
1215 if ((err = IS_ERR(port->phydev)))
1218 port->phydev->irq = PHY_POLL;
1220 if ((err = register_netdev(dev)))
1223 printk(KERN_INFO "%s: MII PHY %i on %s\n", dev->name, plat->phy,
1224 npe_name(port->npe));
1229 phy_disconnect(port->phydev);
1231 npe_port_tab[NPE_ID(port->id)] = NULL;
1232 platform_set_drvdata(pdev, NULL);
1233 release_resource(port->mem_res);
1235 npe_release(port->npe);
1241 static int __devexit eth_remove_one(struct platform_device *pdev)
1243 struct net_device *dev = platform_get_drvdata(pdev);
1244 struct port *port = netdev_priv(dev);
1246 unregister_netdev(dev);
1247 phy_disconnect(port->phydev);
1248 npe_port_tab[NPE_ID(port->id)] = NULL;
1249 platform_set_drvdata(pdev, NULL);
1250 npe_release(port->npe);
1251 release_resource(port->mem_res);
1256 static struct platform_driver ixp4xx_eth_driver = {
1257 .driver.name = DRV_NAME,
1258 .probe = eth_init_one,
1259 .remove = eth_remove_one,
1262 static int __init eth_init_module(void)
1265 if ((err = ixp4xx_mdio_register()))
1267 return platform_driver_register(&ixp4xx_eth_driver);
1270 static void __exit eth_cleanup_module(void)
1272 platform_driver_unregister(&ixp4xx_eth_driver);
1273 ixp4xx_mdio_remove();
1276 MODULE_AUTHOR("Krzysztof Halasa");
1277 MODULE_DESCRIPTION("Intel IXP4xx Ethernet driver");
1278 MODULE_LICENSE("GPL v2");
1279 MODULE_ALIAS("platform:ixp4xx_eth");
1280 module_init(eth_init_module);
1281 module_exit(eth_cleanup_module);