scif_nodeqp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * Intel SCIF driver.
  16. *
  17. */
  18. #include "../bus/scif_bus.h"
  19. #include "scif_peer_bus.h"
  20. #include "scif_main.h"
  21. #include "scif_nodeqp.h"
  22. #include "scif_map.h"
  23. /*
  24. ************************************************************************
  25. * SCIF node Queue Pair (QP) setup flow:
  26. *
  27. * 1) SCIF driver gets probed with a scif_hw_dev via the scif_hw_bus
  28. * 2) scif_setup_qp(..) allocates the local qp and calls
  29. * scif_setup_qp_connect(..) which allocates and maps the local
  30. * buffer for the inbound QP
  31. * 3) The local node updates the device page with the DMA address of the QP
  32. * 4) A delayed work is scheduled (qp_dwork) which periodically reads if
  33. * the peer node has updated its QP DMA address
  34. * 5) Once a valid non zero address is found in the QP DMA address field
  35. * in the device page, the local node maps the remote node's QP,
  36. * updates its outbound QP and sends a SCIF_INIT message to the peer
  37. * 6) The SCIF_INIT message is received by the peer node QP interrupt bottom
  38. * half handler by calling scif_init(..)
  39. * 7) scif_init(..) registers a new SCIF peer node by calling
  40. * scif_peer_register_device(..) which signifies the addition of a new
  41. * SCIF node
  42. * 8) On the mgmt node, P2P network setup/teardown is initiated if all the
  43. * remote nodes are online via scif_p2p_setup(..)
  44. * 9) For P2P setup, the host maps the remote nodes' aperture and memory
  45. * bars and sends a SCIF_NODE_ADD message to both nodes
  46. * 10) As part of scif_nodeadd, both nodes set up their local inbound
  47. * QPs and send a SCIF_NODE_ADD_ACK to the mgmt node
  48. * 11) As part of scif_node_add_ack(..) the mgmt node forwards the
  49. * SCIF_NODE_ADD_ACK to the remote nodes
  50. * 12) As part of scif_node_add_ack(..) the remote nodes update their
  51. * outbound QPs, make sure they can access memory on the remote node
  52. * and then add a new SCIF peer node by calling
  53. * scif_peer_register_device(..) which signifies the addition of a new
  54. * SCIF node.
  55. * 13) The SCIF network is now established across all nodes.
  56. *
  57. ************************************************************************
  58. * SCIF node QP teardown flow (initiated by non mgmt node):
  59. *
  60. * 1) SCIF driver gets a remove callback with a scif_hw_dev via the scif_hw_bus
  61. * 2) The device page QP DMA address field is updated with 0x0
  62. * 3) A non mgmt node now cleans up all local data structures and sends a
  63. * SCIF_EXIT message to the peer and waits for a SCIF_EXIT_ACK
  64. * 4) As part of scif_exit(..) handling scif_disconnect_node(..) is called
  65. * 5) scif_disconnect_node(..) sends a SCIF_NODE_REMOVE message to all the
  66. * peers and waits for a SCIF_NODE_REMOVE_ACK
  67. * 6) As part of scif_node_remove(..) a remote node unregisters the peer
  68. * node from the SCIF network and sends a SCIF_NODE_REMOVE_ACK
  69. * 7) When the mgmt node has received all the SCIF_NODE_REMOVE_ACKs
  70. * it sends itself a node remove message whose handling cleans up local
  71. * data structures and unregisters the peer node from the SCIF network
  72. * 8) The mgmt node sends a SCIF_EXIT_ACK
  73. * 9) Upon receipt of the SCIF_EXIT_ACK the node initiating the teardown
  74. * completes the SCIF remove routine
  75. * 10) The SCIF network is now torn down for the node initiating the
  76. * teardown sequence
  77. *
  78. ************************************************************************
  79. * SCIF node QP teardown flow (initiated by mgmt node):
  80. *
  81. * 1) SCIF driver gets a remove callback with a scif_hw_dev via the scif_hw_bus
  82. * 2) The device page QP DMA address field is updated with 0x0
  83. * 3) The mgmt node calls scif_disconnect_node(..)
  84. * 4) scif_disconnect_node(..) sends a SCIF_NODE_REMOVE message to all the peers
  85. * and waits for a SCIF_NODE_REMOVE_ACK
  86. * 5) As part of scif_node_remove(..) a remote node unregisters the peer
  87. * node from the SCIF network and sends a SCIF_NODE_REMOVE_ACK
  88. * 6) When the mgmt node has received all the SCIF_NODE_REMOVE_ACKs
  89. * it unregisters the peer node from the SCIF network
  90. * 7) The mgmt node sends a SCIF_EXIT message and waits for a SCIF_EXIT_ACK.
  91. * 8) A non mgmt node upon receipt of a SCIF_EXIT message calls scif_stop(..)
  92. * which would clean up local data structures for all SCIF nodes and
  93. * then send a SCIF_EXIT_ACK back to the mgmt node
  94. * 9) Upon receipt of the SCIF_EXIT_ACK the the mgmt node sends itself a node
  95. * remove message whose handling cleans up local data structures and
  96. * destroys any P2P mappings.
  97. * 10) The SCIF hardware device for which a remove callback was received is now
  98. * disconnected from the SCIF network.
  99. */
  100. /*
  101. * Initializes "local" data structures for the QP. Allocates the QP
  102. * ring buffer (rb) and initializes the "in bound" queue.
  103. */
  104. int scif_setup_qp_connect(struct scif_qp *qp, dma_addr_t *qp_offset,
  105. int local_size, struct scif_dev *scifdev)
  106. {
  107. void *local_q = qp->inbound_q.rb_base;
  108. int err = 0;
  109. u32 tmp_rd = 0;
  110. spin_lock_init(&qp->send_lock);
  111. spin_lock_init(&qp->recv_lock);
  112. /* Allocate rb only if not already allocated */
  113. if (!local_q) {
  114. local_q = kzalloc(local_size, GFP_KERNEL);
  115. if (!local_q) {
  116. err = -ENOMEM;
  117. return err;
  118. }
  119. }
  120. err = scif_map_single(&qp->local_buf, local_q, scifdev, local_size);
  121. if (err)
  122. goto kfree;
  123. /*
  124. * To setup the inbound_q, the buffer lives locally, the read pointer
  125. * is remote and the write pointer is local.
  126. */
  127. scif_rb_init(&qp->inbound_q,
  128. &tmp_rd,
  129. &qp->local_write,
  130. local_q, get_count_order(local_size));
  131. /*
  132. * The read pointer is NULL initially and it is unsafe to use the ring
  133. * buffer til this changes!
  134. */
  135. qp->inbound_q.read_ptr = NULL;
  136. err = scif_map_single(qp_offset, qp,
  137. scifdev, sizeof(struct scif_qp));
  138. if (err)
  139. goto unmap;
  140. qp->local_qp = *qp_offset;
  141. return err;
  142. unmap:
  143. scif_unmap_single(qp->local_buf, scifdev, local_size);
  144. qp->local_buf = 0;
  145. kfree:
  146. kfree(local_q);
  147. return err;
  148. }
  149. /* When the other side has already done it's allocation, this is called */
  150. int scif_setup_qp_accept(struct scif_qp *qp, dma_addr_t *qp_offset,
  151. dma_addr_t phys, int local_size,
  152. struct scif_dev *scifdev)
  153. {
  154. void *local_q;
  155. void *remote_q;
  156. struct scif_qp *remote_qp;
  157. int remote_size;
  158. int err = 0;
  159. spin_lock_init(&qp->send_lock);
  160. spin_lock_init(&qp->recv_lock);
  161. /* Start by figuring out where we need to point */
  162. remote_qp = scif_ioremap(phys, sizeof(struct scif_qp), scifdev);
  163. if (!remote_qp)
  164. return -EIO;
  165. qp->remote_qp = remote_qp;
  166. if (qp->remote_qp->magic != SCIFEP_MAGIC) {
  167. err = -EIO;
  168. goto iounmap;
  169. }
  170. qp->remote_buf = remote_qp->local_buf;
  171. remote_size = qp->remote_qp->inbound_q.size;
  172. remote_q = scif_ioremap(qp->remote_buf, remote_size, scifdev);
  173. if (!remote_q) {
  174. err = -EIO;
  175. goto iounmap;
  176. }
  177. qp->remote_qp->local_write = 0;
  178. /*
  179. * To setup the outbound_q, the buffer lives in remote memory,
  180. * the read pointer is local, the write pointer is remote
  181. */
  182. scif_rb_init(&qp->outbound_q,
  183. &qp->local_read,
  184. &qp->remote_qp->local_write,
  185. remote_q,
  186. get_count_order(remote_size));
  187. local_q = kzalloc(local_size, GFP_KERNEL);
  188. if (!local_q) {
  189. err = -ENOMEM;
  190. goto iounmap_1;
  191. }
  192. err = scif_map_single(&qp->local_buf, local_q, scifdev, local_size);
  193. if (err)
  194. goto kfree;
  195. qp->remote_qp->local_read = 0;
  196. /*
  197. * To setup the inbound_q, the buffer lives locally, the read pointer
  198. * is remote and the write pointer is local
  199. */
  200. scif_rb_init(&qp->inbound_q,
  201. &qp->remote_qp->local_read,
  202. &qp->local_write,
  203. local_q, get_count_order(local_size));
  204. err = scif_map_single(qp_offset, qp, scifdev,
  205. sizeof(struct scif_qp));
  206. if (err)
  207. goto unmap;
  208. qp->local_qp = *qp_offset;
  209. return err;
  210. unmap:
  211. scif_unmap_single(qp->local_buf, scifdev, local_size);
  212. qp->local_buf = 0;
  213. kfree:
  214. kfree(local_q);
  215. iounmap_1:
  216. scif_iounmap(remote_q, remote_size, scifdev);
  217. qp->outbound_q.rb_base = NULL;
  218. iounmap:
  219. scif_iounmap(qp->remote_qp, sizeof(struct scif_qp), scifdev);
  220. qp->remote_qp = NULL;
  221. return err;
  222. }
  223. int scif_setup_qp_connect_response(struct scif_dev *scifdev,
  224. struct scif_qp *qp, u64 payload)
  225. {
  226. int err = 0;
  227. void *r_buf;
  228. int remote_size;
  229. phys_addr_t tmp_phys;
  230. qp->remote_qp = scif_ioremap(payload, sizeof(struct scif_qp), scifdev);
  231. if (!qp->remote_qp) {
  232. err = -ENOMEM;
  233. goto error;
  234. }
  235. if (qp->remote_qp->magic != SCIFEP_MAGIC) {
  236. dev_err(&scifdev->sdev->dev,
  237. "SCIFEP_MAGIC mismatch between self %d remote %d\n",
  238. scif_dev[scif_info.nodeid].node, scifdev->node);
  239. err = -ENODEV;
  240. goto error;
  241. }
  242. tmp_phys = qp->remote_qp->local_buf;
  243. remote_size = qp->remote_qp->inbound_q.size;
  244. r_buf = scif_ioremap(tmp_phys, remote_size, scifdev);
  245. if (!r_buf)
  246. return -EIO;
  247. qp->local_read = 0;
  248. scif_rb_init(&qp->outbound_q,
  249. &qp->local_read,
  250. &qp->remote_qp->local_write,
  251. r_buf,
  252. get_count_order(remote_size));
  253. /*
  254. * Because the node QP may already be processing an INIT message, set
  255. * the read pointer so the cached read offset isn't lost
  256. */
  257. qp->remote_qp->local_read = qp->inbound_q.current_read_offset;
  258. /*
  259. * resetup the inbound_q now that we know where the
  260. * inbound_read really is.
  261. */
  262. scif_rb_init(&qp->inbound_q,
  263. &qp->remote_qp->local_read,
  264. &qp->local_write,
  265. qp->inbound_q.rb_base,
  266. get_count_order(qp->inbound_q.size));
  267. error:
  268. return err;
  269. }
  270. static __always_inline void
  271. scif_send_msg_intr(struct scif_dev *scifdev)
  272. {
  273. struct scif_hw_dev *sdev = scifdev->sdev;
  274. if (scifdev_is_p2p(scifdev))
  275. sdev->hw_ops->send_p2p_intr(sdev, scifdev->rdb, &scifdev->mmio);
  276. else
  277. sdev->hw_ops->send_intr(sdev, scifdev->rdb);
  278. }
  279. int scif_qp_response(phys_addr_t phys, struct scif_dev *scifdev)
  280. {
  281. int err = 0;
  282. struct scifmsg msg;
  283. err = scif_setup_qp_connect_response(scifdev, scifdev->qpairs, phys);
  284. if (!err) {
  285. /*
  286. * Now that everything is setup and mapped, we're ready
  287. * to tell the peer about our queue's location
  288. */
  289. msg.uop = SCIF_INIT;
  290. msg.dst.node = scifdev->node;
  291. err = scif_nodeqp_send(scifdev, &msg);
  292. }
  293. return err;
  294. }
  295. void scif_send_exit(struct scif_dev *scifdev)
  296. {
  297. struct scifmsg msg;
  298. int ret;
  299. scifdev->exit = OP_IN_PROGRESS;
  300. msg.uop = SCIF_EXIT;
  301. msg.src.node = scif_info.nodeid;
  302. msg.dst.node = scifdev->node;
  303. ret = scif_nodeqp_send(scifdev, &msg);
  304. if (ret)
  305. goto done;
  306. /* Wait for a SCIF_EXIT_ACK message */
  307. wait_event_timeout(scif_info.exitwq, scifdev->exit == OP_COMPLETED,
  308. SCIF_NODE_ALIVE_TIMEOUT);
  309. done:
  310. scifdev->exit = OP_IDLE;
  311. }
  312. int scif_setup_qp(struct scif_dev *scifdev)
  313. {
  314. int err = 0;
  315. int local_size;
  316. struct scif_qp *qp;
  317. local_size = SCIF_NODE_QP_SIZE;
  318. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  319. if (!qp) {
  320. err = -ENOMEM;
  321. return err;
  322. }
  323. qp->magic = SCIFEP_MAGIC;
  324. scifdev->qpairs = qp;
  325. err = scif_setup_qp_connect(qp, &scifdev->qp_dma_addr,
  326. local_size, scifdev);
  327. if (err)
  328. goto free_qp;
  329. /*
  330. * We're as setup as we can be. The inbound_q is setup, w/o a usable
  331. * outbound q. When we get a message, the read_ptr will be updated,
  332. * and we will pull the message.
  333. */
  334. return err;
  335. free_qp:
  336. kfree(scifdev->qpairs);
  337. scifdev->qpairs = NULL;
  338. return err;
  339. }
  340. static void scif_p2p_freesg(struct scatterlist *sg)
  341. {
  342. kfree(sg);
  343. }
  344. static struct scatterlist *
  345. scif_p2p_setsg(phys_addr_t pa, int page_size, int page_cnt)
  346. {
  347. struct scatterlist *sg;
  348. struct page *page;
  349. int i;
  350. sg = kcalloc(page_cnt, sizeof(struct scatterlist), GFP_KERNEL);
  351. if (!sg)
  352. return NULL;
  353. sg_init_table(sg, page_cnt);
  354. for (i = 0; i < page_cnt; i++) {
  355. page = pfn_to_page(pa >> PAGE_SHIFT);
  356. sg_set_page(&sg[i], page, page_size, 0);
  357. pa += page_size;
  358. }
  359. return sg;
  360. }
  361. /* Init p2p mappings required to access peerdev from scifdev */
  362. static struct scif_p2p_info *
  363. scif_init_p2p_info(struct scif_dev *scifdev, struct scif_dev *peerdev)
  364. {
  365. struct scif_p2p_info *p2p;
  366. int num_mmio_pages, num_aper_pages, sg_page_shift, err, num_aper_chunks;
  367. struct scif_hw_dev *psdev = peerdev->sdev;
  368. struct scif_hw_dev *sdev = scifdev->sdev;
  369. num_mmio_pages = psdev->mmio->len >> PAGE_SHIFT;
  370. num_aper_pages = psdev->aper->len >> PAGE_SHIFT;
  371. p2p = kzalloc(sizeof(*p2p), GFP_KERNEL);
  372. if (!p2p)
  373. return NULL;
  374. p2p->ppi_sg[SCIF_PPI_MMIO] = scif_p2p_setsg(psdev->mmio->pa,
  375. PAGE_SIZE, num_mmio_pages);
  376. if (!p2p->ppi_sg[SCIF_PPI_MMIO])
  377. goto free_p2p;
  378. p2p->sg_nentries[SCIF_PPI_MMIO] = num_mmio_pages;
  379. sg_page_shift = get_order(min(psdev->aper->len, (u64)(1 << 30)));
  380. num_aper_chunks = num_aper_pages >> (sg_page_shift - PAGE_SHIFT);
  381. p2p->ppi_sg[SCIF_PPI_APER] = scif_p2p_setsg(psdev->aper->pa,
  382. 1 << sg_page_shift,
  383. num_aper_chunks);
  384. p2p->sg_nentries[SCIF_PPI_APER] = num_aper_chunks;
  385. err = dma_map_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
  386. num_mmio_pages, PCI_DMA_BIDIRECTIONAL);
  387. if (err != num_mmio_pages)
  388. goto scif_p2p_free;
  389. err = dma_map_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_APER],
  390. num_aper_chunks, PCI_DMA_BIDIRECTIONAL);
  391. if (err != num_aper_chunks)
  392. goto dma_unmap;
  393. p2p->ppi_da[SCIF_PPI_MMIO] = sg_dma_address(p2p->ppi_sg[SCIF_PPI_MMIO]);
  394. p2p->ppi_da[SCIF_PPI_APER] = sg_dma_address(p2p->ppi_sg[SCIF_PPI_APER]);
  395. p2p->ppi_len[SCIF_PPI_MMIO] = num_mmio_pages;
  396. p2p->ppi_len[SCIF_PPI_APER] = num_aper_pages;
  397. p2p->ppi_peer_id = peerdev->node;
  398. return p2p;
  399. dma_unmap:
  400. dma_unmap_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
  401. p2p->sg_nentries[SCIF_PPI_MMIO], DMA_BIDIRECTIONAL);
  402. scif_p2p_free:
  403. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
  404. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
  405. free_p2p:
  406. kfree(p2p);
  407. return NULL;
  408. }
  409. /* Uninitialize and release resources from a p2p mapping */
  410. static void scif_deinit_p2p_info(struct scif_dev *scifdev,
  411. struct scif_p2p_info *p2p)
  412. {
  413. struct scif_hw_dev *sdev = scifdev->sdev;
  414. dma_unmap_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
  415. p2p->sg_nentries[SCIF_PPI_MMIO], DMA_BIDIRECTIONAL);
  416. dma_unmap_sg(&sdev->dev, p2p->ppi_sg[SCIF_PPI_APER],
  417. p2p->sg_nentries[SCIF_PPI_APER], DMA_BIDIRECTIONAL);
  418. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
  419. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
  420. kfree(p2p);
  421. }
  422. /**
  423. * scif_node_connect: Respond to SCIF_NODE_CONNECT interrupt message
  424. * @dst: Destination node
  425. *
  426. * Connect the src and dst node by setting up the p2p connection
  427. * between them. Management node here acts like a proxy.
  428. */
  429. static void scif_node_connect(struct scif_dev *scifdev, int dst)
  430. {
  431. struct scif_dev *dev_j = scifdev;
  432. struct scif_dev *dev_i = NULL;
  433. struct scif_p2p_info *p2p_ij = NULL; /* bus addr for j from i */
  434. struct scif_p2p_info *p2p_ji = NULL; /* bus addr for i from j */
  435. struct scif_p2p_info *p2p;
  436. struct list_head *pos, *tmp;
  437. struct scifmsg msg;
  438. int err;
  439. u64 tmppayload;
  440. if (dst < 1 || dst > scif_info.maxid)
  441. return;
  442. dev_i = &scif_dev[dst];
  443. if (!_scifdev_alive(dev_i))
  444. return;
  445. /*
  446. * If the p2p connection is already setup or in the process of setting
  447. * up then just ignore this request. The requested node will get
  448. * informed by SCIF_NODE_ADD_ACK or SCIF_NODE_ADD_NACK
  449. */
  450. if (!list_empty(&dev_i->p2p)) {
  451. list_for_each_safe(pos, tmp, &dev_i->p2p) {
  452. p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
  453. if (p2p->ppi_peer_id == dev_j->node)
  454. return;
  455. }
  456. }
  457. p2p_ij = scif_init_p2p_info(dev_i, dev_j);
  458. if (!p2p_ij)
  459. return;
  460. p2p_ji = scif_init_p2p_info(dev_j, dev_i);
  461. if (!p2p_ji) {
  462. scif_deinit_p2p_info(dev_i, p2p_ij);
  463. return;
  464. }
  465. list_add_tail(&p2p_ij->ppi_list, &dev_i->p2p);
  466. list_add_tail(&p2p_ji->ppi_list, &dev_j->p2p);
  467. /*
  468. * Send a SCIF_NODE_ADD to dev_i, pass it its bus address
  469. * as seen from dev_j
  470. */
  471. msg.uop = SCIF_NODE_ADD;
  472. msg.src.node = dev_j->node;
  473. msg.dst.node = dev_i->node;
  474. msg.payload[0] = p2p_ji->ppi_da[SCIF_PPI_APER];
  475. msg.payload[1] = p2p_ij->ppi_da[SCIF_PPI_MMIO];
  476. msg.payload[2] = p2p_ij->ppi_da[SCIF_PPI_APER];
  477. msg.payload[3] = p2p_ij->ppi_len[SCIF_PPI_APER] << PAGE_SHIFT;
  478. err = scif_nodeqp_send(dev_i, &msg);
  479. if (err) {
  480. dev_err(&scifdev->sdev->dev,
  481. "%s %d error %d\n", __func__, __LINE__, err);
  482. return;
  483. }
  484. /* Same as above but to dev_j */
  485. msg.uop = SCIF_NODE_ADD;
  486. msg.src.node = dev_i->node;
  487. msg.dst.node = dev_j->node;
  488. tmppayload = msg.payload[0];
  489. msg.payload[0] = msg.payload[2];
  490. msg.payload[2] = tmppayload;
  491. msg.payload[1] = p2p_ji->ppi_da[SCIF_PPI_MMIO];
  492. msg.payload[3] = p2p_ji->ppi_len[SCIF_PPI_APER] << PAGE_SHIFT;
  493. scif_nodeqp_send(dev_j, &msg);
  494. }
  495. static void scif_p2p_setup(void)
  496. {
  497. int i, j;
  498. if (!scif_info.p2p_enable)
  499. return;
  500. for (i = 1; i <= scif_info.maxid; i++)
  501. if (!_scifdev_alive(&scif_dev[i]))
  502. return;
  503. for (i = 1; i <= scif_info.maxid; i++) {
  504. for (j = 1; j <= scif_info.maxid; j++) {
  505. struct scif_dev *scifdev = &scif_dev[i];
  506. if (i == j)
  507. continue;
  508. scif_node_connect(scifdev, j);
  509. }
  510. }
  511. }
  512. static char *message_types[] = {"BAD",
  513. "INIT",
  514. "EXIT",
  515. "SCIF_EXIT_ACK",
  516. "SCIF_NODE_ADD",
  517. "SCIF_NODE_ADD_ACK",
  518. "SCIF_NODE_ADD_NACK",
  519. "REMOVE_NODE",
  520. "REMOVE_NODE_ACK",
  521. "CNCT_REQ",
  522. "CNCT_GNT",
  523. "CNCT_GNTACK",
  524. "CNCT_GNTNACK",
  525. "CNCT_REJ",
  526. "DISCNCT",
  527. "DISCNT_ACK",
  528. "CLIENT_SENT",
  529. "CLIENT_RCVD",
  530. "SCIF_GET_NODE_INFO",
  531. "REGISTER",
  532. "REGISTER_ACK",
  533. "REGISTER_NACK",
  534. "UNREGISTER",
  535. "UNREGISTER_ACK",
  536. "UNREGISTER_NACK",
  537. "ALLOC_REQ",
  538. "ALLOC_GNT",
  539. "ALLOC_REJ",
  540. "FREE_PHYS",
  541. "FREE_VIRT",
  542. "MUNMAP",
  543. "MARK",
  544. "MARK_ACK",
  545. "MARK_NACK",
  546. "WAIT",
  547. "WAIT_ACK",
  548. "WAIT_NACK",
  549. "SIGNAL_LOCAL",
  550. "SIGNAL_REMOTE",
  551. "SIG_ACK",
  552. "SIG_NACK"};
  553. static void
  554. scif_display_message(struct scif_dev *scifdev, struct scifmsg *msg,
  555. const char *label)
  556. {
  557. if (!scif_info.en_msg_log)
  558. return;
  559. if (msg->uop > SCIF_MAX_MSG) {
  560. dev_err(&scifdev->sdev->dev,
  561. "%s: unknown msg type %d\n", label, msg->uop);
  562. return;
  563. }
  564. dev_info(&scifdev->sdev->dev,
  565. "%s: msg type %s, src %d:%d, dest %d:%d payload 0x%llx:0x%llx:0x%llx:0x%llx\n",
  566. label, message_types[msg->uop], msg->src.node, msg->src.port,
  567. msg->dst.node, msg->dst.port, msg->payload[0], msg->payload[1],
  568. msg->payload[2], msg->payload[3]);
  569. }
  570. int _scif_nodeqp_send(struct scif_dev *scifdev, struct scifmsg *msg)
  571. {
  572. struct scif_qp *qp = scifdev->qpairs;
  573. int err = -ENOMEM, loop_cnt = 0;
  574. scif_display_message(scifdev, msg, "Sent");
  575. if (!qp) {
  576. err = -EINVAL;
  577. goto error;
  578. }
  579. spin_lock(&qp->send_lock);
  580. while ((err = scif_rb_write(&qp->outbound_q,
  581. msg, sizeof(struct scifmsg)))) {
  582. mdelay(1);
  583. #define SCIF_NODEQP_SEND_TO_MSEC (3 * 1000)
  584. if (loop_cnt++ > (SCIF_NODEQP_SEND_TO_MSEC)) {
  585. err = -ENODEV;
  586. break;
  587. }
  588. }
  589. if (!err)
  590. scif_rb_commit(&qp->outbound_q);
  591. spin_unlock(&qp->send_lock);
  592. if (!err) {
  593. if (scifdev_self(scifdev))
  594. /*
  595. * For loopback we need to emulate an interrupt by
  596. * queuing work for the queue handling real node
  597. * Qp interrupts.
  598. */
  599. queue_work(scifdev->intr_wq, &scifdev->intr_bh);
  600. else
  601. scif_send_msg_intr(scifdev);
  602. }
  603. error:
  604. if (err)
  605. dev_dbg(&scifdev->sdev->dev,
  606. "%s %d error %d uop %d\n",
  607. __func__, __LINE__, err, msg->uop);
  608. return err;
  609. }
  610. /**
  611. * scif_nodeqp_send - Send a message on the node queue pair
  612. * @scifdev: Scif Device.
  613. * @msg: The message to be sent.
  614. */
  615. int scif_nodeqp_send(struct scif_dev *scifdev, struct scifmsg *msg)
  616. {
  617. int err;
  618. struct device *spdev = NULL;
  619. if (msg->uop > SCIF_EXIT_ACK) {
  620. /* Dont send messages once the exit flow has begun */
  621. if (OP_IDLE != scifdev->exit)
  622. return -ENODEV;
  623. spdev = scif_get_peer_dev(scifdev);
  624. if (IS_ERR(spdev)) {
  625. err = PTR_ERR(spdev);
  626. return err;
  627. }
  628. }
  629. err = _scif_nodeqp_send(scifdev, msg);
  630. if (msg->uop > SCIF_EXIT_ACK)
  631. scif_put_peer_dev(spdev);
  632. return err;
  633. }
  634. /*
  635. * scif_misc_handler:
  636. *
  637. * Work queue handler for servicing miscellaneous SCIF tasks.
  638. * Examples include:
  639. * 1) Remote fence requests.
  640. * 2) Destruction of temporary registered windows
  641. * created during scif_vreadfrom()/scif_vwriteto().
  642. * 3) Cleanup of zombie endpoints.
  643. */
  644. void scif_misc_handler(struct work_struct *work)
  645. {
  646. scif_rma_handle_remote_fences();
  647. scif_rma_destroy_windows();
  648. scif_rma_destroy_tcw_invalid();
  649. scif_cleanup_zombie_epd();
  650. }
  651. /**
  652. * scif_init() - Respond to SCIF_INIT interrupt message
  653. * @scifdev: Remote SCIF device node
  654. * @msg: Interrupt message
  655. */
  656. static __always_inline void
  657. scif_init(struct scif_dev *scifdev, struct scifmsg *msg)
  658. {
  659. /*
  660. * Allow the thread waiting for device page updates for the peer QP DMA
  661. * address to complete initializing the inbound_q.
  662. */
  663. flush_delayed_work(&scifdev->qp_dwork);
  664. scif_peer_register_device(scifdev);
  665. if (scif_is_mgmt_node()) {
  666. mutex_lock(&scif_info.conflock);
  667. scif_p2p_setup();
  668. mutex_unlock(&scif_info.conflock);
  669. }
  670. }
  671. /**
  672. * scif_exit() - Respond to SCIF_EXIT interrupt message
  673. * @scifdev: Remote SCIF device node
  674. * @msg: Interrupt message
  675. *
  676. * This function stops the SCIF interface for the node which sent
  677. * the SCIF_EXIT message and starts waiting for that node to
  678. * resetup the queue pair again.
  679. */
  680. static __always_inline void
  681. scif_exit(struct scif_dev *scifdev, struct scifmsg *unused)
  682. {
  683. scifdev->exit_ack_pending = true;
  684. if (scif_is_mgmt_node())
  685. scif_disconnect_node(scifdev->node, false);
  686. else
  687. scif_stop(scifdev);
  688. schedule_delayed_work(&scifdev->qp_dwork,
  689. msecs_to_jiffies(1000));
  690. }
  691. /**
  692. * scif_exitack() - Respond to SCIF_EXIT_ACK interrupt message
  693. * @scifdev: Remote SCIF device node
  694. * @msg: Interrupt message
  695. *
  696. */
  697. static __always_inline void
  698. scif_exit_ack(struct scif_dev *scifdev, struct scifmsg *unused)
  699. {
  700. scifdev->exit = OP_COMPLETED;
  701. wake_up(&scif_info.exitwq);
  702. }
  703. /**
  704. * scif_node_add() - Respond to SCIF_NODE_ADD interrupt message
  705. * @scifdev: Remote SCIF device node
  706. * @msg: Interrupt message
  707. *
  708. * When the mgmt node driver has finished initializing a MIC node queue pair it
  709. * marks the node as online. It then looks for all currently online MIC cards
  710. * and send a SCIF_NODE_ADD message to identify the ID of the new card for
  711. * peer to peer initialization
  712. *
  713. * The local node allocates its incoming queue and sends its address in the
  714. * SCIF_NODE_ADD_ACK message back to the mgmt node, the mgmt node "reflects"
  715. * this message to the new node
  716. */
  717. static __always_inline void
  718. scif_node_add(struct scif_dev *scifdev, struct scifmsg *msg)
  719. {
  720. struct scif_dev *newdev;
  721. dma_addr_t qp_offset;
  722. int qp_connect;
  723. struct scif_hw_dev *sdev;
  724. dev_dbg(&scifdev->sdev->dev,
  725. "Scifdev %d:%d received NODE_ADD msg for node %d\n",
  726. scifdev->node, msg->dst.node, msg->src.node);
  727. dev_dbg(&scifdev->sdev->dev,
  728. "Remote address for this node's aperture %llx\n",
  729. msg->payload[0]);
  730. newdev = &scif_dev[msg->src.node];
  731. newdev->node = msg->src.node;
  732. newdev->sdev = scif_dev[SCIF_MGMT_NODE].sdev;
  733. sdev = newdev->sdev;
  734. if (scif_setup_intr_wq(newdev)) {
  735. dev_err(&scifdev->sdev->dev,
  736. "failed to setup interrupts for %d\n", msg->src.node);
  737. goto interrupt_setup_error;
  738. }
  739. newdev->mmio.va = ioremap_nocache(msg->payload[1], sdev->mmio->len);
  740. if (!newdev->mmio.va) {
  741. dev_err(&scifdev->sdev->dev,
  742. "failed to map mmio for %d\n", msg->src.node);
  743. goto mmio_map_error;
  744. }
  745. newdev->qpairs = kzalloc(sizeof(*newdev->qpairs), GFP_KERNEL);
  746. if (!newdev->qpairs)
  747. goto qp_alloc_error;
  748. /*
  749. * Set the base address of the remote node's memory since it gets
  750. * added to qp_offset
  751. */
  752. newdev->base_addr = msg->payload[0];
  753. qp_connect = scif_setup_qp_connect(newdev->qpairs, &qp_offset,
  754. SCIF_NODE_QP_SIZE, newdev);
  755. if (qp_connect) {
  756. dev_err(&scifdev->sdev->dev,
  757. "failed to setup qp_connect %d\n", qp_connect);
  758. goto qp_connect_error;
  759. }
  760. newdev->db = sdev->hw_ops->next_db(sdev);
  761. newdev->cookie = sdev->hw_ops->request_irq(sdev, scif_intr_handler,
  762. "SCIF_INTR", newdev,
  763. newdev->db);
  764. if (IS_ERR(newdev->cookie))
  765. goto qp_connect_error;
  766. newdev->qpairs->magic = SCIFEP_MAGIC;
  767. newdev->qpairs->qp_state = SCIF_QP_OFFLINE;
  768. msg->uop = SCIF_NODE_ADD_ACK;
  769. msg->dst.node = msg->src.node;
  770. msg->src.node = scif_info.nodeid;
  771. msg->payload[0] = qp_offset;
  772. msg->payload[2] = newdev->db;
  773. scif_nodeqp_send(&scif_dev[SCIF_MGMT_NODE], msg);
  774. return;
  775. qp_connect_error:
  776. kfree(newdev->qpairs);
  777. newdev->qpairs = NULL;
  778. qp_alloc_error:
  779. iounmap(newdev->mmio.va);
  780. newdev->mmio.va = NULL;
  781. mmio_map_error:
  782. interrupt_setup_error:
  783. dev_err(&scifdev->sdev->dev,
  784. "node add failed for node %d\n", msg->src.node);
  785. msg->uop = SCIF_NODE_ADD_NACK;
  786. msg->dst.node = msg->src.node;
  787. msg->src.node = scif_info.nodeid;
  788. scif_nodeqp_send(&scif_dev[SCIF_MGMT_NODE], msg);
  789. }
  790. void scif_poll_qp_state(struct work_struct *work)
  791. {
  792. #define SCIF_NODE_QP_RETRY 100
  793. #define SCIF_NODE_QP_TIMEOUT 100
  794. struct scif_dev *peerdev = container_of(work, struct scif_dev,
  795. p2p_dwork.work);
  796. struct scif_qp *qp = &peerdev->qpairs[0];
  797. if (qp->qp_state != SCIF_QP_ONLINE ||
  798. qp->remote_qp->qp_state != SCIF_QP_ONLINE) {
  799. if (peerdev->p2p_retry++ == SCIF_NODE_QP_RETRY) {
  800. dev_err(&peerdev->sdev->dev,
  801. "Warning: QP check timeout with state %d\n",
  802. qp->qp_state);
  803. goto timeout;
  804. }
  805. schedule_delayed_work(&peerdev->p2p_dwork,
  806. msecs_to_jiffies(SCIF_NODE_QP_TIMEOUT));
  807. return;
  808. }
  809. return;
  810. timeout:
  811. dev_err(&peerdev->sdev->dev,
  812. "%s %d remote node %d offline, state = 0x%x\n",
  813. __func__, __LINE__, peerdev->node, qp->qp_state);
  814. qp->remote_qp->qp_state = SCIF_QP_OFFLINE;
  815. scif_peer_unregister_device(peerdev);
  816. scif_cleanup_scifdev(peerdev);
  817. }
  818. /**
  819. * scif_node_add_ack() - Respond to SCIF_NODE_ADD_ACK interrupt message
  820. * @scifdev: Remote SCIF device node
  821. * @msg: Interrupt message
  822. *
  823. * After a MIC node receives the SCIF_NODE_ADD_ACK message it send this
  824. * message to the mgmt node to confirm the sequence is finished.
  825. *
  826. */
  827. static __always_inline void
  828. scif_node_add_ack(struct scif_dev *scifdev, struct scifmsg *msg)
  829. {
  830. struct scif_dev *peerdev;
  831. struct scif_qp *qp;
  832. struct scif_dev *dst_dev = &scif_dev[msg->dst.node];
  833. dev_dbg(&scifdev->sdev->dev,
  834. "Scifdev %d received SCIF_NODE_ADD_ACK msg src %d dst %d\n",
  835. scifdev->node, msg->src.node, msg->dst.node);
  836. dev_dbg(&scifdev->sdev->dev,
  837. "payload %llx %llx %llx %llx\n", msg->payload[0],
  838. msg->payload[1], msg->payload[2], msg->payload[3]);
  839. if (scif_is_mgmt_node()) {
  840. /*
  841. * the lock serializes with scif_qp_response_ack. The mgmt node
  842. * is forwarding the NODE_ADD_ACK message from src to dst we
  843. * need to make sure that the dst has already received a
  844. * NODE_ADD for src and setup its end of the qp to dst
  845. */
  846. mutex_lock(&scif_info.conflock);
  847. msg->payload[1] = scif_info.maxid;
  848. scif_nodeqp_send(dst_dev, msg);
  849. mutex_unlock(&scif_info.conflock);
  850. return;
  851. }
  852. peerdev = &scif_dev[msg->src.node];
  853. peerdev->sdev = scif_dev[SCIF_MGMT_NODE].sdev;
  854. peerdev->node = msg->src.node;
  855. qp = &peerdev->qpairs[0];
  856. if ((scif_setup_qp_connect_response(peerdev, &peerdev->qpairs[0],
  857. msg->payload[0])))
  858. goto local_error;
  859. peerdev->rdb = msg->payload[2];
  860. qp->remote_qp->qp_state = SCIF_QP_ONLINE;
  861. scif_peer_register_device(peerdev);
  862. schedule_delayed_work(&peerdev->p2p_dwork, 0);
  863. return;
  864. local_error:
  865. scif_cleanup_scifdev(peerdev);
  866. }
  867. /**
  868. * scif_node_add_nack: Respond to SCIF_NODE_ADD_NACK interrupt message
  869. * @msg: Interrupt message
  870. *
  871. * SCIF_NODE_ADD failed, so inform the waiting wq.
  872. */
  873. static __always_inline void
  874. scif_node_add_nack(struct scif_dev *scifdev, struct scifmsg *msg)
  875. {
  876. if (scif_is_mgmt_node()) {
  877. struct scif_dev *dst_dev = &scif_dev[msg->dst.node];
  878. dev_dbg(&scifdev->sdev->dev,
  879. "SCIF_NODE_ADD_NACK received from %d\n", scifdev->node);
  880. scif_nodeqp_send(dst_dev, msg);
  881. }
  882. }
  883. /*
  884. * scif_node_remove: Handle SCIF_NODE_REMOVE message
  885. * @msg: Interrupt message
  886. *
  887. * Handle node removal.
  888. */
  889. static __always_inline void
  890. scif_node_remove(struct scif_dev *scifdev, struct scifmsg *msg)
  891. {
  892. int node = msg->payload[0];
  893. struct scif_dev *scdev = &scif_dev[node];
  894. scdev->node_remove_ack_pending = true;
  895. scif_handle_remove_node(node);
  896. }
  897. /*
  898. * scif_node_remove_ack: Handle SCIF_NODE_REMOVE_ACK message
  899. * @msg: Interrupt message
  900. *
  901. * The peer has acked a SCIF_NODE_REMOVE message.
  902. */
  903. static __always_inline void
  904. scif_node_remove_ack(struct scif_dev *scifdev, struct scifmsg *msg)
  905. {
  906. struct scif_dev *sdev = &scif_dev[msg->payload[0]];
  907. atomic_inc(&sdev->disconn_rescnt);
  908. wake_up(&sdev->disconn_wq);
  909. }
  910. /**
  911. * scif_get_node_info: Respond to SCIF_GET_NODE_INFO interrupt message
  912. * @msg: Interrupt message
  913. *
  914. * Retrieve node info i.e maxid and total from the mgmt node.
  915. */
  916. static __always_inline void
  917. scif_get_node_info_resp(struct scif_dev *scifdev, struct scifmsg *msg)
  918. {
  919. if (scif_is_mgmt_node()) {
  920. swap(msg->dst.node, msg->src.node);
  921. mutex_lock(&scif_info.conflock);
  922. msg->payload[1] = scif_info.maxid;
  923. msg->payload[2] = scif_info.total;
  924. mutex_unlock(&scif_info.conflock);
  925. scif_nodeqp_send(scifdev, msg);
  926. } else {
  927. struct completion *node_info =
  928. (struct completion *)msg->payload[3];
  929. mutex_lock(&scif_info.conflock);
  930. scif_info.maxid = msg->payload[1];
  931. scif_info.total = msg->payload[2];
  932. complete_all(node_info);
  933. mutex_unlock(&scif_info.conflock);
  934. }
  935. }
  936. static void
  937. scif_msg_unknown(struct scif_dev *scifdev, struct scifmsg *msg)
  938. {
  939. /* Bogus Node Qp Message? */
  940. dev_err(&scifdev->sdev->dev,
  941. "Unknown message 0x%xn scifdev->node 0x%x\n",
  942. msg->uop, scifdev->node);
  943. }
  944. static void (*scif_intr_func[SCIF_MAX_MSG + 1])
  945. (struct scif_dev *, struct scifmsg *msg) = {
  946. scif_msg_unknown, /* Error */
  947. scif_init, /* SCIF_INIT */
  948. scif_exit, /* SCIF_EXIT */
  949. scif_exit_ack, /* SCIF_EXIT_ACK */
  950. scif_node_add, /* SCIF_NODE_ADD */
  951. scif_node_add_ack, /* SCIF_NODE_ADD_ACK */
  952. scif_node_add_nack, /* SCIF_NODE_ADD_NACK */
  953. scif_node_remove, /* SCIF_NODE_REMOVE */
  954. scif_node_remove_ack, /* SCIF_NODE_REMOVE_ACK */
  955. scif_cnctreq, /* SCIF_CNCT_REQ */
  956. scif_cnctgnt, /* SCIF_CNCT_GNT */
  957. scif_cnctgnt_ack, /* SCIF_CNCT_GNTACK */
  958. scif_cnctgnt_nack, /* SCIF_CNCT_GNTNACK */
  959. scif_cnctrej, /* SCIF_CNCT_REJ */
  960. scif_discnct, /* SCIF_DISCNCT */
  961. scif_discnt_ack, /* SCIF_DISCNT_ACK */
  962. scif_clientsend, /* SCIF_CLIENT_SENT */
  963. scif_clientrcvd, /* SCIF_CLIENT_RCVD */
  964. scif_get_node_info_resp,/* SCIF_GET_NODE_INFO */
  965. scif_recv_reg, /* SCIF_REGISTER */
  966. scif_recv_reg_ack, /* SCIF_REGISTER_ACK */
  967. scif_recv_reg_nack, /* SCIF_REGISTER_NACK */
  968. scif_recv_unreg, /* SCIF_UNREGISTER */
  969. scif_recv_unreg_ack, /* SCIF_UNREGISTER_ACK */
  970. scif_recv_unreg_nack, /* SCIF_UNREGISTER_NACK */
  971. scif_alloc_req, /* SCIF_ALLOC_REQ */
  972. scif_alloc_gnt_rej, /* SCIF_ALLOC_GNT */
  973. scif_alloc_gnt_rej, /* SCIF_ALLOC_REJ */
  974. scif_free_virt, /* SCIF_FREE_VIRT */
  975. scif_recv_munmap, /* SCIF_MUNMAP */
  976. scif_recv_mark, /* SCIF_MARK */
  977. scif_recv_mark_resp, /* SCIF_MARK_ACK */
  978. scif_recv_mark_resp, /* SCIF_MARK_NACK */
  979. scif_recv_wait, /* SCIF_WAIT */
  980. scif_recv_wait_resp, /* SCIF_WAIT_ACK */
  981. scif_recv_wait_resp, /* SCIF_WAIT_NACK */
  982. scif_recv_sig_local, /* SCIF_SIG_LOCAL */
  983. scif_recv_sig_remote, /* SCIF_SIG_REMOTE */
  984. scif_recv_sig_resp, /* SCIF_SIG_ACK */
  985. scif_recv_sig_resp, /* SCIF_SIG_NACK */
  986. };
  987. /**
  988. * scif_nodeqp_msg_handler() - Common handler for node messages
  989. * @scifdev: Remote device to respond to
  990. * @qp: Remote memory pointer
  991. * @msg: The message to be handled.
  992. *
  993. * This routine calls the appropriate routine to handle a Node Qp
  994. * message receipt
  995. */
  996. static int scif_max_msg_id = SCIF_MAX_MSG;
  997. static void
  998. scif_nodeqp_msg_handler(struct scif_dev *scifdev,
  999. struct scif_qp *qp, struct scifmsg *msg)
  1000. {
  1001. scif_display_message(scifdev, msg, "Rcvd");
  1002. if (msg->uop > (u32)scif_max_msg_id) {
  1003. /* Bogus Node Qp Message? */
  1004. dev_err(&scifdev->sdev->dev,
  1005. "Unknown message 0x%xn scifdev->node 0x%x\n",
  1006. msg->uop, scifdev->node);
  1007. return;
  1008. }
  1009. scif_intr_func[msg->uop](scifdev, msg);
  1010. }
  1011. /**
  1012. * scif_nodeqp_intrhandler() - Interrupt handler for node messages
  1013. * @scifdev: Remote device to respond to
  1014. * @qp: Remote memory pointer
  1015. *
  1016. * This routine is triggered by the interrupt mechanism. It reads
  1017. * messages from the node queue RB and calls the Node QP Message handling
  1018. * routine.
  1019. */
  1020. void scif_nodeqp_intrhandler(struct scif_dev *scifdev, struct scif_qp *qp)
  1021. {
  1022. struct scifmsg msg;
  1023. int read_size;
  1024. do {
  1025. read_size = scif_rb_get_next(&qp->inbound_q, &msg, sizeof(msg));
  1026. if (!read_size)
  1027. break;
  1028. scif_nodeqp_msg_handler(scifdev, qp, &msg);
  1029. /*
  1030. * The node queue pair is unmapped so skip the read pointer
  1031. * update after receipt of a SCIF_EXIT_ACK
  1032. */
  1033. if (SCIF_EXIT_ACK == msg.uop)
  1034. break;
  1035. scif_rb_update_read_ptr(&qp->inbound_q);
  1036. } while (1);
  1037. }
  1038. /**
  1039. * scif_loopb_wq_handler - Loopback Workqueue Handler.
  1040. * @work: loop back work
  1041. *
  1042. * This work queue routine is invoked by the loopback work queue handler.
  1043. * It grabs the recv lock, dequeues any available messages from the head
  1044. * of the loopback message list, calls the node QP message handler,
  1045. * waits for it to return, then frees up this message and dequeues more
  1046. * elements of the list if available.
  1047. */
  1048. static void scif_loopb_wq_handler(struct work_struct *unused)
  1049. {
  1050. struct scif_dev *scifdev = scif_info.loopb_dev;
  1051. struct scif_qp *qp = scifdev->qpairs;
  1052. struct scif_loopb_msg *msg;
  1053. do {
  1054. msg = NULL;
  1055. spin_lock(&qp->recv_lock);
  1056. if (!list_empty(&scif_info.loopb_recv_q)) {
  1057. msg = list_first_entry(&scif_info.loopb_recv_q,
  1058. struct scif_loopb_msg,
  1059. list);
  1060. list_del(&msg->list);
  1061. }
  1062. spin_unlock(&qp->recv_lock);
  1063. if (msg) {
  1064. scif_nodeqp_msg_handler(scifdev, qp, &msg->msg);
  1065. kfree(msg);
  1066. }
  1067. } while (msg);
  1068. }
  1069. /**
  1070. * scif_loopb_msg_handler() - Workqueue handler for loopback messages.
  1071. * @scifdev: SCIF device
  1072. * @qp: Queue pair.
  1073. *
  1074. * This work queue routine is triggered when a loopback message is received.
  1075. *
  1076. * We need special handling for receiving Node Qp messages on a loopback SCIF
  1077. * device via two workqueues for receiving messages.
  1078. *
  1079. * The reason we need the extra workqueue which is not required with *normal*
  1080. * non-loopback SCIF devices is the potential classic deadlock described below:
  1081. *
  1082. * Thread A tries to send a message on a loopback SCIF device and blocks since
  1083. * there is no space in the RB while it has the send_lock held or another
  1084. * lock called lock X for example.
  1085. *
  1086. * Thread B: The Loopback Node QP message receive workqueue receives the message
  1087. * and tries to send a message (eg an ACK) to the loopback SCIF device. It tries
  1088. * to grab the send lock again or lock X and deadlocks with Thread A. The RB
  1089. * cannot be drained any further due to this classic deadlock.
  1090. *
  1091. * In order to avoid deadlocks as mentioned above we have an extra level of
  1092. * indirection achieved by having two workqueues.
  1093. * 1) The first workqueue whose handler is scif_loopb_msg_handler reads
  1094. * messages from the Node QP RB, adds them to a list and queues work for the
  1095. * second workqueue.
  1096. *
  1097. * 2) The second workqueue whose handler is scif_loopb_wq_handler dequeues
  1098. * messages from the list, handles them, frees up the memory and dequeues
  1099. * more elements from the list if possible.
  1100. */
  1101. int
  1102. scif_loopb_msg_handler(struct scif_dev *scifdev, struct scif_qp *qp)
  1103. {
  1104. int read_size;
  1105. struct scif_loopb_msg *msg;
  1106. do {
  1107. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  1108. if (!msg)
  1109. return -ENOMEM;
  1110. read_size = scif_rb_get_next(&qp->inbound_q, &msg->msg,
  1111. sizeof(struct scifmsg));
  1112. if (read_size != sizeof(struct scifmsg)) {
  1113. kfree(msg);
  1114. scif_rb_update_read_ptr(&qp->inbound_q);
  1115. break;
  1116. }
  1117. spin_lock(&qp->recv_lock);
  1118. list_add_tail(&msg->list, &scif_info.loopb_recv_q);
  1119. spin_unlock(&qp->recv_lock);
  1120. queue_work(scif_info.loopb_wq, &scif_info.loopb_work);
  1121. scif_rb_update_read_ptr(&qp->inbound_q);
  1122. } while (read_size == sizeof(struct scifmsg));
  1123. return read_size;
  1124. }
  1125. /**
  1126. * scif_setup_loopback_qp - One time setup work for Loopback Node Qp.
  1127. * @scifdev: SCIF device
  1128. *
  1129. * Sets up the required loopback workqueues, queue pairs and ring buffers
  1130. */
  1131. int scif_setup_loopback_qp(struct scif_dev *scifdev)
  1132. {
  1133. int err = 0;
  1134. void *local_q;
  1135. struct scif_qp *qp;
  1136. err = scif_setup_intr_wq(scifdev);
  1137. if (err)
  1138. goto exit;
  1139. INIT_LIST_HEAD(&scif_info.loopb_recv_q);
  1140. snprintf(scif_info.loopb_wqname, sizeof(scif_info.loopb_wqname),
  1141. "SCIF LOOPB %d", scifdev->node);
  1142. scif_info.loopb_wq =
  1143. alloc_ordered_workqueue(scif_info.loopb_wqname, 0);
  1144. if (!scif_info.loopb_wq) {
  1145. err = -ENOMEM;
  1146. goto destroy_intr;
  1147. }
  1148. INIT_WORK(&scif_info.loopb_work, scif_loopb_wq_handler);
  1149. /* Allocate Self Qpair */
  1150. scifdev->qpairs = kzalloc(sizeof(*scifdev->qpairs), GFP_KERNEL);
  1151. if (!scifdev->qpairs) {
  1152. err = -ENOMEM;
  1153. goto destroy_loopb_wq;
  1154. }
  1155. qp = scifdev->qpairs;
  1156. qp->magic = SCIFEP_MAGIC;
  1157. spin_lock_init(&qp->send_lock);
  1158. spin_lock_init(&qp->recv_lock);
  1159. local_q = kzalloc(SCIF_NODE_QP_SIZE, GFP_KERNEL);
  1160. if (!local_q) {
  1161. err = -ENOMEM;
  1162. goto free_qpairs;
  1163. }
  1164. /*
  1165. * For loopback the inbound_q and outbound_q are essentially the same
  1166. * since the Node sends a message on the loopback interface to the
  1167. * outbound_q which is then received on the inbound_q.
  1168. */
  1169. scif_rb_init(&qp->outbound_q,
  1170. &qp->local_read,
  1171. &qp->local_write,
  1172. local_q, get_count_order(SCIF_NODE_QP_SIZE));
  1173. scif_rb_init(&qp->inbound_q,
  1174. &qp->local_read,
  1175. &qp->local_write,
  1176. local_q, get_count_order(SCIF_NODE_QP_SIZE));
  1177. scif_info.nodeid = scifdev->node;
  1178. scif_peer_register_device(scifdev);
  1179. scif_info.loopb_dev = scifdev;
  1180. return err;
  1181. free_qpairs:
  1182. kfree(scifdev->qpairs);
  1183. destroy_loopb_wq:
  1184. destroy_workqueue(scif_info.loopb_wq);
  1185. destroy_intr:
  1186. scif_destroy_intr_wq(scifdev);
  1187. exit:
  1188. return err;
  1189. }
  1190. /**
  1191. * scif_destroy_loopback_qp - One time uninit work for Loopback Node Qp
  1192. * @scifdev: SCIF device
  1193. *
  1194. * Destroys the workqueues and frees up the Ring Buffer and Queue Pair memory.
  1195. */
  1196. int scif_destroy_loopback_qp(struct scif_dev *scifdev)
  1197. {
  1198. scif_peer_unregister_device(scifdev);
  1199. destroy_workqueue(scif_info.loopb_wq);
  1200. scif_destroy_intr_wq(scifdev);
  1201. kfree(scifdev->qpairs->outbound_q.rb_base);
  1202. kfree(scifdev->qpairs);
  1203. scifdev->sdev = NULL;
  1204. scif_info.loopb_dev = NULL;
  1205. return 0;
  1206. }
  1207. void scif_destroy_p2p(struct scif_dev *scifdev)
  1208. {
  1209. struct scif_dev *peer_dev;
  1210. struct scif_p2p_info *p2p;
  1211. struct list_head *pos, *tmp;
  1212. int bd;
  1213. mutex_lock(&scif_info.conflock);
  1214. /* Free P2P mappings in the given node for all its peer nodes */
  1215. list_for_each_safe(pos, tmp, &scifdev->p2p) {
  1216. p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
  1217. dma_unmap_sg(&scifdev->sdev->dev, p2p->ppi_sg[SCIF_PPI_MMIO],
  1218. p2p->sg_nentries[SCIF_PPI_MMIO],
  1219. DMA_BIDIRECTIONAL);
  1220. dma_unmap_sg(&scifdev->sdev->dev, p2p->ppi_sg[SCIF_PPI_APER],
  1221. p2p->sg_nentries[SCIF_PPI_APER],
  1222. DMA_BIDIRECTIONAL);
  1223. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
  1224. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
  1225. list_del(pos);
  1226. kfree(p2p);
  1227. }
  1228. /* Free P2P mapping created in the peer nodes for the given node */
  1229. for (bd = SCIF_MGMT_NODE + 1; bd <= scif_info.maxid; bd++) {
  1230. peer_dev = &scif_dev[bd];
  1231. list_for_each_safe(pos, tmp, &peer_dev->p2p) {
  1232. p2p = list_entry(pos, struct scif_p2p_info, ppi_list);
  1233. if (p2p->ppi_peer_id == scifdev->node) {
  1234. dma_unmap_sg(&peer_dev->sdev->dev,
  1235. p2p->ppi_sg[SCIF_PPI_MMIO],
  1236. p2p->sg_nentries[SCIF_PPI_MMIO],
  1237. DMA_BIDIRECTIONAL);
  1238. dma_unmap_sg(&peer_dev->sdev->dev,
  1239. p2p->ppi_sg[SCIF_PPI_APER],
  1240. p2p->sg_nentries[SCIF_PPI_APER],
  1241. DMA_BIDIRECTIONAL);
  1242. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_MMIO]);
  1243. scif_p2p_freesg(p2p->ppi_sg[SCIF_PPI_APER]);
  1244. list_del(pos);
  1245. kfree(p2p);
  1246. }
  1247. }
  1248. }
  1249. mutex_unlock(&scif_info.conflock);
  1250. }