• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * drivers/pci/pcie/aer/aerdrv_core.c
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * This file implements the core part of PCI-Express AER. When an pci-express
9  * error is delivered, an error message will be collected and printed to
10  * console, then, an error recovery procedure will be executed by following
11  * the pci error recovery rules.
12  *
13  * Copyright (C) 2006 Intel Corp.
14  *	Tom Long Nguyen (tom.l.nguyen@intel.com)
15  *	Zhang Yanmin (yanmin.zhang@intel.com)
16  *
17  */
18 
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/pm.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/kfifo.h>
28 #include "aerdrv.h"
29 
30 static bool forceload;
31 static bool nosourceid;
32 module_param(forceload, bool, 0);
33 module_param(nosourceid, bool, 0);
34 
35 #define	PCI_EXP_AER_FLAGS	(PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
36 				 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
37 
pci_enable_pcie_error_reporting(struct pci_dev * dev)38 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
39 {
40 	if (pcie_aer_get_firmware_first(dev))
41 		return -EIO;
42 
43 	if (!pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR))
44 		return -EIO;
45 
46 	return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
47 }
48 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
49 
pci_disable_pcie_error_reporting(struct pci_dev * dev)50 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
51 {
52 	if (pcie_aer_get_firmware_first(dev))
53 		return -EIO;
54 
55 	return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
56 					  PCI_EXP_AER_FLAGS);
57 }
58 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
59 
pci_cleanup_aer_uncorrect_error_status(struct pci_dev * dev)60 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
61 {
62 	int pos;
63 	u32 status;
64 
65 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
66 	if (!pos)
67 		return -EIO;
68 
69 	pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
70 	if (status)
71 		pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
72 
73 	return 0;
74 }
75 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
76 
77 /**
78  * add_error_device - list device to be handled
79  * @e_info: pointer to error info
80  * @dev: pointer to pci_dev to be added
81  */
add_error_device(struct aer_err_info * e_info,struct pci_dev * dev)82 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
83 {
84 	if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
85 		e_info->dev[e_info->error_dev_num] = dev;
86 		e_info->error_dev_num++;
87 		return 0;
88 	}
89 	return -ENOSPC;
90 }
91 
92 /**
93  * is_error_source - check whether the device is source of reported error
94  * @dev: pointer to pci_dev to be checked
95  * @e_info: pointer to reported error info
96  */
is_error_source(struct pci_dev * dev,struct aer_err_info * e_info)97 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
98 {
99 	int pos;
100 	u32 status, mask;
101 	u16 reg16;
102 
103 	/*
104 	 * When bus id is equal to 0, it might be a bad id
105 	 * reported by root port.
106 	 */
107 	if (!nosourceid && (PCI_BUS_NUM(e_info->id) != 0)) {
108 		/* Device ID match? */
109 		if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
110 			return true;
111 
112 		/* Continue id comparing if there is no multiple error */
113 		if (!e_info->multi_error_valid)
114 			return false;
115 	}
116 
117 	/*
118 	 * When either
119 	 *      1) nosourceid==y;
120 	 *      2) bus id is equal to 0. Some ports might lose the bus
121 	 *              id of error source id;
122 	 *      3) There are multiple errors and prior id comparing fails;
123 	 * We check AER status registers to find possible reporter.
124 	 */
125 	if (atomic_read(&dev->enable_cnt) == 0)
126 		return false;
127 
128 	/* Check if AER is enabled */
129 	pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
130 	if (!(reg16 & PCI_EXP_AER_FLAGS))
131 		return false;
132 
133 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
134 	if (!pos)
135 		return false;
136 
137 	/* Check if error is recorded */
138 	if (e_info->severity == AER_CORRECTABLE) {
139 		pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
140 		pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
141 	} else {
142 		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
143 		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
144 	}
145 	if (status & ~mask)
146 		return true;
147 
148 	return false;
149 }
150 
find_device_iter(struct pci_dev * dev,void * data)151 static int find_device_iter(struct pci_dev *dev, void *data)
152 {
153 	struct aer_err_info *e_info = (struct aer_err_info *)data;
154 
155 	if (is_error_source(dev, e_info)) {
156 		/* List this device */
157 		if (add_error_device(e_info, dev)) {
158 			/* We cannot handle more... Stop iteration */
159 			/* TODO: Should print error message here? */
160 			return 1;
161 		}
162 
163 		/* If there is only a single error, stop iteration */
164 		if (!e_info->multi_error_valid)
165 			return 1;
166 	}
167 	return 0;
168 }
169 
170 /**
171  * find_source_device - search through device hierarchy for source device
172  * @parent: pointer to Root Port pci_dev data structure
173  * @e_info: including detailed error information such like id
174  *
175  * Return true if found.
176  *
177  * Invoked by DPC when error is detected at the Root Port.
178  * Caller of this function must set id, severity, and multi_error_valid of
179  * struct aer_err_info pointed by @e_info properly.  This function must fill
180  * e_info->error_dev_num and e_info->dev[], based on the given information.
181  */
find_source_device(struct pci_dev * parent,struct aer_err_info * e_info)182 static bool find_source_device(struct pci_dev *parent,
183 		struct aer_err_info *e_info)
184 {
185 	struct pci_dev *dev = parent;
186 	int result;
187 
188 	/* Must reset in this function */
189 	e_info->error_dev_num = 0;
190 
191 	/* Is Root Port an agent that sends error message? */
192 	result = find_device_iter(dev, e_info);
193 	if (result)
194 		return true;
195 
196 	pci_walk_bus(parent->subordinate, find_device_iter, e_info);
197 
198 	if (!e_info->error_dev_num) {
199 		dev_printk(KERN_DEBUG, &parent->dev,
200 				"can't find device of ID%04x\n",
201 				e_info->id);
202 		return false;
203 	}
204 	return true;
205 }
206 
report_error_detected(struct pci_dev * dev,void * data)207 static int report_error_detected(struct pci_dev *dev, void *data)
208 {
209 	pci_ers_result_t vote;
210 	const struct pci_error_handlers *err_handler;
211 	struct aer_broadcast_data *result_data;
212 	result_data = (struct aer_broadcast_data *) data;
213 
214 	device_lock(&dev->dev);
215 	dev->error_state = result_data->state;
216 
217 	if (!dev->driver ||
218 		!dev->driver->err_handler ||
219 		!dev->driver->err_handler->error_detected) {
220 		if (result_data->state == pci_channel_io_frozen &&
221 			!(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
222 			/*
223 			 * In case of fatal recovery, if one of down-
224 			 * stream device has no driver. We might be
225 			 * unable to recover because a later insmod
226 			 * of a driver for this device is unaware of
227 			 * its hw state.
228 			 */
229 			dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
230 				   dev->driver ?
231 				   "no AER-aware driver" : "no driver");
232 		}
233 
234 		/*
235 		 * If there's any device in the subtree that does not
236 		 * have an error_detected callback, returning
237 		 * PCI_ERS_RESULT_NO_AER_DRIVER prevents calling of
238 		 * the subsequent mmio_enabled/slot_reset/resume
239 		 * callbacks of "any" device in the subtree. All the
240 		 * devices in the subtree are left in the error state
241 		 * without recovery.
242 		 */
243 
244 		if (!(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE))
245 			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
246 		else
247 			vote = PCI_ERS_RESULT_NONE;
248 	} else {
249 		err_handler = dev->driver->err_handler;
250 		vote = err_handler->error_detected(dev, result_data->state);
251 	}
252 
253 	result_data->result = merge_result(result_data->result, vote);
254 	device_unlock(&dev->dev);
255 	return 0;
256 }
257 
report_mmio_enabled(struct pci_dev * dev,void * data)258 static int report_mmio_enabled(struct pci_dev *dev, void *data)
259 {
260 	pci_ers_result_t vote;
261 	const struct pci_error_handlers *err_handler;
262 	struct aer_broadcast_data *result_data;
263 	result_data = (struct aer_broadcast_data *) data;
264 
265 	device_lock(&dev->dev);
266 	if (!dev->driver ||
267 		!dev->driver->err_handler ||
268 		!dev->driver->err_handler->mmio_enabled)
269 		goto out;
270 
271 	err_handler = dev->driver->err_handler;
272 	vote = err_handler->mmio_enabled(dev);
273 	result_data->result = merge_result(result_data->result, vote);
274 out:
275 	device_unlock(&dev->dev);
276 	return 0;
277 }
278 
report_slot_reset(struct pci_dev * dev,void * data)279 static int report_slot_reset(struct pci_dev *dev, void *data)
280 {
281 	pci_ers_result_t vote;
282 	const struct pci_error_handlers *err_handler;
283 	struct aer_broadcast_data *result_data;
284 	result_data = (struct aer_broadcast_data *) data;
285 
286 	device_lock(&dev->dev);
287 	if (!dev->driver ||
288 		!dev->driver->err_handler ||
289 		!dev->driver->err_handler->slot_reset)
290 		goto out;
291 
292 	err_handler = dev->driver->err_handler;
293 	vote = err_handler->slot_reset(dev);
294 	result_data->result = merge_result(result_data->result, vote);
295 out:
296 	device_unlock(&dev->dev);
297 	return 0;
298 }
299 
report_resume(struct pci_dev * dev,void * data)300 static int report_resume(struct pci_dev *dev, void *data)
301 {
302 	const struct pci_error_handlers *err_handler;
303 
304 	device_lock(&dev->dev);
305 	dev->error_state = pci_channel_io_normal;
306 
307 	if (!dev->driver ||
308 		!dev->driver->err_handler ||
309 		!dev->driver->err_handler->resume)
310 		goto out;
311 
312 	err_handler = dev->driver->err_handler;
313 	err_handler->resume(dev);
314 out:
315 	device_unlock(&dev->dev);
316 	return 0;
317 }
318 
319 /**
320  * broadcast_error_message - handle message broadcast to downstream drivers
321  * @dev: pointer to from where in a hierarchy message is broadcasted down
322  * @state: error state
323  * @error_mesg: message to print
324  * @cb: callback to be broadcasted
325  *
326  * Invoked during error recovery process. Once being invoked, the content
327  * of error severity will be broadcasted to all downstream drivers in a
328  * hierarchy in question.
329  */
broadcast_error_message(struct pci_dev * dev,enum pci_channel_state state,char * error_mesg,int (* cb)(struct pci_dev *,void *))330 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
331 	enum pci_channel_state state,
332 	char *error_mesg,
333 	int (*cb)(struct pci_dev *, void *))
334 {
335 	struct aer_broadcast_data result_data;
336 
337 	dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
338 	result_data.state = state;
339 	if (cb == report_error_detected)
340 		result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
341 	else
342 		result_data.result = PCI_ERS_RESULT_RECOVERED;
343 
344 	if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
345 		/*
346 		 * If the error is reported by a bridge, we think this error
347 		 * is related to the downstream link of the bridge, so we
348 		 * do error recovery on all subordinates of the bridge instead
349 		 * of the bridge and clear the error status of the bridge.
350 		 */
351 		if (cb == report_error_detected)
352 			dev->error_state = state;
353 		pci_walk_bus(dev->subordinate, cb, &result_data);
354 		if (cb == report_resume) {
355 			pci_cleanup_aer_uncorrect_error_status(dev);
356 			dev->error_state = pci_channel_io_normal;
357 		}
358 	} else {
359 		/*
360 		 * If the error is reported by an end point, we think this
361 		 * error is related to the upstream link of the end point.
362 		 */
363 		if (state == pci_channel_io_normal)
364 			/*
365 			 * the error is non fatal so the bus is ok, just invoke
366 			 * the callback for the function that logged the error.
367 			 */
368 			cb(dev, &result_data);
369 		else
370 			pci_walk_bus(dev->bus, cb, &result_data);
371 	}
372 
373 	return result_data.result;
374 }
375 
376 /**
377  * default_reset_link - default reset function
378  * @dev: pointer to pci_dev data structure
379  *
380  * Invoked when performing link reset on a Downstream Port or a
381  * Root Port with no aer driver.
382  */
default_reset_link(struct pci_dev * dev)383 static pci_ers_result_t default_reset_link(struct pci_dev *dev)
384 {
385 	pci_reset_bridge_secondary_bus(dev);
386 	dev_printk(KERN_DEBUG, &dev->dev, "downstream link has been reset\n");
387 	return PCI_ERS_RESULT_RECOVERED;
388 }
389 
find_aer_service_iter(struct device * device,void * data)390 static int find_aer_service_iter(struct device *device, void *data)
391 {
392 	struct pcie_port_service_driver *service_driver, **drv;
393 
394 	drv = (struct pcie_port_service_driver **) data;
395 
396 	if (device->bus == &pcie_port_bus_type && device->driver) {
397 		service_driver = to_service_driver(device->driver);
398 		if (service_driver->service == PCIE_PORT_SERVICE_AER) {
399 			*drv = service_driver;
400 			return 1;
401 		}
402 	}
403 
404 	return 0;
405 }
406 
find_aer_service(struct pci_dev * dev)407 static struct pcie_port_service_driver *find_aer_service(struct pci_dev *dev)
408 {
409 	struct pcie_port_service_driver *drv = NULL;
410 
411 	device_for_each_child(&dev->dev, &drv, find_aer_service_iter);
412 
413 	return drv;
414 }
415 
reset_link(struct pci_dev * dev)416 static pci_ers_result_t reset_link(struct pci_dev *dev)
417 {
418 	struct pci_dev *udev;
419 	pci_ers_result_t status;
420 	struct pcie_port_service_driver *driver;
421 
422 	if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
423 		/* Reset this port for all subordinates */
424 		udev = dev;
425 	} else {
426 		/* Reset the upstream component (likely downstream port) */
427 		udev = dev->bus->self;
428 	}
429 
430 	/* Use the aer driver of the component firstly */
431 	driver = find_aer_service(udev);
432 
433 	if (driver && driver->reset_link) {
434 		status = driver->reset_link(udev);
435 	} else if (pci_pcie_type(udev) == PCI_EXP_TYPE_DOWNSTREAM ||
436 		pci_pcie_type(udev) == PCI_EXP_TYPE_ROOT_PORT) {
437 		status = default_reset_link(udev);
438 	} else {
439 		dev_printk(KERN_DEBUG, &dev->dev,
440 			"no link-reset support at upstream device %s\n",
441 			pci_name(udev));
442 		return PCI_ERS_RESULT_DISCONNECT;
443 	}
444 
445 	if (status != PCI_ERS_RESULT_RECOVERED) {
446 		dev_printk(KERN_DEBUG, &dev->dev,
447 			"link reset at upstream device %s failed\n",
448 			pci_name(udev));
449 		return PCI_ERS_RESULT_DISCONNECT;
450 	}
451 
452 	return status;
453 }
454 
455 /**
456  * do_recovery - handle nonfatal/fatal error recovery process
457  * @dev: pointer to a pci_dev data structure of agent detecting an error
458  * @severity: error severity type
459  *
460  * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
461  * error detected message to all downstream drivers within a hierarchy in
462  * question and return the returned code.
463  */
do_recovery(struct pci_dev * dev,int severity)464 static void do_recovery(struct pci_dev *dev, int severity)
465 {
466 	pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
467 	enum pci_channel_state state;
468 
469 	if (severity == AER_FATAL)
470 		state = pci_channel_io_frozen;
471 	else
472 		state = pci_channel_io_normal;
473 
474 	status = broadcast_error_message(dev,
475 			state,
476 			"error_detected",
477 			report_error_detected);
478 
479 	if (severity == AER_FATAL) {
480 		result = reset_link(dev);
481 		if (result != PCI_ERS_RESULT_RECOVERED)
482 			goto failed;
483 	}
484 
485 	if (status == PCI_ERS_RESULT_CAN_RECOVER)
486 		status = broadcast_error_message(dev,
487 				state,
488 				"mmio_enabled",
489 				report_mmio_enabled);
490 
491 	if (status == PCI_ERS_RESULT_NEED_RESET) {
492 		/*
493 		 * TODO: Should call platform-specific
494 		 * functions to reset slot before calling
495 		 * drivers' slot_reset callbacks?
496 		 */
497 		status = broadcast_error_message(dev,
498 				state,
499 				"slot_reset",
500 				report_slot_reset);
501 	}
502 
503 	if (status != PCI_ERS_RESULT_RECOVERED)
504 		goto failed;
505 
506 	broadcast_error_message(dev,
507 				state,
508 				"resume",
509 				report_resume);
510 
511 	dev_info(&dev->dev, "AER: Device recovery successful\n");
512 	return;
513 
514 failed:
515 	/* TODO: Should kernel panic here? */
516 	dev_info(&dev->dev, "AER: Device recovery failed\n");
517 }
518 
519 /**
520  * handle_error_source - handle logging error into an event log
521  * @aerdev: pointer to pcie_device data structure of the root port
522  * @dev: pointer to pci_dev data structure of error source device
523  * @info: comprehensive error information
524  *
525  * Invoked when an error being detected by Root Port.
526  */
handle_error_source(struct pcie_device * aerdev,struct pci_dev * dev,struct aer_err_info * info)527 static void handle_error_source(struct pcie_device *aerdev,
528 	struct pci_dev *dev,
529 	struct aer_err_info *info)
530 {
531 	int pos;
532 
533 	if (info->severity == AER_CORRECTABLE) {
534 		/*
535 		 * Correctable error does not need software intervention.
536 		 * No need to go through error recovery process.
537 		 */
538 		pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
539 		if (pos)
540 			pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
541 					info->status);
542 	} else
543 		do_recovery(dev, info->severity);
544 }
545 
546 #ifdef CONFIG_ACPI_APEI_PCIEAER
547 static void aer_recover_work_func(struct work_struct *work);
548 
549 #define AER_RECOVER_RING_ORDER		4
550 #define AER_RECOVER_RING_SIZE		(1 << AER_RECOVER_RING_ORDER)
551 
552 struct aer_recover_entry {
553 	u8	bus;
554 	u8	devfn;
555 	u16	domain;
556 	int	severity;
557 	struct aer_capability_regs *regs;
558 };
559 
560 static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
561 		    AER_RECOVER_RING_SIZE);
562 /*
563  * Mutual exclusion for writers of aer_recover_ring, reader side don't
564  * need lock, because there is only one reader and lock is not needed
565  * between reader and writer.
566  */
567 static DEFINE_SPINLOCK(aer_recover_ring_lock);
568 static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
569 
aer_recover_queue(int domain,unsigned int bus,unsigned int devfn,int severity,struct aer_capability_regs * aer_regs)570 void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
571 		       int severity, struct aer_capability_regs *aer_regs)
572 {
573 	unsigned long flags;
574 	struct aer_recover_entry entry = {
575 		.bus		= bus,
576 		.devfn		= devfn,
577 		.domain		= domain,
578 		.severity	= severity,
579 		.regs		= aer_regs,
580 	};
581 
582 	spin_lock_irqsave(&aer_recover_ring_lock, flags);
583 	if (kfifo_put(&aer_recover_ring, entry))
584 		schedule_work(&aer_recover_work);
585 	else
586 		pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
587 		       domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
588 	spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
589 }
590 EXPORT_SYMBOL_GPL(aer_recover_queue);
591 
aer_recover_work_func(struct work_struct * work)592 static void aer_recover_work_func(struct work_struct *work)
593 {
594 	struct aer_recover_entry entry;
595 	struct pci_dev *pdev;
596 
597 	while (kfifo_get(&aer_recover_ring, &entry)) {
598 		pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
599 						   entry.devfn);
600 		if (!pdev) {
601 			pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
602 			       entry.domain, entry.bus,
603 			       PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
604 			continue;
605 		}
606 		cper_print_aer(pdev, entry.severity, entry.regs);
607 		do_recovery(pdev, entry.severity);
608 		pci_dev_put(pdev);
609 	}
610 }
611 #endif
612 
613 /**
614  * get_device_error_info - read error status from dev and store it to info
615  * @dev: pointer to the device expected to have a error record
616  * @info: pointer to structure to store the error record
617  *
618  * Return 1 on success, 0 on error.
619  *
620  * Note that @info is reused among all error devices. Clear fields properly.
621  */
get_device_error_info(struct pci_dev * dev,struct aer_err_info * info)622 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
623 {
624 	int pos, temp;
625 
626 	/* Must reset in this function */
627 	info->status = 0;
628 	info->tlp_header_valid = 0;
629 
630 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
631 
632 	/* The device might not support AER */
633 	if (!pos)
634 		return 1;
635 
636 	if (info->severity == AER_CORRECTABLE) {
637 		pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
638 			&info->status);
639 		pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
640 			&info->mask);
641 		if (!(info->status & ~info->mask))
642 			return 0;
643 	} else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
644 		info->severity == AER_NONFATAL) {
645 
646 		/* Link is still healthy for IO reads */
647 		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
648 			&info->status);
649 		pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
650 			&info->mask);
651 		if (!(info->status & ~info->mask))
652 			return 0;
653 
654 		/* Get First Error Pointer */
655 		pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
656 		info->first_error = PCI_ERR_CAP_FEP(temp);
657 
658 		if (info->status & AER_LOG_TLP_MASKS) {
659 			info->tlp_header_valid = 1;
660 			pci_read_config_dword(dev,
661 				pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
662 			pci_read_config_dword(dev,
663 				pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
664 			pci_read_config_dword(dev,
665 				pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
666 			pci_read_config_dword(dev,
667 				pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
668 		}
669 	}
670 
671 	return 1;
672 }
673 
aer_process_err_devices(struct pcie_device * p_device,struct aer_err_info * e_info)674 static inline void aer_process_err_devices(struct pcie_device *p_device,
675 			struct aer_err_info *e_info)
676 {
677 	int i;
678 
679 	/* Report all before handle them, not to lost records by reset etc. */
680 	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
681 		if (get_device_error_info(e_info->dev[i], e_info))
682 			aer_print_error(e_info->dev[i], e_info);
683 	}
684 	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
685 		if (get_device_error_info(e_info->dev[i], e_info))
686 			handle_error_source(p_device, e_info->dev[i], e_info);
687 	}
688 }
689 
690 /**
691  * aer_isr_one_error - consume an error detected by root port
692  * @p_device: pointer to error root port service device
693  * @e_src: pointer to an error source
694  */
aer_isr_one_error(struct pcie_device * p_device,struct aer_err_source * e_src)695 static void aer_isr_one_error(struct pcie_device *p_device,
696 		struct aer_err_source *e_src)
697 {
698 	struct aer_err_info *e_info;
699 
700 	/* struct aer_err_info might be big, so we allocate it with slab */
701 	e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
702 	if (!e_info) {
703 		dev_printk(KERN_DEBUG, &p_device->port->dev,
704 			"Can't allocate mem when processing AER errors\n");
705 		return;
706 	}
707 
708 	/*
709 	 * There is a possibility that both correctable error and
710 	 * uncorrectable error being logged. Report correctable error first.
711 	 */
712 	if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
713 		e_info->id = ERR_COR_ID(e_src->id);
714 		e_info->severity = AER_CORRECTABLE;
715 
716 		if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
717 			e_info->multi_error_valid = 1;
718 		else
719 			e_info->multi_error_valid = 0;
720 
721 		aer_print_port_info(p_device->port, e_info);
722 
723 		if (find_source_device(p_device->port, e_info))
724 			aer_process_err_devices(p_device, e_info);
725 	}
726 
727 	if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
728 		e_info->id = ERR_UNCOR_ID(e_src->id);
729 
730 		if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
731 			e_info->severity = AER_FATAL;
732 		else
733 			e_info->severity = AER_NONFATAL;
734 
735 		if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
736 			e_info->multi_error_valid = 1;
737 		else
738 			e_info->multi_error_valid = 0;
739 
740 		aer_print_port_info(p_device->port, e_info);
741 
742 		if (find_source_device(p_device->port, e_info))
743 			aer_process_err_devices(p_device, e_info);
744 	}
745 
746 	kfree(e_info);
747 }
748 
749 /**
750  * get_e_source - retrieve an error source
751  * @rpc: pointer to the root port which holds an error
752  * @e_src: pointer to store retrieved error source
753  *
754  * Return 1 if an error source is retrieved, otherwise 0.
755  *
756  * Invoked by DPC handler to consume an error.
757  */
get_e_source(struct aer_rpc * rpc,struct aer_err_source * e_src)758 static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
759 {
760 	unsigned long flags;
761 
762 	/* Lock access to Root error producer/consumer index */
763 	spin_lock_irqsave(&rpc->e_lock, flags);
764 	if (rpc->prod_idx == rpc->cons_idx) {
765 		spin_unlock_irqrestore(&rpc->e_lock, flags);
766 		return 0;
767 	}
768 
769 	*e_src = rpc->e_sources[rpc->cons_idx];
770 	rpc->cons_idx++;
771 	if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
772 		rpc->cons_idx = 0;
773 	spin_unlock_irqrestore(&rpc->e_lock, flags);
774 
775 	return 1;
776 }
777 
778 /**
779  * aer_isr - consume errors detected by root port
780  * @work: definition of this work item
781  *
782  * Invoked, as DPC, when root port records new detected error
783  */
aer_isr(struct work_struct * work)784 void aer_isr(struct work_struct *work)
785 {
786 	struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
787 	struct pcie_device *p_device = rpc->rpd;
788 	struct aer_err_source uninitialized_var(e_src);
789 
790 	mutex_lock(&rpc->rpc_mutex);
791 	while (get_e_source(rpc, &e_src))
792 		aer_isr_one_error(p_device, &e_src);
793 	mutex_unlock(&rpc->rpc_mutex);
794 }
795 
796 /**
797  * aer_init - provide AER initialization
798  * @dev: pointer to AER pcie device
799  *
800  * Invoked when AER service driver is loaded.
801  */
aer_init(struct pcie_device * dev)802 int aer_init(struct pcie_device *dev)
803 {
804 	if (forceload) {
805 		dev_printk(KERN_DEBUG, &dev->device,
806 			   "aerdrv forceload requested.\n");
807 		pcie_aer_force_firmware_first(dev->port, 0);
808 	}
809 	return 0;
810 }
811