file_ops.c 40 KB

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