qib_file_ops.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
  3. * All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/pci.h>
  35. #include <linux/poll.h>
  36. #include <linux/cdev.h>
  37. #include <linux/swap.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/highmem.h>
  40. #include <linux/io.h>
  41. #include <linux/uio.h>
  42. #include <linux/jiffies.h>
  43. #include <asm/pgtable.h>
  44. #include <linux/delay.h>
  45. #include "qib.h"
  46. #include "qib_common.h"
  47. #include "qib_user_sdma.h"
  48. static int qib_open(struct inode *, struct file *);
  49. static int qib_close(struct inode *, struct file *);
  50. static ssize_t qib_write(struct file *, const char __user *, size_t, loff_t *);
  51. static ssize_t qib_aio_write(struct kiocb *, const struct iovec *,
  52. unsigned long, loff_t);
  53. static unsigned int qib_poll(struct file *, struct poll_table_struct *);
  54. static int qib_mmapf(struct file *, struct vm_area_struct *);
  55. static const struct file_operations qib_file_ops = {
  56. .owner = THIS_MODULE,
  57. .write = qib_write,
  58. .aio_write = qib_aio_write,
  59. .open = qib_open,
  60. .release = qib_close,
  61. .poll = qib_poll,
  62. .mmap = qib_mmapf
  63. };
  64. /*
  65. * Convert kernel virtual addresses to physical addresses so they don't
  66. * potentially conflict with the chip addresses used as mmap offsets.
  67. * It doesn't really matter what mmap offset we use as long as we can
  68. * interpret it correctly.
  69. */
  70. static u64 cvt_kvaddr(void *p)
  71. {
  72. struct page *page;
  73. u64 paddr = 0;
  74. page = vmalloc_to_page(p);
  75. if (page)
  76. paddr = page_to_pfn(page) << PAGE_SHIFT;
  77. return paddr;
  78. }
  79. static int qib_get_base_info(struct file *fp, void __user *ubase,
  80. size_t ubase_size)
  81. {
  82. struct qib_ctxtdata *rcd = ctxt_fp(fp);
  83. int ret = 0;
  84. struct qib_base_info *kinfo = NULL;
  85. struct qib_devdata *dd = rcd->dd;
  86. struct qib_pportdata *ppd = rcd->ppd;
  87. unsigned subctxt_cnt;
  88. int shared, master;
  89. size_t sz;
  90. subctxt_cnt = rcd->subctxt_cnt;
  91. if (!subctxt_cnt) {
  92. shared = 0;
  93. master = 0;
  94. subctxt_cnt = 1;
  95. } else {
  96. shared = 1;
  97. master = !subctxt_fp(fp);
  98. }
  99. sz = sizeof(*kinfo);
  100. /* If context sharing is not requested, allow the old size structure */
  101. if (!shared)
  102. sz -= 7 * sizeof(u64);
  103. if (ubase_size < sz) {
  104. ret = -EINVAL;
  105. goto bail;
  106. }
  107. kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
  108. if (kinfo == NULL) {
  109. ret = -ENOMEM;
  110. goto bail;
  111. }
  112. ret = dd->f_get_base_info(rcd, kinfo);
  113. if (ret < 0)
  114. goto bail;
  115. kinfo->spi_rcvhdr_cnt = dd->rcvhdrcnt;
  116. kinfo->spi_rcvhdrent_size = dd->rcvhdrentsize;
  117. kinfo->spi_tidegrcnt = rcd->rcvegrcnt;
  118. kinfo->spi_rcv_egrbufsize = dd->rcvegrbufsize;
  119. /*
  120. * have to mmap whole thing
  121. */
  122. kinfo->spi_rcv_egrbuftotlen =
  123. rcd->rcvegrbuf_chunks * rcd->rcvegrbuf_size;
  124. kinfo->spi_rcv_egrperchunk = rcd->rcvegrbufs_perchunk;
  125. kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
  126. rcd->rcvegrbuf_chunks;
  127. kinfo->spi_tidcnt = dd->rcvtidcnt / subctxt_cnt;
  128. if (master)
  129. kinfo->spi_tidcnt += dd->rcvtidcnt % subctxt_cnt;
  130. /*
  131. * for this use, may be cfgctxts summed over all chips that
  132. * are are configured and present
  133. */
  134. kinfo->spi_nctxts = dd->cfgctxts;
  135. /* unit (chip/board) our context is on */
  136. kinfo->spi_unit = dd->unit;
  137. kinfo->spi_port = ppd->port;
  138. /* for now, only a single page */
  139. kinfo->spi_tid_maxsize = PAGE_SIZE;
  140. /*
  141. * Doing this per context, and based on the skip value, etc. This has
  142. * to be the actual buffer size, since the protocol code treats it
  143. * as an array.
  144. *
  145. * These have to be set to user addresses in the user code via mmap.
  146. * These values are used on return to user code for the mmap target
  147. * addresses only. For 32 bit, same 44 bit address problem, so use
  148. * the physical address, not virtual. Before 2.6.11, using the
  149. * page_address() macro worked, but in 2.6.11, even that returns the
  150. * full 64 bit address (upper bits all 1's). So far, using the
  151. * physical addresses (or chip offsets, for chip mapping) works, but
  152. * no doubt some future kernel release will change that, and we'll be
  153. * on to yet another method of dealing with this.
  154. * Normally only one of rcvhdr_tailaddr or rhf_offset is useful
  155. * since the chips with non-zero rhf_offset don't normally
  156. * enable tail register updates to host memory, but for testing,
  157. * both can be enabled and used.
  158. */
  159. kinfo->spi_rcvhdr_base = (u64) rcd->rcvhdrq_phys;
  160. kinfo->spi_rcvhdr_tailaddr = (u64) rcd->rcvhdrqtailaddr_phys;
  161. kinfo->spi_rhf_offset = dd->rhf_offset;
  162. kinfo->spi_rcv_egrbufs = (u64) rcd->rcvegr_phys;
  163. kinfo->spi_pioavailaddr = (u64) dd->pioavailregs_phys;
  164. /* setup per-unit (not port) status area for user programs */
  165. kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
  166. (char *) ppd->statusp -
  167. (char *) dd->pioavailregs_dma;
  168. kinfo->spi_uregbase = (u64) dd->uregbase + dd->ureg_align * rcd->ctxt;
  169. if (!shared) {
  170. kinfo->spi_piocnt = rcd->piocnt;
  171. kinfo->spi_piobufbase = (u64) rcd->piobufs;
  172. kinfo->spi_sendbuf_status = cvt_kvaddr(rcd->user_event_mask);
  173. } else if (master) {
  174. kinfo->spi_piocnt = (rcd->piocnt / subctxt_cnt) +
  175. (rcd->piocnt % subctxt_cnt);
  176. /* Master's PIO buffers are after all the slave's */
  177. kinfo->spi_piobufbase = (u64) rcd->piobufs +
  178. dd->palign *
  179. (rcd->piocnt - kinfo->spi_piocnt);
  180. } else {
  181. unsigned slave = subctxt_fp(fp) - 1;
  182. kinfo->spi_piocnt = rcd->piocnt / subctxt_cnt;
  183. kinfo->spi_piobufbase = (u64) rcd->piobufs +
  184. dd->palign * kinfo->spi_piocnt * slave;
  185. }
  186. if (shared) {
  187. kinfo->spi_sendbuf_status =
  188. cvt_kvaddr(&rcd->user_event_mask[subctxt_fp(fp)]);
  189. /* only spi_subctxt_* fields should be set in this block! */
  190. kinfo->spi_subctxt_uregbase = cvt_kvaddr(rcd->subctxt_uregbase);
  191. kinfo->spi_subctxt_rcvegrbuf =
  192. cvt_kvaddr(rcd->subctxt_rcvegrbuf);
  193. kinfo->spi_subctxt_rcvhdr_base =
  194. cvt_kvaddr(rcd->subctxt_rcvhdr_base);
  195. }
  196. /*
  197. * All user buffers are 2KB buffers. If we ever support
  198. * giving 4KB buffers to user processes, this will need some
  199. * work. Can't use piobufbase directly, because it has
  200. * both 2K and 4K buffer base values.
  201. */
  202. kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->pio2k_bufbase) /
  203. dd->palign;
  204. kinfo->spi_pioalign = dd->palign;
  205. kinfo->spi_qpair = QIB_KD_QP;
  206. /*
  207. * user mode PIO buffers are always 2KB, even when 4KB can
  208. * be received, and sent via the kernel; this is ibmaxlen
  209. * for 2K MTU.
  210. */
  211. kinfo->spi_piosize = dd->piosize2k - 2 * sizeof(u32);
  212. kinfo->spi_mtu = ppd->ibmaxlen; /* maxlen, not ibmtu */
  213. kinfo->spi_ctxt = rcd->ctxt;
  214. kinfo->spi_subctxt = subctxt_fp(fp);
  215. kinfo->spi_sw_version = QIB_KERN_SWVERSION;
  216. kinfo->spi_sw_version |= 1U << 31; /* QLogic-built, not kernel.org */
  217. kinfo->spi_hw_version = dd->revision;
  218. if (master)
  219. kinfo->spi_runtime_flags |= QIB_RUNTIME_MASTER;
  220. sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
  221. if (copy_to_user(ubase, kinfo, sz))
  222. ret = -EFAULT;
  223. bail:
  224. kfree(kinfo);
  225. return ret;
  226. }
  227. /**
  228. * qib_tid_update - update a context TID
  229. * @rcd: the context
  230. * @fp: the qib device file
  231. * @ti: the TID information
  232. *
  233. * The new implementation as of Oct 2004 is that the driver assigns
  234. * the tid and returns it to the caller. To reduce search time, we
  235. * keep a cursor for each context, walking the shadow tid array to find
  236. * one that's not in use.
  237. *
  238. * For now, if we can't allocate the full list, we fail, although
  239. * in the long run, we'll allocate as many as we can, and the
  240. * caller will deal with that by trying the remaining pages later.
  241. * That means that when we fail, we have to mark the tids as not in
  242. * use again, in our shadow copy.
  243. *
  244. * It's up to the caller to free the tids when they are done.
  245. * We'll unlock the pages as they free them.
  246. *
  247. * Also, right now we are locking one page at a time, but since
  248. * the intended use of this routine is for a single group of
  249. * virtually contiguous pages, that should change to improve
  250. * performance.
  251. */
  252. static int qib_tid_update(struct qib_ctxtdata *rcd, struct file *fp,
  253. const struct qib_tid_info *ti)
  254. {
  255. int ret = 0, ntids;
  256. u32 tid, ctxttid, cnt, i, tidcnt, tidoff;
  257. u16 *tidlist;
  258. struct qib_devdata *dd = rcd->dd;
  259. u64 physaddr;
  260. unsigned long vaddr;
  261. u64 __iomem *tidbase;
  262. unsigned long tidmap[8];
  263. struct page **pagep = NULL;
  264. unsigned subctxt = subctxt_fp(fp);
  265. if (!dd->pageshadow) {
  266. ret = -ENOMEM;
  267. goto done;
  268. }
  269. cnt = ti->tidcnt;
  270. if (!cnt) {
  271. ret = -EFAULT;
  272. goto done;
  273. }
  274. ctxttid = rcd->ctxt * dd->rcvtidcnt;
  275. if (!rcd->subctxt_cnt) {
  276. tidcnt = dd->rcvtidcnt;
  277. tid = rcd->tidcursor;
  278. tidoff = 0;
  279. } else if (!subctxt) {
  280. tidcnt = (dd->rcvtidcnt / rcd->subctxt_cnt) +
  281. (dd->rcvtidcnt % rcd->subctxt_cnt);
  282. tidoff = dd->rcvtidcnt - tidcnt;
  283. ctxttid += tidoff;
  284. tid = tidcursor_fp(fp);
  285. } else {
  286. tidcnt = dd->rcvtidcnt / rcd->subctxt_cnt;
  287. tidoff = tidcnt * (subctxt - 1);
  288. ctxttid += tidoff;
  289. tid = tidcursor_fp(fp);
  290. }
  291. if (cnt > tidcnt) {
  292. /* make sure it all fits in tid_pg_list */
  293. qib_devinfo(dd->pcidev, "Process tried to allocate %u "
  294. "TIDs, only trying max (%u)\n", cnt, tidcnt);
  295. cnt = tidcnt;
  296. }
  297. pagep = (struct page **) rcd->tid_pg_list;
  298. tidlist = (u16 *) &pagep[dd->rcvtidcnt];
  299. pagep += tidoff;
  300. tidlist += tidoff;
  301. memset(tidmap, 0, sizeof(tidmap));
  302. /* before decrement; chip actual # */
  303. ntids = tidcnt;
  304. tidbase = (u64 __iomem *) (((char __iomem *) dd->kregbase) +
  305. dd->rcvtidbase +
  306. ctxttid * sizeof(*tidbase));
  307. /* virtual address of first page in transfer */
  308. vaddr = ti->tidvaddr;
  309. if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
  310. cnt * PAGE_SIZE)) {
  311. ret = -EFAULT;
  312. goto done;
  313. }
  314. ret = qib_get_user_pages(vaddr, cnt, pagep);
  315. if (ret) {
  316. /*
  317. * if (ret == -EBUSY)
  318. * We can't continue because the pagep array won't be
  319. * initialized. This should never happen,
  320. * unless perhaps the user has mpin'ed the pages
  321. * themselves.
  322. */
  323. qib_devinfo(dd->pcidev,
  324. "Failed to lock addr %p, %u pages: "
  325. "errno %d\n", (void *) vaddr, cnt, -ret);
  326. goto done;
  327. }
  328. for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
  329. for (; ntids--; tid++) {
  330. if (tid == tidcnt)
  331. tid = 0;
  332. if (!dd->pageshadow[ctxttid + tid])
  333. break;
  334. }
  335. if (ntids < 0) {
  336. /*
  337. * Oops, wrapped all the way through their TIDs,
  338. * and didn't have enough free; see comments at
  339. * start of routine
  340. */
  341. i--; /* last tidlist[i] not filled in */
  342. ret = -ENOMEM;
  343. break;
  344. }
  345. tidlist[i] = tid + tidoff;
  346. /* we "know" system pages and TID pages are same size */
  347. dd->pageshadow[ctxttid + tid] = pagep[i];
  348. dd->physshadow[ctxttid + tid] =
  349. qib_map_page(dd->pcidev, pagep[i], 0, PAGE_SIZE,
  350. PCI_DMA_FROMDEVICE);
  351. /*
  352. * don't need atomic or it's overhead
  353. */
  354. __set_bit(tid, tidmap);
  355. physaddr = dd->physshadow[ctxttid + tid];
  356. /* PERFORMANCE: below should almost certainly be cached */
  357. dd->f_put_tid(dd, &tidbase[tid],
  358. RCVHQ_RCV_TYPE_EXPECTED, physaddr);
  359. /*
  360. * don't check this tid in qib_ctxtshadow, since we
  361. * just filled it in; start with the next one.
  362. */
  363. tid++;
  364. }
  365. if (ret) {
  366. u32 limit;
  367. cleanup:
  368. /* jump here if copy out of updated info failed... */
  369. /* same code that's in qib_free_tid() */
  370. limit = sizeof(tidmap) * BITS_PER_BYTE;
  371. if (limit > tidcnt)
  372. /* just in case size changes in future */
  373. limit = tidcnt;
  374. tid = find_first_bit((const unsigned long *)tidmap, limit);
  375. for (; tid < limit; tid++) {
  376. if (!test_bit(tid, tidmap))
  377. continue;
  378. if (dd->pageshadow[ctxttid + tid]) {
  379. dma_addr_t phys;
  380. phys = dd->physshadow[ctxttid + tid];
  381. dd->physshadow[ctxttid + tid] = dd->tidinvalid;
  382. /* PERFORMANCE: below should almost certainly
  383. * be cached
  384. */
  385. dd->f_put_tid(dd, &tidbase[tid],
  386. RCVHQ_RCV_TYPE_EXPECTED,
  387. dd->tidinvalid);
  388. pci_unmap_page(dd->pcidev, phys, PAGE_SIZE,
  389. PCI_DMA_FROMDEVICE);
  390. dd->pageshadow[ctxttid + tid] = NULL;
  391. }
  392. }
  393. qib_release_user_pages(pagep, cnt);
  394. } else {
  395. /*
  396. * Copy the updated array, with qib_tid's filled in, back
  397. * to user. Since we did the copy in already, this "should
  398. * never fail" If it does, we have to clean up...
  399. */
  400. if (copy_to_user((void __user *)
  401. (unsigned long) ti->tidlist,
  402. tidlist, cnt * sizeof(*tidlist))) {
  403. ret = -EFAULT;
  404. goto cleanup;
  405. }
  406. if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
  407. tidmap, sizeof tidmap)) {
  408. ret = -EFAULT;
  409. goto cleanup;
  410. }
  411. if (tid == tidcnt)
  412. tid = 0;
  413. if (!rcd->subctxt_cnt)
  414. rcd->tidcursor = tid;
  415. else
  416. tidcursor_fp(fp) = tid;
  417. }
  418. done:
  419. return ret;
  420. }
  421. /**
  422. * qib_tid_free - free a context TID
  423. * @rcd: the context
  424. * @subctxt: the subcontext
  425. * @ti: the TID info
  426. *
  427. * right now we are unlocking one page at a time, but since
  428. * the intended use of this routine is for a single group of
  429. * virtually contiguous pages, that should change to improve
  430. * performance. We check that the TID is in range for this context
  431. * but otherwise don't check validity; if user has an error and
  432. * frees the wrong tid, it's only their own data that can thereby
  433. * be corrupted. We do check that the TID was in use, for sanity
  434. * We always use our idea of the saved address, not the address that
  435. * they pass in to us.
  436. */
  437. static int qib_tid_free(struct qib_ctxtdata *rcd, unsigned subctxt,
  438. const struct qib_tid_info *ti)
  439. {
  440. int ret = 0;
  441. u32 tid, ctxttid, cnt, limit, tidcnt;
  442. struct qib_devdata *dd = rcd->dd;
  443. u64 __iomem *tidbase;
  444. unsigned long tidmap[8];
  445. if (!dd->pageshadow) {
  446. ret = -ENOMEM;
  447. goto done;
  448. }
  449. if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
  450. sizeof tidmap)) {
  451. ret = -EFAULT;
  452. goto done;
  453. }
  454. ctxttid = rcd->ctxt * dd->rcvtidcnt;
  455. if (!rcd->subctxt_cnt)
  456. tidcnt = dd->rcvtidcnt;
  457. else if (!subctxt) {
  458. tidcnt = (dd->rcvtidcnt / rcd->subctxt_cnt) +
  459. (dd->rcvtidcnt % rcd->subctxt_cnt);
  460. ctxttid += dd->rcvtidcnt - tidcnt;
  461. } else {
  462. tidcnt = dd->rcvtidcnt / rcd->subctxt_cnt;
  463. ctxttid += tidcnt * (subctxt - 1);
  464. }
  465. tidbase = (u64 __iomem *) ((char __iomem *)(dd->kregbase) +
  466. dd->rcvtidbase +
  467. ctxttid * sizeof(*tidbase));
  468. limit = sizeof(tidmap) * BITS_PER_BYTE;
  469. if (limit > tidcnt)
  470. /* just in case size changes in future */
  471. limit = tidcnt;
  472. tid = find_first_bit(tidmap, limit);
  473. for (cnt = 0; tid < limit; tid++) {
  474. /*
  475. * small optimization; if we detect a run of 3 or so without
  476. * any set, use find_first_bit again. That's mainly to
  477. * accelerate the case where we wrapped, so we have some at
  478. * the beginning, and some at the end, and a big gap
  479. * in the middle.
  480. */
  481. if (!test_bit(tid, tidmap))
  482. continue;
  483. cnt++;
  484. if (dd->pageshadow[ctxttid + tid]) {
  485. struct page *p;
  486. dma_addr_t phys;
  487. p = dd->pageshadow[ctxttid + tid];
  488. dd->pageshadow[ctxttid + tid] = NULL;
  489. phys = dd->physshadow[ctxttid + tid];
  490. dd->physshadow[ctxttid + tid] = dd->tidinvalid;
  491. /* PERFORMANCE: below should almost certainly be
  492. * cached
  493. */
  494. dd->f_put_tid(dd, &tidbase[tid],
  495. RCVHQ_RCV_TYPE_EXPECTED, dd->tidinvalid);
  496. pci_unmap_page(dd->pcidev, phys, PAGE_SIZE,
  497. PCI_DMA_FROMDEVICE);
  498. qib_release_user_pages(&p, 1);
  499. }
  500. }
  501. done:
  502. return ret;
  503. }
  504. /**
  505. * qib_set_part_key - set a partition key
  506. * @rcd: the context
  507. * @key: the key
  508. *
  509. * We can have up to 4 active at a time (other than the default, which is
  510. * always allowed). This is somewhat tricky, since multiple contexts may set
  511. * the same key, so we reference count them, and clean up at exit. All 4
  512. * partition keys are packed into a single qlogic_ib register. It's an
  513. * error for a process to set the same pkey multiple times. We provide no
  514. * mechanism to de-allocate a pkey at this time, we may eventually need to
  515. * do that. I've used the atomic operations, and no locking, and only make
  516. * a single pass through what's available. This should be more than
  517. * adequate for some time. I'll think about spinlocks or the like if and as
  518. * it's necessary.
  519. */
  520. static int qib_set_part_key(struct qib_ctxtdata *rcd, u16 key)
  521. {
  522. struct qib_pportdata *ppd = rcd->ppd;
  523. int i, any = 0, pidx = -1;
  524. u16 lkey = key & 0x7FFF;
  525. int ret;
  526. if (lkey == (QIB_DEFAULT_P_KEY & 0x7FFF)) {
  527. /* nothing to do; this key always valid */
  528. ret = 0;
  529. goto bail;
  530. }
  531. if (!lkey) {
  532. ret = -EINVAL;
  533. goto bail;
  534. }
  535. /*
  536. * Set the full membership bit, because it has to be
  537. * set in the register or the packet, and it seems
  538. * cleaner to set in the register than to force all
  539. * callers to set it.
  540. */
  541. key |= 0x8000;
  542. for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
  543. if (!rcd->pkeys[i] && pidx == -1)
  544. pidx = i;
  545. if (rcd->pkeys[i] == key) {
  546. ret = -EEXIST;
  547. goto bail;
  548. }
  549. }
  550. if (pidx == -1) {
  551. ret = -EBUSY;
  552. goto bail;
  553. }
  554. for (any = i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
  555. if (!ppd->pkeys[i]) {
  556. any++;
  557. continue;
  558. }
  559. if (ppd->pkeys[i] == key) {
  560. atomic_t *pkrefs = &ppd->pkeyrefs[i];
  561. if (atomic_inc_return(pkrefs) > 1) {
  562. rcd->pkeys[pidx] = key;
  563. ret = 0;
  564. goto bail;
  565. } else {
  566. /*
  567. * lost race, decrement count, catch below
  568. */
  569. atomic_dec(pkrefs);
  570. any++;
  571. }
  572. }
  573. if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
  574. /*
  575. * It makes no sense to have both the limited and
  576. * full membership PKEY set at the same time since
  577. * the unlimited one will disable the limited one.
  578. */
  579. ret = -EEXIST;
  580. goto bail;
  581. }
  582. }
  583. if (!any) {
  584. ret = -EBUSY;
  585. goto bail;
  586. }
  587. for (any = i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
  588. if (!ppd->pkeys[i] &&
  589. atomic_inc_return(&ppd->pkeyrefs[i]) == 1) {
  590. rcd->pkeys[pidx] = key;
  591. ppd->pkeys[i] = key;
  592. (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
  593. ret = 0;
  594. goto bail;
  595. }
  596. }
  597. ret = -EBUSY;
  598. bail:
  599. return ret;
  600. }
  601. /**
  602. * qib_manage_rcvq - manage a context's receive queue
  603. * @rcd: the context
  604. * @subctxt: the subcontext
  605. * @start_stop: action to carry out
  606. *
  607. * start_stop == 0 disables receive on the context, for use in queue
  608. * overflow conditions. start_stop==1 re-enables, to be used to
  609. * re-init the software copy of the head register
  610. */
  611. static int qib_manage_rcvq(struct qib_ctxtdata *rcd, unsigned subctxt,
  612. int start_stop)
  613. {
  614. struct qib_devdata *dd = rcd->dd;
  615. unsigned int rcvctrl_op;
  616. if (subctxt)
  617. goto bail;
  618. /* atomically clear receive enable ctxt. */
  619. if (start_stop) {
  620. /*
  621. * On enable, force in-memory copy of the tail register to
  622. * 0, so that protocol code doesn't have to worry about
  623. * whether or not the chip has yet updated the in-memory
  624. * copy or not on return from the system call. The chip
  625. * always resets it's tail register back to 0 on a
  626. * transition from disabled to enabled.
  627. */
  628. if (rcd->rcvhdrtail_kvaddr)
  629. qib_clear_rcvhdrtail(rcd);
  630. rcvctrl_op = QIB_RCVCTRL_CTXT_ENB;
  631. } else
  632. rcvctrl_op = QIB_RCVCTRL_CTXT_DIS;
  633. dd->f_rcvctrl(rcd->ppd, rcvctrl_op, rcd->ctxt);
  634. /* always; new head should be equal to new tail; see above */
  635. bail:
  636. return 0;
  637. }
  638. static void qib_clean_part_key(struct qib_ctxtdata *rcd,
  639. struct qib_devdata *dd)
  640. {
  641. int i, j, pchanged = 0;
  642. u64 oldpkey;
  643. struct qib_pportdata *ppd = rcd->ppd;
  644. /* for debugging only */
  645. oldpkey = (u64) ppd->pkeys[0] |
  646. ((u64) ppd->pkeys[1] << 16) |
  647. ((u64) ppd->pkeys[2] << 32) |
  648. ((u64) ppd->pkeys[3] << 48);
  649. for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
  650. if (!rcd->pkeys[i])
  651. continue;
  652. for (j = 0; j < ARRAY_SIZE(ppd->pkeys); j++) {
  653. /* check for match independent of the global bit */
  654. if ((ppd->pkeys[j] & 0x7fff) !=
  655. (rcd->pkeys[i] & 0x7fff))
  656. continue;
  657. if (atomic_dec_and_test(&ppd->pkeyrefs[j])) {
  658. ppd->pkeys[j] = 0;
  659. pchanged++;
  660. }
  661. break;
  662. }
  663. rcd->pkeys[i] = 0;
  664. }
  665. if (pchanged)
  666. (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
  667. }
  668. /* common code for the mappings on dma_alloc_coherent mem */
  669. static int qib_mmap_mem(struct vm_area_struct *vma, struct qib_ctxtdata *rcd,
  670. unsigned len, void *kvaddr, u32 write_ok, char *what)
  671. {
  672. struct qib_devdata *dd = rcd->dd;
  673. unsigned long pfn;
  674. int ret;
  675. if ((vma->vm_end - vma->vm_start) > len) {
  676. qib_devinfo(dd->pcidev,
  677. "FAIL on %s: len %lx > %x\n", what,
  678. vma->vm_end - vma->vm_start, len);
  679. ret = -EFAULT;
  680. goto bail;
  681. }
  682. /*
  683. * shared context user code requires rcvhdrq mapped r/w, others
  684. * only allowed readonly mapping.
  685. */
  686. if (!write_ok) {
  687. if (vma->vm_flags & VM_WRITE) {
  688. qib_devinfo(dd->pcidev,
  689. "%s must be mapped readonly\n", what);
  690. ret = -EPERM;
  691. goto bail;
  692. }
  693. /* don't allow them to later change with mprotect */
  694. vma->vm_flags &= ~VM_MAYWRITE;
  695. }
  696. pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
  697. ret = remap_pfn_range(vma, vma->vm_start, pfn,
  698. len, vma->vm_page_prot);
  699. if (ret)
  700. qib_devinfo(dd->pcidev, "%s ctxt%u mmap of %lx, %x "
  701. "bytes failed: %d\n", what, rcd->ctxt,
  702. pfn, len, ret);
  703. bail:
  704. return ret;
  705. }
  706. static int mmap_ureg(struct vm_area_struct *vma, struct qib_devdata *dd,
  707. u64 ureg)
  708. {
  709. unsigned long phys;
  710. unsigned long sz;
  711. int ret;
  712. /*
  713. * This is real hardware, so use io_remap. This is the mechanism
  714. * for the user process to update the head registers for their ctxt
  715. * in the chip.
  716. */
  717. sz = dd->flags & QIB_HAS_HDRSUPP ? 2 * PAGE_SIZE : PAGE_SIZE;
  718. if ((vma->vm_end - vma->vm_start) > sz) {
  719. qib_devinfo(dd->pcidev, "FAIL mmap userreg: reqlen "
  720. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  721. ret = -EFAULT;
  722. } else {
  723. phys = dd->physaddr + ureg;
  724. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  725. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  726. ret = io_remap_pfn_range(vma, vma->vm_start,
  727. phys >> PAGE_SHIFT,
  728. vma->vm_end - vma->vm_start,
  729. vma->vm_page_prot);
  730. }
  731. return ret;
  732. }
  733. static int mmap_piobufs(struct vm_area_struct *vma,
  734. struct qib_devdata *dd,
  735. struct qib_ctxtdata *rcd,
  736. unsigned piobufs, unsigned piocnt)
  737. {
  738. unsigned long phys;
  739. int ret;
  740. /*
  741. * When we map the PIO buffers in the chip, we want to map them as
  742. * writeonly, no read possible; unfortunately, x86 doesn't allow
  743. * for this in hardware, but we still prevent users from asking
  744. * for it.
  745. */
  746. if ((vma->vm_end - vma->vm_start) > (piocnt * dd->palign)) {
  747. qib_devinfo(dd->pcidev, "FAIL mmap piobufs: "
  748. "reqlen %lx > PAGE\n",
  749. vma->vm_end - vma->vm_start);
  750. ret = -EINVAL;
  751. goto bail;
  752. }
  753. phys = dd->physaddr + piobufs;
  754. #if defined(__powerpc__)
  755. /* There isn't a generic way to specify writethrough mappings */
  756. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  757. pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
  758. pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
  759. #endif
  760. /*
  761. * don't allow them to later change to readable with mprotect (for when
  762. * not initially mapped readable, as is normally the case)
  763. */
  764. vma->vm_flags &= ~VM_MAYREAD;
  765. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  766. if (qib_wc_pat)
  767. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  768. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  769. vma->vm_end - vma->vm_start,
  770. vma->vm_page_prot);
  771. bail:
  772. return ret;
  773. }
  774. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  775. struct qib_ctxtdata *rcd)
  776. {
  777. struct qib_devdata *dd = rcd->dd;
  778. unsigned long start, size;
  779. size_t total_size, i;
  780. unsigned long pfn;
  781. int ret;
  782. size = rcd->rcvegrbuf_size;
  783. total_size = rcd->rcvegrbuf_chunks * size;
  784. if ((vma->vm_end - vma->vm_start) > total_size) {
  785. qib_devinfo(dd->pcidev, "FAIL on egr bufs: "
  786. "reqlen %lx > actual %lx\n",
  787. vma->vm_end - vma->vm_start,
  788. (unsigned long) total_size);
  789. ret = -EINVAL;
  790. goto bail;
  791. }
  792. if (vma->vm_flags & VM_WRITE) {
  793. qib_devinfo(dd->pcidev, "Can't map eager buffers as "
  794. "writable (flags=%lx)\n", vma->vm_flags);
  795. ret = -EPERM;
  796. goto bail;
  797. }
  798. /* don't allow them to later change to writeable with mprotect */
  799. vma->vm_flags &= ~VM_MAYWRITE;
  800. start = vma->vm_start;
  801. for (i = 0; i < rcd->rcvegrbuf_chunks; i++, start += size) {
  802. pfn = virt_to_phys(rcd->rcvegrbuf[i]) >> PAGE_SHIFT;
  803. ret = remap_pfn_range(vma, start, pfn, size,
  804. vma->vm_page_prot);
  805. if (ret < 0)
  806. goto bail;
  807. }
  808. ret = 0;
  809. bail:
  810. return ret;
  811. }
  812. /*
  813. * qib_file_vma_fault - handle a VMA page fault.
  814. */
  815. static int qib_file_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  816. {
  817. struct page *page;
  818. page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
  819. if (!page)
  820. return VM_FAULT_SIGBUS;
  821. get_page(page);
  822. vmf->page = page;
  823. return 0;
  824. }
  825. static struct vm_operations_struct qib_file_vm_ops = {
  826. .fault = qib_file_vma_fault,
  827. };
  828. static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
  829. struct qib_ctxtdata *rcd, unsigned subctxt)
  830. {
  831. struct qib_devdata *dd = rcd->dd;
  832. unsigned subctxt_cnt;
  833. unsigned long len;
  834. void *addr;
  835. size_t size;
  836. int ret = 0;
  837. subctxt_cnt = rcd->subctxt_cnt;
  838. size = rcd->rcvegrbuf_chunks * rcd->rcvegrbuf_size;
  839. /*
  840. * Each process has all the subctxt uregbase, rcvhdrq, and
  841. * rcvegrbufs mmapped - as an array for all the processes,
  842. * and also separately for this process.
  843. */
  844. if (pgaddr == cvt_kvaddr(rcd->subctxt_uregbase)) {
  845. addr = rcd->subctxt_uregbase;
  846. size = PAGE_SIZE * subctxt_cnt;
  847. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvhdr_base)) {
  848. addr = rcd->subctxt_rcvhdr_base;
  849. size = rcd->rcvhdrq_size * subctxt_cnt;
  850. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvegrbuf)) {
  851. addr = rcd->subctxt_rcvegrbuf;
  852. size *= subctxt_cnt;
  853. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_uregbase +
  854. PAGE_SIZE * subctxt)) {
  855. addr = rcd->subctxt_uregbase + PAGE_SIZE * subctxt;
  856. size = PAGE_SIZE;
  857. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvhdr_base +
  858. rcd->rcvhdrq_size * subctxt)) {
  859. addr = rcd->subctxt_rcvhdr_base +
  860. rcd->rcvhdrq_size * subctxt;
  861. size = rcd->rcvhdrq_size;
  862. } else if (pgaddr == cvt_kvaddr(&rcd->user_event_mask[subctxt])) {
  863. addr = rcd->user_event_mask;
  864. size = PAGE_SIZE;
  865. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvegrbuf +
  866. size * subctxt)) {
  867. addr = rcd->subctxt_rcvegrbuf + size * subctxt;
  868. /* rcvegrbufs are read-only on the slave */
  869. if (vma->vm_flags & VM_WRITE) {
  870. qib_devinfo(dd->pcidev,
  871. "Can't map eager buffers as "
  872. "writable (flags=%lx)\n", vma->vm_flags);
  873. ret = -EPERM;
  874. goto bail;
  875. }
  876. /*
  877. * Don't allow permission to later change to writeable
  878. * with mprotect.
  879. */
  880. vma->vm_flags &= ~VM_MAYWRITE;
  881. } else
  882. goto bail;
  883. len = vma->vm_end - vma->vm_start;
  884. if (len > size) {
  885. ret = -EINVAL;
  886. goto bail;
  887. }
  888. vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
  889. vma->vm_ops = &qib_file_vm_ops;
  890. vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
  891. ret = 1;
  892. bail:
  893. return ret;
  894. }
  895. /**
  896. * qib_mmapf - mmap various structures into user space
  897. * @fp: the file pointer
  898. * @vma: the VM area
  899. *
  900. * We use this to have a shared buffer between the kernel and the user code
  901. * for the rcvhdr queue, egr buffers, and the per-context user regs and pio
  902. * buffers in the chip. We have the open and close entries so we can bump
  903. * the ref count and keep the driver from being unloaded while still mapped.
  904. */
  905. static int qib_mmapf(struct file *fp, struct vm_area_struct *vma)
  906. {
  907. struct qib_ctxtdata *rcd;
  908. struct qib_devdata *dd;
  909. u64 pgaddr, ureg;
  910. unsigned piobufs, piocnt;
  911. int ret, match = 1;
  912. rcd = ctxt_fp(fp);
  913. if (!rcd || !(vma->vm_flags & VM_SHARED)) {
  914. ret = -EINVAL;
  915. goto bail;
  916. }
  917. dd = rcd->dd;
  918. /*
  919. * This is the qib_do_user_init() code, mapping the shared buffers
  920. * and per-context user registers into the user process. The address
  921. * referred to by vm_pgoff is the file offset passed via mmap().
  922. * For shared contexts, this is the kernel vmalloc() address of the
  923. * pages to share with the master.
  924. * For non-shared or master ctxts, this is a physical address.
  925. * We only do one mmap for each space mapped.
  926. */
  927. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  928. /*
  929. * Check for 0 in case one of the allocations failed, but user
  930. * called mmap anyway.
  931. */
  932. if (!pgaddr) {
  933. ret = -EINVAL;
  934. goto bail;
  935. }
  936. /*
  937. * Physical addresses must fit in 40 bits for our hardware.
  938. * Check for kernel virtual addresses first, anything else must
  939. * match a HW or memory address.
  940. */
  941. ret = mmap_kvaddr(vma, pgaddr, rcd, subctxt_fp(fp));
  942. if (ret) {
  943. if (ret > 0)
  944. ret = 0;
  945. goto bail;
  946. }
  947. ureg = dd->uregbase + dd->ureg_align * rcd->ctxt;
  948. if (!rcd->subctxt_cnt) {
  949. /* ctxt is not shared */
  950. piocnt = rcd->piocnt;
  951. piobufs = rcd->piobufs;
  952. } else if (!subctxt_fp(fp)) {
  953. /* caller is the master */
  954. piocnt = (rcd->piocnt / rcd->subctxt_cnt) +
  955. (rcd->piocnt % rcd->subctxt_cnt);
  956. piobufs = rcd->piobufs +
  957. dd->palign * (rcd->piocnt - piocnt);
  958. } else {
  959. unsigned slave = subctxt_fp(fp) - 1;
  960. /* caller is a slave */
  961. piocnt = rcd->piocnt / rcd->subctxt_cnt;
  962. piobufs = rcd->piobufs + dd->palign * piocnt * slave;
  963. }
  964. if (pgaddr == ureg)
  965. ret = mmap_ureg(vma, dd, ureg);
  966. else if (pgaddr == piobufs)
  967. ret = mmap_piobufs(vma, dd, rcd, piobufs, piocnt);
  968. else if (pgaddr == dd->pioavailregs_phys)
  969. /* in-memory copy of pioavail registers */
  970. ret = qib_mmap_mem(vma, rcd, PAGE_SIZE,
  971. (void *) dd->pioavailregs_dma, 0,
  972. "pioavail registers");
  973. else if (pgaddr == rcd->rcvegr_phys)
  974. ret = mmap_rcvegrbufs(vma, rcd);
  975. else if (pgaddr == (u64) rcd->rcvhdrq_phys)
  976. /*
  977. * The rcvhdrq itself; multiple pages, contiguous
  978. * from an i/o perspective. Shared contexts need
  979. * to map r/w, so we allow writing.
  980. */
  981. ret = qib_mmap_mem(vma, rcd, rcd->rcvhdrq_size,
  982. rcd->rcvhdrq, 1, "rcvhdrq");
  983. else if (pgaddr == (u64) rcd->rcvhdrqtailaddr_phys)
  984. /* in-memory copy of rcvhdrq tail register */
  985. ret = qib_mmap_mem(vma, rcd, PAGE_SIZE,
  986. rcd->rcvhdrtail_kvaddr, 0,
  987. "rcvhdrq tail");
  988. else
  989. match = 0;
  990. if (!match)
  991. ret = -EINVAL;
  992. vma->vm_private_data = NULL;
  993. if (ret < 0)
  994. qib_devinfo(dd->pcidev,
  995. "mmap Failure %d: off %llx len %lx\n",
  996. -ret, (unsigned long long)pgaddr,
  997. vma->vm_end - vma->vm_start);
  998. bail:
  999. return ret;
  1000. }
  1001. static unsigned int qib_poll_urgent(struct qib_ctxtdata *rcd,
  1002. struct file *fp,
  1003. struct poll_table_struct *pt)
  1004. {
  1005. struct qib_devdata *dd = rcd->dd;
  1006. unsigned pollflag;
  1007. poll_wait(fp, &rcd->wait, pt);
  1008. spin_lock_irq(&dd->uctxt_lock);
  1009. if (rcd->urgent != rcd->urgent_poll) {
  1010. pollflag = POLLIN | POLLRDNORM;
  1011. rcd->urgent_poll = rcd->urgent;
  1012. } else {
  1013. pollflag = 0;
  1014. set_bit(QIB_CTXT_WAITING_URG, &rcd->flag);
  1015. }
  1016. spin_unlock_irq(&dd->uctxt_lock);
  1017. return pollflag;
  1018. }
  1019. static unsigned int qib_poll_next(struct qib_ctxtdata *rcd,
  1020. struct file *fp,
  1021. struct poll_table_struct *pt)
  1022. {
  1023. struct qib_devdata *dd = rcd->dd;
  1024. unsigned pollflag;
  1025. poll_wait(fp, &rcd->wait, pt);
  1026. spin_lock_irq(&dd->uctxt_lock);
  1027. if (dd->f_hdrqempty(rcd)) {
  1028. set_bit(QIB_CTXT_WAITING_RCV, &rcd->flag);
  1029. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_INTRAVAIL_ENB, rcd->ctxt);
  1030. pollflag = 0;
  1031. } else
  1032. pollflag = POLLIN | POLLRDNORM;
  1033. spin_unlock_irq(&dd->uctxt_lock);
  1034. return pollflag;
  1035. }
  1036. static unsigned int qib_poll(struct file *fp, struct poll_table_struct *pt)
  1037. {
  1038. struct qib_ctxtdata *rcd;
  1039. unsigned pollflag;
  1040. rcd = ctxt_fp(fp);
  1041. if (!rcd)
  1042. pollflag = POLLERR;
  1043. else if (rcd->poll_type == QIB_POLL_TYPE_URGENT)
  1044. pollflag = qib_poll_urgent(rcd, fp, pt);
  1045. else if (rcd->poll_type == QIB_POLL_TYPE_ANYRCV)
  1046. pollflag = qib_poll_next(rcd, fp, pt);
  1047. else /* invalid */
  1048. pollflag = POLLERR;
  1049. return pollflag;
  1050. }
  1051. /*
  1052. * Check that userland and driver are compatible for subcontexts.
  1053. */
  1054. static int qib_compatible_subctxts(int user_swmajor, int user_swminor)
  1055. {
  1056. /* this code is written long-hand for clarity */
  1057. if (QIB_USER_SWMAJOR != user_swmajor) {
  1058. /* no promise of compatibility if major mismatch */
  1059. return 0;
  1060. }
  1061. if (QIB_USER_SWMAJOR == 1) {
  1062. switch (QIB_USER_SWMINOR) {
  1063. case 0:
  1064. case 1:
  1065. case 2:
  1066. /* no subctxt implementation so cannot be compatible */
  1067. return 0;
  1068. case 3:
  1069. /* 3 is only compatible with itself */
  1070. return user_swminor == 3;
  1071. default:
  1072. /* >= 4 are compatible (or are expected to be) */
  1073. return user_swminor >= 4;
  1074. }
  1075. }
  1076. /* make no promises yet for future major versions */
  1077. return 0;
  1078. }
  1079. static int init_subctxts(struct qib_devdata *dd,
  1080. struct qib_ctxtdata *rcd,
  1081. const struct qib_user_info *uinfo)
  1082. {
  1083. int ret = 0;
  1084. unsigned num_subctxts;
  1085. size_t size;
  1086. /*
  1087. * If the user is requesting zero subctxts,
  1088. * skip the subctxt allocation.
  1089. */
  1090. if (uinfo->spu_subctxt_cnt <= 0)
  1091. goto bail;
  1092. num_subctxts = uinfo->spu_subctxt_cnt;
  1093. /* Check for subctxt compatibility */
  1094. if (!qib_compatible_subctxts(uinfo->spu_userversion >> 16,
  1095. uinfo->spu_userversion & 0xffff)) {
  1096. qib_devinfo(dd->pcidev,
  1097. "Mismatched user version (%d.%d) and driver "
  1098. "version (%d.%d) while context sharing. Ensure "
  1099. "that driver and library are from the same "
  1100. "release.\n",
  1101. (int) (uinfo->spu_userversion >> 16),
  1102. (int) (uinfo->spu_userversion & 0xffff),
  1103. QIB_USER_SWMAJOR, QIB_USER_SWMINOR);
  1104. goto bail;
  1105. }
  1106. if (num_subctxts > QLOGIC_IB_MAX_SUBCTXT) {
  1107. ret = -EINVAL;
  1108. goto bail;
  1109. }
  1110. rcd->subctxt_uregbase = vmalloc_user(PAGE_SIZE * num_subctxts);
  1111. if (!rcd->subctxt_uregbase) {
  1112. ret = -ENOMEM;
  1113. goto bail;
  1114. }
  1115. /* Note: rcd->rcvhdrq_size isn't initialized yet. */
  1116. size = ALIGN(dd->rcvhdrcnt * dd->rcvhdrentsize *
  1117. sizeof(u32), PAGE_SIZE) * num_subctxts;
  1118. rcd->subctxt_rcvhdr_base = vmalloc_user(size);
  1119. if (!rcd->subctxt_rcvhdr_base) {
  1120. ret = -ENOMEM;
  1121. goto bail_ureg;
  1122. }
  1123. rcd->subctxt_rcvegrbuf = vmalloc_user(rcd->rcvegrbuf_chunks *
  1124. rcd->rcvegrbuf_size *
  1125. num_subctxts);
  1126. if (!rcd->subctxt_rcvegrbuf) {
  1127. ret = -ENOMEM;
  1128. goto bail_rhdr;
  1129. }
  1130. rcd->subctxt_cnt = uinfo->spu_subctxt_cnt;
  1131. rcd->subctxt_id = uinfo->spu_subctxt_id;
  1132. rcd->active_slaves = 1;
  1133. rcd->redirect_seq_cnt = 1;
  1134. set_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag);
  1135. goto bail;
  1136. bail_rhdr:
  1137. vfree(rcd->subctxt_rcvhdr_base);
  1138. bail_ureg:
  1139. vfree(rcd->subctxt_uregbase);
  1140. rcd->subctxt_uregbase = NULL;
  1141. bail:
  1142. return ret;
  1143. }
  1144. static int setup_ctxt(struct qib_pportdata *ppd, int ctxt,
  1145. struct file *fp, const struct qib_user_info *uinfo)
  1146. {
  1147. struct qib_devdata *dd = ppd->dd;
  1148. struct qib_ctxtdata *rcd;
  1149. void *ptmp = NULL;
  1150. int ret;
  1151. rcd = qib_create_ctxtdata(ppd, ctxt);
  1152. /*
  1153. * Allocate memory for use in qib_tid_update() at open to
  1154. * reduce cost of expected send setup per message segment
  1155. */
  1156. if (rcd)
  1157. ptmp = kmalloc(dd->rcvtidcnt * sizeof(u16) +
  1158. dd->rcvtidcnt * sizeof(struct page **),
  1159. GFP_KERNEL);
  1160. if (!rcd || !ptmp) {
  1161. qib_dev_err(dd, "Unable to allocate ctxtdata "
  1162. "memory, failing open\n");
  1163. ret = -ENOMEM;
  1164. goto bailerr;
  1165. }
  1166. rcd->userversion = uinfo->spu_userversion;
  1167. ret = init_subctxts(dd, rcd, uinfo);
  1168. if (ret)
  1169. goto bailerr;
  1170. rcd->tid_pg_list = ptmp;
  1171. rcd->pid = current->pid;
  1172. init_waitqueue_head(&dd->rcd[ctxt]->wait);
  1173. strlcpy(rcd->comm, current->comm, sizeof(rcd->comm));
  1174. ctxt_fp(fp) = rcd;
  1175. qib_stats.sps_ctxts++;
  1176. ret = 0;
  1177. goto bail;
  1178. bailerr:
  1179. dd->rcd[ctxt] = NULL;
  1180. kfree(rcd);
  1181. kfree(ptmp);
  1182. bail:
  1183. return ret;
  1184. }
  1185. static inline int usable(struct qib_pportdata *ppd, int active_only)
  1186. {
  1187. struct qib_devdata *dd = ppd->dd;
  1188. u32 linkok = active_only ? QIBL_LINKACTIVE :
  1189. (QIBL_LINKINIT | QIBL_LINKARMED | QIBL_LINKACTIVE);
  1190. return dd && (dd->flags & QIB_PRESENT) && dd->kregbase && ppd->lid &&
  1191. (ppd->lflags & linkok);
  1192. }
  1193. static int find_free_ctxt(int unit, struct file *fp,
  1194. const struct qib_user_info *uinfo)
  1195. {
  1196. struct qib_devdata *dd = qib_lookup(unit);
  1197. struct qib_pportdata *ppd = NULL;
  1198. int ret;
  1199. u32 ctxt;
  1200. if (!dd || (uinfo->spu_port && uinfo->spu_port > dd->num_pports)) {
  1201. ret = -ENODEV;
  1202. goto bail;
  1203. }
  1204. /*
  1205. * If users requests specific port, only try that one port, else
  1206. * select "best" port below, based on context.
  1207. */
  1208. if (uinfo->spu_port) {
  1209. ppd = dd->pport + uinfo->spu_port - 1;
  1210. if (!usable(ppd, 0)) {
  1211. ret = -ENETDOWN;
  1212. goto bail;
  1213. }
  1214. }
  1215. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
  1216. if (dd->rcd[ctxt])
  1217. continue;
  1218. /*
  1219. * The setting and clearing of user context rcd[x] protected
  1220. * by the qib_mutex
  1221. */
  1222. if (!ppd) {
  1223. /* choose port based on ctxt, if up, else 1st up */
  1224. ppd = dd->pport + (ctxt % dd->num_pports);
  1225. if (!usable(ppd, 0)) {
  1226. int i;
  1227. for (i = 0; i < dd->num_pports; i++) {
  1228. ppd = dd->pport + i;
  1229. if (usable(ppd, 0))
  1230. break;
  1231. }
  1232. if (i == dd->num_pports) {
  1233. ret = -ENETDOWN;
  1234. goto bail;
  1235. }
  1236. }
  1237. }
  1238. ret = setup_ctxt(ppd, ctxt, fp, uinfo);
  1239. goto bail;
  1240. }
  1241. ret = -EBUSY;
  1242. bail:
  1243. return ret;
  1244. }
  1245. static int get_a_ctxt(struct file *fp, const struct qib_user_info *uinfo)
  1246. {
  1247. struct qib_pportdata *ppd;
  1248. int ret = 0, devmax;
  1249. int npresent, nup;
  1250. int ndev;
  1251. u32 port = uinfo->spu_port, ctxt;
  1252. devmax = qib_count_units(&npresent, &nup);
  1253. for (ndev = 0; ndev < devmax; ndev++) {
  1254. struct qib_devdata *dd = qib_lookup(ndev);
  1255. /* device portion of usable() */
  1256. if (!(dd && (dd->flags & QIB_PRESENT) && dd->kregbase))
  1257. continue;
  1258. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts; ctxt++) {
  1259. if (dd->rcd[ctxt])
  1260. continue;
  1261. if (port) {
  1262. if (port > dd->num_pports)
  1263. continue;
  1264. ppd = dd->pport + port - 1;
  1265. if (!usable(ppd, 0))
  1266. continue;
  1267. } else {
  1268. /*
  1269. * choose port based on ctxt, if up, else
  1270. * first port that's up for multi-port HCA
  1271. */
  1272. ppd = dd->pport + (ctxt % dd->num_pports);
  1273. if (!usable(ppd, 0)) {
  1274. int j;
  1275. ppd = NULL;
  1276. for (j = 0; j < dd->num_pports &&
  1277. !ppd; j++)
  1278. if (usable(dd->pport + j, 0))
  1279. ppd = dd->pport + j;
  1280. if (!ppd)
  1281. continue; /* to next unit */
  1282. }
  1283. }
  1284. ret = setup_ctxt(ppd, ctxt, fp, uinfo);
  1285. goto done;
  1286. }
  1287. }
  1288. if (npresent) {
  1289. if (nup == 0)
  1290. ret = -ENETDOWN;
  1291. else
  1292. ret = -EBUSY;
  1293. } else
  1294. ret = -ENXIO;
  1295. done:
  1296. return ret;
  1297. }
  1298. static int find_shared_ctxt(struct file *fp,
  1299. const struct qib_user_info *uinfo)
  1300. {
  1301. int devmax, ndev, i;
  1302. int ret = 0;
  1303. devmax = qib_count_units(NULL, NULL);
  1304. for (ndev = 0; ndev < devmax; ndev++) {
  1305. struct qib_devdata *dd = qib_lookup(ndev);
  1306. /* device portion of usable() */
  1307. if (!(dd && (dd->flags & QIB_PRESENT) && dd->kregbase))
  1308. continue;
  1309. for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
  1310. struct qib_ctxtdata *rcd = dd->rcd[i];
  1311. /* Skip ctxts which are not yet open */
  1312. if (!rcd || !rcd->cnt)
  1313. continue;
  1314. /* Skip ctxt if it doesn't match the requested one */
  1315. if (rcd->subctxt_id != uinfo->spu_subctxt_id)
  1316. continue;
  1317. /* Verify the sharing process matches the master */
  1318. if (rcd->subctxt_cnt != uinfo->spu_subctxt_cnt ||
  1319. rcd->userversion != uinfo->spu_userversion ||
  1320. rcd->cnt >= rcd->subctxt_cnt) {
  1321. ret = -EINVAL;
  1322. goto done;
  1323. }
  1324. ctxt_fp(fp) = rcd;
  1325. subctxt_fp(fp) = rcd->cnt++;
  1326. rcd->subpid[subctxt_fp(fp)] = current->pid;
  1327. tidcursor_fp(fp) = 0;
  1328. rcd->active_slaves |= 1 << subctxt_fp(fp);
  1329. ret = 1;
  1330. goto done;
  1331. }
  1332. }
  1333. done:
  1334. return ret;
  1335. }
  1336. static int qib_open(struct inode *in, struct file *fp)
  1337. {
  1338. /* The real work is performed later in qib_assign_ctxt() */
  1339. fp->private_data = kzalloc(sizeof(struct qib_filedata), GFP_KERNEL);
  1340. if (fp->private_data) /* no cpu affinity by default */
  1341. ((struct qib_filedata *)fp->private_data)->rec_cpu_num = -1;
  1342. return fp->private_data ? 0 : -ENOMEM;
  1343. }
  1344. /*
  1345. * Get ctxt early, so can set affinity prior to memory allocation.
  1346. */
  1347. static int qib_assign_ctxt(struct file *fp, const struct qib_user_info *uinfo)
  1348. {
  1349. int ret;
  1350. int i_minor;
  1351. unsigned swmajor, swminor;
  1352. /* Check to be sure we haven't already initialized this file */
  1353. if (ctxt_fp(fp)) {
  1354. ret = -EINVAL;
  1355. goto done;
  1356. }
  1357. /* for now, if major version is different, bail */
  1358. swmajor = uinfo->spu_userversion >> 16;
  1359. if (swmajor != QIB_USER_SWMAJOR) {
  1360. ret = -ENODEV;
  1361. goto done;
  1362. }
  1363. swminor = uinfo->spu_userversion & 0xffff;
  1364. mutex_lock(&qib_mutex);
  1365. if (qib_compatible_subctxts(swmajor, swminor) &&
  1366. uinfo->spu_subctxt_cnt) {
  1367. ret = find_shared_ctxt(fp, uinfo);
  1368. if (ret) {
  1369. if (ret > 0)
  1370. ret = 0;
  1371. goto done_chk_sdma;
  1372. }
  1373. }
  1374. i_minor = iminor(fp->f_dentry->d_inode) - QIB_USER_MINOR_BASE;
  1375. if (i_minor)
  1376. ret = find_free_ctxt(i_minor - 1, fp, uinfo);
  1377. else
  1378. ret = get_a_ctxt(fp, uinfo);
  1379. done_chk_sdma:
  1380. if (!ret) {
  1381. struct qib_filedata *fd = fp->private_data;
  1382. const struct qib_ctxtdata *rcd = fd->rcd;
  1383. const struct qib_devdata *dd = rcd->dd;
  1384. if (dd->flags & QIB_HAS_SEND_DMA) {
  1385. fd->pq = qib_user_sdma_queue_create(&dd->pcidev->dev,
  1386. dd->unit,
  1387. rcd->ctxt,
  1388. fd->subctxt);
  1389. if (!fd->pq)
  1390. ret = -ENOMEM;
  1391. }
  1392. /*
  1393. * If process has NOT already set it's affinity, select and
  1394. * reserve a processor for it, as a rendevous for all
  1395. * users of the driver. If they don't actually later
  1396. * set affinity to this cpu, or set it to some other cpu,
  1397. * it just means that sooner or later we don't recommend
  1398. * a cpu, and let the scheduler do it's best.
  1399. */
  1400. if (!ret && cpus_weight(current->cpus_allowed) >=
  1401. qib_cpulist_count) {
  1402. int cpu;
  1403. cpu = find_first_zero_bit(qib_cpulist,
  1404. qib_cpulist_count);
  1405. if (cpu != qib_cpulist_count) {
  1406. __set_bit(cpu, qib_cpulist);
  1407. fd->rec_cpu_num = cpu;
  1408. }
  1409. } else if (cpus_weight(current->cpus_allowed) == 1 &&
  1410. test_bit(first_cpu(current->cpus_allowed),
  1411. qib_cpulist))
  1412. qib_devinfo(dd->pcidev, "%s PID %u affinity "
  1413. "set to cpu %d; already allocated\n",
  1414. current->comm, current->pid,
  1415. first_cpu(current->cpus_allowed));
  1416. }
  1417. mutex_unlock(&qib_mutex);
  1418. done:
  1419. return ret;
  1420. }
  1421. static int qib_do_user_init(struct file *fp,
  1422. const struct qib_user_info *uinfo)
  1423. {
  1424. int ret;
  1425. struct qib_ctxtdata *rcd = ctxt_fp(fp);
  1426. struct qib_devdata *dd;
  1427. unsigned uctxt;
  1428. /* Subctxts don't need to initialize anything since master did it. */
  1429. if (subctxt_fp(fp)) {
  1430. ret = wait_event_interruptible(rcd->wait,
  1431. !test_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag));
  1432. goto bail;
  1433. }
  1434. dd = rcd->dd;
  1435. /* some ctxts may get extra buffers, calculate that here */
  1436. uctxt = rcd->ctxt - dd->first_user_ctxt;
  1437. if (uctxt < dd->ctxts_extrabuf) {
  1438. rcd->piocnt = dd->pbufsctxt + 1;
  1439. rcd->pio_base = rcd->piocnt * uctxt;
  1440. } else {
  1441. rcd->piocnt = dd->pbufsctxt;
  1442. rcd->pio_base = rcd->piocnt * uctxt +
  1443. dd->ctxts_extrabuf;
  1444. }
  1445. /*
  1446. * All user buffers are 2KB buffers. If we ever support
  1447. * giving 4KB buffers to user processes, this will need some
  1448. * work. Can't use piobufbase directly, because it has
  1449. * both 2K and 4K buffer base values. So check and handle.
  1450. */
  1451. if ((rcd->pio_base + rcd->piocnt) > dd->piobcnt2k) {
  1452. if (rcd->pio_base >= dd->piobcnt2k) {
  1453. qib_dev_err(dd,
  1454. "%u:ctxt%u: no 2KB buffers available\n",
  1455. dd->unit, rcd->ctxt);
  1456. ret = -ENOBUFS;
  1457. goto bail;
  1458. }
  1459. rcd->piocnt = dd->piobcnt2k - rcd->pio_base;
  1460. qib_dev_err(dd, "Ctxt%u: would use 4KB bufs, using %u\n",
  1461. rcd->ctxt, rcd->piocnt);
  1462. }
  1463. rcd->piobufs = dd->pio2k_bufbase + rcd->pio_base * dd->palign;
  1464. qib_chg_pioavailkernel(dd, rcd->pio_base, rcd->piocnt,
  1465. TXCHK_CHG_TYPE_USER, rcd);
  1466. /*
  1467. * try to ensure that processes start up with consistent avail update
  1468. * for their own range, at least. If system very quiet, it might
  1469. * have the in-memory copy out of date at startup for this range of
  1470. * buffers, when a context gets re-used. Do after the chg_pioavail
  1471. * and before the rest of setup, so it's "almost certain" the dma
  1472. * will have occurred (can't 100% guarantee, but should be many
  1473. * decimals of 9s, with this ordering), given how much else happens
  1474. * after this.
  1475. */
  1476. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
  1477. /*
  1478. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  1479. * array for time being. If rcd->ctxt > chip-supported,
  1480. * we need to do extra stuff here to handle by handling overflow
  1481. * through ctxt 0, someday
  1482. */
  1483. ret = qib_create_rcvhdrq(dd, rcd);
  1484. if (!ret)
  1485. ret = qib_setup_eagerbufs(rcd);
  1486. if (ret)
  1487. goto bail_pio;
  1488. rcd->tidcursor = 0; /* start at beginning after open */
  1489. /* initialize poll variables... */
  1490. rcd->urgent = 0;
  1491. rcd->urgent_poll = 0;
  1492. /*
  1493. * Now enable the ctxt for receive.
  1494. * For chips that are set to DMA the tail register to memory
  1495. * when they change (and when the update bit transitions from
  1496. * 0 to 1. So for those chips, we turn it off and then back on.
  1497. * This will (very briefly) affect any other open ctxts, but the
  1498. * duration is very short, and therefore isn't an issue. We
  1499. * explictly set the in-memory tail copy to 0 beforehand, so we
  1500. * don't have to wait to be sure the DMA update has happened
  1501. * (chip resets head/tail to 0 on transition to enable).
  1502. */
  1503. if (rcd->rcvhdrtail_kvaddr)
  1504. qib_clear_rcvhdrtail(rcd);
  1505. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_CTXT_ENB | QIB_RCVCTRL_TIDFLOW_ENB,
  1506. rcd->ctxt);
  1507. /* Notify any waiting slaves */
  1508. if (rcd->subctxt_cnt) {
  1509. clear_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag);
  1510. wake_up(&rcd->wait);
  1511. }
  1512. return 0;
  1513. bail_pio:
  1514. qib_chg_pioavailkernel(dd, rcd->pio_base, rcd->piocnt,
  1515. TXCHK_CHG_TYPE_KERN, rcd);
  1516. bail:
  1517. return ret;
  1518. }
  1519. /**
  1520. * unlock_exptid - unlock any expected TID entries context still had in use
  1521. * @rcd: ctxt
  1522. *
  1523. * We don't actually update the chip here, because we do a bulk update
  1524. * below, using f_clear_tids.
  1525. */
  1526. static void unlock_expected_tids(struct qib_ctxtdata *rcd)
  1527. {
  1528. struct qib_devdata *dd = rcd->dd;
  1529. int ctxt_tidbase = rcd->ctxt * dd->rcvtidcnt;
  1530. int i, cnt = 0, maxtid = ctxt_tidbase + dd->rcvtidcnt;
  1531. for (i = ctxt_tidbase; i < maxtid; i++) {
  1532. struct page *p = dd->pageshadow[i];
  1533. dma_addr_t phys;
  1534. if (!p)
  1535. continue;
  1536. phys = dd->physshadow[i];
  1537. dd->physshadow[i] = dd->tidinvalid;
  1538. dd->pageshadow[i] = NULL;
  1539. pci_unmap_page(dd->pcidev, phys, PAGE_SIZE,
  1540. PCI_DMA_FROMDEVICE);
  1541. qib_release_user_pages(&p, 1);
  1542. cnt++;
  1543. }
  1544. }
  1545. static int qib_close(struct inode *in, struct file *fp)
  1546. {
  1547. int ret = 0;
  1548. struct qib_filedata *fd;
  1549. struct qib_ctxtdata *rcd;
  1550. struct qib_devdata *dd;
  1551. unsigned long flags;
  1552. unsigned ctxt;
  1553. pid_t pid;
  1554. mutex_lock(&qib_mutex);
  1555. fd = (struct qib_filedata *) fp->private_data;
  1556. fp->private_data = NULL;
  1557. rcd = fd->rcd;
  1558. if (!rcd) {
  1559. mutex_unlock(&qib_mutex);
  1560. goto bail;
  1561. }
  1562. dd = rcd->dd;
  1563. /* ensure all pio buffer writes in progress are flushed */
  1564. qib_flush_wc();
  1565. /* drain user sdma queue */
  1566. if (fd->pq) {
  1567. qib_user_sdma_queue_drain(rcd->ppd, fd->pq);
  1568. qib_user_sdma_queue_destroy(fd->pq);
  1569. }
  1570. if (fd->rec_cpu_num != -1)
  1571. __clear_bit(fd->rec_cpu_num, qib_cpulist);
  1572. if (--rcd->cnt) {
  1573. /*
  1574. * XXX If the master closes the context before the slave(s),
  1575. * revoke the mmap for the eager receive queue so
  1576. * the slave(s) don't wait for receive data forever.
  1577. */
  1578. rcd->active_slaves &= ~(1 << fd->subctxt);
  1579. rcd->subpid[fd->subctxt] = 0;
  1580. mutex_unlock(&qib_mutex);
  1581. goto bail;
  1582. }
  1583. /* early; no interrupt users after this */
  1584. spin_lock_irqsave(&dd->uctxt_lock, flags);
  1585. ctxt = rcd->ctxt;
  1586. dd->rcd[ctxt] = NULL;
  1587. pid = rcd->pid;
  1588. rcd->pid = 0;
  1589. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1590. if (rcd->rcvwait_to || rcd->piowait_to ||
  1591. rcd->rcvnowait || rcd->pionowait) {
  1592. rcd->rcvwait_to = 0;
  1593. rcd->piowait_to = 0;
  1594. rcd->rcvnowait = 0;
  1595. rcd->pionowait = 0;
  1596. }
  1597. if (rcd->flag)
  1598. rcd->flag = 0;
  1599. if (dd->kregbase) {
  1600. /* atomically clear receive enable ctxt and intr avail. */
  1601. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_CTXT_DIS |
  1602. QIB_RCVCTRL_INTRAVAIL_DIS, ctxt);
  1603. /* clean up the pkeys for this ctxt user */
  1604. qib_clean_part_key(rcd, dd);
  1605. qib_disarm_piobufs(dd, rcd->pio_base, rcd->piocnt);
  1606. qib_chg_pioavailkernel(dd, rcd->pio_base,
  1607. rcd->piocnt, TXCHK_CHG_TYPE_KERN, NULL);
  1608. dd->f_clear_tids(dd, rcd);
  1609. if (dd->pageshadow)
  1610. unlock_expected_tids(rcd);
  1611. qib_stats.sps_ctxts--;
  1612. }
  1613. mutex_unlock(&qib_mutex);
  1614. qib_free_ctxtdata(dd, rcd); /* after releasing the mutex */
  1615. bail:
  1616. kfree(fd);
  1617. return ret;
  1618. }
  1619. static int qib_ctxt_info(struct file *fp, struct qib_ctxt_info __user *uinfo)
  1620. {
  1621. struct qib_ctxt_info info;
  1622. int ret;
  1623. size_t sz;
  1624. struct qib_ctxtdata *rcd = ctxt_fp(fp);
  1625. struct qib_filedata *fd;
  1626. fd = (struct qib_filedata *) fp->private_data;
  1627. info.num_active = qib_count_active_units();
  1628. info.unit = rcd->dd->unit;
  1629. info.port = rcd->ppd->port;
  1630. info.ctxt = rcd->ctxt;
  1631. info.subctxt = subctxt_fp(fp);
  1632. /* Number of user ctxts available for this device. */
  1633. info.num_ctxts = rcd->dd->cfgctxts - rcd->dd->first_user_ctxt;
  1634. info.num_subctxts = rcd->subctxt_cnt;
  1635. info.rec_cpu = fd->rec_cpu_num;
  1636. sz = sizeof(info);
  1637. if (copy_to_user(uinfo, &info, sz)) {
  1638. ret = -EFAULT;
  1639. goto bail;
  1640. }
  1641. ret = 0;
  1642. bail:
  1643. return ret;
  1644. }
  1645. static int qib_sdma_get_inflight(struct qib_user_sdma_queue *pq,
  1646. u32 __user *inflightp)
  1647. {
  1648. const u32 val = qib_user_sdma_inflight_counter(pq);
  1649. if (put_user(val, inflightp))
  1650. return -EFAULT;
  1651. return 0;
  1652. }
  1653. static int qib_sdma_get_complete(struct qib_pportdata *ppd,
  1654. struct qib_user_sdma_queue *pq,
  1655. u32 __user *completep)
  1656. {
  1657. u32 val;
  1658. int err;
  1659. if (!pq)
  1660. return -EINVAL;
  1661. err = qib_user_sdma_make_progress(ppd, pq);
  1662. if (err < 0)
  1663. return err;
  1664. val = qib_user_sdma_complete_counter(pq);
  1665. if (put_user(val, completep))
  1666. return -EFAULT;
  1667. return 0;
  1668. }
  1669. static int disarm_req_delay(struct qib_ctxtdata *rcd)
  1670. {
  1671. int ret = 0;
  1672. if (!usable(rcd->ppd, 1)) {
  1673. int i;
  1674. /*
  1675. * if link is down, or otherwise not usable, delay
  1676. * the caller up to 30 seconds, so we don't thrash
  1677. * in trying to get the chip back to ACTIVE, and
  1678. * set flag so they make the call again.
  1679. */
  1680. if (rcd->user_event_mask) {
  1681. /*
  1682. * subctxt_cnt is 0 if not shared, so do base
  1683. * separately, first, then remaining subctxt, if any
  1684. */
  1685. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  1686. &rcd->user_event_mask[0]);
  1687. for (i = 1; i < rcd->subctxt_cnt; i++)
  1688. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  1689. &rcd->user_event_mask[i]);
  1690. }
  1691. for (i = 0; !usable(rcd->ppd, 1) && i < 300; i++)
  1692. msleep(100);
  1693. ret = -ENETDOWN;
  1694. }
  1695. return ret;
  1696. }
  1697. /*
  1698. * Find all user contexts in use, and set the specified bit in their
  1699. * event mask.
  1700. * See also find_ctxt() for a similar use, that is specific to send buffers.
  1701. */
  1702. int qib_set_uevent_bits(struct qib_pportdata *ppd, const int evtbit)
  1703. {
  1704. struct qib_ctxtdata *rcd;
  1705. unsigned ctxt;
  1706. int ret = 0;
  1707. spin_lock(&ppd->dd->uctxt_lock);
  1708. for (ctxt = ppd->dd->first_user_ctxt; ctxt < ppd->dd->cfgctxts;
  1709. ctxt++) {
  1710. rcd = ppd->dd->rcd[ctxt];
  1711. if (!rcd)
  1712. continue;
  1713. if (rcd->user_event_mask) {
  1714. int i;
  1715. /*
  1716. * subctxt_cnt is 0 if not shared, so do base
  1717. * separately, first, then remaining subctxt, if any
  1718. */
  1719. set_bit(evtbit, &rcd->user_event_mask[0]);
  1720. for (i = 1; i < rcd->subctxt_cnt; i++)
  1721. set_bit(evtbit, &rcd->user_event_mask[i]);
  1722. }
  1723. ret = 1;
  1724. break;
  1725. }
  1726. spin_unlock(&ppd->dd->uctxt_lock);
  1727. return ret;
  1728. }
  1729. /*
  1730. * clear the event notifier events for this context.
  1731. * For the DISARM_BUFS case, we also take action (this obsoletes
  1732. * the older QIB_CMD_DISARM_BUFS, but we keep it for backwards
  1733. * compatibility.
  1734. * Other bits don't currently require actions, just atomically clear.
  1735. * User process then performs actions appropriate to bit having been
  1736. * set, if desired, and checks again in future.
  1737. */
  1738. static int qib_user_event_ack(struct qib_ctxtdata *rcd, int subctxt,
  1739. unsigned long events)
  1740. {
  1741. int ret = 0, i;
  1742. for (i = 0; i <= _QIB_MAX_EVENT_BIT; i++) {
  1743. if (!test_bit(i, &events))
  1744. continue;
  1745. if (i == _QIB_EVENT_DISARM_BUFS_BIT) {
  1746. (void)qib_disarm_piobufs_ifneeded(rcd);
  1747. ret = disarm_req_delay(rcd);
  1748. } else
  1749. clear_bit(i, &rcd->user_event_mask[subctxt]);
  1750. }
  1751. return ret;
  1752. }
  1753. static ssize_t qib_write(struct file *fp, const char __user *data,
  1754. size_t count, loff_t *off)
  1755. {
  1756. const struct qib_cmd __user *ucmd;
  1757. struct qib_ctxtdata *rcd;
  1758. const void __user *src;
  1759. size_t consumed, copy = 0;
  1760. struct qib_cmd cmd;
  1761. ssize_t ret = 0;
  1762. void *dest;
  1763. if (count < sizeof(cmd.type)) {
  1764. ret = -EINVAL;
  1765. goto bail;
  1766. }
  1767. ucmd = (const struct qib_cmd __user *) data;
  1768. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1769. ret = -EFAULT;
  1770. goto bail;
  1771. }
  1772. consumed = sizeof(cmd.type);
  1773. switch (cmd.type) {
  1774. case QIB_CMD_ASSIGN_CTXT:
  1775. case QIB_CMD_USER_INIT:
  1776. copy = sizeof(cmd.cmd.user_info);
  1777. dest = &cmd.cmd.user_info;
  1778. src = &ucmd->cmd.user_info;
  1779. break;
  1780. case QIB_CMD_RECV_CTRL:
  1781. copy = sizeof(cmd.cmd.recv_ctrl);
  1782. dest = &cmd.cmd.recv_ctrl;
  1783. src = &ucmd->cmd.recv_ctrl;
  1784. break;
  1785. case QIB_CMD_CTXT_INFO:
  1786. copy = sizeof(cmd.cmd.ctxt_info);
  1787. dest = &cmd.cmd.ctxt_info;
  1788. src = &ucmd->cmd.ctxt_info;
  1789. break;
  1790. case QIB_CMD_TID_UPDATE:
  1791. case QIB_CMD_TID_FREE:
  1792. copy = sizeof(cmd.cmd.tid_info);
  1793. dest = &cmd.cmd.tid_info;
  1794. src = &ucmd->cmd.tid_info;
  1795. break;
  1796. case QIB_CMD_SET_PART_KEY:
  1797. copy = sizeof(cmd.cmd.part_key);
  1798. dest = &cmd.cmd.part_key;
  1799. src = &ucmd->cmd.part_key;
  1800. break;
  1801. case QIB_CMD_DISARM_BUFS:
  1802. case QIB_CMD_PIOAVAILUPD: /* force an update of PIOAvail reg */
  1803. copy = 0;
  1804. src = NULL;
  1805. dest = NULL;
  1806. break;
  1807. case QIB_CMD_POLL_TYPE:
  1808. copy = sizeof(cmd.cmd.poll_type);
  1809. dest = &cmd.cmd.poll_type;
  1810. src = &ucmd->cmd.poll_type;
  1811. break;
  1812. case QIB_CMD_ARMLAUNCH_CTRL:
  1813. copy = sizeof(cmd.cmd.armlaunch_ctrl);
  1814. dest = &cmd.cmd.armlaunch_ctrl;
  1815. src = &ucmd->cmd.armlaunch_ctrl;
  1816. break;
  1817. case QIB_CMD_SDMA_INFLIGHT:
  1818. copy = sizeof(cmd.cmd.sdma_inflight);
  1819. dest = &cmd.cmd.sdma_inflight;
  1820. src = &ucmd->cmd.sdma_inflight;
  1821. break;
  1822. case QIB_CMD_SDMA_COMPLETE:
  1823. copy = sizeof(cmd.cmd.sdma_complete);
  1824. dest = &cmd.cmd.sdma_complete;
  1825. src = &ucmd->cmd.sdma_complete;
  1826. break;
  1827. case QIB_CMD_ACK_EVENT:
  1828. copy = sizeof(cmd.cmd.event_mask);
  1829. dest = &cmd.cmd.event_mask;
  1830. src = &ucmd->cmd.event_mask;
  1831. break;
  1832. default:
  1833. ret = -EINVAL;
  1834. goto bail;
  1835. }
  1836. if (copy) {
  1837. if ((count - consumed) < copy) {
  1838. ret = -EINVAL;
  1839. goto bail;
  1840. }
  1841. if (copy_from_user(dest, src, copy)) {
  1842. ret = -EFAULT;
  1843. goto bail;
  1844. }
  1845. consumed += copy;
  1846. }
  1847. rcd = ctxt_fp(fp);
  1848. if (!rcd && cmd.type != QIB_CMD_ASSIGN_CTXT) {
  1849. ret = -EINVAL;
  1850. goto bail;
  1851. }
  1852. switch (cmd.type) {
  1853. case QIB_CMD_ASSIGN_CTXT:
  1854. ret = qib_assign_ctxt(fp, &cmd.cmd.user_info);
  1855. if (ret)
  1856. goto bail;
  1857. break;
  1858. case QIB_CMD_USER_INIT:
  1859. ret = qib_do_user_init(fp, &cmd.cmd.user_info);
  1860. if (ret)
  1861. goto bail;
  1862. ret = qib_get_base_info(fp, (void __user *) (unsigned long)
  1863. cmd.cmd.user_info.spu_base_info,
  1864. cmd.cmd.user_info.spu_base_info_size);
  1865. break;
  1866. case QIB_CMD_RECV_CTRL:
  1867. ret = qib_manage_rcvq(rcd, subctxt_fp(fp), cmd.cmd.recv_ctrl);
  1868. break;
  1869. case QIB_CMD_CTXT_INFO:
  1870. ret = qib_ctxt_info(fp, (struct qib_ctxt_info __user *)
  1871. (unsigned long) cmd.cmd.ctxt_info);
  1872. break;
  1873. case QIB_CMD_TID_UPDATE:
  1874. ret = qib_tid_update(rcd, fp, &cmd.cmd.tid_info);
  1875. break;
  1876. case QIB_CMD_TID_FREE:
  1877. ret = qib_tid_free(rcd, subctxt_fp(fp), &cmd.cmd.tid_info);
  1878. break;
  1879. case QIB_CMD_SET_PART_KEY:
  1880. ret = qib_set_part_key(rcd, cmd.cmd.part_key);
  1881. break;
  1882. case QIB_CMD_DISARM_BUFS:
  1883. (void)qib_disarm_piobufs_ifneeded(rcd);
  1884. ret = disarm_req_delay(rcd);
  1885. break;
  1886. case QIB_CMD_PIOAVAILUPD:
  1887. qib_force_pio_avail_update(rcd->dd);
  1888. break;
  1889. case QIB_CMD_POLL_TYPE:
  1890. rcd->poll_type = cmd.cmd.poll_type;
  1891. break;
  1892. case QIB_CMD_ARMLAUNCH_CTRL:
  1893. rcd->dd->f_set_armlaunch(rcd->dd, cmd.cmd.armlaunch_ctrl);
  1894. break;
  1895. case QIB_CMD_SDMA_INFLIGHT:
  1896. ret = qib_sdma_get_inflight(user_sdma_queue_fp(fp),
  1897. (u32 __user *) (unsigned long)
  1898. cmd.cmd.sdma_inflight);
  1899. break;
  1900. case QIB_CMD_SDMA_COMPLETE:
  1901. ret = qib_sdma_get_complete(rcd->ppd,
  1902. user_sdma_queue_fp(fp),
  1903. (u32 __user *) (unsigned long)
  1904. cmd.cmd.sdma_complete);
  1905. break;
  1906. case QIB_CMD_ACK_EVENT:
  1907. ret = qib_user_event_ack(rcd, subctxt_fp(fp),
  1908. cmd.cmd.event_mask);
  1909. break;
  1910. }
  1911. if (ret >= 0)
  1912. ret = consumed;
  1913. bail:
  1914. return ret;
  1915. }
  1916. static ssize_t qib_aio_write(struct kiocb *iocb, const struct iovec *iov,
  1917. unsigned long dim, loff_t off)
  1918. {
  1919. struct qib_filedata *fp = iocb->ki_filp->private_data;
  1920. struct qib_ctxtdata *rcd = ctxt_fp(iocb->ki_filp);
  1921. struct qib_user_sdma_queue *pq = fp->pq;
  1922. if (!dim || !pq)
  1923. return -EINVAL;
  1924. return qib_user_sdma_writev(rcd, pq, iov, dim);
  1925. }
  1926. static struct class *qib_class;
  1927. static dev_t qib_dev;
  1928. int qib_cdev_init(int minor, const char *name,
  1929. const struct file_operations *fops,
  1930. struct cdev **cdevp, struct device **devp)
  1931. {
  1932. const dev_t dev = MKDEV(MAJOR(qib_dev), minor);
  1933. struct cdev *cdev;
  1934. struct device *device = NULL;
  1935. int ret;
  1936. cdev = cdev_alloc();
  1937. if (!cdev) {
  1938. printk(KERN_ERR QIB_DRV_NAME
  1939. ": Could not allocate cdev for minor %d, %s\n",
  1940. minor, name);
  1941. ret = -ENOMEM;
  1942. goto done;
  1943. }
  1944. cdev->owner = THIS_MODULE;
  1945. cdev->ops = fops;
  1946. kobject_set_name(&cdev->kobj, name);
  1947. ret = cdev_add(cdev, dev, 1);
  1948. if (ret < 0) {
  1949. printk(KERN_ERR QIB_DRV_NAME
  1950. ": Could not add cdev for minor %d, %s (err %d)\n",
  1951. minor, name, -ret);
  1952. goto err_cdev;
  1953. }
  1954. device = device_create(qib_class, NULL, dev, NULL, name);
  1955. if (!IS_ERR(device))
  1956. goto done;
  1957. ret = PTR_ERR(device);
  1958. device = NULL;
  1959. printk(KERN_ERR QIB_DRV_NAME ": Could not create "
  1960. "device for minor %d, %s (err %d)\n",
  1961. minor, name, -ret);
  1962. err_cdev:
  1963. cdev_del(cdev);
  1964. cdev = NULL;
  1965. done:
  1966. *cdevp = cdev;
  1967. *devp = device;
  1968. return ret;
  1969. }
  1970. void qib_cdev_cleanup(struct cdev **cdevp, struct device **devp)
  1971. {
  1972. struct device *device = *devp;
  1973. if (device) {
  1974. device_unregister(device);
  1975. *devp = NULL;
  1976. }
  1977. if (*cdevp) {
  1978. cdev_del(*cdevp);
  1979. *cdevp = NULL;
  1980. }
  1981. }
  1982. static struct cdev *wildcard_cdev;
  1983. static struct device *wildcard_device;
  1984. int __init qib_dev_init(void)
  1985. {
  1986. int ret;
  1987. ret = alloc_chrdev_region(&qib_dev, 0, QIB_NMINORS, QIB_DRV_NAME);
  1988. if (ret < 0) {
  1989. printk(KERN_ERR QIB_DRV_NAME ": Could not allocate "
  1990. "chrdev region (err %d)\n", -ret);
  1991. goto done;
  1992. }
  1993. qib_class = class_create(THIS_MODULE, "ipath");
  1994. if (IS_ERR(qib_class)) {
  1995. ret = PTR_ERR(qib_class);
  1996. printk(KERN_ERR QIB_DRV_NAME ": Could not create "
  1997. "device class (err %d)\n", -ret);
  1998. unregister_chrdev_region(qib_dev, QIB_NMINORS);
  1999. }
  2000. done:
  2001. return ret;
  2002. }
  2003. void qib_dev_cleanup(void)
  2004. {
  2005. if (qib_class) {
  2006. class_destroy(qib_class);
  2007. qib_class = NULL;
  2008. }
  2009. unregister_chrdev_region(qib_dev, QIB_NMINORS);
  2010. }
  2011. static atomic_t user_count = ATOMIC_INIT(0);
  2012. static void qib_user_remove(struct qib_devdata *dd)
  2013. {
  2014. if (atomic_dec_return(&user_count) == 0)
  2015. qib_cdev_cleanup(&wildcard_cdev, &wildcard_device);
  2016. qib_cdev_cleanup(&dd->user_cdev, &dd->user_device);
  2017. }
  2018. static int qib_user_add(struct qib_devdata *dd)
  2019. {
  2020. char name[10];
  2021. int ret;
  2022. if (atomic_inc_return(&user_count) == 1) {
  2023. ret = qib_cdev_init(0, "ipath", &qib_file_ops,
  2024. &wildcard_cdev, &wildcard_device);
  2025. if (ret)
  2026. goto done;
  2027. }
  2028. snprintf(name, sizeof(name), "ipath%d", dd->unit);
  2029. ret = qib_cdev_init(dd->unit + 1, name, &qib_file_ops,
  2030. &dd->user_cdev, &dd->user_device);
  2031. if (ret)
  2032. qib_user_remove(dd);
  2033. done:
  2034. return ret;
  2035. }
  2036. /*
  2037. * Create per-unit files in /dev
  2038. */
  2039. int qib_device_create(struct qib_devdata *dd)
  2040. {
  2041. int r, ret;
  2042. r = qib_user_add(dd);
  2043. ret = qib_diag_add(dd);
  2044. if (r && !ret)
  2045. ret = r;
  2046. return ret;
  2047. }
  2048. /*
  2049. * Remove per-unit files in /dev
  2050. * void, core kernel returns no errors for this stuff
  2051. */
  2052. void qib_device_remove(struct qib_devdata *dd)
  2053. {
  2054. qib_user_remove(dd);
  2055. qib_diag_remove(dd);
  2056. }