blob: 99a914a027f8530dc3abe923d3edf33d46bd1a3b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Schmielau4e57b682005-10-30 15:03:48 -080015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/pcieport_if.h>
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080017#include <linux/aer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "portdrv.h"
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080020#include "aer/aerdrv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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"
28MODULE_AUTHOR(DRIVER_AUTHOR);
29MODULE_DESCRIPTION(DRIVER_DESC);
30MODULE_LICENSE("GPL");
31
32/* global data */
33static const char device_name[] = "pcieport-driver";
34
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080035static int pcie_portdrv_save_config(struct pci_dev *dev)
36{
37 return pci_save_state(dev);
38}
39
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080040static int pcie_portdrv_restore_config(struct pci_dev *dev)
41{
42 int retval;
43
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080044 retval = pci_enable_device(dev);
45 if (retval)
46 return retval;
47 pci_set_master(dev);
48 return 0;
49}
50
Zhang, Yanmin0bed2082006-09-28 14:35:59 +080051#ifdef CONFIG_PM
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010052static 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. Wysocki90d25f22008-12-06 15:07:59 +010058static int pcie_portdrv_suspend_late(struct pci_dev *dev, pm_message_t state)
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080059{
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010060 return pci_save_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080061}
62
Rafael J. Wysocki90d25f22008-12-06 15:07:59 +010063static int pcie_portdrv_resume_early(struct pci_dev *dev)
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080064{
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010065 return pci_restore_state(dev);
66}
67
68static int pcie_portdrv_resume(struct pci_dev *dev)
69{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080070 pcie_portdrv_restore_config(dev);
71 return pcie_port_device_resume(dev);
72}
73#else
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010074#define pcie_portdrv_suspend NULL
Rafael J. Wysocki90d25f22008-12-06 15:07:59 +010075#define pcie_portdrv_suspend_late NULL
76#define pcie_portdrv_resume_early NULL
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010077#define pcie_portdrv_resume NULL
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080078#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * pcie_portdrv_probe - Probe PCI-Express port devices
82 * @dev: PCI-Express port device being probed
83 *
84 * If detected invokes the pcie_port_device_register() method for
85 * this port device.
86 *
87 */
88static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
89 const struct pci_device_id *id )
90{
91 int status;
92
93 status = pcie_port_device_probe(dev);
94 if (status)
95 return status;
96
97 if (pci_enable_device(dev) < 0)
98 return -ENODEV;
99
100 pci_set_master(dev);
Kenji Kaneshige83e42bc2006-12-06 22:07:30 +0900101 if (!dev->irq && dev->pin) {
Bjorn Helgaas34a2e152008-08-25 15:45:20 -0600102 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
Bjorn Helgaas34438ba2008-06-13 10:52:13 -0600103 "check vendor BIOS\n", dev->vendor, dev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Randy Dunlape4fd1f42006-07-06 21:36:01 -0700105 if (pcie_port_device_register(dev)) {
106 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -ENOMEM;
Randy Dunlape4fd1f42006-07-06 21:36:01 -0700108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800110 pcie_portdrv_save_config(dev);
111
112 pci_enable_pcie_error_reporting(dev);
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115}
116
117static void pcie_portdrv_remove (struct pci_dev *dev)
118{
119 pcie_port_device_remove(dev);
long5823d102005-06-22 09:09:54 -0700120 kfree(pci_get_drvdata(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800123static int error_detected_iter(struct device *device, void *data)
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400124{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800125 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;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400129
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800130 result_data = (struct aer_broadcast_data *) data;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400131
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800132 if (device->bus == &pcie_port_bus_type && device->driver) {
133 driver = to_service_driver(device->driver);
134 if (!driver ||
135 !driver->err_handler ||
136 !driver->err_handler->error_detected)
137 return 0;
138
139 pcie_device = to_pcie_device(device);
140
141 /* Forward error detected message to service drivers */
142 status = driver->err_handler->error_detected(
143 pcie_device->port,
144 result_data->state);
145 result_data->result =
146 merge_result(result_data->result, status);
147 }
148
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400149 return 0;
150}
151
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800152static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
153 enum pci_channel_state error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800155 struct aer_broadcast_data result_data =
156 {error, PCI_ERS_RESULT_CAN_RECOVER};
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700157 int retval;
long5823d102005-06-22 09:09:54 -0700158
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700159 /* can not fail */
160 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800161
162 return result_data.result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800165static int mmio_enabled_iter(struct device *device, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800167 struct pcie_device *pcie_device;
168 struct pcie_port_service_driver *driver;
169 pci_ers_result_t status, *result;
170
171 result = (pci_ers_result_t *) data;
172
173 if (device->bus == &pcie_port_bus_type && device->driver) {
174 driver = to_service_driver(device->driver);
175 if (driver &&
176 driver->err_handler &&
177 driver->err_handler->mmio_enabled) {
178 pcie_device = to_pcie_device(device);
179
180 /* Forward error message to service drivers */
181 status = driver->err_handler->mmio_enabled(
182 pcie_device->port);
183 *result = merge_result(*result, status);
184 }
185 }
186
187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800189
190static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
191{
192 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700193 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800194
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700195 /* get true return value from &status */
196 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800197 return status;
198}
199
200static int slot_reset_iter(struct device *device, void *data)
201{
202 struct pcie_device *pcie_device;
203 struct pcie_port_service_driver *driver;
204 pci_ers_result_t status, *result;
205
206 result = (pci_ers_result_t *) data;
207
208 if (device->bus == &pcie_port_bus_type && device->driver) {
209 driver = to_service_driver(device->driver);
210 if (driver &&
211 driver->err_handler &&
212 driver->err_handler->slot_reset) {
213 pcie_device = to_pcie_device(device);
214
215 /* Forward error message to service drivers */
216 status = driver->err_handler->slot_reset(
217 pcie_device->port);
218 *result = merge_result(*result, status);
219 }
220 }
221
222 return 0;
223}
224
225static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
226{
Linas Vepstasbf164412007-11-02 15:14:28 -0500227 pci_ers_result_t status = PCI_ERS_RESULT_NONE;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700228 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800229
230 /* If fatal, restore cfg space for possible link reset at upstream */
231 if (dev->error_state == pci_channel_io_frozen) {
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100232 pci_restore_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800233 pcie_portdrv_restore_config(dev);
234 pci_enable_pcie_error_reporting(dev);
235 }
236
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700237 /* get true return value from &status */
238 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800239
240 return status;
241}
242
243static int resume_iter(struct device *device, void *data)
244{
245 struct pcie_device *pcie_device;
246 struct pcie_port_service_driver *driver;
247
248 if (device->bus == &pcie_port_bus_type && device->driver) {
249 driver = to_service_driver(device->driver);
250 if (driver &&
251 driver->err_handler &&
252 driver->err_handler->resume) {
253 pcie_device = to_pcie_device(device);
254
255 /* Forward error message to service drivers */
256 driver->err_handler->resume(pcie_device->port);
257 }
258 }
259
260 return 0;
261}
262
263static void pcie_portdrv_err_resume(struct pci_dev *dev)
264{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700265 int retval;
266 /* nothing to do with error value, if it ever happens */
267 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/*
271 * LINUX Device Driver Model
272 */
273static 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 */ }
277};
278MODULE_DEVICE_TABLE(pci, port_pci_ids);
279
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800280static 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,
285};
286
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100287static struct pci_driver pcie_portdriver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .name = (char *)device_name,
289 .id_table = &port_pci_ids[0],
290
291 .probe = pcie_portdrv_probe,
292 .remove = pcie_portdrv_remove,
293
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100294 .suspend = pcie_portdrv_suspend,
Rafael J. Wysocki90d25f22008-12-06 15:07:59 +0100295 .suspend_late = pcie_portdrv_suspend_late,
296 .resume_early = pcie_portdrv_resume_early,
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100297 .resume = pcie_portdrv_resume,
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800298
299 .err_handler = &pcie_portdrv_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302static int __init pcie_portdrv_init(void)
303{
Randy Dunlap20d51662006-07-08 22:58:25 -0700304 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Randy Dunlap20d51662006-07-08 22:58:25 -0700306 retval = pcie_port_bus_register();
307 if (retval) {
308 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
309 goto out;
310 }
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100311 retval = pci_register_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (retval)
313 pcie_port_bus_unregister();
Randy Dunlap20d51662006-07-08 22:58:25 -0700314 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return retval;
316}
317
318static void __exit pcie_portdrv_exit(void)
319{
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100320 pci_unregister_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pcie_port_bus_unregister();
322}
323
324module_init(pcie_portdrv_init);
325module_exit(pcie_portdrv_exit);