eeh_driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /*
  259. * We don't remove the corresponding PE instances because
  260. * we need the information afterwords. The attached EEH
  261. * devices are expected to be attached soon when calling
  262. * into pcibios_add_pci_devices().
  263. */
  264. if (bus)
  265. __pcibios_remove_pci_devices(bus, 0);
  266. /* Reset the pci controller. (Asserts RST#; resets config space).
  267. * Reconfigure bridges and devices. Don't try to bring the system
  268. * up if the reset failed for some reason.
  269. */
  270. rc = eeh_reset_pe(pe);
  271. if (rc)
  272. return rc;
  273. /* Restore PE */
  274. eeh_ops->configure_bridge(pe);
  275. eeh_pe_restore_bars(pe);
  276. /* Give the system 5 seconds to finish running the user-space
  277. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  278. * this is a hack, but if we don't do this, and try to bring
  279. * the device up before the scripts have taken it down,
  280. * potentially weird things happen.
  281. */
  282. if (bus) {
  283. ssleep(5);
  284. pcibios_add_pci_devices(bus);
  285. }
  286. pe->freeze_count = cnt;
  287. return 0;
  288. }
  289. /* The longest amount of time to wait for a pci device
  290. * to come back on line, in seconds.
  291. */
  292. #define MAX_WAIT_FOR_RECOVERY 150
  293. /**
  294. * eeh_handle_event - Reset a PCI device after hard lockup.
  295. * @pe: EEH PE
  296. *
  297. * While PHB detects address or data parity errors on particular PCI
  298. * slot, the associated PE will be frozen. Besides, DMA's occurring
  299. * to wild addresses (which usually happen due to bugs in device
  300. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  301. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  302. *
  303. * Recovery process consists of unplugging the device driver (which
  304. * generated hotplug events to userspace), then issuing a PCI #RST to
  305. * the device, then reconfiguring the PCI config space for all bridges
  306. * & devices under this slot, and then finally restarting the device
  307. * drivers (which cause a second set of hotplug events to go out to
  308. * userspace).
  309. */
  310. void eeh_handle_event(struct eeh_pe *pe)
  311. {
  312. struct pci_bus *frozen_bus;
  313. int rc = 0;
  314. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  315. frozen_bus = eeh_pe_bus_get(pe);
  316. if (!frozen_bus) {
  317. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  318. __func__, pe->phb->global_number, pe->addr);
  319. return;
  320. }
  321. pe->freeze_count++;
  322. if (pe->freeze_count > EEH_MAX_ALLOWED_FREEZES)
  323. goto excess_failures;
  324. pr_warning("EEH: This PCI device has failed %d times in the last hour\n",
  325. pe->freeze_count);
  326. /* Walk the various device drivers attached to this slot through
  327. * a reset sequence, giving each an opportunity to do what it needs
  328. * to accomplish the reset. Each child gets a report of the
  329. * status ... if any child can't handle the reset, then the entire
  330. * slot is dlpar removed and added.
  331. */
  332. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  333. /* Get the current PCI slot state. This can take a long time,
  334. * sometimes over 3 seconds for certain systems.
  335. */
  336. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  337. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  338. printk(KERN_WARNING "EEH: Permanent failure\n");
  339. goto hard_fail;
  340. }
  341. /* Since rtas may enable MMIO when posting the error log,
  342. * don't post the error log until after all dev drivers
  343. * have been informed.
  344. */
  345. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  346. /* If all device drivers were EEH-unaware, then shut
  347. * down all of the device drivers, and hope they
  348. * go down willingly, without panicing the system.
  349. */
  350. if (result == PCI_ERS_RESULT_NONE) {
  351. rc = eeh_reset_device(pe, frozen_bus);
  352. if (rc) {
  353. printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
  354. goto hard_fail;
  355. }
  356. }
  357. /* If all devices reported they can proceed, then re-enable MMIO */
  358. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  359. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  360. if (rc < 0)
  361. goto hard_fail;
  362. if (rc) {
  363. result = PCI_ERS_RESULT_NEED_RESET;
  364. } else {
  365. result = PCI_ERS_RESULT_NONE;
  366. eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
  367. }
  368. }
  369. /* If all devices reported they can proceed, then re-enable DMA */
  370. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  371. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  372. if (rc < 0)
  373. goto hard_fail;
  374. if (rc)
  375. result = PCI_ERS_RESULT_NEED_RESET;
  376. else
  377. result = PCI_ERS_RESULT_RECOVERED;
  378. }
  379. /* If any device has a hard failure, then shut off everything. */
  380. if (result == PCI_ERS_RESULT_DISCONNECT) {
  381. printk(KERN_WARNING "EEH: Device driver gave up\n");
  382. goto hard_fail;
  383. }
  384. /* If any device called out for a reset, then reset the slot */
  385. if (result == PCI_ERS_RESULT_NEED_RESET) {
  386. rc = eeh_reset_device(pe, NULL);
  387. if (rc) {
  388. printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
  389. goto hard_fail;
  390. }
  391. result = PCI_ERS_RESULT_NONE;
  392. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  393. }
  394. /* All devices should claim they have recovered by now. */
  395. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  396. (result != PCI_ERS_RESULT_NONE)) {
  397. printk(KERN_WARNING "EEH: Not recovered\n");
  398. goto hard_fail;
  399. }
  400. /* Tell all device drivers that they can resume operations */
  401. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  402. return;
  403. excess_failures:
  404. /*
  405. * About 90% of all real-life EEH failures in the field
  406. * are due to poorly seated PCI cards. Only 10% or so are
  407. * due to actual, failed cards.
  408. */
  409. pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
  410. "last hour and has been permanently disabled.\n"
  411. "Please try reseating or replacing it.\n",
  412. pe->phb->global_number, pe->addr,
  413. pe->freeze_count);
  414. goto perm_error;
  415. hard_fail:
  416. pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
  417. "Please try reseating or replacing it\n",
  418. pe->phb->global_number, pe->addr);
  419. perm_error:
  420. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  421. /* Notify all devices that they're about to go down. */
  422. eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
  423. /* Shut down the device drivers for good. */
  424. if (frozen_bus)
  425. pcibios_remove_pci_devices(frozen_bus);
  426. }