file_ops.c 44 KB

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