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