scif_fd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "scif_main.h"
  19. static int scif_fdopen(struct inode *inode, struct file *f)
  20. {
  21. struct scif_endpt *priv = scif_open();
  22. if (!priv)
  23. return -ENOMEM;
  24. f->private_data = priv;
  25. return 0;
  26. }
  27. static int scif_fdclose(struct inode *inode, struct file *f)
  28. {
  29. struct scif_endpt *priv = f->private_data;
  30. return scif_close(priv);
  31. }
  32. static int scif_fdmmap(struct file *f, struct vm_area_struct *vma)
  33. {
  34. struct scif_endpt *priv = f->private_data;
  35. return scif_mmap(vma, priv);
  36. }
  37. static unsigned int scif_fdpoll(struct file *f, poll_table *wait)
  38. {
  39. struct scif_endpt *priv = f->private_data;
  40. return __scif_pollfd(f, wait, priv);
  41. }
  42. static int scif_fdflush(struct file *f, fl_owner_t id)
  43. {
  44. struct scif_endpt *ep = f->private_data;
  45. spin_lock(&ep->lock);
  46. /*
  47. * The listening endpoint stashes the open file information before
  48. * waiting for incoming connections. The release callback would never be
  49. * called if the application closed the endpoint, while waiting for
  50. * incoming connections from a separate thread since the file descriptor
  51. * reference count is bumped up in the accept IOCTL. Call the flush
  52. * routine if the id matches the endpoint open file information so that
  53. * the listening endpoint can be woken up and the fd released.
  54. */
  55. if (ep->files == id)
  56. __scif_flush(ep);
  57. spin_unlock(&ep->lock);
  58. return 0;
  59. }
  60. static __always_inline void scif_err_debug(int err, const char *str)
  61. {
  62. /*
  63. * ENOTCONN is a common uninteresting error which is
  64. * flooding debug messages to the console unnecessarily.
  65. */
  66. if (err < 0 && err != -ENOTCONN)
  67. dev_dbg(scif_info.mdev.this_device, "%s err %d\n", str, err);
  68. }
  69. static long scif_fdioctl(struct file *f, unsigned int cmd, unsigned long arg)
  70. {
  71. struct scif_endpt *priv = f->private_data;
  72. void __user *argp = (void __user *)arg;
  73. int err = 0;
  74. struct scifioctl_msg request;
  75. bool non_block = false;
  76. non_block = !!(f->f_flags & O_NONBLOCK);
  77. switch (cmd) {
  78. case SCIF_BIND:
  79. {
  80. int pn;
  81. if (copy_from_user(&pn, argp, sizeof(pn)))
  82. return -EFAULT;
  83. pn = scif_bind(priv, pn);
  84. if (pn < 0)
  85. return pn;
  86. if (copy_to_user(argp, &pn, sizeof(pn)))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. case SCIF_LISTEN:
  91. return scif_listen(priv, arg);
  92. case SCIF_CONNECT:
  93. {
  94. struct scifioctl_connect req;
  95. struct scif_endpt *ep = (struct scif_endpt *)priv;
  96. if (copy_from_user(&req, argp, sizeof(req)))
  97. return -EFAULT;
  98. err = __scif_connect(priv, &req.peer, non_block);
  99. if (err < 0)
  100. return err;
  101. req.self.node = ep->port.node;
  102. req.self.port = ep->port.port;
  103. if (copy_to_user(argp, &req, sizeof(req)))
  104. return -EFAULT;
  105. return 0;
  106. }
  107. /*
  108. * Accept is done in two halves. The request ioctl does the basic
  109. * functionality of accepting the request and returning the information
  110. * about it including the internal ID of the end point. The register
  111. * is done with the internal ID on a new file descriptor opened by the
  112. * requesting process.
  113. */
  114. case SCIF_ACCEPTREQ:
  115. {
  116. struct scifioctl_accept request;
  117. scif_epd_t *ep = (scif_epd_t *)&request.endpt;
  118. if (copy_from_user(&request, argp, sizeof(request)))
  119. return -EFAULT;
  120. err = scif_accept(priv, &request.peer, ep, request.flags);
  121. if (err < 0)
  122. return err;
  123. if (copy_to_user(argp, &request, sizeof(request))) {
  124. scif_close(*ep);
  125. return -EFAULT;
  126. }
  127. /*
  128. * Add to the list of user mode eps where the second half
  129. * of the accept is not yet completed.
  130. */
  131. mutex_lock(&scif_info.eplock);
  132. list_add_tail(&((*ep)->miacceptlist), &scif_info.uaccept);
  133. list_add_tail(&((*ep)->liacceptlist), &priv->li_accept);
  134. (*ep)->listenep = priv;
  135. priv->acceptcnt++;
  136. mutex_unlock(&scif_info.eplock);
  137. return 0;
  138. }
  139. case SCIF_ACCEPTREG:
  140. {
  141. struct scif_endpt *priv = f->private_data;
  142. struct scif_endpt *newep;
  143. struct scif_endpt *lisep;
  144. struct scif_endpt *fep = NULL;
  145. struct scif_endpt *tmpep;
  146. struct list_head *pos, *tmpq;
  147. /* Finally replace the pointer to the accepted endpoint */
  148. if (copy_from_user(&newep, argp, sizeof(void *)))
  149. return -EFAULT;
  150. /* Remove form the user accept queue */
  151. mutex_lock(&scif_info.eplock);
  152. list_for_each_safe(pos, tmpq, &scif_info.uaccept) {
  153. tmpep = list_entry(pos,
  154. struct scif_endpt, miacceptlist);
  155. if (tmpep == newep) {
  156. list_del(pos);
  157. fep = tmpep;
  158. break;
  159. }
  160. }
  161. if (!fep) {
  162. mutex_unlock(&scif_info.eplock);
  163. return -ENOENT;
  164. }
  165. lisep = newep->listenep;
  166. list_for_each_safe(pos, tmpq, &lisep->li_accept) {
  167. tmpep = list_entry(pos,
  168. struct scif_endpt, liacceptlist);
  169. if (tmpep == newep) {
  170. list_del(pos);
  171. lisep->acceptcnt--;
  172. break;
  173. }
  174. }
  175. mutex_unlock(&scif_info.eplock);
  176. /* Free the resources automatically created from the open. */
  177. scif_anon_inode_fput(priv);
  178. scif_teardown_ep(priv);
  179. scif_add_epd_to_zombie_list(priv, !SCIF_EPLOCK_HELD);
  180. f->private_data = newep;
  181. return 0;
  182. }
  183. case SCIF_SEND:
  184. {
  185. struct scif_endpt *priv = f->private_data;
  186. if (copy_from_user(&request, argp,
  187. sizeof(struct scifioctl_msg))) {
  188. err = -EFAULT;
  189. goto send_err;
  190. }
  191. err = scif_user_send(priv, (void __user *)request.msg,
  192. request.len, request.flags);
  193. if (err < 0)
  194. goto send_err;
  195. if (copy_to_user(&
  196. ((struct scifioctl_msg __user *)argp)->out_len,
  197. &err, sizeof(err))) {
  198. err = -EFAULT;
  199. goto send_err;
  200. }
  201. err = 0;
  202. send_err:
  203. scif_err_debug(err, "scif_send");
  204. return err;
  205. }
  206. case SCIF_RECV:
  207. {
  208. struct scif_endpt *priv = f->private_data;
  209. if (copy_from_user(&request, argp,
  210. sizeof(struct scifioctl_msg))) {
  211. err = -EFAULT;
  212. goto recv_err;
  213. }
  214. err = scif_user_recv(priv, (void __user *)request.msg,
  215. request.len, request.flags);
  216. if (err < 0)
  217. goto recv_err;
  218. if (copy_to_user(&
  219. ((struct scifioctl_msg __user *)argp)->out_len,
  220. &err, sizeof(err))) {
  221. err = -EFAULT;
  222. goto recv_err;
  223. }
  224. err = 0;
  225. recv_err:
  226. scif_err_debug(err, "scif_recv");
  227. return err;
  228. }
  229. case SCIF_GET_NODEIDS:
  230. {
  231. struct scifioctl_node_ids node_ids;
  232. int entries;
  233. u16 *nodes;
  234. void __user *unodes, *uself;
  235. u16 self;
  236. if (copy_from_user(&node_ids, argp, sizeof(node_ids))) {
  237. err = -EFAULT;
  238. goto getnodes_err2;
  239. }
  240. entries = min_t(int, scif_info.maxid, node_ids.len);
  241. nodes = kmalloc_array(entries, sizeof(u16), GFP_KERNEL);
  242. if (entries && !nodes) {
  243. err = -ENOMEM;
  244. goto getnodes_err2;
  245. }
  246. node_ids.len = scif_get_node_ids(nodes, entries, &self);
  247. unodes = (void __user *)node_ids.nodes;
  248. if (copy_to_user(unodes, nodes, sizeof(u16) * entries)) {
  249. err = -EFAULT;
  250. goto getnodes_err1;
  251. }
  252. uself = (void __user *)node_ids.self;
  253. if (copy_to_user(uself, &self, sizeof(u16))) {
  254. err = -EFAULT;
  255. goto getnodes_err1;
  256. }
  257. if (copy_to_user(argp, &node_ids, sizeof(node_ids))) {
  258. err = -EFAULT;
  259. goto getnodes_err1;
  260. }
  261. getnodes_err1:
  262. kfree(nodes);
  263. getnodes_err2:
  264. return err;
  265. }
  266. case SCIF_REG:
  267. {
  268. struct scif_endpt *priv = f->private_data;
  269. struct scifioctl_reg reg;
  270. off_t ret;
  271. if (copy_from_user(&reg, argp, sizeof(reg))) {
  272. err = -EFAULT;
  273. goto reg_err;
  274. }
  275. if (reg.flags & SCIF_MAP_KERNEL) {
  276. err = -EINVAL;
  277. goto reg_err;
  278. }
  279. ret = scif_register(priv, (void *)reg.addr, reg.len,
  280. reg.offset, reg.prot, reg.flags);
  281. if (ret < 0) {
  282. err = (int)ret;
  283. goto reg_err;
  284. }
  285. if (copy_to_user(&((struct scifioctl_reg __user *)argp)
  286. ->out_offset, &ret, sizeof(reg.out_offset))) {
  287. err = -EFAULT;
  288. goto reg_err;
  289. }
  290. err = 0;
  291. reg_err:
  292. scif_err_debug(err, "scif_register");
  293. return err;
  294. }
  295. case SCIF_UNREG:
  296. {
  297. struct scif_endpt *priv = f->private_data;
  298. struct scifioctl_unreg unreg;
  299. if (copy_from_user(&unreg, argp, sizeof(unreg))) {
  300. err = -EFAULT;
  301. goto unreg_err;
  302. }
  303. err = scif_unregister(priv, unreg.offset, unreg.len);
  304. unreg_err:
  305. scif_err_debug(err, "scif_unregister");
  306. return err;
  307. }
  308. case SCIF_READFROM:
  309. {
  310. struct scif_endpt *priv = f->private_data;
  311. struct scifioctl_copy copy;
  312. if (copy_from_user(&copy, argp, sizeof(copy))) {
  313. err = -EFAULT;
  314. goto readfrom_err;
  315. }
  316. err = scif_readfrom(priv, copy.loffset, copy.len, copy.roffset,
  317. copy.flags);
  318. readfrom_err:
  319. scif_err_debug(err, "scif_readfrom");
  320. return err;
  321. }
  322. case SCIF_WRITETO:
  323. {
  324. struct scif_endpt *priv = f->private_data;
  325. struct scifioctl_copy copy;
  326. if (copy_from_user(&copy, argp, sizeof(copy))) {
  327. err = -EFAULT;
  328. goto writeto_err;
  329. }
  330. err = scif_writeto(priv, copy.loffset, copy.len, copy.roffset,
  331. copy.flags);
  332. writeto_err:
  333. scif_err_debug(err, "scif_writeto");
  334. return err;
  335. }
  336. case SCIF_VREADFROM:
  337. {
  338. struct scif_endpt *priv = f->private_data;
  339. struct scifioctl_copy copy;
  340. if (copy_from_user(&copy, argp, sizeof(copy))) {
  341. err = -EFAULT;
  342. goto vreadfrom_err;
  343. }
  344. err = scif_vreadfrom(priv, (void __force *)copy.addr, copy.len,
  345. copy.roffset, copy.flags);
  346. vreadfrom_err:
  347. scif_err_debug(err, "scif_vreadfrom");
  348. return err;
  349. }
  350. case SCIF_VWRITETO:
  351. {
  352. struct scif_endpt *priv = f->private_data;
  353. struct scifioctl_copy copy;
  354. if (copy_from_user(&copy, argp, sizeof(copy))) {
  355. err = -EFAULT;
  356. goto vwriteto_err;
  357. }
  358. err = scif_vwriteto(priv, (void __force *)copy.addr, copy.len,
  359. copy.roffset, copy.flags);
  360. vwriteto_err:
  361. scif_err_debug(err, "scif_vwriteto");
  362. return err;
  363. }
  364. case SCIF_FENCE_MARK:
  365. {
  366. struct scif_endpt *priv = f->private_data;
  367. struct scifioctl_fence_mark mark;
  368. int tmp_mark = 0;
  369. if (copy_from_user(&mark, argp, sizeof(mark))) {
  370. err = -EFAULT;
  371. goto fence_mark_err;
  372. }
  373. err = scif_fence_mark(priv, mark.flags, &tmp_mark);
  374. if (err)
  375. goto fence_mark_err;
  376. if (copy_to_user((void __user *)mark.mark, &tmp_mark,
  377. sizeof(tmp_mark))) {
  378. err = -EFAULT;
  379. goto fence_mark_err;
  380. }
  381. fence_mark_err:
  382. scif_err_debug(err, "scif_fence_mark");
  383. return err;
  384. }
  385. case SCIF_FENCE_WAIT:
  386. {
  387. struct scif_endpt *priv = f->private_data;
  388. err = scif_fence_wait(priv, arg);
  389. scif_err_debug(err, "scif_fence_wait");
  390. return err;
  391. }
  392. case SCIF_FENCE_SIGNAL:
  393. {
  394. struct scif_endpt *priv = f->private_data;
  395. struct scifioctl_fence_signal signal;
  396. if (copy_from_user(&signal, argp, sizeof(signal))) {
  397. err = -EFAULT;
  398. goto fence_signal_err;
  399. }
  400. err = scif_fence_signal(priv, signal.loff, signal.lval,
  401. signal.roff, signal.rval, signal.flags);
  402. fence_signal_err:
  403. scif_err_debug(err, "scif_fence_signal");
  404. return err;
  405. }
  406. }
  407. return -EINVAL;
  408. }
  409. const struct file_operations scif_fops = {
  410. .open = scif_fdopen,
  411. .release = scif_fdclose,
  412. .unlocked_ioctl = scif_fdioctl,
  413. .mmap = scif_fdmmap,
  414. .poll = scif_fdpoll,
  415. .flush = scif_fdflush,
  416. .owner = THIS_MODULE,
  417. };