2 * rionet - Ethernet driver over RapidIO messaging services
4 * Copyright 2005 MontaVista Software, Inc.
5 * Matt Porter <mporter@kernel.crashing.org>
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.
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>
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>
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"
32 MODULE_AUTHOR(DRV_AUTHOR);
33 MODULE_DESCRIPTION(DRV_DESC);
34 MODULE_LICENSE("GPL");
36 #define RIONET_DEFAULT_MSGLEVEL \
42 #define RIONET_DOORBELL_JOIN 0x1000
43 #define RIONET_DOORBELL_LEAVE 0x1001
45 #define RIONET_MAILBOX 0
47 #define RIONET_TX_RING_SIZE CONFIG_RIONET_TX_SIZE
48 #define RIONET_RX_RING_SIZE CONFIG_RIONET_RX_SIZE
50 static LIST_HEAD(rionet_peers);
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;
67 struct list_head node;
72 static int rionet_check = 0;
73 static int rionet_capable = 1;
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.
81 static struct rio_dev *rionet_active[RIO_MAX_ROUTE_ENTRIES];
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)
91 #define RIONET_MAC_MATCH(x) (*(u32 *)x == 0x00010001)
92 #define RIONET_GET_DESTID(x) (*(u16 *)(x + 4))
94 static struct net_device_stats *rionet_stats(struct net_device *ndev)
96 struct rionet_private *rnet = ndev->priv;
100 static int rionet_rx_clean(struct net_device *ndev)
104 struct rionet_private *rnet = ndev->priv;
110 if (!rnet->rx_skb[i])
113 if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
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]);
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",
128 rnet->stats.rx_errors++;
130 rnet->stats.rx_packets++;
131 rnet->stats.rx_bytes += RIO_MAX_MSG_SIZE;
134 } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
139 static void rionet_rx_fill(struct net_device *ndev, int end)
142 struct rionet_private *rnet = ndev->priv;
146 rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
148 if (!rnet->rx_skb[i])
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);
158 static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
159 struct rio_dev *rdev)
161 struct rionet_private *rnet = ndev->priv;
163 rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
164 rnet->tx_skb[rnet->tx_slot] = skb;
166 rnet->stats.tx_packets++;
167 rnet->stats.tx_bytes += skb->len;
169 if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
170 netif_stop_queue(ndev);
173 rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
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);
182 static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
185 struct rionet_private *rnet = ndev->priv;
186 struct ethhdr *eth = (struct ethhdr *)skb->data;
190 local_irq_save(flags);
191 if (!spin_trylock(&rnet->tx_lock)) {
192 local_irq_restore(flags);
193 return NETDEV_TX_LOCKED;
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",
201 return NETDEV_TX_BUSY;
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,
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]);
215 spin_unlock_irqrestore(&rnet->tx_lock, flags);
220 static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
223 struct net_device *ndev = dev_id;
224 struct rionet_private *rnet = ndev->priv;
225 struct rionet_peer *peer;
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;
236 rio_mport_send_doorbell(mport, sid,
237 RIONET_DOORBELL_JOIN);
239 } else if (info == RIONET_DOORBELL_LEAVE) {
240 rionet_active[sid] = NULL;
242 if (netif_msg_intr(rnet))
243 printk(KERN_WARNING "%s: unhandled doorbell\n",
248 static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
251 struct net_device *ndev = dev_id;
252 struct rionet_private *rnet = (struct rionet_private *)ndev->priv;
254 if (netif_msg_intr(rnet))
255 printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
256 DRV_NAME, mbox, slot);
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);
264 static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
266 struct net_device *ndev = dev_id;
267 struct rionet_private *rnet = ndev->priv;
269 spin_lock(&rnet->lock);
271 if (netif_msg_intr(rnet))
273 "%s: outbound message event, mbox %d slot %d\n",
274 DRV_NAME, mbox, slot);
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;
281 rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
285 if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
286 netif_wake_queue(ndev);
288 spin_unlock(&rnet->lock);
291 static int rionet_open(struct net_device *ndev)
294 struct rionet_peer *peer, *tmp;
296 struct rionet_private *rnet = ndev->priv;
298 if (netif_msg_ifup(rnet))
299 printk(KERN_INFO "%s: open\n", DRV_NAME);
301 if ((rc = rio_request_inb_dbell(rnet->mport,
303 RIONET_DOORBELL_JOIN,
304 RIONET_DOORBELL_LEAVE,
305 rionet_dbell_event)) < 0)
308 if ((rc = rio_request_inb_mbox(rnet->mport,
312 rionet_inb_msg_event)) < 0)
315 if ((rc = rio_request_outb_mbox(rnet->mport,
319 rionet_outb_msg_event)) < 0)
322 /* Initialize inbound message ring */
323 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
324 rnet->rx_skb[i] = NULL;
326 rionet_rx_fill(ndev, 0);
332 netif_carrier_on(ndev);
333 netif_start_queue(ndev);
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)))
340 printk(KERN_ERR "%s: error requesting doorbells\n",
346 * If device has initialized inbound doorbells,
347 * send a join message
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);
358 static int rionet_close(struct net_device *ndev)
360 struct rionet_private *rnet = (struct rionet_private *)ndev->priv;
361 struct rionet_peer *peer, *tmp;
364 if (netif_msg_ifup(rnet))
365 printk(KERN_INFO "%s: close\n", DRV_NAME);
367 netif_stop_queue(ndev);
368 netif_carrier_off(ndev);
370 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
372 kfree_skb(rnet->rx_skb[i]);
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;
379 rio_release_outb_dbell(peer->rdev, peer->res);
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);
390 static void rionet_remove(struct rio_dev *rdev)
392 struct net_device *ndev = NULL;
393 struct rionet_peer *peer, *tmp;
395 unregister_netdev(ndev);
398 list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
399 list_del(&peer->node);
404 static void rionet_get_drvinfo(struct net_device *ndev,
405 struct ethtool_drvinfo *info)
407 struct rionet_private *rnet = ndev->priv;
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);
415 static u32 rionet_get_msglevel(struct net_device *ndev)
417 struct rionet_private *rnet = ndev->priv;
419 return rnet->msg_enable;
422 static void rionet_set_msglevel(struct net_device *ndev, u32 value)
424 struct rionet_private *rnet = ndev->priv;
426 rnet->msg_enable = value;
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,
436 static int rionet_setup_netdev(struct rio_mport *mport)
439 struct net_device *ndev = NULL;
440 struct rionet_private *rnet;
443 /* Allocate our net_device structure */
444 ndev = alloc_etherdev(sizeof(struct rionet_private));
446 printk(KERN_INFO "%s: could not allocate ethernet device.\n",
452 /* Set up private area */
453 rnet = (struct rionet_private *)ndev->priv;
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;
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);
474 SET_MODULE_OWNER(ndev);
476 spin_lock_init(&rnet->lock);
477 spin_lock_init(&rnet->tx_lock);
479 rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
481 rc = register_netdev(ndev);
485 printk("%s: %s %s Version %s, MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
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]);
498 * XXX Make multi-net safe
500 static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
503 u32 lpef, lsrc_ops, ldst_ops;
504 struct rionet_peer *peer;
506 /* If local device is not rionet capable, give up quickly */
511 * First time through, make sure local device is rionet
512 * capable, setup netdev, and set flags so this is skipped
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,
519 rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
521 if (!is_rionet_capable(lpef, lsrc_ops, ldst_ops)) {
523 "%s: local device is not network capable\n",
530 rc = rionet_setup_netdev(rdev->net->hport);
535 * If the remote device has mailbox/doorbell capabilities,
536 * add it to the peer list.
538 if (dev_rionet_capable(rdev)) {
539 if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
544 list_add_tail(&peer->node, &rionet_peers);
551 static struct rio_device_id rionet_id_table[] = {
552 {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
555 static struct rio_driver rionet_driver = {
557 .id_table = rionet_id_table,
558 .probe = rionet_probe,
559 .remove = rionet_remove,
562 static int __init rionet_init(void)
564 return rio_register_driver(&rionet_driver);
567 static void __exit rionet_exit(void)
569 rio_unregister_driver(&rionet_driver);
572 module_init(rionet_init);
573 module_exit(rionet_exit);