aerdrv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. static int set_device_error_reporting(struct pci_dev *dev, void *data)
  64. {
  65. bool enable = *((bool *)data);
  66. if ((dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
  67. (dev->pcie_type == PCI_EXP_TYPE_UPSTREAM) ||
  68. (dev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)) {
  69. if (enable)
  70. pci_enable_pcie_error_reporting(dev);
  71. else
  72. pci_disable_pcie_error_reporting(dev);
  73. }
  74. if (enable)
  75. pcie_set_ecrc_checking(dev);
  76. return 0;
  77. }
  78. /**
  79. * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
  80. * @dev: pointer to root port's pci_dev data structure
  81. * @enable: true = enable error reporting, false = disable error reporting.
  82. */
  83. static void set_downstream_devices_error_reporting(struct pci_dev *dev,
  84. bool enable)
  85. {
  86. set_device_error_reporting(dev, &enable);
  87. if (!dev->subordinate)
  88. return;
  89. pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
  90. }
  91. /**
  92. * aer_enable_rootport - enable Root Port's interrupts when receiving messages
  93. * @rpc: pointer to a Root Port data structure
  94. *
  95. * Invoked when PCIe bus loads AER service driver.
  96. */
  97. static void aer_enable_rootport(struct aer_rpc *rpc)
  98. {
  99. struct pci_dev *pdev = rpc->rpd->port;
  100. int pos, aer_pos;
  101. u16 reg16;
  102. u32 reg32;
  103. pos = pci_pcie_cap(pdev);
  104. /* Clear PCIe Capability's Device Status */
  105. pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, &reg16);
  106. pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
  107. /* Disable system error generation in response to error messages */
  108. pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, &reg16);
  109. reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
  110. pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
  111. aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  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 = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
  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 = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
  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. init_waitqueue_head(&rpc->wait_release);
  223. /* Use PCIe bus function to store rpc into PCIe device */
  224. set_service_data(dev, rpc);
  225. return rpc;
  226. }
  227. /**
  228. * aer_remove - clean up resources
  229. * @dev: pointer to the pcie_dev data structure
  230. *
  231. * Invoked when PCI Express bus unloads or AER probe fails.
  232. */
  233. static void aer_remove(struct pcie_device *dev)
  234. {
  235. struct aer_rpc *rpc = get_service_data(dev);
  236. if (rpc) {
  237. /* If register interrupt service, it must be free. */
  238. if (rpc->isr)
  239. free_irq(dev->irq, dev);
  240. wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
  241. aer_disable_rootport(rpc);
  242. kfree(rpc);
  243. set_service_data(dev, NULL);
  244. }
  245. }
  246. /**
  247. * aer_probe - initialize resources
  248. * @dev: pointer to the pcie_dev data structure
  249. * @id: pointer to the service id data structure
  250. *
  251. * Invoked when PCI Express bus loads AER service driver.
  252. */
  253. static int __devinit aer_probe(struct pcie_device *dev)
  254. {
  255. int status;
  256. struct aer_rpc *rpc;
  257. struct device *device = &dev->device;
  258. /* Init */
  259. status = aer_init(dev);
  260. if (status)
  261. return status;
  262. /* Alloc rpc data structure */
  263. rpc = aer_alloc_rpc(dev);
  264. if (!rpc) {
  265. dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
  266. aer_remove(dev);
  267. return -ENOMEM;
  268. }
  269. /* Request IRQ ISR */
  270. status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
  271. if (status) {
  272. dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
  273. aer_remove(dev);
  274. return status;
  275. }
  276. rpc->isr = 1;
  277. aer_enable_rootport(rpc);
  278. return status;
  279. }
  280. /**
  281. * aer_root_reset - reset link on Root Port
  282. * @dev: pointer to Root Port's pci_dev data structure
  283. *
  284. * Invoked by Port Bus driver when performing link reset at Root Port.
  285. */
  286. static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
  287. {
  288. u32 reg32;
  289. int pos;
  290. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  291. /* Disable Root's interrupt in response to error messages */
  292. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  293. reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
  294. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  295. aer_do_secondary_bus_reset(dev);
  296. dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
  297. /* Clear Root Error Status */
  298. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
  299. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
  300. /* Enable Root Port's interrupt in response to error messages */
  301. pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
  302. reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
  303. pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
  304. return PCI_ERS_RESULT_RECOVERED;
  305. }
  306. /**
  307. * aer_error_detected - update severity status
  308. * @dev: pointer to Root Port's pci_dev data structure
  309. * @error: error severity being notified by port bus
  310. *
  311. * Invoked by Port Bus driver during error recovery.
  312. */
  313. static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
  314. enum pci_channel_state error)
  315. {
  316. /* Root Port has no impact. Always recovers. */
  317. return PCI_ERS_RESULT_CAN_RECOVER;
  318. }
  319. /**
  320. * aer_error_resume - clean up corresponding error status bits
  321. * @dev: pointer to Root Port's pci_dev data structure
  322. *
  323. * Invoked by Port Bus driver during nonfatal recovery.
  324. */
  325. static void aer_error_resume(struct pci_dev *dev)
  326. {
  327. int pos;
  328. u32 status, mask;
  329. u16 reg16;
  330. /* Clean up Root device status */
  331. pos = pci_pcie_cap(dev);
  332. pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
  333. pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
  334. /* Clean AER Root Error Status */
  335. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  336. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  337. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  338. if (dev->error_state == pci_channel_io_normal)
  339. status &= ~mask; /* Clear corresponding nonfatal bits */
  340. else
  341. status &= mask; /* Clear corresponding fatal bits */
  342. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  343. }
  344. /**
  345. * aer_service_init - register AER root service driver
  346. *
  347. * Invoked when AER root service driver is loaded.
  348. */
  349. static int __init aer_service_init(void)
  350. {
  351. if (pcie_aer_disable)
  352. return -ENXIO;
  353. if (!pci_msi_enabled())
  354. return -ENXIO;
  355. return pcie_port_service_register(&aerdriver);
  356. }
  357. /**
  358. * aer_service_exit - unregister AER root service driver
  359. *
  360. * Invoked when AER root service driver is unloaded.
  361. */
  362. static void __exit aer_service_exit(void)
  363. {
  364. pcie_port_service_unregister(&aerdriver);
  365. }
  366. module_init(aer_service_init);
  367. module_exit(aer_service_exit);