file_ops.c 43 KB

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