blob: 92fb8235c76689c2ea3b6f1c079a65536bd035a1 [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/io.h>
20#include "ixp2400_rx.ucode"
21#include "ixp2400_tx.ucode"
22#include "ixpdev_priv.h"
23#include "ixpdev.h"
24
Lennert Buytenhek12747782005-11-23 12:50:08 +010025#define DRV_MODULE_VERSION "0.2"
26
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010027static int nds_count;
28static struct net_device **nds;
29static int nds_open;
30static void (*set_port_admin_status)(int port, int up);
31
32static struct ixpdev_rx_desc * const rx_desc =
33 (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
34static struct ixpdev_tx_desc * const tx_desc =
35 (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
36static int tx_pointer;
37
38
39static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev)
40{
41 struct ixpdev_priv *ip = netdev_priv(dev);
42 struct ixpdev_tx_desc *desc;
43 int entry;
Dongdong Deng48719532009-08-23 19:49:07 -070044 unsigned long flags;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010045
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
Dongdong Deng48719532009-08-23 19:49:07 -070067 local_irq_save(flags);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010068 ip->tx_queue_entries++;
69 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
70 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -070071 local_irq_restore(flags);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010072
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
Stephen Hemmingerd27e7c32008-04-16 16:37:37 -0700111 skb = netdev_alloc_skb(dev, desc->pkt_length + 2);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100112 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
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100118 netif_receive_skb(skb);
119 }
120
121err:
122 ixp2000_reg_write(RING_RX_PENDING, _desc);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700123 processed++;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100124 }
125
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700126 return processed;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100127}
128
129/* dev always points to nds[0]. */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700130static int ixpdev_poll(struct napi_struct *napi, int budget)
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100131{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700132 struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi);
133 struct net_device *dev = ip->dev;
134 int rx;
135
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700136 rx = 0;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100137 do {
138 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
139
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700140 rx = ixpdev_rx(dev, rx, budget);
141 if (rx >= budget)
142 break;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100143 } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
144
Ben Hutchings288379f2009-01-19 16:43:59 -0800145 napi_complete(napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100146 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
147
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700148 return rx;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100149}
150
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100151static void ixpdev_tx_complete(void)
152{
153 int channel;
154 u32 wake;
155
156 wake = 0;
157 while (1) {
158 struct ixpdev_priv *ip;
159 u32 desc;
160 int entry;
161
162 desc = ixp2000_reg_read(RING_TX_DONE);
163 if (desc == 0)
164 break;
165
166 /* @@@ Check whether entries come back in order. */
167 entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc);
168 channel = tx_desc[entry].channel;
169
170 if (channel < 0 || channel >= nds_count) {
171 printk(KERN_ERR "ixp2000: txcomp channel index "
172 "out of bounds (%d, %.8i, %d)\n",
173 channel, (unsigned int)desc, entry);
174 continue;
175 }
176
177 ip = netdev_priv(nds[channel]);
178 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
179 wake |= 1 << channel;
180 ip->tx_queue_entries--;
181 }
182
183 for (channel = 0; wake != 0; channel++) {
184 if (wake & (1 << channel)) {
185 netif_wake_queue(nds[channel]);
186 wake &= ~(1 << channel);
187 }
188 }
189}
190
David Howells7d12e782006-10-05 14:55:46 +0100191static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100192{
193 u32 status;
194
195 status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
196 if (status == 0)
197 return IRQ_NONE;
198
199 /*
200 * Any of the eight receive units signaled RX?
201 */
202 if (status & 0x00ff) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700203 struct net_device *dev = nds[0];
204 struct ixpdev_priv *ip = netdev_priv(dev);
205
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100206 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700207 if (likely(napi_schedule_prep(&ip->napi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800208 __napi_schedule(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100209 } else {
210 printk(KERN_CRIT "ixp2000: irq while polling!!\n");
211 }
212 }
213
214 /*
215 * Any of the eight transmit units signaled TXdone?
216 */
217 if (status & 0xff00) {
218 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
219 ixpdev_tx_complete();
220 }
221
222 return IRQ_HANDLED;
223}
224
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100225#ifdef CONFIG_NET_POLL_CONTROLLER
226static void ixpdev_poll_controller(struct net_device *dev)
227{
228 disable_irq(IRQ_IXP2000_THDA0);
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -0700229 ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100230 enable_irq(IRQ_IXP2000_THDA0);
231}
232#endif
233
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100234static int ixpdev_open(struct net_device *dev)
235{
236 struct ixpdev_priv *ip = netdev_priv(dev);
237 int err;
238
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700239 napi_enable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100240 if (!nds_open++) {
241 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700242 IRQF_SHARED, "ixp2000_eth", nds);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100243 if (err) {
244 nds_open--;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700245 napi_disable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100246 return err;
247 }
248
249 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
250 }
251
252 set_port_admin_status(ip->channel, 1);
253 netif_start_queue(dev);
254
255 return 0;
256}
257
258static int ixpdev_close(struct net_device *dev)
259{
260 struct ixpdev_priv *ip = netdev_priv(dev);
261
262 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700263 napi_disable(&ip->napi);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100264 set_port_admin_status(ip->channel, 0);
265
266 if (!--nds_open) {
267 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
268 free_irq(IRQ_IXP2000_THDA0, nds);
269 }
270
271 return 0;
272}
273
Alexander Beregalove7090e32009-04-15 12:52:44 +0000274static const struct net_device_ops ixpdev_netdev_ops = {
275 .ndo_open = ixpdev_open,
276 .ndo_stop = ixpdev_close,
277 .ndo_start_xmit = ixpdev_xmit,
278 .ndo_change_mtu = eth_change_mtu,
279 .ndo_validate_addr = eth_validate_addr,
280 .ndo_set_mac_address = eth_mac_addr,
281#ifdef CONFIG_NET_POLL_CONTROLLER
282 .ndo_poll_controller = ixpdev_poll_controller,
283#endif
284};
285
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100286struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
287{
288 struct net_device *dev;
289 struct ixpdev_priv *ip;
290
291 dev = alloc_etherdev(sizeof_priv);
292 if (dev == NULL)
293 return NULL;
294
Alexander Beregalove7090e32009-04-15 12:52:44 +0000295 dev->netdev_ops = &ixpdev_netdev_ops;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100296
297 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100298
299 ip = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700300 ip->dev = dev;
301 netif_napi_add(dev, &ip->napi, ixpdev_poll, 64);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100302 ip->channel = channel;
303 ip->tx_queue_entries = 0;
304
305 return dev;
306}
307
308int ixpdev_init(int __nds_count, struct net_device **__nds,
309 void (*__set_port_admin_status)(int port, int up))
310{
311 int i;
312 int err;
313
Lennert Buytenhek5d4fe2c2006-03-29 15:12:44 +0200314 BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100315
Lennert Buytenhek12747782005-11-23 12:50:08 +0100316 printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
317
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100318 nds_count = __nds_count;
319 nds = __nds;
320 set_port_admin_status = __set_port_admin_status;
321
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100322 for (i = 0; i < RX_BUF_COUNT; i++) {
323 void *buf;
324
325 buf = (void *)get_zeroed_page(GFP_KERNEL);
326 if (buf == NULL) {
327 err = -ENOMEM;
328 while (--i >= 0)
329 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100330 goto err_out;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100331 }
332 rx_desc[i].buf_addr = virt_to_phys(buf);
333 rx_desc[i].buf_length = PAGE_SIZE;
334 }
335
336 /* @@@ Maybe we shouldn't be preallocating TX buffers. */
337 for (i = 0; i < TX_BUF_COUNT; i++) {
338 void *buf;
339
340 buf = (void *)get_zeroed_page(GFP_KERNEL);
341 if (buf == NULL) {
342 err = -ENOMEM;
343 while (--i >= 0)
344 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
345 goto err_free_rx;
346 }
347 tx_desc[i].buf_addr = virt_to_phys(buf);
348 }
349
350 /* 256 entries, ring status set means 'empty', base address 0x0000. */
351 ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
352 ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
353 ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
354
355 /* 256 entries, ring status set means 'full', base address 0x0400. */
356 ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
357 ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
358 ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
359
360 for (i = 0; i < RX_BUF_COUNT; i++) {
361 ixp2000_reg_write(RING_RX_PENDING,
362 RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
363 }
364
365 ixp2000_uengine_load(0, &ixp2400_rx);
366 ixp2000_uengine_start_contexts(0, 0xff);
367
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100368 /* 256 entries, ring status set means 'empty', base address 0x0800. */
369 ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
370 ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
371 ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
372
373 /* 256 entries, ring status set means 'full', base address 0x0c00. */
374 ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
375 ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
376 ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
377
378 ixp2000_uengine_load(1, &ixp2400_tx);
379 ixp2000_uengine_start_contexts(1, 0xff);
380
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100381 for (i = 0; i < nds_count; i++) {
382 err = register_netdev(nds[i]);
383 if (err) {
384 while (--i >= 0)
385 unregister_netdev(nds[i]);
386 goto err_free_tx;
387 }
388 }
389
Lennert Buytenhek7ed98bf2005-11-23 12:49:43 +0100390 for (i = 0; i < nds_count; i++) {
391 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
392 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
393 nds[i]->dev_addr[0], nds[i]->dev_addr[1],
394 nds[i]->dev_addr[2], nds[i]->dev_addr[3],
395 nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
396 }
397
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100398 return 0;
399
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100400err_free_tx:
401 for (i = 0; i < TX_BUF_COUNT; i++)
402 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
403
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100404err_free_rx:
405 for (i = 0; i < RX_BUF_COUNT; i++)
406 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
407
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100408err_out:
409 return err;
410}
411
412void ixpdev_deinit(void)
413{
414 int i;
415
416 /* @@@ Flush out pending packets. */
417
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100418 for (i = 0; i < nds_count; i++)
419 unregister_netdev(nds[i]);
420
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100421 ixp2000_uengine_stop_contexts(1, 0xff);
422 ixp2000_uengine_stop_contexts(0, 0xff);
423 ixp2000_uengine_reset(0x3);
424
425 for (i = 0; i < TX_BUF_COUNT; i++)
426 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
427
428 for (i = 0; i < RX_BUF_COUNT; i++)
429 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100430}