rionet.c 14 KB

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