blob: 5ea566e20b375410ed2cc5b19d30d8eec8992fd8 [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. Wysockia79d6822008-12-27 16:28:58 +010058static int pcie_portdrv_resume(struct pci_dev *dev)
59{
Rafael J. Wysocki27be54a2009-02-04 02:00:11 +010060 pci_set_master(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080061 return pcie_port_device_resume(dev);
62}
63#else
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010064#define pcie_portdrv_suspend NULL
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010065#define pcie_portdrv_resume NULL
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080066#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * pcie_portdrv_probe - Probe PCI-Express port devices
70 * @dev: PCI-Express port device being probed
71 *
72 * If detected invokes the pcie_port_device_register() method for
73 * this port device.
74 *
75 */
76static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
77 const struct pci_device_id *id )
78{
79 int status;
80
81 status = pcie_port_device_probe(dev);
82 if (status)
83 return status;
84
85 if (pci_enable_device(dev) < 0)
86 return -ENODEV;
87
88 pci_set_master(dev);
Kenji Kaneshige83e42bc2006-12-06 22:07:30 +090089 if (!dev->irq && dev->pin) {
Bjorn Helgaas34a2e152008-08-25 15:45:20 -060090 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
Bjorn Helgaas34438ba2008-06-13 10:52:13 -060091 "check vendor BIOS\n", dev->vendor, dev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Randy Dunlape4fd1f42006-07-06 21:36:01 -070093 if (pcie_port_device_register(dev)) {
94 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -ENOMEM;
Randy Dunlape4fd1f42006-07-06 21:36:01 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080098 pcie_portdrv_save_config(dev);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
103static void pcie_portdrv_remove (struct pci_dev *dev)
104{
105 pcie_port_device_remove(dev);
Alex Chiangd8998712009-03-07 19:35:47 -0700106 pci_disable_device(dev);
long5823d102005-06-22 09:09:54 -0700107 kfree(pci_get_drvdata(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800110static int error_detected_iter(struct device *device, void *data)
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400111{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800112 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;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400116
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800117 result_data = (struct aer_broadcast_data *) data;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400118
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800119 if (device->bus == &pcie_port_bus_type && device->driver) {
120 driver = to_service_driver(device->driver);
121 if (!driver ||
122 !driver->err_handler ||
123 !driver->err_handler->error_detected)
124 return 0;
125
126 pcie_device = to_pcie_device(device);
127
128 /* Forward error detected message to service drivers */
129 status = driver->err_handler->error_detected(
130 pcie_device->port,
131 result_data->state);
132 result_data->result =
133 merge_result(result_data->result, status);
134 }
135
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400136 return 0;
137}
138
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800139static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
140 enum pci_channel_state error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800142 struct aer_broadcast_data result_data =
143 {error, PCI_ERS_RESULT_CAN_RECOVER};
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700144 int retval;
long5823d102005-06-22 09:09:54 -0700145
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700146 /* can not fail */
147 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800148
149 return result_data.result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800152static int mmio_enabled_iter(struct device *device, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800154 struct pcie_device *pcie_device;
155 struct pcie_port_service_driver *driver;
156 pci_ers_result_t status, *result;
157
158 result = (pci_ers_result_t *) data;
159
160 if (device->bus == &pcie_port_bus_type && device->driver) {
161 driver = to_service_driver(device->driver);
162 if (driver &&
163 driver->err_handler &&
164 driver->err_handler->mmio_enabled) {
165 pcie_device = to_pcie_device(device);
166
167 /* Forward error message to service drivers */
168 status = driver->err_handler->mmio_enabled(
169 pcie_device->port);
170 *result = merge_result(*result, status);
171 }
172 }
173
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800176
177static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
178{
179 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700180 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800181
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700182 /* get true return value from &status */
183 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800184 return status;
185}
186
187static int slot_reset_iter(struct device *device, void *data)
188{
189 struct pcie_device *pcie_device;
190 struct pcie_port_service_driver *driver;
191 pci_ers_result_t status, *result;
192
193 result = (pci_ers_result_t *) data;
194
195 if (device->bus == &pcie_port_bus_type && device->driver) {
196 driver = to_service_driver(device->driver);
197 if (driver &&
198 driver->err_handler &&
199 driver->err_handler->slot_reset) {
200 pcie_device = to_pcie_device(device);
201
202 /* Forward error message to service drivers */
203 status = driver->err_handler->slot_reset(
204 pcie_device->port);
205 *result = merge_result(*result, status);
206 }
207 }
208
209 return 0;
210}
211
212static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
213{
Linas Vepstasbf164412007-11-02 15:14:28 -0500214 pci_ers_result_t status = PCI_ERS_RESULT_NONE;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700215 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800216
217 /* If fatal, restore cfg space for possible link reset at upstream */
218 if (dev->error_state == pci_channel_io_frozen) {
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100219 pci_restore_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800220 pcie_portdrv_restore_config(dev);
221 pci_enable_pcie_error_reporting(dev);
222 }
223
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700224 /* get true return value from &status */
225 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800226
227 return status;
228}
229
230static int resume_iter(struct device *device, void *data)
231{
232 struct pcie_device *pcie_device;
233 struct pcie_port_service_driver *driver;
234
235 if (device->bus == &pcie_port_bus_type && device->driver) {
236 driver = to_service_driver(device->driver);
237 if (driver &&
238 driver->err_handler &&
239 driver->err_handler->resume) {
240 pcie_device = to_pcie_device(device);
241
242 /* Forward error message to service drivers */
243 driver->err_handler->resume(pcie_device->port);
244 }
245 }
246
247 return 0;
248}
249
250static void pcie_portdrv_err_resume(struct pci_dev *dev)
251{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700252 int retval;
253 /* nothing to do with error value, if it ever happens */
254 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/*
258 * LINUX Device Driver Model
259 */
260static 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 */ }
264};
265MODULE_DEVICE_TABLE(pci, port_pci_ids);
266
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800267static 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,
272};
273
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100274static struct pci_driver pcie_portdriver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 .name = (char *)device_name,
276 .id_table = &port_pci_ids[0],
277
278 .probe = pcie_portdrv_probe,
279 .remove = pcie_portdrv_remove,
280
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100281 .suspend = pcie_portdrv_suspend,
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100282 .resume = pcie_portdrv_resume,
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800283
284 .err_handler = &pcie_portdrv_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287static int __init pcie_portdrv_init(void)
288{
Randy Dunlap20d51662006-07-08 22:58:25 -0700289 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Randy Dunlap20d51662006-07-08 22:58:25 -0700291 retval = pcie_port_bus_register();
292 if (retval) {
293 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
294 goto out;
295 }
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100296 retval = pci_register_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (retval)
298 pcie_port_bus_unregister();
Randy Dunlap20d51662006-07-08 22:58:25 -0700299 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return retval;
301}
302
303static void __exit pcie_portdrv_exit(void)
304{
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100305 pci_unregister_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 pcie_port_bus_unregister();
307}
308
309module_init(pcie_portdrv_init);
310module_exit(pcie_portdrv_exit);