qib_file_ops.c 61 KB

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