qib_init.c 48 KB

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