ipath_file_ops.c 52 KB

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