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