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>
35 #include <linux/slab.h>
37 #include <mach/qmgr.h>
42 #define DEBUG_PKT_BYTES 0
46 #define DRV_NAME "ixp4xx_eth"
50 #define RX_DESCS 64 /* also length of all RX queues */
51 #define TX_DESCS 16 /* also length of all TX queues */
52 #define TXDONE_QUEUE_LEN 64 /* dwords */
54 #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
55 #define REGS_SIZE 0x1000
56 #define MAX_MRU 1536 /* 0x600 */
57 #define RX_BUFF_SIZE ALIGN((NET_IP_ALIGN) + MAX_MRU, 4)
59 #define NAPI_WEIGHT 16
60 #define MDIO_INTERVAL (3 * HZ)
61 #define MAX_MDIO_RETRIES 100 /* microseconds, typically 30 cycles */
62 #define MAX_CLOSE_WAIT 1000 /* microseconds, typically 2-3 cycles */
64 #define NPE_ID(port_id) ((port_id) >> 4)
65 #define PHYSICAL_ID(port_id) ((NPE_ID(port_id) + 2) % 3)
66 #define TX_QUEUE(port_id) (NPE_ID(port_id) + 23)
67 #define RXFREE_QUEUE(port_id) (NPE_ID(port_id) + 26)
68 #define TXDONE_QUEUE 31
70 /* TX Control Registers */
71 #define TX_CNTRL0_TX_EN 0x01
72 #define TX_CNTRL0_HALFDUPLEX 0x02
73 #define TX_CNTRL0_RETRY 0x04
74 #define TX_CNTRL0_PAD_EN 0x08
75 #define TX_CNTRL0_APPEND_FCS 0x10
76 #define TX_CNTRL0_2DEFER 0x20
77 #define TX_CNTRL0_RMII 0x40 /* reduced MII */
78 #define TX_CNTRL1_RETRIES 0x0F /* 4 bits */
80 /* RX Control Registers */
81 #define RX_CNTRL0_RX_EN 0x01
82 #define RX_CNTRL0_PADSTRIP_EN 0x02
83 #define RX_CNTRL0_SEND_FCS 0x04
84 #define RX_CNTRL0_PAUSE_EN 0x08
85 #define RX_CNTRL0_LOOP_EN 0x10
86 #define RX_CNTRL0_ADDR_FLTR_EN 0x20
87 #define RX_CNTRL0_RX_RUNT_EN 0x40
88 #define RX_CNTRL0_BCAST_DIS 0x80
89 #define RX_CNTRL1_DEFER_EN 0x01
91 /* Core Control Register */
92 #define CORE_RESET 0x01
93 #define CORE_RX_FIFO_FLUSH 0x02
94 #define CORE_TX_FIFO_FLUSH 0x04
95 #define CORE_SEND_JAM 0x08
96 #define CORE_MDC_EN 0x10 /* MDIO using NPE-B ETH-0 only */
98 #define DEFAULT_TX_CNTRL0 (TX_CNTRL0_TX_EN | TX_CNTRL0_RETRY | \
99 TX_CNTRL0_PAD_EN | TX_CNTRL0_APPEND_FCS | \
101 #define DEFAULT_RX_CNTRL0 RX_CNTRL0_RX_EN
102 #define DEFAULT_CORE_CNTRL CORE_MDC_EN
105 /* NPE message codes */
106 #define NPE_GETSTATUS 0x00
107 #define NPE_EDB_SETPORTADDRESS 0x01
108 #define NPE_EDB_GETMACADDRESSDATABASE 0x02
109 #define NPE_EDB_SETMACADDRESSSDATABASE 0x03
110 #define NPE_GETSTATS 0x04
111 #define NPE_RESETSTATS 0x05
112 #define NPE_SETMAXFRAMELENGTHS 0x06
113 #define NPE_VLAN_SETRXTAGMODE 0x07
114 #define NPE_VLAN_SETDEFAULTRXVID 0x08
115 #define NPE_VLAN_SETPORTVLANTABLEENTRY 0x09
116 #define NPE_VLAN_SETPORTVLANTABLERANGE 0x0A
117 #define NPE_VLAN_SETRXQOSENTRY 0x0B
118 #define NPE_VLAN_SETPORTIDEXTRACTIONMODE 0x0C
119 #define NPE_STP_SETBLOCKINGSTATE 0x0D
120 #define NPE_FW_SETFIREWALLMODE 0x0E
121 #define NPE_PC_SETFRAMECONTROLDURATIONID 0x0F
122 #define NPE_PC_SETAPMACTABLE 0x11
123 #define NPE_SETLOOPBACK_MODE 0x12
124 #define NPE_PC_SETBSSIDTABLE 0x13
125 #define NPE_ADDRESS_FILTER_CONFIG 0x14
126 #define NPE_APPENDFCSCONFIG 0x15
127 #define NPE_NOTIFY_MAC_RECOVERY_DONE 0x16
128 #define NPE_MAC_RECOVERY_START 0x17
132 typedef struct sk_buff buffer_t
;
133 #define free_buffer dev_kfree_skb
134 #define free_buffer_irq dev_kfree_skb_irq
136 typedef void buffer_t
;
137 #define free_buffer kfree
138 #define free_buffer_irq kfree
142 u32 tx_control
[2], __res1
[2]; /* 000 */
143 u32 rx_control
[2], __res2
[2]; /* 010 */
144 u32 random_seed
, __res3
[3]; /* 020 */
145 u32 partial_empty_threshold
, __res4
; /* 030 */
146 u32 partial_full_threshold
, __res5
; /* 038 */
147 u32 tx_start_bytes
, __res6
[3]; /* 040 */
148 u32 tx_deferral
, rx_deferral
, __res7
[2];/* 050 */
149 u32 tx_2part_deferral
[2], __res8
[2]; /* 060 */
150 u32 slot_time
, __res9
[3]; /* 070 */
151 u32 mdio_command
[4]; /* 080 */
152 u32 mdio_status
[4]; /* 090 */
153 u32 mcast_mask
[6], __res10
[2]; /* 0A0 */
154 u32 mcast_addr
[6], __res11
[2]; /* 0C0 */
155 u32 int_clock_threshold
, __res12
[3]; /* 0E0 */
156 u32 hw_addr
[6], __res13
[61]; /* 0F0 */
157 u32 core_control
; /* 1FC */
161 struct resource
*mem_res
;
162 struct eth_regs __iomem
*regs
;
164 struct net_device
*netdev
;
165 struct napi_struct napi
;
166 struct phy_device
*phydev
;
167 struct eth_plat_info
*plat
;
168 buffer_t
*rx_buff_tab
[RX_DESCS
], *tx_buff_tab
[TX_DESCS
];
169 struct desc
*desc_tab
; /* coherent */
171 int id
; /* logical port ID */
176 /* NPE message structure */
179 u8 cmd
, eth_id
, byte2
, byte3
;
180 u8 byte4
, byte5
, byte6
, byte7
;
182 u8 byte3
, byte2
, eth_id
, cmd
;
183 u8 byte7
, byte6
, byte5
, byte4
;
187 /* Ethernet packet descriptor */
189 u32 next
; /* pointer to next buffer, unused */
192 u16 buf_len
; /* buffer length */
193 u16 pkt_len
; /* packet length */
194 u32 data
; /* pointer to data buffer in RAM */
202 u16 pkt_len
; /* packet length */
203 u16 buf_len
; /* buffer length */
204 u32 data
; /* pointer to data buffer in RAM */
214 u8 dst_mac_0
, dst_mac_1
, dst_mac_2
, dst_mac_3
;
215 u8 dst_mac_4
, dst_mac_5
, src_mac_0
, src_mac_1
;
216 u8 src_mac_2
, src_mac_3
, src_mac_4
, src_mac_5
;
218 u8 dst_mac_3
, dst_mac_2
, dst_mac_1
, dst_mac_0
;
219 u8 src_mac_1
, src_mac_0
, dst_mac_5
, dst_mac_4
;
220 u8 src_mac_5
, src_mac_4
, src_mac_3
, src_mac_2
;
225 #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
226 (n) * sizeof(struct desc))
227 #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
229 #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
230 ((n) + RX_DESCS) * sizeof(struct desc))
231 #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
234 static inline void memcpy_swab32(u32
*dest
, u32
*src
, int cnt
)
237 for (i
= 0; i
< cnt
; i
++)
238 dest
[i
] = swab32(src
[i
]);
242 static spinlock_t mdio_lock
;
243 static struct eth_regs __iomem
*mdio_regs
; /* mdio command and status only */
244 struct mii_bus
*mdio_bus
;
245 static int ports_open
;
246 static struct port
*npe_port_tab
[MAX_NPES
];
247 static struct dma_pool
*dma_pool
;
250 static int ixp4xx_mdio_cmd(struct mii_bus
*bus
, int phy_id
, int location
,
255 if (__raw_readl(&mdio_regs
->mdio_command
[3]) & 0x80) {
256 printk(KERN_ERR
"%s: MII not ready to transmit\n", bus
->name
);
261 __raw_writel(cmd
& 0xFF, &mdio_regs
->mdio_command
[0]);
262 __raw_writel(cmd
>> 8, &mdio_regs
->mdio_command
[1]);
264 __raw_writel(((phy_id
<< 5) | location
) & 0xFF,
265 &mdio_regs
->mdio_command
[2]);
266 __raw_writel((phy_id
>> 3) | (write
<< 2) | 0x80 /* GO */,
267 &mdio_regs
->mdio_command
[3]);
269 while ((cycles
< MAX_MDIO_RETRIES
) &&
270 (__raw_readl(&mdio_regs
->mdio_command
[3]) & 0x80)) {
275 if (cycles
== MAX_MDIO_RETRIES
) {
276 printk(KERN_ERR
"%s #%i: MII write failed\n", bus
->name
,
282 printk(KERN_DEBUG
"%s #%i: mdio_%s() took %i cycles\n", bus
->name
,
283 phy_id
, write
? "write" : "read", cycles
);
289 if (__raw_readl(&mdio_regs
->mdio_status
[3]) & 0x80) {
291 printk(KERN_DEBUG
"%s #%i: MII read failed\n", bus
->name
,
294 return 0xFFFF; /* don't return error */
297 return (__raw_readl(&mdio_regs
->mdio_status
[0]) & 0xFF) |
298 ((__raw_readl(&mdio_regs
->mdio_status
[1]) & 0xFF) << 8);
301 static int ixp4xx_mdio_read(struct mii_bus
*bus
, int phy_id
, int location
)
306 spin_lock_irqsave(&mdio_lock
, flags
);
307 ret
= ixp4xx_mdio_cmd(bus
, phy_id
, location
, 0, 0);
308 spin_unlock_irqrestore(&mdio_lock
, flags
);
310 printk(KERN_DEBUG
"%s #%i: MII read [%i] -> 0x%X\n", bus
->name
,
311 phy_id
, location
, ret
);
316 static int ixp4xx_mdio_write(struct mii_bus
*bus
, int phy_id
, int location
,
322 spin_lock_irqsave(&mdio_lock
, flags
);
323 ret
= ixp4xx_mdio_cmd(bus
, phy_id
, location
, 1, val
);
324 spin_unlock_irqrestore(&mdio_lock
, flags
);
326 printk(KERN_DEBUG
"%s #%i: MII write [%i] <- 0x%X, err = %i\n",
327 bus
->name
, phy_id
, location
, val
, ret
);
332 static int ixp4xx_mdio_register(void)
336 if (!(mdio_bus
= mdiobus_alloc()))
339 if (cpu_is_ixp43x()) {
340 /* IXP43x lacks NPE-B and uses NPE-C for MII PHY access */
341 if (!(ixp4xx_read_feature_bits() & IXP4XX_FEATURE_NPEC_ETH
))
343 mdio_regs
= (struct eth_regs __iomem
*)IXP4XX_EthC_BASE_VIRT
;
345 /* All MII PHY accesses use NPE-B Ethernet registers */
346 if (!(ixp4xx_read_feature_bits() & IXP4XX_FEATURE_NPEB_ETH0
))
348 mdio_regs
= (struct eth_regs __iomem
*)IXP4XX_EthB_BASE_VIRT
;
351 __raw_writel(DEFAULT_CORE_CNTRL
, &mdio_regs
->core_control
);
352 spin_lock_init(&mdio_lock
);
353 mdio_bus
->name
= "IXP4xx MII Bus";
354 mdio_bus
->read
= &ixp4xx_mdio_read
;
355 mdio_bus
->write
= &ixp4xx_mdio_write
;
356 strcpy(mdio_bus
->id
, "0");
358 if ((err
= mdiobus_register(mdio_bus
)))
359 mdiobus_free(mdio_bus
);
363 static void ixp4xx_mdio_remove(void)
365 mdiobus_unregister(mdio_bus
);
366 mdiobus_free(mdio_bus
);
370 static void ixp4xx_adjust_link(struct net_device
*dev
)
372 struct port
*port
= netdev_priv(dev
);
373 struct phy_device
*phydev
= port
->phydev
;
378 printk(KERN_INFO
"%s: link down\n", dev
->name
);
383 if (port
->speed
== phydev
->speed
&& port
->duplex
== phydev
->duplex
)
386 port
->speed
= phydev
->speed
;
387 port
->duplex
= phydev
->duplex
;
390 __raw_writel(DEFAULT_TX_CNTRL0
& ~TX_CNTRL0_HALFDUPLEX
,
391 &port
->regs
->tx_control
[0]);
393 __raw_writel(DEFAULT_TX_CNTRL0
| TX_CNTRL0_HALFDUPLEX
,
394 &port
->regs
->tx_control
[0]);
396 printk(KERN_INFO
"%s: link up, speed %u Mb/s, %s duplex\n",
397 dev
->name
, port
->speed
, port
->duplex
? "full" : "half");
401 static inline void debug_pkt(struct net_device
*dev
, const char *func
,
407 printk(KERN_DEBUG
"%s: %s(%i) ", dev
->name
, func
, len
);
408 for (i
= 0; i
< len
; i
++) {
409 if (i
>= DEBUG_PKT_BYTES
)
412 ((i
== 6) || (i
== 12) || (i
>= 14)) ? " " : "",
420 static inline void debug_desc(u32 phys
, struct desc
*desc
)
423 printk(KERN_DEBUG
"%X: %X %3X %3X %08X %2X < %2X %4X %X"
424 " %X %X %02X%02X%02X%02X%02X%02X < %02X%02X%02X%02X%02X%02X\n",
425 phys
, desc
->next
, desc
->buf_len
, desc
->pkt_len
,
426 desc
->data
, desc
->dest_id
, desc
->src_id
, desc
->flags
,
427 desc
->qos
, desc
->padlen
, desc
->vlan_tci
,
428 desc
->dst_mac_0
, desc
->dst_mac_1
, desc
->dst_mac_2
,
429 desc
->dst_mac_3
, desc
->dst_mac_4
, desc
->dst_mac_5
,
430 desc
->src_mac_0
, desc
->src_mac_1
, desc
->src_mac_2
,
431 desc
->src_mac_3
, desc
->src_mac_4
, desc
->src_mac_5
);
435 static inline int queue_get_desc(unsigned int queue
, struct port
*port
,
438 u32 phys
, tab_phys
, n_desc
;
441 if (!(phys
= qmgr_get_entry(queue
)))
444 phys
&= ~0x1F; /* mask out non-address bits */
445 tab_phys
= is_tx
? tx_desc_phys(port
, 0) : rx_desc_phys(port
, 0);
446 tab
= is_tx
? tx_desc_ptr(port
, 0) : rx_desc_ptr(port
, 0);
447 n_desc
= (phys
- tab_phys
) / sizeof(struct desc
);
448 BUG_ON(n_desc
>= (is_tx
? TX_DESCS
: RX_DESCS
));
449 debug_desc(phys
, &tab
[n_desc
]);
450 BUG_ON(tab
[n_desc
].next
);
454 static inline void queue_put_desc(unsigned int queue
, u32 phys
,
457 debug_desc(phys
, desc
);
459 qmgr_put_entry(queue
, phys
);
460 /* Don't check for queue overflow here, we've allocated sufficient
461 length and queues >= 32 don't support this check anyway. */
465 static inline void dma_unmap_tx(struct port
*port
, struct desc
*desc
)
468 dma_unmap_single(&port
->netdev
->dev
, desc
->data
,
469 desc
->buf_len
, DMA_TO_DEVICE
);
471 dma_unmap_single(&port
->netdev
->dev
, desc
->data
& ~3,
472 ALIGN((desc
->data
& 3) + desc
->buf_len
, 4),
478 static void eth_rx_irq(void *pdev
)
480 struct net_device
*dev
= pdev
;
481 struct port
*port
= netdev_priv(dev
);
484 printk(KERN_DEBUG
"%s: eth_rx_irq\n", dev
->name
);
486 qmgr_disable_irq(port
->plat
->rxq
);
487 napi_schedule(&port
->napi
);
490 static int eth_poll(struct napi_struct
*napi
, int budget
)
492 struct port
*port
= container_of(napi
, struct port
, napi
);
493 struct net_device
*dev
= port
->netdev
;
494 unsigned int rxq
= port
->plat
->rxq
, rxfreeq
= RXFREE_QUEUE(port
->id
);
498 printk(KERN_DEBUG
"%s: eth_poll\n", dev
->name
);
501 while (received
< budget
) {
506 struct sk_buff
*temp
;
510 if ((n
= queue_get_desc(rxq
, port
, 0)) < 0) {
512 printk(KERN_DEBUG
"%s: eth_poll napi_complete\n",
516 qmgr_enable_irq(rxq
);
517 if (!qmgr_stat_below_low_watermark(rxq
) &&
518 napi_reschedule(napi
)) { /* not empty again */
520 printk(KERN_DEBUG
"%s: eth_poll"
521 " napi_reschedule successed\n",
524 qmgr_disable_irq(rxq
);
528 printk(KERN_DEBUG
"%s: eth_poll all done\n",
531 return received
; /* all work done */
534 desc
= rx_desc_ptr(port
, n
);
537 if ((skb
= netdev_alloc_skb(dev
, RX_BUFF_SIZE
))) {
538 phys
= dma_map_single(&dev
->dev
, skb
->data
,
539 RX_BUFF_SIZE
, DMA_FROM_DEVICE
);
540 if (dma_mapping_error(&dev
->dev
, phys
)) {
546 skb
= netdev_alloc_skb(dev
,
547 ALIGN(NET_IP_ALIGN
+ desc
->pkt_len
, 4));
551 dev
->stats
.rx_dropped
++;
552 /* put the desc back on RX-ready queue */
553 desc
->buf_len
= MAX_MRU
;
555 queue_put_desc(rxfreeq
, rx_desc_phys(port
, n
), desc
);
559 /* process received frame */
562 skb
= port
->rx_buff_tab
[n
];
563 dma_unmap_single(&dev
->dev
, desc
->data
- NET_IP_ALIGN
,
564 RX_BUFF_SIZE
, DMA_FROM_DEVICE
);
566 dma_sync_single_for_cpu(&dev
->dev
, desc
->data
- NET_IP_ALIGN
,
567 RX_BUFF_SIZE
, DMA_FROM_DEVICE
);
568 memcpy_swab32((u32
*)skb
->data
, (u32
*)port
->rx_buff_tab
[n
],
569 ALIGN(NET_IP_ALIGN
+ desc
->pkt_len
, 4) / 4);
571 skb_reserve(skb
, NET_IP_ALIGN
);
572 skb_put(skb
, desc
->pkt_len
);
574 debug_pkt(dev
, "eth_poll", skb
->data
, skb
->len
);
576 skb
->protocol
= eth_type_trans(skb
, dev
);
577 dev
->stats
.rx_packets
++;
578 dev
->stats
.rx_bytes
+= skb
->len
;
579 netif_receive_skb(skb
);
581 /* put the new buffer on RX-free queue */
583 port
->rx_buff_tab
[n
] = temp
;
584 desc
->data
= phys
+ NET_IP_ALIGN
;
586 desc
->buf_len
= MAX_MRU
;
588 queue_put_desc(rxfreeq
, rx_desc_phys(port
, n
), desc
);
593 printk(KERN_DEBUG
"eth_poll(): end, not all work done\n");
595 return received
; /* not all work done */
599 static void eth_txdone_irq(void *unused
)
604 printk(KERN_DEBUG DRV_NAME
": eth_txdone_irq\n");
606 while ((phys
= qmgr_get_entry(TXDONE_QUEUE
)) != 0) {
613 BUG_ON(npe_id
>= MAX_NPES
);
614 port
= npe_port_tab
[npe_id
];
616 phys
&= ~0x1F; /* mask out non-address bits */
617 n_desc
= (phys
- tx_desc_phys(port
, 0)) / sizeof(struct desc
);
618 BUG_ON(n_desc
>= TX_DESCS
);
619 desc
= tx_desc_ptr(port
, n_desc
);
620 debug_desc(phys
, desc
);
622 if (port
->tx_buff_tab
[n_desc
]) { /* not the draining packet */
623 port
->netdev
->stats
.tx_packets
++;
624 port
->netdev
->stats
.tx_bytes
+= desc
->pkt_len
;
626 dma_unmap_tx(port
, desc
);
628 printk(KERN_DEBUG
"%s: eth_txdone_irq free %p\n",
629 port
->netdev
->name
, port
->tx_buff_tab
[n_desc
]);
631 free_buffer_irq(port
->tx_buff_tab
[n_desc
]);
632 port
->tx_buff_tab
[n_desc
] = NULL
;
635 start
= qmgr_stat_below_low_watermark(port
->plat
->txreadyq
);
636 queue_put_desc(port
->plat
->txreadyq
, phys
, desc
);
637 if (start
) { /* TX-ready queue was empty */
639 printk(KERN_DEBUG
"%s: eth_txdone_irq xmit ready\n",
642 netif_wake_queue(port
->netdev
);
647 static int eth_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
649 struct port
*port
= netdev_priv(dev
);
650 unsigned int txreadyq
= port
->plat
->txreadyq
;
651 int len
, offset
, bytes
, n
;
657 printk(KERN_DEBUG
"%s: eth_xmit\n", dev
->name
);
660 if (unlikely(skb
->len
> MAX_MRU
)) {
662 dev
->stats
.tx_errors
++;
666 debug_pkt(dev
, "eth_xmit", skb
->data
, skb
->len
);
670 offset
= 0; /* no need to keep alignment */
674 offset
= (int)skb
->data
& 3; /* keep 32-bit alignment */
675 bytes
= ALIGN(offset
+ len
, 4);
676 if (!(mem
= kmalloc(bytes
, GFP_ATOMIC
))) {
678 dev
->stats
.tx_dropped
++;
681 memcpy_swab32(mem
, (u32
*)((int)skb
->data
& ~3), bytes
/ 4);
685 phys
= dma_map_single(&dev
->dev
, mem
, bytes
, DMA_TO_DEVICE
);
686 if (dma_mapping_error(&dev
->dev
, phys
)) {
692 dev
->stats
.tx_dropped
++;
696 n
= queue_get_desc(txreadyq
, port
, 1);
698 desc
= tx_desc_ptr(port
, n
);
701 port
->tx_buff_tab
[n
] = skb
;
703 port
->tx_buff_tab
[n
] = mem
;
705 desc
->data
= phys
+ offset
;
706 desc
->buf_len
= desc
->pkt_len
= len
;
708 /* NPE firmware pads short frames with zeros internally */
710 queue_put_desc(TX_QUEUE(port
->id
), tx_desc_phys(port
, n
), desc
);
711 dev
->trans_start
= jiffies
;
713 if (qmgr_stat_below_low_watermark(txreadyq
)) { /* empty */
715 printk(KERN_DEBUG
"%s: eth_xmit queue full\n", dev
->name
);
717 netif_stop_queue(dev
);
718 /* we could miss TX ready interrupt */
719 /* really empty in fact */
720 if (!qmgr_stat_below_low_watermark(txreadyq
)) {
722 printk(KERN_DEBUG
"%s: eth_xmit ready again\n",
725 netif_wake_queue(dev
);
730 printk(KERN_DEBUG
"%s: eth_xmit end\n", dev
->name
);
736 static void eth_set_mcast_list(struct net_device
*dev
)
738 struct port
*port
= netdev_priv(dev
);
739 struct dev_mc_list
*mclist
;
740 u8 diffs
[ETH_ALEN
], *addr
;
743 if ((dev
->flags
& IFF_PROMISC
) || netdev_mc_empty(dev
)) {
744 __raw_writel(DEFAULT_RX_CNTRL0
& ~RX_CNTRL0_ADDR_FLTR_EN
,
745 &port
->regs
->rx_control
[0]);
749 memset(diffs
, 0, ETH_ALEN
);
752 netdev_for_each_mc_addr(mclist
, dev
) {
754 addr
= mclist
->dmi_addr
; /* first MAC address */
755 for (i
= 0; i
< ETH_ALEN
; i
++)
756 diffs
[i
] |= addr
[i
] ^ mclist
->dmi_addr
[i
];
759 for (i
= 0; i
< ETH_ALEN
; i
++) {
760 __raw_writel(addr
[i
], &port
->regs
->mcast_addr
[i
]);
761 __raw_writel(~diffs
[i
], &port
->regs
->mcast_mask
[i
]);
764 __raw_writel(DEFAULT_RX_CNTRL0
| RX_CNTRL0_ADDR_FLTR_EN
,
765 &port
->regs
->rx_control
[0]);
769 static int eth_ioctl(struct net_device
*dev
, struct ifreq
*req
, int cmd
)
771 struct port
*port
= netdev_priv(dev
);
773 if (!netif_running(dev
))
775 return phy_mii_ioctl(port
->phydev
, if_mii(req
), cmd
);
778 /* ethtool support */
780 static void ixp4xx_get_drvinfo(struct net_device
*dev
,
781 struct ethtool_drvinfo
*info
)
783 struct port
*port
= netdev_priv(dev
);
784 strcpy(info
->driver
, DRV_NAME
);
785 snprintf(info
->fw_version
, sizeof(info
->fw_version
), "%u:%u:%u:%u",
786 port
->firmware
[0], port
->firmware
[1],
787 port
->firmware
[2], port
->firmware
[3]);
788 strcpy(info
->bus_info
, "internal");
791 static int ixp4xx_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
793 struct port
*port
= netdev_priv(dev
);
794 return phy_ethtool_gset(port
->phydev
, cmd
);
797 static int ixp4xx_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
799 struct port
*port
= netdev_priv(dev
);
800 return phy_ethtool_sset(port
->phydev
, cmd
);
803 static int ixp4xx_nway_reset(struct net_device
*dev
)
805 struct port
*port
= netdev_priv(dev
);
806 return phy_start_aneg(port
->phydev
);
809 static const struct ethtool_ops ixp4xx_ethtool_ops
= {
810 .get_drvinfo
= ixp4xx_get_drvinfo
,
811 .get_settings
= ixp4xx_get_settings
,
812 .set_settings
= ixp4xx_set_settings
,
813 .nway_reset
= ixp4xx_nway_reset
,
814 .get_link
= ethtool_op_get_link
,
818 static int request_queues(struct port
*port
)
822 err
= qmgr_request_queue(RXFREE_QUEUE(port
->id
), RX_DESCS
, 0, 0,
823 "%s:RX-free", port
->netdev
->name
);
827 err
= qmgr_request_queue(port
->plat
->rxq
, RX_DESCS
, 0, 0,
828 "%s:RX", port
->netdev
->name
);
832 err
= qmgr_request_queue(TX_QUEUE(port
->id
), TX_DESCS
, 0, 0,
833 "%s:TX", port
->netdev
->name
);
837 err
= qmgr_request_queue(port
->plat
->txreadyq
, TX_DESCS
, 0, 0,
838 "%s:TX-ready", port
->netdev
->name
);
842 /* TX-done queue handles skbs sent out by the NPEs */
844 err
= qmgr_request_queue(TXDONE_QUEUE
, TXDONE_QUEUE_LEN
, 0, 0,
845 "%s:TX-done", DRV_NAME
);
852 qmgr_release_queue(port
->plat
->txreadyq
);
854 qmgr_release_queue(TX_QUEUE(port
->id
));
856 qmgr_release_queue(port
->plat
->rxq
);
858 qmgr_release_queue(RXFREE_QUEUE(port
->id
));
859 printk(KERN_DEBUG
"%s: unable to request hardware queues\n",
864 static void release_queues(struct port
*port
)
866 qmgr_release_queue(RXFREE_QUEUE(port
->id
));
867 qmgr_release_queue(port
->plat
->rxq
);
868 qmgr_release_queue(TX_QUEUE(port
->id
));
869 qmgr_release_queue(port
->plat
->txreadyq
);
872 qmgr_release_queue(TXDONE_QUEUE
);
875 static int init_queues(struct port
*port
)
880 if (!(dma_pool
= dma_pool_create(DRV_NAME
, NULL
,
881 POOL_ALLOC_SIZE
, 32, 0)))
884 if (!(port
->desc_tab
= dma_pool_alloc(dma_pool
, GFP_KERNEL
,
885 &port
->desc_tab_phys
)))
887 memset(port
->desc_tab
, 0, POOL_ALLOC_SIZE
);
888 memset(port
->rx_buff_tab
, 0, sizeof(port
->rx_buff_tab
)); /* tables */
889 memset(port
->tx_buff_tab
, 0, sizeof(port
->tx_buff_tab
));
891 /* Setup RX buffers */
892 for (i
= 0; i
< RX_DESCS
; i
++) {
893 struct desc
*desc
= rx_desc_ptr(port
, i
);
894 buffer_t
*buff
; /* skb or kmalloc()ated memory */
897 if (!(buff
= netdev_alloc_skb(port
->netdev
, RX_BUFF_SIZE
)))
901 if (!(buff
= kmalloc(RX_BUFF_SIZE
, GFP_KERNEL
)))
905 desc
->buf_len
= MAX_MRU
;
906 desc
->data
= dma_map_single(&port
->netdev
->dev
, data
,
907 RX_BUFF_SIZE
, DMA_FROM_DEVICE
);
908 if (dma_mapping_error(&port
->netdev
->dev
, desc
->data
)) {
912 desc
->data
+= NET_IP_ALIGN
;
913 port
->rx_buff_tab
[i
] = buff
;
919 static void destroy_queues(struct port
*port
)
923 if (port
->desc_tab
) {
924 for (i
= 0; i
< RX_DESCS
; i
++) {
925 struct desc
*desc
= rx_desc_ptr(port
, i
);
926 buffer_t
*buff
= port
->rx_buff_tab
[i
];
928 dma_unmap_single(&port
->netdev
->dev
,
929 desc
->data
- NET_IP_ALIGN
,
930 RX_BUFF_SIZE
, DMA_FROM_DEVICE
);
934 for (i
= 0; i
< TX_DESCS
; i
++) {
935 struct desc
*desc
= tx_desc_ptr(port
, i
);
936 buffer_t
*buff
= port
->tx_buff_tab
[i
];
938 dma_unmap_tx(port
, desc
);
942 dma_pool_free(dma_pool
, port
->desc_tab
, port
->desc_tab_phys
);
943 port
->desc_tab
= NULL
;
946 if (!ports_open
&& dma_pool
) {
947 dma_pool_destroy(dma_pool
);
952 static int eth_open(struct net_device
*dev
)
954 struct port
*port
= netdev_priv(dev
);
955 struct npe
*npe
= port
->npe
;
959 if (!npe_running(npe
)) {
960 err
= npe_load_firmware(npe
, npe_name(npe
), &dev
->dev
);
964 if (npe_recv_message(npe
, &msg
, "ETH_GET_STATUS")) {
965 printk(KERN_ERR
"%s: %s not responding\n", dev
->name
,
969 port
->firmware
[0] = msg
.byte4
;
970 port
->firmware
[1] = msg
.byte5
;
971 port
->firmware
[2] = msg
.byte6
;
972 port
->firmware
[3] = msg
.byte7
;
975 memset(&msg
, 0, sizeof(msg
));
976 msg
.cmd
= NPE_VLAN_SETRXQOSENTRY
;
977 msg
.eth_id
= port
->id
;
978 msg
.byte5
= port
->plat
->rxq
| 0x80;
979 msg
.byte7
= port
->plat
->rxq
<< 4;
980 for (i
= 0; i
< 8; i
++) {
982 if (npe_send_recv_message(port
->npe
, &msg
, "ETH_SET_RXQ"))
986 msg
.cmd
= NPE_EDB_SETPORTADDRESS
;
987 msg
.eth_id
= PHYSICAL_ID(port
->id
);
988 msg
.byte2
= dev
->dev_addr
[0];
989 msg
.byte3
= dev
->dev_addr
[1];
990 msg
.byte4
= dev
->dev_addr
[2];
991 msg
.byte5
= dev
->dev_addr
[3];
992 msg
.byte6
= dev
->dev_addr
[4];
993 msg
.byte7
= dev
->dev_addr
[5];
994 if (npe_send_recv_message(port
->npe
, &msg
, "ETH_SET_MAC"))
997 memset(&msg
, 0, sizeof(msg
));
998 msg
.cmd
= NPE_FW_SETFIREWALLMODE
;
999 msg
.eth_id
= port
->id
;
1000 if (npe_send_recv_message(port
->npe
, &msg
, "ETH_SET_FIREWALL_MODE"))
1003 if ((err
= request_queues(port
)) != 0)
1006 if ((err
= init_queues(port
)) != 0) {
1007 destroy_queues(port
);
1008 release_queues(port
);
1012 port
->speed
= 0; /* force "link up" message */
1013 phy_start(port
->phydev
);
1015 for (i
= 0; i
< ETH_ALEN
; i
++)
1016 __raw_writel(dev
->dev_addr
[i
], &port
->regs
->hw_addr
[i
]);
1017 __raw_writel(0x08, &port
->regs
->random_seed
);
1018 __raw_writel(0x12, &port
->regs
->partial_empty_threshold
);
1019 __raw_writel(0x30, &port
->regs
->partial_full_threshold
);
1020 __raw_writel(0x08, &port
->regs
->tx_start_bytes
);
1021 __raw_writel(0x15, &port
->regs
->tx_deferral
);
1022 __raw_writel(0x08, &port
->regs
->tx_2part_deferral
[0]);
1023 __raw_writel(0x07, &port
->regs
->tx_2part_deferral
[1]);
1024 __raw_writel(0x80, &port
->regs
->slot_time
);
1025 __raw_writel(0x01, &port
->regs
->int_clock_threshold
);
1027 /* Populate queues with buffers, no failure after this point */
1028 for (i
= 0; i
< TX_DESCS
; i
++)
1029 queue_put_desc(port
->plat
->txreadyq
,
1030 tx_desc_phys(port
, i
), tx_desc_ptr(port
, i
));
1032 for (i
= 0; i
< RX_DESCS
; i
++)
1033 queue_put_desc(RXFREE_QUEUE(port
->id
),
1034 rx_desc_phys(port
, i
), rx_desc_ptr(port
, i
));
1036 __raw_writel(TX_CNTRL1_RETRIES
, &port
->regs
->tx_control
[1]);
1037 __raw_writel(DEFAULT_TX_CNTRL0
, &port
->regs
->tx_control
[0]);
1038 __raw_writel(0, &port
->regs
->rx_control
[1]);
1039 __raw_writel(DEFAULT_RX_CNTRL0
, &port
->regs
->rx_control
[0]);
1041 napi_enable(&port
->napi
);
1042 eth_set_mcast_list(dev
);
1043 netif_start_queue(dev
);
1045 qmgr_set_irq(port
->plat
->rxq
, QUEUE_IRQ_SRC_NOT_EMPTY
,
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 napi_schedule(&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 */
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 phy_stop(port
->phydev
);
1137 qmgr_disable_irq(TXDONE_QUEUE
);
1138 destroy_queues(port
);
1139 release_queues(port
);
1143 static const struct net_device_ops ixp4xx_netdev_ops
= {
1144 .ndo_open
= eth_open
,
1145 .ndo_stop
= eth_close
,
1146 .ndo_start_xmit
= eth_xmit
,
1147 .ndo_set_multicast_list
= eth_set_mcast_list
,
1148 .ndo_do_ioctl
= eth_ioctl
,
1149 .ndo_change_mtu
= eth_change_mtu
,
1150 .ndo_set_mac_address
= eth_mac_addr
,
1151 .ndo_validate_addr
= eth_validate_addr
,
1154 static int __devinit
eth_init_one(struct platform_device
*pdev
)
1157 struct net_device
*dev
;
1158 struct eth_plat_info
*plat
= pdev
->dev
.platform_data
;
1160 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1163 if (!(dev
= alloc_etherdev(sizeof(struct port
))))
1166 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1167 port
= netdev_priv(dev
);
1169 port
->id
= pdev
->id
;
1172 case IXP4XX_ETH_NPEA
:
1173 port
->regs
= (struct eth_regs __iomem
*)IXP4XX_EthA_BASE_VIRT
;
1174 regs_phys
= IXP4XX_EthA_BASE_PHYS
;
1176 case IXP4XX_ETH_NPEB
:
1177 port
->regs
= (struct eth_regs __iomem
*)IXP4XX_EthB_BASE_VIRT
;
1178 regs_phys
= IXP4XX_EthB_BASE_PHYS
;
1180 case IXP4XX_ETH_NPEC
:
1181 port
->regs
= (struct eth_regs __iomem
*)IXP4XX_EthC_BASE_VIRT
;
1182 regs_phys
= IXP4XX_EthC_BASE_PHYS
;
1189 dev
->netdev_ops
= &ixp4xx_netdev_ops
;
1190 dev
->ethtool_ops
= &ixp4xx_ethtool_ops
;
1191 dev
->tx_queue_len
= 100;
1193 netif_napi_add(dev
, &port
->napi
, eth_poll
, NAPI_WEIGHT
);
1195 if (!(port
->npe
= npe_request(NPE_ID(port
->id
)))) {
1200 port
->mem_res
= request_mem_region(regs_phys
, REGS_SIZE
, dev
->name
);
1201 if (!port
->mem_res
) {
1207 npe_port_tab
[NPE_ID(port
->id
)] = port
;
1208 memcpy(dev
->dev_addr
, plat
->hwaddr
, ETH_ALEN
);
1210 platform_set_drvdata(pdev
, dev
);
1212 __raw_writel(DEFAULT_CORE_CNTRL
| CORE_RESET
,
1213 &port
->regs
->core_control
);
1215 __raw_writel(DEFAULT_CORE_CNTRL
, &port
->regs
->core_control
);
1218 snprintf(phy_id
, MII_BUS_ID_SIZE
+ 3, PHY_ID_FMT
, "0", plat
->phy
);
1219 port
->phydev
= phy_connect(dev
, phy_id
, &ixp4xx_adjust_link
, 0,
1220 PHY_INTERFACE_MODE_MII
);
1221 if ((err
= IS_ERR(port
->phydev
)))
1224 port
->phydev
->irq
= PHY_POLL
;
1226 if ((err
= register_netdev(dev
)))
1229 printk(KERN_INFO
"%s: MII PHY %i on %s\n", dev
->name
, plat
->phy
,
1230 npe_name(port
->npe
));
1235 phy_disconnect(port
->phydev
);
1237 npe_port_tab
[NPE_ID(port
->id
)] = NULL
;
1238 platform_set_drvdata(pdev
, NULL
);
1239 release_resource(port
->mem_res
);
1241 npe_release(port
->npe
);
1247 static int __devexit
eth_remove_one(struct platform_device
*pdev
)
1249 struct net_device
*dev
= platform_get_drvdata(pdev
);
1250 struct port
*port
= netdev_priv(dev
);
1252 unregister_netdev(dev
);
1253 phy_disconnect(port
->phydev
);
1254 npe_port_tab
[NPE_ID(port
->id
)] = NULL
;
1255 platform_set_drvdata(pdev
, NULL
);
1256 npe_release(port
->npe
);
1257 release_resource(port
->mem_res
);
1262 static struct platform_driver ixp4xx_eth_driver
= {
1263 .driver
.name
= DRV_NAME
,
1264 .probe
= eth_init_one
,
1265 .remove
= eth_remove_one
,
1268 static int __init
eth_init_module(void)
1271 if ((err
= ixp4xx_mdio_register()))
1273 return platform_driver_register(&ixp4xx_eth_driver
);
1276 static void __exit
eth_cleanup_module(void)
1278 platform_driver_unregister(&ixp4xx_eth_driver
);
1279 ixp4xx_mdio_remove();
1282 MODULE_AUTHOR("Krzysztof Halasa");
1283 MODULE_DESCRIPTION("Intel IXP4xx Ethernet driver");
1284 MODULE_LICENSE("GPL v2");
1285 MODULE_ALIAS("platform:ixp4xx_eth");
1286 module_init(eth_init_module
);
1287 module_exit(eth_cleanup_module
);