eeh_driver.c 15 KB

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