aerdrv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/pci.h>
  18. #include <linux/pci-acpi.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. static int aer_probe(struct pcie_device *dev);
  37. static void aer_remove(struct pcie_device *dev);
  38. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  39. enum pci_channel_state error);
  40. static void aer_error_resume(struct pci_dev *dev);
  41. static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
  42. static const struct pci_error_handlers aer_error_handlers = {
  43. .error_detected = aer_error_detected,
  44. .resume = aer_error_resume,
  45. };
  46. static struct pcie_port_service_driver aerdriver = {
  47. .name = "aer",
  48. .port_type = PCI_EXP_TYPE_ROOT_PORT,
  49. .service = PCIE_PORT_SERVICE_AER,
  50. .probe = aer_probe,
  51. .remove = aer_remove,
  52. .err_handler = &aer_error_handlers,
  53. .reset_link = aer_root_reset,
  54. };
  55. static int pcie_aer_disable;
  56. void pci_no_aer(void)
  57. {
  58. pcie_aer_disable = 1;
  59. }
  60. bool pci_aer_available(void)
  61. {
  62. return !pcie_aer_disable && pci_msi_enabled();
  63. }
  64. static int set_device_error_reporting(struct pci_dev *dev, void *data)
  65. {
  66. bool enable = *((bool *)data);
  67. int type = pci_pcie_type(dev);
  68. if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
  69. (type == PCI_EXP_TYPE_UPSTREAM) ||
  70. (type == PCI_EXP_TYPE_DOWNSTREAM)) {
  71. if (enable)
  72. pci_enable_pcie_error_reporting(dev);
  73. else
  74. pci_disable_pcie_error_reporting(dev);
  75. }
  76. if (enable)
  77. pcie_set_ecrc_checking(dev);
  78. return 0;
  79. }
  80. /**
  81. * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
  82. * @dev: pointer to root port's pci_dev data structure
  83. * @enable: true = enable error reporting, false = disable error reporting.
  84. */
  85. static void set_downstream_devices_error_reporting(struct pci_dev *dev,
  86. bool enable)
  87. {
  88. set_device_error_reporting(dev, &enable);
  89. if (!dev->subordinate)
  90. return;
  91. pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
  92. }
  93. /**
  94. * aer_enable_rootport - enable Root Port's interrupts when receiving messages
  95. * @rpc: pointer to a Root Port data structure
  96. *
  97. * Invoked when PCIe bus loads AER service driver.
  98. */
  99. static void aer_enable_rootport(struct aer_rpc *rpc)
  100. {
  101. struct pci_dev *pdev = rpc->rpd->port;
  102. int aer_pos;
  103. u16 reg16;
  104. u32 reg32;
  105. /* Clear PCIe Capability's Device Status */
  106. pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
  107. pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
  108. /* Disable system error generation in response to error messages */
  109. pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
  110. SYSTEM_ERROR_INTR_ON_MESG_MASK);
  111. aer_pos = pdev->aer_cap;
  112. /* Clear error status */
  113. pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
  114. pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
  115. pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
  116. pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
  117. pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
  118. pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
  119. /*
  120. * Enable error reporting for the root port device and downstream port
  121. * devices.
  122. */
  123. set_downstream_devices_error_reporting(pdev, true);
  124. /* Enable Root Port's interrupt in response to error messages */
  125. pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
  126. reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
  127. pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
  128. }
  129. /**
  130. * aer_disable_rootport - disable Root Port's interrupts when receiving messages
  131. * @rpc: pointer to a Root Port data structure
  132. *
  133. * Invoked when PCIe bus unloads AER service driver.
  134. */
  135. static void aer_disable_rootport(struct aer_rpc *rpc)
  136. {
  137. struct pci_dev *pdev = rpc->rpd->port;
  138. u32 reg32;
  139. int pos;
  140. /*
  141. * Disable error reporting for the root port device and downstream port
  142. * devices.
  143. */
  144. set_downstream_devices_error_reporting(pdev, false);
  145. pos = pdev->aer_cap;
  146. /* Disable Root's interrupt in response to error messages */
  147. pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  148. reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
  149. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  150. /* Clear Root's error status reg */
  151. pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  152. pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
  153. }
  154. /**
  155. * aer_irq - Root Port's ISR
  156. * @irq: IRQ assigned to Root Port
  157. * @context: pointer to Root Port data structure
  158. *
  159. * Invoked when Root Port detects AER messages.
  160. */
  161. irqreturn_t aer_irq(int irq, void *context)
  162. {
  163. unsigned int status, id;
  164. struct pcie_device *pdev = (struct pcie_device *)context;
  165. struct aer_rpc *rpc = get_service_data(pdev);
  166. int next_prod_idx;
  167. unsigned long flags;
  168. int pos;
  169. pos = pdev->port->aer_cap;
  170. /*
  171. * Must lock access to Root Error Status Reg, Root Error ID Reg,
  172. * and Root error producer/consumer index
  173. */
  174. spin_lock_irqsave(&rpc->e_lock, flags);
  175. /* Read error status */
  176. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
  177. if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
  178. spin_unlock_irqrestore(&rpc->e_lock, flags);
  179. return IRQ_NONE;
  180. }
  181. /* Read error source and clear error status */
  182. pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
  183. pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
  184. /* Store error source for later DPC handler */
  185. next_prod_idx = rpc->prod_idx + 1;
  186. if (next_prod_idx == AER_ERROR_SOURCES_MAX)
  187. next_prod_idx = 0;
  188. if (next_prod_idx == rpc->cons_idx) {
  189. /*
  190. * Error Storm Condition - possibly the same error occurred.
  191. * Drop the error.
  192. */
  193. spin_unlock_irqrestore(&rpc->e_lock, flags);
  194. return IRQ_HANDLED;
  195. }
  196. rpc->e_sources[rpc->prod_idx].status = status;
  197. rpc->e_sources[rpc->prod_idx].id = id;
  198. rpc->prod_idx = next_prod_idx;
  199. spin_unlock_irqrestore(&rpc->e_lock, flags);
  200. /* Invoke DPC handler */
  201. schedule_work(&rpc->dpc_handler);
  202. return IRQ_HANDLED;
  203. }
  204. EXPORT_SYMBOL_GPL(aer_irq);
  205. /**
  206. * aer_alloc_rpc - allocate Root Port data structure
  207. * @dev: pointer to the pcie_dev data structure
  208. *
  209. * Invoked when Root Port's AER service is loaded.
  210. */
  211. static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
  212. {
  213. struct aer_rpc *rpc;
  214. rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
  215. if (!rpc)
  216. return NULL;
  217. /* Initialize Root lock access, e_lock, to Root Error Status Reg */
  218. spin_lock_init(&rpc->e_lock);
  219. rpc->rpd = dev;
  220. INIT_WORK(&rpc->dpc_handler, aer_isr);
  221. mutex_init(&rpc->rpc_mutex);
  222. /* Use PCIe bus function to store rpc into PCIe device */
  223. set_service_data(dev, rpc);
  224. return rpc;
  225. }
  226. /**
  227. * aer_remove - clean up resources
  228. * @dev: pointer to the pcie_dev data structure
  229. *
  230. * Invoked when PCI Express bus unloads or AER probe fails.
  231. */
  232. static void aer_remove(struct pcie_device *dev)
  233. {
  234. struct aer_rpc *rpc = get_service_data(dev);
  235. if (rpc) {
  236. /* If register interrupt service, it must be free. */
  237. if (rpc->isr)
  238. free_irq(dev->irq, dev);
  239. flush_work(&rpc->dpc_handler);
  240. aer_disable_rootport(rpc);
  241. kfree(rpc);
  242. set_service_data(dev, NULL);
  243. }
  244. }
  245. /**
  246. * aer_probe - initialize resources
  247. * @dev: pointer to the pcie_dev data structure
  248. *
  249. * Invoked when PCI Express bus loads AER service driver.
  250. */
  251. static int aer_probe(struct pcie_device *dev)
  252. {
  253. int status;
  254. struct aer_rpc *rpc;
  255. struct device *device = &dev->device;
  256. /* Alloc rpc data structure */
  257. rpc = aer_alloc_rpc(dev);
  258. if (!rpc) {
  259. dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
  260. aer_remove(dev);
  261. return -ENOMEM;
  262. }
  263. /* Request IRQ ISR */
  264. status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
  265. if (status) {
  266. dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
  267. aer_remove(dev);
  268. return status;
  269. }
  270. rpc->isr = 1;
  271. aer_enable_rootport(rpc);
  272. return status;
  273. }
  274. /**
  275. * aer_root_reset - reset link on Root Port
  276. * @dev: pointer to Root Port's pci_dev data structure
  277. *
  278. * Invoked by Port Bus driver when performing link reset at Root Port.
  279. */
  280. static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
  281. {
  282. u32 reg32;
  283. int pos;
  284. pos = dev->aer_cap;
  285. /* Disable Root's interrupt in response to error messages */
  286. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  287. reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
  288. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  289. pci_reset_bridge_secondary_bus(dev);
  290. dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
  291. /* Clear Root Error Status */
  292. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  293. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
  294. /* Enable Root Port's interrupt in response to error messages */
  295. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  296. reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
  297. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  298. return PCI_ERS_RESULT_RECOVERED;
  299. }
  300. /**
  301. * aer_error_detected - update severity status
  302. * @dev: pointer to Root Port's pci_dev data structure
  303. * @error: error severity being notified by port bus
  304. *
  305. * Invoked by Port Bus driver during error recovery.
  306. */
  307. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  308. enum pci_channel_state error)
  309. {
  310. /* Root Port has no impact. Always recovers. */
  311. return PCI_ERS_RESULT_CAN_RECOVER;
  312. }
  313. /**
  314. * aer_error_resume - clean up corresponding error status bits
  315. * @dev: pointer to Root Port's pci_dev data structure
  316. *
  317. * Invoked by Port Bus driver during nonfatal recovery.
  318. */
  319. static void aer_error_resume(struct pci_dev *dev)
  320. {
  321. int pos;
  322. u32 status, mask;
  323. u16 reg16;
  324. /* Clean up Root device status */
  325. pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &reg16);
  326. pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
  327. /* Clean AER Root Error Status */
  328. pos = dev->aer_cap;
  329. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  330. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  331. if (dev->error_state == pci_channel_io_normal)
  332. status &= ~mask; /* Clear corresponding nonfatal bits */
  333. else
  334. status &= mask; /* Clear corresponding fatal bits */
  335. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  336. }
  337. /**
  338. * aer_service_init - register AER root service driver
  339. *
  340. * Invoked when AER root service driver is loaded.
  341. */
  342. static int __init aer_service_init(void)
  343. {
  344. if (!pci_aer_available() || aer_acpi_firmware_first())
  345. return -ENXIO;
  346. return pcie_port_service_register(&aerdriver);
  347. }
  348. device_initcall(aer_service_init);