qib_init.c 47 KB

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