qib_file_ops.c 61 KB

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