scif_api.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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 <linux/scif.h>
  19. #include "scif_main.h"
  20. #include "scif_map.h"
  21. static const char * const scif_ep_states[] = {
  22. "Unbound",
  23. "Bound",
  24. "Listening",
  25. "Connected",
  26. "Connecting",
  27. "Mapping",
  28. "Closing",
  29. "Close Listening",
  30. "Disconnected",
  31. "Zombie"};
  32. enum conn_async_state {
  33. ASYNC_CONN_IDLE = 1, /* ep setup for async connect */
  34. ASYNC_CONN_INPROGRESS, /* async connect in progress */
  35. ASYNC_CONN_FLUSH_WORK /* async work flush in progress */
  36. };
  37. /*
  38. * File operations for anonymous inode file associated with a SCIF endpoint,
  39. * used in kernel mode SCIF poll. Kernel mode SCIF poll calls portions of the
  40. * poll API in the kernel and these take in a struct file *. Since a struct
  41. * file is not available to kernel mode SCIF, it uses an anonymous file for
  42. * this purpose.
  43. */
  44. const struct file_operations scif_anon_fops = {
  45. .owner = THIS_MODULE,
  46. };
  47. scif_epd_t scif_open(void)
  48. {
  49. struct scif_endpt *ep;
  50. int err;
  51. might_sleep();
  52. ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  53. if (!ep)
  54. goto err_ep_alloc;
  55. ep->qp_info.qp = kzalloc(sizeof(*ep->qp_info.qp), GFP_KERNEL);
  56. if (!ep->qp_info.qp)
  57. goto err_qp_alloc;
  58. err = scif_anon_inode_getfile(ep);
  59. if (err)
  60. goto err_anon_inode;
  61. spin_lock_init(&ep->lock);
  62. mutex_init(&ep->sendlock);
  63. mutex_init(&ep->recvlock);
  64. scif_rma_ep_init(ep);
  65. ep->state = SCIFEP_UNBOUND;
  66. dev_dbg(scif_info.mdev.this_device,
  67. "SCIFAPI open: ep %p success\n", ep);
  68. return ep;
  69. err_anon_inode:
  70. kfree(ep->qp_info.qp);
  71. err_qp_alloc:
  72. kfree(ep);
  73. err_ep_alloc:
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL_GPL(scif_open);
  77. /*
  78. * scif_disconnect_ep - Disconnects the endpoint if found
  79. * @epd: The end point returned from scif_open()
  80. */
  81. static struct scif_endpt *scif_disconnect_ep(struct scif_endpt *ep)
  82. {
  83. struct scifmsg msg;
  84. struct scif_endpt *fep = NULL;
  85. struct scif_endpt *tmpep;
  86. struct list_head *pos, *tmpq;
  87. int err;
  88. /*
  89. * Wake up any threads blocked in send()/recv() before closing
  90. * out the connection. Grabbing and releasing the send/recv lock
  91. * will ensure that any blocked senders/receivers have exited for
  92. * Ring 0 endpoints. It is a Ring 0 bug to call send/recv after
  93. * close. Ring 3 endpoints are not affected since close will not
  94. * be called while there are IOCTLs executing.
  95. */
  96. wake_up_interruptible(&ep->sendwq);
  97. wake_up_interruptible(&ep->recvwq);
  98. mutex_lock(&ep->sendlock);
  99. mutex_unlock(&ep->sendlock);
  100. mutex_lock(&ep->recvlock);
  101. mutex_unlock(&ep->recvlock);
  102. /* Remove from the connected list */
  103. mutex_lock(&scif_info.connlock);
  104. list_for_each_safe(pos, tmpq, &scif_info.connected) {
  105. tmpep = list_entry(pos, struct scif_endpt, list);
  106. if (tmpep == ep) {
  107. list_del(pos);
  108. fep = tmpep;
  109. spin_lock(&ep->lock);
  110. break;
  111. }
  112. }
  113. if (!fep) {
  114. /*
  115. * The other side has completed the disconnect before
  116. * the end point can be removed from the list. Therefore
  117. * the ep lock is not locked, traverse the disconnected
  118. * list to find the endpoint and release the conn lock.
  119. */
  120. list_for_each_safe(pos, tmpq, &scif_info.disconnected) {
  121. tmpep = list_entry(pos, struct scif_endpt, list);
  122. if (tmpep == ep) {
  123. list_del(pos);
  124. break;
  125. }
  126. }
  127. mutex_unlock(&scif_info.connlock);
  128. return NULL;
  129. }
  130. init_completion(&ep->discon);
  131. msg.uop = SCIF_DISCNCT;
  132. msg.src = ep->port;
  133. msg.dst = ep->peer;
  134. msg.payload[0] = (u64)ep;
  135. msg.payload[1] = ep->remote_ep;
  136. err = scif_nodeqp_send(ep->remote_dev, &msg);
  137. spin_unlock(&ep->lock);
  138. mutex_unlock(&scif_info.connlock);
  139. if (!err)
  140. /* Wait for the remote node to respond with SCIF_DISCNT_ACK */
  141. wait_for_completion_timeout(&ep->discon,
  142. SCIF_NODE_ALIVE_TIMEOUT);
  143. return ep;
  144. }
  145. int scif_close(scif_epd_t epd)
  146. {
  147. struct scif_endpt *ep = (struct scif_endpt *)epd;
  148. struct scif_endpt *tmpep;
  149. struct list_head *pos, *tmpq;
  150. enum scif_epd_state oldstate;
  151. bool flush_conn;
  152. dev_dbg(scif_info.mdev.this_device, "SCIFAPI close: ep %p %s\n",
  153. ep, scif_ep_states[ep->state]);
  154. might_sleep();
  155. spin_lock(&ep->lock);
  156. flush_conn = (ep->conn_async_state == ASYNC_CONN_INPROGRESS);
  157. spin_unlock(&ep->lock);
  158. if (flush_conn)
  159. flush_work(&scif_info.conn_work);
  160. spin_lock(&ep->lock);
  161. oldstate = ep->state;
  162. ep->state = SCIFEP_CLOSING;
  163. switch (oldstate) {
  164. case SCIFEP_ZOMBIE:
  165. dev_err(scif_info.mdev.this_device,
  166. "SCIFAPI close: zombie state unexpected\n");
  167. /* fall through */
  168. case SCIFEP_DISCONNECTED:
  169. spin_unlock(&ep->lock);
  170. scif_unregister_all_windows(epd);
  171. /* Remove from the disconnected list */
  172. mutex_lock(&scif_info.connlock);
  173. list_for_each_safe(pos, tmpq, &scif_info.disconnected) {
  174. tmpep = list_entry(pos, struct scif_endpt, list);
  175. if (tmpep == ep) {
  176. list_del(pos);
  177. break;
  178. }
  179. }
  180. mutex_unlock(&scif_info.connlock);
  181. break;
  182. case SCIFEP_UNBOUND:
  183. case SCIFEP_BOUND:
  184. case SCIFEP_CONNECTING:
  185. spin_unlock(&ep->lock);
  186. break;
  187. case SCIFEP_MAPPING:
  188. case SCIFEP_CONNECTED:
  189. case SCIFEP_CLOSING:
  190. {
  191. spin_unlock(&ep->lock);
  192. scif_unregister_all_windows(epd);
  193. scif_disconnect_ep(ep);
  194. break;
  195. }
  196. case SCIFEP_LISTENING:
  197. case SCIFEP_CLLISTEN:
  198. {
  199. struct scif_conreq *conreq;
  200. struct scifmsg msg;
  201. struct scif_endpt *aep;
  202. spin_unlock(&ep->lock);
  203. mutex_lock(&scif_info.eplock);
  204. /* remove from listen list */
  205. list_for_each_safe(pos, tmpq, &scif_info.listen) {
  206. tmpep = list_entry(pos, struct scif_endpt, list);
  207. if (tmpep == ep)
  208. list_del(pos);
  209. }
  210. /* Remove any dangling accepts */
  211. while (ep->acceptcnt) {
  212. aep = list_first_entry(&ep->li_accept,
  213. struct scif_endpt, liacceptlist);
  214. list_del(&aep->liacceptlist);
  215. scif_put_port(aep->port.port);
  216. list_for_each_safe(pos, tmpq, &scif_info.uaccept) {
  217. tmpep = list_entry(pos, struct scif_endpt,
  218. miacceptlist);
  219. if (tmpep == aep) {
  220. list_del(pos);
  221. break;
  222. }
  223. }
  224. mutex_unlock(&scif_info.eplock);
  225. mutex_lock(&scif_info.connlock);
  226. list_for_each_safe(pos, tmpq, &scif_info.connected) {
  227. tmpep = list_entry(pos,
  228. struct scif_endpt, list);
  229. if (tmpep == aep) {
  230. list_del(pos);
  231. break;
  232. }
  233. }
  234. list_for_each_safe(pos, tmpq, &scif_info.disconnected) {
  235. tmpep = list_entry(pos,
  236. struct scif_endpt, list);
  237. if (tmpep == aep) {
  238. list_del(pos);
  239. break;
  240. }
  241. }
  242. mutex_unlock(&scif_info.connlock);
  243. scif_teardown_ep(aep);
  244. mutex_lock(&scif_info.eplock);
  245. scif_add_epd_to_zombie_list(aep, SCIF_EPLOCK_HELD);
  246. ep->acceptcnt--;
  247. }
  248. spin_lock(&ep->lock);
  249. mutex_unlock(&scif_info.eplock);
  250. /* Remove and reject any pending connection requests. */
  251. while (ep->conreqcnt) {
  252. conreq = list_first_entry(&ep->conlist,
  253. struct scif_conreq, list);
  254. list_del(&conreq->list);
  255. msg.uop = SCIF_CNCT_REJ;
  256. msg.dst.node = conreq->msg.src.node;
  257. msg.dst.port = conreq->msg.src.port;
  258. msg.payload[0] = conreq->msg.payload[0];
  259. msg.payload[1] = conreq->msg.payload[1];
  260. /*
  261. * No Error Handling on purpose for scif_nodeqp_send().
  262. * If the remote node is lost we still want free the
  263. * connection requests on the self node.
  264. */
  265. scif_nodeqp_send(&scif_dev[conreq->msg.src.node],
  266. &msg);
  267. ep->conreqcnt--;
  268. kfree(conreq);
  269. }
  270. spin_unlock(&ep->lock);
  271. /* If a kSCIF accept is waiting wake it up */
  272. wake_up_interruptible(&ep->conwq);
  273. break;
  274. }
  275. }
  276. scif_put_port(ep->port.port);
  277. scif_anon_inode_fput(ep);
  278. scif_teardown_ep(ep);
  279. scif_add_epd_to_zombie_list(ep, !SCIF_EPLOCK_HELD);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(scif_close);
  283. /**
  284. * scif_flush() - Wakes up any blocking accepts. The endpoint will no longer
  285. * accept new connections.
  286. * @epd: The end point returned from scif_open()
  287. */
  288. int __scif_flush(scif_epd_t epd)
  289. {
  290. struct scif_endpt *ep = (struct scif_endpt *)epd;
  291. switch (ep->state) {
  292. case SCIFEP_LISTENING:
  293. {
  294. ep->state = SCIFEP_CLLISTEN;
  295. /* If an accept is waiting wake it up */
  296. wake_up_interruptible(&ep->conwq);
  297. break;
  298. }
  299. default:
  300. break;
  301. }
  302. return 0;
  303. }
  304. int scif_bind(scif_epd_t epd, u16 pn)
  305. {
  306. struct scif_endpt *ep = (struct scif_endpt *)epd;
  307. int ret = 0;
  308. int tmp;
  309. dev_dbg(scif_info.mdev.this_device,
  310. "SCIFAPI bind: ep %p %s requested port number %d\n",
  311. ep, scif_ep_states[ep->state], pn);
  312. if (pn) {
  313. /*
  314. * Similar to IETF RFC 1700, SCIF ports below
  315. * SCIF_ADMIN_PORT_END can only be bound by system (or root)
  316. * processes or by processes executed by privileged users.
  317. */
  318. if (pn < SCIF_ADMIN_PORT_END && !capable(CAP_SYS_ADMIN)) {
  319. ret = -EACCES;
  320. goto scif_bind_admin_exit;
  321. }
  322. }
  323. spin_lock(&ep->lock);
  324. if (ep->state == SCIFEP_BOUND) {
  325. ret = -EINVAL;
  326. goto scif_bind_exit;
  327. } else if (ep->state != SCIFEP_UNBOUND) {
  328. ret = -EISCONN;
  329. goto scif_bind_exit;
  330. }
  331. if (pn) {
  332. tmp = scif_rsrv_port(pn);
  333. if (tmp != pn) {
  334. ret = -EINVAL;
  335. goto scif_bind_exit;
  336. }
  337. } else {
  338. pn = scif_get_new_port();
  339. if (!pn) {
  340. ret = -ENOSPC;
  341. goto scif_bind_exit;
  342. }
  343. }
  344. ep->state = SCIFEP_BOUND;
  345. ep->port.node = scif_info.nodeid;
  346. ep->port.port = pn;
  347. ep->conn_async_state = ASYNC_CONN_IDLE;
  348. ret = pn;
  349. dev_dbg(scif_info.mdev.this_device,
  350. "SCIFAPI bind: bound to port number %d\n", pn);
  351. scif_bind_exit:
  352. spin_unlock(&ep->lock);
  353. scif_bind_admin_exit:
  354. return ret;
  355. }
  356. EXPORT_SYMBOL_GPL(scif_bind);
  357. int scif_listen(scif_epd_t epd, int backlog)
  358. {
  359. struct scif_endpt *ep = (struct scif_endpt *)epd;
  360. dev_dbg(scif_info.mdev.this_device,
  361. "SCIFAPI listen: ep %p %s\n", ep, scif_ep_states[ep->state]);
  362. spin_lock(&ep->lock);
  363. switch (ep->state) {
  364. case SCIFEP_ZOMBIE:
  365. case SCIFEP_CLOSING:
  366. case SCIFEP_CLLISTEN:
  367. case SCIFEP_UNBOUND:
  368. case SCIFEP_DISCONNECTED:
  369. spin_unlock(&ep->lock);
  370. return -EINVAL;
  371. case SCIFEP_LISTENING:
  372. case SCIFEP_CONNECTED:
  373. case SCIFEP_CONNECTING:
  374. case SCIFEP_MAPPING:
  375. spin_unlock(&ep->lock);
  376. return -EISCONN;
  377. case SCIFEP_BOUND:
  378. break;
  379. }
  380. ep->state = SCIFEP_LISTENING;
  381. ep->backlog = backlog;
  382. ep->conreqcnt = 0;
  383. ep->acceptcnt = 0;
  384. INIT_LIST_HEAD(&ep->conlist);
  385. init_waitqueue_head(&ep->conwq);
  386. INIT_LIST_HEAD(&ep->li_accept);
  387. spin_unlock(&ep->lock);
  388. /*
  389. * Listen status is complete so delete the qp information not needed
  390. * on a listen before placing on the list of listening ep's
  391. */
  392. scif_teardown_ep(ep);
  393. ep->qp_info.qp = NULL;
  394. mutex_lock(&scif_info.eplock);
  395. list_add_tail(&ep->list, &scif_info.listen);
  396. mutex_unlock(&scif_info.eplock);
  397. return 0;
  398. }
  399. EXPORT_SYMBOL_GPL(scif_listen);
  400. /*
  401. ************************************************************************
  402. * SCIF connection flow:
  403. *
  404. * 1) A SCIF listening endpoint can call scif_accept(..) to wait for SCIF
  405. * connections via a SCIF_CNCT_REQ message
  406. * 2) A SCIF endpoint can initiate a SCIF connection by calling
  407. * scif_connect(..) which calls scif_setup_qp_connect(..) which
  408. * allocates the local qp for the endpoint ring buffer and then sends
  409. * a SCIF_CNCT_REQ to the remote node and waits for a SCIF_CNCT_GNT or
  410. * a SCIF_CNCT_REJ message
  411. * 3) The peer node handles a SCIF_CNCT_REQ via scif_cnctreq_resp(..) which
  412. * wakes up any threads blocked in step 1 or sends a SCIF_CNCT_REJ
  413. * message otherwise
  414. * 4) A thread blocked waiting for incoming connections allocates its local
  415. * endpoint QP and ring buffer following which it sends a SCIF_CNCT_GNT
  416. * and waits for a SCIF_CNCT_GNT(N)ACK. If the allocation fails then
  417. * the node sends a SCIF_CNCT_REJ message
  418. * 5) Upon receipt of a SCIF_CNCT_GNT or a SCIF_CNCT_REJ message the
  419. * connecting endpoint is woken up as part of handling
  420. * scif_cnctgnt_resp(..) following which it maps the remote endpoints'
  421. * QP, updates its outbound QP and sends a SCIF_CNCT_GNTACK message on
  422. * success or a SCIF_CNCT_GNTNACK message on failure and completes
  423. * the scif_connect(..) API
  424. * 6) Upon receipt of a SCIF_CNCT_GNT(N)ACK the accepting endpoint blocked
  425. * in step 4 is woken up and completes the scif_accept(..) API
  426. * 7) The SCIF connection is now established between the two SCIF endpoints.
  427. */
  428. static int scif_conn_func(struct scif_endpt *ep)
  429. {
  430. int err = 0;
  431. struct scifmsg msg;
  432. struct device *spdev;
  433. err = scif_reserve_dma_chan(ep);
  434. if (err) {
  435. dev_err(&ep->remote_dev->sdev->dev,
  436. "%s %d err %d\n", __func__, __LINE__, err);
  437. ep->state = SCIFEP_BOUND;
  438. goto connect_error_simple;
  439. }
  440. /* Initiate the first part of the endpoint QP setup */
  441. err = scif_setup_qp_connect(ep->qp_info.qp, &ep->qp_info.qp_offset,
  442. SCIF_ENDPT_QP_SIZE, ep->remote_dev);
  443. if (err) {
  444. dev_err(&ep->remote_dev->sdev->dev,
  445. "%s err %d qp_offset 0x%llx\n",
  446. __func__, err, ep->qp_info.qp_offset);
  447. ep->state = SCIFEP_BOUND;
  448. goto connect_error_simple;
  449. }
  450. spdev = scif_get_peer_dev(ep->remote_dev);
  451. if (IS_ERR(spdev)) {
  452. err = PTR_ERR(spdev);
  453. goto cleanup_qp;
  454. }
  455. /* Format connect message and send it */
  456. msg.src = ep->port;
  457. msg.dst = ep->conn_port;
  458. msg.uop = SCIF_CNCT_REQ;
  459. msg.payload[0] = (u64)ep;
  460. msg.payload[1] = ep->qp_info.qp_offset;
  461. err = _scif_nodeqp_send(ep->remote_dev, &msg);
  462. if (err)
  463. goto connect_error_dec;
  464. scif_put_peer_dev(spdev);
  465. /*
  466. * Wait for the remote node to respond with SCIF_CNCT_GNT or
  467. * SCIF_CNCT_REJ message.
  468. */
  469. err = wait_event_timeout(ep->conwq, ep->state != SCIFEP_CONNECTING,
  470. SCIF_NODE_ALIVE_TIMEOUT);
  471. if (!err) {
  472. dev_err(&ep->remote_dev->sdev->dev,
  473. "%s %d timeout\n", __func__, __LINE__);
  474. ep->state = SCIFEP_BOUND;
  475. }
  476. spdev = scif_get_peer_dev(ep->remote_dev);
  477. if (IS_ERR(spdev)) {
  478. err = PTR_ERR(spdev);
  479. goto cleanup_qp;
  480. }
  481. if (ep->state == SCIFEP_MAPPING) {
  482. err = scif_setup_qp_connect_response(ep->remote_dev,
  483. ep->qp_info.qp,
  484. ep->qp_info.gnt_pld);
  485. /*
  486. * If the resource to map the queue are not available then
  487. * we need to tell the other side to terminate the accept
  488. */
  489. if (err) {
  490. dev_err(&ep->remote_dev->sdev->dev,
  491. "%s %d err %d\n", __func__, __LINE__, err);
  492. msg.uop = SCIF_CNCT_GNTNACK;
  493. msg.payload[0] = ep->remote_ep;
  494. _scif_nodeqp_send(ep->remote_dev, &msg);
  495. ep->state = SCIFEP_BOUND;
  496. goto connect_error_dec;
  497. }
  498. msg.uop = SCIF_CNCT_GNTACK;
  499. msg.payload[0] = ep->remote_ep;
  500. err = _scif_nodeqp_send(ep->remote_dev, &msg);
  501. if (err) {
  502. ep->state = SCIFEP_BOUND;
  503. goto connect_error_dec;
  504. }
  505. ep->state = SCIFEP_CONNECTED;
  506. mutex_lock(&scif_info.connlock);
  507. list_add_tail(&ep->list, &scif_info.connected);
  508. mutex_unlock(&scif_info.connlock);
  509. dev_dbg(&ep->remote_dev->sdev->dev,
  510. "SCIFAPI connect: ep %p connected\n", ep);
  511. } else if (ep->state == SCIFEP_BOUND) {
  512. dev_dbg(&ep->remote_dev->sdev->dev,
  513. "SCIFAPI connect: ep %p connection refused\n", ep);
  514. err = -ECONNREFUSED;
  515. goto connect_error_dec;
  516. }
  517. scif_put_peer_dev(spdev);
  518. return err;
  519. connect_error_dec:
  520. scif_put_peer_dev(spdev);
  521. cleanup_qp:
  522. scif_cleanup_ep_qp(ep);
  523. connect_error_simple:
  524. return err;
  525. }
  526. /*
  527. * scif_conn_handler:
  528. *
  529. * Workqueue handler for servicing non-blocking SCIF connect
  530. *
  531. */
  532. void scif_conn_handler(struct work_struct *work)
  533. {
  534. struct scif_endpt *ep;
  535. do {
  536. ep = NULL;
  537. spin_lock(&scif_info.nb_connect_lock);
  538. if (!list_empty(&scif_info.nb_connect_list)) {
  539. ep = list_first_entry(&scif_info.nb_connect_list,
  540. struct scif_endpt, conn_list);
  541. list_del(&ep->conn_list);
  542. }
  543. spin_unlock(&scif_info.nb_connect_lock);
  544. if (ep) {
  545. ep->conn_err = scif_conn_func(ep);
  546. wake_up_interruptible(&ep->conn_pend_wq);
  547. }
  548. } while (ep);
  549. }
  550. int __scif_connect(scif_epd_t epd, struct scif_port_id *dst, bool non_block)
  551. {
  552. struct scif_endpt *ep = (struct scif_endpt *)epd;
  553. int err = 0;
  554. struct scif_dev *remote_dev;
  555. struct device *spdev;
  556. dev_dbg(scif_info.mdev.this_device, "SCIFAPI connect: ep %p %s\n", ep,
  557. scif_ep_states[ep->state]);
  558. if (!scif_dev || dst->node > scif_info.maxid)
  559. return -ENODEV;
  560. might_sleep();
  561. remote_dev = &scif_dev[dst->node];
  562. spdev = scif_get_peer_dev(remote_dev);
  563. if (IS_ERR(spdev)) {
  564. err = PTR_ERR(spdev);
  565. return err;
  566. }
  567. spin_lock(&ep->lock);
  568. switch (ep->state) {
  569. case SCIFEP_ZOMBIE:
  570. case SCIFEP_CLOSING:
  571. err = -EINVAL;
  572. break;
  573. case SCIFEP_DISCONNECTED:
  574. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS)
  575. ep->conn_async_state = ASYNC_CONN_FLUSH_WORK;
  576. else
  577. err = -EINVAL;
  578. break;
  579. case SCIFEP_LISTENING:
  580. case SCIFEP_CLLISTEN:
  581. err = -EOPNOTSUPP;
  582. break;
  583. case SCIFEP_CONNECTING:
  584. case SCIFEP_MAPPING:
  585. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS)
  586. err = -EINPROGRESS;
  587. else
  588. err = -EISCONN;
  589. break;
  590. case SCIFEP_CONNECTED:
  591. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS)
  592. ep->conn_async_state = ASYNC_CONN_FLUSH_WORK;
  593. else
  594. err = -EISCONN;
  595. break;
  596. case SCIFEP_UNBOUND:
  597. ep->port.port = scif_get_new_port();
  598. if (!ep->port.port) {
  599. err = -ENOSPC;
  600. } else {
  601. ep->port.node = scif_info.nodeid;
  602. ep->conn_async_state = ASYNC_CONN_IDLE;
  603. }
  604. /* Fall through */
  605. case SCIFEP_BOUND:
  606. /*
  607. * If a non-blocking connect has been already initiated
  608. * (conn_async_state is either ASYNC_CONN_INPROGRESS or
  609. * ASYNC_CONN_FLUSH_WORK), the end point could end up in
  610. * SCIF_BOUND due an error in the connection process
  611. * (e.g., connection refused) If conn_async_state is
  612. * ASYNC_CONN_INPROGRESS - transition to ASYNC_CONN_FLUSH_WORK
  613. * so that the error status can be collected. If the state is
  614. * already ASYNC_CONN_FLUSH_WORK - then set the error to
  615. * EINPROGRESS since some other thread is waiting to collect
  616. * error status.
  617. */
  618. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS) {
  619. ep->conn_async_state = ASYNC_CONN_FLUSH_WORK;
  620. } else if (ep->conn_async_state == ASYNC_CONN_FLUSH_WORK) {
  621. err = -EINPROGRESS;
  622. } else {
  623. ep->conn_port = *dst;
  624. init_waitqueue_head(&ep->sendwq);
  625. init_waitqueue_head(&ep->recvwq);
  626. init_waitqueue_head(&ep->conwq);
  627. ep->conn_async_state = 0;
  628. if (unlikely(non_block))
  629. ep->conn_async_state = ASYNC_CONN_INPROGRESS;
  630. }
  631. break;
  632. }
  633. if (err || ep->conn_async_state == ASYNC_CONN_FLUSH_WORK)
  634. goto connect_simple_unlock1;
  635. ep->state = SCIFEP_CONNECTING;
  636. ep->remote_dev = &scif_dev[dst->node];
  637. ep->qp_info.qp->magic = SCIFEP_MAGIC;
  638. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS) {
  639. init_waitqueue_head(&ep->conn_pend_wq);
  640. spin_lock(&scif_info.nb_connect_lock);
  641. list_add_tail(&ep->conn_list, &scif_info.nb_connect_list);
  642. spin_unlock(&scif_info.nb_connect_lock);
  643. err = -EINPROGRESS;
  644. schedule_work(&scif_info.conn_work);
  645. }
  646. connect_simple_unlock1:
  647. spin_unlock(&ep->lock);
  648. scif_put_peer_dev(spdev);
  649. if (err) {
  650. return err;
  651. } else if (ep->conn_async_state == ASYNC_CONN_FLUSH_WORK) {
  652. flush_work(&scif_info.conn_work);
  653. err = ep->conn_err;
  654. spin_lock(&ep->lock);
  655. ep->conn_async_state = ASYNC_CONN_IDLE;
  656. spin_unlock(&ep->lock);
  657. } else {
  658. err = scif_conn_func(ep);
  659. }
  660. return err;
  661. }
  662. int scif_connect(scif_epd_t epd, struct scif_port_id *dst)
  663. {
  664. return __scif_connect(epd, dst, false);
  665. }
  666. EXPORT_SYMBOL_GPL(scif_connect);
  667. /**
  668. * scif_accept() - Accept a connection request from the remote node
  669. *
  670. * The function accepts a connection request from the remote node. Successful
  671. * complete is indicate by a new end point being created and passed back
  672. * to the caller for future reference.
  673. *
  674. * Upon successful complete a zero will be returned and the peer information
  675. * will be filled in.
  676. *
  677. * If the end point is not in the listening state -EINVAL will be returned.
  678. *
  679. * If during the connection sequence resource allocation fails the -ENOMEM
  680. * will be returned.
  681. *
  682. * If the function is called with the ASYNC flag set and no connection requests
  683. * are pending it will return -EAGAIN.
  684. *
  685. * If the remote side is not sending any connection requests the caller may
  686. * terminate this function with a signal. If so a -EINTR will be returned.
  687. */
  688. int scif_accept(scif_epd_t epd, struct scif_port_id *peer,
  689. scif_epd_t *newepd, int flags)
  690. {
  691. struct scif_endpt *lep = (struct scif_endpt *)epd;
  692. struct scif_endpt *cep;
  693. struct scif_conreq *conreq;
  694. struct scifmsg msg;
  695. int err;
  696. struct device *spdev;
  697. dev_dbg(scif_info.mdev.this_device,
  698. "SCIFAPI accept: ep %p %s\n", lep, scif_ep_states[lep->state]);
  699. if (flags & ~SCIF_ACCEPT_SYNC)
  700. return -EINVAL;
  701. if (!peer || !newepd)
  702. return -EINVAL;
  703. might_sleep();
  704. spin_lock(&lep->lock);
  705. if (lep->state != SCIFEP_LISTENING) {
  706. spin_unlock(&lep->lock);
  707. return -EINVAL;
  708. }
  709. if (!lep->conreqcnt && !(flags & SCIF_ACCEPT_SYNC)) {
  710. /* No connection request present and we do not want to wait */
  711. spin_unlock(&lep->lock);
  712. return -EAGAIN;
  713. }
  714. lep->files = current->files;
  715. retry_connection:
  716. spin_unlock(&lep->lock);
  717. /* Wait for the remote node to send us a SCIF_CNCT_REQ */
  718. err = wait_event_interruptible(lep->conwq,
  719. (lep->conreqcnt ||
  720. (lep->state != SCIFEP_LISTENING)));
  721. if (err)
  722. return err;
  723. if (lep->state != SCIFEP_LISTENING)
  724. return -EINTR;
  725. spin_lock(&lep->lock);
  726. if (!lep->conreqcnt)
  727. goto retry_connection;
  728. /* Get the first connect request off the list */
  729. conreq = list_first_entry(&lep->conlist, struct scif_conreq, list);
  730. list_del(&conreq->list);
  731. lep->conreqcnt--;
  732. spin_unlock(&lep->lock);
  733. /* Fill in the peer information */
  734. peer->node = conreq->msg.src.node;
  735. peer->port = conreq->msg.src.port;
  736. cep = kzalloc(sizeof(*cep), GFP_KERNEL);
  737. if (!cep) {
  738. err = -ENOMEM;
  739. goto scif_accept_error_epalloc;
  740. }
  741. spin_lock_init(&cep->lock);
  742. mutex_init(&cep->sendlock);
  743. mutex_init(&cep->recvlock);
  744. cep->state = SCIFEP_CONNECTING;
  745. cep->remote_dev = &scif_dev[peer->node];
  746. cep->remote_ep = conreq->msg.payload[0];
  747. scif_rma_ep_init(cep);
  748. err = scif_reserve_dma_chan(cep);
  749. if (err) {
  750. dev_err(scif_info.mdev.this_device,
  751. "%s %d err %d\n", __func__, __LINE__, err);
  752. goto scif_accept_error_qpalloc;
  753. }
  754. cep->qp_info.qp = kzalloc(sizeof(*cep->qp_info.qp), GFP_KERNEL);
  755. if (!cep->qp_info.qp) {
  756. err = -ENOMEM;
  757. goto scif_accept_error_qpalloc;
  758. }
  759. err = scif_anon_inode_getfile(cep);
  760. if (err)
  761. goto scif_accept_error_anon_inode;
  762. cep->qp_info.qp->magic = SCIFEP_MAGIC;
  763. spdev = scif_get_peer_dev(cep->remote_dev);
  764. if (IS_ERR(spdev)) {
  765. err = PTR_ERR(spdev);
  766. goto scif_accept_error_map;
  767. }
  768. err = scif_setup_qp_accept(cep->qp_info.qp, &cep->qp_info.qp_offset,
  769. conreq->msg.payload[1], SCIF_ENDPT_QP_SIZE,
  770. cep->remote_dev);
  771. if (err) {
  772. dev_dbg(&cep->remote_dev->sdev->dev,
  773. "SCIFAPI accept: ep %p new %p scif_setup_qp_accept %d qp_offset 0x%llx\n",
  774. lep, cep, err, cep->qp_info.qp_offset);
  775. scif_put_peer_dev(spdev);
  776. goto scif_accept_error_map;
  777. }
  778. cep->port.node = lep->port.node;
  779. cep->port.port = lep->port.port;
  780. cep->peer.node = peer->node;
  781. cep->peer.port = peer->port;
  782. init_waitqueue_head(&cep->sendwq);
  783. init_waitqueue_head(&cep->recvwq);
  784. init_waitqueue_head(&cep->conwq);
  785. msg.uop = SCIF_CNCT_GNT;
  786. msg.src = cep->port;
  787. msg.payload[0] = cep->remote_ep;
  788. msg.payload[1] = cep->qp_info.qp_offset;
  789. msg.payload[2] = (u64)cep;
  790. err = _scif_nodeqp_send(cep->remote_dev, &msg);
  791. scif_put_peer_dev(spdev);
  792. if (err)
  793. goto scif_accept_error_map;
  794. retry:
  795. /* Wait for the remote node to respond with SCIF_CNCT_GNT(N)ACK */
  796. err = wait_event_timeout(cep->conwq, cep->state != SCIFEP_CONNECTING,
  797. SCIF_NODE_ACCEPT_TIMEOUT);
  798. if (!err && scifdev_alive(cep))
  799. goto retry;
  800. err = !err ? -ENODEV : 0;
  801. if (err)
  802. goto scif_accept_error_map;
  803. kfree(conreq);
  804. spin_lock(&cep->lock);
  805. if (cep->state == SCIFEP_CLOSING) {
  806. /*
  807. * Remote failed to allocate resources and NAKed the grant.
  808. * There is at this point nothing referencing the new end point.
  809. */
  810. spin_unlock(&cep->lock);
  811. scif_teardown_ep(cep);
  812. kfree(cep);
  813. /* If call with sync flag then go back and wait. */
  814. if (flags & SCIF_ACCEPT_SYNC) {
  815. spin_lock(&lep->lock);
  816. goto retry_connection;
  817. }
  818. return -EAGAIN;
  819. }
  820. scif_get_port(cep->port.port);
  821. *newepd = (scif_epd_t)cep;
  822. spin_unlock(&cep->lock);
  823. return 0;
  824. scif_accept_error_map:
  825. scif_anon_inode_fput(cep);
  826. scif_accept_error_anon_inode:
  827. scif_teardown_ep(cep);
  828. scif_accept_error_qpalloc:
  829. kfree(cep);
  830. scif_accept_error_epalloc:
  831. msg.uop = SCIF_CNCT_REJ;
  832. msg.dst.node = conreq->msg.src.node;
  833. msg.dst.port = conreq->msg.src.port;
  834. msg.payload[0] = conreq->msg.payload[0];
  835. msg.payload[1] = conreq->msg.payload[1];
  836. scif_nodeqp_send(&scif_dev[conreq->msg.src.node], &msg);
  837. kfree(conreq);
  838. return err;
  839. }
  840. EXPORT_SYMBOL_GPL(scif_accept);
  841. /*
  842. * scif_msg_param_check:
  843. * @epd: The end point returned from scif_open()
  844. * @len: Length to receive
  845. * @flags: blocking or non blocking
  846. *
  847. * Validate parameters for messaging APIs scif_send(..)/scif_recv(..).
  848. */
  849. static inline int scif_msg_param_check(scif_epd_t epd, int len, int flags)
  850. {
  851. int ret = -EINVAL;
  852. if (len < 0)
  853. goto err_ret;
  854. if (flags && (!(flags & SCIF_RECV_BLOCK)))
  855. goto err_ret;
  856. ret = 0;
  857. err_ret:
  858. return ret;
  859. }
  860. static int _scif_send(scif_epd_t epd, void *msg, int len, int flags)
  861. {
  862. struct scif_endpt *ep = (struct scif_endpt *)epd;
  863. struct scifmsg notif_msg;
  864. int curr_xfer_len = 0, sent_len = 0, write_count;
  865. int ret = 0;
  866. struct scif_qp *qp = ep->qp_info.qp;
  867. if (flags & SCIF_SEND_BLOCK)
  868. might_sleep();
  869. spin_lock(&ep->lock);
  870. while (sent_len != len && SCIFEP_CONNECTED == ep->state) {
  871. write_count = scif_rb_space(&qp->outbound_q);
  872. if (write_count) {
  873. /* Best effort to send as much data as possible */
  874. curr_xfer_len = min(len - sent_len, write_count);
  875. ret = scif_rb_write(&qp->outbound_q, msg,
  876. curr_xfer_len);
  877. if (ret < 0)
  878. break;
  879. /* Success. Update write pointer */
  880. scif_rb_commit(&qp->outbound_q);
  881. /*
  882. * Send a notification to the peer about the
  883. * produced data message.
  884. */
  885. notif_msg.src = ep->port;
  886. notif_msg.uop = SCIF_CLIENT_SENT;
  887. notif_msg.payload[0] = ep->remote_ep;
  888. ret = _scif_nodeqp_send(ep->remote_dev, &notif_msg);
  889. if (ret)
  890. break;
  891. sent_len += curr_xfer_len;
  892. msg = msg + curr_xfer_len;
  893. continue;
  894. }
  895. curr_xfer_len = min(len - sent_len, SCIF_ENDPT_QP_SIZE - 1);
  896. /* Not enough RB space. return for the Non Blocking case */
  897. if (!(flags & SCIF_SEND_BLOCK))
  898. break;
  899. spin_unlock(&ep->lock);
  900. /* Wait for a SCIF_CLIENT_RCVD message in the Blocking case */
  901. ret =
  902. wait_event_interruptible(ep->sendwq,
  903. (SCIFEP_CONNECTED != ep->state) ||
  904. (scif_rb_space(&qp->outbound_q) >=
  905. curr_xfer_len));
  906. spin_lock(&ep->lock);
  907. if (ret)
  908. break;
  909. }
  910. if (sent_len)
  911. ret = sent_len;
  912. else if (!ret && SCIFEP_CONNECTED != ep->state)
  913. ret = SCIFEP_DISCONNECTED == ep->state ?
  914. -ECONNRESET : -ENOTCONN;
  915. spin_unlock(&ep->lock);
  916. return ret;
  917. }
  918. static int _scif_recv(scif_epd_t epd, void *msg, int len, int flags)
  919. {
  920. int read_size;
  921. struct scif_endpt *ep = (struct scif_endpt *)epd;
  922. struct scifmsg notif_msg;
  923. int curr_recv_len = 0, remaining_len = len, read_count;
  924. int ret = 0;
  925. struct scif_qp *qp = ep->qp_info.qp;
  926. if (flags & SCIF_RECV_BLOCK)
  927. might_sleep();
  928. spin_lock(&ep->lock);
  929. while (remaining_len && (SCIFEP_CONNECTED == ep->state ||
  930. SCIFEP_DISCONNECTED == ep->state)) {
  931. read_count = scif_rb_count(&qp->inbound_q, remaining_len);
  932. if (read_count) {
  933. /*
  934. * Best effort to recv as much data as there
  935. * are bytes to read in the RB particularly
  936. * important for the Non Blocking case.
  937. */
  938. curr_recv_len = min(remaining_len, read_count);
  939. read_size = scif_rb_get_next(&qp->inbound_q,
  940. msg, curr_recv_len);
  941. if (ep->state == SCIFEP_CONNECTED) {
  942. /*
  943. * Update the read pointer only if the endpoint
  944. * is still connected else the read pointer
  945. * might no longer exist since the peer has
  946. * freed resources!
  947. */
  948. scif_rb_update_read_ptr(&qp->inbound_q);
  949. /*
  950. * Send a notification to the peer about the
  951. * consumed data message only if the EP is in
  952. * SCIFEP_CONNECTED state.
  953. */
  954. notif_msg.src = ep->port;
  955. notif_msg.uop = SCIF_CLIENT_RCVD;
  956. notif_msg.payload[0] = ep->remote_ep;
  957. ret = _scif_nodeqp_send(ep->remote_dev,
  958. &notif_msg);
  959. if (ret)
  960. break;
  961. }
  962. remaining_len -= curr_recv_len;
  963. msg = msg + curr_recv_len;
  964. continue;
  965. }
  966. /*
  967. * Bail out now if the EP is in SCIFEP_DISCONNECTED state else
  968. * we will keep looping forever.
  969. */
  970. if (ep->state == SCIFEP_DISCONNECTED)
  971. break;
  972. /*
  973. * Return in the Non Blocking case if there is no data
  974. * to read in this iteration.
  975. */
  976. if (!(flags & SCIF_RECV_BLOCK))
  977. break;
  978. curr_recv_len = min(remaining_len, SCIF_ENDPT_QP_SIZE - 1);
  979. spin_unlock(&ep->lock);
  980. /*
  981. * Wait for a SCIF_CLIENT_SEND message in the blocking case
  982. * or until other side disconnects.
  983. */
  984. ret =
  985. wait_event_interruptible(ep->recvwq,
  986. SCIFEP_CONNECTED != ep->state ||
  987. scif_rb_count(&qp->inbound_q,
  988. curr_recv_len)
  989. >= curr_recv_len);
  990. spin_lock(&ep->lock);
  991. if (ret)
  992. break;
  993. }
  994. if (len - remaining_len)
  995. ret = len - remaining_len;
  996. else if (!ret && ep->state != SCIFEP_CONNECTED)
  997. ret = ep->state == SCIFEP_DISCONNECTED ?
  998. -ECONNRESET : -ENOTCONN;
  999. spin_unlock(&ep->lock);
  1000. return ret;
  1001. }
  1002. /**
  1003. * scif_user_send() - Send data to connection queue
  1004. * @epd: The end point returned from scif_open()
  1005. * @msg: Address to place data
  1006. * @len: Length to receive
  1007. * @flags: blocking or non blocking
  1008. *
  1009. * This function is called from the driver IOCTL entry point
  1010. * only and is a wrapper for _scif_send().
  1011. */
  1012. int scif_user_send(scif_epd_t epd, void __user *msg, int len, int flags)
  1013. {
  1014. struct scif_endpt *ep = (struct scif_endpt *)epd;
  1015. int err = 0;
  1016. int sent_len = 0;
  1017. char *tmp;
  1018. int loop_len;
  1019. int chunk_len = min(len, (1 << (MAX_ORDER + PAGE_SHIFT - 1)));
  1020. dev_dbg(scif_info.mdev.this_device,
  1021. "SCIFAPI send (U): ep %p %s\n", ep, scif_ep_states[ep->state]);
  1022. if (!len)
  1023. return 0;
  1024. err = scif_msg_param_check(epd, len, flags);
  1025. if (err)
  1026. goto send_err;
  1027. tmp = kmalloc(chunk_len, GFP_KERNEL);
  1028. if (!tmp) {
  1029. err = -ENOMEM;
  1030. goto send_err;
  1031. }
  1032. /*
  1033. * Grabbing the lock before breaking up the transfer in
  1034. * multiple chunks is required to ensure that messages do
  1035. * not get fragmented and reordered.
  1036. */
  1037. mutex_lock(&ep->sendlock);
  1038. while (sent_len != len) {
  1039. loop_len = len - sent_len;
  1040. loop_len = min(chunk_len, loop_len);
  1041. if (copy_from_user(tmp, msg, loop_len)) {
  1042. err = -EFAULT;
  1043. goto send_free_err;
  1044. }
  1045. err = _scif_send(epd, tmp, loop_len, flags);
  1046. if (err < 0)
  1047. goto send_free_err;
  1048. sent_len += err;
  1049. msg += err;
  1050. if (err != loop_len)
  1051. goto send_free_err;
  1052. }
  1053. send_free_err:
  1054. mutex_unlock(&ep->sendlock);
  1055. kfree(tmp);
  1056. send_err:
  1057. return err < 0 ? err : sent_len;
  1058. }
  1059. /**
  1060. * scif_user_recv() - Receive data from connection queue
  1061. * @epd: The end point returned from scif_open()
  1062. * @msg: Address to place data
  1063. * @len: Length to receive
  1064. * @flags: blocking or non blocking
  1065. *
  1066. * This function is called from the driver IOCTL entry point
  1067. * only and is a wrapper for _scif_recv().
  1068. */
  1069. int scif_user_recv(scif_epd_t epd, void __user *msg, int len, int flags)
  1070. {
  1071. struct scif_endpt *ep = (struct scif_endpt *)epd;
  1072. int err = 0;
  1073. int recv_len = 0;
  1074. char *tmp;
  1075. int loop_len;
  1076. int chunk_len = min(len, (1 << (MAX_ORDER + PAGE_SHIFT - 1)));
  1077. dev_dbg(scif_info.mdev.this_device,
  1078. "SCIFAPI recv (U): ep %p %s\n", ep, scif_ep_states[ep->state]);
  1079. if (!len)
  1080. return 0;
  1081. err = scif_msg_param_check(epd, len, flags);
  1082. if (err)
  1083. goto recv_err;
  1084. tmp = kmalloc(chunk_len, GFP_KERNEL);
  1085. if (!tmp) {
  1086. err = -ENOMEM;
  1087. goto recv_err;
  1088. }
  1089. /*
  1090. * Grabbing the lock before breaking up the transfer in
  1091. * multiple chunks is required to ensure that messages do
  1092. * not get fragmented and reordered.
  1093. */
  1094. mutex_lock(&ep->recvlock);
  1095. while (recv_len != len) {
  1096. loop_len = len - recv_len;
  1097. loop_len = min(chunk_len, loop_len);
  1098. err = _scif_recv(epd, tmp, loop_len, flags);
  1099. if (err < 0)
  1100. goto recv_free_err;
  1101. if (copy_to_user(msg, tmp, err)) {
  1102. err = -EFAULT;
  1103. goto recv_free_err;
  1104. }
  1105. recv_len += err;
  1106. msg += err;
  1107. if (err != loop_len)
  1108. goto recv_free_err;
  1109. }
  1110. recv_free_err:
  1111. mutex_unlock(&ep->recvlock);
  1112. kfree(tmp);
  1113. recv_err:
  1114. return err < 0 ? err : recv_len;
  1115. }
  1116. /**
  1117. * scif_send() - Send data to connection queue
  1118. * @epd: The end point returned from scif_open()
  1119. * @msg: Address to place data
  1120. * @len: Length to receive
  1121. * @flags: blocking or non blocking
  1122. *
  1123. * This function is called from the kernel mode only and is
  1124. * a wrapper for _scif_send().
  1125. */
  1126. int scif_send(scif_epd_t epd, void *msg, int len, int flags)
  1127. {
  1128. struct scif_endpt *ep = (struct scif_endpt *)epd;
  1129. int ret;
  1130. dev_dbg(scif_info.mdev.this_device,
  1131. "SCIFAPI send (K): ep %p %s\n", ep, scif_ep_states[ep->state]);
  1132. if (!len)
  1133. return 0;
  1134. ret = scif_msg_param_check(epd, len, flags);
  1135. if (ret)
  1136. return ret;
  1137. if (!ep->remote_dev)
  1138. return -ENOTCONN;
  1139. /*
  1140. * Grab the mutex lock in the blocking case only
  1141. * to ensure messages do not get fragmented/reordered.
  1142. * The non blocking mode is protected using spin locks
  1143. * in _scif_send().
  1144. */
  1145. if (flags & SCIF_SEND_BLOCK)
  1146. mutex_lock(&ep->sendlock);
  1147. ret = _scif_send(epd, msg, len, flags);
  1148. if (flags & SCIF_SEND_BLOCK)
  1149. mutex_unlock(&ep->sendlock);
  1150. return ret;
  1151. }
  1152. EXPORT_SYMBOL_GPL(scif_send);
  1153. /**
  1154. * scif_recv() - Receive data from connection queue
  1155. * @epd: The end point returned from scif_open()
  1156. * @msg: Address to place data
  1157. * @len: Length to receive
  1158. * @flags: blocking or non blocking
  1159. *
  1160. * This function is called from the kernel mode only and is
  1161. * a wrapper for _scif_recv().
  1162. */
  1163. int scif_recv(scif_epd_t epd, void *msg, int len, int flags)
  1164. {
  1165. struct scif_endpt *ep = (struct scif_endpt *)epd;
  1166. int ret;
  1167. dev_dbg(scif_info.mdev.this_device,
  1168. "SCIFAPI recv (K): ep %p %s\n", ep, scif_ep_states[ep->state]);
  1169. if (!len)
  1170. return 0;
  1171. ret = scif_msg_param_check(epd, len, flags);
  1172. if (ret)
  1173. return ret;
  1174. /*
  1175. * Grab the mutex lock in the blocking case only
  1176. * to ensure messages do not get fragmented/reordered.
  1177. * The non blocking mode is protected using spin locks
  1178. * in _scif_send().
  1179. */
  1180. if (flags & SCIF_RECV_BLOCK)
  1181. mutex_lock(&ep->recvlock);
  1182. ret = _scif_recv(epd, msg, len, flags);
  1183. if (flags & SCIF_RECV_BLOCK)
  1184. mutex_unlock(&ep->recvlock);
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL_GPL(scif_recv);
  1188. static inline void _scif_poll_wait(struct file *f, wait_queue_head_t *wq,
  1189. poll_table *p, struct scif_endpt *ep)
  1190. {
  1191. /*
  1192. * Because poll_wait makes a GFP_KERNEL allocation, give up the lock
  1193. * and regrab it afterwards. Because the endpoint state might have
  1194. * changed while the lock was given up, the state must be checked
  1195. * again after re-acquiring the lock. The code in __scif_pollfd(..)
  1196. * does this.
  1197. */
  1198. spin_unlock(&ep->lock);
  1199. poll_wait(f, wq, p);
  1200. spin_lock(&ep->lock);
  1201. }
  1202. __poll_t
  1203. __scif_pollfd(struct file *f, poll_table *wait, struct scif_endpt *ep)
  1204. {
  1205. __poll_t mask = 0;
  1206. dev_dbg(scif_info.mdev.this_device,
  1207. "SCIFAPI pollfd: ep %p %s\n", ep, scif_ep_states[ep->state]);
  1208. spin_lock(&ep->lock);
  1209. /* Endpoint is waiting for a non-blocking connect to complete */
  1210. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS) {
  1211. _scif_poll_wait(f, &ep->conn_pend_wq, wait, ep);
  1212. if (ep->conn_async_state == ASYNC_CONN_INPROGRESS) {
  1213. if (ep->state == SCIFEP_CONNECTED ||
  1214. ep->state == SCIFEP_DISCONNECTED ||
  1215. ep->conn_err)
  1216. mask |= EPOLLOUT;
  1217. goto exit;
  1218. }
  1219. }
  1220. /* Endpoint is listening for incoming connection requests */
  1221. if (ep->state == SCIFEP_LISTENING) {
  1222. _scif_poll_wait(f, &ep->conwq, wait, ep);
  1223. if (ep->state == SCIFEP_LISTENING) {
  1224. if (ep->conreqcnt)
  1225. mask |= EPOLLIN;
  1226. goto exit;
  1227. }
  1228. }
  1229. /* Endpoint is connected or disconnected */
  1230. if (ep->state == SCIFEP_CONNECTED || ep->state == SCIFEP_DISCONNECTED) {
  1231. if (poll_requested_events(wait) & EPOLLIN)
  1232. _scif_poll_wait(f, &ep->recvwq, wait, ep);
  1233. if (poll_requested_events(wait) & EPOLLOUT)
  1234. _scif_poll_wait(f, &ep->sendwq, wait, ep);
  1235. if (ep->state == SCIFEP_CONNECTED ||
  1236. ep->state == SCIFEP_DISCONNECTED) {
  1237. /* Data can be read without blocking */
  1238. if (scif_rb_count(&ep->qp_info.qp->inbound_q, 1))
  1239. mask |= EPOLLIN;
  1240. /* Data can be written without blocking */
  1241. if (scif_rb_space(&ep->qp_info.qp->outbound_q))
  1242. mask |= EPOLLOUT;
  1243. /* Return EPOLLHUP if endpoint is disconnected */
  1244. if (ep->state == SCIFEP_DISCONNECTED)
  1245. mask |= EPOLLHUP;
  1246. goto exit;
  1247. }
  1248. }
  1249. /* Return EPOLLERR if the endpoint is in none of the above states */
  1250. mask |= EPOLLERR;
  1251. exit:
  1252. spin_unlock(&ep->lock);
  1253. return mask;
  1254. }
  1255. /**
  1256. * scif_poll() - Kernel mode SCIF poll
  1257. * @ufds: Array of scif_pollepd structures containing the end points
  1258. * and events to poll on
  1259. * @nfds: Size of the ufds array
  1260. * @timeout_msecs: Timeout in msecs, -ve implies infinite timeout
  1261. *
  1262. * The code flow in this function is based on do_poll(..) in select.c
  1263. *
  1264. * Returns the number of endpoints which have pending events or 0 in
  1265. * the event of a timeout. If a signal is used for wake up, -EINTR is
  1266. * returned.
  1267. */
  1268. int
  1269. scif_poll(struct scif_pollepd *ufds, unsigned int nfds, long timeout_msecs)
  1270. {
  1271. struct poll_wqueues table;
  1272. poll_table *pt;
  1273. int i, count = 0, timed_out = timeout_msecs == 0;
  1274. __poll_t mask;
  1275. u64 timeout = timeout_msecs < 0 ? MAX_SCHEDULE_TIMEOUT
  1276. : msecs_to_jiffies(timeout_msecs);
  1277. poll_initwait(&table);
  1278. pt = &table.pt;
  1279. while (1) {
  1280. for (i = 0; i < nfds; i++) {
  1281. pt->_key = ufds[i].events | EPOLLERR | EPOLLHUP;
  1282. mask = __scif_pollfd(ufds[i].epd->anon,
  1283. pt, ufds[i].epd);
  1284. mask &= ufds[i].events | EPOLLERR | EPOLLHUP;
  1285. if (mask) {
  1286. count++;
  1287. pt->_qproc = NULL;
  1288. }
  1289. ufds[i].revents = mask;
  1290. }
  1291. pt->_qproc = NULL;
  1292. if (!count) {
  1293. count = table.error;
  1294. if (signal_pending(current))
  1295. count = -EINTR;
  1296. }
  1297. if (count || timed_out)
  1298. break;
  1299. if (!schedule_timeout_interruptible(timeout))
  1300. timed_out = 1;
  1301. }
  1302. poll_freewait(&table);
  1303. return count;
  1304. }
  1305. EXPORT_SYMBOL_GPL(scif_poll);
  1306. int scif_get_node_ids(u16 *nodes, int len, u16 *self)
  1307. {
  1308. int online = 0;
  1309. int offset = 0;
  1310. int node;
  1311. if (!scif_is_mgmt_node())
  1312. scif_get_node_info();
  1313. *self = scif_info.nodeid;
  1314. mutex_lock(&scif_info.conflock);
  1315. len = min_t(int, len, scif_info.total);
  1316. for (node = 0; node <= scif_info.maxid; node++) {
  1317. if (_scifdev_alive(&scif_dev[node])) {
  1318. online++;
  1319. if (offset < len)
  1320. nodes[offset++] = node;
  1321. }
  1322. }
  1323. dev_dbg(scif_info.mdev.this_device,
  1324. "SCIFAPI get_node_ids total %d online %d filled in %d nodes\n",
  1325. scif_info.total, online, offset);
  1326. mutex_unlock(&scif_info.conflock);
  1327. return online;
  1328. }
  1329. EXPORT_SYMBOL_GPL(scif_get_node_ids);
  1330. static int scif_add_client_dev(struct device *dev, struct subsys_interface *si)
  1331. {
  1332. struct scif_client *client =
  1333. container_of(si, struct scif_client, si);
  1334. struct scif_peer_dev *spdev =
  1335. container_of(dev, struct scif_peer_dev, dev);
  1336. if (client->probe)
  1337. client->probe(spdev);
  1338. return 0;
  1339. }
  1340. static void scif_remove_client_dev(struct device *dev,
  1341. struct subsys_interface *si)
  1342. {
  1343. struct scif_client *client =
  1344. container_of(si, struct scif_client, si);
  1345. struct scif_peer_dev *spdev =
  1346. container_of(dev, struct scif_peer_dev, dev);
  1347. if (client->remove)
  1348. client->remove(spdev);
  1349. }
  1350. void scif_client_unregister(struct scif_client *client)
  1351. {
  1352. subsys_interface_unregister(&client->si);
  1353. }
  1354. EXPORT_SYMBOL_GPL(scif_client_unregister);
  1355. int scif_client_register(struct scif_client *client)
  1356. {
  1357. struct subsys_interface *si = &client->si;
  1358. si->name = client->name;
  1359. si->subsys = &scif_peer_bus;
  1360. si->add_dev = scif_add_client_dev;
  1361. si->remove_dev = scif_remove_client_dev;
  1362. return subsys_interface_register(&client->si);
  1363. }
  1364. EXPORT_SYMBOL_GPL(scif_client_register);