aerdrv_core.c 13.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
B
Bjorn Helgaas 已提交
3 4 5 6
 * Implement the core part of PCIe AER. When a PCIe error is delivered, an
 * error message will be collected and printed to console, then an error
 * recovery procedure will be executed by following the PCI error recovery
 * rules.
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright (C) 2006 Intel Corp.
 *	Tom Long Nguyen (tom.l.nguyen@intel.com)
 *	Zhang Yanmin (yanmin.zhang@intel.com)
 */

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/pm.h>
#include <linux/suspend.h>
#include <linux/delay.h>
20
#include <linux/slab.h>
21
#include <linux/kfifo.h>
22
#include "aerdrv.h"
23
#include "../../pci.h"
24

25 26 27
#define	PCI_EXP_AER_FLAGS	(PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
				 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)

28 29
int pci_enable_pcie_error_reporting(struct pci_dev *dev)
{
30
	if (pcie_aer_get_firmware_first(dev))
31 32
		return -EIO;

K
Keith Busch 已提交
33
	if (!dev->aer_cap)
34 35
		return -EIO;

36
	return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
37
}
38
EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
39 40 41

int pci_disable_pcie_error_reporting(struct pci_dev *dev)
{
42
	if (pcie_aer_get_firmware_first(dev))
43 44
		return -EIO;

45 46
	return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
					  PCI_EXP_AER_FLAGS);
47
}
48
EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
49 50 51 52

int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
{
	int pos;
53
	u32 status;
54

K
Keith Busch 已提交
55
	pos = dev->aer_cap;
56 57 58 59
	if (!pos)
		return -EIO;

	pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
60 61
	if (status)
		pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
62 63 64

	return 0;
}
65
EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
66

67 68 69 70 71 72 73 74 75
int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
{
	int pos;
	u32 status;
	int port_type;

	if (!pci_is_pcie(dev))
		return -ENODEV;

K
Keith Busch 已提交
76
	pos = dev->aer_cap;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	if (!pos)
		return -EIO;

	port_type = pci_pcie_type(dev);
	if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
		pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
		pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
	}

	pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
	pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);

	pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
	pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);

	return 0;
}

K
Keith Busch 已提交
95 96 97 98 99 100
int pci_aer_init(struct pci_dev *dev)
{
	dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
	return pci_cleanup_aer_error_status_regs(dev);
}

101 102 103 104 105
/**
 * add_error_device - list device to be handled
 * @e_info: pointer to error info
 * @dev: pointer to pci_dev to be added
 */
Z
Zhang, Yanmin 已提交
106 107 108 109 110
static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
{
	if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
		e_info->dev[e_info->error_dev_num] = dev;
		e_info->error_dev_num++;
111
		return 0;
112
	}
113
	return -ENOSPC;
Z
Zhang, Yanmin 已提交
114 115
}

116 117 118 119 120 121
/**
 * is_error_source - check whether the device is source of reported error
 * @dev: pointer to pci_dev to be checked
 * @e_info: pointer to reported error info
 */
static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
122 123
{
	int pos;
124
	u32 status, mask;
125 126 127 128 129 130
	u16 reg16;

	/*
	 * When bus id is equal to 0, it might be a bad id
	 * reported by root port.
	 */
131
	if ((PCI_BUS_NUM(e_info->id) != 0) &&
132
	    !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
133 134
		/* Device ID match? */
		if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
135
			return true;
Z
Zhang, Yanmin 已提交
136

137
		/* Continue id comparing if there is no multiple error */
H
Hidetoshi Seto 已提交
138
		if (!e_info->multi_error_valid)
139
			return false;
140 141 142
	}

	/*
Z
Zhang, Yanmin 已提交
143
	 * When either
144
	 *      1) bus id is equal to 0. Some ports might lose the bus
Z
Zhang, Yanmin 已提交
145
	 *              id of error source id;
146 147
	 *      2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
	 *      3) There are multiple errors and prior ID comparing fails;
148
	 * We check AER status registers to find possible reporter.
149 150
	 */
	if (atomic_read(&dev->enable_cnt) == 0)
151 152
		return false;

153
	/* Check if AER is enabled */
154 155
	pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
	if (!(reg16 & PCI_EXP_AER_FLAGS))
156
		return false;
157

K
Keith Busch 已提交
158
	pos = dev->aer_cap;
159
	if (!pos)
160
		return false;
161

162
	/* Check if error is recorded */
163
	if (e_info->severity == AER_CORRECTABLE) {
164 165
		pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
		pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
166
	} else {
167 168
		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
169
	}
170 171
	if (status & ~mask)
		return true;
172

173 174
	return false;
}
Z
Zhang, Yanmin 已提交
175

