ipath_file_ops.c 68 KB

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