qib_file_ops.c 61 KB

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