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