ipath_init_chip.c 31 KB

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