rionet.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. #include <linux/reboot.h>
  26. #define DRV_NAME "rionet"
  27. #define DRV_VERSION "0.3"
  28. #define DRV_AUTHOR "Matt Porter <mporter@kernel.crashing.org>"
  29. #define DRV_DESC "Ethernet over RapidIO"
  30. MODULE_AUTHOR(DRV_AUTHOR);
  31. MODULE_DESCRIPTION(DRV_DESC);
  32. MODULE_LICENSE("GPL");
  33. #define RIONET_DEFAULT_MSGLEVEL \
  34. (NETIF_MSG_DRV | \
  35. NETIF_MSG_LINK | \
  36. NETIF_MSG_RX_ERR | \
  37. NETIF_MSG_TX_ERR)
  38. #define RIONET_DOORBELL_JOIN 0x1000
  39. #define RIONET_DOORBELL_LEAVE 0x1001
  40. #define RIONET_MAILBOX 0
  41. #define RIONET_TX_RING_SIZE CONFIG_RIONET_TX_SIZE
  42. #define RIONET_RX_RING_SIZE CONFIG_RIONET_RX_SIZE
  43. #define RIONET_MAX_NETS 8
  44. #define RIONET_MSG_SIZE RIO_MAX_MSG_SIZE
  45. #define RIONET_MAX_MTU (RIONET_MSG_SIZE - ETH_HLEN)
  46. struct rionet_private {
  47. struct rio_mport *mport;
  48. struct sk_buff *rx_skb[RIONET_RX_RING_SIZE];
  49. struct sk_buff *tx_skb[RIONET_TX_RING_SIZE];
  50. int rx_slot;
  51. int tx_slot;
  52. int tx_cnt;
  53. int ack_slot;
  54. spinlock_t lock;
  55. spinlock_t tx_lock;
  56. u32 msg_enable;
  57. bool open;
  58. };
  59. struct rionet_peer {
  60. struct list_head node;
  61. struct rio_dev *rdev;
  62. struct resource *res;
  63. };
  64. struct rionet_net {
  65. struct net_device *ndev;
  66. struct list_head peers;
  67. spinlock_t lock; /* net info access lock */
  68. struct rio_dev **active;
  69. int nact; /* number of active peers */
  70. };
  71. static struct rionet_net nets[RIONET_MAX_NETS];
  72. #define is_rionet_capable(src_ops, dst_ops) \
  73. ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
  74. (dst_ops & RIO_DST_OPS_DATA_MSG) && \
  75. (src_ops & RIO_SRC_OPS_DOORBELL) && \
  76. (dst_ops & RIO_DST_OPS_DOORBELL))
  77. #define dev_rionet_capable(dev) \
  78. is_rionet_capable(dev->src_ops, dev->dst_ops)
  79. #define RIONET_MAC_MATCH(x) (!memcmp((x), "\00\01\00\01", 4))
  80. #define RIONET_GET_DESTID(x) ((*((u8 *)x + 4) << 8) | *((u8 *)x + 5))
  81. static int rionet_rx_clean(struct net_device *ndev)
  82. {
  83. int i;
  84. int error = 0;
  85. struct rionet_private *rnet = netdev_priv(ndev);
  86. void *data;
  87. i = rnet->rx_slot;
  88. do {
  89. if (!rnet->rx_skb[i])
  90. continue;
  91. if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX)))
  92. break;
  93. rnet->rx_skb[i]->data = data;
  94. skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
  95. rnet->rx_skb[i]->protocol =
  96. eth_type_trans(rnet->rx_skb[i], ndev);
  97. error = netif_rx(rnet->rx_skb[i]);
  98. if (error == NET_RX_DROP) {
  99. ndev->stats.rx_dropped++;
  100. } else {
  101. ndev->stats.rx_packets++;
  102. ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE;
  103. }
  104. } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot);
  105. return i;
  106. }
  107. static void rionet_rx_fill(struct net_device *ndev, int end)
  108. {
  109. int i;
  110. struct rionet_private *rnet = netdev_priv(ndev);
  111. i = rnet->rx_slot;
  112. do {
  113. rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE);
  114. if (!rnet->rx_skb[i])
  115. break;
  116. rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX,
  117. rnet->rx_skb[i]->data);
  118. } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end);
  119. rnet->rx_slot = i;
  120. }
  121. static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev,
  122. struct rio_dev *rdev)
  123. {
  124. struct rionet_private *rnet = netdev_priv(ndev);
  125. rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len);
  126. rnet->tx_skb[rnet->tx_slot] = skb;
  127. ndev->stats.tx_packets++;
  128. ndev->stats.tx_bytes += skb->len;
  129. if (++rnet->tx_cnt == RIONET_TX_RING_SIZE)
  130. netif_stop_queue(ndev);
  131. ++rnet->tx_slot;
  132. rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1);
  133. if (netif_msg_tx_queued(rnet))
  134. printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME,
  135. skb->len);
  136. return 0;
  137. }
  138. static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  139. {
  140. int i;
  141. struct rionet_private *rnet = netdev_priv(ndev);
  142. struct ethhdr *eth = (struct ethhdr *)skb->data;
  143. u16 destid;
  144. unsigned long flags;
  145. int add_num = 1;
  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 (is_multicast_ether_addr(eth->h_dest))
  152. add_num = nets[rnet->mport->id].nact;
  153. if ((rnet->tx_cnt + add_num) > RIONET_TX_RING_SIZE) {
  154. netif_stop_queue(ndev);
  155. spin_unlock_irqrestore(&rnet->tx_lock, flags);
  156. printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
  157. ndev->name);
  158. return NETDEV_TX_BUSY;
  159. }
  160. if (is_multicast_ether_addr(eth->h_dest)) {
  161. int count = 0;
  162. for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size);
  163. i++)
  164. if (nets[rnet->mport->id].active[i]) {
  165. rionet_queue_tx_msg(skb, ndev,
  166. nets[rnet->mport->id].active[i]);
  167. if (count)
  168. atomic_inc(&skb->users);
  169. count++;
  170. }
  171. } else if (RIONET_MAC_MATCH(eth->h_dest)) {
  172. destid = RIONET_GET_DESTID(eth->h_dest);
  173. if (nets[rnet->mport->id].active[destid])
  174. rionet_queue_tx_msg(skb, ndev,
  175. nets[rnet->mport->id].active[destid]);
  176. else {
  177. /*
  178. * If the target device was removed from the list of
  179. * active peers but we still have TX packets targeting
  180. * it just report sending a packet to the target
  181. * (without actual packet transfer).
  182. */
  183. dev_kfree_skb_any(skb);
  184. ndev->stats.tx_packets++;
  185. ndev->stats.tx_bytes += skb->len;
  186. }
  187. }
  188. spin_unlock_irqrestore(&rnet->tx_lock, flags);
  189. return NETDEV_TX_OK;
  190. }
  191. static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid,
  192. u16 info)
  193. {
  194. struct net_device *ndev = dev_id;
  195. struct rionet_private *rnet = netdev_priv(ndev);
  196. struct rionet_peer *peer;
  197. unsigned char netid = rnet->mport->id;
  198. if (netif_msg_intr(rnet))
  199. printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x",
  200. DRV_NAME, sid, tid, info);
  201. if (info == RIONET_DOORBELL_JOIN) {
  202. if (!nets[netid].active[sid]) {
  203. spin_lock(&nets[netid].lock);
  204. list_for_each_entry(peer, &nets[netid].peers, node) {
  205. if (peer->rdev->destid == sid) {
  206. nets[netid].active[sid] = peer->rdev;
  207. nets[netid].nact++;
  208. }
  209. }
  210. spin_unlock(&nets[netid].lock);
  211. rio_mport_send_doorbell(mport, sid,
  212. RIONET_DOORBELL_JOIN);
  213. }
  214. } else if (info == RIONET_DOORBELL_LEAVE) {
  215. spin_lock(&nets[netid].lock);
  216. if (nets[netid].active[sid]) {
  217. nets[netid].active[sid] = NULL;
  218. nets[netid].nact--;
  219. }
  220. spin_unlock(&nets[netid].lock);
  221. } else {
  222. if (netif_msg_intr(rnet))
  223. printk(KERN_WARNING "%s: unhandled doorbell\n",
  224. DRV_NAME);
  225. }
  226. }
  227. static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
  228. {
  229. int n;
  230. struct net_device *ndev = dev_id;
  231. struct rionet_private *rnet = netdev_priv(ndev);
  232. if (netif_msg_intr(rnet))
  233. printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n",
  234. DRV_NAME, mbox, slot);
  235. spin_lock(&rnet->lock);
  236. if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot)
  237. rionet_rx_fill(ndev, n);
  238. spin_unlock(&rnet->lock);
  239. }
  240. static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot)
  241. {
  242. struct net_device *ndev = dev_id;
  243. struct rionet_private *rnet = netdev_priv(ndev);
  244. spin_lock(&rnet->tx_lock);
  245. if (netif_msg_intr(rnet))
  246. printk(KERN_INFO
  247. "%s: outbound message event, mbox %d slot %d\n",
  248. DRV_NAME, mbox, slot);
  249. while (rnet->tx_cnt && (rnet->ack_slot != slot)) {
  250. /* dma unmap single */
  251. dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]);
  252. rnet->tx_skb[rnet->ack_slot] = NULL;
  253. ++rnet->ack_slot;
  254. rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1);
  255. rnet->tx_cnt--;
  256. }
  257. if (rnet->tx_cnt < RIONET_TX_RING_SIZE)
  258. netif_wake_queue(ndev);
  259. spin_unlock(&rnet->tx_lock);
  260. }
  261. static int rionet_open(struct net_device *ndev)
  262. {
  263. int i, rc = 0;
  264. struct rionet_peer *peer;
  265. struct rionet_private *rnet = netdev_priv(ndev);
  266. unsigned char netid = rnet->mport->id;
  267. unsigned long flags;
  268. if (netif_msg_ifup(rnet))
  269. printk(KERN_INFO "%s: open\n", DRV_NAME);
  270. if ((rc = rio_request_inb_dbell(rnet->mport,
  271. (void *)ndev,
  272. RIONET_DOORBELL_JOIN,
  273. RIONET_DOORBELL_LEAVE,
  274. rionet_dbell_event)) < 0)
  275. goto out;
  276. if ((rc = rio_request_inb_mbox(rnet->mport,
  277. (void *)ndev,
  278. RIONET_MAILBOX,
  279. RIONET_RX_RING_SIZE,
  280. rionet_inb_msg_event)) < 0)
  281. goto out;
  282. if ((rc = rio_request_outb_mbox(rnet->mport,
  283. (void *)ndev,
  284. RIONET_MAILBOX,
  285. RIONET_TX_RING_SIZE,
  286. rionet_outb_msg_event)) < 0)
  287. goto out;
  288. /* Initialize inbound message ring */
  289. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  290. rnet->rx_skb[i] = NULL;
  291. rnet->rx_slot = 0;
  292. rionet_rx_fill(ndev, 0);
  293. rnet->tx_slot = 0;
  294. rnet->tx_cnt = 0;
  295. rnet->ack_slot = 0;
  296. netif_carrier_on(ndev);
  297. netif_start_queue(ndev);
  298. spin_lock_irqsave(&nets[netid].lock, flags);
  299. list_for_each_entry(peer, &nets[netid].peers, node) {
  300. /* Send a join message */
  301. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
  302. }
  303. spin_unlock_irqrestore(&nets[netid].lock, flags);
  304. rnet->open = true;
  305. out:
  306. return rc;
  307. }
  308. static int rionet_close(struct net_device *ndev)
  309. {
  310. struct rionet_private *rnet = netdev_priv(ndev);
  311. struct rionet_peer *peer;
  312. unsigned char netid = rnet->mport->id;
  313. unsigned long flags;
  314. int i;
  315. if (netif_msg_ifup(rnet))
  316. printk(KERN_INFO "%s: close %s\n", DRV_NAME, ndev->name);
  317. netif_stop_queue(ndev);
  318. netif_carrier_off(ndev);
  319. rnet->open = false;
  320. for (i = 0; i < RIONET_RX_RING_SIZE; i++)
  321. kfree_skb(rnet->rx_skb[i]);
  322. spin_lock_irqsave(&nets[netid].lock, flags);
  323. list_for_each_entry(peer, &nets[netid].peers, node) {
  324. if (nets[netid].active[peer->rdev->destid]) {
  325. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE);
  326. nets[netid].active[peer->rdev->destid] = NULL;
  327. }
  328. if (peer->res)
  329. rio_release_outb_dbell(peer->rdev, peer->res);
  330. }
  331. spin_unlock_irqrestore(&nets[netid].lock, flags);
  332. rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN,
  333. RIONET_DOORBELL_LEAVE);
  334. rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX);
  335. rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX);
  336. return 0;
  337. }
  338. static void rionet_remove_dev(struct device *dev, struct subsys_interface *sif)
  339. {
  340. struct rio_dev *rdev = to_rio_dev(dev);
  341. unsigned char netid = rdev->net->hport->id;
  342. struct rionet_peer *peer;
  343. int state, found = 0;
  344. unsigned long flags;
  345. if (!dev_rionet_capable(rdev))
  346. return;
  347. spin_lock_irqsave(&nets[netid].lock, flags);
  348. list_for_each_entry(peer, &nets[netid].peers, node) {
  349. if (peer->rdev == rdev) {
  350. list_del(&peer->node);
  351. if (nets[netid].active[rdev->destid]) {
  352. state = atomic_read(&rdev->state);
  353. if (state != RIO_DEVICE_GONE &&
  354. state != RIO_DEVICE_INITIALIZING) {
  355. rio_send_doorbell(rdev,
  356. RIONET_DOORBELL_LEAVE);
  357. }
  358. nets[netid].active[rdev->destid] = NULL;
  359. nets[netid].nact--;
  360. }
  361. found = 1;
  362. break;
  363. }
  364. }
  365. spin_unlock_irqrestore(&nets[netid].lock, flags);
  366. if (found) {
  367. if (peer->res)
  368. rio_release_outb_dbell(rdev, peer->res);
  369. kfree(peer);
  370. }
  371. }
  372. static void rionet_get_drvinfo(struct net_device *ndev,
  373. struct ethtool_drvinfo *info)
  374. {
  375. struct rionet_private *rnet = netdev_priv(ndev);
  376. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  377. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  378. strlcpy(info->fw_version, "n/a", sizeof(info->fw_version));
  379. strlcpy(info->bus_info, rnet->mport->name, sizeof(info->bus_info));
  380. }
  381. static u32 rionet_get_msglevel(struct net_device *ndev)
  382. {
  383. struct rionet_private *rnet = netdev_priv(ndev);
  384. return rnet->msg_enable;
  385. }
  386. static void rionet_set_msglevel(struct net_device *ndev, u32 value)
  387. {
  388. struct rionet_private *rnet = netdev_priv(ndev);
  389. rnet->msg_enable = value;
  390. }
  391. static int rionet_change_mtu(struct net_device *ndev, int new_mtu)
  392. {
  393. if ((new_mtu < 68) || (new_mtu > RIONET_MAX_MTU)) {
  394. printk(KERN_ERR "%s: Invalid MTU size %d\n",
  395. ndev->name, new_mtu);
  396. return -EINVAL;
  397. }
  398. ndev->mtu = new_mtu;
  399. return 0;
  400. }
  401. static const struct ethtool_ops rionet_ethtool_ops = {
  402. .get_drvinfo = rionet_get_drvinfo,
  403. .get_msglevel = rionet_get_msglevel,
  404. .set_msglevel = rionet_set_msglevel,
  405. .get_link = ethtool_op_get_link,
  406. };
  407. static const struct net_device_ops rionet_netdev_ops = {
  408. .ndo_open = rionet_open,
  409. .ndo_stop = rionet_close,
  410. .ndo_start_xmit = rionet_start_xmit,
  411. .ndo_change_mtu = rionet_change_mtu,
  412. .ndo_validate_addr = eth_validate_addr,
  413. .ndo_set_mac_address = eth_mac_addr,
  414. };
  415. static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
  416. {
  417. int rc = 0;
  418. struct rionet_private *rnet;
  419. u16 device_id;
  420. const size_t rionet_active_bytes = sizeof(void *) *
  421. RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
  422. nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
  423. get_order(rionet_active_bytes));
  424. if (!nets[mport->id].active) {
  425. rc = -ENOMEM;
  426. goto out;
  427. }
  428. memset((void *)nets[mport->id].active, 0, rionet_active_bytes);
  429. /* Set up private area */
  430. rnet = netdev_priv(ndev);
  431. rnet->mport = mport;
  432. rnet->open = false;
  433. /* Set the default MAC address */
  434. device_id = rio_local_get_device_id(mport);
  435. ndev->dev_addr[0] = 0x00;
  436. ndev->dev_addr[1] = 0x01;
  437. ndev->dev_addr[2] = 0x00;
  438. ndev->dev_addr[3] = 0x01;
  439. ndev->dev_addr[4] = device_id >> 8;
  440. ndev->dev_addr[5] = device_id & 0xff;
  441. ndev->netdev_ops = &rionet_netdev_ops;
  442. ndev->mtu = RIONET_MAX_MTU;
  443. ndev->features = NETIF_F_LLTX;
  444. SET_NETDEV_DEV(ndev, &mport->dev);
  445. ndev->ethtool_ops = &rionet_ethtool_ops;
  446. spin_lock_init(&rnet->lock);
  447. spin_lock_init(&rnet->tx_lock);
  448. rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
  449. rc = register_netdev(ndev);
  450. if (rc != 0) {
  451. free_pages((unsigned long)nets[mport->id].active,
  452. get_order(rionet_active_bytes));
  453. goto out;
  454. }
  455. printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n",
  456. ndev->name,
  457. DRV_NAME,
  458. DRV_DESC,
  459. DRV_VERSION,
  460. ndev->dev_addr,
  461. mport->name);
  462. out:
  463. return rc;
  464. }
  465. static int rionet_add_dev(struct device *dev, struct subsys_interface *sif)
  466. {
  467. int rc = -ENODEV;
  468. u32 lsrc_ops, ldst_ops;
  469. struct rionet_peer *peer;
  470. struct net_device *ndev = NULL;
  471. struct rio_dev *rdev = to_rio_dev(dev);
  472. unsigned char netid = rdev->net->hport->id;
  473. if (netid >= RIONET_MAX_NETS)
  474. return rc;
  475. /*
  476. * If first time through this net, make sure local device is rionet
  477. * capable and setup netdev (this step will be skipped in later probes
  478. * on the same net).
  479. */
  480. if (!nets[netid].ndev) {
  481. rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
  482. &lsrc_ops);
  483. rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
  484. &ldst_ops);
  485. if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
  486. printk(KERN_ERR
  487. "%s: local device %s is not network capable\n",
  488. DRV_NAME, rdev->net->hport->name);
  489. goto out;
  490. }
  491. /* Allocate our net_device structure */
  492. ndev = alloc_etherdev(sizeof(struct rionet_private));
  493. if (ndev == NULL) {
  494. rc = -ENOMEM;
  495. goto out;
  496. }
  497. rc = rionet_setup_netdev(rdev->net->hport, ndev);
  498. if (rc) {
  499. printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n",
  500. DRV_NAME, rc);
  501. free_netdev(ndev);
  502. goto out;
  503. }
  504. INIT_LIST_HEAD(&nets[netid].peers);
  505. spin_lock_init(&nets[netid].lock);
  506. nets[netid].nact = 0;
  507. nets[netid].ndev = ndev;
  508. }
  509. /*
  510. * If the remote device has mailbox/doorbell capabilities,
  511. * add it to the peer list.
  512. */
  513. if (dev_rionet_capable(rdev)) {
  514. struct rionet_private *rnet;
  515. unsigned long flags;
  516. rnet = netdev_priv(nets[netid].ndev);
  517. peer = kzalloc(sizeof(*peer), GFP_KERNEL);
  518. if (!peer) {
  519. rc = -ENOMEM;
  520. goto out;
  521. }
  522. peer->rdev = rdev;
  523. peer->res = rio_request_outb_dbell(peer->rdev,
  524. RIONET_DOORBELL_JOIN,
  525. RIONET_DOORBELL_LEAVE);
  526. if (!peer->res) {
  527. pr_err("%s: error requesting doorbells\n", DRV_NAME);
  528. kfree(peer);
  529. rc = -ENOMEM;
  530. goto out;
  531. }
  532. spin_lock_irqsave(&nets[netid].lock, flags);
  533. list_add_tail(&peer->node, &nets[netid].peers);
  534. spin_unlock_irqrestore(&nets[netid].lock, flags);
  535. pr_debug("%s: %s add peer %s\n",
  536. DRV_NAME, __func__, rio_name(rdev));
  537. /* If netdev is already opened, send join request to new peer */
  538. if (rnet->open)
  539. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
  540. }
  541. return 0;
  542. out:
  543. return rc;
  544. }
  545. static int rionet_shutdown(struct notifier_block *nb, unsigned long code,
  546. void *unused)
  547. {
  548. struct rionet_peer *peer;
  549. unsigned long flags;
  550. int i;
  551. pr_debug("%s: %s\n", DRV_NAME, __func__);
  552. for (i = 0; i < RIONET_MAX_NETS; i++) {
  553. if (!nets[i].ndev)
  554. continue;
  555. spin_lock_irqsave(&nets[i].lock, flags);
  556. list_for_each_entry(peer, &nets[i].peers, node) {
  557. if (nets[i].active[peer->rdev->destid]) {
  558. rio_send_doorbell(peer->rdev,
  559. RIONET_DOORBELL_LEAVE);
  560. nets[i].active[peer->rdev->destid] = NULL;
  561. }
  562. }
  563. spin_unlock_irqrestore(&nets[i].lock, flags);
  564. }
  565. return NOTIFY_DONE;
  566. }
  567. static void rionet_remove_mport(struct device *dev,
  568. struct class_interface *class_intf)
  569. {
  570. struct rio_mport *mport = to_rio_mport(dev);
  571. struct net_device *ndev;
  572. int id = mport->id;
  573. pr_debug("%s %s\n", __func__, mport->name);
  574. WARN(nets[id].nact, "%s called when connected to %d peers\n",
  575. __func__, nets[id].nact);
  576. WARN(!nets[id].ndev, "%s called for mport without NDEV\n",
  577. __func__);
  578. if (nets[id].ndev) {
  579. ndev = nets[id].ndev;
  580. netif_stop_queue(ndev);
  581. unregister_netdev(ndev);
  582. free_pages((unsigned long)nets[id].active,
  583. get_order(sizeof(void *) *
  584. RIO_MAX_ROUTE_ENTRIES(mport->sys_size)));
  585. nets[id].active = NULL;
  586. free_netdev(ndev);
  587. nets[id].ndev = NULL;
  588. }
  589. }
  590. #ifdef MODULE
  591. static struct rio_device_id rionet_id_table[] = {
  592. {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)},
  593. { 0, } /* terminate list */
  594. };
  595. MODULE_DEVICE_TABLE(rapidio, rionet_id_table);
  596. #endif
  597. static struct subsys_interface rionet_interface = {
  598. .name = "rionet",
  599. .subsys = &rio_bus_type,
  600. .add_dev = rionet_add_dev,
  601. .remove_dev = rionet_remove_dev,
  602. };
  603. static struct notifier_block rionet_notifier = {
  604. .notifier_call = rionet_shutdown,
  605. };
  606. /* the rio_mport_interface is used to handle local mport devices */
  607. static struct class_interface rio_mport_interface __refdata = {
  608. .class = &rio_mport_class,
  609. .add_dev = NULL,
  610. .remove_dev = rionet_remove_mport,
  611. };
  612. static int __init rionet_init(void)
  613. {
  614. int ret;
  615. ret = register_reboot_notifier(&rionet_notifier);
  616. if (ret) {
  617. pr_err("%s: failed to register reboot notifier (err=%d)\n",
  618. DRV_NAME, ret);
  619. return ret;
  620. }
  621. ret = class_interface_register(&rio_mport_interface);
  622. if (ret) {
  623. pr_err("%s: class_interface_register error: %d\n",
  624. DRV_NAME, ret);
  625. return ret;
  626. }
  627. return subsys_interface_register(&rionet_interface);
  628. }
  629. static void __exit rionet_exit(void)
  630. {
  631. unregister_reboot_notifier(&rionet_notifier);
  632. subsys_interface_unregister(&rionet_interface);
  633. class_interface_unregister(&rio_mport_interface);
  634. }
  635. late_initcall(rionet_init);
  636. module_exit(rionet_exit);