rionet.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. atomic_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. dev_kfree_skb_any(skb);
  180. ndev->stats.tx_packets++;
  181. ndev->stats.tx_bytes += skb->len;
  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 int rionet_change_mtu(struct net_device *ndev, int new_mtu)
  388. {
  389. if ((new_mtu < 68) || (new_mtu > RIONET_MAX_MTU)) {
  390. printk(KERN_ERR "%s: Invalid MTU size %d\n",
  391. ndev->name, new_mtu);
  392. return -EINVAL;
  393. }
  394. ndev->mtu = new_mtu;
  395. return 0;
  396. }
  397. static const struct ethtool_ops rionet_ethtool_ops = {
  398. .get_drvinfo = rionet_get_drvinfo,
  399. .get_msglevel = rionet_get_msglevel,
  400. .set_msglevel = rionet_set_msglevel,
  401. .get_link = ethtool_op_get_link,
  402. };
  403. static const struct net_device_ops rionet_netdev_ops = {
  404. .ndo_open = rionet_open,
  405. .ndo_stop = rionet_close,
  406. .ndo_start_xmit = rionet_start_xmit,
  407. .ndo_change_mtu = rionet_change_mtu,
  408. .ndo_validate_addr = eth_validate_addr,
  409. .ndo_set_mac_address = eth_mac_addr,
  410. };
  411. static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev)
  412. {
  413. int rc = 0;
  414. struct rionet_private *rnet;
  415. u16 device_id;
  416. const size_t rionet_active_bytes = sizeof(void *) *
  417. RIO_MAX_ROUTE_ENTRIES(mport->sys_size);
  418. nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL,
  419. get_order(rionet_active_bytes));
  420. if (!nets[mport->id].active) {
  421. rc = -ENOMEM;
  422. goto out;
  423. }
  424. memset((void *)nets[mport->id].active, 0, rionet_active_bytes);
  425. /* Set up private area */
  426. rnet = netdev_priv(ndev);
  427. rnet->mport = mport;
  428. rnet->open = false;
  429. /* Set the default MAC address */
  430. device_id = rio_local_get_device_id(mport);
  431. ndev->dev_addr[0] = 0x00;
  432. ndev->dev_addr[1] = 0x01;
  433. ndev->dev_addr[2] = 0x00;
  434. ndev->dev_addr[3] = 0x01;
  435. ndev->dev_addr[4] = device_id >> 8;
  436. ndev->dev_addr[5] = device_id & 0xff;
  437. ndev->netdev_ops = &rionet_netdev_ops;
  438. ndev->mtu = RIONET_MAX_MTU;
  439. ndev->features = NETIF_F_LLTX;
  440. SET_NETDEV_DEV(ndev, &mport->dev);
  441. ndev->ethtool_ops = &rionet_ethtool_ops;
  442. spin_lock_init(&rnet->lock);
  443. spin_lock_init(&rnet->tx_lock);
  444. rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL;
  445. rc = register_netdev(ndev);
  446. if (rc != 0) {
  447. free_pages((unsigned long)nets[mport->id].active,
  448. get_order(rionet_active_bytes));
  449. goto out;
  450. }
  451. printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n",
  452. ndev->name,
  453. DRV_NAME,
  454. DRV_DESC,
  455. DRV_VERSION,
  456. ndev->dev_addr,
  457. mport->name);
  458. out:
  459. return rc;
  460. }
  461. static int rionet_add_dev(struct device *dev, struct subsys_interface *sif)
  462. {
  463. int rc = -ENODEV;
  464. u32 lsrc_ops, ldst_ops;
  465. struct rionet_peer *peer;
  466. struct net_device *ndev = NULL;
  467. struct rio_dev *rdev = to_rio_dev(dev);
  468. unsigned char netid = rdev->net->hport->id;
  469. if (netid >= RIONET_MAX_NETS)
  470. return rc;
  471. /*
  472. * If first time through this net, make sure local device is rionet
  473. * capable and setup netdev (this step will be skipped in later probes
  474. * on the same net).
  475. */
  476. if (!nets[netid].ndev) {
  477. rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR,
  478. &lsrc_ops);
  479. rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR,
  480. &ldst_ops);
  481. if (!is_rionet_capable(lsrc_ops, ldst_ops)) {
  482. printk(KERN_ERR
  483. "%s: local device %s is not network capable\n",
  484. DRV_NAME, rdev->net->hport->name);
  485. goto out;
  486. }
  487. /* Allocate our net_device structure */
  488. ndev = alloc_etherdev(sizeof(struct rionet_private));
  489. if (ndev == NULL) {
  490. rc = -ENOMEM;
  491. goto out;
  492. }
  493. rc = rionet_setup_netdev(rdev->net->hport, ndev);
  494. if (rc) {
  495. printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n",
  496. DRV_NAME, rc);
  497. free_netdev(ndev);
  498. goto out;
  499. }
  500. INIT_LIST_HEAD(&nets[netid].peers);
  501. spin_lock_init(&nets[netid].lock);
  502. nets[netid].nact = 0;
  503. nets[netid].ndev = ndev;
  504. }
  505. /*
  506. * If the remote device has mailbox/doorbell capabilities,
  507. * add it to the peer list.
  508. */
  509. if (dev_rionet_capable(rdev)) {
  510. struct rionet_private *rnet;
  511. unsigned long flags;
  512. rnet = netdev_priv(nets[netid].ndev);
  513. peer = kzalloc(sizeof(*peer), GFP_KERNEL);
  514. if (!peer) {
  515. rc = -ENOMEM;
  516. goto out;
  517. }
  518. peer->rdev = rdev;
  519. peer->res = rio_request_outb_dbell(peer->rdev,
  520. RIONET_DOORBELL_JOIN,
  521. RIONET_DOORBELL_LEAVE);
  522. if (!peer->res) {
  523. pr_err("%s: error requesting doorbells\n", DRV_NAME);
  524. kfree(peer);
  525. rc = -ENOMEM;
  526. goto out;
  527. }
  528. spin_lock_irqsave(&nets[netid].lock, flags);
  529. list_add_tail(&peer->node, &nets[netid].peers);
  530. spin_unlock_irqrestore(&nets[netid].lock, flags);
  531. pr_debug("%s: %s add peer %s\n",
  532. DRV_NAME, __func__, rio_name(rdev));
  533. /* If netdev is already opened, send join request to new peer */
  534. if (rnet->open)
  535. rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN);
  536. }
  537. return 0;
  538. out:
  539. return rc;
  540. }
  541. static int rionet_shutdown(struct notifier_block *nb, unsigned long code,
  542. void *unused)
  543. {
  544. struct rionet_peer *peer;
  545. unsigned long flags;
  546. int i;
  547. pr_debug("%s: %s\n", DRV_NAME, __func__);
  548. for (i = 0; i < RIONET_MAX_NETS; i++) {
  549. if (!nets[i].ndev)
  550. continue;
  551. spin_lock_irqsave(&nets[i].lock, flags);
  552. list_for_each_entry(peer, &nets[i].peers, node) {
  553. if (nets[i].active[peer->rdev->destid]) {
  554. rio_send_doorbell(peer->rdev,
  555. RIONET_DOORBELL_LEAVE);
  556. nets[i].active[peer->rdev->destid] = NULL;
  557. }
  558. }
  559. spin_unlock_irqrestore(&nets[i].lock, flags);
  560. }
  561. return NOTIFY_DONE;
  562. }
  563. static void rionet_remove_mport(struct device *dev,
  564. struct class_interface *class_intf)
  565. {
  566. struct rio_mport *mport = to_rio_mport(dev);
  567. struct net_device *ndev;
  568. int id = mport->id;
  569. pr_debug("%s %s\n", __func__, mport->name);
  570. WARN(nets[id].nact, "%s called when connected to %d peers\n",
  571. __func__, nets[id].nact);
  572. WARN(!nets[id].ndev, "%s called for mport without NDEV\n",
  573. __func__);
  574. if (nets[id].ndev) {
  575. ndev = nets[id].ndev;
  576. netif_stop_queue(ndev);
  577. unregister_netdev(ndev);
  578. free_pages((unsigned long)nets[id].active,
  579. get_order(sizeof(void *) *
  580. RIO_MAX_ROUTE_ENTRIES(mport->sys_size)));
  581. nets[id].active = NULL;
  582. free_netdev(ndev);
  583. nets[id].ndev = NULL;
  584. }
  585. }
  586. #ifdef MODULE
  587. static struct rio_device_id rionet_id_table[] = {
  588. {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)},
  589. { 0, } /* terminate list */
  590. };
  591. MODULE_DEVICE_TABLE(rapidio, rionet_id_table);
  592. #endif
  593. static struct subsys_interface rionet_interface = {
  594. .name = "rionet",
  595. .subsys = &rio_bus_type,
  596. .add_dev = rionet_add_dev,
  597. .remove_dev = rionet_remove_dev,
  598. };
  599. static struct notifier_block rionet_notifier = {
  600. .notifier_call = rionet_shutdown,
  601. };
  602. /* the rio_mport_interface is used to handle local mport devices */
  603. static struct class_interface rio_mport_interface __refdata = {
  604. .class = &rio_mport_class,
  605. .add_dev = NULL,
  606. .remove_dev = rionet_remove_mport,
  607. };
  608. static int __init rionet_init(void)
  609. {
  610. int ret;
  611. ret = register_reboot_notifier(&rionet_notifier);
  612. if (ret) {
  613. pr_err("%s: failed to register reboot notifier (err=%d)\n",
  614. DRV_NAME, ret);
  615. return ret;
  616. }
  617. ret = class_interface_register(&rio_mport_interface);
  618. if (ret) {
  619. pr_err("%s: class_interface_register error: %d\n",
  620. DRV_NAME, ret);
  621. return ret;
  622. }
  623. return subsys_interface_register(&rionet_interface);
  624. }
  625. static void __exit rionet_exit(void)
  626. {
  627. unregister_reboot_notifier(&rionet_notifier);
  628. subsys_interface_unregister(&rionet_interface);
  629. class_interface_unregister(&rio_mport_interface);
  630. }
  631. late_initcall(rionet_init);
  632. module_exit(rionet_exit);