b43: fix two warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / rionet.c
blobede937ee50c704e373554f9881782dea7840b31b
1 /*
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 \
37 (NETIF_MSG_DRV | \
38 NETIF_MSG_LINK | \
39 NETIF_MSG_RX_ERR | \
40 NETIF_MSG_TX_ERR)
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 int rx_slot;
57 int tx_slot;
58 int tx_cnt;
59 int ack_slot;
60 spinlock_t lock;
61 spinlock_t tx_lock;
62 u32 msg_enable;
65 struct rionet_peer {
66 struct list_head node;
67 struct rio_dev *rdev;
68 struct resource *res;
71 static int rionet_check = 0;
72 static int rionet_capable = 1;
75 * This is a fast lookup table for translating TX
76 * Ethernet packets into a destination RIO device. It
77 * could be made into a hash table to save memory depending
78 * on system trade-offs.
80 static struct rio_dev **rionet_active;
82 #define is_rionet_capable(pef, src_ops, dst_ops) \
83 ((pef & RIO_PEF_INB_MBOX) && \
84 (pef & RIO_PEF_INB_DOORBELL) && \
85 (src_ops & RIO_SRC_OPS_DOORBELL) && \
86 (dst_ops & RIO_DST_OPS_DOORBELL))
87 #define dev_rionet_capable(dev) \
88 is_rionet_capable(dev->pef, dev->src_ops, dev->dst_ops)
90 #define RIONET_MAC_MATCH(x) (*(u32 *)x == 0x00010001)
91 #define RIONET_GET_DESTID(x) (*(u16 *)(x + 4))
93 static int rionet_rx_clean(struct net_device *ndev)
95 int i;
96 int error = 0;
97 struct rionet_private *rnet = netdev_priv(ndev);
98 void *data;
100 i = rnet->rx_slot;
102 do {
103 if (!rnet->rx_skb[i])
104 continue;
106 if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
107 break;
109 rnet->rx_skb[i]->data = data;
110 skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
111 rnet->rx_skb[i]->protocol =
112 eth_type_trans(rnet->rx_skb[i], ndev);
113 error = netif_rx(rnet->rx_skb[i]);
115 if (error == NET_RX_DROP) {
116 ndev->stats.rx_dropped++;
117 } else {
118 ndev->stats.rx_packets++;
119 ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
122 } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
124 return i;
127 static void rionet_rx_fill(struct net_device *ndev, int end)
129 int i;
130 struct rionet_private *rnet = netdev_priv(ndev);
132 i = rnet->rx_slot;
133 do {
134 rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
136 if (!rnet->rx_skb[i])
137 break;
139 rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
140 rnet->rx_skb[i]->data);
141 } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
143 rnet->rx_slot = i;
146 static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
147 struct rio_dev *rdev)
149 struct rionet_private *rnet = netdev_priv(ndev);
151 rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
152 rnet->tx_skb[rnet->tx_slot] = skb;
154 ndev->stats.tx_packets++;
155 ndev->stats.tx_bytes += skb->len;
157 if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
158 netif_stop_queue(ndev);
160 ++rnet->tx_slot;
161 rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
163 if (netif_msg_tx_queued(rnet))
164 printk(KERN_INFO "%s: queued skb %8.8x len %8.8x\n", DRV_NAME,
165 (u32) skb, skb->len);
167 return 0;
170 static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
172 int i;
173 struct rionet_private *rnet = netdev_priv(ndev);
174 struct ethhdr *eth = (struct ethhdr *)skb->data;
175 u16 destid;
176 unsigned long flags;
178 local_irq_save(flags);
179 if (!spin_trylock(&rnet->tx_lock)) {
180 local_irq_restore(flags);
181 return NETDEV_TX_LOCKED;
184 if ((rnet->tx_cnt + 1) > RIONET_TX_RING_SIZE) {
185 netif_stop_queue(ndev);
186 spin_unlock_irqrestore(&rnet->tx_lock, flags);
187 printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
188 ndev->name);
189 return NETDEV_TX_BUSY;
192 if (eth->h_dest[0] & 0x01) {
193 for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
194 i++)
195 if (rionet_active[i])
196 rionet_queue_tx_msg(skb, ndev,
197 rionet_active[i]);
198 } else if (RIONET_MAC_MATCH(eth->h_dest)) {
199 destid = RIONET_GET_DESTID(eth->h_dest);
200 if (rionet_active[destid])
201 rionet_queue_tx_msg(skb, ndev, rionet_active[destid]);
204 spin_unlock_irqrestore(&rnet->tx_lock, flags);
206 return NETDEV_TX_OK;
209 static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
210 u16 info)
212 struct net_device *ndev = dev_id;
213 struct rionet_private *rnet = netdev_priv(ndev);
214 struct rionet_peer *peer;
216 if (netif_msg_intr(rnet))
217 printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
218 DRV_NAME, sid, tid, info);
219 if (info == RIONET_DOORBELL_JOIN) {
220 if (!rionet_active[sid]) {
221 list_for_each_entry(peer, &rionet_peers, node) {
222 if (peer->rdev->destid == sid)
223 rionet_active[sid] = peer->rdev;
225 rio_mport_send_doorbell(mport, sid,
226 RIONET_DOORBELL_JOIN);
228 } else if (info == RIONET_DOORBELL_LEAVE) {
229 rionet_active[sid] = NULL;
230 } else {
231 if (netif_msg_intr(rnet))
232 printk(KERN_WARNING "%s: unhandled doorbell\n",
233 DRV_NAME);
237 static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
239 int n;
240 struct net_device *ndev = dev_id;
241 struct rionet_private *rnet = netdev_priv(ndev);
243 if (netif_msg_intr(rnet))
244 printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
245 DRV_NAME, mbox, slot);
247 spin_lock(&rnet->lock);
248 if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
249 rionet_rx_fill(ndev, n);
250 spin_unlock(&rnet->lock);
253 static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
255 struct net_device *ndev = dev_id;
256 struct rionet_private *rnet = netdev_priv(ndev);
258 spin_lock(&rnet->lock);
260 if (netif_msg_intr(rnet))
261 printk(KERN_INFO
262 "%s: outbound message event, mbox %d slot %d\n",
263 DRV_NAME, mbox, slot);
265 while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
266 /* dma unmap single */
267 dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
268 rnet->tx_skb[rnet->ack_slot] = NULL;
269 ++rnet->ack_slot;
270 rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
271 rnet->tx_cnt--;
274 if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
275 netif_wake_queue(ndev);
277 spin_unlock(&rnet->lock);
280 static int rionet_open(struct net_device *ndev)
282 int i, rc = 0;
283 struct rionet_peer *peer, *tmp;
284 u32 pwdcsr;
285 struct rionet_private *rnet = netdev_priv(ndev);
287 if (netif_msg_ifup(rnet))
288 printk(KERN_INFO "%s: open\n", DRV_NAME);
290 if ((rc = rio_request_inb_dbell(rnet->mport,
291 (void *)ndev,
292 RIONET_DOORBELL_JOIN,
293 RIONET_DOORBELL_LEAVE,
294 rionet_dbell_event)) < 0)
295 goto out;
297 if ((rc = rio_request_inb_mbox(rnet->mport,
298 (void *)ndev,
299 RIONET_MAILBOX,
300 RIONET_RX_RING_SIZE,
301 rionet_inb_msg_event)) < 0)
302 goto out;
304 if ((rc = rio_request_outb_mbox(rnet->mport,
305 (void *)ndev,
306 RIONET_MAILBOX,
307 RIONET_TX_RING_SIZE,
308 rionet_outb_msg_event)) < 0)
309 goto out;
311 /* Initialize inbound message ring */
312 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
313 rnet->rx_skb[i] = NULL;
314 rnet->rx_slot = 0;
315 rionet_rx_fill(ndev, 0);
317 rnet->tx_slot = 0;
318 rnet->tx_cnt = 0;
319 rnet->ack_slot = 0;
321 netif_carrier_on(ndev);
322 netif_start_queue(ndev);
324 list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
325 if (!(peer->res = rio_request_outb_dbell(peer->rdev,
326 RIONET_DOORBELL_JOIN,
327 RIONET_DOORBELL_LEAVE)))
329 printk(KERN_ERR "%s: error requesting doorbells\n",
330 DRV_NAME);
331 continue;
335 * If device has initialized inbound doorbells,
336 * send a join message
338 rio_read_config_32(peer->rdev, RIO_WRITE_PORT_CSR, &pwdcsr);
339 if (pwdcsr & RIO_DOORBELL_AVAIL)
340 rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
343 out:
344 return rc;
347 static int rionet_close(struct net_device *ndev)
349 struct rionet_private *rnet = netdev_priv(ndev);
350 struct rionet_peer *peer, *tmp;
351 int i;
353 if (netif_msg_ifup(rnet))
354 printk(KERN_INFO "%s: close\n", DRV_NAME);
356 netif_stop_queue(ndev);
357 netif_carrier_off(ndev);
359 for (i = 0; i < RIONET_RX_RING_SIZE; i++)
360 kfree_skb(rnet->rx_skb[i]);
362 list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
363 if (rionet_active[peer->rdev->destid]) {
364 rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
365 rionet_active[peer->rdev->destid] = NULL;
367 rio_release_outb_dbell(peer->rdev, peer->res);
370 rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
371 RIONET_DOORBELL_LEAVE);
372 rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
373 rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
375 return 0;
378 static void rionet_remove(struct rio_dev *rdev)
380 struct net_device *ndev = NULL;
381 struct rionet_peer *peer, *tmp;
383 free_pages((unsigned long)rionet_active, rdev->net->hport->sys_size ?
384 __ilog2(sizeof(void *)) + 4 : 0);
385 unregister_netdev(ndev);
386 kfree(ndev);
388 list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
389 list_del(&peer->node);
390 kfree(peer);
394 static void rionet_get_drvinfo(struct net_device *ndev,
395 struct ethtool_drvinfo *info)
397 struct rionet_private *rnet = netdev_priv(ndev);
399 strcpy(info->driver, DRV_NAME);
400 strcpy(info->version, DRV_VERSION);
401 strcpy(info->fw_version, "n/a");
402 strcpy(info->bus_info, rnet->mport->name);
405 static u32 rionet_get_msglevel(struct net_device *ndev)
407 struct rionet_private *rnet = netdev_priv(ndev);
409 return rnet->msg_enable;
412 static void rionet_set_msglevel(struct net_device *ndev, u32 value)
414 struct rionet_private *rnet = netdev_priv(ndev);
416 rnet->msg_enable = value;
419 static const struct ethtool_ops rionet_ethtool_ops = {
420 .get_drvinfo = rionet_get_drvinfo,
421 .get_msglevel = rionet_get_msglevel,
422 .set_msglevel = rionet_set_msglevel,
423 .get_link = ethtool_op_get_link,
426 static const struct net_device_ops rionet_netdev_ops = {
427 .ndo_open = rionet_open,
428 .ndo_stop = rionet_close,
429 .ndo_start_xmit = rionet_start_xmit,
430 .ndo_change_mtu = eth_change_mtu,
431 .ndo_validate_addr = eth_validate_addr,
432 .ndo_set_mac_address = eth_mac_addr,
435 static int rionet_setup_netdev(struct rio_mport *mport)
437 int rc = 0;
438 struct net_device *ndev = NULL;
439 struct rionet_private *rnet;
440 u16 device_id;
442 /* Allocate our net_device structure */
443 ndev = alloc_etherdev(sizeof(struct rionet_private));
444 if (ndev == NULL) {
445 printk(KERN_INFO "%s: could not allocate ethernet device.\n",
446 DRV_NAME);
447 rc = -ENOMEM;
448 goto out;
451 rionet_active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
452 mport->sys_size ? __ilog2(sizeof(void *)) + 4 : 0);
453 if (!rionet_active) {
454 rc = -ENOMEM;
455 goto out;
457 memset((void *)rionet_active, 0, sizeof(void *) *
458 RIO_MAX_ROUTE_ENTRIES(mport->sys_size));
460 /* Set up private area */
461 rnet = netdev_priv(ndev);
462 rnet->mport = mport;
464 /* Set the default MAC address */
465 device_id = rio_local_get_device_id(mport);
466 ndev->dev_addr[0] = 0x00;
467 ndev->dev_addr[1] = 0x01;
468 ndev->dev_addr[2] = 0x00;
469 ndev->dev_addr[3] = 0x01;
470 ndev->dev_addr[4] = device_id >> 8;
471 ndev->dev_addr[5] = device_id & 0xff;
473 ndev->netdev_ops = &rionet_netdev_ops;
474 ndev->mtu = RIO_MAX_MSG_SIZE - 14;
475 ndev->features = NETIF_F_LLTX;
476 SET_ETHTOOL_OPS(ndev, &rionet_ethtool_ops);
478 spin_lock_init(&rnet->lock);
479 spin_lock_init(&rnet->tx_lock);
481 rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
483 rc = register_netdev(ndev);
484 if (rc != 0)
485 goto out;
487 printk("%s: %s %s Version %s, MAC %pM\n",
488 ndev->name,
489 DRV_NAME,
490 DRV_DESC,
491 DRV_VERSION,
492 ndev->dev_addr);
494 out:
495 return rc;
499 * XXX Make multi-net safe
501 static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
503 int rc = -ENODEV;
504 u32 lpef, lsrc_ops, ldst_ops;
505 struct rionet_peer *peer;
507 /* If local device is not rionet capable, give up quickly */
508 if (!rionet_capable)
509 goto out;
512 * First time through, make sure local device is rionet
513 * capable, setup netdev, and set flags so this is skipped
514 * on later probes
516 if (!rionet_check) {
517 rio_local_read_config_32(rdev->net->hport, RIO_PEF_CAR, &lpef);
518 rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
519 &lsrc_ops);
520 rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
521 &ldst_ops);
522 if (!is_rionet_capable(lpef, lsrc_ops, ldst_ops)) {
523 printk(KERN_ERR
524 "%s: local device is not network capable\n",
525 DRV_NAME);
526 rionet_check = 1;
527 rionet_capable = 0;
528 goto out;
531 rc = rionet_setup_netdev(rdev->net->hport);
532 rionet_check = 1;
536 * If the remote device has mailbox/doorbell capabilities,
537 * add it to the peer list.
539 if (dev_rionet_capable(rdev)) {
540 if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
541 rc = -ENOMEM;
542 goto out;
544 peer->rdev = rdev;
545 list_add_tail(&peer->node, &rionet_peers);
548 out:
549 return rc;
552 static struct rio_device_id rionet_id_table[] = {
553 {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
556 static struct rio_driver rionet_driver = {
557 .name = "rionet",
558 .id_table = rionet_id_table,
559 .probe = rionet_probe,
560 .remove = rionet_remove,
563 static int __init rionet_init(void)
565 return rio_register_driver(&rionet_driver);
568 static void __exit rionet_exit(void)
570 rio_unregister_driver(&rionet_driver);
573 module_init(rionet_init);
574 module_exit(rionet_exit);