GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / pci / pcie / err.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file implements the error recovery as a core part of PCIe error
4  * reporting. When a PCIe error is delivered, an error message will be
5  * collected and printed to console, then, an error recovery procedure
6  * will be executed by following the PCI error recovery 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 #define dev_fmt(fmt) "AER: " fmt
14
15 #include <linux/pci.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/aer.h>
21 #include "portdrv.h"
22 #include "../pci.h"
23
24 static pci_ers_result_t merge_result(enum pci_ers_result orig,
25                                   enum pci_ers_result new)
26 {
27         if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
28                 return PCI_ERS_RESULT_NO_AER_DRIVER;
29
30         if (new == PCI_ERS_RESULT_NONE)
31                 return orig;
32
33         switch (orig) {
34         case PCI_ERS_RESULT_CAN_RECOVER:
35         case PCI_ERS_RESULT_RECOVERED:
36                 orig = new;
37                 break;
38         case PCI_ERS_RESULT_DISCONNECT:
39                 if (new == PCI_ERS_RESULT_NEED_RESET)
40                         orig = PCI_ERS_RESULT_NEED_RESET;
41                 break;
42         default:
43                 break;
44         }
45
46         return orig;
47 }
48
49 static int report_error_detected(struct pci_dev *dev,
50                                  pci_channel_state_t state,
51                                  enum pci_ers_result *result)
52 {
53         pci_ers_result_t vote;
54         const struct pci_error_handlers *err_handler;
55
56         device_lock(&dev->dev);
57         if (!pci_dev_set_io_state(dev, state) ||
58                 !dev->driver ||
59                 !dev->driver->err_handler ||
60                 !dev->driver->err_handler->error_detected) {
61                 /*
62                  * If any device in the subtree does not have an error_detected
63                  * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
64                  * error callbacks of "any" device in the subtree, and will
65                  * exit in the disconnected error state.
66                  */
67                 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
68                         vote = PCI_ERS_RESULT_NO_AER_DRIVER;
69                         pci_info(dev, "can't recover (no error_detected callback)\n");
70                 } else {
71                         vote = PCI_ERS_RESULT_NONE;
72                 }
73         } else {
74                 err_handler = dev->driver->err_handler;
75                 vote = err_handler->error_detected(dev, state);
76         }
77         pci_uevent_ers(dev, vote);
78         *result = merge_result(*result, vote);
79         device_unlock(&dev->dev);
80         return 0;
81 }
82
83 static int pci_pm_runtime_get_sync(struct pci_dev *pdev, void *data)
84 {
85         pm_runtime_get_sync(&pdev->dev);
86         return 0;
87 }
88
89 static int pci_pm_runtime_put(struct pci_dev *pdev, void *data)
90 {
91         pm_runtime_put(&pdev->dev);
92         return 0;
93 }
94
95 static int report_frozen_detected(struct pci_dev *dev, void *data)
96 {
97         return report_error_detected(dev, pci_channel_io_frozen, data);
98 }
99
100 static int report_normal_detected(struct pci_dev *dev, void *data)
101 {
102         return report_error_detected(dev, pci_channel_io_normal, data);
103 }
104
105 static int report_mmio_enabled(struct pci_dev *dev, void *data)
106 {
107         pci_ers_result_t vote, *result = data;
108         const struct pci_error_handlers *err_handler;
109
110         device_lock(&dev->dev);
111         if (!dev->driver ||
112                 !dev->driver->err_handler ||
113                 !dev->driver->err_handler->mmio_enabled)
114                 goto out;
115
116         err_handler = dev->driver->err_handler;
117         vote = err_handler->mmio_enabled(dev);
118         *result = merge_result(*result, vote);
119 out:
120         device_unlock(&dev->dev);
121         return 0;
122 }
123
124 static int report_slot_reset(struct pci_dev *dev, void *data)
125 {
126         pci_ers_result_t vote, *result = data;
127         const struct pci_error_handlers *err_handler;
128
129         device_lock(&dev->dev);
130         if (!dev->driver ||
131                 !dev->driver->err_handler ||
132                 !dev->driver->err_handler->slot_reset)
133                 goto out;
134
135         err_handler = dev->driver->err_handler;
136         vote = err_handler->slot_reset(dev);
137         *result = merge_result(*result, vote);
138 out:
139         device_unlock(&dev->dev);
140         return 0;
141 }
142
143 static int report_resume(struct pci_dev *dev, void *data)
144 {
145         const struct pci_error_handlers *err_handler;
146
147         device_lock(&dev->dev);
148         if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
149                 !dev->driver ||
150                 !dev->driver->err_handler ||
151                 !dev->driver->err_handler->resume)
152                 goto out;
153
154         err_handler = dev->driver->err_handler;
155         err_handler->resume(dev);
156 out:
157         pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
158         device_unlock(&dev->dev);
159         return 0;
160 }
161
162 /**
163  * pci_walk_bridge - walk bridges potentially AER affected
164  * @bridge:     bridge which may be a Port or an RCEC
165  * @cb:         callback to be called for each device found
166  * @userdata:   arbitrary pointer to be passed to callback
167  *
168  * If the device provided is a bridge, walk the subordinate bus, including
169  * any bridged devices on buses under this bus.  Call the provided callback
170  * on each device found.
171  *
172  * If the device provided has no subordinate bus, e.g., an RCEC, call the
173  * callback on the device itself.
174  */
175 static void pci_walk_bridge(struct pci_dev *bridge,
176                             int (*cb)(struct pci_dev *, void *),
177                             void *userdata)
178 {
179         if (bridge->subordinate)
180                 pci_walk_bus(bridge->subordinate, cb, userdata);
181         else
182                 cb(bridge, userdata);
183 }
184
185 pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
186                 pci_channel_state_t state,
187                 pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
188 {
189         int type = pci_pcie_type(dev);
190         struct pci_dev *bridge;
191         pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
192         struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
193
194         /*
195          * If the error was detected by a Root Port, Downstream Port, or
196          * RCEC, recovery runs on the device itself.  For Ports, that also
197          * includes any subordinate devices.
198          *
199          * If it was detected by another device (Endpoint, etc), recovery
200          * runs on the device and anything else under the same Port, i.e.,
201          * everything under "bridge".
202          */
203         if (type == PCI_EXP_TYPE_ROOT_PORT ||
204             type == PCI_EXP_TYPE_DOWNSTREAM ||
205             type == PCI_EXP_TYPE_RC_EC)
206                 bridge = dev;
207         else
208                 bridge = pci_upstream_bridge(dev);
209
210         pci_walk_bridge(bridge, pci_pm_runtime_get_sync, NULL);
211
212         pci_dbg(bridge, "broadcast error_detected message\n");
213         if (state == pci_channel_io_frozen) {
214                 pci_walk_bridge(bridge, report_frozen_detected, &status);
215                 if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
216                         pci_warn(bridge, "subordinate device reset failed\n");
217                         goto failed;
218                 }
219         } else {
220                 pci_walk_bridge(bridge, report_normal_detected, &status);
221         }
222
223         if (status == PCI_ERS_RESULT_CAN_RECOVER) {
224                 status = PCI_ERS_RESULT_RECOVERED;
225                 pci_dbg(bridge, "broadcast mmio_enabled message\n");
226                 pci_walk_bridge(bridge, report_mmio_enabled, &status);
227         }
228
229         if (status == PCI_ERS_RESULT_NEED_RESET) {
230                 /*
231                  * TODO: Should call platform-specific
232                  * functions to reset slot before calling
233                  * drivers' slot_reset callbacks?
234                  */
235                 status = PCI_ERS_RESULT_RECOVERED;
236                 pci_dbg(bridge, "broadcast slot_reset message\n");
237                 pci_walk_bridge(bridge, report_slot_reset, &status);
238         }
239
240         if (status != PCI_ERS_RESULT_RECOVERED)
241                 goto failed;
242
243         pci_dbg(bridge, "broadcast resume message\n");
244         pci_walk_bridge(bridge, report_resume, &status);
245
246         /*
247          * If we have native control of AER, clear error status in the Root
248          * Port or Downstream Port that signaled the error.  If the
249          * platform retained control of AER, it is responsible for clearing
250          * this status.  In that case, the signaling device may not even be
251          * visible to the OS.
252          */
253         if (host->native_aer || pcie_ports_native) {
254                 pcie_clear_device_status(bridge);
255                 pci_aer_clear_nonfatal_status(bridge);
256         }
257
258         pci_walk_bridge(bridge, pci_pm_runtime_put, NULL);
259
260         pci_info(bridge, "device recovery successful\n");
261         return status;
262
263 failed:
264         pci_walk_bridge(bridge, pci_pm_runtime_put, NULL);
265
266         pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
267
268         /* TODO: Should kernel panic here? */
269         pci_info(bridge, "device recovery failed\n");
270
271         return status;
272 }