qib_pcie.c 19 KB

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