]>
Commit | Line | Data |
---|---|---|
6c2b374d ZY |
1 | /* |
2 | * drivers/pci/pcie/aer/aerdrv.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 AER root port service driver. The driver will | |
9 | * register an irq handler. When root port triggers an AER interrupt, the irq | |
10 | * handler will collect root port status and schedule a work. | |
11 | * | |
12 | * Copyright (C) 2006 Intel Corp. | |
13 | * Tom Long Nguyen (tom.l.nguyen@intel.com) | |
14 | * Zhang Yanmin (yanmin.zhang@intel.com) | |
15 | * | |
16 | */ | |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/pci.h> | |
d43c36dc | 20 | #include <linux/sched.h> |
6c2b374d ZY |
21 | #include <linux/kernel.h> |
22 | #include <linux/errno.h> | |
23 | #include <linux/pm.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/pcieport_if.h> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
6c2b374d ZY |
29 | |
30 | #include "aerdrv.h" | |
5d9526d0 | 31 | #include "../../pci.h" |
6c2b374d ZY |
32 | |
33 | /* | |
34 | * Version Information | |
35 | */ | |
36 | #define DRIVER_VERSION "v1.0" | |
37 | #define DRIVER_AUTHOR "tom.l.nguyen@intel.com" | |
38 | #define DRIVER_DESC "Root Port Advanced Error Reporting Driver" | |
39 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
40 | MODULE_DESCRIPTION(DRIVER_DESC); | |
41 | MODULE_LICENSE("GPL"); | |
42 | ||
c9a91883 | 43 | static int __devinit aer_probe(struct pcie_device *dev); |
6c2b374d | 44 | static void aer_remove(struct pcie_device *dev); |
6c2b374d ZY |
45 | static pci_ers_result_t aer_error_detected(struct pci_dev *dev, |
46 | enum pci_channel_state error); | |
47 | static void aer_error_resume(struct pci_dev *dev); | |
48 | static pci_ers_result_t aer_root_reset(struct pci_dev *dev); | |
49 | ||
6c2b374d ZY |
50 | static struct pci_error_handlers aer_error_handlers = { |
51 | .error_detected = aer_error_detected, | |
c9a91883 | 52 | .resume = aer_error_resume, |
6c2b374d ZY |
53 | }; |
54 | ||
c1996c27 | 55 | static struct pcie_port_service_driver aerdriver = { |
6c2b374d | 56 | .name = "aer", |
694f88ef | 57 | .port_type = PCI_EXP_TYPE_ROOT_PORT, |
22106368 | 58 | .service = PCIE_PORT_SERVICE_AER, |
6c2b374d ZY |
59 | |
60 | .probe = aer_probe, | |
61 | .remove = aer_remove, | |
62 | ||
6c2b374d ZY |
63 | .err_handler = &aer_error_handlers, |
64 | ||
65 | .reset_link = aer_root_reset, | |
66 | }; | |
67 | ||
7f785763 RD |
68 | static int pcie_aer_disable; |
69 | ||
70 | void pci_no_aer(void) | |
71 | { | |
72 | pcie_aer_disable = 1; /* has priority over 'forceload' */ | |
73 | } | |
74 | ||
6c2b374d ZY |
75 | /** |
76 | * aer_irq - Root Port's ISR | |
77 | * @irq: IRQ assigned to Root Port | |
78 | * @context: pointer to Root Port data structure | |
6c2b374d ZY |
79 | * |
80 | * Invoked when Root Port detects AER messages. | |
81 | **/ | |
634deb02 | 82 | irqreturn_t aer_irq(int irq, void *context) |
6c2b374d ZY |
83 | { |
84 | unsigned int status, id; | |
85 | struct pcie_device *pdev = (struct pcie_device *)context; | |
86 | struct aer_rpc *rpc = get_service_data(pdev); | |
87 | int next_prod_idx; | |
88 | unsigned long flags; | |
89 | int pos; | |
90 | ||
0927678f | 91 | pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR); |
6c2b374d ZY |
92 | /* |
93 | * Must lock access to Root Error Status Reg, Root Error ID Reg, | |
94 | * and Root error producer/consumer index | |
95 | */ | |
96 | spin_lock_irqsave(&rpc->e_lock, flags); | |
97 | ||
98 | /* Read error status */ | |
99 | pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status); | |
100 | if (!(status & ROOT_ERR_STATUS_MASKS)) { | |
101 | spin_unlock_irqrestore(&rpc->e_lock, flags); | |
102 | return IRQ_NONE; | |
103 | } | |
104 | ||
105 | /* Read error source and clear error status */ | |
106 | pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id); | |
107 | pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status); | |
108 | ||
109 | /* Store error source for later DPC handler */ | |
110 | next_prod_idx = rpc->prod_idx + 1; | |
111 | if (next_prod_idx == AER_ERROR_SOURCES_MAX) | |
112 | next_prod_idx = 0; | |
113 | if (next_prod_idx == rpc->cons_idx) { | |
114 | /* | |
115 | * Error Storm Condition - possibly the same error occurred. | |
116 | * Drop the error. | |
117 | */ | |
118 | spin_unlock_irqrestore(&rpc->e_lock, flags); | |
119 | return IRQ_HANDLED; | |
120 | } | |
121 | rpc->e_sources[rpc->prod_idx].status = status; | |
122 | rpc->e_sources[rpc->prod_idx].id = id; | |
123 | rpc->prod_idx = next_prod_idx; | |
124 | spin_unlock_irqrestore(&rpc->e_lock, flags); | |
125 | ||
126 | /* Invoke DPC handler */ | |
127 | schedule_work(&rpc->dpc_handler); | |
128 | ||
129 | return IRQ_HANDLED; | |
130 | } | |
634deb02 | 131 | EXPORT_SYMBOL_GPL(aer_irq); |
6c2b374d ZY |
132 | |
133 | /** | |
134 | * aer_alloc_rpc - allocate Root Port data structure | |
135 | * @dev: pointer to the pcie_dev data structure | |
136 | * | |
137 | * Invoked when Root Port's AER service is loaded. | |
138 | **/ | |
c9a91883 | 139 | static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev) |
6c2b374d ZY |
140 | { |
141 | struct aer_rpc *rpc; | |
142 | ||
c9a91883 HS |
143 | rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL); |
144 | if (!rpc) | |
6c2b374d ZY |
145 | return NULL; |
146 | ||
6c2b374d ZY |
147 | /* |
148 | * Initialize Root lock access, e_lock, to Root Error Status Reg, | |
149 | * Root Error ID Reg, and Root error producer/consumer index. | |
150 | */ | |
f5609d7e | 151 | spin_lock_init(&rpc->e_lock); |
6c2b374d ZY |
152 | |
153 | rpc->rpd = dev; | |
65f27f38 | 154 | INIT_WORK(&rpc->dpc_handler, aer_isr); |
6c2b374d ZY |
155 | rpc->prod_idx = rpc->cons_idx = 0; |
156 | mutex_init(&rpc->rpc_mutex); | |
157 | init_waitqueue_head(&rpc->wait_release); | |
158 | ||
45e829ea | 159 | /* Use PCIe bus function to store rpc into PCIe device */ |
6c2b374d ZY |
160 | set_service_data(dev, rpc); |
161 | ||
162 | return rpc; | |
163 | } | |
164 | ||
165 | /** | |
166 | * aer_remove - clean up resources | |
167 | * @dev: pointer to the pcie_dev data structure | |
168 | * | |
169 | * Invoked when PCI Express bus unloads or AER probe fails. | |
170 | **/ | |
171 | static void aer_remove(struct pcie_device *dev) | |
172 | { | |
173 | struct aer_rpc *rpc = get_service_data(dev); | |
174 | ||
175 | if (rpc) { | |
176 | /* If register interrupt service, it must be free. */ | |
177 | if (rpc->isr) | |
178 | free_irq(dev->irq, dev); | |
179 | ||
180 | wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx); | |
181 | ||
182 | aer_delete_rootport(rpc); | |
183 | set_service_data(dev, NULL); | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * aer_probe - initialize resources | |
189 | * @dev: pointer to the pcie_dev data structure | |
190 | * @id: pointer to the service id data structure | |
191 | * | |
192 | * Invoked when PCI Express bus loads AER service driver. | |
193 | **/ | |
c9a91883 | 194 | static int __devinit aer_probe(struct pcie_device *dev) |
6c2b374d ZY |
195 | { |
196 | int status; | |
197 | struct aer_rpc *rpc; | |
198 | struct device *device = &dev->device; | |
199 | ||
200 | /* Init */ | |
c9a91883 HS |
201 | status = aer_init(dev); |
202 | if (status) | |
6c2b374d ZY |
203 | return status; |
204 | ||
205 | /* Alloc rpc data structure */ | |
c9a91883 HS |
206 | rpc = aer_alloc_rpc(dev); |
207 | if (!rpc) { | |
531f254e | 208 | dev_printk(KERN_DEBUG, device, "alloc rpc failed\n"); |
6c2b374d ZY |
209 | aer_remove(dev); |
210 | return -ENOMEM; | |
211 | } | |
212 | ||
213 | /* Request IRQ ISR */ | |
c9a91883 HS |
214 | status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev); |
215 | if (status) { | |
531f254e | 216 | dev_printk(KERN_DEBUG, device, "request IRQ failed\n"); |
6c2b374d ZY |
217 | aer_remove(dev); |
218 | return status; | |
219 | } | |
220 | ||
221 | rpc->isr = 1; | |
222 | ||
223 | aer_enable_rootport(rpc); | |
224 | ||
225 | return status; | |
226 | } | |
227 | ||
228 | /** | |
229 | * aer_root_reset - reset link on Root Port | |
230 | * @dev: pointer to Root Port's pci_dev data structure | |
231 | * | |
232 | * Invoked by Port Bus driver when performing link reset at Root Port. | |
233 | **/ | |
234 | static pci_ers_result_t aer_root_reset(struct pci_dev *dev) | |
235 | { | |
236 | u16 p2p_ctrl; | |
237 | u32 status; | |
238 | int pos; | |
239 | ||
0927678f | 240 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
6c2b374d ZY |
241 | |
242 | /* Disable Root's interrupt in response to error messages */ | |
243 | pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0); | |
244 | ||
245 | /* Assert Secondary Bus Reset */ | |
246 | pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl); | |
247 | p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET; | |
248 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl); | |
249 | ||
250 | /* De-assert Secondary Bus Reset */ | |
251 | p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET; | |
252 | pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl); | |
253 | ||
254 | /* | |
255 | * System software must wait for at least 100ms from the end | |
256 | * of a reset of one or more device before it is permitted | |
257 | * to issue Configuration Requests to those devices. | |
258 | */ | |
259 | msleep(200); | |
531f254e | 260 | dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n"); |
6c2b374d ZY |
261 | |
262 | /* Enable Root Port's interrupt in response to error messages */ | |
263 | pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status); | |
264 | pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status); | |
265 | pci_write_config_dword(dev, | |
266 | pos + PCI_ERR_ROOT_COMMAND, | |
267 | ROOT_PORT_INTR_ON_MESG_MASK); | |
268 | ||
269 | return PCI_ERS_RESULT_RECOVERED; | |
270 | } | |
271 | ||
272 | /** | |
273 | * aer_error_detected - update severity status | |
274 | * @dev: pointer to Root Port's pci_dev data structure | |
275 | * @error: error severity being notified by port bus | |
276 | * | |
277 | * Invoked by Port Bus driver during error recovery. | |
278 | **/ | |
279 | static pci_ers_result_t aer_error_detected(struct pci_dev *dev, | |
280 | enum pci_channel_state error) | |
281 | { | |
282 | /* Root Port has no impact. Always recovers. */ | |
283 | return PCI_ERS_RESULT_CAN_RECOVER; | |
284 | } | |
285 | ||
286 | /** | |
287 | * aer_error_resume - clean up corresponding error status bits | |
288 | * @dev: pointer to Root Port's pci_dev data structure | |
289 | * | |
290 | * Invoked by Port Bus driver during nonfatal recovery. | |
291 | **/ | |
292 | static void aer_error_resume(struct pci_dev *dev) | |
293 | { | |
294 | int pos; | |
295 | u32 status, mask; | |
296 | u16 reg16; | |
297 | ||
298 | /* Clean up Root device status */ | |
39a53062 | 299 | pos = pci_pcie_cap(dev); |
6c2b374d ZY |
300 | pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, ®16); |
301 | pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16); | |
302 | ||
303 | /* Clean AER Root Error Status */ | |
0927678f | 304 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
6c2b374d ZY |
305 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status); |
306 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask); | |
307 | if (dev->error_state == pci_channel_io_normal) | |
308 | status &= ~mask; /* Clear corresponding nonfatal bits */ | |
309 | else | |
310 | status &= mask; /* Clear corresponding fatal bits */ | |
311 | pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status); | |
312 | } | |
313 | ||
314 | /** | |
315 | * aer_service_init - register AER root service driver | |
316 | * | |
317 | * Invoked when AER root service driver is loaded. | |
318 | **/ | |
319 | static int __init aer_service_init(void) | |
320 | { | |
7f785763 RD |
321 | if (pcie_aer_disable) |
322 | return -ENXIO; | |
3e77a3f7 AK |
323 | if (!pci_msi_enabled()) |
324 | return -ENXIO; | |
c1996c27 | 325 | return pcie_port_service_register(&aerdriver); |
6c2b374d ZY |
326 | } |
327 | ||
328 | /** | |
329 | * aer_service_exit - unregister AER root service driver | |
330 | * | |
331 | * Invoked when AER root service driver is unloaded. | |
332 | **/ | |
333 | static void __exit aer_service_exit(void) | |
334 | { | |
c1996c27 | 335 | pcie_port_service_unregister(&aerdriver); |
6c2b374d ZY |
336 | } |
337 | ||
338 | module_init(aer_service_init); | |
339 | module_exit(aer_service_exit); |