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_resume(struct pci_dev *dev)
61 return pcie_port_device_resume(dev);
64 #define pcie_portdrv_suspend NULL
65 #define pcie_portdrv_resume NULL
69 * pcie_portdrv_probe - Probe PCI-Express port devices
70 * @dev: PCI-Express port device being probed
72 * If detected invokes the pcie_port_device_register() method for
76 static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
77 const struct pci_device_id *id )
81 status = pcie_port_device_probe(dev);
85 if (pci_enable_device(dev) < 0)
89 if (!dev->irq && dev->pin) {
90 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
91 "check vendor BIOS\n", dev->vendor, dev->device);
93 if (pcie_port_device_register(dev)) {
94 pci_disable_device(dev);
98 pcie_portdrv_save_config(dev);
103 static void pcie_portdrv_remove (struct pci_dev *dev)
105 pcie_port_device_remove(dev);
106 pci_disable_device(dev);
107 kfree(pci_get_drvdata(dev));
110 static int error_detected_iter(struct device *device, void *data)
112 struct pcie_device *pcie_device;
113 struct pcie_port_service_driver *driver;
114 struct aer_broadcast_data *result_data;
115 pci_ers_result_t status;
117 result_data = (struct aer_broadcast_data *) data;
119 if (device->bus == &pcie_port_bus_type && device->driver) {
120 driver = to_service_driver(device->driver);
122 !driver->err_handler ||
123 !driver->err_handler->error_detected)
126 pcie_device = to_pcie_device(device);
128 /* Forward error detected message to service drivers */
129 status = driver->err_handler->error_detected(
132 result_data->result =
133 merge_result(result_data->result, status);
139 static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
140 enum pci_channel_state error)
142 struct aer_broadcast_data result_data =
143 {error, PCI_ERS_RESULT_CAN_RECOVER};
147 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
149 return result_data.result;
152 static int mmio_enabled_iter(struct device *device, void *data)
154 struct pcie_device *pcie_device;
155 struct pcie_port_service_driver *driver;
156 pci_ers_result_t status, *result;
158 result = (pci_ers_result_t *) data;
160 if (device->bus == &pcie_port_bus_type && device->driver) {
161 driver = to_service_driver(device->driver);
163 driver->err_handler &&
164 driver->err_handler->mmio_enabled) {
165 pcie_device = to_pcie_device(device);
167 /* Forward error message to service drivers */
168 status = driver->err_handler->mmio_enabled(
170 *result = merge_result(*result, status);
177 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
179 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
182 /* get true return value from &status */
183 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
187 static int slot_reset_iter(struct device *device, void *data)
189 struct pcie_device *pcie_device;
190 struct pcie_port_service_driver *driver;
191 pci_ers_result_t status, *result;
193 result = (pci_ers_result_t *) data;
195 if (device->bus == &pcie_port_bus_type && device->driver) {
196 driver = to_service_driver(device->driver);
198 driver->err_handler &&
199 driver->err_handler->slot_reset) {
200 pcie_device = to_pcie_device(device);
202 /* Forward error message to service drivers */
203 status = driver->err_handler->slot_reset(
205 *result = merge_result(*result, status);
212 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
214 pci_ers_result_t status = PCI_ERS_RESULT_NONE;
217 /* If fatal, restore cfg space for possible link reset at upstream */
218 if (dev->error_state == pci_channel_io_frozen) {
219 pci_restore_state(dev);
220 pcie_portdrv_restore_config(dev);
221 pci_enable_pcie_error_reporting(dev);
224 /* get true return value from &status */
225 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
230 static int resume_iter(struct device *device, void *data)
232 struct pcie_device *pcie_device;
233 struct pcie_port_service_driver *driver;
235 if (device->bus == &pcie_port_bus_type && device->driver) {
236 driver = to_service_driver(device->driver);
238 driver->err_handler &&
239 driver->err_handler->resume) {
240 pcie_device = to_pcie_device(device);
242 /* Forward error message to service drivers */
243 driver->err_handler->resume(pcie_device->port);
250 static void pcie_portdrv_err_resume(struct pci_dev *dev)
253 /* nothing to do with error value, if it ever happens */
254 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
258 * LINUX Device Driver Model
260 static const struct pci_device_id port_pci_ids[] = { {
261 /* handle any PCI-Express port */
262 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
263 }, { /* end: all zeroes */ }
265 MODULE_DEVICE_TABLE(pci, port_pci_ids);
267 static struct pci_error_handlers pcie_portdrv_err_handler = {
268 .error_detected = pcie_portdrv_error_detected,
269 .mmio_enabled = pcie_portdrv_mmio_enabled,
270 .slot_reset = pcie_portdrv_slot_reset,
271 .resume = pcie_portdrv_err_resume,
274 static struct pci_driver pcie_portdriver = {
275 .name = (char *)device_name,
276 .id_table = &port_pci_ids[0],
278 .probe = pcie_portdrv_probe,
279 .remove = pcie_portdrv_remove,
281 .suspend = pcie_portdrv_suspend,
282 .resume = pcie_portdrv_resume,
284 .err_handler = &pcie_portdrv_err_handler,
287 static int __init pcie_portdrv_init(void)
291 retval = pcie_port_bus_register();
293 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
296 retval = pci_register_driver(&pcie_portdriver);
298 pcie_port_bus_unregister();
303 static void __exit pcie_portdrv_exit(void)
305 pci_unregister_driver(&pcie_portdriver);
306 pcie_port_bus_unregister();
309 module_init(pcie_portdrv_init);
310 module_exit(pcie_portdrv_exit);