dpc.c 8.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3
/*
 * PCI Express Downstream Port Containment services driver
4 5
 * Author: Keith Busch <keith.busch@intel.com>
 *
6 7 8
 * Copyright (C) 2016 Intel Corp.
 */

9
#include <linux/aer.h>
10 11
#include <linux/delay.h>
#include <linux/interrupt.h>
12
#include <linux/init.h>
13
#include <linux/pci.h>
14

15
#include "portdrv.h"
K
Keith Busch 已提交
16
#include "../pci.h"
17 18 19

struct dpc_dev {
	struct pcie_device	*dev;
20
	u16			cap_pos;
21
	bool			rp_extensions;
22
	u8			rp_log_size;
D
Dongdong Liu 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
};

static const char * const rp_pio_error_string[] = {
	"Configuration Request received UR Completion",	 /* Bit Position 0  */
	"Configuration Request received CA Completion",	 /* Bit Position 1  */
	"Configuration Request Completion Timeout",	 /* Bit Position 2  */
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	"I/O Request received UR Completion",		 /* Bit Position 8  */
	"I/O Request received CA Completion",		 /* Bit Position 9  */
	"I/O Request Completion Timeout",		 /* Bit Position 10 */
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	"Memory Request received UR Completion",	 /* Bit Position 16 */
	"Memory Request received CA Completion",	 /* Bit Position 17 */
	"Memory Request Completion Timeout",		 /* Bit Position 18 */
45 46
};

47 48 49 50
static int dpc_wait_rp_inactive(struct dpc_dev *dpc)
{
	unsigned long timeout = jiffies + HZ;
	struct pci_dev *pdev = dpc->dev->port;
51
	struct device *dev = &dpc->dev->device;
52
	u16 cap = dpc->cap_pos, status;
53

54
	pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
55 56 57
	while (status & PCI_EXP_DPC_RP_BUSY &&
					!time_after(jiffies, timeout)) {
		msleep(10);
58
		pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
59 60
	}
	if (status & PCI_EXP_DPC_RP_BUSY) {
61
		dev_warn(dev, "DPC root port still busy\n");
62 63 64 65 66
		return -EBUSY;
	}
	return 0;
}

67
static pci_ers_result_t dpc_reset_link(struct pci_dev *pdev)
68
{
69 70 71
	struct dpc_dev *dpc;
	struct pcie_device *pciedev;
	struct device *devdpc;
72

73
	u16 cap;
74 75 76 77 78 79 80 81 82 83 84 85 86 87

	/*
	 * DPC disables the Link automatically in hardware, so it has
	 * already been reset by the time we get here.
	 */
	devdpc = pcie_port_find_device(pdev, PCIE_PORT_SERVICE_DPC);
	pciedev = to_pcie_device(devdpc);
	dpc = get_service_data(pciedev);
	cap = dpc->cap_pos;

	/*
	 * Wait until the Link is inactive, then clear DPC Trigger Status
	 * to allow the Port to leave DPC.
	 */
88
	pcie_wait_for_link(pdev, false);
89

90
	if (dpc->rp_extensions && dpc_wait_rp_inactive(dpc))
91
		return PCI_ERS_RESULT_DISCONNECT;
D
Dongdong Liu 已提交
92

93
	pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
94
			      PCI_EXP_DPC_STATUS_TRIGGER);
95

96 97 98
	return PCI_ERS_RESULT_RECOVERED;
}

99

100
static void dpc_process_rp_pio_error(struct dpc_dev *dpc)
D
Dongdong Liu 已提交
101
{
102
	struct device *dev = &dpc->dev->device;
D
Dongdong Liu 已提交
103
	struct pci_dev *pdev = dpc->dev->port;
104 105
	u16 cap = dpc->cap_pos, dpc_status, first_error;
	u32 status, mask, sev, syserr, exc, dw0, dw1, dw2, dw3, log, prefix;
D
Dongdong Liu 已提交
106 107
	int i;

108 109
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS, &status);
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_MASK, &mask);
110
	dev_err(dev, "rp_pio_status: %#010x, rp_pio_mask: %#010x\n",
111
		status, mask);
