blob: 13c8972886e6e788cf47916507376e5138c1c195 [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"
Stefan Assmann7e8af372009-12-03 18:00:10 +010027#define DRIVER_DESC "PCIe Port Bus Driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028MODULE_AUTHOR(DRIVER_AUTHOR);
29MODULE_DESCRIPTION(DRIVER_DESC);
30MODULE_LICENSE("GPL");
31
32/* global data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080034static int pcie_portdrv_restore_config(struct pci_dev *dev)
35{
36 int retval;
37
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080038 retval = pci_enable_device(dev);
39 if (retval)
40 return retval;
41 pci_set_master(dev);
42 return 0;
43}
44
Zhang, Yanmin0bed2082006-09-28 14:35:59 +080045#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -080046static const struct dev_pm_ops pcie_portdrv_pm_ops = {
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010047 .suspend = pcie_port_device_suspend,
48 .resume = pcie_port_device_resume,
49 .freeze = pcie_port_device_suspend,
50 .thaw = pcie_port_device_resume,
51 .poweroff = pcie_port_device_suspend,
52 .restore = pcie_port_device_resume,
53};
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010054
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010055#define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010056
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010057#else /* !PM */
58
59#define PCIE_PORTDRV_PM_OPS NULL
60#endif /* !PM */
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * pcie_portdrv_probe - Probe PCI-Express port devices
64 * @dev: PCI-Express port device being probed
65 *
Hidetoshi Seto40da4182009-12-15 11:38:04 +090066 * If detected invokes the pcie_port_device_register() method for
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * this port device.
68 *
69 */
Kenji Kaneshige898294c2009-11-25 21:00:53 +090070static int __devinit pcie_portdrv_probe(struct pci_dev *dev,
71 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Kenji Kaneshige898294c2009-11-25 21:00:53 +090073 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Kenji Kaneshige898294c2009-11-25 21:00:53 +090075 if (!pci_is_pcie(dev) ||
76 ((dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
77 (dev->pcie_type != PCI_EXP_TYPE_UPSTREAM) &&
78 (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)))
79 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Hidetoshi Seto40da4182009-12-15 11:38:04 +090081 if (!dev->irq && dev->pin) {
Bjorn Helgaas34a2e152008-08-25 15:45:20 -060082 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
Bjorn Helgaas34438ba2008-06-13 10:52:13 -060083 "check vendor BIOS\n", dev->vendor, dev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Rafael J. Wysockif118c0c2009-01-13 14:42:01 +010085 status = pcie_port_device_register(dev);
86 if (status)
87 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rafael J. Wysocki87d2e2e2009-01-13 14:43:07 +010089 pci_save_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
Hidetoshi Seto40da4182009-12-15 11:38:04 +090094static void pcie_portdrv_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 pcie_port_device_remove(dev);
Alex Chiangd8998712009-03-07 19:35:47 -070097 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800100static int error_detected_iter(struct device *device, void *data)
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400101{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800102 struct pcie_device *pcie_device;
103 struct pcie_port_service_driver *driver;
104 struct aer_broadcast_data *result_data;
105 pci_ers_result_t status;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400106
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800107 result_data = (struct aer_broadcast_data *) data;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400108
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800109 if (device->bus == &pcie_port_bus_type && device->driver) {
110 driver = to_service_driver(device->driver);
111 if (!driver ||
112 !driver->err_handler ||
113 !driver->err_handler->error_detected)
114 return 0;
115
116 pcie_device = to_pcie_device(device);
117
118 /* Forward error detected message to service drivers */
119 status = driver->err_handler->error_detected(
120 pcie_device->port,
121 result_data->state);
122 result_data->result =
123 merge_result(result_data->result, status);
124 }
125
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400126 return 0;
127}
128
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800129static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
130 enum pci_channel_state error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900132 struct aer_broadcast_data data = {error, PCI_ERS_RESULT_CAN_RECOVER};
133 int ret;
long5823d102005-06-22 09:09:54 -0700134
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700135 /* can not fail */
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900136 ret = device_for_each_child(&dev->dev, &data, error_detected_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800137
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900138 return data.result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800141static int mmio_enabled_iter(struct device *device, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800143 struct pcie_device *pcie_device;
144 struct pcie_port_service_driver *driver;
145 pci_ers_result_t status, *result;
146
147 result = (pci_ers_result_t *) data;
148
149 if (device->bus == &pcie_port_bus_type && device->driver) {
150 driver = to_service_driver(device->driver);
151 if (driver &&
152 driver->err_handler &&
153 driver->err_handler->mmio_enabled) {
154 pcie_device = to_pcie_device(device);
155
156 /* Forward error message to service drivers */
157 status = driver->err_handler->mmio_enabled(
158 pcie_device->port);
159 *result = merge_result(*result, status);
160 }
161 }
162
163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800165
166static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
167{
168 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700169 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800170
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700171 /* get true return value from &status */
172 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800173 return status;
174}
175
176static int slot_reset_iter(struct device *device, void *data)
177{
178 struct pcie_device *pcie_device;
179 struct pcie_port_service_driver *driver;
180 pci_ers_result_t status, *result;
181
182 result = (pci_ers_result_t *) data;
183
184 if (device->bus == &pcie_port_bus_type && device->driver) {
185 driver = to_service_driver(device->driver);
186 if (driver &&
187 driver->err_handler &&
188 driver->err_handler->slot_reset) {
189 pcie_device = to_pcie_device(device);
190
191 /* Forward error message to service drivers */
192 status = driver->err_handler->slot_reset(
193 pcie_device->port);
194 *result = merge_result(*result, status);
195 }
196 }
197
198 return 0;
199}
200
201static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
202{
Zhang, Yanmin029091d2009-04-30 14:48:29 +0800203 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700204 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800205
206 /* If fatal, restore cfg space for possible link reset at upstream */
207 if (dev->error_state == pci_channel_io_frozen) {
Rafael J. Wysockie9d82882009-09-14 22:25:11 +0200208 dev->state_saved = true;
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100209 pci_restore_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800210 pcie_portdrv_restore_config(dev);
211 pci_enable_pcie_error_reporting(dev);
212 }
213
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700214 /* get true return value from &status */
215 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800216
217 return status;
218}
219
220static int resume_iter(struct device *device, void *data)
221{
222 struct pcie_device *pcie_device;
223 struct pcie_port_service_driver *driver;
224
225 if (device->bus == &pcie_port_bus_type && device->driver) {
226 driver = to_service_driver(device->driver);
227 if (driver &&
228 driver->err_handler &&
229 driver->err_handler->resume) {
230 pcie_device = to_pcie_device(device);
231
232 /* Forward error message to service drivers */
233 driver->err_handler->resume(pcie_device->port);
234 }
235 }
236
237 return 0;
238}
239
240static void pcie_portdrv_err_resume(struct pci_dev *dev)
241{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700242 int retval;
243 /* nothing to do with error value, if it ever happens */
244 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247/*
248 * LINUX Device Driver Model
249 */
250static const struct pci_device_id port_pci_ids[] = { {
251 /* handle any PCI-Express port */
252 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
253 }, { /* end: all zeroes */ }
254};
255MODULE_DEVICE_TABLE(pci, port_pci_ids);
256
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800257static struct pci_error_handlers pcie_portdrv_err_handler = {
258 .error_detected = pcie_portdrv_error_detected,
259 .mmio_enabled = pcie_portdrv_mmio_enabled,
260 .slot_reset = pcie_portdrv_slot_reset,
261 .resume = pcie_portdrv_err_resume,
262};
263
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100264static struct pci_driver pcie_portdriver = {
Bjorn Helgaase3fb20f2009-10-05 16:47:34 -0600265 .name = "pcieport",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .id_table = &port_pci_ids[0],
267
268 .probe = pcie_portdrv_probe,
269 .remove = pcie_portdrv_remove,
270
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800271 .err_handler = &pcie_portdrv_err_handler,
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +0100272
273 .driver.pm = PCIE_PORTDRV_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
276static int __init pcie_portdrv_init(void)
277{
Randy Dunlap20d51662006-07-08 22:58:25 -0700278 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Randy Dunlap20d51662006-07-08 22:58:25 -0700280 retval = pcie_port_bus_register();
281 if (retval) {
282 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
283 goto out;
284 }
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100285 retval = pci_register_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (retval)
287 pcie_port_bus_unregister();
Randy Dunlap20d51662006-07-08 22:58:25 -0700288 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return retval;
290}
291
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900292static void __exit pcie_portdrv_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100294 pci_unregister_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 pcie_port_bus_unregister();
296}
297
298module_init(pcie_portdrv_init);
299module_exit(pcie_portdrv_exit);