qib_init.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
  3. * All rights reserved.
  4. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/pci.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/delay.h>
  38. #include <linux/idr.h>
  39. #include "qib.h"
  40. #include "qib_common.h"
  41. /*
  42. * min buffers we want to have per context, after driver
  43. */
  44. #define QIB_MIN_USER_CTXT_BUFCNT 7
  45. #define QLOGIC_IB_R_SOFTWARE_MASK 0xFF
  46. #define QLOGIC_IB_R_SOFTWARE_SHIFT 24
  47. #define QLOGIC_IB_R_EMULATOR_MASK (1ULL<<62)
  48. /*
  49. * Number of ctxts we are configured to use (to allow for more pio
  50. * buffers per ctxt, etc.) Zero means use chip value.
  51. */
  52. ushort qib_cfgctxts;
  53. module_param_named(cfgctxts, qib_cfgctxts, ushort, S_IRUGO);
  54. MODULE_PARM_DESC(cfgctxts, "Set max number of contexts to use");
  55. /*
  56. * If set, do not write to any regs if avoidable, hack to allow
  57. * check for deranged default register values.
  58. */
  59. ushort qib_mini_init;
  60. module_param_named(mini_init, qib_mini_init, ushort, S_IRUGO);
  61. MODULE_PARM_DESC(mini_init, "If set, do minimal diag init");
  62. unsigned qib_n_krcv_queues;
  63. module_param_named(krcvqs, qib_n_krcv_queues, uint, S_IRUGO);
  64. MODULE_PARM_DESC(krcvqs, "number of kernel receive queues per IB port");
  65. /*
  66. * qib_wc_pat parameter:
  67. * 0 is WC via MTRR
  68. * 1 is WC via PAT
  69. * If PAT initialization fails, code reverts back to MTRR
  70. */
  71. unsigned qib_wc_pat = 1; /* default (1) is to use PAT, not MTRR */
  72. module_param_named(wc_pat, qib_wc_pat, uint, S_IRUGO);
  73. MODULE_PARM_DESC(wc_pat, "enable write-combining via PAT mechanism");
  74. struct workqueue_struct *qib_wq;
  75. struct workqueue_struct *qib_cq_wq;
  76. static void verify_interrupt(unsigned long);
  77. static struct idr qib_unit_table;
  78. u32 qib_cpulist_count;
  79. unsigned long *qib_cpulist;
  80. /* set number of contexts we'll actually use */
  81. void qib_set_ctxtcnt(struct qib_devdata *dd)
  82. {
  83. if (!qib_cfgctxts) {
  84. dd->cfgctxts = dd->first_user_ctxt + num_online_cpus();
  85. if (dd->cfgctxts > dd->ctxtcnt)
  86. dd->cfgctxts = dd->ctxtcnt;
  87. } else if (qib_cfgctxts < dd->num_pports)
  88. dd->cfgctxts = dd->ctxtcnt;
  89. else if (qib_cfgctxts <= dd->ctxtcnt)
  90. dd->cfgctxts = qib_cfgctxts;
  91. else
  92. dd->cfgctxts = dd->ctxtcnt;
  93. }
  94. /*
  95. * Common code for creating the receive context array.
  96. */
  97. int qib_create_ctxts(struct qib_devdata *dd)
  98. {
  99. unsigned i;
  100. int ret;
  101. /*
  102. * Allocate full ctxtcnt array, rather than just cfgctxts, because
  103. * cleanup iterates across all possible ctxts.
  104. */
  105. dd->rcd = kzalloc(sizeof(*dd->rcd) * dd->ctxtcnt, GFP_KERNEL);
  106. if (!dd->rcd) {
  107. qib_dev_err(dd, "Unable to allocate ctxtdata array, "
  108. "failing\n");
  109. ret = -ENOMEM;
  110. goto done;
  111. }
  112. /* create (one or more) kctxt */
  113. for (i = 0; i < dd->first_user_ctxt; ++i) {
  114. struct qib_pportdata *ppd;
  115. struct qib_ctxtdata *rcd;
  116. if (dd->skip_kctxt_mask & (1 << i))
  117. continue;
  118. ppd = dd->pport + (i % dd->num_pports);
  119. rcd = qib_create_ctxtdata(ppd, i);
  120. if (!rcd) {
  121. qib_dev_err(dd, "Unable to allocate ctxtdata"
  122. " for Kernel ctxt, failing\n");
  123. ret = -ENOMEM;
  124. goto done;
  125. }
  126. rcd->pkeys[0] = QIB_DEFAULT_P_KEY;
  127. rcd->seq_cnt = 1;
  128. }
  129. ret = 0;
  130. done:
  131. return ret;
  132. }
  133. /*
  134. * Common code for user and kernel context setup.
  135. */
  136. struct qib_ctxtdata *qib_create_ctxtdata(struct qib_pportdata *ppd, u32 ctxt)
  137. {
  138. struct qib_devdata *dd = ppd->dd;
  139. struct qib_ctxtdata *rcd;
  140. rcd = kzalloc(sizeof(*rcd), GFP_KERNEL);
  141. if (rcd) {
  142. INIT_LIST_HEAD(&rcd->qp_wait_list);
  143. rcd->ppd = ppd;
  144. rcd->dd = dd;
  145. rcd->cnt = 1;
  146. rcd->ctxt = ctxt;
  147. dd->rcd[ctxt] = rcd;
  148. dd->f_init_ctxt(rcd);
  149. /*
  150. * To avoid wasting a lot of memory, we allocate 32KB chunks
  151. * of physically contiguous memory, advance through it until
  152. * used up and then allocate more. Of course, we need
  153. * memory to store those extra pointers, now. 32KB seems to
  154. * be the most that is "safe" under memory pressure
  155. * (creating large files and then copying them over
  156. * NFS while doing lots of MPI jobs). The OOM killer can
  157. * get invoked, even though we say we can sleep and this can
  158. * cause significant system problems....
  159. */
  160. rcd->rcvegrbuf_size = 0x8000;
  161. rcd->rcvegrbufs_perchunk =
  162. rcd->rcvegrbuf_size / dd->rcvegrbufsize;
  163. rcd->rcvegrbuf_chunks = (rcd->rcvegrcnt +
  164. rcd->rcvegrbufs_perchunk - 1) /
  165. rcd->rcvegrbufs_perchunk;
  166. }
  167. return rcd;
  168. }
  169. /*
  170. * Common code for initializing the physical port structure.
  171. */
  172. void qib_init_pportdata(struct qib_pportdata *ppd, struct qib_devdata *dd,
  173. u8 hw_pidx, u8 port)
  174. {
  175. ppd->dd = dd;
  176. ppd->hw_pidx = hw_pidx;
  177. ppd->port = port; /* IB port number, not index */
  178. spin_lock_init(&ppd->sdma_lock);
  179. spin_lock_init(&ppd->lflags_lock);
  180. init_waitqueue_head(&ppd->state_wait);
  181. init_timer(&ppd->symerr_clear_timer);
  182. ppd->symerr_clear_timer.function = qib_clear_symerror_on_linkup;
  183. ppd->symerr_clear_timer.data = (unsigned long)ppd;
  184. }
  185. static int init_pioavailregs(struct qib_devdata *dd)
  186. {
  187. int ret, pidx;
  188. u64 *status_page;
  189. dd->pioavailregs_dma = dma_alloc_coherent(
  190. &dd->pcidev->dev, PAGE_SIZE, &dd->pioavailregs_phys,
  191. GFP_KERNEL);
  192. if (!dd->pioavailregs_dma) {
  193. qib_dev_err(dd, "failed to allocate PIOavail reg area "
  194. "in memory\n");
  195. ret = -ENOMEM;
  196. goto done;
  197. }
  198. /*
  199. * We really want L2 cache aligned, but for current CPUs of
  200. * interest, they are the same.
  201. */
  202. status_page = (u64 *)
  203. ((char *) dd->pioavailregs_dma +
  204. ((2 * L1_CACHE_BYTES +
  205. dd->pioavregs * sizeof(u64)) & ~L1_CACHE_BYTES));
  206. /* device status comes first, for backwards compatibility */
  207. dd->devstatusp = status_page;
  208. *status_page++ = 0;
  209. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  210. dd->pport[pidx].statusp = status_page;
  211. *status_page++ = 0;
  212. }
  213. /*
  214. * Setup buffer to hold freeze and other messages, accessible to
  215. * apps, following statusp. This is per-unit, not per port.
  216. */
  217. dd->freezemsg = (char *) status_page;
  218. *dd->freezemsg = 0;
  219. /* length of msg buffer is "whatever is left" */
  220. ret = (char *) status_page - (char *) dd->pioavailregs_dma;
  221. dd->freezelen = PAGE_SIZE - ret;
  222. ret = 0;
  223. done:
  224. return ret;
  225. }
  226. /**
  227. * init_shadow_tids - allocate the shadow TID array
  228. * @dd: the qlogic_ib device
  229. *
  230. * allocate the shadow TID array, so we can qib_munlock previous
  231. * entries. It may make more sense to move the pageshadow to the
  232. * ctxt data structure, so we only allocate memory for ctxts actually
  233. * in use, since we at 8k per ctxt, now.
  234. * We don't want failures here to prevent use of the driver/chip,
  235. * so no return value.
  236. */
  237. static void init_shadow_tids(struct qib_devdata *dd)
  238. {
  239. struct page **pages;
  240. dma_addr_t *addrs;
  241. pages = vmalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(struct page *));
  242. if (!pages) {
  243. qib_dev_err(dd, "failed to allocate shadow page * "
  244. "array, no expected sends!\n");
  245. goto bail;
  246. }
  247. addrs = vmalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(dma_addr_t));
  248. if (!addrs) {
  249. qib_dev_err(dd, "failed to allocate shadow dma handle "
  250. "array, no expected sends!\n");
  251. goto bail_free;
  252. }
  253. memset(pages, 0, dd->cfgctxts * dd->rcvtidcnt * sizeof(struct page *));
  254. memset(addrs, 0, dd->cfgctxts * dd->rcvtidcnt * sizeof(dma_addr_t));
  255. dd->pageshadow = pages;
  256. dd->physshadow = addrs;
  257. return;
  258. bail_free:
  259. vfree(pages);
  260. bail:
  261. dd->pageshadow = NULL;
  262. }
  263. /*
  264. * Do initialization for device that is only needed on
  265. * first detect, not on resets.
  266. */
  267. static int loadtime_init(struct qib_devdata *dd)
  268. {
  269. int ret = 0;
  270. if (((dd->revision >> QLOGIC_IB_R_SOFTWARE_SHIFT) &
  271. QLOGIC_IB_R_SOFTWARE_MASK) != QIB_CHIP_SWVERSION) {
  272. qib_dev_err(dd, "Driver only handles version %d, "
  273. "chip swversion is %d (%llx), failng\n",
  274. QIB_CHIP_SWVERSION,
  275. (int)(dd->revision >>
  276. QLOGIC_IB_R_SOFTWARE_SHIFT) &
  277. QLOGIC_IB_R_SOFTWARE_MASK,
  278. (unsigned long long) dd->revision);
  279. ret = -ENOSYS;
  280. goto done;
  281. }
  282. if (dd->revision & QLOGIC_IB_R_EMULATOR_MASK)
  283. qib_devinfo(dd->pcidev, "%s", dd->boardversion);
  284. spin_lock_init(&dd->pioavail_lock);
  285. spin_lock_init(&dd->sendctrl_lock);
  286. spin_lock_init(&dd->uctxt_lock);
  287. spin_lock_init(&dd->qib_diag_trans_lock);
  288. spin_lock_init(&dd->eep_st_lock);
  289. mutex_init(&dd->eep_lock);
  290. if (qib_mini_init)
  291. goto done;
  292. ret = init_pioavailregs(dd);
  293. init_shadow_tids(dd);
  294. qib_get_eeprom_info(dd);
  295. /* setup time (don't start yet) to verify we got interrupt */
  296. init_timer(&dd->intrchk_timer);
  297. dd->intrchk_timer.function = verify_interrupt;
  298. dd->intrchk_timer.data = (unsigned long) dd;
  299. done:
  300. return ret;
  301. }
  302. /**
  303. * init_after_reset - re-initialize after a reset
  304. * @dd: the qlogic_ib device
  305. *
  306. * sanity check at least some of the values after reset, and
  307. * ensure no receive or transmit (explictly, in case reset
  308. * failed
  309. */
  310. static int init_after_reset(struct qib_devdata *dd)
  311. {
  312. int i;
  313. /*
  314. * Ensure chip does no sends or receives, tail updates, or
  315. * pioavail updates while we re-initialize. This is mostly
  316. * for the driver data structures, not chip registers.
  317. */
  318. for (i = 0; i < dd->num_pports; ++i) {
  319. /*
  320. * ctxt == -1 means "all contexts". Only really safe for
  321. * _dis_abling things, as here.
  322. */
  323. dd->f_rcvctrl(dd->pport + i, QIB_RCVCTRL_CTXT_DIS |
  324. QIB_RCVCTRL_INTRAVAIL_DIS |
  325. QIB_RCVCTRL_TAILUPD_DIS, -1);
  326. /* Redundant across ports for some, but no big deal. */
  327. dd->f_sendctrl(dd->pport + i, QIB_SENDCTRL_SEND_DIS |
  328. QIB_SENDCTRL_AVAIL_DIS);
  329. }
  330. return 0;
  331. }
  332. static void enable_chip(struct qib_devdata *dd)
  333. {
  334. u64 rcvmask;
  335. int i;
  336. /*
  337. * Enable PIO send, and update of PIOavail regs to memory.
  338. */
  339. for (i = 0; i < dd->num_pports; ++i)
  340. dd->f_sendctrl(dd->pport + i, QIB_SENDCTRL_SEND_ENB |
  341. QIB_SENDCTRL_AVAIL_ENB);
  342. /*
  343. * Enable kernel ctxts' receive and receive interrupt.
  344. * Other ctxts done as user opens and inits them.
  345. */
  346. rcvmask = QIB_RCVCTRL_CTXT_ENB | QIB_RCVCTRL_INTRAVAIL_ENB;
  347. rcvmask |= (dd->flags & QIB_NODMA_RTAIL) ?
  348. QIB_RCVCTRL_TAILUPD_DIS : QIB_RCVCTRL_TAILUPD_ENB;
  349. for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
  350. struct qib_ctxtdata *rcd = dd->rcd[i];
  351. if (rcd)
  352. dd->f_rcvctrl(rcd->ppd, rcvmask, i);
  353. }
  354. }
  355. static void verify_interrupt(unsigned long opaque)
  356. {
  357. struct qib_devdata *dd = (struct qib_devdata *) opaque;
  358. if (!dd)
  359. return; /* being torn down */
  360. /*
  361. * If we don't have a lid or any interrupts, let the user know and
  362. * don't bother checking again.
  363. */
  364. if (dd->int_counter == 0) {
  365. if (!dd->f_intr_fallback(dd))
  366. dev_err(&dd->pcidev->dev, "No interrupts detected, "
  367. "not usable.\n");
  368. else /* re-arm the timer to see if fallback works */
  369. mod_timer(&dd->intrchk_timer, jiffies + HZ/2);
  370. }
  371. }
  372. static void init_piobuf_state(struct qib_devdata *dd)
  373. {
  374. int i, pidx;
  375. u32 uctxts;
  376. /*
  377. * Ensure all buffers are free, and fifos empty. Buffers
  378. * are common, so only do once for port 0.
  379. *
  380. * After enable and qib_chg_pioavailkernel so we can safely
  381. * enable pioavail updates and PIOENABLE. After this, packets
  382. * are ready and able to go out.
  383. */
  384. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_ALL);
  385. for (pidx = 0; pidx < dd->num_pports; ++pidx)
  386. dd->f_sendctrl(dd->pport + pidx, QIB_SENDCTRL_FLUSH);
  387. /*
  388. * If not all sendbufs are used, add the one to each of the lower
  389. * numbered contexts. pbufsctxt and lastctxt_piobuf are
  390. * calculated in chip-specific code because it may cause some
  391. * chip-specific adjustments to be made.
  392. */
  393. uctxts = dd->cfgctxts - dd->first_user_ctxt;
  394. dd->ctxts_extrabuf = dd->pbufsctxt ?
  395. dd->lastctxt_piobuf - (dd->pbufsctxt * uctxts) : 0;
  396. /*
  397. * Set up the shadow copies of the piobufavail registers,
  398. * which we compare against the chip registers for now, and
  399. * the in memory DMA'ed copies of the registers.
  400. * By now pioavail updates to memory should have occurred, so
  401. * copy them into our working/shadow registers; this is in
  402. * case something went wrong with abort, but mostly to get the
  403. * initial values of the generation bit correct.
  404. */
  405. for (i = 0; i < dd->pioavregs; i++) {
  406. __le64 tmp;
  407. tmp = dd->pioavailregs_dma[i];
  408. /*
  409. * Don't need to worry about pioavailkernel here
  410. * because we will call qib_chg_pioavailkernel() later
  411. * in initialization, to busy out buffers as needed.
  412. */
  413. dd->pioavailshadow[i] = le64_to_cpu(tmp);
  414. }
  415. while (i < ARRAY_SIZE(dd->pioavailshadow))
  416. dd->pioavailshadow[i++] = 0; /* for debugging sanity */
  417. /* after pioavailshadow is setup */
  418. qib_chg_pioavailkernel(dd, 0, dd->piobcnt2k + dd->piobcnt4k,
  419. TXCHK_CHG_TYPE_KERN, NULL);
  420. dd->f_initvl15_bufs(dd);
  421. }
  422. /**
  423. * qib_init - do the actual initialization sequence on the chip
  424. * @dd: the qlogic_ib device
  425. * @reinit: reinitializing, so don't allocate new memory
  426. *
  427. * Do the actual initialization sequence on the chip. This is done
  428. * both from the init routine called from the PCI infrastructure, and
  429. * when we reset the chip, or detect that it was reset internally,
  430. * or it's administratively re-enabled.
  431. *
  432. * Memory allocation here and in called routines is only done in
  433. * the first case (reinit == 0). We have to be careful, because even
  434. * without memory allocation, we need to re-write all the chip registers
  435. * TIDs, etc. after the reset or enable has completed.
  436. */
  437. int qib_init(struct qib_devdata *dd, int reinit)
  438. {
  439. int ret = 0, pidx, lastfail = 0;
  440. u32 portok = 0;
  441. unsigned i;
  442. struct qib_ctxtdata *rcd;
  443. struct qib_pportdata *ppd;
  444. unsigned long flags;
  445. /* Set linkstate to unknown, so we can watch for a transition. */
  446. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  447. ppd = dd->pport + pidx;
  448. spin_lock_irqsave(&ppd->lflags_lock, flags);
  449. ppd->lflags &= ~(QIBL_LINKACTIVE | QIBL_LINKARMED |
  450. QIBL_LINKDOWN | QIBL_LINKINIT |
  451. QIBL_LINKV);
  452. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  453. }
  454. if (reinit)
  455. ret = init_after_reset(dd);
  456. else
  457. ret = loadtime_init(dd);
  458. if (ret)
  459. goto done;
  460. /* Bypass most chip-init, to get to device creation */
  461. if (qib_mini_init)
  462. return 0;
  463. ret = dd->f_late_initreg(dd);
  464. if (ret)
  465. goto done;
  466. /* dd->rcd can be NULL if early init failed */
  467. for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
  468. /*
  469. * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
  470. * re-init, the simplest way to handle this is to free
  471. * existing, and re-allocate.
  472. * Need to re-create rest of ctxt 0 ctxtdata as well.
  473. */
  474. rcd = dd->rcd[i];
  475. if (!rcd)
  476. continue;
  477. lastfail = qib_create_rcvhdrq(dd, rcd);
  478. if (!lastfail)
  479. lastfail = qib_setup_eagerbufs(rcd);
  480. if (lastfail) {
  481. qib_dev_err(dd, "failed to allocate kernel ctxt's "
  482. "rcvhdrq and/or egr bufs\n");
  483. continue;
  484. }
  485. }
  486. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  487. int mtu;
  488. if (lastfail)
  489. ret = lastfail;
  490. ppd = dd->pport + pidx;
  491. mtu = ib_mtu_enum_to_int(qib_ibmtu);
  492. if (mtu == -1) {
  493. mtu = QIB_DEFAULT_MTU;
  494. qib_ibmtu = 0; /* don't leave invalid value */
  495. }
  496. /* set max we can ever have for this driver load */
  497. ppd->init_ibmaxlen = min(mtu > 2048 ?
  498. dd->piosize4k : dd->piosize2k,
  499. dd->rcvegrbufsize +
  500. (dd->rcvhdrentsize << 2));
  501. /*
  502. * Have to initialize ibmaxlen, but this will normally
  503. * change immediately in qib_set_mtu().
  504. */
  505. ppd->ibmaxlen = ppd->init_ibmaxlen;
  506. qib_set_mtu(ppd, mtu);
  507. spin_lock_irqsave(&ppd->lflags_lock, flags);
  508. ppd->lflags |= QIBL_IB_LINK_DISABLED;
  509. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  510. lastfail = dd->f_bringup_serdes(ppd);
  511. if (lastfail) {
  512. qib_devinfo(dd->pcidev,
  513. "Failed to bringup IB port %u\n", ppd->port);
  514. lastfail = -ENETDOWN;
  515. continue;
  516. }
  517. /* let link come up, and enable IBC */
  518. spin_lock_irqsave(&ppd->lflags_lock, flags);
  519. ppd->lflags &= ~QIBL_IB_LINK_DISABLED;
  520. spin_unlock_irqrestore(&ppd->lflags_lock, flags);
  521. portok++;
  522. }
  523. if (!portok) {
  524. /* none of the ports initialized */
  525. if (!ret && lastfail)
  526. ret = lastfail;
  527. else if (!ret)
  528. ret = -ENETDOWN;
  529. /* but continue on, so we can debug cause */
  530. }
  531. enable_chip(dd);
  532. init_piobuf_state(dd);
  533. done:
  534. if (!ret) {
  535. /* chip is OK for user apps; mark it as initialized */
  536. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  537. ppd = dd->pport + pidx;
  538. /*
  539. * Set status even if port serdes is not initialized
  540. * so that diags will work.
  541. */
  542. *ppd->statusp |= QIB_STATUS_CHIP_PRESENT |
  543. QIB_STATUS_INITTED;
  544. if (!ppd->link_speed_enabled)
  545. continue;
  546. if (dd->flags & QIB_HAS_SEND_DMA)
  547. ret = qib_setup_sdma(ppd);
  548. init_timer(&ppd->hol_timer);
  549. ppd->hol_timer.function = qib_hol_event;
  550. ppd->hol_timer.data = (unsigned long)ppd;
  551. ppd->hol_state = QIB_HOL_UP;
  552. }
  553. /* now we can enable all interrupts from the chip */
  554. dd->f_set_intr_state(dd, 1);
  555. /*
  556. * Setup to verify we get an interrupt, and fallback
  557. * to an alternate if necessary and possible.
  558. */
  559. mod_timer(&dd->intrchk_timer, jiffies + HZ/2);
  560. /* start stats retrieval timer */
  561. mod_timer(&dd->stats_timer, jiffies + HZ * ACTIVITY_TIMER);
  562. }
  563. /* if ret is non-zero, we probably should do some cleanup here... */
  564. return ret;
  565. }
  566. /*
  567. * These next two routines are placeholders in case we don't have per-arch
  568. * code for controlling write combining. If explicit control of write
  569. * combining is not available, performance will probably be awful.
  570. */
  571. int __attribute__((weak)) qib_enable_wc(struct qib_devdata *dd)
  572. {
  573. return -EOPNOTSUPP;
  574. }
  575. void __attribute__((weak)) qib_disable_wc(struct qib_devdata *dd)
  576. {
  577. }
  578. static inline struct qib_devdata *__qib_lookup(int unit)
  579. {
  580. return idr_find(&qib_unit_table, unit);
  581. }
  582. struct qib_devdata *qib_lookup(int unit)
  583. {
  584. struct qib_devdata *dd;
  585. unsigned long flags;
  586. spin_lock_irqsave(&qib_devs_lock, flags);
  587. dd = __qib_lookup(unit);
  588. spin_unlock_irqrestore(&qib_devs_lock, flags);
  589. return dd;
  590. }
  591. /*
  592. * Stop the timers during unit shutdown, or after an error late
  593. * in initialization.
  594. */
  595. static void qib_stop_timers(struct qib_devdata *dd)
  596. {
  597. struct qib_pportdata *ppd;
  598. int pidx;
  599. if (dd->stats_timer.data) {
  600. del_timer_sync(&dd->stats_timer);
  601. dd->stats_timer.data = 0;
  602. }
  603. if (dd->intrchk_timer.data) {
  604. del_timer_sync(&dd->intrchk_timer);
  605. dd->intrchk_timer.data = 0;
  606. }
  607. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  608. ppd = dd->pport + pidx;
  609. if (ppd->hol_timer.data)
  610. del_timer_sync(&ppd->hol_timer);
  611. if (ppd->led_override_timer.data) {
  612. del_timer_sync(&ppd->led_override_timer);
  613. atomic_set(&ppd->led_override_timer_active, 0);
  614. }
  615. if (ppd->symerr_clear_timer.data)
  616. del_timer_sync(&ppd->symerr_clear_timer);
  617. }
  618. }
  619. /**
  620. * qib_shutdown_device - shut down a device
  621. * @dd: the qlogic_ib device
  622. *
  623. * This is called to make the device quiet when we are about to
  624. * unload the driver, and also when the device is administratively
  625. * disabled. It does not free any data structures.
  626. * Everything it does has to be setup again by qib_init(dd, 1)
  627. */
  628. static void qib_shutdown_device(struct qib_devdata *dd)
  629. {
  630. struct qib_pportdata *ppd;
  631. unsigned pidx;
  632. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  633. ppd = dd->pport + pidx;
  634. spin_lock_irq(&ppd->lflags_lock);
  635. ppd->lflags &= ~(QIBL_LINKDOWN | QIBL_LINKINIT |
  636. QIBL_LINKARMED | QIBL_LINKACTIVE |
  637. QIBL_LINKV);
  638. spin_unlock_irq(&ppd->lflags_lock);
  639. *ppd->statusp &= ~(QIB_STATUS_IB_CONF | QIB_STATUS_IB_READY);
  640. }
  641. dd->flags &= ~QIB_INITTED;
  642. /* mask interrupts, but not errors */
  643. dd->f_set_intr_state(dd, 0);
  644. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  645. ppd = dd->pport + pidx;
  646. dd->f_rcvctrl(ppd, QIB_RCVCTRL_TAILUPD_DIS |
  647. QIB_RCVCTRL_CTXT_DIS |
  648. QIB_RCVCTRL_INTRAVAIL_DIS |
  649. QIB_RCVCTRL_PKEY_ENB, -1);
  650. /*
  651. * Gracefully stop all sends allowing any in progress to
  652. * trickle out first.
  653. */
  654. dd->f_sendctrl(ppd, QIB_SENDCTRL_CLEAR);
  655. }
  656. /*
  657. * Enough for anything that's going to trickle out to have actually
  658. * done so.
  659. */
  660. udelay(20);
  661. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  662. ppd = dd->pport + pidx;
  663. dd->f_setextled(ppd, 0); /* make sure LEDs are off */
  664. if (dd->flags & QIB_HAS_SEND_DMA)
  665. qib_teardown_sdma(ppd);
  666. dd->f_sendctrl(ppd, QIB_SENDCTRL_AVAIL_DIS |
  667. QIB_SENDCTRL_SEND_DIS);
  668. /*
  669. * Clear SerdesEnable.
  670. * We can't count on interrupts since we are stopping.
  671. */
  672. dd->f_quiet_serdes(ppd);
  673. }
  674. qib_update_eeprom_log(dd);
  675. }
  676. /**
  677. * qib_free_ctxtdata - free a context's allocated data
  678. * @dd: the qlogic_ib device
  679. * @rcd: the ctxtdata structure
  680. *
  681. * free up any allocated data for a context
  682. * This should not touch anything that would affect a simultaneous
  683. * re-allocation of context data, because it is called after qib_mutex
  684. * is released (and can be called from reinit as well).
  685. * It should never change any chip state, or global driver state.
  686. */
  687. void qib_free_ctxtdata(struct qib_devdata *dd, struct qib_ctxtdata *rcd)
  688. {
  689. if (!rcd)
  690. return;
  691. if (rcd->rcvhdrq) {
  692. dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
  693. rcd->rcvhdrq, rcd->rcvhdrq_phys);
  694. rcd->rcvhdrq = NULL;
  695. if (rcd->rcvhdrtail_kvaddr) {
  696. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  697. rcd->rcvhdrtail_kvaddr,
  698. rcd->rcvhdrqtailaddr_phys);
  699. rcd->rcvhdrtail_kvaddr = NULL;
  700. }
  701. }
  702. if (rcd->rcvegrbuf) {
  703. unsigned e;
  704. for (e = 0; e < rcd->rcvegrbuf_chunks; e++) {
  705. void *base = rcd->rcvegrbuf[e];
  706. size_t size = rcd->rcvegrbuf_size;
  707. dma_free_coherent(&dd->pcidev->dev, size,
  708. base, rcd->rcvegrbuf_phys[e]);
  709. }
  710. kfree(rcd->rcvegrbuf);
  711. rcd->rcvegrbuf = NULL;
  712. kfree(rcd->rcvegrbuf_phys);
  713. rcd->rcvegrbuf_phys = NULL;
  714. rcd->rcvegrbuf_chunks = 0;
  715. }
  716. kfree(rcd->tid_pg_list);
  717. vfree(rcd->user_event_mask);
  718. vfree(rcd->subctxt_uregbase);
  719. vfree(rcd->subctxt_rcvegrbuf);
  720. vfree(rcd->subctxt_rcvhdr_base);
  721. kfree(rcd);
  722. }
  723. /*
  724. * Perform a PIO buffer bandwidth write test, to verify proper system
  725. * configuration. Even when all the setup calls work, occasionally
  726. * BIOS or other issues can prevent write combining from working, or
  727. * can cause other bandwidth problems to the chip.
  728. *
  729. * This test simply writes the same buffer over and over again, and
  730. * measures close to the peak bandwidth to the chip (not testing
  731. * data bandwidth to the wire). On chips that use an address-based
  732. * trigger to send packets to the wire, this is easy. On chips that
  733. * use a count to trigger, we want to make sure that the packet doesn't
  734. * go out on the wire, or trigger flow control checks.
  735. */
  736. static void qib_verify_pioperf(struct qib_devdata *dd)
  737. {
  738. u32 pbnum, cnt, lcnt;
  739. u32 __iomem *piobuf;
  740. u32 *addr;
  741. u64 msecs, emsecs;
  742. piobuf = dd->f_getsendbuf(dd->pport, 0ULL, &pbnum);
  743. if (!piobuf) {
  744. qib_devinfo(dd->pcidev,
  745. "No PIObufs for checking perf, skipping\n");
  746. return;
  747. }
  748. /*
  749. * Enough to give us a reasonable test, less than piobuf size, and
  750. * likely multiple of store buffer length.
  751. */
  752. cnt = 1024;
  753. addr = vmalloc(cnt);
  754. if (!addr) {
  755. qib_devinfo(dd->pcidev,
  756. "Couldn't get memory for checking PIO perf,"
  757. " skipping\n");
  758. goto done;
  759. }
  760. preempt_disable(); /* we want reasonably accurate elapsed time */
  761. msecs = 1 + jiffies_to_msecs(jiffies);
  762. for (lcnt = 0; lcnt < 10000U; lcnt++) {
  763. /* wait until we cross msec boundary */
  764. if (jiffies_to_msecs(jiffies) >= msecs)
  765. break;
  766. udelay(1);
  767. }
  768. dd->f_set_armlaunch(dd, 0);
  769. /*
  770. * length 0, no dwords actually sent
  771. */
  772. writeq(0, piobuf);
  773. qib_flush_wc();
  774. /*
  775. * This is only roughly accurate, since even with preempt we
  776. * still take interrupts that could take a while. Running for
  777. * >= 5 msec seems to get us "close enough" to accurate values.
  778. */
  779. msecs = jiffies_to_msecs(jiffies);
  780. for (emsecs = lcnt = 0; emsecs <= 5UL; lcnt++) {
  781. qib_pio_copy(piobuf + 64, addr, cnt >> 2);
  782. emsecs = jiffies_to_msecs(jiffies) - msecs;
  783. }
  784. /* 1 GiB/sec, slightly over IB SDR line rate */
  785. if (lcnt < (emsecs * 1024U))
  786. qib_dev_err(dd,
  787. "Performance problem: bandwidth to PIO buffers is "
  788. "only %u MiB/sec\n",
  789. lcnt / (u32) emsecs);
  790. preempt_enable();
  791. vfree(addr);
  792. done:
  793. /* disarm piobuf, so it's available again */
  794. dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(pbnum));
  795. qib_sendbuf_done(dd, pbnum);
  796. dd->f_set_armlaunch(dd, 1);
  797. }
  798. void qib_free_devdata(struct qib_devdata *dd)
  799. {
  800. unsigned long flags;
  801. spin_lock_irqsave(&qib_devs_lock, flags);
  802. idr_remove(&qib_unit_table, dd->unit);
  803. list_del(&dd->list);
  804. spin_unlock_irqrestore(&qib_devs_lock, flags);
  805. ib_dealloc_device(&dd->verbs_dev.ibdev);
  806. }
  807. /*
  808. * Allocate our primary per-unit data structure. Must be done via verbs
  809. * allocator, because the verbs cleanup process both does cleanup and
  810. * free of the data structure.
  811. * "extra" is for chip-specific data.
  812. *
  813. * Use the idr mechanism to get a unit number for this unit.
  814. */
  815. struct qib_devdata *qib_alloc_devdata(struct pci_dev *pdev, size_t extra)
  816. {
  817. unsigned long flags;
  818. struct qib_devdata *dd;
  819. int ret;
  820. if (!idr_pre_get(&qib_unit_table, GFP_KERNEL)) {
  821. dd = ERR_PTR(-ENOMEM);
  822. goto bail;
  823. }
  824. dd = (struct qib_devdata *) ib_alloc_device(sizeof(*dd) + extra);
  825. if (!dd) {
  826. dd = ERR_PTR(-ENOMEM);
  827. goto bail;
  828. }
  829. spin_lock_irqsave(&qib_devs_lock, flags);
  830. ret = idr_get_new(&qib_unit_table, dd, &dd->unit);
  831. if (ret >= 0)
  832. list_add(&dd->list, &qib_dev_list);
  833. spin_unlock_irqrestore(&qib_devs_lock, flags);
  834. if (ret < 0) {
  835. qib_early_err(&pdev->dev,
  836. "Could not allocate unit ID: error %d\n", -ret);
  837. ib_dealloc_device(&dd->verbs_dev.ibdev);
  838. dd = ERR_PTR(ret);
  839. goto bail;
  840. }
  841. if (!qib_cpulist_count) {
  842. u32 count = num_online_cpus();
  843. qib_cpulist = kzalloc(BITS_TO_LONGS(count) *
  844. sizeof(long), GFP_KERNEL);
  845. if (qib_cpulist)
  846. qib_cpulist_count = count;
  847. else
  848. qib_early_err(&pdev->dev, "Could not alloc cpulist "
  849. "info, cpu affinity might be wrong\n");
  850. }
  851. bail:
  852. return dd;
  853. }
  854. /*
  855. * Called from freeze mode handlers, and from PCI error
  856. * reporting code. Should be paranoid about state of
  857. * system and data structures.
  858. */
  859. void qib_disable_after_error(struct qib_devdata *dd)
  860. {
  861. if (dd->flags & QIB_INITTED) {
  862. u32 pidx;
  863. dd->flags &= ~QIB_INITTED;
  864. if (dd->pport)
  865. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  866. struct qib_pportdata *ppd;
  867. ppd = dd->pport + pidx;
  868. if (dd->flags & QIB_PRESENT) {
  869. qib_set_linkstate(ppd,
  870. QIB_IB_LINKDOWN_DISABLE);
  871. dd->f_setextled(ppd, 0);
  872. }
  873. *ppd->statusp &= ~QIB_STATUS_IB_READY;
  874. }
  875. }
  876. /*
  877. * Mark as having had an error for driver, and also
  878. * for /sys and status word mapped to user programs.
  879. * This marks unit as not usable, until reset.
  880. */
  881. if (dd->devstatusp)
  882. *dd->devstatusp |= QIB_STATUS_HWERROR;
  883. }
  884. static void __devexit qib_remove_one(struct pci_dev *);
  885. static int __devinit qib_init_one(struct pci_dev *,
  886. const struct pci_device_id *);
  887. #define DRIVER_LOAD_MSG "QLogic " QIB_DRV_NAME " loaded: "
  888. #define PFX QIB_DRV_NAME ": "
  889. static const struct pci_device_id qib_pci_tbl[] = {
  890. { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_QLOGIC_IB_6120) },
  891. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_IB_7220) },
  892. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_IB_7322) },
  893. { 0, }
  894. };
  895. MODULE_DEVICE_TABLE(pci, qib_pci_tbl);
  896. struct pci_driver qib_driver = {
  897. .name = QIB_DRV_NAME,
  898. .probe = qib_init_one,
  899. .remove = __devexit_p(qib_remove_one),
  900. .id_table = qib_pci_tbl,
  901. .err_handler = &qib_pci_err_handler,
  902. };
  903. /*
  904. * Do all the generic driver unit- and chip-independent memory
  905. * allocation and initialization.
  906. */
  907. static int __init qlogic_ib_init(void)
  908. {
  909. int ret;
  910. ret = qib_dev_init();
  911. if (ret)
  912. goto bail;
  913. /*
  914. * We create our own workqueue mainly because we want to be
  915. * able to flush it when devices are being removed. We can't
  916. * use schedule_work()/flush_scheduled_work() because both
  917. * unregister_netdev() and linkwatch_event take the rtnl lock,
  918. * so flush_scheduled_work() can deadlock during device
  919. * removal.
  920. */
  921. qib_wq = create_workqueue("qib");
  922. if (!qib_wq) {
  923. ret = -ENOMEM;
  924. goto bail_dev;
  925. }
  926. qib_cq_wq = create_singlethread_workqueue("qib_cq");
  927. if (!qib_cq_wq) {
  928. ret = -ENOMEM;
  929. goto bail_wq;
  930. }
  931. /*
  932. * These must be called before the driver is registered with
  933. * the PCI subsystem.
  934. */
  935. idr_init(&qib_unit_table);
  936. if (!idr_pre_get(&qib_unit_table, GFP_KERNEL)) {
  937. printk(KERN_ERR QIB_DRV_NAME ": idr_pre_get() failed\n");
  938. ret = -ENOMEM;
  939. goto bail_cq_wq;
  940. }
  941. ret = pci_register_driver(&qib_driver);
  942. if (ret < 0) {
  943. printk(KERN_ERR QIB_DRV_NAME
  944. ": Unable to register driver: error %d\n", -ret);
  945. goto bail_unit;
  946. }
  947. /* not fatal if it doesn't work */
  948. if (qib_init_qibfs())
  949. printk(KERN_ERR QIB_DRV_NAME ": Unable to register ipathfs\n");
  950. goto bail; /* all OK */
  951. bail_unit:
  952. idr_destroy(&qib_unit_table);
  953. bail_cq_wq:
  954. destroy_workqueue(qib_cq_wq);
  955. bail_wq:
  956. destroy_workqueue(qib_wq);
  957. bail_dev:
  958. qib_dev_cleanup();
  959. bail:
  960. return ret;
  961. }
  962. module_init(qlogic_ib_init);
  963. /*
  964. * Do the non-unit driver cleanup, memory free, etc. at unload.
  965. */
  966. static void __exit qlogic_ib_cleanup(void)
  967. {
  968. int ret;
  969. ret = qib_exit_qibfs();
  970. if (ret)
  971. printk(KERN_ERR QIB_DRV_NAME ": "
  972. "Unable to cleanup counter filesystem: "
  973. "error %d\n", -ret);
  974. pci_unregister_driver(&qib_driver);
  975. destroy_workqueue(qib_wq);
  976. destroy_workqueue(qib_cq_wq);
  977. qib_cpulist_count = 0;
  978. kfree(qib_cpulist);
  979. idr_destroy(&qib_unit_table);
  980. qib_dev_cleanup();
  981. }
  982. module_exit(qlogic_ib_cleanup);
  983. /* this can only be called after a successful initialization */
  984. static void cleanup_device_data(struct qib_devdata *dd)
  985. {
  986. int ctxt;
  987. int pidx;
  988. struct qib_ctxtdata **tmp;
  989. unsigned long flags;
  990. /* users can't do anything more with chip */
  991. for (pidx = 0; pidx < dd->num_pports; ++pidx)
  992. if (dd->pport[pidx].statusp)
  993. *dd->pport[pidx].statusp &= ~QIB_STATUS_CHIP_PRESENT;
  994. if (!qib_wc_pat)
  995. qib_disable_wc(dd);
  996. if (dd->pioavailregs_dma) {
  997. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  998. (void *) dd->pioavailregs_dma,
  999. dd->pioavailregs_phys);
  1000. dd->pioavailregs_dma = NULL;
  1001. }
  1002. if (dd->pageshadow) {
  1003. struct page **tmpp = dd->pageshadow;
  1004. dma_addr_t *tmpd = dd->physshadow;
  1005. int i, cnt = 0;
  1006. for (ctxt = 0; ctxt < dd->cfgctxts; ctxt++) {
  1007. int ctxt_tidbase = ctxt * dd->rcvtidcnt;
  1008. int maxtid = ctxt_tidbase + dd->rcvtidcnt;
  1009. for (i = ctxt_tidbase; i < maxtid; i++) {
  1010. if (!tmpp[i])
  1011. continue;
  1012. pci_unmap_page(dd->pcidev, tmpd[i],
  1013. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1014. qib_release_user_pages(&tmpp[i], 1);
  1015. tmpp[i] = NULL;
  1016. cnt++;
  1017. }
  1018. }
  1019. tmpp = dd->pageshadow;
  1020. dd->pageshadow = NULL;
  1021. vfree(tmpp);
  1022. }
  1023. /*
  1024. * Free any resources still in use (usually just kernel contexts)
  1025. * at unload; we do for ctxtcnt, because that's what we allocate.
  1026. * We acquire lock to be really paranoid that rcd isn't being
  1027. * accessed from some interrupt-related code (that should not happen,
  1028. * but best to be sure).
  1029. */
  1030. spin_lock_irqsave(&dd->uctxt_lock, flags);
  1031. tmp = dd->rcd;
  1032. dd->rcd = NULL;
  1033. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  1034. for (ctxt = 0; tmp && ctxt < dd->ctxtcnt; ctxt++) {
  1035. struct qib_ctxtdata *rcd = tmp[ctxt];
  1036. tmp[ctxt] = NULL; /* debugging paranoia */
  1037. qib_free_ctxtdata(dd, rcd);
  1038. }
  1039. kfree(tmp);
  1040. kfree(dd->boardname);
  1041. }
  1042. /*
  1043. * Clean up on unit shutdown, or error during unit load after
  1044. * successful initialization.
  1045. */
  1046. static void qib_postinit_cleanup(struct qib_devdata *dd)
  1047. {
  1048. /*
  1049. * Clean up chip-specific stuff.
  1050. * We check for NULL here, because it's outside
  1051. * the kregbase check, and we need to call it
  1052. * after the free_irq. Thus it's possible that
  1053. * the function pointers were never initialized.
  1054. */
  1055. if (dd->f_cleanup)
  1056. dd->f_cleanup(dd);
  1057. qib_pcie_ddcleanup(dd);
  1058. cleanup_device_data(dd);
  1059. qib_free_devdata(dd);
  1060. }
  1061. static int __devinit qib_init_one(struct pci_dev *pdev,
  1062. const struct pci_device_id *ent)
  1063. {
  1064. int ret, j, pidx, initfail;
  1065. struct qib_devdata *dd = NULL;
  1066. ret = qib_pcie_init(pdev, ent);
  1067. if (ret)
  1068. goto bail;
  1069. /*
  1070. * Do device-specific initialiation, function table setup, dd
  1071. * allocation, etc.
  1072. */
  1073. switch (ent->device) {
  1074. case PCI_DEVICE_ID_QLOGIC_IB_6120:
  1075. #ifdef CONFIG_PCI_MSI
  1076. dd = qib_init_iba6120_funcs(pdev, ent);
  1077. #else
  1078. qib_early_err(&pdev->dev, "QLogic PCIE device 0x%x cannot "
  1079. "work if CONFIG_PCI_MSI is not enabled\n",
  1080. ent->device);
  1081. dd = ERR_PTR(-ENODEV);
  1082. #endif
  1083. break;
  1084. case PCI_DEVICE_ID_QLOGIC_IB_7220:
  1085. dd = qib_init_iba7220_funcs(pdev, ent);
  1086. break;
  1087. case PCI_DEVICE_ID_QLOGIC_IB_7322:
  1088. dd = qib_init_iba7322_funcs(pdev, ent);
  1089. break;
  1090. default:
  1091. qib_early_err(&pdev->dev, "Failing on unknown QLogic "
  1092. "deviceid 0x%x\n", ent->device);
  1093. ret = -ENODEV;
  1094. }
  1095. if (IS_ERR(dd))
  1096. ret = PTR_ERR(dd);
  1097. if (ret)
  1098. goto bail; /* error already printed */
  1099. /* do the generic initialization */
  1100. initfail = qib_init(dd, 0);
  1101. ret = qib_register_ib_device(dd);
  1102. /*
  1103. * Now ready for use. this should be cleared whenever we
  1104. * detect a reset, or initiate one. If earlier failure,
  1105. * we still create devices, so diags, etc. can be used
  1106. * to determine cause of problem.
  1107. */
  1108. if (!qib_mini_init && !initfail && !ret)
  1109. dd->flags |= QIB_INITTED;
  1110. j = qib_device_create(dd);
  1111. if (j)
  1112. qib_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
  1113. j = qibfs_add(dd);
  1114. if (j)
  1115. qib_dev_err(dd, "Failed filesystem setup for counters: %d\n",
  1116. -j);
  1117. if (qib_mini_init || initfail || ret) {
  1118. qib_stop_timers(dd);
  1119. flush_scheduled_work();
  1120. for (pidx = 0; pidx < dd->num_pports; ++pidx)
  1121. dd->f_quiet_serdes(dd->pport + pidx);
  1122. if (qib_mini_init)
  1123. goto bail;
  1124. if (!j) {
  1125. (void) qibfs_remove(dd);
  1126. qib_device_remove(dd);
  1127. }
  1128. if (!ret)
  1129. qib_unregister_ib_device(dd);
  1130. qib_postinit_cleanup(dd);
  1131. if (initfail)
  1132. ret = initfail;
  1133. goto bail;
  1134. }
  1135. if (!qib_wc_pat) {
  1136. ret = qib_enable_wc(dd);
  1137. if (ret) {
  1138. qib_dev_err(dd, "Write combining not enabled "
  1139. "(err %d): performance may be poor\n",
  1140. -ret);
  1141. ret = 0;
  1142. }
  1143. }
  1144. qib_verify_pioperf(dd);
  1145. bail:
  1146. return ret;
  1147. }
  1148. static void __devexit qib_remove_one(struct pci_dev *pdev)
  1149. {
  1150. struct qib_devdata *dd = pci_get_drvdata(pdev);
  1151. int ret;
  1152. /* unregister from IB core */
  1153. qib_unregister_ib_device(dd);
  1154. /*
  1155. * Disable the IB link, disable interrupts on the device,
  1156. * clear dma engines, etc.
  1157. */
  1158. if (!qib_mini_init)
  1159. qib_shutdown_device(dd);
  1160. qib_stop_timers(dd);
  1161. /* wait until all of our (qsfp) schedule_work() calls complete */
  1162. flush_scheduled_work();
  1163. ret = qibfs_remove(dd);
  1164. if (ret)
  1165. qib_dev_err(dd, "Failed counters filesystem cleanup: %d\n",
  1166. -ret);
  1167. qib_device_remove(dd);
  1168. qib_postinit_cleanup(dd);
  1169. }
  1170. /**
  1171. * qib_create_rcvhdrq - create a receive header queue
  1172. * @dd: the qlogic_ib device
  1173. * @rcd: the context data
  1174. *
  1175. * This must be contiguous memory (from an i/o perspective), and must be
  1176. * DMA'able (which means for some systems, it will go through an IOMMU,
  1177. * or be forced into a low address range).
  1178. */
  1179. int qib_create_rcvhdrq(struct qib_devdata *dd, struct qib_ctxtdata *rcd)
  1180. {
  1181. unsigned amt;
  1182. if (!rcd->rcvhdrq) {
  1183. dma_addr_t phys_hdrqtail;
  1184. gfp_t gfp_flags;
  1185. amt = ALIGN(dd->rcvhdrcnt * dd->rcvhdrentsize *
  1186. sizeof(u32), PAGE_SIZE);
  1187. gfp_flags = (rcd->ctxt >= dd->first_user_ctxt) ?
  1188. GFP_USER : GFP_KERNEL;
  1189. rcd->rcvhdrq = dma_alloc_coherent(
  1190. &dd->pcidev->dev, amt, &rcd->rcvhdrq_phys,
  1191. gfp_flags | __GFP_COMP);
  1192. if (!rcd->rcvhdrq) {
  1193. qib_dev_err(dd, "attempt to allocate %d bytes "
  1194. "for ctxt %u rcvhdrq failed\n",
  1195. amt, rcd->ctxt);
  1196. goto bail;
  1197. }
  1198. if (rcd->ctxt >= dd->first_user_ctxt) {
  1199. rcd->user_event_mask = vmalloc_user(PAGE_SIZE);
  1200. if (!rcd->user_event_mask)
  1201. goto bail_free_hdrq;
  1202. }
  1203. if (!(dd->flags & QIB_NODMA_RTAIL)) {
  1204. rcd->rcvhdrtail_kvaddr = dma_alloc_coherent(
  1205. &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
  1206. gfp_flags);
  1207. if (!rcd->rcvhdrtail_kvaddr)
  1208. goto bail_free;
  1209. rcd->rcvhdrqtailaddr_phys = phys_hdrqtail;
  1210. }
  1211. rcd->rcvhdrq_size = amt;
  1212. }
  1213. /* clear for security and sanity on each use */
  1214. memset(rcd->rcvhdrq, 0, rcd->rcvhdrq_size);
  1215. if (rcd->rcvhdrtail_kvaddr)
  1216. memset(rcd->rcvhdrtail_kvaddr, 0, PAGE_SIZE);
  1217. return 0;
  1218. bail_free:
  1219. qib_dev_err(dd, "attempt to allocate 1 page for ctxt %u "
  1220. "rcvhdrqtailaddr failed\n", rcd->ctxt);
  1221. vfree(rcd->user_event_mask);
  1222. rcd->user_event_mask = NULL;
  1223. bail_free_hdrq:
  1224. dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
  1225. rcd->rcvhdrq_phys);
  1226. rcd->rcvhdrq = NULL;
  1227. bail:
  1228. return -ENOMEM;
  1229. }
  1230. /**
  1231. * allocate eager buffers, both kernel and user contexts.
  1232. * @rcd: the context we are setting up.
  1233. *
  1234. * Allocate the eager TID buffers and program them into hip.
  1235. * They are no longer completely contiguous, we do multiple allocation
  1236. * calls. Otherwise we get the OOM code involved, by asking for too
  1237. * much per call, with disastrous results on some kernels.
  1238. */
  1239. int qib_setup_eagerbufs(struct qib_ctxtdata *rcd)
  1240. {
  1241. struct qib_devdata *dd = rcd->dd;
  1242. unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
  1243. size_t size;
  1244. gfp_t gfp_flags;
  1245. /*
  1246. * GFP_USER, but without GFP_FS, so buffer cache can be
  1247. * coalesced (we hope); otherwise, even at order 4,
  1248. * heavy filesystem activity makes these fail, and we can
  1249. * use compound pages.
  1250. */
  1251. gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
  1252. egrcnt = rcd->rcvegrcnt;
  1253. egroff = rcd->rcvegr_tid_base;
  1254. egrsize = dd->rcvegrbufsize;
  1255. chunk = rcd->rcvegrbuf_chunks;
  1256. egrperchunk = rcd->rcvegrbufs_perchunk;
  1257. size = rcd->rcvegrbuf_size;
  1258. if (!rcd->rcvegrbuf) {
  1259. rcd->rcvegrbuf =
  1260. kzalloc(chunk * sizeof(rcd->rcvegrbuf[0]),
  1261. GFP_KERNEL);
  1262. if (!rcd->rcvegrbuf)
  1263. goto bail;
  1264. }
  1265. if (!rcd->rcvegrbuf_phys) {
  1266. rcd->rcvegrbuf_phys =
  1267. kmalloc(chunk * sizeof(rcd->rcvegrbuf_phys[0]),
  1268. GFP_KERNEL);
  1269. if (!rcd->rcvegrbuf_phys)
  1270. goto bail_rcvegrbuf;
  1271. }
  1272. for (e = 0; e < rcd->rcvegrbuf_chunks; e++) {
  1273. if (rcd->rcvegrbuf[e])
  1274. continue;
  1275. rcd->rcvegrbuf[e] =
  1276. dma_alloc_coherent(&dd->pcidev->dev, size,
  1277. &rcd->rcvegrbuf_phys[e],
  1278. gfp_flags);
  1279. if (!rcd->rcvegrbuf[e])
  1280. goto bail_rcvegrbuf_phys;
  1281. }
  1282. rcd->rcvegr_phys = rcd->rcvegrbuf_phys[0];
  1283. for (e = chunk = 0; chunk < rcd->rcvegrbuf_chunks; chunk++) {
  1284. dma_addr_t pa = rcd->rcvegrbuf_phys[chunk];
  1285. unsigned i;
  1286. /* clear for security and sanity on each use */
  1287. memset(rcd->rcvegrbuf[chunk], 0, size);
  1288. for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
  1289. dd->f_put_tid(dd, e + egroff +
  1290. (u64 __iomem *)
  1291. ((char __iomem *)
  1292. dd->kregbase +
  1293. dd->rcvegrbase),
  1294. RCVHQ_RCV_TYPE_EAGER, pa);
  1295. pa += egrsize;
  1296. }
  1297. cond_resched(); /* don't hog the cpu */
  1298. }
  1299. return 0;
  1300. bail_rcvegrbuf_phys:
  1301. for (e = 0; e < rcd->rcvegrbuf_chunks && rcd->rcvegrbuf[e]; e++)
  1302. dma_free_coherent(&dd->pcidev->dev, size,
  1303. rcd->rcvegrbuf[e], rcd->rcvegrbuf_phys[e]);
  1304. kfree(rcd->rcvegrbuf_phys);
  1305. rcd->rcvegrbuf_phys = NULL;
  1306. bail_rcvegrbuf:
  1307. kfree(rcd->rcvegrbuf);
  1308. rcd->rcvegrbuf = NULL;
  1309. bail:
  1310. return -ENOMEM;
  1311. }
  1312. /*
  1313. * Note: Changes to this routine should be mirrored
  1314. * for the diagnostics routine qib_remap_ioaddr32().
  1315. * There is also related code for VL15 buffers in qib_init_7322_variables().
  1316. * The teardown code that unmaps is in qib_pcie_ddcleanup()
  1317. */
  1318. int init_chip_wc_pat(struct qib_devdata *dd, u32 vl15buflen)
  1319. {
  1320. u64 __iomem *qib_kregbase = NULL;
  1321. void __iomem *qib_piobase = NULL;
  1322. u64 __iomem *qib_userbase = NULL;
  1323. u64 qib_kreglen;
  1324. u64 qib_pio2koffset = dd->piobufbase & 0xffffffff;
  1325. u64 qib_pio4koffset = dd->piobufbase >> 32;
  1326. u64 qib_pio2klen = dd->piobcnt2k * dd->palign;
  1327. u64 qib_pio4klen = dd->piobcnt4k * dd->align4k;
  1328. u64 qib_physaddr = dd->physaddr;
  1329. u64 qib_piolen;
  1330. u64 qib_userlen = 0;
  1331. /*
  1332. * Free the old mapping because the kernel will try to reuse the
  1333. * old mapping and not create a new mapping with the
  1334. * write combining attribute.
  1335. */
  1336. iounmap(dd->kregbase);
  1337. dd->kregbase = NULL;
  1338. /*
  1339. * Assumes chip address space looks like:
  1340. * - kregs + sregs + cregs + uregs (in any order)
  1341. * - piobufs (2K and 4K bufs in either order)
  1342. * or:
  1343. * - kregs + sregs + cregs (in any order)
  1344. * - piobufs (2K and 4K bufs in either order)
  1345. * - uregs
  1346. */
  1347. if (dd->piobcnt4k == 0) {
  1348. qib_kreglen = qib_pio2koffset;
  1349. qib_piolen = qib_pio2klen;
  1350. } else if (qib_pio2koffset < qib_pio4koffset) {
  1351. qib_kreglen = qib_pio2koffset;
  1352. qib_piolen = qib_pio4koffset + qib_pio4klen - qib_kreglen;
  1353. } else {
  1354. qib_kreglen = qib_pio4koffset;
  1355. qib_piolen = qib_pio2koffset + qib_pio2klen - qib_kreglen;
  1356. }
  1357. qib_piolen += vl15buflen;
  1358. /* Map just the configured ports (not all hw ports) */
  1359. if (dd->uregbase > qib_kreglen)
  1360. qib_userlen = dd->ureg_align * dd->cfgctxts;
  1361. /* Sanity checks passed, now create the new mappings */
  1362. qib_kregbase = ioremap_nocache(qib_physaddr, qib_kreglen);
  1363. if (!qib_kregbase)
  1364. goto bail;
  1365. qib_piobase = ioremap_wc(qib_physaddr + qib_kreglen, qib_piolen);
  1366. if (!qib_piobase)
  1367. goto bail_kregbase;
  1368. if (qib_userlen) {
  1369. qib_userbase = ioremap_nocache(qib_physaddr + dd->uregbase,
  1370. qib_userlen);
  1371. if (!qib_userbase)
  1372. goto bail_piobase;
  1373. }
  1374. dd->kregbase = qib_kregbase;
  1375. dd->kregend = (u64 __iomem *)
  1376. ((char __iomem *) qib_kregbase + qib_kreglen);
  1377. dd->piobase = qib_piobase;
  1378. dd->pio2kbase = (void __iomem *)
  1379. (((char __iomem *) dd->piobase) +
  1380. qib_pio2koffset - qib_kreglen);
  1381. if (dd->piobcnt4k)
  1382. dd->pio4kbase = (void __iomem *)
  1383. (((char __iomem *) dd->piobase) +
  1384. qib_pio4koffset - qib_kreglen);
  1385. if (qib_userlen)
  1386. /* ureg will now be accessed relative to dd->userbase */
  1387. dd->userbase = qib_userbase;
  1388. return 0;
  1389. bail_piobase:
  1390. iounmap(qib_piobase);
  1391. bail_kregbase:
  1392. iounmap(qib_kregbase);
  1393. bail:
  1394. return -ENOMEM;
  1395. }