qib_init.c 41 KB

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