rionet.c 18 KB

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