ipath_file_ops.c 63 KB

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