file_ops.c 42 KB

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