Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / drivers / net / rionet.c
1 /*
2  * rionet - Ethernet driver over RapidIO messaging services
3  *
4  * Copyright 2005 MontaVista Software, Inc.
5  * Matt Porter <mporter@kernel.crashing.org>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/delay.h>
17 #include <linux/rio.h>
18 #include <linux/rio_drv.h>
19 #include <linux/rio_ids.h>
20
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/skbuff.h>
24 #include <linux/crc32.h>
25 #include <linux/ethtool.h>
26
27 #define DRV_NAME        "rionet"
28 #define DRV_VERSION     "0.2"
29 #define DRV_AUTHOR      "Matt Porter <mporter@kernel.crashing.org>"
30 #define DRV_DESC        "Ethernet over RapidIO"
31
32 MODULE_AUTHOR(DRV_AUTHOR);
33 MODULE_DESCRIPTION(DRV_DESC);
34 MODULE_LICENSE("GPL");
35
36 #define RIONET_DEFAULT_MSGLEVEL \
37                         (NETIF_MSG_DRV          | \
38                          NETIF_MSG_LINK         | \
39                          NETIF_MSG_RX_ERR       | \
40                          NETIF_MSG_TX_ERR)
41
42 #define RIONET_DOORBELL_JOIN    0x1000
43 #define RIONET_DOORBELL_LEAVE   0x1001
44
45 #define RIONET_MAILBOX          0
46
47 #define RIONET_TX_RING_SIZE     CONFIG_RIONET_TX_SIZE
48 #define RIONET_RX_RING_SIZE     CONFIG_RIONET_RX_SIZE
49
50 static LIST_HEAD(rionet_peers);
51
52 struct rionet_private {
53         struct rio_mport *mport;
54         struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
55         struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
56         struct net_device_stats stats;
57         int rx_slot;
58         int tx_slot;
59         int tx_cnt;
60         int ack_slot;
61         spinlock_t lock;
62         spinlock_t tx_lock;
63         u32 msg_enable;
64 };
65
66 struct rionet_peer {
67         struct list_head node;
68         struct rio_dev *rdev;
69         struct resource *res;
70 };
71
72 static int rionet_check = 0;
73 static int rionet_capable = 1;
74
75 /*
76  * This is a fast lookup table for for translating TX
77  * Ethernet packets into a destination RIO device. It
78  * could be made into a hash table to save memory depending
79  * on system trade-offs.
80  */
81 static struct rio_dev *rionet_active[RIO_MAX_ROUTE_ENTRIES];
82
83 #define is_rionet_capable(pef, src_ops, dst_ops)                \
84                         ((pef & RIO_PEF_INB_MBOX) &&            \
85                          (pef & RIO_PEF_INB_DOORBELL) &&        \
86                          (src_ops & RIO_SRC_OPS_DOORBELL) &&    \
87                          (dst_ops & RIO_DST_OPS_DOORBELL))
88 #define dev_rionet_capable(dev) \
89         is_rionet_capable(dev->pef, dev->src_ops, dev->dst_ops)
90
91 #define RIONET_MAC_MATCH(x)     (*(u32 *)x == 0x00010001)
92 #define RIONET_GET_DESTID(x)    (*(u16 *)(x + 4))
93
94 static struct net_device_stats *rionet_stats(struct net_device *ndev)
95 {
96         struct rionet_private *rnet = ndev->priv;
97         return &rnet->stats;
98 }
99
100 static int rionet_rx_clean(struct net_device *ndev)
101 {
102         int i;
103         int error = 0;
104         struct rionet_private *rnet = ndev->priv;
105         void *data;
106
107         i = rnet->rx_slot;
108
109         do {
110                 if (!rnet->rx_skb[i])
111                         continue;
112
113                 if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
114                         break;
115
116                 rnet->rx_skb[i]->data = data;
117                 skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
118                 rnet->rx_skb[i]->protocol =
119                     eth_type_trans(rnet->rx_skb[i], ndev);
120                 error = netif_rx(rnet->rx_skb[i]);
121
122                 if (error == NET_RX_DROP) {
123                         rnet->stats.rx_dropped++;
124                 } else if (error == NET_RX_BAD) {
125                         if (netif_msg_rx_err(rnet))
126                                 printk(KERN_WARNING "%s: bad rx packet\n",
127                                        DRV_NAME);
128                         rnet->stats.rx_errors++;
129                 } else {
130                         rnet->stats.rx_packets++;
131                         rnet->stats.rx_bytes += RIO_MAX_MSG_SIZE;
132                 }
133
134         } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
135
136         return i;
137 }
138
139 static void rionet_rx_fill(struct net_device *ndev, int end)
140 {
141         int i;
142         struct rionet_private *rnet = ndev->priv;
143
144         i = rnet->rx_slot;
145         do {
146                 rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
147
148                 if (!rnet->rx_skb[i])
149                         break;
150
151                 rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
152                                    rnet->rx_skb[i]->data);
153         } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
154
155         rnet->rx_slot = i;
156 }
157
158 static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
159                                struct rio_dev *rdev)
160 {
161         struct rionet_private *rnet = ndev->priv;
162
163         rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
164         rnet->tx_skb[rnet->tx_slot] = skb;
165
166         rnet->stats.tx_packets++;
167         rnet->stats.tx_bytes += skb->len;
168
169         if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
170                 netif_stop_queue(ndev);
171
172         ++rnet->tx_slot;
173         rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
174
175         if (netif_msg_tx_queued(rnet))
176                 printk(KERN_INFO "%s: queued skb %8.8x len %8.8x\n", DRV_NAME,
177                        (u32) skb, skb->len);
178
179         return 0;
180 }
181
182 static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
183 {
184         int i;
185         struct rionet_private *rnet = ndev->priv;
186         struct ethhdr *eth = (struct ethhdr *)skb->data;
187         u16 destid;
188         unsigned long flags;
189
190         local_irq_save(flags);
191         if (!spin_trylock(&rnet->tx_lock)) {
192                 local_irq_restore(flags);
193                 return NETDEV_TX_LOCKED;
194         }
195
196         if ((rnet->tx_cnt + 1) > RIONET_TX_RING_SIZE) {
197                 netif_stop_queue(ndev);
198                 spin_unlock_irqrestore(&rnet->tx_lock, flags);
199                 printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
200                        ndev->name);
201                 return NETDEV_TX_BUSY;
202         }
203
204         if (eth->h_dest[0] & 0x01) {
205                 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES; i++)
206                         if (rionet_active[i])
207                                 rionet_queue_tx_msg(skb, ndev,
208                                                     rionet_active[i]);
209         } else if (RIONET_MAC_MATCH(eth->h_dest)) {
210                 destid = RIONET_GET_DESTID(eth->h_dest);
211                 if (rionet_active[destid])
212                         rionet_queue_tx_msg(skb, ndev, rionet_active[destid]);
213         }
214
215         spin_unlock_irqrestore(&rnet->tx_lock, flags);
216
217         return 0;
218 }
219
220 static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
221                                u16 info)
222 {
223         struct net_device *ndev = dev_id;
224         struct rionet_private *rnet = ndev->priv;
225         struct rionet_peer *peer;
226
227         if (netif_msg_intr(rnet))
228                 printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
229                        DRV_NAME, sid, tid, info);
230         if (info == RIONET_DOORBELL_JOIN) {
231                 if (!rionet_active[sid]) {
232                         list_for_each_entry(peer, &rionet_peers, node) {
233                                 if (peer->rdev->destid == sid)
234                                         rionet_active[sid] = peer->rdev;
235                         }
236                         rio_mport_send_doorbell(mport, sid,
237                                                 RIONET_DOORBELL_JOIN);
238                 }
239         } else if (info == RIONET_DOORBELL_LEAVE) {
240                 rionet_active[sid] = NULL;
241         } else {
242                 if (netif_msg_intr(rnet))
243                         printk(KERN_WARNING "%s: unhandled doorbell\n",
244                                DRV_NAME);
245         }
246 }
247
248 static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
249 {
250         int n;
251         struct net_device *ndev = dev_id;
252         struct rionet_private *rnet = (struct rionet_private *)ndev->priv;
253
254         if (netif_msg_intr(rnet))
255                 printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
256                        DRV_NAME, mbox, slot);
257
258         spin_lock(&rnet->lock);
259         if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
260                 rionet_rx_fill(ndev, n);
261         spin_unlock(&rnet->lock);
262 }
263
264 static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
265 {
266         struct net_device *ndev = dev_id;
267         struct rionet_private *rnet = ndev->priv;
268
269         spin_lock(&rnet->lock);
270
271         if (netif_msg_intr(rnet))
272                 printk(KERN_INFO
273                        "%s: outbound message event, mbox %d slot %d\n",
274                        DRV_NAME, mbox, slot);
275
276         while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
277                 /* dma unmap single */
278                 dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
279                 rnet->tx_skb[rnet->ack_slot] = NULL;
280                 ++rnet->ack_slot;
281                 rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
282                 rnet->tx_cnt--;
283         }
284
285         if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
286                 netif_wake_queue(ndev);
287
288         spin_unlock(&rnet->lock);
289 }
290
291 static int rionet_open(struct net_device *ndev)
292 {
293         int i, rc = 0;
294         struct rionet_peer *peer, *tmp;
295         u32 pwdcsr;
296         struct rionet_private *rnet = ndev->priv;
297
298         if (netif_msg_ifup(rnet))
299                 printk(KERN_INFO "%s: open\n", DRV_NAME);
300
301         if ((rc = rio_request_inb_dbell(rnet->mport,
302                                         (void *)ndev,
303                                         RIONET_DOORBELL_JOIN,
304                                         RIONET_DOORBELL_LEAVE,
305                                         rionet_dbell_event)) < 0)
306                 goto out;
307
308         if ((rc = rio_request_inb_mbox(rnet->mport,
309                                        (void *)ndev,
310                                        RIONET_MAILBOX,
311                                        RIONET_RX_RING_SIZE,
312                                        rionet_inb_msg_event)) < 0)
313                 goto out;
314
315         if ((rc = rio_request_outb_mbox(rnet->mport,
316                                         (void *)ndev,
317                                         RIONET_MAILBOX,
318                                         RIONET_TX_RING_SIZE,
319                                         rionet_outb_msg_event)) < 0)
320                 goto out;
321
322         /* Initialize inbound message ring */
323         for (i = 0; i < RIONET_RX_RING_SIZE; i++)
324                 rnet->rx_skb[i] = NULL;
325         rnet->rx_slot = 0;
326         rionet_rx_fill(ndev, 0);
327
328         rnet->tx_slot = 0;
329         rnet->tx_cnt = 0;
330         rnet->ack_slot = 0;
331
332         netif_carrier_on(ndev);
333         netif_start_queue(ndev);
334
335         list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
336                 if (!(peer->res = rio_request_outb_dbell(peer->rdev,
337                                                          RIONET_DOORBELL_JOIN,
338                                                          RIONET_DOORBELL_LEAVE)))
339                 {
340                         printk(KERN_ERR "%s: error requesting doorbells\n",
341                                DRV_NAME);
342                         continue;
343                 }
344
345                 /*
346                  * If device has initialized inbound doorbells,
347                  * send a join message
348                  */
349                 rio_read_config_32(peer->rdev, RIO_WRITE_PORT_CSR, &pwdcsr);
350                 if (pwdcsr & RIO_DOORBELL_AVAIL)
351                         rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
352         }
353
354       out:
355         return rc;
356 }
357
358 static int rionet_close(struct net_device *ndev)
359 {
360         struct rionet_private *rnet = (struct rionet_private *)ndev->priv;
361         struct rionet_peer *peer, *tmp;
362         int i;
363
364         if (netif_msg_ifup(rnet))
365                 printk(KERN_INFO "%s: close\n", DRV_NAME);
366
367         netif_stop_queue(ndev);
368         netif_carrier_off(ndev);
369
370         for (i = 0; i < RIONET_RX_RING_SIZE; i++)
371                 if (rnet->rx_skb[i])
372                         kfree_skb(rnet->rx_skb[i]);
373
374         list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
375                 if (rionet_active[peer->rdev->destid]) {
376                         rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
377                         rionet_active[peer->rdev->destid] = NULL;
378                 }
379                 rio_release_outb_dbell(peer->rdev, peer->res);
380         }
381
382         rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
383                               RIONET_DOORBELL_LEAVE);
384         rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
385         rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
386
387         return 0;
388 }
389
390 static void rionet_remove(struct rio_dev *rdev)
391 {
392         struct net_device *ndev = NULL;
393         struct rionet_peer *peer, *tmp;
394
395         unregister_netdev(ndev);
396         kfree(ndev);
397
398         list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
399                 list_del(&peer->node);
400                 kfree(peer);
401         }
402 }
403
404 static void rionet_get_drvinfo(struct net_device *ndev,
405                                struct ethtool_drvinfo *info)
406 {
407         struct rionet_private *rnet = ndev->priv;
408
409         strcpy(info->driver, DRV_NAME);
410         strcpy(info->version, DRV_VERSION);
411         strcpy(info->fw_version, "n/a");
412         strcpy(info->bus_info, rnet->mport->name);
413 }
414
415 static u32 rionet_get_msglevel(struct net_device *ndev)
416 {
417         struct rionet_private *rnet = ndev->priv;
418
419         return rnet->msg_enable;
420 }
421
422 static void rionet_set_msglevel(struct net_device *ndev, u32 value)
423 {
424         struct rionet_private *rnet = ndev->priv;
425
426         rnet->msg_enable = value;
427 }
428
429 static const struct ethtool_ops rionet_ethtool_ops = {
430         .get_drvinfo = rionet_get_drvinfo,
431         .get_msglevel = rionet_get_msglevel,
432         .set_msglevel = rionet_set_msglevel,
433         .get_link = ethtool_op_get_link,
434 };
435
436 static int rionet_setup_netdev(struct rio_mport *mport)
437 {
438         int rc = 0;
439         struct net_device *ndev = NULL;
440         struct rionet_private *rnet;
441         u16 device_id;
442
443         /* Allocate our net_device structure */
444         ndev = alloc_etherdev(sizeof(struct rionet_private));
445         if (ndev == NULL) {
446                 printk(KERN_INFO "%s: could not allocate ethernet device.\n",
447                        DRV_NAME);
448                 rc = -ENOMEM;
449                 goto out;
450         }
451
452         /* Set up private area */
453         rnet = (struct rionet_private *)ndev->priv;
454         rnet->mport = mport;
455
456         /* Set the default MAC address */
457         device_id = rio_local_get_device_id(mport);
458         ndev->dev_addr[0] = 0x00;
459         ndev->dev_addr[1] = 0x01;
460         ndev->dev_addr[2] = 0x00;
461         ndev->dev_addr[3] = 0x01;
462         ndev->dev_addr[4] = device_id >> 8;
463         ndev->dev_addr[5] = device_id & 0xff;
464
465         /* Fill in the driver function table */
466         ndev->open = &rionet_open;
467         ndev->hard_start_xmit = &rionet_start_xmit;
468         ndev->stop = &rionet_close;
469         ndev->get_stats = &rionet_stats;
470         ndev->mtu = RIO_MAX_MSG_SIZE - 14;
471         ndev->features = NETIF_F_LLTX;
472         SET_ETHTOOL_OPS(ndev, &rionet_ethtool_ops);
473
474         SET_MODULE_OWNER(ndev);
475
476         spin_lock_init(&rnet->lock);
477         spin_lock_init(&rnet->tx_lock);
478
479         rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
480
481         rc = register_netdev(ndev);
482         if (rc != 0)
483                 goto out;
484
485         printk("%s: %s %s Version %s, MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
486                ndev->name,
487                DRV_NAME,
488                DRV_DESC,
489                DRV_VERSION,
490                ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
491                ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
492
493       out:
494         return rc;
495 }
496
497 /*
498  * XXX Make multi-net safe
499  */
500 static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
501 {
502         int rc = -ENODEV;
503         u32 lpef, lsrc_ops, ldst_ops;
504         struct rionet_peer *peer;
505
506         /* If local device is not rionet capable, give up quickly */
507         if (!rionet_capable)
508                 goto out;
509
510         /*
511          * First time through, make sure local device is rionet
512          * capable, setup netdev,  and set flags so this is skipped
513          * on later probes
514          */
515         if (!rionet_check) {
516                 rio_local_read_config_32(rdev->net->hport, RIO_PEF_CAR, &lpef);
517                 rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
518                                          &lsrc_ops);
519                 rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
520                                          &ldst_ops);
521                 if (!is_rionet_capable(lpef, lsrc_ops, ldst_ops)) {
522                         printk(KERN_ERR
523                                "%s: local device is not network capable\n",
524                                DRV_NAME);
525                         rionet_check = 1;
526                         rionet_capable = 0;
527                         goto out;
528                 }
529
530                 rc = rionet_setup_netdev(rdev->net->hport);
531                 rionet_check = 1;
532         }
533
534         /*
535          * If the remote device has mailbox/doorbell capabilities,
536          * add it to the peer list.
537          */
538         if (dev_rionet_capable(rdev)) {
539                 if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
540                         rc = -ENOMEM;
541                         goto out;
542                 }
543                 peer->rdev = rdev;
544                 list_add_tail(&peer->node, &rionet_peers);
545         }
546
547       out:
548         return rc;
549 }
550
551 static struct rio_device_id rionet_id_table[] = {
552         {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
553 };
554
555 static struct rio_driver rionet_driver = {
556         .name = "rionet",
557         .id_table = rionet_id_table,
558         .probe = rionet_probe,
559         .remove = rionet_remove,
560 };
561
562 static int __init rionet_init(void)
563 {
564         return rio_register_driver(&rionet_driver);
565 }
566
567 static void __exit rionet_exit(void)
568 {
569         rio_unregister_driver(&rionet_driver);
570 }
571
572 module_init(rionet_init);
573 module_exit(rionet_exit);