m68k: use KBUILD_DEFCONFIG
[linux-2.6/verdex.git] / drivers / net / ixp2000 / ixpdev.c
blob484cb2ba717f97d1df4f84b43701a9b7cf7b4bca
1 /*
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>
19 #include <asm/mach-types.h>
20 #include <asm/io.h>
21 #include "ixp2400_rx.ucode"
22 #include "ixp2400_tx.ucode"
23 #include "ixpdev_priv.h"
24 #include "ixpdev.h"
26 #define DRV_MODULE_VERSION "0.2"
28 static int nds_count;
29 static struct net_device **nds;
30 static int nds_open;
31 static void (*set_port_admin_status)(int port, int up);
33 static struct ixpdev_rx_desc * const rx_desc =
34 (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
35 static struct ixpdev_tx_desc * const tx_desc =
36 (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
37 static int tx_pointer;
40 static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev)
42 struct ixpdev_priv *ip = netdev_priv(dev);
43 struct ixpdev_tx_desc *desc;
44 int entry;
46 if (unlikely(skb->len > PAGE_SIZE)) {
47 /* @@@ Count drops. */
48 dev_kfree_skb(skb);
49 return 0;
52 entry = tx_pointer;
53 tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT;
55 desc = tx_desc + entry;
56 desc->pkt_length = skb->len;
57 desc->channel = ip->channel;
59 skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr));
60 dev_kfree_skb(skb);
62 ixp2000_reg_write(RING_TX_PENDING,
63 TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc)));
65 dev->trans_start = jiffies;
67 local_irq_disable();
68 ip->tx_queue_entries++;
69 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
70 netif_stop_queue(dev);
71 local_irq_enable();
73 return 0;
77 static int ixpdev_rx(struct net_device *dev, int processed, int budget)
79 while (processed < budget) {
80 struct ixpdev_rx_desc *desc;
81 struct sk_buff *skb;
82 void *buf;
83 u32 _desc;
85 _desc = ixp2000_reg_read(RING_RX_DONE);
86 if (_desc == 0)
87 return 0;
89 desc = rx_desc +
90 ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc));
91 buf = phys_to_virt(desc->buf_addr);
93 if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) {
94 printk(KERN_ERR "ixp2000: rx err, length %d\n",
95 desc->pkt_length);
96 goto err;
99 if (desc->channel < 0 || desc->channel >= nds_count) {
100 printk(KERN_ERR "ixp2000: rx err, channel %d\n",
101 desc->channel);
102 goto err;
105 /* @@@ Make FCS stripping configurable. */
106 desc->pkt_length -= 4;
108 if (unlikely(!netif_running(nds[desc->channel])))
109 goto err;
111 skb = dev_alloc_skb(desc->pkt_length + 2);
112 if (likely(skb != NULL)) {
113 skb_reserve(skb, 2);
114 skb_copy_to_linear_data(skb, buf, desc->pkt_length);
115 skb_put(skb, desc->pkt_length);
116 skb->protocol = eth_type_trans(skb, nds[desc->channel]);
118 skb->dev->last_rx = jiffies;
120 netif_receive_skb(skb);
123 err:
124 ixp2000_reg_write(RING_RX_PENDING, _desc);
125 processed++;
128 return processed;
131 /* dev always points to nds[0]. */
132 static int ixpdev_poll(struct napi_struct *napi, int budget)
134 struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi);
135 struct net_device *dev = ip->dev;
136 int rx;
138 rx = 0;
139 do {
140 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
142 rx = ixpdev_rx(dev, rx, budget);
143 if (rx >= budget)
144 break;
145 } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
147 netif_rx_complete(dev, napi);
148 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
150 return rx;
153 static void ixpdev_tx_complete(void)
155 int channel;
156 u32 wake;
158 wake = 0;
159 while (1) {
160 struct ixpdev_priv *ip;
161 u32 desc;
162 int entry;
164 desc = ixp2000_reg_read(RING_TX_DONE);
165 if (desc == 0)
166 break;
168 /* @@@ Check whether entries come back in order. */
169 entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc);
170 channel = tx_desc[entry].channel;
172 if (channel < 0 || channel >= nds_count) {
173 printk(KERN_ERR "ixp2000: txcomp channel index "
174 "out of bounds (%d, %.8i, %d)\n",
175 channel, (unsigned int)desc, entry);
176 continue;
179 ip = netdev_priv(nds[channel]);
180 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
181 wake |= 1 << channel;
182 ip->tx_queue_entries--;
185 for (channel = 0; wake != 0; channel++) {
186 if (wake & (1 << channel)) {
187 netif_wake_queue(nds[channel]);
188 wake &= ~(1 << channel);
193 static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
195 u32 status;
197 status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
198 if (status == 0)
199 return IRQ_NONE;
202 * Any of the eight receive units signaled RX?
204 if (status & 0x00ff) {
205 struct net_device *dev = nds[0];
206 struct ixpdev_priv *ip = netdev_priv(dev);
208 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
209 if (likely(napi_schedule_prep(&ip->napi))) {
210 __netif_rx_schedule(dev, &ip->napi);
211 } else {
212 printk(KERN_CRIT "ixp2000: irq while polling!!\n");
217 * Any of the eight transmit units signaled TXdone?
219 if (status & 0xff00) {
220 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
221 ixpdev_tx_complete();
224 return IRQ_HANDLED;
227 #ifdef CONFIG_NET_POLL_CONTROLLER
228 static void ixpdev_poll_controller(struct net_device *dev)
230 disable_irq(IRQ_IXP2000_THDA0);
231 ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
232 enable_irq(IRQ_IXP2000_THDA0);
234 #endif
236 static int ixpdev_open(struct net_device *dev)
238 struct ixpdev_priv *ip = netdev_priv(dev);
239 int err;
241 napi_enable(&ip->napi);
242 if (!nds_open++) {
243 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
244 IRQF_SHARED, "ixp2000_eth", nds);
245 if (err) {
246 nds_open--;
247 napi_disable(&ip->napi);
248 return err;
251 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
254 set_port_admin_status(ip->channel, 1);
255 netif_start_queue(dev);
257 return 0;
260 static int ixpdev_close(struct net_device *dev)
262 struct ixpdev_priv *ip = netdev_priv(dev);
264 netif_stop_queue(dev);
265 napi_disable(&ip->napi);
266 set_port_admin_status(ip->channel, 0);
268 if (!--nds_open) {
269 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
270 free_irq(IRQ_IXP2000_THDA0, nds);
273 return 0;
276 struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
278 struct net_device *dev;
279 struct ixpdev_priv *ip;
281 dev = alloc_etherdev(sizeof_priv);
282 if (dev == NULL)
283 return NULL;
285 dev->hard_start_xmit = ixpdev_xmit;
286 dev->open = ixpdev_open;
287 dev->stop = ixpdev_close;
288 #ifdef CONFIG_NET_POLL_CONTROLLER
289 dev->poll_controller = ixpdev_poll_controller;
290 #endif
292 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
294 ip = netdev_priv(dev);
295 ip->dev = dev;
296 netif_napi_add(dev, &ip->napi, ixpdev_poll, 64);
297 ip->channel = channel;
298 ip->tx_queue_entries = 0;
300 return dev;
303 int ixpdev_init(int __nds_count, struct net_device **__nds,
304 void (*__set_port_admin_status)(int port, int up))
306 int i;
307 int err;
309 BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
311 printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
313 nds_count = __nds_count;
314 nds = __nds;
315 set_port_admin_status = __set_port_admin_status;
317 for (i = 0; i < RX_BUF_COUNT; i++) {
318 void *buf;
320 buf = (void *)get_zeroed_page(GFP_KERNEL);
321 if (buf == NULL) {
322 err = -ENOMEM;
323 while (--i >= 0)
324 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
325 goto err_out;
327 rx_desc[i].buf_addr = virt_to_phys(buf);
328 rx_desc[i].buf_length = PAGE_SIZE;
331 /* @@@ Maybe we shouldn't be preallocating TX buffers. */
332 for (i = 0; i < TX_BUF_COUNT; i++) {
333 void *buf;
335 buf = (void *)get_zeroed_page(GFP_KERNEL);
336 if (buf == NULL) {
337 err = -ENOMEM;
338 while (--i >= 0)
339 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
340 goto err_free_rx;
342 tx_desc[i].buf_addr = virt_to_phys(buf);
345 /* 256 entries, ring status set means 'empty', base address 0x0000. */
346 ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
347 ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
348 ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
350 /* 256 entries, ring status set means 'full', base address 0x0400. */
351 ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
352 ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
353 ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
355 for (i = 0; i < RX_BUF_COUNT; i++) {
356 ixp2000_reg_write(RING_RX_PENDING,
357 RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
360 ixp2000_uengine_load(0, &ixp2400_rx);
361 ixp2000_uengine_start_contexts(0, 0xff);
363 /* 256 entries, ring status set means 'empty', base address 0x0800. */
364 ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
365 ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
366 ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
368 /* 256 entries, ring status set means 'full', base address 0x0c00. */
369 ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
370 ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
371 ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
373 ixp2000_uengine_load(1, &ixp2400_tx);
374 ixp2000_uengine_start_contexts(1, 0xff);
376 for (i = 0; i < nds_count; i++) {
377 err = register_netdev(nds[i]);
378 if (err) {
379 while (--i >= 0)
380 unregister_netdev(nds[i]);
381 goto err_free_tx;
385 for (i = 0; i < nds_count; i++) {
386 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
387 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
388 nds[i]->dev_addr[0], nds[i]->dev_addr[1],
389 nds[i]->dev_addr[2], nds[i]->dev_addr[3],
390 nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
393 return 0;
395 err_free_tx:
396 for (i = 0; i < TX_BUF_COUNT; i++)
397 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
399 err_free_rx:
400 for (i = 0; i < RX_BUF_COUNT; i++)
401 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
403 err_out:
404 return err;
407 void ixpdev_deinit(void)
409 int i;
411 /* @@@ Flush out pending packets. */
413 for (i = 0; i < nds_count; i++)
414 unregister_netdev(nds[i]);
416 ixp2000_uengine_stop_contexts(1, 0xff);
417 ixp2000_uengine_stop_contexts(0, 0xff);
418 ixp2000_uengine_reset(0x3);
420 for (i = 0; i < TX_BUF_COUNT; i++)
421 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
423 for (i = 0; i < RX_BUF_COUNT; i++)
424 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));