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