eeh_driver.c 32 KB

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