qib_init.c 41 KB

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