ipath_file_ops.c 72 KB

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