eeh_driver.c 28 KB

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