file_ops.c 42 KB

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