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