qib_init.c 47 KB

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