ipath_init_chip.c 31 KB

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