ipath_file_ops.c 72 KB

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