file_ops.c 40 KB

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