Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: portdrv_pci.c |
| 3 | * Purpose: PCI Express Port Bus Driver |
| 4 | * |
| 5 | * Copyright (C) 2004 Intel |
| 6 | * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/pci.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/pm.h> |
| 14 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pcieport_if.h> |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 17 | #include <linux/aer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "portdrv.h" |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 20 | #include "aer/aerdrv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Version Information |
| 24 | */ |
| 25 | #define DRIVER_VERSION "v1.0" |
| 26 | #define DRIVER_AUTHOR "tom.l.nguyen@intel.com" |
| 27 | #define DRIVER_DESC "PCIE Port Bus Driver" |
| 28 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 29 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | |
| 32 | /* global data */ |
| 33 | static const char device_name[] = "pcieport-driver"; |
| 34 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 35 | static int pcie_portdrv_save_config(struct pci_dev *dev) |
| 36 | { |
| 37 | return pci_save_state(dev); |
| 38 | } |
| 39 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 40 | static int pcie_portdrv_restore_config(struct pci_dev *dev) |
| 41 | { |
| 42 | int retval; |
| 43 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 44 | retval = pci_enable_device(dev); |
| 45 | if (retval) |
| 46 | return retval; |
| 47 | pci_set_master(dev); |
| 48 | return 0; |
| 49 | } |
| 50 | |
Zhang, Yanmin | 0bed208 | 2006-09-28 14:35:59 +0800 | [diff] [blame] | 51 | #ifdef CONFIG_PM |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 52 | static int pcie_portdrv_suspend(struct pci_dev *dev, pm_message_t state) |
| 53 | { |
| 54 | return pcie_port_device_suspend(dev, state); |
| 55 | |
| 56 | } |
| 57 | |
Rafael J. Wysocki | 90d25f2 | 2008-12-06 15:07:59 +0100 | [diff] [blame] | 58 | static int pcie_portdrv_suspend_late(struct pci_dev *dev, pm_message_t state) |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 59 | { |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 60 | return pci_save_state(dev); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Rafael J. Wysocki | 90d25f2 | 2008-12-06 15:07:59 +0100 | [diff] [blame] | 63 | static int pcie_portdrv_resume_early(struct pci_dev *dev) |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 64 | { |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 65 | return pci_restore_state(dev); |
| 66 | } |
| 67 | |
| 68 | static int pcie_portdrv_resume(struct pci_dev *dev) |
| 69 | { |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 70 | pcie_portdrv_restore_config(dev); |
| 71 | return pcie_port_device_resume(dev); |
| 72 | } |
| 73 | #else |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 74 | #define pcie_portdrv_suspend NULL |
Rafael J. Wysocki | 90d25f2 | 2008-12-06 15:07:59 +0100 | [diff] [blame] | 75 | #define pcie_portdrv_suspend_late NULL |
| 76 | #define pcie_portdrv_resume_early NULL |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 77 | #define pcie_portdrv_resume NULL |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 78 | #endif |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * pcie_portdrv_probe - Probe PCI-Express port devices |
| 82 | * @dev: PCI-Express port device being probed |
| 83 | * |
| 84 | * If detected invokes the pcie_port_device_register() method for |
| 85 | * this port device. |
| 86 | * |
| 87 | */ |
| 88 | static int __devinit pcie_portdrv_probe (struct pci_dev *dev, |
| 89 | const struct pci_device_id *id ) |
| 90 | { |
| 91 | int status; |
| 92 | |
| 93 | status = pcie_port_device_probe(dev); |
| 94 | if (status) |
| 95 | return status; |
| 96 | |
| 97 | if (pci_enable_device(dev) < 0) |
| 98 | return -ENODEV; |
| 99 | |
| 100 | pci_set_master(dev); |
Kenji Kaneshige | 83e42bc | 2006-12-06 22:07:30 +0900 | [diff] [blame] | 101 | if (!dev->irq && dev->pin) { |
Bjorn Helgaas | 34a2e15 | 2008-08-25 15:45:20 -0600 | [diff] [blame] | 102 | dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; " |
Bjorn Helgaas | 34438ba | 2008-06-13 10:52:13 -0600 | [diff] [blame] | 103 | "check vendor BIOS\n", dev->vendor, dev->device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Randy Dunlap | e4fd1f4 | 2006-07-06 21:36:01 -0700 | [diff] [blame] | 105 | if (pcie_port_device_register(dev)) { |
| 106 | pci_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return -ENOMEM; |
Randy Dunlap | e4fd1f4 | 2006-07-06 21:36:01 -0700 | [diff] [blame] | 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 110 | pcie_portdrv_save_config(dev); |
| 111 | |
| 112 | pci_enable_pcie_error_reporting(dev); |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void pcie_portdrv_remove (struct pci_dev *dev) |
| 118 | { |
| 119 | pcie_port_device_remove(dev); |
long | 5823d10 | 2005-06-22 09:09:54 -0700 | [diff] [blame] | 120 | kfree(pci_get_drvdata(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 123 | static int error_detected_iter(struct device *device, void *data) |
Henrik Kretzschmar | 6085483 | 2006-07-06 18:05:51 +0400 | [diff] [blame] | 124 | { |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 125 | struct pcie_device *pcie_device; |
| 126 | struct pcie_port_service_driver *driver; |
| 127 | struct aer_broadcast_data *result_data; |
| 128 | pci_ers_result_t status; |
Henrik Kretzschmar | 6085483 | 2006-07-06 18:05:51 +0400 | [diff] [blame] | 129 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 130 | result_data = (struct aer_broadcast_data *) data; |
Henrik Kretzschmar | 6085483 | 2006-07-06 18:05:51 +0400 | [diff] [blame] | 131 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 132 | if (device->bus == &pcie_port_bus_type && device->driver) { |
| 133 | driver = to_service_driver(device->driver); |
| 134 | if (!driver || |
| 135 | !driver->err_handler || |
| 136 | !driver->err_handler->error_detected) |
| 137 | return 0; |
| 138 | |
| 139 | pcie_device = to_pcie_device(device); |
| 140 | |
| 141 | /* Forward error detected message to service drivers */ |
| 142 | status = driver->err_handler->error_detected( |
| 143 | pcie_device->port, |
| 144 | result_data->state); |
| 145 | result_data->result = |
| 146 | merge_result(result_data->result, status); |
| 147 | } |
| 148 | |
Henrik Kretzschmar | 6085483 | 2006-07-06 18:05:51 +0400 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 152 | static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev, |
| 153 | enum pci_channel_state error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 155 | struct aer_broadcast_data result_data = |
| 156 | {error, PCI_ERS_RESULT_CAN_RECOVER}; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 157 | int retval; |
long | 5823d10 | 2005-06-22 09:09:54 -0700 | [diff] [blame] | 158 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 159 | /* can not fail */ |
| 160 | retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 161 | |
| 162 | return result_data.result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 165 | static int mmio_enabled_iter(struct device *device, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 167 | struct pcie_device *pcie_device; |
| 168 | struct pcie_port_service_driver *driver; |
| 169 | pci_ers_result_t status, *result; |
| 170 | |
| 171 | result = (pci_ers_result_t *) data; |
| 172 | |
| 173 | if (device->bus == &pcie_port_bus_type && device->driver) { |
| 174 | driver = to_service_driver(device->driver); |
| 175 | if (driver && |
| 176 | driver->err_handler && |
| 177 | driver->err_handler->mmio_enabled) { |
| 178 | pcie_device = to_pcie_device(device); |
| 179 | |
| 180 | /* Forward error message to service drivers */ |
| 181 | status = driver->err_handler->mmio_enabled( |
| 182 | pcie_device->port); |
| 183 | *result = merge_result(*result, status); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 189 | |
| 190 | static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev) |
| 191 | { |
| 192 | pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 193 | int retval; |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 194 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 195 | /* get true return value from &status */ |
| 196 | retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 197 | return status; |
| 198 | } |
| 199 | |
| 200 | static int slot_reset_iter(struct device *device, void *data) |
| 201 | { |
| 202 | struct pcie_device *pcie_device; |
| 203 | struct pcie_port_service_driver *driver; |
| 204 | pci_ers_result_t status, *result; |
| 205 | |
| 206 | result = (pci_ers_result_t *) data; |
| 207 | |
| 208 | if (device->bus == &pcie_port_bus_type && device->driver) { |
| 209 | driver = to_service_driver(device->driver); |
| 210 | if (driver && |
| 211 | driver->err_handler && |
| 212 | driver->err_handler->slot_reset) { |
| 213 | pcie_device = to_pcie_device(device); |
| 214 | |
| 215 | /* Forward error message to service drivers */ |
| 216 | status = driver->err_handler->slot_reset( |
| 217 | pcie_device->port); |
| 218 | *result = merge_result(*result, status); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev) |
| 226 | { |
Linas Vepstas | bf16441 | 2007-11-02 15:14:28 -0500 | [diff] [blame] | 227 | pci_ers_result_t status = PCI_ERS_RESULT_NONE; |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 228 | int retval; |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 229 | |
| 230 | /* If fatal, restore cfg space for possible link reset at upstream */ |
| 231 | if (dev->error_state == pci_channel_io_frozen) { |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 232 | pci_restore_state(dev); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 233 | pcie_portdrv_restore_config(dev); |
| 234 | pci_enable_pcie_error_reporting(dev); |
| 235 | } |
| 236 | |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 237 | /* get true return value from &status */ |
| 238 | retval = device_for_each_child(&dev->dev, &status, slot_reset_iter); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 239 | |
| 240 | return status; |
| 241 | } |
| 242 | |
| 243 | static int resume_iter(struct device *device, void *data) |
| 244 | { |
| 245 | struct pcie_device *pcie_device; |
| 246 | struct pcie_port_service_driver *driver; |
| 247 | |
| 248 | if (device->bus == &pcie_port_bus_type && device->driver) { |
| 249 | driver = to_service_driver(device->driver); |
| 250 | if (driver && |
| 251 | driver->err_handler && |
| 252 | driver->err_handler->resume) { |
| 253 | pcie_device = to_pcie_device(device); |
| 254 | |
| 255 | /* Forward error message to service drivers */ |
| 256 | driver->err_handler->resume(pcie_device->port); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static void pcie_portdrv_err_resume(struct pci_dev *dev) |
| 264 | { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 265 | int retval; |
| 266 | /* nothing to do with error value, if it ever happens */ |
| 267 | retval = device_for_each_child(&dev->dev, NULL, resume_iter); |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * LINUX Device Driver Model |
| 272 | */ |
| 273 | static const struct pci_device_id port_pci_ids[] = { { |
| 274 | /* handle any PCI-Express port */ |
| 275 | PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0), |
| 276 | }, { /* end: all zeroes */ } |
| 277 | }; |
| 278 | MODULE_DEVICE_TABLE(pci, port_pci_ids); |
| 279 | |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 280 | static struct pci_error_handlers pcie_portdrv_err_handler = { |
| 281 | .error_detected = pcie_portdrv_error_detected, |
| 282 | .mmio_enabled = pcie_portdrv_mmio_enabled, |
| 283 | .slot_reset = pcie_portdrv_slot_reset, |
| 284 | .resume = pcie_portdrv_err_resume, |
| 285 | }; |
| 286 | |
Sam Ravnborg | 3603a6a | 2007-02-27 10:19:17 +0100 | [diff] [blame] | 287 | static struct pci_driver pcie_portdriver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | .name = (char *)device_name, |
| 289 | .id_table = &port_pci_ids[0], |
| 290 | |
| 291 | .probe = pcie_portdrv_probe, |
| 292 | .remove = pcie_portdrv_remove, |
| 293 | |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 294 | .suspend = pcie_portdrv_suspend, |
Rafael J. Wysocki | 90d25f2 | 2008-12-06 15:07:59 +0100 | [diff] [blame] | 295 | .suspend_late = pcie_portdrv_suspend_late, |
| 296 | .resume_early = pcie_portdrv_resume_early, |
Rafael J. Wysocki | a79d682 | 2008-12-27 16:28:58 +0100 | [diff] [blame] | 297 | .resume = pcie_portdrv_resume, |
Zhang, Yanmin | 4bf3392 | 2006-07-31 15:26:16 +0800 | [diff] [blame] | 298 | |
| 299 | .err_handler = &pcie_portdrv_err_handler, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | static int __init pcie_portdrv_init(void) |
| 303 | { |
Randy Dunlap | 20d5166 | 2006-07-08 22:58:25 -0700 | [diff] [blame] | 304 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Randy Dunlap | 20d5166 | 2006-07-08 22:58:25 -0700 | [diff] [blame] | 306 | retval = pcie_port_bus_register(); |
| 307 | if (retval) { |
| 308 | printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval); |
| 309 | goto out; |
| 310 | } |
Sam Ravnborg | 3603a6a | 2007-02-27 10:19:17 +0100 | [diff] [blame] | 311 | retval = pci_register_driver(&pcie_portdriver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (retval) |
| 313 | pcie_port_bus_unregister(); |
Randy Dunlap | 20d5166 | 2006-07-08 22:58:25 -0700 | [diff] [blame] | 314 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return retval; |
| 316 | } |
| 317 | |
| 318 | static void __exit pcie_portdrv_exit(void) |
| 319 | { |
Sam Ravnborg | 3603a6a | 2007-02-27 10:19:17 +0100 | [diff] [blame] | 320 | pci_unregister_driver(&pcie_portdriver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | pcie_port_bus_unregister(); |
| 322 | } |
| 323 | |
| 324 | module_init(pcie_portdrv_init); |
| 325 | module_exit(pcie_portdrv_exit); |