1 /*
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>
16 #include <linux/aer.h>
17 #include <linux/dmi.h>
18 #include <linux/pci-aspm.h>
19 
20 #include "portdrv.h"
21 #include "aer/aerdrv.h"
22 
23 /*
24  * Version Information
25  */
26 #define DRIVER_VERSION "v1.0"
27 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
28 #define DRIVER_DESC "PCIe Port Bus Driver"
29 MODULE_AUTHOR(DRIVER_AUTHOR);
30 MODULE_DESCRIPTION(DRIVER_DESC);
31 MODULE_LICENSE("GPL");
32 
33 /* If this switch is set, PCIe port native services should not be enabled. */
34 bool pcie_ports_disabled;
35 
36 /*
37  * If this switch is set, ACPI _OSC will be used to determine whether or not to
38  * enable PCIe port native services.
39  */
40 bool pcie_ports_auto = true;
41 
pcie_port_setup(char * str)42 static int __init pcie_port_setup(char *str)
43 {
44 	if (!strncmp(str, "compat", 6)) {
45 		pcie_ports_disabled = true;
46 	} 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 	}
53 
54 	return 1;
55 }
56 __setup("pcie_ports=", pcie_port_setup);
57 
58 /* global data */
59 
60 /**
61  * pcie_clear_root_pme_status - Clear root port PME interrupt status.
62  * @dev: PCIe root port or event collector.
63  */
pcie_clear_root_pme_status(struct pci_dev * dev)64 void pcie_clear_root_pme_status(struct pci_dev *dev)
65 {
66 	int rtsta_pos;
67 	u32 rtsta;
68 
69 	rtsta_pos = pci_pcie_cap(dev) + PCI_EXP_RTSTA;
70 
71 	pci_read_config_dword(dev, rtsta_pos, &rtsta);
72 	rtsta |= PCI_EXP_RTSTA_PME;
73 	pci_write_config_dword(dev, rtsta_pos, rtsta);
74 }
75 
pcie_portdrv_restore_config(struct pci_dev * dev)76 static int pcie_portdrv_restore_config(struct pci_dev *dev)
77 {
78 	int retval;
79 
80 	retval = pci_enable_device(dev);
81 	if (retval)
82 		return retval;
83 	pci_set_master(dev);
84 	return 0;
85 }
86 
87 #ifdef CONFIG_PM
pcie_port_resume_noirq(struct device * dev)88 static int pcie_port_resume_noirq(struct device *dev)
89 {
90 	struct pci_dev *pdev = to_pci_dev(dev);
91 
92 	/*
93 	 * Some BIOSes forget to clear Root PME Status bits after system wakeup
94 	 * which breaks ACPI-based runtime wakeup on PCI Express, so clear those
95 	 * bits now just in case (shouldn't hurt).
96 	 */
97 	if(pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT)
98 		pcie_clear_root_pme_status(pdev);
99 	return 0;
100 }
101 
102 static const struct dev_pm_ops pcie_portdrv_pm_ops = {
103 	.suspend	= pcie_port_device_suspend,
104 	.resume		= pcie_port_device_resume,
105 	.freeze		= pcie_port_device_suspend,
106 	.thaw		= pcie_port_device_resume,
107 	.poweroff	= pcie_port_device_suspend,
108 	.restore	= pcie_port_device_resume,
109 	.resume_noirq	= pcie_port_resume_noirq,
110 };
111 
112 #define PCIE_PORTDRV_PM_OPS	(&pcie_portdrv_pm_ops)
113 
114 #else /* !PM */
115 
116 #define PCIE_PORTDRV_PM_OPS	NULL
117 #endif /* !PM */
118 
119 /*
120  * pcie_portdrv_probe - Probe PCI-Express port devices
121  * @dev: PCI-Express port device being probed
122  *
123  * If detected invokes the pcie_port_device_register() method for
124  * this port device.
125  *
126  */
pcie_portdrv_probe(struct pci_dev * dev,const struct pci_device_id * id)127 static int __devinit pcie_portdrv_probe(struct pci_dev *dev,
128 					const struct pci_device_id *id)
129 {
130 	int status;
131 
132 	if (!pci_is_pcie(dev) ||
133 	    ((dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
134 	     (dev->pcie_type != PCI_EXP_TYPE_UPSTREAM) &&
135 	     (dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)))
136 		return -ENODEV;
137 
138 	if (!dev->irq && dev->pin) {
139 		dev_warn(&dev->dev, "device [%04x:%04x] has invalid IRQ; "
140 			 "check vendor BIOS\n", dev->vendor, dev->device);
141 	}
142 	status = pcie_port_device_register(dev);
143 	if (status)
144 		return status;
145 
146 	pci_save_state(dev);
147 
148 	return 0;
149 }
150 
pcie_portdrv_remove(struct pci_dev * dev)151 static void pcie_portdrv_remove(struct pci_dev *dev)
152 {
153 	pcie_port_device_remove(dev);
154 }
155 
error_detected_iter(struct device * device,void * data)156 static int error_detected_iter(struct device *device, void *data)
157 {
158 	struct pcie_device *pcie_device;
159 	struct pcie_port_service_driver *driver;
160 	struct aer_broadcast_data *result_data;
161 	pci_ers_result_t status;
162 
163 	result_data = (struct aer_broadcast_data *) data;
164 
165 	if (device->bus == &pcie_port_bus_type && device->driver) {
166 		driver = to_service_driver(device->driver);
167 		if (!driver ||
168 			!driver->err_handler ||
169 			!driver->err_handler->error_detected)
170 			return 0;
171 
172 		pcie_device = to_pcie_device(device);
173 
174 		/* Forward error detected message to service drivers */
175 		status = driver->err_handler->error_detected(
176 			pcie_device->port,
177 			result_data->state);
178 		result_data->result =
179 			merge_result(result_data->result, status);
180 	}
181 
182 	return 0;
183 }
184 
pcie_portdrv_error_detected(struct pci_dev * dev,enum pci_channel_state error)185 static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
186 					enum pci_channel_state error)
187 {
188 	struct aer_broadcast_data data = {error, PCI_ERS_RESULT_CAN_RECOVER};
189 	int ret;
190 
191 	/* can not fail */
192 	ret = device_for_each_child(&dev->dev, &data, error_detected_iter);
193 
194 	return data.result;
195 }
196 
mmio_enabled_iter(struct device * device,void * data)197 static int mmio_enabled_iter(struct device *device, void *data)
198 {
199 	struct pcie_device *pcie_device;
200 	struct pcie_port_service_driver *driver;
201 	pci_ers_result_t status, *result;
202 
203 	result = (pci_ers_result_t *) data;
204 
205 	if (device->bus == &pcie_port_bus_type && device->driver) {
206 		driver = to_service_driver(device->driver);
207 		if (driver &&
208 			driver->err_handler &&
209 			driver->err_handler->mmio_enabled) {
210 			pcie_device = to_pcie_device(device);
211 
212 			/* Forward error message to service drivers */
213 			status = driver->err_handler->mmio_enabled(
214 					pcie_device->port);
215 			*result = merge_result(*result, status);
216 		}
217 	}
218 
219 	return 0;
220 }
221 
pcie_portdrv_mmio_enabled(struct pci_dev * dev)222 static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
223 {
224 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
225 	int retval;
226 
227 	/* get true return value from &status */
228 	retval = device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
229 	return status;
230 }
231 
slot_reset_iter(struct device * device,void * data)232 static int slot_reset_iter(struct device *device, void *data)
233 {
234 	struct pcie_device *pcie_device;
235 	struct pcie_port_service_driver *driver;
236 	pci_ers_result_t status, *result;
237 
238 	result = (pci_ers_result_t *) data;
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->slot_reset) {
245 			pcie_device = to_pcie_device(device);
246 
247 			/* Forward error message to service drivers */
248 			status = driver->err_handler->slot_reset(
249 					pcie_device->port);
250 			*result = merge_result(*result, status);
251 		}
252 	}
253 
254 	return 0;
255 }
256 
pcie_portdrv_slot_reset(struct pci_dev * dev)257 static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
258 {
259 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
260 	int retval;
261 
262 	/* If fatal, restore cfg space for possible link reset at upstream */
263 	if (dev->error_state == pci_channel_io_frozen) {
264 		dev->state_saved = true;
265 		pci_restore_state(dev);
266 		pcie_portdrv_restore_config(dev);
267 		pci_enable_pcie_error_reporting(dev);
268 	}
269 
270 	/* get true return value from &status */
271 	retval = device_for_each_child(&dev->dev, &status, slot_reset_iter);
272 
273 	return status;
274 }
275 
resume_iter(struct device * device,void * data)276 static int resume_iter(struct device *device, void *data)
277 {
278 	struct pcie_device *pcie_device;
279 	struct pcie_port_service_driver *driver;
280 
281 	if (device->bus == &pcie_port_bus_type && device->driver) {
282 		driver = to_service_driver(device->driver);
283 		if (driver &&
284 			driver->err_handler &&
285 			driver->err_handler->resume) {
286 			pcie_device = to_pcie_device(device);
287 
288 			/* Forward error message to service drivers */
289 			driver->err_handler->resume(pcie_device->port);
290 		}
291 	}
292 
293 	return 0;
294 }
295 
pcie_portdrv_err_resume(struct pci_dev * dev)296 static void pcie_portdrv_err_resume(struct pci_dev *dev)
297 {
298 	int retval;
299 	/* nothing to do with error value, if it ever happens */
300 	retval = device_for_each_child(&dev->dev, NULL, resume_iter);
301 }
302 
303 /*
304  * LINUX Device Driver Model
305  */
306 static const struct pci_device_id port_pci_ids[] = { {
307 	/* handle any PCI-Express port */
308 	PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
309 	}, { /* end: all zeroes */ }
310 };
311 MODULE_DEVICE_TABLE(pci, port_pci_ids);
312 
313 static struct pci_error_handlers pcie_portdrv_err_handler = {
314 		.error_detected = pcie_portdrv_error_detected,
315 		.mmio_enabled = pcie_portdrv_mmio_enabled,
316 		.slot_reset = pcie_portdrv_slot_reset,
317 		.resume = pcie_portdrv_err_resume,
318 };
319 
320 static struct pci_driver pcie_portdriver = {
321 	.name		= "pcieport",
322 	.id_table	= &port_pci_ids[0],
323 
324 	.probe		= pcie_portdrv_probe,
325 	.remove		= pcie_portdrv_remove,
326 
327 	.err_handler 	= &pcie_portdrv_err_handler,
328 
329 	.driver.pm 	= PCIE_PORTDRV_PM_OPS,
330 };
331 
dmi_pcie_pme_disable_msi(const struct dmi_system_id * d)332 static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
333 {
334 	pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
335 			d->ident);
336 	pcie_pme_disable_msi();
337 	return 0;
338 }
339 
340 static struct dmi_system_id __initdata pcie_portdrv_dmi_table[] = {
341 	/*
342 	 * Boxes that should not use MSI for PCIe PME signaling.
343 	 */
344 	{
345 	 .callback = dmi_pcie_pme_disable_msi,
346 	 .ident = "MSI Wind U-100",
347 	 .matches = {
348 		     DMI_MATCH(DMI_SYS_VENDOR,
349 		     		"MICRO-STAR INTERNATIONAL CO., LTD"),
350 		     DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
351 		     },
352 	 },
353 	 {}
354 };
355 
pcie_portdrv_init(void)356 static int __init pcie_portdrv_init(void)
357 {
358 	int retval;
359 
360 	if (pcie_ports_disabled)
361 		return pci_register_driver(&pcie_portdriver);
362 
363 	dmi_check_system(pcie_portdrv_dmi_table);
364 
365 	retval = pcie_port_bus_register();
366 	if (retval) {
367 		printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
368 		goto out;
369 	}
370 	retval = pci_register_driver(&pcie_portdriver);
371 	if (retval)
372 		pcie_port_bus_unregister();
373  out:
374 	return retval;
375 }
376 
377 module_init(pcie_portdrv_init);
378