PCI/AER: Remove aer_recover_work_func() forward declaration
[linux-2.6-block.git] / drivers / pci / pcie / aer / aerdrv_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implement the core part of PCIe AER. When a PCIe error is delivered, an
4  * error message will be collected and printed to console, then an error
5  * recovery procedure will be executed by following the PCI error recovery
6  * rules.
7  *
8  * Copyright (C) 2006 Intel Corp.
9  *      Tom Long Nguyen (tom.l.nguyen@intel.com)
10  *      Zhang Yanmin (yanmin.zhang@intel.com)
11  */
12
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/pm.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/kfifo.h>
22 #include "aerdrv.h"
23 #include "../../pci.h"
24
25 #define PCI_EXP_AER_FLAGS       (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
26                                  PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
27
28 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
29 {
30         if (pcie_aer_get_firmware_first(dev))
31                 return -EIO;
32
33         if (!dev->aer_cap)
34                 return -EIO;
35
36         return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
37 }
38 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
39
40 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
41 {
42         if (pcie_aer_get_firmware_first(dev))
43                 return -EIO;
44
45         return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
46                                           PCI_EXP_AER_FLAGS);
47 }
48 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
49
50 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
51 {
52         int pos;
53         u32 status;
54
55         pos = dev->aer_cap;
56         if (!pos)
57                 return -EIO;
58
59         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
60         if (status)
61                 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
62
63         return 0;
64 }
65 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
66
67 int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
68 {
69         int pos;
70         u32 status;
71         int port_type;
72
73         if (!pci_is_pcie(dev))
74                 return -ENODEV;
75
76         pos = dev->aer_cap;
77         if (!pos)
78                 return -EIO;
79
80         port_type = pci_pcie_type(dev);
81         if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
82                 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
83                 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
84         }
85
86         pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
87         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
88
89         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
90         pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
91
92         return 0;
93 }
94
95 int pci_aer_init(struct pci_dev *dev)
96 {
97         dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
98         return pci_cleanup_aer_error_status_regs(dev);
99 }
100
101 /**
102  * add_error_device - list device to be handled
103  * @e_info: pointer to error info
104  * @dev: pointer to pci_dev to be added
105  */
106 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
107 {
108         if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
109                 e_info->dev[e_info->error_dev_num] = dev;
110                 e_info->error_dev_num++;
111                 return 0;
112         }
113         return -ENOSPC;
114 }
115
116 /**
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  */
121 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
122 {
123         int pos;
124         u32 status, mask;
125         u16 reg16;
126
127         /*
128          * When bus id is equal to 0, it might be a bad id
129          * reported by root port.
130          */
131         if ((PCI_BUS_NUM(e_info->id) != 0) &&
132             !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
133                 /* Device ID match? */
134                 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
135                         return true;
136
137                 /* Continue id comparing if there is no multiple error */
138                 if (!e_info->multi_error_valid)
139                         return false;
140         }
141
142         /*
143          * When either
144          *      1) bus id is equal to 0. Some ports might lose the bus
145          *              id of error source id;
146          *      2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
147          *      3) There are multiple errors and prior ID comparing fails;
148          * We check AER status registers to find possible reporter.
149          */
150         if (atomic_read(&dev->enable_cnt) == 0)
151                 return false;
152
153         /* Check if AER is enabled */
154         pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
155         if (!(reg16 & PCI_EXP_AER_FLAGS))
156                 return false;
157
158         pos = dev->aer_cap;
159         if (!pos)
160                 return false;
161
162         /* Check if error is recorded */
163         if (e_info->severity == AER_CORRECTABLE) {
164                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
165                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
166         } else {
167                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
168                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
169         }
170         if (status & ~mask)
171                 return true;
172
173         return false;
174 }
175
176 static int find_device_iter(struct pci_dev *dev, void *data)
177 {
178         struct aer_err_info *e_info = (struct aer_err_info *)data;
179
180         if (is_error_source(dev, e_info)) {
181                 /* List this device */
182                 if (add_error_device(e_info, dev)) {
183                         /* We cannot handle more... Stop iteration */
184                         /* TODO: Should print error message here? */
185                         return 1;
186                 }
187
188                 /* If there is only a single error, stop iteration */
189                 if (!e_info->multi_error_valid)
190                         return 1;
191         }
192         return 0;
193 }
194
195 /**
196  * find_source_device - search through device hierarchy for source device
197  * @parent: pointer to Root Port pci_dev data structure
198  * @e_info: including detailed error information such like id
199  *
200  * Return true if found.
201  *
202  * Invoked by DPC when error is detected at the Root Port.
203  * Caller of this function must set id, severity, and multi_error_valid of
204  * struct aer_err_info pointed by @e_info properly.  This function must fill
205  * e_info->error_dev_num and e_info->dev[], based on the given information.
206  */
207 static bool find_source_device(struct pci_dev *parent,
208                 struct aer_err_info *e_info)
209 {
210         struct pci_dev *dev = parent;
211         int result;
212
213         /* Must reset in this function */
214         e_info->error_dev_num = 0;
215
216         /* Is Root Port an agent that sends error message? */
217         result = find_device_iter(dev, e_info);
218         if (result)
219                 return true;
220
221         pci_walk_bus(parent->subordinate, find_device_iter, e_info);
222
223         if (!e_info->error_dev_num) {
224                 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
225                            e_info->id);
226                 return false;
227         }
228         return true;
229 }
230
231 /**
232  * handle_error_source - handle logging error into an event log
233  * @aerdev: pointer to pcie_device data structure of the root port
234  * @dev: pointer to pci_dev data structure of error source device
235  * @info: comprehensive error information
236  *
237  * Invoked when an error being detected by Root Port.
238  */
239 static void handle_error_source(struct pcie_device *aerdev,
240         struct pci_dev *dev,
241         struct aer_err_info *info)
242 {
243         int pos;
244
245         if (info->severity == AER_CORRECTABLE) {
246                 /*
247                  * Correctable error does not need software intervention.
248                  * No need to go through error recovery process.
249                  */
250                 pos = dev->aer_cap;
251                 if (pos)
252                         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
253                                         info->status);
254         } else if (info->severity == AER_NONFATAL)
255                 pcie_do_nonfatal_recovery(dev);
256         else if (info->severity == AER_FATAL)
257                 pcie_do_fatal_recovery(dev, PCIE_PORT_SERVICE_AER);
258 }
259
260 #ifdef CONFIG_ACPI_APEI_PCIEAER
261
262 #define AER_RECOVER_RING_ORDER          4
263 #define AER_RECOVER_RING_SIZE           (1 << AER_RECOVER_RING_ORDER)
264
265 struct aer_recover_entry {
266         u8      bus;
267         u8      devfn;
268         u16     domain;
269         int     severity;
270         struct aer_capability_regs *regs;
271 };
272
273 static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
274                     AER_RECOVER_RING_SIZE);
275
276 static void aer_recover_work_func(struct work_struct *work)
277 {
278         struct aer_recover_entry entry;
279         struct pci_dev *pdev;
280
281         while (kfifo_get(&aer_recover_ring, &entry)) {
282                 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
283                                                    entry.devfn);
284                 if (!pdev) {
285                         pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
286                                entry.domain, entry.bus,
287                                PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
288                         continue;
289                 }
290                 cper_print_aer(pdev, entry.severity, entry.regs);
291                 if (entry.severity == AER_NONFATAL)
292                         pcie_do_nonfatal_recovery(pdev);
293                 else if (entry.severity == AER_FATAL)
294                         pcie_do_fatal_recovery(pdev, PCIE_PORT_SERVICE_AER);
295                 pci_dev_put(pdev);
296         }
297 }
298
299 /*
300  * Mutual exclusion for writers of aer_recover_ring, reader side don't
301  * need lock, because there is only one reader and lock is not needed
302  * between reader and writer.
303  */
304 static DEFINE_SPINLOCK(aer_recover_ring_lock);
305 static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
306
307 void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
308                        int severity, struct aer_capability_regs *aer_regs)
309 {
310         unsigned long flags;
311         struct aer_recover_entry entry = {
312                 .bus            = bus,
313                 .devfn          = devfn,
314                 .domain         = domain,
315                 .severity       = severity,
316                 .regs           = aer_regs,
317         };
318
319         spin_lock_irqsave(&aer_recover_ring_lock, flags);
320         if (kfifo_put(&aer_recover_ring, entry))
321                 schedule_work(&aer_recover_work);
322         else
323                 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
324                        domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
325         spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
326 }
327 EXPORT_SYMBOL_GPL(aer_recover_queue);
328 #endif
329
330 /**
331  * get_device_error_info - read error status from dev and store it to info
332  * @dev: pointer to the device expected to have a error record
333  * @info: pointer to structure to store the error record
334  *
335  * Return 1 on success, 0 on error.
336  *
337  * Note that @info is reused among all error devices. Clear fields properly.
338  */
339 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
340 {
341         int pos, temp;
342
343         /* Must reset in this function */
344         info->status = 0;
345         info->tlp_header_valid = 0;
346
347         pos = dev->aer_cap;
348
349         /* The device might not support AER */
350         if (!pos)
351                 return 0;
352
353         if (info->severity == AER_CORRECTABLE) {
354                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
355                         &info->status);
356                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
357                         &info->mask);
358                 if (!(info->status & ~info->mask))
359                         return 0;
360         } else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
361                 info->severity == AER_NONFATAL) {
362
363                 /* Link is still healthy for IO reads */
364                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
365                         &info->status);
366                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
367                         &info->mask);
368                 if (!(info->status & ~info->mask))
369                         return 0;
370
371                 /* Get First Error Pointer */
372                 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
373                 info->first_error = PCI_ERR_CAP_FEP(temp);
374
375                 if (info->status & AER_LOG_TLP_MASKS) {
376                         info->tlp_header_valid = 1;
377                         pci_read_config_dword(dev,
378                                 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
379                         pci_read_config_dword(dev,
380                                 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
381                         pci_read_config_dword(dev,
382                                 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
383                         pci_read_config_dword(dev,
384                                 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
385                 }
386         }
387
388         return 1;
389 }
390
391 static inline void aer_process_err_devices(struct pcie_device *p_device,
392                         struct aer_err_info *e_info)
393 {
394         int i;
395
396         /* Report all before handle them, not to lost records by reset etc. */
397         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
398                 if (get_device_error_info(e_info->dev[i], e_info))
399                         aer_print_error(e_info->dev[i], e_info);
400         }
401         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
402                 if (get_device_error_info(e_info->dev[i], e_info))
403                         handle_error_source(p_device, e_info->dev[i], e_info);
404         }
405 }
406
407 /**
408  * aer_isr_one_error - consume an error detected by root port
409  * @p_device: pointer to error root port service device
410  * @e_src: pointer to an error source
411  */
412 static void aer_isr_one_error(struct pcie_device *p_device,
413                 struct aer_err_source *e_src)
414 {
415         struct aer_rpc *rpc = get_service_data(p_device);
416         struct aer_err_info *e_info = &rpc->e_info;
417
418         /*
419          * There is a possibility that both correctable error and
420          * uncorrectable error being logged. Report correctable error first.
421          */
422         if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
423                 e_info->id = ERR_COR_ID(e_src->id);
424                 e_info->severity = AER_CORRECTABLE;
425
426                 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
427                         e_info->multi_error_valid = 1;
428                 else
429                         e_info->multi_error_valid = 0;
430
431                 aer_print_port_info(p_device->port, e_info);
432
433                 if (find_source_device(p_device->port, e_info))
434                         aer_process_err_devices(p_device, e_info);
435         }
436
437         if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
438                 e_info->id = ERR_UNCOR_ID(e_src->id);
439
440                 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
441                         e_info->severity = AER_FATAL;
442                 else
443                         e_info->severity = AER_NONFATAL;
444
445                 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
446                         e_info->multi_error_valid = 1;
447                 else
448                         e_info->multi_error_valid = 0;
449
450                 aer_print_port_info(p_device->port, e_info);
451
452                 if (find_source_device(p_device->port, e_info))
453                         aer_process_err_devices(p_device, e_info);
454         }
455 }
456
457 /**
458  * get_e_source - retrieve an error source
459  * @rpc: pointer to the root port which holds an error
460  * @e_src: pointer to store retrieved error source
461  *
462  * Return 1 if an error source is retrieved, otherwise 0.
463  *
464  * Invoked by DPC handler to consume an error.
465  */
466 static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
467 {
468         unsigned long flags;
469
470         /* Lock access to Root error producer/consumer index */
471         spin_lock_irqsave(&rpc->e_lock, flags);
472         if (rpc->prod_idx == rpc->cons_idx) {
473                 spin_unlock_irqrestore(&rpc->e_lock, flags);
474                 return 0;
475         }
476
477         *e_src = rpc->e_sources[rpc->cons_idx];
478         rpc->cons_idx++;
479         if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
480                 rpc->cons_idx = 0;
481         spin_unlock_irqrestore(&rpc->e_lock, flags);
482
483         return 1;
484 }
485
486 /**
487  * aer_isr - consume errors detected by root port
488  * @work: definition of this work item
489  *
490  * Invoked, as DPC, when root port records new detected error
491  */
492 void aer_isr(struct work_struct *work)
493 {
494         struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
495         struct pcie_device *p_device = rpc->rpd;
496         struct aer_err_source uninitialized_var(e_src);
497
498         mutex_lock(&rpc->rpc_mutex);
499         while (get_e_source(rpc, &e_src))
500                 aer_isr_one_error(p_device, &e_src);
501         mutex_unlock(&rpc->rpc_mutex);
502 }