eeh_driver.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 = 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. return rc;
  574. /* Give the system 5 seconds to finish running the user-space
  575. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  576. * this is a hack, but if we don't do this, and try to bring
  577. * the device up before the scripts have taken it down,
  578. * potentially weird things happen.
  579. */
  580. if (bus) {
  581. pr_info("EEH: Sleep 5s ahead of complete hotplug\n");
  582. ssleep(5);
  583. /*
  584. * The EEH device is still connected with its parent
  585. * PE. We should disconnect it so the binding can be
  586. * rebuilt when adding PCI devices.
  587. */
  588. edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
  589. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  590. if (pe->type & EEH_PE_VF) {
  591. eeh_add_virt_device(edev, NULL);
  592. } else {
  593. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  594. pci_hp_add_devices(bus);
  595. }
  596. } else if (frozen_bus && rmv_data->removed) {
  597. pr_info("EEH: Sleep 5s ahead of partial hotplug\n");
  598. ssleep(5);
  599. edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
  600. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  601. if (pe->type & EEH_PE_VF)
  602. eeh_add_virt_device(edev, NULL);
  603. else
  604. pci_hp_add_devices(frozen_bus);
  605. }
  606. eeh_pe_state_clear(pe, EEH_PE_KEEP);
  607. pe->tstamp = tstamp;
  608. pe->freeze_count = cnt;
  609. pci_unlock_rescan_remove();
  610. return 0;
  611. }
  612. /* The longest amount of time to wait for a pci device
  613. * to come back on line, in seconds.
  614. */
  615. #define MAX_WAIT_FOR_RECOVERY 300
  616. static void eeh_handle_normal_event(struct eeh_pe *pe)
  617. {
  618. struct pci_bus *frozen_bus;
  619. struct eeh_dev *edev, *tmp;
  620. int rc = 0;
  621. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  622. struct eeh_rmv_data rmv_data = {LIST_HEAD_INIT(rmv_data.edev_list), 0};
  623. frozen_bus = eeh_pe_bus_get(pe);
  624. if (!frozen_bus) {
  625. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  626. __func__, pe->phb->global_number, pe->addr);
  627. return;
  628. }
  629. eeh_pe_update_time_stamp(pe);
  630. pe->freeze_count++;
  631. if (pe->freeze_count > eeh_max_freezes)
  632. goto excess_failures;
  633. pr_warn("EEH: This PCI device has failed %d times in the last hour\n",
  634. pe->freeze_count);
  635. /* Walk the various device drivers attached to this slot through
  636. * a reset sequence, giving each an opportunity to do what it needs
  637. * to accomplish the reset. Each child gets a report of the
  638. * status ... if any child can't handle the reset, then the entire
  639. * slot is dlpar removed and added.
  640. *
  641. * When the PHB is fenced, we have to issue a reset to recover from
  642. * the error. Override the result if necessary to have partially
  643. * hotplug for this case.
  644. */
  645. pr_info("EEH: Notify device drivers to shutdown\n");
  646. eeh_pe_dev_traverse(pe, eeh_report_error, &result);
  647. if ((pe->type & EEH_PE_PHB) &&
  648. result != PCI_ERS_RESULT_NONE &&
  649. result != PCI_ERS_RESULT_NEED_RESET)
  650. result = PCI_ERS_RESULT_NEED_RESET;
  651. /* Get the current PCI slot state. This can take a long time,
  652. * sometimes over 300 seconds for certain systems.
  653. */
  654. rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
  655. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  656. pr_warn("EEH: Permanent failure\n");
  657. goto hard_fail;
  658. }
  659. /* Since rtas may enable MMIO when posting the error log,
  660. * don't post the error log until after all dev drivers
  661. * have been informed.
  662. */
  663. pr_info("EEH: Collect temporary log\n");
  664. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  665. /* If all device drivers were EEH-unaware, then shut
  666. * down all of the device drivers, and hope they
  667. * go down willingly, without panicing the system.
  668. */
  669. if (result == PCI_ERS_RESULT_NONE) {
  670. pr_info("EEH: Reset with hotplug activity\n");
  671. rc = eeh_reset_device(pe, frozen_bus, NULL);
  672. if (rc) {
  673. pr_warn("%s: Unable to reset, err=%d\n",
  674. __func__, rc);
  675. goto hard_fail;
  676. }
  677. }
  678. /* If all devices reported they can proceed, then re-enable MMIO */
  679. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  680. pr_info("EEH: Enable I/O for affected devices\n");
  681. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  682. if (rc < 0)
  683. goto hard_fail;
  684. if (rc) {
  685. result = PCI_ERS_RESULT_NEED_RESET;
  686. } else {
  687. pr_info("EEH: Notify device drivers to resume I/O\n");
  688. eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
  689. }
  690. }
  691. /* If all devices reported they can proceed, then re-enable DMA */
  692. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  693. pr_info("EEH: Enabled DMA for affected devices\n");
  694. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  695. if (rc < 0)
  696. goto hard_fail;
  697. if (rc) {
  698. result = PCI_ERS_RESULT_NEED_RESET;
  699. } else {
  700. /*
  701. * We didn't do PE reset for the case. The PE
  702. * is still in frozen state. Clear it before
  703. * resuming the PE.
  704. */
  705. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  706. result = PCI_ERS_RESULT_RECOVERED;
  707. }
  708. }
  709. /* If any device has a hard failure, then shut off everything. */
  710. if (result == PCI_ERS_RESULT_DISCONNECT) {
  711. pr_warn("EEH: Device driver gave up\n");
  712. goto hard_fail;
  713. }
  714. /* If any device called out for a reset, then reset the slot */
  715. if (result == PCI_ERS_RESULT_NEED_RESET) {
  716. pr_info("EEH: Reset without hotplug activity\n");
  717. rc = eeh_reset_device(pe, NULL, &rmv_data);
  718. if (rc) {
  719. pr_warn("%s: Cannot reset, err=%d\n",
  720. __func__, rc);
  721. goto hard_fail;
  722. }
  723. pr_info("EEH: Notify device drivers "
  724. "the completion of reset\n");
  725. result = PCI_ERS_RESULT_NONE;
  726. eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
  727. }
  728. /* All devices should claim they have recovered by now. */
  729. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  730. (result != PCI_ERS_RESULT_NONE)) {
  731. pr_warn("EEH: Not recovered\n");
  732. goto hard_fail;
  733. }
  734. /*
  735. * For those hot removed VFs, we should add back them after PF get
  736. * recovered properly.
  737. */
  738. list_for_each_entry_safe(edev, tmp, &rmv_data.edev_list, rmv_list) {
  739. eeh_add_virt_device(edev, NULL);
  740. list_del(&edev->rmv_list);
  741. }
  742. /* Tell all device drivers that they can resume operations */
  743. pr_info("EEH: Notify device driver to resume\n");
  744. eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
  745. return;
  746. excess_failures:
  747. /*
  748. * About 90% of all real-life EEH failures in the field
  749. * are due to poorly seated PCI cards. Only 10% or so are
  750. * due to actual, failed cards.
  751. */
  752. pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
  753. "last hour and has been permanently disabled.\n"
  754. "Please try reseating or replacing it.\n",
  755. pe->phb->global_number, pe->addr,
  756. pe->freeze_count);
  757. goto perm_error;
  758. hard_fail:
  759. pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
  760. "Please try reseating or replacing it\n",
  761. pe->phb->global_number, pe->addr);
  762. perm_error:
  763. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  764. /* Notify all devices that they're about to go down. */
  765. eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
  766. /* Mark the PE to be removed permanently */
  767. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  768. /*
  769. * Shut down the device drivers for good. We mark
  770. * all removed devices correctly to avoid access
  771. * the their PCI config any more.
  772. */
  773. if (frozen_bus) {
  774. if (pe->type & EEH_PE_VF) {
  775. eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
  776. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  777. } else {
  778. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  779. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  780. pci_lock_rescan_remove();
  781. pci_hp_remove_devices(frozen_bus);
  782. pci_unlock_rescan_remove();
  783. }
  784. }
  785. }
  786. static void eeh_handle_special_event(void)
  787. {
  788. struct eeh_pe *pe, *phb_pe;
  789. struct pci_bus *bus;
  790. struct pci_controller *hose;
  791. unsigned long flags;
  792. int rc;
  793. do {
  794. rc = eeh_ops->next_error(&pe);
  795. switch (rc) {
  796. case EEH_NEXT_ERR_DEAD_IOC:
  797. /* Mark all PHBs in dead state */
  798. eeh_serialize_lock(&flags);
  799. /* Purge all events */
  800. eeh_remove_event(NULL, true);
  801. list_for_each_entry(hose, &hose_list, list_node) {
  802. phb_pe = eeh_phb_pe_get(hose);
  803. if (!phb_pe) continue;
  804. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  805. }
  806. eeh_serialize_unlock(flags);
  807. break;
  808. case EEH_NEXT_ERR_FROZEN_PE:
  809. case EEH_NEXT_ERR_FENCED_PHB:
  810. case EEH_NEXT_ERR_DEAD_PHB:
  811. /* Mark the PE in fenced state */
  812. eeh_serialize_lock(&flags);
  813. /* Purge all events of the PHB */
  814. eeh_remove_event(pe, true);
  815. if (rc == EEH_NEXT_ERR_DEAD_PHB)
  816. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  817. else
  818. eeh_pe_state_mark(pe,
  819. EEH_PE_ISOLATED | EEH_PE_RECOVERING);
  820. eeh_serialize_unlock(flags);
  821. break;
  822. case EEH_NEXT_ERR_NONE:
  823. return;
  824. default:
  825. pr_warn("%s: Invalid value %d from next_error()\n",
  826. __func__, rc);
  827. return;
  828. }
  829. /*
  830. * For fenced PHB and frozen PE, it's handled as normal
  831. * event. We have to remove the affected PHBs for dead
  832. * PHB and IOC
  833. */
  834. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  835. rc == EEH_NEXT_ERR_FENCED_PHB) {
  836. eeh_handle_normal_event(pe);
  837. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  838. } else {
  839. pci_lock_rescan_remove();
  840. list_for_each_entry(hose, &hose_list, list_node) {
  841. phb_pe = eeh_phb_pe_get(hose);
  842. if (!phb_pe ||
  843. !(phb_pe->state & EEH_PE_ISOLATED) ||
  844. (phb_pe->state & EEH_PE_RECOVERING))
  845. continue;
  846. /* Notify all devices to be down */
  847. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  848. eeh_pe_dev_traverse(pe,
  849. eeh_report_failure, NULL);
  850. bus = eeh_pe_bus_get(phb_pe);
  851. if (!bus) {
  852. pr_err("%s: Cannot find PCI bus for "
  853. "PHB#%d-PE#%x\n",
  854. __func__,
  855. pe->phb->global_number,
  856. pe->addr);
  857. break;
  858. }
  859. pci_hp_remove_devices(bus);
  860. }
  861. pci_unlock_rescan_remove();
  862. }
  863. /*
  864. * If we have detected dead IOC, we needn't proceed
  865. * any more since all PHBs would have been removed
  866. */
  867. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  868. break;
  869. } while (rc != EEH_NEXT_ERR_NONE);
  870. }
  871. /**
  872. * eeh_handle_event - Reset a PCI device after hard lockup.
  873. * @pe: EEH PE
  874. *
  875. * While PHB detects address or data parity errors on particular PCI
  876. * slot, the associated PE will be frozen. Besides, DMA's occurring
  877. * to wild addresses (which usually happen due to bugs in device
  878. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  879. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  880. *
  881. * Recovery process consists of unplugging the device driver (which
  882. * generated hotplug events to userspace), then issuing a PCI #RST to
  883. * the device, then reconfiguring the PCI config space for all bridges
  884. * & devices under this slot, and then finally restarting the device
  885. * drivers (which cause a second set of hotplug events to go out to
  886. * userspace).
  887. */
  888. void eeh_handle_event(struct eeh_pe *pe)
  889. {
  890. if (pe)
  891. eeh_handle_normal_event(pe);
  892. else
  893. eeh_handle_special_event();
  894. }