176 177 178 179 180
static int find_device_iter(struct pci_dev *dev, void *data)
{
	struct aer_err_info *e_info = (struct aer_err_info *)data;

	if (is_error_source(dev, e_info)) {
181 182 183 184 185 186
		/* List this device */
		if (add_error_device(e_info, dev)) {
			/* We cannot handle more... Stop iteration */
			/* TODO: Should print error message here? */
			return 1;
		}
187 188 189 190 191 192

		/* If there is only a single error, stop iteration */
		if (!e_info->multi_error_valid)
			return 1;
	}
	return 0;
193 194 195 196
}

/**
 * find_source_device - search through device hierarchy for source device
R
Randy Dunlap 已提交
197
 * @parent: pointer to Root Port pci_dev data structure
198
 * @e_info: including detailed error information such like id
199
 *
200 201 202
 * Return true if found.
 *
 * Invoked by DPC when error is detected at the Root Port.
203 204 205
 * Caller of this function must set id, severity, and multi_error_valid of
 * struct aer_err_info pointed by @e_info properly.  This function must fill
 * e_info->error_dev_num and e_info->dev[], based on the given information.
R
Randy Dunlap 已提交
206
 */
207
static bool find_source_device(struct pci_dev *parent,
208
		struct aer_err_info *e_info)
209 210
{
	struct pci_dev *dev = parent;
211
	int result;
212

213 214 215
	/* Must reset in this function */
	e_info->error_dev_num = 0;

216
	/* Is Root Port an agent that sends error message? */
217 218
	result = find_device_iter(dev, e_info);
	if (result)
219
		return true;
220

221
	pci_walk_bus(parent->subordinate, find_device_iter, e_info);
222 223

	if (!e_info->error_dev_num) {
224 225
		pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
			   e_info->id);
226 227 228
		return false;
	}
	return true;
229 230 231 232 233 234 235 236 237
}

/**
 * handle_error_source - handle logging error into an event log
 * @aerdev: pointer to pcie_device data structure of the root port
 * @dev: pointer to pci_dev data structure of error source device
 * @info: comprehensive error information
 *
 * Invoked when an error being detected by Root Port.
R
Randy Dunlap 已提交
238
 */
239
static void handle_error_source(struct pcie_device *aerdev,
240
	struct pci_dev *dev,
241
	struct aer_err_info *info)
242 243 244
{
	int pos;

245
	if (info->severity == AER_CORRECTABLE) {
246
		/*
247
		 * Correctable error does not need software intervention.
248 249
		 * No need to go through error recovery process.
		 */
K
Keith Busch 已提交
250
		pos = dev->aer_cap;
251 252
		if (pos)
			pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
253
					info->status);
254
	} else if (info->severity == AER_NONFATAL)
255
		pcie_do_nonfatal_recovery(dev);
256
	else if (info->severity == AER_FATAL)
257
		pcie_do_fatal_recovery(dev);
258 259
}

260 261 262 263 264 265
#ifdef CONFIG_ACPI_APEI_PCIEAER
static void aer_recover_work_func(struct work_struct *work);

