aerdrv.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/pm.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/pcieport_if.h>
  27. #include "aerdrv.h"
  28. #include "../../pci.h"
  29. /*
  30. * Version Information
  31. */
  32. #define DRIVER_VERSION "v1.0"
  33. #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
  34. #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
  35. MODULE_AUTHOR(DRIVER_AUTHOR);
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. static int __devinit aer_probe(struct pcie_device *dev);
  39. static void aer_remove(struct pcie_device *dev);
  40. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  41. enum pci_channel_state error);
  42. static void aer_error_resume(struct pci_dev *dev);
  43. static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
  44. static struct pci_error_handlers aer_error_handlers = {
  45. .error_detected = aer_error_detected,
  46. .resume = aer_error_resume,
  47. };
  48. static struct pcie_port_service_driver aerdriver = {
  49. .name = "aer",
  50. .port_type = PCI_EXP_TYPE_ROOT_PORT,
  51. .service = PCIE_PORT_SERVICE_AER,
  52. .probe = aer_probe,
  53. .remove = aer_remove,
  54. .err_handler = &aer_error_handlers,
  55. .reset_link = aer_root_reset,
  56. };
  57. static int pcie_aer_disable;
  58. void pci_no_aer(void)
  59. {
  60. pcie_aer_disable = 1; /* has priority over 'forceload' */
  61. }
  62. /**
  63. * aer_irq - Root Port's ISR
  64. * @irq: IRQ assigned to Root Port
  65. * @context: pointer to Root Port data structure
  66. *
  67. * Invoked when Root Port detects AER messages.
  68. **/
  69. irqreturn_t aer_irq(int irq, void *context)
  70. {
  71. unsigned int status, id;
  72. struct pcie_device *pdev = (struct pcie_device *)context;
  73. struct aer_rpc *rpc = get_service_data(pdev);
  74. int next_prod_idx;
  75. unsigned long flags;
  76. int pos;
  77. pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
  78. /*
  79. * Must lock access to Root Error Status Reg, Root Error ID Reg,
  80. * and Root error producer/consumer index
  81. */
  82. spin_lock_irqsave(&rpc->e_lock, flags);
  83. /* Read error status */
  84. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
  85. if (!(status & ROOT_ERR_STATUS_MASKS)) {
  86. spin_unlock_irqrestore(&rpc->e_lock, flags);
  87. return IRQ_NONE;
  88. }
  89. /* Read error source and clear error status */
  90. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
  91. pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
  92. /* Store error source for later DPC handler */
  93. next_prod_idx = rpc->prod_idx + 1;
  94. if (next_prod_idx == AER_ERROR_SOURCES_MAX)
  95. next_prod_idx = 0;
  96. if (next_prod_idx == rpc->cons_idx) {
  97. /*
  98. * Error Storm Condition - possibly the same error occurred.
  99. * Drop the error.
  100. */
  101. spin_unlock_irqrestore(&rpc->e_lock, flags);
  102. return IRQ_HANDLED;
  103. }
  104. rpc->e_sources[rpc->prod_idx].status = status;
  105. rpc->e_sources[rpc->prod_idx].id = id;
  106. rpc->prod_idx = next_prod_idx;
  107. spin_unlock_irqrestore(&rpc->e_lock, flags);
  108. /* Invoke DPC handler */
  109. schedule_work(&rpc->dpc_handler);
  110. return IRQ_HANDLED;
  111. }
  112. EXPORT_SYMBOL_GPL(aer_irq);
  113. /**
  114. * aer_alloc_rpc - allocate Root Port data structure
  115. * @dev: pointer to the pcie_dev data structure
  116. *
  117. * Invoked when Root Port's AER service is loaded.
  118. **/
  119. static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
  120. {
  121. struct aer_rpc *rpc;
  122. rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
  123. if (!rpc)
  124. return NULL;
  125. /*
  126. * Initialize Root lock access, e_lock, to Root Error Status Reg,
  127. * Root Error ID Reg, and Root error producer/consumer index.
  128. */
  129. spin_lock_init(&rpc->e_lock);
  130. rpc->rpd = dev;
  131. INIT_WORK(&rpc->dpc_handler, aer_isr);
  132. rpc->prod_idx = rpc->cons_idx = 0;
  133. mutex_init(&rpc->rpc_mutex);
  134. init_waitqueue_head(&rpc->wait_release);
  135. /* Use PCIe bus function to store rpc into PCIe device */
  136. set_service_data(dev, rpc);
  137. return rpc;
  138. }
  139. /**
  140. * aer_remove - clean up resources
  141. * @dev: pointer to the pcie_dev data structure
  142. *
  143. * Invoked when PCI Express bus unloads or AER probe fails.
  144. **/
  145. static void aer_remove(struct pcie_device *dev)
  146. {
  147. struct aer_rpc *rpc = get_service_data(dev);
  148. if (rpc) {
  149. /* If register interrupt service, it must be free. */
  150. if (rpc->isr)
  151. free_irq(dev->irq, dev);
  152. wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
  153. aer_delete_rootport(rpc);
  154. set_service_data(dev, NULL);
  155. }
  156. }
  157. /**
  158. * aer_probe - initialize resources
  159. * @dev: pointer to the pcie_dev data structure
  160. * @id: pointer to the service id data structure
  161. *
  162. * Invoked when PCI Express bus loads AER service driver.
  163. **/
  164. static int __devinit aer_probe(struct pcie_device *dev)
  165. {
  166. int status;
  167. struct aer_rpc *rpc;
  168. struct device *device = &dev->device;
  169. /* Init */
  170. status = aer_init(dev);
  171. if (status)
  172. return status;
  173. /* Alloc rpc data structure */
  174. rpc = aer_alloc_rpc(dev);
  175. if (!rpc) {
  176. dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
  177. aer_remove(dev);
  178. return -ENOMEM;
  179. }
  180. /* Request IRQ ISR */
  181. status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
  182. if (status) {
  183. dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
  184. aer_remove(dev);
  185. return status;
  186. }
  187. rpc->isr = 1;
  188. aer_enable_rootport(rpc);
  189. return status;
  190. }
  191. /**
  192. * aer_root_reset - reset link on Root Port
  193. * @dev: pointer to Root Port's pci_dev data structure
  194. *
  195. * Invoked by Port Bus driver when performing link reset at Root Port.
  196. **/
  197. static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
  198. {
  199. u16 p2p_ctrl;
  200. u32 status;
  201. int pos;
  202. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  203. /* Disable Root's interrupt in response to error messages */
  204. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
  205. /* Assert Secondary Bus Reset */
  206. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
  207. p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
  208. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
  209. /* De-assert Secondary Bus Reset */
  210. p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
  211. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
  212. /*
  213. * System software must wait for at least 100ms from the end
  214. * of a reset of one or more device before it is permitted
  215. * to issue Configuration Requests to those devices.
  216. */
  217. msleep(200);
  218. dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
  219. /* Enable Root Port's interrupt in response to error messages */
  220. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
  221. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
  222. pci_write_config_dword(dev,
  223. pos + PCI_ERR_ROOT_COMMAND,
  224. ROOT_PORT_INTR_ON_MESG_MASK);
  225. return PCI_ERS_RESULT_RECOVERED;
  226. }
  227. /**
  228. * aer_error_detected - update severity status
  229. * @dev: pointer to Root Port's pci_dev data structure
  230. * @error: error severity being notified by port bus
  231. *
  232. * Invoked by Port Bus driver during error recovery.
  233. **/
  234. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  235. enum pci_channel_state error)
  236. {
  237. /* Root Port has no impact. Always recovers. */
  238. return PCI_ERS_RESULT_CAN_RECOVER;
  239. }
  240. /**
  241. * aer_error_resume - clean up corresponding error status bits
  242. * @dev: pointer to Root Port's pci_dev data structure
  243. *
  244. * Invoked by Port Bus driver during nonfatal recovery.
  245. **/
  246. static void aer_error_resume(struct pci_dev *dev)
  247. {
  248. int pos;
  249. u32 status, mask;
  250. u16 reg16;
  251. /* Clean up Root device status */
  252. pos = pci_pcie_cap(dev);
  253. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
  254. pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
  255. /* Clean AER Root Error Status */
  256. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  257. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  258. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  259. if (dev->error_state == pci_channel_io_normal)
  260. status &= ~mask; /* Clear corresponding nonfatal bits */
  261. else
  262. status &= mask; /* Clear corresponding fatal bits */
  263. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  264. }
  265. /**
  266. * aer_service_init - register AER root service driver
  267. *
  268. * Invoked when AER root service driver is loaded.
  269. **/
  270. static int __init aer_service_init(void)
  271. {
  272. if (pcie_aer_disable)
  273. return -ENXIO;
  274. if (!pci_msi_enabled())
  275. return -ENXIO;
  276. return pcie_port_service_register(&aerdriver);
  277. }
  278. /**
  279. * aer_service_exit - unregister AER root service driver
  280. *
  281. * Invoked when AER root service driver is unloaded.
  282. **/
  283. static void __exit aer_service_exit(void)
  284. {
  285. pcie_port_service_unregister(&aerdriver);
  286. }
  287. module_init(aer_service_init);
  288. module_exit(aer_service_exit);