aerdrv.c 9.2 KB

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