eeh_driver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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_MODE_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_MODE_IRQ_DISABLED) {
  103. edev->mode &= ~EEH_MODE_IRQ_DISABLED;
  104. enable_irq(dev->irq);
  105. }
  106. }
  107. /**
  108. * eeh_report_error - Report pci error to each device driver
  109. * @dev: PCI 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 int eeh_report_error(struct pci_dev *dev, void *userdata)
  117. {
  118. enum pci_ers_result rc, *res = userdata;
  119. struct pci_driver *driver = dev->driver;
  120. dev->error_state = pci_channel_io_frozen;
  121. if (!driver)
  122. return 0;
  123. eeh_disable_irq(dev);
  124. if (!driver->err_handler ||
  125. !driver->err_handler->error_detected)
  126. return 0;
  127. rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
  128. /* A driver that needs a reset trumps all others */
  129. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  130. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  131. return 0;
  132. }
  133. /**
  134. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  135. * @dev: PCI device
  136. * @userdata: return value
  137. *
  138. * Tells each device driver that IO ports, MMIO and config space I/O
  139. * are now enabled. Collects up and merges the device driver responses.
  140. * Cumulative response passed back in "userdata".
  141. */
  142. static int eeh_report_mmio_enabled(struct pci_dev *dev, void *userdata)
  143. {
  144. enum pci_ers_result rc, *res = userdata;
  145. struct pci_driver *driver = dev->driver;
  146. if (!driver ||
  147. !driver->err_handler ||
  148. !driver->err_handler->mmio_enabled)
  149. return 0;
  150. rc = driver->err_handler->mmio_enabled(dev);
  151. /* A driver that needs a reset trumps all others */
  152. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  153. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  154. return 0;
  155. }
  156. /**
  157. * eeh_report_reset - Tell device that slot has been reset
  158. * @dev: PCI device
  159. * @userdata: return value
  160. *
  161. * This routine must be called while EEH tries to reset particular
  162. * PCI device so that the associated PCI device driver could take
  163. * some actions, usually to save data the driver needs so that the
  164. * driver can work again while the device is recovered.
  165. */
  166. static int eeh_report_reset(struct pci_dev *dev, void *userdata)
  167. {
  168. enum pci_ers_result rc, *res = userdata;
  169. struct pci_driver *driver = dev->driver;
  170. if (!driver)
  171. return 0;
  172. dev->error_state = pci_channel_io_normal;
  173. eeh_enable_irq(dev);
  174. if (!driver->err_handler ||
  175. !driver->err_handler->slot_reset)
  176. return 0;
  177. rc = driver->err_handler->slot_reset(dev);
  178. if ((*res == PCI_ERS_RESULT_NONE) ||
  179. (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
  180. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  181. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  182. return 0;
  183. }
  184. /**
  185. * eeh_report_resume - Tell device to resume normal operations
  186. * @dev: PCI device
  187. * @userdata: return value
  188. *
  189. * This routine must be called to notify the device driver that it
  190. * could resume so that the device driver can do some initialization
  191. * to make the recovered device work again.
  192. */
  193. static int eeh_report_resume(struct pci_dev *dev, void *userdata)
  194. {
  195. struct pci_driver *driver = dev->driver;
  196. dev->error_state = pci_channel_io_normal;
  197. if (!driver)
  198. return 0;
  199. eeh_enable_irq(dev);
  200. if (!driver->err_handler ||
  201. !driver->err_handler->resume)
  202. return 0;
  203. driver->err_handler->resume(dev);
  204. return 0;
  205. }
  206. /**
  207. * eeh_report_failure - Tell device driver that device is dead.
  208. * @dev: PCI device
  209. * @userdata: return value
  210. *
  211. * This informs the device driver that the device is permanently
  212. * dead, and that no further recovery attempts will be made on it.
  213. */
  214. static int eeh_report_failure(struct pci_dev *dev, void *userdata)
  215. {
  216. struct pci_driver *driver = dev->driver;
  217. dev->error_state = pci_channel_io_perm_failure;
  218. if (!driver)
  219. return 0;
  220. eeh_disable_irq(dev);
  221. if (!driver->err_handler ||
  222. !driver->err_handler->error_detected)
  223. return 0;
  224. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  225. return 0;
  226. }
  227. /**
  228. * eeh_reset_device - Perform actual reset of a pci slot
  229. * @edev: PE associated EEH device
  230. * @bus: PCI bus corresponding to the isolcated slot
  231. *
  232. * This routine must be called to do reset on the indicated PE.
  233. * During the reset, udev might be invoked because those affected
  234. * PCI devices will be removed and then added.
  235. */
  236. static int eeh_reset_device(struct eeh_dev *edev, struct pci_bus *bus)
  237. {
  238. struct device_node *dn;
  239. int cnt, rc;
  240. /* pcibios will clear the counter; save the value */
  241. cnt = edev->freeze_count;
  242. if (bus)
  243. pcibios_remove_pci_devices(bus);
  244. /* Reset the pci controller. (Asserts RST#; resets config space).
  245. * Reconfigure bridges and devices. Don't try to bring the system
  246. * up if the reset failed for some reason.
  247. */
  248. rc = eeh_reset_pe(edev);
  249. if (rc)
  250. return rc;
  251. /* Walk over all functions on this device. */
  252. dn = eeh_dev_to_of_node(edev);
  253. if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
  254. dn = dn->parent->child;
  255. while (dn) {
  256. struct eeh_dev *pedev = of_node_to_eeh_dev(dn);
  257. /* On Power4, always true because eeh_pe_config_addr=0 */
  258. if (edev->pe_config_addr == pedev->pe_config_addr) {
  259. eeh_ops->configure_bridge(dn);
  260. eeh_restore_bars(pedev);
  261. }
  262. dn = dn->sibling;
  263. }
  264. /* Give the system 5 seconds to finish running the user-space
  265. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  266. * this is a hack, but if we don't do this, and try to bring
  267. * the device up before the scripts have taken it down,
  268. * potentially weird things happen.
  269. */
  270. if (bus) {
  271. ssleep(5);
  272. pcibios_add_pci_devices(bus);
  273. }
  274. edev->freeze_count = cnt;
  275. return 0;
  276. }
  277. /* The longest amount of time to wait for a pci device
  278. * to come back on line, in seconds.
  279. */
  280. #define MAX_WAIT_FOR_RECOVERY 150
  281. /**
  282. * eeh_handle_event - Reset a PCI device after hard lockup.
  283. * @event: EEH event
  284. *
  285. * While PHB detects address or data parity errors on particular PCI
  286. * slot, the associated PE will be frozen. Besides, DMA's occurring
  287. * to wild addresses (which usually happen due to bugs in device
  288. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  289. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  290. *
  291. * Recovery process consists of unplugging the device driver (which
  292. * generated hotplug events to userspace), then issuing a PCI #RST to
  293. * the device, then reconfiguring the PCI config space for all bridges
  294. * & devices under this slot, and then finally restarting the device
  295. * drivers (which cause a second set of hotplug events to go out to
  296. * userspace).
  297. */
  298. struct eeh_dev *handle_eeh_events(struct eeh_event *event)
  299. {
  300. struct device_node *frozen_dn;
  301. struct eeh_dev *frozen_edev;
  302. struct pci_bus *frozen_bus;
  303. int rc = 0;
  304. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  305. const char *location, *pci_str, *drv_str, *bus_pci_str, *bus_drv_str;
  306. frozen_dn = eeh_find_device_pe(eeh_dev_to_of_node(event->edev));
  307. if (!frozen_dn) {
  308. location = of_get_property(eeh_dev_to_of_node(event->edev), "ibm,loc-code", NULL);
  309. location = location ? location : "unknown";
  310. printk(KERN_ERR "EEH: Error: Cannot find partition endpoint "
  311. "for location=%s pci addr=%s\n",
  312. location, eeh_pci_name(eeh_dev_to_pci_dev(event->edev)));
  313. return NULL;
  314. }
  315. frozen_bus = pcibios_find_pci_bus(frozen_dn);
  316. location = of_get_property(frozen_dn, "ibm,loc-code", NULL);
  317. location = location ? location : "unknown";
  318. /* There are two different styles for coming up with the PE.
  319. * In the old style, it was the highest EEH-capable device
  320. * which was always an EADS pci bridge. In the new style,
  321. * there might not be any EADS bridges, and even when there are,
  322. * the firmware marks them as "EEH incapable". So another
  323. * two-step is needed to find the pci bus..
  324. */
  325. if (!frozen_bus)
  326. frozen_bus = pcibios_find_pci_bus(frozen_dn->parent);
  327. if (!frozen_bus) {
  328. printk(KERN_ERR "EEH: Cannot find PCI bus "
  329. "for location=%s dn=%s\n",
  330. location, frozen_dn->full_name);
  331. return NULL;
  332. }
  333. frozen_edev = of_node_to_eeh_dev(frozen_dn);
  334. frozen_edev->freeze_count++;
  335. pci_str = eeh_pci_name(eeh_dev_to_pci_dev(event->edev));
  336. drv_str = eeh_pcid_name(eeh_dev_to_pci_dev(event->edev));
  337. if (frozen_edev->freeze_count > EEH_MAX_ALLOWED_FREEZES)
  338. goto excess_failures;
  339. printk(KERN_WARNING
  340. "EEH: This PCI device has failed %d times in the last hour:\n",
  341. frozen_edev->freeze_count);
  342. if (frozen_edev->pdev) {
  343. bus_pci_str = pci_name(frozen_edev->pdev);
  344. bus_drv_str = eeh_pcid_name(frozen_edev->pdev);
  345. printk(KERN_WARNING
  346. "EEH: Bus location=%s driver=%s pci addr=%s\n",
  347. location, bus_drv_str, bus_pci_str);
  348. }
  349. printk(KERN_WARNING
  350. "EEH: Device location=%s driver=%s pci addr=%s\n",
  351. location, drv_str, pci_str);
  352. /* Walk the various device drivers attached to this slot through
  353. * a reset sequence, giving each an opportunity to do what it needs
  354. * to accomplish the reset. Each child gets a report of the
  355. * status ... if any child can't handle the reset, then the entire
  356. * slot is dlpar removed and added.
  357. */
  358. pci_walk_bus(frozen_bus, eeh_report_error, &result);
  359. /* Get the current PCI slot state. This can take a long time,
  360. * sometimes over 3 seconds for certain systems.
  361. */
  362. rc = eeh_ops->wait_state(eeh_dev_to_of_node(frozen_edev), MAX_WAIT_FOR_RECOVERY*1000);
  363. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  364. printk(KERN_WARNING "EEH: Permanent failure\n");
  365. goto hard_fail;
  366. }
  367. /* Since rtas may enable MMIO when posting the error log,
  368. * don't post the error log until after all dev drivers
  369. * have been informed.
  370. */
  371. eeh_slot_error_detail(frozen_edev, EEH_LOG_TEMP);
  372. /* If all device drivers were EEH-unaware, then shut
  373. * down all of the device drivers, and hope they
  374. * go down willingly, without panicing the system.
  375. */
  376. if (result == PCI_ERS_RESULT_NONE) {
  377. rc = eeh_reset_device(frozen_edev, frozen_bus);
  378. if (rc) {
  379. printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
  380. goto hard_fail;
  381. }
  382. }
  383. /* If all devices reported they can proceed, then re-enable MMIO */
  384. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  385. rc = eeh_pci_enable(frozen_edev, EEH_OPT_THAW_MMIO);
  386. if (rc < 0)
  387. goto hard_fail;
  388. if (rc) {
  389. result = PCI_ERS_RESULT_NEED_RESET;
  390. } else {
  391. result = PCI_ERS_RESULT_NONE;
  392. pci_walk_bus(frozen_bus, eeh_report_mmio_enabled, &result);
  393. }
  394. }
  395. /* If all devices reported they can proceed, then re-enable DMA */
  396. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  397. rc = eeh_pci_enable(frozen_edev, EEH_OPT_THAW_DMA);
  398. if (rc < 0)
  399. goto hard_fail;
  400. if (rc)
  401. result = PCI_ERS_RESULT_NEED_RESET;
  402. else
  403. result = PCI_ERS_RESULT_RECOVERED;
  404. }
  405. /* If any device has a hard failure, then shut off everything. */
  406. if (result == PCI_ERS_RESULT_DISCONNECT) {
  407. printk(KERN_WARNING "EEH: Device driver gave up\n");
  408. goto hard_fail;
  409. }
  410. /* If any device called out for a reset, then reset the slot */
  411. if (result == PCI_ERS_RESULT_NEED_RESET) {
  412. rc = eeh_reset_device(frozen_edev, NULL);
  413. if (rc) {
  414. printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
  415. goto hard_fail;
  416. }
  417. result = PCI_ERS_RESULT_NONE;
  418. pci_walk_bus(frozen_bus, eeh_report_reset, &result);
  419. }
  420. /* All devices should claim they have recovered by now. */
  421. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  422. (result != PCI_ERS_RESULT_NONE)) {
  423. printk(KERN_WARNING "EEH: Not recovered\n");
  424. goto hard_fail;
  425. }
  426. /* Tell all device drivers that they can resume operations */
  427. pci_walk_bus(frozen_bus, eeh_report_resume, NULL);
  428. return frozen_edev;
  429. excess_failures:
  430. /*
  431. * About 90% of all real-life EEH failures in the field
  432. * are due to poorly seated PCI cards. Only 10% or so are
  433. * due to actual, failed cards.
  434. */
  435. printk(KERN_ERR
  436. "EEH: PCI device at location=%s driver=%s pci addr=%s\n"
  437. "has failed %d times in the last hour "
  438. "and has been permanently disabled.\n"
  439. "Please try reseating this device or replacing it.\n",
  440. location, drv_str, pci_str, frozen_edev->freeze_count);
  441. goto perm_error;
  442. hard_fail:
  443. printk(KERN_ERR
  444. "EEH: Unable to recover from failure of PCI device "
  445. "at location=%s driver=%s pci addr=%s\n"
  446. "Please try reseating this device or replacing it.\n",
  447. location, drv_str, pci_str);
  448. perm_error:
  449. eeh_slot_error_detail(frozen_edev, EEH_LOG_PERM);
  450. /* Notify all devices that they're about to go down. */
  451. pci_walk_bus(frozen_bus, eeh_report_failure, NULL);
  452. /* Shut down the device drivers for good. */
  453. pcibios_remove_pci_devices(frozen_bus);
  454. return NULL;
  455. }