ipath_file_ops.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/pci.h>
  34. #include <linux/poll.h>
  35. #include <linux/cdev.h>
  36. #include <linux/swap.h>
  37. #include <linux/vmalloc.h>
  38. #include <asm/pgtable.h>
  39. #include "ipath_kernel.h"
  40. #include "ipath_common.h"
  41. /*
  42. * mmap64 doesn't allow all 64 bits for 32-bit applications
  43. * so only use the low 43 bits.
  44. */
  45. #define MMAP64_MASK 0x7FFFFFFFFFFUL
  46. static int ipath_open(struct inode *, struct file *);
  47. static int ipath_close(struct inode *, struct file *);
  48. static ssize_t ipath_write(struct file *, const char __user *, size_t,
  49. loff_t *);
  50. static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
  51. static int ipath_mmap(struct file *, struct vm_area_struct *);
  52. static struct file_operations ipath_file_ops = {
  53. .owner = THIS_MODULE,
  54. .write = ipath_write,
  55. .open = ipath_open,
  56. .release = ipath_close,
  57. .poll = ipath_poll,
  58. .mmap = ipath_mmap
  59. };
  60. static int ipath_get_base_info(struct file *fp,
  61. void __user *ubase, size_t ubase_size)
  62. {
  63. struct ipath_portdata *pd = port_fp(fp);
  64. int ret = 0;
  65. struct ipath_base_info *kinfo = NULL;
  66. struct ipath_devdata *dd = pd->port_dd;
  67. unsigned subport_cnt;
  68. int shared, master;
  69. size_t sz;
  70. subport_cnt = pd->port_subport_cnt;
  71. if (!subport_cnt) {
  72. shared = 0;
  73. master = 0;
  74. subport_cnt = 1;
  75. } else {
  76. shared = 1;
  77. master = !subport_fp(fp);
  78. }
  79. sz = sizeof(*kinfo);
  80. /* If port sharing is not requested, allow the old size structure */
  81. if (!shared)
  82. sz -= 3 * sizeof(u64);
  83. if (ubase_size < sz) {
  84. ipath_cdbg(PROC,
  85. "Base size %zu, need %zu (version mismatch?)\n",
  86. ubase_size, sz);
  87. ret = -EINVAL;
  88. goto bail;
  89. }
  90. kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
  91. if (kinfo == NULL) {
  92. ret = -ENOMEM;
  93. goto bail;
  94. }
  95. ret = dd->ipath_f_get_base_info(pd, kinfo);
  96. if (ret < 0)
  97. goto bail;
  98. kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
  99. kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
  100. kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
  101. kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
  102. /*
  103. * have to mmap whole thing
  104. */
  105. kinfo->spi_rcv_egrbuftotlen =
  106. pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  107. kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
  108. kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
  109. pd->port_rcvegrbuf_chunks;
  110. kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
  111. if (master)
  112. kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
  113. /*
  114. * for this use, may be ipath_cfgports summed over all chips that
  115. * are are configured and present
  116. */
  117. kinfo->spi_nports = dd->ipath_cfgports;
  118. /* unit (chip/board) our port is on */
  119. kinfo->spi_unit = dd->ipath_unit;
  120. /* for now, only a single page */
  121. kinfo->spi_tid_maxsize = PAGE_SIZE;
  122. /*
  123. * Doing this per port, and based on the skip value, etc. This has
  124. * to be the actual buffer size, since the protocol code treats it
  125. * as an array.
  126. *
  127. * These have to be set to user addresses in the user code via mmap.
  128. * These values are used on return to user code for the mmap target
  129. * addresses only. For 32 bit, same 44 bit address problem, so use
  130. * the physical address, not virtual. Before 2.6.11, using the
  131. * page_address() macro worked, but in 2.6.11, even that returns the
  132. * full 64 bit address (upper bits all 1's). So far, using the
  133. * physical addresses (or chip offsets, for chip mapping) works, but
  134. * no doubt some future kernel release will change that, and we'll be
  135. * on to yet another method of dealing with this.
  136. */
  137. kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
  138. kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
  139. kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
  140. kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
  141. kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
  142. (void *) dd->ipath_statusp -
  143. (void *) dd->ipath_pioavailregs_dma;
  144. if (!shared) {
  145. kinfo->spi_piocnt = dd->ipath_pbufsport;
  146. kinfo->spi_piobufbase = (u64) pd->port_piobufs;
  147. kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
  148. dd->ipath_palign * pd->port_port;
  149. } else if (master) {
  150. kinfo->spi_piocnt = (dd->ipath_pbufsport / subport_cnt) +
  151. (dd->ipath_pbufsport % subport_cnt);
  152. /* Master's PIO buffers are after all the slave's */
  153. kinfo->spi_piobufbase = (u64) pd->port_piobufs +
  154. dd->ipath_palign *
  155. (dd->ipath_pbufsport - kinfo->spi_piocnt);
  156. kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
  157. dd->ipath_palign * pd->port_port;
  158. } else {
  159. unsigned slave = subport_fp(fp) - 1;
  160. kinfo->spi_piocnt = dd->ipath_pbufsport / subport_cnt;
  161. kinfo->spi_piobufbase = (u64) pd->port_piobufs +
  162. dd->ipath_palign * kinfo->spi_piocnt * slave;
  163. kinfo->__spi_uregbase = ((u64) pd->subport_uregbase +
  164. PAGE_SIZE * slave) & MMAP64_MASK;
  165. kinfo->spi_rcvhdr_base = ((u64) pd->subport_rcvhdr_base +
  166. pd->port_rcvhdrq_size * slave) & MMAP64_MASK;
  167. kinfo->spi_rcvhdr_tailaddr =
  168. (u64) pd->port_rcvhdrqtailaddr_phys & MMAP64_MASK;
  169. kinfo->spi_rcv_egrbufs = ((u64) pd->subport_rcvegrbuf +
  170. dd->ipath_rcvegrcnt * dd->ipath_rcvegrbufsize * slave) &
  171. MMAP64_MASK;
  172. }
  173. kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->ipath_piobufbase) /
  174. dd->ipath_palign;
  175. kinfo->spi_pioalign = dd->ipath_palign;
  176. kinfo->spi_qpair = IPATH_KD_QP;
  177. kinfo->spi_piosize = dd->ipath_ibmaxlen;
  178. kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
  179. kinfo->spi_port = pd->port_port;
  180. kinfo->spi_subport = subport_fp(fp);
  181. kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
  182. kinfo->spi_hw_version = dd->ipath_revision;
  183. if (master) {
  184. kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
  185. kinfo->spi_subport_uregbase =
  186. (u64) pd->subport_uregbase & MMAP64_MASK;
  187. kinfo->spi_subport_rcvegrbuf =
  188. (u64) pd->subport_rcvegrbuf & MMAP64_MASK;
  189. kinfo->spi_subport_rcvhdr_base =
  190. (u64) pd->subport_rcvhdr_base & MMAP64_MASK;
  191. ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
  192. kinfo->spi_port, kinfo->spi_runtime_flags,
  193. (unsigned long long) kinfo->spi_subport_uregbase,
  194. (unsigned long long) kinfo->spi_subport_rcvegrbuf,
  195. (unsigned long long) kinfo->spi_subport_rcvhdr_base);
  196. }
  197. if (copy_to_user(ubase, kinfo, sizeof(*kinfo)))
  198. ret = -EFAULT;
  199. bail:
  200. kfree(kinfo);
  201. return ret;
  202. }
  203. /**
  204. * ipath_tid_update - update a port TID
  205. * @pd: the port
  206. * @fp: the ipath device file
  207. * @ti: the TID information
  208. *
  209. * The new implementation as of Oct 2004 is that the driver assigns
  210. * the tid and returns it to the caller. To make it easier to
  211. * catch bugs, and to reduce search time, we keep a cursor for
  212. * each port, walking the shadow tid array to find one that's not
  213. * in use.
  214. *
  215. * For now, if we can't allocate the full list, we fail, although
  216. * in the long run, we'll allocate as many as we can, and the
  217. * caller will deal with that by trying the remaining pages later.
  218. * That means that when we fail, we have to mark the tids as not in
  219. * use again, in our shadow copy.
  220. *
  221. * It's up to the caller to free the tids when they are done.
  222. * We'll unlock the pages as they free them.
  223. *
  224. * Also, right now we are locking one page at a time, but since
  225. * the intended use of this routine is for a single group of
  226. * virtually contiguous pages, that should change to improve
  227. * performance.
  228. */
  229. static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
  230. const struct ipath_tid_info *ti)
  231. {
  232. int ret = 0, ntids;
  233. u32 tid, porttid, cnt, i, tidcnt, tidoff;
  234. u16 *tidlist;
  235. struct ipath_devdata *dd = pd->port_dd;
  236. u64 physaddr;
  237. unsigned long vaddr;
  238. u64 __iomem *tidbase;
  239. unsigned long tidmap[8];
  240. struct page **pagep = NULL;
  241. unsigned subport = subport_fp(fp);
  242. if (!dd->ipath_pageshadow) {
  243. ret = -ENOMEM;
  244. goto done;
  245. }
  246. cnt = ti->tidcnt;
  247. if (!cnt) {
  248. ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
  249. (unsigned long long) ti->tidlist);
  250. /*
  251. * Should we treat as success? likely a bug
  252. */
  253. ret = -EFAULT;
  254. goto done;
  255. }
  256. porttid = pd->port_port * dd->ipath_rcvtidcnt;
  257. if (!pd->port_subport_cnt) {
  258. tidcnt = dd->ipath_rcvtidcnt;
  259. tid = pd->port_tidcursor;
  260. tidoff = 0;
  261. } else if (!subport) {
  262. tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
  263. (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
  264. tidoff = dd->ipath_rcvtidcnt - tidcnt;
  265. porttid += tidoff;
  266. tid = tidcursor_fp(fp);
  267. } else {
  268. tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
  269. tidoff = tidcnt * (subport - 1);
  270. porttid += tidoff;
  271. tid = tidcursor_fp(fp);
  272. }
  273. if (cnt > tidcnt) {
  274. /* make sure it all fits in port_tid_pg_list */
  275. dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
  276. "TIDs, only trying max (%u)\n", cnt, tidcnt);
  277. cnt = tidcnt;
  278. }
  279. pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
  280. tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
  281. memset(tidmap, 0, sizeof(tidmap));
  282. /* before decrement; chip actual # */
  283. ntids = tidcnt;
  284. tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
  285. dd->ipath_rcvtidbase +
  286. porttid * sizeof(*tidbase));
  287. ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
  288. pd->port_port, cnt, tid, tidbase);
  289. /* virtual address of first page in transfer */
  290. vaddr = ti->tidvaddr;
  291. if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
  292. cnt * PAGE_SIZE)) {
  293. ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
  294. (void *)vaddr, cnt);
  295. ret = -EFAULT;
  296. goto done;
  297. }
  298. ret = ipath_get_user_pages(vaddr, cnt, pagep);
  299. if (ret) {
  300. if (ret == -EBUSY) {
  301. ipath_dbg("Failed to lock addr %p, %u pages "
  302. "(already locked)\n",
  303. (void *) vaddr, cnt);
  304. /*
  305. * for now, continue, and see what happens but with
  306. * the new implementation, this should never happen,
  307. * unless perhaps the user has mpin'ed the pages
  308. * themselves (something we need to test)
  309. */
  310. ret = 0;
  311. } else {
  312. dev_info(&dd->pcidev->dev,
  313. "Failed to lock addr %p, %u pages: "
  314. "errno %d\n", (void *) vaddr, cnt, -ret);
  315. goto done;
  316. }
  317. }
  318. for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
  319. for (; ntids--; tid++) {
  320. if (tid == tidcnt)
  321. tid = 0;
  322. if (!dd->ipath_pageshadow[porttid + tid])
  323. break;
  324. }
  325. if (ntids < 0) {
  326. /*
  327. * oops, wrapped all the way through their TIDs,
  328. * and didn't have enough free; see comments at
  329. * start of routine
  330. */
  331. ipath_dbg("Not enough free TIDs for %u pages "
  332. "(index %d), failing\n", cnt, i);
  333. i--; /* last tidlist[i] not filled in */
  334. ret = -ENOMEM;
  335. break;
  336. }
  337. tidlist[i] = tid + tidoff;
  338. ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
  339. "vaddr %lx\n", i, tid + tidoff, vaddr);
  340. /* we "know" system pages and TID pages are same size */
  341. dd->ipath_pageshadow[porttid + tid] = pagep[i];
  342. dd->ipath_physshadow[porttid + tid] = ipath_map_page(
  343. dd->pcidev, pagep[i], 0, PAGE_SIZE,
  344. PCI_DMA_FROMDEVICE);
  345. /*
  346. * don't need atomic or it's overhead
  347. */
  348. __set_bit(tid, tidmap);
  349. physaddr = dd->ipath_physshadow[porttid + tid];
  350. ipath_stats.sps_pagelocks++;
  351. ipath_cdbg(VERBOSE,
  352. "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
  353. tid, vaddr, (unsigned long long) physaddr,
  354. pagep[i]);
  355. dd->ipath_f_put_tid(dd, &tidbase[tid], 1, physaddr);
  356. /*
  357. * don't check this tid in ipath_portshadow, since we
  358. * just filled it in; start with the next one.
  359. */
  360. tid++;
  361. }
  362. if (ret) {
  363. u32 limit;
  364. cleanup:
  365. /* jump here if copy out of updated info failed... */
  366. ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
  367. -ret, i, cnt);
  368. /* same code that's in ipath_free_tid() */
  369. limit = sizeof(tidmap) * BITS_PER_BYTE;
  370. if (limit > tidcnt)
  371. /* just in case size changes in future */
  372. limit = tidcnt;
  373. tid = find_first_bit((const unsigned long *)tidmap, limit);
  374. for (; tid < limit; tid++) {
  375. if (!test_bit(tid, tidmap))
  376. continue;
  377. if (dd->ipath_pageshadow[porttid + tid]) {
  378. ipath_cdbg(VERBOSE, "Freeing TID %u\n",
  379. tid);
  380. dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
  381. dd->ipath_tidinvalid);
  382. pci_unmap_page(dd->pcidev,
  383. dd->ipath_physshadow[porttid + tid],
  384. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  385. dd->ipath_pageshadow[porttid + tid] = NULL;
  386. ipath_stats.sps_pageunlocks++;
  387. }
  388. }
  389. ipath_release_user_pages(pagep, cnt);
  390. } else {
  391. /*
  392. * Copy the updated array, with ipath_tid's filled in, back
  393. * to user. Since we did the copy in already, this "should
  394. * never fail" If it does, we have to clean up...
  395. */
  396. if (copy_to_user((void __user *)
  397. (unsigned long) ti->tidlist,
  398. tidlist, cnt * sizeof(*tidlist))) {
  399. ret = -EFAULT;
  400. goto cleanup;
  401. }
  402. if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
  403. tidmap, sizeof tidmap)) {
  404. ret = -EFAULT;
  405. goto cleanup;
  406. }
  407. if (tid == tidcnt)
  408. tid = 0;
  409. if (!pd->port_subport_cnt)
  410. pd->port_tidcursor = tid;
  411. else
  412. tidcursor_fp(fp) = tid;
  413. }
  414. done:
  415. if (ret)
  416. ipath_dbg("Failed to map %u TID pages, failing with %d\n",
  417. ti->tidcnt, -ret);
  418. return ret;
  419. }
  420. /**
  421. * ipath_tid_free - free a port TID
  422. * @pd: the port
  423. * @subport: the subport
  424. * @ti: the TID info
  425. *
  426. * right now we are unlocking one page at a time, but since
  427. * the intended use of this routine is for a single group of
  428. * virtually contiguous pages, that should change to improve
  429. * performance. We check that the TID is in range for this port
  430. * but otherwise don't check validity; if user has an error and
  431. * frees the wrong tid, it's only their own data that can thereby
  432. * be corrupted. We do check that the TID was in use, for sanity
  433. * We always use our idea of the saved address, not the address that
  434. * they pass in to us.
  435. */
  436. static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
  437. const struct ipath_tid_info *ti)
  438. {
  439. int ret = 0;
  440. u32 tid, porttid, cnt, limit, tidcnt;
  441. struct ipath_devdata *dd = pd->port_dd;
  442. u64 __iomem *tidbase;
  443. unsigned long tidmap[8];
  444. if (!dd->ipath_pageshadow) {
  445. ret = -ENOMEM;
  446. goto done;
  447. }
  448. if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
  449. sizeof tidmap)) {
  450. ret = -EFAULT;
  451. goto done;
  452. }
  453. porttid = pd->port_port * dd->ipath_rcvtidcnt;
  454. if (!pd->port_subport_cnt)
  455. tidcnt = dd->ipath_rcvtidcnt;
  456. else if (!subport) {
  457. tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
  458. (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
  459. porttid += dd->ipath_rcvtidcnt - tidcnt;
  460. } else {
  461. tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
  462. porttid += tidcnt * (subport - 1);
  463. }
  464. tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
  465. dd->ipath_rcvtidbase +
  466. porttid * sizeof(*tidbase));
  467. limit = sizeof(tidmap) * BITS_PER_BYTE;
  468. if (limit > tidcnt)
  469. /* just in case size changes in future */
  470. limit = tidcnt;
  471. tid = find_first_bit(tidmap, limit);
  472. ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
  473. "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
  474. limit, tid, porttid);
  475. for (cnt = 0; tid < limit; tid++) {
  476. /*
  477. * small optimization; if we detect a run of 3 or so without
  478. * any set, use find_first_bit again. That's mainly to
  479. * accelerate the case where we wrapped, so we have some at
  480. * the beginning, and some at the end, and a big gap
  481. * in the middle.
  482. */
  483. if (!test_bit(tid, tidmap))
  484. continue;
  485. cnt++;
  486. if (dd->ipath_pageshadow[porttid + tid]) {
  487. ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
  488. pd->port_pid, tid);
  489. dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
  490. dd->ipath_tidinvalid);
  491. pci_unmap_page(dd->pcidev,
  492. dd->ipath_physshadow[porttid + tid],
  493. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  494. ipath_release_user_pages(
  495. &dd->ipath_pageshadow[porttid + tid], 1);
  496. dd->ipath_pageshadow[porttid + tid] = NULL;
  497. ipath_stats.sps_pageunlocks++;
  498. } else
  499. ipath_dbg("Unused tid %u, ignoring\n", tid);
  500. }
  501. if (cnt != ti->tidcnt)
  502. ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
  503. ti->tidcnt, cnt);
  504. done:
  505. if (ret)
  506. ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
  507. ti->tidcnt, -ret);
  508. return ret;
  509. }
  510. /**
  511. * ipath_set_part_key - set a partition key
  512. * @pd: the port
  513. * @key: the key
  514. *
  515. * We can have up to 4 active at a time (other than the default, which is
  516. * always allowed). This is somewhat tricky, since multiple ports may set
  517. * the same key, so we reference count them, and clean up at exit. All 4
  518. * partition keys are packed into a single infinipath register. It's an
  519. * error for a process to set the same pkey multiple times. We provide no
  520. * mechanism to de-allocate a pkey at this time, we may eventually need to
  521. * do that. I've used the atomic operations, and no locking, and only make
  522. * a single pass through what's available. This should be more than
  523. * adequate for some time. I'll think about spinlocks or the like if and as
  524. * it's necessary.
  525. */
  526. static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
  527. {
  528. struct ipath_devdata *dd = pd->port_dd;
  529. int i, any = 0, pidx = -1;
  530. u16 lkey = key & 0x7FFF;
  531. int ret;
  532. if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
  533. /* nothing to do; this key always valid */
  534. ret = 0;
  535. goto bail;
  536. }
  537. ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
  538. "%hx:%x %hx:%x %hx:%x %hx:%x\n",
  539. pd->port_port, key, dd->ipath_pkeys[0],
  540. atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
  541. atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
  542. atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
  543. atomic_read(&dd->ipath_pkeyrefs[3]));
  544. if (!lkey) {
  545. ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
  546. pd->port_port);
  547. ret = -EINVAL;
  548. goto bail;
  549. }
  550. /*
  551. * Set the full membership bit, because it has to be
  552. * set in the register or the packet, and it seems
  553. * cleaner to set in the register than to force all
  554. * callers to set it. (see bug 4331)
  555. */
  556. key |= 0x8000;
  557. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  558. if (!pd->port_pkeys[i] && pidx == -1)
  559. pidx = i;
  560. if (pd->port_pkeys[i] == key) {
  561. ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
  562. "(%x) more than once\n",
  563. pd->port_port, key);
  564. ret = -EEXIST;
  565. goto bail;
  566. }
  567. }
  568. if (pidx == -1) {
  569. ipath_dbg("All pkeys for port %u already in use, "
  570. "can't set %x\n", pd->port_port, key);
  571. ret = -EBUSY;
  572. goto bail;
  573. }
  574. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  575. if (!dd->ipath_pkeys[i]) {
  576. any++;
  577. continue;
  578. }
  579. if (dd->ipath_pkeys[i] == key) {
  580. atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
  581. if (atomic_inc_return(pkrefs) > 1) {
  582. pd->port_pkeys[pidx] = key;
  583. ipath_cdbg(VERBOSE, "p%u set key %x "
  584. "matches #%d, count now %d\n",
  585. pd->port_port, key, i,
  586. atomic_read(pkrefs));
  587. ret = 0;
  588. goto bail;
  589. } else {
  590. /*
  591. * lost race, decrement count, catch below
  592. */
  593. atomic_dec(pkrefs);
  594. ipath_cdbg(VERBOSE, "Lost race, count was "
  595. "0, after dec, it's %d\n",
  596. atomic_read(pkrefs));
  597. any++;
  598. }
  599. }
  600. if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
  601. /*
  602. * It makes no sense to have both the limited and
  603. * full membership PKEY set at the same time since
  604. * the unlimited one will disable the limited one.
  605. */
  606. ret = -EEXIST;
  607. goto bail;
  608. }
  609. }
  610. if (!any) {
  611. ipath_dbg("port %u, all pkeys already in use, "
  612. "can't set %x\n", pd->port_port, key);
  613. ret = -EBUSY;
  614. goto bail;
  615. }
  616. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  617. if (!dd->ipath_pkeys[i] &&
  618. atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
  619. u64 pkey;
  620. /* for ipathstats, etc. */
  621. ipath_stats.sps_pkeys[i] = lkey;
  622. pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
  623. pkey =
  624. (u64) dd->ipath_pkeys[0] |
  625. ((u64) dd->ipath_pkeys[1] << 16) |
  626. ((u64) dd->ipath_pkeys[2] << 32) |
  627. ((u64) dd->ipath_pkeys[3] << 48);
  628. ipath_cdbg(PROC, "p%u set key %x in #%d, "
  629. "portidx %d, new pkey reg %llx\n",
  630. pd->port_port, key, i, pidx,
  631. (unsigned long long) pkey);
  632. ipath_write_kreg(
  633. dd, dd->ipath_kregs->kr_partitionkey, pkey);
  634. ret = 0;
  635. goto bail;
  636. }
  637. }
  638. ipath_dbg("port %u, all pkeys already in use 2nd pass, "
  639. "can't set %x\n", pd->port_port, key);
  640. ret = -EBUSY;
  641. bail:
  642. return ret;
  643. }
  644. /**
  645. * ipath_manage_rcvq - manage a port's receive queue
  646. * @pd: the port
  647. * @subport: the subport
  648. * @start_stop: action to carry out
  649. *
  650. * start_stop == 0 disables receive on the port, for use in queue
  651. * overflow conditions. start_stop==1 re-enables, to be used to
  652. * re-init the software copy of the head register
  653. */
  654. static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
  655. int start_stop)
  656. {
  657. struct ipath_devdata *dd = pd->port_dd;
  658. u64 tval;
  659. ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
  660. start_stop ? "en" : "dis", dd->ipath_unit,
  661. pd->port_port, subport);
  662. if (subport)
  663. goto bail;
  664. /* atomically clear receive enable port. */
  665. if (start_stop) {
  666. /*
  667. * On enable, force in-memory copy of the tail register to
  668. * 0, so that protocol code doesn't have to worry about
  669. * whether or not the chip has yet updated the in-memory
  670. * copy or not on return from the system call. The chip
  671. * always resets it's tail register back to 0 on a
  672. * transition from disabled to enabled. This could cause a
  673. * problem if software was broken, and did the enable w/o
  674. * the disable, but eventually the in-memory copy will be
  675. * updated and correct itself, even in the face of software
  676. * bugs.
  677. */
  678. *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0;
  679. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  680. &dd->ipath_rcvctrl);
  681. } else
  682. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  683. &dd->ipath_rcvctrl);
  684. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  685. dd->ipath_rcvctrl);
  686. /* now be sure chip saw it before we return */
  687. tval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  688. if (start_stop) {
  689. /*
  690. * And try to be sure that tail reg update has happened too.
  691. * This should in theory interlock with the RXE changes to
  692. * the tail register. Don't assign it to the tail register
  693. * in memory copy, since we could overwrite an update by the
  694. * chip if we did.
  695. */
  696. tval = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  697. }
  698. /* always; new head should be equal to new tail; see above */
  699. bail:
  700. return 0;
  701. }
  702. static void ipath_clean_part_key(struct ipath_portdata *pd,
  703. struct ipath_devdata *dd)
  704. {
  705. int i, j, pchanged = 0;
  706. u64 oldpkey;
  707. /* for debugging only */
  708. oldpkey = (u64) dd->ipath_pkeys[0] |
  709. ((u64) dd->ipath_pkeys[1] << 16) |
  710. ((u64) dd->ipath_pkeys[2] << 32) |
  711. ((u64) dd->ipath_pkeys[3] << 48);
  712. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  713. if (!pd->port_pkeys[i])
  714. continue;
  715. ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
  716. pd->port_pkeys[i]);
  717. for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
  718. /* check for match independent of the global bit */
  719. if ((dd->ipath_pkeys[j] & 0x7fff) !=
  720. (pd->port_pkeys[i] & 0x7fff))
  721. continue;
  722. if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
  723. ipath_cdbg(VERBOSE, "p%u clear key "
  724. "%x matches #%d\n",
  725. pd->port_port,
  726. pd->port_pkeys[i], j);
  727. ipath_stats.sps_pkeys[j] =
  728. dd->ipath_pkeys[j] = 0;
  729. pchanged++;
  730. }
  731. else ipath_cdbg(
  732. VERBOSE, "p%u key %x matches #%d, "
  733. "but ref still %d\n", pd->port_port,
  734. pd->port_pkeys[i], j,
  735. atomic_read(&dd->ipath_pkeyrefs[j]));
  736. break;
  737. }
  738. pd->port_pkeys[i] = 0;
  739. }
  740. if (pchanged) {
  741. u64 pkey = (u64) dd->ipath_pkeys[0] |
  742. ((u64) dd->ipath_pkeys[1] << 16) |
  743. ((u64) dd->ipath_pkeys[2] << 32) |
  744. ((u64) dd->ipath_pkeys[3] << 48);
  745. ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
  746. "new pkey reg %llx\n", pd->port_port,
  747. (unsigned long long) oldpkey,
  748. (unsigned long long) pkey);
  749. ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
  750. pkey);
  751. }
  752. }
  753. /*
  754. * Initialize the port data with the receive buffer sizes
  755. * so this can be done while the master port is locked.
  756. * Otherwise, there is a race with a slave opening the port
  757. * and seeing these fields uninitialized.
  758. */
  759. static void init_user_egr_sizes(struct ipath_portdata *pd)
  760. {
  761. struct ipath_devdata *dd = pd->port_dd;
  762. unsigned egrperchunk, egrcnt, size;
  763. /*
  764. * to avoid wasting a lot of memory, we allocate 32KB chunks of
  765. * physically contiguous memory, advance through it until used up
  766. * and then allocate more. Of course, we need memory to store those
  767. * extra pointers, now. Started out with 256KB, but under heavy
  768. * memory pressure (creating large files and then copying them over
  769. * NFS while doing lots of MPI jobs), we hit some allocation
  770. * failures, even though we can sleep... (2.6.10) Still get
  771. * failures at 64K. 32K is the lowest we can go without wasting
  772. * additional memory.
  773. */
  774. size = 0x8000;
  775. egrperchunk = size / dd->ipath_rcvegrbufsize;
  776. egrcnt = dd->ipath_rcvegrcnt;
  777. pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
  778. pd->port_rcvegrbufs_perchunk = egrperchunk;
  779. pd->port_rcvegrbuf_size = size;
  780. }
  781. /**
  782. * ipath_create_user_egr - allocate eager TID buffers
  783. * @pd: the port to allocate TID buffers for
  784. *
  785. * This routine is now quite different for user and kernel, because
  786. * the kernel uses skb's, for the accelerated network performance
  787. * This is the user port version
  788. *
  789. * Allocate the eager TID buffers and program them into infinipath
  790. * They are no longer completely contiguous, we do multiple allocation
  791. * calls.
  792. */
  793. static int ipath_create_user_egr(struct ipath_portdata *pd)
  794. {
  795. struct ipath_devdata *dd = pd->port_dd;
  796. unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
  797. size_t size;
  798. int ret;
  799. gfp_t gfp_flags;
  800. /*
  801. * GFP_USER, but without GFP_FS, so buffer cache can be
  802. * coalesced (we hope); otherwise, even at order 4,
  803. * heavy filesystem activity makes these fail, and we can
  804. * use compound pages.
  805. */
  806. gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  807. egrcnt = dd->ipath_rcvegrcnt;
  808. /* TID number offset for this port */
  809. egroff = pd->port_port * egrcnt;
  810. egrsize = dd->ipath_rcvegrbufsize;
  811. ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
  812. "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
  813. chunk = pd->port_rcvegrbuf_chunks;
  814. egrperchunk = pd->port_rcvegrbufs_perchunk;
  815. size = pd->port_rcvegrbuf_size;
  816. pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
  817. GFP_KERNEL);
  818. if (!pd->port_rcvegrbuf) {
  819. ret = -ENOMEM;
  820. goto bail;
  821. }
  822. pd->port_rcvegrbuf_phys =
  823. kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
  824. GFP_KERNEL);
  825. if (!pd->port_rcvegrbuf_phys) {
  826. ret = -ENOMEM;
  827. goto bail_rcvegrbuf;
  828. }
  829. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  830. pd->port_rcvegrbuf[e] = dma_alloc_coherent(
  831. &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
  832. gfp_flags);
  833. if (!pd->port_rcvegrbuf[e]) {
  834. ret = -ENOMEM;
  835. goto bail_rcvegrbuf_phys;
  836. }
  837. }
  838. pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
  839. for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
  840. dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
  841. unsigned i;
  842. for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
  843. dd->ipath_f_put_tid(dd, e + egroff +
  844. (u64 __iomem *)
  845. ((char __iomem *)
  846. dd->ipath_kregbase +
  847. dd->ipath_rcvegrbase), 0, pa);
  848. pa += egrsize;
  849. }
  850. cond_resched(); /* don't hog the cpu */
  851. }
  852. ret = 0;
  853. goto bail;
  854. bail_rcvegrbuf_phys:
  855. for (e = 0; e < pd->port_rcvegrbuf_chunks &&
  856. pd->port_rcvegrbuf[e]; e++) {
  857. dma_free_coherent(&dd->pcidev->dev, size,
  858. pd->port_rcvegrbuf[e],
  859. pd->port_rcvegrbuf_phys[e]);
  860. }
  861. kfree(pd->port_rcvegrbuf_phys);
  862. pd->port_rcvegrbuf_phys = NULL;
  863. bail_rcvegrbuf:
  864. kfree(pd->port_rcvegrbuf);
  865. pd->port_rcvegrbuf = NULL;
  866. bail:
  867. return ret;
  868. }
  869. /* common code for the mappings on dma_alloc_coherent mem */
  870. static int ipath_mmap_mem(struct vm_area_struct *vma,
  871. struct ipath_portdata *pd, unsigned len, int write_ok,
  872. void *kvaddr, char *what)
  873. {
  874. struct ipath_devdata *dd = pd->port_dd;
  875. unsigned long pfn;
  876. int ret;
  877. if ((vma->vm_end - vma->vm_start) > len) {
  878. dev_info(&dd->pcidev->dev,
  879. "FAIL on %s: len %lx > %x\n", what,
  880. vma->vm_end - vma->vm_start, len);
  881. ret = -EFAULT;
  882. goto bail;
  883. }
  884. if (!write_ok) {
  885. if (vma->vm_flags & VM_WRITE) {
  886. dev_info(&dd->pcidev->dev,
  887. "%s must be mapped readonly\n", what);
  888. ret = -EPERM;
  889. goto bail;
  890. }
  891. /* don't allow them to later change with mprotect */
  892. vma->vm_flags &= ~VM_MAYWRITE;
  893. }
  894. pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
  895. ret = remap_pfn_range(vma, vma->vm_start, pfn,
  896. len, vma->vm_page_prot);
  897. if (ret)
  898. dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
  899. "bytes r%c failed: %d\n", what, pd->port_port,
  900. pfn, len, write_ok?'w':'o', ret);
  901. else
  902. ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
  903. "r%c\n", what, pd->port_port, pfn, len,
  904. write_ok?'w':'o');
  905. bail:
  906. return ret;
  907. }
  908. static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
  909. u64 ureg)
  910. {
  911. unsigned long phys;
  912. int ret;
  913. /*
  914. * This is real hardware, so use io_remap. This is the mechanism
  915. * for the user process to update the head registers for their port
  916. * in the chip.
  917. */
  918. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  919. dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
  920. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  921. ret = -EFAULT;
  922. } else {
  923. phys = dd->ipath_physaddr + ureg;
  924. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  925. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  926. ret = io_remap_pfn_range(vma, vma->vm_start,
  927. phys >> PAGE_SHIFT,
  928. vma->vm_end - vma->vm_start,
  929. vma->vm_page_prot);
  930. }
  931. return ret;
  932. }
  933. static int mmap_piobufs(struct vm_area_struct *vma,
  934. struct ipath_devdata *dd,
  935. struct ipath_portdata *pd,
  936. unsigned piobufs, unsigned piocnt)
  937. {
  938. unsigned long phys;
  939. int ret;
  940. /*
  941. * When we map the PIO buffers in the chip, we want to map them as
  942. * writeonly, no read possible. This prevents access to previous
  943. * process data, and catches users who might try to read the i/o
  944. * space due to a bug.
  945. */
  946. if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
  947. dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
  948. "reqlen %lx > PAGE\n",
  949. vma->vm_end - vma->vm_start);
  950. ret = -EINVAL;
  951. goto bail;
  952. }
  953. phys = dd->ipath_physaddr + piobufs;
  954. /*
  955. * Don't mark this as non-cached, or we don't get the
  956. * write combining behavior we want on the PIO buffers!
  957. */
  958. #if defined(__powerpc__)
  959. /* There isn't a generic way to specify writethrough mappings */
  960. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  961. pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
  962. pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
  963. #endif
  964. /*
  965. * don't allow them to later change to readable with mprotect (for when
  966. * not initially mapped readable, as is normally the case)
  967. */
  968. vma->vm_flags &= ~VM_MAYREAD;
  969. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  970. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  971. vma->vm_end - vma->vm_start,
  972. vma->vm_page_prot);
  973. bail:
  974. return ret;
  975. }
  976. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  977. struct ipath_portdata *pd)
  978. {
  979. struct ipath_devdata *dd = pd->port_dd;
  980. unsigned long start, size;
  981. size_t total_size, i;
  982. unsigned long pfn;
  983. int ret;
  984. size = pd->port_rcvegrbuf_size;
  985. total_size = pd->port_rcvegrbuf_chunks * size;
  986. if ((vma->vm_end - vma->vm_start) > total_size) {
  987. dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
  988. "reqlen %lx > actual %lx\n",
  989. vma->vm_end - vma->vm_start,
  990. (unsigned long) total_size);
  991. ret = -EINVAL;
  992. goto bail;
  993. }
  994. if (vma->vm_flags & VM_WRITE) {
  995. dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
  996. "writable (flags=%lx)\n", vma->vm_flags);
  997. ret = -EPERM;
  998. goto bail;
  999. }
  1000. /* don't allow them to later change to writeable with mprotect */
  1001. vma->vm_flags &= ~VM_MAYWRITE;
  1002. start = vma->vm_start;
  1003. for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
  1004. pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
  1005. ret = remap_pfn_range(vma, start, pfn, size,
  1006. vma->vm_page_prot);
  1007. if (ret < 0)
  1008. goto bail;
  1009. }
  1010. ret = 0;
  1011. bail:
  1012. return ret;
  1013. }
  1014. /*
  1015. * ipath_file_vma_nopage - handle a VMA page fault.
  1016. */
  1017. static struct page *ipath_file_vma_nopage(struct vm_area_struct *vma,
  1018. unsigned long address, int *type)
  1019. {
  1020. unsigned long offset = address - vma->vm_start;
  1021. struct page *page = NOPAGE_SIGBUS;
  1022. void *pageptr;
  1023. /*
  1024. * Convert the vmalloc address into a struct page.
  1025. */
  1026. pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
  1027. page = vmalloc_to_page(pageptr);
  1028. if (!page)
  1029. goto out;
  1030. /* Increment the reference count. */
  1031. get_page(page);
  1032. if (type)
  1033. *type = VM_FAULT_MINOR;
  1034. out:
  1035. return page;
  1036. }
  1037. static struct vm_operations_struct ipath_file_vm_ops = {
  1038. .nopage = ipath_file_vma_nopage,
  1039. };
  1040. static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
  1041. struct ipath_portdata *pd, unsigned subport)
  1042. {
  1043. unsigned long len;
  1044. struct ipath_devdata *dd;
  1045. void *addr;
  1046. size_t size;
  1047. int ret;
  1048. /* If the port is not shared, all addresses should be physical */
  1049. if (!pd->port_subport_cnt) {
  1050. ret = -EINVAL;
  1051. goto bail;
  1052. }
  1053. dd = pd->port_dd;
  1054. size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  1055. /*
  1056. * Master has all the slave uregbase, rcvhdrq, and
  1057. * rcvegrbufs mmapped.
  1058. */
  1059. if (subport == 0) {
  1060. unsigned num_slaves = pd->port_subport_cnt - 1;
  1061. if (pgaddr == ((u64) pd->subport_uregbase & MMAP64_MASK)) {
  1062. addr = pd->subport_uregbase;
  1063. size = PAGE_SIZE * num_slaves;
  1064. } else if (pgaddr == ((u64) pd->subport_rcvhdr_base &
  1065. MMAP64_MASK)) {
  1066. addr = pd->subport_rcvhdr_base;
  1067. size = pd->port_rcvhdrq_size * num_slaves;
  1068. } else if (pgaddr == ((u64) pd->subport_rcvegrbuf &
  1069. MMAP64_MASK)) {
  1070. addr = pd->subport_rcvegrbuf;
  1071. size *= num_slaves;
  1072. } else {
  1073. ret = -EINVAL;
  1074. goto bail;
  1075. }
  1076. } else if (pgaddr == (((u64) pd->subport_uregbase +
  1077. PAGE_SIZE * (subport - 1)) & MMAP64_MASK)) {
  1078. addr = pd->subport_uregbase + PAGE_SIZE * (subport - 1);
  1079. size = PAGE_SIZE;
  1080. } else if (pgaddr == (((u64) pd->subport_rcvhdr_base +
  1081. pd->port_rcvhdrq_size * (subport - 1)) &
  1082. MMAP64_MASK)) {
  1083. addr = pd->subport_rcvhdr_base +
  1084. pd->port_rcvhdrq_size * (subport - 1);
  1085. size = pd->port_rcvhdrq_size;
  1086. } else if (pgaddr == (((u64) pd->subport_rcvegrbuf +
  1087. size * (subport - 1)) & MMAP64_MASK)) {
  1088. addr = pd->subport_rcvegrbuf + size * (subport - 1);
  1089. /* rcvegrbufs are read-only on the slave */
  1090. if (vma->vm_flags & VM_WRITE) {
  1091. dev_info(&dd->pcidev->dev,
  1092. "Can't map eager buffers as "
  1093. "writable (flags=%lx)\n", vma->vm_flags);
  1094. ret = -EPERM;
  1095. goto bail;
  1096. }
  1097. /*
  1098. * Don't allow permission to later change to writeable
  1099. * with mprotect.
  1100. */
  1101. vma->vm_flags &= ~VM_MAYWRITE;
  1102. } else {
  1103. ret = -EINVAL;
  1104. goto bail;
  1105. }
  1106. len = vma->vm_end - vma->vm_start;
  1107. if (len > size) {
  1108. ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
  1109. ret = -EINVAL;
  1110. goto bail;
  1111. }
  1112. vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
  1113. vma->vm_ops = &ipath_file_vm_ops;
  1114. vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
  1115. ret = 0;
  1116. bail:
  1117. return ret;
  1118. }
  1119. /**
  1120. * ipath_mmap - mmap various structures into user space
  1121. * @fp: the file pointer
  1122. * @vma: the VM area
  1123. *
  1124. * We use this to have a shared buffer between the kernel and the user code
  1125. * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
  1126. * buffers in the chip. We have the open and close entries so we can bump
  1127. * the ref count and keep the driver from being unloaded while still mapped.
  1128. */
  1129. static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
  1130. {
  1131. struct ipath_portdata *pd;
  1132. struct ipath_devdata *dd;
  1133. u64 pgaddr, ureg;
  1134. unsigned piobufs, piocnt;
  1135. int ret;
  1136. pd = port_fp(fp);
  1137. if (!pd) {
  1138. ret = -EINVAL;
  1139. goto bail;
  1140. }
  1141. dd = pd->port_dd;
  1142. /*
  1143. * This is the ipath_do_user_init() code, mapping the shared buffers
  1144. * into the user process. The address referred to by vm_pgoff is the
  1145. * file offset passed via mmap(). For shared ports, this is the
  1146. * kernel vmalloc() address of the pages to share with the master.
  1147. * For non-shared or master ports, this is a physical address.
  1148. * We only do one mmap for each space mapped.
  1149. */
  1150. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  1151. /*
  1152. * Check for 0 in case one of the allocations failed, but user
  1153. * called mmap anyway.
  1154. */
  1155. if (!pgaddr) {
  1156. ret = -EINVAL;
  1157. goto bail;
  1158. }
  1159. ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
  1160. (unsigned long long) pgaddr, vma->vm_start,
  1161. vma->vm_end - vma->vm_start, dd->ipath_unit,
  1162. pd->port_port, subport_fp(fp));
  1163. /*
  1164. * Physical addresses must fit in 40 bits for our hardware.
  1165. * Check for kernel virtual addresses first, anything else must
  1166. * match a HW or memory address.
  1167. */
  1168. if (pgaddr >= (1ULL<<40)) {
  1169. ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
  1170. goto bail;
  1171. }
  1172. if (!pd->port_subport_cnt) {
  1173. /* port is not shared */
  1174. ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  1175. piocnt = dd->ipath_pbufsport;
  1176. piobufs = pd->port_piobufs;
  1177. } else if (!subport_fp(fp)) {
  1178. /* caller is the master */
  1179. ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  1180. piocnt = (dd->ipath_pbufsport / pd->port_subport_cnt) +
  1181. (dd->ipath_pbufsport % pd->port_subport_cnt);
  1182. piobufs = pd->port_piobufs +
  1183. dd->ipath_palign * (dd->ipath_pbufsport - piocnt);
  1184. } else {
  1185. unsigned slave = subport_fp(fp) - 1;
  1186. /* caller is a slave */
  1187. ureg = 0;
  1188. piocnt = dd->ipath_pbufsport / pd->port_subport_cnt;
  1189. piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
  1190. }
  1191. if (pgaddr == ureg)
  1192. ret = mmap_ureg(vma, dd, ureg);
  1193. else if (pgaddr == piobufs)
  1194. ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
  1195. else if (pgaddr == dd->ipath_pioavailregs_phys)
  1196. /* in-memory copy of pioavail registers */
  1197. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1198. (void *) dd->ipath_pioavailregs_dma,
  1199. "pioavail registers");
  1200. else if (subport_fp(fp))
  1201. /* Subports don't mmap the physical receive buffers */
  1202. ret = -EINVAL;
  1203. else if (pgaddr == pd->port_rcvegr_phys)
  1204. ret = mmap_rcvegrbufs(vma, pd);
  1205. else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
  1206. /*
  1207. * The rcvhdrq itself; readonly except on HT (so have
  1208. * to allow writable mapping), multiple pages, contiguous
  1209. * from an i/o perspective.
  1210. */
  1211. ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
  1212. pd->port_rcvhdrq,
  1213. "rcvhdrq");
  1214. else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
  1215. /* in-memory copy of rcvhdrq tail register */
  1216. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1217. pd->port_rcvhdrtail_kvaddr,
  1218. "rcvhdrq tail");
  1219. else
  1220. ret = -EINVAL;
  1221. vma->vm_private_data = NULL;
  1222. if (ret < 0)
  1223. dev_info(&dd->pcidev->dev,
  1224. "Failure %d on off %llx len %lx\n",
  1225. -ret, (unsigned long long)pgaddr,
  1226. vma->vm_end - vma->vm_start);
  1227. bail:
  1228. return ret;
  1229. }
  1230. static unsigned int ipath_poll(struct file *fp,
  1231. struct poll_table_struct *pt)
  1232. {
  1233. struct ipath_portdata *pd;
  1234. u32 head, tail;
  1235. int bit;
  1236. unsigned pollflag = 0;
  1237. struct ipath_devdata *dd;
  1238. pd = port_fp(fp);
  1239. if (!pd)
  1240. goto bail;
  1241. dd = pd->port_dd;
  1242. bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
  1243. set_bit(bit, &dd->ipath_rcvctrl);
  1244. /*
  1245. * Before blocking, make sure that head is still == tail,
  1246. * reading from the chip, so we can be sure the interrupt
  1247. * enable has made it to the chip. If not equal, disable
  1248. * interrupt again and return immediately. This avoids races,
  1249. * and the overhead of the chip read doesn't matter much at
  1250. * this point, since we are waiting for something anyway.
  1251. */
  1252. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1253. dd->ipath_rcvctrl);
  1254. head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
  1255. tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  1256. if (tail == head) {
  1257. set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1258. if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
  1259. (void)ipath_write_ureg(dd, ur_rcvhdrhead,
  1260. dd->ipath_rhdrhead_intr_off
  1261. | head, pd->port_port);
  1262. poll_wait(fp, &pd->port_wait, pt);
  1263. if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
  1264. /* timed out, no packets received */
  1265. clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1266. pd->port_rcvwait_to++;
  1267. }
  1268. else
  1269. pollflag = POLLIN | POLLRDNORM;
  1270. }
  1271. else {
  1272. /* it's already happened; don't do wait_event overhead */
  1273. pollflag = POLLIN | POLLRDNORM;
  1274. pd->port_rcvnowait++;
  1275. }
  1276. clear_bit(bit, &dd->ipath_rcvctrl);
  1277. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1278. dd->ipath_rcvctrl);
  1279. bail:
  1280. return pollflag;
  1281. }
  1282. static int init_subports(struct ipath_devdata *dd,
  1283. struct ipath_portdata *pd,
  1284. const struct ipath_user_info *uinfo)
  1285. {
  1286. int ret = 0;
  1287. unsigned num_slaves;
  1288. size_t size;
  1289. /* Old user binaries don't know about subports */
  1290. if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR)
  1291. goto bail;
  1292. /*
  1293. * If the user is requesting zero or one port,
  1294. * skip the subport allocation.
  1295. */
  1296. if (uinfo->spu_subport_cnt <= 1)
  1297. goto bail;
  1298. if (uinfo->spu_subport_cnt > 4) {
  1299. ret = -EINVAL;
  1300. goto bail;
  1301. }
  1302. num_slaves = uinfo->spu_subport_cnt - 1;
  1303. pd->subport_uregbase = vmalloc(PAGE_SIZE * num_slaves);
  1304. if (!pd->subport_uregbase) {
  1305. ret = -ENOMEM;
  1306. goto bail;
  1307. }
  1308. /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
  1309. size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1310. sizeof(u32), PAGE_SIZE) * num_slaves;
  1311. pd->subport_rcvhdr_base = vmalloc(size);
  1312. if (!pd->subport_rcvhdr_base) {
  1313. ret = -ENOMEM;
  1314. goto bail_ureg;
  1315. }
  1316. pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
  1317. pd->port_rcvegrbuf_size *
  1318. num_slaves);
  1319. if (!pd->subport_rcvegrbuf) {
  1320. ret = -ENOMEM;
  1321. goto bail_rhdr;
  1322. }
  1323. pd->port_subport_cnt = uinfo->spu_subport_cnt;
  1324. pd->port_subport_id = uinfo->spu_subport_id;
  1325. pd->active_slaves = 1;
  1326. goto bail;
  1327. bail_rhdr:
  1328. vfree(pd->subport_rcvhdr_base);
  1329. bail_ureg:
  1330. vfree(pd->subport_uregbase);
  1331. pd->subport_uregbase = NULL;
  1332. bail:
  1333. return ret;
  1334. }
  1335. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1336. struct file *fp,
  1337. const struct ipath_user_info *uinfo)
  1338. {
  1339. struct ipath_portdata *pd;
  1340. int ret;
  1341. if (!(pd = dd->ipath_pd[port])) {
  1342. void *ptmp;
  1343. pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1344. /*
  1345. * Allocate memory for use in ipath_tid_update() just once
  1346. * at open, not per call. Reduces cost of expected send
  1347. * setup.
  1348. */
  1349. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1350. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1351. GFP_KERNEL);
  1352. if (!pd || !ptmp) {
  1353. ipath_dev_err(dd, "Unable to allocate portdata "
  1354. "memory, failing open\n");
  1355. ret = -ENOMEM;
  1356. kfree(pd);
  1357. kfree(ptmp);
  1358. goto bail;
  1359. }
  1360. dd->ipath_pd[port] = pd;
  1361. dd->ipath_pd[port]->port_port = port;
  1362. dd->ipath_pd[port]->port_dd = dd;
  1363. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1364. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1365. }
  1366. if (!pd->port_cnt) {
  1367. pd->userversion = uinfo->spu_userversion;
  1368. init_user_egr_sizes(pd);
  1369. if ((ret = init_subports(dd, pd, uinfo)) != 0)
  1370. goto bail;
  1371. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1372. current->comm, current->pid, dd->ipath_unit,
  1373. port);
  1374. pd->port_cnt = 1;
  1375. port_fp(fp) = pd;
  1376. pd->port_pid = current->pid;
  1377. strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
  1378. ipath_stats.sps_ports++;
  1379. ret = 0;
  1380. } else
  1381. ret = -EBUSY;
  1382. bail:
  1383. return ret;
  1384. }
  1385. static inline int usable(struct ipath_devdata *dd)
  1386. {
  1387. return dd &&
  1388. (dd->ipath_flags & IPATH_PRESENT) &&
  1389. dd->ipath_kregbase &&
  1390. dd->ipath_lid &&
  1391. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1392. | IPATH_LINKUNK));
  1393. }
  1394. static int find_free_port(int unit, struct file *fp,
  1395. const struct ipath_user_info *uinfo)
  1396. {
  1397. struct ipath_devdata *dd = ipath_lookup(unit);
  1398. int ret, i;
  1399. if (!dd) {
  1400. ret = -ENODEV;
  1401. goto bail;
  1402. }
  1403. if (!usable(dd)) {
  1404. ret = -ENETDOWN;
  1405. goto bail;
  1406. }
  1407. for (i = 1; i < dd->ipath_cfgports; i++) {
  1408. ret = try_alloc_port(dd, i, fp, uinfo);
  1409. if (ret != -EBUSY)
  1410. goto bail;
  1411. }
  1412. ret = -EBUSY;
  1413. bail:
  1414. return ret;
  1415. }
  1416. static int find_best_unit(struct file *fp,
  1417. const struct ipath_user_info *uinfo)
  1418. {
  1419. int ret = 0, i, prefunit = -1, devmax;
  1420. int maxofallports, npresent, nup;
  1421. int ndev;
  1422. devmax = ipath_count_units(&npresent, &nup, &maxofallports);
  1423. /*
  1424. * This code is present to allow a knowledgeable person to
  1425. * specify the layout of processes to processors before opening
  1426. * this driver, and then we'll assign the process to the "closest"
  1427. * InfiniPath chip to that processor (we assume reasonable connectivity,
  1428. * for now). This code assumes that if affinity has been set
  1429. * before this point, that at most one cpu is set; for now this
  1430. * is reasonable. I check for both cpus_empty() and cpus_full(),
  1431. * in case some kernel variant sets none of the bits when no
  1432. * affinity is set. 2.6.11 and 12 kernels have all present
  1433. * cpus set. Some day we'll have to fix it up further to handle
  1434. * a cpu subset. This algorithm fails for two HT chips connected
  1435. * in tunnel fashion. Eventually this needs real topology
  1436. * information. There may be some issues with dual core numbering
  1437. * as well. This needs more work prior to release.
  1438. */
  1439. if (!cpus_empty(current->cpus_allowed) &&
  1440. !cpus_full(current->cpus_allowed)) {
  1441. int ncpus = num_online_cpus(), curcpu = -1;
  1442. for (i = 0; i < ncpus; i++)
  1443. if (cpu_isset(i, current->cpus_allowed)) {
  1444. ipath_cdbg(PROC, "%s[%u] affinity set for "
  1445. "cpu %d\n", current->comm,
  1446. current->pid, i);
  1447. curcpu = i;
  1448. }
  1449. if (curcpu != -1) {
  1450. if (npresent) {
  1451. prefunit = curcpu / (ncpus / npresent);
  1452. ipath_dbg("%s[%u] %d chips, %d cpus, "
  1453. "%d cpus/chip, select unit %d\n",
  1454. current->comm, current->pid,
  1455. npresent, ncpus, ncpus / npresent,
  1456. prefunit);
  1457. }
  1458. }
  1459. }
  1460. /*
  1461. * user ports start at 1, kernel port is 0
  1462. * For now, we do round-robin access across all chips
  1463. */
  1464. if (prefunit != -1)
  1465. devmax = prefunit + 1;
  1466. recheck:
  1467. for (i = 1; i < maxofallports; i++) {
  1468. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1469. ndev++) {
  1470. struct ipath_devdata *dd = ipath_lookup(ndev);
  1471. if (!usable(dd))
  1472. continue; /* can't use this unit */
  1473. if (i >= dd->ipath_cfgports)
  1474. /*
  1475. * Maxed out on users of this unit. Try
  1476. * next.
  1477. */
  1478. continue;
  1479. ret = try_alloc_port(dd, i, fp, uinfo);
  1480. if (!ret)
  1481. goto done;
  1482. }
  1483. }
  1484. if (npresent) {
  1485. if (nup == 0) {
  1486. ret = -ENETDOWN;
  1487. ipath_dbg("No ports available (none initialized "
  1488. "and ready)\n");
  1489. } else {
  1490. if (prefunit > 0) {
  1491. /* if started above 0, retry from 0 */
  1492. ipath_cdbg(PROC,
  1493. "%s[%u] no ports on prefunit "
  1494. "%d, clear and re-check\n",
  1495. current->comm, current->pid,
  1496. prefunit);
  1497. devmax = ipath_count_units(NULL, NULL,
  1498. NULL);
  1499. prefunit = -1;
  1500. goto recheck;
  1501. }
  1502. ret = -EBUSY;
  1503. ipath_dbg("No ports available\n");
  1504. }
  1505. } else {
  1506. ret = -ENXIO;
  1507. ipath_dbg("No boards found\n");
  1508. }
  1509. done:
  1510. return ret;
  1511. }
  1512. static int find_shared_port(struct file *fp,
  1513. const struct ipath_user_info *uinfo)
  1514. {
  1515. int devmax, ndev, i;
  1516. int ret = 0;
  1517. devmax = ipath_count_units(NULL, NULL, NULL);
  1518. for (ndev = 0; ndev < devmax; ndev++) {
  1519. struct ipath_devdata *dd = ipath_lookup(ndev);
  1520. if (!dd)
  1521. continue;
  1522. for (i = 1; i < dd->ipath_cfgports; i++) {
  1523. struct ipath_portdata *pd = dd->ipath_pd[i];
  1524. /* Skip ports which are not yet open */
  1525. if (!pd || !pd->port_cnt)
  1526. continue;
  1527. /* Skip port if it doesn't match the requested one */
  1528. if (pd->port_subport_id != uinfo->spu_subport_id)
  1529. continue;
  1530. /* Verify the sharing process matches the master */
  1531. if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
  1532. pd->userversion != uinfo->spu_userversion ||
  1533. pd->port_cnt >= pd->port_subport_cnt) {
  1534. ret = -EINVAL;
  1535. goto done;
  1536. }
  1537. port_fp(fp) = pd;
  1538. subport_fp(fp) = pd->port_cnt++;
  1539. tidcursor_fp(fp) = 0;
  1540. pd->active_slaves |= 1 << subport_fp(fp);
  1541. ipath_cdbg(PROC,
  1542. "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
  1543. current->comm, current->pid,
  1544. subport_fp(fp),
  1545. pd->port_comm, pd->port_pid,
  1546. dd->ipath_unit, pd->port_port);
  1547. ret = 1;
  1548. goto done;
  1549. }
  1550. }
  1551. done:
  1552. return ret;
  1553. }
  1554. static int ipath_open(struct inode *in, struct file *fp)
  1555. {
  1556. /* The real work is performed later in ipath_do_user_init() */
  1557. fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
  1558. return fp->private_data ? 0 : -ENOMEM;
  1559. }
  1560. static int ipath_do_user_init(struct file *fp,
  1561. const struct ipath_user_info *uinfo)
  1562. {
  1563. int ret;
  1564. struct ipath_portdata *pd;
  1565. struct ipath_devdata *dd;
  1566. u32 head32;
  1567. int i_minor;
  1568. unsigned swminor;
  1569. /* Check to be sure we haven't already initialized this file */
  1570. if (port_fp(fp)) {
  1571. ret = -EINVAL;
  1572. goto done;
  1573. }
  1574. /* for now, if major version is different, bail */
  1575. if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) {
  1576. ipath_dbg("User major version %d not same as driver "
  1577. "major %d\n", uinfo->spu_userversion >> 16,
  1578. IPATH_USER_SWMAJOR);
  1579. ret = -ENODEV;
  1580. goto done;
  1581. }
  1582. swminor = uinfo->spu_userversion & 0xffff;
  1583. if (swminor != IPATH_USER_SWMINOR)
  1584. ipath_dbg("User minor version %d not same as driver "
  1585. "minor %d\n", swminor, IPATH_USER_SWMINOR);
  1586. mutex_lock(&ipath_mutex);
  1587. if (swminor == IPATH_USER_SWMINOR && uinfo->spu_subport_cnt &&
  1588. (ret = find_shared_port(fp, uinfo))) {
  1589. mutex_unlock(&ipath_mutex);
  1590. if (ret > 0)
  1591. ret = 0;
  1592. goto done;
  1593. }
  1594. i_minor = iminor(fp->f_dentry->d_inode) - IPATH_USER_MINOR_BASE;
  1595. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1596. (long)fp->f_dentry->d_inode->i_rdev, i_minor);
  1597. if (i_minor)
  1598. ret = find_free_port(i_minor - 1, fp, uinfo);
  1599. else
  1600. ret = find_best_unit(fp, uinfo);
  1601. mutex_unlock(&ipath_mutex);
  1602. if (ret)
  1603. goto done;
  1604. pd = port_fp(fp);
  1605. dd = pd->port_dd;
  1606. if (uinfo->spu_rcvhdrsize) {
  1607. ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
  1608. if (ret)
  1609. goto done;
  1610. }
  1611. /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
  1612. /* for right now, kernel piobufs are at end, so port 1 is at 0 */
  1613. pd->port_piobufs = dd->ipath_piobufbase +
  1614. dd->ipath_pbufsport * (pd->port_port - 1) * dd->ipath_palign;
  1615. ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
  1616. pd->port_port, pd->port_piobufs);
  1617. /*
  1618. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  1619. * array for time being. If pd->port_port > chip-supported,
  1620. * we need to do extra stuff here to handle by handling overflow
  1621. * through port 0, someday
  1622. */
  1623. ret = ipath_create_rcvhdrq(dd, pd);
  1624. if (!ret)
  1625. ret = ipath_create_user_egr(pd);
  1626. if (ret)
  1627. goto done;
  1628. /*
  1629. * set the eager head register for this port to the current values
  1630. * of the tail pointers, since we don't know if they were
  1631. * updated on last use of the port.
  1632. */
  1633. head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
  1634. ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
  1635. dd->ipath_lastegrheads[pd->port_port] = -1;
  1636. dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
  1637. ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
  1638. pd->port_port, head32);
  1639. pd->port_tidcursor = 0; /* start at beginning after open */
  1640. /*
  1641. * now enable the port; the tail registers will be written to memory
  1642. * by the chip as soon as it sees the write to
  1643. * dd->ipath_kregs->kr_rcvctrl. The update only happens on
  1644. * transition from 0 to 1, so clear it first, then set it as part of
  1645. * enabling the port. This will (very briefly) affect any other
  1646. * open ports, but it shouldn't be long enough to be an issue.
  1647. * We explictly set the in-memory copy to 0 beforehand, so we don't
  1648. * have to wait to be sure the DMA update has happened.
  1649. */
  1650. *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL;
  1651. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  1652. &dd->ipath_rcvctrl);
  1653. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1654. dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
  1655. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1656. dd->ipath_rcvctrl);
  1657. done:
  1658. return ret;
  1659. }
  1660. /**
  1661. * unlock_exptid - unlock any expected TID entries port still had in use
  1662. * @pd: port
  1663. *
  1664. * We don't actually update the chip here, because we do a bulk update
  1665. * below, using ipath_f_clear_tids.
  1666. */
  1667. static void unlock_expected_tids(struct ipath_portdata *pd)
  1668. {
  1669. struct ipath_devdata *dd = pd->port_dd;
  1670. int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
  1671. int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1672. ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
  1673. pd->port_port);
  1674. for (i = port_tidbase; i < maxtid; i++) {
  1675. if (!dd->ipath_pageshadow[i])
  1676. continue;
  1677. pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
  1678. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1679. ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
  1680. 1);
  1681. dd->ipath_pageshadow[i] = NULL;
  1682. cnt++;
  1683. ipath_stats.sps_pageunlocks++;
  1684. }
  1685. if (cnt)
  1686. ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
  1687. pd->port_port, cnt);
  1688. if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
  1689. ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
  1690. (unsigned long long) ipath_stats.sps_pagelocks,
  1691. (unsigned long long)
  1692. ipath_stats.sps_pageunlocks);
  1693. }
  1694. static int ipath_close(struct inode *in, struct file *fp)
  1695. {
  1696. int ret = 0;
  1697. struct ipath_filedata *fd;
  1698. struct ipath_portdata *pd;
  1699. struct ipath_devdata *dd;
  1700. unsigned port;
  1701. ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
  1702. (long)in->i_rdev, fp->private_data);
  1703. mutex_lock(&ipath_mutex);
  1704. fd = (struct ipath_filedata *) fp->private_data;
  1705. fp->private_data = NULL;
  1706. pd = fd->pd;
  1707. if (!pd) {
  1708. mutex_unlock(&ipath_mutex);
  1709. goto bail;
  1710. }
  1711. if (--pd->port_cnt) {
  1712. /*
  1713. * XXX If the master closes the port before the slave(s),
  1714. * revoke the mmap for the eager receive queue so
  1715. * the slave(s) don't wait for receive data forever.
  1716. */
  1717. pd->active_slaves &= ~(1 << fd->subport);
  1718. mutex_unlock(&ipath_mutex);
  1719. goto bail;
  1720. }
  1721. port = pd->port_port;
  1722. dd = pd->port_dd;
  1723. if (pd->port_hdrqfull) {
  1724. ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
  1725. "during run\n", pd->port_comm, pd->port_pid,
  1726. pd->port_hdrqfull);
  1727. pd->port_hdrqfull = 0;
  1728. }
  1729. if (pd->port_rcvwait_to || pd->port_piowait_to
  1730. || pd->port_rcvnowait || pd->port_pionowait) {
  1731. ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
  1732. "%u rcv %u, pio already\n",
  1733. pd->port_port, pd->port_rcvwait_to,
  1734. pd->port_piowait_to, pd->port_rcvnowait,
  1735. pd->port_pionowait);
  1736. pd->port_rcvwait_to = pd->port_piowait_to =
  1737. pd->port_rcvnowait = pd->port_pionowait = 0;
  1738. }
  1739. if (pd->port_flag) {
  1740. ipath_dbg("port %u port_flag still set to 0x%lx\n",
  1741. pd->port_port, pd->port_flag);
  1742. pd->port_flag = 0;
  1743. }
  1744. if (dd->ipath_kregbase) {
  1745. int i;
  1746. /* atomically clear receive enable port. */
  1747. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
  1748. &dd->ipath_rcvctrl);
  1749. ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
  1750. dd->ipath_rcvctrl);
  1751. /* and read back from chip to be sure that nothing
  1752. * else is in flight when we do the rest */
  1753. (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1754. /* clean up the pkeys for this port user */
  1755. ipath_clean_part_key(pd, dd);
  1756. /*
  1757. * be paranoid, and never write 0's to these, just use an
  1758. * unused part of the port 0 tail page. Of course,
  1759. * rcvhdraddr points to a large chunk of memory, so this
  1760. * could still trash things, but at least it won't trash
  1761. * page 0, and by disabling the port, it should stop "soon",
  1762. * even if a packet or two is in already in flight after we
  1763. * disabled the port.
  1764. */
  1765. ipath_write_kreg_port(dd,
  1766. dd->ipath_kregs->kr_rcvhdrtailaddr, port,
  1767. dd->ipath_dummy_hdrq_phys);
  1768. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1769. pd->port_port, dd->ipath_dummy_hdrq_phys);
  1770. i = dd->ipath_pbufsport * (port - 1);
  1771. ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
  1772. dd->ipath_f_clear_tids(dd, pd->port_port);
  1773. if (dd->ipath_pageshadow)
  1774. unlock_expected_tids(pd);
  1775. ipath_stats.sps_ports--;
  1776. ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
  1777. pd->port_comm, pd->port_pid,
  1778. dd->ipath_unit, port);
  1779. }
  1780. pd->port_pid = 0;
  1781. dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
  1782. mutex_unlock(&ipath_mutex);
  1783. ipath_free_pddata(dd, pd); /* after releasing the mutex */
  1784. bail:
  1785. kfree(fd);
  1786. return ret;
  1787. }
  1788. static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
  1789. struct ipath_port_info __user *uinfo)
  1790. {
  1791. struct ipath_port_info info;
  1792. int nup;
  1793. int ret;
  1794. size_t sz;
  1795. (void) ipath_count_units(NULL, &nup, NULL);
  1796. info.num_active = nup;
  1797. info.unit = pd->port_dd->ipath_unit;
  1798. info.port = pd->port_port;
  1799. info.subport = subport;
  1800. /* Don't return new fields if old library opened the port. */
  1801. if ((pd->userversion & 0xffff) == IPATH_USER_SWMINOR) {
  1802. /* Number of user ports available for this device. */
  1803. info.num_ports = pd->port_dd->ipath_cfgports - 1;
  1804. info.num_subports = pd->port_subport_cnt;
  1805. sz = sizeof(info);
  1806. } else
  1807. sz = sizeof(info) - 2 * sizeof(u16);
  1808. if (copy_to_user(uinfo, &info, sz)) {
  1809. ret = -EFAULT;
  1810. goto bail;
  1811. }
  1812. ret = 0;
  1813. bail:
  1814. return ret;
  1815. }
  1816. static int ipath_get_slave_info(struct ipath_portdata *pd,
  1817. void __user *slave_mask_addr)
  1818. {
  1819. int ret = 0;
  1820. if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
  1821. ret = -EFAULT;
  1822. return ret;
  1823. }
  1824. static ssize_t ipath_write(struct file *fp, const char __user *data,
  1825. size_t count, loff_t *off)
  1826. {
  1827. const struct ipath_cmd __user *ucmd;
  1828. struct ipath_portdata *pd;
  1829. const void __user *src;
  1830. size_t consumed, copy;
  1831. struct ipath_cmd cmd;
  1832. ssize_t ret = 0;
  1833. void *dest;
  1834. if (count < sizeof(cmd.type)) {
  1835. ret = -EINVAL;
  1836. goto bail;
  1837. }
  1838. ucmd = (const struct ipath_cmd __user *) data;
  1839. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1840. ret = -EFAULT;
  1841. goto bail;
  1842. }
  1843. consumed = sizeof(cmd.type);
  1844. switch (cmd.type) {
  1845. case IPATH_CMD_USER_INIT:
  1846. copy = sizeof(cmd.cmd.user_info);
  1847. dest = &cmd.cmd.user_info;
  1848. src = &ucmd->cmd.user_info;
  1849. break;
  1850. case IPATH_CMD_RECV_CTRL:
  1851. copy = sizeof(cmd.cmd.recv_ctrl);
  1852. dest = &cmd.cmd.recv_ctrl;
  1853. src = &ucmd->cmd.recv_ctrl;
  1854. break;
  1855. case IPATH_CMD_PORT_INFO:
  1856. copy = sizeof(cmd.cmd.port_info);
  1857. dest = &cmd.cmd.port_info;
  1858. src = &ucmd->cmd.port_info;
  1859. break;
  1860. case IPATH_CMD_TID_UPDATE:
  1861. case IPATH_CMD_TID_FREE:
  1862. copy = sizeof(cmd.cmd.tid_info);
  1863. dest = &cmd.cmd.tid_info;
  1864. src = &ucmd->cmd.tid_info;
  1865. break;
  1866. case IPATH_CMD_SET_PART_KEY:
  1867. copy = sizeof(cmd.cmd.part_key);
  1868. dest = &cmd.cmd.part_key;
  1869. src = &ucmd->cmd.part_key;
  1870. break;
  1871. case IPATH_CMD_SLAVE_INFO:
  1872. copy = sizeof(cmd.cmd.slave_mask_addr);
  1873. dest = &cmd.cmd.slave_mask_addr;
  1874. src = &ucmd->cmd.slave_mask_addr;
  1875. break;
  1876. default:
  1877. ret = -EINVAL;
  1878. goto bail;
  1879. }
  1880. if ((count - consumed) < copy) {
  1881. ret = -EINVAL;
  1882. goto bail;
  1883. }
  1884. if (copy_from_user(dest, src, copy)) {
  1885. ret = -EFAULT;
  1886. goto bail;
  1887. }
  1888. consumed += copy;
  1889. pd = port_fp(fp);
  1890. if (!pd && cmd.type != IPATH_CMD_USER_INIT) {
  1891. ret = -EINVAL;
  1892. goto bail;
  1893. }
  1894. switch (cmd.type) {
  1895. case IPATH_CMD_USER_INIT:
  1896. ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
  1897. if (ret)
  1898. goto bail;
  1899. ret = ipath_get_base_info(
  1900. fp, (void __user *) (unsigned long)
  1901. cmd.cmd.user_info.spu_base_info,
  1902. cmd.cmd.user_info.spu_base_info_size);
  1903. break;
  1904. case IPATH_CMD_RECV_CTRL:
  1905. ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
  1906. break;
  1907. case IPATH_CMD_PORT_INFO:
  1908. ret = ipath_port_info(pd, subport_fp(fp),
  1909. (struct ipath_port_info __user *)
  1910. (unsigned long) cmd.cmd.port_info);
  1911. break;
  1912. case IPATH_CMD_TID_UPDATE:
  1913. ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
  1914. break;
  1915. case IPATH_CMD_TID_FREE:
  1916. ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
  1917. break;
  1918. case IPATH_CMD_SET_PART_KEY:
  1919. ret = ipath_set_part_key(pd, cmd.cmd.part_key);
  1920. break;
  1921. case IPATH_CMD_SLAVE_INFO:
  1922. ret = ipath_get_slave_info(pd,
  1923. (void __user *) (unsigned long)
  1924. cmd.cmd.slave_mask_addr);
  1925. break;
  1926. }
  1927. if (ret >= 0)
  1928. ret = consumed;
  1929. bail:
  1930. return ret;
  1931. }
  1932. static struct class *ipath_class;
  1933. static int init_cdev(int minor, char *name, struct file_operations *fops,
  1934. struct cdev **cdevp, struct class_device **class_devp)
  1935. {
  1936. const dev_t dev = MKDEV(IPATH_MAJOR, minor);
  1937. struct cdev *cdev = NULL;
  1938. struct class_device *class_dev = NULL;
  1939. int ret;
  1940. cdev = cdev_alloc();
  1941. if (!cdev) {
  1942. printk(KERN_ERR IPATH_DRV_NAME
  1943. ": Could not allocate cdev for minor %d, %s\n",
  1944. minor, name);
  1945. ret = -ENOMEM;
  1946. goto done;
  1947. }
  1948. cdev->owner = THIS_MODULE;
  1949. cdev->ops = fops;
  1950. kobject_set_name(&cdev->kobj, name);
  1951. ret = cdev_add(cdev, dev, 1);
  1952. if (ret < 0) {
  1953. printk(KERN_ERR IPATH_DRV_NAME
  1954. ": Could not add cdev for minor %d, %s (err %d)\n",
  1955. minor, name, -ret);
  1956. goto err_cdev;
  1957. }
  1958. class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
  1959. if (IS_ERR(class_dev)) {
  1960. ret = PTR_ERR(class_dev);
  1961. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  1962. "class_dev for minor %d, %s (err %d)\n",
  1963. minor, name, -ret);
  1964. goto err_cdev;
  1965. }
  1966. goto done;
  1967. err_cdev:
  1968. cdev_del(cdev);
  1969. cdev = NULL;
  1970. done:
  1971. if (ret >= 0) {
  1972. *cdevp = cdev;
  1973. *class_devp = class_dev;
  1974. } else {
  1975. *cdevp = NULL;
  1976. *class_devp = NULL;
  1977. }
  1978. return ret;
  1979. }
  1980. int ipath_cdev_init(int minor, char *name, struct file_operations *fops,
  1981. struct cdev **cdevp, struct class_device **class_devp)
  1982. {
  1983. return init_cdev(minor, name, fops, cdevp, class_devp);
  1984. }
  1985. static void cleanup_cdev(struct cdev **cdevp,
  1986. struct class_device **class_devp)
  1987. {
  1988. struct class_device *class_dev = *class_devp;
  1989. if (class_dev) {
  1990. class_device_unregister(class_dev);
  1991. *class_devp = NULL;
  1992. }
  1993. if (*cdevp) {
  1994. cdev_del(*cdevp);
  1995. *cdevp = NULL;
  1996. }
  1997. }
  1998. void ipath_cdev_cleanup(struct cdev **cdevp,
  1999. struct class_device **class_devp)
  2000. {
  2001. cleanup_cdev(cdevp, class_devp);
  2002. }
  2003. static struct cdev *wildcard_cdev;
  2004. static struct class_device *wildcard_class_dev;
  2005. static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
  2006. static int user_init(void)
  2007. {
  2008. int ret;
  2009. ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
  2010. if (ret < 0) {
  2011. printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
  2012. "chrdev region (err %d)\n", -ret);
  2013. goto done;
  2014. }
  2015. ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
  2016. if (IS_ERR(ipath_class)) {
  2017. ret = PTR_ERR(ipath_class);
  2018. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  2019. "device class (err %d)\n", -ret);
  2020. goto bail;
  2021. }
  2022. goto done;
  2023. bail:
  2024. unregister_chrdev_region(dev, IPATH_NMINORS);
  2025. done:
  2026. return ret;
  2027. }
  2028. static void user_cleanup(void)
  2029. {
  2030. if (ipath_class) {
  2031. class_destroy(ipath_class);
  2032. ipath_class = NULL;
  2033. }
  2034. unregister_chrdev_region(dev, IPATH_NMINORS);
  2035. }
  2036. static atomic_t user_count = ATOMIC_INIT(0);
  2037. static atomic_t user_setup = ATOMIC_INIT(0);
  2038. int ipath_user_add(struct ipath_devdata *dd)
  2039. {
  2040. char name[10];
  2041. int ret;
  2042. if (atomic_inc_return(&user_count) == 1) {
  2043. ret = user_init();
  2044. if (ret < 0) {
  2045. ipath_dev_err(dd, "Unable to set up user support: "
  2046. "error %d\n", -ret);
  2047. goto bail;
  2048. }
  2049. ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
  2050. &wildcard_class_dev);
  2051. if (ret < 0) {
  2052. ipath_dev_err(dd, "Could not create wildcard "
  2053. "minor: error %d\n", -ret);
  2054. goto bail_user;
  2055. }
  2056. atomic_set(&user_setup, 1);
  2057. }
  2058. snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
  2059. ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
  2060. &dd->user_cdev, &dd->user_class_dev);
  2061. if (ret < 0)
  2062. ipath_dev_err(dd, "Could not create user minor %d, %s\n",
  2063. dd->ipath_unit + 1, name);
  2064. goto bail;
  2065. bail_user:
  2066. user_cleanup();
  2067. bail:
  2068. return ret;
  2069. }
  2070. void ipath_user_remove(struct ipath_devdata *dd)
  2071. {
  2072. cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
  2073. if (atomic_dec_return(&user_count) == 0) {
  2074. if (atomic_read(&user_setup) == 0)
  2075. goto bail;
  2076. cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
  2077. user_cleanup();
  2078. atomic_set(&user_setup, 0);
  2079. }
  2080. bail:
  2081. return;
  2082. }