qib_pcie.c 19 KB

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