ipath_file_ops.c 70 KB

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