ipath_file_ops.c 72 KB

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