ipath_file_ops.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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. static int ipath_open(struct inode *, struct file *);
  42. static int ipath_close(struct inode *, struct file *);
  43. static ssize_t ipath_write(struct file *, const char __user *, size_t,
  44. loff_t *);
  45. static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
  46. static int ipath_mmap(struct file *, struct vm_area_struct *);
  47. static struct file_operations ipath_file_ops = {
  48. .owner = THIS_MODULE,
  49. .write = ipath_write,
  50. .open = ipath_open,
  51. .release = ipath_close,
  52. .poll = ipath_poll,
  53. .mmap = ipath_mmap
  54. };
  55. static int ipath_get_base_info(struct ipath_portdata *pd,
  56. void __user *ubase, size_t ubase_size)
  57. {
  58. int ret = 0;
  59. struct ipath_base_info *kinfo = NULL;
  60. struct ipath_devdata *dd = pd->port_dd;
  61. if (ubase_size < sizeof(*kinfo)) {
  62. ipath_cdbg(PROC,
  63. "Base size %lu, need %lu (version mismatch?)\n",
  64. (unsigned long) ubase_size,
  65. (unsigned long) sizeof(*kinfo));
  66. ret = -EINVAL;
  67. goto bail;
  68. }
  69. kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
  70. if (kinfo == NULL) {
  71. ret = -ENOMEM;
  72. goto bail;
  73. }
  74. ret = dd->ipath_f_get_base_info(pd, kinfo);
  75. if (ret < 0)
  76. goto bail;
  77. kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
  78. kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
  79. kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
  80. kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
  81. /*
  82. * have to mmap whole thing
  83. */
  84. kinfo->spi_rcv_egrbuftotlen =
  85. pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  86. kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
  87. kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
  88. pd->port_rcvegrbuf_chunks;
  89. kinfo->spi_tidcnt = dd->ipath_rcvtidcnt;
  90. /*
  91. * for this use, may be ipath_cfgports summed over all chips that
  92. * are are configured and present
  93. */
  94. kinfo->spi_nports = dd->ipath_cfgports;
  95. /* unit (chip/board) our port is on */
  96. kinfo->spi_unit = dd->ipath_unit;
  97. /* for now, only a single page */
  98. kinfo->spi_tid_maxsize = PAGE_SIZE;
  99. /*
  100. * Doing this per port, and based on the skip value, etc. This has
  101. * to be the actual buffer size, since the protocol code treats it
  102. * as an array.
  103. *
  104. * These have to be set to user addresses in the user code via mmap.
  105. * These values are used on return to user code for the mmap target
  106. * addresses only. For 32 bit, same 44 bit address problem, so use
  107. * the physical address, not virtual. Before 2.6.11, using the
  108. * page_address() macro worked, but in 2.6.11, even that returns the
  109. * full 64 bit address (upper bits all 1's). So far, using the
  110. * physical addresses (or chip offsets, for chip mapping) works, but
  111. * no doubt some future kernel release will chang that, and we'll be
  112. * on to yet another method of dealing with this
  113. */
  114. kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
  115. kinfo->spi_rcvhdr_tailaddr = (u64)pd->port_rcvhdrqtailaddr_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 == (IPATH_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. gfp_t gfp_flags;
  665. /*
  666. * GFP_USER, but without GFP_FS, so buffer cache can be
  667. * coalesced (we hope); otherwise, even at order 4,
  668. * heavy filesystem activity makes these fail, and we can
  669. * use compound pages.
  670. */
  671. gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  672. egrcnt = dd->ipath_rcvegrcnt;
  673. /* TID number offset for this port */
  674. egroff = pd->port_port * egrcnt;
  675. egrsize = dd->ipath_rcvegrbufsize;
  676. ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
  677. "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
  678. /*
  679. * to avoid wasting a lot of memory, we allocate 32KB chunks of
  680. * physically contiguous memory, advance through it until used up
  681. * and then allocate more. Of course, we need memory to store those
  682. * extra pointers, now. Started out with 256KB, but under heavy
  683. * memory pressure (creating large files and then copying them over
  684. * NFS while doing lots of MPI jobs), we hit some allocation
  685. * failures, even though we can sleep... (2.6.10) Still get
  686. * failures at 64K. 32K is the lowest we can go without wasting
  687. * additional memory.
  688. */
  689. size = 0x8000;
  690. alloced = ALIGN(egrsize * egrcnt, size);
  691. egrperchunk = size / egrsize;
  692. chunk = (egrcnt + egrperchunk - 1) / egrperchunk;
  693. pd->port_rcvegrbuf_chunks = chunk;
  694. pd->port_rcvegrbufs_perchunk = egrperchunk;
  695. pd->port_rcvegrbuf_size = size;
  696. pd->port_rcvegrbuf = vmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]));
  697. if (!pd->port_rcvegrbuf) {
  698. ret = -ENOMEM;
  699. goto bail;
  700. }
  701. pd->port_rcvegrbuf_phys =
  702. vmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]));
  703. if (!pd->port_rcvegrbuf_phys) {
  704. ret = -ENOMEM;
  705. goto bail_rcvegrbuf;
  706. }
  707. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  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. }
  739. vfree(pd->port_rcvegrbuf_phys);
  740. pd->port_rcvegrbuf_phys = NULL;
  741. bail_rcvegrbuf:
  742. vfree(pd->port_rcvegrbuf);
  743. pd->port_rcvegrbuf = NULL;
  744. bail:
  745. return ret;
  746. }
  747. static int ipath_do_user_init(struct ipath_portdata *pd,
  748. const struct ipath_user_info *uinfo)
  749. {
  750. int ret = 0;
  751. struct ipath_devdata *dd = pd->port_dd;
  752. u32 head32;
  753. /* for now, if major version is different, bail */
  754. if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) {
  755. dev_info(&dd->pcidev->dev,
  756. "User major version %d not same as driver "
  757. "major %d\n", uinfo->spu_userversion >> 16,
  758. IPATH_USER_SWMAJOR);
  759. ret = -ENODEV;
  760. goto done;
  761. }
  762. if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR)
  763. ipath_dbg("User minor version %d not same as driver "
  764. "minor %d\n", uinfo->spu_userversion & 0xffff,
  765. IPATH_USER_SWMINOR);
  766. if (uinfo->spu_rcvhdrsize) {
  767. ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
  768. if (ret)
  769. goto done;
  770. }
  771. /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
  772. /* for right now, kernel piobufs are at end, so port 1 is at 0 */
  773. pd->port_piobufs = dd->ipath_piobufbase +
  774. dd->ipath_pbufsport * (pd->port_port -
  775. 1) * dd->ipath_palign;
  776. ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
  777. pd->port_port, pd->port_piobufs);
  778. /*
  779. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  780. * array for time being. If pd->port_port > chip-supported,
  781. * we need to do extra stuff here to handle by handling overflow
  782. * through port 0, someday
  783. */
  784. ret = ipath_create_rcvhdrq(dd, pd);
  785. if (!ret)
  786. ret = ipath_create_user_egr(pd);
  787. if (ret)
  788. goto done;
  789. /*
  790. * set the eager head register for this port to the current values
  791. * of the tail pointers, since we don't know if they were
  792. * updated on last use of the port.
  793. */
  794. head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
  795. ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
  796. dd->ipath_lastegrheads[pd->port_port] = -1;
  797. dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
  798. ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
  799. pd->port_port, head32);
  800. pd->port_tidcursor = 0; /* start at beginning after open */
  801. /*
  802. * now enable the port; the tail registers will be written to memory
  803. * by the chip as soon as it sees the write to
  804. * dd->ipath_kregs->kr_rcvctrl. The update only happens on
  805. * transition from 0 to 1, so clear it first, then set it as part of
  806. * enabling the port. This will (very briefly) affect any other
  807. * open ports, but it shouldn't be long enough to be an issue.
  808. * We explictly set the in-memory copy to 0 beforehand, so we don't
  809. * have to wait to be sure the DMA update has happened.
  810. */
  811. *pd->port_rcvhdrtail_kvaddr = 0ULL;
  812. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  813. &dd->ipath_rcvctrl);
  814. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  815. dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
  816. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  817. dd->ipath_rcvctrl);
  818. done:
  819. return ret;
  820. }
  821. /* common code for the mappings on dma_alloc_coherent mem */
  822. static int ipath_mmap_mem(struct vm_area_struct *vma,
  823. struct ipath_portdata *pd, unsigned len,
  824. int write_ok, dma_addr_t addr, char *what)
  825. {
  826. struct ipath_devdata *dd = pd->port_dd;
  827. unsigned pfn = (unsigned long)addr >> PAGE_SHIFT;
  828. int ret;
  829. if ((vma->vm_end - vma->vm_start) > len) {
  830. dev_info(&dd->pcidev->dev,
  831. "FAIL on %s: len %lx > %x\n", what,
  832. vma->vm_end - vma->vm_start, len);
  833. ret = -EFAULT;
  834. goto bail;
  835. }
  836. if (!write_ok) {
  837. if (vma->vm_flags & VM_WRITE) {
  838. dev_info(&dd->pcidev->dev,
  839. "%s must be mapped readonly\n", what);
  840. ret = -EPERM;
  841. goto bail;
  842. }
  843. /* don't allow them to later change with mprotect */
  844. vma->vm_flags &= ~VM_MAYWRITE;
  845. }
  846. ret = remap_pfn_range(vma, vma->vm_start, pfn,
  847. len, vma->vm_page_prot);
  848. if (ret)
  849. dev_info(&dd->pcidev->dev,
  850. "%s port%u mmap of %lx, %x bytes r%c failed: %d\n",
  851. what, pd->port_port, (unsigned long)addr, len,
  852. write_ok?'w':'o', ret);
  853. else
  854. ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes r%c\n",
  855. what, pd->port_port, (unsigned long)addr, len,
  856. write_ok?'w':'o');
  857. bail:
  858. return ret;
  859. }
  860. static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
  861. u64 ureg)
  862. {
  863. unsigned long phys;
  864. int ret;
  865. /*
  866. * This is real hardware, so use io_remap. This is the mechanism
  867. * for the user process to update the head registers for their port
  868. * in the chip.
  869. */
  870. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  871. dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
  872. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  873. ret = -EFAULT;
  874. } else {
  875. phys = dd->ipath_physaddr + ureg;
  876. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  877. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  878. ret = io_remap_pfn_range(vma, vma->vm_start,
  879. phys >> PAGE_SHIFT,
  880. vma->vm_end - vma->vm_start,
  881. vma->vm_page_prot);
  882. }
  883. return ret;
  884. }
  885. static int mmap_piobufs(struct vm_area_struct *vma,
  886. struct ipath_devdata *dd,
  887. struct ipath_portdata *pd)
  888. {
  889. unsigned long phys;
  890. int ret;
  891. /*
  892. * When we map the PIO buffers in the chip, we want to map them as
  893. * writeonly, no read possible. This prevents access to previous
  894. * process data, and catches users who might try to read the i/o
  895. * space due to a bug.
  896. */
  897. if ((vma->vm_end - vma->vm_start) >
  898. (dd->ipath_pbufsport * dd->ipath_palign)) {
  899. dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
  900. "reqlen %lx > PAGE\n",
  901. vma->vm_end - vma->vm_start);
  902. ret = -EFAULT;
  903. goto bail;
  904. }
  905. phys = dd->ipath_physaddr + pd->port_piobufs;
  906. /*
  907. * Don't mark this as non-cached, or we don't get the
  908. * write combining behavior we want on the PIO buffers!
  909. */
  910. #if defined(__powerpc__)
  911. /* There isn't a generic way to specify writethrough mappings */
  912. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  913. pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
  914. pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
  915. #endif
  916. /*
  917. * don't allow them to later change to readable with mprotect (for when
  918. * not initially mapped readable, as is normally the case)
  919. */
  920. vma->vm_flags &= ~VM_MAYREAD;
  921. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  922. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  923. vma->vm_end - vma->vm_start,
  924. vma->vm_page_prot);
  925. bail:
  926. return ret;
  927. }
  928. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  929. struct ipath_portdata *pd)
  930. {
  931. struct ipath_devdata *dd = pd->port_dd;
  932. unsigned long start, size;
  933. size_t total_size, i;
  934. dma_addr_t *phys;
  935. int ret;
  936. size = pd->port_rcvegrbuf_size;
  937. total_size = pd->port_rcvegrbuf_chunks * size;
  938. if ((vma->vm_end - vma->vm_start) > total_size) {
  939. dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
  940. "reqlen %lx > actual %lx\n",
  941. vma->vm_end - vma->vm_start,
  942. (unsigned long) total_size);
  943. ret = -EFAULT;
  944. goto bail;
  945. }
  946. if (vma->vm_flags & VM_WRITE) {
  947. dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
  948. "writable (flags=%lx)\n", vma->vm_flags);
  949. ret = -EPERM;
  950. goto bail;
  951. }
  952. /* don't allow them to later change to writeable with mprotect */
  953. vma->vm_flags &= ~VM_MAYWRITE;
  954. start = vma->vm_start;
  955. phys = pd->port_rcvegrbuf_phys;
  956. for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
  957. ret = remap_pfn_range(vma, start, phys[i] >> PAGE_SHIFT,
  958. size, vma->vm_page_prot);
  959. if (ret < 0)
  960. goto bail;
  961. }
  962. ret = 0;
  963. bail:
  964. return ret;
  965. }
  966. /**
  967. * ipath_mmap - mmap various structures into user space
  968. * @fp: the file pointer
  969. * @vma: the VM area
  970. *
  971. * We use this to have a shared buffer between the kernel and the user code
  972. * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
  973. * buffers in the chip. We have the open and close entries so we can bump
  974. * the ref count and keep the driver from being unloaded while still mapped.
  975. */
  976. static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
  977. {
  978. struct ipath_portdata *pd;
  979. struct ipath_devdata *dd;
  980. u64 pgaddr, ureg;
  981. int ret;
  982. pd = port_fp(fp);
  983. dd = pd->port_dd;
  984. /*
  985. * This is the ipath_do_user_init() code, mapping the shared buffers
  986. * into the user process. The address referred to by vm_pgoff is the
  987. * virtual, not physical, address; we only do one mmap for each
  988. * space mapped.
  989. */
  990. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  991. /*
  992. * Must fit in 40 bits for our hardware; some checked elsewhere,
  993. * but we'll be paranoid. Check for 0 is mostly in case one of the
  994. * allocations failed, but user called mmap anyway. We want to catch
  995. * that before it can match.
  996. */
  997. if (!pgaddr || pgaddr >= (1ULL<<40)) {
  998. ipath_dev_err(dd, "Bad phys addr %llx, start %lx, end %lx\n",
  999. (unsigned long long)pgaddr, vma->vm_start, vma->vm_end);
  1000. return -EINVAL;
  1001. }
  1002. /* just the offset of the port user registers, not physical addr */
  1003. ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  1004. ipath_cdbg(MM, "ushare: pgaddr %llx vm_start=%lx, vmlen %lx\n",
  1005. (unsigned long long) pgaddr, vma->vm_start,
  1006. vma->vm_end - vma->vm_start);
  1007. if (vma->vm_start & (PAGE_SIZE-1)) {
  1008. ipath_dev_err(dd,
  1009. "vm_start not aligned: %lx, end=%lx phys %lx\n",
  1010. vma->vm_start, vma->vm_end, (unsigned long)pgaddr);
  1011. ret = -EINVAL;
  1012. }
  1013. else if (pgaddr == ureg)
  1014. ret = mmap_ureg(vma, dd, ureg);
  1015. else if (pgaddr == pd->port_piobufs)
  1016. ret = mmap_piobufs(vma, dd, pd);
  1017. else if (pgaddr == (u64) pd->port_rcvegr_phys)
  1018. ret = mmap_rcvegrbufs(vma, pd);
  1019. else if (pgaddr == (u64) pd->port_rcvhdrq_phys) {
  1020. /*
  1021. * The rcvhdrq itself; readonly except on HT (so have
  1022. * to allow writable mapping), multiple pages, contiguous
  1023. * from an i/o perspective.
  1024. */
  1025. unsigned total_size =
  1026. ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize
  1027. * sizeof(u32), PAGE_SIZE);
  1028. ret = ipath_mmap_mem(vma, pd, total_size, 1,
  1029. pd->port_rcvhdrq_phys,
  1030. "rcvhdrq");
  1031. }
  1032. else if (pgaddr == (u64)pd->port_rcvhdrqtailaddr_phys)
  1033. /* in-memory copy of rcvhdrq tail register */
  1034. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1035. pd->port_rcvhdrqtailaddr_phys,
  1036. "rcvhdrq tail");
  1037. else if (pgaddr == dd->ipath_pioavailregs_phys)
  1038. /* in-memory copy of pioavail registers */
  1039. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1040. dd->ipath_pioavailregs_phys,
  1041. "pioavail registers");
  1042. else
  1043. ret = -EINVAL;
  1044. vma->vm_private_data = NULL;
  1045. if (ret < 0)
  1046. dev_info(&dd->pcidev->dev,
  1047. "Failure %d on addr %lx, off %lx\n",
  1048. -ret, vma->vm_start, vma->vm_pgoff);
  1049. return ret;
  1050. }
  1051. static unsigned int ipath_poll(struct file *fp,
  1052. struct poll_table_struct *pt)
  1053. {
  1054. struct ipath_portdata *pd;
  1055. u32 head, tail;
  1056. int bit;
  1057. unsigned pollflag = 0;
  1058. struct ipath_devdata *dd;
  1059. pd = port_fp(fp);
  1060. dd = pd->port_dd;
  1061. bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
  1062. set_bit(bit, &dd->ipath_rcvctrl);
  1063. /*
  1064. * Before blocking, make sure that head is still == tail,
  1065. * reading from the chip, so we can be sure the interrupt
  1066. * enable has made it to the chip. If not equal, disable
  1067. * interrupt again and return immediately. This avoids races,
  1068. * and the overhead of the chip read doesn't matter much at
  1069. * this point, since we are waiting for something anyway.
  1070. */
  1071. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1072. dd->ipath_rcvctrl);
  1073. head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
  1074. tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  1075. if (tail == head) {
  1076. set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1077. if(dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
  1078. (void)ipath_write_ureg(dd, ur_rcvhdrhead,
  1079. dd->ipath_rhdrhead_intr_off
  1080. | head, pd->port_port);
  1081. poll_wait(fp, &pd->port_wait, pt);
  1082. if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
  1083. /* timed out, no packets received */
  1084. clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1085. pd->port_rcvwait_to++;
  1086. }
  1087. else
  1088. pollflag = POLLIN | POLLRDNORM;
  1089. }
  1090. else {
  1091. /* it's already happened; don't do wait_event overhead */
  1092. pollflag = POLLIN | POLLRDNORM;
  1093. pd->port_rcvnowait++;
  1094. }
  1095. clear_bit(bit, &dd->ipath_rcvctrl);
  1096. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1097. dd->ipath_rcvctrl);
  1098. return pollflag;
  1099. }
  1100. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1101. struct file *fp)
  1102. {
  1103. int ret;
  1104. if (!dd->ipath_pd[port]) {
  1105. void *p, *ptmp;
  1106. p = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1107. /*
  1108. * Allocate memory for use in ipath_tid_update() just once
  1109. * at open, not per call. Reduces cost of expected send
  1110. * setup.
  1111. */
  1112. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1113. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1114. GFP_KERNEL);
  1115. if (!p || !ptmp) {
  1116. ipath_dev_err(dd, "Unable to allocate portdata "
  1117. "memory, failing open\n");
  1118. ret = -ENOMEM;
  1119. kfree(p);
  1120. kfree(ptmp);
  1121. goto bail;
  1122. }
  1123. dd->ipath_pd[port] = p;
  1124. dd->ipath_pd[port]->port_port = port;
  1125. dd->ipath_pd[port]->port_dd = dd;
  1126. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1127. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1128. }
  1129. if (!dd->ipath_pd[port]->port_cnt) {
  1130. dd->ipath_pd[port]->port_cnt = 1;
  1131. fp->private_data = (void *) dd->ipath_pd[port];
  1132. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1133. current->comm, current->pid, dd->ipath_unit,
  1134. port);
  1135. dd->ipath_pd[port]->port_pid = current->pid;
  1136. strncpy(dd->ipath_pd[port]->port_comm, current->comm,
  1137. sizeof(dd->ipath_pd[port]->port_comm));
  1138. ipath_stats.sps_ports++;
  1139. ret = 0;
  1140. goto bail;
  1141. }
  1142. ret = -EBUSY;
  1143. bail:
  1144. return ret;
  1145. }
  1146. static inline int usable(struct ipath_devdata *dd)
  1147. {
  1148. return dd &&
  1149. (dd->ipath_flags & IPATH_PRESENT) &&
  1150. dd->ipath_kregbase &&
  1151. dd->ipath_lid &&
  1152. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1153. | IPATH_LINKUNK));
  1154. }
  1155. static int find_free_port(int unit, struct file *fp)
  1156. {
  1157. struct ipath_devdata *dd = ipath_lookup(unit);
  1158. int ret, i;
  1159. if (!dd) {
  1160. ret = -ENODEV;
  1161. goto bail;
  1162. }
  1163. if (!usable(dd)) {
  1164. ret = -ENETDOWN;
  1165. goto bail;
  1166. }
  1167. for (i = 0; i < dd->ipath_cfgports; i++) {
  1168. ret = try_alloc_port(dd, i, fp);
  1169. if (ret != -EBUSY)
  1170. goto bail;
  1171. }
  1172. ret = -EBUSY;
  1173. bail:
  1174. return ret;
  1175. }
  1176. static int find_best_unit(struct file *fp)
  1177. {
  1178. int ret = 0, i, prefunit = -1, devmax;
  1179. int maxofallports, npresent, nup;
  1180. int ndev;
  1181. (void) ipath_count_units(&npresent, &nup, &maxofallports);
  1182. /*
  1183. * This code is present to allow a knowledgeable person to
  1184. * specify the layout of processes to processors before opening
  1185. * this driver, and then we'll assign the process to the "closest"
  1186. * InfiniPath chip to that processor (we assume reasonable connectivity,
  1187. * for now). This code assumes that if affinity has been set
  1188. * before this point, that at most one cpu is set; for now this
  1189. * is reasonable. I check for both cpus_empty() and cpus_full(),
  1190. * in case some kernel variant sets none of the bits when no
  1191. * affinity is set. 2.6.11 and 12 kernels have all present
  1192. * cpus set. Some day we'll have to fix it up further to handle
  1193. * a cpu subset. This algorithm fails for two HT chips connected
  1194. * in tunnel fashion. Eventually this needs real topology
  1195. * information. There may be some issues with dual core numbering
  1196. * as well. This needs more work prior to release.
  1197. */
  1198. if (!cpus_empty(current->cpus_allowed) &&
  1199. !cpus_full(current->cpus_allowed)) {
  1200. int ncpus = num_online_cpus(), curcpu = -1;
  1201. for (i = 0; i < ncpus; i++)
  1202. if (cpu_isset(i, current->cpus_allowed)) {
  1203. ipath_cdbg(PROC, "%s[%u] affinity set for "
  1204. "cpu %d\n", current->comm,
  1205. current->pid, i);
  1206. curcpu = i;
  1207. }
  1208. if (curcpu != -1) {
  1209. if (npresent) {
  1210. prefunit = curcpu / (ncpus / npresent);
  1211. ipath_dbg("%s[%u] %d chips, %d cpus, "
  1212. "%d cpus/chip, select unit %d\n",
  1213. current->comm, current->pid,
  1214. npresent, ncpus, ncpus / npresent,
  1215. prefunit);
  1216. }
  1217. }
  1218. }
  1219. /*
  1220. * user ports start at 1, kernel port is 0
  1221. * For now, we do round-robin access across all chips
  1222. */
  1223. if (prefunit != -1)
  1224. devmax = prefunit + 1;
  1225. else
  1226. devmax = ipath_count_units(NULL, NULL, NULL);
  1227. recheck:
  1228. for (i = 1; i < maxofallports; i++) {
  1229. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1230. ndev++) {
  1231. struct ipath_devdata *dd = ipath_lookup(ndev);
  1232. if (!usable(dd))
  1233. continue; /* can't use this unit */
  1234. if (i >= dd->ipath_cfgports)
  1235. /*
  1236. * Maxed out on users of this unit. Try
  1237. * next.
  1238. */
  1239. continue;
  1240. ret = try_alloc_port(dd, i, fp);
  1241. if (!ret)
  1242. goto done;
  1243. }
  1244. }
  1245. if (npresent) {
  1246. if (nup == 0) {
  1247. ret = -ENETDOWN;
  1248. ipath_dbg("No ports available (none initialized "
  1249. "and ready)\n");
  1250. } else {
  1251. if (prefunit > 0) {
  1252. /* if started above 0, retry from 0 */
  1253. ipath_cdbg(PROC,
  1254. "%s[%u] no ports on prefunit "
  1255. "%d, clear and re-check\n",
  1256. current->comm, current->pid,
  1257. prefunit);
  1258. devmax = ipath_count_units(NULL, NULL,
  1259. NULL);
  1260. prefunit = -1;
  1261. goto recheck;
  1262. }
  1263. ret = -EBUSY;
  1264. ipath_dbg("No ports available\n");
  1265. }
  1266. } else {
  1267. ret = -ENXIO;
  1268. ipath_dbg("No boards found\n");
  1269. }
  1270. done:
  1271. return ret;
  1272. }
  1273. static int ipath_open(struct inode *in, struct file *fp)
  1274. {
  1275. int ret, user_minor;
  1276. mutex_lock(&ipath_mutex);
  1277. user_minor = iminor(in) - IPATH_USER_MINOR_BASE;
  1278. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1279. (long)in->i_rdev, user_minor);
  1280. if (user_minor)
  1281. ret = find_free_port(user_minor - 1, fp);
  1282. else
  1283. ret = find_best_unit(fp);
  1284. mutex_unlock(&ipath_mutex);
  1285. return ret;
  1286. }
  1287. /**
  1288. * unlock_exptid - unlock any expected TID entries port still had in use
  1289. * @pd: port
  1290. *
  1291. * We don't actually update the chip here, because we do a bulk update
  1292. * below, using ipath_f_clear_tids.
  1293. */
  1294. static void unlock_expected_tids(struct ipath_portdata *pd)
  1295. {
  1296. struct ipath_devdata *dd = pd->port_dd;
  1297. int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
  1298. int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1299. ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
  1300. pd->port_port);
  1301. for (i = port_tidbase; i < maxtid; i++) {
  1302. if (!dd->ipath_pageshadow[i])
  1303. continue;
  1304. ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
  1305. 1);
  1306. dd->ipath_pageshadow[i] = NULL;
  1307. cnt++;
  1308. ipath_stats.sps_pageunlocks++;
  1309. }
  1310. if (cnt)
  1311. ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
  1312. pd->port_port, cnt);
  1313. if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
  1314. ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
  1315. (unsigned long long) ipath_stats.sps_pagelocks,
  1316. (unsigned long long)
  1317. ipath_stats.sps_pageunlocks);
  1318. }
  1319. static int ipath_close(struct inode *in, struct file *fp)
  1320. {
  1321. int ret = 0;
  1322. struct ipath_portdata *pd;
  1323. struct ipath_devdata *dd;
  1324. unsigned port;
  1325. ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
  1326. (long)in->i_rdev, fp->private_data);
  1327. mutex_lock(&ipath_mutex);
  1328. pd = port_fp(fp);
  1329. port = pd->port_port;
  1330. fp->private_data = NULL;
  1331. dd = pd->port_dd;
  1332. if (pd->port_hdrqfull) {
  1333. ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
  1334. "during run\n", pd->port_comm, pd->port_pid,
  1335. pd->port_hdrqfull);
  1336. pd->port_hdrqfull = 0;
  1337. }
  1338. if (pd->port_rcvwait_to || pd->port_piowait_to
  1339. || pd->port_rcvnowait || pd->port_pionowait) {
  1340. ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
  1341. "%u rcv %u, pio already\n",
  1342. pd->port_port, pd->port_rcvwait_to,
  1343. pd->port_piowait_to, pd->port_rcvnowait,
  1344. pd->port_pionowait);
  1345. pd->port_rcvwait_to = pd->port_piowait_to =
  1346. pd->port_rcvnowait = pd->port_pionowait = 0;
  1347. }
  1348. if (pd->port_flag) {
  1349. ipath_dbg("port %u port_flag still set to 0x%lx\n",
  1350. pd->port_port, pd->port_flag);
  1351. pd->port_flag = 0;
  1352. }
  1353. if (dd->ipath_kregbase) {
  1354. int i;
  1355. /* atomically clear receive enable port. */
  1356. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
  1357. &dd->ipath_rcvctrl);
  1358. ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
  1359. dd->ipath_rcvctrl);
  1360. /* and read back from chip to be sure that nothing
  1361. * else is in flight when we do the rest */
  1362. (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1363. /* clean up the pkeys for this port user */
  1364. ipath_clean_part_key(pd, dd);
  1365. /*
  1366. * be paranoid, and never write 0's to these, just use an
  1367. * unused part of the port 0 tail page. Of course,
  1368. * rcvhdraddr points to a large chunk of memory, so this
  1369. * could still trash things, but at least it won't trash
  1370. * page 0, and by disabling the port, it should stop "soon",
  1371. * even if a packet or two is in already in flight after we
  1372. * disabled the port.
  1373. */
  1374. ipath_write_kreg_port(dd,
  1375. dd->ipath_kregs->kr_rcvhdrtailaddr, port,
  1376. dd->ipath_dummy_hdrq_phys);
  1377. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1378. pd->port_port, dd->ipath_dummy_hdrq_phys);
  1379. i = dd->ipath_pbufsport * (port - 1);
  1380. ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
  1381. if (dd->ipath_pageshadow)
  1382. unlock_expected_tids(pd);
  1383. ipath_stats.sps_ports--;
  1384. ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
  1385. pd->port_comm, pd->port_pid,
  1386. dd->ipath_unit, port);
  1387. dd->ipath_f_clear_tids(dd, pd->port_port);
  1388. }
  1389. pd->port_cnt = 0;
  1390. pd->port_pid = 0;
  1391. dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
  1392. mutex_unlock(&ipath_mutex);
  1393. ipath_free_pddata(dd, pd); /* after releasing the mutex */
  1394. return ret;
  1395. }
  1396. static int ipath_port_info(struct ipath_portdata *pd,
  1397. struct ipath_port_info __user *uinfo)
  1398. {
  1399. struct ipath_port_info info;
  1400. int nup;
  1401. int ret;
  1402. (void) ipath_count_units(NULL, &nup, NULL);
  1403. info.num_active = nup;
  1404. info.unit = pd->port_dd->ipath_unit;
  1405. info.port = pd->port_port;
  1406. if (copy_to_user(uinfo, &info, sizeof(info))) {
  1407. ret = -EFAULT;
  1408. goto bail;
  1409. }
  1410. ret = 0;
  1411. bail:
  1412. return ret;
  1413. }
  1414. static ssize_t ipath_write(struct file *fp, const char __user *data,
  1415. size_t count, loff_t *off)
  1416. {
  1417. const struct ipath_cmd __user *ucmd;
  1418. struct ipath_portdata *pd;
  1419. const void __user *src;
  1420. size_t consumed, copy;
  1421. struct ipath_cmd cmd;
  1422. ssize_t ret = 0;
  1423. void *dest;
  1424. if (count < sizeof(cmd.type)) {
  1425. ret = -EINVAL;
  1426. goto bail;
  1427. }
  1428. ucmd = (const struct ipath_cmd __user *) data;
  1429. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1430. ret = -EFAULT;
  1431. goto bail;
  1432. }
  1433. consumed = sizeof(cmd.type);
  1434. switch (cmd.type) {
  1435. case IPATH_CMD_USER_INIT:
  1436. copy = sizeof(cmd.cmd.user_info);
  1437. dest = &cmd.cmd.user_info;
  1438. src = &ucmd->cmd.user_info;
  1439. break;
  1440. case IPATH_CMD_RECV_CTRL:
  1441. copy = sizeof(cmd.cmd.recv_ctrl);
  1442. dest = &cmd.cmd.recv_ctrl;
  1443. src = &ucmd->cmd.recv_ctrl;
  1444. break;
  1445. case IPATH_CMD_PORT_INFO:
  1446. copy = sizeof(cmd.cmd.port_info);
  1447. dest = &cmd.cmd.port_info;
  1448. src = &ucmd->cmd.port_info;
  1449. break;
  1450. case IPATH_CMD_TID_UPDATE:
  1451. case IPATH_CMD_TID_FREE:
  1452. copy = sizeof(cmd.cmd.tid_info);
  1453. dest = &cmd.cmd.tid_info;
  1454. src = &ucmd->cmd.tid_info;
  1455. break;
  1456. case IPATH_CMD_SET_PART_KEY:
  1457. copy = sizeof(cmd.cmd.part_key);
  1458. dest = &cmd.cmd.part_key;
  1459. src = &ucmd->cmd.part_key;
  1460. break;
  1461. default:
  1462. ret = -EINVAL;
  1463. goto bail;
  1464. }
  1465. if ((count - consumed) < copy) {
  1466. ret = -EINVAL;
  1467. goto bail;
  1468. }
  1469. if (copy_from_user(dest, src, copy)) {
  1470. ret = -EFAULT;
  1471. goto bail;
  1472. }
  1473. consumed += copy;
  1474. pd = port_fp(fp);
  1475. switch (cmd.type) {
  1476. case IPATH_CMD_USER_INIT:
  1477. ret = ipath_do_user_init(pd, &cmd.cmd.user_info);
  1478. if (ret < 0)
  1479. goto bail;
  1480. ret = ipath_get_base_info(
  1481. pd, (void __user *) (unsigned long)
  1482. cmd.cmd.user_info.spu_base_info,
  1483. cmd.cmd.user_info.spu_base_info_size);
  1484. break;
  1485. case IPATH_CMD_RECV_CTRL:
  1486. ret = ipath_manage_rcvq(pd, cmd.cmd.recv_ctrl);
  1487. break;
  1488. case IPATH_CMD_PORT_INFO:
  1489. ret = ipath_port_info(pd,
  1490. (struct ipath_port_info __user *)
  1491. (unsigned long) cmd.cmd.port_info);
  1492. break;
  1493. case IPATH_CMD_TID_UPDATE:
  1494. ret = ipath_tid_update(pd, &cmd.cmd.tid_info);
  1495. break;
  1496. case IPATH_CMD_TID_FREE:
  1497. ret = ipath_tid_free(pd, &cmd.cmd.tid_info);
  1498. break;
  1499. case IPATH_CMD_SET_PART_KEY:
  1500. ret = ipath_set_part_key(pd, cmd.cmd.part_key);
  1501. break;
  1502. }
  1503. if (ret >= 0)
  1504. ret = consumed;
  1505. bail:
  1506. return ret;
  1507. }
  1508. static struct class *ipath_class;
  1509. static int init_cdev(int minor, char *name, struct file_operations *fops,
  1510. struct cdev **cdevp, struct class_device **class_devp)
  1511. {
  1512. const dev_t dev = MKDEV(IPATH_MAJOR, minor);
  1513. struct cdev *cdev = NULL;
  1514. struct class_device *class_dev = NULL;
  1515. int ret;
  1516. cdev = cdev_alloc();
  1517. if (!cdev) {
  1518. printk(KERN_ERR IPATH_DRV_NAME
  1519. ": Could not allocate cdev for minor %d, %s\n",
  1520. minor, name);
  1521. ret = -ENOMEM;
  1522. goto done;
  1523. }
  1524. cdev->owner = THIS_MODULE;
  1525. cdev->ops = fops;
  1526. kobject_set_name(&cdev->kobj, name);
  1527. ret = cdev_add(cdev, dev, 1);
  1528. if (ret < 0) {
  1529. printk(KERN_ERR IPATH_DRV_NAME
  1530. ": Could not add cdev for minor %d, %s (err %d)\n",
  1531. minor, name, -ret);
  1532. goto err_cdev;
  1533. }
  1534. class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
  1535. if (IS_ERR(class_dev)) {
  1536. ret = PTR_ERR(class_dev);
  1537. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  1538. "class_dev for minor %d, %s (err %d)\n",
  1539. minor, name, -ret);
  1540. goto err_cdev;
  1541. }
  1542. goto done;
  1543. err_cdev:
  1544. cdev_del(cdev);
  1545. cdev = NULL;
  1546. done:
  1547. if (ret >= 0) {
  1548. *cdevp = cdev;
  1549. *class_devp = class_dev;
  1550. } else {
  1551. *cdevp = NULL;
  1552. *class_devp = NULL;
  1553. }
  1554. return ret;
  1555. }
  1556. int ipath_cdev_init(int minor, char *name, struct file_operations *fops,
  1557. struct cdev **cdevp, struct class_device **class_devp)
  1558. {
  1559. return init_cdev(minor, name, fops, cdevp, class_devp);
  1560. }
  1561. static void cleanup_cdev(struct cdev **cdevp,
  1562. struct class_device **class_devp)
  1563. {
  1564. struct class_device *class_dev = *class_devp;
  1565. if (class_dev) {
  1566. class_device_unregister(class_dev);
  1567. *class_devp = NULL;
  1568. }
  1569. if (*cdevp) {
  1570. cdev_del(*cdevp);
  1571. *cdevp = NULL;
  1572. }
  1573. }
  1574. void ipath_cdev_cleanup(struct cdev **cdevp,
  1575. struct class_device **class_devp)
  1576. {
  1577. cleanup_cdev(cdevp, class_devp);
  1578. }
  1579. static struct cdev *wildcard_cdev;
  1580. static struct class_device *wildcard_class_dev;
  1581. static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
  1582. static int user_init(void)
  1583. {
  1584. int ret;
  1585. ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
  1586. if (ret < 0) {
  1587. printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
  1588. "chrdev region (err %d)\n", -ret);
  1589. goto done;
  1590. }
  1591. ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
  1592. if (IS_ERR(ipath_class)) {
  1593. ret = PTR_ERR(ipath_class);
  1594. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  1595. "device class (err %d)\n", -ret);
  1596. goto bail;
  1597. }
  1598. goto done;
  1599. bail:
  1600. unregister_chrdev_region(dev, IPATH_NMINORS);
  1601. done:
  1602. return ret;
  1603. }
  1604. static void user_cleanup(void)
  1605. {
  1606. if (ipath_class) {
  1607. class_destroy(ipath_class);
  1608. ipath_class = NULL;
  1609. }
  1610. unregister_chrdev_region(dev, IPATH_NMINORS);
  1611. }
  1612. static atomic_t user_count = ATOMIC_INIT(0);
  1613. static atomic_t user_setup = ATOMIC_INIT(0);
  1614. int ipath_user_add(struct ipath_devdata *dd)
  1615. {
  1616. char name[10];
  1617. int ret;
  1618. if (atomic_inc_return(&user_count) == 1) {
  1619. ret = user_init();
  1620. if (ret < 0) {
  1621. ipath_dev_err(dd, "Unable to set up user support: "
  1622. "error %d\n", -ret);
  1623. goto bail;
  1624. }
  1625. ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
  1626. &wildcard_class_dev);
  1627. if (ret < 0) {
  1628. ipath_dev_err(dd, "Could not create wildcard "
  1629. "minor: error %d\n", -ret);
  1630. goto bail_user;
  1631. }
  1632. atomic_set(&user_setup, 1);
  1633. }
  1634. snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
  1635. ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
  1636. &dd->user_cdev, &dd->user_class_dev);
  1637. if (ret < 0)
  1638. ipath_dev_err(dd, "Could not create user minor %d, %s\n",
  1639. dd->ipath_unit + 1, name);
  1640. goto bail;
  1641. bail_user:
  1642. user_cleanup();
  1643. bail:
  1644. return ret;
  1645. }
  1646. void ipath_user_remove(struct ipath_devdata *dd)
  1647. {
  1648. cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
  1649. if (atomic_dec_return(&user_count) == 0) {
  1650. if (atomic_read(&user_setup) == 0)
  1651. goto bail;
  1652. cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
  1653. user_cleanup();
  1654. atomic_set(&user_setup, 0);
  1655. }
  1656. bail:
  1657. return;
  1658. }