Merge commit 'v2.6.27-rc3' into x86/xsave
[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/io.h>
20 #include "ixp2400_rx.ucode"
21 #include "ixp2400_tx.ucode"
22 #include "ixpdev_priv.h"
23 #include "ixpdev.h"
24
25 #define DRV_MODULE_VERSION      "0.2"
26
27 static int nds_count;
28 static struct net_device **nds;
29 static int nds_open;
30 static void (*set_port_admin_status)(int port, int up);
31
32 static struct ixpdev_rx_desc * const rx_desc =
33         (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE);
34 static struct ixpdev_tx_desc * const tx_desc =
35         (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE);
36 static int tx_pointer;
37
38
39 static 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;
44
45         if (unlikely(skb->len > PAGE_SIZE)) {
46                 /* @@@ Count drops.  */
47                 dev_kfree_skb(skb);
48                 return 0;
49         }
50
51         entry = tx_pointer;
52         tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT;
53
54         desc = tx_desc + entry;
55         desc->pkt_length = skb->len;
56         desc->channel = ip->channel;
57
58         skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr));
59         dev_kfree_skb(skb);
60
61         ixp2000_reg_write(RING_TX_PENDING,
62                 TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc)));
63
64         dev->trans_start = jiffies;
65
66         local_irq_disable();
67         ip->tx_queue_entries++;
68         if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN)
69                 netif_stop_queue(dev);
70         local_irq_enable();
71
72         return 0;
73 }
74
75
76 static int ixpdev_rx(struct net_device *dev, int processed, int budget)
77 {
78         while (processed < budget) {
79                 struct ixpdev_rx_desc *desc;
80                 struct sk_buff *skb;
81                 void *buf;
82                 u32 _desc;
83
84                 _desc = ixp2000_reg_read(RING_RX_DONE);
85                 if (_desc == 0)
86                         return 0;
87
88                 desc = rx_desc +
89                         ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc));
90                 buf = phys_to_virt(desc->buf_addr);
91
92                 if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) {
93                         printk(KERN_ERR "ixp2000: rx err, length %d\n",
94                                         desc->pkt_length);
95                         goto err;
96                 }
97
98                 if (desc->channel < 0 || desc->channel >= nds_count) {
99                         printk(KERN_ERR "ixp2000: rx err, channel %d\n",
100                                         desc->channel);
101                         goto err;
102                 }
103
104                 /* @@@ Make FCS stripping configurable.  */
105                 desc->pkt_length -= 4;
106
107                 if (unlikely(!netif_running(nds[desc->channel])))
108                         goto err;
109
110                 skb = netdev_alloc_skb(dev, desc->pkt_length + 2);
111                 if (likely(skb != NULL)) {
112                         skb_reserve(skb, 2);
113                         skb_copy_to_linear_data(skb, buf, desc->pkt_length);
114                         skb_put(skb, desc->pkt_length);
115                         skb->protocol = eth_type_trans(skb, nds[desc->channel]);
116
117                         dev->last_rx = jiffies;
118
119                         netif_receive_skb(skb);
120                 }
121
122 err:
123                 ixp2000_reg_write(RING_RX_PENDING, _desc);
124                 processed++;
125         }
126
127         return processed;
128 }
129
130 /* dev always points to nds[0].  */
131 static int ixpdev_poll(struct napi_struct *napi, int budget)
132 {
133         struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi);
134         struct net_device *dev = ip->dev;
135         int rx;
136
137         rx = 0;
138         do {
139                 ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff);
140
141                 rx = ixpdev_rx(dev, rx, budget);
142                 if (rx >= budget)
143                         break;
144         } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff);
145
146         netif_rx_complete(dev, napi);
147         ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff);
148
149         return rx;
150 }
151
152 static 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
192 static irqreturn_t ixpdev_interrupt(int irq, void *dev_id)
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                 struct net_device *dev = nds[0];
205                 struct ixpdev_priv *ip = netdev_priv(dev);
206
207                 ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff);
208                 if (likely(napi_schedule_prep(&ip->napi))) {
209                         __netif_rx_schedule(dev, &ip->napi);
210                 } else {
211                         printk(KERN_CRIT "ixp2000: irq while polling!!\n");
212                 }
213         }
214
215         /*
216          * Any of the eight transmit units signaled TXdone?
217          */
218         if (status & 0xff00) {
219                 ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00);
220                 ixpdev_tx_complete();
221         }
222
223         return IRQ_HANDLED;
224 }
225
226 #ifdef CONFIG_NET_POLL_CONTROLLER
227 static void ixpdev_poll_controller(struct net_device *dev)
228 {
229         disable_irq(IRQ_IXP2000_THDA0);
230         ixpdev_interrupt(IRQ_IXP2000_THDA0, dev);
231         enable_irq(IRQ_IXP2000_THDA0);
232 }
233 #endif
234
235 static int ixpdev_open(struct net_device *dev)
236 {
237         struct ixpdev_priv *ip = netdev_priv(dev);
238         int err;
239
240         napi_enable(&ip->napi);
241         if (!nds_open++) {
242                 err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt,
243                                         IRQF_SHARED, "ixp2000_eth", nds);
244                 if (err) {
245                         nds_open--;
246                         napi_disable(&ip->napi);
247                         return err;
248                 }
249
250                 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff);
251         }
252
253         set_port_admin_status(ip->channel, 1);
254         netif_start_queue(dev);
255
256         return 0;
257 }
258
259 static int ixpdev_close(struct net_device *dev)
260 {
261         struct ixpdev_priv *ip = netdev_priv(dev);
262
263         netif_stop_queue(dev);
264         napi_disable(&ip->napi);
265         set_port_admin_status(ip->channel, 0);
266
267         if (!--nds_open) {
268                 ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff);
269                 free_irq(IRQ_IXP2000_THDA0, nds);
270         }
271
272         return 0;
273 }
274
275 struct net_device *ixpdev_alloc(int channel, int sizeof_priv)
276 {
277         struct net_device *dev;
278         struct ixpdev_priv *ip;
279
280         dev = alloc_etherdev(sizeof_priv);
281         if (dev == NULL)
282                 return NULL;
283
284         dev->hard_start_xmit = ixpdev_xmit;
285         dev->open = ixpdev_open;
286         dev->stop = ixpdev_close;
287 #ifdef CONFIG_NET_POLL_CONTROLLER
288         dev->poll_controller = ixpdev_poll_controller;
289 #endif
290
291         dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
292
293         ip = netdev_priv(dev);
294         ip->dev = dev;
295         netif_napi_add(dev, &ip->napi, ixpdev_poll, 64);
296         ip->channel = channel;
297         ip->tx_queue_entries = 0;
298
299         return dev;
300 }
301
302 int ixpdev_init(int __nds_count, struct net_device **__nds,
303                 void (*__set_port_admin_status)(int port, int up))
304 {
305         int i;
306         int err;
307
308         BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192);
309
310         printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION);
311
312         nds_count = __nds_count;
313         nds = __nds;
314         set_port_admin_status = __set_port_admin_status;
315
316         for (i = 0; i < RX_BUF_COUNT; i++) {
317                 void *buf;
318
319                 buf = (void *)get_zeroed_page(GFP_KERNEL);
320                 if (buf == NULL) {
321                         err = -ENOMEM;
322                         while (--i >= 0)
323                                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
324                         goto err_out;
325                 }
326                 rx_desc[i].buf_addr = virt_to_phys(buf);
327                 rx_desc[i].buf_length = PAGE_SIZE;
328         }
329
330         /* @@@ Maybe we shouldn't be preallocating TX buffers.  */
331         for (i = 0; i < TX_BUF_COUNT; i++) {
332                 void *buf;
333
334                 buf = (void *)get_zeroed_page(GFP_KERNEL);
335                 if (buf == NULL) {
336                         err = -ENOMEM;
337                         while (--i >= 0)
338                                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
339                         goto err_free_rx;
340                 }
341                 tx_desc[i].buf_addr = virt_to_phys(buf);
342         }
343
344         /* 256 entries, ring status set means 'empty', base address 0x0000.  */
345         ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000);
346         ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000);
347         ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000);
348
349         /* 256 entries, ring status set means 'full', base address 0x0400.  */
350         ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400);
351         ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000);
352         ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000);
353
354         for (i = 0; i < RX_BUF_COUNT; i++) {
355                 ixp2000_reg_write(RING_RX_PENDING,
356                         RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc)));
357         }
358
359         ixp2000_uengine_load(0, &ixp2400_rx);
360         ixp2000_uengine_start_contexts(0, 0xff);
361
362         /* 256 entries, ring status set means 'empty', base address 0x0800.  */
363         ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800);
364         ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000);
365         ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000);
366
367         /* 256 entries, ring status set means 'full', base address 0x0c00.  */
368         ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00);
369         ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000);
370         ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000);
371
372         ixp2000_uengine_load(1, &ixp2400_tx);
373         ixp2000_uengine_start_contexts(1, 0xff);
374
375         for (i = 0; i < nds_count; i++) {
376                 err = register_netdev(nds[i]);
377                 if (err) {
378                         while (--i >= 0)
379                                 unregister_netdev(nds[i]);
380                         goto err_free_tx;
381                 }
382         }
383
384         for (i = 0; i < nds_count; i++) {
385                 printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), "
386                         "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i,
387                         nds[i]->dev_addr[0], nds[i]->dev_addr[1],
388                         nds[i]->dev_addr[2], nds[i]->dev_addr[3],
389                         nds[i]->dev_addr[4], nds[i]->dev_addr[5]);
390         }
391
392         return 0;
393
394 err_free_tx:
395         for (i = 0; i < TX_BUF_COUNT; i++)
396                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
397
398 err_free_rx:
399         for (i = 0; i < RX_BUF_COUNT; i++)
400                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
401
402 err_out:
403         return err;
404
405
406 void ixpdev_deinit(void)
407 {
408         int i;
409
410         /* @@@ Flush out pending packets.  */
411
412         for (i = 0; i < nds_count; i++)
413                 unregister_netdev(nds[i]);
414
415         ixp2000_uengine_stop_contexts(1, 0xff);
416         ixp2000_uengine_stop_contexts(0, 0xff);
417         ixp2000_uengine_reset(0x3);
418
419         for (i = 0; i < TX_BUF_COUNT; i++)
420                 free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr));
421
422         for (i = 0; i < RX_BUF_COUNT; i++)
423                 free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr));
424 }