#define AER_RECOVER_RING_ORDER		4
#define AER_RECOVER_RING_SIZE		(1 << AER_RECOVER_RING_ORDER)

R
Ryan Desfosses 已提交
266
struct aer_recover_entry {
267 268 269 270
	u8	bus;
	u8	devfn;
	u16	domain;
	int	severity;
271
	struct aer_capability_regs *regs;
272 273 274 275 276 277 278 279 280 281 282 283 284
};

static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
		    AER_RECOVER_RING_SIZE);
/*
 * Mutual exclusion for writers of aer_recover_ring, reader side don't
 * need lock, because there is only one reader and lock is not needed
 * between reader and writer.
 */
static DEFINE_SPINLOCK(aer_recover_ring_lock);
static DECLARE_WORK(aer_recover_work, aer_recover_work_func);

void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
285
		       int severity, struct aer_capability_regs *aer_regs)
286 287 288 289 290 291 292
{
	unsigned long flags;
	struct aer_recover_entry entry = {
		.bus		= bus,
		.devfn		= devfn,
		.domain		= domain,
		.severity	= severity,
293
		.regs		= aer_regs,
294 295 296
	};

	spin_lock_irqsave(&aer_recover_ring_lock, flags);
S
Stefani Seibold 已提交
297
	if (kfifo_put(&aer_recover_ring, entry))
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		schedule_work(&aer_recover_work);
	else
		pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
		       domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
	spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
}
EXPORT_SYMBOL_GPL(aer_recover_queue);

static void aer_recover_work_func(struct work_struct *work)
{
	struct aer_recover_entry entry;
	struct pci_dev *pdev;

	while (kfifo_get(&aer_recover_ring, &entry)) {
		pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
						   entry.devfn);
		if (!pdev) {
			pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
			       entry.domain, entry.bus,
			       PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
			continue;
		}
320
		cper_print_aer(pdev, entry.severity, entry.regs);
321
		if (entry.severity == AER_NONFATAL)
322
			pcie_do_nonfatal_recovery(pdev);
323
		else if (entry.severity == AER_FATAL)
324
			pcie_do_fatal_recovery(pdev);
325
		pci_dev_put(pdev);
326 327 328 329
	}
}
#endif

330 331 332 333 334 335
/**
 * get_device_error_info - read error status from dev and store it to info
 * @dev: pointer to the device expected to have a error record
 * @info: pointer to structure to store the error record
 *
 * Return 1 on success, 0 on error.
336 337
 *
 * Note that @info is reused among all error devices. Clear fields properly.
338
 */
339 340
static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
{
341
	int pos, temp;
342

343
	/* Must reset in this function */
344
	info->status = 0;
H
Hidetoshi Seto 已提交
345
	info->tlp_header_valid = 0;
346

K
Keith Busch 已提交
347
	pos = dev->aer_cap;
348 349 350

	/* The device might not support AER */
	if (!pos)
351
		return 0;
352 353 354 355

	if (info->severity == AER_CORRECTABLE) {
		pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
			&info->status);
356 357 358
		pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
			&info->mask);
		if (!(info->status & ~info->mask))
359
			return 0;
360
	} else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
361 362 363 364 365
		info->severity == AER_NONFATAL) {

		/* Link is still healthy for IO reads */
		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
			&info->status);
366 367 368
		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
			&info->mask);
		if (!(info->status & ~info->mask))
369
			return 0;
370

371 372
		/* Get First Error Pointer */
		pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
H
Hidetoshi Seto 已提交
373
		info->first_error = PCI_ERR_CAP_FEP(temp);
374