D
Dongdong Liu 已提交
112

113 114 115
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SEVERITY, &sev);
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SYSERROR, &syserr);
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_EXCEPTION, &exc);
116
	dev_err(dev, "RP PIO severity=%#010x, syserror=%#010x, exception=%#010x\n",
117
		sev, syserr, exc);
D
Dongdong Liu 已提交
118 119

	/* Get First Error Pointer */
120
	pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &dpc_status);
121
	first_error = (dpc_status & 0x1f00) >> 8;
D
Dongdong Liu 已提交
122

123
	for (i = 0; i < ARRAY_SIZE(rp_pio_error_string); i++) {
124
		if ((status & ~mask) & (1 << i))
125
			dev_err(dev, "[%2d] %s%s\n", i, rp_pio_error_string[i],
126
				first_error == i ? " (First)" : "");
127 128
	}

129
	if (dpc->rp_log_size < 4)
130
		goto clear_status;
131
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG,
132
			      &dw0);
133
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 4,
134
			      &dw1);
135
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 8,
136
			      &dw2);
137
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 12,
138
			      &dw3);
139
	dev_err(dev, "TLP Header: %#010x %#010x %#010x %#010x\n",
140
		dw0, dw1, dw2, dw3);
D
Dongdong Liu 已提交
141

142
	if (dpc->rp_log_size < 5)
143
		goto clear_status;
144 145
	pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_IMPSPEC_LOG, &log);
	dev_err(dev, "RP PIO ImpSpec Log %#010x\n", log);
146

147
	for (i = 0; i < dpc->rp_log_size - 5; i++) {
D
Dongdong Liu 已提交
148
		pci_read_config_dword(pdev,
149 150
			cap + PCI_EXP_DPC_RP_PIO_TLPPREFIX_LOG, &prefix);
		dev_err(dev, "TLP Prefix Header: dw%d, %#010x\n", i, prefix);
151
	}
152 153
 clear_status:
	pci_write_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS, status);
D
Dongdong Liu 已提交
154 155
}

156
static irqreturn_t dpc_handler(int irq, void *context)
157
{
158
	struct aer_err_info info;
159
	struct dpc_dev *dpc = context;
160
	struct pci_dev *pdev = dpc->dev->port;
161
	struct device *dev = &dpc->dev->device;
162
	u16 cap = dpc->cap_pos, status, source, reason, ext_reason;
163

164
	pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
165
	pci_read_config_word(pdev, cap + PCI_EXP_DPC_SOURCE_ID, &source);
166

167
	dev_info(dev, "DPC containment event, status:%#06x source:%#06x\n",
168
		 status, source);
169

170 171
	reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN) >> 1;
	ext_reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN_EXT) >> 5;
172 173 174 175 176 177 178
	dev_warn(dev, "DPC %s detected, remove downstream devices\n",
		 (reason == 0) ? "unmasked uncorrectable error" :
		 (reason == 1) ? "ERR_NONFATAL" :
		 (reason == 2) ? "ERR_FATAL" :
		 (ext_reason == 0) ? "RP PIO error" :
		 (ext_reason == 1) ? "software trigger" :
				     "reserved error");
179

180
	/* show RP PIO error detail information */
181
	if (dpc->rp_extensions && reason == 3 && ext_reason == 0)
182
		dpc_process_rp_pio_error(dpc);
183 184 185 186
	else if (reason == 0 && aer_get_device_error_info(pdev, &info)) {
		aer_print_error(pdev, &info);
		pci_cleanup_aer_uncorrect_error_status(pdev);
	}
187

188 189
	/* We configure DPC so it only triggers on ERR_FATAL */
	pcie_do_fatal_recovery(pdev, PCIE_PORT_SERVICE_DPC);
190 191

	return IRQ_HANDLED;
