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>
38 #include <linux/module.h>
43 * This file contains PCIe utility routines that are common to the
44 * various QLogic InfiniPath adapters
48 * Code to adjust PCIe capabilities.
49 * To minimize the change footprint, we call it
50 * from qib_pcie_params, which every chip-specific
51 * file calls, even though this violates some
52 * expectations of harmlessness.
54 static void qib_tune_pcie_caps(struct qib_devdata
*);
55 static void qib_tune_pcie_coalesce(struct qib_devdata
*);
58 * Do all the common PCIe setup and initialization.
59 * devdata is not yet allocated, and is not allocated until after this
60 * routine returns success. Therefore qib_dev_err() can't be used for error
63 int qib_pcie_init(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
67 ret
= pci_enable_device(pdev
);
70 * This can happen (in theory) iff:
71 * We did a chip reset, and then failed to reprogram the
72 * BAR, or the chip reset due to an internal error. We then
73 * unloaded the driver and reloaded it.
75 * Both reset cases set the BAR back to initial state. For
76 * the latter case, the AER sticky error bit at offset 0x718
77 * should be set, but the Linux kernel doesn't yet know
78 * about that, it appears. If the original BAR was retained
79 * in the kernel data structures, this may be OK.
81 qib_early_err(&pdev
->dev
, "pci enable failed: error %d\n",
86 ret
= pci_request_regions(pdev
, QIB_DRV_NAME
);
88 qib_devinfo(pdev
, "pci_request_regions fails: err %d\n", -ret
);
92 ret
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
95 * If the 64 bit setup fails, try 32 bit. Some systems
96 * do not setup 64 bit maps on systems with 2GB or less
99 ret
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
101 qib_devinfo(pdev
, "Unable to set DMA mask: %d\n", ret
);
104 ret
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
106 ret
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
108 qib_early_err(&pdev
->dev
,
109 "Unable to set DMA consistent mask: %d\n", ret
);
113 pci_set_master(pdev
);
114 ret
= pci_enable_pcie_error_reporting(pdev
);
116 qib_early_err(&pdev
->dev
,
117 "Unable to enable pcie error reporting: %d\n",
124 pci_disable_device(pdev
);
125 pci_release_regions(pdev
);
131 * Do remaining PCIe setup, once dd is allocated, and save away
132 * fields required to re-initialize after a chip reset, or for
133 * various other purposes
135 int qib_pcie_ddinit(struct qib_devdata
*dd
, struct pci_dev
*pdev
,
136 const struct pci_device_id
*ent
)
139 resource_size_t addr
;
142 pci_set_drvdata(pdev
, dd
);
144 addr
= pci_resource_start(pdev
, 0);
145 len
= pci_resource_len(pdev
, 0);
147 #if defined(__powerpc__)
148 /* There isn't a generic way to specify writethrough mappings */
149 dd
->kregbase
= __ioremap(addr
, len
, _PAGE_NO_CACHE
| _PAGE_WRITETHRU
);
151 dd
->kregbase
= ioremap_nocache(addr
, len
);
157 dd
->kregend
= (u64 __iomem
*)((void __iomem
*) dd
->kregbase
+ len
);
158 dd
->physaddr
= addr
; /* used for io_remap, etc. */
161 * Save BARs to rewrite after device reset. Save all 64 bits of
165 dd
->pcibar1
= addr
>> 32;
166 dd
->deviceid
= ent
->device
; /* save for later use */
167 dd
->vendorid
= ent
->vendor
;
173 * Do PCIe cleanup, after chip-specific cleanup, etc. Just prior
174 * to releasing the dd memory.
175 * void because none of the core pcie cleanup returns are void
177 void qib_pcie_ddcleanup(struct qib_devdata
*dd
)
179 u64 __iomem
*base
= (void __iomem
*) dd
->kregbase
;
184 iounmap(dd
->piobase
);
186 iounmap(dd
->userbase
);
188 iounmap(dd
->piovl15base
);
190 pci_disable_device(dd
->pcidev
);
191 pci_release_regions(dd
->pcidev
);
193 pci_set_drvdata(dd
->pcidev
, NULL
);
196 static void qib_msix_setup(struct qib_devdata
*dd
, int pos
, u32
*msixcnt
,
197 struct qib_msix_entry
*qib_msix_entry
)
201 struct msix_entry
*msix_entry
;
204 ret
= pci_msix_vec_count(dd
->pcidev
);
208 nvec
= min(nvec
, ret
);
210 /* We can't pass qib_msix_entry array to qib_msix_setup
211 * so use a dummy msix_entry array and copy the allocated
212 * irq back to the qib_msix_entry array. */
213 msix_entry
= kcalloc(nvec
, sizeof(*msix_entry
), GFP_KERNEL
);
217 for (i
= 0; i
< nvec
; i
++)
218 msix_entry
[i
] = qib_msix_entry
[i
].msix
;
220 ret
= pci_enable_msix_range(dd
->pcidev
, msix_entry
, 1, nvec
);
222 goto free_msix_entry
;
226 for (i
= 0; i
< nvec
; i
++)
227 qib_msix_entry
[i
].msix
= msix_entry
[i
];
239 "pci_enable_msix_range %d vectors failed: %d, falling back to INTx\n",
242 qib_enable_intx(dd
->pcidev
);
246 * We save the msi lo and hi values, so we can restore them after
247 * chip reset (the kernel PCI infrastructure doesn't yet handle that
250 static int qib_msi_setup(struct qib_devdata
*dd
, int pos
)
252 struct pci_dev
*pdev
= dd
->pcidev
;
256 ret
= pci_enable_msi(pdev
);
259 "pci_enable_msi failed: %d, interrupts may not work\n",
261 /* continue even if it fails, we may still be OK... */
263 pci_read_config_dword(pdev
, pos
+ PCI_MSI_ADDRESS_LO
,
265 pci_read_config_dword(pdev
, pos
+ PCI_MSI_ADDRESS_HI
,
267 pci_read_config_word(pdev
, pos
+ PCI_MSI_FLAGS
, &control
);
268 /* now save the data (vector) info */
269 pci_read_config_word(pdev
, pos
+ ((control
& PCI_MSI_FLAGS_64BIT
)
275 int qib_pcie_params(struct qib_devdata
*dd
, u32 minw
, u32
*nent
,
276 struct qib_msix_entry
*entry
)
279 int pos
= 0, ret
= 1;
281 if (!pci_is_pcie(dd
->pcidev
)) {
282 qib_dev_err(dd
, "Can't find PCI Express capability!\n");
283 /* set up something... */
285 dd
->lbus_speed
= 2500; /* Gen1, 2.5GHz */
289 pos
= dd
->pcidev
->msix_cap
;
290 if (nent
&& *nent
&& pos
) {
291 qib_msix_setup(dd
, pos
, nent
, entry
);
292 ret
= 0; /* did it, either MSIx or INTx */
294 pos
= dd
->pcidev
->msi_cap
;
296 ret
= qib_msi_setup(dd
, pos
);
298 qib_dev_err(dd
, "No PCI MSI or MSIx capability!\n");
301 qib_enable_intx(dd
->pcidev
);
303 pcie_capability_read_word(dd
->pcidev
, PCI_EXP_LNKSTA
, &linkstat
);
305 * speed is bits 0-3, linkwidth is bits 4-8
306 * no defines for them in headers
308 speed
= linkstat
& 0xf;
311 dd
->lbus_width
= linkstat
;
315 dd
->lbus_speed
= 2500; /* Gen1, 2.5GHz */
318 dd
->lbus_speed
= 5000; /* Gen1, 5GHz */
320 default: /* not defined, assume gen1 */
321 dd
->lbus_speed
= 2500;
326 * Check against expected pcie width and complain if "wrong"
327 * on first initialization, not afterwards (i.e., reset).
329 if (minw
&& linkstat
< minw
)
331 "PCIe width %u (x%u HCA), performance reduced\n",
334 qib_tune_pcie_caps(dd
);
336 qib_tune_pcie_coalesce(dd
);
339 /* fill in string, even on errors */
340 snprintf(dd
->lbus_info
, sizeof(dd
->lbus_info
),
341 "PCIe,%uMHz,x%u\n", dd
->lbus_speed
, dd
->lbus_width
);
346 * Setup pcie interrupt stuff again after a reset. I'd like to just call
347 * pci_enable_msi() again for msi, but when I do that,
348 * the MSI enable bit doesn't get set in the command word, and
349 * we switch to to a different interrupt vector, which is confusing,
350 * so I instead just do it all inline. Perhaps somehow can tie this
351 * into the PCIe hotplug support at some point
353 int qib_reinit_intr(struct qib_devdata
*dd
)
359 /* If we aren't using MSI, don't restore it */
363 pos
= dd
->pcidev
->msi_cap
;
366 "Can't find MSI capability, can't restore MSI settings\n");
368 /* nothing special for MSIx, just MSI */
371 pci_write_config_dword(dd
->pcidev
, pos
+ PCI_MSI_ADDRESS_LO
,
373 pci_write_config_dword(dd
->pcidev
, pos
+ PCI_MSI_ADDRESS_HI
,
375 pci_read_config_word(dd
->pcidev
, pos
+ PCI_MSI_FLAGS
, &control
);
376 if (!(control
& PCI_MSI_FLAGS_ENABLE
)) {
377 control
|= PCI_MSI_FLAGS_ENABLE
;
378 pci_write_config_word(dd
->pcidev
, pos
+ PCI_MSI_FLAGS
,
381 /* now rewrite the data (vector) info */
382 pci_write_config_word(dd
->pcidev
, pos
+
383 ((control
& PCI_MSI_FLAGS_64BIT
) ? 12 : 8),
387 if (!ret
&& (dd
->flags
& QIB_HAS_INTX
)) {
388 qib_enable_intx(dd
->pcidev
);
392 /* and now set the pci master bit again */
393 pci_set_master(dd
->pcidev
);
399 * Disable msi interrupt if enabled, and clear msi_lo.
400 * This is used primarily for the fallback to INTx, but
401 * is also used in reinit after reset, and during cleanup.
403 void qib_nomsi(struct qib_devdata
*dd
)
406 pci_disable_msi(dd
->pcidev
);
410 * Same as qib_nosmi, but for MSIx.
412 void qib_nomsix(struct qib_devdata
*dd
)
414 pci_disable_msix(dd
->pcidev
);
418 * Similar to pci_intx(pdev, 1), except that we make sure
421 void qib_enable_intx(struct pci_dev
*pdev
)
426 /* first, turn on INTx */
427 pci_read_config_word(pdev
, PCI_COMMAND
, &cw
);
428 new = cw
& ~PCI_COMMAND_INTX_DISABLE
;
430 pci_write_config_word(pdev
, PCI_COMMAND
, new);
434 /* then turn off MSI */
435 pci_read_config_word(pdev
, pos
+ PCI_MSI_FLAGS
, &cw
);
436 new = cw
& ~PCI_MSI_FLAGS_ENABLE
;
438 pci_write_config_word(pdev
, pos
+ PCI_MSI_FLAGS
, new);
440 pos
= pdev
->msix_cap
;
442 /* then turn off MSIx */
443 pci_read_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
, &cw
);
444 new = cw
& ~PCI_MSIX_FLAGS_ENABLE
;
446 pci_write_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
, new);
451 * These two routines are helper routines for the device reset code
452 * to move all the pcie code out of the chip-specific driver code.
454 void qib_pcie_getcmd(struct qib_devdata
*dd
, u16
*cmd
, u8
*iline
, u8
*cline
)
456 pci_read_config_word(dd
->pcidev
, PCI_COMMAND
, cmd
);
457 pci_read_config_byte(dd
->pcidev
, PCI_INTERRUPT_LINE
, iline
);
458 pci_read_config_byte(dd
->pcidev
, PCI_CACHE_LINE_SIZE
, cline
);
461 void qib_pcie_reenable(struct qib_devdata
*dd
, u16 cmd
, u8 iline
, u8 cline
)
465 r
= pci_write_config_dword(dd
->pcidev
, PCI_BASE_ADDRESS_0
,
468 qib_dev_err(dd
, "rewrite of BAR0 failed: %d\n", r
);
469 r
= pci_write_config_dword(dd
->pcidev
, PCI_BASE_ADDRESS_1
,
472 qib_dev_err(dd
, "rewrite of BAR1 failed: %d\n", r
);
473 /* now re-enable memory access, and restore cosmetic settings */
474 pci_write_config_word(dd
->pcidev
, PCI_COMMAND
, cmd
);
475 pci_write_config_byte(dd
->pcidev
, PCI_INTERRUPT_LINE
, iline
);
476 pci_write_config_byte(dd
->pcidev
, PCI_CACHE_LINE_SIZE
, cline
);
477 r
= pci_enable_device(dd
->pcidev
);
480 "pci_enable_device failed after reset: %d\n", r
);
484 static int qib_pcie_coalesce
;
485 module_param_named(pcie_coalesce
, qib_pcie_coalesce
, int, S_IRUGO
);
486 MODULE_PARM_DESC(pcie_coalesce
, "tune PCIe colescing on some Intel chipsets");
489 * Enable PCIe completion and data coalescing, on Intel 5x00 and 7300
490 * chipsets. This is known to be unsafe for some revisions of some
491 * of these chipsets, with some BIOS settings, and enabling it on those
492 * systems may result in the system crashing, and/or data corruption.
494 static void qib_tune_pcie_coalesce(struct qib_devdata
*dd
)
497 struct pci_dev
*parent
;
501 if (!qib_pcie_coalesce
)
504 /* Find out supported and configured values for parent (root) */
505 parent
= dd
->pcidev
->bus
->self
;
506 if (parent
->bus
->parent
) {
507 qib_devinfo(dd
->pcidev
, "Parent not root\n");
510 if (!pci_is_pcie(parent
))
512 if (parent
->vendor
!= 0x8086)
516 * - bit 12: Max_rdcmp_Imt_EN: need to set to 1
517 * - bit 11: COALESCE_FORCE: need to set to 0
518 * - bit 10: COALESCE_EN: need to set to 1
519 * (but limitations on some on some chipsets)
521 * On the Intel 5000, 5100, and 7300 chipsets, there is
522 * also: - bit 25:24: COALESCE_MODE, need to set to 0
524 devid
= parent
->device
;
525 if (devid
>= 0x25e2 && devid
<= 0x25fa) {
527 if (parent
->revision
<= 0xb2)
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
);
555 * BIOS may not set PCIe bus-utilization parameters for best performance.
556 * Check and optionally adjust them to maximize our throughput.
558 static int qib_pcie_caps
;
559 module_param_named(pcie_caps
, qib_pcie_caps
, int, S_IRUGO
);
560 MODULE_PARM_DESC(pcie_caps
, "Max PCIe tuning: Payload (0..3), ReadReq (4..7)");
562 static void qib_tune_pcie_caps(struct qib_devdata
*dd
)
564 struct pci_dev
*parent
;
565 u16 rc_mpss
, rc_mps
, ep_mpss
, ep_mps
;
566 u16 rc_mrrs
, ep_mrrs
, max_mrrs
;
568 /* Find out supported and configured values for parent (root) */
569 parent
= dd
->pcidev
->bus
->self
;
570 if (!pci_is_root_bus(parent
->bus
)) {
571 qib_devinfo(dd
->pcidev
, "Parent not root\n");
575 if (!pci_is_pcie(parent
) || !pci_is_pcie(dd
->pcidev
))
578 rc_mpss
= parent
->pcie_mpss
;
579 rc_mps
= ffs(pcie_get_mps(parent
)) - 8;
580 /* Find out supported and configured values for endpoint (us) */
581 ep_mpss
= dd
->pcidev
->pcie_mpss
;
582 ep_mps
= ffs(pcie_get_mps(dd
->pcidev
)) - 8;
584 /* Find max payload supported by root, endpoint */
585 if (rc_mpss
> ep_mpss
)
588 /* If Supported greater than limit in module param, limit it */
589 if (rc_mpss
> (qib_pcie_caps
& 7))
590 rc_mpss
= qib_pcie_caps
& 7;
591 /* If less than (allowed, supported), bump root payload */
592 if (rc_mpss
> rc_mps
) {
594 pcie_set_mps(parent
, 128 << rc_mps
);
596 /* If less than (allowed, supported), bump endpoint payload */
597 if (rc_mpss
> ep_mps
) {
599 pcie_set_mps(dd
->pcidev
, 128 << ep_mps
);
603 * Now the Read Request size.
604 * No field for max supported, but PCIe spec limits it to 4096,
605 * which is code '5' (log2(4096) - 7)
608 if (max_mrrs
> ((qib_pcie_caps
>> 4) & 7))
609 max_mrrs
= (qib_pcie_caps
>> 4) & 7;
611 max_mrrs
= 128 << max_mrrs
;
612 rc_mrrs
= pcie_get_readrq(parent
);
613 ep_mrrs
= pcie_get_readrq(dd
->pcidev
);
615 if (max_mrrs
> rc_mrrs
) {
617 pcie_set_readrq(parent
, rc_mrrs
);
619 if (max_mrrs
> ep_mrrs
) {
621 pcie_set_readrq(dd
->pcidev
, ep_mrrs
);
624 /* End of PCIe capability tuning */
627 * From here through qib_pci_err_handler definition is invoked via
628 * PCI error infrastructure, registered via pci
630 static pci_ers_result_t
631 qib_pci_error_detected(struct pci_dev
*pdev
, pci_channel_state_t state
)
633 struct qib_devdata
*dd
= pci_get_drvdata(pdev
);
634 pci_ers_result_t ret
= PCI_ERS_RESULT_RECOVERED
;
637 case pci_channel_io_normal
:
638 qib_devinfo(pdev
, "State Normal, ignoring\n");
641 case pci_channel_io_frozen
:
642 qib_devinfo(pdev
, "State Frozen, requesting reset\n");
643 pci_disable_device(pdev
);
644 ret
= PCI_ERS_RESULT_NEED_RESET
;
647 case pci_channel_io_perm_failure
:
648 qib_devinfo(pdev
, "State Permanent Failure, disabling\n");
650 /* no more register accesses! */
651 dd
->flags
&= ~QIB_PRESENT
;
652 qib_disable_after_error(dd
);
654 /* else early, or other problem */
655 ret
= PCI_ERS_RESULT_DISCONNECT
;
658 default: /* shouldn't happen */
659 qib_devinfo(pdev
, "QIB PCI errors detected (state %d)\n",
666 static pci_ers_result_t
667 qib_pci_mmio_enabled(struct pci_dev
*pdev
)
670 struct qib_devdata
*dd
= pci_get_drvdata(pdev
);
671 pci_ers_result_t ret
= PCI_ERS_RESULT_RECOVERED
;
673 if (dd
&& dd
->pport
) {
674 words
= dd
->f_portcntr(dd
->pport
, QIBPORTCNTR_WORDRCV
);
676 ret
= PCI_ERS_RESULT_NEED_RESET
;
679 "QIB mmio_enabled function called, read wordscntr %Lx, returning %d\n",
684 static pci_ers_result_t
685 qib_pci_slot_reset(struct pci_dev
*pdev
)
687 qib_devinfo(pdev
, "QIB slot_reset function called, ignored\n");
688 return PCI_ERS_RESULT_CAN_RECOVER
;
691 static pci_ers_result_t
692 qib_pci_link_reset(struct pci_dev
*pdev
)
694 qib_devinfo(pdev
, "QIB link_reset function called, ignored\n");
695 return PCI_ERS_RESULT_CAN_RECOVER
;
699 qib_pci_resume(struct pci_dev
*pdev
)
701 struct qib_devdata
*dd
= pci_get_drvdata(pdev
);
703 qib_devinfo(pdev
, "QIB resume function called\n");
704 pci_cleanup_aer_uncorrect_error_status(pdev
);
706 * Running jobs will fail, since it's asynchronous
707 * unlike sysfs-requested reset. Better than
710 qib_init(dd
, 1); /* same as re-init after reset */
713 const struct pci_error_handlers qib_pci_err_handler
= {
714 .error_detected
= qib_pci_error_detected
,
715 .mmio_enabled
= qib_pci_mmio_enabled
,
716 .link_reset
= qib_pci_link_reset
,
717 .slot_reset
= qib_pci_slot_reset
,
718 .resume
= qib_pci_resume
,