ipath_file_ops.c 51 KB

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