ipath_init_chip.c 31 KB

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