aerdrv.c 8.7 KB

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