ipath_file_ops.c 69 KB

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