blob: 037690e08f5f023a0e012806efbe9627b2450c7c [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
40#ifdef CONFIG_PM
41static int pcie_portdrv_restore_config(struct pci_dev *dev)
42{
43 int retval;
44
45 pci_restore_state(dev);
46 retval = pci_enable_device(dev);
47 if (retval)
48 return retval;
49 pci_set_master(dev);
50 return 0;
51}
52
53static int pcie_portdrv_suspend(struct pci_dev *dev, pm_message_t state)
54{
55 int ret = pcie_port_device_suspend(dev, state);
56
57 if (!ret)
58 ret = pcie_portdrv_save_config(dev);
59 return ret;
60}
61
62static int pcie_portdrv_resume(struct pci_dev *dev)
63{
64 pcie_portdrv_restore_config(dev);
65 return pcie_port_device_resume(dev);
66}
67#else
68#define pcie_portdrv_suspend NULL
69#define pcie_portdrv_resume NULL
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * pcie_portdrv_probe - Probe PCI-Express port devices
74 * @dev: PCI-Express port device being probed
75 *
76 * If detected invokes the pcie_port_device_register() method for
77 * this port device.
78 *
79 */
80static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
81 const struct pci_device_id *id )
82{
83 int status;
84
85 status = pcie_port_device_probe(dev);
86 if (status)
87 return status;
88
89 if (pci_enable_device(dev) < 0)
90 return -ENODEV;
91
92 pci_set_master(dev);
93 if (!dev->irq) {
94 printk(KERN_WARNING
95 "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n",
96 __FUNCTION__, dev->device, dev->vendor);
97 }
Randy Dunlape4fd1f42006-07-06 21:36:01 -070098 if (pcie_port_device_register(dev)) {
99 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -ENOMEM;
Randy Dunlape4fd1f42006-07-06 21:36:01 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800103 pcie_portdrv_save_config(dev);
104
105 pci_enable_pcie_error_reporting(dev);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110static void pcie_portdrv_remove (struct pci_dev *dev)
111{
112 pcie_port_device_remove(dev);
long5823d102005-06-22 09:09:54 -0700113 kfree(pci_get_drvdata(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800116static int error_detected_iter(struct device *device, void *data)
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400117{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800118 struct pcie_device *pcie_device;
119 struct pcie_port_service_driver *driver;
120 struct aer_broadcast_data *result_data;
121 pci_ers_result_t status;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400122
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800123 result_data = (struct aer_broadcast_data *) data;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400124
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800125 if (device->bus == &pcie_port_bus_type && device->driver) {
126 driver = to_service_driver(device->driver);
127 if (!driver ||
128 !driver->err_handler ||
129 !driver->err_handler->error_detected)
130 return 0;
131
132 pcie_device = to_pcie_device(device);
133
134 /* Forward error detected message to service drivers */
135 status = driver->err_handler->error_detected(
136 pcie_device->port,
137 result_data->state);
138 result_data->result =
139 merge_result(result_data->result, status);
140 }
141
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400142 return 0;
143}
144
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800145static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
146 enum pci_channel_state error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800148 struct aer_broadcast_data result_data =
149 {error, PCI_ERS_RESULT_CAN_RECOVER};
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700150 int retval;
long5823d102005-06-22 09:09:54 -0700151
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700152 /* can not fail */
153 retval = device_for_each_child(&dev->dev, &result_data, error_detected_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800154
155 return result_data.result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800158static int mmio_enabled_iter(struct device *device, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800160 struct pcie_device *pcie_device;
161 struct pcie_port_service_driver *driver;
162 pci_ers_result_t status, *result;
163
164 result = (pci_ers_result_t *) data;
165
166 if (device->bus == &pcie_port_bus_type && device->driver) {
167 driver = to_service_driver(device->driver);
168 if (driver &&
169 driver->err_handler &&
170 driver->err_handler->mmio_enabled) {
171 pcie_device = to_pcie_device(device);
172
173 /* Forward error message to service drivers */
174 status = driver->err_handler->mmio_enabled(
175 pcie_device->port);
176 *result = merge_result(*result, status);
177 }
178 }
179
180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800182
183static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
184{
185 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700186 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800187
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700188 /* get true return value from &status */
189 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800190 return status;
191}
192
193static int slot_reset_iter(struct device *device, void *data)
194{
195 struct pcie_device *pcie_device;
196 struct pcie_port_service_driver *driver;
197 pci_ers_result_t status, *result;
198
199 result = (pci_ers_result_t *) data;
200
201 if (device->bus == &pcie_port_bus_type && device->driver) {
202 driver = to_service_driver(device->driver);
203 if (driver &&
204 driver->err_handler &&
205 driver->err_handler->slot_reset) {
206 pcie_device = to_pcie_device(device);
207
208 /* Forward error message to service drivers */
209 status = driver->err_handler->slot_reset(
210 pcie_device->port);
211 *result = merge_result(*result, status);
212 }
213 }
214
215 return 0;
216}
217
218static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
219{
220 pci_ers_result_t status;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700221 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800222
223 /* If fatal, restore cfg space for possible link reset at upstream */
224 if (dev->error_state == pci_channel_io_frozen) {
225 pcie_portdrv_restore_config(dev);
226 pci_enable_pcie_error_reporting(dev);
227 }
228
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700229 /* get true return value from &status */
230 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800231
232 return status;
233}
234
235static int resume_iter(struct device *device, void *data)
236{
237 struct pcie_device *pcie_device;
238 struct pcie_port_service_driver *driver;
239
240 if (device->bus == &pcie_port_bus_type && device->driver) {
241 driver = to_service_driver(device->driver);
242 if (driver &&
243 driver->err_handler &&
244 driver->err_handler->resume) {
245 pcie_device = to_pcie_device(device);
246
247 /* Forward error message to service drivers */
248 driver->err_handler->resume(pcie_device->port);
249 }
250 }
251
252 return 0;
253}
254
255static void pcie_portdrv_err_resume(struct pci_dev *dev)
256{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700257 int retval;
258 /* nothing to do with error value, if it ever happens */
259 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*
263 * LINUX Device Driver Model
264 */
265static const struct pci_device_id port_pci_ids[] = { {
266 /* handle any PCI-Express port */
267 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
268 }, { /* end: all zeroes */ }
269};
270MODULE_DEVICE_TABLE(pci, port_pci_ids);
271
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800272static struct pci_error_handlers pcie_portdrv_err_handler = {
273 .error_detected = pcie_portdrv_error_detected,
274 .mmio_enabled = pcie_portdrv_mmio_enabled,
275 .slot_reset = pcie_portdrv_slot_reset,
276 .resume = pcie_portdrv_err_resume,
277};
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static struct pci_driver pcie_portdrv = {
280 .name = (char *)device_name,
281 .id_table = &port_pci_ids[0],
282
283 .probe = pcie_portdrv_probe,
284 .remove = pcie_portdrv_remove,
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .suspend = pcie_portdrv_suspend,
287 .resume = pcie_portdrv_resume,
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800288
289 .err_handler = &pcie_portdrv_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
292static int __init pcie_portdrv_init(void)
293{
Randy Dunlap20d51662006-07-08 22:58:25 -0700294 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Randy Dunlap20d51662006-07-08 22:58:25 -0700296 retval = pcie_port_bus_register();
297 if (retval) {
298 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
299 goto out;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 retval = pci_register_driver(&pcie_portdrv);
302 if (retval)
303 pcie_port_bus_unregister();
Randy Dunlap20d51662006-07-08 22:58:25 -0700304 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return retval;
306}
307
308static void __exit pcie_portdrv_exit(void)
309{
310 pci_unregister_driver(&pcie_portdrv);
311 pcie_port_bus_unregister();
312}
313
314module_init(pcie_portdrv_init);
315module_exit(pcie_portdrv_exit);