blob: 09f03f493beab1827295482f70a5138ddaeb0d81 [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
12#include <linux/config.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/init.h>
18#include <linux/moduleparam.h>
19#include <asm/arch/uengine.h>
20#include <asm/mach-types.h>
21#include <asm/io.h>
22#include "ixp2400_rx.ucode"
23#include "ixp2400_tx.ucode"
24#include "ixpdev_priv.h"
25#include "ixpdev.h"
26
Lennert Buytenhek12747782005-11-23 12:50:08 +010027#define DRV_MODULE_VERSION "0.2"
28
Lennert Buytenhek15d014d2005-11-11 18:23:13 +010029static int nds_count;
30static struct net_device **nds;
31static int nds_open;
32static void (*set_port_admin_status)(int port, int up);
33
34static struct ixpdev_rx_desc * const rx_desc =
35 (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
36static struct ixpdev_tx_desc * const tx_desc =
37 (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
38static int tx_pointer;
39
40
41static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev)
42{
43 struct ixpdev_priv *ip = netdev_priv(dev);
44 struct ixpdev_tx_desc *desc;
45 int entry;
46
47 if (unlikely(skb->len > PAGE_SIZE)) {
48 /* @@@ Count drops. */
49 dev_kfree_skb(skb);
50 return 0;
51 }
52
53 entry = tx_pointer;
54 tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT;
55
56 desc = tx_desc + entry;
57 desc->pkt_length = skb->len;
58 desc->channel = ip->channel;
59
60 skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr));
61 dev_kfree_skb(skb);
62
63 ixp2000_reg_write(RING_TX_PENDING,
64 TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc)));
65
66 dev->trans_start = jiffies;
67
68 local_irq_disable();
69 ip->tx_queue_entries++;
70 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
71 netif_stop_queue(dev);
72 local_irq_enable();
73
74 return 0;
75}
76
77
78static int ixpdev_rx(struct net_device *dev, int *budget)
79{
80 while (*budget > 0) {
81 struct ixpdev_rx_desc *desc;
82 struct sk_buff *skb;
83 void *buf;
84 u32 _desc;
85
86 _desc = ixp2000_reg_read(RING_RX_DONE);
87 if (_desc == 0)
88 return 0;
89
90 desc = rx_desc +
91 ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc));
92 buf = phys_to_virt(desc->buf_addr);
93
94 if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) {
95 printk(KERN_ERR "ixp2000: rx err, length %d\n",
96 desc->pkt_length);
97 goto err;
98 }
99
100 if (desc->channel < 0 || desc->channel >= nds_count) {
101 printk(KERN_ERR "ixp2000: rx err, channel %d\n",
102 desc->channel);
103 goto err;
104 }
105
106 /* @@@ Make FCS stripping configurable. */
107 desc->pkt_length -= 4;
108
109 if (unlikely(!netif_running(nds[desc->channel])))
110 goto err;
111
112 skb = dev_alloc_skb(desc->pkt_length + 2);
113 if (likely(skb != NULL)) {
114 skb->dev = nds[desc->channel];
115 skb_reserve(skb, 2);
116 eth_copy_and_sum(skb, buf, desc->pkt_length, 0);
117 skb_put(skb, desc->pkt_length);
118 skb->protocol = eth_type_trans(skb, skb->dev);
119
120 skb->dev->last_rx = jiffies;
121
122 netif_receive_skb(skb);
123 }
124
125err:
126 ixp2000_reg_write(RING_RX_PENDING, _desc);
127 dev->quota--;
128 (*budget)--;
129 }
130
131 return 1;
132}
133
134/* dev always points to nds[0]. */
135static int ixpdev_poll(struct net_device *dev, int *budget)
136{
137 /* @@@ Have to stop polling when nds[0] is administratively
138 * downed while we are polling. */
139 do {
140 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
141
142 if (ixpdev_rx(dev, budget))
143 return 1;
144 } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
145
146 netif_rx_complete(dev);
147 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
148
149 return 0;
150}
151
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100152static void ixpdev_tx_complete(void)
153{
154 int channel;
155 u32 wake;
156
157 wake = 0;
158 while (1) {
159 struct ixpdev_priv *ip;
160 u32 desc;
161 int entry;
162
163 desc = ixp2000_reg_read(RING_TX_DONE);
164 if (desc == 0)
165 break;
166
167 /* @@@ Check whether entries come back in order. */
168 entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc);
169 channel = tx_desc[entry].channel;
170
171 if (channel < 0 || channel >= nds_count) {
172 printk(KERN_ERR "ixp2000: txcomp channel index "
173 "out of bounds (%d, %.8i, %d)\n",
174 channel, (unsigned int)desc, entry);
175 continue;
176 }
177
178 ip = netdev_priv(nds[channel]);
179 if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
180 wake |= 1 << channel;
181 ip->tx_queue_entries--;
182 }
183
184 for (channel = 0; wake != 0; channel++) {
185 if (wake & (1 << channel)) {
186 netif_wake_queue(nds[channel]);
187 wake &= ~(1 << channel);
188 }
189 }
190}
191
192static irqreturn_t ixpdev_interrupt(int irq, void *dev_id, struct pt_regs *regs)
193{
194 u32 status;
195
196 status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
197 if (status == 0)
198 return IRQ_NONE;
199
200 /*
201 * Any of the eight receive units signaled RX?
202 */
203 if (status & 0x00ff) {
204 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
Stephen Hemmingerd359b6f2005-12-01 11:12:47 -0800205 if (likely(__netif_rx_schedule_prep(nds[0]))) {
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100206 __netif_rx_schedule(nds[0]);
207 } else {
208 printk(KERN_CRIT "ixp2000: irq while polling!!\n");
209 }
210 }
211
212 /*
213 * Any of the eight transmit units signaled TXdone?
214 */
215 if (status & 0xff00) {
216 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
217 ixpdev_tx_complete();
218 }
219
220 return IRQ_HANDLED;
221}
222
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100223#ifdef CONFIG_NET_POLL_CONTROLLER
224static void ixpdev_poll_controller(struct net_device *dev)
225{
226 disable_irq(IRQ_IXP2000_THDA0);
227 ixpdev_interrupt(IRQ_IXP2000_THDA0, dev, NULL);
228 enable_irq(IRQ_IXP2000_THDA0);
229}
230#endif
231
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100232static int ixpdev_open(struct net_device *dev)
233{
234 struct ixpdev_priv *ip = netdev_priv(dev);
235 int err;
236
237 if (!nds_open++) {
238 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
239 SA_SHIRQ, "ixp2000_eth", nds);
240 if (err) {
241 nds_open--;
242 return err;
243 }
244
245 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
246 }
247
248 set_port_admin_status(ip->channel, 1);
249 netif_start_queue(dev);
250
251 return 0;
252}
253
254static int ixpdev_close(struct net_device *dev)
255{
256 struct ixpdev_priv *ip = netdev_priv(dev);
257
258 netif_stop_queue(dev);
259 set_port_admin_status(ip->channel, 0);
260
261 if (!--nds_open) {
262 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
263 free_irq(IRQ_IXP2000_THDA0, nds);
264 }
265
266 return 0;
267}
268
269struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
270{
271 struct net_device *dev;
272 struct ixpdev_priv *ip;
273
274 dev = alloc_etherdev(sizeof_priv);
275 if (dev == NULL)
276 return NULL;
277
278 dev->hard_start_xmit = ixpdev_xmit;
279 dev->poll = ixpdev_poll;
280 dev->open = ixpdev_open;
281 dev->stop = ixpdev_close;
Lennert Buytenhek8ce51d62005-11-23 12:50:04 +0100282#ifdef CONFIG_NET_POLL_CONTROLLER
283 dev->poll_controller = ixpdev_poll_controller;
284#endif
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100285
286 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
287 dev->weight = 64;
288
289 ip = netdev_priv(dev);
290 ip->channel = channel;
291 ip->tx_queue_entries = 0;
292
293 return dev;
294}
295
296int ixpdev_init(int __nds_count, struct net_device **__nds,
297 void (*__set_port_admin_status)(int port, int up))
298{
299 int i;
300 int err;
301
302 if (RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192) {
303 static void __too_many_rx_or_tx_buffers(void);
304 __too_many_rx_or_tx_buffers();
305 }
306
Lennert Buytenhek12747782005-11-23 12:50:08 +0100307 printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
308
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100309 nds_count = __nds_count;
310 nds = __nds;
311 set_port_admin_status = __set_port_admin_status;
312
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100313 for (i = 0; i < RX_BUF_COUNT; i++) {
314 void *buf;
315
316 buf = (void *)get_zeroed_page(GFP_KERNEL);
317 if (buf == NULL) {
318 err = -ENOMEM;
319 while (--i >= 0)
320 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100321 goto err_out;
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100322 }
323 rx_desc[i].buf_addr = virt_to_phys(buf);
324 rx_desc[i].buf_length = PAGE_SIZE;
325 }
326
327 /* @@@ Maybe we shouldn't be preallocating TX buffers. */
328 for (i = 0; i < TX_BUF_COUNT; i++) {
329 void *buf;
330
331 buf = (void *)get_zeroed_page(GFP_KERNEL);
332 if (buf == NULL) {
333 err = -ENOMEM;
334 while (--i >= 0)
335 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
336 goto err_free_rx;
337 }
338 tx_desc[i].buf_addr = virt_to_phys(buf);
339 }
340
341 /* 256 entries, ring status set means 'empty', base address 0x0000. */
342 ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
343 ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
344 ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
345
346 /* 256 entries, ring status set means 'full', base address 0x0400. */
347 ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
348 ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
349 ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
350
351 for (i = 0; i < RX_BUF_COUNT; i++) {
352 ixp2000_reg_write(RING_RX_PENDING,
353 RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
354 }
355
356 ixp2000_uengine_load(0, &ixp2400_rx);
357 ixp2000_uengine_start_contexts(0, 0xff);
358
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100359 /* 256 entries, ring status set means 'empty', base address 0x0800. */
360 ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
361 ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
362 ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
363
364 /* 256 entries, ring status set means 'full', base address 0x0c00. */
365 ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
366 ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
367 ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
368
369 ixp2000_uengine_load(1, &ixp2400_tx);
370 ixp2000_uengine_start_contexts(1, 0xff);
371
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100372 for (i = 0; i < nds_count; i++) {
373 err = register_netdev(nds[i]);
374 if (err) {
375 while (--i >= 0)
376 unregister_netdev(nds[i]);
377 goto err_free_tx;
378 }
379 }
380
Lennert Buytenhek7ed98bf2005-11-23 12:49:43 +0100381 for (i = 0; i < nds_count; i++) {
382 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
383 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
384 nds[i]->dev_addr[0], nds[i]->dev_addr[1],
385 nds[i]->dev_addr[2], nds[i]->dev_addr[3],
386 nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
387 }
388
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100389 return 0;
390
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100391err_free_tx:
392 for (i = 0; i < TX_BUF_COUNT; i++)
393 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
394
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100395err_free_rx:
396 for (i = 0; i < RX_BUF_COUNT; i++)
397 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
398
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100399err_out:
400 return err;
401}
402
403void ixpdev_deinit(void)
404{
405 int i;
406
407 /* @@@ Flush out pending packets. */
408
Lennert Buytenhek0c499192005-11-23 12:49:05 +0100409 for (i = 0; i < nds_count; i++)
410 unregister_netdev(nds[i]);
411
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100412 ixp2000_uengine_stop_contexts(1, 0xff);
413 ixp2000_uengine_stop_contexts(0, 0xff);
414 ixp2000_uengine_reset(0x3);
415
416 for (i = 0; i < TX_BUF_COUNT; i++)
417 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
418
419 for (i = 0; i < RX_BUF_COUNT; i++)
420 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
Lennert Buytenhek15d014d2005-11-11 18:23:13 +0100421}