eeh_driver.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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_pe_reset_full(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_pe_reset_full(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 void 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#%x-PE#%x\n",
  628. __func__, pe->phb->global_number, pe->addr);
  629. return;
  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;
  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#%x-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#%x-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. }
  786. }
  787. }
  788. static void eeh_handle_special_event(void)
  789. {
  790. struct eeh_pe *pe, *phb_pe;
  791. struct pci_bus *bus;
  792. struct pci_controller *hose;
  793. unsigned long flags;
  794. int rc;
  795. do {
  796. rc = eeh_ops->next_error(&pe);
  797. switch (rc) {
  798. case EEH_NEXT_ERR_DEAD_IOC:
  799. /* Mark all PHBs in dead state */
  800. eeh_serialize_lock(&flags);
  801. /* Purge all events */
  802. eeh_remove_event(NULL, true);
  803. list_for_each_entry(hose, &hose_list, list_node) {
  804. phb_pe = eeh_phb_pe_get(hose);
  805. if (!phb_pe) continue;
  806. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  807. }
  808. eeh_serialize_unlock(flags);
  809. break;
  810. case EEH_NEXT_ERR_FROZEN_PE:
  811. case EEH_NEXT_ERR_FENCED_PHB:
  812. case EEH_NEXT_ERR_DEAD_PHB:
  813. /* Mark the PE in fenced state */
  814. eeh_serialize_lock(&flags);
  815. /* Purge all events of the PHB */
  816. eeh_remove_event(pe, true);
  817. if (rc == EEH_NEXT_ERR_DEAD_PHB)
  818. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  819. else
  820. eeh_pe_state_mark(pe,
  821. EEH_PE_ISOLATED | EEH_PE_RECOVERING);
  822. eeh_serialize_unlock(flags);
  823. break;
  824. case EEH_NEXT_ERR_NONE:
  825. return;
  826. default:
  827. pr_warn("%s: Invalid value %d from next_error()\n",
  828. __func__, rc);
  829. return;
  830. }
  831. /*
  832. * For fenced PHB and frozen PE, it's handled as normal
  833. * event. We have to remove the affected PHBs for dead
  834. * PHB and IOC
  835. */
  836. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  837. rc == EEH_NEXT_ERR_FENCED_PHB) {
  838. eeh_handle_normal_event(pe);
  839. eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
  840. } else {
  841. pci_lock_rescan_remove();
  842. list_for_each_entry(hose, &hose_list, list_node) {
  843. phb_pe = eeh_phb_pe_get(hose);
  844. if (!phb_pe ||
  845. !(phb_pe->state & EEH_PE_ISOLATED) ||
  846. (phb_pe->state & EEH_PE_RECOVERING))
  847. continue;
  848. /* Notify all devices to be down */
  849. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
  850. eeh_pe_dev_traverse(pe,
  851. eeh_report_failure, NULL);
  852. bus = eeh_pe_bus_get(phb_pe);
  853. if (!bus) {
  854. pr_err("%s: Cannot find PCI bus for "
  855. "PHB#%x-PE#%x\n",
  856. __func__,
  857. pe->phb->global_number,
  858. pe->addr);
  859. break;
  860. }
  861. pci_hp_remove_devices(bus);
  862. }
  863. pci_unlock_rescan_remove();
  864. }
  865. /*
  866. * If we have detected dead IOC, we needn't proceed
  867. * any more since all PHBs would have been removed
  868. */
  869. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  870. break;
  871. } while (rc != EEH_NEXT_ERR_NONE);
  872. }
  873. /**
  874. * eeh_handle_event - Reset a PCI device after hard lockup.
  875. * @pe: EEH PE
  876. *
  877. * While PHB detects address or data parity errors on particular PCI
  878. * slot, the associated PE will be frozen. Besides, DMA's occurring
  879. * to wild addresses (which usually happen due to bugs in device
  880. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  881. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  882. *
  883. * Recovery process consists of unplugging the device driver (which
  884. * generated hotplug events to userspace), then issuing a PCI #RST to
  885. * the device, then reconfiguring the PCI config space for all bridges
  886. * & devices under this slot, and then finally restarting the device
  887. * drivers (which cause a second set of hotplug events to go out to
  888. * userspace).
  889. */
  890. void eeh_handle_event(struct eeh_pe *pe)
  891. {
  892. if (pe)
  893. eeh_handle_normal_event(pe);
  894. else
  895. eeh_handle_special_event();
  896. }