OMAP3 PM: CPUFreq driver for OMAP3
[linux-ginger.git] / drivers / pci / pcie / portdrv_pci.c
blobf635e476d6322d5032082e5697c208c18f3174b7
1 /*
2 * File: portdrv_pci.c
3 * Purpose: PCI Express Port Bus Driver
5 * Copyright (C) 2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7 */
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>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
17 #include <linux/aer.h>
19 #include "portdrv.h"
20 #include "aer/aerdrv.h"
23 * Version Information
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");
32 /* global data */
34 static int pcie_portdrv_restore_config(struct pci_dev *dev)
36 int retval;
38 retval = pci_enable_device(dev);
39 if (retval)
40 return retval;
41 pci_set_master(dev);
42 return 0;
45 #ifdef CONFIG_PM
46 static struct dev_pm_ops pcie_portdrv_pm_ops = {
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,
55 #define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
57 #else /* !PM */
59 #define PCIE_PORTDRV_PM_OPS NULL
60 #endif /* !PM */
63 * pcie_portdrv_probe - Probe PCI-Express port devices
64 * @dev: PCI-Express port device being probed
66 * If detected invokes the pcie_port_device_register() method for
67 * this port device.
70 static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
71 const struct pci_device_id *id )
73 int status;
75 status = pcie_port_device_probe(dev);
76 if (status)
77 return status;
79 if (!dev->irq && dev->pin) {
80 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
81 "check vendor BIOS\n", dev->vendor, dev->device);
83 status = pcie_port_device_register(dev);
84 if (status)
85 return status;
87 pci_save_state(dev);
89 return 0;
92 static void pcie_portdrv_remove (struct pci_dev *dev)
94 pcie_port_device_remove(dev);
95 pci_disable_device(dev);
98 static int error_detected_iter(struct device *device, void *data)
100 struct pcie_device *pcie_device;
101 struct pcie_port_service_driver *driver;
102 struct aer_broadcast_data *result_data;
103 pci_ers_result_t status;
105 result_data = (struct aer_broadcast_data *) data;
107 if (device->bus == &pcie_port_bus_type && device->driver) {
108 driver = to_service_driver(device->driver);
109 if (!driver ||
110 !driver->err_handler ||
111 !driver->err_handler->error_detected)
112 return 0;
114 pcie_device = to_pcie_device(device);
116 /* Forward error detected message to service drivers */
117 status = driver->err_handler->error_detected(
118 pcie_device->port,
119 result_data->state);
120 result_data->result =
121 merge_result(result_data->result, status);
124 return 0;
127 static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
128 enum pci_channel_state error)
130 struct aer_broadcast_data result_data =
131 {error, PCI_ERS_RESULT_CAN_RECOVER};
132 int retval;
134 /* can not fail */
135 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
137 return result_data.result;
140 static int mmio_enabled_iter(struct device *device, void *data)
142 struct pcie_device *pcie_device;
143 struct pcie_port_service_driver *driver;
144 pci_ers_result_t status, *result;
146 result = (pci_ers_result_t *) data;
148 if (device->bus == &pcie_port_bus_type && device->driver) {
149 driver = to_service_driver(device->driver);
150 if (driver &&
151 driver->err_handler &&
152 driver->err_handler->mmio_enabled) {
153 pcie_device = to_pcie_device(device);
155 /* Forward error message to service drivers */
156 status = driver->err_handler->mmio_enabled(
157 pcie_device->port);
158 *result = merge_result(*result, status);
162 return 0;
165 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
167 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
168 int retval;
170 /* get true return value from &status */
171 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
172 return status;
175 static int slot_reset_iter(struct device *device, void *data)
177 struct pcie_device *pcie_device;
178 struct pcie_port_service_driver *driver;
179 pci_ers_result_t status, *result;
181 result = (pci_ers_result_t *) data;
183 if (device->bus == &pcie_port_bus_type && device->driver) {
184 driver = to_service_driver(device->driver);
185 if (driver &&
186 driver->err_handler &&
187 driver->err_handler->slot_reset) {
188 pcie_device = to_pcie_device(device);
190 /* Forward error message to service drivers */
191 status = driver->err_handler->slot_reset(
192 pcie_device->port);
193 *result = merge_result(*result, status);
197 return 0;
200 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
202 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
203 int retval;
205 /* If fatal, restore cfg space for possible link reset at upstream */
206 if (dev->error_state == pci_channel_io_frozen) {
207 dev->state_saved = true;
208 pci_restore_state(dev);
209 pcie_portdrv_restore_config(dev);
210 pci_enable_pcie_error_reporting(dev);
213 /* get true return value from &status */
214 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
216 return status;
219 static int resume_iter(struct device *device, void *data)
221 struct pcie_device *pcie_device;
222 struct pcie_port_service_driver *driver;
224 if (device->bus == &pcie_port_bus_type && device->driver) {
225 driver = to_service_driver(device->driver);
226 if (driver &&
227 driver->err_handler &&
228 driver->err_handler->resume) {
229 pcie_device = to_pcie_device(device);
231 /* Forward error message to service drivers */
232 driver->err_handler->resume(pcie_device->port);
236 return 0;
239 static void pcie_portdrv_err_resume(struct pci_dev *dev)
241 int retval;
242 /* nothing to do with error value, if it ever happens */
243 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
247 * LINUX Device Driver Model
249 static const struct pci_device_id port_pci_ids[] = { {
250 /* handle any PCI-Express port */
251 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
252 }, { /* end: all zeroes */ }
254 MODULE_DEVICE_TABLE(pci, port_pci_ids);
256 static struct pci_error_handlers pcie_portdrv_err_handler = {
257 .error_detected = pcie_portdrv_error_detected,
258 .mmio_enabled = pcie_portdrv_mmio_enabled,
259 .slot_reset = pcie_portdrv_slot_reset,
260 .resume = pcie_portdrv_err_resume,
263 static struct pci_driver pcie_portdriver = {
264 .name = "pcieport",
265 .id_table = &port_pci_ids[0],
267 .probe = pcie_portdrv_probe,
268 .remove = pcie_portdrv_remove,
270 .err_handler = &pcie_portdrv_err_handler,
272 .driver.pm = PCIE_PORTDRV_PM_OPS,
275 static int __init pcie_portdrv_init(void)
277 int retval;
279 retval = pcie_port_bus_register();
280 if (retval) {
281 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
282 goto out;
284 retval = pci_register_driver(&pcie_portdriver);
285 if (retval)
286 pcie_port_bus_unregister();
287 out:
288 return retval;
291 static void __exit pcie_portdrv_exit(void)
293 pci_unregister_driver(&pcie_portdriver);
294 pcie_port_bus_unregister();
297 module_init(pcie_portdrv_init);
298 module_exit(pcie_portdrv_exit);