blob: 8f1338d1c53facf5bb9a1b8f5c72f433d1ac411b [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>
15#include <linux/pcieport_if.h>
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080016#include <linux/aer.h>
Rafael J. Wysockic39fae12010-02-17 23:40:07 +010017#include <linux/dmi.h>
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020018#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "portdrv.h"
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080021#include "aer/aerdrv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/*
24 * Version Information
25 */
26#define DRIVER_VERSION "v1.0"
27#define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
Stefan Assmann7e8af372009-12-03 18:00:10 +010028#define DRIVER_DESC "PCIe Port Bus Driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029MODULE_AUTHOR(DRIVER_AUTHOR);
30MODULE_DESCRIPTION(DRIVER_DESC);
31MODULE_LICENSE("GPL");
32
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +020033/* If this switch is set, PCIe port native services should not be enabled. */
34bool pcie_ports_disabled;
35
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020036/*
37 * If this switch is set, ACPI _OSC will be used to determine whether or not to
38 * enable PCIe port native services.
39 */
40bool pcie_ports_auto = true;
41
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +020042static int __init pcie_port_setup(char *str)
43{
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020044 if (!strncmp(str, "compat", 6)) {
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +020045 pcie_ports_disabled = true;
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +020046 } else if (!strncmp(str, "native", 6)) {
47 pcie_ports_disabled = false;
48 pcie_ports_auto = false;
49 } else if (!strncmp(str, "auto", 4)) {
50 pcie_ports_disabled = false;
51 pcie_ports_auto = true;
52 }
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +020053
54 return 1;
55}
56__setup("pcie_ports=", pcie_port_setup);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* global data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080060static int pcie_portdrv_restore_config(struct pci_dev *dev)
61{
62 int retval;
63
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080064 retval = pci_enable_device(dev);
65 if (retval)
66 return retval;
67 pci_set_master(dev);
68 return 0;
69}
70
Zhang, Yanmin0bed2082006-09-28 14:35:59 +080071#ifdef CONFIG_PM
Alexey Dobriyan47145212009-12-14 18:00:08 -080072static const struct dev_pm_ops pcie_portdrv_pm_ops = {
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010073 .suspend = pcie_port_device_suspend,
74 .resume = pcie_port_device_resume,
75 .freeze = pcie_port_device_suspend,
76 .thaw = pcie_port_device_resume,
77 .poweroff = pcie_port_device_suspend,
78 .restore = pcie_port_device_resume,
79};
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010080
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010081#define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
Rafael J. Wysockia79d6822008-12-27 16:28:58 +010082
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +010083#else /* !PM */
84
85#define PCIE_PORTDRV_PM_OPS NULL
86#endif /* !PM */
Zhang, Yanmin4bf33922006-07-31 15:26:16 +080087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * pcie_portdrv_probe - Probe PCI-Express port devices
90 * @dev: PCI-Express port device being probed
91 *
Hidetoshi Seto40da4182009-12-15 11:38:04 +090092 * If detected invokes the pcie_port_device_register() method for
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * this port device.
94 *
95 */
Kenji Kaneshige898294c2009-11-25 21:00:53 +090096static int __devinit pcie_portdrv_probe(struct pci_dev *dev,
97 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Kenji Kaneshige898294c2009-11-25 21:00:53 +090099 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Kenji Kaneshige898294c2009-11-25 21:00:53 +0900101 if (!pci_is_pcie(dev) ||
102 ((dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
103 (dev->pcie_type != PCI_EXP_TYPE_UPSTREAM) &&
104 (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)))
105 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900107 if (!dev->irq && dev->pin) {
Bjorn Helgaas34a2e152008-08-25 15:45:20 -0600108 dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
Bjorn Helgaas34438ba2008-06-13 10:52:13 -0600109 "check vendor BIOS\n", dev->vendor, dev->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Rafael J. Wysockif118c0c2009-01-13 14:42:01 +0100111 status = pcie_port_device_register(dev);
112 if (status)
113 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Rafael J. Wysocki87d2e2e2009-01-13 14:43:07 +0100115 pci_save_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900120static void pcie_portdrv_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 pcie_port_device_remove(dev);
Alex Chiangd8998712009-03-07 19:35:47 -0700123 pci_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800126static int error_detected_iter(struct device *device, void *data)
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400127{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800128 struct pcie_device *pcie_device;
129 struct pcie_port_service_driver *driver;
130 struct aer_broadcast_data *result_data;
131 pci_ers_result_t status;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400132
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800133 result_data = (struct aer_broadcast_data *) data;
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400134
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800135 if (device->bus == &pcie_port_bus_type && device->driver) {
136 driver = to_service_driver(device->driver);
137 if (!driver ||
138 !driver->err_handler ||
139 !driver->err_handler->error_detected)
140 return 0;
141
142 pcie_device = to_pcie_device(device);
143
144 /* Forward error detected message to service drivers */
145 status = driver->err_handler->error_detected(
146 pcie_device->port,
147 result_data->state);
148 result_data->result =
149 merge_result(result_data->result, status);
150 }
151
Henrik Kretzschmar60854832006-07-06 18:05:51 +0400152 return 0;
153}
154
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800155static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
156 enum pci_channel_state error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900158 struct aer_broadcast_data data = {error, PCI_ERS_RESULT_CAN_RECOVER};
159 int ret;
long5823d102005-06-22 09:09:54 -0700160
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700161 /* can not fail */
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900162 ret = device_for_each_child(&dev->dev, &data, error_detected_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800163
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900164 return data.result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800167static int mmio_enabled_iter(struct device *device, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800169 struct pcie_device *pcie_device;
170 struct pcie_port_service_driver *driver;
171 pci_ers_result_t status, *result;
172
173 result = (pci_ers_result_t *) data;
174
175 if (device->bus == &pcie_port_bus_type && device->driver) {
176 driver = to_service_driver(device->driver);
177 if (driver &&
178 driver->err_handler &&
179 driver->err_handler->mmio_enabled) {
180 pcie_device = to_pcie_device(device);
181
182 /* Forward error message to service drivers */
183 status = driver->err_handler->mmio_enabled(
184 pcie_device->port);
185 *result = merge_result(*result, status);
186 }
187 }
188
189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800191
192static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
193{
194 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700195 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800196
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700197 /* get true return value from &status */
198 retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800199 return status;
200}
201
202static int slot_reset_iter(struct device *device, void *data)
203{
204 struct pcie_device *pcie_device;
205 struct pcie_port_service_driver *driver;
206 pci_ers_result_t status, *result;
207
208 result = (pci_ers_result_t *) data;
209
210 if (device->bus == &pcie_port_bus_type && device->driver) {
211 driver = to_service_driver(device->driver);
212 if (driver &&
213 driver->err_handler &&
214 driver->err_handler->slot_reset) {
215 pcie_device = to_pcie_device(device);
216
217 /* Forward error message to service drivers */
218 status = driver->err_handler->slot_reset(
219 pcie_device->port);
220 *result = merge_result(*result, status);
221 }
222 }
223
224 return 0;
225}
226
227static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
228{
Zhang, Yanmin029091d2009-04-30 14:48:29 +0800229 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700230 int retval;
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800231
232 /* If fatal, restore cfg space for possible link reset at upstream */
233 if (dev->error_state == pci_channel_io_frozen) {
Rafael J. Wysockie9d82882009-09-14 22:25:11 +0200234 dev->state_saved = true;
Rafael J. Wysockia79d6822008-12-27 16:28:58 +0100235 pci_restore_state(dev);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800236 pcie_portdrv_restore_config(dev);
237 pci_enable_pcie_error_reporting(dev);
238 }
239
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700240 /* get true return value from &status */
241 retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800242
243 return status;
244}
245
246static int resume_iter(struct device *device, void *data)
247{
248 struct pcie_device *pcie_device;
249 struct pcie_port_service_driver *driver;
250
251 if (device->bus == &pcie_port_bus_type && device->driver) {
252 driver = to_service_driver(device->driver);
253 if (driver &&
254 driver->err_handler &&
255 driver->err_handler->resume) {
256 pcie_device = to_pcie_device(device);
257
258 /* Forward error message to service drivers */
259 driver->err_handler->resume(pcie_device->port);
260 }
261 }
262
263 return 0;
264}
265
266static void pcie_portdrv_err_resume(struct pci_dev *dev)
267{
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700268 int retval;
269 /* nothing to do with error value, if it ever happens */
270 retval = device_for_each_child(&dev->dev, NULL, resume_iter);
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
274 * LINUX Device Driver Model
275 */
276static const struct pci_device_id port_pci_ids[] = { {
277 /* handle any PCI-Express port */
278 PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
279 }, { /* end: all zeroes */ }
280};
281MODULE_DEVICE_TABLE(pci, port_pci_ids);
282
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800283static struct pci_error_handlers pcie_portdrv_err_handler = {
284 .error_detected = pcie_portdrv_error_detected,
285 .mmio_enabled = pcie_portdrv_mmio_enabled,
286 .slot_reset = pcie_portdrv_slot_reset,
287 .resume = pcie_portdrv_err_resume,
288};
289
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100290static struct pci_driver pcie_portdriver = {
Bjorn Helgaase3fb20f2009-10-05 16:47:34 -0600291 .name = "pcieport",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .id_table = &port_pci_ids[0],
293
294 .probe = pcie_portdrv_probe,
295 .remove = pcie_portdrv_remove,
296
Zhang, Yanmin4bf33922006-07-31 15:26:16 +0800297 .err_handler = &pcie_portdrv_err_handler,
Rafael J. Wysocki3a3c2442009-02-15 22:32:48 +0100298
299 .driver.pm = PCIE_PORTDRV_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Rafael J. Wysockic39fae12010-02-17 23:40:07 +0100302static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
303{
304 pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
305 d->ident);
306 pcie_pme_disable_msi();
307 return 0;
308}
309
310static struct dmi_system_id __initdata pcie_portdrv_dmi_table[] = {
311 /*
312 * Boxes that should not use MSI for PCIe PME signaling.
313 */
314 {
315 .callback = dmi_pcie_pme_disable_msi,
316 .ident = "MSI Wind U-100",
317 .matches = {
318 DMI_MATCH(DMI_SYS_VENDOR,
319 "MICRO-STAR INTERNATIONAL CO., LTD"),
320 DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
321 },
322 },
323 {}
324};
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int __init pcie_portdrv_init(void)
327{
Randy Dunlap20d51662006-07-08 22:58:25 -0700328 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +0200330 if (pcie_ports_disabled) {
331 pcie_no_aspm();
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +0200332 return -EACCES;
Rafael J. Wysocki28eb5f22010-08-21 22:02:38 +0200333 }
Rafael J. Wysocki79dd9182010-08-21 01:51:44 +0200334
Rafael J. Wysockic39fae12010-02-17 23:40:07 +0100335 dmi_check_system(pcie_portdrv_dmi_table);
336
Randy Dunlap20d51662006-07-08 22:58:25 -0700337 retval = pcie_port_bus_register();
338 if (retval) {
339 printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
340 goto out;
341 }
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100342 retval = pci_register_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (retval)
344 pcie_port_bus_unregister();
Randy Dunlap20d51662006-07-08 22:58:25 -0700345 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return retval;
347}
348
Hidetoshi Seto40da4182009-12-15 11:38:04 +0900349static void __exit pcie_portdrv_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Sam Ravnborg3603a6a2007-02-27 10:19:17 +0100351 pci_unregister_driver(&pcie_portdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 pcie_port_bus_unregister();
353}
354
355module_init(pcie_portdrv_init);
356module_exit(pcie_portdrv_exit);