ipath_file_ops.c 72 KB

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