ipath_init_chip.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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_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), 0,
  125. dd->ipath_port0_skbinfo[e].phys);
  126. }
  127. ret = 0;
  128. bail:
  129. return ret;
  130. }
  131. static int bringup_link(struct ipath_devdata *dd)
  132. {
  133. u64 val, ibc;
  134. int ret = 0;
  135. /* hold IBC in reset */
  136. dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
  137. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  138. dd->ipath_control);
  139. /*
  140. * Note that prior to try 14 or 15 of IB, the credit scaling
  141. * wasn't working, because it was swapped for writes with the
  142. * 1 bit default linkstate field
  143. */
  144. /* ignore pbc and align word */
  145. val = dd->ipath_piosize2k - 2 * sizeof(u32);
  146. /*
  147. * for ICRC, which we only send in diag test pkt mode, and we
  148. * don't need to worry about that for mtu
  149. */
  150. val += 1;
  151. /*
  152. * Set the IBC maxpktlength to the size of our pio buffers the
  153. * maxpktlength is in words. This is *not* the IB data MTU.
  154. */
  155. ibc = (val / sizeof(u32)) << INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
  156. /* in KB */
  157. ibc |= 0x5ULL << INFINIPATH_IBCC_FLOWCTRLWATERMARK_SHIFT;
  158. /*
  159. * How often flowctrl sent. More or less in usecs; balance against
  160. * watermark value, so that in theory senders always get a flow
  161. * control update in time to not let the IB link go idle.
  162. */
  163. ibc |= 0x3ULL << INFINIPATH_IBCC_FLOWCTRLPERIOD_SHIFT;
  164. /* max error tolerance */
  165. ibc |= 0xfULL << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
  166. /* use "real" buffer space for */
  167. ibc |= 4ULL << INFINIPATH_IBCC_CREDITSCALE_SHIFT;
  168. /* IB credit flow control. */
  169. ibc |= 0xfULL << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
  170. /* initially come up waiting for TS1, without sending anything. */
  171. dd->ipath_ibcctrl = ibc;
  172. /*
  173. * Want to start out with both LINKCMD and LINKINITCMD in NOP
  174. * (0 and 0). Don't put linkinitcmd in ipath_ibcctrl, want that
  175. * to stay a NOP
  176. */
  177. ibc |= INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
  178. INFINIPATH_IBCC_LINKINITCMD_SHIFT;
  179. ipath_cdbg(VERBOSE, "Writing 0x%llx to ibcctrl\n",
  180. (unsigned long long) ibc);
  181. ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl, ibc);
  182. // be sure chip saw it
  183. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
  184. ret = dd->ipath_f_bringup_serdes(dd);
  185. if (ret)
  186. dev_info(&dd->pcidev->dev, "Could not initialize SerDes, "
  187. "not usable\n");
  188. else {
  189. /* enable IBC */
  190. dd->ipath_control |= INFINIPATH_C_LINKENABLE;
  191. ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
  192. dd->ipath_control);
  193. }
  194. return ret;
  195. }
  196. static struct ipath_portdata *create_portdata0(struct ipath_devdata *dd)
  197. {
  198. struct ipath_portdata *pd = NULL;
  199. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  200. if (pd) {
  201. pd->port_dd = dd;
  202. pd->port_cnt = 1;
  203. /* The port 0 pkey table is used by the layer interface. */
  204. pd->port_pkeys[0] = IPATH_DEFAULT_P_KEY;
  205. }
  206. return pd;
  207. }
  208. static int init_chip_first(struct ipath_devdata *dd,
  209. struct ipath_portdata **pdp)
  210. {
  211. struct ipath_portdata *pd = NULL;
  212. int ret = 0;
  213. u64 val;
  214. /*
  215. * skip cfgports stuff because we are not allocating memory,
  216. * and we don't want problems if the portcnt changed due to
  217. * cfgports. We do still check and report a difference, if
  218. * not same (should be impossible).
  219. */
  220. dd->ipath_portcnt =
  221. ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
  222. if (!ipath_cfgports)
  223. dd->ipath_cfgports = dd->ipath_portcnt;
  224. else if (ipath_cfgports <= dd->ipath_portcnt) {
  225. dd->ipath_cfgports = ipath_cfgports;
  226. ipath_dbg("Configured to use %u ports out of %u in chip\n",
  227. dd->ipath_cfgports, dd->ipath_portcnt);
  228. } else {
  229. dd->ipath_cfgports = dd->ipath_portcnt;
  230. ipath_dbg("Tried to configured to use %u ports; chip "
  231. "only supports %u\n", ipath_cfgports,
  232. dd->ipath_portcnt);
  233. }
  234. /*
  235. * Allocate full portcnt array, rather than just cfgports, because
  236. * cleanup iterates across all possible ports.
  237. */
  238. dd->ipath_pd = kzalloc(sizeof(*dd->ipath_pd) * dd->ipath_portcnt,
  239. GFP_KERNEL);
  240. if (!dd->ipath_pd) {
  241. ipath_dev_err(dd, "Unable to allocate portdata array, "
  242. "failing\n");
  243. ret = -ENOMEM;
  244. goto done;
  245. }
  246. dd->ipath_lastegrheads = kzalloc(sizeof(*dd->ipath_lastegrheads)
  247. * dd->ipath_cfgports,
  248. GFP_KERNEL);
  249. dd->ipath_lastrcvhdrqtails =
  250. kzalloc(sizeof(*dd->ipath_lastrcvhdrqtails)
  251. * dd->ipath_cfgports, GFP_KERNEL);
  252. if (!dd->ipath_lastegrheads || !dd->ipath_lastrcvhdrqtails) {
  253. ipath_dev_err(dd, "Unable to allocate head arrays, "
  254. "failing\n");
  255. ret = -ENOMEM;
  256. goto done;
  257. }
  258. pd = create_portdata0(dd);
  259. if (!pd) {
  260. ipath_dev_err(dd, "Unable to allocate portdata for port "
  261. "0, failing\n");
  262. ret = -ENOMEM;
  263. goto done;
  264. }
  265. dd->ipath_pd[0] = pd;
  266. dd->ipath_rcvtidcnt =
  267. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidcnt);
  268. dd->ipath_rcvtidbase =
  269. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidbase);
  270. dd->ipath_rcvegrcnt =
  271. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
  272. dd->ipath_rcvegrbase =
  273. ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrbase);
  274. dd->ipath_palign =
  275. ipath_read_kreg32(dd, dd->ipath_kregs->kr_pagealign);
  276. dd->ipath_piobufbase =
  277. ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiobufbase);
  278. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiosize);
  279. dd->ipath_piosize2k = val & ~0U;
  280. dd->ipath_piosize4k = val >> 32;
  281. dd->ipath_ibmtu = 4096; /* default to largest legal MTU */
  282. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpiobufcnt);
  283. dd->ipath_piobcnt2k = val & ~0U;
  284. dd->ipath_piobcnt4k = val >> 32;
  285. dd->ipath_pio2kbase =
  286. (u32 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
  287. (dd->ipath_piobufbase & 0xffffffff));
  288. if (dd->ipath_piobcnt4k) {
  289. dd->ipath_pio4kbase = (u32 __iomem *)
  290. (((char __iomem *) dd->ipath_kregbase) +
  291. (dd->ipath_piobufbase >> 32));
  292. /*
  293. * 4K buffers take 2 pages; we use roundup just to be
  294. * paranoid; we calculate it once here, rather than on
  295. * ever buf allocate
  296. */
  297. dd->ipath_4kalign = ALIGN(dd->ipath_piosize4k,
  298. dd->ipath_palign);
  299. ipath_dbg("%u 2k(%x) piobufs @ %p, %u 4k(%x) @ %p "
  300. "(%x aligned)\n",
  301. dd->ipath_piobcnt2k, dd->ipath_piosize2k,
  302. dd->ipath_pio2kbase, dd->ipath_piobcnt4k,
  303. dd->ipath_piosize4k, dd->ipath_pio4kbase,
  304. dd->ipath_4kalign);
  305. }
  306. else ipath_dbg("%u 2k piobufs @ %p\n",
  307. dd->ipath_piobcnt2k, dd->ipath_pio2kbase);
  308. spin_lock_init(&dd->ipath_tid_lock);
  309. done:
  310. *pdp = pd;
  311. return ret;
  312. }
  313. /**
  314. * init_chip_reset - re-initialize after a reset, or enable
  315. * @dd: the infinipath device
  316. * @pdp: output for port data
  317. *
  318. * sanity check at least some of the values after reset, and
  319. * ensure no receive or transmit (explictly, in case reset
  320. * failed
  321. */
  322. static int init_chip_reset(struct ipath_devdata *dd,
  323. struct ipath_portdata **pdp)
  324. {
  325. u32 rtmp;
  326. *pdp = dd->ipath_pd[0];
  327. /* ensure chip does no sends or receives while we re-initialize */
  328. dd->ipath_control = dd->ipath_sendctrl = dd->ipath_rcvctrl = 0U;
  329. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, 0);
  330. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0);
  331. ipath_write_kreg(dd, dd->ipath_kregs->kr_control, 0);
  332. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_portcnt);
  333. if (dd->ipath_portcnt != rtmp)
  334. dev_info(&dd->pcidev->dev, "portcnt was %u before "
  335. "reset, now %u, using original\n",
  336. dd->ipath_portcnt, rtmp);
  337. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidcnt);
  338. if (rtmp != dd->ipath_rcvtidcnt)
  339. dev_info(&dd->pcidev->dev, "tidcnt was %u before "
  340. "reset, now %u, using original\n",
  341. dd->ipath_rcvtidcnt, rtmp);
  342. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvtidbase);
  343. if (rtmp != dd->ipath_rcvtidbase)
  344. dev_info(&dd->pcidev->dev, "tidbase was %u before "
  345. "reset, now %u, using original\n",
  346. dd->ipath_rcvtidbase, rtmp);
  347. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrcnt);
  348. if (rtmp != dd->ipath_rcvegrcnt)
  349. dev_info(&dd->pcidev->dev, "egrcnt was %u before "
  350. "reset, now %u, using original\n",
  351. dd->ipath_rcvegrcnt, rtmp);
  352. rtmp = ipath_read_kreg32(dd, dd->ipath_kregs->kr_rcvegrbase);
  353. if (rtmp != dd->ipath_rcvegrbase)
  354. dev_info(&dd->pcidev->dev, "egrbase was %u before "
  355. "reset, now %u, using original\n",
  356. dd->ipath_rcvegrbase, rtmp);
  357. return 0;
  358. }
  359. static int init_pioavailregs(struct ipath_devdata *dd)
  360. {
  361. int ret;
  362. dd->ipath_pioavailregs_dma = dma_alloc_coherent(
  363. &dd->pcidev->dev, PAGE_SIZE, &dd->ipath_pioavailregs_phys,
  364. GFP_KERNEL);
  365. if (!dd->ipath_pioavailregs_dma) {
  366. ipath_dev_err(dd, "failed to allocate PIOavail reg area "
  367. "in memory\n");
  368. ret = -ENOMEM;
  369. goto done;
  370. }
  371. /*
  372. * we really want L2 cache aligned, but for current CPUs of
  373. * interest, they are the same.
  374. */
  375. dd->ipath_statusp = (u64 *)
  376. ((char *)dd->ipath_pioavailregs_dma +
  377. ((2 * L1_CACHE_BYTES +
  378. dd->ipath_pioavregs * sizeof(u64)) & ~L1_CACHE_BYTES));
  379. /* copy the current value now that it's really allocated */
  380. *dd->ipath_statusp = dd->_ipath_status;
  381. /*
  382. * setup buffer to hold freeze msg, accessible to apps,
  383. * following statusp
  384. */
  385. dd->ipath_freezemsg = (char *)&dd->ipath_statusp[1];
  386. /* and its length */
  387. dd->ipath_freezelen = L1_CACHE_BYTES - sizeof(dd->ipath_statusp[0]);
  388. ret = 0;
  389. done:
  390. return ret;
  391. }
  392. /**
  393. * init_shadow_tids - allocate the shadow TID array
  394. * @dd: the infinipath device
  395. *
  396. * allocate the shadow TID array, so we can ipath_munlock previous
  397. * entries. It may make more sense to move the pageshadow to the
  398. * port data structure, so we only allocate memory for ports actually
  399. * in use, since we at 8k per port, now.
  400. */
  401. static void init_shadow_tids(struct ipath_devdata *dd)
  402. {
  403. struct page **pages;
  404. dma_addr_t *addrs;
  405. pages = vmalloc(dd->ipath_cfgports * dd->ipath_rcvtidcnt *
  406. sizeof(struct page *));
  407. if (!pages) {
  408. ipath_dev_err(dd, "failed to allocate shadow page * "
  409. "array, no expected sends!\n");
  410. dd->ipath_pageshadow = NULL;
  411. return;
  412. }
  413. addrs = vmalloc(dd->ipath_cfgports * dd->ipath_rcvtidcnt *
  414. sizeof(dma_addr_t));
  415. if (!addrs) {
  416. ipath_dev_err(dd, "failed to allocate shadow dma handle "
  417. "array, no expected sends!\n");
  418. vfree(dd->ipath_pageshadow);
  419. dd->ipath_pageshadow = NULL;
  420. return;
  421. }
  422. memset(pages, 0, dd->ipath_cfgports * dd->ipath_rcvtidcnt *
  423. sizeof(struct page *));
  424. dd->ipath_pageshadow = pages;
  425. dd->ipath_physshadow = addrs;
  426. }
  427. static void enable_chip(struct ipath_devdata *dd,
  428. struct ipath_portdata *pd, int reinit)
  429. {
  430. u32 val;
  431. int i;
  432. if (!reinit)
  433. init_waitqueue_head(&ipath_state_wait);
  434. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  435. dd->ipath_rcvctrl);
  436. /* Enable PIO send, and update of PIOavail regs to memory. */
  437. dd->ipath_sendctrl = INFINIPATH_S_PIOENABLE |
  438. INFINIPATH_S_PIOBUFAVAILUPD;
  439. ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
  440. dd->ipath_sendctrl);
  441. /*
  442. * enable port 0 receive, and receive interrupt. other ports
  443. * done as user opens and inits them.
  444. */
  445. dd->ipath_rcvctrl = INFINIPATH_R_TAILUPD |
  446. (1ULL << INFINIPATH_R_PORTENABLE_SHIFT) |
  447. (1ULL << INFINIPATH_R_INTRAVAIL_SHIFT);
  448. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
  449. dd->ipath_rcvctrl);
  450. /*
  451. * now ready for use. this should be cleared whenever we
  452. * detect a reset, or initiate one.
  453. */
  454. dd->ipath_flags |= IPATH_INITTED;
  455. /*
  456. * init our shadow copies of head from tail values, and write
  457. * head values to match.
  458. */
  459. val = ipath_read_ureg32(dd, ur_rcvegrindextail, 0);
  460. (void)ipath_write_ureg(dd, ur_rcvegrindexhead, val, 0);
  461. dd->ipath_port0head = ipath_read_ureg32(dd, ur_rcvhdrtail, 0);
  462. /* Initialize so we interrupt on next packet received */
  463. (void)ipath_write_ureg(dd, ur_rcvhdrhead,
  464. dd->ipath_rhdrhead_intr_off |
  465. dd->ipath_port0head, 0);
  466. /*
  467. * by now pioavail updates to memory should have occurred, so
  468. * copy them into our working/shadow registers; this is in
  469. * case something went wrong with abort, but mostly to get the
  470. * initial values of the generation bit correct.
  471. */
  472. for (i = 0; i < dd->ipath_pioavregs; i++) {
  473. __le64 val;
  474. /*
  475. * Chip Errata bug 6641; even and odd qwords>3 are swapped.
  476. */
  477. if (i > 3) {
  478. if (i & 1)
  479. val = dd->ipath_pioavailregs_dma[i - 1];
  480. else
  481. val = dd->ipath_pioavailregs_dma[i + 1];
  482. }
  483. else
  484. val = dd->ipath_pioavailregs_dma[i];
  485. dd->ipath_pioavailshadow[i] = le64_to_cpu(val);
  486. }
  487. /* can get counters, stats, etc. */
  488. dd->ipath_flags |= IPATH_PRESENT;
  489. }
  490. static int init_housekeeping(struct ipath_devdata *dd,
  491. struct ipath_portdata **pdp, int reinit)
  492. {
  493. char boardn[32];
  494. int ret = 0;
  495. /*
  496. * have to clear shadow copies of registers at init that are
  497. * not otherwise set here, or all kinds of bizarre things
  498. * happen with driver on chip reset
  499. */
  500. dd->ipath_rcvhdrsize = 0;
  501. /*
  502. * Don't clear ipath_flags as 8bit mode was set before
  503. * entering this func. However, we do set the linkstate to
  504. * unknown, so we can watch for a transition.
  505. * PRESENT is set because we want register reads to work,
  506. * and the kernel infrastructure saw it in config space;
  507. * We clear it if we have failures.
  508. */
  509. dd->ipath_flags |= IPATH_LINKUNK | IPATH_PRESENT;
  510. dd->ipath_flags &= ~(IPATH_LINKACTIVE | IPATH_LINKARMED |
  511. IPATH_LINKDOWN | IPATH_LINKINIT);
  512. ipath_cdbg(VERBOSE, "Try to read spc chip revision\n");
  513. dd->ipath_revision =
  514. ipath_read_kreg64(dd, dd->ipath_kregs->kr_revision);
  515. /*
  516. * set up fundamental info we need to use the chip; we assume
  517. * if the revision reg and these regs are OK, we don't need to
  518. * special case the rest
  519. */
  520. dd->ipath_sregbase =
  521. ipath_read_kreg32(dd, dd->ipath_kregs->kr_sendregbase);
  522. dd->ipath_cregbase =
  523. ipath_read_kreg32(dd, dd->ipath_kregs->kr_counterregbase);
  524. dd->ipath_uregbase =
  525. ipath_read_kreg32(dd, dd->ipath_kregs->kr_userregbase);
  526. ipath_cdbg(VERBOSE, "ipath_kregbase %p, sendbase %x usrbase %x, "
  527. "cntrbase %x\n", dd->ipath_kregbase, dd->ipath_sregbase,
  528. dd->ipath_uregbase, dd->ipath_cregbase);
  529. if ((dd->ipath_revision & 0xffffffff) == 0xffffffff
  530. || (dd->ipath_sregbase & 0xffffffff) == 0xffffffff
  531. || (dd->ipath_cregbase & 0xffffffff) == 0xffffffff
  532. || (dd->ipath_uregbase & 0xffffffff) == 0xffffffff) {
  533. ipath_dev_err(dd, "Register read failures from chip, "
  534. "giving up initialization\n");
  535. dd->ipath_flags &= ~IPATH_PRESENT;
  536. ret = -ENODEV;
  537. goto done;
  538. }
  539. /* clear diagctrl register, in case diags were running and crashed */
  540. ipath_write_kreg (dd, dd->ipath_kregs->kr_hwdiagctrl, 0);
  541. /* clear the initial reset flag, in case first driver load */
  542. ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear,
  543. INFINIPATH_E_RESET);
  544. if (reinit)
  545. ret = init_chip_reset(dd, pdp);
  546. else
  547. ret = init_chip_first(dd, pdp);
  548. if (ret)
  549. goto done;
  550. ipath_cdbg(VERBOSE, "Revision %llx (PCI %x), %u ports, %u tids, "
  551. "%u egrtids\n", (unsigned long long) dd->ipath_revision,
  552. dd->ipath_pcirev, dd->ipath_portcnt, dd->ipath_rcvtidcnt,
  553. dd->ipath_rcvegrcnt);
  554. if (((dd->ipath_revision >> INFINIPATH_R_SOFTWARE_SHIFT) &
  555. INFINIPATH_R_SOFTWARE_MASK) != IPATH_CHIP_SWVERSION) {
  556. ipath_dev_err(dd, "Driver only handles version %d, "
  557. "chip swversion is %d (%llx), failng\n",
  558. IPATH_CHIP_SWVERSION,
  559. (int)(dd->ipath_revision >>
  560. INFINIPATH_R_SOFTWARE_SHIFT) &
  561. INFINIPATH_R_SOFTWARE_MASK,
  562. (unsigned long long) dd->ipath_revision);
  563. ret = -ENOSYS;
  564. goto done;
  565. }
  566. dd->ipath_majrev = (u8) ((dd->ipath_revision >>
  567. INFINIPATH_R_CHIPREVMAJOR_SHIFT) &
  568. INFINIPATH_R_CHIPREVMAJOR_MASK);
  569. dd->ipath_minrev = (u8) ((dd->ipath_revision >>
  570. INFINIPATH_R_CHIPREVMINOR_SHIFT) &
  571. INFINIPATH_R_CHIPREVMINOR_MASK);
  572. dd->ipath_boardrev = (u8) ((dd->ipath_revision >>
  573. INFINIPATH_R_BOARDID_SHIFT) &
  574. INFINIPATH_R_BOARDID_MASK);
  575. ret = dd->ipath_f_get_boardname(dd, boardn, sizeof boardn);
  576. snprintf(dd->ipath_boardversion, sizeof(dd->ipath_boardversion),
  577. "Driver %u.%u, %s, InfiniPath%u %u.%u, PCI %u, "
  578. "SW Compat %u\n",
  579. IPATH_CHIP_VERS_MAJ, IPATH_CHIP_VERS_MIN, boardn,
  580. (unsigned)(dd->ipath_revision >> INFINIPATH_R_ARCH_SHIFT) &
  581. INFINIPATH_R_ARCH_MASK,
  582. dd->ipath_majrev, dd->ipath_minrev, dd->ipath_pcirev,
  583. (unsigned)(dd->ipath_revision >>
  584. INFINIPATH_R_SOFTWARE_SHIFT) &
  585. INFINIPATH_R_SOFTWARE_MASK);
  586. ipath_dbg("%s", dd->ipath_boardversion);
  587. done:
  588. return ret;
  589. }
  590. /**
  591. * ipath_init_chip - do the actual initialization sequence on the chip
  592. * @dd: the infinipath device
  593. * @reinit: reinitializing, so don't allocate new memory
  594. *
  595. * Do the actual initialization sequence on the chip. This is done
  596. * both from the init routine called from the PCI infrastructure, and
  597. * when we reset the chip, or detect that it was reset internally,
  598. * or it's administratively re-enabled.
  599. *
  600. * Memory allocation here and in called routines is only done in
  601. * the first case (reinit == 0). We have to be careful, because even
  602. * without memory allocation, we need to re-write all the chip registers
  603. * TIDs, etc. after the reset or enable has completed.
  604. */
  605. int ipath_init_chip(struct ipath_devdata *dd, int reinit)
  606. {
  607. int ret = 0, i;
  608. u32 val32, kpiobufs;
  609. u32 piobufs, uports;
  610. u64 val;
  611. struct ipath_portdata *pd = NULL; /* keep gcc4 happy */
  612. gfp_t gfp_flags = GFP_USER | __GFP_COMP;
  613. ret = init_housekeeping(dd, &pd, reinit);
  614. if (ret)
  615. goto done;
  616. /*
  617. * we ignore most issues after reporting them, but have to specially
  618. * handle hardware-disabled chips.
  619. */
  620. if (ret == 2) {
  621. /* unique error, known to ipath_init_one */
  622. ret = -EPERM;
  623. goto done;
  624. }
  625. /*
  626. * We could bump this to allow for full rcvegrcnt + rcvtidcnt,
  627. * but then it no longer nicely fits power of two, and since
  628. * we now use routines that backend onto __get_free_pages, the
  629. * rest would be wasted.
  630. */
  631. dd->ipath_rcvhdrcnt = dd->ipath_rcvegrcnt;
  632. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrcnt,
  633. dd->ipath_rcvhdrcnt);
  634. /*
  635. * Set up the shadow copies of the piobufavail registers,
  636. * which we compare against the chip registers for now, and
  637. * the in memory DMA'ed copies of the registers. This has to
  638. * be done early, before we calculate lastport, etc.
  639. */
  640. piobufs = dd->ipath_piobcnt2k + dd->ipath_piobcnt4k;
  641. /*
  642. * calc number of pioavail registers, and save it; we have 2
  643. * bits per buffer.
  644. */
  645. dd->ipath_pioavregs = ALIGN(piobufs, sizeof(u64) * BITS_PER_BYTE / 2)
  646. / (sizeof(u64) * BITS_PER_BYTE / 2);
  647. uports = dd->ipath_cfgports ? dd->ipath_cfgports - 1 : 0;
  648. if (ipath_kpiobufs == 0) {
  649. /* not set by user (this is default) */
  650. if (piobufs >= (uports * IPATH_MIN_USER_PORT_BUFCNT) + 32)
  651. kpiobufs = 32;
  652. else
  653. kpiobufs = 16;
  654. }
  655. else
  656. kpiobufs = ipath_kpiobufs;
  657. if (kpiobufs + (uports * IPATH_MIN_USER_PORT_BUFCNT) > piobufs) {
  658. i = (int) piobufs -
  659. (int) (uports * IPATH_MIN_USER_PORT_BUFCNT);
  660. if (i < 0)
  661. i = 0;
  662. dev_info(&dd->pcidev->dev, "Allocating %d PIO bufs of "
  663. "%d for kernel leaves too few for %d user ports "
  664. "(%d each); using %u\n", kpiobufs,
  665. piobufs, uports, IPATH_MIN_USER_PORT_BUFCNT, i);
  666. /*
  667. * shouldn't change ipath_kpiobufs, because could be
  668. * different for different devices...
  669. */
  670. kpiobufs = i;
  671. }
  672. dd->ipath_lastport_piobuf = piobufs - kpiobufs;
  673. dd->ipath_pbufsport =
  674. uports ? dd->ipath_lastport_piobuf / uports : 0;
  675. val32 = dd->ipath_lastport_piobuf - (dd->ipath_pbufsport * uports);
  676. if (val32 > 0) {
  677. ipath_dbg("allocating %u pbufs/port leaves %u unused, "
  678. "add to kernel\n", dd->ipath_pbufsport, val32);
  679. dd->ipath_lastport_piobuf -= val32;
  680. ipath_dbg("%u pbufs/port leaves %u unused, add to kernel\n",
  681. dd->ipath_pbufsport, val32);
  682. }
  683. dd->ipath_lastpioindex = dd->ipath_lastport_piobuf;
  684. ipath_cdbg(VERBOSE, "%d PIO bufs for kernel out of %d total %u "
  685. "each for %u user ports\n", kpiobufs,
  686. piobufs, dd->ipath_pbufsport, uports);
  687. dd->ipath_f_early_init(dd);
  688. /* early_init sets rcvhdrentsize and rcvhdrsize, so this must be
  689. * done after early_init */
  690. dd->ipath_hdrqlast =
  691. dd->ipath_rcvhdrentsize * (dd->ipath_rcvhdrcnt - 1);
  692. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrentsize,
  693. dd->ipath_rcvhdrentsize);
  694. ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
  695. dd->ipath_rcvhdrsize);
  696. if (!reinit) {
  697. ret = init_pioavailregs(dd);
  698. init_shadow_tids(dd);
  699. if (ret)
  700. goto done;
  701. }
  702. (void)ipath_write_kreg(dd, dd->ipath_kregs->kr_sendpioavailaddr,
  703. dd->ipath_pioavailregs_phys);
  704. /*
  705. * this is to detect s/w errors, which the h/w works around by
  706. * ignoring the low 6 bits of address, if it wasn't aligned.
  707. */
  708. val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_sendpioavailaddr);
  709. if (val != dd->ipath_pioavailregs_phys) {
  710. ipath_dev_err(dd, "Catastrophic software error, "
  711. "SendPIOAvailAddr written as %lx, "
  712. "read back as %llx\n",
  713. (unsigned long) dd->ipath_pioavailregs_phys,
  714. (unsigned long long) val);
  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. /*
  757. * Set up the port 0 (kernel) rcvhdr q and egr TIDs. If doing
  758. * re-init, the simplest way to handle this is to free
  759. * existing, and re-allocate.
  760. * Need to re-create rest of port 0 portdata as well.
  761. */
  762. if (reinit) {
  763. /* Alloc and init new ipath_portdata for port0,
  764. * Then free old pd. Could lead to fragmentation, but also
  765. * makes later support for hot-swap easier.
  766. */
  767. struct ipath_portdata *npd;
  768. npd = create_portdata0(dd);
  769. if (npd) {
  770. ipath_free_pddata(dd, pd);
  771. dd->ipath_pd[0] = pd = npd;
  772. } else {
  773. ipath_dev_err(dd, "Unable to allocate portdata for"
  774. " port 0, failing\n");
  775. ret = -ENOMEM;
  776. goto done;
  777. }
  778. }
  779. dd->ipath_f_tidtemplate(dd);
  780. ret = ipath_create_rcvhdrq(dd, pd);
  781. if (!ret) {
  782. dd->ipath_hdrqtailptr =
  783. (volatile __le64 *)pd->port_rcvhdrtail_kvaddr;
  784. ret = create_port0_egr(dd);
  785. }
  786. if (ret)
  787. ipath_dev_err(dd, "failed to allocate port 0 (kernel) "
  788. "rcvhdrq and/or egr bufs\n");
  789. else
  790. enable_chip(dd, pd, reinit);
  791. if (!ret && !reinit) {
  792. /* used when we close a port, for DMA already in flight at close */
  793. dd->ipath_dummy_hdrq = dma_alloc_coherent(
  794. &dd->pcidev->dev, pd->port_rcvhdrq_size,
  795. &dd->ipath_dummy_hdrq_phys,
  796. gfp_flags);
  797. if (!dd->ipath_dummy_hdrq ) {
  798. dev_info(&dd->pcidev->dev,
  799. "Couldn't allocate 0x%lx bytes for dummy hdrq\n",
  800. pd->port_rcvhdrq_size);
  801. /* fallback to just 0'ing */
  802. dd->ipath_dummy_hdrq_phys = 0UL;
  803. }
  804. }
  805. /*
  806. * cause retrigger of pending interrupts ignored during init,
  807. * even if we had errors
  808. */
  809. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, 0ULL);
  810. if(!dd->ipath_stats_timer_active) {
  811. /*
  812. * first init, or after an admin disable/enable
  813. * set up stats retrieval timer, even if we had errors
  814. * in last portion of setup
  815. */
  816. init_timer(&dd->ipath_stats_timer);
  817. dd->ipath_stats_timer.function = ipath_get_faststats;
  818. dd->ipath_stats_timer.data = (unsigned long) dd;
  819. /* every 5 seconds; */
  820. dd->ipath_stats_timer.expires = jiffies + 5 * HZ;
  821. /* takes ~16 seconds to overflow at full IB 4x bandwdith */
  822. add_timer(&dd->ipath_stats_timer);
  823. dd->ipath_stats_timer_active = 1;
  824. }
  825. done:
  826. if (!ret) {
  827. *dd->ipath_statusp |= IPATH_STATUS_CHIP_PRESENT;
  828. if (!dd->ipath_f_intrsetup(dd)) {
  829. /* now we can enable all interrupts from the chip */
  830. ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask,
  831. -1LL);
  832. /* force re-interrupt of any pending interrupts. */
  833. ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear,
  834. 0ULL);
  835. /* chip is usable; mark it as initialized */
  836. *dd->ipath_statusp |= IPATH_STATUS_INITTED;
  837. } else
  838. ipath_dev_err(dd, "No interrupts enabled, couldn't "
  839. "setup interrupt address\n");
  840. if (dd->ipath_cfgports > ipath_stats.sps_nports)
  841. /*
  842. * sps_nports is a global, so, we set it to
  843. * the highest number of ports of any of the
  844. * chips we find; we never decrement it, at
  845. * least for now. Since this might have changed
  846. * over disable/enable or prior to reset, always
  847. * do the check and potentially adjust.
  848. */
  849. ipath_stats.sps_nports = dd->ipath_cfgports;
  850. } else
  851. ipath_dbg("Failed (%d) to initialize chip\n", ret);
  852. /* if ret is non-zero, we probably should do some cleanup
  853. here... */
  854. return ret;
  855. }
  856. static int ipath_set_kpiobufs(const char *str, struct kernel_param *kp)
  857. {
  858. struct ipath_devdata *dd;
  859. unsigned long flags;
  860. unsigned short val;
  861. int ret;
  862. ret = ipath_parse_ushort(str, &val);
  863. spin_lock_irqsave(&ipath_devs_lock, flags);
  864. if (ret < 0)
  865. goto bail;
  866. if (val == 0) {
  867. ret = -EINVAL;
  868. goto bail;
  869. }
  870. list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
  871. if (dd->ipath_kregbase)
  872. continue;
  873. if (val > (dd->ipath_piobcnt2k + dd->ipath_piobcnt4k -
  874. (dd->ipath_cfgports *
  875. IPATH_MIN_USER_PORT_BUFCNT)))
  876. {
  877. ipath_dev_err(
  878. dd,
  879. "Allocating %d PIO bufs for kernel leaves "
  880. "too few for %d user ports (%d each)\n",
  881. val, dd->ipath_cfgports - 1,
  882. IPATH_MIN_USER_PORT_BUFCNT);
  883. ret = -EINVAL;
  884. goto bail;
  885. }
  886. dd->ipath_lastport_piobuf =
  887. dd->ipath_piobcnt2k + dd->ipath_piobcnt4k - val;
  888. }
  889. ipath_kpiobufs = val;
  890. ret = 0;
  891. bail:
  892. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  893. return ret;
  894. }