192 193 194 195 196 197 198 199 200 201 202 203 204
}

static irqreturn_t dpc_irq(int irq, void *context)
{
	struct dpc_dev *dpc = (struct dpc_dev *)context;
	struct pci_dev *pdev = dpc->dev->port;
	u16 cap = dpc->cap_pos, status;

	pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);

	if (!(status & PCI_EXP_DPC_STATUS_INTERRUPT) || status == (u16)(~0))
		return IRQ_NONE;

205 206
	pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
			      PCI_EXP_DPC_STATUS_INTERRUPT);
207
	if (status & PCI_EXP_DPC_STATUS_TRIGGER)
208
		return IRQ_WAKE_THREAD;
209 210 211 212 213 214 215 216
	return IRQ_HANDLED;
}

#define FLAG(x, y) (((x) & (y)) ? '+' : '-')
static int dpc_probe(struct pcie_device *dev)
{
	struct dpc_dev *dpc;
	struct pci_dev *pdev = dev->port;
217
	struct device *device = &dev->device;
218 219 220
	int status;
	u16 ctl, cap;

221 222 223
	if (pcie_aer_get_firmware_first(pdev))
		return -ENOTSUPP;

224
	dpc = devm_kzalloc(device, sizeof(*dpc), GFP_KERNEL);
225 226 227 228 229 230 231
	if (!dpc)
		return -ENOMEM;

	dpc->cap_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC);
	dpc->dev = dev;
	set_service_data(dev, dpc);

232 233 234
	status = devm_request_threaded_irq(device, dev->irq, dpc_irq,
					   dpc_handler, IRQF_SHARED,
					   "pcie-dpc", dpc);
235
	if (status) {
236
		dev_warn(device, "request IRQ%d failed: %d\n", dev->irq,
237
			 status);
238
		return status;
239 240 241 242 243
	}

	pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CAP, &cap);
	pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);

244
	dpc->rp_extensions = (cap & PCI_EXP_DPC_CAP_RP_EXT);
245 246 247 248 249 250 251 252
	if (dpc->rp_extensions) {
		dpc->rp_log_size = (cap & PCI_EXP_DPC_RP_PIO_LOG_SIZE) >> 8;
		if (dpc->rp_log_size < 4 || dpc->rp_log_size > 9) {
			dev_err(device, "RP PIO log size %u is invalid\n",
				dpc->rp_log_size);
			dpc->rp_log_size = 0;
		}
	}
253

254
	ctl = (ctl & 0xfff4) | PCI_EXP_DPC_CTL_EN_FATAL | PCI_EXP_DPC_CTL_INT_EN;
255 256
	pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);

257
	dev_info(device, "DPC error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
258
		cap & PCI_EXP_DPC_IRQ, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT),
259
		FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP),
260
		FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), dpc->rp_log_size,
261 262 263 264 265 266 267 268 269 270 271
		FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE));
	return status;
}

static void dpc_remove(struct pcie_device *dev)
{
	struct dpc_dev *dpc = get_service_data(dev);
	struct pci_dev *pdev = dev->port;
	u16 ctl;

	pci_read_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, &ctl);
272
	ctl &= ~(PCI_EXP_DPC_CTL_EN_FATAL | PCI_EXP_DPC_CTL_INT_EN);
273 274 275 276 277
	pci_write_config_word(pdev, dpc->cap_pos + PCI_EXP_DPC_CTL, ctl);
}

static struct pcie_port_service_driver dpcdriver = {
	.name		= "dpc",
278
	.port_type	= PCIE_ANY_PORT,
279 280 281
	.service	= PCIE_PORT_SERVICE_DPC,
	.probe		= dpc_probe,
	.remove		= dpc_remove,
282
	.reset_link	= dpc_reset_link,
283 284 285 286 287 288
};

static int __init dpc_service_init(void)
{
	return pcie_port_service_register(&dpcdriver);
}
289
device_initcall(dpc_service_init);