file_ops.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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_filedata *fd;
  157. struct hfi1_devdata *dd = container_of(inode->i_cdev,
  158. struct hfi1_devdata,
  159. user_cdev);
  160. /* Just take a ref now. Not all opens result in a context assign */
  161. kobject_get(&dd->kobj);
  162. /* The real work is performed later in assign_ctxt() */
  163. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  164. if (fd) {
  165. fd->rec_cpu_num = -1; /* no cpu affinity by default */
  166. fd->mm = current->mm;
  167. }
  168. fp->private_data = fd;
  169. return fd ? 0 : -ENOMEM;
  170. }
  171. static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
  172. unsigned long arg)
  173. {
  174. struct hfi1_filedata *fd = fp->private_data;
  175. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  176. struct hfi1_user_info uinfo;
  177. struct hfi1_tid_info tinfo;
  178. int ret = 0;
  179. unsigned long addr;
  180. int uval = 0;
  181. unsigned long ul_uval = 0;
  182. u16 uval16 = 0;
  183. hfi1_cdbg(IOCTL, "IOCTL recv: 0x%x", cmd);
  184. if (cmd != HFI1_IOCTL_ASSIGN_CTXT &&
  185. cmd != HFI1_IOCTL_GET_VERS &&
  186. !uctxt)
  187. return -EINVAL;
  188. switch (cmd) {
  189. case HFI1_IOCTL_ASSIGN_CTXT:
  190. if (uctxt)
  191. return -EINVAL;
  192. if (copy_from_user(&uinfo,
  193. (struct hfi1_user_info __user *)arg,
  194. sizeof(uinfo)))
  195. return -EFAULT;
  196. ret = assign_ctxt(fp, &uinfo);
  197. if (ret < 0)
  198. return ret;
  199. ret = setup_ctxt(fp);
  200. if (ret)
  201. return ret;
  202. ret = user_init(fp);
  203. break;
  204. case HFI1_IOCTL_CTXT_INFO:
  205. ret = get_ctxt_info(fp, (void __user *)(unsigned long)arg,
  206. sizeof(struct hfi1_ctxt_info));
  207. break;
  208. case HFI1_IOCTL_USER_INFO:
  209. ret = get_base_info(fp, (void __user *)(unsigned long)arg,
  210. sizeof(struct hfi1_base_info));
  211. break;
  212. case HFI1_IOCTL_CREDIT_UPD:
  213. if (uctxt)
  214. sc_return_credits(uctxt->sc);
  215. break;
  216. case HFI1_IOCTL_TID_UPDATE:
  217. if (copy_from_user(&tinfo,
  218. (struct hfi11_tid_info __user *)arg,
  219. sizeof(tinfo)))
  220. return -EFAULT;
  221. ret = hfi1_user_exp_rcv_setup(fp, &tinfo);
  222. if (!ret) {
  223. /*
  224. * Copy the number of tidlist entries we used
  225. * and the length of the buffer we registered.
  226. * These fields are adjacent in the structure so
  227. * we can copy them at the same time.
  228. */
  229. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  230. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  231. sizeof(tinfo.tidcnt) +
  232. sizeof(tinfo.length)))
  233. ret = -EFAULT;
  234. }
  235. break;
  236. case HFI1_IOCTL_TID_FREE:
  237. if (copy_from_user(&tinfo,
  238. (struct hfi11_tid_info __user *)arg,
  239. sizeof(tinfo)))
  240. return -EFAULT;
  241. ret = hfi1_user_exp_rcv_clear(fp, &tinfo);
  242. if (ret)
  243. break;
  244. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  245. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  246. sizeof(tinfo.tidcnt)))
  247. ret = -EFAULT;
  248. break;
  249. case HFI1_IOCTL_TID_INVAL_READ:
  250. if (copy_from_user(&tinfo,
  251. (struct hfi11_tid_info __user *)arg,
  252. sizeof(tinfo)))
  253. return -EFAULT;
  254. ret = hfi1_user_exp_rcv_invalid(fp, &tinfo);
  255. if (ret)
  256. break;
  257. addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
  258. if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
  259. sizeof(tinfo.tidcnt)))
  260. ret = -EFAULT;
  261. break;
  262. case HFI1_IOCTL_RECV_CTRL:
  263. ret = get_user(uval, (int __user *)arg);
  264. if (ret != 0)
  265. return -EFAULT;
  266. ret = manage_rcvq(uctxt, fd->subctxt, uval);
  267. break;
  268. case HFI1_IOCTL_POLL_TYPE:
  269. ret = get_user(uval, (int __user *)arg);
  270. if (ret != 0)
  271. return -EFAULT;
  272. uctxt->poll_type = (typeof(uctxt->poll_type))uval;
  273. break;
  274. case HFI1_IOCTL_ACK_EVENT:
  275. ret = get_user(ul_uval, (unsigned long __user *)arg);
  276. if (ret != 0)
  277. return -EFAULT;
  278. ret = user_event_ack(uctxt, fd->subctxt, ul_uval);
  279. break;
  280. case HFI1_IOCTL_SET_PKEY:
  281. ret = get_user(uval16, (u16 __user *)arg);
  282. if (ret != 0)
  283. return -EFAULT;
  284. if (HFI1_CAP_IS_USET(PKEY_CHECK))
  285. ret = set_ctxt_pkey(uctxt, fd->subctxt, uval16);
  286. else
  287. return -EPERM;
  288. break;
  289. case HFI1_IOCTL_CTXT_RESET: {
  290. struct send_context *sc;
  291. struct hfi1_devdata *dd;
  292. if (!uctxt || !uctxt->dd || !uctxt->sc)
  293. return -EINVAL;
  294. /*
  295. * There is no protection here. User level has to
  296. * guarantee that no one will be writing to the send
  297. * context while it is being re-initialized.
  298. * If user level breaks that guarantee, it will break
  299. * it's own context and no one else's.
  300. */
  301. dd = uctxt->dd;
  302. sc = uctxt->sc;
  303. /*
  304. * Wait until the interrupt handler has marked the
  305. * context as halted or frozen. Report error if we time
  306. * out.
  307. */
  308. wait_event_interruptible_timeout(
  309. sc->halt_wait, (sc->flags & SCF_HALTED),
  310. msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
  311. if (!(sc->flags & SCF_HALTED))
  312. return -ENOLCK;
  313. /*
  314. * If the send context was halted due to a Freeze,
  315. * wait until the device has been "unfrozen" before
  316. * resetting the context.
  317. */
  318. if (sc->flags & SCF_FROZEN) {
  319. wait_event_interruptible_timeout(
  320. dd->event_queue,
  321. !(ACCESS_ONCE(dd->flags) & HFI1_FROZEN),
  322. msecs_to_jiffies(SEND_CTXT_HALT_TIMEOUT));
  323. if (dd->flags & HFI1_FROZEN)
  324. return -ENOLCK;
  325. if (dd->flags & HFI1_FORCED_FREEZE)
  326. /*
  327. * Don't allow context reset if we are into
  328. * forced freeze
  329. */
  330. return -ENODEV;
  331. sc_disable(sc);
  332. ret = sc_enable(sc);
  333. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_ENB,
  334. uctxt->ctxt);
  335. } else {
  336. ret = sc_restart(sc);
  337. }
  338. if (!ret)
  339. sc_return_credits(sc);
  340. break;
  341. }
  342. case HFI1_IOCTL_GET_VERS:
  343. uval = HFI1_USER_SWVERSION;
  344. if (put_user(uval, (int __user *)arg))
  345. return -EFAULT;
  346. break;
  347. default:
  348. return -EINVAL;
  349. }
  350. return ret;
  351. }
  352. static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)
  353. {
  354. struct hfi1_filedata *fd = kiocb->ki_filp->private_data;
  355. struct hfi1_user_sdma_pkt_q *pq = fd->pq;
  356. struct hfi1_user_sdma_comp_q *cq = fd->cq;
  357. int done = 0, reqs = 0;
  358. unsigned long dim = from->nr_segs;
  359. if (!cq || !pq)
  360. return -EIO;
  361. if (!iter_is_iovec(from) || !dim)
  362. return -EINVAL;
  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. return -ENOSPC;
  367. while (dim) {
  368. int ret;
  369. unsigned long count = 0;
  370. ret = hfi1_user_sdma_process_request(
  371. kiocb->ki_filp, (struct iovec *)(from->iov + done),
  372. dim, &count);
  373. if (ret) {
  374. reqs = ret;
  375. break;
  376. }
  377. dim -= count;
  378. done += count;
  379. reqs++;
  380. }
  381. return 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(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. mutex_unlock(&hfi1_mutex);
  664. goto done;
  665. }
  666. spin_lock_irqsave(&dd->uctxt_lock, flags);
  667. /*
  668. * Disable receive context and interrupt available, reset all
  669. * RcvCtxtCtrl bits to default values.
  670. */
  671. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
  672. HFI1_RCVCTRL_TIDFLOW_DIS |
  673. HFI1_RCVCTRL_INTRAVAIL_DIS |
  674. HFI1_RCVCTRL_TAILUPD_DIS |
  675. HFI1_RCVCTRL_ONE_PKT_EGR_DIS |
  676. HFI1_RCVCTRL_NO_RHQ_DROP_DIS |
  677. HFI1_RCVCTRL_NO_EGR_DROP_DIS, uctxt->ctxt);
  678. /* Clear the context's J_KEY */
  679. hfi1_clear_ctxt_jkey(dd, uctxt->ctxt);
  680. /*
  681. * Reset context integrity checks to default.
  682. * (writes to CSRs probably belong in chip.c)
  683. */
  684. write_kctxt_csr(dd, uctxt->sc->hw_context, SEND_CTXT_CHECK_ENABLE,
  685. hfi1_pkt_default_send_ctxt_mask(dd, uctxt->sc->type));
  686. sc_disable(uctxt->sc);
  687. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  688. dd->rcd[uctxt->ctxt] = NULL;
  689. hfi1_user_exp_rcv_free(fdata);
  690. hfi1_clear_ctxt_pkey(dd, uctxt->ctxt);
  691. uctxt->rcvwait_to = 0;
  692. uctxt->piowait_to = 0;
  693. uctxt->rcvnowait = 0;
  694. uctxt->pionowait = 0;
  695. uctxt->event_flags = 0;
  696. hfi1_stats.sps_ctxts--;
  697. if (++dd->freectxts == dd->num_user_contexts)
  698. aspm_enable_all(dd);
  699. mutex_unlock(&hfi1_mutex);
  700. hfi1_free_ctxtdata(dd, uctxt);
  701. done:
  702. kobject_put(&dd->kobj);
  703. kfree(fdata);
  704. return 0;
  705. }
  706. /*
  707. * Convert kernel *virtual* addresses to physical addresses.
  708. * This is used to vmalloc'ed addresses.
  709. */
  710. static u64 kvirt_to_phys(void *addr)
  711. {
  712. struct page *page;
  713. u64 paddr = 0;
  714. page = vmalloc_to_page(addr);
  715. if (page)
  716. paddr = page_to_pfn(page) << PAGE_SHIFT;
  717. return paddr;
  718. }
  719. static int assign_ctxt(struct file *fp, struct hfi1_user_info *uinfo)
  720. {
  721. int i_minor, ret = 0;
  722. unsigned int swmajor, swminor;
  723. swmajor = uinfo->userversion >> 16;
  724. if (swmajor != HFI1_USER_SWMAJOR) {
  725. ret = -ENODEV;
  726. goto done;
  727. }
  728. swminor = uinfo->userversion & 0xffff;
  729. mutex_lock(&hfi1_mutex);
  730. /* First, lets check if we need to setup a shared context? */
  731. if (uinfo->subctxt_cnt) {
  732. struct hfi1_filedata *fd = fp->private_data;
  733. ret = find_shared_ctxt(fp, uinfo);
  734. if (ret < 0)
  735. goto done_unlock;
  736. if (ret) {
  737. fd->rec_cpu_num =
  738. hfi1_get_proc_affinity(fd->uctxt->numa_id);
  739. }
  740. }
  741. /*
  742. * We execute the following block if we couldn't find a
  743. * shared context or if context sharing is not required.
  744. */
  745. if (!ret) {
  746. i_minor = iminor(file_inode(fp)) - HFI1_USER_MINOR_BASE;
  747. ret = get_user_context(fp, uinfo, i_minor);
  748. }
  749. done_unlock:
  750. mutex_unlock(&hfi1_mutex);
  751. done:
  752. return ret;
  753. }
  754. static int get_user_context(struct file *fp, struct hfi1_user_info *uinfo,
  755. int devno)
  756. {
  757. struct hfi1_devdata *dd = NULL;
  758. int devmax, npresent, nup;
  759. devmax = hfi1_count_units(&npresent, &nup);
  760. if (!npresent)
  761. return -ENXIO;
  762. if (!nup)
  763. return -ENETDOWN;
  764. dd = hfi1_lookup(devno);
  765. if (!dd)
  766. return -ENODEV;
  767. else if (!dd->freectxts)
  768. return -EBUSY;
  769. return allocate_ctxt(fp, dd, uinfo);
  770. }
  771. static int find_shared_ctxt(struct file *fp,
  772. const struct hfi1_user_info *uinfo)
  773. {
  774. int devmax, ndev, i;
  775. int ret = 0;
  776. struct hfi1_filedata *fd = fp->private_data;
  777. devmax = hfi1_count_units(NULL, NULL);
  778. for (ndev = 0; ndev < devmax; ndev++) {
  779. struct hfi1_devdata *dd = hfi1_lookup(ndev);
  780. if (!(dd && (dd->flags & HFI1_PRESENT) && dd->kregbase))
  781. continue;
  782. for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
  783. struct hfi1_ctxtdata *uctxt = dd->rcd[i];
  784. /* Skip ctxts which are not yet open */
  785. if (!uctxt || !uctxt->cnt)
  786. continue;
  787. /* Skip ctxt if it doesn't match the requested one */
  788. if (memcmp(uctxt->uuid, uinfo->uuid,
  789. sizeof(uctxt->uuid)) ||
  790. uctxt->jkey != generate_jkey(current_uid()) ||
  791. uctxt->subctxt_id != uinfo->subctxt_id ||
  792. uctxt->subctxt_cnt != uinfo->subctxt_cnt)
  793. continue;
  794. /* Verify the sharing process matches the master */
  795. if (uctxt->userversion != uinfo->userversion ||
  796. uctxt->cnt >= uctxt->subctxt_cnt) {
  797. ret = -EINVAL;
  798. goto done;
  799. }
  800. fd->uctxt = uctxt;
  801. fd->subctxt = uctxt->cnt++;
  802. uctxt->active_slaves |= 1 << fd->subctxt;
  803. ret = 1;
  804. goto done;
  805. }
  806. }
  807. done:
  808. return ret;
  809. }
  810. static int allocate_ctxt(struct file *fp, struct hfi1_devdata *dd,
  811. struct hfi1_user_info *uinfo)
  812. {
  813. struct hfi1_filedata *fd = fp->private_data;
  814. struct hfi1_ctxtdata *uctxt;
  815. unsigned ctxt;
  816. int ret, numa;
  817. if (dd->flags & HFI1_FROZEN) {
  818. /*
  819. * Pick an error that is unique from all other errors
  820. * that are returned so the user process knows that
  821. * it tried to allocate while the SPC was frozen. It
  822. * it should be able to retry with success in a short
  823. * while.
  824. */
  825. return -EIO;
  826. }
  827. for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts; ctxt++)
  828. if (!dd->rcd[ctxt])
  829. break;
  830. if (ctxt == dd->num_rcv_contexts)
  831. return -EBUSY;
  832. /*
  833. * If we don't have a NUMA node requested, preference is towards
  834. * device NUMA node.
  835. */
  836. fd->rec_cpu_num = hfi1_get_proc_affinity(dd->node);
  837. if (fd->rec_cpu_num != -1)
  838. numa = cpu_to_node(fd->rec_cpu_num);
  839. else
  840. numa = numa_node_id();
  841. uctxt = hfi1_create_ctxtdata(dd->pport, ctxt, numa);
  842. if (!uctxt) {
  843. dd_dev_err(dd,
  844. "Unable to allocate ctxtdata memory, failing open\n");
  845. return -ENOMEM;
  846. }
  847. hfi1_cdbg(PROC, "[%u:%u] pid %u assigned to CPU %d (NUMA %u)",
  848. uctxt->ctxt, fd->subctxt, current->pid, fd->rec_cpu_num,
  849. uctxt->numa_id);
  850. /*
  851. * Allocate and enable a PIO send context.
  852. */
  853. uctxt->sc = sc_alloc(dd, SC_USER, uctxt->rcvhdrqentsize,
  854. uctxt->dd->node);
  855. if (!uctxt->sc)
  856. return -ENOMEM;
  857. hfi1_cdbg(PROC, "allocated send context %u(%u)\n", uctxt->sc->sw_index,
  858. uctxt->sc->hw_context);
  859. ret = sc_enable(uctxt->sc);
  860. if (ret)
  861. return ret;
  862. /*
  863. * Setup shared context resources if the user-level has requested
  864. * shared contexts and this is the 'master' process.
  865. * This has to be done here so the rest of the sub-contexts find the
  866. * proper master.
  867. */
  868. if (uinfo->subctxt_cnt && !fd->subctxt) {
  869. ret = init_subctxts(uctxt, uinfo);
  870. /*
  871. * On error, we don't need to disable and de-allocate the
  872. * send context because it will be done during file close
  873. */
  874. if (ret)
  875. return ret;
  876. }
  877. uctxt->userversion = uinfo->userversion;
  878. uctxt->flags = hfi1_cap_mask; /* save current flag state */
  879. init_waitqueue_head(&uctxt->wait);
  880. strlcpy(uctxt->comm, current->comm, sizeof(uctxt->comm));
  881. memcpy(uctxt->uuid, uinfo->uuid, sizeof(uctxt->uuid));
  882. uctxt->jkey = generate_jkey(current_uid());
  883. INIT_LIST_HEAD(&uctxt->sdma_queues);
  884. spin_lock_init(&uctxt->sdma_qlock);
  885. hfi1_stats.sps_ctxts++;
  886. /*
  887. * Disable ASPM when there are open user/PSM contexts to avoid
  888. * issues with ASPM L1 exit latency
  889. */
  890. if (dd->freectxts-- == dd->num_user_contexts)
  891. aspm_disable_all(dd);
  892. fd->uctxt = uctxt;
  893. return 0;
  894. }
  895. static int init_subctxts(struct hfi1_ctxtdata *uctxt,
  896. const struct hfi1_user_info *uinfo)
  897. {
  898. unsigned num_subctxts;
  899. num_subctxts = uinfo->subctxt_cnt;
  900. if (num_subctxts > HFI1_MAX_SHARED_CTXTS)
  901. return -EINVAL;
  902. uctxt->subctxt_cnt = uinfo->subctxt_cnt;
  903. uctxt->subctxt_id = uinfo->subctxt_id;
  904. uctxt->active_slaves = 1;
  905. uctxt->redirect_seq_cnt = 1;
  906. set_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
  907. return 0;
  908. }
  909. static int setup_subctxt(struct hfi1_ctxtdata *uctxt)
  910. {
  911. int ret = 0;
  912. unsigned num_subctxts = uctxt->subctxt_cnt;
  913. uctxt->subctxt_uregbase = vmalloc_user(PAGE_SIZE);
  914. if (!uctxt->subctxt_uregbase) {
  915. ret = -ENOMEM;
  916. goto bail;
  917. }
  918. /* We can take the size of the RcvHdr Queue from the master */
  919. uctxt->subctxt_rcvhdr_base = vmalloc_user(uctxt->rcvhdrq_size *
  920. num_subctxts);
  921. if (!uctxt->subctxt_rcvhdr_base) {
  922. ret = -ENOMEM;
  923. goto bail_ureg;
  924. }
  925. uctxt->subctxt_rcvegrbuf = vmalloc_user(uctxt->egrbufs.size *
  926. num_subctxts);
  927. if (!uctxt->subctxt_rcvegrbuf) {
  928. ret = -ENOMEM;
  929. goto bail_rhdr;
  930. }
  931. goto bail;
  932. bail_rhdr:
  933. vfree(uctxt->subctxt_rcvhdr_base);
  934. bail_ureg:
  935. vfree(uctxt->subctxt_uregbase);
  936. uctxt->subctxt_uregbase = NULL;
  937. bail:
  938. return ret;
  939. }
  940. static int user_init(struct file *fp)
  941. {
  942. unsigned int rcvctrl_ops = 0;
  943. struct hfi1_filedata *fd = fp->private_data;
  944. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  945. /* make sure that the context has already been setup */
  946. if (!test_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags))
  947. return -EFAULT;
  948. /* initialize poll variables... */
  949. uctxt->urgent = 0;
  950. uctxt->urgent_poll = 0;
  951. /*
  952. * Now enable the ctxt for receive.
  953. * For chips that are set to DMA the tail register to memory
  954. * when they change (and when the update bit transitions from
  955. * 0 to 1. So for those chips, we turn it off and then back on.
  956. * This will (very briefly) affect any other open ctxts, but the
  957. * duration is very short, and therefore isn't an issue. We
  958. * explicitly set the in-memory tail copy to 0 beforehand, so we
  959. * don't have to wait to be sure the DMA update has happened
  960. * (chip resets head/tail to 0 on transition to enable).
  961. */
  962. if (uctxt->rcvhdrtail_kvaddr)
  963. clear_rcvhdrtail(uctxt);
  964. /* Setup J_KEY before enabling the context */
  965. hfi1_set_ctxt_jkey(uctxt->dd, uctxt->ctxt, uctxt->jkey);
  966. rcvctrl_ops = HFI1_RCVCTRL_CTXT_ENB;
  967. if (HFI1_CAP_UGET_MASK(uctxt->flags, HDRSUPP))
  968. rcvctrl_ops |= HFI1_RCVCTRL_TIDFLOW_ENB;
  969. /*
  970. * Ignore the bit in the flags for now until proper
  971. * support for multiple packet per rcv array entry is
  972. * added.
  973. */
  974. if (!HFI1_CAP_UGET_MASK(uctxt->flags, MULTI_PKT_EGR))
  975. rcvctrl_ops |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
  976. if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_EGR_FULL))
  977. rcvctrl_ops |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
  978. if (HFI1_CAP_UGET_MASK(uctxt->flags, NODROP_RHQ_FULL))
  979. rcvctrl_ops |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
  980. /*
  981. * The RcvCtxtCtrl.TailUpd bit has to be explicitly written.
  982. * We can't rely on the correct value to be set from prior
  983. * uses of the chip or ctxt. Therefore, add the rcvctrl op
  984. * for both cases.
  985. */
  986. if (HFI1_CAP_UGET_MASK(uctxt->flags, DMA_RTAIL))
  987. rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_ENB;
  988. else
  989. rcvctrl_ops |= HFI1_RCVCTRL_TAILUPD_DIS;
  990. hfi1_rcvctrl(uctxt->dd, rcvctrl_ops, uctxt->ctxt);
  991. /* Notify any waiting slaves */
  992. if (uctxt->subctxt_cnt) {
  993. clear_bit(HFI1_CTXT_MASTER_UNINIT, &uctxt->event_flags);
  994. wake_up(&uctxt->wait);
  995. }
  996. return 0;
  997. }
  998. static int get_ctxt_info(struct file *fp, void __user *ubase, __u32 len)
  999. {
  1000. struct hfi1_ctxt_info cinfo;
  1001. struct hfi1_filedata *fd = fp->private_data;
  1002. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1003. int ret = 0;
  1004. memset(&cinfo, 0, sizeof(cinfo));
  1005. cinfo.runtime_flags = (((uctxt->flags >> HFI1_CAP_MISC_SHIFT) &
  1006. HFI1_CAP_MISC_MASK) << HFI1_CAP_USER_SHIFT) |
  1007. HFI1_CAP_UGET_MASK(uctxt->flags, MASK) |
  1008. HFI1_CAP_KGET_MASK(uctxt->flags, K2U);
  1009. /* adjust flag if this fd is not able to cache */
  1010. if (!fd->handler)
  1011. cinfo.runtime_flags |= HFI1_CAP_TID_UNMAP; /* no caching */
  1012. cinfo.num_active = hfi1_count_active_units();
  1013. cinfo.unit = uctxt->dd->unit;
  1014. cinfo.ctxt = uctxt->ctxt;
  1015. cinfo.subctxt = fd->subctxt;
  1016. cinfo.rcvtids = roundup(uctxt->egrbufs.alloced,
  1017. uctxt->dd->rcv_entries.group_size) +
  1018. uctxt->expected_count;
  1019. cinfo.credits = uctxt->sc->credits;
  1020. cinfo.numa_node = uctxt->numa_id;
  1021. cinfo.rec_cpu = fd->rec_cpu_num;
  1022. cinfo.send_ctxt = uctxt->sc->hw_context;
  1023. cinfo.egrtids = uctxt->egrbufs.alloced;
  1024. cinfo.rcvhdrq_cnt = uctxt->rcvhdrq_cnt;
  1025. cinfo.rcvhdrq_entsize = uctxt->rcvhdrqentsize << 2;
  1026. cinfo.sdma_ring_size = fd->cq->nentries;
  1027. cinfo.rcvegr_size = uctxt->egrbufs.rcvtid_size;
  1028. trace_hfi1_ctxt_info(uctxt->dd, uctxt->ctxt, fd->subctxt, cinfo);
  1029. if (copy_to_user(ubase, &cinfo, sizeof(cinfo)))
  1030. ret = -EFAULT;
  1031. return ret;
  1032. }
  1033. static int setup_ctxt(struct file *fp)
  1034. {
  1035. struct hfi1_filedata *fd = fp->private_data;
  1036. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1037. struct hfi1_devdata *dd = uctxt->dd;
  1038. int ret = 0;
  1039. /*
  1040. * Context should be set up only once, including allocation and
  1041. * programming of eager buffers. This is done if context sharing
  1042. * is not requested or by the master process.
  1043. */
  1044. if (!uctxt->subctxt_cnt || !fd->subctxt) {
  1045. ret = hfi1_init_ctxt(uctxt->sc);
  1046. if (ret)
  1047. goto done;
  1048. /* Now allocate the RcvHdr queue and eager buffers. */
  1049. ret = hfi1_create_rcvhdrq(dd, uctxt);
  1050. if (ret)
  1051. goto done;
  1052. ret = hfi1_setup_eagerbufs(uctxt);
  1053. if (ret)
  1054. goto done;
  1055. if (uctxt->subctxt_cnt && !fd->subctxt) {
  1056. ret = setup_subctxt(uctxt);
  1057. if (ret)
  1058. goto done;
  1059. }
  1060. } else {
  1061. ret = wait_event_interruptible(uctxt->wait, !test_bit(
  1062. HFI1_CTXT_MASTER_UNINIT,
  1063. &uctxt->event_flags));
  1064. if (ret)
  1065. goto done;
  1066. }
  1067. ret = hfi1_user_sdma_alloc_queues(uctxt, fp);
  1068. if (ret)
  1069. goto done;
  1070. /*
  1071. * Expected receive has to be setup for all processes (including
  1072. * shared contexts). However, it has to be done after the master
  1073. * context has been fully configured as it depends on the
  1074. * eager/expected split of the RcvArray entries.
  1075. * Setting it up here ensures that the subcontexts will be waiting
  1076. * (due to the above wait_event_interruptible() until the master
  1077. * is setup.
  1078. */
  1079. ret = hfi1_user_exp_rcv_init(fp);
  1080. if (ret)
  1081. goto done;
  1082. set_bit(HFI1_CTXT_SETUP_DONE, &uctxt->event_flags);
  1083. done:
  1084. return ret;
  1085. }
  1086. static int get_base_info(struct file *fp, void __user *ubase, __u32 len)
  1087. {
  1088. struct hfi1_base_info binfo;
  1089. struct hfi1_filedata *fd = fp->private_data;
  1090. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1091. struct hfi1_devdata *dd = uctxt->dd;
  1092. ssize_t sz;
  1093. unsigned offset;
  1094. int ret = 0;
  1095. trace_hfi1_uctxtdata(uctxt->dd, uctxt);
  1096. memset(&binfo, 0, sizeof(binfo));
  1097. binfo.hw_version = dd->revision;
  1098. binfo.sw_version = HFI1_KERN_SWVERSION;
  1099. binfo.bthqp = kdeth_qp;
  1100. binfo.jkey = uctxt->jkey;
  1101. /*
  1102. * If more than 64 contexts are enabled the allocated credit
  1103. * return will span two or three contiguous pages. Since we only
  1104. * map the page containing the context's credit return address,
  1105. * we need to calculate the offset in the proper page.
  1106. */
  1107. offset = ((u64)uctxt->sc->hw_free -
  1108. (u64)dd->cr_base[uctxt->numa_id].va) % PAGE_SIZE;
  1109. binfo.sc_credits_addr = HFI1_MMAP_TOKEN(PIO_CRED, uctxt->ctxt,
  1110. fd->subctxt, offset);
  1111. binfo.pio_bufbase = HFI1_MMAP_TOKEN(PIO_BUFS, uctxt->ctxt,
  1112. fd->subctxt,
  1113. uctxt->sc->base_addr);
  1114. binfo.pio_bufbase_sop = HFI1_MMAP_TOKEN(PIO_BUFS_SOP,
  1115. uctxt->ctxt,
  1116. fd->subctxt,
  1117. uctxt->sc->base_addr);
  1118. binfo.rcvhdr_bufbase = HFI1_MMAP_TOKEN(RCV_HDRQ, uctxt->ctxt,
  1119. fd->subctxt,
  1120. uctxt->rcvhdrq);
  1121. binfo.rcvegr_bufbase = HFI1_MMAP_TOKEN(RCV_EGRBUF, uctxt->ctxt,
  1122. fd->subctxt,
  1123. uctxt->egrbufs.rcvtids[0].phys);
  1124. binfo.sdma_comp_bufbase = HFI1_MMAP_TOKEN(SDMA_COMP, uctxt->ctxt,
  1125. fd->subctxt, 0);
  1126. /*
  1127. * user regs are at
  1128. * (RXE_PER_CONTEXT_USER + (ctxt * RXE_PER_CONTEXT_SIZE))
  1129. */
  1130. binfo.user_regbase = HFI1_MMAP_TOKEN(UREGS, uctxt->ctxt,
  1131. fd->subctxt, 0);
  1132. offset = offset_in_page((((uctxt->ctxt - dd->first_user_ctxt) *
  1133. HFI1_MAX_SHARED_CTXTS) + fd->subctxt) *
  1134. sizeof(*dd->events));
  1135. binfo.events_bufbase = HFI1_MMAP_TOKEN(EVENTS, uctxt->ctxt,
  1136. fd->subctxt,
  1137. offset);
  1138. binfo.status_bufbase = HFI1_MMAP_TOKEN(STATUS, uctxt->ctxt,
  1139. fd->subctxt,
  1140. dd->status);
  1141. if (HFI1_CAP_IS_USET(DMA_RTAIL))
  1142. binfo.rcvhdrtail_base = HFI1_MMAP_TOKEN(RTAIL, uctxt->ctxt,
  1143. fd->subctxt, 0);
  1144. if (uctxt->subctxt_cnt) {
  1145. binfo.subctxt_uregbase = HFI1_MMAP_TOKEN(SUBCTXT_UREGS,
  1146. uctxt->ctxt,
  1147. fd->subctxt, 0);
  1148. binfo.subctxt_rcvhdrbuf = HFI1_MMAP_TOKEN(SUBCTXT_RCV_HDRQ,
  1149. uctxt->ctxt,
  1150. fd->subctxt, 0);
  1151. binfo.subctxt_rcvegrbuf = HFI1_MMAP_TOKEN(SUBCTXT_EGRBUF,
  1152. uctxt->ctxt,
  1153. fd->subctxt, 0);
  1154. }
  1155. sz = (len < sizeof(binfo)) ? len : sizeof(binfo);
  1156. if (copy_to_user(ubase, &binfo, sz))
  1157. ret = -EFAULT;
  1158. return ret;
  1159. }
  1160. static unsigned int poll_urgent(struct file *fp,
  1161. struct poll_table_struct *pt)
  1162. {
  1163. struct hfi1_filedata *fd = fp->private_data;
  1164. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1165. struct hfi1_devdata *dd = uctxt->dd;
  1166. unsigned pollflag;
  1167. poll_wait(fp, &uctxt->wait, pt);
  1168. spin_lock_irq(&dd->uctxt_lock);
  1169. if (uctxt->urgent != uctxt->urgent_poll) {
  1170. pollflag = POLLIN | POLLRDNORM;
  1171. uctxt->urgent_poll = uctxt->urgent;
  1172. } else {
  1173. pollflag = 0;
  1174. set_bit(HFI1_CTXT_WAITING_URG, &uctxt->event_flags);
  1175. }
  1176. spin_unlock_irq(&dd->uctxt_lock);
  1177. return pollflag;
  1178. }
  1179. static unsigned int poll_next(struct file *fp,
  1180. struct poll_table_struct *pt)
  1181. {
  1182. struct hfi1_filedata *fd = fp->private_data;
  1183. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  1184. struct hfi1_devdata *dd = uctxt->dd;
  1185. unsigned pollflag;
  1186. poll_wait(fp, &uctxt->wait, pt);
  1187. spin_lock_irq(&dd->uctxt_lock);
  1188. if (hdrqempty(uctxt)) {
  1189. set_bit(HFI1_CTXT_WAITING_RCV, &uctxt->event_flags);
  1190. hfi1_rcvctrl(dd, HFI1_RCVCTRL_INTRAVAIL_ENB, uctxt->ctxt);
  1191. pollflag = 0;
  1192. } else {
  1193. pollflag = POLLIN | POLLRDNORM;
  1194. }
  1195. spin_unlock_irq(&dd->uctxt_lock);
  1196. return pollflag;
  1197. }
  1198. /*
  1199. * Find all user contexts in use, and set the specified bit in their
  1200. * event mask.
  1201. * See also find_ctxt() for a similar use, that is specific to send buffers.
  1202. */
  1203. int hfi1_set_uevent_bits(struct hfi1_pportdata *ppd, const int evtbit)
  1204. {
  1205. struct hfi1_ctxtdata *uctxt;
  1206. struct hfi1_devdata *dd = ppd->dd;
  1207. unsigned ctxt;
  1208. int ret = 0;
  1209. unsigned long flags;
  1210. if (!dd->events) {
  1211. ret = -EINVAL;
  1212. goto done;
  1213. }
  1214. spin_lock_irqsave(&dd->uctxt_lock, flags);
  1215. for (ctxt = dd->first_user_ctxt; ctxt < dd->num_rcv_contexts;
  1216. ctxt++) {
  1217. uctxt = dd->rcd[ctxt];
  1218. if (uctxt) {
  1219. unsigned long *evs = dd->events +
  1220. (uctxt->ctxt - dd->first_user_ctxt) *
  1221. HFI1_MAX_SHARED_CTXTS;
  1222. int i;
  1223. /*
  1224. * subctxt_cnt is 0 if not shared, so do base
  1225. * separately, first, then remaining subctxt, if any
  1226. */
  1227. set_bit(evtbit, evs);
  1228. for (i = 1; i < uctxt->subctxt_cnt; i++)
  1229. set_bit(evtbit, evs + i);
  1230. }
  1231. }
  1232. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1233. done:
  1234. return ret;
  1235. }
  1236. /**
  1237. * manage_rcvq - manage a context's receive queue
  1238. * @uctxt: the context
  1239. * @subctxt: the sub-context
  1240. * @start_stop: action to carry out
  1241. *
  1242. * start_stop == 0 disables receive on the context, for use in queue
  1243. * overflow conditions. start_stop==1 re-enables, to be used to
  1244. * re-init the software copy of the head register
  1245. */
  1246. static int manage_rcvq(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
  1247. int start_stop)
  1248. {
  1249. struct hfi1_devdata *dd = uctxt->dd;
  1250. unsigned int rcvctrl_op;
  1251. if (subctxt)
  1252. goto bail;
  1253. /* atomically clear receive enable ctxt. */
  1254. if (start_stop) {
  1255. /*
  1256. * On enable, force in-memory copy of the tail register to
  1257. * 0, so that protocol code doesn't have to worry about
  1258. * whether or not the chip has yet updated the in-memory
  1259. * copy or not on return from the system call. The chip
  1260. * always resets it's tail register back to 0 on a
  1261. * transition from disabled to enabled.
  1262. */
  1263. if (uctxt->rcvhdrtail_kvaddr)
  1264. clear_rcvhdrtail(uctxt);
  1265. rcvctrl_op = HFI1_RCVCTRL_CTXT_ENB;
  1266. } else {
  1267. rcvctrl_op = HFI1_RCVCTRL_CTXT_DIS;
  1268. }
  1269. hfi1_rcvctrl(dd, rcvctrl_op, uctxt->ctxt);
  1270. /* always; new head should be equal to new tail; see above */
  1271. bail:
  1272. return 0;
  1273. }
  1274. /*
  1275. * clear the event notifier events for this context.
  1276. * User process then performs actions appropriate to bit having been
  1277. * set, if desired, and checks again in future.
  1278. */
  1279. static int user_event_ack(struct hfi1_ctxtdata *uctxt, int subctxt,
  1280. unsigned long events)
  1281. {
  1282. int i;
  1283. struct hfi1_devdata *dd = uctxt->dd;
  1284. unsigned long *evs;
  1285. if (!dd->events)
  1286. return 0;
  1287. evs = dd->events + ((uctxt->ctxt - dd->first_user_ctxt) *
  1288. HFI1_MAX_SHARED_CTXTS) + subctxt;
  1289. for (i = 0; i <= _HFI1_MAX_EVENT_BIT; i++) {
  1290. if (!test_bit(i, &events))
  1291. continue;
  1292. clear_bit(i, evs);
  1293. }
  1294. return 0;
  1295. }
  1296. static int set_ctxt_pkey(struct hfi1_ctxtdata *uctxt, unsigned subctxt,
  1297. u16 pkey)
  1298. {
  1299. int ret = -ENOENT, i, intable = 0;
  1300. struct hfi1_pportdata *ppd = uctxt->ppd;
  1301. struct hfi1_devdata *dd = uctxt->dd;
  1302. if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY) {
  1303. ret = -EINVAL;
  1304. goto done;
  1305. }
  1306. for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++)
  1307. if (pkey == ppd->pkeys[i]) {
  1308. intable = 1;
  1309. break;
  1310. }
  1311. if (intable)
  1312. ret = hfi1_set_ctxt_pkey(dd, uctxt->ctxt, pkey);
  1313. done:
  1314. return ret;
  1315. }
  1316. static void user_remove(struct hfi1_devdata *dd)
  1317. {
  1318. hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device);
  1319. }
  1320. static int user_add(struct hfi1_devdata *dd)
  1321. {
  1322. char name[10];
  1323. int ret;
  1324. snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit);
  1325. ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops,
  1326. &dd->user_cdev, &dd->user_device,
  1327. true, &dd->kobj);
  1328. if (ret)
  1329. user_remove(dd);
  1330. return ret;
  1331. }
  1332. /*
  1333. * Create per-unit files in /dev
  1334. */
  1335. int hfi1_device_create(struct hfi1_devdata *dd)
  1336. {
  1337. return user_add(dd);
  1338. }
  1339. /*
  1340. * Remove per-unit files in /dev
  1341. * void, core kernel returns no errors for this stuff
  1342. */
  1343. void hfi1_device_remove(struct hfi1_devdata *dd)
  1344. {
  1345. user_remove(dd);
  1346. }