aerdrv.c 12 KB

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