rionet.c 14 KB

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