blob: aceb04b67b60f55979258256e84e93209faf3f24 [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, Yanmin70298c62009-06-16 13:34:38 +0800102static int set_device_error_reporting(struct pci_dev *dev, void *data)
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700103{
104 bool enable = *((bool *)data);
105
Kenji Kaneshige694f88e2009-11-25 21:06:15 +0900106 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)) {
Andrew Patterson43c16402009-04-22 16:52:09 -0600109 if (enable)
110 pci_enable_pcie_error_reporting(dev);
111 else
112 pci_disable_pcie_error_reporting(dev);
113 }
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700114
115 if (enable)
Andrew Patterson43c16402009-04-22 16:52:09 -0600116 pcie_set_ecrc_checking(dev);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800117
118 return 0;
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700119}
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);
Alex Chiangcb4cb4a2009-03-05 19:28:40 -0700130
131 if (!dev->subordinate)
132 return;
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700133 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
134}
135
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800136static inline int compare_device_id(struct pci_dev *dev,
137 struct aer_err_info *e_info)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800138{
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800139 if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
140 /*
141 * Device ID match
142 */
143 return 1;
144 }
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800145
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800146 return 0;
147}
148
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800149static 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;
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900155 }
156
157 return 0;
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800158}
159
160
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800161#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)
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800179 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 */
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900185 if (!e_info->multi_error_valid)
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800186 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;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800195 }
196
197 /*
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800198 * 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.
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800204 */
205 if (atomic_read(&dev->enable_cnt) == 0)
206 return 0;
Kenji Kaneshige39a53062009-11-11 14:31:38 +0900207 pos = pci_pcie_cap(dev);
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800208 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) {
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900225 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) {
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800228 add_error_device(e_info, dev);
229 goto added;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800230 }
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800231 } else {
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900232 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) {
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800235 add_error_device(e_info, dev);
236 goto added;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800237 }
238 }
239
240 return 0;
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800241
242added:
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900243 if (e_info->multi_error_valid)
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800244 return 0;
245 else
246 return 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800247}
248
249/**
250 * find_source_device - search through device hierarchy for source device
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800251 * @parent: pointer to Root Port pci_dev data structure
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800252 * @err_info: including detailed error information such like id
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800253 *
254 * Invoked when error is detected at the Root Port.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800255 */
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800256static void find_source_device(struct pci_dev *parent,
257 struct aer_err_info *e_info)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800258{
259 struct pci_dev *dev = parent;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800260 int result;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800261
262 /* Is Root Port an agent that sends error message? */
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800263 result = find_device_iter(dev, e_info);
264 if (result)
265 return;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800266
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800267 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800268}
269
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800270static int report_error_detected(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800271{
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 */
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600291 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
292 dev->driver ?
293 "no AER-aware driver" : "no driver");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800294 }
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800295 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800296 }
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);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800301 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800302}
303
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800304static int report_mmio_enabled(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800305{
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)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800314 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800315
316 err_handler = dev->driver->err_handler;
317 vote = err_handler->mmio_enabled(dev);
318 result_data->result = merge_result(result_data->result, vote);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800319 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800320}
321
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800322static int report_slot_reset(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800323{
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)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800332 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800333
334 err_handler = dev->driver->err_handler;
335 vote = err_handler->slot_reset(dev);
336 result_data->result = merge_result(result_data->result, vote);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800337 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800338}
339
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800340static int report_resume(struct pci_dev *dev, void *data)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800341{
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 ||
Hidetoshi Setob0b801d2008-12-01 16:31:06 +0900348 !dev->driver->err_handler->resume)
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800349 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800350
351 err_handler = dev->driver->err_handler;
352 err_handler->resume(dev);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800353 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800354}
355
356/**
357 * broadcast_error_message - handle message broadcast to downstream drivers
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800358 * @dev: pointer to from where in a hierarchy message is broadcasted down
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800359 * @state: error state
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800360 * @error_mesg: message to print
361 * @cb: callback to be broadcasted
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800362 *
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.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800366 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800367static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
368 enum pci_channel_state state,
369 char *error_mesg,
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800370 int (*cb)(struct pci_dev *, void *))
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800371{
372 struct aer_broadcast_data result_data;
373
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600374 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800375 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 }
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900395 } else {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800396 /*
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;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800415 struct find_aer_service_data *result;
416
417 result = (struct find_aer_service_data *) data;
418
419 if (device->bus == &pcie_port_bus_type) {
Kenji Kaneshige694f88e2009-11-25 21:06:15 +0900420 struct pcie_device *pcie = to_pcie_device(device);
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100421
Kenji Kaneshige694f88e2009-11-25 21:06:15 +0900422 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800423 result->is_downstream = 1;
424
425 driver = device->driver;
426 if (driver) {
427 service_driver = to_service_driver(driver);
Rafael J. Wysocki22106362009-01-13 14:46:46 +0100428 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800429 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{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700441 int retval;
442 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800443}
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
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900455 udev = dev->bus->self;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800456
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 {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600472 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
473 "support\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800474 return PCI_ERS_RESULT_DISCONNECT;
475 }
476 }
477
478 status = data.aer_driver->reset_link(udev);
479 if (status != PCI_ERS_RESULT_RECOVERED) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600480 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
481 "device %s failed\n", pci_name(udev));
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800482 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.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800497 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800498static 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.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800557 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900558static void handle_error_source(struct pcie_device *aerdev,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800559 struct pci_dev *dev,
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800560 struct aer_err_info *info)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800561{
562 pci_ers_result_t status = 0;
563 int pos;
564
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800565 if (info->severity == AER_CORRECTABLE) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800566 /*
567 * Correctable error does not need software intevention.
568 * No need to go through error recovery process.
569 */
Jesse Barnes09276782008-10-18 17:33:19 -0700570 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800571 if (pos)
572 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800573 info->status);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800574 } else {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800575 status = do_recovery(aerdev, dev, info->severity);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800576 if (status == PCI_ERS_RESULT_RECOVERED) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600577 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
578 "successfully recovered\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800579 } else {
580 /* TODO: Should kernel panic here? */
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600581 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
582 "recover\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800583 }
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 *
Stefan Assmann45e829e2009-12-03 06:49:24 -0500591 * Invoked when PCIe bus loads AER service driver.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800592 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800593void 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
Kenji Kaneshige39a53062009-11-11 14:31:38 +0900600 pos = pci_pcie_cap(pdev);
Stefan Assmann45e829e2009-12-03 06:49:24 -0500601 /* Clear PCIe Capability's Device Status */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800602 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
Jesse Barnes09276782008-10-18 17:33:19 -0700610 aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800611 /* 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
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700619 /*
620 * Enable error reporting for the root port device and downstream port
621 * devices.
622 */
623 set_downstream_devices_error_reporting(pdev, true);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800624
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 *
Stefan Assmann45e829e2009-12-03 06:49:24 -0500635 * Invoked when PCIe bus unloads AER service driver.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800636 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800637static void disable_root_aer(struct aer_rpc *rpc)
638{
639 struct pci_dev *pdev = rpc->rpd->port;
640 u32 reg32;
641 int pos;
642
Andrew Patterson1f9f13c2009-02-20 16:04:59 -0700643 /*
644 * Disable error reporting for the root port device and downstream port
645 * devices.
646 */
647 set_downstream_devices_error_reporting(pdev, false);
648
Jesse Barnes09276782008-10-18 17:33:19 -0700649 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800650 /* 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.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800663 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900664static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800665{
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
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900684/**
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 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800691static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
692{
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900693 int pos, temp;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800694
Hidetoshi Seto1b4ffcf2009-09-07 17:09:58 +0900695 info->status = 0;
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900696 info->tlp_header_valid = 0;
Hidetoshi Seto1b4ffcf2009-09-07 17:09:58 +0900697
Jesse Barnes09276782008-10-18 17:33:19 -0700698 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800699
700 /* The device might not support AER */
701 if (!pos)
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900702 return 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800703
704 if (info->severity == AER_CORRECTABLE) {
705 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
706 &info->status);
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900707 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
708 &info->mask);
709 if (!(info->status & ~info->mask))
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900710 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800711 } 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);
Hidetoshi Seto0d90c3a2009-09-07 17:12:25 +0900717 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
718 &info->mask);
719 if (!(info->status & ~info->mask))
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900720 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800721
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900722 /* Get First Error Pointer */
723 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900724 info->first_error = PCI_ERR_CAP_FEP(temp);
Hidetoshi Setoe7a0d922009-09-07 17:13:42 +0900725
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800726 if (info->status & AER_LOG_TLP_MASKS) {
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900727 info->tlp_header_valid = 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800728 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
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900739 return 1;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800740}
741
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800742static 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
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900753 /* Report all before handle them, not to lost records by reset etc. */
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800754 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900755 if (get_device_error_info(e_info->dev[i], e_info))
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800756 aer_print_error(e_info->dev[i], e_info);
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900757 }
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);
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800761 }
762}
763
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800764/**
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
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800768 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800769static void aer_isr_one_error(struct pcie_device *p_device,
770 struct aer_err_source *e_src)
771{
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800772 struct aer_err_info *e_info;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800773 int i;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800774
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 }
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800782
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
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800793 memset(e_info, 0, sizeof(struct aer_err_info));
794
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800795 /* Init comprehensive error information */
796 if (i & PCI_ERR_ROOT_COR_RCV) {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800797 e_info->id = ERR_COR_ID(e_src->id);
798 e_info->severity = AER_CORRECTABLE;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800799 } else {
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800800 e_info->id = ERR_UNCOR_ID(e_src->id);
801 e_info->severity = ((e_src->status >> 6) & 1);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800802 }
803 if (e_src->status &
804 (PCI_ERR_ROOT_MULTI_COR_RCV |
805 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
Hidetoshi Seto273024d2009-09-07 17:16:20 +0900806 e_info->multi_error_valid = 1;
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800807
Hidetoshi Seto79e4b892009-09-07 17:16:45 +0900808 aer_print_port_info(p_device->port, e_info);
809
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800810 find_source_device(p_device->port, e_info);
Zhang, Yanmin3d5505c2009-06-16 13:35:16 +0800811 aer_process_err_devices(p_device, e_info);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800812 }
Zhang, Yanmin28eb27c2009-06-16 13:35:11 +0800813
814 kfree(e_info);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800815}
816
817/**
818 * aer_isr - consume errors detected by root port
David Howells65f27f32006-11-22 14:55:48 +0000819 * @work: definition of this work item
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800820 *
821 * Invoked, as DPC, when root port records new detected error
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800822 */
David Howells65f27f32006-11-22 14:55:48 +0000823void aer_isr(struct work_struct *work)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800824{
David Howells65f27f32006-11-22 14:55:48 +0000825 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
826 struct pcie_device *p_device = rpc->rpd;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800827 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
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800845 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800846void 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.
Randy Dunlapd885c6b2007-11-28 09:04:23 -0800859 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800860int aer_init(struct pcie_device *dev)
861{
Matt Domsch05843962009-11-02 11:51:24 -0600862 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;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800870
Hidetoshi Setob1c089b2009-09-07 17:16:59 +0900871 return 0;
Matt Domsch05843962009-11-02 11:51:24 -0600872out:
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;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800880}