qib_file_ops.c 59 KB

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