ipath_file_ops.c 72 KB

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