ipath_init_chip.c 31 KB

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