ipath_file_ops.c 64 KB

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