qib_pcie.c 20 KB

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