rionet.c 14 KB

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