portdrv_pci.c 9.0 KB

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