ipath_file_ops.c 65 KB

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