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