scif_nodeqp.c 37 KB

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