qib_pcie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. pci_set_master(pdev);
  102. ret = pci_enable_pcie_error_reporting(pdev);
  103. if (ret)
  104. qib_early_err(&pdev->dev,
  105. "Unable to enable pcie error reporting: %d\n",
  106. ret);
  107. goto done;
  108. bail:
  109. pci_disable_device(pdev);
  110. pci_release_regions(pdev);
  111. done:
  112. return ret;
  113. }
  114. /*
  115. * Do remaining PCIe setup, once dd is allocated, and save away
  116. * fields required to re-initialize after a chip reset, or for
  117. * various other purposes
  118. */
  119. int qib_pcie_ddinit(struct qib_devdata *dd, struct pci_dev *pdev,
  120. const struct pci_device_id *ent)
  121. {
  122. unsigned long len;
  123. resource_size_t addr;
  124. dd->pcidev = pdev;
  125. pci_set_drvdata(pdev, dd);
  126. addr = pci_resource_start(pdev, 0);
  127. len = pci_resource_len(pdev, 0);
  128. #if defined(__powerpc__)
  129. /* There isn't a generic way to specify writethrough mappings */
  130. dd->kregbase = __ioremap(addr, len, _PAGE_NO_CACHE | _PAGE_WRITETHRU);
  131. #else
  132. dd->kregbase = ioremap_nocache(addr, len);
  133. #endif
  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 msix_entry *msix_entry)
  170. {
  171. int ret;
  172. u32 tabsize = 0;
  173. u16 msix_flags;
  174. pci_read_config_word(dd->pcidev, pos + PCI_MSIX_FLAGS, &msix_flags);
  175. tabsize = 1 + (msix_flags & PCI_MSIX_FLAGS_QSIZE);
  176. if (tabsize > *msixcnt)
  177. tabsize = *msixcnt;
  178. ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
  179. if (ret > 0) {
  180. tabsize = ret;
  181. ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
  182. }
  183. if (ret) {
  184. qib_dev_err(dd, "pci_enable_msix %d vectors failed: %d, "
  185. "falling back to INTx\n", tabsize, ret);
  186. tabsize = 0;
  187. }
  188. *msixcnt = tabsize;
  189. if (ret)
  190. qib_enable_intx(dd->pcidev);
  191. }
  192. /**
  193. * We save the msi lo and hi values, so we can restore them after
  194. * chip reset (the kernel PCI infrastructure doesn't yet handle that
  195. * correctly.
  196. */
  197. static int qib_msi_setup(struct qib_devdata *dd, int pos)
  198. {
  199. struct pci_dev *pdev = dd->pcidev;
  200. u16 control;
  201. int ret;
  202. ret = pci_enable_msi(pdev);
  203. if (ret)
  204. qib_dev_err(dd, "pci_enable_msi failed: %d, "
  205. "interrupts may not work\n", ret);
  206. /* continue even if it fails, we may still be OK... */
  207. pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_LO,
  208. &dd->msi_lo);
  209. pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_HI,
  210. &dd->msi_hi);
  211. pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &control);
  212. /* now save the data (vector) info */
  213. pci_read_config_word(pdev, pos + ((control & PCI_MSI_FLAGS_64BIT)
  214. ? 12 : 8),
  215. &dd->msi_data);
  216. return ret;
  217. }
  218. int qib_pcie_params(struct qib_devdata *dd, u32 minw, u32 *nent,
  219. struct msix_entry *entry)
  220. {
  221. u16 linkstat, speed;
  222. int pos = 0, pose, ret = 1;
  223. pose = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
  224. if (!pose) {
  225. qib_dev_err(dd, "Can't find PCI Express capability!\n");
  226. /* set up something... */
  227. dd->lbus_width = 1;
  228. dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
  229. goto bail;
  230. }
  231. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSIX);
  232. if (nent && *nent && pos) {
  233. qib_msix_setup(dd, pos, nent, entry);
  234. ret = 0; /* did it, either MSIx or INTx */
  235. } else {
  236. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
  237. if (pos)
  238. ret = qib_msi_setup(dd, pos);
  239. else
  240. qib_dev_err(dd, "No PCI MSI or MSIx capability!\n");
  241. }
  242. if (!pos)
  243. qib_enable_intx(dd->pcidev);
  244. pci_read_config_word(dd->pcidev, pose + PCI_EXP_LNKSTA, &linkstat);
  245. /*
  246. * speed is bits 0-3, linkwidth is bits 4-8
  247. * no defines for them in headers
  248. */
  249. speed = linkstat & 0xf;
  250. linkstat >>= 4;
  251. linkstat &= 0x1f;
  252. dd->lbus_width = linkstat;
  253. switch (speed) {
  254. case 1:
  255. dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
  256. break;
  257. case 2:
  258. dd->lbus_speed = 5000; /* Gen1, 5GHz */
  259. break;
  260. default: /* not defined, assume gen1 */
  261. dd->lbus_speed = 2500;
  262. break;
  263. }
  264. /*
  265. * Check against expected pcie width and complain if "wrong"
  266. * on first initialization, not afterwards (i.e., reset).
  267. */
  268. if (minw && linkstat < minw)
  269. qib_dev_err(dd,
  270. "PCIe width %u (x%u HCA), performance reduced\n",
  271. linkstat, minw);
  272. qib_tune_pcie_caps(dd);
  273. qib_tune_pcie_coalesce(dd);
  274. bail:
  275. /* fill in string, even on errors */
  276. snprintf(dd->lbus_info, sizeof(dd->lbus_info),
  277. "PCIe,%uMHz,x%u\n", dd->lbus_speed, dd->lbus_width);
  278. return ret;
  279. }
  280. /*
  281. * Setup pcie interrupt stuff again after a reset. I'd like to just call
  282. * pci_enable_msi() again for msi, but when I do that,
  283. * the MSI enable bit doesn't get set in the command word, and
  284. * we switch to to a different interrupt vector, which is confusing,
  285. * so I instead just do it all inline. Perhaps somehow can tie this
  286. * into the PCIe hotplug support at some point
  287. */
  288. int qib_reinit_intr(struct qib_devdata *dd)
  289. {
  290. int pos;
  291. u16 control;
  292. int ret = 0;
  293. /* If we aren't using MSI, don't restore it */
  294. if (!dd->msi_lo)
  295. goto bail;
  296. pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
  297. if (!pos) {
  298. qib_dev_err(dd, "Can't find MSI capability, "
  299. "can't restore MSI settings\n");
  300. ret = 0;
  301. /* nothing special for MSIx, just MSI */
  302. goto bail;
  303. }
  304. pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_LO,
  305. dd->msi_lo);
  306. pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_HI,
  307. dd->msi_hi);
  308. pci_read_config_word(dd->pcidev, pos + PCI_MSI_FLAGS, &control);
  309. if (!(control & PCI_MSI_FLAGS_ENABLE)) {
  310. control |= PCI_MSI_FLAGS_ENABLE;
  311. pci_write_config_word(dd->pcidev, pos + PCI_MSI_FLAGS,
  312. control);
  313. }
  314. /* now rewrite the data (vector) info */
  315. pci_write_config_word(dd->pcidev, pos +
  316. ((control & PCI_MSI_FLAGS_64BIT) ? 12 : 8),
  317. dd->msi_data);
  318. ret = 1;
  319. bail:
  320. if (!ret && (dd->flags & QIB_HAS_INTX)) {
  321. qib_enable_intx(dd->pcidev);
  322. ret = 1;
  323. }
  324. /* and now set the pci master bit again */
  325. pci_set_master(dd->pcidev);
  326. return ret;
  327. }
  328. /*
  329. * Disable msi interrupt if enabled, and clear msi_lo.
  330. * This is used primarily for the fallback to INTx, but
  331. * is also used in reinit after reset, and during cleanup.
  332. */
  333. void qib_nomsi(struct qib_devdata *dd)
  334. {
  335. dd->msi_lo = 0;
  336. pci_disable_msi(dd->pcidev);
  337. }
  338. /*
  339. * Same as qib_nosmi, but for MSIx.
  340. */
  341. void qib_nomsix(struct qib_devdata *dd)
  342. {
  343. pci_disable_msix(dd->pcidev);
  344. }
  345. /*
  346. * Similar to pci_intx(pdev, 1), except that we make sure
  347. * msi(x) is off.
  348. */
  349. void qib_enable_intx(struct pci_dev *pdev)
  350. {
  351. u16 cw, new;
  352. int pos;
  353. /* first, turn on INTx */
  354. pci_read_config_word(pdev, PCI_COMMAND, &cw);
  355. new = cw & ~PCI_COMMAND_INTX_DISABLE;
  356. if (new != cw)
  357. pci_write_config_word(pdev, PCI_COMMAND, new);
  358. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  359. if (pos) {
  360. /* then turn off MSI */
  361. pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &cw);
  362. new = cw & ~PCI_MSI_FLAGS_ENABLE;
  363. if (new != cw)
  364. pci_write_config_word(pdev, pos + PCI_MSI_FLAGS, new);
  365. }
  366. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  367. if (pos) {
  368. /* then turn off MSIx */
  369. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &cw);
  370. new = cw & ~PCI_MSIX_FLAGS_ENABLE;
  371. if (new != cw)
  372. pci_write_config_word(pdev, pos + PCI_MSIX_FLAGS, new);
  373. }
  374. }
  375. /*
  376. * These two routines are helper routines for the device reset code
  377. * to move all the pcie code out of the chip-specific driver code.
  378. */
  379. void qib_pcie_getcmd(struct qib_devdata *dd, u16 *cmd, u8 *iline, u8 *cline)
  380. {
  381. pci_read_config_word(dd->pcidev, PCI_COMMAND, cmd);
  382. pci_read_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
  383. pci_read_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
  384. }
  385. void qib_pcie_reenable(struct qib_devdata *dd, u16 cmd, u8 iline, u8 cline)
  386. {
  387. int r;
  388. r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
  389. dd->pcibar0);
  390. if (r)
  391. qib_dev_err(dd, "rewrite of BAR0 failed: %d\n", r);
  392. r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
  393. dd->pcibar1);
  394. if (r)
  395. qib_dev_err(dd, "rewrite of BAR1 failed: %d\n", r);
  396. /* now re-enable memory access, and restore cosmetic settings */
  397. pci_write_config_word(dd->pcidev, PCI_COMMAND, cmd);
  398. pci_write_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
  399. pci_write_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
  400. r = pci_enable_device(dd->pcidev);
  401. if (r)
  402. qib_dev_err(dd, "pci_enable_device failed after "
  403. "reset: %d\n", r);
  404. }
  405. /* code to adjust PCIe capabilities. */
  406. static int fld2val(int wd, int mask)
  407. {
  408. int lsbmask;
  409. if (!mask)
  410. return 0;
  411. wd &= mask;
  412. lsbmask = mask ^ (mask & (mask - 1));
  413. wd /= lsbmask;
  414. return wd;
  415. }
  416. static int val2fld(int wd, int mask)
  417. {
  418. int lsbmask;
  419. if (!mask)
  420. return 0;
  421. lsbmask = mask ^ (mask & (mask - 1));
  422. wd *= lsbmask;
  423. return wd;
  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 int qib_tune_pcie_coalesce(struct qib_devdata *dd)
  435. {
  436. int r;
  437. struct pci_dev *parent;
  438. int ppos;
  439. u16 devid;
  440. u32 mask, bits, val;
  441. if (!qib_pcie_coalesce)
  442. return 0;
  443. /* Find out supported and configured values for parent (root) */
  444. parent = dd->pcidev->bus->self;
  445. if (parent->bus->parent) {
  446. qib_devinfo(dd->pcidev, "Parent not root\n");
  447. return 1;
  448. }
  449. ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
  450. if (!ppos)
  451. return 1;
  452. if (parent->vendor != 0x8086)
  453. return 1;
  454. /*
  455. * - bit 12: Max_rdcmp_Imt_EN: need to set to 1
  456. * - bit 11: COALESCE_FORCE: need to set to 0
  457. * - bit 10: COALESCE_EN: need to set to 1
  458. * (but limitations on some on some chipsets)
  459. *
  460. * On the Intel 5000, 5100, and 7300 chipsets, there is
  461. * also: - bit 25:24: COALESCE_MODE, need to set to 0
  462. */
  463. devid = parent->device;
  464. if (devid >= 0x25e2 && devid <= 0x25fa) {
  465. u8 rev;
  466. /* 5000 P/V/X/Z */
  467. pci_read_config_byte(parent, PCI_REVISION_ID, &rev);
  468. if (rev <= 0xb2)
  469. bits = 1U << 10;
  470. else
  471. bits = 7U << 10;
  472. mask = (3U << 24) | (7U << 10);
  473. } else if (devid >= 0x65e2 && devid <= 0x65fa) {
  474. /* 5100 */
  475. bits = 1U << 10;
  476. mask = (3U << 24) | (7U << 10);
  477. } else if (devid >= 0x4021 && devid <= 0x402e) {
  478. /* 5400 */
  479. bits = 7U << 10;
  480. mask = 7U << 10;
  481. } else if (devid >= 0x3604 && devid <= 0x360a) {
  482. /* 7300 */
  483. bits = 7U << 10;
  484. mask = (3U << 24) | (7U << 10);
  485. } else {
  486. /* not one of the chipsets that we know about */
  487. return 1;
  488. }
  489. pci_read_config_dword(parent, 0x48, &val);
  490. val &= ~mask;
  491. val |= bits;
  492. r = pci_write_config_dword(parent, 0x48, val);
  493. return 0;
  494. }
  495. /*
  496. * BIOS may not set PCIe bus-utilization parameters for best performance.
  497. * Check and optionally adjust them to maximize our throughput.
  498. */
  499. static int qib_pcie_caps;
  500. module_param_named(pcie_caps, qib_pcie_caps, int, S_IRUGO);
  501. MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (4lsb), ReadReq (D4..7)");
  502. static int qib_tune_pcie_caps(struct qib_devdata *dd)
  503. {
  504. int ret = 1; /* Assume the worst */
  505. struct pci_dev *parent;
  506. int ppos, epos;
  507. u16 pcaps, pctl, ecaps, ectl;
  508. int rc_sup, ep_sup;
  509. int rc_cur, ep_cur;
  510. /* Find out supported and configured values for parent (root) */
  511. parent = dd->pcidev->bus->self;
  512. if (parent->bus->parent) {
  513. qib_devinfo(dd->pcidev, "Parent not root\n");
  514. goto bail;
  515. }
  516. ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
  517. if (ppos) {
  518. pci_read_config_word(parent, ppos + PCI_EXP_DEVCAP, &pcaps);
  519. pci_read_config_word(parent, ppos + PCI_EXP_DEVCTL, &pctl);
  520. } else
  521. goto bail;
  522. /* Find out supported and configured values for endpoint (us) */
  523. epos = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
  524. if (epos) {
  525. pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCAP, &ecaps);
  526. pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, &ectl);
  527. } else
  528. goto bail;
  529. ret = 0;
  530. /* Find max payload supported by root, endpoint */
  531. rc_sup = fld2val(pcaps, PCI_EXP_DEVCAP_PAYLOAD);
  532. ep_sup = fld2val(ecaps, PCI_EXP_DEVCAP_PAYLOAD);
  533. if (rc_sup > ep_sup)
  534. rc_sup = ep_sup;
  535. rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_PAYLOAD);
  536. ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_PAYLOAD);
  537. /* If Supported greater than limit in module param, limit it */
  538. if (rc_sup > (qib_pcie_caps & 7))
  539. rc_sup = qib_pcie_caps & 7;
  540. /* If less than (allowed, supported), bump root payload */
  541. if (rc_sup > rc_cur) {
  542. rc_cur = rc_sup;
  543. pctl = (pctl & ~PCI_EXP_DEVCTL_PAYLOAD) |
  544. val2fld(rc_cur, PCI_EXP_DEVCTL_PAYLOAD);
  545. pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
  546. }
  547. /* If less than (allowed, supported), bump endpoint payload */
  548. if (rc_sup > ep_cur) {
  549. ep_cur = rc_sup;
  550. ectl = (ectl & ~PCI_EXP_DEVCTL_PAYLOAD) |
  551. val2fld(ep_cur, PCI_EXP_DEVCTL_PAYLOAD);
  552. pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
  553. }
  554. /*
  555. * Now the Read Request size.
  556. * No field for max supported, but PCIe spec limits it to 4096,
  557. * which is code '5' (log2(4096) - 7)
  558. */
  559. rc_sup = 5;
  560. if (rc_sup > ((qib_pcie_caps >> 4) & 7))
  561. rc_sup = (qib_pcie_caps >> 4) & 7;
  562. rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_READRQ);
  563. ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_READRQ);
  564. if (rc_sup > rc_cur) {
  565. rc_cur = rc_sup;
  566. pctl = (pctl & ~PCI_EXP_DEVCTL_READRQ) |
  567. val2fld(rc_cur, PCI_EXP_DEVCTL_READRQ);
  568. pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
  569. }
  570. if (rc_sup > ep_cur) {
  571. ep_cur = rc_sup;
  572. ectl = (ectl & ~PCI_EXP_DEVCTL_READRQ) |
  573. val2fld(ep_cur, PCI_EXP_DEVCTL_READRQ);
  574. pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
  575. }
  576. bail:
  577. return ret;
  578. }
  579. /* End of PCIe capability tuning */
  580. /*
  581. * From here through qib_pci_err_handler definition is invoked via
  582. * PCI error infrastructure, registered via pci
  583. */
  584. static pci_ers_result_t
  585. qib_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  586. {
  587. struct qib_devdata *dd = pci_get_drvdata(pdev);
  588. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  589. switch (state) {
  590. case pci_channel_io_normal:
  591. qib_devinfo(pdev, "State Normal, ignoring\n");
  592. break;
  593. case pci_channel_io_frozen:
  594. qib_devinfo(pdev, "State Frozen, requesting reset\n");
  595. pci_disable_device(pdev);
  596. ret = PCI_ERS_RESULT_NEED_RESET;
  597. break;
  598. case pci_channel_io_perm_failure:
  599. qib_devinfo(pdev, "State Permanent Failure, disabling\n");
  600. if (dd) {
  601. /* no more register accesses! */
  602. dd->flags &= ~QIB_PRESENT;
  603. qib_disable_after_error(dd);
  604. }
  605. /* else early, or other problem */
  606. ret = PCI_ERS_RESULT_DISCONNECT;
  607. break;
  608. default: /* shouldn't happen */
  609. qib_devinfo(pdev, "QIB PCI errors detected (state %d)\n",
  610. state);
  611. break;
  612. }
  613. return ret;
  614. }
  615. static pci_ers_result_t
  616. qib_pci_mmio_enabled(struct pci_dev *pdev)
  617. {
  618. u64 words = 0U;
  619. struct qib_devdata *dd = pci_get_drvdata(pdev);
  620. pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
  621. if (dd && dd->pport) {
  622. words = dd->f_portcntr(dd->pport, QIBPORTCNTR_WORDRCV);
  623. if (words == ~0ULL)
  624. ret = PCI_ERS_RESULT_NEED_RESET;
  625. }
  626. qib_devinfo(pdev, "QIB mmio_enabled function called, "
  627. "read wordscntr %Lx, returning %d\n", words, ret);
  628. return ret;
  629. }
  630. static pci_ers_result_t
  631. qib_pci_slot_reset(struct pci_dev *pdev)
  632. {
  633. qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
  634. return PCI_ERS_RESULT_CAN_RECOVER;
  635. }
  636. static pci_ers_result_t
  637. qib_pci_link_reset(struct pci_dev *pdev)
  638. {
  639. qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
  640. return PCI_ERS_RESULT_CAN_RECOVER;
  641. }
  642. static void
  643. qib_pci_resume(struct pci_dev *pdev)
  644. {
  645. struct qib_devdata *dd = pci_get_drvdata(pdev);
  646. qib_devinfo(pdev, "QIB resume function called\n");
  647. pci_cleanup_aer_uncorrect_error_status(pdev);
  648. /*
  649. * Running jobs will fail, since it's asynchronous
  650. * unlike sysfs-requested reset. Better than
  651. * doing nothing.
  652. */
  653. qib_init(dd, 1); /* same as re-init after reset */
  654. }
  655. struct pci_error_handlers qib_pci_err_handler = {
  656. .error_detected = qib_pci_error_detected,
  657. .mmio_enabled = qib_pci_mmio_enabled,
  658. .link_reset = qib_pci_link_reset,
  659. .slot_reset = qib_pci_slot_reset,
  660. .resume = qib_pci_resume,
  661. };