eeh_driver.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. /**
  80. * eeh_disable_irq - Disable interrupt for the recovering device
  81. * @dev: PCI device
  82. *
  83. * This routine must be called when reporting temporary or permanent
  84. * error to the particular PCI device to disable interrupt of that
  85. * device. If the device has enabled MSI or MSI-X interrupt, we needn't
  86. * do real work because EEH should freeze DMA transfers for those PCI
  87. * devices encountering EEH errors, which includes MSI or MSI-X.
  88. */
  89. static void eeh_disable_irq(struct pci_dev *dev)
  90. {
  91. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  92. /* Don't disable MSI and MSI-X interrupts. They are
  93. * effectively disabled by the DMA Stopped state
  94. * when an EEH error occurs.
  95. */
  96. if (dev->msi_enabled || dev->msix_enabled)
  97. return;
  98. if (!irq_has_action(dev->irq))
  99. return;
  100. edev->mode |= EEH_DEV_IRQ_DISABLED;
  101. disable_irq_nosync(dev->irq);
  102. }
  103. /**
  104. * eeh_enable_irq - Enable interrupt for the recovering device
  105. * @dev: PCI device
  106. *
  107. * This routine must be called to enable interrupt while failed
  108. * device could be resumed.
  109. */
  110. static void eeh_enable_irq(struct pci_dev *dev)
  111. {
  112. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  113. if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
  114. edev->mode &= ~EEH_DEV_IRQ_DISABLED;
  115. /*
  116. * FIXME !!!!!
  117. *
  118. * This is just ass backwards. This maze has
  119. * unbalanced irq_enable/disable calls. So instead of
  120. * finding the root cause it works around the warning
  121. * in the irq_enable code by conditionally calling
  122. * into it.
  123. *
  124. * That's just wrong.The warning in the core code is
  125. * there to tell people to fix their assymetries in
  126. * their own code, not by abusing the core information
  127. * to avoid it.
  128. *
  129. * I so wish that the assymetry would be the other way
  130. * round and a few more irq_disable calls render that
  131. * shit unusable forever.
  132. *
  133. * tglx
  134. */
  135. if (irqd_irq_disabled(irq_get_irq_data(dev->irq)))
  136. enable_irq(dev->irq);
  137. }
  138. }
  139. static bool eeh_dev_removed(struct eeh_dev *edev)
  140. {
  141. /* EEH device removed ? */
  142. if (!edev || (edev->mode & EEH_DEV_REMOVED))
  143. return true;
  144. return false;
  145. }
  146. static void *eeh_dev_save_state(void *data, void *userdata)
  147. {
  148. struct eeh_dev *edev = data;
  149. struct pci_dev *pdev;
  150. if (!edev)
  151. return NULL;
  152. pdev = eeh_dev_to_pci_dev(edev);
  153. if (!pdev)
  154. return NULL;
  155. pci_save_state(pdev);
  156. return NULL;
  157. }
  158. /**
  159. * eeh_report_error - Report pci error to each device driver
  160. * @data: eeh device
  161. * @userdata: return value
  162. *
  163. * Report an EEH error to each device driver, collect up and
  164. * merge the device driver responses. Cumulative response
  165. * passed back in "userdata".
  166. */
  167. static void *eeh_report_error(void *data, void *userdata)
  168. {
  169. struct eeh_dev *edev = (struct eeh_dev *)data;
  170. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  171. enum pci_ers_result rc, *res = userdata;
  172. struct pci_driver *driver;
  173. if (!dev || eeh_dev_removed(edev))
  174. return NULL;
  175. dev->error_state = pci_channel_io_frozen;
  176. driver = eeh_pcid_get(dev);
  177. if (!driver) return NULL;
  178. eeh_disable_irq(dev);
  179. if (!driver->err_handler ||
  180. !driver->err_handler->error_detected) {
  181. eeh_pcid_put(dev);
  182. return NULL;
  183. }
  184. rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
  185. /* A driver that needs a reset trumps all others */
  186. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  187. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  188. eeh_pcid_put(dev);
  189. return NULL;
  190. }
  191. /**
  192. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  193. * @data: eeh device
  194. * @userdata: return value
  195. *
  196. * Tells each device driver that IO ports, MMIO and config space I/O
  197. * are now enabled. Collects up and merges the device driver responses.
  198. * Cumulative response passed back in "userdata".
  199. */
  200. static void *eeh_report_mmio_enabled(void *data, void *userdata)
  201. {
  202. struct eeh_dev *edev = (struct eeh_dev *)data;
  203. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  204. enum pci_ers_result rc, *res = userdata;
  205. struct pci_driver *driver;
  206. if (!dev || eeh_dev_removed(edev))
  207. return NULL;
  208. driver = eeh_pcid_get(dev);
  209. if (!driver) return NULL;
  210. if (!driver->err_handler ||
  211. !driver->err_handler->mmio_enabled ||
  212. (edev->mode & EEH_DEV_NO_HANDLER)) {
  213. eeh_pcid_put(dev);
  214. return NULL;
  215. }
  216. rc = driver->err_handler->mmio_enabled(dev);
  217. /* A driver that needs a reset trumps all others */
  218. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  219. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  220. eeh_pcid_put(dev);
  221. return NULL;
  222. }
  223. /**
  224. * eeh_report_reset - Tell device that slot has been reset
  225. * @data: eeh device
  226. * @userdata: return value
  227. *
  228. * This routine must be called while EEH tries to reset particular
  229. * PCI device so that the associated PCI device driver could take
  230. * some actions, usually to save data the driver needs so that the
  231. * driver can work again while the device is recovered.
  232. */
  233. static void *eeh_report_reset(void *data, void *userdata)
  234. {
  235. struct eeh_dev *edev = (struct eeh_dev *)data;
  236. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  237. enum pci_ers_result rc, *res = userdata;
  238. struct pci_driver *driver;
  239. if (!dev || eeh_dev_removed(edev))
  240. return NULL;
  241. dev->error_state = pci_channel_io_normal;
  242. driver = eeh_pcid_get(dev);
  243. if (!driver) return NULL;
  244. eeh_enable_irq(dev);
  245. if (!driver->err_handler ||
  246. !driver->err_handler->slot_reset ||
  247. (edev->mode & EEH_DEV_NO_HANDLER)) {
  248. eeh_pcid_put(dev);
  249. return NULL;
  250. }
  251. rc = driver->err_handler->slot_reset(dev);
  252. if ((*res == PCI_ERS_RESULT_NONE) ||
  253. (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
  254. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  255. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  256. eeh_pcid_put(dev);
  257. return NULL;
  258. }
  259. static void *eeh_dev_restore_state(void *data, void *userdata)
  260. {
  261. struct eeh_dev *edev = data;
  262. struct pci_dev *pdev;
  263. if (!edev)
  264. return NULL;
  265. pdev = eeh_dev_to_pci_dev(edev);
  266. if (!pdev)
  267. return NULL;
  268. pci_restore_state(pdev);
  269. return NULL;
  270. }
  271. /**
  272. * eeh_report_resume - Tell device to resume normal operations
  273. * @data: eeh device
  274. * @userdata: return value
  275. *
  276. * This routine must be called to notify the device driver that it
  277. * could resume so that the device driver can do some initialization
  278. * to make the recovered device work again.
  279. */
  280. static void *eeh_report_resume(void *data, void *userdata)
  281. {
  282. struct eeh_dev *edev = (struct eeh_dev *)data;
  283. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  284. struct pci_driver *driver;
  285. if (!dev || eeh_dev_removed(edev))
  286. return NULL;
  287. dev->error_state = pci_channel_io_normal;
  288. driver = eeh_pcid_get(dev);
  289. if (!driver) return NULL;
  290. eeh_enable_irq(dev);
  291. if (!driver->err_handler ||
  292. !driver->err_handler->resume ||
  293. (edev->mode & EEH_DEV_NO_HANDLER)) {
  294. edev->mode &= ~EEH_DEV_NO_HANDLER;
  295. eeh_pcid_put(dev);
  296. return NULL;
  297. }
  298. driver->err_handler->resume(dev);
  299. eeh_pcid_put(dev);
  300. return NULL;
  301. }
  302. /**
  303. * eeh_report_failure - Tell device driver that device is dead.
  304. * @data: eeh device
  305. * @userdata: return value
  306. *
  307. * This informs the device driver that the device is permanently
  308. * dead, and that no further recovery attempts will be made on it.
  309. */
  310. static void *eeh_report_failure(void *data, void *userdata)
  311. {
  312. struct eeh_dev *edev = (struct eeh_dev *)data;
  313. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  314. struct pci_driver *driver;
  315. if (!dev || eeh_dev_removed(edev))
  316. return NULL;
  317. dev->error_state = pci_channel_io_perm_failure;
  318. driver = eeh_pcid_get(dev);
  319. if (!driver) return NULL;
  320. eeh_disable_irq(dev);
  321. if (!driver->err_handler ||
  322. !driver->err_handler->error_detected) {
  323. eeh_pcid_put(dev);
  324. return NULL;
  325. }
  326. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  327. eeh_pcid_put(dev);
  328. return NULL;
  329. }
  330. static void *eeh_rmv_device(void *data, void *userdata)
  331. {
  332. struct pci_driver *driver;
  333. struct eeh_dev *edev = (struct eeh_dev *)data;
  334. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  335. int *removed = (int *)userdata;
  336. /*
  337. * Actually, we should remove the PCI bridges as well.
  338. * However, that's lots of complexity to do that,
  339. * particularly some of devices under the bridge might
  340. * support EEH. So we just care about PCI devices for
  341. * simplicity here.
  342. */
  343. if (!dev || (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE))
  344. return NULL;
  345. /*
  346. * We rely on count-based pcibios_release_device() to
  347. * detach permanently offlined PEs. Unfortunately, that's
  348. * not reliable enough. We might have the permanently
  349. * offlined PEs attached, but we needn't take care of
  350. * them and their child devices.
  351. */
  352. if (eeh_dev_removed(edev))
  353. return NULL;
  354. driver = eeh_pcid_get(dev);
  355. if (driver) {
  356. eeh_pcid_put(dev);
  357. if (driver->err_handler)
  358. return NULL;
  359. }
  360. /* Remove it from PCI subsystem */
  361. pr_debug("EEH: Removing %s without EEH sensitive driver\n",
  362. pci_name(dev));
  363. edev->bus = dev->bus;
  364. edev->mode |= EEH_DEV_DISCONNECTED;
  365. (*removed)++;
  366. pci_lock_rescan_remove();
  367. pci_stop_and_remove_bus_device(dev);
  368. pci_unlock_rescan_remove();
  369. return NULL;
  370. }
  371. static void *eeh_pe_detach_dev(void *data, void *userdata)
  372. {
  373. struct eeh_pe *pe = (struct eeh_pe *)data;
  374. struct eeh_dev *edev, *tmp;
  375. eeh_pe_for_each_dev(pe, edev, tmp) {
  376. if (!(edev->mode & EEH_DEV_DISCONNECTED))
  377. continue;
  378. edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
  379. eeh_rmv_from_parent_pe(edev);
  380. }
  381. return NULL;
  382. }
  383. /*
  384. * Explicitly clear PE's frozen state for PowerNV where
  385. * we have frozen PE until BAR restore is completed. It's
  386. * harmless to clear it for pSeries. To be consistent with
  387. * PE reset (for 3 times), we try to clear the frozen state
  388. * for 3 times as well.
  389. */
  390. static void *__eeh_clear_pe_frozen_state(void *data, void *flag)
  391. {
  392. struct eeh_pe *pe = (struct eeh_pe *)data;
  393. bool *clear_sw_state = flag;
  394. int i, rc = 1;
  395. for (i = 0; rc && i < 3; i++)
  396. rc = eeh_unfreeze_pe(pe, clear_sw_state);
  397. /* Stop immediately on any errors */
  398. if (rc) {
  399. pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
  400. __func__, rc, pe->phb->global_number, pe->addr);
  401. return (void *)pe;
  402. }
  403. return NULL;
  404. }
  405. static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
  406. bool clear_sw_state)
  407. {
  408. void *rc;
  409. rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
  410. if (!rc)
  411. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  412. return rc ? -EIO : 0;
  413. }
  414. int eeh_pe_reset_and_recover(struct eeh_pe *pe)
  415. {
  416. int result, ret;
  417. /* Bail if the PE is being recovered */
  418. if (pe->state & EEH_PE_RECOVERING)
  419. return 0;
  420. /* Put the PE into recovery mode */
  421. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  422. /* Save states */
  423. eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
  424. /* Report error */
  425. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  426. /* Issue reset */
  427. ret = eeh_reset_pe(pe);
  428. if (ret) {
  429. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  430. return ret;
  431. }
  432. /* Unfreeze the PE */
  433. ret = eeh_clear_pe_frozen_state(pe, true);
  434. if (ret) {
  435. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  436. return ret;
  437. }
  438. /* Notify completion of reset */
  439. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  440. /* Restore device state */
  441. eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
  442. /* Resume */
  443. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  444. /* Clear recovery mode */
  445. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  446. return 0;
  447. }
  448. /**
  449. * eeh_reset_device - Perform actual reset of a pci slot
  450. * @pe: EEH PE
  451. * @bus: PCI bus corresponding to the isolcated slot
  452. *
  453. * This routine must be called to do reset on the indicated PE.
  454. * During the reset, udev might be invoked because those affected
  455. * PCI devices will be removed and then added.
  456. */
  457. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus)
  458. {
  459. struct pci_bus *frozen_bus = eeh_pe_bus_get(pe);
  460. struct timeval tstamp;
  461. int cnt, rc, removed = 0;
  462. /* pcibios will clear the counter; save the value */
  463. cnt = pe->freeze_count;
  464. tstamp = pe->tstamp;
  465. /*
  466. * We don't remove the corresponding PE instances because
  467. * we need the information afterwords. The attached EEH
  468. * devices are expected to be attached soon when calling
  469. * into pcibios_add_pci_devices().
  470. */
  471. eeh_pe_state_mark(pe, EEH_PE_KEEP);
  472. if (bus) {
  473. pci_lock_rescan_remove();
  474. pcibios_remove_pci_devices(bus);
  475. pci_unlock_rescan_remove();
  476. } else if (frozen_bus) {
  477. eeh_pe_dev_traverse(pe, eeh_rmv_device, &removed);
  478. }
  479. /*
  480. * Reset the pci controller. (Asserts RST#; resets config space).
  481. * Reconfigure bridges and devices. Don't try to bring the system
  482. * up if the reset failed for some reason.
  483. *
  484. * During the reset, it's very dangerous to have uncontrolled PCI
  485. * config accesses. So we prefer to block them. However, controlled
  486. * PCI config accesses initiated from EEH itself are allowed.
  487. */
  488. rc = eeh_reset_pe(pe);
  489. if (rc)
  490. return rc;
  491. pci_lock_rescan_remove();
  492. /* Restore PE */
  493. eeh_ops->configure_bridge(pe);
  494. eeh_pe_restore_bars(pe);
  495. /* Clear frozen state */
  496. rc = eeh_clear_pe_frozen_state(pe, false);
  497. if (rc)
  498. return rc;
  499. /* Give the system 5 seconds to finish running the user-space
  500. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  501. * this is a hack, but if we don't do this, and try to bring
  502. * the device up before the scripts have taken it down,
  503. * potentially weird things happen.
  504. */
  505. if (bus) {
  506. pr_info("EEH: Sleep 5s ahead of complete hotplug\n");
  507. ssleep(5);
  508. /*
  509. * The EEH device is still connected with its parent
  510. * PE. We should disconnect it so the binding can be
  511. * rebuilt when adding PCI devices.
  512. */
  513. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  514. pcibios_add_pci_devices(bus);
  515. } else if (frozen_bus && removed) {
  516. pr_info("EEH: Sleep 5s ahead of partial hotplug\n");
  517. ssleep(5);
  518. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  519. pcibios_add_pci_devices(frozen_bus);
  520. }
  521. eeh_pe_state_clear(pe, EEH_PE_KEEP);
  522. pe->tstamp = tstamp;
  523. pe->freeze_count = cnt;
  524. pci_unlock_rescan_remove();
  525. return 0;
  526. }
  527. /* The longest amount of time to wait for a pci device
  528. * to come back on line, in seconds.
  529. */
  530. #define MAX_WAIT_FOR_RECOVERY 300
  531. static void eeh_handle_normal_event(struct eeh_pe *pe)
  532. {
  533. struct pci_bus *frozen_bus;
  534. int rc = 0;
  535. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  536. frozen_bus = eeh_pe_bus_get(pe);
  537. if (!frozen_bus) {
  538. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  539. __func__, pe->phb->global_number, pe->addr);
  540. return;
  541. }
  542. eeh_pe_update_time_stamp(pe);
  543. pe->freeze_count++;
  544. if (pe->freeze_count > eeh_max_freezes)
  545. goto excess_failures;
  546. pr_warn("EEH: This PCI device has failed %d times in the last hour\n",
  547. pe->freeze_count);
  548. /* Walk the various device drivers attached to this slot through
  549. * a reset sequence, giving each an opportunity to do what it needs
  550. * to accomplish the reset. Each child gets a report of the
  551. * status ... if any child can't handle the reset, then the entire
  552. * slot is dlpar removed and added.
  553. */
  554. pr_info("EEH: Notify device drivers to shutdown\n");
  555. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  556. /* Get the current PCI slot state. This can take a long time,
  557. * sometimes over 300 seconds for certain systems.
  558. */
  559. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  560. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  561. pr_warn("EEH: Permanent failure\n");
  562. goto hard_fail;
  563. }
  564. /* Since rtas may enable MMIO when posting the error log,
  565. * don't post the error log until after all dev drivers
  566. * have been informed.
  567. */
  568. pr_info("EEH: Collect temporary log\n");
  569. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  570. /* If all device drivers were EEH-unaware, then shut
  571. * down all of the device drivers, and hope they
  572. * go down willingly, without panicing the system.
  573. */
  574. if (result == PCI_ERS_RESULT_NONE) {
  575. pr_info("EEH: Reset with hotplug activity\n");
  576. rc = eeh_reset_device(pe, frozen_bus);
  577. if (rc) {
  578. pr_warn("%s: Unable to reset, err=%d\n",
  579. __func__, rc);
  580. goto hard_fail;
  581. }
  582. }
  583. /* If all devices reported they can proceed, then re-enable MMIO */
  584. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  585. pr_info("EEH: Enable I/O for affected devices\n");
  586. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  587. if (rc < 0)
  588. goto hard_fail;
  589. if (rc) {
  590. result = PCI_ERS_RESULT_NEED_RESET;
  591. } else {
  592. pr_info("EEH: Notify device drivers to resume I/O\n");
  593. eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
  594. }
  595. }
  596. /* If all devices reported they can proceed, then re-enable DMA */
  597. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  598. pr_info("EEH: Enabled DMA for affected devices\n");
  599. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  600. if (rc < 0)
  601. goto hard_fail;
  602. if (rc) {
  603. result = PCI_ERS_RESULT_NEED_RESET;
  604. } else {
  605. /*
  606. * We didn't do PE reset for the case. The PE
  607. * is still in frozen state. Clear it before
  608. * resuming the PE.
  609. */
  610. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  611. result = PCI_ERS_RESULT_RECOVERED;
  612. }
  613. }
  614. /* If any device has a hard failure, then shut off everything. */
  615. if (result == PCI_ERS_RESULT_DISCONNECT) {
  616. pr_warn("EEH: Device driver gave up\n");
  617. goto hard_fail;
  618. }
  619. /* If any device called out for a reset, then reset the slot */
  620. if (result == PCI_ERS_RESULT_NEED_RESET) {
  621. pr_info("EEH: Reset without hotplug activity\n");
  622. rc = eeh_reset_device(pe, NULL);
  623. if (rc) {
  624. pr_warn("%s: Cannot reset, err=%d\n",
  625. __func__, rc);
  626. goto hard_fail;
  627. }
  628. pr_info("EEH: Notify device drivers "
  629. "the completion of reset\n");
  630. result = PCI_ERS_RESULT_NONE;
  631. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  632. }
  633. /* All devices should claim they have recovered by now. */
  634. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  635. (result != PCI_ERS_RESULT_NONE)) {
  636. pr_warn("EEH: Not recovered\n");
  637. goto hard_fail;
  638. }
  639. /* Tell all device drivers that they can resume operations */
  640. pr_info("EEH: Notify device driver to resume\n");
  641. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  642. return;
  643. excess_failures:
  644. /*
  645. * About 90% of all real-life EEH failures in the field
  646. * are due to poorly seated PCI cards. Only 10% or so are
  647. * due to actual, failed cards.
  648. */
  649. pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
  650. "last hour and has been permanently disabled.\n"
  651. "Please try reseating or replacing it.\n",
  652. pe->phb->global_number, pe->addr,
  653. pe->freeze_count);
  654. goto perm_error;
  655. hard_fail:
  656. pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
  657. "Please try reseating or replacing it\n",
  658. pe->phb->global_number, pe->addr);
  659. perm_error:
  660. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  661. /* Notify all devices that they're about to go down. */
  662. eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
  663. /* Mark the PE to be removed permanently */
  664. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  665. /*
  666. * Shut down the device drivers for good. We mark
  667. * all removed devices correctly to avoid access
  668. * the their PCI config any more.
  669. */
  670. if (frozen_bus) {
  671. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  672. pci_lock_rescan_remove();
  673. pcibios_remove_pci_devices(frozen_bus);
  674. pci_unlock_rescan_remove();
  675. }
  676. }
  677. static void eeh_handle_special_event(void)
  678. {
  679. struct eeh_pe *pe, *phb_pe;
  680. struct pci_bus *bus;
  681. struct pci_controller *hose;
  682. unsigned long flags;
  683. int rc;
  684. do {
  685. rc = eeh_ops->next_error(&pe);
  686. switch (rc) {
  687. case EEH_NEXT_ERR_DEAD_IOC:
  688. /* Mark all PHBs in dead state */
  689. eeh_serialize_lock(&flags);
  690. /* Purge all events */
  691. eeh_remove_event(NULL, true);
  692. list_for_each_entry(hose, &hose_list, list_node) {
  693. phb_pe = eeh_phb_pe_get(hose);
  694. if (!phb_pe) continue;
  695. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  696. }
  697. eeh_serialize_unlock(flags);
  698. break;
  699. case EEH_NEXT_ERR_FROZEN_PE:
  700. case EEH_NEXT_ERR_FENCED_PHB:
  701. case EEH_NEXT_ERR_DEAD_PHB:
  702. /* Mark the PE in fenced state */
  703. eeh_serialize_lock(&flags);
  704. /* Purge all events of the PHB */
  705. eeh_remove_event(pe, true);
  706. if (rc == EEH_NEXT_ERR_DEAD_PHB)
  707. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  708. else
  709. eeh_pe_state_mark(pe,
  710. EEH_PE_ISOLATED | EEH_PE_RECOVERING);
  711. eeh_serialize_unlock(flags);
  712. break;
  713. case EEH_NEXT_ERR_NONE:
  714. return;
  715. default:
  716. pr_warn("%s: Invalid value %d from next_error()\n",
  717. __func__, rc);
  718. return;
  719. }
  720. /*
  721. * For fenced PHB and frozen PE, it's handled as normal
  722. * event. We have to remove the affected PHBs for dead
  723. * PHB and IOC
  724. */
  725. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  726. rc == EEH_NEXT_ERR_FENCED_PHB) {
  727. eeh_handle_normal_event(pe);
  728. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  729. } else {
  730. pci_lock_rescan_remove();
  731. list_for_each_entry(hose, &hose_list, list_node) {
  732. phb_pe = eeh_phb_pe_get(hose);
  733. if (!phb_pe ||
  734. !(phb_pe->state & EEH_PE_ISOLATED) ||
  735. (phb_pe->state & EEH_PE_RECOVERING))
  736. continue;
  737. /* Notify all devices to be down */
  738. bus = eeh_pe_bus_get(phb_pe);
  739. eeh_pe_dev_traverse(pe,
  740. eeh_report_failure, NULL);
  741. pcibios_remove_pci_devices(bus);
  742. }
  743. pci_unlock_rescan_remove();
  744. }
  745. /*
  746. * If we have detected dead IOC, we needn't proceed
  747. * any more since all PHBs would have been removed
  748. */
  749. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  750. break;
  751. } while (rc != EEH_NEXT_ERR_NONE);
  752. }
  753. /**
  754. * eeh_handle_event - Reset a PCI device after hard lockup.
  755. * @pe: EEH PE
  756. *
  757. * While PHB detects address or data parity errors on particular PCI
  758. * slot, the associated PE will be frozen. Besides, DMA's occurring
  759. * to wild addresses (which usually happen due to bugs in device
  760. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  761. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  762. *
  763. * Recovery process consists of unplugging the device driver (which
  764. * generated hotplug events to userspace), then issuing a PCI #RST to
  765. * the device, then reconfiguring the PCI config space for all bridges
  766. * & devices under this slot, and then finally restarting the device
  767. * drivers (which cause a second set of hotplug events to go out to
  768. * userspace).
  769. */
  770. void eeh_handle_event(struct eeh_pe *pe)
  771. {
  772. if (pe)
  773. eeh_handle_normal_event(pe);
  774. else
  775. eeh_handle_special_event();
  776. }