aerdrv.c 8.9 KB

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