375
		if (info->status & AER_LOG_TLP_MASKS) {
H
Hidetoshi Seto 已提交
376
			info->tlp_header_valid = 1;
377 378 379 380 381 382 383 384 385 386 387
			pci_read_config_dword(dev,
				pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
			pci_read_config_dword(dev,
				pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
			pci_read_config_dword(dev,
				pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
			pci_read_config_dword(dev,
				pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
		}
	}

388
	return 1;
389 390
}

Z
Zhang, Yanmin 已提交
391 392 393 394 395
static inline void aer_process_err_devices(struct pcie_device *p_device,
			struct aer_err_info *e_info)
{
	int i;

396
	/* Report all before handle them, not to lost records by reset etc. */
Z
Zhang, Yanmin 已提交
397
	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
398
		if (get_device_error_info(e_info->dev[i], e_info))
Z
Zhang, Yanmin 已提交
399
			aer_print_error(e_info->dev[i], e_info);
400 401 402 403
	}
	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
		if (get_device_error_info(e_info->dev[i], e_info))
			handle_error_source(p_device, e_info->dev[i], e_info);
Z
Zhang, Yanmin 已提交
404 405 406
	}
}

407 408 409 410
/**
 * aer_isr_one_error - consume an error detected by root port
 * @p_device: pointer to error root port service device
 * @e_src: pointer to an error source
R
Randy Dunlap 已提交
411
 */
412 413 414
static void aer_isr_one_error(struct pcie_device *p_device,
		struct aer_err_source *e_src)
{
415 416
	struct aer_rpc *rpc = get_service_data(p_device);
	struct aer_err_info *e_info = &rpc->e_info;
417 418 419 420 421

	/*
	 * There is a possibility that both correctable error and
	 * uncorrectable error being logged. Report correctable error first.
	 */
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
		e_info->id = ERR_COR_ID(e_src->id);
		e_info->severity = AER_CORRECTABLE;

		if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
			e_info->multi_error_valid = 1;
		else
			e_info->multi_error_valid = 0;

		aer_print_port_info(p_device->port, e_info);

		if (find_source_device(p_device->port, e_info))
			aer_process_err_devices(p_device, e_info);
	}

	if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
		e_info->id = ERR_UNCOR_ID(e_src->id);

		if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
			e_info->severity = AER_FATAL;
		else
			e_info->severity = AER_NONFATAL;

		if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
H
Hidetoshi Seto 已提交
446
			e_info->multi_error_valid = 1;
447 448
		else
			e_info->multi_error_valid = 0;
449

450 451
		aer_print_port_info(p_device->port, e_info);

452 453
		if (find_source_device(p_device->port, e_info))
			aer_process_err_devices(p_device, e_info);
454 455 456
	}
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
/**
 * get_e_source - retrieve an error source
 * @rpc: pointer to the root port which holds an error
 * @e_src: pointer to store retrieved error source
 *
 * Return 1 if an error source is retrieved, otherwise 0.
 *
 * Invoked by DPC handler to consume an error.
 */
static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
{
	unsigned long flags;

	/* Lock access to Root error producer/consumer index */
	spin_lock_irqsave(&rpc->e_lock, flags);
472 473 474
	if (rpc->prod_idx == rpc->cons_idx) {
		spin_unlock_irqrestore(&rpc->e_lock, flags);
		return 0;
475
	}
476 477 478 479 480

	*e_src = rpc->e_sources[rpc->cons_idx];
	rpc->cons_idx++;
	if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
		rpc->cons_idx = 0;
481 482
	spin_unlock_irqrestore(&rpc->e_lock, flags);

483
	return 1;
484 485
}

486 487
/**
 * aer_isr - consume errors detected by root port
488
 * @work: definition of this work item
489 490
 *
 * Invoked, as DPC, when root port records new detected error
R
Randy Dunlap 已提交
491
 */
492
void aer_isr(struct work_struct *work)
493
{
494 495
	struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
	struct pcie_device *p_device = rpc->rpd;
496
	struct aer_err_source uninitialized_var(e_src);
497 498

	mutex_lock(&rpc->rpc_mutex);
499 500
	while (get_e_source(rpc, &e_src))
		aer_isr_one_error(p_device, &e_src);
501 502
	mutex_unlock(&rpc->rpc_mutex);
}