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