eeh_driver.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 removed_vf_list;
  38. int removed_dev_count;
  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. if (!edev->pdev) {
  251. eeh_edev_info(edev, "no device");
  252. return;
  253. }
  254. device_lock(&edev->pdev->dev);
  255. if (eeh_edev_actionable(edev)) {
  256. driver = eeh_pcid_get(edev->pdev);
  257. if (!driver)
  258. eeh_edev_info(edev, "no driver");
  259. else if (!driver->err_handler)
  260. eeh_edev_info(edev, "driver not EEH aware");
  261. else if (edev->mode & EEH_DEV_NO_HANDLER)
  262. eeh_edev_info(edev, "driver bound too late");
  263. else {
  264. new_result = fn(edev, driver);
  265. eeh_edev_info(edev, "%s driver reports: '%s'",
  266. driver->name,
  267. pci_ers_result_name(new_result));
  268. if (result)
  269. *result = pci_ers_merge_result(*result,
  270. new_result);
  271. }
  272. if (driver)
  273. eeh_pcid_put(edev->pdev);
  274. } else {
  275. eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!edev->pdev,
  276. !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
  277. }
  278. device_unlock(&edev->pdev->dev);
  279. }
  280. static void eeh_pe_report(const char *name, struct eeh_pe *root,
  281. eeh_report_fn fn, enum pci_ers_result *result)
  282. {
  283. struct eeh_pe *pe;
  284. struct eeh_dev *edev, *tmp;
  285. pr_info("EEH: Beginning: '%s'\n", name);
  286. eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp)
  287. eeh_pe_report_edev(edev, fn, result);
  288. if (result)
  289. pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
  290. name, pci_ers_result_name(*result));
  291. else
  292. pr_info("EEH: Finished:'%s'", name);
  293. }
  294. /**
  295. * eeh_report_error - Report pci error to each device driver
  296. * @edev: eeh device
  297. * @driver: device's PCI driver
  298. *
  299. * Report an EEH error to each device driver.
  300. */
  301. static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
  302. struct pci_driver *driver)
  303. {
  304. enum pci_ers_result rc;
  305. struct pci_dev *dev = edev->pdev;
  306. if (!driver->err_handler->error_detected)
  307. return PCI_ERS_RESULT_NONE;
  308. eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
  309. driver->name);
  310. rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
  311. edev->in_error = true;
  312. pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
  313. return rc;
  314. }
  315. /**
  316. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  317. * @edev: eeh device
  318. * @driver: device's PCI driver
  319. *
  320. * Tells each device driver that IO ports, MMIO and config space I/O
  321. * are now enabled.
  322. */
  323. static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
  324. struct pci_driver *driver)
  325. {
  326. if (!driver->err_handler->mmio_enabled)
  327. return PCI_ERS_RESULT_NONE;
  328. eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
  329. return driver->err_handler->mmio_enabled(edev->pdev);
  330. }
  331. /**
  332. * eeh_report_reset - Tell device that slot has been reset
  333. * @edev: eeh device
  334. * @driver: device's PCI driver
  335. *
  336. * This routine must be called while EEH tries to reset particular
  337. * PCI device so that the associated PCI device driver could take
  338. * some actions, usually to save data the driver needs so that the
  339. * driver can work again while the device is recovered.
  340. */
  341. static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
  342. struct pci_driver *driver)
  343. {
  344. if (!driver->err_handler->slot_reset || !edev->in_error)
  345. return PCI_ERS_RESULT_NONE;
  346. eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
  347. return driver->err_handler->slot_reset(edev->pdev);
  348. }
  349. static void *eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
  350. {
  351. struct pci_dev *pdev;
  352. if (!edev)
  353. return NULL;
  354. /*
  355. * The content in the config space isn't saved because
  356. * the blocked config space on some adapters. We have
  357. * to restore the initial saved config space when the
  358. * EEH device is created.
  359. */
  360. if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
  361. if (list_is_last(&edev->entry, &edev->pe->edevs))
  362. eeh_pe_restore_bars(edev->pe);
  363. return NULL;
  364. }
  365. pdev = eeh_dev_to_pci_dev(edev);
  366. if (!pdev)
  367. return NULL;
  368. pci_restore_state(pdev);
  369. return NULL;
  370. }
  371. /**
  372. * eeh_report_resume - Tell device to resume normal operations
  373. * @edev: eeh device
  374. * @driver: device's PCI driver
  375. *
  376. * This routine must be called to notify the device driver that it
  377. * could resume so that the device driver can do some initialization
  378. * to make the recovered device work again.
  379. */
  380. static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
  381. struct pci_driver *driver)
  382. {
  383. if (!driver->err_handler->resume || !edev->in_error)
  384. return PCI_ERS_RESULT_NONE;
  385. eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
  386. driver->err_handler->resume(edev->pdev);
  387. pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
  388. #ifdef CONFIG_PCI_IOV
  389. if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev))
  390. eeh_ops->notify_resume(eeh_dev_to_pdn(edev));
  391. #endif
  392. return PCI_ERS_RESULT_NONE;
  393. }
  394. /**
  395. * eeh_report_failure - Tell device driver that device is dead.
  396. * @edev: eeh device
  397. * @driver: device's PCI driver
  398. *
  399. * This informs the device driver that the device is permanently
  400. * dead, and that no further recovery attempts will be made on it.
  401. */
  402. static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
  403. struct pci_driver *driver)
  404. {
  405. enum pci_ers_result rc;
  406. if (!driver->err_handler->error_detected)
  407. return PCI_ERS_RESULT_NONE;
  408. eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
  409. driver->name);
  410. rc = driver->err_handler->error_detected(edev->pdev,
  411. pci_channel_io_perm_failure);
  412. pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_DISCONNECT);
  413. return rc;
  414. }
  415. static void *eeh_add_virt_device(struct eeh_dev *edev)
  416. {
  417. struct pci_driver *driver;
  418. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  419. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  420. if (!(edev->physfn)) {
  421. pr_warn("%s: EEH dev %04x:%02x:%02x.%01x not for VF\n",
  422. __func__, pdn->phb->global_number, pdn->busno,
  423. PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
  424. return NULL;
  425. }
  426. driver = eeh_pcid_get(dev);
  427. if (driver) {
  428. if (driver->err_handler) {
  429. eeh_pcid_put(dev);
  430. return NULL;
  431. }
  432. eeh_pcid_put(dev);
  433. }
  434. #ifdef CONFIG_PCI_IOV
  435. pci_iov_add_virtfn(edev->physfn, pdn->vf_index);
  436. #endif
  437. return NULL;
  438. }
  439. static void *eeh_rmv_device(struct eeh_dev *edev, void *userdata)
  440. {
  441. struct pci_driver *driver;
  442. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  443. struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
  444. /*
  445. * Actually, we should remove the PCI bridges as well.
  446. * However, that's lots of complexity to do that,
  447. * particularly some of devices under the bridge might
  448. * support EEH. So we just care about PCI devices for
  449. * simplicity here.
  450. */
  451. if (!dev || (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
  452. return NULL;
  453. /*
  454. * We rely on count-based pcibios_release_device() to
  455. * detach permanently offlined PEs. Unfortunately, that's
  456. * not reliable enough. We might have the permanently
  457. * offlined PEs attached, but we needn't take care of
  458. * them and their child devices.
  459. */
  460. if (eeh_dev_removed(edev))
  461. return NULL;
  462. if (rmv_data) {
  463. if (eeh_pe_passed(edev->pe))
  464. return NULL;
  465. driver = eeh_pcid_get(dev);
  466. if (driver) {
  467. if (driver->err_handler &&
  468. driver->err_handler->error_detected &&
  469. driver->err_handler->slot_reset) {
  470. eeh_pcid_put(dev);
  471. return NULL;
  472. }
  473. eeh_pcid_put(dev);
  474. }
  475. }
  476. /* Remove it from PCI subsystem */
  477. pr_debug("EEH: Removing %s without EEH sensitive driver\n",
  478. pci_name(dev));
  479. edev->mode |= EEH_DEV_DISCONNECTED;
  480. if (rmv_data)
  481. rmv_data->removed_dev_count++;
  482. if (edev->physfn) {
  483. #ifdef CONFIG_PCI_IOV
  484. struct pci_dn *pdn = eeh_dev_to_pdn(edev);
  485. pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
  486. edev->pdev = NULL;
  487. /*
  488. * We have to set the VF PE number to invalid one, which is
  489. * required to plug the VF successfully.
  490. */
  491. pdn->pe_number = IODA_INVALID_PE;
  492. #endif
  493. if (rmv_data)
  494. list_add(&edev->rmv_entry, &rmv_data->removed_vf_list);
  495. } else {
  496. pci_lock_rescan_remove();
  497. pci_stop_and_remove_bus_device(dev);
  498. pci_unlock_rescan_remove();
  499. }
  500. return NULL;
  501. }
  502. static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
  503. {
  504. struct eeh_dev *edev, *tmp;
  505. eeh_pe_for_each_dev(pe, edev, tmp) {
  506. if (!(edev->mode & EEH_DEV_DISCONNECTED))
  507. continue;
  508. edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
  509. eeh_rmv_from_parent_pe(edev);
  510. }
  511. return NULL;
  512. }
  513. /*
  514. * Explicitly clear PE's frozen state for PowerNV where
  515. * we have frozen PE until BAR restore is completed. It's
  516. * harmless to clear it for pSeries. To be consistent with
  517. * PE reset (for 3 times), we try to clear the frozen state
  518. * for 3 times as well.
  519. */
  520. static void *__eeh_clear_pe_frozen_state(struct eeh_pe *pe, void *flag)
  521. {
  522. bool clear_sw_state = *(bool *)flag;
  523. int i, rc = 1;
  524. for (i = 0; rc && i < 3; i++)
  525. rc = eeh_unfreeze_pe(pe, clear_sw_state);
  526. /* Stop immediately on any errors */
  527. if (rc) {
  528. pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
  529. __func__, rc, pe->phb->global_number, pe->addr);
  530. return (void *)pe;
  531. }
  532. return NULL;
  533. }
  534. static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
  535. bool clear_sw_state)
  536. {
  537. void *rc;
  538. rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
  539. if (!rc)
  540. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  541. return rc ? -EIO : 0;
  542. }
  543. int eeh_pe_reset_and_recover(struct eeh_pe *pe)
  544. {
  545. int ret;
  546. /* Bail if the PE is being recovered */
  547. if (pe->state & EEH_PE_RECOVERING)
  548. return 0;
  549. /* Put the PE into recovery mode */
  550. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  551. /* Save states */
  552. eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
  553. /* Issue reset */
  554. ret = eeh_pe_reset_full(pe);
  555. if (ret) {
  556. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  557. return ret;
  558. }
  559. /* Unfreeze the PE */
  560. ret = eeh_clear_pe_frozen_state(pe, true);
  561. if (ret) {
  562. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  563. return ret;
  564. }
  565. /* Restore device state */
  566. eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
  567. /* Clear recovery mode */
  568. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  569. return 0;
  570. }
  571. /**
  572. * eeh_reset_device - Perform actual reset of a pci slot
  573. * @driver_eeh_aware: Does the device's driver provide EEH support?
  574. * @pe: EEH PE
  575. * @bus: PCI bus corresponding to the isolcated slot
  576. * @rmv_data: Optional, list to record removed devices
  577. *
  578. * This routine must be called to do reset on the indicated PE.
  579. * During the reset, udev might be invoked because those affected
  580. * PCI devices will be removed and then added.
  581. */
  582. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
  583. struct eeh_rmv_data *rmv_data,
  584. bool driver_eeh_aware)
  585. {
  586. time64_t tstamp;
  587. int cnt, rc;
  588. struct eeh_dev *edev;
  589. /* pcibios will clear the counter; save the value */
  590. cnt = pe->freeze_count;
  591. tstamp = pe->tstamp;
  592. /*
  593. * We don't remove the corresponding PE instances because
  594. * we need the information afterwords. The attached EEH
  595. * devices are expected to be attached soon when calling
  596. * into pci_hp_add_devices().
  597. */
  598. eeh_pe_state_mark(pe, EEH_PE_KEEP);
  599. if (driver_eeh_aware || (pe->type & EEH_PE_VF)) {
  600. eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
  601. } else {
  602. pci_lock_rescan_remove();
  603. pci_hp_remove_devices(bus);
  604. pci_unlock_rescan_remove();
  605. }
  606. /*
  607. * Reset the pci controller. (Asserts RST#; resets config space).
  608. * Reconfigure bridges and devices. Don't try to bring the system
  609. * up if the reset failed for some reason.
  610. *
  611. * During the reset, it's very dangerous to have uncontrolled PCI
  612. * config accesses. So we prefer to block them. However, controlled
  613. * PCI config accesses initiated from EEH itself are allowed.
  614. */
  615. rc = eeh_pe_reset_full(pe);
  616. if (rc)
  617. return rc;
  618. pci_lock_rescan_remove();
  619. /* Restore PE */
  620. eeh_ops->configure_bridge(pe);
  621. eeh_pe_restore_bars(pe);
  622. /* Clear frozen state */
  623. rc = eeh_clear_pe_frozen_state(pe, false);
  624. if (rc) {
  625. pci_unlock_rescan_remove();
  626. return rc;
  627. }
  628. /* Give the system 5 seconds to finish running the user-space
  629. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  630. * this is a hack, but if we don't do this, and try to bring
  631. * the device up before the scripts have taken it down,
  632. * potentially weird things happen.
  633. */
  634. if (!driver_eeh_aware || rmv_data->removed_dev_count) {
  635. pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
  636. (driver_eeh_aware ? "partial" : "complete"));
  637. ssleep(5);
  638. /*
  639. * The EEH device is still connected with its parent
  640. * PE. We should disconnect it so the binding can be
  641. * rebuilt when adding PCI devices.
  642. */
  643. edev = list_first_entry(&pe->edevs, struct eeh_dev, entry);
  644. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  645. if (pe->type & EEH_PE_VF) {
  646. eeh_add_virt_device(edev);
  647. } else {
  648. if (!driver_eeh_aware)
  649. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  650. pci_hp_add_devices(bus);
  651. }
  652. }
  653. eeh_pe_state_clear(pe, EEH_PE_KEEP);
  654. pe->tstamp = tstamp;
  655. pe->freeze_count = cnt;
  656. pci_unlock_rescan_remove();
  657. return 0;
  658. }
  659. /* The longest amount of time to wait for a pci device
  660. * to come back on line, in seconds.
  661. */
  662. #define MAX_WAIT_FOR_RECOVERY 300
  663. /**
  664. * eeh_handle_normal_event - Handle EEH events on a specific PE
  665. * @pe: EEH PE - which should not be used after we return, as it may
  666. * have been invalidated.
  667. *
  668. * Attempts to recover the given PE. If recovery fails or the PE has failed
  669. * too many times, remove the PE.
  670. *
  671. * While PHB detects address or data parity errors on particular PCI
  672. * slot, the associated PE will be frozen. Besides, DMA's occurring
  673. * to wild addresses (which usually happen due to bugs in device
  674. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  675. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  676. *
  677. * Recovery process consists of unplugging the device driver (which
  678. * generated hotplug events to userspace), then issuing a PCI #RST to
  679. * the device, then reconfiguring the PCI config space for all bridges
  680. * & devices under this slot, and then finally restarting the device
  681. * drivers (which cause a second set of hotplug events to go out to
  682. * userspace).
  683. */
  684. void eeh_handle_normal_event(struct eeh_pe *pe)
  685. {
  686. struct pci_bus *bus;
  687. struct eeh_dev *edev, *tmp;
  688. struct eeh_pe *tmp_pe;
  689. int rc = 0;
  690. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  691. struct eeh_rmv_data rmv_data =
  692. {LIST_HEAD_INIT(rmv_data.removed_vf_list), 0};
  693. bus = eeh_pe_bus_get(pe);
  694. if (!bus) {
  695. pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
  696. __func__, pe->phb->global_number, pe->addr);
  697. return;
  698. }
  699. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  700. eeh_pe_update_time_stamp(pe);
  701. pe->freeze_count++;
  702. if (pe->freeze_count > eeh_max_freezes) {
  703. pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
  704. pe->phb->global_number, pe->addr,
  705. pe->freeze_count);
  706. result = PCI_ERS_RESULT_DISCONNECT;
  707. }
  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. if (result != PCI_ERS_RESULT_DISCONNECT) {
  719. pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
  720. pe->freeze_count, eeh_max_freezes);
  721. pr_info("EEH: Notify device drivers to shutdown\n");
  722. eeh_set_channel_state(pe, pci_channel_io_frozen);
  723. eeh_set_irq_state(pe, false);
  724. eeh_pe_report("error_detected(IO frozen)", pe,
  725. eeh_report_error, &result);
  726. if ((pe->type & EEH_PE_PHB) &&
  727. result != PCI_ERS_RESULT_NONE &&
  728. result != PCI_ERS_RESULT_NEED_RESET)
  729. result = PCI_ERS_RESULT_NEED_RESET;
  730. }
  731. /* Get the current PCI slot state. This can take a long time,
  732. * sometimes over 300 seconds for certain systems.
  733. */
  734. if (result != PCI_ERS_RESULT_DISCONNECT) {
  735. rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  736. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  737. pr_warn("EEH: Permanent failure\n");
  738. result = PCI_ERS_RESULT_DISCONNECT;
  739. }
  740. }
  741. /* Since rtas may enable MMIO when posting the error log,
  742. * don't post the error log until after all dev drivers
  743. * have been informed.
  744. */
  745. if (result != PCI_ERS_RESULT_DISCONNECT) {
  746. pr_info("EEH: Collect temporary log\n");
  747. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  748. }
  749. /* If all device drivers were EEH-unaware, then shut
  750. * down all of the device drivers, and hope they
  751. * go down willingly, without panicing the system.
  752. */
  753. if (result == PCI_ERS_RESULT_NONE) {
  754. pr_info("EEH: Reset with hotplug activity\n");
  755. rc = eeh_reset_device(pe, bus, NULL, false);
  756. if (rc) {
  757. pr_warn("%s: Unable to reset, err=%d\n",
  758. __func__, rc);
  759. result = PCI_ERS_RESULT_DISCONNECT;
  760. }
  761. }
  762. /* If all devices reported they can proceed, then re-enable MMIO */
  763. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  764. pr_info("EEH: Enable I/O for affected devices\n");
  765. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  766. if (rc < 0) {
  767. result = PCI_ERS_RESULT_DISCONNECT;
  768. } else if (rc) {
  769. result = PCI_ERS_RESULT_NEED_RESET;
  770. } else {
  771. pr_info("EEH: Notify device drivers to resume I/O\n");
  772. eeh_pe_report("mmio_enabled", pe,
  773. eeh_report_mmio_enabled, &result);
  774. }
  775. }
  776. /* If all devices reported they can proceed, then re-enable DMA */
  777. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  778. pr_info("EEH: Enabled DMA for affected devices\n");
  779. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  780. if (rc < 0) {
  781. result = PCI_ERS_RESULT_DISCONNECT;
  782. } else if (rc) {
  783. result = PCI_ERS_RESULT_NEED_RESET;
  784. } else {
  785. /*
  786. * We didn't do PE reset for the case. The PE
  787. * is still in frozen state. Clear it before
  788. * resuming the PE.
  789. */
  790. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  791. result = PCI_ERS_RESULT_RECOVERED;
  792. }
  793. }
  794. /* If any device called out for a reset, then reset the slot */
  795. if (result == PCI_ERS_RESULT_NEED_RESET) {
  796. pr_info("EEH: Reset without hotplug activity\n");
  797. rc = eeh_reset_device(pe, bus, &rmv_data, true);
  798. if (rc) {
  799. pr_warn("%s: Cannot reset, err=%d\n",
  800. __func__, rc);
  801. result = PCI_ERS_RESULT_DISCONNECT;
  802. } else {
  803. result = PCI_ERS_RESULT_NONE;
  804. eeh_set_channel_state(pe, pci_channel_io_normal);
  805. eeh_set_irq_state(pe, true);
  806. eeh_pe_report("slot_reset", pe, eeh_report_reset,
  807. &result);
  808. }
  809. }
  810. if ((result == PCI_ERS_RESULT_RECOVERED) ||
  811. (result == PCI_ERS_RESULT_NONE)) {
  812. /*
  813. * For those hot removed VFs, we should add back them after PF
  814. * get recovered properly.
  815. */
  816. list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list,
  817. rmv_entry) {
  818. eeh_add_virt_device(edev);
  819. list_del(&edev->rmv_entry);
  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. } else {
  834. /*
  835. * About 90% of all real-life EEH failures in the field
  836. * are due to poorly seated PCI cards. Only 10% or so are
  837. * due to actual, failed cards.
  838. */
  839. pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
  840. "Please try reseating or replacing it\n",
  841. pe->phb->global_number, pe->addr);
  842. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  843. /* Notify all devices that they're about to go down. */
  844. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  845. eeh_set_irq_state(pe, false);
  846. eeh_pe_report("error_detected(permanent failure)", pe,
  847. eeh_report_failure, NULL);
  848. /* Mark the PE to be removed permanently */
  849. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  850. /*
  851. * Shut down the device drivers for good. We mark
  852. * all removed devices correctly to avoid access
  853. * the their PCI config any more.
  854. */
  855. if (pe->type & EEH_PE_VF) {
  856. eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
  857. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  858. } else {
  859. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  860. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  861. pci_lock_rescan_remove();
  862. pci_hp_remove_devices(bus);
  863. pci_unlock_rescan_remove();
  864. /* The passed PE should no longer be used */
  865. return;
  866. }
  867. }
  868. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  869. }
  870. /**
  871. * eeh_handle_special_event - Handle EEH events without a specific failing PE
  872. *
  873. * Called when an EEH event is detected but can't be narrowed down to a
  874. * specific PE. Iterates through possible failures and handles them as
  875. * necessary.
  876. */
  877. void eeh_handle_special_event(void)
  878. {
  879. struct eeh_pe *pe, *phb_pe;
  880. struct pci_bus *bus;
  881. struct pci_controller *hose;
  882. unsigned long flags;
  883. int rc;
  884. do {
  885. rc = eeh_ops->next_error(&pe);
  886. switch (rc) {
  887. case EEH_NEXT_ERR_DEAD_IOC:
  888. /* Mark all PHBs in dead state */
  889. eeh_serialize_lock(&flags);
  890. /* Purge all events */
  891. eeh_remove_event(NULL, true);
  892. list_for_each_entry(hose, &hose_list, list_node) {
  893. phb_pe = eeh_phb_pe_get(hose);
  894. if (!phb_pe) continue;
  895. eeh_pe_mark_isolated(phb_pe);
  896. }
  897. eeh_serialize_unlock(flags);
  898. break;
  899. case EEH_NEXT_ERR_FROZEN_PE:
  900. case EEH_NEXT_ERR_FENCED_PHB:
  901. case EEH_NEXT_ERR_DEAD_PHB:
  902. /* Mark the PE in fenced state */
  903. eeh_serialize_lock(&flags);
  904. /* Purge all events of the PHB */
  905. eeh_remove_event(pe, true);
  906. if (rc != EEH_NEXT_ERR_DEAD_PHB)
  907. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  908. eeh_pe_mark_isolated(pe);
  909. eeh_serialize_unlock(flags);
  910. break;
  911. case EEH_NEXT_ERR_NONE:
  912. return;
  913. default:
  914. pr_warn("%s: Invalid value %d from next_error()\n",
  915. __func__, rc);
  916. return;
  917. }
  918. /*
  919. * For fenced PHB and frozen PE, it's handled as normal
  920. * event. We have to remove the affected PHBs for dead
  921. * PHB and IOC
  922. */
  923. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  924. rc == EEH_NEXT_ERR_FENCED_PHB) {
  925. eeh_handle_normal_event(pe);
  926. } else {
  927. pci_lock_rescan_remove();
  928. list_for_each_entry(hose, &hose_list, list_node) {
  929. phb_pe = eeh_phb_pe_get(hose);
  930. if (!phb_pe ||
  931. !(phb_pe->state & EEH_PE_ISOLATED) ||
  932. (phb_pe->state & EEH_PE_RECOVERING))
  933. continue;
  934. /* Notify all devices to be down */
  935. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  936. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  937. eeh_pe_report(
  938. "error_detected(permanent failure)", pe,
  939. eeh_report_failure, NULL);
  940. bus = eeh_pe_bus_get(phb_pe);
  941. if (!bus) {
  942. pr_err("%s: Cannot find PCI bus for "
  943. "PHB#%x-PE#%x\n",
  944. __func__,
  945. pe->phb->global_number,
  946. pe->addr);
  947. break;
  948. }
  949. pci_hp_remove_devices(bus);
  950. }
  951. pci_unlock_rescan_remove();
  952. }
  953. /*
  954. * If we have detected dead IOC, we needn't proceed
  955. * any more since all PHBs would have been removed
  956. */
  957. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  958. break;
  959. } while (rc != EEH_NEXT_ERR_NONE);
  960. }