aerdrv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * drivers/pci/pcie/aer/aerdrv.c
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * This file implements the AER root port service driver. The driver will
  9. * register an irq handler. When root port triggers an AER interrupt, the irq
  10. * handler will collect root port status and schedule a work.
  11. *
  12. * Copyright (C) 2006 Intel Corp.
  13. * Tom Long Nguyen (tom.l.nguyen@intel.com)
  14. * Zhang Yanmin (yanmin.zhang@intel.com)
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/pci-acpi.h>
  20. #include <linux/sched.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/pm.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/pcieport_if.h>
  28. #include <linux/slab.h>
  29. #include "aerdrv.h"
  30. #include "../../pci.h"
  31. /*
  32. * Version Information
  33. */
  34. #define DRIVER_VERSION "v1.0"
  35. #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
  36. #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
  37. MODULE_AUTHOR(DRIVER_AUTHOR);
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. static int aer_probe(struct pcie_device *dev);
  41. static void aer_remove(struct pcie_device *dev);
  42. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  43. enum pci_channel_state error);
  44. static void aer_error_resume(struct pci_dev *dev);
  45. static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
  46. static const struct pci_error_handlers aer_error_handlers = {
  47. .error_detected = aer_error_detected,
  48. .resume = aer_error_resume,
  49. };
  50. static struct pcie_port_service_driver aerdriver = {
  51. .name = "aer",
  52. .port_type = PCI_EXP_TYPE_ROOT_PORT,
  53. .service = PCIE_PORT_SERVICE_AER,
  54. .probe = aer_probe,
  55. .remove = aer_remove,
  56. .err_handler = &aer_error_handlers,
  57. .reset_link = aer_root_reset,
  58. };
  59. static int pcie_aer_disable;
  60. void pci_no_aer(void)
  61. {
  62. pcie_aer_disable = 1; /* has priority over 'forceload' */
  63. }
  64. bool pci_aer_available(void)
  65. {
  66. return !pcie_aer_disable && pci_msi_enabled();
  67. }
  68. static int set_device_error_reporting(struct pci_dev *dev, void *data)
  69. {
  70. bool enable = *((bool *)data);
  71. int type = pci_pcie_type(dev);
  72. if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
  73. (type == PCI_EXP_TYPE_UPSTREAM) ||
  74. (type == PCI_EXP_TYPE_DOWNSTREAM)) {
  75. if (enable)
  76. pci_enable_pcie_error_reporting(dev);
  77. else
  78. pci_disable_pcie_error_reporting(dev);
  79. }
  80. if (enable)
  81. pcie_set_ecrc_checking(dev);
  82. return 0;
  83. }
  84. /**
  85. * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
  86. * @dev: pointer to root port's pci_dev data structure
  87. * @enable: true = enable error reporting, false = disable error reporting.
  88. */
  89. static void set_downstream_devices_error_reporting(struct pci_dev *dev,
  90. bool enable)
  91. {
  92. set_device_error_reporting(dev, &enable);
  93. if (!dev->subordinate)
  94. return;
  95. pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
  96. }
  97. /**
  98. * aer_enable_rootport - enable Root Port's interrupts when receiving messages
  99. * @rpc: pointer to a Root Port data structure
  100. *
  101. * Invoked when PCIe bus loads AER service driver.
  102. */
  103. static void aer_enable_rootport(struct aer_rpc *rpc)
  104. {
  105. struct pci_dev *pdev = rpc->rpd->port;
  106. int aer_pos;
  107. u16 reg16;
  108. u32 reg32;
  109. /* Clear PCIe Capability's Device Status */
  110. pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
  111. pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
  112. /* Disable system error generation in response to error messages */
  113. pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
  114. SYSTEM_ERROR_INTR_ON_MESG_MASK);
  115. aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  116. /* Clear error status */
  117. pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
  118. pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
  119. pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
  120. pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
  121. pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
  122. pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
  123. /*
  124. * Enable error reporting for the root port device and downstream port
  125. * devices.
  126. */
  127. set_downstream_devices_error_reporting(pdev, true);
  128. /* Enable Root Port's interrupt in response to error messages */
  129. pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
  130. reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
  131. pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
  132. }
  133. /**
  134. * aer_disable_rootport - disable Root Port's interrupts when receiving messages
  135. * @rpc: pointer to a Root Port data structure
  136. *
  137. * Invoked when PCIe bus unloads AER service driver.
  138. */
  139. static void aer_disable_rootport(struct aer_rpc *rpc)
  140. {
  141. struct pci_dev *pdev = rpc->rpd->port;
  142. u32 reg32;
  143. int pos;
  144. /*
  145. * Disable error reporting for the root port device and downstream port
  146. * devices.
  147. */
  148. set_downstream_devices_error_reporting(pdev, false);
  149. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  150. /* Disable Root's interrupt in response to error messages */
  151. pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  152. reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
  153. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  154. /* Clear Root's error status reg */
  155. pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  156. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
  157. }
  158. /**
  159. * aer_irq - Root Port's ISR
  160. * @irq: IRQ assigned to Root Port
  161. * @context: pointer to Root Port data structure
  162. *
  163. * Invoked when Root Port detects AER messages.
  164. */
  165. irqreturn_t aer_irq(int irq, void *context)
  166. {
  167. unsigned int status, id;
  168. struct pcie_device *pdev = (struct pcie_device *)context;
  169. struct aer_rpc *rpc = get_service_data(pdev);
  170. int next_prod_idx;
  171. unsigned long flags;
  172. int pos;
  173. pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
  174. /*
  175. * Must lock access to Root Error Status Reg, Root Error ID Reg,
  176. * and Root error producer/consumer index
  177. */
  178. spin_lock_irqsave(&rpc->e_lock, flags);
  179. /* Read error status */
  180. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
  181. if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
  182. spin_unlock_irqrestore(&rpc->e_lock, flags);
  183. return IRQ_NONE;
  184. }
  185. /* Read error source and clear error status */
  186. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
  187. pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
  188. /* Store error source for later DPC handler */
  189. next_prod_idx = rpc->prod_idx + 1;
  190. if (next_prod_idx == AER_ERROR_SOURCES_MAX)
  191. next_prod_idx = 0;
  192. if (next_prod_idx == rpc->cons_idx) {
  193. /*
  194. * Error Storm Condition - possibly the same error occurred.
  195. * Drop the error.
  196. */
  197. spin_unlock_irqrestore(&rpc->e_lock, flags);
  198. return IRQ_HANDLED;
  199. }
  200. rpc->e_sources[rpc->prod_idx].status = status;
  201. rpc->e_sources[rpc->prod_idx].id = id;
  202. rpc->prod_idx = next_prod_idx;
  203. spin_unlock_irqrestore(&rpc->e_lock, flags);
  204. /* Invoke DPC handler */
  205. schedule_work(&rpc->dpc_handler);
  206. return IRQ_HANDLED;
  207. }
  208. EXPORT_SYMBOL_GPL(aer_irq);
  209. /**
  210. * aer_alloc_rpc - allocate Root Port data structure
  211. * @dev: pointer to the pcie_dev data structure
  212. *
  213. * Invoked when Root Port's AER service is loaded.
  214. */
  215. static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
  216. {
  217. struct aer_rpc *rpc;
  218. rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
  219. if (!rpc)
  220. return NULL;
  221. /* Initialize Root lock access, e_lock, to Root Error Status Reg */
  222. spin_lock_init(&rpc->e_lock);
  223. rpc->rpd = dev;
  224. INIT_WORK(&rpc->dpc_handler, aer_isr);
  225. mutex_init(&rpc->rpc_mutex);
  226. /* Use PCIe bus function to store rpc into PCIe device */
  227. set_service_data(dev, rpc);
  228. return rpc;
  229. }
  230. /**
  231. * aer_remove - clean up resources
  232. * @dev: pointer to the pcie_dev data structure
  233. *
  234. * Invoked when PCI Express bus unloads or AER probe fails.
  235. */
  236. static void aer_remove(struct pcie_device *dev)
  237. {
  238. struct aer_rpc *rpc = get_service_data(dev);
  239. if (rpc) {
  240. /* If register interrupt service, it must be free. */
  241. if (rpc->isr)
  242. free_irq(dev->irq, dev);
  243. flush_work(&rpc->dpc_handler);
  244. aer_disable_rootport(rpc);
  245. kfree(rpc);
  246. set_service_data(dev, NULL);
  247. }
  248. }
  249. /**
  250. * aer_probe - initialize resources
  251. * @dev: pointer to the pcie_dev data structure
  252. * @id: pointer to the service id data structure
  253. *
  254. * Invoked when PCI Express bus loads AER service driver.
  255. */
  256. static int aer_probe(struct pcie_device *dev)
  257. {
  258. int status;
  259. struct aer_rpc *rpc;
  260. struct device *device = &dev->device;
  261. /* Init */
  262. status = aer_init(dev);
  263. if (status)
  264. return status;
  265. /* Alloc rpc data structure */
  266. rpc = aer_alloc_rpc(dev);
  267. if (!rpc) {
  268. dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
  269. aer_remove(dev);
  270. return -ENOMEM;
  271. }
  272. /* Request IRQ ISR */
  273. status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
  274. if (status) {
  275. dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
  276. aer_remove(dev);
  277. return status;
  278. }
  279. rpc->isr = 1;
  280. aer_enable_rootport(rpc);
  281. return status;
  282. }
  283. /**
  284. * aer_root_reset - reset link on Root Port
  285. * @dev: pointer to Root Port's pci_dev data structure
  286. *
  287. * Invoked by Port Bus driver when performing link reset at Root Port.
  288. */
  289. static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
  290. {
  291. u32 reg32;
  292. int pos;
  293. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  294. /* Disable Root's interrupt in response to error messages */
  295. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  296. reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
  297. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  298. pci_reset_bridge_secondary_bus(dev);
  299. dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
  300. /* Clear Root Error Status */
  301. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  302. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
  303. /* Enable Root Port's interrupt in response to error messages */
  304. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  305. reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
  306. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  307. return PCI_ERS_RESULT_RECOVERED;
  308. }
  309. /**
  310. * aer_error_detected - update severity status
  311. * @dev: pointer to Root Port's pci_dev data structure
  312. * @error: error severity being notified by port bus
  313. *
  314. * Invoked by Port Bus driver during error recovery.
  315. */
  316. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  317. enum pci_channel_state error)
  318. {
  319. /* Root Port has no impact. Always recovers. */
  320. return PCI_ERS_RESULT_CAN_RECOVER;
  321. }
  322. /**
  323. * aer_error_resume - clean up corresponding error status bits
  324. * @dev: pointer to Root Port's pci_dev data structure
  325. *
  326. * Invoked by Port Bus driver during nonfatal recovery.
  327. */
  328. static void aer_error_resume(struct pci_dev *dev)
  329. {
  330. int pos;
  331. u32 status, mask;
  332. u16 reg16;
  333. /* Clean up Root device status */
  334. pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &reg16);
  335. pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
  336. /* Clean AER Root Error Status */
  337. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  338. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  339. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  340. if (dev->error_state == pci_channel_io_normal)
  341. status &= ~mask; /* Clear corresponding nonfatal bits */
  342. else
  343. status &= mask; /* Clear corresponding fatal bits */
  344. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  345. }
  346. /**
  347. * aer_service_init - register AER root service driver
  348. *
  349. * Invoked when AER root service driver is loaded.
  350. */
  351. static int __init aer_service_init(void)
  352. {
  353. if (!pci_aer_available() || aer_acpi_firmware_first())
  354. return -ENXIO;
  355. return pcie_port_service_register(&aerdriver);
  356. }
  357. /**
  358. * aer_service_exit - unregister AER root service driver
  359. *
  360. * Invoked when AER root service driver is unloaded.
  361. */
  362. static void __exit aer_service_exit(void)
  363. {
  364. pcie_port_service_unregister(&aerdriver);
  365. }
  366. module_init(aer_service_init);
  367. module_exit(aer_service_exit);