rionet.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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(pef, src_ops, dst_ops) \
  70. ((pef & RIO_PEF_INB_MBOX) && \
  71. (pef & RIO_PEF_INB_DOORBELL) && \
  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->pef, dev->src_ops, dev->dst_ops)
  76. #define RIONET_MAC_MATCH(x) (*(u32 *)x == 0x00010001)
  77. #define RIONET_GET_DESTID(x) (*(u16 *)(x + 4))
  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 (is_multicast_ether_addr(eth->h_dest)) {
  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. u32 pwdcsr;
  233. struct rionet_private *rnet = netdev_priv(ndev);
  234. if (netif_msg_ifup(rnet))
  235. printk(KERN_INFO "%s: open\n", DRV_NAME);
  236. if ((rc = rio_request_inb_dbell(rnet->mport,
  237. (void *)ndev,
  238. RIONET_DOORBELL_JOIN,
  239. RIONET_DOORBELL_LEAVE,
  240. rionet_dbell_event)) < 0)
  241. goto out;
  242. if ((rc = rio_request_inb_mbox(rnet->mport,
  243. (void *)ndev,
  244. RIONET_MAILBOX,
  245. RIONET_RX_RING_SIZE,
  246. rionet_inb_msg_event)) < 0)
  247. goto out;
  248. if ((rc = rio_request_outb_mbox(rnet->mport,
  249. (void *)ndev,
  250. RIONET_MAILBOX,
  251. RIONET_TX_RING_SIZE,
  252. rionet_outb_msg_event)) < 0)
  253. goto out;
  254. /* Initialize inbound message ring */
  255. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  256. rnet->rx_skb[i] = NULL;
  257. rnet->rx_slot = 0;
  258. rionet_rx_fill(ndev, 0);
  259. rnet->tx_slot = 0;
  260. rnet->tx_cnt = 0;
  261. rnet->ack_slot = 0;
  262. netif_carrier_on(ndev);
  263. netif_start_queue(ndev);
  264. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  265. if (!(peer->res = rio_request_outb_dbell(peer->rdev,
  266. RIONET_DOORBELL_JOIN,
  267. RIONET_DOORBELL_LEAVE)))
  268. {
  269. printk(KERN_ERR "%s: error requesting doorbells\n",
  270. DRV_NAME);
  271. continue;
  272. }
  273. /*
  274. * If device has initialized inbound doorbells,
  275. * send a join message
  276. */
  277. rio_read_config_32(peer->rdev, RIO_WRITE_PORT_CSR, &pwdcsr);
  278. if (pwdcsr & RIO_DOORBELL_AVAIL)
  279. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
  280. }
  281. out:
  282. return rc;
  283. }
  284. static int rionet_close(struct net_device *ndev)
  285. {
  286. struct rionet_private *rnet = netdev_priv(ndev);
  287. struct rionet_peer *peer, *tmp;
  288. int i;
  289. if (netif_msg_ifup(rnet))
  290. printk(KERN_INFO "%s: close\n", DRV_NAME);
  291. netif_stop_queue(ndev);
  292. netif_carrier_off(ndev);
  293. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  294. kfree_skb(rnet->rx_skb[i]);
  295. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  296. if (rionet_active[peer->rdev->destid]) {
  297. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
  298. rionet_active[peer->rdev->destid] = NULL;
  299. }
  300. rio_release_outb_dbell(peer->rdev, peer->res);
  301. }
  302. rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
  303. RIONET_DOORBELL_LEAVE);
  304. rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
  305. rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
  306. return 0;
  307. }
  308. static void rionet_remove(struct rio_dev *rdev)
  309. {
  310. struct net_device *ndev = rio_get_drvdata(rdev);
  311. struct rionet_peer *peer, *tmp;
  312. free_pages((unsigned long)rionet_active, rdev->net->hport->sys_size ?
  313. __fls(sizeof(void *)) + 4 : 0);
  314. unregister_netdev(ndev);
  315. free_netdev(ndev);
  316. list_for_each_entry_safe(peer, tmp, &rionet_peers, node) {
  317. list_del(&peer->node);
  318. kfree(peer);
  319. }
  320. }
  321. static void rionet_get_drvinfo(struct net_device *ndev,
  322. struct ethtool_drvinfo *info)
  323. {
  324. struct rionet_private *rnet = netdev_priv(ndev);
  325. strcpy(info->driver, DRV_NAME);
  326. strcpy(info->version, DRV_VERSION);
  327. strcpy(info->fw_version, "n/a");
  328. strcpy(info->bus_info, rnet->mport->name);
  329. }
  330. static u32 rionet_get_msglevel(struct net_device *ndev)
  331. {
  332. struct rionet_private *rnet = netdev_priv(ndev);
  333. return rnet->msg_enable;
  334. }
  335. static void rionet_set_msglevel(struct net_device *ndev, u32 value)
  336. {
  337. struct rionet_private *rnet = netdev_priv(ndev);
  338. rnet->msg_enable = value;
  339. }
  340. static const struct ethtool_ops rionet_ethtool_ops = {
  341. .get_drvinfo = rionet_get_drvinfo,
  342. .get_msglevel = rionet_get_msglevel,
  343. .set_msglevel = rionet_set_msglevel,
  344. .get_link = ethtool_op_get_link,
  345. };
  346. static const struct net_device_ops rionet_netdev_ops = {
  347. .ndo_open = rionet_open,
  348. .ndo_stop = rionet_close,
  349. .ndo_start_xmit = rionet_start_xmit,
  350. .ndo_change_mtu = eth_change_mtu,
  351. .ndo_validate_addr = eth_validate_addr,
  352. .ndo_set_mac_address = eth_mac_addr,
  353. };
  354. static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
  355. {
  356. int rc = 0;
  357. struct rionet_private *rnet;
  358. u16 device_id;
  359. rionet_active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
  360. mport->sys_size ? __fls(sizeof(void *)) + 4 : 0);
  361. if (!rionet_active) {
  362. rc = -ENOMEM;
  363. goto out;
  364. }
  365. memset((void *)rionet_active, 0, sizeof(void *) *
  366. RIO_MAX_ROUTE_ENTRIES(mport->sys_size));
  367. /* Set up private area */
  368. rnet = netdev_priv(ndev);
  369. rnet->mport = mport;
  370. /* Set the default MAC address */
  371. device_id = rio_local_get_device_id(mport);
  372. ndev->dev_addr[0] = 0x00;
  373. ndev->dev_addr[1] = 0x01;
  374. ndev->dev_addr[2] = 0x00;
  375. ndev->dev_addr[3] = 0x01;
  376. ndev->dev_addr[4] = device_id >> 8;
  377. ndev->dev_addr[5] = device_id & 0xff;
  378. ndev->netdev_ops = &rionet_netdev_ops;
  379. ndev->mtu = RIO_MAX_MSG_SIZE - 14;
  380. ndev->features = NETIF_F_LLTX;
  381. SET_ETHTOOL_OPS(ndev, &rionet_ethtool_ops);
  382. spin_lock_init(&rnet->lock);
  383. spin_lock_init(&rnet->tx_lock);
  384. rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
  385. rc = register_netdev(ndev);
  386. if (rc != 0)
  387. goto out;
  388. printk("%s: %s %s Version %s, MAC %pM\n",
  389. ndev->name,
  390. DRV_NAME,
  391. DRV_DESC,
  392. DRV_VERSION,
  393. ndev->dev_addr);
  394. out:
  395. return rc;
  396. }
  397. /*
  398. * XXX Make multi-net safe
  399. */
  400. static int rionet_probe(struct rio_dev *rdev, const struct rio_device_id *id)
  401. {
  402. int rc = -ENODEV;
  403. u32 lpef, lsrc_ops, ldst_ops;
  404. struct rionet_peer *peer;
  405. struct net_device *ndev = NULL;
  406. /* If local device is not rionet capable, give up quickly */
  407. if (!rionet_capable)
  408. goto out;
  409. /* Allocate our net_device structure */
  410. ndev = alloc_etherdev(sizeof(struct rionet_private));
  411. if (ndev == NULL) {
  412. printk(KERN_INFO "%s: could not allocate ethernet device.\n",
  413. DRV_NAME);
  414. rc = -ENOMEM;
  415. goto out;
  416. }
  417. /*
  418. * First time through, make sure local device is rionet
  419. * capable, setup netdev, and set flags so this is skipped
  420. * on later probes
  421. */
  422. if (!rionet_check) {
  423. rio_local_read_config_32(rdev->net->hport, RIO_PEF_CAR, &lpef);
  424. rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
  425. &lsrc_ops);
  426. rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
  427. &ldst_ops);
  428. if (!is_rionet_capable(lpef, lsrc_ops, ldst_ops)) {
  429. printk(KERN_ERR
  430. "%s: local device is not network capable\n",
  431. DRV_NAME);
  432. rionet_check = 1;
  433. rionet_capable = 0;
  434. goto out;
  435. }
  436. rc = rionet_setup_netdev(rdev->net->hport, ndev);
  437. rionet_check = 1;
  438. }
  439. /*
  440. * If the remote device has mailbox/doorbell capabilities,
  441. * add it to the peer list.
  442. */
  443. if (dev_rionet_capable(rdev)) {
  444. if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) {
  445. rc = -ENOMEM;
  446. goto out;
  447. }
  448. peer->rdev = rdev;
  449. list_add_tail(&peer->node, &rionet_peers);
  450. }
  451. rio_set_drvdata(rdev, ndev);
  452. out:
  453. return rc;
  454. }
  455. static struct rio_device_id rionet_id_table[] = {
  456. {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}
  457. };
  458. static struct rio_driver rionet_driver = {
  459. .name = "rionet",
  460. .id_table = rionet_id_table,
  461. .probe = rionet_probe,
  462. .remove = rionet_remove,
  463. };
  464. static int __init rionet_init(void)
  465. {
  466. return rio_register_driver(&rionet_driver);
  467. }
  468. static void __exit rionet_exit(void)
  469. {
  470. rio_unregister_driver(&rionet_driver);
  471. }
  472. late_initcall(rionet_init);
  473. module_exit(rionet_exit);