ipath_file_ops.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  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. struct page *p;
  505. p = dd->ipath_pageshadow[porttid + tid];
  506. dd->ipath_pageshadow[porttid + tid] = NULL;
  507. ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
  508. pd->port_pid, tid);
  509. dd->ipath_f_put_tid(dd, &tidbase[tid],
  510. RCVHQ_RCV_TYPE_EXPECTED,
  511. dd->ipath_tidinvalid);
  512. pci_unmap_page(dd->pcidev,
  513. dd->ipath_physshadow[porttid + tid],
  514. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  515. ipath_release_user_pages(&p, 1);
  516. ipath_stats.sps_pageunlocks++;
  517. } else
  518. ipath_dbg("Unused tid %u, ignoring\n", tid);
  519. }
  520. if (cnt != ti->tidcnt)
  521. ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
  522. ti->tidcnt, cnt);
  523. done:
  524. if (ret)
  525. ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
  526. ti->tidcnt, -ret);
  527. return ret;
  528. }
  529. /**
  530. * ipath_set_part_key - set a partition key
  531. * @pd: the port
  532. * @key: the key
  533. *
  534. * We can have up to 4 active at a time (other than the default, which is
  535. * always allowed). This is somewhat tricky, since multiple ports may set
  536. * the same key, so we reference count them, and clean up at exit. All 4
  537. * partition keys are packed into a single infinipath register. It's an
  538. * error for a process to set the same pkey multiple times. We provide no
  539. * mechanism to de-allocate a pkey at this time, we may eventually need to
  540. * do that. I've used the atomic operations, and no locking, and only make
  541. * a single pass through what's available. This should be more than
  542. * adequate for some time. I'll think about spinlocks or the like if and as
  543. * it's necessary.
  544. */
  545. static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
  546. {
  547. struct ipath_devdata *dd = pd->port_dd;
  548. int i, any = 0, pidx = -1;
  549. u16 lkey = key & 0x7FFF;
  550. int ret;
  551. if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
  552. /* nothing to do; this key always valid */
  553. ret = 0;
  554. goto bail;
  555. }
  556. ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
  557. "%hx:%x %hx:%x %hx:%x %hx:%x\n",
  558. pd->port_port, key, dd->ipath_pkeys[0],
  559. atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
  560. atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
  561. atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
  562. atomic_read(&dd->ipath_pkeyrefs[3]));
  563. if (!lkey) {
  564. ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
  565. pd->port_port);
  566. ret = -EINVAL;
  567. goto bail;
  568. }
  569. /*
  570. * Set the full membership bit, because it has to be
  571. * set in the register or the packet, and it seems
  572. * cleaner to set in the register than to force all
  573. * callers to set it. (see bug 4331)
  574. */
  575. key |= 0x8000;
  576. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  577. if (!pd->port_pkeys[i] && pidx == -1)
  578. pidx = i;
  579. if (pd->port_pkeys[i] == key) {
  580. ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
  581. "(%x) more than once\n",
  582. pd->port_port, key);
  583. ret = -EEXIST;
  584. goto bail;
  585. }
  586. }
  587. if (pidx == -1) {
  588. ipath_dbg("All pkeys for port %u already in use, "
  589. "can't set %x\n", pd->port_port, key);
  590. ret = -EBUSY;
  591. goto bail;
  592. }
  593. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  594. if (!dd->ipath_pkeys[i]) {
  595. any++;
  596. continue;
  597. }
  598. if (dd->ipath_pkeys[i] == key) {
  599. atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
  600. if (atomic_inc_return(pkrefs) > 1) {
  601. pd->port_pkeys[pidx] = key;
  602. ipath_cdbg(VERBOSE, "p%u set key %x "
  603. "matches #%d, count now %d\n",
  604. pd->port_port, key, i,
  605. atomic_read(pkrefs));
  606. ret = 0;
  607. goto bail;
  608. } else {
  609. /*
  610. * lost race, decrement count, catch below
  611. */
  612. atomic_dec(pkrefs);
  613. ipath_cdbg(VERBOSE, "Lost race, count was "
  614. "0, after dec, it's %d\n",
  615. atomic_read(pkrefs));
  616. any++;
  617. }
  618. }
  619. if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
  620. /*
  621. * It makes no sense to have both the limited and
  622. * full membership PKEY set at the same time since
  623. * the unlimited one will disable the limited one.
  624. */
  625. ret = -EEXIST;
  626. goto bail;
  627. }
  628. }
  629. if (!any) {
  630. ipath_dbg("port %u, all pkeys already in use, "
  631. "can't set %x\n", pd->port_port, key);
  632. ret = -EBUSY;
  633. goto bail;
  634. }
  635. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  636. if (!dd->ipath_pkeys[i] &&
  637. atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
  638. u64 pkey;
  639. /* for ipathstats, etc. */
  640. ipath_stats.sps_pkeys[i] = lkey;
  641. pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
  642. pkey =
  643. (u64) dd->ipath_pkeys[0] |
  644. ((u64) dd->ipath_pkeys[1] << 16) |
  645. ((u64) dd->ipath_pkeys[2] << 32) |
  646. ((u64) dd->ipath_pkeys[3] << 48);
  647. ipath_cdbg(PROC, "p%u set key %x in #%d, "
  648. "portidx %d, new pkey reg %llx\n",
  649. pd->port_port, key, i, pidx,
  650. (unsigned long long) pkey);
  651. ipath_write_kreg(
  652. dd, dd->ipath_kregs->kr_partitionkey, pkey);
  653. ret = 0;
  654. goto bail;
  655. }
  656. }
  657. ipath_dbg("port %u, all pkeys already in use 2nd pass, "
  658. "can't set %x\n", pd->port_port, key);
  659. ret = -EBUSY;
  660. bail:
  661. return ret;
  662. }
  663. /**
  664. * ipath_manage_rcvq - manage a port's receive queue
  665. * @pd: the port
  666. * @subport: the subport
  667. * @start_stop: action to carry out
  668. *
  669. * start_stop == 0 disables receive on the port, for use in queue
  670. * overflow conditions. start_stop==1 re-enables, to be used to
  671. * re-init the software copy of the head register
  672. */
  673. static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
  674. int start_stop)
  675. {
  676. struct ipath_devdata *dd = pd->port_dd;
  677. ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
  678. start_stop ? "en" : "dis", dd->ipath_unit,
  679. pd->port_port, subport);
  680. if (subport)
  681. goto bail;
  682. /* atomically clear receive enable port. */
  683. if (start_stop) {
  684. /*
  685. * On enable, force in-memory copy of the tail register to
  686. * 0, so that protocol code doesn't have to worry about
  687. * whether or not the chip has yet updated the in-memory
  688. * copy or not on return from the system call. The chip
  689. * always resets it's tail register back to 0 on a
  690. * transition from disabled to enabled. This could cause a
  691. * problem if software was broken, and did the enable w/o
  692. * the disable, but eventually the in-memory copy will be
  693. * updated and correct itself, even in the face of software
  694. * bugs.
  695. */
  696. *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0;
  697. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  698. &dd->ipath_rcvctrl);
  699. } else
  700. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  701. &dd->ipath_rcvctrl);
  702. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  703. dd->ipath_rcvctrl);
  704. /* now be sure chip saw it before we return */
  705. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  706. if (start_stop) {
  707. /*
  708. * And try to be sure that tail reg update has happened too.
  709. * This should in theory interlock with the RXE changes to
  710. * the tail register. Don't assign it to the tail register
  711. * in memory copy, since we could overwrite an update by the
  712. * chip if we did.
  713. */
  714. ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  715. }
  716. /* always; new head should be equal to new tail; see above */
  717. bail:
  718. return 0;
  719. }
  720. static void ipath_clean_part_key(struct ipath_portdata *pd,
  721. struct ipath_devdata *dd)
  722. {
  723. int i, j, pchanged = 0;
  724. u64 oldpkey;
  725. /* for debugging only */
  726. oldpkey = (u64) dd->ipath_pkeys[0] |
  727. ((u64) dd->ipath_pkeys[1] << 16) |
  728. ((u64) dd->ipath_pkeys[2] << 32) |
  729. ((u64) dd->ipath_pkeys[3] << 48);
  730. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  731. if (!pd->port_pkeys[i])
  732. continue;
  733. ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
  734. pd->port_pkeys[i]);
  735. for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
  736. /* check for match independent of the global bit */
  737. if ((dd->ipath_pkeys[j] & 0x7fff) !=
  738. (pd->port_pkeys[i] & 0x7fff))
  739. continue;
  740. if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
  741. ipath_cdbg(VERBOSE, "p%u clear key "
  742. "%x matches #%d\n",
  743. pd->port_port,
  744. pd->port_pkeys[i], j);
  745. ipath_stats.sps_pkeys[j] =
  746. dd->ipath_pkeys[j] = 0;
  747. pchanged++;
  748. }
  749. else ipath_cdbg(
  750. VERBOSE, "p%u key %x matches #%d, "
  751. "but ref still %d\n", pd->port_port,
  752. pd->port_pkeys[i], j,
  753. atomic_read(&dd->ipath_pkeyrefs[j]));
  754. break;
  755. }
  756. pd->port_pkeys[i] = 0;
  757. }
  758. if (pchanged) {
  759. u64 pkey = (u64) dd->ipath_pkeys[0] |
  760. ((u64) dd->ipath_pkeys[1] << 16) |
  761. ((u64) dd->ipath_pkeys[2] << 32) |
  762. ((u64) dd->ipath_pkeys[3] << 48);
  763. ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
  764. "new pkey reg %llx\n", pd->port_port,
  765. (unsigned long long) oldpkey,
  766. (unsigned long long) pkey);
  767. ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
  768. pkey);
  769. }
  770. }
  771. /*
  772. * Initialize the port data with the receive buffer sizes
  773. * so this can be done while the master port is locked.
  774. * Otherwise, there is a race with a slave opening the port
  775. * and seeing these fields uninitialized.
  776. */
  777. static void init_user_egr_sizes(struct ipath_portdata *pd)
  778. {
  779. struct ipath_devdata *dd = pd->port_dd;
  780. unsigned egrperchunk, egrcnt, size;
  781. /*
  782. * to avoid wasting a lot of memory, we allocate 32KB chunks of
  783. * physically contiguous memory, advance through it until used up
  784. * and then allocate more. Of course, we need memory to store those
  785. * extra pointers, now. Started out with 256KB, but under heavy
  786. * memory pressure (creating large files and then copying them over
  787. * NFS while doing lots of MPI jobs), we hit some allocation
  788. * failures, even though we can sleep... (2.6.10) Still get
  789. * failures at 64K. 32K is the lowest we can go without wasting
  790. * additional memory.
  791. */
  792. size = 0x8000;
  793. egrperchunk = size / dd->ipath_rcvegrbufsize;
  794. egrcnt = dd->ipath_rcvegrcnt;
  795. pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
  796. pd->port_rcvegrbufs_perchunk = egrperchunk;
  797. pd->port_rcvegrbuf_size = size;
  798. }
  799. /**
  800. * ipath_create_user_egr - allocate eager TID buffers
  801. * @pd: the port to allocate TID buffers for
  802. *
  803. * This routine is now quite different for user and kernel, because
  804. * the kernel uses skb's, for the accelerated network performance
  805. * This is the user port version
  806. *
  807. * Allocate the eager TID buffers and program them into infinipath
  808. * They are no longer completely contiguous, we do multiple allocation
  809. * calls.
  810. */
  811. static int ipath_create_user_egr(struct ipath_portdata *pd)
  812. {
  813. struct ipath_devdata *dd = pd->port_dd;
  814. unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
  815. size_t size;
  816. int ret;
  817. gfp_t gfp_flags;
  818. /*
  819. * GFP_USER, but without GFP_FS, so buffer cache can be
  820. * coalesced (we hope); otherwise, even at order 4,
  821. * heavy filesystem activity makes these fail, and we can
  822. * use compound pages.
  823. */
  824. gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  825. egrcnt = dd->ipath_rcvegrcnt;
  826. /* TID number offset for this port */
  827. egroff = pd->port_port * egrcnt;
  828. egrsize = dd->ipath_rcvegrbufsize;
  829. ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
  830. "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
  831. chunk = pd->port_rcvegrbuf_chunks;
  832. egrperchunk = pd->port_rcvegrbufs_perchunk;
  833. size = pd->port_rcvegrbuf_size;
  834. pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
  835. GFP_KERNEL);
  836. if (!pd->port_rcvegrbuf) {
  837. ret = -ENOMEM;
  838. goto bail;
  839. }
  840. pd->port_rcvegrbuf_phys =
  841. kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
  842. GFP_KERNEL);
  843. if (!pd->port_rcvegrbuf_phys) {
  844. ret = -ENOMEM;
  845. goto bail_rcvegrbuf;
  846. }
  847. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  848. pd->port_rcvegrbuf[e] = dma_alloc_coherent(
  849. &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
  850. gfp_flags);
  851. if (!pd->port_rcvegrbuf[e]) {
  852. ret = -ENOMEM;
  853. goto bail_rcvegrbuf_phys;
  854. }
  855. }
  856. pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
  857. for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
  858. dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
  859. unsigned i;
  860. for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
  861. dd->ipath_f_put_tid(dd, e + egroff +
  862. (u64 __iomem *)
  863. ((char __iomem *)
  864. dd->ipath_kregbase +
  865. dd->ipath_rcvegrbase),
  866. RCVHQ_RCV_TYPE_EAGER, pa);
  867. pa += egrsize;
  868. }
  869. cond_resched(); /* don't hog the cpu */
  870. }
  871. ret = 0;
  872. goto bail;
  873. bail_rcvegrbuf_phys:
  874. for (e = 0; e < pd->port_rcvegrbuf_chunks &&
  875. pd->port_rcvegrbuf[e]; e++) {
  876. dma_free_coherent(&dd->pcidev->dev, size,
  877. pd->port_rcvegrbuf[e],
  878. pd->port_rcvegrbuf_phys[e]);
  879. }
  880. kfree(pd->port_rcvegrbuf_phys);
  881. pd->port_rcvegrbuf_phys = NULL;
  882. bail_rcvegrbuf:
  883. kfree(pd->port_rcvegrbuf);
  884. pd->port_rcvegrbuf = NULL;
  885. bail:
  886. return ret;
  887. }
  888. /* common code for the mappings on dma_alloc_coherent mem */
  889. static int ipath_mmap_mem(struct vm_area_struct *vma,
  890. struct ipath_portdata *pd, unsigned len, int write_ok,
  891. void *kvaddr, char *what)
  892. {
  893. struct ipath_devdata *dd = pd->port_dd;
  894. unsigned long pfn;
  895. int ret;
  896. if ((vma->vm_end - vma->vm_start) > len) {
  897. dev_info(&dd->pcidev->dev,
  898. "FAIL on %s: len %lx > %x\n", what,
  899. vma->vm_end - vma->vm_start, len);
  900. ret = -EFAULT;
  901. goto bail;
  902. }
  903. if (!write_ok) {
  904. if (vma->vm_flags & VM_WRITE) {
  905. dev_info(&dd->pcidev->dev,
  906. "%s must be mapped readonly\n", what);
  907. ret = -EPERM;
  908. goto bail;
  909. }
  910. /* don't allow them to later change with mprotect */
  911. vma->vm_flags &= ~VM_MAYWRITE;
  912. }
  913. pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
  914. ret = remap_pfn_range(vma, vma->vm_start, pfn,
  915. len, vma->vm_page_prot);
  916. if (ret)
  917. dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
  918. "bytes r%c failed: %d\n", what, pd->port_port,
  919. pfn, len, write_ok?'w':'o', ret);
  920. else
  921. ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
  922. "r%c\n", what, pd->port_port, pfn, len,
  923. write_ok?'w':'o');
  924. bail:
  925. return ret;
  926. }
  927. static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
  928. u64 ureg)
  929. {
  930. unsigned long phys;
  931. int ret;
  932. /*
  933. * This is real hardware, so use io_remap. This is the mechanism
  934. * for the user process to update the head registers for their port
  935. * in the chip.
  936. */
  937. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  938. dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
  939. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  940. ret = -EFAULT;
  941. } else {
  942. phys = dd->ipath_physaddr + ureg;
  943. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  944. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  945. ret = io_remap_pfn_range(vma, vma->vm_start,
  946. phys >> PAGE_SHIFT,
  947. vma->vm_end - vma->vm_start,
  948. vma->vm_page_prot);
  949. }
  950. return ret;
  951. }
  952. static int mmap_piobufs(struct vm_area_struct *vma,
  953. struct ipath_devdata *dd,
  954. struct ipath_portdata *pd,
  955. unsigned piobufs, unsigned piocnt)
  956. {
  957. unsigned long phys;
  958. int ret;
  959. /*
  960. * When we map the PIO buffers in the chip, we want to map them as
  961. * writeonly, no read possible. This prevents access to previous
  962. * process data, and catches users who might try to read the i/o
  963. * space due to a bug.
  964. */
  965. if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
  966. dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
  967. "reqlen %lx > PAGE\n",
  968. vma->vm_end - vma->vm_start);
  969. ret = -EINVAL;
  970. goto bail;
  971. }
  972. phys = dd->ipath_physaddr + piobufs;
  973. /*
  974. * Don't mark this as non-cached, or we don't get the
  975. * write combining behavior we want on the PIO buffers!
  976. */
  977. #if defined(__powerpc__)
  978. /* There isn't a generic way to specify writethrough mappings */
  979. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  980. pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
  981. pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
  982. #endif
  983. /*
  984. * don't allow them to later change to readable with mprotect (for when
  985. * not initially mapped readable, as is normally the case)
  986. */
  987. vma->vm_flags &= ~VM_MAYREAD;
  988. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  989. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  990. vma->vm_end - vma->vm_start,
  991. vma->vm_page_prot);
  992. bail:
  993. return ret;
  994. }
  995. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  996. struct ipath_portdata *pd)
  997. {
  998. struct ipath_devdata *dd = pd->port_dd;
  999. unsigned long start, size;
  1000. size_t total_size, i;
  1001. unsigned long pfn;
  1002. int ret;
  1003. size = pd->port_rcvegrbuf_size;
  1004. total_size = pd->port_rcvegrbuf_chunks * size;
  1005. if ((vma->vm_end - vma->vm_start) > total_size) {
  1006. dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
  1007. "reqlen %lx > actual %lx\n",
  1008. vma->vm_end - vma->vm_start,
  1009. (unsigned long) total_size);
  1010. ret = -EINVAL;
  1011. goto bail;
  1012. }
  1013. if (vma->vm_flags & VM_WRITE) {
  1014. dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
  1015. "writable (flags=%lx)\n", vma->vm_flags);
  1016. ret = -EPERM;
  1017. goto bail;
  1018. }
  1019. /* don't allow them to later change to writeable with mprotect */
  1020. vma->vm_flags &= ~VM_MAYWRITE;
  1021. start = vma->vm_start;
  1022. for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
  1023. pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
  1024. ret = remap_pfn_range(vma, start, pfn, size,
  1025. vma->vm_page_prot);
  1026. if (ret < 0)
  1027. goto bail;
  1028. }
  1029. ret = 0;
  1030. bail:
  1031. return ret;
  1032. }
  1033. /*
  1034. * ipath_file_vma_nopage - handle a VMA page fault.
  1035. */
  1036. static struct page *ipath_file_vma_nopage(struct vm_area_struct *vma,
  1037. unsigned long address, int *type)
  1038. {
  1039. unsigned long offset = address - vma->vm_start;
  1040. struct page *page = NOPAGE_SIGBUS;
  1041. void *pageptr;
  1042. /*
  1043. * Convert the vmalloc address into a struct page.
  1044. */
  1045. pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
  1046. page = vmalloc_to_page(pageptr);
  1047. if (!page)
  1048. goto out;
  1049. /* Increment the reference count. */
  1050. get_page(page);
  1051. if (type)
  1052. *type = VM_FAULT_MINOR;
  1053. out:
  1054. return page;
  1055. }
  1056. static struct vm_operations_struct ipath_file_vm_ops = {
  1057. .nopage = ipath_file_vma_nopage,
  1058. };
  1059. static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
  1060. struct ipath_portdata *pd, unsigned subport)
  1061. {
  1062. unsigned long len;
  1063. struct ipath_devdata *dd;
  1064. void *addr;
  1065. size_t size;
  1066. int ret = 0;
  1067. /* If the port is not shared, all addresses should be physical */
  1068. if (!pd->port_subport_cnt)
  1069. goto bail;
  1070. dd = pd->port_dd;
  1071. size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  1072. /*
  1073. * Each process has all the subport uregbase, rcvhdrq, and
  1074. * rcvegrbufs mmapped - as an array for all the processes,
  1075. * and also separately for this process.
  1076. */
  1077. if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
  1078. addr = pd->subport_uregbase;
  1079. size = PAGE_SIZE * pd->port_subport_cnt;
  1080. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
  1081. addr = pd->subport_rcvhdr_base;
  1082. size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
  1083. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
  1084. addr = pd->subport_rcvegrbuf;
  1085. size *= pd->port_subport_cnt;
  1086. } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
  1087. PAGE_SIZE * subport)) {
  1088. addr = pd->subport_uregbase + PAGE_SIZE * subport;
  1089. size = PAGE_SIZE;
  1090. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
  1091. pd->port_rcvhdrq_size * subport)) {
  1092. addr = pd->subport_rcvhdr_base +
  1093. pd->port_rcvhdrq_size * subport;
  1094. size = pd->port_rcvhdrq_size;
  1095. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
  1096. size * subport)) {
  1097. addr = pd->subport_rcvegrbuf + size * subport;
  1098. /* rcvegrbufs are read-only on the slave */
  1099. if (vma->vm_flags & VM_WRITE) {
  1100. dev_info(&dd->pcidev->dev,
  1101. "Can't map eager buffers as "
  1102. "writable (flags=%lx)\n", vma->vm_flags);
  1103. ret = -EPERM;
  1104. goto bail;
  1105. }
  1106. /*
  1107. * Don't allow permission to later change to writeable
  1108. * with mprotect.
  1109. */
  1110. vma->vm_flags &= ~VM_MAYWRITE;
  1111. } else {
  1112. goto bail;
  1113. }
  1114. len = vma->vm_end - vma->vm_start;
  1115. if (len > size) {
  1116. ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
  1117. ret = -EINVAL;
  1118. goto bail;
  1119. }
  1120. vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
  1121. vma->vm_ops = &ipath_file_vm_ops;
  1122. vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
  1123. ret = 1;
  1124. bail:
  1125. return ret;
  1126. }
  1127. /**
  1128. * ipath_mmap - mmap various structures into user space
  1129. * @fp: the file pointer
  1130. * @vma: the VM area
  1131. *
  1132. * We use this to have a shared buffer between the kernel and the user code
  1133. * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
  1134. * buffers in the chip. We have the open and close entries so we can bump
  1135. * the ref count and keep the driver from being unloaded while still mapped.
  1136. */
  1137. static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
  1138. {
  1139. struct ipath_portdata *pd;
  1140. struct ipath_devdata *dd;
  1141. u64 pgaddr, ureg;
  1142. unsigned piobufs, piocnt;
  1143. int ret;
  1144. pd = port_fp(fp);
  1145. if (!pd) {
  1146. ret = -EINVAL;
  1147. goto bail;
  1148. }
  1149. dd = pd->port_dd;
  1150. /*
  1151. * This is the ipath_do_user_init() code, mapping the shared buffers
  1152. * into the user process. The address referred to by vm_pgoff is the
  1153. * file offset passed via mmap(). For shared ports, this is the
  1154. * kernel vmalloc() address of the pages to share with the master.
  1155. * For non-shared or master ports, this is a physical address.
  1156. * We only do one mmap for each space mapped.
  1157. */
  1158. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  1159. /*
  1160. * Check for 0 in case one of the allocations failed, but user
  1161. * called mmap anyway.
  1162. */
  1163. if (!pgaddr) {
  1164. ret = -EINVAL;
  1165. goto bail;
  1166. }
  1167. ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
  1168. (unsigned long long) pgaddr, vma->vm_start,
  1169. vma->vm_end - vma->vm_start, dd->ipath_unit,
  1170. pd->port_port, subport_fp(fp));
  1171. /*
  1172. * Physical addresses must fit in 40 bits for our hardware.
  1173. * Check for kernel virtual addresses first, anything else must
  1174. * match a HW or memory address.
  1175. */
  1176. ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
  1177. if (ret) {
  1178. if (ret > 0)
  1179. ret = 0;
  1180. goto bail;
  1181. }
  1182. ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
  1183. if (!pd->port_subport_cnt) {
  1184. /* port is not shared */
  1185. piocnt = dd->ipath_pbufsport;
  1186. piobufs = pd->port_piobufs;
  1187. } else if (!subport_fp(fp)) {
  1188. /* caller is the master */
  1189. piocnt = (dd->ipath_pbufsport / pd->port_subport_cnt) +
  1190. (dd->ipath_pbufsport % pd->port_subport_cnt);
  1191. piobufs = pd->port_piobufs +
  1192. dd->ipath_palign * (dd->ipath_pbufsport - piocnt);
  1193. } else {
  1194. unsigned slave = subport_fp(fp) - 1;
  1195. /* caller is a slave */
  1196. piocnt = dd->ipath_pbufsport / pd->port_subport_cnt;
  1197. piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
  1198. }
  1199. if (pgaddr == ureg)
  1200. ret = mmap_ureg(vma, dd, ureg);
  1201. else if (pgaddr == piobufs)
  1202. ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
  1203. else if (pgaddr == dd->ipath_pioavailregs_phys)
  1204. /* in-memory copy of pioavail registers */
  1205. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1206. (void *) dd->ipath_pioavailregs_dma,
  1207. "pioavail registers");
  1208. else if (pgaddr == pd->port_rcvegr_phys)
  1209. ret = mmap_rcvegrbufs(vma, pd);
  1210. else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
  1211. /*
  1212. * The rcvhdrq itself; readonly except on HT (so have
  1213. * to allow writable mapping), multiple pages, contiguous
  1214. * from an i/o perspective.
  1215. */
  1216. ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
  1217. pd->port_rcvhdrq,
  1218. "rcvhdrq");
  1219. else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
  1220. /* in-memory copy of rcvhdrq tail register */
  1221. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1222. pd->port_rcvhdrtail_kvaddr,
  1223. "rcvhdrq tail");
  1224. else
  1225. ret = -EINVAL;
  1226. vma->vm_private_data = NULL;
  1227. if (ret < 0)
  1228. dev_info(&dd->pcidev->dev,
  1229. "Failure %d on off %llx len %lx\n",
  1230. -ret, (unsigned long long)pgaddr,
  1231. vma->vm_end - vma->vm_start);
  1232. bail:
  1233. return ret;
  1234. }
  1235. static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
  1236. {
  1237. unsigned pollflag = 0;
  1238. if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
  1239. pd->port_hdrqfull != pd->port_hdrqfull_poll) {
  1240. pollflag |= POLLIN | POLLRDNORM;
  1241. pd->port_hdrqfull_poll = pd->port_hdrqfull;
  1242. }
  1243. return pollflag;
  1244. }
  1245. static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
  1246. struct file *fp,
  1247. struct poll_table_struct *pt)
  1248. {
  1249. unsigned pollflag = 0;
  1250. struct ipath_devdata *dd;
  1251. dd = pd->port_dd;
  1252. /* variable access in ipath_poll_hdrqfull() needs this */
  1253. rmb();
  1254. pollflag = ipath_poll_hdrqfull(pd);
  1255. if (pd->port_urgent != pd->port_urgent_poll) {
  1256. pollflag |= POLLIN | POLLRDNORM;
  1257. pd->port_urgent_poll = pd->port_urgent;
  1258. }
  1259. if (!pollflag) {
  1260. /* this saves a spin_lock/unlock in interrupt handler... */
  1261. set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
  1262. /* flush waiting flag so don't miss an event... */
  1263. wmb();
  1264. poll_wait(fp, &pd->port_wait, pt);
  1265. }
  1266. return pollflag;
  1267. }
  1268. static unsigned int ipath_poll_next(struct ipath_portdata *pd,
  1269. struct file *fp,
  1270. struct poll_table_struct *pt)
  1271. {
  1272. u32 head;
  1273. u32 tail;
  1274. unsigned pollflag = 0;
  1275. struct ipath_devdata *dd;
  1276. dd = pd->port_dd;
  1277. /* variable access in ipath_poll_hdrqfull() needs this */
  1278. rmb();
  1279. pollflag = ipath_poll_hdrqfull(pd);
  1280. head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
  1281. tail = *(volatile u64 *)pd->port_rcvhdrtail_kvaddr;
  1282. if (head != tail)
  1283. pollflag |= POLLIN | POLLRDNORM;
  1284. else {
  1285. /* this saves a spin_lock/unlock in interrupt handler */
  1286. set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1287. /* flush waiting flag so we don't miss an event */
  1288. wmb();
  1289. set_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT,
  1290. &dd->ipath_rcvctrl);
  1291. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1292. dd->ipath_rcvctrl);
  1293. if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
  1294. ipath_write_ureg(dd, ur_rcvhdrhead,
  1295. dd->ipath_rhdrhead_intr_off | head,
  1296. pd->port_port);
  1297. poll_wait(fp, &pd->port_wait, pt);
  1298. }
  1299. return pollflag;
  1300. }
  1301. static unsigned int ipath_poll(struct file *fp,
  1302. struct poll_table_struct *pt)
  1303. {
  1304. struct ipath_portdata *pd;
  1305. unsigned pollflag;
  1306. pd = port_fp(fp);
  1307. if (!pd)
  1308. pollflag = 0;
  1309. else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
  1310. pollflag = ipath_poll_urgent(pd, fp, pt);
  1311. else
  1312. pollflag = ipath_poll_next(pd, fp, pt);
  1313. return pollflag;
  1314. }
  1315. static int ipath_supports_subports(int user_swmajor, int user_swminor)
  1316. {
  1317. /* no subport implementation prior to software version 1.3 */
  1318. return (user_swmajor > 1) || (user_swminor >= 3);
  1319. }
  1320. static int ipath_compatible_subports(int user_swmajor, int user_swminor)
  1321. {
  1322. /* this code is written long-hand for clarity */
  1323. if (IPATH_USER_SWMAJOR != user_swmajor) {
  1324. /* no promise of compatibility if major mismatch */
  1325. return 0;
  1326. }
  1327. if (IPATH_USER_SWMAJOR == 1) {
  1328. switch (IPATH_USER_SWMINOR) {
  1329. case 0:
  1330. case 1:
  1331. case 2:
  1332. /* no subport implementation so cannot be compatible */
  1333. return 0;
  1334. case 3:
  1335. /* 3 is only compatible with itself */
  1336. return user_swminor == 3;
  1337. default:
  1338. /* >= 4 are compatible (or are expected to be) */
  1339. return user_swminor >= 4;
  1340. }
  1341. }
  1342. /* make no promises yet for future major versions */
  1343. return 0;
  1344. }
  1345. static int init_subports(struct ipath_devdata *dd,
  1346. struct ipath_portdata *pd,
  1347. const struct ipath_user_info *uinfo)
  1348. {
  1349. int ret = 0;
  1350. unsigned num_subports;
  1351. size_t size;
  1352. /*
  1353. * If the user is requesting zero subports,
  1354. * skip the subport allocation.
  1355. */
  1356. if (uinfo->spu_subport_cnt <= 0)
  1357. goto bail;
  1358. /* Self-consistency check for ipath_compatible_subports() */
  1359. if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
  1360. !ipath_compatible_subports(IPATH_USER_SWMAJOR,
  1361. IPATH_USER_SWMINOR)) {
  1362. dev_info(&dd->pcidev->dev,
  1363. "Inconsistent ipath_compatible_subports()\n");
  1364. goto bail;
  1365. }
  1366. /* Check for subport compatibility */
  1367. if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
  1368. uinfo->spu_userversion & 0xffff)) {
  1369. dev_info(&dd->pcidev->dev,
  1370. "Mismatched user version (%d.%d) and driver "
  1371. "version (%d.%d) while port sharing. Ensure "
  1372. "that driver and library are from the same "
  1373. "release.\n",
  1374. (int) (uinfo->spu_userversion >> 16),
  1375. (int) (uinfo->spu_userversion & 0xffff),
  1376. IPATH_USER_SWMAJOR,
  1377. IPATH_USER_SWMINOR);
  1378. goto bail;
  1379. }
  1380. if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
  1381. ret = -EINVAL;
  1382. goto bail;
  1383. }
  1384. num_subports = uinfo->spu_subport_cnt;
  1385. pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
  1386. if (!pd->subport_uregbase) {
  1387. ret = -ENOMEM;
  1388. goto bail;
  1389. }
  1390. /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
  1391. size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1392. sizeof(u32), PAGE_SIZE) * num_subports;
  1393. pd->subport_rcvhdr_base = vmalloc(size);
  1394. if (!pd->subport_rcvhdr_base) {
  1395. ret = -ENOMEM;
  1396. goto bail_ureg;
  1397. }
  1398. pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
  1399. pd->port_rcvegrbuf_size *
  1400. num_subports);
  1401. if (!pd->subport_rcvegrbuf) {
  1402. ret = -ENOMEM;
  1403. goto bail_rhdr;
  1404. }
  1405. pd->port_subport_cnt = uinfo->spu_subport_cnt;
  1406. pd->port_subport_id = uinfo->spu_subport_id;
  1407. pd->active_slaves = 1;
  1408. set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
  1409. memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
  1410. memset(pd->subport_rcvhdr_base, 0, size);
  1411. memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
  1412. pd->port_rcvegrbuf_size *
  1413. num_subports);
  1414. goto bail;
  1415. bail_rhdr:
  1416. vfree(pd->subport_rcvhdr_base);
  1417. bail_ureg:
  1418. vfree(pd->subport_uregbase);
  1419. pd->subport_uregbase = NULL;
  1420. bail:
  1421. return ret;
  1422. }
  1423. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1424. struct file *fp,
  1425. const struct ipath_user_info *uinfo)
  1426. {
  1427. struct ipath_portdata *pd;
  1428. int ret;
  1429. if (!(pd = dd->ipath_pd[port])) {
  1430. void *ptmp;
  1431. pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1432. /*
  1433. * Allocate memory for use in ipath_tid_update() just once
  1434. * at open, not per call. Reduces cost of expected send
  1435. * setup.
  1436. */
  1437. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1438. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1439. GFP_KERNEL);
  1440. if (!pd || !ptmp) {
  1441. ipath_dev_err(dd, "Unable to allocate portdata "
  1442. "memory, failing open\n");
  1443. ret = -ENOMEM;
  1444. kfree(pd);
  1445. kfree(ptmp);
  1446. goto bail;
  1447. }
  1448. dd->ipath_pd[port] = pd;
  1449. dd->ipath_pd[port]->port_port = port;
  1450. dd->ipath_pd[port]->port_dd = dd;
  1451. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1452. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1453. }
  1454. if (!pd->port_cnt) {
  1455. pd->userversion = uinfo->spu_userversion;
  1456. init_user_egr_sizes(pd);
  1457. if ((ret = init_subports(dd, pd, uinfo)) != 0)
  1458. goto bail;
  1459. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1460. current->comm, current->pid, dd->ipath_unit,
  1461. port);
  1462. pd->port_cnt = 1;
  1463. port_fp(fp) = pd;
  1464. pd->port_pid = current->pid;
  1465. strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
  1466. ipath_stats.sps_ports++;
  1467. ret = 0;
  1468. } else
  1469. ret = -EBUSY;
  1470. bail:
  1471. return ret;
  1472. }
  1473. static inline int usable(struct ipath_devdata *dd)
  1474. {
  1475. return dd &&
  1476. (dd->ipath_flags & IPATH_PRESENT) &&
  1477. dd->ipath_kregbase &&
  1478. dd->ipath_lid &&
  1479. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1480. | IPATH_LINKUNK));
  1481. }
  1482. static int find_free_port(int unit, struct file *fp,
  1483. const struct ipath_user_info *uinfo)
  1484. {
  1485. struct ipath_devdata *dd = ipath_lookup(unit);
  1486. int ret, i;
  1487. if (!dd) {
  1488. ret = -ENODEV;
  1489. goto bail;
  1490. }
  1491. if (!usable(dd)) {
  1492. ret = -ENETDOWN;
  1493. goto bail;
  1494. }
  1495. for (i = 1; i < dd->ipath_cfgports; i++) {
  1496. ret = try_alloc_port(dd, i, fp, uinfo);
  1497. if (ret != -EBUSY)
  1498. goto bail;
  1499. }
  1500. ret = -EBUSY;
  1501. bail:
  1502. return ret;
  1503. }
  1504. static int find_best_unit(struct file *fp,
  1505. const struct ipath_user_info *uinfo)
  1506. {
  1507. int ret = 0, i, prefunit = -1, devmax;
  1508. int maxofallports, npresent, nup;
  1509. int ndev;
  1510. devmax = ipath_count_units(&npresent, &nup, &maxofallports);
  1511. /*
  1512. * This code is present to allow a knowledgeable person to
  1513. * specify the layout of processes to processors before opening
  1514. * this driver, and then we'll assign the process to the "closest"
  1515. * InfiniPath chip to that processor (we assume reasonable connectivity,
  1516. * for now). This code assumes that if affinity has been set
  1517. * before this point, that at most one cpu is set; for now this
  1518. * is reasonable. I check for both cpus_empty() and cpus_full(),
  1519. * in case some kernel variant sets none of the bits when no
  1520. * affinity is set. 2.6.11 and 12 kernels have all present
  1521. * cpus set. Some day we'll have to fix it up further to handle
  1522. * a cpu subset. This algorithm fails for two HT chips connected
  1523. * in tunnel fashion. Eventually this needs real topology
  1524. * information. There may be some issues with dual core numbering
  1525. * as well. This needs more work prior to release.
  1526. */
  1527. if (!cpus_empty(current->cpus_allowed) &&
  1528. !cpus_full(current->cpus_allowed)) {
  1529. int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
  1530. for (i = 0; i < ncpus; i++)
  1531. if (cpu_isset(i, current->cpus_allowed)) {
  1532. ipath_cdbg(PROC, "%s[%u] affinity set for "
  1533. "cpu %d/%d\n", current->comm,
  1534. current->pid, i, ncpus);
  1535. curcpu = i;
  1536. nset++;
  1537. }
  1538. if (curcpu != -1 && nset != ncpus) {
  1539. if (npresent) {
  1540. prefunit = curcpu / (ncpus / npresent);
  1541. ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
  1542. "%d cpus/chip, select unit %d\n",
  1543. current->comm, current->pid,
  1544. npresent, ncpus, ncpus / npresent,
  1545. prefunit);
  1546. }
  1547. }
  1548. }
  1549. /*
  1550. * user ports start at 1, kernel port is 0
  1551. * For now, we do round-robin access across all chips
  1552. */
  1553. if (prefunit != -1)
  1554. devmax = prefunit + 1;
  1555. recheck:
  1556. for (i = 1; i < maxofallports; i++) {
  1557. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1558. ndev++) {
  1559. struct ipath_devdata *dd = ipath_lookup(ndev);
  1560. if (!usable(dd))
  1561. continue; /* can't use this unit */
  1562. if (i >= dd->ipath_cfgports)
  1563. /*
  1564. * Maxed out on users of this unit. Try
  1565. * next.
  1566. */
  1567. continue;
  1568. ret = try_alloc_port(dd, i, fp, uinfo);
  1569. if (!ret)
  1570. goto done;
  1571. }
  1572. }
  1573. if (npresent) {
  1574. if (nup == 0) {
  1575. ret = -ENETDOWN;
  1576. ipath_dbg("No ports available (none initialized "
  1577. "and ready)\n");
  1578. } else {
  1579. if (prefunit > 0) {
  1580. /* if started above 0, retry from 0 */
  1581. ipath_cdbg(PROC,
  1582. "%s[%u] no ports on prefunit "
  1583. "%d, clear and re-check\n",
  1584. current->comm, current->pid,
  1585. prefunit);
  1586. devmax = ipath_count_units(NULL, NULL,
  1587. NULL);
  1588. prefunit = -1;
  1589. goto recheck;
  1590. }
  1591. ret = -EBUSY;
  1592. ipath_dbg("No ports available\n");
  1593. }
  1594. } else {
  1595. ret = -ENXIO;
  1596. ipath_dbg("No boards found\n");
  1597. }
  1598. done:
  1599. return ret;
  1600. }
  1601. static int find_shared_port(struct file *fp,
  1602. const struct ipath_user_info *uinfo)
  1603. {
  1604. int devmax, ndev, i;
  1605. int ret = 0;
  1606. devmax = ipath_count_units(NULL, NULL, NULL);
  1607. for (ndev = 0; ndev < devmax; ndev++) {
  1608. struct ipath_devdata *dd = ipath_lookup(ndev);
  1609. if (!dd)
  1610. continue;
  1611. for (i = 1; i < dd->ipath_cfgports; i++) {
  1612. struct ipath_portdata *pd = dd->ipath_pd[i];
  1613. /* Skip ports which are not yet open */
  1614. if (!pd || !pd->port_cnt)
  1615. continue;
  1616. /* Skip port if it doesn't match the requested one */
  1617. if (pd->port_subport_id != uinfo->spu_subport_id)
  1618. continue;
  1619. /* Verify the sharing process matches the master */
  1620. if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
  1621. pd->userversion != uinfo->spu_userversion ||
  1622. pd->port_cnt >= pd->port_subport_cnt) {
  1623. ret = -EINVAL;
  1624. goto done;
  1625. }
  1626. port_fp(fp) = pd;
  1627. subport_fp(fp) = pd->port_cnt++;
  1628. tidcursor_fp(fp) = 0;
  1629. pd->active_slaves |= 1 << subport_fp(fp);
  1630. ipath_cdbg(PROC,
  1631. "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
  1632. current->comm, current->pid,
  1633. subport_fp(fp),
  1634. pd->port_comm, pd->port_pid,
  1635. dd->ipath_unit, pd->port_port);
  1636. ret = 1;
  1637. goto done;
  1638. }
  1639. }
  1640. done:
  1641. return ret;
  1642. }
  1643. static int ipath_open(struct inode *in, struct file *fp)
  1644. {
  1645. /* The real work is performed later in ipath_assign_port() */
  1646. fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
  1647. return fp->private_data ? 0 : -ENOMEM;
  1648. }
  1649. /* Get port early, so can set affinity prior to memory allocation */
  1650. static int ipath_assign_port(struct file *fp,
  1651. const struct ipath_user_info *uinfo)
  1652. {
  1653. int ret;
  1654. int i_minor;
  1655. unsigned swmajor, swminor;
  1656. /* Check to be sure we haven't already initialized this file */
  1657. if (port_fp(fp)) {
  1658. ret = -EINVAL;
  1659. goto done;
  1660. }
  1661. /* for now, if major version is different, bail */
  1662. swmajor = uinfo->spu_userversion >> 16;
  1663. if (swmajor != IPATH_USER_SWMAJOR) {
  1664. ipath_dbg("User major version %d not same as driver "
  1665. "major %d\n", uinfo->spu_userversion >> 16,
  1666. IPATH_USER_SWMAJOR);
  1667. ret = -ENODEV;
  1668. goto done;
  1669. }
  1670. swminor = uinfo->spu_userversion & 0xffff;
  1671. if (swminor != IPATH_USER_SWMINOR)
  1672. ipath_dbg("User minor version %d not same as driver "
  1673. "minor %d\n", swminor, IPATH_USER_SWMINOR);
  1674. mutex_lock(&ipath_mutex);
  1675. if (ipath_compatible_subports(swmajor, swminor) &&
  1676. uinfo->spu_subport_cnt &&
  1677. (ret = find_shared_port(fp, uinfo))) {
  1678. mutex_unlock(&ipath_mutex);
  1679. if (ret > 0)
  1680. ret = 0;
  1681. goto done;
  1682. }
  1683. i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
  1684. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1685. (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
  1686. if (i_minor)
  1687. ret = find_free_port(i_minor - 1, fp, uinfo);
  1688. else
  1689. ret = find_best_unit(fp, uinfo);
  1690. mutex_unlock(&ipath_mutex);
  1691. done:
  1692. return ret;
  1693. }
  1694. static int ipath_do_user_init(struct file *fp,
  1695. const struct ipath_user_info *uinfo)
  1696. {
  1697. int ret;
  1698. struct ipath_portdata *pd = port_fp(fp);
  1699. struct ipath_devdata *dd;
  1700. u32 head32;
  1701. /* Subports don't need to initialize anything since master did it. */
  1702. if (subport_fp(fp)) {
  1703. ret = wait_event_interruptible(pd->port_wait,
  1704. !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
  1705. goto done;
  1706. }
  1707. dd = pd->port_dd;
  1708. if (uinfo->spu_rcvhdrsize) {
  1709. ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
  1710. if (ret)
  1711. goto done;
  1712. }
  1713. /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
  1714. /* for right now, kernel piobufs are at end, so port 1 is at 0 */
  1715. pd->port_piobufs = dd->ipath_piobufbase +
  1716. dd->ipath_pbufsport * (pd->port_port - 1) * dd->ipath_palign;
  1717. ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
  1718. pd->port_port, pd->port_piobufs);
  1719. /*
  1720. * Now allocate the rcvhdr Q and eager TIDs; skip the TID
  1721. * array for time being. If pd->port_port > chip-supported,
  1722. * we need to do extra stuff here to handle by handling overflow
  1723. * through port 0, someday
  1724. */
  1725. ret = ipath_create_rcvhdrq(dd, pd);
  1726. if (!ret)
  1727. ret = ipath_create_user_egr(pd);
  1728. if (ret)
  1729. goto done;
  1730. /*
  1731. * set the eager head register for this port to the current values
  1732. * of the tail pointers, since we don't know if they were
  1733. * updated on last use of the port.
  1734. */
  1735. head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
  1736. ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
  1737. dd->ipath_lastegrheads[pd->port_port] = -1;
  1738. dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
  1739. ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
  1740. pd->port_port, head32);
  1741. pd->port_tidcursor = 0; /* start at beginning after open */
  1742. /* initialize poll variables... */
  1743. pd->port_urgent = 0;
  1744. pd->port_urgent_poll = 0;
  1745. pd->port_hdrqfull_poll = pd->port_hdrqfull;
  1746. /*
  1747. * now enable the port; the tail registers will be written to memory
  1748. * by the chip as soon as it sees the write to
  1749. * dd->ipath_kregs->kr_rcvctrl. The update only happens on
  1750. * transition from 0 to 1, so clear it first, then set it as part of
  1751. * enabling the port. This will (very briefly) affect any other
  1752. * open ports, but it shouldn't be long enough to be an issue.
  1753. * We explictly set the in-memory copy to 0 beforehand, so we don't
  1754. * have to wait to be sure the DMA update has happened.
  1755. */
  1756. *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL;
  1757. set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
  1758. &dd->ipath_rcvctrl);
  1759. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1760. dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
  1761. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1762. dd->ipath_rcvctrl);
  1763. /* Notify any waiting slaves */
  1764. if (pd->port_subport_cnt) {
  1765. clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
  1766. wake_up(&pd->port_wait);
  1767. }
  1768. done:
  1769. return ret;
  1770. }
  1771. /**
  1772. * unlock_exptid - unlock any expected TID entries port still had in use
  1773. * @pd: port
  1774. *
  1775. * We don't actually update the chip here, because we do a bulk update
  1776. * below, using ipath_f_clear_tids.
  1777. */
  1778. static void unlock_expected_tids(struct ipath_portdata *pd)
  1779. {
  1780. struct ipath_devdata *dd = pd->port_dd;
  1781. int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
  1782. int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
  1783. ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
  1784. pd->port_port);
  1785. for (i = port_tidbase; i < maxtid; i++) {
  1786. if (!dd->ipath_pageshadow[i])
  1787. continue;
  1788. pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
  1789. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1790. ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
  1791. 1);
  1792. dd->ipath_pageshadow[i] = NULL;
  1793. cnt++;
  1794. ipath_stats.sps_pageunlocks++;
  1795. }
  1796. if (cnt)
  1797. ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
  1798. pd->port_port, cnt);
  1799. if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
  1800. ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
  1801. (unsigned long long) ipath_stats.sps_pagelocks,
  1802. (unsigned long long)
  1803. ipath_stats.sps_pageunlocks);
  1804. }
  1805. static int ipath_close(struct inode *in, struct file *fp)
  1806. {
  1807. int ret = 0;
  1808. struct ipath_filedata *fd;
  1809. struct ipath_portdata *pd;
  1810. struct ipath_devdata *dd;
  1811. unsigned port;
  1812. ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
  1813. (long)in->i_rdev, fp->private_data);
  1814. mutex_lock(&ipath_mutex);
  1815. fd = (struct ipath_filedata *) fp->private_data;
  1816. fp->private_data = NULL;
  1817. pd = fd->pd;
  1818. if (!pd) {
  1819. mutex_unlock(&ipath_mutex);
  1820. goto bail;
  1821. }
  1822. if (--pd->port_cnt) {
  1823. /*
  1824. * XXX If the master closes the port before the slave(s),
  1825. * revoke the mmap for the eager receive queue so
  1826. * the slave(s) don't wait for receive data forever.
  1827. */
  1828. pd->active_slaves &= ~(1 << fd->subport);
  1829. mutex_unlock(&ipath_mutex);
  1830. goto bail;
  1831. }
  1832. port = pd->port_port;
  1833. dd = pd->port_dd;
  1834. if (pd->port_hdrqfull) {
  1835. ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
  1836. "during run\n", pd->port_comm, pd->port_pid,
  1837. pd->port_hdrqfull);
  1838. pd->port_hdrqfull = 0;
  1839. }
  1840. if (pd->port_rcvwait_to || pd->port_piowait_to
  1841. || pd->port_rcvnowait || pd->port_pionowait) {
  1842. ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
  1843. "%u rcv %u, pio already\n",
  1844. pd->port_port, pd->port_rcvwait_to,
  1845. pd->port_piowait_to, pd->port_rcvnowait,
  1846. pd->port_pionowait);
  1847. pd->port_rcvwait_to = pd->port_piowait_to =
  1848. pd->port_rcvnowait = pd->port_pionowait = 0;
  1849. }
  1850. if (pd->port_flag) {
  1851. ipath_dbg("port %u port_flag still set to 0x%lx\n",
  1852. pd->port_port, pd->port_flag);
  1853. pd->port_flag = 0;
  1854. }
  1855. if (dd->ipath_kregbase) {
  1856. int i;
  1857. /* atomically clear receive enable port and intr avail. */
  1858. clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
  1859. &dd->ipath_rcvctrl);
  1860. clear_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT,
  1861. &dd->ipath_rcvctrl);
  1862. ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
  1863. dd->ipath_rcvctrl);
  1864. /* and read back from chip to be sure that nothing
  1865. * else is in flight when we do the rest */
  1866. (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  1867. /* clean up the pkeys for this port user */
  1868. ipath_clean_part_key(pd, dd);
  1869. /*
  1870. * be paranoid, and never write 0's to these, just use an
  1871. * unused part of the port 0 tail page. Of course,
  1872. * rcvhdraddr points to a large chunk of memory, so this
  1873. * could still trash things, but at least it won't trash
  1874. * page 0, and by disabling the port, it should stop "soon",
  1875. * even if a packet or two is in already in flight after we
  1876. * disabled the port.
  1877. */
  1878. ipath_write_kreg_port(dd,
  1879. dd->ipath_kregs->kr_rcvhdrtailaddr, port,
  1880. dd->ipath_dummy_hdrq_phys);
  1881. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
  1882. pd->port_port, dd->ipath_dummy_hdrq_phys);
  1883. i = dd->ipath_pbufsport * (port - 1);
  1884. ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
  1885. dd->ipath_f_clear_tids(dd, pd->port_port);
  1886. if (dd->ipath_pageshadow)
  1887. unlock_expected_tids(pd);
  1888. ipath_stats.sps_ports--;
  1889. ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
  1890. pd->port_comm, pd->port_pid,
  1891. dd->ipath_unit, port);
  1892. }
  1893. pd->port_pid = 0;
  1894. dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
  1895. mutex_unlock(&ipath_mutex);
  1896. ipath_free_pddata(dd, pd); /* after releasing the mutex */
  1897. bail:
  1898. kfree(fd);
  1899. return ret;
  1900. }
  1901. static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
  1902. struct ipath_port_info __user *uinfo)
  1903. {
  1904. struct ipath_port_info info;
  1905. int nup;
  1906. int ret;
  1907. size_t sz;
  1908. (void) ipath_count_units(NULL, &nup, NULL);
  1909. info.num_active = nup;
  1910. info.unit = pd->port_dd->ipath_unit;
  1911. info.port = pd->port_port;
  1912. info.subport = subport;
  1913. /* Don't return new fields if old library opened the port. */
  1914. if (ipath_supports_subports(pd->userversion >> 16,
  1915. pd->userversion & 0xffff)) {
  1916. /* Number of user ports available for this device. */
  1917. info.num_ports = pd->port_dd->ipath_cfgports - 1;
  1918. info.num_subports = pd->port_subport_cnt;
  1919. sz = sizeof(info);
  1920. } else
  1921. sz = sizeof(info) - 2 * sizeof(u16);
  1922. if (copy_to_user(uinfo, &info, sz)) {
  1923. ret = -EFAULT;
  1924. goto bail;
  1925. }
  1926. ret = 0;
  1927. bail:
  1928. return ret;
  1929. }
  1930. static int ipath_get_slave_info(struct ipath_portdata *pd,
  1931. void __user *slave_mask_addr)
  1932. {
  1933. int ret = 0;
  1934. if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
  1935. ret = -EFAULT;
  1936. return ret;
  1937. }
  1938. static int ipath_force_pio_avail_update(struct ipath_devdata *dd)
  1939. {
  1940. u64 reg = dd->ipath_sendctrl;
  1941. clear_bit(IPATH_S_PIOBUFAVAILUPD, &reg);
  1942. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, reg);
  1943. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
  1944. return 0;
  1945. }
  1946. static ssize_t ipath_write(struct file *fp, const char __user *data,
  1947. size_t count, loff_t *off)
  1948. {
  1949. const struct ipath_cmd __user *ucmd;
  1950. struct ipath_portdata *pd;
  1951. const void __user *src;
  1952. size_t consumed, copy;
  1953. struct ipath_cmd cmd;
  1954. ssize_t ret = 0;
  1955. void *dest;
  1956. if (count < sizeof(cmd.type)) {
  1957. ret = -EINVAL;
  1958. goto bail;
  1959. }
  1960. ucmd = (const struct ipath_cmd __user *) data;
  1961. if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
  1962. ret = -EFAULT;
  1963. goto bail;
  1964. }
  1965. consumed = sizeof(cmd.type);
  1966. switch (cmd.type) {
  1967. case IPATH_CMD_ASSIGN_PORT:
  1968. case __IPATH_CMD_USER_INIT:
  1969. case IPATH_CMD_USER_INIT:
  1970. copy = sizeof(cmd.cmd.user_info);
  1971. dest = &cmd.cmd.user_info;
  1972. src = &ucmd->cmd.user_info;
  1973. break;
  1974. case IPATH_CMD_RECV_CTRL:
  1975. copy = sizeof(cmd.cmd.recv_ctrl);
  1976. dest = &cmd.cmd.recv_ctrl;
  1977. src = &ucmd->cmd.recv_ctrl;
  1978. break;
  1979. case IPATH_CMD_PORT_INFO:
  1980. copy = sizeof(cmd.cmd.port_info);
  1981. dest = &cmd.cmd.port_info;
  1982. src = &ucmd->cmd.port_info;
  1983. break;
  1984. case IPATH_CMD_TID_UPDATE:
  1985. case IPATH_CMD_TID_FREE:
  1986. copy = sizeof(cmd.cmd.tid_info);
  1987. dest = &cmd.cmd.tid_info;
  1988. src = &ucmd->cmd.tid_info;
  1989. break;
  1990. case IPATH_CMD_SET_PART_KEY:
  1991. copy = sizeof(cmd.cmd.part_key);
  1992. dest = &cmd.cmd.part_key;
  1993. src = &ucmd->cmd.part_key;
  1994. break;
  1995. case __IPATH_CMD_SLAVE_INFO:
  1996. copy = sizeof(cmd.cmd.slave_mask_addr);
  1997. dest = &cmd.cmd.slave_mask_addr;
  1998. src = &ucmd->cmd.slave_mask_addr;
  1999. break;
  2000. case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
  2001. copy = 0;
  2002. src = NULL;
  2003. dest = NULL;
  2004. break;
  2005. case IPATH_CMD_POLL_TYPE:
  2006. copy = sizeof(cmd.cmd.poll_type);
  2007. dest = &cmd.cmd.poll_type;
  2008. src = &ucmd->cmd.poll_type;
  2009. break;
  2010. default:
  2011. ret = -EINVAL;
  2012. goto bail;
  2013. }
  2014. if (copy) {
  2015. if ((count - consumed) < copy) {
  2016. ret = -EINVAL;
  2017. goto bail;
  2018. }
  2019. if (copy_from_user(dest, src, copy)) {
  2020. ret = -EFAULT;
  2021. goto bail;
  2022. }
  2023. consumed += copy;
  2024. }
  2025. pd = port_fp(fp);
  2026. if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
  2027. cmd.type != IPATH_CMD_ASSIGN_PORT) {
  2028. ret = -EINVAL;
  2029. goto bail;
  2030. }
  2031. switch (cmd.type) {
  2032. case IPATH_CMD_ASSIGN_PORT:
  2033. ret = ipath_assign_port(fp, &cmd.cmd.user_info);
  2034. if (ret)
  2035. goto bail;
  2036. break;
  2037. case __IPATH_CMD_USER_INIT:
  2038. /* backwards compatibility, get port first */
  2039. ret = ipath_assign_port(fp, &cmd.cmd.user_info);
  2040. if (ret)
  2041. goto bail;
  2042. /* and fall through to current version. */
  2043. case IPATH_CMD_USER_INIT:
  2044. ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
  2045. if (ret)
  2046. goto bail;
  2047. ret = ipath_get_base_info(
  2048. fp, (void __user *) (unsigned long)
  2049. cmd.cmd.user_info.spu_base_info,
  2050. cmd.cmd.user_info.spu_base_info_size);
  2051. break;
  2052. case IPATH_CMD_RECV_CTRL:
  2053. ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
  2054. break;
  2055. case IPATH_CMD_PORT_INFO:
  2056. ret = ipath_port_info(pd, subport_fp(fp),
  2057. (struct ipath_port_info __user *)
  2058. (unsigned long) cmd.cmd.port_info);
  2059. break;
  2060. case IPATH_CMD_TID_UPDATE:
  2061. ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
  2062. break;
  2063. case IPATH_CMD_TID_FREE:
  2064. ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
  2065. break;
  2066. case IPATH_CMD_SET_PART_KEY:
  2067. ret = ipath_set_part_key(pd, cmd.cmd.part_key);
  2068. break;
  2069. case __IPATH_CMD_SLAVE_INFO:
  2070. ret = ipath_get_slave_info(pd,
  2071. (void __user *) (unsigned long)
  2072. cmd.cmd.slave_mask_addr);
  2073. break;
  2074. case IPATH_CMD_PIOAVAILUPD:
  2075. ret = ipath_force_pio_avail_update(pd->port_dd);
  2076. break;
  2077. case IPATH_CMD_POLL_TYPE:
  2078. pd->poll_type = cmd.cmd.poll_type;
  2079. break;
  2080. }
  2081. if (ret >= 0)
  2082. ret = consumed;
  2083. bail:
  2084. return ret;
  2085. }
  2086. static struct class *ipath_class;
  2087. static int init_cdev(int minor, char *name, const struct file_operations *fops,
  2088. struct cdev **cdevp, struct class_device **class_devp)
  2089. {
  2090. const dev_t dev = MKDEV(IPATH_MAJOR, minor);
  2091. struct cdev *cdev = NULL;
  2092. struct class_device *class_dev = NULL;
  2093. int ret;
  2094. cdev = cdev_alloc();
  2095. if (!cdev) {
  2096. printk(KERN_ERR IPATH_DRV_NAME
  2097. ": Could not allocate cdev for minor %d, %s\n",
  2098. minor, name);
  2099. ret = -ENOMEM;
  2100. goto done;
  2101. }
  2102. cdev->owner = THIS_MODULE;
  2103. cdev->ops = fops;
  2104. kobject_set_name(&cdev->kobj, name);
  2105. ret = cdev_add(cdev, dev, 1);
  2106. if (ret < 0) {
  2107. printk(KERN_ERR IPATH_DRV_NAME
  2108. ": Could not add cdev for minor %d, %s (err %d)\n",
  2109. minor, name, -ret);
  2110. goto err_cdev;
  2111. }
  2112. class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
  2113. if (IS_ERR(class_dev)) {
  2114. ret = PTR_ERR(class_dev);
  2115. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  2116. "class_dev for minor %d, %s (err %d)\n",
  2117. minor, name, -ret);
  2118. goto err_cdev;
  2119. }
  2120. goto done;
  2121. err_cdev:
  2122. cdev_del(cdev);
  2123. cdev = NULL;
  2124. done:
  2125. if (ret >= 0) {
  2126. *cdevp = cdev;
  2127. *class_devp = class_dev;
  2128. } else {
  2129. *cdevp = NULL;
  2130. *class_devp = NULL;
  2131. }
  2132. return ret;
  2133. }
  2134. int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
  2135. struct cdev **cdevp, struct class_device **class_devp)
  2136. {
  2137. return init_cdev(minor, name, fops, cdevp, class_devp);
  2138. }
  2139. static void cleanup_cdev(struct cdev **cdevp,
  2140. struct class_device **class_devp)
  2141. {
  2142. struct class_device *class_dev = *class_devp;
  2143. if (class_dev) {
  2144. class_device_unregister(class_dev);
  2145. *class_devp = NULL;
  2146. }
  2147. if (*cdevp) {
  2148. cdev_del(*cdevp);
  2149. *cdevp = NULL;
  2150. }
  2151. }
  2152. void ipath_cdev_cleanup(struct cdev **cdevp,
  2153. struct class_device **class_devp)
  2154. {
  2155. cleanup_cdev(cdevp, class_devp);
  2156. }
  2157. static struct cdev *wildcard_cdev;
  2158. static struct class_device *wildcard_class_dev;
  2159. static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
  2160. static int user_init(void)
  2161. {
  2162. int ret;
  2163. ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
  2164. if (ret < 0) {
  2165. printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
  2166. "chrdev region (err %d)\n", -ret);
  2167. goto done;
  2168. }
  2169. ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
  2170. if (IS_ERR(ipath_class)) {
  2171. ret = PTR_ERR(ipath_class);
  2172. printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
  2173. "device class (err %d)\n", -ret);
  2174. goto bail;
  2175. }
  2176. goto done;
  2177. bail:
  2178. unregister_chrdev_region(dev, IPATH_NMINORS);
  2179. done:
  2180. return ret;
  2181. }
  2182. static void user_cleanup(void)
  2183. {
  2184. if (ipath_class) {
  2185. class_destroy(ipath_class);
  2186. ipath_class = NULL;
  2187. }
  2188. unregister_chrdev_region(dev, IPATH_NMINORS);
  2189. }
  2190. static atomic_t user_count = ATOMIC_INIT(0);
  2191. static atomic_t user_setup = ATOMIC_INIT(0);
  2192. int ipath_user_add(struct ipath_devdata *dd)
  2193. {
  2194. char name[10];
  2195. int ret;
  2196. if (atomic_inc_return(&user_count) == 1) {
  2197. ret = user_init();
  2198. if (ret < 0) {
  2199. ipath_dev_err(dd, "Unable to set up user support: "
  2200. "error %d\n", -ret);
  2201. goto bail;
  2202. }
  2203. ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
  2204. &wildcard_class_dev);
  2205. if (ret < 0) {
  2206. ipath_dev_err(dd, "Could not create wildcard "
  2207. "minor: error %d\n", -ret);
  2208. goto bail_user;
  2209. }
  2210. atomic_set(&user_setup, 1);
  2211. }
  2212. snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
  2213. ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
  2214. &dd->user_cdev, &dd->user_class_dev);
  2215. if (ret < 0)
  2216. ipath_dev_err(dd, "Could not create user minor %d, %s\n",
  2217. dd->ipath_unit + 1, name);
  2218. goto bail;
  2219. bail_user:
  2220. user_cleanup();
  2221. bail:
  2222. return ret;
  2223. }
  2224. void ipath_user_remove(struct ipath_devdata *dd)
  2225. {
  2226. cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
  2227. if (atomic_dec_return(&user_count) == 0) {
  2228. if (atomic_read(&user_setup) == 0)
  2229. goto bail;
  2230. cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
  2231. user_cleanup();
  2232. atomic_set(&user_setup, 0);
  2233. }
  2234. bail:
  2235. return;
  2236. }