blob: 9735c19bf39cf5bc0b8e8d06627f2cc243a1998b [file] [log] [blame]
Bjorn Helgaase1e86ee2018-01-26 14:12:23 -06001// SPDX-License-Identifier: GPL-2.0
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * Implement the AER root port service driver. The driver registers an IRQ
4 * handler. When a root port triggers an AER interrupt, the IRQ handler
5 * collects root port status and schedules work.
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08006 *
7 * Copyright (C) 2006 Intel Corp.
8 * Tom Long Nguyen (tom.l.nguyen@intel.com)
9 * Zhang Yanmin (yanmin.zhang@intel.com)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080010 */
11
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080012#include <linux/pci.h>
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010013#include <linux/pci-acpi.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040014#include <linux/sched.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080015#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/pm.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080022
23#include "aerdrv.h"
Alex Chiang5d9526d2008-06-04 11:39:07 -060024#include "../../pci.h"
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080025
Bill Pemberton15856ad2012-11-21 15:35:00 -050026static int aer_probe(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080027static void aer_remove(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080028static void aer_error_resume(struct pci_dev *dev);
29static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
30
Sam Ravnborgc1996c22007-02-27 10:22:00 +010031static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080032 .name = "aer",
Kenji Kaneshige694f88e2009-11-25 21:06:15 +090033 .port_type = PCI_EXP_TYPE_ROOT_PORT,
Rafael J. Wysocki22106362009-01-13 14:46:46 +010034 .service = PCIE_PORT_SERVICE_AER,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080035
36 .probe = aer_probe,
37 .remove = aer_remove,
Christoph Hellwigc5dc3c62017-06-19 20:04:58 +020038 .error_resume = aer_error_resume,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080039 .reset_link = aer_root_reset,
40};
41
Randy Dunlap7f785762007-10-05 13:17:58 -070042static int pcie_aer_disable;
43
44void pci_no_aer(void)
45{
Bjorn Helgaas7ece1412016-09-06 16:24:37 -050046 pcie_aer_disable = 1;
Randy Dunlap7f785762007-10-05 13:17:58 -070047}
48
Rafael J. Wysockif1a7bfa2010-08-21 01:50:52 +020049bool pci_aer_available(void)
50{
51 return !pcie_aer_disable && pci_msi_enabled();
52}
53
Hidetoshi Seto843f4692010-04-15 13:10:53 +090054static int set_device_error_reporting(struct pci_dev *dev, void *data)
55{
56 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +080057 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +090058
Yijing Wang62f87c02012-07-24 17:20:03 +080059 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
60 (type == PCI_EXP_TYPE_UPSTREAM) ||
61 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +090062 if (enable)
63 pci_enable_pcie_error_reporting(dev);
64 else
65 pci_disable_pcie_error_reporting(dev);
66 }
67
68 if (enable)
69 pcie_set_ecrc_checking(dev);
70
71 return 0;
72}
73
74/**
75 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
76 * @dev: pointer to root port's pci_dev data structure
77 * @enable: true = enable error reporting, false = disable error reporting.
78 */
79static void set_downstream_devices_error_reporting(struct pci_dev *dev,
80 bool enable)
81{
82 set_device_error_reporting(dev, &enable);
83
84 if (!dev->subordinate)
85 return;
86 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
87}
88
89/**
90 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
91 * @rpc: pointer to a Root Port data structure
92 *
93 * Invoked when PCIe bus loads AER service driver.
94 */
95static void aer_enable_rootport(struct aer_rpc *rpc)
96{
Keith Busche13d17f2018-04-09 16:04:42 -060097 struct pci_dev *pdev = rpc->rpd;
Jiang Liu43bd4ee2012-07-24 17:20:11 +080098 int aer_pos;
Hidetoshi Seto843f4692010-04-15 13:10:53 +090099 u16 reg16;
100 u32 reg32;
101
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900102 /* Clear PCIe Capability's Device Status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +0800103 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
104 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900105
106 /* Disable system error generation in response to error messages */
Jiang Liu43bd4ee2012-07-24 17:20:11 +0800107 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
108 SYSTEM_ERROR_INTR_ON_MESG_MASK);
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900109
Keith Busch66b80802016-09-27 16:23:34 -0400110 aer_pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900111 /* Clear error status */
112 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
113 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
114 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
115 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
116 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
117 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
118
119 /*
120 * Enable error reporting for the root port device and downstream port
121 * devices.
122 */
123 set_downstream_devices_error_reporting(pdev, true);
124
125 /* Enable Root Port's interrupt in response to error messages */
126 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
127 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
128 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
129}
130
131/**
132 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
133 * @rpc: pointer to a Root Port data structure
134 *
135 * Invoked when PCIe bus unloads AER service driver.
136 */
137static void aer_disable_rootport(struct aer_rpc *rpc)
138{
Keith Busche13d17f2018-04-09 16:04:42 -0600139 struct pci_dev *pdev = rpc->rpd;
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900140 u32 reg32;
141 int pos;
142
143 /*
144 * Disable error reporting for the root port device and downstream port
145 * devices.
146 */
147 set_downstream_devices_error_reporting(pdev, false);
148
Keith Busch66b80802016-09-27 16:23:34 -0400149 pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +0900150 /* Disable Root's interrupt in response to error messages */
151 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
152 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
153 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
154
155 /* Clear Root's error status reg */
156 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
157 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
158}
159
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800160/**
161 * aer_irq - Root Port's ISR
162 * @irq: IRQ assigned to Root Port
163 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800164 *
165 * Invoked when Root Port detects AER messages.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900166 */
Huang Ying634deb02009-04-24 10:45:23 +0800167irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800168{
169 unsigned int status, id;
170 struct pcie_device *pdev = (struct pcie_device *)context;
171 struct aer_rpc *rpc = get_service_data(pdev);
172 int next_prod_idx;
173 unsigned long flags;
174 int pos;
175
Keith Busch66b80802016-09-27 16:23:34 -0400176 pos = pdev->port->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800177 /*
178 * Must lock access to Root Error Status Reg, Root Error ID Reg,
179 * and Root error producer/consumer index
180 */
181 spin_lock_irqsave(&rpc->e_lock, flags);
182
183 /* Read error status */
184 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
Hidetoshi Setoe167bfc2010-04-15 13:18:26 +0900185 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800186 spin_unlock_irqrestore(&rpc->e_lock, flags);
187 return IRQ_NONE;
188 }
189
190 /* Read error source and clear error status */
Hidetoshi Setof647a442010-04-15 13:17:33 +0900191 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800192 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
193
194 /* Store error source for later DPC handler */
195 next_prod_idx = rpc->prod_idx + 1;
196 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
197 next_prod_idx = 0;
198 if (next_prod_idx == rpc->cons_idx) {
199 /*
200 * Error Storm Condition - possibly the same error occurred.
201 * Drop the error.
202 */
203 spin_unlock_irqrestore(&rpc->e_lock, flags);
204 return IRQ_HANDLED;
205 }
206 rpc->e_sources[rpc->prod_idx].status = status;
207 rpc->e_sources[rpc->prod_idx].id = id;
208 rpc->prod_idx = next_prod_idx;
209 spin_unlock_irqrestore(&rpc->e_lock, flags);
210
211 /* Invoke DPC handler */
212 schedule_work(&rpc->dpc_handler);
213
214 return IRQ_HANDLED;
215}
Huang Ying634deb02009-04-24 10:45:23 +0800216EXPORT_SYMBOL_GPL(aer_irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800217
218/**
219 * aer_alloc_rpc - allocate Root Port data structure
220 * @dev: pointer to the pcie_dev data structure
221 *
222 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900223 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900224static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800225{
226 struct aer_rpc *rpc;
227
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900228 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
229 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800230 return NULL;
231
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900232 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700233 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800234
Keith Busche13d17f2018-04-09 16:04:42 -0600235 rpc->rpd = dev->port;
David Howells65f27f32006-11-22 14:55:48 +0000236 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800237 mutex_init(&rpc->rpc_mutex);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800238
Stefan Assmann45e829e2009-12-03 06:49:24 -0500239 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800240 set_service_data(dev, rpc);
241
242 return rpc;
243}
244
245/**
246 * aer_remove - clean up resources
247 * @dev: pointer to the pcie_dev data structure
248 *
249 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900250 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800251static void aer_remove(struct pcie_device *dev)
252{
253 struct aer_rpc *rpc = get_service_data(dev);
254
255 if (rpc) {
256 /* If register interrupt service, it must be free. */
257 if (rpc->isr)
258 free_irq(dev->irq, dev);
259
Sebastian Andrzej Siewior4ae21822016-01-25 10:08:00 -0600260 flush_work(&rpc->dpc_handler);
Hidetoshi Seto460d2982010-04-15 13:10:03 +0900261 aer_disable_rootport(rpc);
262 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800263 set_service_data(dev, NULL);
264 }
265}
266
267/**
268 * aer_probe - initialize resources
269 * @dev: pointer to the pcie_dev data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800270 *
271 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900272 */
Bill Pemberton15856ad2012-11-21 15:35:00 -0500273static int aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800274{
275 int status;
276 struct aer_rpc *rpc;
Bjorn Helgaas576700b2016-11-21 15:24:25 -0600277 struct device *device = &dev->port->dev;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800278
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800279 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900280 rpc = aer_alloc_rpc(dev);
281 if (!rpc) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -0600282 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800283 aer_remove(dev);
284 return -ENOMEM;
285 }
286
287 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900288 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
289 if (status) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -0600290 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
291 dev->irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800292 aer_remove(dev);
293 return status;
294 }
295
296 rpc->isr = 1;
297
298 aer_enable_rootport(rpc);
Bjorn Helgaas68a55ae2016-11-21 15:34:02 -0600299 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
300 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800301}
302
303/**
304 * aer_root_reset - reset link on Root Port
305 * @dev: pointer to Root Port's pci_dev data structure
306 *
307 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900308 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800309static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
310{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900311 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800312 int pos;
313
Keith Busch66b80802016-09-27 16:23:34 -0400314 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800315
316 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900317 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
318 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
319 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800320
Alex Williamson1b95ce82013-08-08 14:10:20 -0600321 pci_reset_bridge_secondary_bus(dev);
Frederick Lawler7506dc72018-01-18 12:55:24 -0600322 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800323
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900324 /* Clear Root Error Status */
325 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
326 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
327
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800328 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900329 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
330 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
331 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800332
333 return PCI_ERS_RESULT_RECOVERED;
334}
335
336/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800337 * aer_error_resume - clean up corresponding error status bits
338 * @dev: pointer to Root Port's pci_dev data structure
339 *
340 * Invoked by Port Bus driver during nonfatal recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900341 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800342static void aer_error_resume(struct pci_dev *dev)
343{
344 int pos;
345 u32 status, mask;
346 u16 reg16;
347
348 /* Clean up Root device status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +0800349 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &reg16);
350 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800351
352 /* Clean AER Root Error Status */
Keith Busch66b80802016-09-27 16:23:34 -0400353 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800354 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
355 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
Oza Pawandeep7e9084b2018-05-17 16:44:13 -0500356 status &= ~mask; /* Clear corresponding nonfatal bits */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800357 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
358}
359
360/**
361 * aer_service_init - register AER root service driver
362 *
363 * Invoked when AER root service driver is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900364 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800365static int __init aer_service_init(void)
366{
Rafael J. Wysockib22c3d82010-09-20 18:50:00 +0200367 if (!pci_aer_available() || aer_acpi_firmware_first())
Andi Kleen3e77a3f2009-09-16 22:40:22 +0200368 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100369 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800370}
Paul Gortmaker87563362016-08-24 16:57:46 -0400371device_initcall(aer_service_init);