qib_init.c 47 KB

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