ipath_init_chip.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/pci.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/vmalloc.h>
  35. #include "ipath_kernel.h"
  36. #include "ips_common.h"
  37. /*
  38. * min buffers we want to have per port, after driver
  39. */
  40. #define IPATH_MIN_USER_PORT_BUFCNT 8
  41. /*
  42. * Number of ports we are configured to use (to allow for more pio
  43. * buffers per port, etc.) Zero means use chip value.
  44. */
  45. static ushort ipath_cfgports;
  46. module_param_named(cfgports, ipath_cfgports, ushort, S_IRUGO);
  47. MODULE_PARM_DESC(cfgports, "Set max number of ports to use");
  48. /*
  49. * Number of buffers reserved for driver (layered drivers and SMA
  50. * send). Reserved at end of buffer list. Initialized based on
  51. * number of PIO buffers if not set via module interface.
  52. * The problem with this is that it's global, but we'll use different
  53. * numbers for different chip types. So the default value is not
  54. * very useful. I've redefined it for the 1.3 release so that it's
  55. * zero unless set by the user to something else, in which case we
  56. * try to respect it.
  57. */
  58. static ushort ipath_kpiobufs;
  59. static int ipath_set_kpiobufs(const char *val, struct kernel_param *kp);
  60. module_param_call(kpiobufs, ipath_set_kpiobufs, param_get_ushort,
  61. &ipath_kpiobufs, S_IWUSR | S_IRUGO);
  62. MODULE_PARM_DESC(kpiobufs, "Set number of PIO buffers for driver");
  63. /**
  64. * create_port0_egr - allocate the eager TID buffers
  65. * @dd: the infinipath device
  66. *
  67. * This code is now quite different for user and kernel, because
  68. * the kernel uses skb's, for the accelerated network performance.
  69. * This is the kernel (port0) version.
  70. *
  71. * Allocate the eager TID buffers and program them into infinipath.
  72. * We use the network layer alloc_skb() allocator to allocate the
  73. * memory, and either use the buffers as is for things like SMA
  74. * packets, or pass the buffers up to the ipath layered driver and
  75. * thence the network layer, replacing them as we do so (see
  76. * ipath_rcv_layer()).
  77. */
  78. static int create_port0_egr(struct ipath_devdata *dd)
  79. {
  80. unsigned e, egrcnt;
  81. struct sk_buff **skbs;
  82. int ret;
  83. egrcnt = dd->ipath_rcvegrcnt;
  84. skbs = vmalloc(sizeof(*dd->ipath_port0_skbs) * egrcnt);
  85. if (skbs == NULL) {
  86. ipath_dev_err(dd, "allocation error for eager TID "
  87. "skb array\n");
  88. ret = -ENOMEM;
  89. goto bail;
  90. }
  91. for (e = 0; e < egrcnt; e++) {
  92. /*
  93. * This is a bit tricky in that we allocate extra
  94. * space for 2 bytes of the 14 byte ethernet header.
  95. * These two bytes are passed in the ipath header so
  96. * the rest of the data is word aligned. We allocate
  97. * 4 bytes so that the data buffer stays word aligned.
  98. * See ipath_kreceive() for more details.
  99. */
  100. skbs[e] = ipath_alloc_skb(dd, GFP_KERNEL);
  101. if (!skbs[e]) {
  102. ipath_dev_err(dd, "SKB allocation error for "
  103. "eager TID %u\n", e);
  104. while (e != 0)
  105. dev_kfree_skb(skbs[--e]);
  106. ret = -ENOMEM;
  107. goto bail;
  108. }
  109. }
  110. /*
  111. * After loop above, so we can test non-NULL to see if ready
  112. * to use at receive, etc.
  113. */
  114. dd->ipath_port0_skbs = skbs;
  115. for (e = 0; e < egrcnt; e++) {
  116. unsigned long phys =
  117. virt_to_phys(dd->ipath_port0_skbs[e]->data);
  118. dd->ipath_f_put_tid(dd, e + (u64 __iomem *)
  119. ((char __iomem *) dd->ipath_kregbase +
  120. dd->ipath_rcvegrbase), 0, phys);
  121. }
  122. ret = 0;
  123. bail:
  124. return ret;
  125. }
  126. static int bringup_link(struct ipath_devdata *dd)
  127. {
  128. u64 val, ibc;
  129. int ret = 0;
  130. /* hold IBC in reset */
  131. dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
  132. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  133. dd->ipath_control);
  134. /*
  135. * Note that prior to try 14 or 15 of IB, the credit scaling
  136. * wasn't working, because it was swapped for writes with the
  137. * 1 bit default linkstate field
  138. */
  139. /* ignore pbc and align word */
  140. val = dd->ipath_piosize2k - 2 * sizeof(u32);
  141. /*
  142. * for ICRC, which we only send in diag test pkt mode, and we
  143. * don't need to worry about that for mtu
  144. */
  145. val += 1;
  146. /*
  147. * Set the IBC maxpktlength to the size of our pio buffers the
  148. * maxpktlength is in words. This is *not* the IB data MTU.
  149. */
  150. ibc = (val / sizeof(u32)) << INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
  151. /* in KB */
  152. ibc |= 0x5ULL << INFINIPATH_IBCC_FLOWCTRLWATERMARK_SHIFT;
  153. /*
  154. * How often flowctrl sent. More or less in usecs; balance against
  155. * watermark value, so that in theory senders always get a flow
  156. * control update in time to not let the IB link go idle.
  157. */
  158. ibc |= 0x3ULL << INFINIPATH_IBCC_FLOWCTRLPERIOD_SHIFT;
  159. /* max error tolerance */
  160. ibc |= 0xfULL << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
  161. /* use "real" buffer space for */
  162. ibc |= 4ULL << INFINIPATH_IBCC_CREDITSCALE_SHIFT;
  163. /* IB credit flow control. */
  164. ibc |= 0xfULL << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
  165. /* initially come up waiting for TS1, without sending anything. */
  166. dd->ipath_ibcctrl = ibc;
  167. /*
  168. * Want to start out with both LINKCMD and LINKINITCMD in NOP
  169. * (0 and 0). Don't put linkinitcmd in ipath_ibcctrl, want that
  170. * to stay a NOP
  171. */
  172. ibc |= INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
  173. INFINIPATH_IBCC_LINKINITCMD_SHIFT;
  174. ipath_cdbg(VERBOSE, "Writing 0x%llx to ibcctrl\n",
  175. (unsigned long long) ibc);
  176. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl, ibc);
  177. // be sure chip saw it
  178. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  179. ret = dd->ipath_f_bringup_serdes(dd);
  180. if (ret)
  181. dev_info(&dd->pcidev->dev, "Could not initialize SerDes, "
  182. "not usable\n");
  183. else {
  184. /* enable IBC */
  185. dd->ipath_control |= INFINIPATH_C_LINKENABLE;
  186. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  187. dd->ipath_control);
  188. }
  189. return ret;
  190. }
  191. static int init_chip_first(struct ipath_devdata *dd,
  192. struct ipath_portdata **pdp)
  193. {
  194. struct ipath_portdata *pd = NULL;
  195. int ret = 0;
  196. u64 val;
  197. /*
  198. * skip cfgports stuff because we are not allocating memory,
  199. * and we don't want problems if the portcnt changed due to
  200. * cfgports. We do still check and report a difference, if
  201. * not same (should be impossible).
  202. */
  203. dd->ipath_portcnt =
  204. ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
  205. if (!ipath_cfgports)
  206. dd->ipath_cfgports = dd->ipath_portcnt;
  207. else if (ipath_cfgports <= dd->ipath_portcnt) {
  208. dd->ipath_cfgports = ipath_cfgports;
  209. ipath_dbg("Configured to use %u ports out of %u in chip\n",
  210. dd->ipath_cfgports, dd->ipath_portcnt);
  211. } else {
  212. dd->ipath_cfgports = dd->ipath_portcnt;
  213. ipath_dbg("Tried to configured to use %u ports; chip "
  214. "only supports %u\n", ipath_cfgports,
  215. dd->ipath_portcnt);
  216. }
  217. dd->ipath_pd = kzalloc(sizeof(*dd->ipath_pd) * dd->ipath_cfgports,
  218. GFP_KERNEL);
  219. if (!dd->ipath_pd) {
  220. ipath_dev_err(dd, "Unable to allocate portdata array, "
  221. "failing\n");
  222. ret = -ENOMEM;
  223. goto done;
  224. }
  225. dd->ipath_lastegrheads = kzalloc(sizeof(*dd->ipath_lastegrheads)
  226. * dd->ipath_cfgports,
  227. GFP_KERNEL);
  228. dd->ipath_lastrcvhdrqtails =
  229. kzalloc(sizeof(*dd->ipath_lastrcvhdrqtails)
  230. * dd->ipath_cfgports, GFP_KERNEL);
  231. if (!dd->ipath_lastegrheads || !dd->ipath_lastrcvhdrqtails) {
  232. ipath_dev_err(dd, "Unable to allocate head arrays, "
  233. "failing\n");
  234. ret = -ENOMEM;
  235. goto done;
  236. }
  237. dd->ipath_pd[0] = kzalloc(sizeof(*pd), GFP_KERNEL);
  238. if (!dd->ipath_pd[0]) {
  239. ipath_dev_err(dd, "Unable to allocate portdata for port "
  240. "0, failing\n");
  241. ret = -ENOMEM;
  242. goto done;
  243. }
  244. pd = dd->ipath_pd[0];
  245. pd->port_dd = dd;
  246. pd->port_port = 0;
  247. pd->port_cnt = 1;
  248. /* The port 0 pkey table is used by the layer interface. */
  249. pd->port_pkeys[0] = IPS_DEFAULT_P_KEY;
  250. dd->ipath_rcvtidcnt =
  251. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidcnt);
  252. dd->ipath_rcvtidbase =
  253. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidbase);
  254. dd->ipath_rcvegrcnt =
  255. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
  256. dd->ipath_rcvegrbase =
  257. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrbase);
  258. dd->ipath_palign =
  259. ipath_read_kreg32(dd, dd->ipath_kregs->kr_pagealign);
  260. dd->ipath_piobufbase =
  261. ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiobufbase);
  262. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiosize);
  263. dd->ipath_piosize2k = val & ~0U;
  264. dd->ipath_piosize4k = val >> 32;
  265. dd->ipath_ibmtu = 4096; /* default to largest legal MTU */
  266. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiobufcnt);
  267. dd->ipath_piobcnt2k = val & ~0U;
  268. dd->ipath_piobcnt4k = val >> 32;
  269. dd->ipath_pio2kbase =
  270. (u32 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
  271. (dd->ipath_piobufbase & 0xffffffff));
  272. if (dd->ipath_piobcnt4k) {
  273. dd->ipath_pio4kbase = (u32 __iomem *)
  274. (((char __iomem *) dd->ipath_kregbase) +
  275. (dd->ipath_piobufbase >> 32));
  276. /*
  277. * 4K buffers take 2 pages; we use roundup just to be
  278. * paranoid; we calculate it once here, rather than on
  279. * ever buf allocate
  280. */
  281. dd->ipath_4kalign = ALIGN(dd->ipath_piosize4k,
  282. dd->ipath_palign);
  283. ipath_dbg("%u 2k(%x) piobufs @ %p, %u 4k(%x) @ %p "
  284. "(%x aligned)\n",
  285. dd->ipath_piobcnt2k, dd->ipath_piosize2k,
  286. dd->ipath_pio2kbase, dd->ipath_piobcnt4k,
  287. dd->ipath_piosize4k, dd->ipath_pio4kbase,
  288. dd->ipath_4kalign);
  289. }
  290. else ipath_dbg("%u 2k piobufs @ %p\n",
  291. dd->ipath_piobcnt2k, dd->ipath_pio2kbase);
  292. spin_lock_init(&dd->ipath_tid_lock);
  293. done:
  294. *pdp = pd;
  295. return ret;
  296. }
  297. /**
  298. * init_chip_reset - re-initialize after a reset, or enable
  299. * @dd: the infinipath device
  300. * @pdp: output for port data
  301. *
  302. * sanity check at least some of the values after reset, and
  303. * ensure no receive or transmit (explictly, in case reset
  304. * failed
  305. */
  306. static int init_chip_reset(struct ipath_devdata *dd,
  307. struct ipath_portdata **pdp)
  308. {
  309. struct ipath_portdata *pd;
  310. u32 rtmp;
  311. *pdp = pd = dd->ipath_pd[0];
  312. /* ensure chip does no sends or receives while we re-initialize */
  313. dd->ipath_control = dd->ipath_sendctrl = dd->ipath_rcvctrl = 0U;
  314. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 0);
  315. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0);
  316. ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0);
  317. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
  318. if (dd->ipath_portcnt != rtmp)
  319. dev_info(&dd->pcidev->dev, "portcnt was %u before "
  320. "reset, now %u, using original\n",
  321. dd->ipath_portcnt, rtmp);
  322. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidcnt);
  323. if (rtmp != dd->ipath_rcvtidcnt)
  324. dev_info(&dd->pcidev->dev, "tidcnt was %u before "
  325. "reset, now %u, using original\n",
  326. dd->ipath_rcvtidcnt, rtmp);
  327. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidbase);
  328. if (rtmp != dd->ipath_rcvtidbase)
  329. dev_info(&dd->pcidev->dev, "tidbase was %u before "
  330. "reset, now %u, using original\n",
  331. dd->ipath_rcvtidbase, rtmp);
  332. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
  333. if (rtmp != dd->ipath_rcvegrcnt)
  334. dev_info(&dd->pcidev->dev, "egrcnt was %u before "
  335. "reset, now %u, using original\n",
  336. dd->ipath_rcvegrcnt, rtmp);
  337. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrbase);
  338. if (rtmp != dd->ipath_rcvegrbase)
  339. dev_info(&dd->pcidev->dev, "egrbase was %u before "
  340. "reset, now %u, using original\n",
  341. dd->ipath_rcvegrbase, rtmp);
  342. return 0;
  343. }
  344. static int init_pioavailregs(struct ipath_devdata *dd)
  345. {
  346. int ret;
  347. dd->ipath_pioavailregs_dma = dma_alloc_coherent(
  348. &dd->pcidev->dev, PAGE_SIZE, &dd->ipath_pioavailregs_phys,
  349. GFP_KERNEL);
  350. if (!dd->ipath_pioavailregs_dma) {
  351. ipath_dev_err(dd, "failed to allocate PIOavail reg area "
  352. "in memory\n");
  353. ret = -ENOMEM;
  354. goto done;
  355. }
  356. /*
  357. * we really want L2 cache aligned, but for current CPUs of
  358. * interest, they are the same.
  359. */
  360. dd->ipath_statusp = (u64 *)
  361. ((char *)dd->ipath_pioavailregs_dma +
  362. ((2 * L1_CACHE_BYTES +
  363. dd->ipath_pioavregs * sizeof(u64)) & ~L1_CACHE_BYTES));
  364. /* copy the current value now that it's really allocated */
  365. *dd->ipath_statusp = dd->_ipath_status;
  366. /*
  367. * setup buffer to hold freeze msg, accessible to apps,
  368. * following statusp
  369. */
  370. dd->ipath_freezemsg = (char *)&dd->ipath_statusp[1];
  371. /* and its length */
  372. dd->ipath_freezelen = L1_CACHE_BYTES - sizeof(dd->ipath_statusp[0]);
  373. if (dd->ipath_unit * 64 > (IPATH_PORT0_RCVHDRTAIL_SIZE - 64)) {
  374. ipath_dev_err(dd, "unit %u too large for port 0 "
  375. "rcvhdrtail buffer size\n", dd->ipath_unit);
  376. ret = -ENODEV;
  377. }
  378. else
  379. ret = 0;
  380. /* so we can get current tail in ipath_kreceive(), per chip */
  381. dd->ipath_hdrqtailptr = &ipath_port0_rcvhdrtail[
  382. dd->ipath_unit * (64 / sizeof(*ipath_port0_rcvhdrtail))];
  383. done:
  384. return ret;
  385. }
  386. /**
  387. * init_shadow_tids - allocate the shadow TID array
  388. * @dd: the infinipath device
  389. *
  390. * allocate the shadow TID array, so we can ipath_munlock previous
  391. * entries. It may make more sense to move the pageshadow to the
  392. * port data structure, so we only allocate memory for ports actually
  393. * in use, since we at 8k per port, now.
  394. */
  395. static void init_shadow_tids(struct ipath_devdata *dd)
  396. {
  397. dd->ipath_pageshadow = (struct page **)
  398. vmalloc(dd->ipath_cfgports * dd->ipath_rcvtidcnt *
  399. sizeof(struct page *));
  400. if (!dd->ipath_pageshadow)
  401. ipath_dev_err(dd, "failed to allocate shadow page * "
  402. "array, no expected sends!\n");
  403. else
  404. memset(dd->ipath_pageshadow, 0,
  405. dd->ipath_cfgports * dd->ipath_rcvtidcnt *
  406. sizeof(struct page *));
  407. }
  408. static void enable_chip(struct ipath_devdata *dd,
  409. struct ipath_portdata *pd, int reinit)
  410. {
  411. u32 val;
  412. int i;
  413. if (!reinit) {
  414. init_waitqueue_head(&ipath_sma_state_wait);
  415. }
  416. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  417. dd->ipath_rcvctrl);
  418. /* Enable PIO send, and update of PIOavail regs to memory. */
  419. dd->ipath_sendctrl = INFINIPATH_S_PIOENABLE |
  420. INFINIPATH_S_PIOBUFAVAILUPD;
  421. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  422. dd->ipath_sendctrl);
  423. /*
  424. * enable port 0 receive, and receive interrupt. other ports
  425. * done as user opens and inits them.
  426. */
  427. dd->ipath_rcvctrl = INFINIPATH_R_TAILUPD |
  428. (1ULL << INFINIPATH_R_PORTENABLE_SHIFT) |
  429. (1ULL << INFINIPATH_R_INTRAVAIL_SHIFT);
  430. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  431. dd->ipath_rcvctrl);
  432. /*
  433. * now ready for use. this should be cleared whenever we
  434. * detect a reset, or initiate one.
  435. */
  436. dd->ipath_flags |= IPATH_INITTED;
  437. /*
  438. * init our shadow copies of head from tail values, and write
  439. * head values to match.
  440. */
  441. val = ipath_read_ureg32(dd, ur_rcvegrindextail, 0);
  442. (void)ipath_write_ureg(dd, ur_rcvegrindexhead, val, 0);
  443. dd->ipath_port0head = ipath_read_ureg32(dd, ur_rcvhdrtail, 0);
  444. /* Initialize so we interrupt on next packet received */
  445. (void)ipath_write_ureg(dd, ur_rcvhdrhead,
  446. dd->ipath_rhdrhead_intr_off |
  447. dd->ipath_port0head, 0);
  448. /*
  449. * by now pioavail updates to memory should have occurred, so
  450. * copy them into our working/shadow registers; this is in
  451. * case something went wrong with abort, but mostly to get the
  452. * initial values of the generation bit correct.
  453. */
  454. for (i = 0; i < dd->ipath_pioavregs; i++) {
  455. __le64 val;
  456. /*
  457. * Chip Errata bug 6641; even and odd qwords>3 are swapped.
  458. */
  459. if (i > 3) {
  460. if (i & 1)
  461. val = dd->ipath_pioavailregs_dma[i - 1];
  462. else
  463. val = dd->ipath_pioavailregs_dma[i + 1];
  464. }
  465. else
  466. val = dd->ipath_pioavailregs_dma[i];
  467. dd->ipath_pioavailshadow[i] = le64_to_cpu(val);
  468. }
  469. /* can get counters, stats, etc. */
  470. dd->ipath_flags |= IPATH_PRESENT;
  471. }
  472. static int init_housekeeping(struct ipath_devdata *dd,
  473. struct ipath_portdata **pdp, int reinit)
  474. {
  475. char boardn[32];
  476. int ret = 0;
  477. /*
  478. * have to clear shadow copies of registers at init that are
  479. * not otherwise set here, or all kinds of bizarre things
  480. * happen with driver on chip reset
  481. */
  482. dd->ipath_rcvhdrsize = 0;
  483. /*
  484. * Don't clear ipath_flags as 8bit mode was set before
  485. * entering this func. However, we do set the linkstate to
  486. * unknown, so we can watch for a transition.
  487. * PRESENT is set because we want register reads to work,
  488. * and the kernel infrastructure saw it in config space;
  489. * We clear it if we have failures.
  490. */
  491. dd->ipath_flags |= IPATH_LINKUNK | IPATH_PRESENT;
  492. dd->ipath_flags &= ~(IPATH_LINKACTIVE | IPATH_LINKARMED |
  493. IPATH_LINKDOWN | IPATH_LINKINIT);
  494. ipath_cdbg(VERBOSE, "Try to read spc chip revision\n");
  495. dd->ipath_revision =
  496. ipath_read_kreg64(dd, dd->ipath_kregs->kr_revision);
  497. /*
  498. * set up fundamental info we need to use the chip; we assume
  499. * if the revision reg and these regs are OK, we don't need to
  500. * special case the rest
  501. */
  502. dd->ipath_sregbase =
  503. ipath_read_kreg32(dd, dd->ipath_kregs->kr_sendregbase);
  504. dd->ipath_cregbase =
  505. ipath_read_kreg32(dd, dd->ipath_kregs->kr_counterregbase);
  506. dd->ipath_uregbase =
  507. ipath_read_kreg32(dd, dd->ipath_kregs->kr_userregbase);
  508. ipath_cdbg(VERBOSE, "ipath_kregbase %p, sendbase %x usrbase %x, "
  509. "cntrbase %x\n", dd->ipath_kregbase, dd->ipath_sregbase,
  510. dd->ipath_uregbase, dd->ipath_cregbase);
  511. if ((dd->ipath_revision & 0xffffffff) == 0xffffffff
  512. || (dd->ipath_sregbase & 0xffffffff) == 0xffffffff
  513. || (dd->ipath_cregbase & 0xffffffff) == 0xffffffff
  514. || (dd->ipath_uregbase & 0xffffffff) == 0xffffffff) {
  515. ipath_dev_err(dd, "Register read failures from chip, "
  516. "giving up initialization\n");
  517. dd->ipath_flags &= ~IPATH_PRESENT;
  518. ret = -ENODEV;
  519. goto done;
  520. }
  521. /* clear the initial reset flag, in case first driver load */
  522. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear,
  523. INFINIPATH_E_RESET);
  524. if (reinit)
  525. ret = init_chip_reset(dd, pdp);
  526. else
  527. ret = init_chip_first(dd, pdp);
  528. if (ret)
  529. goto done;
  530. ipath_cdbg(VERBOSE, "Revision %llx (PCI %x), %u ports, %u tids, "
  531. "%u egrtids\n", (unsigned long long) dd->ipath_revision,
  532. dd->ipath_pcirev, dd->ipath_portcnt, dd->ipath_rcvtidcnt,
  533. dd->ipath_rcvegrcnt);
  534. if (((dd->ipath_revision >> INFINIPATH_R_SOFTWARE_SHIFT) &
  535. INFINIPATH_R_SOFTWARE_MASK) != IPATH_CHIP_SWVERSION) {
  536. ipath_dev_err(dd, "Driver only handles version %d, "
  537. "chip swversion is %d (%llx), failng\n",
  538. IPATH_CHIP_SWVERSION,
  539. (int)(dd->ipath_revision >>
  540. INFINIPATH_R_SOFTWARE_SHIFT) &
  541. INFINIPATH_R_SOFTWARE_MASK,
  542. (unsigned long long) dd->ipath_revision);
  543. ret = -ENOSYS;
  544. goto done;
  545. }
  546. dd->ipath_majrev = (u8) ((dd->ipath_revision >>
  547. INFINIPATH_R_CHIPREVMAJOR_SHIFT) &
  548. INFINIPATH_R_CHIPREVMAJOR_MASK);
  549. dd->ipath_minrev = (u8) ((dd->ipath_revision >>
  550. INFINIPATH_R_CHIPREVMINOR_SHIFT) &
  551. INFINIPATH_R_CHIPREVMINOR_MASK);
  552. dd->ipath_boardrev = (u8) ((dd->ipath_revision >>
  553. INFINIPATH_R_BOARDID_SHIFT) &
  554. INFINIPATH_R_BOARDID_MASK);
  555. ret = dd->ipath_f_get_boardname(dd, boardn, sizeof boardn);
  556. snprintf(dd->ipath_boardversion, sizeof(dd->ipath_boardversion),
  557. "Driver %u.%u, %s, InfiniPath%u %u.%u, PCI %u, "
  558. "SW Compat %u\n",
  559. IPATH_CHIP_VERS_MAJ, IPATH_CHIP_VERS_MIN, boardn,
  560. (unsigned)(dd->ipath_revision >> INFINIPATH_R_ARCH_SHIFT) &
  561. INFINIPATH_R_ARCH_MASK,
  562. dd->ipath_majrev, dd->ipath_minrev, dd->ipath_pcirev,
  563. (unsigned)(dd->ipath_revision >>
  564. INFINIPATH_R_SOFTWARE_SHIFT) &
  565. INFINIPATH_R_SOFTWARE_MASK);
  566. ipath_dbg("%s", dd->ipath_boardversion);
  567. done:
  568. return ret;
  569. }
  570. /**
  571. * ipath_init_chip - do the actual initialization sequence on the chip
  572. * @dd: the infinipath device
  573. * @reinit: reinitializing, so don't allocate new memory
  574. *
  575. * Do the actual initialization sequence on the chip. This is done
  576. * both from the init routine called from the PCI infrastructure, and
  577. * when we reset the chip, or detect that it was reset internally,
  578. * or it's administratively re-enabled.
  579. *
  580. * Memory allocation here and in called routines is only done in
  581. * the first case (reinit == 0). We have to be careful, because even
  582. * without memory allocation, we need to re-write all the chip registers
  583. * TIDs, etc. after the reset or enable has completed.
  584. */
  585. int ipath_init_chip(struct ipath_devdata *dd, int reinit)
  586. {
  587. int ret = 0, i;
  588. u32 val32, kpiobufs;
  589. u64 val, atmp;
  590. struct ipath_portdata *pd = NULL; /* keep gcc4 happy */
  591. ret = init_housekeeping(dd, &pd, reinit);
  592. if (ret)
  593. goto done;
  594. /*
  595. * we ignore most issues after reporting them, but have to specially
  596. * handle hardware-disabled chips.
  597. */
  598. if (ret == 2) {
  599. /* unique error, known to ipath_init_one */
  600. ret = -EPERM;
  601. goto done;
  602. }
  603. /*
  604. * We could bump this to allow for full rcvegrcnt + rcvtidcnt,
  605. * but then it no longer nicely fits power of two, and since
  606. * we now use routines that backend onto __get_free_pages, the
  607. * rest would be wasted.
  608. */
  609. dd->ipath_rcvhdrcnt = dd->ipath_rcvegrcnt;
  610. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrcnt,
  611. dd->ipath_rcvhdrcnt);
  612. /*
  613. * Set up the shadow copies of the piobufavail registers,
  614. * which we compare against the chip registers for now, and
  615. * the in memory DMA'ed copies of the registers. This has to
  616. * be done early, before we calculate lastport, etc.
  617. */
  618. val = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k;
  619. /*
  620. * calc number of pioavail registers, and save it; we have 2
  621. * bits per buffer.
  622. */
  623. dd->ipath_pioavregs = ALIGN(val, sizeof(u64) * BITS_PER_BYTE / 2)
  624. / (sizeof(u64) * BITS_PER_BYTE / 2);
  625. if (ipath_kpiobufs == 0) {
  626. /* not set by user, or set explictly to default */
  627. if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) > 128)
  628. kpiobufs = 32;
  629. else
  630. kpiobufs = 16;
  631. }
  632. else
  633. kpiobufs = ipath_kpiobufs;
  634. if (kpiobufs >
  635. (dd->ipath_piobcnt2k + dd->ipath_piobcnt4k -
  636. (dd->ipath_cfgports * IPATH_MIN_USER_PORT_BUFCNT))) {
  637. i = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k -
  638. (dd->ipath_cfgports * IPATH_MIN_USER_PORT_BUFCNT);
  639. if (i < 0)
  640. i = 0;
  641. dev_info(&dd->pcidev->dev, "Allocating %d PIO bufs for "
  642. "kernel leaves too few for %d user ports "
  643. "(%d each); using %u\n", kpiobufs,
  644. dd->ipath_cfgports - 1,
  645. IPATH_MIN_USER_PORT_BUFCNT, i);
  646. /*
  647. * shouldn't change ipath_kpiobufs, because could be
  648. * different for different devices...
  649. */
  650. kpiobufs = i;
  651. }
  652. dd->ipath_lastport_piobuf =
  653. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k - kpiobufs;
  654. dd->ipath_pbufsport = dd->ipath_cfgports > 1
  655. ? dd->ipath_lastport_piobuf / (dd->ipath_cfgports - 1)
  656. : 0;
  657. val32 = dd->ipath_lastport_piobuf -
  658. (dd->ipath_pbufsport * (dd->ipath_cfgports - 1));
  659. if (val32 > 0) {
  660. ipath_dbg("allocating %u pbufs/port leaves %u unused, "
  661. "add to kernel\n", dd->ipath_pbufsport, val32);
  662. dd->ipath_lastport_piobuf -= val32;
  663. ipath_dbg("%u pbufs/port leaves %u unused, add to kernel\n",
  664. dd->ipath_pbufsport, val32);
  665. }
  666. dd->ipath_lastpioindex = dd->ipath_lastport_piobuf;
  667. ipath_cdbg(VERBOSE, "%d PIO bufs for kernel out of %d total %u "
  668. "each for %u user ports\n", kpiobufs,
  669. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k,
  670. dd->ipath_pbufsport, dd->ipath_cfgports - 1);
  671. dd->ipath_f_early_init(dd);
  672. /* early_init sets rcvhdrentsize and rcvhdrsize, so this must be
  673. * done after early_init */
  674. dd->ipath_hdrqlast =
  675. dd->ipath_rcvhdrentsize * (dd->ipath_rcvhdrcnt - 1);
  676. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrentsize,
  677. dd->ipath_rcvhdrentsize);
  678. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
  679. dd->ipath_rcvhdrsize);
  680. if (!reinit) {
  681. ret = init_pioavailregs(dd);
  682. init_shadow_tids(dd);
  683. if (ret)
  684. goto done;
  685. }
  686. (void)ipath_write_kreg(dd, dd->ipath_kregs->kr_sendpioavailaddr,
  687. dd->ipath_pioavailregs_phys);
  688. /*
  689. * this is to detect s/w errors, which the h/w works around by
  690. * ignoring the low 6 bits of address, if it wasn't aligned.
  691. */
  692. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpioavailaddr);
  693. if (val != dd->ipath_pioavailregs_phys) {
  694. ipath_dev_err(dd, "Catastrophic software error, "
  695. "SendPIOAvailAddr written as %lx, "
  696. "read back as %llx\n",
  697. (unsigned long) dd->ipath_pioavailregs_phys,
  698. (unsigned long long) val);
  699. ret = -EINVAL;
  700. goto done;
  701. }
  702. val = ipath_port0_rcvhdrtail_dma + dd->ipath_unit * 64;
  703. /* verify that the alignment requirement was met */
  704. ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
  705. 0, val);
  706. atmp = ipath_read_kreg64_port(
  707. dd, dd->ipath_kregs->kr_rcvhdrtailaddr, 0);
  708. if (val != atmp) {
  709. ipath_dev_err(dd, "Catastrophic software error, "
  710. "RcvHdrTailAddr0 written as %llx, "
  711. "read back as %llx from %x\n",
  712. (unsigned long long) val,
  713. (unsigned long long) atmp,
  714. dd->ipath_kregs->kr_rcvhdrtailaddr);
  715. ret = -EINVAL;
  716. goto done;
  717. }
  718. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvbthqp, IPATH_KD_QP);
  719. /*
  720. * make sure we are not in freeze, and PIO send enabled, so
  721. * writes to pbc happen
  722. */
  723. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrmask, 0ULL);
  724. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
  725. ~0ULL&~INFINIPATH_HWE_MEMBISTFAILED);
  726. ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0ULL);
  727. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  728. INFINIPATH_S_PIOENABLE);
  729. /*
  730. * before error clears, since we expect serdes pll errors during
  731. * this, the first time after reset
  732. */
  733. if (bringup_link(dd)) {
  734. dev_info(&dd->pcidev->dev, "Failed to bringup IB link\n");
  735. ret = -ENETDOWN;
  736. goto done;
  737. }
  738. /*
  739. * clear any "expected" hwerrs from reset and/or initialization
  740. * clear any that aren't enabled (at least this once), and then
  741. * set the enable mask
  742. */
  743. dd->ipath_f_init_hwerrors(dd);
  744. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
  745. ~0ULL&~INFINIPATH_HWE_MEMBISTFAILED);
  746. ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrmask,
  747. dd->ipath_hwerrmask);
  748. dd->ipath_maskederrs = dd->ipath_ignorederrs;
  749. /* clear all */
  750. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
  751. /* enable errors that are masked, at least this first time. */
  752. ipath_write_kreg(dd, dd->ipath_kregs->kr_errormask,
  753. ~dd->ipath_maskederrs);
  754. /* clear any interrups up to this point (ints still not enabled) */
  755. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
  756. ipath_stats.sps_lid[dd->ipath_unit] = dd->ipath_lid;
  757. /*
  758. * Set up the port 0 (kernel) rcvhdr q and egr TIDs. If doing
  759. * re-init, the simplest way to handle this is to free
  760. * existing, and re-allocate.
  761. */
  762. if (reinit)
  763. ipath_free_pddata(dd, 0, 0);
  764. dd->ipath_f_tidtemplate(dd);
  765. ret = ipath_create_rcvhdrq(dd, pd);
  766. if (!ret)
  767. ret = create_port0_egr(dd);
  768. if (ret)
  769. ipath_dev_err(dd, "failed to allocate port 0 (kernel) "
  770. "rcvhdrq and/or egr bufs\n");
  771. else
  772. enable_chip(dd, pd, reinit);
  773. /*
  774. * cause retrigger of pending interrupts ignored during init,
  775. * even if we had errors
  776. */
  777. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, 0ULL);
  778. if(!dd->ipath_stats_timer_active) {
  779. /*
  780. * first init, or after an admin disable/enable
  781. * set up stats retrieval timer, even if we had errors
  782. * in last portion of setup
  783. */
  784. init_timer(&dd->ipath_stats_timer);
  785. dd->ipath_stats_timer.function = ipath_get_faststats;
  786. dd->ipath_stats_timer.data = (unsigned long) dd;
  787. /* every 5 seconds; */
  788. dd->ipath_stats_timer.expires = jiffies + 5 * HZ;
  789. /* takes ~16 seconds to overflow at full IB 4x bandwdith */
  790. add_timer(&dd->ipath_stats_timer);
  791. dd->ipath_stats_timer_active = 1;
  792. }
  793. done:
  794. if (!ret) {
  795. ipath_get_guid(dd);
  796. *dd->ipath_statusp |= IPATH_STATUS_CHIP_PRESENT;
  797. if (!dd->ipath_f_intrsetup(dd)) {
  798. /* now we can enable all interrupts from the chip */
  799. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask,
  800. -1LL);
  801. /* force re-interrupt of any pending interrupts. */
  802. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear,
  803. 0ULL);
  804. /* chip is usable; mark it as initialized */
  805. *dd->ipath_statusp |= IPATH_STATUS_INITTED;
  806. } else
  807. ipath_dev_err(dd, "No interrupts enabled, couldn't "
  808. "setup interrupt address\n");
  809. if (dd->ipath_cfgports > ipath_stats.sps_nports)
  810. /*
  811. * sps_nports is a global, so, we set it to
  812. * the highest number of ports of any of the
  813. * chips we find; we never decrement it, at
  814. * least for now. Since this might have changed
  815. * over disable/enable or prior to reset, always
  816. * do the check and potentially adjust.
  817. */
  818. ipath_stats.sps_nports = dd->ipath_cfgports;
  819. } else
  820. ipath_dbg("Failed (%d) to initialize chip\n", ret);
  821. /* if ret is non-zero, we probably should do some cleanup
  822. here... */
  823. return ret;
  824. }
  825. static int ipath_set_kpiobufs(const char *str, struct kernel_param *kp)
  826. {
  827. struct ipath_devdata *dd;
  828. unsigned long flags;
  829. unsigned short val;
  830. int ret;
  831. ret = ipath_parse_ushort(str, &val);
  832. spin_lock_irqsave(&ipath_devs_lock, flags);
  833. if (ret < 0)
  834. goto bail;
  835. if (val == 0) {
  836. ret = -EINVAL;
  837. goto bail;
  838. }
  839. list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
  840. if (dd->ipath_kregbase)
  841. continue;
  842. if (val > (dd->ipath_piobcnt2k + dd->ipath_piobcnt4k -
  843. (dd->ipath_cfgports *
  844. IPATH_MIN_USER_PORT_BUFCNT)))
  845. {
  846. ipath_dev_err(
  847. dd,
  848. "Allocating %d PIO bufs for kernel leaves "
  849. "too few for %d user ports (%d each)\n",
  850. val, dd->ipath_cfgports - 1,
  851. IPATH_MIN_USER_PORT_BUFCNT);
  852. ret = -EINVAL;
  853. goto bail;
  854. }
  855. dd->ipath_lastport_piobuf =
  856. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k - val;
  857. }
  858. ret = 0;
  859. bail:
  860. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  861. return ret;
  862. }