eeh_driver.c 24 KB

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