qib_init.c 47 KB

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