file_ops.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Copyright(c) 2015-2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/poll.h>
  48. #include <linux/cdev.h>
  49. #include <linux/vmalloc.h>
  50. #include <linux/io.h>
  51. #include <linux/sched/mm.h>
  52. #include <linux/bitmap.h>
  53. #include <rdma/ib.h>
  54. #include "hfi.h"
  55. #include "pio.h"
  56. #include "device.h"
  57. #include "common.h"
  58. #include "trace.h"
  59. #include "mmu_rb.h"
  60. #include "user_sdma.h"
  61. #include "user_exp_rcv.h"
  62. #include "aspm.h"
  63. #undef pr_fmt
  64. #define pr_fmt(fmt) DRIVER_NAME ": " fmt
  65. #define SEND_CTXT_HALT_TIMEOUT 1000 /* msecs */
  66. /*
  67. * File operation functions
  68. */
  69. static int hfi1_file_open(struct inode *inode, struct file *fp);
  70. static int hfi1_file_close(struct inode *inode, struct file *fp);
  71. static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from);
  72. static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt);
  73. static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma);
  74. static u64 kvirt_to_phys(void *addr);
  75. static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len);
  76. static void init_subctxts(struct hfi1_ctxtdata *uctxt,
  77. const struct hfi1_user_info *uinfo);
  78. static int init_user_ctxt(struct hfi1_filedata *fd,
  79. struct hfi1_ctxtdata *uctxt);
  80. static void user_init(struct hfi1_ctxtdata *uctxt);
  81. static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
  82. static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len);
  83. static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
  84. u32 len);
  85. static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
  86. u32 len);
  87. static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
  88. u32 len);
  89. static int setup_base_ctxt(struct hfi1_filedata *fd,
  90. struct hfi1_ctxtdata *uctxt);
  91. static int setup_subctxt(struct hfi1_ctxtdata *uctxt);
  92. static int find_sub_ctxt(struct hfi1_filedata *fd,
  93. const struct hfi1_user_info *uinfo);
  94. static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
  95. struct hfi1_user_info *uinfo,
  96. struct hfi1_ctxtdata **cd);
  97. static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt);
  98. static unsigned int poll_urgent(struct file *fp, struct poll_table_struct *pt);
  99. static unsigned int poll_next(struct file *fp, struct poll_table_struct *pt);
  100. static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
  101. unsigned long arg);
  102. static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg);
  103. static int ctxt_reset(struct hfi1_ctxtdata *uctxt);
  104. static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
  105. unsigned long arg);
  106. static int vma_fault(struct vm_fault *vmf);
  107. static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
  108. unsigned long arg);
  109. static const struct file_operations hfi1_file_ops = {
  110. .owner = THIS_MODULE,
  111. .write_iter = hfi1_write_iter,
  112. .open = hfi1_file_open,
  113. .release = hfi1_file_close,
  114. .unlocked_ioctl = hfi1_file_ioctl,
  115. .poll = hfi1_poll,
  116. .mmap = hfi1_file_mmap,
  117. .llseek = noop_llseek,
  118. };
  119. static const struct vm_operations_struct vm_ops = {
  120. .fault = vma_fault,
  121. };
  122. /*
  123. * Types of memories mapped into user processes' space
  124. */
  125. enum mmap_types {
  126. PIO_BUFS = 1,
  127. PIO_BUFS_SOP,
  128. PIO_CRED,
  129. RCV_HDRQ,
  130. RCV_EGRBUF,
  131. UREGS,
  132. EVENTS,
  133. STATUS,
  134. RTAIL,
  135. SUBCTXT_UREGS,
  136. SUBCTXT_RCV_HDRQ,
  137. SUBCTXT_EGRBUF,
  138. SDMA_COMP
  139. };
  140. /*
  141. * Masks and offsets defining the mmap tokens
  142. */
  143. #define HFI1_MMAP_OFFSET_MASK 0xfffULL
  144. #define HFI1_MMAP_OFFSET_SHIFT 0
  145. #define HFI1_MMAP_SUBCTXT_MASK 0xfULL
  146. #define HFI1_MMAP_SUBCTXT_SHIFT 12
  147. #define HFI1_MMAP_CTXT_MASK 0xffULL
  148. #define HFI1_MMAP_CTXT_SHIFT 16
  149. #define HFI1_MMAP_TYPE_MASK 0xfULL
  150. #define HFI1_MMAP_TYPE_SHIFT 24
  151. #define HFI1_MMAP_MAGIC_MASK 0xffffffffULL
  152. #define HFI1_MMAP_MAGIC_SHIFT 32
  153. #define HFI1_MMAP_MAGIC 0xdabbad00
  154. #define HFI1_MMAP_TOKEN_SET(field, val) \
  155. (((val) & HFI1_MMAP_##field##_MASK) << HFI1_MMAP_##field##_SHIFT)
  156. #define HFI1_MMAP_TOKEN_GET(field, token) \
  157. (((token) >> HFI1_MMAP_##field##_SHIFT) & HFI1_MMAP_##field##_MASK)
  158. #define HFI1_MMAP_TOKEN(type, ctxt, subctxt, addr) \
  159. (HFI1_MMAP_TOKEN_SET(MAGIC, HFI1_MMAP_MAGIC) | \
  160. HFI1_MMAP_TOKEN_SET(TYPE, type) | \
  161. HFI1_MMAP_TOKEN_SET(CTXT, ctxt) | \
  162. HFI1_MMAP_TOKEN_SET(SUBCTXT, subctxt) | \
  163. HFI1_MMAP_TOKEN_SET(OFFSET, (offset_in_page(addr))))
  164. #define dbg(fmt, ...) \
  165. pr_info(fmt, ##__VA_ARGS__)
  166. static inline int is_valid_mmap(u64 token)
  167. {
  168. return (HFI1_MMAP_TOKEN_GET(MAGIC, token) == HFI1_MMAP_MAGIC);
  169. }
  170. static int hfi1_file_open(struct inode *inode, struct file *fp)
  171. {
  172. struct hfi1_filedata *fd;
  173. struct hfi1_devdata *dd = container_of(inode->i_cdev,
  174. struct hfi1_devdata,
  175. user_cdev);
  176. if (!((dd->flags & HFI1_PRESENT) && dd->kregbase1))
  177. return -EINVAL;
  178. if (!atomic_inc_not_zero(&dd->user_refcount))
  179. return -ENXIO;
  180. /* Just take a ref now. Not all opens result in a context assign */
  181. kobject_get(&dd->kobj);
  182. /* The real work is performed later in assign_ctxt() */
  183. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  184. if (fd) {
  185. fd->rec_cpu_num = -1; /* no cpu affinity by default */
  186. fd->mm = current->mm;
  187. mmgrab(fd->mm);
  188. fd->dd = dd;
  189. fp->private_data = fd;
  190. } else {
  191. fp->private_data = NULL;
  192. if (atomic_dec_and_test(&dd->user_refcount))
  193. complete(&dd->user_comp);
  194. return -ENOMEM;
  195. }
  196. return 0;
  197. }
  198. static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
  199. unsigned long arg)
  200. {
  201. struct hfi1_filedata *fd = fp->private_data;
  202. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  203. int ret = 0;
  204. int uval = 0;
  205. hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
  206. if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
  207. cmd != HFI1_IOCTL_GET_VERS &&
  208. !uctxt)
  209. return -EINVAL;
  210. switch (cmd) {
  211. case HFI1_IOCTL_ASSIGN_CTXT:
  212. ret = assign_ctxt(fd, arg, _IOC_SIZE(cmd));
  213. break;
  214. case HFI1_IOCTL_CTXT_INFO:
  215. ret = get_ctxt_info(fd, arg, _IOC_SIZE(cmd));
  216. break;
  217. case HFI1_IOCTL_USER_INFO:
  218. ret = get_base_info(fd, arg, _IOC_SIZE(cmd));
  219. break;
  220. case HFI1_IOCTL_CREDIT_UPD:
  221. if (uctxt)
  222. sc_return_credits(uctxt->sc);
  223. break;
  224. case HFI1_IOCTL_TID_UPDATE:
  225. ret = user_exp_rcv_setup(fd, arg, _IOC_SIZE(cmd));
  226. break;
  227. case HFI1_IOCTL_TID_FREE:
  228. ret = user_exp_rcv_clear(fd, arg, _IOC_SIZE(cmd));
  229. break;
  230. case HFI1_IOCTL_TID_INVAL_READ:
  231. ret = user_exp_rcv_invalid(fd, arg, _IOC_SIZE(cmd));
  232. break;
  233. case HFI1_IOCTL_RECV_CTRL:
  234. ret = manage_rcvq(uctxt, fd->subctxt, arg);
  235. break;
  236. case HFI1_IOCTL_POLL_TYPE:
  237. if (get_user(uval, (int __user *)arg))
  238. return -EFAULT;
  239. uctxt->poll_type = (typeof(uctxt->poll_type))uval;
  240. break;
  241. case HFI1_IOCTL_ACK_EVENT:
  242. ret = user_event_ack(uctxt, fd->subctxt, arg);
  243. break;
  244. case HFI1_IOCTL_SET_PKEY:
  245. ret = set_ctxt_pkey(uctxt, arg);
  246. break;
  247. case HFI1_IOCTL_CTXT_RESET:
  248. ret = ctxt_reset(uctxt);
  249. break;
  250. case HFI1_IOCTL_GET_VERS:
  251. uval = HFI1_USER_SWVERSION;
  252. if (put_user(uval, (int __user *)arg))
  253. return -EFAULT;
  254. break;
  255. default:
  256. return -EINVAL;
  257. }
  258. return ret;
  259. }
  260. static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
  261. {
  262. struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
  263. struct hfi1_user_sdma_pkt_q *pq = fd->pq;
  264. struct hfi1_user_sdma_comp_q *cq = fd->cq;
  265. int done = 0, reqs = 0;
  266. unsigned long dim = from->nr_segs;
  267. if (!cq || !pq)
  268. return -EIO;
  269. if (!iter_is_iovec(from) || !dim)
  270. return -EINVAL;
  271. trace_hfi1_sdma_request(fd->dd, fd->uctxt->ctxt, fd->subctxt, dim);
  272. if (atomic_read(&pq->n_reqs) == pq->n_max_reqs)
  273. return -ENOSPC;
  274. while (dim) {
  275. int ret;
  276. unsigned long count = 0;
  277. ret = hfi1_user_sdma_process_request(
  278. fd, (struct iovec *)(from->iov + done),
  279. dim, &count);
  280. if (ret) {
  281. reqs = ret;
  282. break;
  283. }
  284. dim -= count;
  285. done += count;
  286. reqs++;
  287. }
  288. return reqs;
  289. }
  290. static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma)
  291. {
  292. struct hfi1_filedata *fd = fp->private_data;
  293. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  294. struct hfi1_devdata *dd;
  295. unsigned long flags;
  296. u64 token = vma->vm_pgoff << PAGE_SHIFT,
  297. memaddr = 0;
  298. void *memvirt = NULL;
  299. u8 subctxt, mapio = 0, vmf = 0, type;
  300. ssize_t memlen = 0;
  301. int ret = 0;
  302. u16 ctxt;
  303. if (!is_valid_mmap(token) || !uctxt ||
  304. !(vma->vm_flags & VM_SHARED)) {
  305. ret = -EINVAL;
  306. goto done;
  307. }
  308. dd = uctxt->dd;
  309. ctxt = HFI1_MMAP_TOKEN_GET(CTXT, token);
  310. subctxt = HFI1_MMAP_TOKEN_GET(SUBCTXT, token);
  311. type = HFI1_MMAP_TOKEN_GET(TYPE, token);
  312. if (ctxt != uctxt->ctxt || subctxt != fd->subctxt) {
  313. ret = -EINVAL;
  314. goto done;
  315. }
  316. flags = vma->vm_flags;
  317. switch (type) {
  318. case PIO_BUFS:
  319. case PIO_BUFS_SOP:
  320. memaddr = ((dd->physaddr + TXE_PIO_SEND) +
  321. /* chip pio base */
  322. (uctxt->sc->hw_context * BIT(16))) +
  323. /* 64K PIO space / ctxt */
  324. (type == PIO_BUFS_SOP ?
  325. (TXE_PIO_SIZE / 2) : 0); /* sop? */
  326. /*
  327. * Map only the amount allocated to the context, not the
  328. * entire available context's PIO space.
  329. */
  330. memlen = PAGE_ALIGN(uctxt->sc->credits * PIO_BLOCK_SIZE);
  331. flags &= ~VM_MAYREAD;
  332. flags |= VM_DONTCOPY | VM_DONTEXPAND;
  333. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  334. mapio = 1;
  335. break;
  336. case PIO_CRED:
  337. if (flags & VM_WRITE) {
  338. ret = -EPERM;
  339. goto done;
  340. }
  341. /*
  342. * The credit return location for this context could be on the
  343. * second or third page allocated for credit returns (if number
  344. * of enabled contexts > 64 and 128 respectively).
  345. */
  346. memvirt = dd->cr_base[uctxt->numa_id].va;
  347. memaddr = virt_to_phys(memvirt) +
  348. (((u64)uctxt->sc->hw_free -
  349. (u64)dd->cr_base[uctxt->numa_id].va) & PAGE_MASK);
  350. memlen = PAGE_SIZE;
  351. flags &= ~VM_MAYWRITE;
  352. flags |= VM_DONTCOPY | VM_DONTEXPAND;
  353. /*
  354. * The driver has already allocated memory for credit
  355. * returns and programmed it into the chip. Has that
  356. * memory been flagged as non-cached?
  357. */
  358. /* vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); */
  359. mapio = 1;
  360. break;
  361. case RCV_HDRQ:
  362. memlen = uctxt->rcvhdrq_size;
  363. memvirt = uctxt->rcvhdrq;
  364. break;
  365. case RCV_EGRBUF: {
  366. unsigned long addr;
  367. int i;
  368. /*
  369. * The RcvEgr buffer need to be handled differently
  370. * as multiple non-contiguous pages need to be mapped
  371. * into the user process.
  372. */
  373. memlen = uctxt->egrbufs.size;
  374. if ((vma->vm_end - vma->vm_start) != memlen) {
  375. dd_dev_err(dd, "Eager buffer map size invalid (%lu != %lu)\n",
  376. (vma->vm_end - vma->vm_start), memlen);
  377. ret = -EINVAL;
  378. goto done;
  379. }
  380. if (vma->vm_flags & VM_WRITE) {
  381. ret = -EPERM;
  382. goto done;
  383. }
  384. vma->vm_flags &= ~VM_MAYWRITE;
  385. addr = vma->vm_start;
  386. for (i = 0 ; i < uctxt->egrbufs.numbufs; i++) {
  387. memlen = uctxt->egrbufs.buffers[i].len;
  388. memvirt = uctxt->egrbufs.buffers[i].addr;
  389. ret = remap_pfn_range(
  390. vma, addr,
  391. /*
  392. * virt_to_pfn() does the same, but
  393. * it's not available on x86_64
  394. * when CONFIG_MMU is enabled.
  395. */
  396. PFN_DOWN(__pa(memvirt)),
  397. memlen,
  398. vma->vm_page_prot);
  399. if (ret < 0)
  400. goto done;
  401. addr += memlen;
  402. }
  403. ret = 0;
  404. goto done;
  405. }
  406. case UREGS:
  407. /*
  408. * Map only the page that contains this context's user
  409. * registers.
  410. */
  411. memaddr = (unsigned long)
  412. (dd->physaddr + RXE_PER_CONTEXT_USER)
  413. + (uctxt->ctxt * RXE_PER_CONTEXT_SIZE);
  414. /*
  415. * TidFlow table is on the same page as the rest of the
  416. * user registers.
  417. */
  418. memlen = PAGE_SIZE;
  419. flags |= VM_DONTCOPY | VM_DONTEXPAND;
  420. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  421. mapio = 1;
  422. break;
  423. case EVENTS:
  424. /*
  425. * Use the page where this context's flags are. User level
  426. * knows where it's own bitmap is within the page.
  427. */
  428. memaddr = (unsigned long)
  429. (dd->events + uctxt_offset(uctxt)) & PAGE_MASK;
  430. memlen = PAGE_SIZE;
  431. /*
  432. * v3.7 removes VM_RESERVED but the effect is kept by
  433. * using VM_IO.
  434. */
  435. flags |= VM_IO | VM_DONTEXPAND;
  436. vmf = 1;
  437. break;
  438. case STATUS:
  439. if (flags & (unsigned long)(VM_WRITE | VM_EXEC)) {
  440. ret = -EPERM;
  441. goto done;
  442. }
  443. memaddr = kvirt_to_phys((void *)dd->status);
  444. memlen = PAGE_SIZE;
  445. flags |= VM_IO | VM_DONTEXPAND;
  446. break;
  447. case RTAIL:
  448. if (!HFI1_CAP_IS_USET(DMA_RTAIL)) {
  449. /*
  450. * If the memory allocation failed, the context alloc
  451. * also would have failed, so we would never get here
  452. */
  453. ret = -EINVAL;
  454. goto done;
  455. }
  456. if (flags & VM_WRITE) {
  457. ret = -EPERM;
  458. goto done;
  459. }
  460. memlen = PAGE_SIZE;
  461. memvirt = (void *)uctxt->rcvhdrtail_kvaddr;
  462. flags &= ~VM_MAYWRITE;
  463. break;
  464. case SUBCTXT_UREGS:
  465. memaddr = (u64)uctxt->subctxt_uregbase;
  466. memlen = PAGE_SIZE;
  467. flags |= VM_IO | VM_DONTEXPAND;
  468. vmf = 1;
  469. break;
  470. case SUBCTXT_RCV_HDRQ:
  471. memaddr = (u64)uctxt->subctxt_rcvhdr_base;
  472. memlen = uctxt->rcvhdrq_size * uctxt->subctxt_cnt;
  473. flags |= VM_IO | VM_DONTEXPAND;
  474. vmf = 1;
  475. break;
  476. case SUBCTXT_EGRBUF:
  477. memaddr = (u64)uctxt->subctxt_rcvegrbuf;
  478. memlen = uctxt->egrbufs.size * uctxt->subctxt_cnt;
  479. flags |= VM_IO | VM_DONTEXPAND;
  480. flags &= ~VM_MAYWRITE;
  481. vmf = 1;
  482. break;
  483. case SDMA_COMP: {
  484. struct hfi1_user_sdma_comp_q *cq = fd->cq;
  485. if (!cq) {
  486. ret = -EFAULT;
  487. goto done;
  488. }
  489. memaddr = (u64)cq->comps;
  490. memlen = PAGE_ALIGN(sizeof(*cq->comps) * cq->nentries);
  491. flags |= VM_IO | VM_DONTEXPAND;
  492. vmf = 1;
  493. break;
  494. }
  495. default:
  496. ret = -EINVAL;
  497. break;
  498. }
  499. if ((vma->vm_end - vma->vm_start) != memlen) {
  500. hfi1_cdbg(PROC, "%u:%u Memory size mismatch %lu:%lu",
  501. uctxt->ctxt, fd->subctxt,
  502. (vma->vm_end - vma->vm_start), memlen);
  503. ret = -EINVAL;
  504. goto done;
  505. }
  506. vma->vm_flags = flags;
  507. hfi1_cdbg(PROC,
  508. "%u:%u type:%u io/vf:%d/%d, addr:0x%llx, len:%lu(%lu), flags:0x%lx\n",
  509. ctxt, subctxt, type, mapio, vmf, memaddr, memlen,
  510. vma->vm_end - vma->vm_start, vma->vm_flags);
  511. if (vmf) {
  512. vma->vm_pgoff = PFN_DOWN(memaddr);
  513. vma->vm_ops = &vm_ops;
  514. ret = 0;
  515. } else if (mapio) {
  516. ret = io_remap_pfn_range(vma, vma->vm_start,
  517. PFN_DOWN(memaddr),
  518. memlen,
  519. vma->vm_page_prot);
  520. } else if (memvirt) {
  521. ret = remap_pfn_range(vma, vma->vm_start,
  522. PFN_DOWN(__pa(memvirt)),
  523. memlen,
  524. vma->vm_page_prot);
  525. } else {
  526. ret = remap_pfn_range(vma, vma->vm_start,
  527. PFN_DOWN(memaddr),
  528. memlen,
  529. vma->vm_page_prot);
  530. }
  531. done:
  532. return ret;
  533. }
  534. /*
  535. * Local (non-chip) user memory is not mapped right away but as it is
  536. * accessed by the user-level code.
  537. */
  538. static int vma_fault(struct vm_fault *vmf)
  539. {
  540. struct page *page;
  541. page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
  542. if (!page)
  543. return VM_FAULT_SIGBUS;
  544. get_page(page);
  545. vmf->page = page;
  546. return 0;
  547. }
  548. static unsigned int hfi1_poll(struct file *fp, struct poll_table_struct *pt)
  549. {
  550. struct hfi1_ctxtdata *uctxt;
  551. unsigned pollflag;
  552. uctxt = ((struct hfi1_filedata *)fp->private_data)->uctxt;
  553. if (!uctxt)
  554. pollflag = POLLERR;
  555. else if (uctxt->poll_type == HFI1_POLL_TYPE_URGENT)
  556. pollflag = poll_urgent(fp, pt);
  557. else if (uctxt->poll_type == HFI1_POLL_TYPE_ANYRCV)
  558. pollflag = poll_next(fp, pt);
  559. else /* invalid */
  560. pollflag = POLLERR;
  561. return pollflag;
  562. }
  563. static int hfi1_file_close(struct inode *inode, struct file *fp)
  564. {
  565. struct hfi1_filedata *fdata = fp->private_data;
  566. struct hfi1_ctxtdata *uctxt = fdata->uctxt;
  567. struct hfi1_devdata *dd = container_of(inode->i_cdev,
  568. struct hfi1_devdata,
  569. user_cdev);
  570. unsigned long flags, *ev;
  571. fp->private_data = NULL;
  572. if (!uctxt)
  573. goto done;
  574. hfi1_cdbg(PROC, "closing ctxt %u:%u", uctxt->ctxt, fdata->subctxt);
  575. flush_wc();
  576. /* drain user sdma queue */
  577. hfi1_user_sdma_free_queues(fdata, uctxt);
  578. /* release the cpu */
  579. hfi1_put_proc_affinity(fdata->rec_cpu_num);
  580. /* clean up rcv side */
  581. hfi1_user_exp_rcv_free(fdata);
  582. /*
  583. * fdata->uctxt is used in the above cleanup. It is not ready to be
  584. * removed until here.
  585. */
  586. fdata->uctxt = NULL;
  587. hfi1_rcd_put(uctxt);
  588. /*
  589. * Clear any left over, unhandled events so the next process that
  590. * gets this context doesn't get confused.
  591. */
  592. ev = dd->events + uctxt_offset(uctxt) + fdata->subctxt;
  593. *ev = 0;
  594. spin_lock_irqsave(&dd->uctxt_lock, flags);
  595. __clear_bit(fdata->subctxt, uctxt->in_use_ctxts);
  596. if (!bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
  597. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  598. goto done;
  599. }
  600. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  601. /*
  602. * Disable receive context and interrupt available, reset all
  603. * RcvCtxtCtrl bits to default values.
  604. */
  605. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
  606. HFI1_RCVCTRL_TIDFLOW_DIS |
  607. HFI1_RCVCTRL_INTRAVAIL_DIS |
  608. HFI1_RCVCTRL_TAILUPD_DIS |
  609. HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
  610. HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
  611. HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt);
  612. /* Clear the context's J_KEY */
  613. hfi1_clear_ctxt_jkey(dd, uctxt);
  614. /*
  615. * If a send context is allocated, reset context integrity
  616. * checks to default and disable the send context.
  617. */
  618. if (uctxt->sc) {
  619. set_pio_integrity(uctxt->sc);
  620. sc_disable(uctxt->sc);
  621. }
  622. hfi1_free_ctxt_rcv_groups(uctxt);
  623. hfi1_clear_ctxt_pkey(dd, uctxt);
  624. uctxt->event_flags = 0;
  625. deallocate_ctxt(uctxt);
  626. done:
  627. mmdrop(fdata->mm);
  628. kobject_put(&dd->kobj);
  629. if (atomic_dec_and_test(&dd->user_refcount))
  630. complete(&dd->user_comp);
  631. kfree(fdata);
  632. return 0;
  633. }
  634. /*
  635. * Convert kernel *virtual* addresses to physical addresses.
  636. * This is used to vmalloc'ed addresses.
  637. */
  638. static u64 kvirt_to_phys(void *addr)
  639. {
  640. struct page *page;
  641. u64 paddr = 0;
  642. page = vmalloc_to_page(addr);
  643. if (page)
  644. paddr = page_to_pfn(page) << PAGE_SHIFT;
  645. return paddr;
  646. }
  647. /**
  648. * complete_subctxt
  649. * @fd: valid filedata pointer
  650. *
  651. * Sub-context info can only be set up after the base context
  652. * has been completed. This is indicated by the clearing of the
  653. * HFI1_CTXT_BASE_UINIT bit.
  654. *
  655. * Wait for the bit to be cleared, and then complete the subcontext
  656. * initialization.
  657. *
  658. */
  659. static int complete_subctxt(struct hfi1_filedata *fd)
  660. {
  661. int ret;
  662. unsigned long flags;
  663. /*
  664. * sub-context info can only be set up after the base context
  665. * has been completed.
  666. */
  667. ret = wait_event_interruptible(
  668. fd->uctxt->wait,
  669. !test_bit(HFI1_CTXT_BASE_UNINIT, &fd->uctxt->event_flags));
  670. if (test_bit(HFI1_CTXT_BASE_FAILED, &fd->uctxt->event_flags))
  671. ret = -ENOMEM;
  672. /* Finish the sub-context init */
  673. if (!ret) {
  674. fd->rec_cpu_num = hfi1_get_proc_affinity(fd->uctxt->numa_id);
  675. ret = init_user_ctxt(fd, fd->uctxt);
  676. }
  677. if (ret) {
  678. hfi1_rcd_put(fd->uctxt);
  679. fd->uctxt = NULL;
  680. spin_lock_irqsave(&fd->dd->uctxt_lock, flags);
  681. __clear_bit(fd->subctxt, fd->uctxt->in_use_ctxts);
  682. spin_unlock_irqrestore(&fd->dd->uctxt_lock, flags);
  683. }
  684. return ret;
  685. }
  686. static int assign_ctxt(struct hfi1_filedata *fd, unsigned long arg, u32 len)
  687. {
  688. int ret;
  689. unsigned int swmajor;
  690. struct hfi1_ctxtdata *uctxt = NULL;
  691. struct hfi1_user_info uinfo;
  692. if (fd->uctxt)
  693. return -EINVAL;
  694. if (sizeof(uinfo) != len)
  695. return -EINVAL;
  696. if (copy_from_user(&uinfo, (void __user *)arg, sizeof(uinfo)))
  697. return -EFAULT;
  698. swmajor = uinfo.userversion >> 16;
  699. if (swmajor != HFI1_USER_SWMAJOR)
  700. return -ENODEV;
  701. if (uinfo.subctxt_cnt > HFI1_MAX_SHARED_CTXTS)
  702. return -EINVAL;
  703. /*
  704. * Acquire the mutex to protect against multiple creations of what
  705. * could be a shared base context.
  706. */
  707. mutex_lock(&hfi1_mutex);
  708. /*
  709. * Get a sub context if available (fd->uctxt will be set).
  710. * ret < 0 error, 0 no context, 1 sub-context found
  711. */
  712. ret = find_sub_ctxt(fd, &uinfo);
  713. /*
  714. * Allocate a base context if context sharing is not required or a
  715. * sub context wasn't found.
  716. */
  717. if (!ret)
  718. ret = allocate_ctxt(fd, fd->dd, &uinfo, &uctxt);
  719. mutex_unlock(&hfi1_mutex);
  720. /* Depending on the context type, finish the appropriate init */
  721. switch (ret) {
  722. case 0:
  723. ret = setup_base_ctxt(fd, uctxt);
  724. if (ret)
  725. deallocate_ctxt(uctxt);
  726. break;
  727. case 1:
  728. ret = complete_subctxt(fd);
  729. break;
  730. default:
  731. break;
  732. }
  733. return ret;
  734. }
  735. /**
  736. * match_ctxt
  737. * @fd: valid filedata pointer
  738. * @uinfo: user info to compare base context with
  739. * @uctxt: context to compare uinfo to.
  740. *
  741. * Compare the given context with the given information to see if it
  742. * can be used for a sub context.
  743. */
  744. static int match_ctxt(struct hfi1_filedata *fd,
  745. const struct hfi1_user_info *uinfo,
  746. struct hfi1_ctxtdata *uctxt)
  747. {
  748. struct hfi1_devdata *dd = fd->dd;
  749. unsigned long flags;
  750. u16 subctxt;
  751. /* Skip dynamically allocated kernel contexts */
  752. if (uctxt->sc && (uctxt->sc->type == SC_KERNEL))
  753. return 0;
  754. /* Skip ctxt if it doesn't match the requested one */
  755. if (memcmp(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid)) ||
  756. uctxt->jkey != generate_jkey(current_uid()) ||
  757. uctxt->subctxt_id != uinfo->subctxt_id ||
  758. uctxt->subctxt_cnt != uinfo->subctxt_cnt)
  759. return 0;
  760. /* Verify the sharing process matches the base */
  761. if (uctxt->userversion != uinfo->userversion)
  762. return -EINVAL;
  763. /* Find an unused sub context */
  764. spin_lock_irqsave(&dd->uctxt_lock, flags);
  765. if (bitmap_empty(uctxt->in_use_ctxts, HFI1_MAX_SHARED_CTXTS)) {
  766. /* context is being closed, do not use */
  767. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  768. return 0;
  769. }
  770. subctxt = find_first_zero_bit(uctxt->in_use_ctxts,
  771. HFI1_MAX_SHARED_CTXTS);
  772. if (subctxt >= uctxt->subctxt_cnt) {
  773. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  774. return -EBUSY;
  775. }
  776. fd->subctxt = subctxt;
  777. __set_bit(fd->subctxt, uctxt->in_use_ctxts);
  778. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  779. fd->uctxt = uctxt;
  780. hfi1_rcd_get(uctxt);
  781. return 1;
  782. }
  783. /**
  784. * find_sub_ctxt
  785. * @fd: valid filedata pointer
  786. * @uinfo: matching info to use to find a possible context to share.
  787. *
  788. * The hfi1_mutex must be held when this function is called. It is
  789. * necessary to ensure serialized creation of shared contexts.
  790. *
  791. * Return:
  792. * 0 No sub-context found
  793. * 1 Subcontext found and allocated
  794. * errno EINVAL (incorrect parameters)
  795. * EBUSY (all sub contexts in use)
  796. */
  797. static int find_sub_ctxt(struct hfi1_filedata *fd,
  798. const struct hfi1_user_info *uinfo)
  799. {
  800. struct hfi1_ctxtdata *uctxt;
  801. struct hfi1_devdata *dd = fd->dd;
  802. u16 i;
  803. int ret;
  804. if (!uinfo->subctxt_cnt)
  805. return 0;
  806. for (i = dd->first_dyn_alloc_ctxt; i < dd->num_rcv_contexts; i++) {
  807. uctxt = hfi1_rcd_get_by_index(dd, i);
  808. if (uctxt) {
  809. ret = match_ctxt(fd, uinfo, uctxt);
  810. hfi1_rcd_put(uctxt);
  811. /* value of != 0 will return */
  812. if (ret)
  813. return ret;
  814. }
  815. }
  816. return 0;
  817. }
  818. static int allocate_ctxt(struct hfi1_filedata *fd, struct hfi1_devdata *dd,
  819. struct hfi1_user_info *uinfo,
  820. struct hfi1_ctxtdata **rcd)
  821. {
  822. struct hfi1_ctxtdata *uctxt;
  823. int ret, numa;
  824. if (dd->flags & HFI1_FROZEN) {
  825. /*
  826. * Pick an error that is unique from all other errors
  827. * that are returned so the user process knows that
  828. * it tried to allocate while the SPC was frozen. It
  829. * it should be able to retry with success in a short
  830. * while.
  831. */
  832. return -EIO;
  833. }
  834. if (!dd->freectxts)
  835. return -EBUSY;
  836. /*
  837. * If we don't have a NUMA node requested, preference is towards
  838. * device NUMA node.
  839. */
  840. fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
  841. if (fd->rec_cpu_num != -1)
  842. numa = cpu_to_node(fd->rec_cpu_num);
  843. else
  844. numa = numa_node_id();
  845. ret = hfi1_create_ctxtdata(dd->pport, numa, &uctxt);
  846. if (ret < 0) {
  847. dd_dev_err(dd, "user ctxtdata allocation failed\n");
  848. return ret;
  849. }
  850. hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
  851. uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
  852. uctxt->numa_id);
  853. /*
  854. * Allocate and enable a PIO send context.
  855. */
  856. uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize, dd->node);
  857. if (!uctxt->sc) {
  858. ret = -ENOMEM;
  859. goto ctxdata_free;
  860. }
  861. hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
  862. uctxt->sc->hw_context);
  863. ret = sc_enable(uctxt->sc);
  864. if (ret)
  865. goto ctxdata_free;
  866. /*
  867. * Setup sub context information if the user-level has requested
  868. * sub contexts.
  869. * This has to be done here so the rest of the sub-contexts find the
  870. * proper base context.
  871. */
  872. if (uinfo->subctxt_cnt)
  873. init_subctxts(uctxt, uinfo);
  874. uctxt->userversion = uinfo->userversion;
  875. uctxt->flags = hfi1_cap_mask; /* save current flag state */
  876. init_waitqueue_head(&uctxt->wait);
  877. strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
  878. memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
  879. uctxt->jkey = generate_jkey(current_uid());
  880. hfi1_stats.sps_ctxts++;
  881. /*
  882. * Disable ASPM when there are open user/PSM contexts to avoid
  883. * issues with ASPM L1 exit latency
  884. */
  885. if (dd->freectxts-- == dd->num_user_contexts)
  886. aspm_disable_all(dd);
  887. *rcd = uctxt;
  888. return 0;
  889. ctxdata_free:
  890. hfi1_free_ctxt(uctxt);
  891. return ret;
  892. }
  893. static void deallocate_ctxt(struct hfi1_ctxtdata *uctxt)
  894. {
  895. mutex_lock(&hfi1_mutex);
  896. hfi1_stats.sps_ctxts--;
  897. if (++uctxt->dd->freectxts == uctxt->dd->num_user_contexts)
  898. aspm_enable_all(uctxt->dd);
  899. mutex_unlock(&hfi1_mutex);
  900. hfi1_free_ctxt(uctxt);
  901. }
  902. static void init_subctxts(struct hfi1_ctxtdata *uctxt,
  903. const struct hfi1_user_info *uinfo)
  904. {
  905. uctxt->subctxt_cnt = uinfo->subctxt_cnt;
  906. uctxt->subctxt_id = uinfo->subctxt_id;
  907. set_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
  908. }
  909. static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
  910. {
  911. int ret = 0;
  912. u16 num_subctxts = uctxt->subctxt_cnt;
  913. uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
  914. if (!uctxt->subctxt_uregbase)
  915. return -ENOMEM;
  916. /* We can take the size of the RcvHdr Queue from the master */
  917. uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
  918. num_subctxts);
  919. if (!uctxt->subctxt_rcvhdr_base) {
  920. ret = -ENOMEM;
  921. goto bail_ureg;
  922. }
  923. uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
  924. num_subctxts);
  925. if (!uctxt->subctxt_rcvegrbuf) {
  926. ret = -ENOMEM;
  927. goto bail_rhdr;
  928. }
  929. return 0;
  930. bail_rhdr:
  931. vfree(uctxt->subctxt_rcvhdr_base);
  932. uctxt->subctxt_rcvhdr_base = NULL;
  933. bail_ureg:
  934. vfree(uctxt->subctxt_uregbase);
  935. uctxt->subctxt_uregbase = NULL;
  936. return ret;
  937. }
  938. static void user_init(struct hfi1_ctxtdata *uctxt)
  939. {
  940. unsigned int rcvctrl_ops = 0;
  941. /* initialize poll variables... */
  942. uctxt->urgent = 0;
  943. uctxt->urgent_poll = 0;
  944. /*
  945. * Now enable the ctxt for receive.
  946. * For chips that are set to DMA the tail register to memory
  947. * when they change (and when the update bit transitions from
  948. * 0 to 1. So for those chips, we turn it off and then back on.
  949. * This will (very briefly) affect any other open ctxts, but the
  950. * duration is very short, and therefore isn't an issue. We
  951. * explicitly set the in-memory tail copy to 0 beforehand, so we
  952. * don't have to wait to be sure the DMA update has happened
  953. * (chip resets head/tail to 0 on transition to enable).
  954. */
  955. if (uctxt->rcvhdrtail_kvaddr)
  956. clear_rcvhdrtail(uctxt);
  957. /* Setup J_KEY before enabling the context */
  958. hfi1_set_ctxt_jkey(uctxt->dd, uctxt, uctxt->jkey);
  959. rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
  960. if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
  961. rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
  962. /*
  963. * Ignore the bit in the flags for now until proper
  964. * support for multiple packet per rcv array entry is
  965. * added.
  966. */
  967. if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
  968. rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
  969. if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
  970. rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
  971. if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
  972. rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
  973. /*
  974. * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
  975. * We can't rely on the correct value to be set from prior
  976. * uses of the chip or ctxt. Therefore, add the rcvctrl op
  977. * for both cases.
  978. */
  979. if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
  980. rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
  981. else
  982. rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
  983. hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt);
  984. }
  985. static int get_ctxt_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
  986. {
  987. struct hfi1_ctxt_info cinfo;
  988. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  989. if (sizeof(cinfo) != len)
  990. return -EINVAL;
  991. memset(&cinfo, 0, sizeof(cinfo));
  992. cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
  993. HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
  994. HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
  995. HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
  996. /* adjust flag if this fd is not able to cache */
  997. if (!fd->handler)
  998. cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
  999. cinfo.num_active = hfi1_count_active_units();
  1000. cinfo.unit = uctxt->dd->unit;
  1001. cinfo.ctxt = uctxt->ctxt;
  1002. cinfo.subctxt = fd->subctxt;
  1003. cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
  1004. uctxt->dd->rcv_entries.group_size) +
  1005. uctxt->expected_count;
  1006. cinfo.credits = uctxt->sc->credits;
  1007. cinfo.numa_node = uctxt->numa_id;
  1008. cinfo.rec_cpu = fd->rec_cpu_num;
  1009. cinfo.send_ctxt = uctxt->sc->hw_context;
  1010. cinfo.egrtids = uctxt->egrbufs.alloced;
  1011. cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
  1012. cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
  1013. cinfo.sdma_ring_size = fd->cq->nentries;
  1014. cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
  1015. trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
  1016. if (copy_to_user((void __user *)arg, &cinfo, len))
  1017. return -EFAULT;
  1018. return 0;
  1019. }
  1020. static int init_user_ctxt(struct hfi1_filedata *fd,
  1021. struct hfi1_ctxtdata *uctxt)
  1022. {
  1023. int ret;
  1024. ret = hfi1_user_sdma_alloc_queues(uctxt, fd);
  1025. if (ret)
  1026. return ret;
  1027. ret = hfi1_user_exp_rcv_init(fd, uctxt);
  1028. if (ret)
  1029. hfi1_user_sdma_free_queues(fd, uctxt);
  1030. return ret;
  1031. }
  1032. static int setup_base_ctxt(struct hfi1_filedata *fd,
  1033. struct hfi1_ctxtdata *uctxt)
  1034. {
  1035. struct hfi1_devdata *dd = uctxt->dd;
  1036. int ret = 0;
  1037. hfi1_init_ctxt(uctxt->sc);
  1038. /* Now allocate the RcvHdr queue and eager buffers. */
  1039. ret = hfi1_create_rcvhdrq(dd, uctxt);
  1040. if (ret)
  1041. goto done;
  1042. ret = hfi1_setup_eagerbufs(uctxt);
  1043. if (ret)
  1044. goto done;
  1045. /* If sub-contexts are enabled, do the appropriate setup */
  1046. if (uctxt->subctxt_cnt)
  1047. ret = setup_subctxt(uctxt);
  1048. if (ret)
  1049. goto done;
  1050. ret = hfi1_alloc_ctxt_rcv_groups(uctxt);
  1051. if (ret)
  1052. goto done;
  1053. ret = init_user_ctxt(fd, uctxt);
  1054. if (ret)
  1055. goto done;
  1056. user_init(uctxt);
  1057. /* Now that the context is set up, the fd can get a reference. */
  1058. fd->uctxt = uctxt;
  1059. hfi1_rcd_get(uctxt);
  1060. done:
  1061. if (uctxt->subctxt_cnt) {
  1062. /*
  1063. * On error, set the failed bit so sub-contexts will clean up
  1064. * correctly.
  1065. */
  1066. if (ret)
  1067. set_bit(HFI1_CTXT_BASE_FAILED, &uctxt->event_flags);
  1068. /*
  1069. * Base context is done (successfully or not), notify anybody
  1070. * using a sub-context that is waiting for this completion.
  1071. */
  1072. clear_bit(HFI1_CTXT_BASE_UNINIT, &uctxt->event_flags);
  1073. wake_up(&uctxt->wait);
  1074. }
  1075. return ret;
  1076. }
  1077. static int get_base_info(struct hfi1_filedata *fd, unsigned long arg, u32 len)
  1078. {
  1079. struct hfi1_base_info binfo;
  1080. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1081. struct hfi1_devdata *dd = uctxt->dd;
  1082. unsigned offset;
  1083. trace_hfi1_uctxtdata(uctxt->dd, uctxt, fd->subctxt);
  1084. if (sizeof(binfo) != len)
  1085. return -EINVAL;
  1086. memset(&binfo, 0, sizeof(binfo));
  1087. binfo.hw_version = dd->revision;
  1088. binfo.sw_version = HFI1_KERN_SWVERSION;
  1089. binfo.bthqp = kdeth_qp;
  1090. binfo.jkey = uctxt->jkey;
  1091. /*
  1092. * If more than 64 contexts are enabled the allocated credit
  1093. * return will span two or three contiguous pages. Since we only
  1094. * map the page containing the context's credit return address,
  1095. * we need to calculate the offset in the proper page.
  1096. */
  1097. offset = ((u64)uctxt->sc->hw_free -
  1098. (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
  1099. binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
  1100. fd->subctxt, offset);
  1101. binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
  1102. fd->subctxt,
  1103. uctxt->sc->base_addr);
  1104. binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
  1105. uctxt->ctxt,
  1106. fd->subctxt,
  1107. uctxt->sc->base_addr);
  1108. binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
  1109. fd->subctxt,
  1110. uctxt->rcvhdrq);
  1111. binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
  1112. fd->subctxt,
  1113. uctxt->egrbufs.rcvtids[0].dma);
  1114. binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
  1115. fd->subctxt, 0);
  1116. /*
  1117. * user regs are at
  1118. * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
  1119. */
  1120. binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
  1121. fd->subctxt, 0);
  1122. offset = offset_in_page((uctxt_offset(uctxt) + fd->subctxt) *
  1123. sizeof(*dd->events));
  1124. binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
  1125. fd->subctxt,
  1126. offset);
  1127. binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
  1128. fd->subctxt,
  1129. dd->status);
  1130. if (HFI1_CAP_IS_USET(DMA_RTAIL))
  1131. binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
  1132. fd->subctxt, 0);
  1133. if (uctxt->subctxt_cnt) {
  1134. binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
  1135. uctxt->ctxt,
  1136. fd->subctxt, 0);
  1137. binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
  1138. uctxt->ctxt,
  1139. fd->subctxt, 0);
  1140. binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
  1141. uctxt->ctxt,
  1142. fd->subctxt, 0);
  1143. }
  1144. if (copy_to_user((void __user *)arg, &binfo, len))
  1145. return -EFAULT;
  1146. return 0;
  1147. }
  1148. /**
  1149. * user_exp_rcv_setup - Set up the given tid rcv list
  1150. * @fd: file data of the current driver instance
  1151. * @arg: ioctl argumnent for user space information
  1152. * @len: length of data structure associated with ioctl command
  1153. *
  1154. * Wrapper to validate ioctl information before doing _rcv_setup.
  1155. *
  1156. */
  1157. static int user_exp_rcv_setup(struct hfi1_filedata *fd, unsigned long arg,
  1158. u32 len)
  1159. {
  1160. int ret;
  1161. unsigned long addr;
  1162. struct hfi1_tid_info tinfo;
  1163. if (sizeof(tinfo) != len)
  1164. return -EINVAL;
  1165. if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
  1166. return -EFAULT;
  1167. ret = hfi1_user_exp_rcv_setup(fd, &tinfo);
  1168. if (!ret) {
  1169. /*
  1170. * Copy the number of tidlist entries we used
  1171. * and the length of the buffer we registered.
  1172. */
  1173. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  1174. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  1175. sizeof(tinfo.tidcnt)))
  1176. return -EFAULT;
  1177. addr = arg + offsetof(struct hfi1_tid_info, length);
  1178. if (copy_to_user((void __user *)addr, &tinfo.length,
  1179. sizeof(tinfo.length)))
  1180. ret = -EFAULT;
  1181. }
  1182. return ret;
  1183. }
  1184. /**
  1185. * user_exp_rcv_clear - Clear the given tid rcv list
  1186. * @fd: file data of the current driver instance
  1187. * @arg: ioctl argumnent for user space information
  1188. * @len: length of data structure associated with ioctl command
  1189. *
  1190. * The hfi1_user_exp_rcv_clear() can be called from the error path. Because
  1191. * of this, we need to use this wrapper to copy the user space information
  1192. * before doing the clear.
  1193. */
  1194. static int user_exp_rcv_clear(struct hfi1_filedata *fd, unsigned long arg,
  1195. u32 len)
  1196. {
  1197. int ret;
  1198. unsigned long addr;
  1199. struct hfi1_tid_info tinfo;
  1200. if (sizeof(tinfo) != len)
  1201. return -EINVAL;
  1202. if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
  1203. return -EFAULT;
  1204. ret = hfi1_user_exp_rcv_clear(fd, &tinfo);
  1205. if (!ret) {
  1206. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  1207. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  1208. sizeof(tinfo.tidcnt)))
  1209. return -EFAULT;
  1210. }
  1211. return ret;
  1212. }
  1213. /**
  1214. * user_exp_rcv_invalid - Invalidate the given tid rcv list
  1215. * @fd: file data of the current driver instance
  1216. * @arg: ioctl argumnent for user space information
  1217. * @len: length of data structure associated with ioctl command
  1218. *
  1219. * Wrapper to validate ioctl information before doing _rcv_invalid.
  1220. *
  1221. */
  1222. static int user_exp_rcv_invalid(struct hfi1_filedata *fd, unsigned long arg,
  1223. u32 len)
  1224. {
  1225. int ret;
  1226. unsigned long addr;
  1227. struct hfi1_tid_info tinfo;
  1228. if (sizeof(tinfo) != len)
  1229. return -EINVAL;
  1230. if (!fd->invalid_tids)
  1231. return -EINVAL;
  1232. if (copy_from_user(&tinfo, (void __user *)arg, (sizeof(tinfo))))
  1233. return -EFAULT;
  1234. ret = hfi1_user_exp_rcv_invalid(fd, &tinfo);
  1235. if (ret)
  1236. return ret;
  1237. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  1238. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  1239. sizeof(tinfo.tidcnt)))
  1240. ret = -EFAULT;
  1241. return ret;
  1242. }
  1243. static unsigned int poll_urgent(struct file *fp,
  1244. struct poll_table_struct *pt)
  1245. {
  1246. struct hfi1_filedata *fd = fp->private_data;
  1247. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1248. struct hfi1_devdata *dd = uctxt->dd;
  1249. unsigned pollflag;
  1250. poll_wait(fp, &uctxt->wait, pt);
  1251. spin_lock_irq(&dd->uctxt_lock);
  1252. if (uctxt->urgent != uctxt->urgent_poll) {
  1253. pollflag = POLLIN | POLLRDNORM;
  1254. uctxt->urgent_poll = uctxt->urgent;
  1255. } else {
  1256. pollflag = 0;
  1257. set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
  1258. }
  1259. spin_unlock_irq(&dd->uctxt_lock);
  1260. return pollflag;
  1261. }
  1262. static unsigned int poll_next(struct file *fp,
  1263. struct poll_table_struct *pt)
  1264. {
  1265. struct hfi1_filedata *fd = fp->private_data;
  1266. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1267. struct hfi1_devdata *dd = uctxt->dd;
  1268. unsigned pollflag;
  1269. poll_wait(fp, &uctxt->wait, pt);
  1270. spin_lock_irq(&dd->uctxt_lock);
  1271. if (hdrqempty(uctxt)) {
  1272. set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
  1273. hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt);
  1274. pollflag = 0;
  1275. } else {
  1276. pollflag = POLLIN | POLLRDNORM;
  1277. }
  1278. spin_unlock_irq(&dd->uctxt_lock);
  1279. return pollflag;
  1280. }
  1281. /*
  1282. * Find all user contexts in use, and set the specified bit in their
  1283. * event mask.
  1284. * See also find_ctxt() for a similar use, that is specific to send buffers.
  1285. */
  1286. int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
  1287. {
  1288. struct hfi1_ctxtdata *uctxt;
  1289. struct hfi1_devdata *dd = ppd->dd;
  1290. u16 ctxt;
  1291. if (!dd->events)
  1292. return -EINVAL;
  1293. for (ctxt = dd->first_dyn_alloc_ctxt; ctxt < dd->num_rcv_contexts;
  1294. ctxt++) {
  1295. uctxt = hfi1_rcd_get_by_index(dd, ctxt);
  1296. if (uctxt) {
  1297. unsigned long *evs;
  1298. int i;
  1299. /*
  1300. * subctxt_cnt is 0 if not shared, so do base
  1301. * separately, first, then remaining subctxt, if any
  1302. */
  1303. evs = dd->events + uctxt_offset(uctxt);
  1304. set_bit(evtbit, evs);
  1305. for (i = 1; i < uctxt->subctxt_cnt; i++)
  1306. set_bit(evtbit, evs + i);
  1307. hfi1_rcd_put(uctxt);
  1308. }
  1309. }
  1310. return 0;
  1311. }
  1312. /**
  1313. * manage_rcvq - manage a context's receive queue
  1314. * @uctxt: the context
  1315. * @subctxt: the sub-context
  1316. * @start_stop: action to carry out
  1317. *
  1318. * start_stop == 0 disables receive on the context, for use in queue
  1319. * overflow conditions. start_stop==1 re-enables, to be used to
  1320. * re-init the software copy of the head register
  1321. */
  1322. static int manage_rcvq(struct hfi1_ctxtdata *uctxt, u16 subctxt,
  1323. unsigned long arg)
  1324. {
  1325. struct hfi1_devdata *dd = uctxt->dd;
  1326. unsigned int rcvctrl_op;
  1327. int start_stop;
  1328. if (subctxt)
  1329. return 0;
  1330. if (get_user(start_stop, (int __user *)arg))
  1331. return -EFAULT;
  1332. /* atomically clear receive enable ctxt. */
  1333. if (start_stop) {
  1334. /*
  1335. * On enable, force in-memory copy of the tail register to
  1336. * 0, so that protocol code doesn't have to worry about
  1337. * whether or not the chip has yet updated the in-memory
  1338. * copy or not on return from the system call. The chip
  1339. * always resets it's tail register back to 0 on a
  1340. * transition from disabled to enabled.
  1341. */
  1342. if (uctxt->rcvhdrtail_kvaddr)
  1343. clear_rcvhdrtail(uctxt);
  1344. rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
  1345. } else {
  1346. rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
  1347. }
  1348. hfi1_rcvctrl(dd, rcvctrl_op, uctxt);
  1349. /* always; new head should be equal to new tail; see above */
  1350. return 0;
  1351. }
  1352. /*
  1353. * clear the event notifier events for this context.
  1354. * User process then performs actions appropriate to bit having been
  1355. * set, if desired, and checks again in future.
  1356. */
  1357. static int user_event_ack(struct hfi1_ctxtdata *uctxt, u16 subctxt,
  1358. unsigned long arg)
  1359. {
  1360. int i;
  1361. struct hfi1_devdata *dd = uctxt->dd;
  1362. unsigned long *evs;
  1363. unsigned long events;
  1364. if (!dd->events)
  1365. return 0;
  1366. if (get_user(events, (unsigned long __user *)arg))
  1367. return -EFAULT;
  1368. evs = dd->events + uctxt_offset(uctxt) + subctxt;
  1369. for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
  1370. if (!test_bit(i, &events))
  1371. continue;
  1372. clear_bit(i, evs);
  1373. }
  1374. return 0;
  1375. }
  1376. static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned long arg)
  1377. {
  1378. int i;
  1379. struct hfi1_pportdata *ppd = uctxt->ppd;
  1380. struct hfi1_devdata *dd = uctxt->dd;
  1381. u16 pkey;
  1382. if (!HFI1_CAP_IS_USET(PKEY_CHECK))
  1383. return -EPERM;
  1384. if (get_user(pkey, (u16 __user *)arg))
  1385. return -EFAULT;
  1386. if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
  1387. return -EINVAL;
  1388. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
  1389. if (pkey == ppd->pkeys[i])
  1390. return hfi1_set_ctxt_pkey(dd, uctxt, pkey);
  1391. return -ENOENT;
  1392. }
  1393. /**
  1394. * ctxt_reset - Reset the user context
  1395. * @uctxt: valid user context
  1396. */
  1397. static int ctxt_reset(struct hfi1_ctxtdata *uctxt)
  1398. {
  1399. struct send_context *sc;
  1400. struct hfi1_devdata *dd;
  1401. int ret = 0;
  1402. if (!uctxt || !uctxt->dd || !uctxt->sc)
  1403. return -EINVAL;
  1404. /*
  1405. * There is no protection here. User level has to guarantee that
  1406. * no one will be writing to the send context while it is being
  1407. * re-initialized. If user level breaks that guarantee, it will
  1408. * break it's own context and no one else's.
  1409. */
  1410. dd = uctxt->dd;
  1411. sc = uctxt->sc;
  1412. /*
  1413. * Wait until the interrupt handler has marked the context as
  1414. * halted or frozen. Report error if we time out.
  1415. */
  1416. wait_event_interruptible_timeout(
  1417. sc->halt_wait, (sc->flags & SCF_HALTED),
  1418. msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
  1419. if (!(sc->flags & SCF_HALTED))
  1420. return -ENOLCK;
  1421. /*
  1422. * If the send context was halted due to a Freeze, wait until the
  1423. * device has been "unfrozen" before resetting the context.
  1424. */
  1425. if (sc->flags & SCF_FROZEN) {
  1426. wait_event_interruptible_timeout(
  1427. dd->event_queue,
  1428. !(READ_ONCE(dd->flags) & HFI1_FROZEN),
  1429. msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
  1430. if (dd->flags & HFI1_FROZEN)
  1431. return -ENOLCK;
  1432. if (dd->flags & HFI1_FORCED_FREEZE)
  1433. /*
  1434. * Don't allow context reset if we are into
  1435. * forced freeze
  1436. */
  1437. return -ENODEV;
  1438. sc_disable(sc);
  1439. ret = sc_enable(sc);
  1440. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB, uctxt);
  1441. } else {
  1442. ret = sc_restart(sc);
  1443. }
  1444. if (!ret)
  1445. sc_return_credits(sc);
  1446. return ret;
  1447. }
  1448. static void user_remove(struct hfi1_devdata *dd)
  1449. {
  1450. hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
  1451. }
  1452. static int user_add(struct hfi1_devdata *dd)
  1453. {
  1454. char name[10];
  1455. int ret;
  1456. snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
  1457. ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
  1458. &dd->user_cdev, &dd->user_device,
  1459. true, &dd->kobj);
  1460. if (ret)
  1461. user_remove(dd);
  1462. return ret;
  1463. }
  1464. /*
  1465. * Create per-unit files in /dev
  1466. */
  1467. int hfi1_device_create(struct hfi1_devdata *dd)
  1468. {
  1469. return user_add(dd);
  1470. }
  1471. /*
  1472. * Remove per-unit files in /dev
  1473. * void, core kernel returns no errors for this stuff
  1474. */
  1475. void hfi1_device_remove(struct hfi1_devdata *dd)
  1476. {
  1477. user_remove(dd);
  1478. }