qib_pcie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (c) 2008, 2009 QLogic Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/pci.h>
  33. #include <linux/io.h>
  34. #include <linux/delay.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/aer.h>
  37. #include "qib.h"
  38. /*
  39. * This file contains PCIe utility routines that are common to the
  40. * various QLogic InfiniPath adapters
  41. */
  42. /*
  43. * Code to adjust PCIe capabilities.
  44. * To minimize the change footprint, we call it
  45. * from qib_pcie_params, which every chip-specific
  46. * file calls, even though this violates some
  47. * expectations of harmlessness.
  48. */
  49. static int qib_tune_pcie_caps(struct qib_devdata *);
  50. static int qib_tune_pcie_coalesce(struct qib_devdata *);
  51. /*
  52. * Do all the common PCIe setup and initialization.
  53. * devdata is not yet allocated, and is not allocated until after this
  54. * routine returns success. Therefore qib_dev_err() can't be used for error
  55. * printing.
  56. */
  57. int qib_pcie_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  58. {
  59. int ret;
  60. ret = pci_enable_device(pdev);
  61. if (ret) {
  62. /*
  63. * This can happen (in theory) iff:
  64. * We did a chip reset, and then failed to reprogram the
  65. * BAR, or the chip reset due to an internal error. We then
  66. * unloaded the driver and reloaded it.
  67. *
  68. * Both reset cases set the BAR back to initial state. For
  69. * the latter case, the AER sticky error bit at offset 0x718
  70. * should be set, but the Linux kernel doesn't yet know
  71. * about that, it appears. If the original BAR was retained
  72. * in the kernel data structures, this may be OK.
  73. */
  74. qib_early_err(&pdev->dev, "pci enable failed: error %d\n",
  75. -ret);
  76. goto done;
  77. }
  78. ret = pci_request_regions(pdev, QIB_DRV_NAME);
  79. if (ret) {
  80. qib_devinfo(pdev, "pci_request_regions fails: err %d\n", -ret);
  81. goto bail;
  82. }
  83. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  84. if (ret) {
  85. /*
  86. * If the 64 bit setup fails, try 32 bit. Some systems
  87. * do not setup 64 bit maps on systems with 2GB or less
  88. * memory installed.
  89. */
  90. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  91. if (ret) {
  92. qib_devinfo(pdev, "Unable to set DMA mask: %d\n", ret);
  93. goto bail;
  94. }
  95. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  96. } else
  97. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  98. if (ret) {
  99. qib_early_err(&pdev->dev,
  100. "Unable to set DMA consistent mask: %d\n", ret);
  101. goto bail;
  102. }
  103. pci_set_master(pdev);
  104. ret = pci_enable_pcie_error_reporting(pdev);
  105. if (ret) {
  106. qib_early_err(&pdev->dev,
  107. "Unable to enable pcie error reporting: %d\n",
  108. ret);
  109. ret = 0;
  110. }
  111. goto done;
  112. bail:
  113. pci_disable_device(pdev);
  114. pci_release_regions(pdev);
  115. done:
  116. return ret;
  117. }
  118. /*
  119. * Do remaining PCIe setup, once dd is allocated, and save away
  120. * fields required to re-initialize after a chip reset, or for
  121. * various other purposes
  122. */
  123. int qib_pcie_ddinit(struct qib_devdata *dd, struct pci_dev *pdev,
  124. const struct pci_device_id *ent)
  125. {
  126. unsigned long len;
  127. resource_size_t addr;
  128. dd->pcidev = pdev;
  129. pci_set_drvdata(pdev, dd);
  130. addr = pci_resource_start(pdev, 0);
  131. len = pci_resource_len(pdev, 0);
  132. #if defined(__powerpc__)
  133. /* There isn't a generic way to specify writethrough mappings */
  134. dd->kregbase = __ioremap(addr, len, _PAGE_NO_CACHE | _PAGE_WRITETHRU);
  135. #else
  136. dd->kregbase = ioremap_nocache(addr, len);
  137. #endif
  138. if (!dd->kregbase)
  139. return -ENOMEM;
  140. dd->kregend = (u64 __iomem *)((void __iomem *) dd->kregbase + len);
  141. dd->physaddr = addr; /* used for io_remap, etc. */
  142. /*
  143. * Save BARs to rewrite after device reset. Save all 64 bits of
  144. * BAR, just in case.
  145. */
  146. dd->pcibar0 = addr;
  147. dd->pcibar1 = addr >> 32;
  148. dd->deviceid = ent->device; /* save for later use */
  149. dd->vendorid = ent->vendor;
  150. return 0;
  151. }
  152. /*
  153. * Do PCIe cleanup, after chip-specific cleanup, etc. Just prior
  154. * to releasing the dd memory.
  155. * void because none of the core pcie cleanup returns are void
  156. */
  157. void qib_pcie_ddcleanup(struct qib_devdata *dd)
  158. {
  159. u64 __iomem *base = (void __iomem *) dd->kregbase;
  160. dd->kregbase = NULL;
  161. iounmap(base);
  162. if (dd->piobase)
  163. iounmap(dd->piobase);
  164. if (dd->userbase)
  165. iounmap(dd->userbase);
  166. if (dd->piovl15base)
  167. iounmap(dd->piovl15base);
  168. pci_disable_device(dd->pcidev);
  169. pci_release_regions(dd->pcidev);
  170. pci_set_drvdata(dd->pcidev, NULL);
  171. }
  172. static void qib_msix_setup(struct qib_devdata *dd, int pos, u32 *msixcnt,
  173. struct msix_entry *msix_entry)
  174. {
  175. int ret;
  176. u32 tabsize = 0;
  177. u16 msix_flags;
  178. pci_read_config_word(dd->pcidev, pos + PCI_MSIX_FLAGS, &msix_flags);
  179. tabsize = 1 + (msix_flags & PCI_MSIX_FLAGS_QSIZE);
  180. if (tabsize > *msixcnt)
  181. tabsize = *msixcnt;
  182. ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
  183. if (ret > 0) {
  184. tabsize = ret;
  185. ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
  186. }
  187. if (ret) {
  188. qib_dev_err(dd, "pci_enable_msix %d vectors failed: %d, "
  189. "falling back to INTx\n", tabsize, ret);
  190. tabsize = 0;
  191. }
  192. *msixcnt = tabsize;
  193. if (ret)
  194. qib_enable_intx(dd->pcidev);
  195. }
  196. /**
  197. * We save the msi lo and hi values, so we can restore them after
  198. * chip reset (the kernel PCI infrastructure doesn't yet handle that
  199. * correctly.
  200. */
  201. static int qib_msi_setup(struct qib_devdata *dd, int pos)
  202. {
  203. struct pci_dev *pdev = dd->pcidev;
  204. u16 control;
  205. int ret;
  206. ret = pci_enable_msi(pdev);
  207. if (ret)
  208. qib_dev_err(dd, "pci_enable_msi failed: %d, "
  209. "interrupts may not work\n", ret);
  210. /* continue even if it fails, we may still be OK... */
  211. pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_LO,
  212. &dd->msi_lo);
  213. pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_HI,
  214. &dd->msi_hi);
  215. pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &control);
  216. /* now save the data (vector) info */
  217. pci_read_config_word(pdev, pos + ((control & PCI_MSI_FLAGS_64BIT)
  218. ? 12 : 8),
  219. &dd->msi_data);
  220. return ret;
  221. }
  222. int qib_pcie_params(struct qib_devdata *dd, u32 minw, u32 *nent,
  223. struct msix_entry *entry)
  224. {
  225. u16 linkstat, speed;
  226. int pos = 0, pose, ret = 1;
  227. pose = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
  228. if (!pose) {
  229. qib_dev_err(dd, "Can't find PCI Express capability!\n");
  230. /* set up something... */
  231. dd->lbus_width = 1;
  232. dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
  233. goto bail;
  234. }
  235. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSIX);
  236. if (nent && *nent && pos) {
  237. qib_msix_setup(dd, pos, nent, entry);
  238. ret = 0; /* did it, either MSIx or INTx */
  239. } else {
  240. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
  241. if (pos)
  242. ret = qib_msi_setup(dd, pos);
  243. else
  244. qib_dev_err(dd, "No PCI MSI or MSIx capability!\n");
  245. }
  246. if (!pos)
  247. qib_enable_intx(dd->pcidev);
  248. pci_read_config_word(dd->pcidev, pose + PCI_EXP_LNKSTA, &linkstat);
  249. /*
  250. * speed is bits 0-3, linkwidth is bits 4-8
  251. * no defines for them in headers
  252. */
  253. speed = linkstat & 0xf;
  254. linkstat >>= 4;
  255. linkstat &= 0x1f;
  256. dd->lbus_width = linkstat;
  257. switch (speed) {
  258. case 1:
  259. dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
  260. break;
  261. case 2:
  262. dd->lbus_speed = 5000; /* Gen1, 5GHz */
  263. break;
  264. default: /* not defined, assume gen1 */
  265. dd->lbus_speed = 2500;
  266. break;
  267. }
  268. /*
  269. * Check against expected pcie width and complain if "wrong"
  270. * on first initialization, not afterwards (i.e., reset).
  271. */
  272. if (minw && linkstat < minw)
  273. qib_dev_err(dd,
  274. "PCIe width %u (x%u HCA), performance reduced\n",
  275. linkstat, minw);
  276. qib_tune_pcie_caps(dd);
  277. qib_tune_pcie_coalesce(dd);
  278. bail:
  279. /* fill in string, even on errors */
  280. snprintf(dd->lbus_info, sizeof(dd->lbus_info),
  281. "PCIe,%uMHz,x%u\n", dd->lbus_speed, dd->lbus_width);
  282. return ret;
  283. }
  284. /*
  285. * Setup pcie interrupt stuff again after a reset. I'd like to just call
  286. * pci_enable_msi() again for msi, but when I do that,
  287. * the MSI enable bit doesn't get set in the command word, and
  288. * we switch to to a different interrupt vector, which is confusing,
  289. * so I instead just do it all inline. Perhaps somehow can tie this
  290. * into the PCIe hotplug support at some point
  291. */
  292. int qib_reinit_intr(struct qib_devdata *dd)
  293. {
  294. int pos;
  295. u16 control;
  296. int ret = 0;
  297. /* If we aren't using MSI, don't restore it */
  298. if (!dd->msi_lo)
  299. goto bail;
  300. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
  301. if (!pos) {
  302. qib_dev_err(dd, "Can't find MSI capability, "
  303. "can't restore MSI settings\n");
  304. ret = 0;
  305. /* nothing special for MSIx, just MSI */
  306. goto bail;
  307. }
  308. pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_LO,
  309. dd->msi_lo);
  310. pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_HI,
  311. dd->msi_hi);
  312. pci_read_config_word(dd->pcidev, pos + PCI_MSI_FLAGS, &control);
  313. if (!(control & PCI_MSI_FLAGS_ENABLE)) {
  314. control |= PCI_MSI_FLAGS_ENABLE;
  315. pci_write_config_word(dd->pcidev, pos + PCI_MSI_FLAGS,
  316. control);
  317. }
  318. /* now rewrite the data (vector) info */
  319. pci_write_config_word(dd->pcidev, pos +
  320. ((control & PCI_MSI_FLAGS_64BIT) ? 12 : 8),
  321. dd->msi_data);
  322. ret = 1;
  323. bail:
  324. if (!ret && (dd->flags & QIB_HAS_INTX)) {
  325. qib_enable_intx(dd->pcidev);
  326. ret = 1;
  327. }
  328. /* and now set the pci master bit again */
  329. pci_set_master(dd->pcidev);
  330. return ret;
  331. }
  332. /*
  333. * Disable msi interrupt if enabled, and clear msi_lo.
  334. * This is used primarily for the fallback to INTx, but
  335. * is also used in reinit after reset, and during cleanup.
  336. */
  337. void qib_nomsi(struct qib_devdata *dd)
  338. {
  339. dd->msi_lo = 0;
  340. pci_disable_msi(dd->pcidev);
  341. }
  342. /*
  343. * Same as qib_nosmi, but for MSIx.
  344. */
  345. void qib_nomsix(struct qib_devdata *dd)
  346. {
  347. pci_disable_msix(dd->pcidev);
  348. }
  349. /*
  350. * Similar to pci_intx(pdev, 1), except that we make sure
  351. * msi(x) is off.
  352. */
  353. void qib_enable_intx(struct pci_dev *pdev)
  354. {
  355. u16 cw, new;
  356. int pos;
  357. /* first, turn on INTx */
  358. pci_read_config_word(pdev, PCI_COMMAND, &cw);
  359. new = cw & ~PCI_COMMAND_INTX_DISABLE;
  360. if (new != cw)
  361. pci_write_config_word(pdev, PCI_COMMAND, new);
  362. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  363. if (pos) {
  364. /* then turn off MSI */
  365. pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &cw);
  366. new = cw & ~PCI_MSI_FLAGS_ENABLE;
  367. if (new != cw)
  368. pci_write_config_word(pdev, pos + PCI_MSI_FLAGS, new);
  369. }
  370. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  371. if (pos) {
  372. /* then turn off MSIx */
  373. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &cw);
  374. new = cw & ~PCI_MSIX_FLAGS_ENABLE;
  375. if (new != cw)
  376. pci_write_config_word(pdev, pos + PCI_MSIX_FLAGS, new);
  377. }
  378. }
  379. /*
  380. * These two routines are helper routines for the device reset code
  381. * to move all the pcie code out of the chip-specific driver code.
  382. */
  383. void qib_pcie_getcmd(struct qib_devdata *dd, u16 *cmd, u8 *iline, u8 *cline)
  384. {
  385. pci_read_config_word(dd->pcidev, PCI_COMMAND, cmd);
  386. pci_read_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
  387. pci_read_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
  388. }
  389. void qib_pcie_reenable(struct qib_devdata *dd, u16 cmd, u8 iline, u8 cline)
  390. {
  391. int r;
  392. r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
  393. dd->pcibar0);
  394. if (r)
  395. qib_dev_err(dd, "rewrite of BAR0 failed: %d\n", r);
  396. r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
  397. dd->pcibar1);
  398. if (r)
  399. qib_dev_err(dd, "rewrite of BAR1 failed: %d\n", r);
  400. /* now re-enable memory access, and restore cosmetic settings */
  401. pci_write_config_word(dd->pcidev, PCI_COMMAND, cmd);
  402. pci_write_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
  403. pci_write_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
  404. r = pci_enable_device(dd->pcidev);
  405. if (r)
  406. qib_dev_err(dd, "pci_enable_device failed after "
  407. "reset: %d\n", r);
  408. }
  409. /* code to adjust PCIe capabilities. */
  410. static int fld2val(int wd, int mask)
  411. {
  412. int lsbmask;
  413. if (!mask)
  414. return 0;
  415. wd &= mask;
  416. lsbmask = mask ^ (mask & (mask - 1));
  417. wd /= lsbmask;
  418. return wd;
  419. }
  420. static int val2fld(int wd, int mask)
  421. {
  422. int lsbmask;
  423. if (!mask)
  424. return 0;
  425. lsbmask = mask ^ (mask & (mask - 1));
  426. wd *= lsbmask;
  427. return wd;
  428. }
  429. static int qib_pcie_coalesce;
  430. module_param_named(pcie_coalesce, qib_pcie_coalesce, int, S_IRUGO);
  431. MODULE_PARM_DESC(pcie_coalesce, "tune PCIe colescing on some Intel chipsets");
  432. /*
  433. * Enable PCIe completion and data coalescing, on Intel 5x00 and 7300
  434. * chipsets. This is known to be unsafe for some revisions of some
  435. * of these chipsets, with some BIOS settings, and enabling it on those
  436. * systems may result in the system crashing, and/or data corruption.
  437. */
  438. static int qib_tune_pcie_coalesce(struct qib_devdata *dd)
  439. {
  440. int r;
  441. struct pci_dev *parent;
  442. int ppos;
  443. u16 devid;
  444. u32 mask, bits, val;
  445. if (!qib_pcie_coalesce)
  446. return 0;
  447. /* Find out supported and configured values for parent (root) */
  448. parent = dd->pcidev->bus->self;
  449. if (parent->bus->parent) {
  450. qib_devinfo(dd->pcidev, "Parent not root\n");
  451. return 1;
  452. }
  453. ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
  454. if (!ppos)
  455. return 1;
  456. if (parent->vendor != 0x8086)
  457. return 1;
  458. /*
  459. * - bit 12: Max_rdcmp_Imt_EN: need to set to 1
  460. * - bit 11: COALESCE_FORCE: need to set to 0
  461. * - bit 10: COALESCE_EN: need to set to 1
  462. * (but limitations on some on some chipsets)
  463. *
  464. * On the Intel 5000, 5100, and 7300 chipsets, there is
  465. * also: - bit 25:24: COALESCE_MODE, need to set to 0
  466. */
  467. devid = parent->device;
  468. if (devid >= 0x25e2 && devid <= 0x25fa) {
  469. /* 5000 P/V/X/Z */
  470. if (parent->revision <= 0xb2)
  471. bits = 1U << 10;
  472. else
  473. bits = 7U << 10;
  474. mask = (3U << 24) | (7U << 10);
  475. } else if (devid >= 0x65e2 && devid <= 0x65fa) {
  476. /* 5100 */
  477. bits = 1U << 10;
  478. mask = (3U << 24) | (7U << 10);
  479. } else if (devid >= 0x4021 && devid <= 0x402e) {
  480. /* 5400 */
  481. bits = 7U << 10;
  482. mask = 7U << 10;
  483. } else if (devid >= 0x3604 && devid <= 0x360a) {
  484. /* 7300 */
  485. bits = 7U << 10;
  486. mask = (3U << 24) | (7U << 10);
  487. } else {
  488. /* not one of the chipsets that we know about */
  489. return 1;
  490. }
  491. pci_read_config_dword(parent, 0x48, &val);
  492. val &= ~mask;
  493. val |= bits;
  494. r = pci_write_config_dword(parent, 0x48, val);
  495. return 0;
  496. }
  497. /*
  498. * BIOS may not set PCIe bus-utilization parameters for best performance.
  499. * Check and optionally adjust them to maximize our throughput.
  500. */
  501. static int qib_pcie_caps;
  502. module_param_named(pcie_caps, qib_pcie_caps, int, S_IRUGO);
  503. MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (4lsb), ReadReq (D4..7)");
  504. static int qib_tune_pcie_caps(struct qib_devdata *dd)
  505. {
  506. int ret = 1; /* Assume the worst */
  507. struct pci_dev *parent;
  508. int ppos, epos;
  509. u16 pcaps, pctl, ecaps, ectl;
  510. int rc_sup, ep_sup;
  511. int rc_cur, ep_cur;
  512. /* Find out supported and configured values for parent (root) */
  513. parent = dd->pcidev->bus->self;
  514. if (parent->bus->parent) {
  515. qib_devinfo(dd->pcidev, "Parent not root\n");
  516. goto bail;
  517. }
  518. ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
  519. if (ppos) {
  520. pci_read_config_word(parent, ppos + PCI_EXP_DEVCAP, &pcaps);
  521. pci_read_config_word(parent, ppos + PCI_EXP_DEVCTL, &pctl);
  522. } else
  523. goto bail;
  524. /* Find out supported and configured values for endpoint (us) */
  525. epos = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
  526. if (epos) {
  527. pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCAP, &ecaps);
  528. pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, &ectl);
  529. } else
  530. goto bail;
  531. ret = 0;
  532. /* Find max payload supported by root, endpoint */
  533. rc_sup = fld2val(pcaps, PCI_EXP_DEVCAP_PAYLOAD);
  534. ep_sup = fld2val(ecaps, PCI_EXP_DEVCAP_PAYLOAD);
  535. if (rc_sup > ep_sup)
  536. rc_sup = ep_sup;
  537. rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_PAYLOAD);
  538. ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_PAYLOAD);
  539. /* If Supported greater than limit in module param, limit it */
  540. if (rc_sup > (qib_pcie_caps & 7))
  541. rc_sup = qib_pcie_caps & 7;
  542. /* If less than (allowed, supported), bump root payload */
  543. if (rc_sup > rc_cur) {
  544. rc_cur = rc_sup;
  545. pctl = (pctl & ~PCI_EXP_DEVCTL_PAYLOAD) |
  546. val2fld(rc_cur, PCI_EXP_DEVCTL_PAYLOAD);
  547. pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
  548. }
  549. /* If less than (allowed, supported), bump endpoint payload */
  550. if (rc_sup > ep_cur) {
  551. ep_cur = rc_sup;
  552. ectl = (ectl & ~PCI_EXP_DEVCTL_PAYLOAD) |
  553. val2fld(ep_cur, PCI_EXP_DEVCTL_PAYLOAD);
  554. pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
  555. }
  556. /*
  557. * Now the Read Request size.
  558. * No field for max supported, but PCIe spec limits it to 4096,
  559. * which is code '5' (log2(4096) - 7)
  560. */
  561. rc_sup = 5;
  562. if (rc_sup > ((qib_pcie_caps >> 4) & 7))
  563. rc_sup = (qib_pcie_caps >> 4) & 7;
  564. rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_READRQ);
  565. ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_READRQ);
  566. if (rc_sup > rc_cur) {
  567. rc_cur = rc_sup;
  568. pctl = (pctl & ~PCI_EXP_DEVCTL_READRQ) |
  569. val2fld(rc_cur, PCI_EXP_DEVCTL_READRQ);
  570. pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
  571. }
  572. if (rc_sup > ep_cur) {
  573. ep_cur = rc_sup;
  574. ectl = (ectl & ~PCI_EXP_DEVCTL_READRQ) |
  575. val2fld(ep_cur, PCI_EXP_DEVCTL_READRQ);
  576. pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
  577. }
  578. bail:
  579. return ret;
  580. }
  581. /* End of PCIe capability tuning */
  582. /*
  583. * From here through qib_pci_err_handler definition is invoked via
  584. * PCI error infrastructure, registered via pci
  585. */
  586. static pci_ers_result_t
  587. qib_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  588. {
  589. struct qib_devdata *dd = pci_get_drvdata(pdev);
  590. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  591. switch (state) {
  592. case pci_channel_io_normal:
  593. qib_devinfo(pdev, "State Normal, ignoring\n");
  594. break;
  595. case pci_channel_io_frozen:
  596. qib_devinfo(pdev, "State Frozen, requesting reset\n");
  597. pci_disable_device(pdev);
  598. ret = PCI_ERS_RESULT_NEED_RESET;
  599. break;
  600. case pci_channel_io_perm_failure:
  601. qib_devinfo(pdev, "State Permanent Failure, disabling\n");
  602. if (dd) {
  603. /* no more register accesses! */
  604. dd->flags &= ~QIB_PRESENT;
  605. qib_disable_after_error(dd);
  606. }
  607. /* else early, or other problem */
  608. ret = PCI_ERS_RESULT_DISCONNECT;
  609. break;
  610. default: /* shouldn't happen */
  611. qib_devinfo(pdev, "QIB PCI errors detected (state %d)\n",
  612. state);
  613. break;
  614. }
  615. return ret;
  616. }
  617. static pci_ers_result_t
  618. qib_pci_mmio_enabled(struct pci_dev *pdev)
  619. {
  620. u64 words = 0U;
  621. struct qib_devdata *dd = pci_get_drvdata(pdev);
  622. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  623. if (dd && dd->pport) {
  624. words = dd->f_portcntr(dd->pport, QIBPORTCNTR_WORDRCV);
  625. if (words == ~0ULL)
  626. ret = PCI_ERS_RESULT_NEED_RESET;
  627. }
  628. qib_devinfo(pdev, "QIB mmio_enabled function called, "
  629. "read wordscntr %Lx, returning %d\n", words, ret);
  630. return ret;
  631. }
  632. static pci_ers_result_t
  633. qib_pci_slot_reset(struct pci_dev *pdev)
  634. {
  635. qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
  636. return PCI_ERS_RESULT_CAN_RECOVER;
  637. }
  638. static pci_ers_result_t
  639. qib_pci_link_reset(struct pci_dev *pdev)
  640. {
  641. qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
  642. return PCI_ERS_RESULT_CAN_RECOVER;
  643. }
  644. static void
  645. qib_pci_resume(struct pci_dev *pdev)
  646. {
  647. struct qib_devdata *dd = pci_get_drvdata(pdev);
  648. qib_devinfo(pdev, "QIB resume function called\n");
  649. pci_cleanup_aer_uncorrect_error_status(pdev);
  650. /*
  651. * Running jobs will fail, since it's asynchronous
  652. * unlike sysfs-requested reset. Better than
  653. * doing nothing.
  654. */
  655. qib_init(dd, 1); /* same as re-init after reset */
  656. }
  657. struct pci_error_handlers qib_pci_err_handler = {
  658. .error_detected = qib_pci_error_detected,
  659. .mmio_enabled = qib_pci_mmio_enabled,
  660. .link_reset = qib_pci_link_reset,
  661. .slot_reset = qib_pci_slot_reset,
  662. .resume = qib_pci_resume,
  663. };