scif_nodeqp.c 38 KB

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