ipath_file_ops.c 67 KB

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