eeh_driver.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. struct eeh_rmv_data {
  37. struct list_head edev_list;
  38. int removed;
  39. };
  40. static int eeh_result_priority(enum pci_ers_result result)
  41. {
  42. switch (result) {
  43. case PCI_ERS_RESULT_NONE:
  44. return 1;
  45. case PCI_ERS_RESULT_NO_AER_DRIVER:
  46. return 2;
  47. case PCI_ERS_RESULT_RECOVERED:
  48. return 3;
  49. case PCI_ERS_RESULT_CAN_RECOVER:
  50. return 4;
  51. case PCI_ERS_RESULT_DISCONNECT:
  52. return 5;
  53. case PCI_ERS_RESULT_NEED_RESET:
  54. return 6;
  55. default:
  56. WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", (int)result);
  57. return 0;
  58. }
  59. };
  60. const char *pci_ers_result_name(enum pci_ers_result result)
  61. {
  62. switch (result) {
  63. case PCI_ERS_RESULT_NONE:
  64. return "none";
  65. case PCI_ERS_RESULT_CAN_RECOVER:
  66. return "can recover";
  67. case PCI_ERS_RESULT_NEED_RESET:
  68. return "need reset";
  69. case PCI_ERS_RESULT_DISCONNECT:
  70. return "disconnect";
  71. case PCI_ERS_RESULT_RECOVERED:
  72. return "recovered";
  73. case PCI_ERS_RESULT_NO_AER_DRIVER:
  74. return "no AER driver";
  75. default:
  76. WARN_ONCE(1, "Unknown result type: %d\n", (int)result);
  77. return "unknown";
  78. }
  79. };
  80. static __printf(2, 3) void eeh_edev_info(const struct eeh_dev *edev,
  81. const char *fmt, ...)
  82. {
  83. struct va_format vaf;
  84. va_list args;
  85. va_start(args, fmt);
  86. vaf.fmt = fmt;
  87. vaf.va = &args;
  88. printk(KERN_INFO "EEH: PE#%x (PCI %s): %pV\n", edev->pe_config_addr,
  89. edev->pdev ? dev_name(&edev->pdev->dev) : "none", &vaf);
  90. va_end(args);
  91. }
  92. static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old,
  93. enum pci_ers_result new)
  94. {
  95. if (eeh_result_priority(new) > eeh_result_priority(old))
  96. return new;
  97. return old;
  98. }
  99. static bool eeh_dev_removed(struct eeh_dev *edev)
  100. {
  101. return !edev || (edev->mode & EEH_DEV_REMOVED);
  102. }
  103. static bool eeh_edev_actionable(struct eeh_dev *edev)
  104. {
  105. return (edev->pdev && !eeh_dev_removed(edev) &&
  106. !eeh_pe_passed(edev->pe));
  107. }
  108. /**
  109. * eeh_pcid_get - Get the PCI device driver
  110. * @pdev: PCI device
  111. *
  112. * The function is used to retrieve the PCI device driver for
  113. * the indicated PCI device. Besides, we will increase the reference
  114. * of the PCI device driver to prevent that being unloaded on
  115. * the fly. Otherwise, kernel crash would be seen.
  116. */
  117. static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
  118. {
  119. if (!pdev || !pdev->driver)
  120. return NULL;
  121. if (!try_module_get(pdev->driver->driver.owner))
  122. return NULL;
  123. return pdev->driver;
  124. }
  125. /**
  126. * eeh_pcid_put - Dereference on the PCI device driver
  127. * @pdev: PCI device
  128. *
  129. * The function is called to do dereference on the PCI device
  130. * driver of the indicated PCI device.
  131. */
  132. static inline void eeh_pcid_put(struct pci_dev *pdev)
  133. {
  134. if (!pdev || !pdev->driver)
  135. return;
  136. module_put(pdev->driver->driver.owner);
  137. }
  138. /**
  139. * eeh_disable_irq - Disable interrupt for the recovering device
  140. * @dev: PCI device
  141. *
  142. * This routine must be called when reporting temporary or permanent
  143. * error to the particular PCI device to disable interrupt of that
  144. * device. If the device has enabled MSI or MSI-X interrupt, we needn't
  145. * do real work because EEH should freeze DMA transfers for those PCI
  146. * devices encountering EEH errors, which includes MSI or MSI-X.
  147. */
  148. static void eeh_disable_irq(struct eeh_dev *edev)
  149. {
  150. /* Don't disable MSI and MSI-X interrupts. They are
  151. * effectively disabled by the DMA Stopped state
  152. * when an EEH error occurs.
  153. */
  154. if (edev->pdev->msi_enabled || edev->pdev->msix_enabled)
  155. return;
  156. if (!irq_has_action(edev->pdev->irq))
  157. return;
  158. edev->mode |= EEH_DEV_IRQ_DISABLED;
  159. disable_irq_nosync(edev->pdev->irq);
  160. }
  161. /**
  162. * eeh_enable_irq - Enable interrupt for the recovering device
  163. * @dev: PCI device
  164. *
  165. * This routine must be called to enable interrupt while failed
  166. * device could be resumed.
  167. */
  168. static void eeh_enable_irq(struct eeh_dev *edev)
  169. {
  170. if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
  171. edev->mode &= ~EEH_DEV_IRQ_DISABLED;
  172. /*
  173. * FIXME !!!!!
  174. *
  175. * This is just ass backwards. This maze has
  176. * unbalanced irq_enable/disable calls. So instead of
  177. * finding the root cause it works around the warning
  178. * in the irq_enable code by conditionally calling
  179. * into it.
  180. *
  181. * That's just wrong.The warning in the core code is
  182. * there to tell people to fix their asymmetries in
  183. * their own code, not by abusing the core information
  184. * to avoid it.
  185. *
  186. * I so wish that the assymetry would be the other way
  187. * round and a few more irq_disable calls render that
  188. * shit unusable forever.
  189. *
  190. * tglx
  191. */
  192. if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq)))
  193. enable_irq(edev->pdev->irq);
  194. }
  195. }
  196. static void *eeh_dev_save_state(struct eeh_dev *edev, void *userdata)
  197. {
  198. struct pci_dev *pdev;
  199. if (!edev)
  200. return NULL;
  201. /*
  202. * We cannot access the config space on some adapters.
  203. * Otherwise, it will cause fenced PHB. We don't save
  204. * the content in their config space and will restore
  205. * from the initial config space saved when the EEH
  206. * device is created.
  207. */
  208. if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
  209. return NULL;
  210. pdev = eeh_dev_to_pci_dev(edev);
  211. if (!pdev)
  212. return NULL;
  213. pci_save_state(pdev);
  214. return NULL;
  215. }
  216. static void eeh_set_channel_state(struct eeh_pe *root, enum pci_channel_state s)
  217. {
  218. struct eeh_pe *pe;
  219. struct eeh_dev *edev, *tmp;
  220. eeh_for_each_pe(root, pe)
  221. eeh_pe_for_each_dev(pe, edev, tmp)
  222. if (eeh_edev_actionable(edev))
  223. edev->pdev->error_state = s;
  224. }
  225. static void eeh_set_irq_state(struct eeh_pe *root, bool enable)
  226. {
  227. struct eeh_pe *pe;
  228. struct eeh_dev *edev, *tmp;
  229. eeh_for_each_pe(root, pe) {
  230. eeh_pe_for_each_dev(pe, edev, tmp) {
  231. if (!eeh_edev_actionable(edev))
  232. continue;
  233. if (!eeh_pcid_get(edev->pdev))
  234. continue;
  235. if (enable)
  236. eeh_enable_irq(edev);
  237. else
  238. eeh_disable_irq(edev);
  239. eeh_pcid_put(edev->pdev);
  240. }
  241. }
  242. }
  243. typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *,
  244. struct pci_driver *);
  245. static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn,
  246. enum pci_ers_result *result)
  247. {
  248. struct pci_driver *driver;
  249. enum pci_ers_result new_result;
  250. device_lock(&edev->pdev->dev);
  251. if (eeh_edev_actionable(edev)) {
  252. driver = eeh_pcid_get(edev->pdev);
  253. if (!driver)
  254. eeh_edev_info(edev, "no driver");
  255. else if (!driver->err_handler)
  256. eeh_edev_info(edev, "driver not EEH aware");
  257. else if (edev->mode & EEH_DEV_NO_HANDLER)
  258. eeh_edev_info(edev, "driver bound too late");
  259. else {
  260. new_result = fn(edev, driver);
  261. eeh_edev_info(edev, "%s driver reports: '%s'",
  262. driver->name,
  263. pci_ers_result_name(new_result));
  264. if (result)
  265. *result = pci_ers_merge_result(*result,
  266. new_result);
  267. }
  268. if (driver)
  269. eeh_pcid_put(edev->pdev);
  270. } else {
  271. eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!edev->pdev,
  272. !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
  273. }
  274. device_unlock(&edev->pdev->dev);
  275. }
  276. static void eeh_pe_report(const char *name, struct eeh_pe *root,
  277. eeh_report_fn fn, enum pci_ers_result *result)
  278. {
  279. struct eeh_pe *pe;
  280. struct eeh_dev *edev, *tmp;
  281. pr_info("EEH: Beginning: '%s'\n", name);
  282. eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp)
  283. eeh_pe_report_edev(edev, fn, result);
  284. if (result)
  285. pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
  286. name, pci_ers_result_name(*result));
  287. else
  288. pr_info("EEH: Finished:'%s'", name);
  289. }
  290. /**
  291. * eeh_report_error - Report pci error to each device driver
  292. * @edev: eeh device
  293. * @driver: device's PCI driver
  294. *
  295. * Report an EEH error to each device driver.
  296. */
  297. static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
  298. struct pci_driver *driver)
  299. {
  300. enum pci_ers_result rc;
  301. struct pci_dev *dev = edev->pdev;
  302. if (!driver->err_handler->error_detected)
  303. return PCI_ERS_RESULT_NONE;
  304. eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
  305. driver->name);
  306. rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
  307. edev->in_error = true;
  308. pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
  309. return rc;
  310. }
  311. /**
  312. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  313. * @edev: eeh device
  314. * @driver: device's PCI driver
  315. *
  316. * Tells each device driver that IO ports, MMIO and config space I/O
  317. * are now enabled.
  318. */
  319. static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
  320. struct pci_driver *driver)
  321. {
  322. if (!driver->err_handler->mmio_enabled)
  323. return PCI_ERS_RESULT_NONE;
  324. eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
  325. return driver->err_handler->mmio_enabled(edev->pdev);
  326. }
  327. /**
  328. * eeh_report_reset - Tell device that slot has been reset
  329. * @edev: eeh device
  330. * @driver: device's PCI driver
  331. *
  332. * This routine must be called while EEH tries to reset particular
  333. * PCI device so that the associated PCI device driver could take
  334. * some actions, usually to save data the driver needs so that the
  335. * driver can work again while the device is recovered.
  336. */
  337. static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
  338. struct pci_driver *driver)
  339. {
  340. if (!driver->err_handler->slot_reset || !edev->in_error)
  341. return PCI_ERS_RESULT_NONE;
  342. eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
  343. return driver->err_handler->slot_reset(edev->pdev);
  344. }
  345. static void *eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
  346. {
  347. struct pci_dev *pdev;
  348. if (!edev)
  349. return NULL;
  350. /*
  351. * The content in the config space isn't saved because
  352. * the blocked config space on some adapters. We have
  353. * to restore the initial saved config space when the
  354. * EEH device is created.
  355. */
  356. if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
  357. if (list_is_last(&edev->list, &edev->pe->edevs))
  358. eeh_pe_restore_bars(edev->pe);
  359. return NULL;
  360. }
  361. pdev = eeh_dev_to_pci_dev(edev);
  362. if (!pdev)
  363. return NULL;
  364. pci_restore_state(pdev);
  365. return NULL;
  366. }
  367. /**
  368. * eeh_report_resume - Tell device to resume normal operations
  369. * @edev: eeh device
  370. * @driver: device's PCI driver
  371. *
  372. * This routine must be called to notify the device driver that it
  373. * could resume so that the device driver can do some initialization
  374. * to make the recovered device work again.
  375. */
  376. static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
  377. struct pci_driver *driver)
  378. {
  379. if (!driver->err_handler->resume || !edev->in_error)
  380. return PCI_ERS_RESULT_NONE;
  381. eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
  382. driver->err_handler->resume(edev->pdev);
  383. pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
  384. #ifdef CONFIG_PCI_IOV
  385. if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev))
  386. eeh_ops->notify_resume(eeh_dev_to_pdn(edev));
  387. #endif
  388. return PCI_ERS_RESULT_NONE;
  389. }
  390. /**
  391. * eeh_report_failure - Tell device driver that device is dead.
  392. * @edev: eeh device
  393. * @driver: device's PCI driver
  394. *
  395. * This informs the device driver that the device is permanently
  396. * dead, and that no further recovery attempts will be made on it.
  397. */
  398. static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
  399. struct pci_driver *driver)
  400. {
  401. enum pci_ers_result rc;
  402. if (!driver->err_handler->error_detected)
  403. return PCI_ERS_RESULT_NONE;
  404. eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
  405. driver->name);
  406. rc = driver->err_handler->error_detected(edev->pdev,
  407. pci_channel_io_perm_failure);
  408. pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_DISCONNECT);
  409. return rc;
  410. }
  411. static void *eeh_add_virt_device(void *data, void *userdata)
  412. {
  413. struct pci_driver *driver;
  414. struct eeh_dev *edev = (struct eeh_dev *)data;
  415. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  416. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  417. if (!(edev->physfn)) {
  418. pr_warn("%s: EEH dev %04x:%02x:%02x.%01x not for VF\n",
  419. __func__, pdn->phb->global_number, pdn->busno,
  420. PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
  421. return NULL;
  422. }
  423. driver = eeh_pcid_get(dev);
  424. if (driver) {
  425. if (driver->err_handler) {
  426. eeh_pcid_put(dev);
  427. return NULL;
  428. }
  429. eeh_pcid_put(dev);
  430. }
  431. #ifdef CONFIG_PCI_IOV
  432. pci_iov_add_virtfn(edev->physfn, pdn->vf_index);
  433. #endif
  434. return NULL;
  435. }
  436. static void *eeh_rmv_device(struct eeh_dev *edev, void *userdata)
  437. {
  438. struct pci_driver *driver;
  439. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  440. struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
  441. int *removed = rmv_data ? &rmv_data->removed : NULL;
  442. /*
  443. * Actually, we should remove the PCI bridges as well.
  444. * However, that's lots of complexity to do that,
  445. * particularly some of devices under the bridge might
  446. * support EEH. So we just care about PCI devices for
  447. * simplicity here.
  448. */
  449. if (!dev || (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
  450. return NULL;
  451. /*
  452. * We rely on count-based pcibios_release_device() to
  453. * detach permanently offlined PEs. Unfortunately, that's
  454. * not reliable enough. We might have the permanently
  455. * offlined PEs attached, but we needn't take care of
  456. * them and their child devices.
  457. */
  458. if (eeh_dev_removed(edev))
  459. return NULL;
  460. if (removed) {
  461. if (eeh_pe_passed(edev->pe))
  462. return NULL;
  463. driver = eeh_pcid_get(dev);
  464. if (driver) {
  465. if (driver->err_handler &&
  466. driver->err_handler->error_detected &&
  467. driver->err_handler->slot_reset) {
  468. eeh_pcid_put(dev);
  469. return NULL;
  470. }
  471. eeh_pcid_put(dev);
  472. }
  473. }
  474. /* Remove it from PCI subsystem */
  475. pr_debug("EEH: Removing %s without EEH sensitive driver\n",
  476. pci_name(dev));
  477. edev->bus = dev->bus;
  478. edev->mode |= EEH_DEV_DISCONNECTED;
  479. if (removed)
  480. (*removed)++;
  481. if (edev->physfn) {
  482. #ifdef CONFIG_PCI_IOV
  483. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  484. pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
  485. edev->pdev = NULL;
  486. /*
  487. * We have to set the VF PE number to invalid one, which is
  488. * required to plug the VF successfully.
  489. */
  490. pdn->pe_number = IODA_INVALID_PE;
  491. #endif
  492. if (rmv_data)
  493. list_add(&edev->rmv_list, &rmv_data->edev_list);
  494. } else {
  495. pci_lock_rescan_remove();
  496. pci_stop_and_remove_bus_device(dev);
  497. pci_unlock_rescan_remove();
  498. }
  499. return NULL;
  500. }
  501. static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
  502. {
  503. struct eeh_dev *edev, *tmp;
  504. eeh_pe_for_each_dev(pe, edev, tmp) {
  505. if (!(edev->mode & EEH_DEV_DISCONNECTED))
  506. continue;
  507. edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
  508. eeh_rmv_from_parent_pe(edev);
  509. }
  510. return NULL;
  511. }
  512. /*
  513. * Explicitly clear PE's frozen state for PowerNV where
  514. * we have frozen PE until BAR restore is completed. It's
  515. * harmless to clear it for pSeries. To be consistent with
  516. * PE reset (for 3 times), we try to clear the frozen state
  517. * for 3 times as well.
  518. */
  519. static void *__eeh_clear_pe_frozen_state(struct eeh_pe *pe, void *flag)
  520. {
  521. bool clear_sw_state = *(bool *)flag;
  522. int i, rc = 1;
  523. for (i = 0; rc && i < 3; i++)
  524. rc = eeh_unfreeze_pe(pe, clear_sw_state);
  525. /* Stop immediately on any errors */
  526. if (rc) {
  527. pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
  528. __func__, rc, pe->phb->global_number, pe->addr);
  529. return (void *)pe;
  530. }
  531. return NULL;
  532. }
  533. static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
  534. bool clear_sw_state)
  535. {
  536. void *rc;
  537. rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
  538. if (!rc)
  539. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  540. return rc ? -EIO : 0;
  541. }
  542. int eeh_pe_reset_and_recover(struct eeh_pe *pe)
  543. {
  544. int ret;
  545. /* Bail if the PE is being recovered */
  546. if (pe->state & EEH_PE_RECOVERING)
  547. return 0;
  548. /* Put the PE into recovery mode */
  549. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  550. /* Save states */
  551. eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
  552. /* Issue reset */
  553. ret = eeh_pe_reset_full(pe);
  554. if (ret) {
  555. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  556. return ret;
  557. }
  558. /* Unfreeze the PE */
  559. ret = eeh_clear_pe_frozen_state(pe, true);
  560. if (ret) {
  561. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  562. return ret;
  563. }
  564. /* Restore device state */
  565. eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
  566. /* Clear recovery mode */
  567. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  568. return 0;
  569. }
  570. /**
  571. * eeh_reset_device - Perform actual reset of a pci slot
  572. * @driver_eeh_aware: Does the device's driver provide EEH support?
  573. * @pe: EEH PE
  574. * @bus: PCI bus corresponding to the isolcated slot
  575. * @rmv_data: Optional, list to record removed devices
  576. *
  577. * This routine must be called to do reset on the indicated PE.
  578. * During the reset, udev might be invoked because those affected
  579. * PCI devices will be removed and then added.
  580. */
  581. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
  582. struct eeh_rmv_data *rmv_data,
  583. bool driver_eeh_aware)
  584. {
  585. time64_t tstamp;
  586. int cnt, rc;
  587. struct eeh_dev *edev;
  588. /* pcibios will clear the counter; save the value */
  589. cnt = pe->freeze_count;
  590. tstamp = pe->tstamp;
  591. /*
  592. * We don't remove the corresponding PE instances because
  593. * we need the information afterwords. The attached EEH
  594. * devices are expected to be attached soon when calling
  595. * into pci_hp_add_devices().
  596. */
  597. eeh_pe_state_mark(pe, EEH_PE_KEEP);
  598. if (driver_eeh_aware || (pe->type & EEH_PE_VF)) {
  599. eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
  600. } else {
  601. pci_lock_rescan_remove();
  602. pci_hp_remove_devices(bus);
  603. pci_unlock_rescan_remove();
  604. }
  605. /*
  606. * Reset the pci controller. (Asserts RST#; resets config space).
  607. * Reconfigure bridges and devices. Don't try to bring the system
  608. * up if the reset failed for some reason.
  609. *
  610. * During the reset, it's very dangerous to have uncontrolled PCI
  611. * config accesses. So we prefer to block them. However, controlled
  612. * PCI config accesses initiated from EEH itself are allowed.
  613. */
  614. rc = eeh_pe_reset_full(pe);
  615. if (rc)
  616. return rc;
  617. pci_lock_rescan_remove();
  618. /* Restore PE */
  619. eeh_ops->configure_bridge(pe);
  620. eeh_pe_restore_bars(pe);
  621. /* Clear frozen state */
  622. rc = eeh_clear_pe_frozen_state(pe, false);
  623. if (rc) {
  624. pci_unlock_rescan_remove();
  625. return rc;
  626. }
  627. /* Give the system 5 seconds to finish running the user-space
  628. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  629. * this is a hack, but if we don't do this, and try to bring
  630. * the device up before the scripts have taken it down,
  631. * potentially weird things happen.
  632. */
  633. if (!driver_eeh_aware || rmv_data->removed) {
  634. pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
  635. (driver_eeh_aware ? "partial" : "complete"));
  636. ssleep(5);
  637. /*
  638. * The EEH device is still connected with its parent
  639. * PE. We should disconnect it so the binding can be
  640. * rebuilt when adding PCI devices.
  641. */
  642. edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
  643. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  644. if (pe->type & EEH_PE_VF) {
  645. eeh_add_virt_device(edev, NULL);
  646. } else {
  647. if (!driver_eeh_aware)
  648. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  649. pci_hp_add_devices(bus);
  650. }
  651. }
  652. eeh_pe_state_clear(pe, EEH_PE_KEEP);
  653. pe->tstamp = tstamp;
  654. pe->freeze_count = cnt;
  655. pci_unlock_rescan_remove();
  656. return 0;
  657. }
  658. /* The longest amount of time to wait for a pci device
  659. * to come back on line, in seconds.
  660. */
  661. #define MAX_WAIT_FOR_RECOVERY 300
  662. /**
  663. * eeh_handle_normal_event - Handle EEH events on a specific PE
  664. * @pe: EEH PE - which should not be used after we return, as it may
  665. * have been invalidated.
  666. *
  667. * Attempts to recover the given PE. If recovery fails or the PE has failed
  668. * too many times, remove the PE.
  669. *
  670. * While PHB detects address or data parity errors on particular PCI
  671. * slot, the associated PE will be frozen. Besides, DMA's occurring
  672. * to wild addresses (which usually happen due to bugs in device
  673. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  674. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  675. *
  676. * Recovery process consists of unplugging the device driver (which
  677. * generated hotplug events to userspace), then issuing a PCI #RST to
  678. * the device, then reconfiguring the PCI config space for all bridges
  679. * & devices under this slot, and then finally restarting the device
  680. * drivers (which cause a second set of hotplug events to go out to
  681. * userspace).
  682. */
  683. void eeh_handle_normal_event(struct eeh_pe *pe)
  684. {
  685. struct pci_bus *bus;
  686. struct eeh_dev *edev, *tmp;
  687. struct eeh_pe *tmp_pe;
  688. int rc = 0;
  689. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  690. struct eeh_rmv_data rmv_data = {LIST_HEAD_INIT(rmv_data.edev_list), 0};
  691. bus = eeh_pe_bus_get(pe);
  692. if (!bus) {
  693. pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
  694. __func__, pe->phb->global_number, pe->addr);
  695. return;
  696. }
  697. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  698. eeh_pe_update_time_stamp(pe);
  699. pe->freeze_count++;
  700. if (pe->freeze_count > eeh_max_freezes) {
  701. pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
  702. pe->phb->global_number, pe->addr,
  703. pe->freeze_count);
  704. goto hard_fail;
  705. }
  706. pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
  707. pe->freeze_count, eeh_max_freezes);
  708. /* Walk the various device drivers attached to this slot through
  709. * a reset sequence, giving each an opportunity to do what it needs
  710. * to accomplish the reset. Each child gets a report of the
  711. * status ... if any child can't handle the reset, then the entire
  712. * slot is dlpar removed and added.
  713. *
  714. * When the PHB is fenced, we have to issue a reset to recover from
  715. * the error. Override the result if necessary to have partially
  716. * hotplug for this case.
  717. */
  718. pr_info("EEH: Notify device drivers to shutdown\n");
  719. eeh_set_channel_state(pe, pci_channel_io_frozen);
  720. eeh_set_irq_state(pe, false);
  721. eeh_pe_report("error_detected(IO frozen)", pe, eeh_report_error,
  722. &result);
  723. if ((pe->type & EEH_PE_PHB) &&
  724. result != PCI_ERS_RESULT_NONE &&
  725. result != PCI_ERS_RESULT_NEED_RESET)
  726. result = PCI_ERS_RESULT_NEED_RESET;
  727. /* Get the current PCI slot state. This can take a long time,
  728. * sometimes over 300 seconds for certain systems.
  729. */
  730. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  731. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  732. pr_warn("EEH: Permanent failure\n");
  733. goto hard_fail;
  734. }
  735. /* Since rtas may enable MMIO when posting the error log,
  736. * don't post the error log until after all dev drivers
  737. * have been informed.
  738. */
  739. pr_info("EEH: Collect temporary log\n");
  740. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  741. /* If all device drivers were EEH-unaware, then shut
  742. * down all of the device drivers, and hope they
  743. * go down willingly, without panicing the system.
  744. */
  745. if (result == PCI_ERS_RESULT_NONE) {
  746. pr_info("EEH: Reset with hotplug activity\n");
  747. rc = eeh_reset_device(pe, bus, NULL, false);
  748. if (rc) {
  749. pr_warn("%s: Unable to reset, err=%d\n",
  750. __func__, rc);
  751. goto hard_fail;
  752. }
  753. }
  754. /* If all devices reported they can proceed, then re-enable MMIO */
  755. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  756. pr_info("EEH: Enable I/O for affected devices\n");
  757. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  758. if (rc < 0)
  759. goto hard_fail;
  760. if (rc) {
  761. result = PCI_ERS_RESULT_NEED_RESET;
  762. } else {
  763. pr_info("EEH: Notify device drivers to resume I/O\n");
  764. eeh_pe_report("mmio_enabled", pe,
  765. eeh_report_mmio_enabled, &result);
  766. }
  767. }
  768. /* If all devices reported they can proceed, then re-enable DMA */
  769. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  770. pr_info("EEH: Enabled DMA for affected devices\n");
  771. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  772. if (rc < 0)
  773. goto hard_fail;
  774. if (rc) {
  775. result = PCI_ERS_RESULT_NEED_RESET;
  776. } else {
  777. /*
  778. * We didn't do PE reset for the case. The PE
  779. * is still in frozen state. Clear it before
  780. * resuming the PE.
  781. */
  782. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  783. result = PCI_ERS_RESULT_RECOVERED;
  784. }
  785. }
  786. /* If any device has a hard failure, then shut off everything. */
  787. if (result == PCI_ERS_RESULT_DISCONNECT) {
  788. pr_warn("EEH: Device driver gave up\n");
  789. goto hard_fail;
  790. }
  791. /* If any device called out for a reset, then reset the slot */
  792. if (result == PCI_ERS_RESULT_NEED_RESET) {
  793. pr_info("EEH: Reset without hotplug activity\n");
  794. rc = eeh_reset_device(pe, bus, &rmv_data, true);
  795. if (rc) {
  796. pr_warn("%s: Cannot reset, err=%d\n",
  797. __func__, rc);
  798. goto hard_fail;
  799. }
  800. pr_info("EEH: Notify device drivers "
  801. "the completion of reset\n");
  802. result = PCI_ERS_RESULT_NONE;
  803. eeh_set_channel_state(pe, pci_channel_io_normal);
  804. eeh_set_irq_state(pe, true);
  805. eeh_pe_report("slot_reset", pe, eeh_report_reset, &result);
  806. }
  807. /* All devices should claim they have recovered by now. */
  808. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  809. (result != PCI_ERS_RESULT_NONE)) {
  810. pr_warn("EEH: Not recovered\n");
  811. goto hard_fail;
  812. }
  813. /*
  814. * For those hot removed VFs, we should add back them after PF get
  815. * recovered properly.
  816. */
  817. list_for_each_entry_safe(edev, tmp, &rmv_data.edev_list, rmv_list) {
  818. eeh_add_virt_device(edev, NULL);
  819. list_del(&edev->rmv_list);
  820. }
  821. /* Tell all device drivers that they can resume operations */
  822. pr_info("EEH: Notify device driver to resume\n");
  823. eeh_set_channel_state(pe, pci_channel_io_normal);
  824. eeh_set_irq_state(pe, true);
  825. eeh_pe_report("resume", pe, eeh_report_resume, NULL);
  826. eeh_for_each_pe(pe, tmp_pe) {
  827. eeh_pe_for_each_dev(tmp_pe, edev, tmp) {
  828. edev->mode &= ~EEH_DEV_NO_HANDLER;
  829. edev->in_error = false;
  830. }
  831. }
  832. pr_info("EEH: Recovery successful.\n");
  833. goto final;
  834. hard_fail:
  835. /*
  836. * About 90% of all real-life EEH failures in the field
  837. * are due to poorly seated PCI cards. Only 10% or so are
  838. * due to actual, failed cards.
  839. */
  840. pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
  841. "Please try reseating or replacing it\n",
  842. pe->phb->global_number, pe->addr);
  843. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  844. /* Notify all devices that they're about to go down. */
  845. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  846. eeh_set_irq_state(pe, false);
  847. eeh_pe_report("error_detected(permanent failure)", pe,
  848. eeh_report_failure, NULL);
  849. /* Mark the PE to be removed permanently */
  850. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  851. /*
  852. * Shut down the device drivers for good. We mark
  853. * all removed devices correctly to avoid access
  854. * the their PCI config any more.
  855. */
  856. if (pe->type & EEH_PE_VF) {
  857. eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
  858. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  859. } else {
  860. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  861. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  862. pci_lock_rescan_remove();
  863. pci_hp_remove_devices(bus);
  864. pci_unlock_rescan_remove();
  865. /* The passed PE should no longer be used */
  866. return;
  867. }
  868. final:
  869. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  870. }
  871. /**
  872. * eeh_handle_special_event - Handle EEH events without a specific failing PE
  873. *
  874. * Called when an EEH event is detected but can't be narrowed down to a
  875. * specific PE. Iterates through possible failures and handles them as
  876. * necessary.
  877. */
  878. void eeh_handle_special_event(void)
  879. {
  880. struct eeh_pe *pe, *phb_pe;
  881. struct pci_bus *bus;
  882. struct pci_controller *hose;
  883. unsigned long flags;
  884. int rc;
  885. do {
  886. rc = eeh_ops->next_error(&pe);
  887. switch (rc) {
  888. case EEH_NEXT_ERR_DEAD_IOC:
  889. /* Mark all PHBs in dead state */
  890. eeh_serialize_lock(&flags);
  891. /* Purge all events */
  892. eeh_remove_event(NULL, true);
  893. list_for_each_entry(hose, &hose_list, list_node) {
  894. phb_pe = eeh_phb_pe_get(hose);
  895. if (!phb_pe) continue;
  896. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  897. }
  898. eeh_serialize_unlock(flags);
  899. break;
  900. case EEH_NEXT_ERR_FROZEN_PE:
  901. case EEH_NEXT_ERR_FENCED_PHB:
  902. case EEH_NEXT_ERR_DEAD_PHB:
  903. /* Mark the PE in fenced state */
  904. eeh_serialize_lock(&flags);
  905. /* Purge all events of the PHB */
  906. eeh_remove_event(pe, true);
  907. if (rc == EEH_NEXT_ERR_DEAD_PHB)
  908. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  909. else
  910. eeh_pe_state_mark(pe,
  911. EEH_PE_ISOLATED | EEH_PE_RECOVERING);
  912. eeh_serialize_unlock(flags);
  913. break;
  914. case EEH_NEXT_ERR_NONE:
  915. return;
  916. default:
  917. pr_warn("%s: Invalid value %d from next_error()\n",
  918. __func__, rc);
  919. return;
  920. }
  921. /*
  922. * For fenced PHB and frozen PE, it's handled as normal
  923. * event. We have to remove the affected PHBs for dead
  924. * PHB and IOC
  925. */
  926. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  927. rc == EEH_NEXT_ERR_FENCED_PHB) {
  928. eeh_handle_normal_event(pe);
  929. } else {
  930. pci_lock_rescan_remove();
  931. list_for_each_entry(hose, &hose_list, list_node) {
  932. phb_pe = eeh_phb_pe_get(hose);
  933. if (!phb_pe ||
  934. !(phb_pe->state & EEH_PE_ISOLATED) ||
  935. (phb_pe->state & EEH_PE_RECOVERING))
  936. continue;
  937. /* Notify all devices to be down */
  938. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  939. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  940. eeh_pe_report(
  941. "error_detected(permanent failure)", pe,
  942. eeh_report_failure, NULL);
  943. bus = eeh_pe_bus_get(phb_pe);
  944. if (!bus) {
  945. pr_err("%s: Cannot find PCI bus for "
  946. "PHB#%x-PE#%x\n",
  947. __func__,
  948. pe->phb->global_number,
  949. pe->addr);
  950. break;
  951. }
  952. pci_hp_remove_devices(bus);
  953. }
  954. pci_unlock_rescan_remove();
  955. }
  956. /*
  957. * If we have detected dead IOC, we needn't proceed
  958. * any more since all PHBs would have been removed
  959. */
  960. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  961. break;
  962. } while (rc != EEH_NEXT_ERR_NONE);
  963. }