qib_file_ops.c 59 KB

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