qib_file_ops.c 59 KB

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