2 * IXP2000 MSF network device driver
3 * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org>
4 * Dedicated to Marija Kulikova.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/init.h>
17 #include <linux/moduleparam.h>
18 #include <asm/hardware/uengine.h>
20 #include "ixp2400_rx.ucode"
21 #include "ixp2400_tx.ucode"
22 #include "ixpdev_priv.h"
25 #define DRV_MODULE_VERSION "0.2"
28 static struct net_device **nds;
30 static void (*set_port_admin_status)(int port, int up);
32 static struct ixpdev_rx_desc * const rx_desc =
33 (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
34 static struct ixpdev_tx_desc * const tx_desc =
35 (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
36 static int tx_pointer;
39 static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev)
41 struct ixpdev_priv *ip = netdev_priv(dev);
42 struct ixpdev_tx_desc *desc;
45 if (unlikely(skb->len > PAGE_SIZE)) {
46 /* @@@ Count drops. */
52 tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT;
54 desc = tx_desc + entry;
55 desc->pkt_length = skb->len;
56 desc->channel = ip->channel;
58 skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr));
61 ixp2000_reg_write(RING_TX_PENDING,
62 TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc)));
64 dev->trans_start = jiffies;
67 ip->tx_queue_entries++;
68 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
69 netif_stop_queue(dev);
76 static int ixpdev_rx(struct net_device *dev, int processed, int budget)
78 while (processed < budget) {
79 struct ixpdev_rx_desc *desc;
84 _desc = ixp2000_reg_read(RING_RX_DONE);
89 ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc));
90 buf = phys_to_virt(desc->buf_addr);
92 if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) {
93 printk(KERN_ERR "ixp2000: rx err, length %d\n",
98 if (desc->channel < 0 || desc->channel >= nds_count) {
99 printk(KERN_ERR "ixp2000: rx err, channel %d\n",
104 /* @@@ Make FCS stripping configurable. */
105 desc->pkt_length -= 4;
107 if (unlikely(!netif_running(nds[desc->channel])))
110 skb = netdev_alloc_skb(dev, desc->pkt_length + 2);
111 if (likely(skb != NULL)) {
113 skb_copy_to_linear_data(skb, buf, desc->pkt_length);
114 skb_put(skb, desc->pkt_length);
115 skb->protocol = eth_type_trans(skb, nds[desc->channel]);
117 netif_receive_skb(skb);
121 ixp2000_reg_write(RING_RX_PENDING, _desc);
128 /* dev always points to nds[0]. */
129 static int ixpdev_poll(struct napi_struct *napi, int budget)
131 struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi);
132 struct net_device *dev = ip->dev;
137 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
139 rx = ixpdev_rx(dev, rx, budget);
142 } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
145 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
150 static void ixpdev_tx_complete(void)
157 struct ixpdev_priv *ip;
161 desc = ixp2000_reg_read(RING_TX_DONE);
165 /* @@@ Check whether entries come back in order. */
166 entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc);
167 channel = tx_desc[entry].channel;
169 if (channel < 0 || channel >= nds_count) {
170 printk(KERN_ERR "ixp2000: txcomp channel index "
171 "out of bounds (%d, %.8i, %d)\n",
172 channel, (unsigned int)desc, entry);
176 ip = netdev_priv(nds[channel]);
177 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
178 wake |= 1 << channel;
179 ip->tx_queue_entries--;
182 for (channel = 0; wake != 0; channel++) {
183 if (wake & (1 << channel)) {
184 netif_wake_queue(nds[channel]);
185 wake &= ~(1 << channel);
190 static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
194 status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
199 * Any of the eight receive units signaled RX?
201 if (status & 0x00ff) {
202 struct net_device *dev = nds[0];
203 struct ixpdev_priv *ip = netdev_priv(dev);
205 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
206 if (likely(napi_schedule_prep(&ip->napi))) {
207 __napi_schedule(&ip->napi);
209 printk(KERN_CRIT "ixp2000: irq while polling!!\n");
214 * Any of the eight transmit units signaled TXdone?
216 if (status & 0xff00) {
217 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
218 ixpdev_tx_complete();
224 #ifdef CONFIG_NET_POLL_CONTROLLER
225 static void ixpdev_poll_controller(struct net_device *dev)
227 disable_irq(IRQ_IXP2000_THDA0);
228 ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
229 enable_irq(IRQ_IXP2000_THDA0);
233 static int ixpdev_open(struct net_device *dev)
235 struct ixpdev_priv *ip = netdev_priv(dev);
238 napi_enable(&ip->napi);
240 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
241 IRQF_SHARED, "ixp2000_eth", nds);
244 napi_disable(&ip->napi);
248 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
251 set_port_admin_status(ip->channel, 1);
252 netif_start_queue(dev);
257 static int ixpdev_close(struct net_device *dev)
259 struct ixpdev_priv *ip = netdev_priv(dev);
261 netif_stop_queue(dev);
262 napi_disable(&ip->napi);
263 set_port_admin_status(ip->channel, 0);
266 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
267 free_irq(IRQ_IXP2000_THDA0, nds);
273 static const struct net_device_ops ixpdev_netdev_ops = {
274 .ndo_open = ixpdev_open,
275 .ndo_stop = ixpdev_close,
276 .ndo_start_xmit = ixpdev_xmit,
277 .ndo_change_mtu = eth_change_mtu,
278 .ndo_validate_addr = eth_validate_addr,
279 .ndo_set_mac_address = eth_mac_addr,
280 #ifdef CONFIG_NET_POLL_CONTROLLER
281 .ndo_poll_controller = ixpdev_poll_controller,
285 struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
287 struct net_device *dev;
288 struct ixpdev_priv *ip;
290 dev = alloc_etherdev(sizeof_priv);
294 dev->netdev_ops = &ixpdev_netdev_ops;
296 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
298 ip = netdev_priv(dev);
300 netif_napi_add(dev, &ip->napi, ixpdev_poll, 64);
301 ip->channel = channel;
302 ip->tx_queue_entries = 0;
307 int ixpdev_init(int __nds_count, struct net_device **__nds,
308 void (*__set_port_admin_status)(int port, int up))
313 BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
315 printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
317 nds_count = __nds_count;
319 set_port_admin_status = __set_port_admin_status;
321 for (i = 0; i < RX_BUF_COUNT; i++) {
324 buf = (void *)get_zeroed_page(GFP_KERNEL);
328 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
331 rx_desc[i].buf_addr = virt_to_phys(buf);
332 rx_desc[i].buf_length = PAGE_SIZE;
335 /* @@@ Maybe we shouldn't be preallocating TX buffers. */
336 for (i = 0; i < TX_BUF_COUNT; i++) {
339 buf = (void *)get_zeroed_page(GFP_KERNEL);
343 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
346 tx_desc[i].buf_addr = virt_to_phys(buf);
349 /* 256 entries, ring status set means 'empty', base address 0x0000. */
350 ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
351 ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
352 ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
354 /* 256 entries, ring status set means 'full', base address 0x0400. */
355 ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
356 ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
357 ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
359 for (i = 0; i < RX_BUF_COUNT; i++) {
360 ixp2000_reg_write(RING_RX_PENDING,
361 RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
364 ixp2000_uengine_load(0, &ixp2400_rx);
365 ixp2000_uengine_start_contexts(0, 0xff);
367 /* 256 entries, ring status set means 'empty', base address 0x0800. */
368 ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
369 ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
370 ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
372 /* 256 entries, ring status set means 'full', base address 0x0c00. */
373 ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
374 ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
375 ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
377 ixp2000_uengine_load(1, &ixp2400_tx);
378 ixp2000_uengine_start_contexts(1, 0xff);
380 for (i = 0; i < nds_count; i++) {
381 err = register_netdev(nds[i]);
384 unregister_netdev(nds[i]);
389 for (i = 0; i < nds_count; i++) {
390 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
391 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
392 nds[i]->dev_addr[0], nds[i]->dev_addr[1],
393 nds[i]->dev_addr[2], nds[i]->dev_addr[3],
394 nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
400 for (i = 0; i < TX_BUF_COUNT; i++)
401 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
404 for (i = 0; i < RX_BUF_COUNT; i++)
405 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
411 void ixpdev_deinit(void)
415 /* @@@ Flush out pending packets. */
417 for (i = 0; i < nds_count; i++)
418 unregister_netdev(nds[i]);
420 ixp2000_uengine_stop_contexts(1, 0xff);
421 ixp2000_uengine_stop_contexts(0, 0xff);
422 ixp2000_uengine_reset(0x3);
424 for (i = 0; i < TX_BUF_COUNT; i++)
425 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
427 for (i = 0; i < RX_BUF_COUNT; i++)
428 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));