rionet.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/delay.h>
  16. #include <linux/rio.h>
  17. #include <linux/rio_drv.h>
  18. #include <linux/slab.h>
  19. #include <linux/rio_ids.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/crc32.h>
  24. #include <linux/ethtool.h>
  25. #define DRV_NAME "rionet"
  26. #define DRV_VERSION "0.2"
  27. #define DRV_AUTHOR "Matt Porter <mporter@kernel.crashing.org>"
  28. #define DRV_DESC "Ethernet over RapidIO"
  29. MODULE_AUTHOR(DRV_AUTHOR);
  30. MODULE_DESCRIPTION(DRV_DESC);
  31. MODULE_LICENSE("GPL");
  32. #define RIONET_DEFAULT_MSGLEVEL \
  33. (NETIF_MSG_DRV | \
  34. NETIF_MSG_LINK | \
  35. NETIF_MSG_RX_ERR | \
  36. NETIF_MSG_TX_ERR)
  37. #define RIONET_DOORBELL_JOIN 0x1000
  38. #define RIONET_DOORBELL_LEAVE 0x1001
  39. #define RIONET_MAILBOX 0
  40. #define RIONET_TX_RING_SIZE CONFIG_RIONET_TX_SIZE
  41. #define RIONET_RX_RING_SIZE CONFIG_RIONET_RX_SIZE
  42. static LIST_HEAD(rionet_peers);
  43. struct rionet_private {
  44. struct rio_mport *mport;
  45. struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
  46. struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
  47. int rx_slot;
  48. int tx_slot;
  49. int tx_cnt;
  50. int ack_slot;
  51. spinlock_t lock;
  52. spinlock_t tx_lock;
  53. u32 msg_enable;
  54. };
  55. struct rionet_peer {
  56. struct list_head node;
  57. struct rio_dev *rdev;
  58. struct resource *res;
  59. };
  60. static int rionet_check = 0;
  61. static int rionet_capable = 1;
  62. /*
  63. * This is a fast lookup table for translating TX
  64. * Ethernet packets into a destination RIO device. It
  65. * could be made into a hash table to save memory depending
  66. * on system trade-offs.
  67. */
  68. static struct rio_dev **rionet_active;
  69. #define is_rionet_capable(src_ops, dst_ops) \
  70. ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
  71. (dst_ops & RIO_DST_OPS_DATA_MSG) && \
  72. (src_ops & RIO_SRC_OPS_DOORBELL) && \
  73. (dst_ops & RIO_DST_OPS_DOORBELL))
  74. #define dev_rionet_capable(dev) \
  75. is_rionet_capable(dev->src_ops, dev->dst_ops)
  76. #define RIONET_MAC_MATCH(x) (!memcmp((x), "\00\01\00\01", 4))
  77. #define RIONET_GET_DESTID(x) ((*((u8 *)x + 4) << 8) | *((u8 *)x + 5))
  78. static int rionet_rx_clean(struct net_device *ndev)
  79. {
  80. int i;
  81. int error = 0;
  82. struct rionet_private *rnet = netdev_priv(ndev);
  83. void *data;
  84. i = rnet->rx_slot;
  85. do {
  86. if (!rnet->rx_skb[i])
  87. continue;
  88. if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
  89. break;
  90. rnet->rx_skb[i]->data = data;
  91. skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
  92. rnet->rx_skb[i]->protocol =
  93. eth_type_trans(rnet->rx_skb[i], ndev);
  94. error = netif_rx(rnet->rx_skb[i]);
  95. if (error == NET_RX_DROP) {
  96. ndev->stats.rx_dropped++;
  97. } else {
  98. ndev->stats.rx_packets++;
  99. ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
  100. }
  101. } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
  102. return i;
  103. }
  104. static void rionet_rx_fill(struct net_device *ndev, int end)
  105. {
  106. int i;
  107. struct rionet_private *rnet = netdev_priv(ndev);
  108. i = rnet->rx_slot;
  109. do {
  110. rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
  111. if (!rnet->rx_skb[i])
  112. break;
  113. rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
  114. rnet->rx_skb[i]->data);
  115. } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
  116. rnet->rx_slot = i;
  117. }
  118. static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
  119. struct rio_dev *rdev)
  120. {
  121. struct rionet_private *rnet = netdev_priv(ndev);
  122. rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
  123. rnet->tx_skb[rnet->tx_slot] = skb;
  124. ndev->stats.tx_packets++;
  125. ndev->stats.tx_bytes += skb->len;
  126. if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
  127. netif_stop_queue(ndev);
  128. ++rnet->tx_slot;
  129. rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
  130. if (netif_msg_tx_queued(rnet))
  131. printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME,
  132. skb->len);
  133. return 0;
  134. }
  135. static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  136. {
  137. int i;
  138. struct rionet_private *rnet = netdev_priv(ndev);
  139. struct ethhdr *eth = (struct ethhdr *)skb->data;
  140. u16 destid;
  141. unsigned long flags;
  142. local_irq_save(flags);
  143. if (!spin_trylock(&rnet->tx_lock)) {
  144. local_irq_restore(flags);
  145. return NETDEV_TX_LOCKED;
  146. }
  147. if ((rnet->tx_cnt + 1) > RIONET_TX_RING_SIZE) {
  148. netif_stop_queue(ndev);
  149. spin_unlock_irqrestore(&rnet->tx_lock, flags);
  150. printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
  151. ndev->name);
  152. return NETDEV_TX_BUSY;
  153. }
  154. if (eth->h_dest[0] & 0x01) {
  155. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
  156. i++)
  157. if (rionet_active[i])
  158. rionet_queue_tx_msg(skb, ndev,
  159. rionet_active[i]);
  160. } else if (RIONET_MAC_MATCH(eth->h_dest)) {
  161. destid = RIONET_GET_DESTID(eth->h_dest);
  162. if (rionet_active[destid])
  163. rionet_queue_tx_msg(skb, ndev, rionet_active[destid]);
  164. }
  165. spin_unlock_irqrestore(&rnet->tx_lock, flags);
  166. return NETDEV_TX_OK;
  167. }
  168. static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
  169. u16 info)
  170. {
  171. struct net_device *ndev = dev_id;
  172. struct rionet_private *rnet = netdev_priv(ndev);
  173. struct rionet_peer *peer;
  174. if (netif_msg_intr(rnet))
  175. printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
  176. DRV_NAME, sid, tid, info);
  177. if (info == RIONET_DOORBELL_JOIN) {
  178. if (!rionet_active[sid]) {
  179. list_for_each_entry(peer, &rionet_peers, node) {
  180. if (peer->rdev->destid == sid)
  181. rionet_active[sid] = peer->rdev;
  182. }
  183. rio_mport_send_doorbell(mport, sid,
  184. RIONET_DOORBELL_JOIN);
  185. }
  186. } else if (info == RIONET_DOORBELL_LEAVE) {
  187. rionet_active[sid] = NULL;
  188. } else {
  189. if (netif_msg_intr(rnet))
  190. printk(KERN_WARNING "%s: unhandled doorbell\n",
  191. DRV_NAME);
  192. }
  193. }
  194. static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
  195. {
  196. int n;
  197. struct net_device *ndev = dev_id;
  198. struct rionet_private *rnet = netdev_priv(ndev);
  199. if (netif_msg_intr(rnet))
  200. printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
  201. DRV_NAME, mbox, slot);
  202. spin_lock(&rnet->lock);
  203. if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
  204. rionet_rx_fill(ndev, n);
  205. spin_unlock(&rnet->lock);
  206. }
  207. static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
  208. {
  209. struct net_device *ndev = dev_id;
  210. struct rionet_private *rnet = netdev_priv(ndev);
  211. spin_lock(&rnet->lock);
  212. if (netif_msg_intr(rnet))
  213. printk(KERN_INFO
  214. "%s: outbound message event, mbox %d slot %d\n",
  215. DRV_NAME, mbox, slot);
  216. while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
  217. /* dma unmap single */
  218. dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
  219. rnet->tx_skb[rnet->ack_slot] = NULL;
  220. ++rnet->ack_slot;
  221. rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
  222. rnet->tx_cnt--;
  223. }
  224. if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
  225. netif_wake_queue(ndev);
  226. spin_unlock(&rnet->lock);
  227. }
  228. static int rionet_open(struct net_device *ndev)
  229. {
  230. int i, rc = 0;
  231. struct rionet_peer *peer, *tmp;
  232. struct rionet_private *rnet = netdev_priv(ndev);
  233. if (netif_msg_ifup(rnet))
  234. printk(KERN_INFO "%s: open\n", DRV_NAME);
  235. if ((rc = rio_request_inb_dbell(rnet->mport,
  236. (void *)ndev,
  237. RIONET_DOORBELL_JOIN,
  238. RIONET_DOORBELL_LEAVE,
  239. rionet_dbell_event)) < 0)
  240. goto out;
  241. if ((rc = rio_request_inb_mbox(rnet->mport,
  242. (void *)ndev,
  243. RIONET_MAILBOX,
  244. RIONET_RX_RING_SIZE,
  245. rionet_inb_msg_event)) < 0)
  246. goto out;
  247. if ((rc = rio_request_outb_mbox(rnet->mport,
  248. (void *)ndev,
  249. RIONET_MAILBOX,
  250. RIONET_TX_RING_SIZE,
  251. rionet_outb_msg_event)) < 0)
  252. goto out;
  253. /* Initialize inbound message ring */
  254. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  255. rnet->rx_skb[i] = NULL;
  256. rnet->rx_slot = 0;
  257. rionet_rx_fill(ndev, 0);
  258. rnet->tx_slot = 0;
  259. rnet->tx_cnt = 0;
  260. rnet->ack_slot = 0;
  261. netif_carrier_on(ndev);
  262. netif_start_queue(ndev);
  263. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  264. if (!(peer->res = rio_request_outb_dbell(peer->rdev,
  265. RIONET_DOORBELL_JOIN,
  266. RIONET_DOORBELL_LEAVE)))
  267. {
  268. printk(KERN_ERR "%s: error requesting doorbells\n",
  269. DRV_NAME);
  270. continue;
  271. }
  272. /* Send a join message */
  273. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
  274. }
  275. out:
  276. return rc;
  277. }
  278. static int rionet_close(struct net_device *ndev)
  279. {
  280. struct rionet_private *rnet = netdev_priv(ndev);
  281. struct rionet_peer *peer, *tmp;
  282. int i;
  283. if (netif_msg_ifup(rnet))
  284. printk(KERN_INFO "%s: close\n", DRV_NAME);
  285. netif_stop_queue(ndev);
  286. netif_carrier_off(ndev);
  287. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  288. kfree_skb(rnet->rx_skb[i]);
  289. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  290. if (rionet_active[peer->rdev->destid]) {
  291. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
  292. rionet_active[peer->rdev->destid] = NULL;
  293. }
  294. rio_release_outb_dbell(peer->rdev, peer->res);
  295. }
  296. rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
  297. RIONET_DOORBELL_LEAVE);
  298. rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
  299. rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
  300. return 0;
  301. }
  302. static void rionet_remove(struct rio_dev *rdev)
  303. {
  304. struct net_device *ndev = rio_get_drvdata(rdev);
  305. struct rionet_peer *peer, *tmp;
  306. free_pages((unsigned long)rionet_active, rdev->net->hport->sys_size ?
  307. __fls(sizeof(void *)) + 4 : 0);
  308. unregister_netdev(ndev);
  309. free_netdev(ndev);
  310. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  311. list_del(&peer->node);
  312. kfree(peer);
  313. }
  314. }
  315. static void rionet_get_drvinfo(struct net_device *ndev,
  316. struct ethtool_drvinfo *info)
  317. {
  318. struct rionet_private *rnet = netdev_priv(ndev);
  319. strcpy(info->driver, DRV_NAME);
  320. strcpy(info->version, DRV_VERSION);
  321. strcpy(info->fw_version, "n/a");
  322. strcpy(info->bus_info, rnet->mport->name);
  323. }
  324. static u32 rionet_get_msglevel(struct net_device *ndev)
  325. {
  326. struct rionet_private *rnet = netdev_priv(ndev);
  327. return rnet->msg_enable;
  328. }
  329. static void rionet_set_msglevel(struct net_device *ndev, u32 value)
  330. {
  331. struct rionet_private *rnet = netdev_priv(ndev);
  332. rnet->msg_enable = value;
  333. }
  334. static const struct ethtool_ops rionet_ethtool_ops = {
  335. .get_drvinfo = rionet_get_drvinfo,
  336. .get_msglevel = rionet_get_msglevel,
  337. .set_msglevel = rionet_set_msglevel,
  338. .get_link = ethtool_op_get_link,
  339. };
  340. static const struct net_device_ops rionet_netdev_ops = {
  341. .ndo_open = rionet_open,
  342. .ndo_stop = rionet_close,
  343. .ndo_start_xmit = rionet_start_xmit,
  344. .ndo_change_mtu = eth_change_mtu,
  345. .ndo_validate_addr = eth_validate_addr,
  346. .ndo_set_mac_address = eth_mac_addr,
  347. };
  348. static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
  349. {
  350. int rc = 0;
  351. struct rionet_private *rnet;
  352. u16 device_id;
  353. rionet_active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
  354. mport->sys_size ? __fls(sizeof(void *)) + 4 : 0);
  355. if (!rionet_active) {
  356. rc = -ENOMEM;
  357. goto out;
  358. }
  359. memset((void *)rionet_active, 0, sizeof(void *) *
  360. RIO_MAX_ROUTE_ENTRIES(mport->sys_size));
  361. /* Set up private area */
  362. rnet = netdev_priv(ndev);
  363. rnet->mport = mport;
  364. /* Set the default MAC address */
  365. device_id = rio_local_get_device_id(mport);
  366. ndev->dev_addr[0] = 0x00;
  367. ndev->dev_addr[1] = 0x01;
  368. ndev->dev_addr[2] = 0x00;
  369. ndev->dev_addr[3] = 0x01;
  370. ndev->dev_addr[4] = device_id >> 8;
  371. ndev->dev_addr[5] = device_id & 0xff;
  372. ndev->netdev_ops = &rionet_netdev_ops;
  373. ndev->mtu = RIO_MAX_MSG_SIZE - 14;
  374. ndev->features = NETIF_F_LLTX;
  375. SET_ETHTOOL_OPS(ndev, &rionet_ethtool_ops);
  376. spin_lock_init(&rnet->lock);
  377. spin_lock_init(&rnet->tx_lock);
  378. rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
  379. rc = register_netdev(ndev);
  380. if (rc != 0)
  381. goto out;
  382. printk("%s: %s %s Version %s, MAC %pM\n",
  383. ndev->name,
  384. DRV_NAME,
  385. DRV_DESC,
  386. DRV_VERSION,
  387. ndev->dev_addr);
  388. out:
  389. return rc;
  390. }
  391. /*
  392. * XXX Make multi-net safe
  393. */
  394. static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
  395. {
  396. int rc = -ENODEV;
  397. u32 lsrc_ops, ldst_ops;
  398. struct rionet_peer *peer;
  399. struct net_device *ndev = NULL;
  400. /* If local device is not rionet capable, give up quickly */
  401. if (!rionet_capable)
  402. goto out;
  403. /* Allocate our net_device structure */
  404. ndev = alloc_etherdev(sizeof(struct rionet_private));
  405. if (ndev == NULL) {
  406. printk(KERN_INFO "%s: could not allocate ethernet device.\n",
  407. DRV_NAME);
  408. rc = -ENOMEM;
  409. goto out;
  410. }
  411. /*
  412. * First time through, make sure local device is rionet
  413. * capable, setup netdev, and set flags so this is skipped
  414. * on later probes
  415. */
  416. if (!rionet_check) {
  417. rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
  418. &lsrc_ops);
  419. rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
  420. &ldst_ops);
  421. if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
  422. printk(KERN_ERR
  423. "%s: local device is not network capable\n",
  424. DRV_NAME);
  425. rionet_check = 1;
  426. rionet_capable = 0;
  427. goto out;
  428. }
  429. rc = rionet_setup_netdev(rdev->net->hport, ndev);
  430. rionet_check = 1;
  431. }
  432. /*
  433. * If the remote device has mailbox/doorbell capabilities,
  434. * add it to the peer list.
  435. */
  436. if (dev_rionet_capable(rdev)) {
  437. if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
  438. rc = -ENOMEM;
  439. goto out;
  440. }
  441. peer->rdev = rdev;
  442. list_add_tail(&peer->node, &rionet_peers);
  443. }
  444. rio_set_drvdata(rdev, ndev);
  445. out:
  446. return rc;
  447. }
  448. static struct rio_device_id rionet_id_table[] = {
  449. {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
  450. };
  451. static struct rio_driver rionet_driver = {
  452. .name = "rionet",
  453. .id_table = rionet_id_table,
  454. .probe = rionet_probe,
  455. .remove = rionet_remove,
  456. };
  457. static int __init rionet_init(void)
  458. {
  459. return rio_register_driver(&rionet_driver);
  460. }
  461. static void __exit rionet_exit(void)
  462. {
  463. rio_unregister_driver(&rionet_driver);
  464. }
  465. late_initcall(rionet_init);
  466. module_exit(rionet_exit);