ipath_file_ops.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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 "ips_common.h"
  41. #include "ipath_layer.h"
  42. static int ipath_open(struct inode *, struct file *);
  43. static int ipath_close(struct inode *, struct file *);
  44. static ssize_t ipath_write(struct file *, const char __user *, size_t,
  45. loff_t *);
  46. static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
  47. static int ipath_mmap(struct file *, struct vm_area_struct *);
  48. static struct file_operations ipath_file_ops = {
  49. .owner = THIS_MODULE,
  50. .write = ipath_write,
  51. .open = ipath_open,
  52. .release = ipath_close,
  53. .poll = ipath_poll,
  54. .mmap = ipath_mmap
  55. };
  56. static int ipath_get_base_info(struct ipath_portdata *pd,
  57. void __user *ubase, size_t ubase_size)
  58. {
  59. int ret = 0;
  60. struct ipath_base_info *kinfo = NULL;
  61. struct ipath_devdata *dd = pd->port_dd;
  62. if (ubase_size < sizeof(*kinfo)) {
  63. ipath_cdbg(PROC,
  64. "Base size %lu, need %lu (version mismatch?)\n",
  65. (unsigned long) ubase_size,
  66. (unsigned long) sizeof(*kinfo));
  67. ret = -EINVAL;
  68. goto bail;
  69. }
  70. kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
  71. if (kinfo == NULL) {
  72. ret = -ENOMEM;
  73. goto bail;
  74. }
  75. ret = dd->ipath_f_get_base_info(pd, kinfo);
  76. if (ret < 0)
  77. goto bail;
  78. kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
  79. kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
  80. kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
  81. kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
  82. /*
  83. * have to mmap whole thing
  84. */
  85. kinfo->spi_rcv_egrbuftotlen =
  86. pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  87. kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
  88. kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
  89. pd->port_rcvegrbuf_chunks;
  90. kinfo->spi_tidcnt = dd->ipath_rcvtidcnt;
  91. /*
  92. * for this use, may be ipath_cfgports summed over all chips that
  93. * are are configured and present
  94. */
  95. kinfo->spi_nports = dd->ipath_cfgports;
  96. /* unit (chip/board) our port is on */
  97. kinfo->spi_unit = dd->ipath_unit;
  98. /* for now, only a single page */
  99. kinfo->spi_tid_maxsize = PAGE_SIZE;
  100. /*
  101. * Doing this per port, and based on the skip value, etc. This has
  102. * to be the actual buffer size, since the protocol code treats it
  103. * as an array.
  104. *
  105. * These have to be set to user addresses in the user code via mmap.
  106. * These values are used on return to user code for the mmap target
  107. * addresses only. For 32 bit, same 44 bit address problem, so use
  108. * the physical address, not virtual. Before 2.6.11, using the
  109. * page_address() macro worked, but in 2.6.11, even that returns the
  110. * full 64 bit address (upper bits all 1's). So far, using the
  111. * physical addresses (or chip offsets, for chip mapping) works, but
  112. * no doubt some future kernel release will chang that, and we'll be
  113. * on to yet another method of dealing with this
  114. */
  115. kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
  116. kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
  117. kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
  118. kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
  119. (void *) dd->ipath_statusp -
  120. (void *) dd->ipath_pioavailregs_dma;
  121. kinfo->spi_piobufbase = (u64) pd->port_piobufs;
  122. kinfo->__spi_uregbase =
  123. dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  124. kinfo->spi_pioindex = dd->ipath_pbufsport * (pd->port_port - 1);
  125. kinfo->spi_piocnt = dd->ipath_pbufsport;
  126. kinfo->spi_pioalign = dd->ipath_palign;
  127. kinfo->spi_qpair = IPATH_KD_QP;
  128. kinfo->spi_piosize = dd->ipath_ibmaxlen;
  129. kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
  130. kinfo->spi_port = pd->port_port;
  131. kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
  132. kinfo->spi_hw_version = dd->ipath_revision;
  133. if (copy_to_user(ubase, kinfo, sizeof(*kinfo)))
  134. ret = -EFAULT;
  135. bail:
  136. kfree(kinfo);
  137. return ret;
  138. }
  139. /**
  140. * ipath_tid_update - update a port TID
  141. * @pd: the port
  142. * @ti: the TID information
  143. *
  144. * The new implementation as of Oct 2004 is that the driver assigns
  145. * the tid and returns it to the caller. To make it easier to
  146. * catch bugs, and to reduce search time, we keep a cursor for
  147. * each port, walking the shadow tid array to find one that's not
  148. * in use.
  149. *
  150. * For now, if we can't allocate the full list, we fail, although
  151. * in the long run, we'll allocate as many as we can, and the
  152. * caller will deal with that by trying the remaining pages later.
  153. * That means that when we fail, we have to mark the tids as not in
  154. * use again, in our shadow copy.
  155. *
  156. * It's up to the caller to free the tids when they are done.
  157. * We'll unlock the pages as they free them.
  158. *
  159. * Also, right now we are locking one page at a time, but since
  160. * the intended use of this routine is for a single group of
  161. * virtually contiguous pages, that should change to improve
  162. * performance.
  163. */
  164. static int ipath_tid_update(struct ipath_portdata *pd,
  165. const struct ipath_tid_info *ti)
  166. {
  167. int ret = 0, ntids;
  168. u32 tid, porttid, cnt, i, tidcnt;
  169. u16 *tidlist;
  170. struct ipath_devdata *dd = pd->port_dd;
  171. u64 physaddr;
  172. unsigned long vaddr;
  173. u64 __iomem *tidbase;
  174. unsigned long tidmap[8];
  175. struct page **pagep = NULL;
  176. if (!dd->ipath_pageshadow) {
  177. ret = -ENOMEM;
  178. goto done;
  179. }
  180. cnt = ti->tidcnt;
  181. if (!cnt) {
  182. ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
  183. (unsigned long long) ti->tidlist);
  184. /*
  185. * Should we treat as success? likely a bug
  186. */
  187. ret = -EFAULT;
  188. goto done;
  189. }
  190. tidcnt = dd->ipath_rcvtidcnt;
  191. if (cnt >= tidcnt) {
  192. /* make sure it all fits in port_tid_pg_list */
  193. dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
  194. "TIDs, only trying max (%u)\n", cnt, tidcnt);
  195. cnt = tidcnt;
  196. }
  197. pagep = (struct page **)pd->port_tid_pg_list;
  198. tidlist = (u16 *) (&pagep[cnt]);
  199. memset(tidmap, 0, sizeof(tidmap));
  200. tid = pd->port_tidcursor;
  201. /* before decrement; chip actual # */
  202. porttid = pd->port_port * tidcnt;
  203. ntids = tidcnt;
  204. tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
  205. dd->ipath_rcvtidbase +
  206. porttid * sizeof(*tidbase));
  207. ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
  208. pd->port_port, cnt, tid, tidbase);
  209. /* virtual address of first page in transfer */
  210. vaddr = ti->tidvaddr;
  211. if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
  212. cnt * PAGE_SIZE)) {
  213. ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
  214. (void *)vaddr, cnt);
  215. ret = -EFAULT;
  216. goto done;
  217. }
  218. ret = ipath_get_user_pages(vaddr, cnt, pagep);
  219. if (ret) {
  220. if (ret == -EBUSY) {
  221. ipath_dbg("Failed to lock addr %p, %u pages "
  222. "(already locked)\n",
  223. (void *) vaddr, cnt);
  224. /*
  225. * for now, continue, and see what happens but with
  226. * the new implementation, this should never happen,
  227. * unless perhaps the user has mpin'ed the pages
  228. * themselves (something we need to test)
  229. */
  230. ret = 0;
  231. } else {
  232. dev_info(&dd->pcidev->dev,
  233. "Failed to lock addr %p, %u pages: "
  234. "errno %d\n", (void *) vaddr, cnt, -ret);
  235. goto done;
  236. }
  237. }
  238. for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
  239. for (; ntids--; tid++) {
  240. if (tid == tidcnt)
  241. tid = 0;
  242. if (!dd->ipath_pageshadow[porttid + tid])
  243. break;
  244. }
  245. if (ntids < 0) {
  246. /*
  247. * oops, wrapped all the way through their TIDs,
  248. * and didn't have enough free; see comments at
  249. * start of routine
  250. */
  251. ipath_dbg("Not enough free TIDs for %u pages "
  252. "(index %d), failing\n", cnt, i);
  253. i--; /* last tidlist[i] not filled in */
  254. ret = -ENOMEM;
  255. break;
  256. }
  257. tidlist[i] = tid;
  258. ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
  259. "vaddr %lx\n", i, tid, vaddr);
  260. /* we "know" system pages and TID pages are same size */
  261. dd->ipath_pageshadow[porttid + tid] = pagep[i];
  262. /*
  263. * don't need atomic or it's overhead
  264. */
  265. __set_bit(tid, tidmap);
  266. physaddr = page_to_phys(pagep[i]);
  267. ipath_stats.sps_pagelocks++;
  268. ipath_cdbg(VERBOSE,
  269. "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
  270. tid, vaddr, (unsigned long long) physaddr,
  271. pagep[i]);
  272. dd->ipath_f_put_tid(dd, &tidbase[tid], 1, physaddr);
  273. /*
  274. * don't check this tid in ipath_portshadow, since we
  275. * just filled it in; start with the next one.
  276. */
  277. tid++;
  278. }
  279. if (ret) {
  280. u32 limit;
  281. cleanup:
  282. /* jump here if copy out of updated info failed... */
  283. ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
  284. -ret, i, cnt);
  285. /* same code that's in ipath_free_tid() */
  286. limit = sizeof(tidmap) * BITS_PER_BYTE;
  287. if (limit > tidcnt)
  288. /* just in case size changes in future */
  289. limit = tidcnt;
  290. tid = find_first_bit((const unsigned long *)tidmap, limit);
  291. for (; tid < limit; tid++) {
  292. if (!test_bit(tid, tidmap))
  293. continue;
  294. if (dd->ipath_pageshadow[porttid + tid]) {
  295. ipath_cdbg(VERBOSE, "Freeing TID %u\n",
  296. tid);
  297. dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
  298. dd->ipath_tidinvalid);
  299. dd->ipath_pageshadow[porttid + tid] = NULL;
  300. ipath_stats.sps_pageunlocks++;
  301. }
  302. }
  303. ipath_release_user_pages(pagep, cnt);
  304. } else {
  305. /*
  306. * Copy the updated array, with ipath_tid's filled in, back
  307. * to user. Since we did the copy in already, this "should
  308. * never fail" If it does, we have to clean up...
  309. */
  310. if (copy_to_user((void __user *)
  311. (unsigned long) ti->tidlist,
  312. tidlist, cnt * sizeof(*tidlist))) {
  313. ret = -EFAULT;
  314. goto cleanup;
  315. }
  316. if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
  317. tidmap, sizeof tidmap)) {
  318. ret = -EFAULT;
  319. goto cleanup;
  320. }
  321. if (tid == tidcnt)
  322. tid = 0;
  323. pd->port_tidcursor = tid;
  324. }
  325. done:
  326. if (ret)
  327. ipath_dbg("Failed to map %u TID pages, failing with %d\n",
  328. ti->tidcnt, -ret);
  329. return ret;
  330. }
  331. /**
  332. * ipath_tid_free - free a port TID
  333. * @pd: the port
  334. * @ti: the TID info
  335. *
  336. * right now we are unlocking one page at a time, but since
  337. * the intended use of this routine is for a single group of
  338. * virtually contiguous pages, that should change to improve
  339. * performance. We check that the TID is in range for this port
  340. * but otherwise don't check validity; if user has an error and
  341. * frees the wrong tid, it's only their own data that can thereby
  342. * be corrupted. We do check that the TID was in use, for sanity
  343. * We always use our idea of the saved address, not the address that
  344. * they pass in to us.
  345. */
  346. static int ipath_tid_free(struct ipath_portdata *pd,
  347. const struct ipath_tid_info *ti)
  348. {
  349. int ret = 0;
  350. u32 tid, porttid, cnt, limit, tidcnt;
  351. struct ipath_devdata *dd = pd->port_dd;
  352. u64 __iomem *tidbase;
  353. unsigned long tidmap[8];
  354. if (!dd->ipath_pageshadow) {
  355. ret = -ENOMEM;
  356. goto done;
  357. }
  358. if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
  359. sizeof tidmap)) {
  360. ret = -EFAULT;
  361. goto done;
  362. }
  363. porttid = pd->port_port * dd->ipath_rcvtidcnt;
  364. tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
  365. dd->ipath_rcvtidbase +
  366. porttid * sizeof(*tidbase));
  367. tidcnt = dd->ipath_rcvtidcnt;
  368. limit = sizeof(tidmap) * BITS_PER_BYTE;
  369. if (limit > tidcnt)
  370. /* just in case size changes in future */
  371. limit = tidcnt;
  372. tid = find_first_bit(tidmap, limit);
  373. ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
  374. "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
  375. limit, tid, porttid);
  376. for (cnt = 0; tid < limit; tid++) {
  377. /*
  378. * small optimization; if we detect a run of 3 or so without
  379. * any set, use find_first_bit again. That's mainly to
  380. * accelerate the case where we wrapped, so we have some at
  381. * the beginning, and some at the end, and a big gap
  382. * in the middle.
  383. */
  384. if (!test_bit(tid, tidmap))
  385. continue;
  386. cnt++;
  387. if (dd->ipath_pageshadow[porttid + tid]) {
  388. ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
  389. pd->port_pid, tid);
  390. dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
  391. dd->ipath_tidinvalid);
  392. ipath_release_user_pages(
  393. &dd->ipath_pageshadow[porttid + tid], 1);
  394. dd->ipath_pageshadow[porttid + tid] = NULL;
  395. ipath_stats.sps_pageunlocks++;
  396. } else
  397. ipath_dbg("Unused tid %u, ignoring\n", tid);
  398. }
  399. if (cnt != ti->tidcnt)
  400. ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
  401. ti->tidcnt, cnt);
  402. done:
  403. if (ret)
  404. ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
  405. ti->tidcnt, -ret);
  406. return ret;
  407. }
  408. /**
  409. * ipath_set_part_key - set a partition key
  410. * @pd: the port
  411. * @key: the key
  412. *
  413. * We can have up to 4 active at a time (other than the default, which is
  414. * always allowed). This is somewhat tricky, since multiple ports may set
  415. * the same key, so we reference count them, and clean up at exit. All 4
  416. * partition keys are packed into a single infinipath register. It's an
  417. * error for a process to set the same pkey multiple times. We provide no
  418. * mechanism to de-allocate a pkey at this time, we may eventually need to
  419. * do that. I've used the atomic operations, and no locking, and only make
  420. * a single pass through what's available. This should be more than
  421. * adequate for some time. I'll think about spinlocks or the like if and as
  422. * it's necessary.
  423. */
  424. static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
  425. {
  426. struct ipath_devdata *dd = pd->port_dd;
  427. int i, any = 0, pidx = -1;
  428. u16 lkey = key & 0x7FFF;
  429. int ret;
  430. if (lkey == (IPS_DEFAULT_P_KEY & 0x7FFF)) {
  431. /* nothing to do; this key always valid */
  432. ret = 0;
  433. goto bail;
  434. }
  435. ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
  436. "%hx:%x %hx:%x %hx:%x %hx:%x\n",
  437. pd->port_port, key, dd->ipath_pkeys[0],
  438. atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
  439. atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
  440. atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
  441. atomic_read(&dd->ipath_pkeyrefs[3]));
  442. if (!lkey) {
  443. ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
  444. pd->port_port);
  445. ret = -EINVAL;
  446. goto bail;
  447. }
  448. /*
  449. * Set the full membership bit, because it has to be
  450. * set in the register or the packet, and it seems
  451. * cleaner to set in the register than to force all
  452. * callers to set it. (see bug 4331)
  453. */
  454. key |= 0x8000;
  455. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  456. if (!pd->port_pkeys[i] && pidx == -1)
  457. pidx = i;
  458. if (pd->port_pkeys[i] == key) {
  459. ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
  460. "(%x) more than once\n",
  461. pd->port_port, key);
  462. ret = -EEXIST;
  463. goto bail;
  464. }
  465. }
  466. if (pidx == -1) {
  467. ipath_dbg("All pkeys for port %u already in use, "
  468. "can't set %x\n", pd->port_port, key);
  469. ret = -EBUSY;
  470. goto bail;
  471. }
  472. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  473. if (!dd->ipath_pkeys[i]) {
  474. any++;
  475. continue;
  476. }
  477. if (dd->ipath_pkeys[i] == key) {
  478. atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
  479. if (atomic_inc_return(pkrefs) > 1) {
  480. pd->port_pkeys[pidx] = key;
  481. ipath_cdbg(VERBOSE, "p%u set key %x "
  482. "matches #%d, count now %d\n",
  483. pd->port_port, key, i,
  484. atomic_read(pkrefs));
  485. ret = 0;
  486. goto bail;
  487. } else {
  488. /*
  489. * lost race, decrement count, catch below
  490. */
  491. atomic_dec(pkrefs);
  492. ipath_cdbg(VERBOSE, "Lost race, count was "
  493. "0, after dec, it's %d\n",
  494. atomic_read(pkrefs));
  495. any++;
  496. }
  497. }
  498. if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
  499. /*
  500. * It makes no sense to have both the limited and
  501. * full membership PKEY set at the same time since
  502. * the unlimited one will disable the limited one.
  503. */
  504. ret = -EEXIST;
  505. goto bail;
  506. }
  507. }
  508. if (!any) {
  509. ipath_dbg("port %u, all pkeys already in use, "
  510. "can't set %x\n", pd->port_port, key);
  511. ret = -EBUSY;
  512. goto bail;
  513. }
  514. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  515. if (!dd->ipath_pkeys[i] &&
  516. atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
  517. u64 pkey;
  518. /* for ipathstats, etc. */
  519. ipath_stats.sps_pkeys[i] = lkey;
  520. pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
  521. pkey =
  522. (u64) dd->ipath_pkeys[0] |
  523. ((u64) dd->ipath_pkeys[1] << 16) |
  524. ((u64) dd->ipath_pkeys[2] << 32) |
  525. ((u64) dd->ipath_pkeys[3] << 48);
  526. ipath_cdbg(PROC, "p%u set key %x in #%d, "
  527. "portidx %d, new pkey reg %llx\n",
  528. pd->port_port, key, i, pidx,
  529. (unsigned long long) pkey);
  530. ipath_write_kreg(
  531. dd, dd->ipath_kregs->kr_partitionkey, pkey);
  532. ret = 0;
  533. goto bail;
  534. }
  535. }
  536. ipath_dbg("port %u, all pkeys already in use 2nd pass, "
  537. "can't set %x\n", pd->port_port, key);
  538. ret = -EBUSY;
  539. bail:
  540. return ret;
  541. }
  542. /**
  543. * ipath_manage_rcvq - manage a port's receive queue
  544. * @pd: the port
  545. * @start_stop: action to carry out
  546. *
  547. * start_stop == 0 disables receive on the port, for use in queue
  548. * overflow conditions. start_stop==1 re-enables, to be used to
  549. * re-init the software copy of the head register
  550. */
  551. static int ipath_manage_rcvq(struct ipath_portdata *pd, int start_stop)
  552. {
  553. struct ipath_devdata *dd = pd->port_dd;
  554. u64 tval;
  555. ipath_cdbg(PROC, "%sabling rcv for unit %u port %u\n",
  556. start_stop ? "en" : "dis", dd->ipath_unit,
  557. pd->port_port);
  558. /* atomically clear receive enable port. */
  559. if (start_stop) {
  560. /*
  561. * On enable, force in-memory copy of the tail register to
  562. * 0, so that protocol code doesn't have to worry about
  563. * whether or not the chip has yet updated the in-memory
  564. * copy or not on return from the system call. The chip
  565. * always resets it's tail register back to 0 on a
  566. * transition from disabled to enabled. This could cause a
  567. * problem if software was broken, and did the enable w/o
  568. * the disable, but eventually the in-memory copy will be
  569. * updated and correct itself, even in the face of software
  570. * bugs.
  571. */
  572. *pd->port_rcvhdrtail_kvaddr = 0;
  573. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  574. &dd->ipath_rcvctrl);
  575. } else
  576. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  577. &dd->ipath_rcvctrl);
  578. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  579. dd->ipath_rcvctrl);
  580. /* now be sure chip saw it before we return */
  581. tval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  582. if (start_stop) {
  583. /*
  584. * And try to be sure that tail reg update has happened too.
  585. * This should in theory interlock with the RXE changes to
  586. * the tail register. Don't assign it to the tail register
  587. * in memory copy, since we could overwrite an update by the
  588. * chip if we did.
  589. */
  590. tval = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  591. }
  592. /* always; new head should be equal to new tail; see above */
  593. return 0;
  594. }
  595. static void ipath_clean_part_key(struct ipath_portdata *pd,
  596. struct ipath_devdata *dd)
  597. {
  598. int i, j, pchanged = 0;
  599. u64 oldpkey;
  600. /* for debugging only */
  601. oldpkey = (u64) dd->ipath_pkeys[0] |
  602. ((u64) dd->ipath_pkeys[1] << 16) |
  603. ((u64) dd->ipath_pkeys[2] << 32) |
  604. ((u64) dd->ipath_pkeys[3] << 48);
  605. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  606. if (!pd->port_pkeys[i])
  607. continue;
  608. ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
  609. pd->port_pkeys[i]);
  610. for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
  611. /* check for match independent of the global bit */
  612. if ((dd->ipath_pkeys[j] & 0x7fff) !=
  613. (pd->port_pkeys[i] & 0x7fff))
  614. continue;
  615. if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
  616. ipath_cdbg(VERBOSE, "p%u clear key "
  617. "%x matches #%d\n",
  618. pd->port_port,
  619. pd->port_pkeys[i], j);
  620. ipath_stats.sps_pkeys[j] =
  621. dd->ipath_pkeys[j] = 0;
  622. pchanged++;
  623. }
  624. else ipath_cdbg(
  625. VERBOSE, "p%u key %x matches #%d, "
  626. "but ref still %d\n", pd->port_port,
  627. pd->port_pkeys[i], j,
  628. atomic_read(&dd->ipath_pkeyrefs[j]));
  629. break;
  630. }
  631. pd->port_pkeys[i] = 0;
  632. }
  633. if (pchanged) {
  634. u64 pkey = (u64) dd->ipath_pkeys[0] |
  635. ((u64) dd->ipath_pkeys[1] << 16) |
  636. ((u64) dd->ipath_pkeys[2] << 32) |
  637. ((u64) dd->ipath_pkeys[3] << 48);
  638. ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
  639. "new pkey reg %llx\n", pd->port_port,
  640. (unsigned long long) oldpkey,
  641. (unsigned long long) pkey);
  642. ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
  643. pkey);
  644. }
  645. }
  646. /**
  647. * ipath_create_user_egr - allocate eager TID buffers
  648. * @pd: the port to allocate TID buffers for
  649. *
  650. * This routine is now quite different for user and kernel, because
  651. * the kernel uses skb's, for the accelerated network performance
  652. * This is the user port version
  653. *
  654. * Allocate the eager TID buffers and program them into infinipath
  655. * They are no longer completely contiguous, we do multiple allocation
  656. * calls.
  657. */
  658. static int ipath_create_user_egr(struct ipath_portdata *pd)
  659. {
  660. struct ipath_devdata *dd = pd->port_dd;
  661. unsigned e, egrcnt, alloced, egrperchunk, chunk, egrsize, egroff;
  662. size_t size;
  663. int ret;
  664. egrcnt = dd->ipath_rcvegrcnt;
  665. /* TID number offset for this port */
  666. egroff = pd->port_port * egrcnt;
  667. egrsize = dd->ipath_rcvegrbufsize;
  668. ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
  669. "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
  670. /*
  671. * to avoid wasting a lot of memory, we allocate 32KB chunks of
  672. * physically contiguous memory, advance through it until used up
  673. * and then allocate more. Of course, we need memory to store those
  674. * extra pointers, now. Started out with 256KB, but under heavy
  675. * memory pressure (creating large files and then copying them over
  676. * NFS while doing lots of MPI jobs), we hit some allocation
  677. * failures, even though we can sleep... (2.6.10) Still get
  678. * failures at 64K. 32K is the lowest we can go without waiting
  679. * more memory again. It seems likely that the coalescing in
  680. * free_pages, etc. still has issues (as it has had previously
  681. * during 2.6.x development).
  682. */
  683. size = 0x8000;
  684. alloced = ALIGN(egrsize * egrcnt, size);
  685. egrperchunk = size / egrsize;
  686. chunk = (egrcnt + egrperchunk - 1) / egrperchunk;
  687. pd->port_rcvegrbuf_chunks = chunk;
  688. pd->port_rcvegrbufs_perchunk = egrperchunk;
  689. pd->port_rcvegrbuf_size = size;
  690. pd->port_rcvegrbuf = vmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]));
  691. if (!pd->port_rcvegrbuf) {
  692. ret = -ENOMEM;
  693. goto bail;
  694. }
  695. pd->port_rcvegrbuf_phys =
  696. vmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]));
  697. if (!pd->port_rcvegrbuf_phys) {
  698. ret = -ENOMEM;
  699. goto bail_rcvegrbuf;
  700. }
  701. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  702. /*
  703. * GFP_USER, but without GFP_FS, so buffer cache can be
  704. * coalesced (we hope); otherwise, even at order 4,
  705. * heavy filesystem activity makes these fail
  706. */
  707. gfp_t gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  708. pd->port_rcvegrbuf[e] = dma_alloc_coherent(
  709. &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
  710. gfp_flags);
  711. if (!pd->port_rcvegrbuf[e]) {
  712. ret = -ENOMEM;
  713. goto bail_rcvegrbuf_phys;
  714. }
  715. }
  716. pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
  717. for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
  718. dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
  719. unsigned i;
  720. for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
  721. dd->ipath_f_put_tid(dd, e + egroff +
  722. (u64 __iomem *)
  723. ((char __iomem *)
  724. dd->ipath_kregbase +
  725. dd->ipath_rcvegrbase), 0, pa);
  726. pa += egrsize;
  727. }
  728. cond_resched(); /* don't hog the cpu */
  729. }
  730. ret = 0;
  731. goto bail;
  732. bail_rcvegrbuf_phys:
  733. for (e = 0; e < pd->port_rcvegrbuf_chunks &&
  734. pd->port_rcvegrbuf[e]; e++)
  735. dma_free_coherent(&dd->pcidev->dev, size,
  736. pd->port_rcvegrbuf[e],
  737. pd->port_rcvegrbuf_phys[e]);
  738. vfree(pd->port_rcvegrbuf_phys);
  739. pd->port_rcvegrbuf_phys = NULL;
  740. bail_rcvegrbuf:
  741. vfree(pd->port_rcvegrbuf);
  742. pd->port_rcvegrbuf = NULL;
  743. bail:
  744. return ret;
  745. }
  746. static int ipath_do_user_init(struct ipath_portdata *pd,
  747. const struct ipath_user_info *uinfo)
  748. {
  749. int ret = 0;
  750. struct ipath_devdata *dd = pd->port_dd;
  751. u64 physaddr, uaddr, off, atmp;
  752. struct page *pagep;
  753. u32 head32;
  754. u64 head;
  755. /* for now, if major version is different, bail */
  756. if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) {
  757. dev_info(&dd->pcidev->dev,
  758. "User major version %d not same as driver "
  759. "major %d\n", uinfo->spu_userversion >> 16,
  760. IPATH_USER_SWMAJOR);
  761. ret = -ENODEV;
  762. goto done;
  763. }
  764. if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR)
  765. ipath_dbg("User minor version %d not same as driver "
  766. "minor %d\n", uinfo->spu_userversion & 0xffff,
  767. IPATH_USER_SWMINOR);
  768. if (uinfo->spu_rcvhdrsize) {
  769. ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
  770. if (ret)
  771. goto done;
  772. }
  773. /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
  774. /* set up for the rcvhdr Q tail register writeback to user memory */
  775. if (!uinfo->spu_rcvhdraddr ||
  776. !access_ok(VERIFY_WRITE, (u64 __user *) (unsigned long)
  777. uinfo->spu_rcvhdraddr, sizeof(u64))) {
  778. ipath_dbg("Port %d rcvhdrtail addr %llx not valid\n",
  779. pd->port_port,
  780. (unsigned long long) uinfo->spu_rcvhdraddr);
  781. ret = -EINVAL;
  782. goto done;
  783. }
  784. off = offset_in_page(uinfo->spu_rcvhdraddr);
  785. uaddr = PAGE_MASK & (unsigned long) uinfo->spu_rcvhdraddr;
  786. ret = ipath_get_user_pages_nocopy(uaddr, &pagep);
  787. if (ret) {
  788. dev_info(&dd->pcidev->dev, "Failed to lookup and lock "
  789. "address %llx for rcvhdrtail: errno %d\n",
  790. (unsigned long long) uinfo->spu_rcvhdraddr, -ret);
  791. goto done;
  792. }
  793. ipath_stats.sps_pagelocks++;
  794. pd->port_rcvhdrtail_uaddr = uaddr;
  795. pd->port_rcvhdrtail_pagep = pagep;
  796. pd->port_rcvhdrtail_kvaddr =
  797. page_address(pagep);
  798. pd->port_rcvhdrtail_kvaddr += off;
  799. physaddr = page_to_phys(pagep) + off;
  800. ipath_cdbg(VERBOSE, "port %d user addr %llx hdrtailaddr, %llx "
  801. "physical (off=%llx)\n",
  802. pd->port_port,
  803. (unsigned long long) uinfo->spu_rcvhdraddr,
  804. (unsigned long long) physaddr, (unsigned long long) off);
  805. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  806. pd->port_port, physaddr);
  807. atmp = ipath_read_kreg64_port(dd,
  808. dd->ipath_kregs->kr_rcvhdrtailaddr,
  809. pd->port_port);
  810. if (physaddr != atmp) {
  811. ipath_dev_err(dd,
  812. "Catastrophic software error, "
  813. "RcvHdrTailAddr%u written as %llx, "
  814. "read back as %llx\n", pd->port_port,
  815. (unsigned long long) physaddr,
  816. (unsigned long long) atmp);
  817. ret = -EINVAL;
  818. goto done;
  819. }
  820. /* for right now, kernel piobufs are at end, so port 1 is at 0 */
  821. pd->port_piobufs = dd->ipath_piobufbase +
  822. dd->ipath_pbufsport * (pd->port_port -
  823. 1) * dd->ipath_palign;
  824. ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
  825. pd->port_port, pd->port_piobufs);
  826. /*
  827. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  828. * array for time being. If pd->port_port > chip-supported,
  829. * we need to do extra stuff here to handle by handling overflow
  830. * through port 0, someday
  831. */
  832. ret = ipath_create_rcvhdrq(dd, pd);
  833. if (!ret)
  834. ret = ipath_create_user_egr(pd);
  835. if (ret)
  836. goto done;
  837. /* enable receives now */
  838. /* atomically set enable bit for this port */
  839. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  840. &dd->ipath_rcvctrl);
  841. /*
  842. * set the head registers for this port to the current values
  843. * of the tail pointers, since we don't know if they were
  844. * updated on last use of the port.
  845. */
  846. head32 = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  847. head = (u64) head32;
  848. ipath_write_ureg(dd, ur_rcvhdrhead, head, pd->port_port);
  849. head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
  850. ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
  851. dd->ipath_lastegrheads[pd->port_port] = -1;
  852. dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
  853. ipath_cdbg(VERBOSE, "Wrote port%d head %llx, egrhead %x from "
  854. "tail regs\n", pd->port_port,
  855. (unsigned long long) head, head32);
  856. pd->port_tidcursor = 0; /* start at beginning after open */
  857. /*
  858. * now enable the port; the tail registers will be written to memory
  859. * by the chip as soon as it sees the write to
  860. * dd->ipath_kregs->kr_rcvctrl. The update only happens on
  861. * transition from 0 to 1, so clear it first, then set it as part of
  862. * enabling the port. This will (very briefly) affect any other
  863. * open ports, but it shouldn't be long enough to be an issue.
  864. */
  865. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  866. dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
  867. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  868. dd->ipath_rcvctrl);
  869. done:
  870. return ret;
  871. }
  872. static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
  873. u64 ureg)
  874. {
  875. unsigned long phys;
  876. int ret;
  877. /* it's the real hardware, so io_remap works */
  878. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  879. dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
  880. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  881. ret = -EFAULT;
  882. } else {
  883. phys = dd->ipath_physaddr + ureg;
  884. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  885. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  886. ret = io_remap_pfn_range(vma, vma->vm_start,
  887. phys >> PAGE_SHIFT,
  888. vma->vm_end - vma->vm_start,
  889. vma->vm_page_prot);
  890. }
  891. return ret;
  892. }
  893. static int mmap_piobufs(struct vm_area_struct *vma,
  894. struct ipath_devdata *dd,
  895. struct ipath_portdata *pd)
  896. {
  897. unsigned long phys;
  898. int ret;
  899. /*
  900. * When we map the PIO buffers, we want to map them as writeonly, no
  901. * read possible.
  902. */
  903. if ((vma->vm_end - vma->vm_start) >
  904. (dd->ipath_pbufsport * dd->ipath_palign)) {
  905. dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
  906. "reqlen %lx > PAGE\n",
  907. vma->vm_end - vma->vm_start);
  908. ret = -EFAULT;
  909. goto bail;
  910. }
  911. phys = dd->ipath_physaddr + pd->port_piobufs;
  912. /*
  913. * Do *NOT* mark this as non-cached (PWT bit), or we don't get the
  914. * write combining behavior we want on the PIO buffers!
  915. * vma->vm_page_prot =
  916. * pgprot_noncached(vma->vm_page_prot);
  917. */
  918. if (vma->vm_flags & VM_READ) {
  919. dev_info(&dd->pcidev->dev,
  920. "Can't map piobufs as readable (flags=%lx)\n",
  921. vma->vm_flags);
  922. ret = -EPERM;
  923. goto bail;
  924. }
  925. /* don't allow them to later change to readable with mprotect */
  926. vma->vm_flags &= ~VM_MAYWRITE;
  927. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  928. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  929. vma->vm_end - vma->vm_start,
  930. vma->vm_page_prot);
  931. bail:
  932. return ret;
  933. }
  934. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  935. struct ipath_portdata *pd)
  936. {
  937. struct ipath_devdata *dd = pd->port_dd;
  938. unsigned long start, size;
  939. size_t total_size, i;
  940. dma_addr_t *phys;
  941. int ret;
  942. if (!pd->port_rcvegrbuf) {
  943. ret = -EFAULT;
  944. goto bail;
  945. }
  946. size = pd->port_rcvegrbuf_size;
  947. total_size = pd->port_rcvegrbuf_chunks * size;
  948. if ((vma->vm_end - vma->vm_start) > total_size) {
  949. dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
  950. "reqlen %lx > actual %lx\n",
  951. vma->vm_end - vma->vm_start,
  952. (unsigned long) total_size);
  953. ret = -EFAULT;
  954. goto bail;
  955. }
  956. if (vma->vm_flags & VM_WRITE) {
  957. dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
  958. "writable (flags=%lx)\n", vma->vm_flags);
  959. ret = -EPERM;
  960. goto bail;
  961. }
  962. start = vma->vm_start;
  963. phys = pd->port_rcvegrbuf_phys;
  964. /* don't allow them to later change to writeable with mprotect */
  965. vma->vm_flags &= ~VM_MAYWRITE;
  966. for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
  967. ret = remap_pfn_range(vma, start, phys[i] >> PAGE_SHIFT,
  968. size, vma->vm_page_prot);
  969. if (ret < 0)
  970. goto bail;
  971. }
  972. ret = 0;
  973. bail:
  974. return ret;
  975. }
  976. static int mmap_rcvhdrq(struct vm_area_struct *vma,
  977. struct ipath_portdata *pd)
  978. {
  979. struct ipath_devdata *dd = pd->port_dd;
  980. size_t total_size;
  981. int ret;
  982. /*
  983. * kmalloc'ed memory, physically contiguous; this is from
  984. * spi_rcvhdr_base; we allow user to map read-write so they can
  985. * write hdrq entries to allow protocol code to directly poll
  986. * whether a hdrq entry has been written.
  987. */
  988. total_size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  989. sizeof(u32), PAGE_SIZE);
  990. if ((vma->vm_end - vma->vm_start) > total_size) {
  991. dev_info(&dd->pcidev->dev,
  992. "FAIL on rcvhdrq: reqlen %lx > actual %lx\n",
  993. vma->vm_end - vma->vm_start,
  994. (unsigned long) total_size);
  995. ret = -EFAULT;
  996. goto bail;
  997. }
  998. ret = remap_pfn_range(vma, vma->vm_start,
  999. pd->port_rcvhdrq_phys >> PAGE_SHIFT,
  1000. vma->vm_end - vma->vm_start,
  1001. vma->vm_page_prot);
  1002. bail:
  1003. return ret;
  1004. }
  1005. static int mmap_pioavailregs(struct vm_area_struct *vma,
  1006. struct ipath_portdata *pd)
  1007. {
  1008. struct ipath_devdata *dd = pd->port_dd;
  1009. int ret;
  1010. /*
  1011. * when we map the PIO bufferavail registers, we want to map them as
  1012. * readonly, no write possible.
  1013. *
  1014. * kmalloc'ed memory, physically contiguous, one page only, readonly
  1015. */
  1016. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  1017. dev_info(&dd->pcidev->dev, "FAIL on pioavailregs_dma: "
  1018. "reqlen %lx > actual %lx\n",
  1019. vma->vm_end - vma->vm_start,
  1020. (unsigned long) PAGE_SIZE);
  1021. ret = -EFAULT;
  1022. goto bail;
  1023. }
  1024. if (vma->vm_flags & VM_WRITE) {
  1025. dev_info(&dd->pcidev->dev,
  1026. "Can't map pioavailregs as writable (flags=%lx)\n",
  1027. vma->vm_flags);
  1028. ret = -EPERM;
  1029. goto bail;
  1030. }
  1031. /* don't allow them to later change with mprotect */
  1032. vma->vm_flags &= ~VM_MAYWRITE;
  1033. ret = remap_pfn_range(vma, vma->vm_start,
  1034. dd->ipath_pioavailregs_phys >> PAGE_SHIFT,
  1035. PAGE_SIZE, vma->vm_page_prot);
  1036. bail:
  1037. return ret;
  1038. }
  1039. /**
  1040. * ipath_mmap - mmap various structures into user space
  1041. * @fp: the file pointer
  1042. * @vma: the VM area
  1043. *
  1044. * We use this to have a shared buffer between the kernel and the user code
  1045. * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
  1046. * buffers in the chip. We have the open and close entries so we can bump
  1047. * the ref count and keep the driver from being unloaded while still mapped.
  1048. */
  1049. static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
  1050. {
  1051. struct ipath_portdata *pd;
  1052. struct ipath_devdata *dd;
  1053. u64 pgaddr, ureg;
  1054. int ret;
  1055. pd = port_fp(fp);
  1056. dd = pd->port_dd;
  1057. /*
  1058. * This is the ipath_do_user_init() code, mapping the shared buffers
  1059. * into the user process. The address referred to by vm_pgoff is the
  1060. * virtual, not physical, address; we only do one mmap for each
  1061. * space mapped.
  1062. */
  1063. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  1064. /*
  1065. * note that ureg does *NOT* have the kregvirt as part of it, to be
  1066. * sure that for 32 bit programs, we don't end up trying to map a >
  1067. * 44 address. Has to match ipath_get_base_info() code that sets
  1068. * __spi_uregbase
  1069. */
  1070. ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  1071. ipath_cdbg(MM, "ushare: pgaddr %llx vm_start=%lx, vmlen %lx\n",
  1072. (unsigned long long) pgaddr, vma->vm_start,
  1073. vma->vm_end - vma->vm_start);
  1074. if (pgaddr == ureg)
  1075. ret = mmap_ureg(vma, dd, ureg);
  1076. else if (pgaddr == pd->port_piobufs)
  1077. ret = mmap_piobufs(vma, dd, pd);
  1078. else if (pgaddr == (u64) pd->port_rcvegr_phys)
  1079. ret = mmap_rcvegrbufs(vma, pd);
  1080. else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
  1081. ret = mmap_rcvhdrq(vma, pd);
  1082. else if (pgaddr == dd->ipath_pioavailregs_phys)
  1083. ret = mmap_pioavailregs(vma, pd);
  1084. else
  1085. ret = -EINVAL;
  1086. vma->vm_private_data = NULL;
  1087. if (ret < 0)
  1088. dev_info(&dd->pcidev->dev,
  1089. "Failure %d on addr %lx, off %lx\n",
  1090. -ret, vma->vm_start, vma->vm_pgoff);
  1091. return ret;
  1092. }
  1093. static unsigned int ipath_poll(struct file *fp,
  1094. struct poll_table_struct *pt)
  1095. {
  1096. struct ipath_portdata *pd;
  1097. u32 head, tail;
  1098. int bit;
  1099. struct ipath_devdata *dd;
  1100. pd = port_fp(fp);
  1101. dd = pd->port_dd;
  1102. bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
  1103. set_bit(bit, &dd->ipath_rcvctrl);
  1104. /*
  1105. * Before blocking, make sure that head is still == tail,
  1106. * reading from the chip, so we can be sure the interrupt
  1107. * enable has made it to the chip. If not equal, disable
  1108. * interrupt again and return immediately. This avoids races,
  1109. * and the overhead of the chip read doesn't matter much at
  1110. * this point, since we are waiting for something anyway.
  1111. */
  1112. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1113. dd->ipath_rcvctrl);
  1114. head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
  1115. tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  1116. if (tail == head) {
  1117. set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1118. if(dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
  1119. (void)ipath_write_ureg(dd, ur_rcvhdrhead,
  1120. dd->ipath_rhdrhead_intr_off
  1121. | head, pd->port_port);
  1122. poll_wait(fp, &pd->port_wait, pt);
  1123. if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
  1124. /* timed out, no packets received */
  1125. clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1126. pd->port_rcvwait_to++;
  1127. }
  1128. }
  1129. else {
  1130. /* it's already happened; don't do wait_event overhead */
  1131. pd->port_rcvnowait++;
  1132. }
  1133. clear_bit(bit, &dd->ipath_rcvctrl);
  1134. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1135. dd->ipath_rcvctrl);
  1136. return 0;
  1137. }
  1138. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1139. struct file *fp)
  1140. {
  1141. int ret;
  1142. if (!dd->ipath_pd[port]) {
  1143. void *p, *ptmp;
  1144. p = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1145. /*
  1146. * Allocate memory for use in ipath_tid_update() just once
  1147. * at open, not per call. Reduces cost of expected send
  1148. * setup.
  1149. */
  1150. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1151. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1152. GFP_KERNEL);
  1153. if (!p || !ptmp) {
  1154. ipath_dev_err(dd, "Unable to allocate portdata "
  1155. "memory, failing open\n");
  1156. ret = -ENOMEM;
  1157. kfree(p);
  1158. kfree(ptmp);
  1159. goto bail;
  1160. }
  1161. dd->ipath_pd[port] = p;
  1162. dd->ipath_pd[port]->port_port = port;
  1163. dd->ipath_pd[port]->port_dd = dd;
  1164. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1165. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1166. }
  1167. if (!dd->ipath_pd[port]->port_cnt) {
  1168. dd->ipath_pd[port]->port_cnt = 1;
  1169. fp->private_data = (void *) dd->ipath_pd[port];
  1170. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1171. current->comm, current->pid, dd->ipath_unit,
  1172. port);
  1173. dd->ipath_pd[port]->port_pid = current->pid;
  1174. strncpy(dd->ipath_pd[port]->port_comm, current->comm,
  1175. sizeof(dd->ipath_pd[port]->port_comm));
  1176. ipath_stats.sps_ports++;
  1177. ret = 0;
  1178. goto bail;
  1179. }
  1180. ret = -EBUSY;
  1181. bail:
  1182. return ret;
  1183. }
  1184. static inline int usable(struct ipath_devdata *dd)
  1185. {
  1186. return dd &&
  1187. (dd->ipath_flags & IPATH_PRESENT) &&
  1188. dd->ipath_kregbase &&
  1189. dd->ipath_lid &&
  1190. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1191. | IPATH_LINKUNK));
  1192. }
  1193. static int find_free_port(int unit, struct file *fp)
  1194. {
  1195. struct ipath_devdata *dd = ipath_lookup(unit);
  1196. int ret, i;
  1197. if (!dd) {
  1198. ret = -ENODEV;
  1199. goto bail;
  1200. }
  1201. if (!usable(dd)) {
  1202. ret = -ENETDOWN;
  1203. goto bail;
  1204. }
  1205. for (i = 0; i < dd->ipath_cfgports; i++) {
  1206. ret = try_alloc_port(dd, i, fp);
  1207. if (ret != -EBUSY)
  1208. goto bail;
  1209. }
  1210. ret = -EBUSY;
  1211. bail:
  1212. return ret;
  1213. }
  1214. static int find_best_unit(struct file *fp)
  1215. {
  1216. int ret = 0, i, prefunit = -1, devmax;
  1217. int maxofallports, npresent, nup;
  1218. int ndev;
  1219. (void) ipath_count_units(&npresent, &nup, &maxofallports);
  1220. /*
  1221. * This code is present to allow a knowledgeable person to
  1222. * specify the layout of processes to processors before opening
  1223. * this driver, and then we'll assign the process to the "closest"
  1224. * HT-400 to that processor (we assume reasonable connectivity,
  1225. * for now). This code assumes that if affinity has been set
  1226. * before this point, that at most one cpu is set; for now this
  1227. * is reasonable. I check for both cpus_empty() and cpus_full(),
  1228. * in case some kernel variant sets none of the bits when no
  1229. * affinity is set. 2.6.11 and 12 kernels have all present
  1230. * cpus set. Some day we'll have to fix it up further to handle
  1231. * a cpu subset. This algorithm fails for two HT-400's connected
  1232. * in tunnel fashion. Eventually this needs real topology
  1233. * information. There may be some issues with dual core numbering
  1234. * as well. This needs more work prior to release.
  1235. */
  1236. if (!cpus_empty(current->cpus_allowed) &&
  1237. !cpus_full(current->cpus_allowed)) {
  1238. int ncpus = num_online_cpus(), curcpu = -1;
  1239. for (i = 0; i < ncpus; i++)
  1240. if (cpu_isset(i, current->cpus_allowed)) {
  1241. ipath_cdbg(PROC, "%s[%u] affinity set for "
  1242. "cpu %d\n", current->comm,
  1243. current->pid, i);
  1244. curcpu = i;
  1245. }
  1246. if (curcpu != -1) {
  1247. if (npresent) {
  1248. prefunit = curcpu / (ncpus / npresent);
  1249. ipath_dbg("%s[%u] %d chips, %d cpus, "
  1250. "%d cpus/chip, select unit %d\n",
  1251. current->comm, current->pid,
  1252. npresent, ncpus, ncpus / npresent,
  1253. prefunit);
  1254. }
  1255. }
  1256. }
  1257. /*
  1258. * user ports start at 1, kernel port is 0
  1259. * For now, we do round-robin access across all chips
  1260. */
  1261. if (prefunit != -1)
  1262. devmax = prefunit + 1;
  1263. else
  1264. devmax = ipath_count_units(NULL, NULL, NULL);
  1265. recheck:
  1266. for (i = 1; i < maxofallports; i++) {
  1267. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1268. ndev++) {
  1269. struct ipath_devdata *dd = ipath_lookup(ndev);
  1270. if (!usable(dd))
  1271. continue; /* can't use this unit */
  1272. if (i >= dd->ipath_cfgports)
  1273. /*
  1274. * Maxed out on users of this unit. Try
  1275. * next.
  1276. */
  1277. continue;
  1278. ret = try_alloc_port(dd, i, fp);
  1279. if (!ret)
  1280. goto done;
  1281. }
  1282. }
  1283. if (npresent) {
  1284. if (nup == 0) {
  1285. ret = -ENETDOWN;
  1286. ipath_dbg("No ports available (none initialized "
  1287. "and ready)\n");
  1288. } else {
  1289. if (prefunit > 0) {
  1290. /* if started above 0, retry from 0 */
  1291. ipath_cdbg(PROC,
  1292. "%s[%u] no ports on prefunit "
  1293. "%d, clear and re-check\n",
  1294. current->comm, current->pid,
  1295. prefunit);
  1296. devmax = ipath_count_units(NULL, NULL,
  1297. NULL);
  1298. prefunit = -1;
  1299. goto recheck;
  1300. }
  1301. ret = -EBUSY;
  1302. ipath_dbg("No ports available\n");
  1303. }
  1304. } else {
  1305. ret = -ENXIO;
  1306. ipath_dbg("No boards found\n");
  1307. }
  1308. done:
  1309. return ret;
  1310. }
  1311. static int ipath_open(struct inode *in, struct file *fp)
  1312. {
  1313. int ret, user_minor;
  1314. mutex_lock(&ipath_mutex);
  1315. user_minor = iminor(in) - IPATH_USER_MINOR_BASE;
  1316. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1317. (long)in->i_rdev, user_minor);
  1318. if (user_minor)
  1319. ret = find_free_port(user_minor - 1, fp);
  1320. else
  1321. ret = find_best_unit(fp);
  1322. mutex_unlock(&ipath_mutex);
  1323. return ret;
  1324. }
  1325. /**
  1326. * unlock_exptid - unlock any expected TID entries port still had in use
  1327. * @pd: port
  1328. *
  1329. * We don't actually update the chip here, because we do a bulk update
  1330. * below, using ipath_f_clear_tids.
  1331. */
  1332. static void unlock_expected_tids(struct ipath_portdata *pd)
  1333. {
  1334. struct ipath_devdata *dd = pd->port_dd;
  1335. int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
  1336. int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1337. ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
  1338. pd->port_port);
  1339. for (i = port_tidbase; i < maxtid; i++) {
  1340. if (!dd->ipath_pageshadow[i])
  1341. continue;
  1342. ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
  1343. 1);
  1344. dd->ipath_pageshadow[i] = NULL;
  1345. cnt++;
  1346. ipath_stats.sps_pageunlocks++;
  1347. }
  1348. if (cnt)
  1349. ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
  1350. pd->port_port, cnt);
  1351. if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
  1352. ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
  1353. (unsigned long long) ipath_stats.sps_pagelocks,
  1354. (unsigned long long)
  1355. ipath_stats.sps_pageunlocks);
  1356. }
  1357. static int ipath_close(struct inode *in, struct file *fp)
  1358. {
  1359. int ret = 0;
  1360. struct ipath_portdata *pd;
  1361. struct ipath_devdata *dd;
  1362. unsigned port;
  1363. ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
  1364. (long)in->i_rdev, fp->private_data);
  1365. mutex_lock(&ipath_mutex);
  1366. pd = port_fp(fp);
  1367. port = pd->port_port;
  1368. fp->private_data = NULL;
  1369. dd = pd->port_dd;
  1370. if (pd->port_hdrqfull) {
  1371. ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
  1372. "during run\n", pd->port_comm, pd->port_pid,
  1373. pd->port_hdrqfull);
  1374. pd->port_hdrqfull = 0;
  1375. }
  1376. if (pd->port_rcvwait_to || pd->port_piowait_to
  1377. || pd->port_rcvnowait || pd->port_pionowait) {
  1378. ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
  1379. "%u rcv %u, pio already\n",
  1380. pd->port_port, pd->port_rcvwait_to,
  1381. pd->port_piowait_to, pd->port_rcvnowait,
  1382. pd->port_pionowait);
  1383. pd->port_rcvwait_to = pd->port_piowait_to =
  1384. pd->port_rcvnowait = pd->port_pionowait = 0;
  1385. }
  1386. if (pd->port_flag) {
  1387. ipath_dbg("port %u port_flag still set to 0x%lx\n",
  1388. pd->port_port, pd->port_flag);
  1389. pd->port_flag = 0;
  1390. }
  1391. if (dd->ipath_kregbase) {
  1392. if (pd->port_rcvhdrtail_uaddr) {
  1393. pd->port_rcvhdrtail_uaddr = 0;
  1394. pd->port_rcvhdrtail_kvaddr = NULL;
  1395. ipath_release_user_pages_on_close(
  1396. &pd->port_rcvhdrtail_pagep, 1);
  1397. pd->port_rcvhdrtail_pagep = NULL;
  1398. ipath_stats.sps_pageunlocks++;
  1399. }
  1400. ipath_write_kreg_port(
  1401. dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  1402. port, 0ULL);
  1403. ipath_write_kreg_port(
  1404. dd, dd->ipath_kregs->kr_rcvhdraddr,
  1405. pd->port_port, 0);
  1406. /* clean up the pkeys for this port user */
  1407. ipath_clean_part_key(pd, dd);
  1408. if (port < dd->ipath_cfgports) {
  1409. int i = dd->ipath_pbufsport * (port - 1);
  1410. ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
  1411. /* atomically clear receive enable port. */
  1412. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
  1413. &dd->ipath_rcvctrl);
  1414. ipath_write_kreg(
  1415. dd,
  1416. dd->ipath_kregs->kr_rcvctrl,
  1417. dd->ipath_rcvctrl);
  1418. if (dd->ipath_pageshadow)
  1419. unlock_expected_tids(pd);
  1420. ipath_stats.sps_ports--;
  1421. ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
  1422. pd->port_comm, pd->port_pid,
  1423. dd->ipath_unit, port);
  1424. }
  1425. }
  1426. pd->port_cnt = 0;
  1427. pd->port_pid = 0;
  1428. dd->ipath_f_clear_tids(dd, pd->port_port);
  1429. ipath_free_pddata(dd, pd->port_port, 0);
  1430. mutex_unlock(&ipath_mutex);
  1431. return ret;
  1432. }
  1433. static int ipath_port_info(struct ipath_portdata *pd,
  1434. struct ipath_port_info __user *uinfo)
  1435. {
  1436. struct ipath_port_info info;
  1437. int nup;
  1438. int ret;
  1439. (void) ipath_count_units(NULL, &nup, NULL);
  1440. info.num_active = nup;
  1441. info.unit = pd->port_dd->ipath_unit;
  1442. info.port = pd->port_port;
  1443. if (copy_to_user(uinfo, &info, sizeof(info))) {
  1444. ret = -EFAULT;
  1445. goto bail;
  1446. }
  1447. ret = 0;
  1448. bail:
  1449. return ret;
  1450. }
  1451. static ssize_t ipath_write(struct file *fp, const char __user *data,
  1452. size_t count, loff_t *off)
  1453. {
  1454. const struct ipath_cmd __user *ucmd;
  1455. struct ipath_portdata *pd;
  1456. const void __user *src;
  1457. size_t consumed, copy;
  1458. struct ipath_cmd cmd;
  1459. ssize_t ret = 0;
  1460. void *dest;
  1461. if (count < sizeof(cmd.type)) {
  1462. ret = -EINVAL;
  1463. goto bail;
  1464. }
  1465. ucmd = (const struct ipath_cmd __user *) data;
  1466. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1467. ret = -EFAULT;
  1468. goto bail;
  1469. }
  1470. consumed = sizeof(cmd.type);
  1471. switch (cmd.type) {
  1472. case IPATH_CMD_USER_INIT:
  1473. copy = sizeof(cmd.cmd.user_info);
  1474. dest = &cmd.cmd.user_info;
  1475. src = &ucmd->cmd.user_info;
  1476. break;
  1477. case IPATH_CMD_RECV_CTRL:
  1478. copy = sizeof(cmd.cmd.recv_ctrl);
  1479. dest = &cmd.cmd.recv_ctrl;
  1480. src = &ucmd->cmd.recv_ctrl;
  1481. break;
  1482. case IPATH_CMD_PORT_INFO:
  1483. copy = sizeof(cmd.cmd.port_info);
  1484. dest = &cmd.cmd.port_info;
  1485. src = &ucmd->cmd.port_info;
  1486. break;
  1487. case IPATH_CMD_TID_UPDATE:
  1488. case IPATH_CMD_TID_FREE:
  1489. copy = sizeof(cmd.cmd.tid_info);
  1490. dest = &cmd.cmd.tid_info;
  1491. src = &ucmd->cmd.tid_info;
  1492. break;
  1493. case IPATH_CMD_SET_PART_KEY:
  1494. copy = sizeof(cmd.cmd.part_key);
  1495. dest = &cmd.cmd.part_key;
  1496. src = &ucmd->cmd.part_key;
  1497. break;
  1498. default:
  1499. ret = -EINVAL;
  1500. goto bail;
  1501. }
  1502. if ((count - consumed) < copy) {
  1503. ret = -EINVAL;
  1504. goto bail;
  1505. }
  1506. if (copy_from_user(dest, src, copy)) {
  1507. ret = -EFAULT;
  1508. goto bail;
  1509. }
  1510. consumed += copy;
  1511. pd = port_fp(fp);
  1512. switch (cmd.type) {
  1513. case IPATH_CMD_USER_INIT:
  1514. ret = ipath_do_user_init(pd, &cmd.cmd.user_info);
  1515. if (ret < 0)
  1516. goto bail;
  1517. ret = ipath_get_base_info(
  1518. pd, (void __user *) (unsigned long)
  1519. cmd.cmd.user_info.spu_base_info,
  1520. cmd.cmd.user_info.spu_base_info_size);
  1521. break;
  1522. case IPATH_CMD_RECV_CTRL:
  1523. ret = ipath_manage_rcvq(pd, cmd.cmd.recv_ctrl);
  1524. break;
  1525. case IPATH_CMD_PORT_INFO:
  1526. ret = ipath_port_info(pd,
  1527. (struct ipath_port_info __user *)
  1528. (unsigned long) cmd.cmd.port_info);
  1529. break;
  1530. case IPATH_CMD_TID_UPDATE:
  1531. ret = ipath_tid_update(pd, &cmd.cmd.tid_info);
  1532. break;
  1533. case IPATH_CMD_TID_FREE:
  1534. ret = ipath_tid_free(pd, &cmd.cmd.tid_info);
  1535. break;
  1536. case IPATH_CMD_SET_PART_KEY:
  1537. ret = ipath_set_part_key(pd, cmd.cmd.part_key);
  1538. break;
  1539. }
  1540. if (ret >= 0)
  1541. ret = consumed;
  1542. bail:
  1543. return ret;
  1544. }
  1545. static struct class *ipath_class;
  1546. static int init_cdev(int minor, char *name, struct file_operations *fops,
  1547. struct cdev **cdevp, struct class_device **class_devp)
  1548. {
  1549. const dev_t dev = MKDEV(IPATH_MAJOR, minor);
  1550. struct cdev *cdev = NULL;
  1551. struct class_device *class_dev = NULL;
  1552. int ret;
  1553. cdev = cdev_alloc();
  1554. if (!cdev) {
  1555. printk(KERN_ERR IPATH_DRV_NAME
  1556. ": Could not allocate cdev for minor %d, %s\n",
  1557. minor, name);
  1558. ret = -ENOMEM;
  1559. goto done;
  1560. }
  1561. cdev->owner = THIS_MODULE;
  1562. cdev->ops = fops;
  1563. kobject_set_name(&cdev->kobj, name);
  1564. ret = cdev_add(cdev, dev, 1);
  1565. if (ret < 0) {
  1566. printk(KERN_ERR IPATH_DRV_NAME
  1567. ": Could not add cdev for minor %d, %s (err %d)\n",
  1568. minor, name, -ret);
  1569. goto err_cdev;
  1570. }
  1571. class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
  1572. if (IS_ERR(class_dev)) {
  1573. ret = PTR_ERR(class_dev);
  1574. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  1575. "class_dev for minor %d, %s (err %d)\n",
  1576. minor, name, -ret);
  1577. goto err_cdev;
  1578. }
  1579. goto done;
  1580. err_cdev:
  1581. cdev_del(cdev);
  1582. cdev = NULL;
  1583. done:
  1584. if (ret >= 0) {
  1585. *cdevp = cdev;
  1586. *class_devp = class_dev;
  1587. } else {
  1588. *cdevp = NULL;
  1589. *class_devp = NULL;
  1590. }
  1591. return ret;
  1592. }
  1593. int ipath_cdev_init(int minor, char *name, struct file_operations *fops,
  1594. struct cdev **cdevp, struct class_device **class_devp)
  1595. {
  1596. return init_cdev(minor, name, fops, cdevp, class_devp);
  1597. }
  1598. static void cleanup_cdev(struct cdev **cdevp,
  1599. struct class_device **class_devp)
  1600. {
  1601. struct class_device *class_dev = *class_devp;
  1602. if (class_dev) {
  1603. class_device_unregister(class_dev);
  1604. *class_devp = NULL;
  1605. }
  1606. if (*cdevp) {
  1607. cdev_del(*cdevp);
  1608. *cdevp = NULL;
  1609. }
  1610. }
  1611. void ipath_cdev_cleanup(struct cdev **cdevp,
  1612. struct class_device **class_devp)
  1613. {
  1614. cleanup_cdev(cdevp, class_devp);
  1615. }
  1616. static struct cdev *wildcard_cdev;
  1617. static struct class_device *wildcard_class_dev;
  1618. static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
  1619. static int user_init(void)
  1620. {
  1621. int ret;
  1622. ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
  1623. if (ret < 0) {
  1624. printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
  1625. "chrdev region (err %d)\n", -ret);
  1626. goto done;
  1627. }
  1628. ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
  1629. if (IS_ERR(ipath_class)) {
  1630. ret = PTR_ERR(ipath_class);
  1631. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  1632. "device class (err %d)\n", -ret);
  1633. goto bail;
  1634. }
  1635. goto done;
  1636. bail:
  1637. unregister_chrdev_region(dev, IPATH_NMINORS);
  1638. done:
  1639. return ret;
  1640. }
  1641. static void user_cleanup(void)
  1642. {
  1643. if (ipath_class) {
  1644. class_destroy(ipath_class);
  1645. ipath_class = NULL;
  1646. }
  1647. unregister_chrdev_region(dev, IPATH_NMINORS);
  1648. }
  1649. static atomic_t user_count = ATOMIC_INIT(0);
  1650. static atomic_t user_setup = ATOMIC_INIT(0);
  1651. int ipath_user_add(struct ipath_devdata *dd)
  1652. {
  1653. char name[10];
  1654. int ret;
  1655. if (atomic_inc_return(&user_count) == 1) {
  1656. ret = user_init();
  1657. if (ret < 0) {
  1658. ipath_dev_err(dd, "Unable to set up user support: "
  1659. "error %d\n", -ret);
  1660. goto bail;
  1661. }
  1662. ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
  1663. &wildcard_class_dev);
  1664. if (ret < 0) {
  1665. ipath_dev_err(dd, "Could not create wildcard "
  1666. "minor: error %d\n", -ret);
  1667. goto bail_sma;
  1668. }
  1669. atomic_set(&user_setup, 1);
  1670. }
  1671. snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
  1672. ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
  1673. &dd->user_cdev, &dd->user_class_dev);
  1674. if (ret < 0)
  1675. ipath_dev_err(dd, "Could not create user minor %d, %s\n",
  1676. dd->ipath_unit + 1, name);
  1677. goto bail;
  1678. bail_sma:
  1679. user_cleanup();
  1680. bail:
  1681. return ret;
  1682. }
  1683. void ipath_user_remove(struct ipath_devdata *dd)
  1684. {
  1685. cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
  1686. if (atomic_dec_return(&user_count) == 0) {
  1687. if (atomic_read(&user_setup) == 0)
  1688. goto bail;
  1689. cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
  1690. user_cleanup();
  1691. atomic_set(&user_setup, 0);
  1692. }
  1693. bail:
  1694. return;
  1695. }