qib_file_ops.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  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. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  767. #endif
  768. /*
  769. * don't allow them to later change to readable with mprotect (for when
  770. * not initially mapped readable, as is normally the case)
  771. */
  772. vma->vm_flags &= ~VM_MAYREAD;
  773. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  774. /* We used PAT if wc_cookie == 0 */
  775. if (!dd->wc_cookie)
  776. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  777. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  778. vma->vm_end - vma->vm_start,
  779. vma->vm_page_prot);
  780. bail:
  781. return ret;
  782. }
  783. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  784. struct qib_ctxtdata *rcd)
  785. {
  786. struct qib_devdata *dd = rcd->dd;
  787. unsigned long start, size;
  788. size_t total_size, i;
  789. unsigned long pfn;
  790. int ret;
  791. size = rcd->rcvegrbuf_size;
  792. total_size = rcd->rcvegrbuf_chunks * size;
  793. if ((vma->vm_end - vma->vm_start) > total_size) {
  794. qib_devinfo(dd->pcidev,
  795. "FAIL on egr bufs: reqlen %lx > actual %lx\n",
  796. vma->vm_end - vma->vm_start,
  797. (unsigned long) total_size);
  798. ret = -EINVAL;
  799. goto bail;
  800. }
  801. if (vma->vm_flags & VM_WRITE) {
  802. qib_devinfo(dd->pcidev,
  803. "Can't map eager buffers as writable (flags=%lx)\n",
  804. vma->vm_flags);
  805. ret = -EPERM;
  806. goto bail;
  807. }
  808. /* don't allow them to later change to writeable with mprotect */
  809. vma->vm_flags &= ~VM_MAYWRITE;
  810. start = vma->vm_start;
  811. for (i = 0; i < rcd->rcvegrbuf_chunks; i++, start += size) {
  812. pfn = virt_to_phys(rcd->rcvegrbuf[i]) >> PAGE_SHIFT;
  813. ret = remap_pfn_range(vma, start, pfn, size,
  814. vma->vm_page_prot);
  815. if (ret < 0)
  816. goto bail;
  817. }
  818. ret = 0;
  819. bail:
  820. return ret;
  821. }
  822. /*
  823. * qib_file_vma_fault - handle a VMA page fault.
  824. */
  825. static int qib_file_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  826. {
  827. struct page *page;
  828. page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
  829. if (!page)
  830. return VM_FAULT_SIGBUS;
  831. get_page(page);
  832. vmf->page = page;
  833. return 0;
  834. }
  835. static const struct vm_operations_struct qib_file_vm_ops = {
  836. .fault = qib_file_vma_fault,
  837. };
  838. static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
  839. struct qib_ctxtdata *rcd, unsigned subctxt)
  840. {
  841. struct qib_devdata *dd = rcd->dd;
  842. unsigned subctxt_cnt;
  843. unsigned long len;
  844. void *addr;
  845. size_t size;
  846. int ret = 0;
  847. subctxt_cnt = rcd->subctxt_cnt;
  848. size = rcd->rcvegrbuf_chunks * rcd->rcvegrbuf_size;
  849. /*
  850. * Each process has all the subctxt uregbase, rcvhdrq, and
  851. * rcvegrbufs mmapped - as an array for all the processes,
  852. * and also separately for this process.
  853. */
  854. if (pgaddr == cvt_kvaddr(rcd->subctxt_uregbase)) {
  855. addr = rcd->subctxt_uregbase;
  856. size = PAGE_SIZE * subctxt_cnt;
  857. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvhdr_base)) {
  858. addr = rcd->subctxt_rcvhdr_base;
  859. size = rcd->rcvhdrq_size * subctxt_cnt;
  860. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvegrbuf)) {
  861. addr = rcd->subctxt_rcvegrbuf;
  862. size *= subctxt_cnt;
  863. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_uregbase +
  864. PAGE_SIZE * subctxt)) {
  865. addr = rcd->subctxt_uregbase + PAGE_SIZE * subctxt;
  866. size = PAGE_SIZE;
  867. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvhdr_base +
  868. rcd->rcvhdrq_size * subctxt)) {
  869. addr = rcd->subctxt_rcvhdr_base +
  870. rcd->rcvhdrq_size * subctxt;
  871. size = rcd->rcvhdrq_size;
  872. } else if (pgaddr == cvt_kvaddr(&rcd->user_event_mask[subctxt])) {
  873. addr = rcd->user_event_mask;
  874. size = PAGE_SIZE;
  875. } else if (pgaddr == cvt_kvaddr(rcd->subctxt_rcvegrbuf +
  876. size * subctxt)) {
  877. addr = rcd->subctxt_rcvegrbuf + size * subctxt;
  878. /* rcvegrbufs are read-only on the slave */
  879. if (vma->vm_flags & VM_WRITE) {
  880. qib_devinfo(dd->pcidev,
  881. "Can't map eager buffers as writable (flags=%lx)\n",
  882. vma->vm_flags);
  883. ret = -EPERM;
  884. goto bail;
  885. }
  886. /*
  887. * Don't allow permission to later change to writeable
  888. * with mprotect.
  889. */
  890. vma->vm_flags &= ~VM_MAYWRITE;
  891. } else
  892. goto bail;
  893. len = vma->vm_end - vma->vm_start;
  894. if (len > size) {
  895. ret = -EINVAL;
  896. goto bail;
  897. }
  898. vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
  899. vma->vm_ops = &qib_file_vm_ops;
  900. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  901. ret = 1;
  902. bail:
  903. return ret;
  904. }
  905. /**
  906. * qib_mmapf - mmap various structures into user space
  907. * @fp: the file pointer
  908. * @vma: the VM area
  909. *
  910. * We use this to have a shared buffer between the kernel and the user code
  911. * for the rcvhdr queue, egr buffers, and the per-context user regs and pio
  912. * buffers in the chip. We have the open and close entries so we can bump
  913. * the ref count and keep the driver from being unloaded while still mapped.
  914. */
  915. static int qib_mmapf(struct file *fp, struct vm_area_struct *vma)
  916. {
  917. struct qib_ctxtdata *rcd;
  918. struct qib_devdata *dd;
  919. u64 pgaddr, ureg;
  920. unsigned piobufs, piocnt;
  921. int ret, match = 1;
  922. rcd = ctxt_fp(fp);
  923. if (!rcd || !(vma->vm_flags & VM_SHARED)) {
  924. ret = -EINVAL;
  925. goto bail;
  926. }
  927. dd = rcd->dd;
  928. /*
  929. * This is the qib_do_user_init() code, mapping the shared buffers
  930. * and per-context user registers into the user process. The address
  931. * referred to by vm_pgoff is the file offset passed via mmap().
  932. * For shared contexts, this is the kernel vmalloc() address of the
  933. * pages to share with the master.
  934. * For non-shared or master ctxts, this is a physical address.
  935. * We only do one mmap for each space mapped.
  936. */
  937. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  938. /*
  939. * Check for 0 in case one of the allocations failed, but user
  940. * called mmap anyway.
  941. */
  942. if (!pgaddr) {
  943. ret = -EINVAL;
  944. goto bail;
  945. }
  946. /*
  947. * Physical addresses must fit in 40 bits for our hardware.
  948. * Check for kernel virtual addresses first, anything else must
  949. * match a HW or memory address.
  950. */
  951. ret = mmap_kvaddr(vma, pgaddr, rcd, subctxt_fp(fp));
  952. if (ret) {
  953. if (ret > 0)
  954. ret = 0;
  955. goto bail;
  956. }
  957. ureg = dd->uregbase + dd->ureg_align * rcd->ctxt;
  958. if (!rcd->subctxt_cnt) {
  959. /* ctxt is not shared */
  960. piocnt = rcd->piocnt;
  961. piobufs = rcd->piobufs;
  962. } else if (!subctxt_fp(fp)) {
  963. /* caller is the master */
  964. piocnt = (rcd->piocnt / rcd->subctxt_cnt) +
  965. (rcd->piocnt % rcd->subctxt_cnt);
  966. piobufs = rcd->piobufs +
  967. dd->palign * (rcd->piocnt - piocnt);
  968. } else {
  969. unsigned slave = subctxt_fp(fp) - 1;
  970. /* caller is a slave */
  971. piocnt = rcd->piocnt / rcd->subctxt_cnt;
  972. piobufs = rcd->piobufs + dd->palign * piocnt * slave;
  973. }
  974. if (pgaddr == ureg)
  975. ret = mmap_ureg(vma, dd, ureg);
  976. else if (pgaddr == piobufs)
  977. ret = mmap_piobufs(vma, dd, rcd, piobufs, piocnt);
  978. else if (pgaddr == dd->pioavailregs_phys)
  979. /* in-memory copy of pioavail registers */
  980. ret = qib_mmap_mem(vma, rcd, PAGE_SIZE,
  981. (void *) dd->pioavailregs_dma, 0,
  982. "pioavail registers");
  983. else if (pgaddr == rcd->rcvegr_phys)
  984. ret = mmap_rcvegrbufs(vma, rcd);
  985. else if (pgaddr == (u64) rcd->rcvhdrq_phys)
  986. /*
  987. * The rcvhdrq itself; multiple pages, contiguous
  988. * from an i/o perspective. Shared contexts need
  989. * to map r/w, so we allow writing.
  990. */
  991. ret = qib_mmap_mem(vma, rcd, rcd->rcvhdrq_size,
  992. rcd->rcvhdrq, 1, "rcvhdrq");
  993. else if (pgaddr == (u64) rcd->rcvhdrqtailaddr_phys)
  994. /* in-memory copy of rcvhdrq tail register */
  995. ret = qib_mmap_mem(vma, rcd, PAGE_SIZE,
  996. rcd->rcvhdrtail_kvaddr, 0,
  997. "rcvhdrq tail");
  998. else
  999. match = 0;
  1000. if (!match)
  1001. ret = -EINVAL;
  1002. vma->vm_private_data = NULL;
  1003. if (ret < 0)
  1004. qib_devinfo(dd->pcidev,
  1005. "mmap Failure %d: off %llx len %lx\n",
  1006. -ret, (unsigned long long)pgaddr,
  1007. vma->vm_end - vma->vm_start);
  1008. bail:
  1009. return ret;
  1010. }
  1011. static unsigned int qib_poll_urgent(struct qib_ctxtdata *rcd,
  1012. struct file *fp,
  1013. struct poll_table_struct *pt)
  1014. {
  1015. struct qib_devdata *dd = rcd->dd;
  1016. unsigned pollflag;
  1017. poll_wait(fp, &rcd->wait, pt);
  1018. spin_lock_irq(&dd->uctxt_lock);
  1019. if (rcd->urgent != rcd->urgent_poll) {
  1020. pollflag = POLLIN | POLLRDNORM;
  1021. rcd->urgent_poll = rcd->urgent;
  1022. } else {
  1023. pollflag = 0;
  1024. set_bit(QIB_CTXT_WAITING_URG, &rcd->flag);
  1025. }
  1026. spin_unlock_irq(&dd->uctxt_lock);
  1027. return pollflag;
  1028. }
  1029. static unsigned int qib_poll_next(struct qib_ctxtdata *rcd,
  1030. struct file *fp,
  1031. struct poll_table_struct *pt)
  1032. {
  1033. struct qib_devdata *dd = rcd->dd;
  1034. unsigned pollflag;
  1035. poll_wait(fp, &rcd->wait, pt);
  1036. spin_lock_irq(&dd->uctxt_lock);
  1037. if (dd->f_hdrqempty(rcd)) {
  1038. set_bit(QIB_CTXT_WAITING_RCV, &rcd->flag);
  1039. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_INTRAVAIL_ENB, rcd->ctxt);
  1040. pollflag = 0;
  1041. } else
  1042. pollflag = POLLIN | POLLRDNORM;
  1043. spin_unlock_irq(&dd->uctxt_lock);
  1044. return pollflag;
  1045. }
  1046. static unsigned int qib_poll(struct file *fp, struct poll_table_struct *pt)
  1047. {
  1048. struct qib_ctxtdata *rcd;
  1049. unsigned pollflag;
  1050. rcd = ctxt_fp(fp);
  1051. if (!rcd)
  1052. pollflag = POLLERR;
  1053. else if (rcd->poll_type == QIB_POLL_TYPE_URGENT)
  1054. pollflag = qib_poll_urgent(rcd, fp, pt);
  1055. else if (rcd->poll_type == QIB_POLL_TYPE_ANYRCV)
  1056. pollflag = qib_poll_next(rcd, fp, pt);
  1057. else /* invalid */
  1058. pollflag = POLLERR;
  1059. return pollflag;
  1060. }
  1061. static void assign_ctxt_affinity(struct file *fp, struct qib_devdata *dd)
  1062. {
  1063. struct qib_filedata *fd = fp->private_data;
  1064. const unsigned int weight = cpumask_weight(&current->cpus_allowed);
  1065. const struct cpumask *local_mask = cpumask_of_pcibus(dd->pcidev->bus);
  1066. int local_cpu;
  1067. /*
  1068. * If process has NOT already set it's affinity, select and
  1069. * reserve a processor for it on the local NUMA node.
  1070. */
  1071. if ((weight >= qib_cpulist_count) &&
  1072. (cpumask_weight(local_mask) <= qib_cpulist_count)) {
  1073. for_each_cpu(local_cpu, local_mask)
  1074. if (!test_and_set_bit(local_cpu, qib_cpulist)) {
  1075. fd->rec_cpu_num = local_cpu;
  1076. return;
  1077. }
  1078. }
  1079. /*
  1080. * If process has NOT already set it's affinity, select and
  1081. * reserve a processor for it, as a rendevous for all
  1082. * users of the driver. If they don't actually later
  1083. * set affinity to this cpu, or set it to some other cpu,
  1084. * it just means that sooner or later we don't recommend
  1085. * a cpu, and let the scheduler do it's best.
  1086. */
  1087. if (weight >= qib_cpulist_count) {
  1088. int cpu;
  1089. cpu = find_first_zero_bit(qib_cpulist,
  1090. qib_cpulist_count);
  1091. if (cpu == qib_cpulist_count)
  1092. qib_dev_err(dd,
  1093. "no cpus avail for affinity PID %u\n",
  1094. current->pid);
  1095. else {
  1096. __set_bit(cpu, qib_cpulist);
  1097. fd->rec_cpu_num = cpu;
  1098. }
  1099. }
  1100. }
  1101. /*
  1102. * Check that userland and driver are compatible for subcontexts.
  1103. */
  1104. static int qib_compatible_subctxts(int user_swmajor, int user_swminor)
  1105. {
  1106. /* this code is written long-hand for clarity */
  1107. if (QIB_USER_SWMAJOR != user_swmajor) {
  1108. /* no promise of compatibility if major mismatch */
  1109. return 0;
  1110. }
  1111. if (QIB_USER_SWMAJOR == 1) {
  1112. switch (QIB_USER_SWMINOR) {
  1113. case 0:
  1114. case 1:
  1115. case 2:
  1116. /* no subctxt implementation so cannot be compatible */
  1117. return 0;
  1118. case 3:
  1119. /* 3 is only compatible with itself */
  1120. return user_swminor == 3;
  1121. default:
  1122. /* >= 4 are compatible (or are expected to be) */
  1123. return user_swminor <= QIB_USER_SWMINOR;
  1124. }
  1125. }
  1126. /* make no promises yet for future major versions */
  1127. return 0;
  1128. }
  1129. static int init_subctxts(struct qib_devdata *dd,
  1130. struct qib_ctxtdata *rcd,
  1131. const struct qib_user_info *uinfo)
  1132. {
  1133. int ret = 0;
  1134. unsigned num_subctxts;
  1135. size_t size;
  1136. /*
  1137. * If the user is requesting zero subctxts,
  1138. * skip the subctxt allocation.
  1139. */
  1140. if (uinfo->spu_subctxt_cnt <= 0)
  1141. goto bail;
  1142. num_subctxts = uinfo->spu_subctxt_cnt;
  1143. /* Check for subctxt compatibility */
  1144. if (!qib_compatible_subctxts(uinfo->spu_userversion >> 16,
  1145. uinfo->spu_userversion & 0xffff)) {
  1146. qib_devinfo(dd->pcidev,
  1147. "Mismatched user version (%d.%d) and driver version (%d.%d) while context sharing. Ensure that driver and library are from the same release.\n",
  1148. (int) (uinfo->spu_userversion >> 16),
  1149. (int) (uinfo->spu_userversion & 0xffff),
  1150. QIB_USER_SWMAJOR, QIB_USER_SWMINOR);
  1151. goto bail;
  1152. }
  1153. if (num_subctxts > QLOGIC_IB_MAX_SUBCTXT) {
  1154. ret = -EINVAL;
  1155. goto bail;
  1156. }
  1157. rcd->subctxt_uregbase = vmalloc_user(PAGE_SIZE * num_subctxts);
  1158. if (!rcd->subctxt_uregbase) {
  1159. ret = -ENOMEM;
  1160. goto bail;
  1161. }
  1162. /* Note: rcd->rcvhdrq_size isn't initialized yet. */
  1163. size = ALIGN(dd->rcvhdrcnt * dd->rcvhdrentsize *
  1164. sizeof(u32), PAGE_SIZE) * num_subctxts;
  1165. rcd->subctxt_rcvhdr_base = vmalloc_user(size);
  1166. if (!rcd->subctxt_rcvhdr_base) {
  1167. ret = -ENOMEM;
  1168. goto bail_ureg;
  1169. }
  1170. rcd->subctxt_rcvegrbuf = vmalloc_user(rcd->rcvegrbuf_chunks *
  1171. rcd->rcvegrbuf_size *
  1172. num_subctxts);
  1173. if (!rcd->subctxt_rcvegrbuf) {
  1174. ret = -ENOMEM;
  1175. goto bail_rhdr;
  1176. }
  1177. rcd->subctxt_cnt = uinfo->spu_subctxt_cnt;
  1178. rcd->subctxt_id = uinfo->spu_subctxt_id;
  1179. rcd->active_slaves = 1;
  1180. rcd->redirect_seq_cnt = 1;
  1181. set_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag);
  1182. goto bail;
  1183. bail_rhdr:
  1184. vfree(rcd->subctxt_rcvhdr_base);
  1185. bail_ureg:
  1186. vfree(rcd->subctxt_uregbase);
  1187. rcd->subctxt_uregbase = NULL;
  1188. bail:
  1189. return ret;
  1190. }
  1191. static int setup_ctxt(struct qib_pportdata *ppd, int ctxt,
  1192. struct file *fp, const struct qib_user_info *uinfo)
  1193. {
  1194. struct qib_filedata *fd = fp->private_data;
  1195. struct qib_devdata *dd = ppd->dd;
  1196. struct qib_ctxtdata *rcd;
  1197. void *ptmp = NULL;
  1198. int ret;
  1199. int numa_id;
  1200. assign_ctxt_affinity(fp, dd);
  1201. numa_id = qib_numa_aware ? ((fd->rec_cpu_num != -1) ?
  1202. cpu_to_node(fd->rec_cpu_num) :
  1203. numa_node_id()) : dd->assigned_node_id;
  1204. rcd = qib_create_ctxtdata(ppd, ctxt, numa_id);
  1205. /*
  1206. * Allocate memory for use in qib_tid_update() at open to
  1207. * reduce cost of expected send setup per message segment
  1208. */
  1209. if (rcd)
  1210. ptmp = kmalloc(dd->rcvtidcnt * sizeof(u16) +
  1211. dd->rcvtidcnt * sizeof(struct page **),
  1212. GFP_KERNEL);
  1213. if (!rcd || !ptmp) {
  1214. qib_dev_err(dd,
  1215. "Unable to allocate ctxtdata memory, failing open\n");
  1216. ret = -ENOMEM;
  1217. goto bailerr;
  1218. }
  1219. rcd->userversion = uinfo->spu_userversion;
  1220. ret = init_subctxts(dd, rcd, uinfo);
  1221. if (ret)
  1222. goto bailerr;
  1223. rcd->tid_pg_list = ptmp;
  1224. rcd->pid = current->pid;
  1225. init_waitqueue_head(&dd->rcd[ctxt]->wait);
  1226. strlcpy(rcd->comm, current->comm, sizeof(rcd->comm));
  1227. ctxt_fp(fp) = rcd;
  1228. qib_stats.sps_ctxts++;
  1229. dd->freectxts--;
  1230. ret = 0;
  1231. goto bail;
  1232. bailerr:
  1233. if (fd->rec_cpu_num != -1)
  1234. __clear_bit(fd->rec_cpu_num, qib_cpulist);
  1235. dd->rcd[ctxt] = NULL;
  1236. kfree(rcd);
  1237. kfree(ptmp);
  1238. bail:
  1239. return ret;
  1240. }
  1241. static inline int usable(struct qib_pportdata *ppd)
  1242. {
  1243. struct qib_devdata *dd = ppd->dd;
  1244. return dd && (dd->flags & QIB_PRESENT) && dd->kregbase && ppd->lid &&
  1245. (ppd->lflags & QIBL_LINKACTIVE);
  1246. }
  1247. /*
  1248. * Select a context on the given device, either using a requested port
  1249. * or the port based on the context number.
  1250. */
  1251. static int choose_port_ctxt(struct file *fp, struct qib_devdata *dd, u32 port,
  1252. const struct qib_user_info *uinfo)
  1253. {
  1254. struct qib_pportdata *ppd = NULL;
  1255. int ret, ctxt;
  1256. if (port) {
  1257. if (!usable(dd->pport + port - 1)) {
  1258. ret = -ENETDOWN;
  1259. goto done;
  1260. } else
  1261. ppd = dd->pport + port - 1;
  1262. }
  1263. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts && dd->rcd[ctxt];
  1264. ctxt++)
  1265. ;
  1266. if (ctxt == dd->cfgctxts) {
  1267. ret = -EBUSY;
  1268. goto done;
  1269. }
  1270. if (!ppd) {
  1271. u32 pidx = ctxt % dd->num_pports;
  1272. if (usable(dd->pport + pidx))
  1273. ppd = dd->pport + pidx;
  1274. else {
  1275. for (pidx = 0; pidx < dd->num_pports && !ppd;
  1276. pidx++)
  1277. if (usable(dd->pport + pidx))
  1278. ppd = dd->pport + pidx;
  1279. }
  1280. }
  1281. ret = ppd ? setup_ctxt(ppd, ctxt, fp, uinfo) : -ENETDOWN;
  1282. done:
  1283. return ret;
  1284. }
  1285. static int find_free_ctxt(int unit, struct file *fp,
  1286. const struct qib_user_info *uinfo)
  1287. {
  1288. struct qib_devdata *dd = qib_lookup(unit);
  1289. int ret;
  1290. if (!dd || (uinfo->spu_port && uinfo->spu_port > dd->num_pports))
  1291. ret = -ENODEV;
  1292. else
  1293. ret = choose_port_ctxt(fp, dd, uinfo->spu_port, uinfo);
  1294. return ret;
  1295. }
  1296. static int get_a_ctxt(struct file *fp, const struct qib_user_info *uinfo,
  1297. unsigned alg)
  1298. {
  1299. struct qib_devdata *udd = NULL;
  1300. int ret = 0, devmax, npresent, nup, ndev, dusable = 0, i;
  1301. u32 port = uinfo->spu_port, ctxt;
  1302. devmax = qib_count_units(&npresent, &nup);
  1303. if (!npresent) {
  1304. ret = -ENXIO;
  1305. goto done;
  1306. }
  1307. if (nup == 0) {
  1308. ret = -ENETDOWN;
  1309. goto done;
  1310. }
  1311. if (alg == QIB_PORT_ALG_ACROSS) {
  1312. unsigned inuse = ~0U;
  1313. /* find device (with ACTIVE ports) with fewest ctxts in use */
  1314. for (ndev = 0; ndev < devmax; ndev++) {
  1315. struct qib_devdata *dd = qib_lookup(ndev);
  1316. unsigned cused = 0, cfree = 0, pusable = 0;
  1317. if (!dd)
  1318. continue;
  1319. if (port && port <= dd->num_pports &&
  1320. usable(dd->pport + port - 1))
  1321. pusable = 1;
  1322. else
  1323. for (i = 0; i < dd->num_pports; i++)
  1324. if (usable(dd->pport + i))
  1325. pusable++;
  1326. if (!pusable)
  1327. continue;
  1328. for (ctxt = dd->first_user_ctxt; ctxt < dd->cfgctxts;
  1329. ctxt++)
  1330. if (dd->rcd[ctxt])
  1331. cused++;
  1332. else
  1333. cfree++;
  1334. if (cfree && cused < inuse) {
  1335. udd = dd;
  1336. inuse = cused;
  1337. }
  1338. }
  1339. if (udd) {
  1340. ret = choose_port_ctxt(fp, udd, port, uinfo);
  1341. goto done;
  1342. }
  1343. } else {
  1344. for (ndev = 0; ndev < devmax; ndev++) {
  1345. struct qib_devdata *dd = qib_lookup(ndev);
  1346. if (dd) {
  1347. ret = choose_port_ctxt(fp, dd, port, uinfo);
  1348. if (!ret)
  1349. goto done;
  1350. if (ret == -EBUSY)
  1351. dusable++;
  1352. }
  1353. }
  1354. }
  1355. ret = dusable ? -EBUSY : -ENETDOWN;
  1356. done:
  1357. return ret;
  1358. }
  1359. static int find_shared_ctxt(struct file *fp,
  1360. const struct qib_user_info *uinfo)
  1361. {
  1362. int devmax, ndev, i;
  1363. int ret = 0;
  1364. devmax = qib_count_units(NULL, NULL);
  1365. for (ndev = 0; ndev < devmax; ndev++) {
  1366. struct qib_devdata *dd = qib_lookup(ndev);
  1367. /* device portion of usable() */
  1368. if (!(dd && (dd->flags & QIB_PRESENT) && dd->kregbase))
  1369. continue;
  1370. for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
  1371. struct qib_ctxtdata *rcd = dd->rcd[i];
  1372. /* Skip ctxts which are not yet open */
  1373. if (!rcd || !rcd->cnt)
  1374. continue;
  1375. /* Skip ctxt if it doesn't match the requested one */
  1376. if (rcd->subctxt_id != uinfo->spu_subctxt_id)
  1377. continue;
  1378. /* Verify the sharing process matches the master */
  1379. if (rcd->subctxt_cnt != uinfo->spu_subctxt_cnt ||
  1380. rcd->userversion != uinfo->spu_userversion ||
  1381. rcd->cnt >= rcd->subctxt_cnt) {
  1382. ret = -EINVAL;
  1383. goto done;
  1384. }
  1385. ctxt_fp(fp) = rcd;
  1386. subctxt_fp(fp) = rcd->cnt++;
  1387. rcd->subpid[subctxt_fp(fp)] = current->pid;
  1388. tidcursor_fp(fp) = 0;
  1389. rcd->active_slaves |= 1 << subctxt_fp(fp);
  1390. ret = 1;
  1391. goto done;
  1392. }
  1393. }
  1394. done:
  1395. return ret;
  1396. }
  1397. static int qib_open(struct inode *in, struct file *fp)
  1398. {
  1399. /* The real work is performed later in qib_assign_ctxt() */
  1400. fp->private_data = kzalloc(sizeof(struct qib_filedata), GFP_KERNEL);
  1401. if (fp->private_data) /* no cpu affinity by default */
  1402. ((struct qib_filedata *)fp->private_data)->rec_cpu_num = -1;
  1403. return fp->private_data ? 0 : -ENOMEM;
  1404. }
  1405. static int find_hca(unsigned int cpu, int *unit)
  1406. {
  1407. int ret = 0, devmax, npresent, nup, ndev;
  1408. *unit = -1;
  1409. devmax = qib_count_units(&npresent, &nup);
  1410. if (!npresent) {
  1411. ret = -ENXIO;
  1412. goto done;
  1413. }
  1414. if (!nup) {
  1415. ret = -ENETDOWN;
  1416. goto done;
  1417. }
  1418. for (ndev = 0; ndev < devmax; ndev++) {
  1419. struct qib_devdata *dd = qib_lookup(ndev);
  1420. if (dd) {
  1421. if (pcibus_to_node(dd->pcidev->bus) < 0) {
  1422. ret = -EINVAL;
  1423. goto done;
  1424. }
  1425. if (cpu_to_node(cpu) ==
  1426. pcibus_to_node(dd->pcidev->bus)) {
  1427. *unit = ndev;
  1428. goto done;
  1429. }
  1430. }
  1431. }
  1432. done:
  1433. return ret;
  1434. }
  1435. static int do_qib_user_sdma_queue_create(struct file *fp)
  1436. {
  1437. struct qib_filedata *fd = fp->private_data;
  1438. struct qib_ctxtdata *rcd = fd->rcd;
  1439. struct qib_devdata *dd = rcd->dd;
  1440. if (dd->flags & QIB_HAS_SEND_DMA) {
  1441. fd->pq = qib_user_sdma_queue_create(&dd->pcidev->dev,
  1442. dd->unit,
  1443. rcd->ctxt,
  1444. fd->subctxt);
  1445. if (!fd->pq)
  1446. return -ENOMEM;
  1447. }
  1448. return 0;
  1449. }
  1450. /*
  1451. * Get ctxt early, so can set affinity prior to memory allocation.
  1452. */
  1453. static int qib_assign_ctxt(struct file *fp, const struct qib_user_info *uinfo)
  1454. {
  1455. int ret;
  1456. int i_minor;
  1457. unsigned swmajor, swminor, alg = QIB_PORT_ALG_ACROSS;
  1458. /* Check to be sure we haven't already initialized this file */
  1459. if (ctxt_fp(fp)) {
  1460. ret = -EINVAL;
  1461. goto done;
  1462. }
  1463. /* for now, if major version is different, bail */
  1464. swmajor = uinfo->spu_userversion >> 16;
  1465. if (swmajor != QIB_USER_SWMAJOR) {
  1466. ret = -ENODEV;
  1467. goto done;
  1468. }
  1469. swminor = uinfo->spu_userversion & 0xffff;
  1470. if (swminor >= 11 && uinfo->spu_port_alg < QIB_PORT_ALG_COUNT)
  1471. alg = uinfo->spu_port_alg;
  1472. mutex_lock(&qib_mutex);
  1473. if (qib_compatible_subctxts(swmajor, swminor) &&
  1474. uinfo->spu_subctxt_cnt) {
  1475. ret = find_shared_ctxt(fp, uinfo);
  1476. if (ret > 0) {
  1477. ret = do_qib_user_sdma_queue_create(fp);
  1478. if (!ret)
  1479. assign_ctxt_affinity(fp, (ctxt_fp(fp))->dd);
  1480. goto done_ok;
  1481. }
  1482. }
  1483. i_minor = iminor(file_inode(fp)) - QIB_USER_MINOR_BASE;
  1484. if (i_minor)
  1485. ret = find_free_ctxt(i_minor - 1, fp, uinfo);
  1486. else {
  1487. int unit;
  1488. const unsigned int cpu = cpumask_first(&current->cpus_allowed);
  1489. const unsigned int weight =
  1490. cpumask_weight(&current->cpus_allowed);
  1491. if (weight == 1 && !test_bit(cpu, qib_cpulist))
  1492. if (!find_hca(cpu, &unit) && unit >= 0)
  1493. if (!find_free_ctxt(unit, fp, uinfo)) {
  1494. ret = 0;
  1495. goto done_chk_sdma;
  1496. }
  1497. ret = get_a_ctxt(fp, uinfo, alg);
  1498. }
  1499. done_chk_sdma:
  1500. if (!ret)
  1501. ret = do_qib_user_sdma_queue_create(fp);
  1502. done_ok:
  1503. mutex_unlock(&qib_mutex);
  1504. done:
  1505. return ret;
  1506. }
  1507. static int qib_do_user_init(struct file *fp,
  1508. const struct qib_user_info *uinfo)
  1509. {
  1510. int ret;
  1511. struct qib_ctxtdata *rcd = ctxt_fp(fp);
  1512. struct qib_devdata *dd;
  1513. unsigned uctxt;
  1514. /* Subctxts don't need to initialize anything since master did it. */
  1515. if (subctxt_fp(fp)) {
  1516. ret = wait_event_interruptible(rcd->wait,
  1517. !test_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag));
  1518. goto bail;
  1519. }
  1520. dd = rcd->dd;
  1521. /* some ctxts may get extra buffers, calculate that here */
  1522. uctxt = rcd->ctxt - dd->first_user_ctxt;
  1523. if (uctxt < dd->ctxts_extrabuf) {
  1524. rcd->piocnt = dd->pbufsctxt + 1;
  1525. rcd->pio_base = rcd->piocnt * uctxt;
  1526. } else {
  1527. rcd->piocnt = dd->pbufsctxt;
  1528. rcd->pio_base = rcd->piocnt * uctxt +
  1529. dd->ctxts_extrabuf;
  1530. }
  1531. /*
  1532. * All user buffers are 2KB buffers. If we ever support
  1533. * giving 4KB buffers to user processes, this will need some
  1534. * work. Can't use piobufbase directly, because it has
  1535. * both 2K and 4K buffer base values. So check and handle.
  1536. */
  1537. if ((rcd->pio_base + rcd->piocnt) > dd->piobcnt2k) {
  1538. if (rcd->pio_base >= dd->piobcnt2k) {
  1539. qib_dev_err(dd,
  1540. "%u:ctxt%u: no 2KB buffers available\n",
  1541. dd->unit, rcd->ctxt);
  1542. ret = -ENOBUFS;
  1543. goto bail;
  1544. }
  1545. rcd->piocnt = dd->piobcnt2k - rcd->pio_base;
  1546. qib_dev_err(dd, "Ctxt%u: would use 4KB bufs, using %u\n",
  1547. rcd->ctxt, rcd->piocnt);
  1548. }
  1549. rcd->piobufs = dd->pio2k_bufbase + rcd->pio_base * dd->palign;
  1550. qib_chg_pioavailkernel(dd, rcd->pio_base, rcd->piocnt,
  1551. TXCHK_CHG_TYPE_USER, rcd);
  1552. /*
  1553. * try to ensure that processes start up with consistent avail update
  1554. * for their own range, at least. If system very quiet, it might
  1555. * have the in-memory copy out of date at startup for this range of
  1556. * buffers, when a context gets re-used. Do after the chg_pioavail
  1557. * and before the rest of setup, so it's "almost certain" the dma
  1558. * will have occurred (can't 100% guarantee, but should be many
  1559. * decimals of 9s, with this ordering), given how much else happens
  1560. * after this.
  1561. */
  1562. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
  1563. /*
  1564. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  1565. * array for time being. If rcd->ctxt > chip-supported,
  1566. * we need to do extra stuff here to handle by handling overflow
  1567. * through ctxt 0, someday
  1568. */
  1569. ret = qib_create_rcvhdrq(dd, rcd);
  1570. if (!ret)
  1571. ret = qib_setup_eagerbufs(rcd);
  1572. if (ret)
  1573. goto bail_pio;
  1574. rcd->tidcursor = 0; /* start at beginning after open */
  1575. /* initialize poll variables... */
  1576. rcd->urgent = 0;
  1577. rcd->urgent_poll = 0;
  1578. /*
  1579. * Now enable the ctxt for receive.
  1580. * For chips that are set to DMA the tail register to memory
  1581. * when they change (and when the update bit transitions from
  1582. * 0 to 1. So for those chips, we turn it off and then back on.
  1583. * This will (very briefly) affect any other open ctxts, but the
  1584. * duration is very short, and therefore isn't an issue. We
  1585. * explicitly set the in-memory tail copy to 0 beforehand, so we
  1586. * don't have to wait to be sure the DMA update has happened
  1587. * (chip resets head/tail to 0 on transition to enable).
  1588. */
  1589. if (rcd->rcvhdrtail_kvaddr)
  1590. qib_clear_rcvhdrtail(rcd);
  1591. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_CTXT_ENB | QIB_RCVCTRL_TIDFLOW_ENB,
  1592. rcd->ctxt);
  1593. /* Notify any waiting slaves */
  1594. if (rcd->subctxt_cnt) {
  1595. clear_bit(QIB_CTXT_MASTER_UNINIT, &rcd->flag);
  1596. wake_up(&rcd->wait);
  1597. }
  1598. return 0;
  1599. bail_pio:
  1600. qib_chg_pioavailkernel(dd, rcd->pio_base, rcd->piocnt,
  1601. TXCHK_CHG_TYPE_KERN, rcd);
  1602. bail:
  1603. return ret;
  1604. }
  1605. /**
  1606. * unlock_exptid - unlock any expected TID entries context still had in use
  1607. * @rcd: ctxt
  1608. *
  1609. * We don't actually update the chip here, because we do a bulk update
  1610. * below, using f_clear_tids.
  1611. */
  1612. static void unlock_expected_tids(struct qib_ctxtdata *rcd)
  1613. {
  1614. struct qib_devdata *dd = rcd->dd;
  1615. int ctxt_tidbase = rcd->ctxt * dd->rcvtidcnt;
  1616. int i, cnt = 0, maxtid = ctxt_tidbase + dd->rcvtidcnt;
  1617. for (i = ctxt_tidbase; i < maxtid; i++) {
  1618. struct page *p = dd->pageshadow[i];
  1619. dma_addr_t phys;
  1620. if (!p)
  1621. continue;
  1622. phys = dd->physshadow[i];
  1623. dd->physshadow[i] = dd->tidinvalid;
  1624. dd->pageshadow[i] = NULL;
  1625. pci_unmap_page(dd->pcidev, phys, PAGE_SIZE,
  1626. PCI_DMA_FROMDEVICE);
  1627. qib_release_user_pages(&p, 1);
  1628. cnt++;
  1629. }
  1630. }
  1631. static int qib_close(struct inode *in, struct file *fp)
  1632. {
  1633. int ret = 0;
  1634. struct qib_filedata *fd;
  1635. struct qib_ctxtdata *rcd;
  1636. struct qib_devdata *dd;
  1637. unsigned long flags;
  1638. unsigned ctxt;
  1639. pid_t pid;
  1640. mutex_lock(&qib_mutex);
  1641. fd = fp->private_data;
  1642. fp->private_data = NULL;
  1643. rcd = fd->rcd;
  1644. if (!rcd) {
  1645. mutex_unlock(&qib_mutex);
  1646. goto bail;
  1647. }
  1648. dd = rcd->dd;
  1649. /* ensure all pio buffer writes in progress are flushed */
  1650. qib_flush_wc();
  1651. /* drain user sdma queue */
  1652. if (fd->pq) {
  1653. qib_user_sdma_queue_drain(rcd->ppd, fd->pq);
  1654. qib_user_sdma_queue_destroy(fd->pq);
  1655. }
  1656. if (fd->rec_cpu_num != -1)
  1657. __clear_bit(fd->rec_cpu_num, qib_cpulist);
  1658. if (--rcd->cnt) {
  1659. /*
  1660. * XXX If the master closes the context before the slave(s),
  1661. * revoke the mmap for the eager receive queue so
  1662. * the slave(s) don't wait for receive data forever.
  1663. */
  1664. rcd->active_slaves &= ~(1 << fd->subctxt);
  1665. rcd->subpid[fd->subctxt] = 0;
  1666. mutex_unlock(&qib_mutex);
  1667. goto bail;
  1668. }
  1669. /* early; no interrupt users after this */
  1670. spin_lock_irqsave(&dd->uctxt_lock, flags);
  1671. ctxt = rcd->ctxt;
  1672. dd->rcd[ctxt] = NULL;
  1673. pid = rcd->pid;
  1674. rcd->pid = 0;
  1675. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1676. if (rcd->rcvwait_to || rcd->piowait_to ||
  1677. rcd->rcvnowait || rcd->pionowait) {
  1678. rcd->rcvwait_to = 0;
  1679. rcd->piowait_to = 0;
  1680. rcd->rcvnowait = 0;
  1681. rcd->pionowait = 0;
  1682. }
  1683. if (rcd->flag)
  1684. rcd->flag = 0;
  1685. if (dd->kregbase) {
  1686. /* atomically clear receive enable ctxt and intr avail. */
  1687. dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_CTXT_DIS |
  1688. QIB_RCVCTRL_INTRAVAIL_DIS, ctxt);
  1689. /* clean up the pkeys for this ctxt user */
  1690. qib_clean_part_key(rcd, dd);
  1691. qib_disarm_piobufs(dd, rcd->pio_base, rcd->piocnt);
  1692. qib_chg_pioavailkernel(dd, rcd->pio_base,
  1693. rcd->piocnt, TXCHK_CHG_TYPE_KERN, NULL);
  1694. dd->f_clear_tids(dd, rcd);
  1695. if (dd->pageshadow)
  1696. unlock_expected_tids(rcd);
  1697. qib_stats.sps_ctxts--;
  1698. dd->freectxts++;
  1699. }
  1700. mutex_unlock(&qib_mutex);
  1701. qib_free_ctxtdata(dd, rcd); /* after releasing the mutex */
  1702. bail:
  1703. kfree(fd);
  1704. return ret;
  1705. }
  1706. static int qib_ctxt_info(struct file *fp, struct qib_ctxt_info __user *uinfo)
  1707. {
  1708. struct qib_ctxt_info info;
  1709. int ret;
  1710. size_t sz;
  1711. struct qib_ctxtdata *rcd = ctxt_fp(fp);
  1712. struct qib_filedata *fd;
  1713. fd = fp->private_data;
  1714. info.num_active = qib_count_active_units();
  1715. info.unit = rcd->dd->unit;
  1716. info.port = rcd->ppd->port;
  1717. info.ctxt = rcd->ctxt;
  1718. info.subctxt = subctxt_fp(fp);
  1719. /* Number of user ctxts available for this device. */
  1720. info.num_ctxts = rcd->dd->cfgctxts - rcd->dd->first_user_ctxt;
  1721. info.num_subctxts = rcd->subctxt_cnt;
  1722. info.rec_cpu = fd->rec_cpu_num;
  1723. sz = sizeof(info);
  1724. if (copy_to_user(uinfo, &info, sz)) {
  1725. ret = -EFAULT;
  1726. goto bail;
  1727. }
  1728. ret = 0;
  1729. bail:
  1730. return ret;
  1731. }
  1732. static int qib_sdma_get_inflight(struct qib_user_sdma_queue *pq,
  1733. u32 __user *inflightp)
  1734. {
  1735. const u32 val = qib_user_sdma_inflight_counter(pq);
  1736. if (put_user(val, inflightp))
  1737. return -EFAULT;
  1738. return 0;
  1739. }
  1740. static int qib_sdma_get_complete(struct qib_pportdata *ppd,
  1741. struct qib_user_sdma_queue *pq,
  1742. u32 __user *completep)
  1743. {
  1744. u32 val;
  1745. int err;
  1746. if (!pq)
  1747. return -EINVAL;
  1748. err = qib_user_sdma_make_progress(ppd, pq);
  1749. if (err < 0)
  1750. return err;
  1751. val = qib_user_sdma_complete_counter(pq);
  1752. if (put_user(val, completep))
  1753. return -EFAULT;
  1754. return 0;
  1755. }
  1756. static int disarm_req_delay(struct qib_ctxtdata *rcd)
  1757. {
  1758. int ret = 0;
  1759. if (!usable(rcd->ppd)) {
  1760. int i;
  1761. /*
  1762. * if link is down, or otherwise not usable, delay
  1763. * the caller up to 30 seconds, so we don't thrash
  1764. * in trying to get the chip back to ACTIVE, and
  1765. * set flag so they make the call again.
  1766. */
  1767. if (rcd->user_event_mask) {
  1768. /*
  1769. * subctxt_cnt is 0 if not shared, so do base
  1770. * separately, first, then remaining subctxt, if any
  1771. */
  1772. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  1773. &rcd->user_event_mask[0]);
  1774. for (i = 1; i < rcd->subctxt_cnt; i++)
  1775. set_bit(_QIB_EVENT_DISARM_BUFS_BIT,
  1776. &rcd->user_event_mask[i]);
  1777. }
  1778. for (i = 0; !usable(rcd->ppd) && i < 300; i++)
  1779. msleep(100);
  1780. ret = -ENETDOWN;
  1781. }
  1782. return ret;
  1783. }
  1784. /*
  1785. * Find all user contexts in use, and set the specified bit in their
  1786. * event mask.
  1787. * See also find_ctxt() for a similar use, that is specific to send buffers.
  1788. */
  1789. int qib_set_uevent_bits(struct qib_pportdata *ppd, const int evtbit)
  1790. {
  1791. struct qib_ctxtdata *rcd;
  1792. unsigned ctxt;
  1793. int ret = 0;
  1794. unsigned long flags;
  1795. spin_lock_irqsave(&ppd->dd->uctxt_lock, flags);
  1796. for (ctxt = ppd->dd->first_user_ctxt; ctxt < ppd->dd->cfgctxts;
  1797. ctxt++) {
  1798. rcd = ppd->dd->rcd[ctxt];
  1799. if (!rcd)
  1800. continue;
  1801. if (rcd->user_event_mask) {
  1802. int i;
  1803. /*
  1804. * subctxt_cnt is 0 if not shared, so do base
  1805. * separately, first, then remaining subctxt, if any
  1806. */
  1807. set_bit(evtbit, &rcd->user_event_mask[0]);
  1808. for (i = 1; i < rcd->subctxt_cnt; i++)
  1809. set_bit(evtbit, &rcd->user_event_mask[i]);
  1810. }
  1811. ret = 1;
  1812. break;
  1813. }
  1814. spin_unlock_irqrestore(&ppd->dd->uctxt_lock, flags);
  1815. return ret;
  1816. }
  1817. /*
  1818. * clear the event notifier events for this context.
  1819. * For the DISARM_BUFS case, we also take action (this obsoletes
  1820. * the older QIB_CMD_DISARM_BUFS, but we keep it for backwards
  1821. * compatibility.
  1822. * Other bits don't currently require actions, just atomically clear.
  1823. * User process then performs actions appropriate to bit having been
  1824. * set, if desired, and checks again in future.
  1825. */
  1826. static int qib_user_event_ack(struct qib_ctxtdata *rcd, int subctxt,
  1827. unsigned long events)
  1828. {
  1829. int ret = 0, i;
  1830. for (i = 0; i <= _QIB_MAX_EVENT_BIT; i++) {
  1831. if (!test_bit(i, &events))
  1832. continue;
  1833. if (i == _QIB_EVENT_DISARM_BUFS_BIT) {
  1834. (void)qib_disarm_piobufs_ifneeded(rcd);
  1835. ret = disarm_req_delay(rcd);
  1836. } else
  1837. clear_bit(i, &rcd->user_event_mask[subctxt]);
  1838. }
  1839. return ret;
  1840. }
  1841. static ssize_t qib_write(struct file *fp, const char __user *data,
  1842. size_t count, loff_t *off)
  1843. {
  1844. const struct qib_cmd __user *ucmd;
  1845. struct qib_ctxtdata *rcd;
  1846. const void __user *src;
  1847. size_t consumed, copy = 0;
  1848. struct qib_cmd cmd;
  1849. ssize_t ret = 0;
  1850. void *dest;
  1851. if (count < sizeof(cmd.type)) {
  1852. ret = -EINVAL;
  1853. goto bail;
  1854. }
  1855. ucmd = (const struct qib_cmd __user *) data;
  1856. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1857. ret = -EFAULT;
  1858. goto bail;
  1859. }
  1860. consumed = sizeof(cmd.type);
  1861. switch (cmd.type) {
  1862. case QIB_CMD_ASSIGN_CTXT:
  1863. case QIB_CMD_USER_INIT:
  1864. copy = sizeof(cmd.cmd.user_info);
  1865. dest = &cmd.cmd.user_info;
  1866. src = &ucmd->cmd.user_info;
  1867. break;
  1868. case QIB_CMD_RECV_CTRL:
  1869. copy = sizeof(cmd.cmd.recv_ctrl);
  1870. dest = &cmd.cmd.recv_ctrl;
  1871. src = &ucmd->cmd.recv_ctrl;
  1872. break;
  1873. case QIB_CMD_CTXT_INFO:
  1874. copy = sizeof(cmd.cmd.ctxt_info);
  1875. dest = &cmd.cmd.ctxt_info;
  1876. src = &ucmd->cmd.ctxt_info;
  1877. break;
  1878. case QIB_CMD_TID_UPDATE:
  1879. case QIB_CMD_TID_FREE:
  1880. copy = sizeof(cmd.cmd.tid_info);
  1881. dest = &cmd.cmd.tid_info;
  1882. src = &ucmd->cmd.tid_info;
  1883. break;
  1884. case QIB_CMD_SET_PART_KEY:
  1885. copy = sizeof(cmd.cmd.part_key);
  1886. dest = &cmd.cmd.part_key;
  1887. src = &ucmd->cmd.part_key;
  1888. break;
  1889. case QIB_CMD_DISARM_BUFS:
  1890. case QIB_CMD_PIOAVAILUPD: /* force an update of PIOAvail reg */
  1891. copy = 0;
  1892. src = NULL;
  1893. dest = NULL;
  1894. break;
  1895. case QIB_CMD_POLL_TYPE:
  1896. copy = sizeof(cmd.cmd.poll_type);
  1897. dest = &cmd.cmd.poll_type;
  1898. src = &ucmd->cmd.poll_type;
  1899. break;
  1900. case QIB_CMD_ARMLAUNCH_CTRL:
  1901. copy = sizeof(cmd.cmd.armlaunch_ctrl);
  1902. dest = &cmd.cmd.armlaunch_ctrl;
  1903. src = &ucmd->cmd.armlaunch_ctrl;
  1904. break;
  1905. case QIB_CMD_SDMA_INFLIGHT:
  1906. copy = sizeof(cmd.cmd.sdma_inflight);
  1907. dest = &cmd.cmd.sdma_inflight;
  1908. src = &ucmd->cmd.sdma_inflight;
  1909. break;
  1910. case QIB_CMD_SDMA_COMPLETE:
  1911. copy = sizeof(cmd.cmd.sdma_complete);
  1912. dest = &cmd.cmd.sdma_complete;
  1913. src = &ucmd->cmd.sdma_complete;
  1914. break;
  1915. case QIB_CMD_ACK_EVENT:
  1916. copy = sizeof(cmd.cmd.event_mask);
  1917. dest = &cmd.cmd.event_mask;
  1918. src = &ucmd->cmd.event_mask;
  1919. break;
  1920. default:
  1921. ret = -EINVAL;
  1922. goto bail;
  1923. }
  1924. if (copy) {
  1925. if ((count - consumed) < copy) {
  1926. ret = -EINVAL;
  1927. goto bail;
  1928. }
  1929. if (copy_from_user(dest, src, copy)) {
  1930. ret = -EFAULT;
  1931. goto bail;
  1932. }
  1933. consumed += copy;
  1934. }
  1935. rcd = ctxt_fp(fp);
  1936. if (!rcd && cmd.type != QIB_CMD_ASSIGN_CTXT) {
  1937. ret = -EINVAL;
  1938. goto bail;
  1939. }
  1940. switch (cmd.type) {
  1941. case QIB_CMD_ASSIGN_CTXT:
  1942. ret = qib_assign_ctxt(fp, &cmd.cmd.user_info);
  1943. if (ret)
  1944. goto bail;
  1945. break;
  1946. case QIB_CMD_USER_INIT:
  1947. ret = qib_do_user_init(fp, &cmd.cmd.user_info);
  1948. if (ret)
  1949. goto bail;
  1950. ret = qib_get_base_info(fp, (void __user *) (unsigned long)
  1951. cmd.cmd.user_info.spu_base_info,
  1952. cmd.cmd.user_info.spu_base_info_size);
  1953. break;
  1954. case QIB_CMD_RECV_CTRL:
  1955. ret = qib_manage_rcvq(rcd, subctxt_fp(fp), cmd.cmd.recv_ctrl);
  1956. break;
  1957. case QIB_CMD_CTXT_INFO:
  1958. ret = qib_ctxt_info(fp, (struct qib_ctxt_info __user *)
  1959. (unsigned long) cmd.cmd.ctxt_info);
  1960. break;
  1961. case QIB_CMD_TID_UPDATE:
  1962. ret = qib_tid_update(rcd, fp, &cmd.cmd.tid_info);
  1963. break;
  1964. case QIB_CMD_TID_FREE:
  1965. ret = qib_tid_free(rcd, subctxt_fp(fp), &cmd.cmd.tid_info);
  1966. break;
  1967. case QIB_CMD_SET_PART_KEY:
  1968. ret = qib_set_part_key(rcd, cmd.cmd.part_key);
  1969. break;
  1970. case QIB_CMD_DISARM_BUFS:
  1971. (void)qib_disarm_piobufs_ifneeded(rcd);
  1972. ret = disarm_req_delay(rcd);
  1973. break;
  1974. case QIB_CMD_PIOAVAILUPD:
  1975. qib_force_pio_avail_update(rcd->dd);
  1976. break;
  1977. case QIB_CMD_POLL_TYPE:
  1978. rcd->poll_type = cmd.cmd.poll_type;
  1979. break;
  1980. case QIB_CMD_ARMLAUNCH_CTRL:
  1981. rcd->dd->f_set_armlaunch(rcd->dd, cmd.cmd.armlaunch_ctrl);
  1982. break;
  1983. case QIB_CMD_SDMA_INFLIGHT:
  1984. ret = qib_sdma_get_inflight(user_sdma_queue_fp(fp),
  1985. (u32 __user *) (unsigned long)
  1986. cmd.cmd.sdma_inflight);
  1987. break;
  1988. case QIB_CMD_SDMA_COMPLETE:
  1989. ret = qib_sdma_get_complete(rcd->ppd,
  1990. user_sdma_queue_fp(fp),
  1991. (u32 __user *) (unsigned long)
  1992. cmd.cmd.sdma_complete);
  1993. break;
  1994. case QIB_CMD_ACK_EVENT:
  1995. ret = qib_user_event_ack(rcd, subctxt_fp(fp),
  1996. cmd.cmd.event_mask);
  1997. break;
  1998. }
  1999. if (ret >= 0)
  2000. ret = consumed;
  2001. bail:
  2002. return ret;
  2003. }
  2004. static ssize_t qib_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2005. {
  2006. struct qib_filedata *fp = iocb->ki_filp->private_data;
  2007. struct qib_ctxtdata *rcd = ctxt_fp(iocb->ki_filp);
  2008. struct qib_user_sdma_queue *pq = fp->pq;
  2009. if (!iter_is_iovec(from) || !from->nr_segs || !pq)
  2010. return -EINVAL;
  2011. return qib_user_sdma_writev(rcd, pq, from->iov, from->nr_segs);
  2012. }
  2013. static struct class *qib_class;
  2014. static dev_t qib_dev;
  2015. int qib_cdev_init(int minor, const char *name,
  2016. const struct file_operations *fops,
  2017. struct cdev **cdevp, struct device **devp)
  2018. {
  2019. const dev_t dev = MKDEV(MAJOR(qib_dev), minor);
  2020. struct cdev *cdev;
  2021. struct device *device = NULL;
  2022. int ret;
  2023. cdev = cdev_alloc();
  2024. if (!cdev) {
  2025. pr_err("Could not allocate cdev for minor %d, %s\n",
  2026. minor, name);
  2027. ret = -ENOMEM;
  2028. goto done;
  2029. }
  2030. cdev->owner = THIS_MODULE;
  2031. cdev->ops = fops;
  2032. kobject_set_name(&cdev->kobj, name);
  2033. ret = cdev_add(cdev, dev, 1);
  2034. if (ret < 0) {
  2035. pr_err("Could not add cdev for minor %d, %s (err %d)\n",
  2036. minor, name, -ret);
  2037. goto err_cdev;
  2038. }
  2039. device = device_create(qib_class, NULL, dev, NULL, "%s", name);
  2040. if (!IS_ERR(device))
  2041. goto done;
  2042. ret = PTR_ERR(device);
  2043. device = NULL;
  2044. pr_err("Could not create device for minor %d, %s (err %d)\n",
  2045. minor, name, -ret);
  2046. err_cdev:
  2047. cdev_del(cdev);
  2048. cdev = NULL;
  2049. done:
  2050. *cdevp = cdev;
  2051. *devp = device;
  2052. return ret;
  2053. }
  2054. void qib_cdev_cleanup(struct cdev **cdevp, struct device **devp)
  2055. {
  2056. struct device *device = *devp;
  2057. if (device) {
  2058. device_unregister(device);
  2059. *devp = NULL;
  2060. }
  2061. if (*cdevp) {
  2062. cdev_del(*cdevp);
  2063. *cdevp = NULL;
  2064. }
  2065. }
  2066. static struct cdev *wildcard_cdev;
  2067. static struct device *wildcard_device;
  2068. int __init qib_dev_init(void)
  2069. {
  2070. int ret;
  2071. ret = alloc_chrdev_region(&qib_dev, 0, QIB_NMINORS, QIB_DRV_NAME);
  2072. if (ret < 0) {
  2073. pr_err("Could not allocate chrdev region (err %d)\n", -ret);
  2074. goto done;
  2075. }
  2076. qib_class = class_create(THIS_MODULE, "ipath");
  2077. if (IS_ERR(qib_class)) {
  2078. ret = PTR_ERR(qib_class);
  2079. pr_err("Could not create device class (err %d)\n", -ret);
  2080. unregister_chrdev_region(qib_dev, QIB_NMINORS);
  2081. }
  2082. done:
  2083. return ret;
  2084. }
  2085. void qib_dev_cleanup(void)
  2086. {
  2087. if (qib_class) {
  2088. class_destroy(qib_class);
  2089. qib_class = NULL;
  2090. }
  2091. unregister_chrdev_region(qib_dev, QIB_NMINORS);
  2092. }
  2093. static atomic_t user_count = ATOMIC_INIT(0);
  2094. static void qib_user_remove(struct qib_devdata *dd)
  2095. {
  2096. if (atomic_dec_return(&user_count) == 0)
  2097. qib_cdev_cleanup(&wildcard_cdev, &wildcard_device);
  2098. qib_cdev_cleanup(&dd->user_cdev, &dd->user_device);
  2099. }
  2100. static int qib_user_add(struct qib_devdata *dd)
  2101. {
  2102. char name[10];
  2103. int ret;
  2104. if (atomic_inc_return(&user_count) == 1) {
  2105. ret = qib_cdev_init(0, "ipath", &qib_file_ops,
  2106. &wildcard_cdev, &wildcard_device);
  2107. if (ret)
  2108. goto done;
  2109. }
  2110. snprintf(name, sizeof(name), "ipath%d", dd->unit);
  2111. ret = qib_cdev_init(dd->unit + 1, name, &qib_file_ops,
  2112. &dd->user_cdev, &dd->user_device);
  2113. if (ret)
  2114. qib_user_remove(dd);
  2115. done:
  2116. return ret;
  2117. }
  2118. /*
  2119. * Create per-unit files in /dev
  2120. */
  2121. int qib_device_create(struct qib_devdata *dd)
  2122. {
  2123. int r, ret;
  2124. r = qib_user_add(dd);
  2125. ret = qib_diag_add(dd);
  2126. if (r && !ret)
  2127. ret = r;
  2128. return ret;
  2129. }
  2130. /*
  2131. * Remove per-unit files in /dev
  2132. * void, core kernel returns no errors for this stuff
  2133. */
  2134. void qib_device_remove(struct qib_devdata *dd)
  2135. {
  2136. qib_user_remove(dd);
  2137. qib_diag_remove(dd);
  2138. }