file_ops.c 40 KB

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