blob: f8ffa47503b746fcf6d10443adbc6e27791efbc4 [file] [log] [blame]
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001/*
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>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080025#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080027#include "aerdrv.h"
28
29static int forceload;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +080030static int nosourceid;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080031module_param(forceload, bool, 0);
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +080032module_param(nosourceid, bool, 0);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080033
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080034int pci_enable_pcie_error_reporting(struct pci_dev *dev)
35{
36 u16 reg16 = 0;
37 int pos;
38
Matt Domsch05843962009-11-02 11:51:24 -060039 if (dev->aer_firmware_first)
40 return -EIO;
41
Yu Zhao270c66b2008-10-19 20:35:20 +080042 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080043 if (!pos)
44 return -EIO;
45
Kenji Kaneshige39a53062009-11-11 14:31:38 +090046 pos = pci_pcie_cap(dev);
Jesse Barnes09276782008-10-18 17:33:19 -070047 if (!pos)
48 return -EIO;
49
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080050 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;
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090056 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
57
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080058 return 0;
59}
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090060EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080061
62int pci_disable_pcie_error_reporting(struct pci_dev *dev)
63{
64 u16 reg16 = 0;
65 int pos;
66
Matt Domsch05843962009-11-02 11:51:24 -060067 if (dev->aer_firmware_first)
68 return -EIO;
69
Kenji Kaneshige39a53062009-11-11 14:31:38 +090070 pos = pci_pcie_cap(dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080071 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);
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090079 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
80
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080081 return 0;
82}
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090083EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080084
85int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
86{
87 int pos;
Andrew Patterson6cdfd992009-12-03 10:28:20 -070088 u32 status;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080089
Jesse Barnes09276782008-10-18 17:33:19 -070090 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080091 if (!pos)
92 return -EIO;
93
94 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
Andrew Patterson6cdfd992009-12-03 10:28:20 -070095 if (status)
96 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080097
98 return 0;
99}
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900100EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800101
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800102static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
103{
104 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
105 e_info->dev[e_info->error_dev_num] = dev;
106 e_info->error_dev_num++;
107 return 1;
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900108 }
109
110 return 0;
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800111}
112
113
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800114#define PCI_BUS(x) (((x) >> 8) & 0xff)
115
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900116/**
117 * is_error_source - check whether the device is source of reported error
118 * @dev: pointer to pci_dev to be checked
119 * @e_info: pointer to reported error info
120 */
121static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800122{
123 int pos;
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900124 u32 status, mask;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800125 u16 reg16;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800126
127 /*
128 * When bus id is equal to 0, it might be a bad id
129 * reported by root port.
130 */
131 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
Hidetoshi Setobd17d472010-04-15 13:13:41 +0900132 /* Device ID match? */
133 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900134 return true;
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800135
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900136 /* Continue id comparing if there is no multiple error */
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900137 if (!e_info->multi_error_valid)
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900138 return false;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800139 }
140
141 /*
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800142 * When either
143 * 1) nosourceid==y;
144 * 2) bus id is equal to 0. Some ports might lose the bus
145 * id of error source id;
146 * 3) There are multiple errors and prior id comparing fails;
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900147 * We check AER status registers to find possible reporter.
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800148 */
149 if (atomic_read(&dev->enable_cnt) == 0)
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900150 return false;
Kenji Kaneshige39a53062009-11-11 14:31:38 +0900151 pos = pci_pcie_cap(dev);
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800152 if (!pos)
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900153 return false;
154
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800155 /* Check if AER is enabled */
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900156 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &reg16);
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800157 if (!(reg16 & (
158 PCI_EXP_DEVCTL_CERE |
159 PCI_EXP_DEVCTL_NFERE |
160 PCI_EXP_DEVCTL_FERE |
161 PCI_EXP_DEVCTL_URRE)))
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900162 return false;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800163 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
164 if (!pos)
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900165 return false;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800166
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900167 /* Check if error is recorded */
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800168 if (e_info->severity == AER_CORRECTABLE) {
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900169 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
170 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800171 } else {
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900172 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
173 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800174 }
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900175 if (status & ~mask)
176 return true;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800177
Hidetoshi Setoc8872752010-04-15 13:12:21 +0900178 return false;
179}
180
181static int find_device_iter(struct pci_dev *dev, void *data)
182{
183 struct aer_err_info *e_info = (struct aer_err_info *)data;
184
185 if (is_error_source(dev, e_info)) {
186 add_error_device(e_info, dev);
187
188 /* If there is only a single error, stop iteration */
189 if (!e_info->multi_error_valid)
190 return 1;
191 }
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800192 return 0;
193}
194
195/**
196 * find_source_device - search through device hierarchy for source device
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800197 * @parent: pointer to Root Port pci_dev data structure
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900198 * @e_info: including detailed error information such like id
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800199 *
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900200 * Return true if found.
201 *
202 * Invoked by DPC when error is detected at the Root Port.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800203 */
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900204static bool find_source_device(struct pci_dev *parent,
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800205 struct aer_err_info *e_info)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800206{
207 struct pci_dev *dev = parent;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800208 int result;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800209
210 /* Is Root Port an agent that sends error message? */
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800211 result = find_device_iter(dev, e_info);
212 if (result)
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900213 return true;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800214
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800215 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900216
217 if (!e_info->error_dev_num) {
218 dev_printk(KERN_DEBUG, &parent->dev,
219 "can't find device of ID%04x\n",
220 e_info->id);
221 return false;
222 }
223 return true;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800224}
225
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800226static int report_error_detected(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800227{
228 pci_ers_result_t vote;
229 struct pci_error_handlers *err_handler;
230 struct aer_broadcast_data *result_data;
231 result_data = (struct aer_broadcast_data *) data;
232
233 dev->error_state = result_data->state;
234
235 if (!dev->driver ||
236 !dev->driver->err_handler ||
237 !dev->driver->err_handler->error_detected) {
238 if (result_data->state == pci_channel_io_frozen &&
239 !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
240 /*
241 * In case of fatal recovery, if one of down-
242 * stream device has no driver. We might be
243 * unable to recover because a later insmod
244 * of a driver for this device is unaware of
245 * its hw state.
246 */
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600247 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
248 dev->driver ?
249 "no AER-aware driver" : "no driver");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800250 }
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800251 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800252 }
253
254 err_handler = dev->driver->err_handler;
255 vote = err_handler->error_detected(dev, result_data->state);
256 result_data->result = merge_result(result_data->result, vote);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800257 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800258}
259
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800260static int report_mmio_enabled(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800261{
262 pci_ers_result_t vote;
263 struct pci_error_handlers *err_handler;
264 struct aer_broadcast_data *result_data;
265 result_data = (struct aer_broadcast_data *) data;
266
267 if (!dev->driver ||
268 !dev->driver->err_handler ||
269 !dev->driver->err_handler->mmio_enabled)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800270 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800271
272 err_handler = dev->driver->err_handler;
273 vote = err_handler->mmio_enabled(dev);
274 result_data->result = merge_result(result_data->result, vote);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800275 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800276}
277
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800278static int report_slot_reset(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800279{
280 pci_ers_result_t vote;
281 struct pci_error_handlers *err_handler;
282 struct aer_broadcast_data *result_data;
283 result_data = (struct aer_broadcast_data *) data;
284
285 if (!dev->driver ||
286 !dev->driver->err_handler ||
287 !dev->driver->err_handler->slot_reset)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800288 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800289
290 err_handler = dev->driver->err_handler;
291 vote = err_handler->slot_reset(dev);
292 result_data->result = merge_result(result_data->result, vote);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800293 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800294}
295
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800296static int report_resume(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800297{
298 struct pci_error_handlers *err_handler;
299
300 dev->error_state = pci_channel_io_normal;
301
302 if (!dev->driver ||
303 !dev->driver->err_handler ||
Hidetoshi Setob0b801d2008-12-01 16:31:06 +0900304 !dev->driver->err_handler->resume)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800305 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800306
307 err_handler = dev->driver->err_handler;
308 err_handler->resume(dev);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800309 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800310}
311
312/**
313 * broadcast_error_message - handle message broadcast to downstream drivers
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800314 * @dev: pointer to from where in a hierarchy message is broadcasted down
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800315 * @state: error state
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800316 * @error_mesg: message to print
317 * @cb: callback to be broadcasted
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800318 *
319 * Invoked during error recovery process. Once being invoked, the content
320 * of error severity will be broadcasted to all downstream drivers in a
321 * hierarchy in question.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800322 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800323static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
324 enum pci_channel_state state,
325 char *error_mesg,
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800326 int (*cb)(struct pci_dev *, void *))
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800327{
328 struct aer_broadcast_data result_data;
329
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600330 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800331 result_data.state = state;
332 if (cb == report_error_detected)
333 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
334 else
335 result_data.result = PCI_ERS_RESULT_RECOVERED;
336
337 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
338 /*
339 * If the error is reported by a bridge, we think this error
340 * is related to the downstream link of the bridge, so we
341 * do error recovery on all subordinates of the bridge instead
342 * of the bridge and clear the error status of the bridge.
343 */
344 if (cb == report_error_detected)
345 dev->error_state = state;
346 pci_walk_bus(dev->subordinate, cb, &result_data);
347 if (cb == report_resume) {
348 pci_cleanup_aer_uncorrect_error_status(dev);
349 dev->error_state = pci_channel_io_normal;
350 }
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900351 } else {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800352 /*
353 * If the error is reported by an end point, we think this
354 * error is related to the upstream link of the end point.
355 */
356 pci_walk_bus(dev->bus, cb, &result_data);
357 }
358
359 return result_data.result;
360}
361
362struct find_aer_service_data {
363 struct pcie_port_service_driver *aer_driver;
364 int is_downstream;
365};
366
367static int find_aer_service_iter(struct device *device, void *data)
368{
369 struct device_driver *driver;
370 struct pcie_port_service_driver *service_driver;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800371 struct find_aer_service_data *result;
372
373 result = (struct find_aer_service_data *) data;
374
375 if (device->bus == &pcie_port_bus_type) {
Kenji Kaneshige694f88e2009-11-25 21:06:15 +0900376 struct pcie_device *pcie = to_pcie_device(device);
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100377
Kenji Kaneshige694f88e2009-11-25 21:06:15 +0900378 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800379 result->is_downstream = 1;
380
381 driver = device->driver;
382 if (driver) {
383 service_driver = to_service_driver(driver);
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100384 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800385 result->aer_driver = service_driver;
386 return 1;
387 }
388 }
389 }
390
391 return 0;
392}
393
394static void find_aer_service(struct pci_dev *dev,
395 struct find_aer_service_data *data)
396{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700397 int retval;
398 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800399}
400
401static pci_ers_result_t reset_link(struct pcie_device *aerdev,
402 struct pci_dev *dev)
403{
404 struct pci_dev *udev;
405 pci_ers_result_t status;
406 struct find_aer_service_data data;
407
408 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
409 udev = dev;
410 else
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900411 udev = dev->bus->self;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800412
413 data.is_downstream = 0;
414 data.aer_driver = NULL;
415 find_aer_service(udev, &data);
416
417 /*
418 * Use the aer driver of the error agent firstly.
419 * If it hasn't the aer driver, use the root port's
420 */
421 if (!data.aer_driver || !data.aer_driver->reset_link) {
422 if (data.is_downstream &&
423 aerdev->device.driver &&
424 to_service_driver(aerdev->device.driver)->reset_link) {
425 data.aer_driver =
426 to_service_driver(aerdev->device.driver);
427 } else {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600428 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
429 "support\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800430 return PCI_ERS_RESULT_DISCONNECT;
431 }
432 }
433
434 status = data.aer_driver->reset_link(udev);
435 if (status != PCI_ERS_RESULT_RECOVERED) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600436 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
437 "device %s failed\n", pci_name(udev));
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800438 return PCI_ERS_RESULT_DISCONNECT;
439 }
440
441 return status;
442}
443
444/**
445 * do_recovery - handle nonfatal/fatal error recovery process
446 * @aerdev: pointer to a pcie_device data structure of root port
447 * @dev: pointer to a pci_dev data structure of agent detecting an error
448 * @severity: error severity type
449 *
450 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
451 * error detected message to all downstream drivers within a hierarchy in
452 * question and return the returned code.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800453 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800454static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
455 struct pci_dev *dev,
456 int severity)
457{
458 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
459 enum pci_channel_state state;
460
461 if (severity == AER_FATAL)
462 state = pci_channel_io_frozen;
463 else
464 state = pci_channel_io_normal;
465
466 status = broadcast_error_message(dev,
467 state,
468 "error_detected",
469 report_error_detected);
470
471 if (severity == AER_FATAL) {
472 result = reset_link(aerdev, dev);
473 if (result != PCI_ERS_RESULT_RECOVERED) {
474 /* TODO: Should panic here? */
475 return result;
476 }
477 }
478
479 if (status == PCI_ERS_RESULT_CAN_RECOVER)
480 status = broadcast_error_message(dev,
481 state,
482 "mmio_enabled",
483 report_mmio_enabled);
484
485 if (status == PCI_ERS_RESULT_NEED_RESET) {
486 /*
487 * TODO: Should call platform-specific
488 * functions to reset slot before calling
489 * drivers' slot_reset callbacks?
490 */
491 status = broadcast_error_message(dev,
492 state,
493 "slot_reset",
494 report_slot_reset);
495 }
496
497 if (status == PCI_ERS_RESULT_RECOVERED)
498 broadcast_error_message(dev,
499 state,
500 "resume",
501 report_resume);
502
503 return status;
504}
505
506/**
507 * handle_error_source - handle logging error into an event log
508 * @aerdev: pointer to pcie_device data structure of the root port
509 * @dev: pointer to pci_dev data structure of error source device
510 * @info: comprehensive error information
511 *
512 * Invoked when an error being detected by Root Port.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800513 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900514static void handle_error_source(struct pcie_device *aerdev,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800515 struct pci_dev *dev,
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800516 struct aer_err_info *info)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800517{
518 pci_ers_result_t status = 0;
519 int pos;
520
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800521 if (info->severity == AER_CORRECTABLE) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800522 /*
523 * Correctable error does not need software intevention.
524 * No need to go through error recovery process.
525 */
Jesse Barnes09276782008-10-18 17:33:19 -0700526 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800527 if (pos)
528 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800529 info->status);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800530 } else {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800531 status = do_recovery(aerdev, dev, info->severity);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800532 if (status == PCI_ERS_RESULT_RECOVERED) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600533 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
534 "successfully recovered\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800535 } else {
536 /* TODO: Should kernel panic here? */
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600537 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
538 "recover\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800539 }
540 }
541}
542
543/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800544 * get_e_source - retrieve an error source
545 * @rpc: pointer to the root port which holds an error
546 *
547 * Invoked by DPC handler to consume an error.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800548 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900549static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800550{
551 struct aer_err_source *e_source;
552 unsigned long flags;
553
554 /* Lock access to Root error producer/consumer index */
555 spin_lock_irqsave(&rpc->e_lock, flags);
556 if (rpc->prod_idx == rpc->cons_idx) {
557 spin_unlock_irqrestore(&rpc->e_lock, flags);
558 return NULL;
559 }
560 e_source = &rpc->e_sources[rpc->cons_idx];
561 rpc->cons_idx++;
562 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
563 rpc->cons_idx = 0;
564 spin_unlock_irqrestore(&rpc->e_lock, flags);
565
566 return e_source;
567}
568
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900569/**
570 * get_device_error_info - read error status from dev and store it to info
571 * @dev: pointer to the device expected to have a error record
572 * @info: pointer to structure to store the error record
573 *
574 * Return 1 on success, 0 on error.
575 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800576static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
577{
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900578 int pos, temp;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800579
Hidetoshi Seto1b4ffcf2009-09-07 17:09:58 +0900580 info->status = 0;
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900581 info->tlp_header_valid = 0;
Hidetoshi Seto1b4ffcf2009-09-07 17:09:58 +0900582
Jesse Barnes09276782008-10-18 17:33:19 -0700583 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800584
585 /* The device might not support AER */
586 if (!pos)
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900587 return 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800588
589 if (info->severity == AER_CORRECTABLE) {
590 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
591 &info->status);
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900592 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
593 &info->mask);
594 if (!(info->status & ~info->mask))
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900595 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800596 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
597 info->severity == AER_NONFATAL) {
598
599 /* Link is still healthy for IO reads */
600 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
601 &info->status);
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900602 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
603 &info->mask);
604 if (!(info->status & ~info->mask))
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900605 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800606
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900607 /* Get First Error Pointer */
608 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900609 info->first_error = PCI_ERR_CAP_FEP(temp);
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900610
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800611 if (info->status & AER_LOG_TLP_MASKS) {
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900612 info->tlp_header_valid = 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800613 pci_read_config_dword(dev,
614 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
615 pci_read_config_dword(dev,
616 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
617 pci_read_config_dword(dev,
618 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
619 pci_read_config_dword(dev,
620 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
621 }
622 }
623
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900624 return 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800625}
626
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800627static inline void aer_process_err_devices(struct pcie_device *p_device,
628 struct aer_err_info *e_info)
629{
630 int i;
631
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900632 /* Report all before handle them, not to lost records by reset etc. */
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800633 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900634 if (get_device_error_info(e_info->dev[i], e_info))
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800635 aer_print_error(e_info->dev[i], e_info);
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900636 }
637 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
638 if (get_device_error_info(e_info->dev[i], e_info))
639 handle_error_source(p_device, e_info->dev[i], e_info);
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800640 }
641}
642
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800643/**
644 * aer_isr_one_error - consume an error detected by root port
645 * @p_device: pointer to error root port service device
646 * @e_src: pointer to an error source
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800647 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800648static void aer_isr_one_error(struct pcie_device *p_device,
649 struct aer_err_source *e_src)
650{
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800651 struct aer_err_info *e_info;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800652 int i;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800653
654 /* struct aer_err_info might be big, so we allocate it with slab */
655 e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
656 if (e_info == NULL) {
657 dev_printk(KERN_DEBUG, &p_device->port->dev,
658 "Can't allocate mem when processing AER errors\n");
659 return;
660 }
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800661
662 /*
663 * There is a possibility that both correctable error and
664 * uncorrectable error being logged. Report correctable error first.
665 */
666 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
667 if (i > 4)
668 break;
669 if (!(e_src->status & i))
670 continue;
671
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800672 memset(e_info, 0, sizeof(struct aer_err_info));
673
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800674 /* Init comprehensive error information */
675 if (i & PCI_ERR_ROOT_COR_RCV) {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800676 e_info->id = ERR_COR_ID(e_src->id);
677 e_info->severity = AER_CORRECTABLE;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800678 } else {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800679 e_info->id = ERR_UNCOR_ID(e_src->id);
680 e_info->severity = ((e_src->status >> 6) & 1);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800681 }
682 if (e_src->status &
683 (PCI_ERR_ROOT_MULTI_COR_RCV |
684 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900685 e_info->multi_error_valid = 1;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800686
Hidetoshi Seto79e4b892009-09-07 17:16:45 +0900687 aer_print_port_info(p_device->port, e_info);
688
Hidetoshi Seto98ca3962010-04-15 13:11:42 +0900689 if (find_source_device(p_device->port, e_info))
690 aer_process_err_devices(p_device, e_info);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800691 }
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800692
693 kfree(e_info);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800694}
695
696/**
697 * aer_isr - consume errors detected by root port
David Howells65f27f32006-11-22 14:55:48 +0000698 * @work: definition of this work item
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800699 *
700 * Invoked, as DPC, when root port records new detected error
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800701 */
David Howells65f27f32006-11-22 14:55:48 +0000702void aer_isr(struct work_struct *work)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800703{
David Howells65f27f32006-11-22 14:55:48 +0000704 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
705 struct pcie_device *p_device = rpc->rpd;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800706 struct aer_err_source *e_src;
707
708 mutex_lock(&rpc->rpc_mutex);
709 e_src = get_e_source(rpc);
710 while (e_src) {
711 aer_isr_one_error(p_device, e_src);
712 e_src = get_e_source(rpc);
713 }
714 mutex_unlock(&rpc->rpc_mutex);
715
716 wake_up(&rpc->wait_release);
717}
718
719/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800720 * aer_init - provide AER initialization
721 * @dev: pointer to AER pcie device
722 *
723 * Invoked when AER service driver is loaded.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800724 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800725int aer_init(struct pcie_device *dev)
726{
Matt Domsch05843962009-11-02 11:51:24 -0600727 if (dev->port->aer_firmware_first) {
728 dev_printk(KERN_DEBUG, &dev->device,
729 "PCIe errors handled by platform firmware.\n");
730 goto out;
731 }
732
733 if (aer_osc_setup(dev))
734 goto out;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800735
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900736 return 0;
Matt Domsch05843962009-11-02 11:51:24 -0600737out:
738 if (forceload) {
739 dev_printk(KERN_DEBUG, &dev->device,
740 "aerdrv forceload requested.\n");
741 dev->port->aer_firmware_first = 0;
742 return 0;
743 }
744 return -ENXIO;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800745}