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