Merge master.kernel.org:/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[linux-2.6] / drivers / net / ixp2000 / ixpdev.c
1 /*
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/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"
25
26 #define DRV_MODULE_VERSION      "0.2"
27
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);
32
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;
38
39
40 static 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
77 static int ixpdev_rx(struct net_device *dev, int *budget)
78 {
79         while (*budget > 0) {
80                 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)) {
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]);
117
118                         skb->dev->last_rx = jiffies;
119
120                         netif_receive_skb(skb);
121                 }
122
123 err:
124                 ixp2000_reg_write(RING_RX_PENDING, _desc);
125                 dev->quota--;
126                 (*budget)--;
127         }
128
129         return 1;
130 }
131
132 /* dev always points to nds[0].  */
133 static int ixpdev_poll(struct net_device *dev, int *budget)
134 {
135         /* @@@ Have to stop polling when nds[0] is administratively
136          * downed while we are polling.  */
137         do {
138                 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
139
140                 if (ixpdev_rx(dev, budget))
141                         return 1;
142         } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
143
144         netif_rx_complete(dev);
145         ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
146
147         return 0;
148 }
149
150 static void ixpdev_tx_complete(void)
151 {
152         int channel;
153         u32 wake;
154
155         wake = 0;
156         while (1) {
157                 struct ixpdev_priv *ip;
158                 u32 desc;
159                 int entry;
160
161                 desc = ixp2000_reg_read(RING_TX_DONE);
162                 if (desc == 0)
163                         break;
164
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;
168
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);
173                         continue;
174                 }
175
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--;
180         }
181
182         for (channel = 0; wake != 0; channel++) {
183                 if (wake & (1 << channel)) {
184                         netif_wake_queue(nds[channel]);
185                         wake &= ~(1 << channel);
186                 }
187         }
188 }
189
190 static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
191 {
192         u32 status;
193
194         status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0);
195         if (status == 0)
196                 return IRQ_NONE;
197
198         /*
199          * Any of the eight receive units signaled RX?
200          */
201         if (status & 0x00ff) {
202                 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
203                 if (likely(__netif_rx_schedule_prep(nds[0]))) {
204                         __netif_rx_schedule(nds[0]);
205                 } else {
206                         printk(KERN_CRIT "ixp2000: irq while polling!!\n");
207                 }
208         }
209
210         /*
211          * Any of the eight transmit units signaled TXdone?
212          */
213         if (status & 0xff00) {
214                 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
215                 ixpdev_tx_complete();
216         }
217
218         return IRQ_HANDLED;
219 }
220
221 #ifdef CONFIG_NET_POLL_CONTROLLER
222 static void ixpdev_poll_controller(struct net_device *dev)
223 {
224         disable_irq(IRQ_IXP2000_THDA0);
225         ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
226         enable_irq(IRQ_IXP2000_THDA0);
227 }
228 #endif
229
230 static int ixpdev_open(struct net_device *dev)
231 {
232         struct ixpdev_priv *ip = netdev_priv(dev);
233         int err;
234
235         if (!nds_open++) {
236                 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
237                                         IRQF_SHARED, "ixp2000_eth", nds);
238                 if (err) {
239                         nds_open--;
240                         return err;
241                 }
242
243                 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
244         }
245
246         set_port_admin_status(ip->channel, 1);
247         netif_start_queue(dev);
248
249         return 0;
250 }
251
252 static int ixpdev_close(struct net_device *dev)
253 {
254         struct ixpdev_priv *ip = netdev_priv(dev);
255
256         netif_stop_queue(dev);
257         set_port_admin_status(ip->channel, 0);
258
259         if (!--nds_open) {
260                 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
261                 free_irq(IRQ_IXP2000_THDA0, nds);
262         }
263
264         return 0;
265 }
266
267 struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
268 {
269         struct net_device *dev;
270         struct ixpdev_priv *ip;
271
272         dev = alloc_etherdev(sizeof_priv);
273         if (dev == NULL)
274                 return NULL;
275
276         dev->hard_start_xmit = ixpdev_xmit;
277         dev->poll = ixpdev_poll;
278         dev->open = ixpdev_open;
279         dev->stop = ixpdev_close;
280 #ifdef CONFIG_NET_POLL_CONTROLLER
281         dev->poll_controller = ixpdev_poll_controller;
282 #endif
283
284         dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
285         dev->weight = 64;
286
287         ip = netdev_priv(dev);
288         ip->channel = channel;
289         ip->tx_queue_entries = 0;
290
291         return dev;
292 }
293
294 int ixpdev_init(int __nds_count, struct net_device **__nds,
295                 void (*__set_port_admin_status)(int port, int up))
296 {
297         int i;
298         int err;
299
300         BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
301
302         printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
303
304         nds_count = __nds_count;
305         nds = __nds;
306         set_port_admin_status = __set_port_admin_status;
307
308         for (i = 0; i < RX_BUF_COUNT; i++) {
309                 void *buf;
310
311                 buf = (void *)get_zeroed_page(GFP_KERNEL);
312                 if (buf == NULL) {
313                         err = -ENOMEM;
314                         while (--i >= 0)
315                                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
316                         goto err_out;
317                 }
318                 rx_desc[i].buf_addr = virt_to_phys(buf);
319                 rx_desc[i].buf_length = PAGE_SIZE;
320         }
321
322         /* @@@ Maybe we shouldn't be preallocating TX buffers.  */
323         for (i = 0; i < TX_BUF_COUNT; i++) {
324                 void *buf;
325
326                 buf = (void *)get_zeroed_page(GFP_KERNEL);
327                 if (buf == NULL) {
328                         err = -ENOMEM;
329                         while (--i >= 0)
330                                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
331                         goto err_free_rx;
332                 }
333                 tx_desc[i].buf_addr = virt_to_phys(buf);
334         }
335
336         /* 256 entries, ring status set means 'empty', base address 0x0000.  */
337         ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
338         ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
339         ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
340
341         /* 256 entries, ring status set means 'full', base address 0x0400.  */
342         ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
343         ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
344         ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
345
346         for (i = 0; i < RX_BUF_COUNT; i++) {
347                 ixp2000_reg_write(RING_RX_PENDING,
348                         RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
349         }
350
351         ixp2000_uengine_load(0, &ixp2400_rx);
352         ixp2000_uengine_start_contexts(0, 0xff);
353
354         /* 256 entries, ring status set means 'empty', base address 0x0800.  */
355         ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
356         ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
357         ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
358
359         /* 256 entries, ring status set means 'full', base address 0x0c00.  */
360         ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
361         ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
362         ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
363
364         ixp2000_uengine_load(1, &ixp2400_tx);
365         ixp2000_uengine_start_contexts(1, 0xff);
366
367         for (i = 0; i < nds_count; i++) {
368                 err = register_netdev(nds[i]);
369                 if (err) {
370                         while (--i >= 0)
371                                 unregister_netdev(nds[i]);
372                         goto err_free_tx;
373                 }
374         }
375
376         for (i = 0; i < nds_count; i++) {
377                 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
378                         "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
379                         nds[i]->dev_addr[0], nds[i]->dev_addr[1],
380                         nds[i]->dev_addr[2], nds[i]->dev_addr[3],
381                         nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
382         }
383
384         return 0;
385
386 err_free_tx:
387         for (i = 0; i < TX_BUF_COUNT; i++)
388                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
389
390 err_free_rx:
391         for (i = 0; i < RX_BUF_COUNT; i++)
392                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
393
394 err_out:
395         return err;
396
397
398 void ixpdev_deinit(void)
399 {
400         int i;
401
402         /* @@@ Flush out pending packets.  */
403
404         for (i = 0; i < nds_count; i++)
405                 unregister_netdev(nds[i]);
406
407         ixp2000_uengine_stop_contexts(1, 0xff);
408         ixp2000_uengine_stop_contexts(0, 0xff);
409         ixp2000_uengine_reset(0x3);
410
411         for (i = 0; i < TX_BUF_COUNT; i++)
412                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
413
414         for (i = 0; i < RX_BUF_COUNT; i++)
415                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
416 }