|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * PCIe TLP Log handling |
| 4 | + * |
| 5 | + * Copyright (C) 2024 Intel Corporation |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/aer.h> |
| 9 | +#include <linux/array_size.h> |
| 10 | +#include <linux/pci.h> |
| 11 | +#include <linux/string.h> |
| 12 | + |
| 13 | +#include "../pci.h" |
| 14 | + |
| 15 | +/** |
| 16 | + * aer_tlp_log_len - Calculates AER Capability TLP Header/Prefix Log length |
| 17 | + * @dev: PCIe device |
| 18 | + * @aercc: AER Capabilities and Control register value |
| 19 | + * |
| 20 | + * Return: TLP Header/Prefix Log length |
| 21 | + */ |
| 22 | +unsigned int aer_tlp_log_len(struct pci_dev *dev, u32 aercc) |
| 23 | +{ |
| 24 | + return 4 + (aercc & PCI_ERR_CAP_PREFIX_LOG_PRESENT) ? |
| 25 | + dev->eetlp_prefix_max : 0; |
| 26 | +} |
| 27 | + |
| 28 | +#ifdef CONFIG_PCIE_DPC |
| 29 | +/** |
| 30 | + * dpc_tlp_log_len - Calculates DPC RP PIO TLP Header/Prefix Log length |
| 31 | + * @dev: PCIe device |
| 32 | + * |
| 33 | + * Return: TLP Header/Prefix Log length |
| 34 | + */ |
| 35 | +unsigned int dpc_tlp_log_len(struct pci_dev *dev) |
| 36 | +{ |
| 37 | + /* Remove ImpSpec Log register from the count */ |
| 38 | + if (dev->dpc_rp_log_size >= 5) |
| 39 | + return dev->dpc_rp_log_size - 1; |
| 40 | + |
| 41 | + return dev->dpc_rp_log_size; |
| 42 | +} |
| 43 | +#endif |
| 44 | + |
| 45 | +/** |
| 46 | + * pcie_read_tlp_log - read TLP Header Log |
| 47 | + * @dev: PCIe device |
| 48 | + * @where: PCI Config offset of TLP Header Log |
| 49 | + * @where2: PCI Config offset of TLP Prefix Log |
| 50 | + * @tlp_len: TLP Log length (Header Log + TLP Prefix Log in DWORDs) |
| 51 | + * @log: TLP Log structure to fill |
| 52 | + * |
| 53 | + * Fill @log from TLP Header Log registers, e.g., AER or DPC. |
| 54 | + * |
| 55 | + * Return: 0 on success and filled TLP Log structure, <0 on error. |
| 56 | + */ |
| 57 | +int pcie_read_tlp_log(struct pci_dev *dev, int where, int where2, |
| 58 | + unsigned int tlp_len, struct pcie_tlp_log *log) |
| 59 | +{ |
| 60 | + unsigned int i; |
| 61 | + int off, ret; |
| 62 | + u32 *to; |
| 63 | + |
| 64 | + memset(log, 0, sizeof(*log)); |
| 65 | + |
| 66 | + for (i = 0; i < tlp_len; i++) { |
| 67 | + if (i < 4) { |
| 68 | + off = where + i * 4; |
| 69 | + to = &log->dw[i]; |
| 70 | + } else { |
| 71 | + off = where2 + (i - 4) * 4; |
| 72 | + to = &log->prefix[i - 4]; |
| 73 | + } |
| 74 | + |
| 75 | + ret = pci_read_config_dword(dev, off, to); |
| 76 | + if (ret) |
| 77 | + return pcibios_err_to_errno(ret); |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +#define EE_PREFIX_STR " E-E Prefixes:" |
| 84 | + |
| 85 | +/** |
| 86 | + * pcie_print_tlp_log - Print TLP Header / Prefix Log contents |
| 87 | + * @dev: PCIe device |
| 88 | + * @log: TLP Log structure |
| 89 | + * @pfx: String prefix |
| 90 | + * |
| 91 | + * Prints TLP Header and Prefix Log information held by @log. |
| 92 | + */ |
| 93 | +void pcie_print_tlp_log(const struct pci_dev *dev, |
| 94 | + const struct pcie_tlp_log *log, const char *pfx) |
| 95 | +{ |
| 96 | + char buf[11 * (4 + ARRAY_SIZE(log->prefix)) + sizeof(EE_PREFIX_STR)]; |
| 97 | + unsigned int i; |
| 98 | + int len; |
| 99 | + |
| 100 | + len = scnprintf(buf, sizeof(buf), "%#010x %#010x %#010x %#010x", |
| 101 | + log->dw[0], log->dw[1], log->dw[2], log->dw[3]); |
| 102 | + |
| 103 | + if (log->prefix[0]) |
| 104 | + len += scnprintf(buf + len, sizeof(buf) - len, EE_PREFIX_STR); |
| 105 | + for (i = 0; i < ARRAY_SIZE(log->prefix); i++) { |
| 106 | + if (!log->prefix[i]) |
| 107 | + break; |
| 108 | + len += scnprintf(buf + len, sizeof(buf) - len, |
| 109 | + " %#010x", log->prefix[i]); |
| 110 | + } |
| 111 | + |
| 112 | + pci_err(dev, "%sTLP Header: %s\n", pfx, buf); |
| 113 | +} |
0 commit comments