qib_pcie.c 18 KB

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