aerdrv.c 9.1 KB

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