ipath_file_ops.c 72 KB

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