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 pci_restore_state(dev
);
45 retval
= pci_enable_device(dev
);
53 static int pcie_portdrv_suspend(struct pci_dev
*dev
, pm_message_t state
)
55 int ret
= pcie_port_device_suspend(dev
, state
);
58 ret
= pcie_portdrv_save_config(dev
);
62 static int pcie_portdrv_resume(struct pci_dev
*dev
)
64 pcie_portdrv_restore_config(dev
);
65 return pcie_port_device_resume(dev
);
68 #define pcie_portdrv_suspend NULL
69 #define pcie_portdrv_resume NULL
73 * pcie_portdrv_probe - Probe PCI-Express port devices
74 * @dev: PCI-Express port device being probed
76 * If detected invokes the pcie_port_device_register() method for
80 static int __devinit
pcie_portdrv_probe (struct pci_dev
*dev
,
81 const struct pci_device_id
*id
)
85 status
= pcie_port_device_probe(dev
);
89 if (pci_enable_device(dev
) < 0)
93 if (!dev
->irq
&& dev
->pin
) {
94 dev_warn(&dev
->dev
, "device [%04x:%04x] has invalid IRQ; "
95 "check vendor BIOS\n", dev
->vendor
, dev
->device
);
97 if (pcie_port_device_register(dev
)) {
98 pci_disable_device(dev
);
102 pcie_portdrv_save_config(dev
);
104 pci_enable_pcie_error_reporting(dev
);
109 static void pcie_portdrv_remove (struct pci_dev
*dev
)
111 pcie_port_device_remove(dev
);
112 kfree(pci_get_drvdata(dev
));
115 static int error_detected_iter(struct device
*device
, void *data
)
117 struct pcie_device
*pcie_device
;
118 struct pcie_port_service_driver
*driver
;
119 struct aer_broadcast_data
*result_data
;
120 pci_ers_result_t status
;
122 result_data
= (struct aer_broadcast_data
*) data
;
124 if (device
->bus
== &pcie_port_bus_type
&& device
->driver
) {
125 driver
= to_service_driver(device
->driver
);
127 !driver
->err_handler
||
128 !driver
->err_handler
->error_detected
)
131 pcie_device
= to_pcie_device(device
);
133 /* Forward error detected message to service drivers */
134 status
= driver
->err_handler
->error_detected(
137 result_data
->result
=
138 merge_result(result_data
->result
, status
);
144 static pci_ers_result_t
pcie_portdrv_error_detected(struct pci_dev
*dev
,
145 enum pci_channel_state error
)
147 struct aer_broadcast_data result_data
=
148 {error
, PCI_ERS_RESULT_CAN_RECOVER
};
152 retval
= device_for_each_child(&dev
->dev
, &result_data
, error_detected_iter
);
154 return result_data
.result
;
157 static int mmio_enabled_iter(struct device
*device
, void *data
)
159 struct pcie_device
*pcie_device
;
160 struct pcie_port_service_driver
*driver
;
161 pci_ers_result_t status
, *result
;
163 result
= (pci_ers_result_t
*) data
;
165 if (device
->bus
== &pcie_port_bus_type
&& device
->driver
) {
166 driver
= to_service_driver(device
->driver
);
168 driver
->err_handler
&&
169 driver
->err_handler
->mmio_enabled
) {
170 pcie_device
= to_pcie_device(device
);
172 /* Forward error message to service drivers */
173 status
= driver
->err_handler
->mmio_enabled(
175 *result
= merge_result(*result
, status
);
182 static pci_ers_result_t
pcie_portdrv_mmio_enabled(struct pci_dev
*dev
)
184 pci_ers_result_t status
= PCI_ERS_RESULT_RECOVERED
;
187 /* get true return value from &status */
188 retval
= device_for_each_child(&dev
->dev
, &status
, mmio_enabled_iter
);
192 static int slot_reset_iter(struct device
*device
, void *data
)
194 struct pcie_device
*pcie_device
;
195 struct pcie_port_service_driver
*driver
;
196 pci_ers_result_t status
, *result
;
198 result
= (pci_ers_result_t
*) data
;
200 if (device
->bus
== &pcie_port_bus_type
&& device
->driver
) {
201 driver
= to_service_driver(device
->driver
);
203 driver
->err_handler
&&
204 driver
->err_handler
->slot_reset
) {
205 pcie_device
= to_pcie_device(device
);
207 /* Forward error message to service drivers */
208 status
= driver
->err_handler
->slot_reset(
210 *result
= merge_result(*result
, status
);
217 static pci_ers_result_t
pcie_portdrv_slot_reset(struct pci_dev
*dev
)
219 pci_ers_result_t status
= PCI_ERS_RESULT_NONE
;
222 /* If fatal, restore cfg space for possible link reset at upstream */
223 if (dev
->error_state
== pci_channel_io_frozen
) {
224 pcie_portdrv_restore_config(dev
);
225 pci_enable_pcie_error_reporting(dev
);
228 /* get true return value from &status */
229 retval
= device_for_each_child(&dev
->dev
, &status
, slot_reset_iter
);
234 static int resume_iter(struct device
*device
, void *data
)
236 struct pcie_device
*pcie_device
;
237 struct pcie_port_service_driver
*driver
;
239 if (device
->bus
== &pcie_port_bus_type
&& device
->driver
) {
240 driver
= to_service_driver(device
->driver
);
242 driver
->err_handler
&&
243 driver
->err_handler
->resume
) {
244 pcie_device
= to_pcie_device(device
);
246 /* Forward error message to service drivers */
247 driver
->err_handler
->resume(pcie_device
->port
);
254 static void pcie_portdrv_err_resume(struct pci_dev
*dev
)
257 /* nothing to do with error value, if it ever happens */
258 retval
= device_for_each_child(&dev
->dev
, NULL
, resume_iter
);
262 * LINUX Device Driver Model
264 static const struct pci_device_id port_pci_ids
[] = { {
265 /* handle any PCI-Express port */
266 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI
<< 8) | 0x00), ~0),
267 }, { /* end: all zeroes */ }
269 MODULE_DEVICE_TABLE(pci
, port_pci_ids
);
271 static struct pci_error_handlers pcie_portdrv_err_handler
= {
272 .error_detected
= pcie_portdrv_error_detected
,
273 .mmio_enabled
= pcie_portdrv_mmio_enabled
,
274 .slot_reset
= pcie_portdrv_slot_reset
,
275 .resume
= pcie_portdrv_err_resume
,
278 static struct pci_driver pcie_portdriver
= {
279 .name
= (char *)device_name
,
280 .id_table
= &port_pci_ids
[0],
282 .probe
= pcie_portdrv_probe
,
283 .remove
= pcie_portdrv_remove
,
285 .suspend
= pcie_portdrv_suspend
,
286 .resume
= pcie_portdrv_resume
,
288 .err_handler
= &pcie_portdrv_err_handler
,
291 static int __init
pcie_portdrv_init(void)
295 retval
= pcie_port_bus_register();
297 printk(KERN_WARNING
"PCIE: bus_register error: %d\n", retval
);
300 retval
= pci_register_driver(&pcie_portdriver
);
302 pcie_port_bus_unregister();
307 static void __exit
pcie_portdrv_exit(void)
309 pci_unregister_driver(&pcie_portdriver
);
310 pcie_port_bus_unregister();
313 module_init(pcie_portdrv_init
);
314 module_exit(pcie_portdrv_exit
);