qib_file_ops.c 61 KB

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