2 * Copyright (c) 2008, 2009 QLogic Corporation. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/pci.h>
35 #include <linux/delay.h>
36 #include <linux/vmalloc.h>
37 #include <linux/aer.h>
42 * This file contains PCIe utility routines that are common to the
43 * various QLogic InfiniPath adapters
47 * Code to adjust PCIe capabilities.
48 * To minimize the change footprint, we call it
49 * from qib_pcie_params, which every chip-specific
50 * file calls, even though this violates some
51 * expectations of harmlessness.
53 static int qib_tune_pcie_caps(struct qib_devdata *);
54 static int qib_tune_pcie_coalesce(struct qib_devdata *);
57 * Do all the common PCIe setup and initialization.
58 * devdata is not yet allocated, and is not allocated until after this
59 * routine returns success. Therefore qib_dev_err() can't be used for error
62 int qib_pcie_init(struct pci_dev *pdev, const struct pci_device_id *ent)
66 ret = pci_enable_device(pdev);
69 * This can happen (in theory) iff:
70 * We did a chip reset, and then failed to reprogram the
71 * BAR, or the chip reset due to an internal error. We then
72 * unloaded the driver and reloaded it.
74 * Both reset cases set the BAR back to initial state. For
75 * the latter case, the AER sticky error bit at offset 0x718
76 * should be set, but the Linux kernel doesn't yet know
77 * about that, it appears. If the original BAR was retained
78 * in the kernel data structures, this may be OK.
80 qib_early_err(&pdev->dev, "pci enable failed: error %d\n",
85 ret = pci_request_regions(pdev, QIB_DRV_NAME);
87 qib_devinfo(pdev, "pci_request_regions fails: err %d\n", -ret);
91 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
94 * If the 64 bit setup fails, try 32 bit. Some systems
95 * do not setup 64 bit maps on systems with 2GB or less
98 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
100 qib_devinfo(pdev, "Unable to set DMA mask: %d\n", ret);
103 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
105 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
107 qib_early_err(&pdev->dev,
108 "Unable to set DMA consistent mask: %d\n", ret);
110 pci_set_master(pdev);
111 ret = pci_enable_pcie_error_reporting(pdev);
113 qib_early_err(&pdev->dev,
114 "Unable to enable pcie error reporting: %d\n",
119 pci_disable_device(pdev);
120 pci_release_regions(pdev);
126 * Do remaining PCIe setup, once dd is allocated, and save away
127 * fields required to re-initialize after a chip reset, or for
128 * various other purposes
130 int qib_pcie_ddinit(struct qib_devdata *dd, struct pci_dev *pdev,
131 const struct pci_device_id *ent)
134 resource_size_t addr;
137 pci_set_drvdata(pdev, dd);
139 addr = pci_resource_start(pdev, 0);
140 len = pci_resource_len(pdev, 0);
142 #if defined(__powerpc__)
143 /* There isn't a generic way to specify writethrough mappings */
144 dd->kregbase = __ioremap(addr, len, _PAGE_NO_CACHE | _PAGE_WRITETHRU);
146 dd->kregbase = ioremap_nocache(addr, len);
152 dd->kregend = (u64 __iomem *)((void __iomem *) dd->kregbase + len);
153 dd->physaddr = addr; /* used for io_remap, etc. */
156 * Save BARs to rewrite after device reset. Save all 64 bits of
160 dd->pcibar1 = addr >> 32;
161 dd->deviceid = ent->device; /* save for later use */
162 dd->vendorid = ent->vendor;
168 * Do PCIe cleanup, after chip-specific cleanup, etc. Just prior
169 * to releasing the dd memory.
170 * void because none of the core pcie cleanup returns are void
172 void qib_pcie_ddcleanup(struct qib_devdata *dd)
174 u64 __iomem *base = (void __iomem *) dd->kregbase;
179 iounmap(dd->piobase);
181 iounmap(dd->userbase);
183 pci_disable_device(dd->pcidev);
184 pci_release_regions(dd->pcidev);
186 pci_set_drvdata(dd->pcidev, NULL);
189 static void qib_msix_setup(struct qib_devdata *dd, int pos, u32 *msixcnt,
190 struct msix_entry *msix_entry)
196 pci_read_config_word(dd->pcidev, pos + PCI_MSIX_FLAGS, &msix_flags);
197 tabsize = 1 + (msix_flags & PCI_MSIX_FLAGS_QSIZE);
198 if (tabsize > *msixcnt)
200 ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
203 ret = pci_enable_msix(dd->pcidev, msix_entry, tabsize);
206 qib_dev_err(dd, "pci_enable_msix %d vectors failed: %d, "
207 "falling back to INTx\n", tabsize, ret);
213 qib_enable_intx(dd->pcidev);
218 * We save the msi lo and hi values, so we can restore them after
219 * chip reset (the kernel PCI infrastructure doesn't yet handle that
222 static int qib_msi_setup(struct qib_devdata *dd, int pos)
224 struct pci_dev *pdev = dd->pcidev;
228 ret = pci_enable_msi(pdev);
230 qib_dev_err(dd, "pci_enable_msi failed: %d, "
231 "interrupts may not work\n", ret);
232 /* continue even if it fails, we may still be OK... */
234 pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_LO,
236 pci_read_config_dword(pdev, pos + PCI_MSI_ADDRESS_HI,
238 pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &control);
239 /* now save the data (vector) info */
240 pci_read_config_word(pdev, pos + ((control & PCI_MSI_FLAGS_64BIT)
246 int qib_pcie_params(struct qib_devdata *dd, u32 minw, u32 *nent,
247 struct msix_entry *entry)
250 int pos = 0, pose, ret = 1;
252 pose = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
254 qib_dev_err(dd, "Can't find PCI Express capability!\n");
255 /* set up something... */
257 dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
261 pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSIX);
262 if (nent && *nent && pos) {
263 qib_msix_setup(dd, pos, nent, entry);
264 ret = 0; /* did it, either MSIx or INTx */
266 pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
268 ret = qib_msi_setup(dd, pos);
270 qib_dev_err(dd, "No PCI MSI or MSIx capability!\n");
273 qib_enable_intx(dd->pcidev);
275 pci_read_config_word(dd->pcidev, pose + PCI_EXP_LNKSTA, &linkstat);
277 * speed is bits 0-3, linkwidth is bits 4-8
278 * no defines for them in headers
280 speed = linkstat & 0xf;
283 dd->lbus_width = linkstat;
287 dd->lbus_speed = 2500; /* Gen1, 2.5GHz */
290 dd->lbus_speed = 5000; /* Gen1, 5GHz */
292 default: /* not defined, assume gen1 */
293 dd->lbus_speed = 2500;
298 * Check against expected pcie width and complain if "wrong"
299 * on first initialization, not afterwards (i.e., reset).
301 if (minw && linkstat < minw)
303 "PCIe width %u (x%u HCA), performance reduced\n",
306 qib_tune_pcie_caps(dd);
308 qib_tune_pcie_coalesce(dd);
311 /* fill in string, even on errors */
312 snprintf(dd->lbus_info, sizeof(dd->lbus_info),
313 "PCIe,%uMHz,x%u\n", dd->lbus_speed, dd->lbus_width);
318 * Setup pcie interrupt stuff again after a reset. I'd like to just call
319 * pci_enable_msi() again for msi, but when I do that,
320 * the MSI enable bit doesn't get set in the command word, and
321 * we switch to to a different interrupt vector, which is confusing,
322 * so I instead just do it all inline. Perhaps somehow can tie this
323 * into the PCIe hotplug support at some point
325 int qib_reinit_intr(struct qib_devdata *dd)
331 /* If we aren't using MSI, don't restore it */
335 pos = pci_find_capability(dd->pcidev, PCI_CAP_ID_MSI);
337 qib_dev_err(dd, "Can't find MSI capability, "
338 "can't restore MSI settings\n");
340 /* nothing special for MSIx, just MSI */
343 pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_LO,
345 pci_write_config_dword(dd->pcidev, pos + PCI_MSI_ADDRESS_HI,
347 pci_read_config_word(dd->pcidev, pos + PCI_MSI_FLAGS, &control);
348 if (!(control & PCI_MSI_FLAGS_ENABLE)) {
349 control |= PCI_MSI_FLAGS_ENABLE;
350 pci_write_config_word(dd->pcidev, pos + PCI_MSI_FLAGS,
353 /* now rewrite the data (vector) info */
354 pci_write_config_word(dd->pcidev, pos +
355 ((control & PCI_MSI_FLAGS_64BIT) ? 12 : 8),
359 if (!ret && (dd->flags & QIB_HAS_INTX)) {
360 qib_enable_intx(dd->pcidev);
364 /* and now set the pci master bit again */
365 pci_set_master(dd->pcidev);
371 * Disable msi interrupt if enabled, and clear msi_lo.
372 * This is used primarily for the fallback to INTx, but
373 * is also used in reinit after reset, and during cleanup.
375 void qib_nomsi(struct qib_devdata *dd)
378 pci_disable_msi(dd->pcidev);
382 * Same as qib_nosmi, but for MSIx.
384 void qib_nomsix(struct qib_devdata *dd)
386 pci_disable_msix(dd->pcidev);
390 * Similar to pci_intx(pdev, 1), except that we make sure
393 void qib_enable_intx(struct pci_dev *pdev)
398 /* first, turn on INTx */
399 pci_read_config_word(pdev, PCI_COMMAND, &cw);
400 new = cw & ~PCI_COMMAND_INTX_DISABLE;
402 pci_write_config_word(pdev, PCI_COMMAND, new);
404 pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
406 /* then turn off MSI */
407 pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &cw);
408 new = cw & ~PCI_MSI_FLAGS_ENABLE;
410 pci_write_config_word(pdev, pos + PCI_MSI_FLAGS, new);
412 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
414 /* then turn off MSIx */
415 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &cw);
416 new = cw & ~PCI_MSIX_FLAGS_ENABLE;
418 pci_write_config_word(pdev, pos + PCI_MSIX_FLAGS, new);
423 * These two routines are helper routines for the device reset code
424 * to move all the pcie code out of the chip-specific driver code.
426 void qib_pcie_getcmd(struct qib_devdata *dd, u16 *cmd, u8 *iline, u8 *cline)
428 pci_read_config_word(dd->pcidev, PCI_COMMAND, cmd);
429 pci_read_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
430 pci_read_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
433 void qib_pcie_reenable(struct qib_devdata *dd, u16 cmd, u8 iline, u8 cline)
436 r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_0,
439 qib_dev_err(dd, "rewrite of BAR0 failed: %d\n", r);
440 r = pci_write_config_dword(dd->pcidev, PCI_BASE_ADDRESS_1,
443 qib_dev_err(dd, "rewrite of BAR1 failed: %d\n", r);
444 /* now re-enable memory access, and restore cosmetic settings */
445 pci_write_config_word(dd->pcidev, PCI_COMMAND, cmd);
446 pci_write_config_byte(dd->pcidev, PCI_INTERRUPT_LINE, iline);
447 pci_write_config_byte(dd->pcidev, PCI_CACHE_LINE_SIZE, cline);
448 r = pci_enable_device(dd->pcidev);
450 qib_dev_err(dd, "pci_enable_device failed after "
454 /* code to adjust PCIe capabilities. */
456 static int fld2val(int wd, int mask)
463 lsbmask = mask ^ (mask & (mask - 1));
468 static int val2fld(int wd, int mask)
474 lsbmask = mask ^ (mask & (mask - 1));
479 static int qib_pcie_coalesce;
480 module_param_named(pcie_coalesce, qib_pcie_coalesce, int, S_IRUGO);
481 MODULE_PARM_DESC(pcie_coalesce, "tune PCIe colescing on some Intel chipsets");
484 * Enable PCIe completion and data coalescing, on Intel 5x00 and 7300
485 * chipsets. This is known to be unsafe for some revisions of some
486 * of these chipsets, with some BIOS settings, and enabling it on those
487 * systems may result in the system crashing, and/or data corruption.
489 static int qib_tune_pcie_coalesce(struct qib_devdata *dd)
492 struct pci_dev *parent;
497 if (!qib_pcie_coalesce)
500 /* Find out supported and configured values for parent (root) */
501 parent = dd->pcidev->bus->self;
502 if (parent->bus->parent) {
503 qib_devinfo(dd->pcidev, "Parent not root\n");
506 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
509 if (parent->vendor != 0x8086)
513 * - bit 12: Max_rdcmp_Imt_EN: need to set to 1
514 * - bit 11: COALESCE_FORCE: need to set to 0
515 * - bit 10: COALESCE_EN: need to set to 1
516 * (but limitations on some on some chipsets)
518 * On the Intel 5000, 5100, and 7300 chipsets, there is
519 * also: - bit 25:24: COALESCE_MODE, need to set to 0
521 devid = parent->device;
522 if (devid >= 0x25e2 && devid <= 0x25fa) {
526 pci_read_config_byte(parent, PCI_REVISION_ID, &rev);
531 mask = (3U << 24) | (7U << 10);
532 } else if (devid >= 0x65e2 && devid <= 0x65fa) {
535 mask = (3U << 24) | (7U << 10);
536 } else if (devid >= 0x4021 && devid <= 0x402e) {
540 } else if (devid >= 0x3604 && devid <= 0x360a) {
543 mask = (3U << 24) | (7U << 10);
545 /* not one of the chipsets that we know about */
548 pci_read_config_dword(parent, 0x48, &val);
551 r = pci_write_config_dword(parent, 0x48, val);
556 * BIOS may not set PCIe bus-utilization parameters for best performance.
557 * Check and optionally adjust them to maximize our throughput.
559 static int qib_pcie_caps;
560 module_param_named(pcie_caps, qib_pcie_caps, int, S_IRUGO);
561 MODULE_PARM_DESC(pcie_caps, "Max PCIe tuning: Payload (4lsb), ReadReq (D4..7)");
563 static int qib_tune_pcie_caps(struct qib_devdata *dd)
565 int ret = 1; /* Assume the worst */
566 struct pci_dev *parent;
568 u16 pcaps, pctl, ecaps, ectl;
572 /* Find out supported and configured values for parent (root) */
573 parent = dd->pcidev->bus->self;
574 if (parent->bus->parent) {
575 qib_devinfo(dd->pcidev, "Parent not root\n");
578 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
580 pci_read_config_word(parent, ppos + PCI_EXP_DEVCAP, &pcaps);
581 pci_read_config_word(parent, ppos + PCI_EXP_DEVCTL, &pctl);
584 /* Find out supported and configured values for endpoint (us) */
585 epos = pci_find_capability(dd->pcidev, PCI_CAP_ID_EXP);
587 pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCAP, &ecaps);
588 pci_read_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, &ectl);
592 /* Find max payload supported by root, endpoint */
593 rc_sup = fld2val(pcaps, PCI_EXP_DEVCAP_PAYLOAD);
594 ep_sup = fld2val(ecaps, PCI_EXP_DEVCAP_PAYLOAD);
598 rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_PAYLOAD);
599 ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_PAYLOAD);
601 /* If Supported greater than limit in module param, limit it */
602 if (rc_sup > (qib_pcie_caps & 7))
603 rc_sup = qib_pcie_caps & 7;
604 /* If less than (allowed, supported), bump root payload */
605 if (rc_sup > rc_cur) {
607 pctl = (pctl & ~PCI_EXP_DEVCTL_PAYLOAD) |
608 val2fld(rc_cur, PCI_EXP_DEVCTL_PAYLOAD);
609 pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
611 /* If less than (allowed, supported), bump endpoint payload */
612 if (rc_sup > ep_cur) {
614 ectl = (ectl & ~PCI_EXP_DEVCTL_PAYLOAD) |
615 val2fld(ep_cur, PCI_EXP_DEVCTL_PAYLOAD);
616 pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
620 * Now the Read Request size.
621 * No field for max supported, but PCIe spec limits it to 4096,
622 * which is code '5' (log2(4096) - 7)
625 if (rc_sup > ((qib_pcie_caps >> 4) & 7))
626 rc_sup = (qib_pcie_caps >> 4) & 7;
627 rc_cur = fld2val(pctl, PCI_EXP_DEVCTL_READRQ);
628 ep_cur = fld2val(ectl, PCI_EXP_DEVCTL_READRQ);
630 if (rc_sup > rc_cur) {
632 pctl = (pctl & ~PCI_EXP_DEVCTL_READRQ) |
633 val2fld(rc_cur, PCI_EXP_DEVCTL_READRQ);
634 pci_write_config_word(parent, ppos + PCI_EXP_DEVCTL, pctl);
636 if (rc_sup > ep_cur) {
638 ectl = (ectl & ~PCI_EXP_DEVCTL_READRQ) |
639 val2fld(ep_cur, PCI_EXP_DEVCTL_READRQ);
640 pci_write_config_word(dd->pcidev, epos + PCI_EXP_DEVCTL, ectl);
645 /* End of PCIe capability tuning */
648 * From here through qib_pci_err_handler definition is invoked via
649 * PCI error infrastructure, registered via pci
651 static pci_ers_result_t
652 qib_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
654 struct qib_devdata *dd = pci_get_drvdata(pdev);
655 pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
658 case pci_channel_io_normal:
659 qib_devinfo(pdev, "State Normal, ignoring\n");
662 case pci_channel_io_frozen:
663 qib_devinfo(pdev, "State Frozen, requesting reset\n");
664 pci_disable_device(pdev);
665 ret = PCI_ERS_RESULT_NEED_RESET;
668 case pci_channel_io_perm_failure:
669 qib_devinfo(pdev, "State Permanent Failure, disabling\n");
671 /* no more register accesses! */
672 dd->flags &= ~QIB_PRESENT;
673 qib_disable_after_error(dd);
675 /* else early, or other problem */
676 ret = PCI_ERS_RESULT_DISCONNECT;
679 default: /* shouldn't happen */
680 qib_devinfo(pdev, "QIB PCI errors detected (state %d)\n",
687 static pci_ers_result_t
688 qib_pci_mmio_enabled(struct pci_dev *pdev)
691 struct qib_devdata *dd = pci_get_drvdata(pdev);
692 pci_ers_result_t ret = PCI_ERS_RESULT_RECOVERED;
694 if (dd && dd->pport) {
695 words = dd->f_portcntr(dd->pport, QIBPORTCNTR_WORDRCV);
697 ret = PCI_ERS_RESULT_NEED_RESET;
699 qib_devinfo(pdev, "QIB mmio_enabled function called, "
700 "read wordscntr %Lx, returning %d\n", words, ret);
704 static pci_ers_result_t
705 qib_pci_slot_reset(struct pci_dev *pdev)
707 qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
708 return PCI_ERS_RESULT_CAN_RECOVER;
711 static pci_ers_result_t
712 qib_pci_link_reset(struct pci_dev *pdev)
714 qib_devinfo(pdev, "QIB link_reset function called, ignored\n");
715 return PCI_ERS_RESULT_CAN_RECOVER;
719 qib_pci_resume(struct pci_dev *pdev)
721 struct qib_devdata *dd = pci_get_drvdata(pdev);
722 qib_devinfo(pdev, "QIB resume function called\n");
723 pci_cleanup_aer_uncorrect_error_status(pdev);
725 * Running jobs will fail, since it's asynchronous
726 * unlike sysfs-requested reset. Better than
729 qib_init(dd, 1); /* same as re-init after reset */
732 struct pci_error_handlers qib_pci_err_handler = {
733 .error_detected = qib_pci_error_detected,
734 .mmio_enabled = qib_pci_mmio_enabled,
735 .link_reset = qib_pci_link_reset,
736 .slot_reset = qib_pci_slot_reset,
737 .resume = qib_pci_resume,