rionet.c 14 KB

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