1 --- a/drivers/net/arm/Kconfig
2 +++ b/drivers/net/arm/Kconfig
5 This is a driver for the ethernet hardware included in EP93xx CPUs.
6 Say Y if you are building a kernel for EP93xx based devices.
9 + tristate "Intel IXP4xx Ethernet support"
10 + depends on ARM && ARCH_IXP4XX && IXP4XX_NPE && IXP4XX_QMGR
13 + Say Y here if you want to use built-in Ethernet ports
14 + on IXP4xx processor.
15 --- a/drivers/net/arm/Makefile
16 +++ b/drivers/net/arm/Makefile
18 obj-$(CONFIG_ARM_ETHER1) += ether1.o
19 obj-$(CONFIG_ARM_AT91_ETHER) += at91_ether.o
20 obj-$(CONFIG_EP93XX_ETH) += ep93xx_eth.o
21 +obj-$(CONFIG_IXP4XX_ETH) += ixp4xx_eth.o
23 +++ b/drivers/net/arm/ixp4xx_eth.c
26 + * Intel IXP4xx Ethernet driver for Linux
28 + * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
30 + * This program is free software; you can redistribute it and/or modify it
31 + * under the terms of version 2 of the GNU General Public License
32 + * as published by the Free Software Foundation.
34 + * Ethernet port config (0x00 is not present on IXP42X):
36 + * logical port 0x00 0x10 0x20
37 + * NPE 0 (NPE-A) 1 (NPE-B) 2 (NPE-C)
38 + * physical PortId 2 0 1
40 + * RX-free queue 26 27 28
41 + * TX-done queue is always 31, per-port RX and TX-ready queues are configurable
45 + * bits 0 -> 1 - NPE ID (RX and TX-done)
46 + * bits 0 -> 2 - priority (TX, per 802.1D)
47 + * bits 3 -> 4 - port ID (user-set?)
48 + * bits 5 -> 31 - physical descriptor address
51 +#include <linux/delay.h>
52 +#include <linux/dma-mapping.h>
53 +#include <linux/dmapool.h>
54 +#include <linux/etherdevice.h>
55 +#include <linux/io.h>
56 +#include <linux/kernel.h>
57 +#include <linux/mii.h>
58 +#include <linux/platform_device.h>
59 +#include <asm/arch/npe.h>
60 +#include <asm/arch/qmgr.h>
62 +#define DEBUG_QUEUES 0
66 +#define DEBUG_PKT_BYTES 0
68 +#define DEBUG_CLOSE 0
70 +#define DRV_NAME "ixp4xx_eth"
74 +#define RX_DESCS 64 /* also length of all RX queues */
75 +#define TX_DESCS 16 /* also length of all TX queues */
76 +#define TXDONE_QUEUE_LEN 64 /* dwords */
78 +#define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
79 +#define REGS_SIZE 0x1000
80 +#define MAX_MRU 1536 /* 0x600 */
81 +#define RX_BUFF_SIZE ALIGN((NET_IP_ALIGN) + MAX_MRU, 4)
83 +#define NAPI_WEIGHT 16
84 +#define MDIO_INTERVAL (3 * HZ)
85 +#define MAX_MDIO_RETRIES 100 /* microseconds, typically 30 cycles */
86 +#define MAX_MII_RESET_RETRIES 100 /* mdio_read() cycles, typically 4 */
87 +#define MAX_CLOSE_WAIT 1000 /* microseconds, typically 2-3 cycles */
89 +#define NPE_ID(port_id) ((port_id) >> 4)
90 +#define PHYSICAL_ID(port_id) ((NPE_ID(port_id) + 2) % 3)
91 +#define TX_QUEUE(port_id) (NPE_ID(port_id) + 23)
92 +#define RXFREE_QUEUE(port_id) (NPE_ID(port_id) + 26)
93 +#define TXDONE_QUEUE 31
95 +/* TX Control Registers */
96 +#define TX_CNTRL0_TX_EN 0x01
97 +#define TX_CNTRL0_HALFDUPLEX 0x02
98 +#define TX_CNTRL0_RETRY 0x04
99 +#define TX_CNTRL0_PAD_EN 0x08
100 +#define TX_CNTRL0_APPEND_FCS 0x10
101 +#define TX_CNTRL0_2DEFER 0x20
102 +#define TX_CNTRL0_RMII 0x40 /* reduced MII */
103 +#define TX_CNTRL1_RETRIES 0x0F /* 4 bits */
105 +/* RX Control Registers */
106 +#define RX_CNTRL0_RX_EN 0x01
107 +#define RX_CNTRL0_PADSTRIP_EN 0x02
108 +#define RX_CNTRL0_SEND_FCS 0x04
109 +#define RX_CNTRL0_PAUSE_EN 0x08
110 +#define RX_CNTRL0_LOOP_EN 0x10
111 +#define RX_CNTRL0_ADDR_FLTR_EN 0x20
112 +#define RX_CNTRL0_RX_RUNT_EN 0x40
113 +#define RX_CNTRL0_BCAST_DIS 0x80
114 +#define RX_CNTRL1_DEFER_EN 0x01
116 +/* Core Control Register */
117 +#define CORE_RESET 0x01
118 +#define CORE_RX_FIFO_FLUSH 0x02
119 +#define CORE_TX_FIFO_FLUSH 0x04
120 +#define CORE_SEND_JAM 0x08
121 +#define CORE_MDC_EN 0x10 /* MDIO using NPE-B ETH-0 only */
123 +#define DEFAULT_TX_CNTRL0 (TX_CNTRL0_TX_EN | TX_CNTRL0_RETRY | \
124 + TX_CNTRL0_PAD_EN | TX_CNTRL0_APPEND_FCS | \
126 +#define DEFAULT_RX_CNTRL0 RX_CNTRL0_RX_EN
127 +#define DEFAULT_CORE_CNTRL CORE_MDC_EN
130 +/* NPE message codes */
131 +#define NPE_GETSTATUS 0x00
132 +#define NPE_EDB_SETPORTADDRESS 0x01
133 +#define NPE_EDB_GETMACADDRESSDATABASE 0x02
134 +#define NPE_EDB_SETMACADDRESSSDATABASE 0x03
135 +#define NPE_GETSTATS 0x04
136 +#define NPE_RESETSTATS 0x05
137 +#define NPE_SETMAXFRAMELENGTHS 0x06
138 +#define NPE_VLAN_SETRXTAGMODE 0x07
139 +#define NPE_VLAN_SETDEFAULTRXVID 0x08
140 +#define NPE_VLAN_SETPORTVLANTABLEENTRY 0x09
141 +#define NPE_VLAN_SETPORTVLANTABLERANGE 0x0A
142 +#define NPE_VLAN_SETRXQOSENTRY 0x0B
143 +#define NPE_VLAN_SETPORTIDEXTRACTIONMODE 0x0C
144 +#define NPE_STP_SETBLOCKINGSTATE 0x0D
145 +#define NPE_FW_SETFIREWALLMODE 0x0E
146 +#define NPE_PC_SETFRAMECONTROLDURATIONID 0x0F
147 +#define NPE_PC_SETAPMACTABLE 0x11
148 +#define NPE_SETLOOPBACK_MODE 0x12
149 +#define NPE_PC_SETBSSIDTABLE 0x13
150 +#define NPE_ADDRESS_FILTER_CONFIG 0x14
151 +#define NPE_APPENDFCSCONFIG 0x15
152 +#define NPE_NOTIFY_MAC_RECOVERY_DONE 0x16
153 +#define NPE_MAC_RECOVERY_START 0x17
157 +typedef struct sk_buff buffer_t;
158 +#define free_buffer dev_kfree_skb
159 +#define free_buffer_irq dev_kfree_skb_irq
161 +typedef void buffer_t;
162 +#define free_buffer kfree
163 +#define free_buffer_irq kfree
167 + u32 tx_control[2], __res1[2]; /* 000 */
168 + u32 rx_control[2], __res2[2]; /* 010 */
169 + u32 random_seed, __res3[3]; /* 020 */
170 + u32 partial_empty_threshold, __res4; /* 030 */
171 + u32 partial_full_threshold, __res5; /* 038 */
172 + u32 tx_start_bytes, __res6[3]; /* 040 */
173 + u32 tx_deferral, rx_deferral, __res7[2];/* 050 */
174 + u32 tx_2part_deferral[2], __res8[2]; /* 060 */
175 + u32 slot_time, __res9[3]; /* 070 */
176 + u32 mdio_command[4]; /* 080 */
177 + u32 mdio_status[4]; /* 090 */
178 + u32 mcast_mask[6], __res10[2]; /* 0A0 */
179 + u32 mcast_addr[6], __res11[2]; /* 0C0 */
180 + u32 int_clock_threshold, __res12[3]; /* 0E0 */
181 + u32 hw_addr[6], __res13[61]; /* 0F0 */
182 + u32 core_control; /* 1FC */
186 + struct resource *mem_res;
187 + struct eth_regs __iomem *regs;
189 + struct net_device *netdev;
190 + struct napi_struct napi;
191 + struct net_device_stats stat;
192 + struct mii_if_info mii;
193 + struct delayed_work mdio_thread;
194 + struct eth_plat_info *plat;
195 + buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
196 + struct desc *desc_tab; /* coherent */
198 + int id; /* logical port ID */
202 +/* NPE message structure */
205 + u8 cmd, eth_id, byte2, byte3;
206 + u8 byte4, byte5, byte6, byte7;
208 + u8 byte3, byte2, eth_id, cmd;
209 + u8 byte7, byte6, byte5, byte4;
213 +/* Ethernet packet descriptor */
215 + u32 next; /* pointer to next buffer, unused */
218 + u16 buf_len; /* buffer length */
219 + u16 pkt_len; /* packet length */
220 + u32 data; /* pointer to data buffer in RAM */
228 + u16 pkt_len; /* packet length */
229 + u16 buf_len; /* buffer length */
230 + u32 data; /* pointer to data buffer in RAM */
240 + u8 dst_mac_0, dst_mac_1, dst_mac_2, dst_mac_3;
241 + u8 dst_mac_4, dst_mac_5, src_mac_0, src_mac_1;
242 + u8 src_mac_2, src_mac_3, src_mac_4, src_mac_5;
244 + u8 dst_mac_3, dst_mac_2, dst_mac_1, dst_mac_0;
245 + u8 src_mac_1, src_mac_0, dst_mac_5, dst_mac_4;
246 + u8 src_mac_5, src_mac_4, src_mac_3, src_mac_2;
251 +#define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
252 + (n) * sizeof(struct desc))
253 +#define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
255 +#define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
256 + ((n) + RX_DESCS) * sizeof(struct desc))
257 +#define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
260 +static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
263 + for (i = 0; i < cnt; i++)
264 + dest[i] = swab32(src[i]);
268 +static spinlock_t mdio_lock;
269 +static struct eth_regs __iomem *mdio_regs; /* mdio command and status only */
270 +static int ports_open;
271 +static struct port *npe_port_tab[MAX_NPES];
272 +static struct dma_pool *dma_pool;
275 +static u16 mdio_cmd(struct net_device *dev, int phy_id, int location,
276 + int write, u16 cmd)
280 + if (__raw_readl(&mdio_regs->mdio_command[3]) & 0x80) {
281 + printk(KERN_ERR "%s: MII not ready to transmit\n", dev->name);
286 + __raw_writel(cmd & 0xFF, &mdio_regs->mdio_command[0]);
287 + __raw_writel(cmd >> 8, &mdio_regs->mdio_command[1]);
289 + __raw_writel(((phy_id << 5) | location) & 0xFF,
290 + &mdio_regs->mdio_command[2]);
291 + __raw_writel((phy_id >> 3) | (write << 2) | 0x80 /* GO */,
292 + &mdio_regs->mdio_command[3]);
294 + while ((cycles < MAX_MDIO_RETRIES) &&
295 + (__raw_readl(&mdio_regs->mdio_command[3]) & 0x80)) {
300 + if (cycles == MAX_MDIO_RETRIES) {
301 + printk(KERN_ERR "%s: MII write failed\n", dev->name);
306 + printk(KERN_DEBUG "%s: mdio_cmd() took %i cycles\n", dev->name,
313 + if (__raw_readl(&mdio_regs->mdio_status[3]) & 0x80) {
314 + printk(KERN_ERR "%s: MII read failed\n", dev->name);
318 + return (__raw_readl(&mdio_regs->mdio_status[0]) & 0xFF) |
319 + (__raw_readl(&mdio_regs->mdio_status[1]) << 8);
322 +static int mdio_read(struct net_device *dev, int phy_id, int location)
324 + unsigned long flags;
327 + spin_lock_irqsave(&mdio_lock, flags);
328 + val = mdio_cmd(dev, phy_id, location, 0, 0);
329 + spin_unlock_irqrestore(&mdio_lock, flags);
333 +static void mdio_write(struct net_device *dev, int phy_id, int location,
336 + unsigned long flags;
338 + spin_lock_irqsave(&mdio_lock, flags);
339 + mdio_cmd(dev, phy_id, location, 1, val);
340 + spin_unlock_irqrestore(&mdio_lock, flags);
343 +static void phy_reset(struct net_device *dev, int phy_id)
345 + struct port *port = netdev_priv(dev);
348 + mdio_write(dev, phy_id, MII_BMCR, port->mii_bmcr | BMCR_RESET);
350 + while (cycles < MAX_MII_RESET_RETRIES) {
351 + if (!(mdio_read(dev, phy_id, MII_BMCR) & BMCR_RESET)) {
353 + printk(KERN_DEBUG "%s: phy_reset() took %i cycles\n",
354 + dev->name, cycles);
362 + printk(KERN_ERR "%s: MII reset failed\n", dev->name);
365 +static void eth_set_duplex(struct port *port)
367 + if (port->mii.full_duplex)
368 + __raw_writel(DEFAULT_TX_CNTRL0 & ~TX_CNTRL0_HALFDUPLEX,
369 + &port->regs->tx_control[0]);
371 + __raw_writel(DEFAULT_TX_CNTRL0 | TX_CNTRL0_HALFDUPLEX,
372 + &port->regs->tx_control[0]);
376 +static void phy_check_media(struct port *port, int init)
378 + if (mii_check_media(&port->mii, 1, init))
379 + eth_set_duplex(port);
380 + if (port->mii.force_media) { /* mii_check_media() doesn't work */
381 + struct net_device *dev = port->netdev;
382 + int cur_link = mii_link_ok(&port->mii);
383 + int prev_link = netif_carrier_ok(dev);
385 + if (!prev_link && cur_link) {
386 + printk(KERN_INFO "%s: link up\n", dev->name);
387 + netif_carrier_on(dev);
388 + } else if (prev_link && !cur_link) {
389 + printk(KERN_INFO "%s: link down\n", dev->name);
390 + netif_carrier_off(dev);
396 +static void mdio_thread(struct work_struct *work)
398 + struct port *port = container_of(work, struct port, mdio_thread.work);
400 + phy_check_media(port, 0);
401 + schedule_delayed_work(&port->mdio_thread, MDIO_INTERVAL);
405 +static inline void debug_pkt(struct net_device *dev, const char *func,
411 + printk(KERN_DEBUG "%s: %s(%i) ", dev->name, func, len);
412 + for (i = 0; i < len; i++) {
413 + if (i >= DEBUG_PKT_BYTES)
416 + ((i == 6) || (i == 12) || (i >= 14)) ? " " : "",
424 +static inline void debug_desc(u32 phys, struct desc *desc)
427 + printk(KERN_DEBUG "%X: %X %3X %3X %08X %2X < %2X %4X %X"
428 + " %X %X %02X%02X%02X%02X%02X%02X < %02X%02X%02X%02X%02X%02X\n",
429 + phys, desc->next, desc->buf_len, desc->pkt_len,
430 + desc->data, desc->dest_id, desc->src_id, desc->flags,
431 + desc->qos, desc->padlen, desc->vlan_tci,
432 + desc->dst_mac_0, desc->dst_mac_1, desc->dst_mac_2,
433 + desc->dst_mac_3, desc->dst_mac_4, desc->dst_mac_5,
434 + desc->src_mac_0, desc->src_mac_1, desc->src_mac_2,
435 + desc->src_mac_3, desc->src_mac_4, desc->src_mac_5);
439 +static inline void debug_queue(unsigned int queue, int is_get, u32 phys)
446 + { TX_QUEUE(0x10), "TX#0 " },
447 + { TX_QUEUE(0x20), "TX#1 " },
448 + { TX_QUEUE(0x00), "TX#2 " },
449 + { RXFREE_QUEUE(0x10), "RX-free#0 " },
450 + { RXFREE_QUEUE(0x20), "RX-free#1 " },
451 + { RXFREE_QUEUE(0x00), "RX-free#2 " },
452 + { TXDONE_QUEUE, "TX-done " },
456 + for (i = 0; i < ARRAY_SIZE(names); i++)
457 + if (names[i].queue == queue)
460 + printk(KERN_DEBUG "Queue %i %s%s %X\n", queue,
461 + i < ARRAY_SIZE(names) ? names[i].name : "",
462 + is_get ? "->" : "<-", phys);
466 +static inline u32 queue_get_entry(unsigned int queue)
468 + u32 phys = qmgr_get_entry(queue);
469 + debug_queue(queue, 1, phys);
473 +static inline int queue_get_desc(unsigned int queue, struct port *port,
476 + u32 phys, tab_phys, n_desc;
479 + if (!(phys = queue_get_entry(queue)))
482 + phys &= ~0x1F; /* mask out non-address bits */
483 + tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
484 + tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
485 + n_desc = (phys - tab_phys) / sizeof(struct desc);
486 + BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
487 + debug_desc(phys, &tab[n_desc]);
488 + BUG_ON(tab[n_desc].next);
492 +static inline void queue_put_desc(unsigned int queue, u32 phys,
495 + debug_queue(queue, 0, phys);
496 + debug_desc(phys, desc);
497 + BUG_ON(phys & 0x1F);
498 + qmgr_put_entry(queue, phys);
499 + BUG_ON(qmgr_stat_overflow(queue));
503 +static inline void dma_unmap_tx(struct port *port, struct desc *desc)
506 + dma_unmap_single(&port->netdev->dev, desc->data,
507 + desc->buf_len, DMA_TO_DEVICE);
509 + dma_unmap_single(&port->netdev->dev, desc->data & ~3,
510 + ALIGN((desc->data & 3) + desc->buf_len, 4),
516 +static void eth_rx_irq(void *pdev)
518 + struct net_device *dev = pdev;
519 + struct port *port = netdev_priv(dev);
522 + printk(KERN_DEBUG "%s: eth_rx_irq\n", dev->name);
524 + qmgr_disable_irq(port->plat->rxq);
525 + netif_rx_schedule(dev, &port->napi);
528 +static int eth_poll(struct napi_struct *napi, int budget)
530 + struct port *port = container_of(napi, struct port, napi);
531 + struct net_device *dev = port->netdev;
532 + unsigned int rxq = port->plat->rxq, rxfreeq = RXFREE_QUEUE(port->id);
536 + printk(KERN_DEBUG "%s: eth_poll\n", dev->name);
539 + while (received < budget) {
540 + struct sk_buff *skb;
544 + struct sk_buff *temp;
548 + if ((n = queue_get_desc(rxq, port, 0)) < 0) {
549 + received = 0; /* No packet received */
551 + printk(KERN_DEBUG "%s: eth_poll netif_rx_complete\n",
554 + netif_rx_complete(dev, napi);
555 + qmgr_enable_irq(rxq);
556 + if (!qmgr_stat_empty(rxq) &&
557 + netif_rx_reschedule(dev, napi)) {
559 + printk(KERN_DEBUG "%s: eth_poll"
560 + " netif_rx_reschedule successed\n",
563 + qmgr_disable_irq(rxq);
567 + printk(KERN_DEBUG "%s: eth_poll all done\n",
570 + return 0; /* all work done */
573 + desc = rx_desc_ptr(port, n);
576 + if ((skb = netdev_alloc_skb(dev, RX_BUFF_SIZE))) {
577 + phys = dma_map_single(&dev->dev, skb->data,
578 + RX_BUFF_SIZE, DMA_FROM_DEVICE);
579 + if (dma_mapping_error(phys)) {
580 + dev_kfree_skb(skb);
585 + skb = netdev_alloc_skb(dev,
586 + ALIGN(NET_IP_ALIGN + desc->pkt_len, 4));
590 + port->stat.rx_dropped++;
591 + /* put the desc back on RX-ready queue */
592 + desc->buf_len = MAX_MRU;
594 + queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
598 + /* process received frame */
601 + skb = port->rx_buff_tab[n];
602 + dma_unmap_single(&dev->dev, desc->data - NET_IP_ALIGN,
603 + RX_BUFF_SIZE, DMA_FROM_DEVICE);
605 + dma_sync_single(&dev->dev, desc->data - NET_IP_ALIGN,
606 + RX_BUFF_SIZE, DMA_FROM_DEVICE);
607 + memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
608 + ALIGN(NET_IP_ALIGN + desc->pkt_len, 4) / 4);
610 + skb_reserve(skb, NET_IP_ALIGN);
611 + skb_put(skb, desc->pkt_len);
613 + debug_pkt(dev, "eth_poll", skb->data, skb->len);
615 + skb->protocol = eth_type_trans(skb, dev);
616 + dev->last_rx = jiffies;
617 + port->stat.rx_packets++;
618 + port->stat.rx_bytes += skb->len;
619 + netif_receive_skb(skb);
621 + /* put the new buffer on RX-free queue */
623 + port->rx_buff_tab[n] = temp;
624 + desc->data = phys + NET_IP_ALIGN;
626 + desc->buf_len = MAX_MRU;
628 + queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
633 + printk(KERN_DEBUG "eth_poll(): end, not all work done\n");
635 + return received; /* not all work done */
639 +static void eth_txdone_irq(void *unused)
644 + printk(KERN_DEBUG DRV_NAME ": eth_txdone_irq\n");
646 + while ((phys = queue_get_entry(TXDONE_QUEUE)) != 0) {
647 + u32 npe_id, n_desc;
653 + BUG_ON(npe_id >= MAX_NPES);
654 + port = npe_port_tab[npe_id];
656 + phys &= ~0x1F; /* mask out non-address bits */
657 + n_desc = (phys - tx_desc_phys(port, 0)) / sizeof(struct desc);
658 + BUG_ON(n_desc >= TX_DESCS);
659 + desc = tx_desc_ptr(port, n_desc);
660 + debug_desc(phys, desc);
662 + if (port->tx_buff_tab[n_desc]) { /* not the draining packet */
663 + port->stat.tx_packets++;
664 + port->stat.tx_bytes += desc->pkt_len;
666 + dma_unmap_tx(port, desc);
668 + printk(KERN_DEBUG "%s: eth_txdone_irq free %p\n",
669 + port->netdev->name, port->tx_buff_tab[n_desc]);
671 + free_buffer_irq(port->tx_buff_tab[n_desc]);
672 + port->tx_buff_tab[n_desc] = NULL;
675 + start = qmgr_stat_empty(port->plat->txreadyq);
676 + queue_put_desc(port->plat->txreadyq, phys, desc);
679 + printk(KERN_DEBUG "%s: eth_txdone_irq xmit ready\n",
680 + port->netdev->name);
682 + netif_wake_queue(port->netdev);
687 +static int eth_xmit(struct sk_buff *skb, struct net_device *dev)
689 + struct port *port = netdev_priv(dev);
690 + unsigned int txreadyq = port->plat->txreadyq;
691 + int len, offset, bytes, n;
697 + printk(KERN_DEBUG "%s: eth_xmit\n", dev->name);
700 + if (unlikely(skb->len > MAX_MRU)) {
701 + dev_kfree_skb(skb);
702 + port->stat.tx_errors++;
703 + return NETDEV_TX_OK;
706 + debug_pkt(dev, "eth_xmit", skb->data, skb->len);
710 + offset = 0; /* no need to keep alignment */
714 + offset = (int)skb->data & 3; /* keep 32-bit alignment */
715 + bytes = ALIGN(offset + len, 4);
716 + if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
717 + dev_kfree_skb(skb);
718 + port->stat.tx_dropped++;
719 + return NETDEV_TX_OK;
721 + memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
722 + dev_kfree_skb(skb);
725 + phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
726 + if (dma_mapping_error(phys)) {
728 + dev_kfree_skb(skb);
732 + port->stat.tx_dropped++;
733 + return NETDEV_TX_OK;
736 + n = queue_get_desc(txreadyq, port, 1);
738 + desc = tx_desc_ptr(port, n);
741 + port->tx_buff_tab[n] = skb;
743 + port->tx_buff_tab[n] = mem;
745 + desc->data = phys + offset;
746 + desc->buf_len = desc->pkt_len = len;
748 + /* NPE firmware pads short frames with zeros internally */
750 + queue_put_desc(TX_QUEUE(port->id), tx_desc_phys(port, n), desc);
751 + dev->trans_start = jiffies;
753 + if (qmgr_stat_empty(txreadyq)) {
755 + printk(KERN_DEBUG "%s: eth_xmit queue full\n", dev->name);
757 + netif_stop_queue(dev);
758 + /* we could miss TX ready interrupt */
759 + if (!qmgr_stat_empty(txreadyq)) {
761 + printk(KERN_DEBUG "%s: eth_xmit ready again\n",
764 + netif_wake_queue(dev);
769 + printk(KERN_DEBUG "%s: eth_xmit end\n", dev->name);
771 + return NETDEV_TX_OK;
775 +static struct net_device_stats *eth_stats(struct net_device *dev)
777 + struct port *port = netdev_priv(dev);
778 + return &port->stat;
781 +static void eth_set_mcast_list(struct net_device *dev)
783 + struct port *port = netdev_priv(dev);
784 + struct dev_mc_list *mclist = dev->mc_list;
785 + u8 diffs[ETH_ALEN], *addr;
786 + int cnt = dev->mc_count, i;
788 + if ((dev->flags & IFF_PROMISC) || !mclist || !cnt) {
789 + __raw_writel(DEFAULT_RX_CNTRL0 & ~RX_CNTRL0_ADDR_FLTR_EN,
790 + &port->regs->rx_control[0]);
794 + memset(diffs, 0, ETH_ALEN);
795 + addr = mclist->dmi_addr; /* first MAC address */
797 + while (--cnt && (mclist = mclist->next))
798 + for (i = 0; i < ETH_ALEN; i++)
799 + diffs[i] |= addr[i] ^ mclist->dmi_addr[i];
801 + for (i = 0; i < ETH_ALEN; i++) {
802 + __raw_writel(addr[i], &port->regs->mcast_addr[i]);
803 + __raw_writel(~diffs[i], &port->regs->mcast_mask[i]);
806 + __raw_writel(DEFAULT_RX_CNTRL0 | RX_CNTRL0_ADDR_FLTR_EN,
807 + &port->regs->rx_control[0]);
811 +static int eth_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
813 + struct port *port = netdev_priv(dev);
814 + unsigned int duplex_chg;
817 + if (!netif_running(dev))
819 + err = generic_mii_ioctl(&port->mii, if_mii(req), cmd, &duplex_chg);
821 + eth_set_duplex(port);
826 +static int request_queues(struct port *port)
830 + err = qmgr_request_queue(RXFREE_QUEUE(port->id), RX_DESCS, 0, 0);
834 + err = qmgr_request_queue(port->plat->rxq, RX_DESCS, 0, 0);
838 + err = qmgr_request_queue(TX_QUEUE(port->id), TX_DESCS, 0, 0);
842 + err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0);
846 + /* TX-done queue handles skbs sent out by the NPEs */
848 + err = qmgr_request_queue(TXDONE_QUEUE, TXDONE_QUEUE_LEN, 0, 0);
855 + qmgr_release_queue(port->plat->txreadyq);
857 + qmgr_release_queue(TX_QUEUE(port->id));
859 + qmgr_release_queue(port->plat->rxq);
861 + qmgr_release_queue(RXFREE_QUEUE(port->id));
862 + printk(KERN_DEBUG "%s: unable to request hardware queues\n",
863 + port->netdev->name);
867 +static void release_queues(struct port *port)
869 + qmgr_release_queue(RXFREE_QUEUE(port->id));
870 + qmgr_release_queue(port->plat->rxq);
871 + qmgr_release_queue(TX_QUEUE(port->id));
872 + qmgr_release_queue(port->plat->txreadyq);
875 + qmgr_release_queue(TXDONE_QUEUE);
878 +static int init_queues(struct port *port)
883 + if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
884 + POOL_ALLOC_SIZE, 32, 0)))
887 + if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
888 + &port->desc_tab_phys)))
890 + memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
891 + memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
892 + memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
894 + /* Setup RX buffers */
895 + for (i = 0; i < RX_DESCS; i++) {
896 + struct desc *desc = rx_desc_ptr(port, i);
897 + buffer_t *buff; /* skb or kmalloc()ated memory */
900 + if (!(buff = netdev_alloc_skb(port->netdev, RX_BUFF_SIZE)))
904 + if (!(buff = kmalloc(RX_BUFF_SIZE, GFP_KERNEL)))
908 + desc->buf_len = MAX_MRU;
909 + desc->data = dma_map_single(&port->netdev->dev, data,
910 + RX_BUFF_SIZE, DMA_FROM_DEVICE);
911 + if (dma_mapping_error(desc->data)) {
915 + desc->data += NET_IP_ALIGN;
916 + port->rx_buff_tab[i] = buff;
922 +static void destroy_queues(struct port *port)
926 + if (port->desc_tab) {
927 + for (i = 0; i < RX_DESCS; i++) {
928 + struct desc *desc = rx_desc_ptr(port, i);
929 + buffer_t *buff = port->rx_buff_tab[i];
931 + dma_unmap_single(&port->netdev->dev,
932 + desc->data - NET_IP_ALIGN,
933 + RX_BUFF_SIZE, DMA_FROM_DEVICE);
937 + for (i = 0; i < TX_DESCS; i++) {
938 + struct desc *desc = tx_desc_ptr(port, i);
939 + buffer_t *buff = port->tx_buff_tab[i];
941 + dma_unmap_tx(port, desc);
945 + dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
946 + port->desc_tab = NULL;
949 + if (!ports_open && dma_pool) {
950 + dma_pool_destroy(dma_pool);
955 +static int eth_open(struct net_device *dev)
957 + struct port *port = netdev_priv(dev);
958 + struct npe *npe = port->npe;
962 + if (!npe_running(npe)) {
963 + err = npe_load_firmware(npe, npe_name(npe), &dev->dev);
967 + if (npe_recv_message(npe, &msg, "ETH_GET_STATUS")) {
968 + printk(KERN_ERR "%s: %s not responding\n", dev->name,
974 + mdio_write(dev, port->plat->phy, MII_BMCR, port->mii_bmcr);
976 + memset(&msg, 0, sizeof(msg));
977 + msg.cmd = NPE_VLAN_SETRXQOSENTRY;
978 + msg.eth_id = port->id;
979 + msg.byte5 = port->plat->rxq | 0x80;
980 + msg.byte7 = port->plat->rxq << 4;
981 + for (i = 0; i < 8; i++) {
983 + if (npe_send_recv_message(port->npe, &msg, "ETH_SET_RXQ"))
987 + msg.cmd = NPE_EDB_SETPORTADDRESS;
988 + msg.eth_id = PHYSICAL_ID(port->id);
989 + msg.byte2 = dev->dev_addr[0];
990 + msg.byte3 = dev->dev_addr[1];
991 + msg.byte4 = dev->dev_addr[2];
992 + msg.byte5 = dev->dev_addr[3];
993 + msg.byte6 = dev->dev_addr[4];
994 + msg.byte7 = dev->dev_addr[5];
995 + if (npe_send_recv_message(port->npe, &msg, "ETH_SET_MAC"))
998 + memset(&msg, 0, sizeof(msg));
999 + msg.cmd = NPE_FW_SETFIREWALLMODE;
1000 + msg.eth_id = port->id;
1001 + if (npe_send_recv_message(port->npe, &msg, "ETH_SET_FIREWALL_MODE"))
1004 + if ((err = request_queues(port)) != 0)
1007 + if ((err = init_queues(port)) != 0) {
1008 + destroy_queues(port);
1009 + release_queues(port);
1013 + for (i = 0; i < ETH_ALEN; i++)
1014 + __raw_writel(dev->dev_addr[i], &port->regs->hw_addr[i]);
1015 + __raw_writel(0x08, &port->regs->random_seed);
1016 + __raw_writel(0x12, &port->regs->partial_empty_threshold);
1017 + __raw_writel(0x30, &port->regs->partial_full_threshold);
1018 + __raw_writel(0x08, &port->regs->tx_start_bytes);
1019 + __raw_writel(0x15, &port->regs->tx_deferral);
1020 + __raw_writel(0x08, &port->regs->tx_2part_deferral[0]);
1021 + __raw_writel(0x07, &port->regs->tx_2part_deferral[1]);
1022 + __raw_writel(0x80, &port->regs->slot_time);
1023 + __raw_writel(0x01, &port->regs->int_clock_threshold);
1025 + /* Populate queues with buffers, no failure after this point */
1026 + for (i = 0; i < TX_DESCS; i++)
1027 + queue_put_desc(port->plat->txreadyq,
1028 + tx_desc_phys(port, i), tx_desc_ptr(port, i));
1030 + for (i = 0; i < RX_DESCS; i++)
1031 + queue_put_desc(RXFREE_QUEUE(port->id),
1032 + rx_desc_phys(port, i), rx_desc_ptr(port, i));
1034 + __raw_writel(TX_CNTRL1_RETRIES, &port->regs->tx_control[1]);
1035 + __raw_writel(DEFAULT_TX_CNTRL0, &port->regs->tx_control[0]);
1036 + __raw_writel(0, &port->regs->rx_control[1]);
1037 + __raw_writel(DEFAULT_RX_CNTRL0, &port->regs->rx_control[0]);
1039 + napi_enable(&port->napi);
1040 + phy_check_media(port, 1);
1041 + eth_set_mcast_list(dev);
1042 + netif_start_queue(dev);
1043 + schedule_delayed_work(&port->mdio_thread, MDIO_INTERVAL);
1045 + qmgr_set_irq(port->plat->rxq, QUEUE_IRQ_SRC_NOT_EMPTY,
1047 + if (!ports_open) {
1048 + qmgr_set_irq(TXDONE_QUEUE, QUEUE_IRQ_SRC_NOT_EMPTY,
1049 + eth_txdone_irq, NULL);
1050 + qmgr_enable_irq(TXDONE_QUEUE);
1053 + /* we may already have RX data, enables IRQ */
1054 + netif_rx_schedule(dev, &port->napi);
1058 +static int eth_close(struct net_device *dev)
1060 + struct port *port = netdev_priv(dev);
1062 + int buffs = RX_DESCS; /* allocated RX buffers */
1066 + qmgr_disable_irq(port->plat->rxq);
1067 + napi_disable(&port->napi);
1068 + netif_stop_queue(dev);
1070 + while (queue_get_desc(RXFREE_QUEUE(port->id), port, 0) >= 0)
1073 + memset(&msg, 0, sizeof(msg));
1074 + msg.cmd = NPE_SETLOOPBACK_MODE;
1075 + msg.eth_id = port->id;
1077 + if (npe_send_recv_message(port->npe, &msg, "ETH_ENABLE_LOOPBACK"))
1078 + printk(KERN_CRIT "%s: unable to enable loopback\n", dev->name);
1081 + do { /* drain RX buffers */
1082 + while (queue_get_desc(port->plat->rxq, port, 0) >= 0)
1086 + if (qmgr_stat_empty(TX_QUEUE(port->id))) {
1087 + /* we have to inject some packet */
1088 + struct desc *desc;
1090 + int n = queue_get_desc(port->plat->txreadyq, port, 1);
1092 + desc = tx_desc_ptr(port, n);
1093 + phys = tx_desc_phys(port, n);
1094 + desc->buf_len = desc->pkt_len = 1;
1096 + queue_put_desc(TX_QUEUE(port->id), phys, desc);
1099 + } while (++i < MAX_CLOSE_WAIT);
1102 + printk(KERN_CRIT "%s: unable to drain RX queue, %i buffer(s)"
1103 + " left in NPE\n", dev->name, buffs);
1106 + printk(KERN_DEBUG "Draining RX queue took %i cycles\n", i);
1110 + while (queue_get_desc(TX_QUEUE(port->id), port, 1) >= 0)
1111 + buffs--; /* cancel TX */
1115 + while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
1119 + } while (++i < MAX_CLOSE_WAIT);
1122 + printk(KERN_CRIT "%s: unable to drain TX queue, %i buffer(s) "
1123 + "left in NPE\n", dev->name, buffs);
1126 + printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
1130 + if (npe_send_recv_message(port->npe, &msg, "ETH_DISABLE_LOOPBACK"))
1131 + printk(KERN_CRIT "%s: unable to disable loopback\n",
1134 + port->mii_bmcr = mdio_read(dev, port->plat->phy, MII_BMCR) &
1135 + ~(BMCR_RESET | BMCR_PDOWN); /* may have been altered */
1136 + mdio_write(dev, port->plat->phy, MII_BMCR,
1137 + port->mii_bmcr | BMCR_PDOWN);
1140 + qmgr_disable_irq(TXDONE_QUEUE);
1141 + cancel_rearming_delayed_work(&port->mdio_thread);
1142 + destroy_queues(port);
1143 + release_queues(port);
1147 +static int __devinit eth_init_one(struct platform_device *pdev)
1149 + struct port *port;
1150 + struct net_device *dev;
1151 + struct eth_plat_info *plat = pdev->dev.platform_data;
1155 + if (!(dev = alloc_etherdev(sizeof(struct port))))
1158 + SET_NETDEV_DEV(dev, &pdev->dev);
1159 + port = netdev_priv(dev);
1160 + port->netdev = dev;
1161 + port->id = pdev->id;
1163 + switch (port->id) {
1164 + case IXP4XX_ETH_NPEA:
1165 + port->regs = (struct eth_regs __iomem *)IXP4XX_EthA_BASE_VIRT;
1166 + regs_phys = IXP4XX_EthA_BASE_PHYS;
1168 + case IXP4XX_ETH_NPEB:
1169 + port->regs = (struct eth_regs __iomem *)IXP4XX_EthB_BASE_VIRT;
1170 + regs_phys = IXP4XX_EthB_BASE_PHYS;
1172 + case IXP4XX_ETH_NPEC:
1173 + port->regs = (struct eth_regs __iomem *)IXP4XX_EthC_BASE_VIRT;
1174 + regs_phys = IXP4XX_EthC_BASE_PHYS;
1181 + dev->open = eth_open;
1182 + dev->hard_start_xmit = eth_xmit;
1183 + dev->stop = eth_close;
1184 + dev->get_stats = eth_stats;
1185 + dev->do_ioctl = eth_ioctl;
1186 + dev->set_multicast_list = eth_set_mcast_list;
1187 + dev->tx_queue_len = 100;
1189 + netif_napi_add(dev, &port->napi, eth_poll, NAPI_WEIGHT);
1191 + if (!(port->npe = npe_request(NPE_ID(port->id)))) {
1196 + if (register_netdev(dev)) {
1201 + port->mem_res = request_mem_region(regs_phys, REGS_SIZE, dev->name);
1202 + if (!port->mem_res) {
1207 + port->plat = plat;
1208 + npe_port_tab[NPE_ID(port->id)] = port;
1209 + memcpy(dev->dev_addr, plat->hwaddr, ETH_ALEN);
1211 + platform_set_drvdata(pdev, dev);
1213 + __raw_writel(DEFAULT_CORE_CNTRL | CORE_RESET,
1214 + &port->regs->core_control);
1216 + __raw_writel(DEFAULT_CORE_CNTRL, &port->regs->core_control);
1219 + port->mii.dev = dev;
1220 + port->mii.mdio_read = mdio_read;
1221 + port->mii.mdio_write = mdio_write;
1222 + port->mii.phy_id = plat->phy;
1223 + port->mii.phy_id_mask = 0x1F;
1224 + port->mii.reg_num_mask = 0x1F;
1226 + printk(KERN_INFO "%s: MII PHY %i on %s\n", dev->name, plat->phy,
1227 + npe_name(port->npe));
1229 + phy_reset(dev, plat->phy);
1230 + port->mii_bmcr = mdio_read(dev, plat->phy, MII_BMCR) &
1231 + ~(BMCR_RESET | BMCR_PDOWN);
1232 + mdio_write(dev, plat->phy, MII_BMCR, port->mii_bmcr | BMCR_PDOWN);
1234 + INIT_DELAYED_WORK(&port->mdio_thread, mdio_thread);
1238 + unregister_netdev(dev);
1240 + npe_release(port->npe);
1246 +static int __devexit eth_remove_one(struct platform_device *pdev)
1248 + struct net_device *dev = platform_get_drvdata(pdev);
1249 + struct port *port = netdev_priv(dev);
1251 + unregister_netdev(dev);
1252 + npe_port_tab[NPE_ID(port->id)] = NULL;
1253 + platform_set_drvdata(pdev, NULL);
1254 + npe_release(port->npe);
1255 + release_resource(port->mem_res);
1260 +static struct platform_driver drv = {
1261 + .driver.name = DRV_NAME,
1262 + .probe = eth_init_one,
1263 + .remove = eth_remove_one,
1266 +static int __init eth_init_module(void)
1268 + if (!(ixp4xx_read_feature_bits() & IXP4XX_FEATURE_NPEB_ETH0))
1271 + /* All MII PHY accesses use NPE-B Ethernet registers */
1272 + spin_lock_init(&mdio_lock);
1273 + mdio_regs = (struct eth_regs __iomem *)IXP4XX_EthB_BASE_VIRT;
1274 + __raw_writel(DEFAULT_CORE_CNTRL, &mdio_regs->core_control);
1276 + return platform_driver_register(&drv);
1279 +static void __exit eth_cleanup_module(void)
1281 + platform_driver_unregister(&drv);
1284 +MODULE_AUTHOR("Krzysztof Halasa");
1285 +MODULE_DESCRIPTION("Intel IXP4xx Ethernet driver");
1286 +MODULE_LICENSE("GPL v2");
1287 +MODULE_ALIAS("platform:ixp4xx_eth");
1288 +module_init(eth_init_module);
1289 +module_exit(eth_cleanup_module);
1290 --- a/arch/arm/mach-ixp4xx/ixp4xx_npe.c
1291 +++ b/arch/arm/mach-ixp4xx/ixp4xx_npe.c
1297 debug_msg(npe, "Sending a message took %i cycles\n", cycles);
1307 debug_msg(npe, "Receiving a message took %i cycles\n", cycles);
1312 --- a/arch/arm/mach-ixp4xx/ixp4xx_qmgr.c
1313 +++ b/arch/arm/mach-ixp4xx/ixp4xx_qmgr.c
1315 case 3: mask[0] = 0xFF; break;
1318 + mask[1] = mask[2] = mask[3] = 0;