3 * Purpose: PCI Express Port Bus Driver
5 * Copyright (C) 2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/pcieport_if.h>
17 #include <linux/aer.h>
20 #include "aer/aerdrv.h"
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");
33 static const char device_name[] = "pcieport-driver";
35 static int pcie_portdrv_save_config(struct pci_dev *dev)
37 return pci_save_state(dev);
40 static int pcie_portdrv_restore_config(struct pci_dev *dev)
44 retval = pci_enable_device(dev);
52 static int pcie_portdrv_suspend(struct pci_dev *dev, pm_message_t state)
54 return pcie_port_device_suspend(dev, state);
58 static int pcie_portdrv_suspend_late(struct pci_dev *dev, pm_message_t state)
60 return pci_save_state(dev);
63 static int pcie_portdrv_resume_early(struct pci_dev *dev)
65 return pci_restore_state(dev);
68 static int pcie_portdrv_resume(struct pci_dev *dev)
70 pcie_portdrv_restore_config(dev);
71 return pcie_port_device_resume(dev);
74 #define pcie_portdrv_suspend NULL
75 #define pcie_portdrv_suspend_late NULL
76 #define pcie_portdrv_resume_early NULL
77 #define pcie_portdrv_resume NULL
81 * pcie_portdrv_probe - Probe PCI-Express port devices
82 * @dev: PCI-Express port device being probed
84 * If detected invokes the pcie_port_device_register() method for
88 static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
89 const struct pci_device_id *id )
93 status = pcie_port_device_probe(dev);
97 if (pci_enable_device(dev) < 0)
101 if (!dev->irq && dev->pin) {
102 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
103 "check vendor BIOS\n", dev->vendor, dev->device);
105 if (pcie_port_device_register(dev)) {
106 pci_disable_device(dev);
110 pcie_portdrv_save_config(dev);
112 pci_enable_pcie_error_reporting(dev);
117 static void pcie_portdrv_remove (struct pci_dev *dev)
119 pcie_port_device_remove(dev);
120 kfree(pci_get_drvdata(dev));
123 static int error_detected_iter(struct device *device, void *data)
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;
130 result_data = (struct aer_broadcast_data *) data;
132 if (device->bus == &pcie_port_bus_type && device->driver) {
133 driver = to_service_driver(device->driver);
135 !driver->err_handler ||
136 !driver->err_handler->error_detected)
139 pcie_device = to_pcie_device(device);
141 /* Forward error detected message to service drivers */
142 status = driver->err_handler->error_detected(
145 result_data->result =
146 merge_result(result_data->result, status);
152 static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
153 enum pci_channel_state error)
155 struct aer_broadcast_data result_data =
156 {error, PCI_ERS_RESULT_CAN_RECOVER};
160 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
162 return result_data.result;
165 static int mmio_enabled_iter(struct device *device, void *data)
167 struct pcie_device *pcie_device;
168 struct pcie_port_service_driver *driver;
169 pci_ers_result_t status, *result;
171 result = (pci_ers_result_t *) data;
173 if (device->bus == &pcie_port_bus_type && device->driver) {
174 driver = to_service_driver(device->driver);
176 driver->err_handler &&
177 driver->err_handler->mmio_enabled) {
178 pcie_device = to_pcie_device(device);
180 /* Forward error message to service drivers */
181 status = driver->err_handler->mmio_enabled(
183 *result = merge_result(*result, status);
190 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
192 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
195 /* get true return value from &status */
196 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
200 static int slot_reset_iter(struct device *device, void *data)
202 struct pcie_device *pcie_device;
203 struct pcie_port_service_driver *driver;
204 pci_ers_result_t status, *result;
206 result = (pci_ers_result_t *) data;
208 if (device->bus == &pcie_port_bus_type && device->driver) {
209 driver = to_service_driver(device->driver);
211 driver->err_handler &&
212 driver->err_handler->slot_reset) {
213 pcie_device = to_pcie_device(device);
215 /* Forward error message to service drivers */
216 status = driver->err_handler->slot_reset(
218 *result = merge_result(*result, status);
225 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
227 pci_ers_result_t status = PCI_ERS_RESULT_NONE;
230 /* If fatal, restore cfg space for possible link reset at upstream */
231 if (dev->error_state == pci_channel_io_frozen) {
232 pci_restore_state(dev);
233 pcie_portdrv_restore_config(dev);
234 pci_enable_pcie_error_reporting(dev);
237 /* get true return value from &status */
238 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
243 static int resume_iter(struct device *device, void *data)
245 struct pcie_device *pcie_device;
246 struct pcie_port_service_driver *driver;
248 if (device->bus == &pcie_port_bus_type && device->driver) {
249 driver = to_service_driver(device->driver);
251 driver->err_handler &&
252 driver->err_handler->resume) {
253 pcie_device = to_pcie_device(device);
255 /* Forward error message to service drivers */
256 driver->err_handler->resume(pcie_device->port);
263 static void pcie_portdrv_err_resume(struct pci_dev *dev)
266 /* nothing to do with error value, if it ever happens */
267 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
271 * LINUX Device Driver Model
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 */ }
278 MODULE_DEVICE_TABLE(pci, port_pci_ids);
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,
287 static struct pci_driver pcie_portdriver = {
288 .name = (char *)device_name,
289 .id_table = &port_pci_ids[0],
291 .probe = pcie_portdrv_probe,
292 .remove = pcie_portdrv_remove,
294 .suspend = pcie_portdrv_suspend,
295 .suspend_late = pcie_portdrv_suspend_late,
296 .resume_early = pcie_portdrv_resume_early,
297 .resume = pcie_portdrv_resume,
299 .err_handler = &pcie_portdrv_err_handler,
302 static int __init pcie_portdrv_init(void)
306 retval = pcie_port_bus_register();
308 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
311 retval = pci_register_driver(&pcie_portdriver);
313 pcie_port_bus_unregister();
318 static void __exit pcie_portdrv_exit(void)
320 pci_unregister_driver(&pcie_portdriver);
321 pcie_port_bus_unregister();
324 module_init(pcie_portdrv_init);
325 module_exit(pcie_portdrv_exit);