eeh_driver.c 28 KB

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