portdrv_pci.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/pm.h>
  13. #include <linux/pm_runtime.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. #include "portdrv.h"
  20. #include "aer/aerdrv.h"
  21. /*
  22. * Version Information
  23. */
  24. #define DRIVER_VERSION "v1.0"
  25. #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
  26. #define DRIVER_DESC "PCIe Port Bus Driver"
  27. MODULE_AUTHOR(DRIVER_AUTHOR);
  28. MODULE_DESCRIPTION(DRIVER_DESC);
  29. MODULE_LICENSE("GPL");
  30. /* If this switch is set, PCIe port native services should not be enabled. */
  31. bool pcie_ports_disabled;
  32. /*
  33. * If this switch is set, ACPI _OSC will be used to determine whether or not to
  34. * enable PCIe port native services.
  35. */
  36. bool pcie_ports_auto = true;
  37. static int __init pcie_port_setup(char *str)
  38. {
  39. if (!strncmp(str, "compat", 6)) {
  40. pcie_ports_disabled = true;
  41. } else if (!strncmp(str, "native", 6)) {
  42. pcie_ports_disabled = false;
  43. pcie_ports_auto = false;
  44. } else if (!strncmp(str, "auto", 4)) {
  45. pcie_ports_disabled = false;
  46. pcie_ports_auto = true;
  47. }
  48. return 1;
  49. }
  50. __setup("pcie_ports=", pcie_port_setup);
  51. /* global data */
  52. /**
  53. * pcie_clear_root_pme_status - Clear root port PME interrupt status.
  54. * @dev: PCIe root port or event collector.
  55. */
  56. void pcie_clear_root_pme_status(struct pci_dev *dev)
  57. {
  58. pcie_capability_set_dword(dev, PCI_EXP_RTSTA, PCI_EXP_RTSTA_PME);
  59. }
  60. static int pcie_portdrv_restore_config(struct pci_dev *dev)
  61. {
  62. int retval;
  63. retval = pci_enable_device(dev);
  64. if (retval)
  65. return retval;
  66. pci_set_master(dev);
  67. return 0;
  68. }
  69. #ifdef CONFIG_PM
  70. static int pcie_port_resume_noirq(struct device *dev)
  71. {
  72. struct pci_dev *pdev = to_pci_dev(dev);
  73. /*
  74. * Some BIOSes forget to clear Root PME Status bits after system wakeup
  75. * which breaks ACPI-based runtime wakeup on PCI Express, so clear those
  76. * bits now just in case (shouldn't hurt).
  77. */
  78. if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT)
  79. pcie_clear_root_pme_status(pdev);
  80. return 0;
  81. }
  82. static const struct dev_pm_ops pcie_portdrv_pm_ops = {
  83. .suspend = pcie_port_device_suspend,
  84. .resume = pcie_port_device_resume,
  85. .freeze = pcie_port_device_suspend,
  86. .thaw = pcie_port_device_resume,
  87. .poweroff = pcie_port_device_suspend,
  88. .restore = pcie_port_device_resume,
  89. .resume_noirq = pcie_port_resume_noirq,
  90. };
  91. #define PCIE_PORTDRV_PM_OPS (&pcie_portdrv_pm_ops)
  92. #else /* !PM */
  93. #define PCIE_PORTDRV_PM_OPS NULL
  94. #endif /* !PM */
  95. /*
  96. * pcie_portdrv_probe - Probe PCI-Express port devices
  97. * @dev: PCI-Express port device being probed
  98. *
  99. * If detected invokes the pcie_port_device_register() method for
  100. * this port device.
  101. *
  102. */
  103. static int pcie_portdrv_probe(struct pci_dev *dev,
  104. const struct pci_device_id *id)
  105. {
  106. int status;
  107. if (!pci_is_pcie(dev) ||
  108. ((pci_pcie_type(dev) != PCI_EXP_TYPE_ROOT_PORT) &&
  109. (pci_pcie_type(dev) != PCI_EXP_TYPE_UPSTREAM) &&
  110. (pci_pcie_type(dev) != PCI_EXP_TYPE_DOWNSTREAM)))
  111. return -ENODEV;
  112. status = pcie_port_device_register(dev);
  113. if (status)
  114. return status;
  115. pci_save_state(dev);
  116. /*
  117. * D3cold may not work properly on some PCIe port, so disable
  118. * it by default.
  119. */
  120. dev->d3cold_allowed = false;
  121. return 0;
  122. }
  123. static void pcie_portdrv_remove(struct pci_dev *dev)
  124. {
  125. pcie_port_device_remove(dev);
  126. }
  127. static int error_detected_iter(struct device *device, void *data)
  128. {
  129. struct pcie_device *pcie_device;
  130. struct pcie_port_service_driver *driver;
  131. struct aer_broadcast_data *result_data;
  132. pci_ers_result_t status;
  133. result_data = (struct aer_broadcast_data *) data;
  134. if (device->bus == &pcie_port_bus_type && device->driver) {
  135. driver = to_service_driver(device->driver);
  136. if (!driver ||
  137. !driver->err_handler ||
  138. !driver->err_handler->error_detected)
  139. return 0;
  140. pcie_device = to_pcie_device(device);
  141. /* Forward error detected message to service drivers */
  142. status = driver->err_handler->error_detected(
  143. pcie_device->port,
  144. result_data->state);
  145. result_data->result =
  146. merge_result(result_data->result, status);
  147. }
  148. return 0;
  149. }
  150. static pci_ers_result_t pcie_portdrv_error_detected(struct pci_dev *dev,
  151. enum pci_channel_state error)
  152. {
  153. struct aer_broadcast_data data = {error, PCI_ERS_RESULT_CAN_RECOVER};
  154. /* get true return value from &data */
  155. device_for_each_child(&dev->dev, &data, error_detected_iter);
  156. return data.result;
  157. }
  158. static int mmio_enabled_iter(struct device *device, void *data)
  159. {
  160. struct pcie_device *pcie_device;
  161. struct pcie_port_service_driver *driver;
  162. pci_ers_result_t status, *result;
  163. result = (pci_ers_result_t *) data;
  164. if (device->bus == &pcie_port_bus_type && device->driver) {
  165. driver = to_service_driver(device->driver);
  166. if (driver &&
  167. driver->err_handler &&
  168. driver->err_handler->mmio_enabled) {
  169. pcie_device = to_pcie_device(device);
  170. /* Forward error message to service drivers */
  171. status = driver->err_handler->mmio_enabled(
  172. pcie_device->port);
  173. *result = merge_result(*result, status);
  174. }
  175. }
  176. return 0;
  177. }
  178. static pci_ers_result_t pcie_portdrv_mmio_enabled(struct pci_dev *dev)
  179. {
  180. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  181. /* get true return value from &status */
  182. device_for_each_child(&dev->dev, &status, mmio_enabled_iter);
  183. return status;
  184. }
  185. static int slot_reset_iter(struct device *device, void *data)
  186. {
  187. struct pcie_device *pcie_device;
  188. struct pcie_port_service_driver *driver;
  189. pci_ers_result_t status, *result;
  190. result = (pci_ers_result_t *) data;
  191. if (device->bus == &pcie_port_bus_type && device->driver) {
  192. driver = to_service_driver(device->driver);
  193. if (driver &&
  194. driver->err_handler &&
  195. driver->err_handler->slot_reset) {
  196. pcie_device = to_pcie_device(device);
  197. /* Forward error message to service drivers */
  198. status = driver->err_handler->slot_reset(
  199. pcie_device->port);
  200. *result = merge_result(*result, status);
  201. }
  202. }
  203. return 0;
  204. }
  205. static pci_ers_result_t pcie_portdrv_slot_reset(struct pci_dev *dev)
  206. {
  207. pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
  208. /* If fatal, restore cfg space for possible link reset at upstream */
  209. if (dev->error_state == pci_channel_io_frozen) {
  210. dev->state_saved = true;
  211. pci_restore_state(dev);
  212. pcie_portdrv_restore_config(dev);
  213. pci_enable_pcie_error_reporting(dev);
  214. }
  215. /* get true return value from &status */
  216. device_for_each_child(&dev->dev, &status, slot_reset_iter);
  217. return status;
  218. }
  219. static int resume_iter(struct device *device, void *data)
  220. {
  221. struct pcie_device *pcie_device;
  222. struct pcie_port_service_driver *driver;
  223. if (device->bus == &pcie_port_bus_type && device->driver) {
  224. driver = to_service_driver(device->driver);
  225. if (driver &&
  226. driver->err_handler &&
  227. driver->err_handler->resume) {
  228. pcie_device = to_pcie_device(device);
  229. /* Forward error message to service drivers */
  230. driver->err_handler->resume(pcie_device->port);
  231. }
  232. }
  233. return 0;
  234. }
  235. static void pcie_portdrv_err_resume(struct pci_dev *dev)
  236. {
  237. device_for_each_child(&dev->dev, NULL, resume_iter);
  238. }
  239. /*
  240. * LINUX Device Driver Model
  241. */
  242. static const struct pci_device_id port_pci_ids[] = { {
  243. /* handle any PCI-Express port */
  244. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
  245. }, { /* end: all zeroes */ }
  246. };
  247. MODULE_DEVICE_TABLE(pci, port_pci_ids);
  248. static const struct pci_error_handlers pcie_portdrv_err_handler = {
  249. .error_detected = pcie_portdrv_error_detected,
  250. .mmio_enabled = pcie_portdrv_mmio_enabled,
  251. .slot_reset = pcie_portdrv_slot_reset,
  252. .resume = pcie_portdrv_err_resume,
  253. };
  254. static struct pci_driver pcie_portdriver = {
  255. .name = "pcieport",
  256. .id_table = &port_pci_ids[0],
  257. .probe = pcie_portdrv_probe,
  258. .remove = pcie_portdrv_remove,
  259. .err_handler = &pcie_portdrv_err_handler,
  260. .driver.pm = PCIE_PORTDRV_PM_OPS,
  261. };
  262. static int __init dmi_pcie_pme_disable_msi(const struct dmi_system_id *d)
  263. {
  264. pr_notice("%s detected: will not use MSI for PCIe PME signaling\n",
  265. d->ident);
  266. pcie_pme_disable_msi();
  267. return 0;
  268. }
  269. static struct dmi_system_id __initdata pcie_portdrv_dmi_table[] = {
  270. /*
  271. * Boxes that should not use MSI for PCIe PME signaling.
  272. */
  273. {
  274. .callback = dmi_pcie_pme_disable_msi,
  275. .ident = "MSI Wind U-100",
  276. .matches = {
  277. DMI_MATCH(DMI_SYS_VENDOR,
  278. "MICRO-STAR INTERNATIONAL CO., LTD"),
  279. DMI_MATCH(DMI_PRODUCT_NAME, "U-100"),
  280. },
  281. },
  282. {}
  283. };
  284. static int __init pcie_portdrv_init(void)
  285. {
  286. int retval;
  287. if (pcie_ports_disabled)
  288. return pci_register_driver(&pcie_portdriver);
  289. dmi_check_system(pcie_portdrv_dmi_table);
  290. retval = pcie_port_bus_register();
  291. if (retval) {
  292. printk(KERN_WARNING "PCIE: bus_register error: %d\n", retval);
  293. goto out;
  294. }
  295. retval = pci_register_driver(&pcie_portdriver);
  296. if (retval)
  297. pcie_port_bus_unregister();
  298. out:
  299. return retval;
  300. }
  301. module_init(pcie_portdrv_init);