blob: 6c0dd49149d0d3047d55059fd2c54889a3e7786b [file] [log] [blame]
Lennert Buytenhek15d014d2005-11-11 18:23:13 +01001/*
2 * IXP2000 MSF network device driver
3 * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org>
4 * Dedicated to Marija Kulikova.
5 *
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.
10 */
11
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010012#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>
Lennert Buytenhek18ec5c72006-03-20 17:10:17 +000018#include <asm/hardware/uengine.h>
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010019#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"
25
Lennert Buytenhek12747782005-11-23 12:50:08 +010026#define DRV_MODULE_VERSION "0.2"
27
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010028static int nds_count;
29static struct net_device **nds;
30static int nds_open;
31static void (*set_port_admin_status)(int port, int up);
32
33static struct ixpdev_rx_desc * const rx_desc =
34 (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
35static struct ixpdev_tx_desc * const tx_desc =
36 (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
37static int tx_pointer;
38
39
40static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev)
41{
42 struct ixpdev_priv *ip = netdev_priv(dev);
43 struct ixpdev_tx_desc *desc;
44 int entry;
45
46 if (unlikely(skb->len > PAGE_SIZE)) {
47 /* @@@ Count drops. */
48 dev_kfree_skb(skb);
49 return 0;
50 }
51
52 entry = tx_pointer;
53 tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT;
54
55 desc = tx_desc + entry;
56 desc->pkt_length = skb->len;
57 desc->channel = ip->channel;
58
59 skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr));
60 dev_kfree_skb(skb);
61
62 ixp2000_reg_write(RING_TX_PENDING,
63 TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc)));
64
65 dev->trans_start = jiffies;
66
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();
72
73 return 0;
74}
75
76
Stephen Hemmingerbea33482007-10-03 16:41:36 -070077static int ixpdev_rx(struct net_device *dev, int processed, int budget)
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010078{
Stephen Hemmingerbea33482007-10-03 16:41:36 -070079 while (processed < budget) {
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010080 struct ixpdev_rx_desc *desc;
81 struct sk_buff *skb;
82 void *buf;
83 u32 _desc;
84
85 _desc = ixp2000_reg_read(RING_RX_DONE);
86 if (_desc == 0)
87 return 0;
88
89 desc = rx_desc +
90 ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc));
91 buf = phys_to_virt(desc->buf_addr);
92
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;
97 }
98
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;
103 }
104
105 /* @@@ Make FCS stripping configurable. */
106 desc->pkt_length -= 4;
107
108 if (unlikely(!netif_running(nds[desc->channel])))
109 goto err;
110
111 skb = dev_alloc_skb(desc->pkt_length + 2);
112 if (likely(skb != NULL)) {
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100113 skb_reserve(skb, 2);
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700114 skb_copy_to_linear_data(skb, buf, desc->pkt_length);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100115 skb_put(skb, desc->pkt_length);
Arnaldo Carvalho de Melo4c13eb62007-04-25 17:40:23 -0700116 skb->protocol = eth_type_trans(skb, nds[desc->channel]);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100117
118 skb->dev->last_rx = jiffies;
119
120 netif_receive_skb(skb);
121 }
122
123err:
124 ixp2000_reg_write(RING_RX_PENDING, _desc);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700125 processed++;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100126 }
127
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700128 return processed;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100129}
130
131/* dev always points to nds[0]. */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700132static int ixpdev_poll(struct napi_struct *napi, int budget)
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100133{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700134 struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi);
135 struct net_device *dev = ip->dev;
136 int rx;
137
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100138 /* @@@ Have to stop polling when nds[0] is administratively
139 * downed while we are polling. */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700140 rx = 0;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100141 do {
142 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
143
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700144 rx = ixpdev_rx(dev, rx, budget);
145 if (rx >= budget)
146 break;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100147 } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
148
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700149 netif_rx_complete(dev, napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100150 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
151
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700152 return rx;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100153}
154
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100155static void ixpdev_tx_complete(void)
156{
157 int channel;
158 u32 wake;
159
160 wake = 0;
161 while (1) {
162 struct ixpdev_priv *ip;
163 u32 desc;
164 int entry;
165
166 desc = ixp2000_reg_read(RING_TX_DONE);
167 if (desc == 0)
168 break;
169
170 /* @@@ Check whether entries come back in order. */
171 entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc);
172 channel = tx_desc[entry].channel;
173
174 if (channel < 0 || channel >= nds_count) {
175 printk(KERN_ERR "ixp2000: txcomp channel index "
176 "out of bounds (%d, %.8i, %d)\n",
177 channel, (unsigned int)desc, entry);
178 continue;
179 }
180
181 ip = netdev_priv(nds[channel]);
182 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
183 wake |= 1 << channel;
184 ip->tx_queue_entries--;
185 }
186
187 for (channel = 0; wake != 0; channel++) {
188 if (wake & (1 << channel)) {
189 netif_wake_queue(nds[channel]);
190 wake &= ~(1 << channel);
191 }
192 }
193}
194
David Howells7d12e782006-10-05 14:55:46 +0100195static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100196{
197 u32 status;
198
199 status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
200 if (status == 0)
201 return IRQ_NONE;
202
203 /*
204 * Any of the eight receive units signaled RX?
205 */
206 if (status & 0x00ff) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700207 struct net_device *dev = nds[0];
208 struct ixpdev_priv *ip = netdev_priv(dev);
209
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100210 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700211 if (likely(napi_schedule_prep(&ip->napi))) {
212 __netif_rx_schedule(dev, &ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100213 } else {
214 printk(KERN_CRIT "ixp2000: irq while polling!!\n");
215 }
216 }
217
218 /*
219 * Any of the eight transmit units signaled TXdone?
220 */
221 if (status & 0xff00) {
222 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
223 ixpdev_tx_complete();
224 }
225
226 return IRQ_HANDLED;
227}
228
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100229#ifdef CONFIG_NET_POLL_CONTROLLER
230static void ixpdev_poll_controller(struct net_device *dev)
231{
232 disable_irq(IRQ_IXP2000_THDA0);
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -0700233 ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100234 enable_irq(IRQ_IXP2000_THDA0);
235}
236#endif
237
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100238static int ixpdev_open(struct net_device *dev)
239{
240 struct ixpdev_priv *ip = netdev_priv(dev);
241 int err;
242
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700243 napi_enable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100244 if (!nds_open++) {
245 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700246 IRQF_SHARED, "ixp2000_eth", nds);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100247 if (err) {
248 nds_open--;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700249 napi_disable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100250 return err;
251 }
252
253 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
254 }
255
256 set_port_admin_status(ip->channel, 1);
257 netif_start_queue(dev);
258
259 return 0;
260}
261
262static int ixpdev_close(struct net_device *dev)
263{
264 struct ixpdev_priv *ip = netdev_priv(dev);
265
266 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700267 napi_disable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100268 set_port_admin_status(ip->channel, 0);
269
270 if (!--nds_open) {
271 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
272 free_irq(IRQ_IXP2000_THDA0, nds);
273 }
274
275 return 0;
276}
277
278struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
279{
280 struct net_device *dev;
281 struct ixpdev_priv *ip;
282
283 dev = alloc_etherdev(sizeof_priv);
284 if (dev == NULL)
285 return NULL;
286
287 dev->hard_start_xmit = ixpdev_xmit;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100288 dev->open = ixpdev_open;
289 dev->stop = ixpdev_close;
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100290#ifdef CONFIG_NET_POLL_CONTROLLER
291 dev->poll_controller = ixpdev_poll_controller;
292#endif
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100293
294 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100295
296 ip = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700297 ip->dev = dev;
298 netif_napi_add(dev, &ip->napi, ixpdev_poll, 64);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100299 ip->channel = channel;
300 ip->tx_queue_entries = 0;
301
302 return dev;
303}
304
305int ixpdev_init(int __nds_count, struct net_device **__nds,
306 void (*__set_port_admin_status)(int port, int up))
307{
308 int i;
309 int err;
310
Lennert Buytenhek5d4fe2c2006-03-29 15:12:44 +0200311 BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100312
Lennert Buytenhek12747782005-11-23 12:50:08 +0100313 printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
314
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100315 nds_count = __nds_count;
316 nds = __nds;
317 set_port_admin_status = __set_port_admin_status;
318
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100319 for (i = 0; i < RX_BUF_COUNT; i++) {
320 void *buf;
321
322 buf = (void *)get_zeroed_page(GFP_KERNEL);
323 if (buf == NULL) {
324 err = -ENOMEM;
325 while (--i >= 0)
326 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100327 goto err_out;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100328 }
329 rx_desc[i].buf_addr = virt_to_phys(buf);
330 rx_desc[i].buf_length = PAGE_SIZE;
331 }
332
333 /* @@@ Maybe we shouldn't be preallocating TX buffers. */
334 for (i = 0; i < TX_BUF_COUNT; i++) {
335 void *buf;
336
337 buf = (void *)get_zeroed_page(GFP_KERNEL);
338 if (buf == NULL) {
339 err = -ENOMEM;
340 while (--i >= 0)
341 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
342 goto err_free_rx;
343 }
344 tx_desc[i].buf_addr = virt_to_phys(buf);
345 }
346
347 /* 256 entries, ring status set means 'empty', base address 0x0000. */
348 ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
349 ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
350 ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
351
352 /* 256 entries, ring status set means 'full', base address 0x0400. */
353 ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
354 ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
355 ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
356
357 for (i = 0; i < RX_BUF_COUNT; i++) {
358 ixp2000_reg_write(RING_RX_PENDING,
359 RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
360 }
361
362 ixp2000_uengine_load(0, &ixp2400_rx);
363 ixp2000_uengine_start_contexts(0, 0xff);
364
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100365 /* 256 entries, ring status set means 'empty', base address 0x0800. */
366 ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
367 ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
368 ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
369
370 /* 256 entries, ring status set means 'full', base address 0x0c00. */
371 ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
372 ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
373 ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
374
375 ixp2000_uengine_load(1, &ixp2400_tx);
376 ixp2000_uengine_start_contexts(1, 0xff);
377
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100378 for (i = 0; i < nds_count; i++) {
379 err = register_netdev(nds[i]);
380 if (err) {
381 while (--i >= 0)
382 unregister_netdev(nds[i]);
383 goto err_free_tx;
384 }
385 }
386
Lennert Buytenhek7ed98bf2005-11-23 12:49:43 +0100387 for (i = 0; i < nds_count; i++) {
388 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
389 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
390 nds[i]->dev_addr[0], nds[i]->dev_addr[1],
391 nds[i]->dev_addr[2], nds[i]->dev_addr[3],
392 nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
393 }
394
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100395 return 0;
396
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100397err_free_tx:
398 for (i = 0; i < TX_BUF_COUNT; i++)
399 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
400
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100401err_free_rx:
402 for (i = 0; i < RX_BUF_COUNT; i++)
403 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
404
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100405err_out:
406 return err;
407}
408
409void ixpdev_deinit(void)
410{
411 int i;
412
413 /* @@@ Flush out pending packets. */
414
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100415 for (i = 0; i < nds_count; i++)
416 unregister_netdev(nds[i]);
417
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100418 ixp2000_uengine_stop_contexts(1, 0xff);
419 ixp2000_uengine_stop_contexts(0, 0xff);
420 ixp2000_uengine_reset(0x3);
421
422 for (i = 0; i < TX_BUF_COUNT; i++)
423 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
424
425 for (i = 0; i < RX_BUF_COUNT; i++)
426 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100427}