file_ops.c 41 KB

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