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/export.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/slab.h>
  40. #include <linux/highmem.h>
  41. #include <linux/io.h>
  42. #include <linux/aio.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/cpu.h>
  45. #include <asm/pgtable.h>
  46. #include "ipath_kernel.h"
  47. #include "ipath_common.h"
  48. #include "ipath_user_sdma.h"
  49. static int ipath_open(struct inode *, struct file *);
  50. static int ipath_close(struct inode *, struct file *);
  51. static ssize_t ipath_write(struct file *, const char __user *, size_t,
  52. loff_t *);
  53. static ssize_t ipath_writev(struct kiocb *, const struct iovec *,
  54. unsigned long , loff_t);
  55. static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
  56. static int ipath_mmap(struct file *, struct vm_area_struct *);
  57. static const struct file_operations ipath_file_ops = {
  58. .owner = THIS_MODULE,
  59. .write = ipath_write,
  60. .aio_write = ipath_writev,
  61. .open = ipath_open,
  62. .release = ipath_close,
  63. .poll = ipath_poll,
  64. .mmap = ipath_mmap,
  65. .llseek = noop_llseek,
  66. };
  67. /*
  68. * Convert kernel virtual addresses to physical addresses so they don't
  69. * potentially conflict with the chip addresses used as mmap offsets.
  70. * It doesn't really matter what mmap offset we use as long as we can
  71. * interpret it correctly.
  72. */
  73. static u64 cvt_kvaddr(void *p)
  74. {
  75. struct page *page;
  76. u64 paddr = 0;
  77. page = vmalloc_to_page(p);
  78. if (page)
  79. paddr = page_to_pfn(page) << PAGE_SHIFT;
  80. return paddr;
  81. }
  82. static int ipath_get_base_info(struct file *fp,
  83. void __user *ubase, size_t ubase_size)
  84. {
  85. struct ipath_portdata *pd = port_fp(fp);
  86. int ret = 0;
  87. struct ipath_base_info *kinfo = NULL;
  88. struct ipath_devdata *dd = pd->port_dd;
  89. unsigned subport_cnt;
  90. int shared, master;
  91. size_t sz;
  92. subport_cnt = pd->port_subport_cnt;
  93. if (!subport_cnt) {
  94. shared = 0;
  95. master = 0;
  96. subport_cnt = 1;
  97. } else {
  98. shared = 1;
  99. master = !subport_fp(fp);
  100. }
  101. sz = sizeof(*kinfo);
  102. /* If port sharing is not requested, allow the old size structure */
  103. if (!shared)
  104. sz -= 7 * sizeof(u64);
  105. if (ubase_size < sz) {
  106. ipath_cdbg(PROC,
  107. "Base size %zu, need %zu (version mismatch?)\n",
  108. ubase_size, sz);
  109. ret = -EINVAL;
  110. goto bail;
  111. }
  112. kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
  113. if (kinfo == NULL) {
  114. ret = -ENOMEM;
  115. goto bail;
  116. }
  117. ret = dd->ipath_f_get_base_info(pd, kinfo);
  118. if (ret < 0)
  119. goto bail;
  120. kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
  121. kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
  122. kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
  123. kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
  124. /*
  125. * have to mmap whole thing
  126. */
  127. kinfo->spi_rcv_egrbuftotlen =
  128. pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  129. kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
  130. kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
  131. pd->port_rcvegrbuf_chunks;
  132. kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
  133. if (master)
  134. kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
  135. /*
  136. * for this use, may be ipath_cfgports summed over all chips that
  137. * are are configured and present
  138. */
  139. kinfo->spi_nports = dd->ipath_cfgports;
  140. /* unit (chip/board) our port is on */
  141. kinfo->spi_unit = dd->ipath_unit;
  142. /* for now, only a single page */
  143. kinfo->spi_tid_maxsize = PAGE_SIZE;
  144. /*
  145. * Doing this per port, and based on the skip value, etc. This has
  146. * to be the actual buffer size, since the protocol code treats it
  147. * as an array.
  148. *
  149. * These have to be set to user addresses in the user code via mmap.
  150. * These values are used on return to user code for the mmap target
  151. * addresses only. For 32 bit, same 44 bit address problem, so use
  152. * the physical address, not virtual. Before 2.6.11, using the
  153. * page_address() macro worked, but in 2.6.11, even that returns the
  154. * full 64 bit address (upper bits all 1's). So far, using the
  155. * physical addresses (or chip offsets, for chip mapping) works, but
  156. * no doubt some future kernel release will change that, and we'll be
  157. * on to yet another method of dealing with this.
  158. */
  159. kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
  160. kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
  161. kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
  162. kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
  163. kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
  164. (void *) dd->ipath_statusp -
  165. (void *) dd->ipath_pioavailregs_dma;
  166. if (!shared) {
  167. kinfo->spi_piocnt = pd->port_piocnt;
  168. kinfo->spi_piobufbase = (u64) pd->port_piobufs;
  169. kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
  170. dd->ipath_ureg_align * pd->port_port;
  171. } else if (master) {
  172. kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +
  173. (pd->port_piocnt % subport_cnt);
  174. /* Master's PIO buffers are after all the slave's */
  175. kinfo->spi_piobufbase = (u64) pd->port_piobufs +
  176. dd->ipath_palign *
  177. (pd->port_piocnt - kinfo->spi_piocnt);
  178. } else {
  179. unsigned slave = subport_fp(fp) - 1;
  180. kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;
  181. kinfo->spi_piobufbase = (u64) pd->port_piobufs +
  182. dd->ipath_palign * kinfo->spi_piocnt * slave;
  183. }
  184. if (shared) {
  185. kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
  186. dd->ipath_ureg_align * pd->port_port;
  187. kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
  188. kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
  189. kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
  190. kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
  191. PAGE_SIZE * subport_fp(fp));
  192. kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
  193. pd->port_rcvhdrq_size * subport_fp(fp));
  194. kinfo->spi_rcvhdr_tailaddr = 0;
  195. kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
  196. pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
  197. subport_fp(fp));
  198. kinfo->spi_subport_uregbase =
  199. cvt_kvaddr(pd->subport_uregbase);
  200. kinfo->spi_subport_rcvegrbuf =
  201. cvt_kvaddr(pd->subport_rcvegrbuf);
  202. kinfo->spi_subport_rcvhdr_base =
  203. cvt_kvaddr(pd->subport_rcvhdr_base);
  204. ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
  205. kinfo->spi_port, kinfo->spi_runtime_flags,
  206. (unsigned long long) kinfo->spi_subport_uregbase,
  207. (unsigned long long) kinfo->spi_subport_rcvegrbuf,
  208. (unsigned long long) kinfo->spi_subport_rcvhdr_base);
  209. }
  210. /*
  211. * All user buffers are 2KB buffers. If we ever support
  212. * giving 4KB buffers to user processes, this will need some
  213. * work.
  214. */
  215. kinfo->spi_pioindex = (kinfo->spi_piobufbase -
  216. (dd->ipath_piobufbase & 0xffffffff)) / dd->ipath_palign;
  217. kinfo->spi_pioalign = dd->ipath_palign;
  218. kinfo->spi_qpair = IPATH_KD_QP;
  219. /*
  220. * user mode PIO buffers are always 2KB, even when 4KB can
  221. * be received, and sent via the kernel; this is ibmaxlen
  222. * for 2K MTU.
  223. */
  224. kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);
  225. kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
  226. kinfo->spi_port = pd->port_port;
  227. kinfo->spi_subport = subport_fp(fp);
  228. kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
  229. kinfo->spi_hw_version = dd->ipath_revision;
  230. if (master) {
  231. kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
  232. }
  233. sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
  234. if (copy_to_user(ubase, kinfo, sz))
  235. ret = -EFAULT;
  236. bail:
  237. kfree(kinfo);
  238. return ret;
  239. }
  240. /**
  241. * ipath_tid_update - update a port TID
  242. * @pd: the port
  243. * @fp: the ipath device file
  244. * @ti: the TID information
  245. *
  246. * The new implementation as of Oct 2004 is that the driver assigns
  247. * the tid and returns it to the caller. To make it easier to
  248. * catch bugs, and to reduce search time, we keep a cursor for
  249. * each port, walking the shadow tid array to find one that's not
  250. * in use.
  251. *
  252. * For now, if we can't allocate the full list, we fail, although
  253. * in the long run, we'll allocate as many as we can, and the
  254. * caller will deal with that by trying the remaining pages later.
  255. * That means that when we fail, we have to mark the tids as not in
  256. * use again, in our shadow copy.
  257. *
  258. * It's up to the caller to free the tids when they are done.
  259. * We'll unlock the pages as they free them.
  260. *
  261. * Also, right now we are locking one page at a time, but since
  262. * the intended use of this routine is for a single group of
  263. * virtually contiguous pages, that should change to improve
  264. * performance.
  265. */
  266. static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
  267. const struct ipath_tid_info *ti)
  268. {
  269. int ret = 0, ntids;
  270. u32 tid, porttid, cnt, i, tidcnt, tidoff;
  271. u16 *tidlist;
  272. struct ipath_devdata *dd = pd->port_dd;
  273. u64 physaddr;
  274. unsigned long vaddr;
  275. u64 __iomem *tidbase;
  276. unsigned long tidmap[8];
  277. struct page **pagep = NULL;
  278. unsigned subport = subport_fp(fp);
  279. if (!dd->ipath_pageshadow) {
  280. ret = -ENOMEM;
  281. goto done;
  282. }
  283. cnt = ti->tidcnt;
  284. if (!cnt) {
  285. ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
  286. (unsigned long long) ti->tidlist);
  287. /*
  288. * Should we treat as success? likely a bug
  289. */
  290. ret = -EFAULT;
  291. goto done;
  292. }
  293. porttid = pd->port_port * dd->ipath_rcvtidcnt;
  294. if (!pd->port_subport_cnt) {
  295. tidcnt = dd->ipath_rcvtidcnt;
  296. tid = pd->port_tidcursor;
  297. tidoff = 0;
  298. } else if (!subport) {
  299. tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
  300. (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
  301. tidoff = dd->ipath_rcvtidcnt - tidcnt;
  302. porttid += tidoff;
  303. tid = tidcursor_fp(fp);
  304. } else {
  305. tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
  306. tidoff = tidcnt * (subport - 1);
  307. porttid += tidoff;
  308. tid = tidcursor_fp(fp);
  309. }
  310. if (cnt > tidcnt) {
  311. /* make sure it all fits in port_tid_pg_list */
  312. dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
  313. "TIDs, only trying max (%u)\n", cnt, tidcnt);
  314. cnt = tidcnt;
  315. }
  316. pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
  317. tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
  318. memset(tidmap, 0, sizeof(tidmap));
  319. /* before decrement; chip actual # */
  320. ntids = tidcnt;
  321. tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
  322. dd->ipath_rcvtidbase +
  323. porttid * sizeof(*tidbase));
  324. ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
  325. pd->port_port, cnt, tid, tidbase);
  326. /* virtual address of first page in transfer */
  327. vaddr = ti->tidvaddr;
  328. if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
  329. cnt * PAGE_SIZE)) {
  330. ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
  331. (void *)vaddr, cnt);
  332. ret = -EFAULT;
  333. goto done;
  334. }
  335. ret = ipath_get_user_pages(vaddr, cnt, pagep);
  336. if (ret) {
  337. if (ret == -EBUSY) {
  338. ipath_dbg("Failed to lock addr %p, %u pages "
  339. "(already locked)\n",
  340. (void *) vaddr, cnt);
  341. /*
  342. * for now, continue, and see what happens but with
  343. * the new implementation, this should never happen,
  344. * unless perhaps the user has mpin'ed the pages
  345. * themselves (something we need to test)
  346. */
  347. ret = 0;
  348. } else {
  349. dev_info(&dd->pcidev->dev,
  350. "Failed to lock addr %p, %u pages: "
  351. "errno %d\n", (void *) vaddr, cnt, -ret);
  352. goto done;
  353. }
  354. }
  355. for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
  356. for (; ntids--; tid++) {
  357. if (tid == tidcnt)
  358. tid = 0;
  359. if (!dd->ipath_pageshadow[porttid + tid])
  360. break;
  361. }
  362. if (ntids < 0) {
  363. /*
  364. * oops, wrapped all the way through their TIDs,
  365. * and didn't have enough free; see comments at
  366. * start of routine
  367. */
  368. ipath_dbg("Not enough free TIDs for %u pages "
  369. "(index %d), failing\n", cnt, i);
  370. i--; /* last tidlist[i] not filled in */
  371. ret = -ENOMEM;
  372. break;
  373. }
  374. tidlist[i] = tid + tidoff;
  375. ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
  376. "vaddr %lx\n", i, tid + tidoff, vaddr);
  377. /* we "know" system pages and TID pages are same size */
  378. dd->ipath_pageshadow[porttid + tid] = pagep[i];
  379. dd->ipath_physshadow[porttid + tid] = ipath_map_page(
  380. dd->pcidev, pagep[i], 0, PAGE_SIZE,
  381. PCI_DMA_FROMDEVICE);
  382. /*
  383. * don't need atomic or it's overhead
  384. */
  385. __set_bit(tid, tidmap);
  386. physaddr = dd->ipath_physshadow[porttid + tid];
  387. ipath_stats.sps_pagelocks++;
  388. ipath_cdbg(VERBOSE,
  389. "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
  390. tid, vaddr, (unsigned long long) physaddr,
  391. pagep[i]);
  392. dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
  393. physaddr);
  394. /*
  395. * don't check this tid in ipath_portshadow, since we
  396. * just filled it in; start with the next one.
  397. */
  398. tid++;
  399. }
  400. if (ret) {
  401. u32 limit;
  402. cleanup:
  403. /* jump here if copy out of updated info failed... */
  404. ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
  405. -ret, i, cnt);
  406. /* same code that's in ipath_free_tid() */
  407. limit = sizeof(tidmap) * BITS_PER_BYTE;
  408. if (limit > tidcnt)
  409. /* just in case size changes in future */
  410. limit = tidcnt;
  411. tid = find_first_bit((const unsigned long *)tidmap, limit);
  412. for (; tid < limit; tid++) {
  413. if (!test_bit(tid, tidmap))
  414. continue;
  415. if (dd->ipath_pageshadow[porttid + tid]) {
  416. ipath_cdbg(VERBOSE, "Freeing TID %u\n",
  417. tid);
  418. dd->ipath_f_put_tid(dd, &tidbase[tid],
  419. RCVHQ_RCV_TYPE_EXPECTED,
  420. dd->ipath_tidinvalid);
  421. pci_unmap_page(dd->pcidev,
  422. dd->ipath_physshadow[porttid + tid],
  423. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  424. dd->ipath_pageshadow[porttid + tid] = NULL;
  425. ipath_stats.sps_pageunlocks++;
  426. }
  427. }
  428. ipath_release_user_pages(pagep, cnt);
  429. } else {
  430. /*
  431. * Copy the updated array, with ipath_tid's filled in, back
  432. * to user. Since we did the copy in already, this "should
  433. * never fail" If it does, we have to clean up...
  434. */
  435. if (copy_to_user((void __user *)
  436. (unsigned long) ti->tidlist,
  437. tidlist, cnt * sizeof(*tidlist))) {
  438. ret = -EFAULT;
  439. goto cleanup;
  440. }
  441. if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
  442. tidmap, sizeof tidmap)) {
  443. ret = -EFAULT;
  444. goto cleanup;
  445. }
  446. if (tid == tidcnt)
  447. tid = 0;
  448. if (!pd->port_subport_cnt)
  449. pd->port_tidcursor = tid;
  450. else
  451. tidcursor_fp(fp) = tid;
  452. }
  453. done:
  454. if (ret)
  455. ipath_dbg("Failed to map %u TID pages, failing with %d\n",
  456. ti->tidcnt, -ret);
  457. return ret;
  458. }
  459. /**
  460. * ipath_tid_free - free a port TID
  461. * @pd: the port
  462. * @subport: the subport
  463. * @ti: the TID info
  464. *
  465. * right now we are unlocking one page at a time, but since
  466. * the intended use of this routine is for a single group of
  467. * virtually contiguous pages, that should change to improve
  468. * performance. We check that the TID is in range for this port
  469. * but otherwise don't check validity; if user has an error and
  470. * frees the wrong tid, it's only their own data that can thereby
  471. * be corrupted. We do check that the TID was in use, for sanity
  472. * We always use our idea of the saved address, not the address that
  473. * they pass in to us.
  474. */
  475. static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
  476. const struct ipath_tid_info *ti)
  477. {
  478. int ret = 0;
  479. u32 tid, porttid, cnt, limit, tidcnt;
  480. struct ipath_devdata *dd = pd->port_dd;
  481. u64 __iomem *tidbase;
  482. unsigned long tidmap[8];
  483. if (!dd->ipath_pageshadow) {
  484. ret = -ENOMEM;
  485. goto done;
  486. }
  487. if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
  488. sizeof tidmap)) {
  489. ret = -EFAULT;
  490. goto done;
  491. }
  492. porttid = pd->port_port * dd->ipath_rcvtidcnt;
  493. if (!pd->port_subport_cnt)
  494. tidcnt = dd->ipath_rcvtidcnt;
  495. else if (!subport) {
  496. tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
  497. (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
  498. porttid += dd->ipath_rcvtidcnt - tidcnt;
  499. } else {
  500. tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
  501. porttid += tidcnt * (subport - 1);
  502. }
  503. tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
  504. dd->ipath_rcvtidbase +
  505. porttid * sizeof(*tidbase));
  506. limit = sizeof(tidmap) * BITS_PER_BYTE;
  507. if (limit > tidcnt)
  508. /* just in case size changes in future */
  509. limit = tidcnt;
  510. tid = find_first_bit(tidmap, limit);
  511. ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
  512. "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
  513. limit, tid, porttid);
  514. for (cnt = 0; tid < limit; tid++) {
  515. /*
  516. * small optimization; if we detect a run of 3 or so without
  517. * any set, use find_first_bit again. That's mainly to
  518. * accelerate the case where we wrapped, so we have some at
  519. * the beginning, and some at the end, and a big gap
  520. * in the middle.
  521. */
  522. if (!test_bit(tid, tidmap))
  523. continue;
  524. cnt++;
  525. if (dd->ipath_pageshadow[porttid + tid]) {
  526. struct page *p;
  527. p = dd->ipath_pageshadow[porttid + tid];
  528. dd->ipath_pageshadow[porttid + tid] = NULL;
  529. ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
  530. pid_nr(pd->port_pid), tid);
  531. dd->ipath_f_put_tid(dd, &tidbase[tid],
  532. RCVHQ_RCV_TYPE_EXPECTED,
  533. dd->ipath_tidinvalid);
  534. pci_unmap_page(dd->pcidev,
  535. dd->ipath_physshadow[porttid + tid],
  536. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  537. ipath_release_user_pages(&p, 1);
  538. ipath_stats.sps_pageunlocks++;
  539. } else
  540. ipath_dbg("Unused tid %u, ignoring\n", tid);
  541. }
  542. if (cnt != ti->tidcnt)
  543. ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
  544. ti->tidcnt, cnt);
  545. done:
  546. if (ret)
  547. ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
  548. ti->tidcnt, -ret);
  549. return ret;
  550. }
  551. /**
  552. * ipath_set_part_key - set a partition key
  553. * @pd: the port
  554. * @key: the key
  555. *
  556. * We can have up to 4 active at a time (other than the default, which is
  557. * always allowed). This is somewhat tricky, since multiple ports may set
  558. * the same key, so we reference count them, and clean up at exit. All 4
  559. * partition keys are packed into a single infinipath register. It's an
  560. * error for a process to set the same pkey multiple times. We provide no
  561. * mechanism to de-allocate a pkey at this time, we may eventually need to
  562. * do that. I've used the atomic operations, and no locking, and only make
  563. * a single pass through what's available. This should be more than
  564. * adequate for some time. I'll think about spinlocks or the like if and as
  565. * it's necessary.
  566. */
  567. static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
  568. {
  569. struct ipath_devdata *dd = pd->port_dd;
  570. int i, any = 0, pidx = -1;
  571. u16 lkey = key & 0x7FFF;
  572. int ret;
  573. if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
  574. /* nothing to do; this key always valid */
  575. ret = 0;
  576. goto bail;
  577. }
  578. ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
  579. "%hx:%x %hx:%x %hx:%x %hx:%x\n",
  580. pd->port_port, key, dd->ipath_pkeys[0],
  581. atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
  582. atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
  583. atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
  584. atomic_read(&dd->ipath_pkeyrefs[3]));
  585. if (!lkey) {
  586. ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
  587. pd->port_port);
  588. ret = -EINVAL;
  589. goto bail;
  590. }
  591. /*
  592. * Set the full membership bit, because it has to be
  593. * set in the register or the packet, and it seems
  594. * cleaner to set in the register than to force all
  595. * callers to set it. (see bug 4331)
  596. */
  597. key |= 0x8000;
  598. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  599. if (!pd->port_pkeys[i] && pidx == -1)
  600. pidx = i;
  601. if (pd->port_pkeys[i] == key) {
  602. ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
  603. "(%x) more than once\n",
  604. pd->port_port, key);
  605. ret = -EEXIST;
  606. goto bail;
  607. }
  608. }
  609. if (pidx == -1) {
  610. ipath_dbg("All pkeys for port %u already in use, "
  611. "can't set %x\n", pd->port_port, key);
  612. ret = -EBUSY;
  613. goto bail;
  614. }
  615. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  616. if (!dd->ipath_pkeys[i]) {
  617. any++;
  618. continue;
  619. }
  620. if (dd->ipath_pkeys[i] == key) {
  621. atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
  622. if (atomic_inc_return(pkrefs) > 1) {
  623. pd->port_pkeys[pidx] = key;
  624. ipath_cdbg(VERBOSE, "p%u set key %x "
  625. "matches #%d, count now %d\n",
  626. pd->port_port, key, i,
  627. atomic_read(pkrefs));
  628. ret = 0;
  629. goto bail;
  630. } else {
  631. /*
  632. * lost race, decrement count, catch below
  633. */
  634. atomic_dec(pkrefs);
  635. ipath_cdbg(VERBOSE, "Lost race, count was "
  636. "0, after dec, it's %d\n",
  637. atomic_read(pkrefs));
  638. any++;
  639. }
  640. }
  641. if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
  642. /*
  643. * It makes no sense to have both the limited and
  644. * full membership PKEY set at the same time since
  645. * the unlimited one will disable the limited one.
  646. */
  647. ret = -EEXIST;
  648. goto bail;
  649. }
  650. }
  651. if (!any) {
  652. ipath_dbg("port %u, all pkeys already in use, "
  653. "can't set %x\n", pd->port_port, key);
  654. ret = -EBUSY;
  655. goto bail;
  656. }
  657. for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
  658. if (!dd->ipath_pkeys[i] &&
  659. atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
  660. u64 pkey;
  661. /* for ipathstats, etc. */
  662. ipath_stats.sps_pkeys[i] = lkey;
  663. pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
  664. pkey =
  665. (u64) dd->ipath_pkeys[0] |
  666. ((u64) dd->ipath_pkeys[1] << 16) |
  667. ((u64) dd->ipath_pkeys[2] << 32) |
  668. ((u64) dd->ipath_pkeys[3] << 48);
  669. ipath_cdbg(PROC, "p%u set key %x in #%d, "
  670. "portidx %d, new pkey reg %llx\n",
  671. pd->port_port, key, i, pidx,
  672. (unsigned long long) pkey);
  673. ipath_write_kreg(
  674. dd, dd->ipath_kregs->kr_partitionkey, pkey);
  675. ret = 0;
  676. goto bail;
  677. }
  678. }
  679. ipath_dbg("port %u, all pkeys already in use 2nd pass, "
  680. "can't set %x\n", pd->port_port, key);
  681. ret = -EBUSY;
  682. bail:
  683. return ret;
  684. }
  685. /**
  686. * ipath_manage_rcvq - manage a port's receive queue
  687. * @pd: the port
  688. * @subport: the subport
  689. * @start_stop: action to carry out
  690. *
  691. * start_stop == 0 disables receive on the port, for use in queue
  692. * overflow conditions. start_stop==1 re-enables, to be used to
  693. * re-init the software copy of the head register
  694. */
  695. static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
  696. int start_stop)
  697. {
  698. struct ipath_devdata *dd = pd->port_dd;
  699. ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
  700. start_stop ? "en" : "dis", dd->ipath_unit,
  701. pd->port_port, subport);
  702. if (subport)
  703. goto bail;
  704. /* atomically clear receive enable port. */
  705. if (start_stop) {
  706. /*
  707. * On enable, force in-memory copy of the tail register to
  708. * 0, so that protocol code doesn't have to worry about
  709. * whether or not the chip has yet updated the in-memory
  710. * copy or not on return from the system call. The chip
  711. * always resets it's tail register back to 0 on a
  712. * transition from disabled to enabled. This could cause a
  713. * problem if software was broken, and did the enable w/o
  714. * the disable, but eventually the in-memory copy will be
  715. * updated and correct itself, even in the face of software
  716. * bugs.
  717. */
  718. if (pd->port_rcvhdrtail_kvaddr)
  719. ipath_clear_rcvhdrtail(pd);
  720. set_bit(dd->ipath_r_portenable_shift + pd->port_port,
  721. &dd->ipath_rcvctrl);
  722. } else
  723. clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
  724. &dd->ipath_rcvctrl);
  725. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  726. dd->ipath_rcvctrl);
  727. /* now be sure chip saw it before we return */
  728. ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  729. if (start_stop) {
  730. /*
  731. * And try to be sure that tail reg update has happened too.
  732. * This should in theory interlock with the RXE changes to
  733. * the tail register. Don't assign it to the tail register
  734. * in memory copy, since we could overwrite an update by the
  735. * chip if we did.
  736. */
  737. ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  738. }
  739. /* always; new head should be equal to new tail; see above */
  740. bail:
  741. return 0;
  742. }
  743. static void ipath_clean_part_key(struct ipath_portdata *pd,
  744. struct ipath_devdata *dd)
  745. {
  746. int i, j, pchanged = 0;
  747. u64 oldpkey;
  748. /* for debugging only */
  749. oldpkey = (u64) dd->ipath_pkeys[0] |
  750. ((u64) dd->ipath_pkeys[1] << 16) |
  751. ((u64) dd->ipath_pkeys[2] << 32) |
  752. ((u64) dd->ipath_pkeys[3] << 48);
  753. for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
  754. if (!pd->port_pkeys[i])
  755. continue;
  756. ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
  757. pd->port_pkeys[i]);
  758. for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
  759. /* check for match independent of the global bit */
  760. if ((dd->ipath_pkeys[j] & 0x7fff) !=
  761. (pd->port_pkeys[i] & 0x7fff))
  762. continue;
  763. if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
  764. ipath_cdbg(VERBOSE, "p%u clear key "
  765. "%x matches #%d\n",
  766. pd->port_port,
  767. pd->port_pkeys[i], j);
  768. ipath_stats.sps_pkeys[j] =
  769. dd->ipath_pkeys[j] = 0;
  770. pchanged++;
  771. }
  772. else ipath_cdbg(
  773. VERBOSE, "p%u key %x matches #%d, "
  774. "but ref still %d\n", pd->port_port,
  775. pd->port_pkeys[i], j,
  776. atomic_read(&dd->ipath_pkeyrefs[j]));
  777. break;
  778. }
  779. pd->port_pkeys[i] = 0;
  780. }
  781. if (pchanged) {
  782. u64 pkey = (u64) dd->ipath_pkeys[0] |
  783. ((u64) dd->ipath_pkeys[1] << 16) |
  784. ((u64) dd->ipath_pkeys[2] << 32) |
  785. ((u64) dd->ipath_pkeys[3] << 48);
  786. ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
  787. "new pkey reg %llx\n", pd->port_port,
  788. (unsigned long long) oldpkey,
  789. (unsigned long long) pkey);
  790. ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
  791. pkey);
  792. }
  793. }
  794. /*
  795. * Initialize the port data with the receive buffer sizes
  796. * so this can be done while the master port is locked.
  797. * Otherwise, there is a race with a slave opening the port
  798. * and seeing these fields uninitialized.
  799. */
  800. static void init_user_egr_sizes(struct ipath_portdata *pd)
  801. {
  802. struct ipath_devdata *dd = pd->port_dd;
  803. unsigned egrperchunk, egrcnt, size;
  804. /*
  805. * to avoid wasting a lot of memory, we allocate 32KB chunks of
  806. * physically contiguous memory, advance through it until used up
  807. * and then allocate more. Of course, we need memory to store those
  808. * extra pointers, now. Started out with 256KB, but under heavy
  809. * memory pressure (creating large files and then copying them over
  810. * NFS while doing lots of MPI jobs), we hit some allocation
  811. * failures, even though we can sleep... (2.6.10) Still get
  812. * failures at 64K. 32K is the lowest we can go without wasting
  813. * additional memory.
  814. */
  815. size = 0x8000;
  816. egrperchunk = size / dd->ipath_rcvegrbufsize;
  817. egrcnt = dd->ipath_rcvegrcnt;
  818. pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
  819. pd->port_rcvegrbufs_perchunk = egrperchunk;
  820. pd->port_rcvegrbuf_size = size;
  821. }
  822. /**
  823. * ipath_create_user_egr - allocate eager TID buffers
  824. * @pd: the port to allocate TID buffers for
  825. *
  826. * This routine is now quite different for user and kernel, because
  827. * the kernel uses skb's, for the accelerated network performance
  828. * This is the user port version
  829. *
  830. * Allocate the eager TID buffers and program them into infinipath
  831. * They are no longer completely contiguous, we do multiple allocation
  832. * calls.
  833. */
  834. static int ipath_create_user_egr(struct ipath_portdata *pd)
  835. {
  836. struct ipath_devdata *dd = pd->port_dd;
  837. unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
  838. size_t size;
  839. int ret;
  840. gfp_t gfp_flags;
  841. /*
  842. * GFP_USER, but without GFP_FS, so buffer cache can be
  843. * coalesced (we hope); otherwise, even at order 4,
  844. * heavy filesystem activity makes these fail, and we can
  845. * use compound pages.
  846. */
  847. gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  848. egrcnt = dd->ipath_rcvegrcnt;
  849. /* TID number offset for this port */
  850. egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
  851. egrsize = dd->ipath_rcvegrbufsize;
  852. ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
  853. "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
  854. chunk = pd->port_rcvegrbuf_chunks;
  855. egrperchunk = pd->port_rcvegrbufs_perchunk;
  856. size = pd->port_rcvegrbuf_size;
  857. pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
  858. GFP_KERNEL);
  859. if (!pd->port_rcvegrbuf) {
  860. ret = -ENOMEM;
  861. goto bail;
  862. }
  863. pd->port_rcvegrbuf_phys =
  864. kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
  865. GFP_KERNEL);
  866. if (!pd->port_rcvegrbuf_phys) {
  867. ret = -ENOMEM;
  868. goto bail_rcvegrbuf;
  869. }
  870. for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
  871. pd->port_rcvegrbuf[e] = dma_alloc_coherent(
  872. &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
  873. gfp_flags);
  874. if (!pd->port_rcvegrbuf[e]) {
  875. ret = -ENOMEM;
  876. goto bail_rcvegrbuf_phys;
  877. }
  878. }
  879. pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
  880. for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
  881. dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
  882. unsigned i;
  883. for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
  884. dd->ipath_f_put_tid(dd, e + egroff +
  885. (u64 __iomem *)
  886. ((char __iomem *)
  887. dd->ipath_kregbase +
  888. dd->ipath_rcvegrbase),
  889. RCVHQ_RCV_TYPE_EAGER, pa);
  890. pa += egrsize;
  891. }
  892. cond_resched(); /* don't hog the cpu */
  893. }
  894. ret = 0;
  895. goto bail;
  896. bail_rcvegrbuf_phys:
  897. for (e = 0; e < pd->port_rcvegrbuf_chunks &&
  898. pd->port_rcvegrbuf[e]; e++) {
  899. dma_free_coherent(&dd->pcidev->dev, size,
  900. pd->port_rcvegrbuf[e],
  901. pd->port_rcvegrbuf_phys[e]);
  902. }
  903. kfree(pd->port_rcvegrbuf_phys);
  904. pd->port_rcvegrbuf_phys = NULL;
  905. bail_rcvegrbuf:
  906. kfree(pd->port_rcvegrbuf);
  907. pd->port_rcvegrbuf = NULL;
  908. bail:
  909. return ret;
  910. }
  911. /* common code for the mappings on dma_alloc_coherent mem */
  912. static int ipath_mmap_mem(struct vm_area_struct *vma,
  913. struct ipath_portdata *pd, unsigned len, int write_ok,
  914. void *kvaddr, char *what)
  915. {
  916. struct ipath_devdata *dd = pd->port_dd;
  917. unsigned long pfn;
  918. int ret;
  919. if ((vma->vm_end - vma->vm_start) > len) {
  920. dev_info(&dd->pcidev->dev,
  921. "FAIL on %s: len %lx > %x\n", what,
  922. vma->vm_end - vma->vm_start, len);
  923. ret = -EFAULT;
  924. goto bail;
  925. }
  926. if (!write_ok) {
  927. if (vma->vm_flags & VM_WRITE) {
  928. dev_info(&dd->pcidev->dev,
  929. "%s must be mapped readonly\n", what);
  930. ret = -EPERM;
  931. goto bail;
  932. }
  933. /* don't allow them to later change with mprotect */
  934. vma->vm_flags &= ~VM_MAYWRITE;
  935. }
  936. pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
  937. ret = remap_pfn_range(vma, vma->vm_start, pfn,
  938. len, vma->vm_page_prot);
  939. if (ret)
  940. dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
  941. "bytes r%c failed: %d\n", what, pd->port_port,
  942. pfn, len, write_ok?'w':'o', ret);
  943. else
  944. ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
  945. "r%c\n", what, pd->port_port, pfn, len,
  946. write_ok?'w':'o');
  947. bail:
  948. return ret;
  949. }
  950. static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
  951. u64 ureg)
  952. {
  953. unsigned long phys;
  954. int ret;
  955. /*
  956. * This is real hardware, so use io_remap. This is the mechanism
  957. * for the user process to update the head registers for their port
  958. * in the chip.
  959. */
  960. if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
  961. dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
  962. "%lx > PAGE\n", vma->vm_end - vma->vm_start);
  963. ret = -EFAULT;
  964. } else {
  965. phys = dd->ipath_physaddr + ureg;
  966. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  967. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  968. ret = io_remap_pfn_range(vma, vma->vm_start,
  969. phys >> PAGE_SHIFT,
  970. vma->vm_end - vma->vm_start,
  971. vma->vm_page_prot);
  972. }
  973. return ret;
  974. }
  975. static int mmap_piobufs(struct vm_area_struct *vma,
  976. struct ipath_devdata *dd,
  977. struct ipath_portdata *pd,
  978. unsigned piobufs, unsigned piocnt)
  979. {
  980. unsigned long phys;
  981. int ret;
  982. /*
  983. * When we map the PIO buffers in the chip, we want to map them as
  984. * writeonly, no read possible. This prevents access to previous
  985. * process data, and catches users who might try to read the i/o
  986. * space due to a bug.
  987. */
  988. if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
  989. dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
  990. "reqlen %lx > PAGE\n",
  991. vma->vm_end - vma->vm_start);
  992. ret = -EINVAL;
  993. goto bail;
  994. }
  995. phys = dd->ipath_physaddr + piobufs;
  996. #if defined(__powerpc__)
  997. /* There isn't a generic way to specify writethrough mappings */
  998. pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
  999. pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
  1000. pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
  1001. #endif
  1002. /*
  1003. * don't allow them to later change to readable with mprotect (for when
  1004. * not initially mapped readable, as is normally the case)
  1005. */
  1006. vma->vm_flags &= ~VM_MAYREAD;
  1007. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  1008. ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
  1009. vma->vm_end - vma->vm_start,
  1010. vma->vm_page_prot);
  1011. bail:
  1012. return ret;
  1013. }
  1014. static int mmap_rcvegrbufs(struct vm_area_struct *vma,
  1015. struct ipath_portdata *pd)
  1016. {
  1017. struct ipath_devdata *dd = pd->port_dd;
  1018. unsigned long start, size;
  1019. size_t total_size, i;
  1020. unsigned long pfn;
  1021. int ret;
  1022. size = pd->port_rcvegrbuf_size;
  1023. total_size = pd->port_rcvegrbuf_chunks * size;
  1024. if ((vma->vm_end - vma->vm_start) > total_size) {
  1025. dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
  1026. "reqlen %lx > actual %lx\n",
  1027. vma->vm_end - vma->vm_start,
  1028. (unsigned long) total_size);
  1029. ret = -EINVAL;
  1030. goto bail;
  1031. }
  1032. if (vma->vm_flags & VM_WRITE) {
  1033. dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
  1034. "writable (flags=%lx)\n", vma->vm_flags);
  1035. ret = -EPERM;
  1036. goto bail;
  1037. }
  1038. /* don't allow them to later change to writeable with mprotect */
  1039. vma->vm_flags &= ~VM_MAYWRITE;
  1040. start = vma->vm_start;
  1041. for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
  1042. pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
  1043. ret = remap_pfn_range(vma, start, pfn, size,
  1044. vma->vm_page_prot);
  1045. if (ret < 0)
  1046. goto bail;
  1047. }
  1048. ret = 0;
  1049. bail:
  1050. return ret;
  1051. }
  1052. /*
  1053. * ipath_file_vma_fault - handle a VMA page fault.
  1054. */
  1055. static int ipath_file_vma_fault(struct vm_area_struct *vma,
  1056. struct vm_fault *vmf)
  1057. {
  1058. struct page *page;
  1059. page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
  1060. if (!page)
  1061. return VM_FAULT_SIGBUS;
  1062. get_page(page);
  1063. vmf->page = page;
  1064. return 0;
  1065. }
  1066. static const struct vm_operations_struct ipath_file_vm_ops = {
  1067. .fault = ipath_file_vma_fault,
  1068. };
  1069. static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
  1070. struct ipath_portdata *pd, unsigned subport)
  1071. {
  1072. unsigned long len;
  1073. struct ipath_devdata *dd;
  1074. void *addr;
  1075. size_t size;
  1076. int ret = 0;
  1077. /* If the port is not shared, all addresses should be physical */
  1078. if (!pd->port_subport_cnt)
  1079. goto bail;
  1080. dd = pd->port_dd;
  1081. size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
  1082. /*
  1083. * Each process has all the subport uregbase, rcvhdrq, and
  1084. * rcvegrbufs mmapped - as an array for all the processes,
  1085. * and also separately for this process.
  1086. */
  1087. if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
  1088. addr = pd->subport_uregbase;
  1089. size = PAGE_SIZE * pd->port_subport_cnt;
  1090. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
  1091. addr = pd->subport_rcvhdr_base;
  1092. size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
  1093. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
  1094. addr = pd->subport_rcvegrbuf;
  1095. size *= pd->port_subport_cnt;
  1096. } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
  1097. PAGE_SIZE * subport)) {
  1098. addr = pd->subport_uregbase + PAGE_SIZE * subport;
  1099. size = PAGE_SIZE;
  1100. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
  1101. pd->port_rcvhdrq_size * subport)) {
  1102. addr = pd->subport_rcvhdr_base +
  1103. pd->port_rcvhdrq_size * subport;
  1104. size = pd->port_rcvhdrq_size;
  1105. } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
  1106. size * subport)) {
  1107. addr = pd->subport_rcvegrbuf + size * subport;
  1108. /* rcvegrbufs are read-only on the slave */
  1109. if (vma->vm_flags & VM_WRITE) {
  1110. dev_info(&dd->pcidev->dev,
  1111. "Can't map eager buffers as "
  1112. "writable (flags=%lx)\n", vma->vm_flags);
  1113. ret = -EPERM;
  1114. goto bail;
  1115. }
  1116. /*
  1117. * Don't allow permission to later change to writeable
  1118. * with mprotect.
  1119. */
  1120. vma->vm_flags &= ~VM_MAYWRITE;
  1121. } else {
  1122. goto bail;
  1123. }
  1124. len = vma->vm_end - vma->vm_start;
  1125. if (len > size) {
  1126. ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
  1127. ret = -EINVAL;
  1128. goto bail;
  1129. }
  1130. vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
  1131. vma->vm_ops = &ipath_file_vm_ops;
  1132. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1133. ret = 1;
  1134. bail:
  1135. return ret;
  1136. }
  1137. /**
  1138. * ipath_mmap - mmap various structures into user space
  1139. * @fp: the file pointer
  1140. * @vma: the VM area
  1141. *
  1142. * We use this to have a shared buffer between the kernel and the user code
  1143. * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
  1144. * buffers in the chip. We have the open and close entries so we can bump
  1145. * the ref count and keep the driver from being unloaded while still mapped.
  1146. */
  1147. static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
  1148. {
  1149. struct ipath_portdata *pd;
  1150. struct ipath_devdata *dd;
  1151. u64 pgaddr, ureg;
  1152. unsigned piobufs, piocnt;
  1153. int ret;
  1154. pd = port_fp(fp);
  1155. if (!pd) {
  1156. ret = -EINVAL;
  1157. goto bail;
  1158. }
  1159. dd = pd->port_dd;
  1160. /*
  1161. * This is the ipath_do_user_init() code, mapping the shared buffers
  1162. * into the user process. The address referred to by vm_pgoff is the
  1163. * file offset passed via mmap(). For shared ports, this is the
  1164. * kernel vmalloc() address of the pages to share with the master.
  1165. * For non-shared or master ports, this is a physical address.
  1166. * We only do one mmap for each space mapped.
  1167. */
  1168. pgaddr = vma->vm_pgoff << PAGE_SHIFT;
  1169. /*
  1170. * Check for 0 in case one of the allocations failed, but user
  1171. * called mmap anyway.
  1172. */
  1173. if (!pgaddr) {
  1174. ret = -EINVAL;
  1175. goto bail;
  1176. }
  1177. ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
  1178. (unsigned long long) pgaddr, vma->vm_start,
  1179. vma->vm_end - vma->vm_start, dd->ipath_unit,
  1180. pd->port_port, subport_fp(fp));
  1181. /*
  1182. * Physical addresses must fit in 40 bits for our hardware.
  1183. * Check for kernel virtual addresses first, anything else must
  1184. * match a HW or memory address.
  1185. */
  1186. ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
  1187. if (ret) {
  1188. if (ret > 0)
  1189. ret = 0;
  1190. goto bail;
  1191. }
  1192. ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
  1193. if (!pd->port_subport_cnt) {
  1194. /* port is not shared */
  1195. piocnt = pd->port_piocnt;
  1196. piobufs = pd->port_piobufs;
  1197. } else if (!subport_fp(fp)) {
  1198. /* caller is the master */
  1199. piocnt = (pd->port_piocnt / pd->port_subport_cnt) +
  1200. (pd->port_piocnt % pd->port_subport_cnt);
  1201. piobufs = pd->port_piobufs +
  1202. dd->ipath_palign * (pd->port_piocnt - piocnt);
  1203. } else {
  1204. unsigned slave = subport_fp(fp) - 1;
  1205. /* caller is a slave */
  1206. piocnt = pd->port_piocnt / pd->port_subport_cnt;
  1207. piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
  1208. }
  1209. if (pgaddr == ureg)
  1210. ret = mmap_ureg(vma, dd, ureg);
  1211. else if (pgaddr == piobufs)
  1212. ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
  1213. else if (pgaddr == dd->ipath_pioavailregs_phys)
  1214. /* in-memory copy of pioavail registers */
  1215. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1216. (void *) dd->ipath_pioavailregs_dma,
  1217. "pioavail registers");
  1218. else if (pgaddr == pd->port_rcvegr_phys)
  1219. ret = mmap_rcvegrbufs(vma, pd);
  1220. else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
  1221. /*
  1222. * The rcvhdrq itself; readonly except on HT (so have
  1223. * to allow writable mapping), multiple pages, contiguous
  1224. * from an i/o perspective.
  1225. */
  1226. ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
  1227. pd->port_rcvhdrq,
  1228. "rcvhdrq");
  1229. else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
  1230. /* in-memory copy of rcvhdrq tail register */
  1231. ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
  1232. pd->port_rcvhdrtail_kvaddr,
  1233. "rcvhdrq tail");
  1234. else
  1235. ret = -EINVAL;
  1236. vma->vm_private_data = NULL;
  1237. if (ret < 0)
  1238. dev_info(&dd->pcidev->dev,
  1239. "Failure %d on off %llx len %lx\n",
  1240. -ret, (unsigned long long)pgaddr,
  1241. vma->vm_end - vma->vm_start);
  1242. bail:
  1243. return ret;
  1244. }
  1245. static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
  1246. {
  1247. unsigned pollflag = 0;
  1248. if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
  1249. pd->port_hdrqfull != pd->port_hdrqfull_poll) {
  1250. pollflag |= POLLIN | POLLRDNORM;
  1251. pd->port_hdrqfull_poll = pd->port_hdrqfull;
  1252. }
  1253. return pollflag;
  1254. }
  1255. static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
  1256. struct file *fp,
  1257. struct poll_table_struct *pt)
  1258. {
  1259. unsigned pollflag = 0;
  1260. struct ipath_devdata *dd;
  1261. dd = pd->port_dd;
  1262. /* variable access in ipath_poll_hdrqfull() needs this */
  1263. rmb();
  1264. pollflag = ipath_poll_hdrqfull(pd);
  1265. if (pd->port_urgent != pd->port_urgent_poll) {
  1266. pollflag |= POLLIN | POLLRDNORM;
  1267. pd->port_urgent_poll = pd->port_urgent;
  1268. }
  1269. if (!pollflag) {
  1270. /* this saves a spin_lock/unlock in interrupt handler... */
  1271. set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
  1272. /* flush waiting flag so don't miss an event... */
  1273. wmb();
  1274. poll_wait(fp, &pd->port_wait, pt);
  1275. }
  1276. return pollflag;
  1277. }
  1278. static unsigned int ipath_poll_next(struct ipath_portdata *pd,
  1279. struct file *fp,
  1280. struct poll_table_struct *pt)
  1281. {
  1282. u32 head;
  1283. u32 tail;
  1284. unsigned pollflag = 0;
  1285. struct ipath_devdata *dd;
  1286. dd = pd->port_dd;
  1287. /* variable access in ipath_poll_hdrqfull() needs this */
  1288. rmb();
  1289. pollflag = ipath_poll_hdrqfull(pd);
  1290. head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
  1291. if (pd->port_rcvhdrtail_kvaddr)
  1292. tail = ipath_get_rcvhdrtail(pd);
  1293. else
  1294. tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
  1295. if (head != tail)
  1296. pollflag |= POLLIN | POLLRDNORM;
  1297. else {
  1298. /* this saves a spin_lock/unlock in interrupt handler */
  1299. set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
  1300. /* flush waiting flag so we don't miss an event */
  1301. wmb();
  1302. set_bit(pd->port_port + dd->ipath_r_intravail_shift,
  1303. &dd->ipath_rcvctrl);
  1304. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  1305. dd->ipath_rcvctrl);
  1306. if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
  1307. ipath_write_ureg(dd, ur_rcvhdrhead,
  1308. dd->ipath_rhdrhead_intr_off | head,
  1309. pd->port_port);
  1310. poll_wait(fp, &pd->port_wait, pt);
  1311. }
  1312. return pollflag;
  1313. }
  1314. static unsigned int ipath_poll(struct file *fp,
  1315. struct poll_table_struct *pt)
  1316. {
  1317. struct ipath_portdata *pd;
  1318. unsigned pollflag;
  1319. pd = port_fp(fp);
  1320. if (!pd)
  1321. pollflag = 0;
  1322. else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
  1323. pollflag = ipath_poll_urgent(pd, fp, pt);
  1324. else
  1325. pollflag = ipath_poll_next(pd, fp, pt);
  1326. return pollflag;
  1327. }
  1328. static int ipath_supports_subports(int user_swmajor, int user_swminor)
  1329. {
  1330. /* no subport implementation prior to software version 1.3 */
  1331. return (user_swmajor > 1) || (user_swminor >= 3);
  1332. }
  1333. static int ipath_compatible_subports(int user_swmajor, int user_swminor)
  1334. {
  1335. /* this code is written long-hand for clarity */
  1336. if (IPATH_USER_SWMAJOR != user_swmajor) {
  1337. /* no promise of compatibility if major mismatch */
  1338. return 0;
  1339. }
  1340. if (IPATH_USER_SWMAJOR == 1) {
  1341. switch (IPATH_USER_SWMINOR) {
  1342. case 0:
  1343. case 1:
  1344. case 2:
  1345. /* no subport implementation so cannot be compatible */
  1346. return 0;
  1347. case 3:
  1348. /* 3 is only compatible with itself */
  1349. return user_swminor == 3;
  1350. default:
  1351. /* >= 4 are compatible (or are expected to be) */
  1352. return user_swminor >= 4;
  1353. }
  1354. }
  1355. /* make no promises yet for future major versions */
  1356. return 0;
  1357. }
  1358. static int init_subports(struct ipath_devdata *dd,
  1359. struct ipath_portdata *pd,
  1360. const struct ipath_user_info *uinfo)
  1361. {
  1362. int ret = 0;
  1363. unsigned num_subports;
  1364. size_t size;
  1365. /*
  1366. * If the user is requesting zero subports,
  1367. * skip the subport allocation.
  1368. */
  1369. if (uinfo->spu_subport_cnt <= 0)
  1370. goto bail;
  1371. /* Self-consistency check for ipath_compatible_subports() */
  1372. if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
  1373. !ipath_compatible_subports(IPATH_USER_SWMAJOR,
  1374. IPATH_USER_SWMINOR)) {
  1375. dev_info(&dd->pcidev->dev,
  1376. "Inconsistent ipath_compatible_subports()\n");
  1377. goto bail;
  1378. }
  1379. /* Check for subport compatibility */
  1380. if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
  1381. uinfo->spu_userversion & 0xffff)) {
  1382. dev_info(&dd->pcidev->dev,
  1383. "Mismatched user version (%d.%d) and driver "
  1384. "version (%d.%d) while port sharing. Ensure "
  1385. "that driver and library are from the same "
  1386. "release.\n",
  1387. (int) (uinfo->spu_userversion >> 16),
  1388. (int) (uinfo->spu_userversion & 0xffff),
  1389. IPATH_USER_SWMAJOR,
  1390. IPATH_USER_SWMINOR);
  1391. goto bail;
  1392. }
  1393. if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
  1394. ret = -EINVAL;
  1395. goto bail;
  1396. }
  1397. num_subports = uinfo->spu_subport_cnt;
  1398. pd->subport_uregbase = vzalloc(PAGE_SIZE * num_subports);
  1399. if (!pd->subport_uregbase) {
  1400. ret = -ENOMEM;
  1401. goto bail;
  1402. }
  1403. /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
  1404. size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
  1405. sizeof(u32), PAGE_SIZE) * num_subports;
  1406. pd->subport_rcvhdr_base = vzalloc(size);
  1407. if (!pd->subport_rcvhdr_base) {
  1408. ret = -ENOMEM;
  1409. goto bail_ureg;
  1410. }
  1411. pd->subport_rcvegrbuf = vzalloc(pd->port_rcvegrbuf_chunks *
  1412. pd->port_rcvegrbuf_size *
  1413. num_subports);
  1414. if (!pd->subport_rcvegrbuf) {
  1415. ret = -ENOMEM;
  1416. goto bail_rhdr;
  1417. }
  1418. pd->port_subport_cnt = uinfo->spu_subport_cnt;
  1419. pd->port_subport_id = uinfo->spu_subport_id;
  1420. pd->active_slaves = 1;
  1421. set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
  1422. goto bail;
  1423. bail_rhdr:
  1424. vfree(pd->subport_rcvhdr_base);
  1425. bail_ureg:
  1426. vfree(pd->subport_uregbase);
  1427. pd->subport_uregbase = NULL;
  1428. bail:
  1429. return ret;
  1430. }
  1431. static int try_alloc_port(struct ipath_devdata *dd, int port,
  1432. struct file *fp,
  1433. const struct ipath_user_info *uinfo)
  1434. {
  1435. struct ipath_portdata *pd;
  1436. int ret;
  1437. if (!(pd = dd->ipath_pd[port])) {
  1438. void *ptmp;
  1439. pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
  1440. /*
  1441. * Allocate memory for use in ipath_tid_update() just once
  1442. * at open, not per call. Reduces cost of expected send
  1443. * setup.
  1444. */
  1445. ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
  1446. dd->ipath_rcvtidcnt * sizeof(struct page **),
  1447. GFP_KERNEL);
  1448. if (!pd || !ptmp) {
  1449. ipath_dev_err(dd, "Unable to allocate portdata "
  1450. "memory, failing open\n");
  1451. ret = -ENOMEM;
  1452. kfree(pd);
  1453. kfree(ptmp);
  1454. goto bail;
  1455. }
  1456. dd->ipath_pd[port] = pd;
  1457. dd->ipath_pd[port]->port_port = port;
  1458. dd->ipath_pd[port]->port_dd = dd;
  1459. dd->ipath_pd[port]->port_tid_pg_list = ptmp;
  1460. init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
  1461. }
  1462. if (!pd->port_cnt) {
  1463. pd->userversion = uinfo->spu_userversion;
  1464. init_user_egr_sizes(pd);
  1465. if ((ret = init_subports(dd, pd, uinfo)) != 0)
  1466. goto bail;
  1467. ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
  1468. current->comm, current->pid, dd->ipath_unit,
  1469. port);
  1470. pd->port_cnt = 1;
  1471. port_fp(fp) = pd;
  1472. pd->port_pid = get_pid(task_pid(current));
  1473. strlcpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
  1474. ipath_stats.sps_ports++;
  1475. ret = 0;
  1476. } else
  1477. ret = -EBUSY;
  1478. bail:
  1479. return ret;
  1480. }
  1481. static inline int usable(struct ipath_devdata *dd)
  1482. {
  1483. return dd &&
  1484. (dd->ipath_flags & IPATH_PRESENT) &&
  1485. dd->ipath_kregbase &&
  1486. dd->ipath_lid &&
  1487. !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
  1488. | IPATH_LINKUNK));
  1489. }
  1490. static int find_free_port(int unit, struct file *fp,
  1491. const struct ipath_user_info *uinfo)
  1492. {
  1493. struct ipath_devdata *dd = ipath_lookup(unit);
  1494. int ret, i;
  1495. if (!dd) {
  1496. ret = -ENODEV;
  1497. goto bail;
  1498. }
  1499. if (!usable(dd)) {
  1500. ret = -ENETDOWN;
  1501. goto bail;
  1502. }
  1503. for (i = 1; i < dd->ipath_cfgports; i++) {
  1504. ret = try_alloc_port(dd, i, fp, uinfo);
  1505. if (ret != -EBUSY)
  1506. goto bail;
  1507. }
  1508. ret = -EBUSY;
  1509. bail:
  1510. return ret;
  1511. }
  1512. static int find_best_unit(struct file *fp,
  1513. const struct ipath_user_info *uinfo)
  1514. {
  1515. int ret = 0, i, prefunit = -1, devmax;
  1516. int maxofallports, npresent, nup;
  1517. int ndev;
  1518. devmax = ipath_count_units(&npresent, &nup, &maxofallports);
  1519. /*
  1520. * This code is present to allow a knowledgeable person to
  1521. * specify the layout of processes to processors before opening
  1522. * this driver, and then we'll assign the process to the "closest"
  1523. * InfiniPath chip to that processor (we assume reasonable connectivity,
  1524. * for now). This code assumes that if affinity has been set
  1525. * before this point, that at most one cpu is set; for now this
  1526. * is reasonable. I check for both cpumask_empty() and cpumask_full(),
  1527. * in case some kernel variant sets none of the bits when no
  1528. * affinity is set. 2.6.11 and 12 kernels have all present
  1529. * cpus set. Some day we'll have to fix it up further to handle
  1530. * a cpu subset. This algorithm fails for two HT chips connected
  1531. * in tunnel fashion. Eventually this needs real topology
  1532. * information. There may be some issues with dual core numbering
  1533. * as well. This needs more work prior to release.
  1534. */
  1535. if (!cpumask_empty(tsk_cpus_allowed(current)) &&
  1536. !cpumask_full(tsk_cpus_allowed(current))) {
  1537. int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
  1538. get_online_cpus();
  1539. for_each_online_cpu(i)
  1540. if (cpumask_test_cpu(i, tsk_cpus_allowed(current))) {
  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. put_online_cpus();
  1548. if (curcpu != -1 && nset != ncpus) {
  1549. if (npresent) {
  1550. prefunit = curcpu / (ncpus / npresent);
  1551. ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
  1552. "%d cpus/chip, select unit %d\n",
  1553. current->comm, current->pid,
  1554. npresent, ncpus, ncpus / npresent,
  1555. prefunit);
  1556. }
  1557. }
  1558. }
  1559. /*
  1560. * user ports start at 1, kernel port is 0
  1561. * For now, we do round-robin access across all chips
  1562. */
  1563. if (prefunit != -1)
  1564. devmax = prefunit + 1;
  1565. recheck:
  1566. for (i = 1; i < maxofallports; i++) {
  1567. for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
  1568. ndev++) {
  1569. struct ipath_devdata *dd = ipath_lookup(ndev);
  1570. if (!usable(dd))
  1571. continue; /* can't use this unit */
  1572. if (i >= dd->ipath_cfgports)
  1573. /*
  1574. * Maxed out on users of this unit. Try
  1575. * next.
  1576. */
  1577. continue;
  1578. ret = try_alloc_port(dd, i, fp, uinfo);
  1579. if (!ret)
  1580. goto done;
  1581. }
  1582. }
  1583. if (npresent) {
  1584. if (nup == 0) {
  1585. ret = -ENETDOWN;
  1586. ipath_dbg("No ports available (none initialized "
  1587. "and ready)\n");
  1588. } else {
  1589. if (prefunit > 0) {
  1590. /* if started above 0, retry from 0 */
  1591. ipath_cdbg(PROC,
  1592. "%s[%u] no ports on prefunit "
  1593. "%d, clear and re-check\n",
  1594. current->comm, current->pid,
  1595. prefunit);
  1596. devmax = ipath_count_units(NULL, NULL,
  1597. NULL);
  1598. prefunit = -1;
  1599. goto recheck;
  1600. }
  1601. ret = -EBUSY;
  1602. ipath_dbg("No ports available\n");
  1603. }
  1604. } else {
  1605. ret = -ENXIO;
  1606. ipath_dbg("No boards found\n");
  1607. }
  1608. done:
  1609. return ret;
  1610. }
  1611. static int find_shared_port(struct file *fp,
  1612. const struct ipath_user_info *uinfo)
  1613. {
  1614. int devmax, ndev, i;
  1615. int ret = 0;
  1616. devmax = ipath_count_units(NULL, NULL, NULL);
  1617. for (ndev = 0; ndev < devmax; ndev++) {
  1618. struct ipath_devdata *dd = ipath_lookup(ndev);
  1619. if (!usable(dd))
  1620. continue;
  1621. for (i = 1; i < dd->ipath_cfgports; i++) {
  1622. struct ipath_portdata *pd = dd->ipath_pd[i];
  1623. /* Skip ports which are not yet open */
  1624. if (!pd || !pd->port_cnt)
  1625. continue;
  1626. /* Skip port if it doesn't match the requested one */
  1627. if (pd->port_subport_id != uinfo->spu_subport_id)
  1628. continue;
  1629. /* Verify the sharing process matches the master */
  1630. if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
  1631. pd->userversion != uinfo->spu_userversion ||
  1632. pd->port_cnt >= pd->port_subport_cnt) {
  1633. ret = -EINVAL;
  1634. goto done;
  1635. }
  1636. port_fp(fp) = pd;
  1637. subport_fp(fp) = pd->port_cnt++;
  1638. pd->port_subpid[subport_fp(fp)] =
  1639. get_pid(task_pid(current));
  1640. tidcursor_fp(fp) = 0;
  1641. pd->active_slaves |= 1 << subport_fp(fp);
  1642. ipath_cdbg(PROC,
  1643. "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
  1644. current->comm, current->pid,
  1645. subport_fp(fp),
  1646. pd->port_comm, pid_nr(pd->port_pid),
  1647. dd->ipath_unit, pd->port_port);
  1648. ret = 1;
  1649. goto done;
  1650. }
  1651. }
  1652. done:
  1653. return ret;
  1654. }
  1655. static int ipath_open(struct inode *in, struct file *fp)
  1656. {
  1657. /* The real work is performed later in ipath_assign_port() */
  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(file_inode(fp)) - IPATH_USER_MINOR_BASE;
  1695. ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
  1696. (long)file_inode(fp)->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. * explicitly 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 = 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. }