ipath_file_ops.c 64 KB

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