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