eeh_driver.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. driver->err_handler->error_detected &&
  359. driver->err_handler->slot_reset &&
  360. driver->err_handler->resume)
  361. return NULL;
  362. }
  363. /* Remove it from PCI subsystem */
  364. pr_debug("EEH: Removing %s without EEH sensitive driver\n",
  365. pci_name(dev));
  366. edev->bus = dev->bus;
  367. edev->mode |= EEH_DEV_DISCONNECTED;
  368. (*removed)++;
  369. pci_lock_rescan_remove();
  370. pci_stop_and_remove_bus_device(dev);
  371. pci_unlock_rescan_remove();
  372. return NULL;
  373. }
  374. static void *eeh_pe_detach_dev(void *data, void *userdata)
  375. {
  376. struct eeh_pe *pe = (struct eeh_pe *)data;
  377. struct eeh_dev *edev, *tmp;
  378. eeh_pe_for_each_dev(pe, edev, tmp) {
  379. if (!(edev->mode & EEH_DEV_DISCONNECTED))
  380. continue;
  381. edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
  382. eeh_rmv_from_parent_pe(edev);
  383. }
  384. return NULL;
  385. }
  386. /*
  387. * Explicitly clear PE's frozen state for PowerNV where
  388. * we have frozen PE until BAR restore is completed. It's
  389. * harmless to clear it for pSeries. To be consistent with
  390. * PE reset (for 3 times), we try to clear the frozen state
  391. * for 3 times as well.
  392. */
  393. static void *__eeh_clear_pe_frozen_state(void *data, void *flag)
  394. {
  395. struct eeh_pe *pe = (struct eeh_pe *)data;
  396. bool *clear_sw_state = flag;
  397. int i, rc = 1;
  398. for (i = 0; rc && i < 3; i++)
  399. rc = eeh_unfreeze_pe(pe, clear_sw_state);
  400. /* Stop immediately on any errors */
  401. if (rc) {
  402. pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
  403. __func__, rc, pe->phb->global_number, pe->addr);
  404. return (void *)pe;
  405. }
  406. return NULL;
  407. }
  408. static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
  409. bool clear_sw_state)
  410. {
  411. void *rc;
  412. rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
  413. if (!rc)
  414. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  415. return rc ? -EIO : 0;
  416. }
  417. int eeh_pe_reset_and_recover(struct eeh_pe *pe)
  418. {
  419. int result, ret;
  420. /* Bail if the PE is being recovered */
  421. if (pe->state & EEH_PE_RECOVERING)
  422. return 0;
  423. /* Put the PE into recovery mode */
  424. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  425. /* Save states */
  426. eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
  427. /* Report error */
  428. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  429. /* Issue reset */
  430. ret = eeh_reset_pe(pe);
  431. if (ret) {
  432. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  433. return ret;
  434. }
  435. /* Unfreeze the PE */
  436. ret = eeh_clear_pe_frozen_state(pe, true);
  437. if (ret) {
  438. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  439. return ret;
  440. }
  441. /* Notify completion of reset */
  442. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  443. /* Restore device state */
  444. eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
  445. /* Resume */
  446. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  447. /* Clear recovery mode */
  448. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  449. return 0;
  450. }
  451. /**
  452. * eeh_reset_device - Perform actual reset of a pci slot
  453. * @pe: EEH PE
  454. * @bus: PCI bus corresponding to the isolcated slot
  455. *
  456. * This routine must be called to do reset on the indicated PE.
  457. * During the reset, udev might be invoked because those affected
  458. * PCI devices will be removed and then added.
  459. */
  460. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus)
  461. {
  462. struct pci_bus *frozen_bus = eeh_pe_bus_get(pe);
  463. struct timeval tstamp;
  464. int cnt, rc, removed = 0;
  465. /* pcibios will clear the counter; save the value */
  466. cnt = pe->freeze_count;
  467. tstamp = pe->tstamp;
  468. /*
  469. * We don't remove the corresponding PE instances because
  470. * we need the information afterwords. The attached EEH
  471. * devices are expected to be attached soon when calling
  472. * into pcibios_add_pci_devices().
  473. */
  474. eeh_pe_state_mark(pe, EEH_PE_KEEP);
  475. if (bus) {
  476. pci_lock_rescan_remove();
  477. pcibios_remove_pci_devices(bus);
  478. pci_unlock_rescan_remove();
  479. } else if (frozen_bus) {
  480. eeh_pe_dev_traverse(pe, eeh_rmv_device, &removed);
  481. }
  482. /*
  483. * Reset the pci controller. (Asserts RST#; resets config space).
  484. * Reconfigure bridges and devices. Don't try to bring the system
  485. * up if the reset failed for some reason.
  486. *
  487. * During the reset, it's very dangerous to have uncontrolled PCI
  488. * config accesses. So we prefer to block them. However, controlled
  489. * PCI config accesses initiated from EEH itself are allowed.
  490. */
  491. rc = eeh_reset_pe(pe);
  492. if (rc)
  493. return rc;
  494. pci_lock_rescan_remove();
  495. /* Restore PE */
  496. eeh_ops->configure_bridge(pe);
  497. eeh_pe_restore_bars(pe);
  498. /* Clear frozen state */
  499. rc = eeh_clear_pe_frozen_state(pe, false);
  500. if (rc)
  501. return rc;
  502. /* Give the system 5 seconds to finish running the user-space
  503. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  504. * this is a hack, but if we don't do this, and try to bring
  505. * the device up before the scripts have taken it down,
  506. * potentially weird things happen.
  507. */
  508. if (bus) {
  509. pr_info("EEH: Sleep 5s ahead of complete hotplug\n");
  510. ssleep(5);
  511. /*
  512. * The EEH device is still connected with its parent
  513. * PE. We should disconnect it so the binding can be
  514. * rebuilt when adding PCI devices.
  515. */
  516. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  517. pcibios_add_pci_devices(bus);
  518. } else if (frozen_bus && removed) {
  519. pr_info("EEH: Sleep 5s ahead of partial hotplug\n");
  520. ssleep(5);
  521. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  522. pcibios_add_pci_devices(frozen_bus);
  523. }
  524. eeh_pe_state_clear(pe, EEH_PE_KEEP);
  525. pe->tstamp = tstamp;
  526. pe->freeze_count = cnt;
  527. pci_unlock_rescan_remove();
  528. return 0;
  529. }
  530. /* The longest amount of time to wait for a pci device
  531. * to come back on line, in seconds.
  532. */
  533. #define MAX_WAIT_FOR_RECOVERY 300
  534. static void eeh_handle_normal_event(struct eeh_pe *pe)
  535. {
  536. struct pci_bus *frozen_bus;
  537. int rc = 0;
  538. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  539. frozen_bus = eeh_pe_bus_get(pe);
  540. if (!frozen_bus) {
  541. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  542. __func__, pe->phb->global_number, pe->addr);
  543. return;
  544. }
  545. eeh_pe_update_time_stamp(pe);
  546. pe->freeze_count++;
  547. if (pe->freeze_count > eeh_max_freezes)
  548. goto excess_failures;
  549. pr_warn("EEH: This PCI device has failed %d times in the last hour\n",
  550. pe->freeze_count);
  551. /* Walk the various device drivers attached to this slot through
  552. * a reset sequence, giving each an opportunity to do what it needs
  553. * to accomplish the reset. Each child gets a report of the
  554. * status ... if any child can't handle the reset, then the entire
  555. * slot is dlpar removed and added.
  556. *
  557. * When the PHB is fenced, we have to issue a reset to recover from
  558. * the error. Override the result if necessary to have partially
  559. * hotplug for this case.
  560. */
  561. pr_info("EEH: Notify device drivers to shutdown\n");
  562. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  563. if ((pe->type & EEH_PE_PHB) &&
  564. result != PCI_ERS_RESULT_NONE &&
  565. result != PCI_ERS_RESULT_NEED_RESET)
  566. result = PCI_ERS_RESULT_NEED_RESET;
  567. /* Get the current PCI slot state. This can take a long time,
  568. * sometimes over 300 seconds for certain systems.
  569. */
  570. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  571. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  572. pr_warn("EEH: Permanent failure\n");
  573. goto hard_fail;
  574. }
  575. /* Since rtas may enable MMIO when posting the error log,
  576. * don't post the error log until after all dev drivers
  577. * have been informed.
  578. */
  579. pr_info("EEH: Collect temporary log\n");
  580. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  581. /* If all device drivers were EEH-unaware, then shut
  582. * down all of the device drivers, and hope they
  583. * go down willingly, without panicing the system.
  584. */
  585. if (result == PCI_ERS_RESULT_NONE) {
  586. pr_info("EEH: Reset with hotplug activity\n");
  587. rc = eeh_reset_device(pe, frozen_bus);
  588. if (rc) {
  589. pr_warn("%s: Unable to reset, err=%d\n",
  590. __func__, rc);
  591. goto hard_fail;
  592. }
  593. }
  594. /* If all devices reported they can proceed, then re-enable MMIO */
  595. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  596. pr_info("EEH: Enable I/O for affected devices\n");
  597. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  598. if (rc < 0)
  599. goto hard_fail;
  600. if (rc) {
  601. result = PCI_ERS_RESULT_NEED_RESET;
  602. } else {
  603. pr_info("EEH: Notify device drivers to resume I/O\n");
  604. eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
  605. }
  606. }
  607. /* If all devices reported they can proceed, then re-enable DMA */
  608. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  609. pr_info("EEH: Enabled DMA for affected devices\n");
  610. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  611. if (rc < 0)
  612. goto hard_fail;
  613. if (rc) {
  614. result = PCI_ERS_RESULT_NEED_RESET;
  615. } else {
  616. /*
  617. * We didn't do PE reset for the case. The PE
  618. * is still in frozen state. Clear it before
  619. * resuming the PE.
  620. */
  621. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  622. result = PCI_ERS_RESULT_RECOVERED;
  623. }
  624. }
  625. /* If any device has a hard failure, then shut off everything. */
  626. if (result == PCI_ERS_RESULT_DISCONNECT) {
  627. pr_warn("EEH: Device driver gave up\n");
  628. goto hard_fail;
  629. }
  630. /* If any device called out for a reset, then reset the slot */
  631. if (result == PCI_ERS_RESULT_NEED_RESET) {
  632. pr_info("EEH: Reset without hotplug activity\n");
  633. rc = eeh_reset_device(pe, NULL);
  634. if (rc) {
  635. pr_warn("%s: Cannot reset, err=%d\n",
  636. __func__, rc);
  637. goto hard_fail;
  638. }
  639. pr_info("EEH: Notify device drivers "
  640. "the completion of reset\n");
  641. result = PCI_ERS_RESULT_NONE;
  642. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  643. }
  644. /* All devices should claim they have recovered by now. */
  645. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  646. (result != PCI_ERS_RESULT_NONE)) {
  647. pr_warn("EEH: Not recovered\n");
  648. goto hard_fail;
  649. }
  650. /* Tell all device drivers that they can resume operations */
  651. pr_info("EEH: Notify device driver to resume\n");
  652. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  653. return;
  654. excess_failures:
  655. /*
  656. * About 90% of all real-life EEH failures in the field
  657. * are due to poorly seated PCI cards. Only 10% or so are
  658. * due to actual, failed cards.
  659. */
  660. pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
  661. "last hour and has been permanently disabled.\n"
  662. "Please try reseating or replacing it.\n",
  663. pe->phb->global_number, pe->addr,
  664. pe->freeze_count);
  665. goto perm_error;
  666. hard_fail:
  667. pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
  668. "Please try reseating or replacing it\n",
  669. pe->phb->global_number, pe->addr);
  670. perm_error:
  671. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  672. /* Notify all devices that they're about to go down. */
  673. eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
  674. /* Mark the PE to be removed permanently */
  675. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  676. /*
  677. * Shut down the device drivers for good. We mark
  678. * all removed devices correctly to avoid access
  679. * the their PCI config any more.
  680. */
  681. if (frozen_bus) {
  682. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  683. pci_lock_rescan_remove();
  684. pcibios_remove_pci_devices(frozen_bus);
  685. pci_unlock_rescan_remove();
  686. }
  687. }
  688. static void eeh_handle_special_event(void)
  689. {
  690. struct eeh_pe *pe, *phb_pe;
  691. struct pci_bus *bus;
  692. struct pci_controller *hose;
  693. unsigned long flags;
  694. int rc;
  695. do {
  696. rc = eeh_ops->next_error(&pe);
  697. switch (rc) {
  698. case EEH_NEXT_ERR_DEAD_IOC:
  699. /* Mark all PHBs in dead state */
  700. eeh_serialize_lock(&flags);
  701. /* Purge all events */
  702. eeh_remove_event(NULL, true);
  703. list_for_each_entry(hose, &hose_list, list_node) {
  704. phb_pe = eeh_phb_pe_get(hose);
  705. if (!phb_pe) continue;
  706. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  707. }
  708. eeh_serialize_unlock(flags);
  709. break;
  710. case EEH_NEXT_ERR_FROZEN_PE:
  711. case EEH_NEXT_ERR_FENCED_PHB:
  712. case EEH_NEXT_ERR_DEAD_PHB:
  713. /* Mark the PE in fenced state */
  714. eeh_serialize_lock(&flags);
  715. /* Purge all events of the PHB */
  716. eeh_remove_event(pe, true);
  717. if (rc == EEH_NEXT_ERR_DEAD_PHB)
  718. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  719. else
  720. eeh_pe_state_mark(pe,
  721. EEH_PE_ISOLATED | EEH_PE_RECOVERING);
  722. eeh_serialize_unlock(flags);
  723. break;
  724. case EEH_NEXT_ERR_NONE:
  725. return;
  726. default:
  727. pr_warn("%s: Invalid value %d from next_error()\n",
  728. __func__, rc);
  729. return;
  730. }
  731. /*
  732. * For fenced PHB and frozen PE, it's handled as normal
  733. * event. We have to remove the affected PHBs for dead
  734. * PHB and IOC
  735. */
  736. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  737. rc == EEH_NEXT_ERR_FENCED_PHB) {
  738. eeh_handle_normal_event(pe);
  739. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  740. } else {
  741. pci_lock_rescan_remove();
  742. list_for_each_entry(hose, &hose_list, list_node) {
  743. phb_pe = eeh_phb_pe_get(hose);
  744. if (!phb_pe ||
  745. !(phb_pe->state & EEH_PE_ISOLATED) ||
  746. (phb_pe->state & EEH_PE_RECOVERING))
  747. continue;
  748. /* Notify all devices to be down */
  749. bus = eeh_pe_bus_get(phb_pe);
  750. eeh_pe_dev_traverse(pe,
  751. eeh_report_failure, NULL);
  752. pcibios_remove_pci_devices(bus);
  753. }
  754. pci_unlock_rescan_remove();
  755. }
  756. /*
  757. * If we have detected dead IOC, we needn't proceed
  758. * any more since all PHBs would have been removed
  759. */
  760. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  761. break;
  762. } while (rc != EEH_NEXT_ERR_NONE);
  763. }
  764. /**
  765. * eeh_handle_event - Reset a PCI device after hard lockup.
  766. * @pe: EEH PE
  767. *
  768. * While PHB detects address or data parity errors on particular PCI
  769. * slot, the associated PE will be frozen. Besides, DMA's occurring
  770. * to wild addresses (which usually happen due to bugs in device
  771. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  772. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  773. *
  774. * Recovery process consists of unplugging the device driver (which
  775. * generated hotplug events to userspace), then issuing a PCI #RST to
  776. * the device, then reconfiguring the PCI config space for all bridges
  777. * & devices under this slot, and then finally restarting the device
  778. * drivers (which cause a second set of hotplug events to go out to
  779. * userspace).
  780. */
  781. void eeh_handle_event(struct eeh_pe *pe)
  782. {
  783. if (pe)
  784. eeh_handle_normal_event(pe);
  785. else
  786. eeh_handle_special_event();
  787. }