eeh_driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
  3. * Copyright IBM Corp. 2004 2005
  4. * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/pci.h>
  29. #include <asm/eeh.h>
  30. #include <asm/eeh_event.h>
  31. #include <asm/ppc-pci.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. /**
  36. * eeh_pcid_name - Retrieve name of PCI device driver
  37. * @pdev: PCI device
  38. *
  39. * This routine is used to retrieve the name of PCI device driver
  40. * if that's valid.
  41. */
  42. static inline const char *eeh_pcid_name(struct pci_dev *pdev)
  43. {
  44. if (pdev && pdev->dev.driver)
  45. return pdev->dev.driver->name;
  46. return "";
  47. }
  48. #if 0
  49. static void print_device_node_tree(struct pci_dn *pdn, int dent)
  50. {
  51. int i;
  52. struct device_node *pc;
  53. if (!pdn)
  54. return;
  55. for (i = 0; i < dent; i++)
  56. printk(" ");
  57. printk("dn=%s mode=%x \tcfg_addr=%x pe_addr=%x \tfull=%s\n",
  58. pdn->node->name, pdn->eeh_mode, pdn->eeh_config_addr,
  59. pdn->eeh_pe_config_addr, pdn->node->full_name);
  60. dent += 3;
  61. pc = pdn->node->child;
  62. while (pc) {
  63. print_device_node_tree(PCI_DN(pc), dent);
  64. pc = pc->sibling;
  65. }
  66. }
  67. #endif
  68. /**
  69. * eeh_disable_irq - Disable interrupt for the recovering device
  70. * @dev: PCI device
  71. *
  72. * This routine must be called when reporting temporary or permanent
  73. * error to the particular PCI device to disable interrupt of that
  74. * device. If the device has enabled MSI or MSI-X interrupt, we needn't
  75. * do real work because EEH should freeze DMA transfers for those PCI
  76. * devices encountering EEH errors, which includes MSI or MSI-X.
  77. */
  78. static void eeh_disable_irq(struct pci_dev *dev)
  79. {
  80. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  81. /* Don't disable MSI and MSI-X interrupts. They are
  82. * effectively disabled by the DMA Stopped state
  83. * when an EEH error occurs.
  84. */
  85. if (dev->msi_enabled || dev->msix_enabled)
  86. return;
  87. if (!irq_has_action(dev->irq))
  88. return;
  89. edev->mode |= EEH_DEV_IRQ_DISABLED;
  90. disable_irq_nosync(dev->irq);
  91. }
  92. /**
  93. * eeh_enable_irq - Enable interrupt for the recovering device
  94. * @dev: PCI device
  95. *
  96. * This routine must be called to enable interrupt while failed
  97. * device could be resumed.
  98. */
  99. static void eeh_enable_irq(struct pci_dev *dev)
  100. {
  101. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  102. if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
  103. edev->mode &= ~EEH_DEV_IRQ_DISABLED;
  104. enable_irq(dev->irq);
  105. }
  106. }
  107. /**
  108. * eeh_report_error - Report pci error to each device driver
  109. * @data: eeh device
  110. * @userdata: return value
  111. *
  112. * Report an EEH error to each device driver, collect up and
  113. * merge the device driver responses. Cumulative response
  114. * passed back in "userdata".
  115. */
  116. static void *eeh_report_error(void *data, void *userdata)
  117. {
  118. struct eeh_dev *edev = (struct eeh_dev *)data;
  119. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  120. enum pci_ers_result rc, *res = userdata;
  121. struct pci_driver *driver = dev->driver;
  122. /* We might not have the associated PCI device,
  123. * then we should continue for next one.
  124. */
  125. if (!dev) return NULL;
  126. dev->error_state = pci_channel_io_frozen;
  127. if (!driver)
  128. return NULL;
  129. eeh_disable_irq(dev);
  130. if (!driver->err_handler ||
  131. !driver->err_handler->error_detected)
  132. return NULL;
  133. rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
  134. /* A driver that needs a reset trumps all others */
  135. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  136. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  137. return NULL;
  138. }
  139. /**
  140. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  141. * @data: eeh device
  142. * @userdata: return value
  143. *
  144. * Tells each device driver that IO ports, MMIO and config space I/O
  145. * are now enabled. Collects up and merges the device driver responses.
  146. * Cumulative response passed back in "userdata".
  147. */
  148. static void *eeh_report_mmio_enabled(void *data, void *userdata)
  149. {
  150. struct eeh_dev *edev = (struct eeh_dev *)data;
  151. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  152. enum pci_ers_result rc, *res = userdata;
  153. struct pci_driver *driver;
  154. if (!dev) return NULL;
  155. if (!(driver = dev->driver) ||
  156. !driver->err_handler ||
  157. !driver->err_handler->mmio_enabled)
  158. return NULL;
  159. rc = driver->err_handler->mmio_enabled(dev);
  160. /* A driver that needs a reset trumps all others */
  161. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  162. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  163. return NULL;
  164. }
  165. /**
  166. * eeh_report_reset - Tell device that slot has been reset
  167. * @data: eeh device
  168. * @userdata: return value
  169. *
  170. * This routine must be called while EEH tries to reset particular
  171. * PCI device so that the associated PCI device driver could take
  172. * some actions, usually to save data the driver needs so that the
  173. * driver can work again while the device is recovered.
  174. */
  175. static void *eeh_report_reset(void *data, void *userdata)
  176. {
  177. struct eeh_dev *edev = (struct eeh_dev *)data;
  178. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  179. enum pci_ers_result rc, *res = userdata;
  180. struct pci_driver *driver;
  181. if (!dev || !(driver = dev->driver))
  182. return NULL;
  183. dev->error_state = pci_channel_io_normal;
  184. eeh_enable_irq(dev);
  185. if (!driver->err_handler ||
  186. !driver->err_handler->slot_reset)
  187. return NULL;
  188. rc = driver->err_handler->slot_reset(dev);
  189. if ((*res == PCI_ERS_RESULT_NONE) ||
  190. (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
  191. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  192. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  193. return NULL;
  194. }
  195. /**
  196. * eeh_report_resume - Tell device to resume normal operations
  197. * @data: eeh device
  198. * @userdata: return value
  199. *
  200. * This routine must be called to notify the device driver that it
  201. * could resume so that the device driver can do some initialization
  202. * to make the recovered device work again.
  203. */
  204. static void *eeh_report_resume(void *data, void *userdata)
  205. {
  206. struct eeh_dev *edev = (struct eeh_dev *)data;
  207. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  208. struct pci_driver *driver;
  209. if (!dev) return NULL;
  210. dev->error_state = pci_channel_io_normal;
  211. if (!(driver = dev->driver))
  212. return NULL;
  213. eeh_enable_irq(dev);
  214. if (!driver->err_handler ||
  215. !driver->err_handler->resume)
  216. return NULL;
  217. driver->err_handler->resume(dev);
  218. return NULL;
  219. }
  220. /**
  221. * eeh_report_failure - Tell device driver that device is dead.
  222. * @data: eeh device
  223. * @userdata: return value
  224. *
  225. * This informs the device driver that the device is permanently
  226. * dead, and that no further recovery attempts will be made on it.
  227. */
  228. static void *eeh_report_failure(void *data, void *userdata)
  229. {
  230. struct eeh_dev *edev = (struct eeh_dev *)data;
  231. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  232. struct pci_driver *driver;
  233. if (!dev) return NULL;
  234. dev->error_state = pci_channel_io_perm_failure;
  235. if (!(driver = dev->driver))
  236. return NULL;
  237. eeh_disable_irq(dev);
  238. if (!driver->err_handler ||
  239. !driver->err_handler->error_detected)
  240. return NULL;
  241. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  242. return NULL;
  243. }
  244. /**
  245. * eeh_reset_device - Perform actual reset of a pci slot
  246. * @pe: EEH PE
  247. * @bus: PCI bus corresponding to the isolcated slot
  248. *
  249. * This routine must be called to do reset on the indicated PE.
  250. * During the reset, udev might be invoked because those affected
  251. * PCI devices will be removed and then added.
  252. */
  253. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus)
  254. {
  255. int cnt, rc;
  256. /* pcibios will clear the counter; save the value */
  257. cnt = pe->freeze_count;
  258. if (bus)
  259. pcibios_remove_pci_devices(bus);
  260. /* Reset the pci controller. (Asserts RST#; resets config space).
  261. * Reconfigure bridges and devices. Don't try to bring the system
  262. * up if the reset failed for some reason.
  263. */
  264. rc = eeh_reset_pe(pe);
  265. if (rc)
  266. return rc;
  267. /* Restore PE */
  268. eeh_ops->configure_bridge(pe);
  269. eeh_pe_restore_bars(pe);
  270. /* Give the system 5 seconds to finish running the user-space
  271. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  272. * this is a hack, but if we don't do this, and try to bring
  273. * the device up before the scripts have taken it down,
  274. * potentially weird things happen.
  275. */
  276. if (bus) {
  277. ssleep(5);
  278. pcibios_add_pci_devices(bus);
  279. }
  280. pe->freeze_count = cnt;
  281. return 0;
  282. }
  283. /* The longest amount of time to wait for a pci device
  284. * to come back on line, in seconds.
  285. */
  286. #define MAX_WAIT_FOR_RECOVERY 150
  287. /**
  288. * eeh_handle_event - Reset a PCI device after hard lockup.
  289. * @pe: EEH PE
  290. *
  291. * While PHB detects address or data parity errors on particular PCI
  292. * slot, the associated PE will be frozen. Besides, DMA's occurring
  293. * to wild addresses (which usually happen due to bugs in device
  294. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  295. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  296. *
  297. * Recovery process consists of unplugging the device driver (which
  298. * generated hotplug events to userspace), then issuing a PCI #RST to
  299. * the device, then reconfiguring the PCI config space for all bridges
  300. * & devices under this slot, and then finally restarting the device
  301. * drivers (which cause a second set of hotplug events to go out to
  302. * userspace).
  303. */
  304. void eeh_handle_event(struct eeh_pe *pe)
  305. {
  306. struct pci_bus *frozen_bus;
  307. int rc = 0;
  308. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  309. frozen_bus = eeh_pe_bus_get(pe);
  310. if (!frozen_bus) {
  311. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  312. __func__, pe->phb->global_number, pe->addr);
  313. return;
  314. }
  315. pe->freeze_count++;
  316. if (pe->freeze_count > EEH_MAX_ALLOWED_FREEZES)
  317. goto excess_failures;
  318. pr_warning("EEH: This PCI device has failed %d times in the last hour\n",
  319. pe->freeze_count);
  320. /* Walk the various device drivers attached to this slot through
  321. * a reset sequence, giving each an opportunity to do what it needs
  322. * to accomplish the reset. Each child gets a report of the
  323. * status ... if any child can't handle the reset, then the entire
  324. * slot is dlpar removed and added.
  325. */
  326. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  327. /* Get the current PCI slot state. This can take a long time,
  328. * sometimes over 3 seconds for certain systems.
  329. */
  330. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  331. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  332. printk(KERN_WARNING "EEH: Permanent failure\n");
  333. goto hard_fail;
  334. }
  335. /* Since rtas may enable MMIO when posting the error log,
  336. * don't post the error log until after all dev drivers
  337. * have been informed.
  338. */
  339. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  340. /* If all device drivers were EEH-unaware, then shut
  341. * down all of the device drivers, and hope they
  342. * go down willingly, without panicing the system.
  343. */
  344. if (result == PCI_ERS_RESULT_NONE) {
  345. rc = eeh_reset_device(pe, frozen_bus);
  346. if (rc) {
  347. printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
  348. goto hard_fail;
  349. }
  350. }
  351. /* If all devices reported they can proceed, then re-enable MMIO */
  352. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  353. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  354. if (rc < 0)
  355. goto hard_fail;
  356. if (rc) {
  357. result = PCI_ERS_RESULT_NEED_RESET;
  358. } else {
  359. result = PCI_ERS_RESULT_NONE;
  360. eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
  361. }
  362. }
  363. /* If all devices reported they can proceed, then re-enable DMA */
  364. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  365. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  366. if (rc < 0)
  367. goto hard_fail;
  368. if (rc)
  369. result = PCI_ERS_RESULT_NEED_RESET;
  370. else
  371. result = PCI_ERS_RESULT_RECOVERED;
  372. }
  373. /* If any device has a hard failure, then shut off everything. */
  374. if (result == PCI_ERS_RESULT_DISCONNECT) {
  375. printk(KERN_WARNING "EEH: Device driver gave up\n");
  376. goto hard_fail;
  377. }
  378. /* If any device called out for a reset, then reset the slot */
  379. if (result == PCI_ERS_RESULT_NEED_RESET) {
  380. rc = eeh_reset_device(pe, NULL);
  381. if (rc) {
  382. printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
  383. goto hard_fail;
  384. }
  385. result = PCI_ERS_RESULT_NONE;
  386. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  387. }
  388. /* All devices should claim they have recovered by now. */
  389. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  390. (result != PCI_ERS_RESULT_NONE)) {
  391. printk(KERN_WARNING "EEH: Not recovered\n");
  392. goto hard_fail;
  393. }
  394. /* Tell all device drivers that they can resume operations */
  395. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  396. return;
  397. excess_failures:
  398. /*
  399. * About 90% of all real-life EEH failures in the field
  400. * are due to poorly seated PCI cards. Only 10% or so are
  401. * due to actual, failed cards.
  402. */
  403. pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
  404. "last hour and has been permanently disabled.\n"
  405. "Please try reseating or replacing it.\n",
  406. pe->phb->global_number, pe->addr,
  407. pe->freeze_count);
  408. goto perm_error;
  409. hard_fail:
  410. pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
  411. "Please try reseating or replacing it\n",
  412. pe->phb->global_number, pe->addr);
  413. perm_error:
  414. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  415. /* Notify all devices that they're about to go down. */
  416. eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
  417. /* Shut down the device drivers for good. */
  418. if (frozen_bus)
  419. pcibios_remove_pci_devices(frozen_bus);
  420. }