ipath_file_ops.c 72 KB

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