ipath_file_ops.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618
  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/smp_lock.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 = vmalloc(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 = vmalloc(size);
  1405. if (!pd->subport_rcvhdr_base) {
  1406. ret = -ENOMEM;
  1407. goto bail_ureg;
  1408. }
  1409. pd->subport_rcvegrbuf = vmalloc(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. memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
  1421. memset(pd->subport_rcvhdr_base, 0, size);
  1422. memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
  1423. pd->port_rcvegrbuf_size *
  1424. num_subports);
  1425. goto bail;
  1426. bail_rhdr:
  1427. vfree(pd->subport_rcvhdr_base);
  1428. bail_ureg:
  1429. vfree(pd->subport_uregbase);
  1430. pd->subport_uregbase = NULL;
  1431. bail:
  1432. return ret;
  1433. }
  1434. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1435. struct file *fp,
  1436. const struct ipath_user_info *uinfo)
  1437. {
  1438. struct ipath_portdata *pd;
  1439. int ret;
  1440. if (!(pd = dd->ipath_pd[port])) {
  1441. void *ptmp;
  1442. pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1443. /*
  1444. * Allocate memory for use in ipath_tid_update() just once
  1445. * at open, not per call. Reduces cost of expected send
  1446. * setup.
  1447. */
  1448. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1449. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1450. GFP_KERNEL);
  1451. if (!pd || !ptmp) {
  1452. ipath_dev_err(dd, "Unable to allocate portdata "
  1453. "memory, failing open\n");
  1454. ret = -ENOMEM;
  1455. kfree(pd);
  1456. kfree(ptmp);
  1457. goto bail;
  1458. }
  1459. dd->ipath_pd[port] = pd;
  1460. dd->ipath_pd[port]->port_port = port;
  1461. dd->ipath_pd[port]->port_dd = dd;
  1462. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1463. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1464. }
  1465. if (!pd->port_cnt) {
  1466. pd->userversion = uinfo->spu_userversion;
  1467. init_user_egr_sizes(pd);
  1468. if ((ret = init_subports(dd, pd, uinfo)) != 0)
  1469. goto bail;
  1470. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1471. current->comm, current->pid, dd->ipath_unit,
  1472. port);
  1473. pd->port_cnt = 1;
  1474. port_fp(fp) = pd;
  1475. pd->port_pid = get_pid(task_pid(current));
  1476. strlcpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
  1477. ipath_stats.sps_ports++;
  1478. ret = 0;
  1479. } else
  1480. ret = -EBUSY;
  1481. bail:
  1482. return ret;
  1483. }
  1484. static inline int usable(struct ipath_devdata *dd)
  1485. {
  1486. return dd &&
  1487. (dd->ipath_flags & IPATH_PRESENT) &&
  1488. dd->ipath_kregbase &&
  1489. dd->ipath_lid &&
  1490. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1491. | IPATH_LINKUNK));
  1492. }
  1493. static int find_free_port(int unit, struct file *fp,
  1494. const struct ipath_user_info *uinfo)
  1495. {
  1496. struct ipath_devdata *dd = ipath_lookup(unit);
  1497. int ret, i;
  1498. if (!dd) {
  1499. ret = -ENODEV;
  1500. goto bail;
  1501. }
  1502. if (!usable(dd)) {
  1503. ret = -ENETDOWN;
  1504. goto bail;
  1505. }
  1506. for (i = 1; i < dd->ipath_cfgports; i++) {
  1507. ret = try_alloc_port(dd, i, fp, uinfo);
  1508. if (ret != -EBUSY)
  1509. goto bail;
  1510. }
  1511. ret = -EBUSY;
  1512. bail:
  1513. return ret;
  1514. }
  1515. static int find_best_unit(struct file *fp,
  1516. const struct ipath_user_info *uinfo)
  1517. {
  1518. int ret = 0, i, prefunit = -1, devmax;
  1519. int maxofallports, npresent, nup;
  1520. int ndev;
  1521. devmax = ipath_count_units(&npresent, &nup, &maxofallports);
  1522. /*
  1523. * This code is present to allow a knowledgeable person to
  1524. * specify the layout of processes to processors before opening
  1525. * this driver, and then we'll assign the process to the "closest"
  1526. * InfiniPath chip to that processor (we assume reasonable connectivity,
  1527. * for now). This code assumes that if affinity has been set
  1528. * before this point, that at most one cpu is set; for now this
  1529. * is reasonable. I check for both cpumask_empty() and cpumask_full(),
  1530. * in case some kernel variant sets none of the bits when no
  1531. * affinity is set. 2.6.11 and 12 kernels have all present
  1532. * cpus set. Some day we'll have to fix it up further to handle
  1533. * a cpu subset. This algorithm fails for two HT chips connected
  1534. * in tunnel fashion. Eventually this needs real topology
  1535. * information. There may be some issues with dual core numbering
  1536. * as well. This needs more work prior to release.
  1537. */
  1538. if (!cpumask_empty(&current->cpus_allowed) &&
  1539. !cpumask_full(&current->cpus_allowed)) {
  1540. int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
  1541. for (i = 0; i < ncpus; i++)
  1542. if (cpumask_test_cpu(i, &current->cpus_allowed)) {
  1543. ipath_cdbg(PROC, "%s[%u] affinity set for "
  1544. "cpu %d/%d\n", current->comm,
  1545. current->pid, i, ncpus);
  1546. curcpu = i;
  1547. nset++;
  1548. }
  1549. if (curcpu != -1 && nset != ncpus) {
  1550. if (npresent) {
  1551. prefunit = curcpu / (ncpus / npresent);
  1552. ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
  1553. "%d cpus/chip, select unit %d\n",
  1554. current->comm, current->pid,
  1555. npresent, ncpus, ncpus / npresent,
  1556. prefunit);
  1557. }
  1558. }
  1559. }
  1560. /*
  1561. * user ports start at 1, kernel port is 0
  1562. * For now, we do round-robin access across all chips
  1563. */
  1564. if (prefunit != -1)
  1565. devmax = prefunit + 1;
  1566. recheck:
  1567. for (i = 1; i < maxofallports; i++) {
  1568. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1569. ndev++) {
  1570. struct ipath_devdata *dd = ipath_lookup(ndev);
  1571. if (!usable(dd))
  1572. continue; /* can't use this unit */
  1573. if (i >= dd->ipath_cfgports)
  1574. /*
  1575. * Maxed out on users of this unit. Try
  1576. * next.
  1577. */
  1578. continue;
  1579. ret = try_alloc_port(dd, i, fp, uinfo);
  1580. if (!ret)
  1581. goto done;
  1582. }
  1583. }
  1584. if (npresent) {
  1585. if (nup == 0) {
  1586. ret = -ENETDOWN;
  1587. ipath_dbg("No ports available (none initialized "
  1588. "and ready)\n");
  1589. } else {
  1590. if (prefunit > 0) {
  1591. /* if started above 0, retry from 0 */
  1592. ipath_cdbg(PROC,
  1593. "%s[%u] no ports on prefunit "
  1594. "%d, clear and re-check\n",
  1595. current->comm, current->pid,
  1596. prefunit);
  1597. devmax = ipath_count_units(NULL, NULL,
  1598. NULL);
  1599. prefunit = -1;
  1600. goto recheck;
  1601. }
  1602. ret = -EBUSY;
  1603. ipath_dbg("No ports available\n");
  1604. }
  1605. } else {
  1606. ret = -ENXIO;
  1607. ipath_dbg("No boards found\n");
  1608. }
  1609. done:
  1610. return ret;
  1611. }
  1612. static int find_shared_port(struct file *fp,
  1613. const struct ipath_user_info *uinfo)
  1614. {
  1615. int devmax, ndev, i;
  1616. int ret = 0;
  1617. devmax = ipath_count_units(NULL, NULL, NULL);
  1618. for (ndev = 0; ndev < devmax; ndev++) {
  1619. struct ipath_devdata *dd = ipath_lookup(ndev);
  1620. if (!usable(dd))
  1621. continue;
  1622. for (i = 1; i < dd->ipath_cfgports; i++) {
  1623. struct ipath_portdata *pd = dd->ipath_pd[i];
  1624. /* Skip ports which are not yet open */
  1625. if (!pd || !pd->port_cnt)
  1626. continue;
  1627. /* Skip port if it doesn't match the requested one */
  1628. if (pd->port_subport_id != uinfo->spu_subport_id)
  1629. continue;
  1630. /* Verify the sharing process matches the master */
  1631. if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
  1632. pd->userversion != uinfo->spu_userversion ||
  1633. pd->port_cnt >= pd->port_subport_cnt) {
  1634. ret = -EINVAL;
  1635. goto done;
  1636. }
  1637. port_fp(fp) = pd;
  1638. subport_fp(fp) = pd->port_cnt++;
  1639. pd->port_subpid[subport_fp(fp)] =
  1640. get_pid(task_pid(current));
  1641. tidcursor_fp(fp) = 0;
  1642. pd->active_slaves |= 1 << subport_fp(fp);
  1643. ipath_cdbg(PROC,
  1644. "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
  1645. current->comm, current->pid,
  1646. subport_fp(fp),
  1647. pd->port_comm, pid_nr(pd->port_pid),
  1648. dd->ipath_unit, pd->port_port);
  1649. ret = 1;
  1650. goto done;
  1651. }
  1652. }
  1653. done:
  1654. return ret;
  1655. }
  1656. static int ipath_open(struct inode *in, struct file *fp)
  1657. {
  1658. /* The real work is performed later in ipath_assign_port() */
  1659. fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
  1660. return fp->private_data ? 0 : -ENOMEM;
  1661. }
  1662. /* Get port early, so can set affinity prior to memory allocation */
  1663. static int ipath_assign_port(struct file *fp,
  1664. const struct ipath_user_info *uinfo)
  1665. {
  1666. int ret;
  1667. int i_minor;
  1668. unsigned swmajor, swminor;
  1669. /* Check to be sure we haven't already initialized this file */
  1670. if (port_fp(fp)) {
  1671. ret = -EINVAL;
  1672. goto done;
  1673. }
  1674. /* for now, if major version is different, bail */
  1675. swmajor = uinfo->spu_userversion >> 16;
  1676. if (swmajor != IPATH_USER_SWMAJOR) {
  1677. ipath_dbg("User major version %d not same as driver "
  1678. "major %d\n", uinfo->spu_userversion >> 16,
  1679. IPATH_USER_SWMAJOR);
  1680. ret = -ENODEV;
  1681. goto done;
  1682. }
  1683. swminor = uinfo->spu_userversion & 0xffff;
  1684. if (swminor != IPATH_USER_SWMINOR)
  1685. ipath_dbg("User minor version %d not same as driver "
  1686. "minor %d\n", swminor, IPATH_USER_SWMINOR);
  1687. mutex_lock(&ipath_mutex);
  1688. if (ipath_compatible_subports(swmajor, swminor) &&
  1689. uinfo->spu_subport_cnt &&
  1690. (ret = find_shared_port(fp, uinfo))) {
  1691. if (ret > 0)
  1692. ret = 0;
  1693. goto done_chk_sdma;
  1694. }
  1695. i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
  1696. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1697. (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
  1698. if (i_minor)
  1699. ret = find_free_port(i_minor - 1, fp, uinfo);
  1700. else
  1701. ret = find_best_unit(fp, uinfo);
  1702. done_chk_sdma:
  1703. if (!ret) {
  1704. struct ipath_filedata *fd = fp->private_data;
  1705. const struct ipath_portdata *pd = fd->pd;
  1706. const struct ipath_devdata *dd = pd->port_dd;
  1707. fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,
  1708. dd->ipath_unit,
  1709. pd->port_port,
  1710. fd->subport);
  1711. if (!fd->pq)
  1712. ret = -ENOMEM;
  1713. }
  1714. mutex_unlock(&ipath_mutex);
  1715. done:
  1716. return ret;
  1717. }
  1718. static int ipath_do_user_init(struct file *fp,
  1719. const struct ipath_user_info *uinfo)
  1720. {
  1721. int ret;
  1722. struct ipath_portdata *pd = port_fp(fp);
  1723. struct ipath_devdata *dd;
  1724. u32 head32;
  1725. /* Subports don't need to initialize anything since master did it. */
  1726. if (subport_fp(fp)) {
  1727. ret = wait_event_interruptible(pd->port_wait,
  1728. !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
  1729. goto done;
  1730. }
  1731. dd = pd->port_dd;
  1732. if (uinfo->spu_rcvhdrsize) {
  1733. ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
  1734. if (ret)
  1735. goto done;
  1736. }
  1737. /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
  1738. /* some ports may get extra buffers, calculate that here */
  1739. if (pd->port_port <= dd->ipath_ports_extrabuf)
  1740. pd->port_piocnt = dd->ipath_pbufsport + 1;
  1741. else
  1742. pd->port_piocnt = dd->ipath_pbufsport;
  1743. /* for right now, kernel piobufs are at end, so port 1 is at 0 */
  1744. if (pd->port_port <= dd->ipath_ports_extrabuf)
  1745. pd->port_pio_base = (dd->ipath_pbufsport + 1)
  1746. * (pd->port_port - 1);
  1747. else
  1748. pd->port_pio_base = dd->ipath_ports_extrabuf +
  1749. dd->ipath_pbufsport * (pd->port_port - 1);
  1750. pd->port_piobufs = dd->ipath_piobufbase +
  1751. pd->port_pio_base * dd->ipath_palign;
  1752. ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"
  1753. " first pio %u\n", pd->port_port, pd->port_piobufs,
  1754. pd->port_piocnt, pd->port_pio_base);
  1755. ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);
  1756. /*
  1757. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  1758. * array for time being. If pd->port_port > chip-supported,
  1759. * we need to do extra stuff here to handle by handling overflow
  1760. * through port 0, someday
  1761. */
  1762. ret = ipath_create_rcvhdrq(dd, pd);
  1763. if (!ret)
  1764. ret = ipath_create_user_egr(pd);
  1765. if (ret)
  1766. goto done;
  1767. /*
  1768. * set the eager head register for this port to the current values
  1769. * of the tail pointers, since we don't know if they were
  1770. * updated on last use of the port.
  1771. */
  1772. head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
  1773. ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
  1774. pd->port_lastrcvhdrqtail = -1;
  1775. ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
  1776. pd->port_port, head32);
  1777. pd->port_tidcursor = 0; /* start at beginning after open */
  1778. /* initialize poll variables... */
  1779. pd->port_urgent = 0;
  1780. pd->port_urgent_poll = 0;
  1781. pd->port_hdrqfull_poll = pd->port_hdrqfull;
  1782. /*
  1783. * Now enable the port for receive.
  1784. * For chips that are set to DMA the tail register to memory
  1785. * when they change (and when the update bit transitions from
  1786. * 0 to 1. So for those chips, we turn it off and then back on.
  1787. * This will (very briefly) affect any other open ports, but the
  1788. * duration is very short, and therefore isn't an issue. We
  1789. * explictly set the in-memory tail copy to 0 beforehand, so we
  1790. * don't have to wait to be sure the DMA update has happened
  1791. * (chip resets head/tail to 0 on transition to enable).
  1792. */
  1793. set_bit(dd->ipath_r_portenable_shift + pd->port_port,
  1794. &dd->ipath_rcvctrl);
  1795. if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
  1796. if (pd->port_rcvhdrtail_kvaddr)
  1797. ipath_clear_rcvhdrtail(pd);
  1798. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1799. dd->ipath_rcvctrl &
  1800. ~(1ULL << dd->ipath_r_tailupd_shift));
  1801. }
  1802. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1803. dd->ipath_rcvctrl);
  1804. /* Notify any waiting slaves */
  1805. if (pd->port_subport_cnt) {
  1806. clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
  1807. wake_up(&pd->port_wait);
  1808. }
  1809. done:
  1810. return ret;
  1811. }
  1812. /**
  1813. * unlock_exptid - unlock any expected TID entries port still had in use
  1814. * @pd: port
  1815. *
  1816. * We don't actually update the chip here, because we do a bulk update
  1817. * below, using ipath_f_clear_tids.
  1818. */
  1819. static void unlock_expected_tids(struct ipath_portdata *pd)
  1820. {
  1821. struct ipath_devdata *dd = pd->port_dd;
  1822. int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
  1823. int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1824. ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
  1825. pd->port_port);
  1826. for (i = port_tidbase; i < maxtid; i++) {
  1827. struct page *ps = dd->ipath_pageshadow[i];
  1828. if (!ps)
  1829. continue;
  1830. dd->ipath_pageshadow[i] = NULL;
  1831. pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
  1832. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1833. ipath_release_user_pages_on_close(&ps, 1);
  1834. cnt++;
  1835. ipath_stats.sps_pageunlocks++;
  1836. }
  1837. if (cnt)
  1838. ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
  1839. pd->port_port, cnt);
  1840. if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
  1841. ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
  1842. (unsigned long long) ipath_stats.sps_pagelocks,
  1843. (unsigned long long)
  1844. ipath_stats.sps_pageunlocks);
  1845. }
  1846. static int ipath_close(struct inode *in, struct file *fp)
  1847. {
  1848. int ret = 0;
  1849. struct ipath_filedata *fd;
  1850. struct ipath_portdata *pd;
  1851. struct ipath_devdata *dd;
  1852. unsigned long flags;
  1853. unsigned port;
  1854. struct pid *pid;
  1855. ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
  1856. (long)in->i_rdev, fp->private_data);
  1857. mutex_lock(&ipath_mutex);
  1858. fd = fp->private_data;
  1859. fp->private_data = NULL;
  1860. pd = fd->pd;
  1861. if (!pd) {
  1862. mutex_unlock(&ipath_mutex);
  1863. goto bail;
  1864. }
  1865. dd = pd->port_dd;
  1866. /* drain user sdma queue */
  1867. ipath_user_sdma_queue_drain(dd, fd->pq);
  1868. ipath_user_sdma_queue_destroy(fd->pq);
  1869. if (--pd->port_cnt) {
  1870. /*
  1871. * XXX If the master closes the port before the slave(s),
  1872. * revoke the mmap for the eager receive queue so
  1873. * the slave(s) don't wait for receive data forever.
  1874. */
  1875. pd->active_slaves &= ~(1 << fd->subport);
  1876. put_pid(pd->port_subpid[fd->subport]);
  1877. pd->port_subpid[fd->subport] = NULL;
  1878. mutex_unlock(&ipath_mutex);
  1879. goto bail;
  1880. }
  1881. /* early; no interrupt users after this */
  1882. spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
  1883. port = pd->port_port;
  1884. dd->ipath_pd[port] = NULL;
  1885. pid = pd->port_pid;
  1886. pd->port_pid = NULL;
  1887. spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
  1888. if (pd->port_rcvwait_to || pd->port_piowait_to
  1889. || pd->port_rcvnowait || pd->port_pionowait) {
  1890. ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
  1891. "%u rcv %u, pio already\n",
  1892. pd->port_port, pd->port_rcvwait_to,
  1893. pd->port_piowait_to, pd->port_rcvnowait,
  1894. pd->port_pionowait);
  1895. pd->port_rcvwait_to = pd->port_piowait_to =
  1896. pd->port_rcvnowait = pd->port_pionowait = 0;
  1897. }
  1898. if (pd->port_flag) {
  1899. ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",
  1900. pd->port_port, pd->port_flag);
  1901. pd->port_flag = 0;
  1902. }
  1903. if (dd->ipath_kregbase) {
  1904. /* atomically clear receive enable port and intr avail. */
  1905. clear_bit(dd->ipath_r_portenable_shift + port,
  1906. &dd->ipath_rcvctrl);
  1907. clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
  1908. &dd->ipath_rcvctrl);
  1909. ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
  1910. dd->ipath_rcvctrl);
  1911. /* and read back from chip to be sure that nothing
  1912. * else is in flight when we do the rest */
  1913. (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1914. /* clean up the pkeys for this port user */
  1915. ipath_clean_part_key(pd, dd);
  1916. /*
  1917. * be paranoid, and never write 0's to these, just use an
  1918. * unused part of the port 0 tail page. Of course,
  1919. * rcvhdraddr points to a large chunk of memory, so this
  1920. * could still trash things, but at least it won't trash
  1921. * page 0, and by disabling the port, it should stop "soon",
  1922. * even if a packet or two is in already in flight after we
  1923. * disabled the port.
  1924. */
  1925. ipath_write_kreg_port(dd,
  1926. dd->ipath_kregs->kr_rcvhdrtailaddr, port,
  1927. dd->ipath_dummy_hdrq_phys);
  1928. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1929. pd->port_port, dd->ipath_dummy_hdrq_phys);
  1930. ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);
  1931. ipath_chg_pioavailkernel(dd, pd->port_pio_base,
  1932. pd->port_piocnt, 1);
  1933. dd->ipath_f_clear_tids(dd, pd->port_port);
  1934. if (dd->ipath_pageshadow)
  1935. unlock_expected_tids(pd);
  1936. ipath_stats.sps_ports--;
  1937. ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
  1938. pd->port_comm, pid_nr(pid),
  1939. dd->ipath_unit, port);
  1940. }
  1941. put_pid(pid);
  1942. mutex_unlock(&ipath_mutex);
  1943. ipath_free_pddata(dd, pd); /* after releasing the mutex */
  1944. bail:
  1945. kfree(fd);
  1946. return ret;
  1947. }
  1948. static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
  1949. struct ipath_port_info __user *uinfo)
  1950. {
  1951. struct ipath_port_info info;
  1952. int nup;
  1953. int ret;
  1954. size_t sz;
  1955. (void) ipath_count_units(NULL, &nup, NULL);
  1956. info.num_active = nup;
  1957. info.unit = pd->port_dd->ipath_unit;
  1958. info.port = pd->port_port;
  1959. info.subport = subport;
  1960. /* Don't return new fields if old library opened the port. */
  1961. if (ipath_supports_subports(pd->userversion >> 16,
  1962. pd->userversion & 0xffff)) {
  1963. /* Number of user ports available for this device. */
  1964. info.num_ports = pd->port_dd->ipath_cfgports - 1;
  1965. info.num_subports = pd->port_subport_cnt;
  1966. sz = sizeof(info);
  1967. } else
  1968. sz = sizeof(info) - 2 * sizeof(u16);
  1969. if (copy_to_user(uinfo, &info, sz)) {
  1970. ret = -EFAULT;
  1971. goto bail;
  1972. }
  1973. ret = 0;
  1974. bail:
  1975. return ret;
  1976. }
  1977. static int ipath_get_slave_info(struct ipath_portdata *pd,
  1978. void __user *slave_mask_addr)
  1979. {
  1980. int ret = 0;
  1981. if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
  1982. ret = -EFAULT;
  1983. return ret;
  1984. }
  1985. static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,
  1986. u32 __user *inflightp)
  1987. {
  1988. const u32 val = ipath_user_sdma_inflight_counter(pq);
  1989. if (put_user(val, inflightp))
  1990. return -EFAULT;
  1991. return 0;
  1992. }
  1993. static int ipath_sdma_get_complete(struct ipath_devdata *dd,
  1994. struct ipath_user_sdma_queue *pq,
  1995. u32 __user *completep)
  1996. {
  1997. u32 val;
  1998. int err;
  1999. err = ipath_user_sdma_make_progress(dd, pq);
  2000. if (err < 0)
  2001. return err;
  2002. val = ipath_user_sdma_complete_counter(pq);
  2003. if (put_user(val, completep))
  2004. return -EFAULT;
  2005. return 0;
  2006. }
  2007. static ssize_t ipath_write(struct file *fp, const char __user *data,
  2008. size_t count, loff_t *off)
  2009. {
  2010. const struct ipath_cmd __user *ucmd;
  2011. struct ipath_portdata *pd;
  2012. const void __user *src;
  2013. size_t consumed, copy;
  2014. struct ipath_cmd cmd;
  2015. ssize_t ret = 0;
  2016. void *dest;
  2017. if (count < sizeof(cmd.type)) {
  2018. ret = -EINVAL;
  2019. goto bail;
  2020. }
  2021. ucmd = (const struct ipath_cmd __user *) data;
  2022. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  2023. ret = -EFAULT;
  2024. goto bail;
  2025. }
  2026. consumed = sizeof(cmd.type);
  2027. switch (cmd.type) {
  2028. case IPATH_CMD_ASSIGN_PORT:
  2029. case __IPATH_CMD_USER_INIT:
  2030. case IPATH_CMD_USER_INIT:
  2031. copy = sizeof(cmd.cmd.user_info);
  2032. dest = &cmd.cmd.user_info;
  2033. src = &ucmd->cmd.user_info;
  2034. break;
  2035. case IPATH_CMD_RECV_CTRL:
  2036. copy = sizeof(cmd.cmd.recv_ctrl);
  2037. dest = &cmd.cmd.recv_ctrl;
  2038. src = &ucmd->cmd.recv_ctrl;
  2039. break;
  2040. case IPATH_CMD_PORT_INFO:
  2041. copy = sizeof(cmd.cmd.port_info);
  2042. dest = &cmd.cmd.port_info;
  2043. src = &ucmd->cmd.port_info;
  2044. break;
  2045. case IPATH_CMD_TID_UPDATE:
  2046. case IPATH_CMD_TID_FREE:
  2047. copy = sizeof(cmd.cmd.tid_info);
  2048. dest = &cmd.cmd.tid_info;
  2049. src = &ucmd->cmd.tid_info;
  2050. break;
  2051. case IPATH_CMD_SET_PART_KEY:
  2052. copy = sizeof(cmd.cmd.part_key);
  2053. dest = &cmd.cmd.part_key;
  2054. src = &ucmd->cmd.part_key;
  2055. break;
  2056. case __IPATH_CMD_SLAVE_INFO:
  2057. copy = sizeof(cmd.cmd.slave_mask_addr);
  2058. dest = &cmd.cmd.slave_mask_addr;
  2059. src = &ucmd->cmd.slave_mask_addr;
  2060. break;
  2061. case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
  2062. copy = 0;
  2063. src = NULL;
  2064. dest = NULL;
  2065. break;
  2066. case IPATH_CMD_POLL_TYPE:
  2067. copy = sizeof(cmd.cmd.poll_type);
  2068. dest = &cmd.cmd.poll_type;
  2069. src = &ucmd->cmd.poll_type;
  2070. break;
  2071. case IPATH_CMD_ARMLAUNCH_CTRL:
  2072. copy = sizeof(cmd.cmd.armlaunch_ctrl);
  2073. dest = &cmd.cmd.armlaunch_ctrl;
  2074. src = &ucmd->cmd.armlaunch_ctrl;
  2075. break;
  2076. case IPATH_CMD_SDMA_INFLIGHT:
  2077. copy = sizeof(cmd.cmd.sdma_inflight);
  2078. dest = &cmd.cmd.sdma_inflight;
  2079. src = &ucmd->cmd.sdma_inflight;
  2080. break;
  2081. case IPATH_CMD_SDMA_COMPLETE:
  2082. copy = sizeof(cmd.cmd.sdma_complete);
  2083. dest = &cmd.cmd.sdma_complete;
  2084. src = &ucmd->cmd.sdma_complete;
  2085. break;
  2086. default:
  2087. ret = -EINVAL;
  2088. goto bail;
  2089. }
  2090. if (copy) {
  2091. if ((count - consumed) < copy) {
  2092. ret = -EINVAL;
  2093. goto bail;
  2094. }
  2095. if (copy_from_user(dest, src, copy)) {
  2096. ret = -EFAULT;
  2097. goto bail;
  2098. }
  2099. consumed += copy;
  2100. }
  2101. pd = port_fp(fp);
  2102. if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
  2103. cmd.type != IPATH_CMD_ASSIGN_PORT) {
  2104. ret = -EINVAL;
  2105. goto bail;
  2106. }
  2107. switch (cmd.type) {
  2108. case IPATH_CMD_ASSIGN_PORT:
  2109. ret = ipath_assign_port(fp, &cmd.cmd.user_info);
  2110. if (ret)
  2111. goto bail;
  2112. break;
  2113. case __IPATH_CMD_USER_INIT:
  2114. /* backwards compatibility, get port first */
  2115. ret = ipath_assign_port(fp, &cmd.cmd.user_info);
  2116. if (ret)
  2117. goto bail;
  2118. /* and fall through to current version. */
  2119. case IPATH_CMD_USER_INIT:
  2120. ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
  2121. if (ret)
  2122. goto bail;
  2123. ret = ipath_get_base_info(
  2124. fp, (void __user *) (unsigned long)
  2125. cmd.cmd.user_info.spu_base_info,
  2126. cmd.cmd.user_info.spu_base_info_size);
  2127. break;
  2128. case IPATH_CMD_RECV_CTRL:
  2129. ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
  2130. break;
  2131. case IPATH_CMD_PORT_INFO:
  2132. ret = ipath_port_info(pd, subport_fp(fp),
  2133. (struct ipath_port_info __user *)
  2134. (unsigned long) cmd.cmd.port_info);
  2135. break;
  2136. case IPATH_CMD_TID_UPDATE:
  2137. ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
  2138. break;
  2139. case IPATH_CMD_TID_FREE:
  2140. ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
  2141. break;
  2142. case IPATH_CMD_SET_PART_KEY:
  2143. ret = ipath_set_part_key(pd, cmd.cmd.part_key);
  2144. break;
  2145. case __IPATH_CMD_SLAVE_INFO:
  2146. ret = ipath_get_slave_info(pd,
  2147. (void __user *) (unsigned long)
  2148. cmd.cmd.slave_mask_addr);
  2149. break;
  2150. case IPATH_CMD_PIOAVAILUPD:
  2151. ipath_force_pio_avail_update(pd->port_dd);
  2152. break;
  2153. case IPATH_CMD_POLL_TYPE:
  2154. pd->poll_type = cmd.cmd.poll_type;
  2155. break;
  2156. case IPATH_CMD_ARMLAUNCH_CTRL:
  2157. if (cmd.cmd.armlaunch_ctrl)
  2158. ipath_enable_armlaunch(pd->port_dd);
  2159. else
  2160. ipath_disable_armlaunch(pd->port_dd);
  2161. break;
  2162. case IPATH_CMD_SDMA_INFLIGHT:
  2163. ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),
  2164. (u32 __user *) (unsigned long)
  2165. cmd.cmd.sdma_inflight);
  2166. break;
  2167. case IPATH_CMD_SDMA_COMPLETE:
  2168. ret = ipath_sdma_get_complete(pd->port_dd,
  2169. user_sdma_queue_fp(fp),
  2170. (u32 __user *) (unsigned long)
  2171. cmd.cmd.sdma_complete);
  2172. break;
  2173. }
  2174. if (ret >= 0)
  2175. ret = consumed;
  2176. bail:
  2177. return ret;
  2178. }
  2179. static ssize_t ipath_writev(struct kiocb *iocb, const struct iovec *iov,
  2180. unsigned long dim, loff_t off)
  2181. {
  2182. struct file *filp = iocb->ki_filp;
  2183. struct ipath_filedata *fp = filp->private_data;
  2184. struct ipath_portdata *pd = port_fp(filp);
  2185. struct ipath_user_sdma_queue *pq = fp->pq;
  2186. if (!dim)
  2187. return -EINVAL;
  2188. return ipath_user_sdma_writev(pd->port_dd, pq, iov, dim);
  2189. }
  2190. static struct class *ipath_class;
  2191. static int init_cdev(int minor, char *name, const struct file_operations *fops,
  2192. struct cdev **cdevp, struct device **devp)
  2193. {
  2194. const dev_t dev = MKDEV(IPATH_MAJOR, minor);
  2195. struct cdev *cdev = NULL;
  2196. struct device *device = NULL;
  2197. int ret;
  2198. cdev = cdev_alloc();
  2199. if (!cdev) {
  2200. printk(KERN_ERR IPATH_DRV_NAME
  2201. ": Could not allocate cdev for minor %d, %s\n",
  2202. minor, name);
  2203. ret = -ENOMEM;
  2204. goto done;
  2205. }
  2206. cdev->owner = THIS_MODULE;
  2207. cdev->ops = fops;
  2208. kobject_set_name(&cdev->kobj, name);
  2209. ret = cdev_add(cdev, dev, 1);
  2210. if (ret < 0) {
  2211. printk(KERN_ERR IPATH_DRV_NAME
  2212. ": Could not add cdev for minor %d, %s (err %d)\n",
  2213. minor, name, -ret);
  2214. goto err_cdev;
  2215. }
  2216. device = device_create(ipath_class, NULL, dev, NULL, name);
  2217. if (IS_ERR(device)) {
  2218. ret = PTR_ERR(device);
  2219. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  2220. "device for minor %d, %s (err %d)\n",
  2221. minor, name, -ret);
  2222. goto err_cdev;
  2223. }
  2224. goto done;
  2225. err_cdev:
  2226. cdev_del(cdev);
  2227. cdev = NULL;
  2228. done:
  2229. if (ret >= 0) {
  2230. *cdevp = cdev;
  2231. *devp = device;
  2232. } else {
  2233. *cdevp = NULL;
  2234. *devp = NULL;
  2235. }
  2236. return ret;
  2237. }
  2238. int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
  2239. struct cdev **cdevp, struct device **devp)
  2240. {
  2241. return init_cdev(minor, name, fops, cdevp, devp);
  2242. }
  2243. static void cleanup_cdev(struct cdev **cdevp,
  2244. struct device **devp)
  2245. {
  2246. struct device *dev = *devp;
  2247. if (dev) {
  2248. device_unregister(dev);
  2249. *devp = NULL;
  2250. }
  2251. if (*cdevp) {
  2252. cdev_del(*cdevp);
  2253. *cdevp = NULL;
  2254. }
  2255. }
  2256. void ipath_cdev_cleanup(struct cdev **cdevp,
  2257. struct device **devp)
  2258. {
  2259. cleanup_cdev(cdevp, devp);
  2260. }
  2261. static struct cdev *wildcard_cdev;
  2262. static struct device *wildcard_dev;
  2263. static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
  2264. static int user_init(void)
  2265. {
  2266. int ret;
  2267. ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
  2268. if (ret < 0) {
  2269. printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
  2270. "chrdev region (err %d)\n", -ret);
  2271. goto done;
  2272. }
  2273. ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
  2274. if (IS_ERR(ipath_class)) {
  2275. ret = PTR_ERR(ipath_class);
  2276. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  2277. "device class (err %d)\n", -ret);
  2278. goto bail;
  2279. }
  2280. goto done;
  2281. bail:
  2282. unregister_chrdev_region(dev, IPATH_NMINORS);
  2283. done:
  2284. return ret;
  2285. }
  2286. static void user_cleanup(void)
  2287. {
  2288. if (ipath_class) {
  2289. class_destroy(ipath_class);
  2290. ipath_class = NULL;
  2291. }
  2292. unregister_chrdev_region(dev, IPATH_NMINORS);
  2293. }
  2294. static atomic_t user_count = ATOMIC_INIT(0);
  2295. static atomic_t user_setup = ATOMIC_INIT(0);
  2296. int ipath_user_add(struct ipath_devdata *dd)
  2297. {
  2298. char name[10];
  2299. int ret;
  2300. if (atomic_inc_return(&user_count) == 1) {
  2301. ret = user_init();
  2302. if (ret < 0) {
  2303. ipath_dev_err(dd, "Unable to set up user support: "
  2304. "error %d\n", -ret);
  2305. goto bail;
  2306. }
  2307. ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
  2308. &wildcard_dev);
  2309. if (ret < 0) {
  2310. ipath_dev_err(dd, "Could not create wildcard "
  2311. "minor: error %d\n", -ret);
  2312. goto bail_user;
  2313. }
  2314. atomic_set(&user_setup, 1);
  2315. }
  2316. snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
  2317. ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
  2318. &dd->user_cdev, &dd->user_dev);
  2319. if (ret < 0)
  2320. ipath_dev_err(dd, "Could not create user minor %d, %s\n",
  2321. dd->ipath_unit + 1, name);
  2322. goto bail;
  2323. bail_user:
  2324. user_cleanup();
  2325. bail:
  2326. return ret;
  2327. }
  2328. void ipath_user_remove(struct ipath_devdata *dd)
  2329. {
  2330. cleanup_cdev(&dd->user_cdev, &dd->user_dev);
  2331. if (atomic_dec_return(&user_count) == 0) {
  2332. if (atomic_read(&user_setup) == 0)
  2333. goto bail;
  2334. cleanup_cdev(&wildcard_cdev, &wildcard_dev);
  2335. user_cleanup();
  2336. atomic_set(&user_setup, 0);
  2337. }
  2338. bail:
  2339. return;
  2340. }