file_ops.c 40 KB

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