qib_init.c 41 KB

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