rionet.c 19 KB

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