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