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