eeh_driver.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
  4. * Copyright IBM Corp. 2004 2005
  5. * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
  6. *
  7. * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci_hotplug.h>
  15. #include <asm/eeh.h>
  16. #include <asm/eeh_event.h>
  17. #include <asm/ppc-pci.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/rtas.h>
  20. struct eeh_rmv_data {
  21. struct list_head removed_vf_list;
  22. int removed_dev_count;
  23. };
  24. static int eeh_result_priority(enum pci_ers_result result)
  25. {
  26. switch (result) {
  27. case PCI_ERS_RESULT_NONE:
  28. return 1;
  29. case PCI_ERS_RESULT_NO_AER_DRIVER:
  30. return 2;
  31. case PCI_ERS_RESULT_RECOVERED:
  32. return 3;
  33. case PCI_ERS_RESULT_CAN_RECOVER:
  34. return 4;
  35. case PCI_ERS_RESULT_DISCONNECT:
  36. return 5;
  37. case PCI_ERS_RESULT_NEED_RESET:
  38. return 6;
  39. default:
  40. WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", result);
  41. return 0;
  42. }
  43. };
  44. static const char *pci_ers_result_name(enum pci_ers_result result)
  45. {
  46. switch (result) {
  47. case PCI_ERS_RESULT_NONE:
  48. return "none";
  49. case PCI_ERS_RESULT_CAN_RECOVER:
  50. return "can recover";
  51. case PCI_ERS_RESULT_NEED_RESET:
  52. return "need reset";
  53. case PCI_ERS_RESULT_DISCONNECT:
  54. return "disconnect";
  55. case PCI_ERS_RESULT_RECOVERED:
  56. return "recovered";
  57. case PCI_ERS_RESULT_NO_AER_DRIVER:
  58. return "no AER driver";
  59. default:
  60. WARN_ONCE(1, "Unknown result type: %d\n", result);
  61. return "unknown";
  62. }
  63. };
  64. static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old,
  65. enum pci_ers_result new)
  66. {
  67. if (eeh_result_priority(new) > eeh_result_priority(old))
  68. return new;
  69. return old;
  70. }
  71. static bool eeh_dev_removed(struct eeh_dev *edev)
  72. {
  73. return !edev || (edev->mode & EEH_DEV_REMOVED);
  74. }
  75. static bool eeh_edev_actionable(struct eeh_dev *edev)
  76. {
  77. if (!edev->pdev)
  78. return false;
  79. if (edev->pdev->error_state == pci_channel_io_perm_failure)
  80. return false;
  81. if (eeh_dev_removed(edev))
  82. return false;
  83. if (eeh_pe_passed(edev->pe))
  84. return false;
  85. return true;
  86. }
  87. /**
  88. * eeh_pcid_get - Get the PCI device driver
  89. * @pdev: PCI device
  90. *
  91. * The function is used to retrieve the PCI device driver for
  92. * the indicated PCI device. Besides, we will increase the reference
  93. * of the PCI device driver to prevent that being unloaded on
  94. * the fly. Otherwise, kernel crash would be seen.
  95. */
  96. static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
  97. {
  98. if (!pdev || !pdev->dev.driver)
  99. return NULL;
  100. if (!try_module_get(pdev->dev.driver->owner))
  101. return NULL;
  102. return to_pci_driver(pdev->dev.driver);
  103. }
  104. /**
  105. * eeh_pcid_put - Dereference on the PCI device driver
  106. * @pdev: PCI device
  107. *
  108. * The function is called to do dereference on the PCI device
  109. * driver of the indicated PCI device.
  110. */
  111. static inline void eeh_pcid_put(struct pci_dev *pdev)
  112. {
  113. if (!pdev || !pdev->dev.driver)
  114. return;
  115. module_put(pdev->dev.driver->owner);
  116. }
  117. /**
  118. * eeh_disable_irq - Disable interrupt for the recovering device
  119. * @dev: PCI device
  120. *
  121. * This routine must be called when reporting temporary or permanent
  122. * error to the particular PCI device to disable interrupt of that
  123. * device. If the device has enabled MSI or MSI-X interrupt, we needn't
  124. * do real work because EEH should freeze DMA transfers for those PCI
  125. * devices encountering EEH errors, which includes MSI or MSI-X.
  126. */
  127. static void eeh_disable_irq(struct eeh_dev *edev)
  128. {
  129. /* Don't disable MSI and MSI-X interrupts. They are
  130. * effectively disabled by the DMA Stopped state
  131. * when an EEH error occurs.
  132. */
  133. if (edev->pdev->msi_enabled || edev->pdev->msix_enabled)
  134. return;
  135. if (!irq_has_action(edev->pdev->irq))
  136. return;
  137. edev->mode |= EEH_DEV_IRQ_DISABLED;
  138. disable_irq_nosync(edev->pdev->irq);
  139. }
  140. /**
  141. * eeh_enable_irq - Enable interrupt for the recovering device
  142. * @dev: PCI device
  143. *
  144. * This routine must be called to enable interrupt while failed
  145. * device could be resumed.
  146. */
  147. static void eeh_enable_irq(struct eeh_dev *edev)
  148. {
  149. if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
  150. edev->mode &= ~EEH_DEV_IRQ_DISABLED;
  151. /*
  152. * FIXME !!!!!
  153. *
  154. * This is just ass backwards. This maze has
  155. * unbalanced irq_enable/disable calls. So instead of
  156. * finding the root cause it works around the warning
  157. * in the irq_enable code by conditionally calling
  158. * into it.
  159. *
  160. * That's just wrong.The warning in the core code is
  161. * there to tell people to fix their asymmetries in
  162. * their own code, not by abusing the core information
  163. * to avoid it.
  164. *
  165. * I so wish that the assymetry would be the other way
  166. * round and a few more irq_disable calls render that
  167. * shit unusable forever.
  168. *
  169. * tglx
  170. */
  171. if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq)))
  172. enable_irq(edev->pdev->irq);
  173. }
  174. }
  175. static void eeh_dev_save_state(struct eeh_dev *edev, void *userdata)
  176. {
  177. struct pci_dev *pdev;
  178. if (!edev)
  179. return;
  180. /*
  181. * We cannot access the config space on some adapters.
  182. * Otherwise, it will cause fenced PHB. We don't save
  183. * the content in their config space and will restore
  184. * from the initial config space saved when the EEH
  185. * device is created.
  186. */
  187. if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
  188. return;
  189. pdev = eeh_dev_to_pci_dev(edev);
  190. if (!pdev)
  191. return;
  192. pci_save_state(pdev);
  193. }
  194. static void eeh_set_channel_state(struct eeh_pe *root, pci_channel_state_t s)
  195. {
  196. struct eeh_pe *pe;
  197. struct eeh_dev *edev, *tmp;
  198. eeh_for_each_pe(root, pe)
  199. eeh_pe_for_each_dev(pe, edev, tmp)
  200. if (eeh_edev_actionable(edev))
  201. edev->pdev->error_state = s;
  202. }
  203. static void eeh_set_irq_state(struct eeh_pe *root, bool enable)
  204. {
  205. struct eeh_pe *pe;
  206. struct eeh_dev *edev, *tmp;
  207. eeh_for_each_pe(root, pe) {
  208. eeh_pe_for_each_dev(pe, edev, tmp) {
  209. if (!eeh_edev_actionable(edev))
  210. continue;
  211. if (!eeh_pcid_get(edev->pdev))
  212. continue;
  213. if (enable)
  214. eeh_enable_irq(edev);
  215. else
  216. eeh_disable_irq(edev);
  217. eeh_pcid_put(edev->pdev);
  218. }
  219. }
  220. }
  221. typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *,
  222. struct pci_dev *,
  223. struct pci_driver *);
  224. static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn,
  225. enum pci_ers_result *result)
  226. {
  227. struct pci_dev *pdev;
  228. struct pci_driver *driver;
  229. enum pci_ers_result new_result;
  230. pdev = edev->pdev;
  231. if (pdev)
  232. get_device(&pdev->dev);
  233. if (!pdev) {
  234. eeh_edev_info(edev, "no device");
  235. *result = PCI_ERS_RESULT_DISCONNECT;
  236. return;
  237. }
  238. device_lock(&pdev->dev);
  239. if (eeh_edev_actionable(edev)) {
  240. driver = eeh_pcid_get(pdev);
  241. if (!driver)
  242. eeh_edev_info(edev, "no driver");
  243. else if (!driver->err_handler)
  244. eeh_edev_info(edev, "driver not EEH aware");
  245. else if (edev->mode & EEH_DEV_NO_HANDLER)
  246. eeh_edev_info(edev, "driver bound too late");
  247. else {
  248. new_result = fn(edev, pdev, driver);
  249. eeh_edev_info(edev, "%s driver reports: '%s'",
  250. driver->name,
  251. pci_ers_result_name(new_result));
  252. if (result)
  253. *result = pci_ers_merge_result(*result,
  254. new_result);
  255. }
  256. if (driver)
  257. eeh_pcid_put(pdev);
  258. } else {
  259. eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!pdev,
  260. !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
  261. }
  262. device_unlock(&pdev->dev);
  263. if (edev->pdev != pdev)
  264. eeh_edev_warn(edev, "Device changed during processing!\n");
  265. put_device(&pdev->dev);
  266. }
  267. static void eeh_pe_report(const char *name, struct eeh_pe *root,
  268. eeh_report_fn fn, enum pci_ers_result *result)
  269. {
  270. struct eeh_pe *pe;
  271. struct eeh_dev *edev, *tmp;
  272. pr_info("EEH: Beginning: '%s'\n", name);
  273. eeh_for_each_pe(root, pe)
  274. eeh_pe_for_each_dev(pe, edev, tmp)
  275. eeh_pe_report_edev(edev, fn, result);
  276. if (result)
  277. pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
  278. name, pci_ers_result_name(*result));
  279. else
  280. pr_info("EEH: Finished:'%s'", name);
  281. }
  282. /**
  283. * eeh_report_error - Report pci error to each device driver
  284. * @edev: eeh device
  285. * @driver: device's PCI driver
  286. *
  287. * Report an EEH error to each device driver.
  288. */
  289. static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
  290. struct pci_dev *pdev,
  291. struct pci_driver *driver)
  292. {
  293. enum pci_ers_result rc;
  294. if (!driver->err_handler->error_detected)
  295. return PCI_ERS_RESULT_NONE;
  296. eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
  297. driver->name);
  298. rc = driver->err_handler->error_detected(pdev, pci_channel_io_frozen);
  299. edev->in_error = true;
  300. pci_uevent_ers(pdev, PCI_ERS_RESULT_NONE);
  301. return rc;
  302. }
  303. /**
  304. * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
  305. * @edev: eeh device
  306. * @driver: device's PCI driver
  307. *
  308. * Tells each device driver that IO ports, MMIO and config space I/O
  309. * are now enabled.
  310. */
  311. static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
  312. struct pci_dev *pdev,
  313. struct pci_driver *driver)
  314. {
  315. if (!driver->err_handler->mmio_enabled)
  316. return PCI_ERS_RESULT_NONE;
  317. eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
  318. return driver->err_handler->mmio_enabled(pdev);
  319. }
  320. /**
  321. * eeh_report_reset - Tell device that slot has been reset
  322. * @edev: eeh device
  323. * @driver: device's PCI driver
  324. *
  325. * This routine must be called while EEH tries to reset particular
  326. * PCI device so that the associated PCI device driver could take
  327. * some actions, usually to save data the driver needs so that the
  328. * driver can work again while the device is recovered.
  329. */
  330. static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
  331. struct pci_dev *pdev,
  332. struct pci_driver *driver)
  333. {
  334. if (!driver->err_handler->slot_reset || !edev->in_error)
  335. return PCI_ERS_RESULT_NONE;
  336. eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
  337. return driver->err_handler->slot_reset(pdev);
  338. }
  339. static void eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
  340. {
  341. struct pci_dev *pdev;
  342. if (!edev)
  343. return;
  344. pci_lock_rescan_remove();
  345. /*
  346. * The content in the config space isn't saved because
  347. * the blocked config space on some adapters. We have
  348. * to restore the initial saved config space when the
  349. * EEH device is created.
  350. */
  351. if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
  352. if (list_is_last(&edev->entry, &edev->pe->edevs))
  353. eeh_pe_restore_bars(edev->pe);
  354. pci_unlock_rescan_remove();
  355. return;
  356. }
  357. pdev = eeh_dev_to_pci_dev(edev);
  358. if (!pdev) {
  359. pci_unlock_rescan_remove();
  360. return;
  361. }
  362. pci_restore_state(pdev);
  363. pci_unlock_rescan_remove();
  364. }
  365. /**
  366. * eeh_report_resume - Tell device to resume normal operations
  367. * @edev: eeh device
  368. * @driver: device's PCI driver
  369. *
  370. * This routine must be called to notify the device driver that it
  371. * could resume so that the device driver can do some initialization
  372. * to make the recovered device work again.
  373. */
  374. static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
  375. struct pci_dev *pdev,
  376. struct pci_driver *driver)
  377. {
  378. if (!driver->err_handler->resume || !edev->in_error)
  379. return PCI_ERS_RESULT_NONE;
  380. eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
  381. driver->err_handler->resume(pdev);
  382. pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
  383. #ifdef CONFIG_PCI_IOV
  384. if (eeh_ops->notify_resume)
  385. eeh_ops->notify_resume(edev);
  386. #endif
  387. return PCI_ERS_RESULT_NONE;
  388. }
  389. /**
  390. * eeh_report_failure - Tell device driver that device is dead.
  391. * @edev: eeh device
  392. * @driver: device's PCI driver
  393. *
  394. * This informs the device driver that the device is permanently
  395. * dead, and that no further recovery attempts will be made on it.
  396. */
  397. static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
  398. struct pci_dev *pdev,
  399. struct pci_driver *driver)
  400. {
  401. enum pci_ers_result rc;
  402. if (!driver->err_handler->error_detected)
  403. return PCI_ERS_RESULT_NONE;
  404. eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
  405. driver->name);
  406. rc = driver->err_handler->error_detected(pdev,
  407. pci_channel_io_perm_failure);
  408. pci_uevent_ers(pdev, PCI_ERS_RESULT_DISCONNECT);
  409. return rc;
  410. }
  411. static void *eeh_add_virt_device(struct eeh_dev *edev)
  412. {
  413. struct pci_driver *driver;
  414. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  415. if (!(edev->physfn)) {
  416. eeh_edev_warn(edev, "Not for VF\n");
  417. return NULL;
  418. }
  419. driver = eeh_pcid_get(dev);
  420. if (driver) {
  421. if (driver->err_handler) {
  422. eeh_pcid_put(dev);
  423. return NULL;
  424. }
  425. eeh_pcid_put(dev);
  426. }
  427. #ifdef CONFIG_PCI_IOV
  428. pci_iov_add_virtfn(edev->physfn, edev->vf_index);
  429. #endif
  430. return NULL;
  431. }
  432. static void eeh_rmv_device(struct eeh_dev *edev, void *userdata)
  433. {
  434. struct pci_driver *driver;
  435. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  436. struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
  437. /*
  438. * Actually, we should remove the PCI bridges as well.
  439. * However, that's lots of complexity to do that,
  440. * particularly some of devices under the bridge might
  441. * support EEH. So we just care about PCI devices for
  442. * simplicity here.
  443. */
  444. if (!eeh_edev_actionable(edev) ||
  445. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
  446. return;
  447. if (rmv_data) {
  448. driver = eeh_pcid_get(dev);
  449. if (driver) {
  450. if (driver->err_handler &&
  451. driver->err_handler->error_detected &&
  452. driver->err_handler->slot_reset) {
  453. eeh_pcid_put(dev);
  454. return;
  455. }
  456. eeh_pcid_put(dev);
  457. }
  458. }
  459. /* Remove it from PCI subsystem */
  460. pr_info("EEH: Removing %s without EEH sensitive driver\n",
  461. pci_name(dev));
  462. edev->mode |= EEH_DEV_DISCONNECTED;
  463. if (rmv_data)
  464. rmv_data->removed_dev_count++;
  465. if (edev->physfn) {
  466. #ifdef CONFIG_PCI_IOV
  467. pci_iov_remove_virtfn(edev->physfn, edev->vf_index);
  468. edev->pdev = NULL;
  469. #endif
  470. if (rmv_data)
  471. list_add(&edev->rmv_entry, &rmv_data->removed_vf_list);
  472. } else {
  473. pci_lock_rescan_remove();
  474. pci_stop_and_remove_bus_device(dev);
  475. pci_unlock_rescan_remove();
  476. }
  477. }
  478. static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
  479. {
  480. struct eeh_dev *edev, *tmp;
  481. eeh_pe_for_each_dev(pe, edev, tmp) {
  482. if (!(edev->mode & EEH_DEV_DISCONNECTED))
  483. continue;
  484. edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
  485. eeh_pe_tree_remove(edev);
  486. }
  487. return NULL;
  488. }
  489. /*
  490. * Explicitly clear PE's frozen state for PowerNV where
  491. * we have frozen PE until BAR restore is completed. It's
  492. * harmless to clear it for pSeries. To be consistent with
  493. * PE reset (for 3 times), we try to clear the frozen state
  494. * for 3 times as well.
  495. */
  496. static int eeh_clear_pe_frozen_state(struct eeh_pe *root, bool include_passed)
  497. {
  498. struct eeh_pe *pe;
  499. int i;
  500. eeh_for_each_pe(root, pe) {
  501. if (include_passed || !eeh_pe_passed(pe)) {
  502. for (i = 0; i < 3; i++)
  503. if (!eeh_unfreeze_pe(pe))
  504. break;
  505. if (i >= 3)
  506. return -EIO;
  507. }
  508. }
  509. eeh_pe_state_clear(root, EEH_PE_ISOLATED, include_passed);
  510. return 0;
  511. }
  512. int eeh_pe_reset_and_recover(struct eeh_pe *pe)
  513. {
  514. int ret;
  515. /* Bail if the PE is being recovered */
  516. if (pe->state & EEH_PE_RECOVERING)
  517. return 0;
  518. /* Put the PE into recovery mode */
  519. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  520. /* Save states */
  521. eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
  522. /* Issue reset */
  523. ret = eeh_pe_reset_full(pe, true);
  524. if (ret) {
  525. eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
  526. return ret;
  527. }
  528. /* Unfreeze the PE */
  529. ret = eeh_clear_pe_frozen_state(pe, true);
  530. if (ret) {
  531. eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
  532. return ret;
  533. }
  534. /* Restore device state */
  535. eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
  536. /* Clear recovery mode */
  537. eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
  538. return 0;
  539. }
  540. /**
  541. * eeh_reset_device - Perform actual reset of a pci slot
  542. * @driver_eeh_aware: Does the device's driver provide EEH support?
  543. * @pe: EEH PE
  544. * @bus: PCI bus corresponding to the isolcated slot
  545. * @rmv_data: Optional, list to record removed devices
  546. *
  547. * This routine must be called to do reset on the indicated PE.
  548. * During the reset, udev might be invoked because those affected
  549. * PCI devices will be removed and then added.
  550. */
  551. static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
  552. struct eeh_rmv_data *rmv_data,
  553. bool driver_eeh_aware)
  554. {
  555. time64_t tstamp;
  556. int cnt, rc;
  557. struct eeh_dev *edev;
  558. struct eeh_pe *tmp_pe;
  559. bool any_passed = false;
  560. eeh_for_each_pe(pe, tmp_pe)
  561. any_passed |= eeh_pe_passed(tmp_pe);
  562. /* pcibios will clear the counter; save the value */
  563. cnt = pe->freeze_count;
  564. tstamp = pe->tstamp;
  565. /*
  566. * We don't remove the corresponding PE instances because
  567. * we need the information afterwords. The attached EEH
  568. * devices are expected to be attached soon when calling
  569. * into pci_hp_add_devices().
  570. */
  571. eeh_pe_state_mark(pe, EEH_PE_KEEP);
  572. if (any_passed || driver_eeh_aware || (pe->type & EEH_PE_VF)) {
  573. eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
  574. } else {
  575. pci_hp_remove_devices(bus);
  576. }
  577. /*
  578. * Reset the pci controller. (Asserts RST#; resets config space).
  579. * Reconfigure bridges and devices. Don't try to bring the system
  580. * up if the reset failed for some reason.
  581. *
  582. * During the reset, it's very dangerous to have uncontrolled PCI
  583. * config accesses. So we prefer to block them. However, controlled
  584. * PCI config accesses initiated from EEH itself are allowed.
  585. */
  586. rc = eeh_pe_reset_full(pe, false);
  587. if (rc)
  588. return rc;
  589. /* Restore PE */
  590. eeh_ops->configure_bridge(pe);
  591. eeh_pe_restore_bars(pe);
  592. /* Clear frozen state */
  593. rc = eeh_clear_pe_frozen_state(pe, false);
  594. if (rc) {
  595. return rc;
  596. }
  597. /* Give the system 5 seconds to finish running the user-space
  598. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  599. * this is a hack, but if we don't do this, and try to bring
  600. * the device up before the scripts have taken it down,
  601. * potentially weird things happen.
  602. */
  603. if (!driver_eeh_aware || rmv_data->removed_dev_count) {
  604. pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
  605. (driver_eeh_aware ? "partial" : "complete"));
  606. ssleep(5);
  607. /*
  608. * The EEH device is still connected with its parent
  609. * PE. We should disconnect it so the binding can be
  610. * rebuilt when adding PCI devices.
  611. */
  612. edev = list_first_entry(&pe->edevs, struct eeh_dev, entry);
  613. eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
  614. if (pe->type & EEH_PE_VF) {
  615. eeh_add_virt_device(edev);
  616. } else {
  617. if (!driver_eeh_aware)
  618. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
  619. pci_hp_add_devices(bus);
  620. }
  621. }
  622. eeh_pe_state_clear(pe, EEH_PE_KEEP, true);
  623. pe->tstamp = tstamp;
  624. pe->freeze_count = cnt;
  625. return 0;
  626. }
  627. /* The longest amount of time to wait for a pci device
  628. * to come back on line, in seconds.
  629. */
  630. #define MAX_WAIT_FOR_RECOVERY 300
  631. /* Walks the PE tree after processing an event to remove any stale PEs.
  632. *
  633. * NB: This needs to be recursive to ensure the leaf PEs get removed
  634. * before their parents do. Although this is possible to do recursively
  635. * we don't since this is easier to read and we need to garantee
  636. * the leaf nodes will be handled first.
  637. */
  638. static void eeh_pe_cleanup(struct eeh_pe *pe)
  639. {
  640. struct eeh_pe *child_pe, *tmp;
  641. list_for_each_entry_safe(child_pe, tmp, &pe->child_list, child)
  642. eeh_pe_cleanup(child_pe);
  643. if (pe->state & EEH_PE_KEEP)
  644. return;
  645. if (!(pe->state & EEH_PE_INVALID))
  646. return;
  647. if (list_empty(&pe->edevs) && list_empty(&pe->child_list)) {
  648. list_del(&pe->child);
  649. kfree(pe);
  650. }
  651. }
  652. /**
  653. * eeh_check_slot_presence - Check if a device is still present in a slot
  654. * @pdev: pci_dev to check
  655. *
  656. * This function may return a false positive if we can't determine the slot's
  657. * presence state. This might happen for PCIe slots if the PE containing
  658. * the upstream bridge is also frozen, or the bridge is part of the same PE
  659. * as the device.
  660. *
  661. * This shouldn't happen often, but you might see it if you hotplug a PCIe
  662. * switch.
  663. */
  664. static bool eeh_slot_presence_check(struct pci_dev *pdev)
  665. {
  666. const struct hotplug_slot_ops *ops;
  667. struct pci_slot *slot;
  668. u8 state;
  669. int rc;
  670. if (!pdev)
  671. return false;
  672. if (pdev->error_state == pci_channel_io_perm_failure)
  673. return false;
  674. slot = pdev->slot;
  675. if (!slot || !slot->hotplug)
  676. return true;
  677. ops = slot->hotplug->ops;
  678. if (!ops || !ops->get_adapter_status)
  679. return true;
  680. /* set the attention indicator while we've got the slot ops */
  681. if (ops->set_attention_status)
  682. ops->set_attention_status(slot->hotplug, 1);
  683. rc = ops->get_adapter_status(slot->hotplug, &state);
  684. if (rc)
  685. return true;
  686. return !!state;
  687. }
  688. static void eeh_clear_slot_attention(struct pci_dev *pdev)
  689. {
  690. const struct hotplug_slot_ops *ops;
  691. struct pci_slot *slot;
  692. if (!pdev)
  693. return;
  694. if (pdev->error_state == pci_channel_io_perm_failure)
  695. return;
  696. slot = pdev->slot;
  697. if (!slot || !slot->hotplug)
  698. return;
  699. ops = slot->hotplug->ops;
  700. if (!ops || !ops->set_attention_status)
  701. return;
  702. ops->set_attention_status(slot->hotplug, 0);
  703. }
  704. /**
  705. * eeh_handle_normal_event - Handle EEH events on a specific PE
  706. * @pe: EEH PE - which should not be used after we return, as it may
  707. * have been invalidated.
  708. *
  709. * Attempts to recover the given PE. If recovery fails or the PE has failed
  710. * too many times, remove the PE.
  711. *
  712. * While PHB detects address or data parity errors on particular PCI
  713. * slot, the associated PE will be frozen. Besides, DMA's occurring
  714. * to wild addresses (which usually happen due to bugs in device
  715. * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
  716. * #PERR or other misc PCI-related errors also can trigger EEH errors.
  717. *
  718. * Recovery process consists of unplugging the device driver (which
  719. * generated hotplug events to userspace), then issuing a PCI #RST to
  720. * the device, then reconfiguring the PCI config space for all bridges
  721. * & devices under this slot, and then finally restarting the device
  722. * drivers (which cause a second set of hotplug events to go out to
  723. * userspace).
  724. */
  725. void eeh_handle_normal_event(struct eeh_pe *pe)
  726. {
  727. struct pci_bus *bus;
  728. struct eeh_dev *edev, *tmp;
  729. struct eeh_pe *tmp_pe;
  730. int rc = 0;
  731. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  732. struct eeh_rmv_data rmv_data =
  733. {LIST_HEAD_INIT(rmv_data.removed_vf_list), 0};
  734. int devices = 0;
  735. pci_lock_rescan_remove();
  736. bus = eeh_pe_bus_get(pe);
  737. if (!bus) {
  738. pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
  739. __func__, pe->phb->global_number, pe->addr);
  740. pci_unlock_rescan_remove();
  741. return;
  742. }
  743. /*
  744. * When devices are hot-removed we might get an EEH due to
  745. * a driver attempting to touch the MMIO space of a removed
  746. * device. In this case we don't have a device to recover
  747. * so suppress the event if we can't find any present devices.
  748. *
  749. * The hotplug driver should take care of tearing down the
  750. * device itself.
  751. */
  752. eeh_for_each_pe(pe, tmp_pe)
  753. eeh_pe_for_each_dev(tmp_pe, edev, tmp)
  754. if (eeh_slot_presence_check(edev->pdev))
  755. devices++;
  756. if (!devices) {
  757. pr_warn("EEH: Frozen PHB#%x-PE#%x is empty!\n",
  758. pe->phb->global_number, pe->addr);
  759. /*
  760. * The device is removed, tear down its state, on powernv
  761. * hotplug driver would take care of it but not on pseries,
  762. * permanently disable the card as it is hot removed.
  763. *
  764. * In the case of powernv, note that the removal of device
  765. * is covered by pci rescan lock, so no problem even if hotplug
  766. * driver attempts to remove the device.
  767. */
  768. goto recover_failed;
  769. }
  770. /* Log the event */
  771. if (pe->type & EEH_PE_PHB) {
  772. pr_err("EEH: Recovering PHB#%x, location: %s\n",
  773. pe->phb->global_number, eeh_pe_loc_get(pe));
  774. } else {
  775. struct eeh_pe *phb_pe = eeh_phb_pe_get(pe->phb);
  776. pr_err("EEH: Recovering PHB#%x-PE#%x\n",
  777. pe->phb->global_number, pe->addr);
  778. pr_err("EEH: PE location: %s, PHB location: %s\n",
  779. eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
  780. }
  781. #ifdef CONFIG_STACKTRACE
  782. /*
  783. * Print the saved stack trace now that we've verified there's
  784. * something to recover.
  785. */
  786. if (pe->trace_entries) {
  787. void **ptrs = (void **) pe->stack_trace;
  788. int i;
  789. pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
  790. pe->phb->global_number, pe->addr);
  791. /* FIXME: Use the same format as dump_stack() */
  792. pr_err("EEH: Call Trace:\n");
  793. for (i = 0; i < pe->trace_entries; i++)
  794. pr_err("EEH: [%pK] %pS\n", ptrs[i], ptrs[i]);
  795. pe->trace_entries = 0;
  796. }
  797. #endif /* CONFIG_STACKTRACE */
  798. eeh_for_each_pe(pe, tmp_pe)
  799. eeh_pe_for_each_dev(tmp_pe, edev, tmp)
  800. edev->mode &= ~EEH_DEV_NO_HANDLER;
  801. eeh_pe_update_time_stamp(pe);
  802. pe->freeze_count++;
  803. if (pe->freeze_count > eeh_max_freezes) {
  804. pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
  805. pe->phb->global_number, pe->addr,
  806. pe->freeze_count);
  807. goto recover_failed;
  808. }
  809. /* Walk the various device drivers attached to this slot through
  810. * a reset sequence, giving each an opportunity to do what it needs
  811. * to accomplish the reset. Each child gets a report of the
  812. * status ... if any child can't handle the reset, then the entire
  813. * slot is dlpar removed and added.
  814. *
  815. * When the PHB is fenced, we have to issue a reset to recover from
  816. * the error. Override the result if necessary to have partially
  817. * hotplug for this case.
  818. */
  819. pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
  820. pe->freeze_count, eeh_max_freezes);
  821. pr_info("EEH: Notify device drivers to shutdown\n");
  822. eeh_set_channel_state(pe, pci_channel_io_frozen);
  823. eeh_set_irq_state(pe, false);
  824. eeh_pe_report("error_detected(IO frozen)", pe,
  825. eeh_report_error, &result);
  826. if (result == PCI_ERS_RESULT_DISCONNECT)
  827. goto recover_failed;
  828. /*
  829. * Error logged on a PHB are always fences which need a full
  830. * PHB reset to clear so force that to happen.
  831. */
  832. if ((pe->type & EEH_PE_PHB) && result != PCI_ERS_RESULT_NONE)
  833. result = PCI_ERS_RESULT_NEED_RESET;
  834. /* Get the current PCI slot state. This can take a long time,
  835. * sometimes over 300 seconds for certain systems.
  836. */
  837. rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY * 1000);
  838. if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
  839. pr_warn("EEH: Permanent failure\n");
  840. goto recover_failed;
  841. }
  842. /* Since rtas may enable MMIO when posting the error log,
  843. * don't post the error log until after all dev drivers
  844. * have been informed.
  845. */
  846. pr_info("EEH: Collect temporary log\n");
  847. eeh_slot_error_detail(pe, EEH_LOG_TEMP);
  848. /* If all device drivers were EEH-unaware, then shut
  849. * down all of the device drivers, and hope they
  850. * go down willingly, without panicing the system.
  851. */
  852. if (result == PCI_ERS_RESULT_NONE) {
  853. pr_info("EEH: Reset with hotplug activity\n");
  854. rc = eeh_reset_device(pe, bus, NULL, false);
  855. if (rc) {
  856. pr_warn("%s: Unable to reset, err=%d\n", __func__, rc);
  857. goto recover_failed;
  858. }
  859. }
  860. /* If all devices reported they can proceed, then re-enable MMIO */
  861. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  862. pr_info("EEH: Enable I/O for affected devices\n");
  863. rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  864. if (rc < 0)
  865. goto recover_failed;
  866. if (rc) {
  867. result = PCI_ERS_RESULT_NEED_RESET;
  868. } else {
  869. pr_info("EEH: Notify device drivers to resume I/O\n");
  870. eeh_pe_report("mmio_enabled", pe,
  871. eeh_report_mmio_enabled, &result);
  872. }
  873. }
  874. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  875. pr_info("EEH: Enabled DMA for affected devices\n");
  876. rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  877. if (rc < 0)
  878. goto recover_failed;
  879. if (rc) {
  880. result = PCI_ERS_RESULT_NEED_RESET;
  881. } else {
  882. /*
  883. * We didn't do PE reset for the case. The PE
  884. * is still in frozen state. Clear it before
  885. * resuming the PE.
  886. */
  887. eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
  888. result = PCI_ERS_RESULT_RECOVERED;
  889. }
  890. }
  891. /* If any device called out for a reset, then reset the slot */
  892. if (result == PCI_ERS_RESULT_NEED_RESET) {
  893. pr_info("EEH: Reset without hotplug activity\n");
  894. rc = eeh_reset_device(pe, bus, &rmv_data, true);
  895. if (rc) {
  896. pr_warn("%s: Cannot reset, err=%d\n", __func__, rc);
  897. goto recover_failed;
  898. }
  899. result = PCI_ERS_RESULT_NONE;
  900. eeh_set_channel_state(pe, pci_channel_io_normal);
  901. eeh_set_irq_state(pe, true);
  902. eeh_pe_report("slot_reset", pe, eeh_report_reset,
  903. &result);
  904. }
  905. if ((result == PCI_ERS_RESULT_RECOVERED) ||
  906. (result == PCI_ERS_RESULT_NONE)) {
  907. /*
  908. * For those hot removed VFs, we should add back them after PF
  909. * get recovered properly.
  910. */
  911. list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list,
  912. rmv_entry) {
  913. eeh_add_virt_device(edev);
  914. list_del(&edev->rmv_entry);
  915. }
  916. /* Tell all device drivers that they can resume operations */
  917. pr_info("EEH: Notify device driver to resume\n");
  918. eeh_set_channel_state(pe, pci_channel_io_normal);
  919. eeh_set_irq_state(pe, true);
  920. eeh_pe_report("resume", pe, eeh_report_resume, NULL);
  921. eeh_for_each_pe(pe, tmp_pe) {
  922. eeh_pe_for_each_dev(tmp_pe, edev, tmp) {
  923. edev->mode &= ~EEH_DEV_NO_HANDLER;
  924. edev->in_error = false;
  925. }
  926. }
  927. pr_info("EEH: Recovery successful.\n");
  928. goto out;
  929. }
  930. recover_failed:
  931. /*
  932. * About 90% of all real-life EEH failures in the field
  933. * are due to poorly seated PCI cards. Only 10% or so are
  934. * due to actual, failed cards.
  935. */
  936. pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
  937. "Please try reseating or replacing it\n",
  938. pe->phb->global_number, pe->addr);
  939. eeh_slot_error_detail(pe, EEH_LOG_PERM);
  940. /* Notify all devices that they're about to go down. */
  941. eeh_set_irq_state(pe, false);
  942. eeh_pe_report("error_detected(permanent failure)", pe,
  943. eeh_report_failure, NULL);
  944. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  945. /* Mark the PE to be removed permanently */
  946. eeh_pe_state_mark(pe, EEH_PE_REMOVED);
  947. /*
  948. * Shut down the device drivers for good. We mark
  949. * all removed devices correctly to avoid access
  950. * the their PCI config any more.
  951. */
  952. if (pe->type & EEH_PE_VF) {
  953. eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
  954. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  955. } else {
  956. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
  957. eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
  958. bus = eeh_pe_bus_get(pe);
  959. if (bus)
  960. pci_hp_remove_devices(bus);
  961. else
  962. pr_err("%s: PCI bus for PHB#%x-PE#%x disappeared\n",
  963. __func__, pe->phb->global_number, pe->addr);
  964. /* The passed PE should no longer be used */
  965. pci_unlock_rescan_remove();
  966. return;
  967. }
  968. out:
  969. /*
  970. * Clean up any PEs without devices. While marked as EEH_PE_RECOVERYING
  971. * we don't want to modify the PE tree structure so we do it here.
  972. */
  973. eeh_pe_cleanup(pe);
  974. /* clear the slot attention LED for all recovered devices */
  975. eeh_for_each_pe(pe, tmp_pe)
  976. eeh_pe_for_each_dev(tmp_pe, edev, tmp)
  977. eeh_clear_slot_attention(edev->pdev);
  978. eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
  979. pci_unlock_rescan_remove();
  980. }
  981. /**
  982. * eeh_handle_special_event - Handle EEH events without a specific failing PE
  983. *
  984. * Called when an EEH event is detected but can't be narrowed down to a
  985. * specific PE. Iterates through possible failures and handles them as
  986. * necessary.
  987. */
  988. void eeh_handle_special_event(void)
  989. {
  990. struct eeh_pe *pe, *phb_pe, *tmp_pe;
  991. struct eeh_dev *edev, *tmp_edev;
  992. struct pci_bus *bus;
  993. struct pci_controller *hose;
  994. unsigned long flags;
  995. int rc;
  996. pci_lock_rescan_remove();
  997. do {
  998. rc = eeh_ops->next_error(&pe);
  999. switch (rc) {
  1000. case EEH_NEXT_ERR_DEAD_IOC:
  1001. /* Mark all PHBs in dead state */
  1002. eeh_serialize_lock(&flags);
  1003. /* Purge all events */
  1004. eeh_remove_event(NULL, true);
  1005. list_for_each_entry(hose, &hose_list, list_node) {
  1006. phb_pe = eeh_phb_pe_get(hose);
  1007. if (!phb_pe) continue;
  1008. eeh_pe_mark_isolated(phb_pe);
  1009. }
  1010. eeh_serialize_unlock(flags);
  1011. break;
  1012. case EEH_NEXT_ERR_FROZEN_PE:
  1013. case EEH_NEXT_ERR_FENCED_PHB:
  1014. case EEH_NEXT_ERR_DEAD_PHB:
  1015. /* Mark the PE in fenced state */
  1016. eeh_serialize_lock(&flags);
  1017. /* Purge all events of the PHB */
  1018. eeh_remove_event(pe, true);
  1019. if (rc != EEH_NEXT_ERR_DEAD_PHB)
  1020. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  1021. eeh_pe_mark_isolated(pe);
  1022. eeh_serialize_unlock(flags);
  1023. break;
  1024. case EEH_NEXT_ERR_NONE:
  1025. pci_unlock_rescan_remove();
  1026. return;
  1027. default:
  1028. pr_warn("%s: Invalid value %d from next_error()\n",
  1029. __func__, rc);
  1030. pci_unlock_rescan_remove();
  1031. return;
  1032. }
  1033. /*
  1034. * For fenced PHB and frozen PE, it's handled as normal
  1035. * event. We have to remove the affected PHBs for dead
  1036. * PHB and IOC
  1037. */
  1038. if (rc == EEH_NEXT_ERR_FROZEN_PE ||
  1039. rc == EEH_NEXT_ERR_FENCED_PHB) {
  1040. eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
  1041. pci_unlock_rescan_remove();
  1042. eeh_handle_normal_event(pe);
  1043. pci_lock_rescan_remove();
  1044. } else {
  1045. eeh_for_each_pe(pe, tmp_pe)
  1046. eeh_pe_for_each_dev(tmp_pe, edev, tmp_edev)
  1047. edev->mode &= ~EEH_DEV_NO_HANDLER;
  1048. /* Notify all devices to be down */
  1049. eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
  1050. eeh_pe_report(
  1051. "error_detected(permanent failure)", pe,
  1052. eeh_report_failure, NULL);
  1053. eeh_set_channel_state(pe, pci_channel_io_perm_failure);
  1054. list_for_each_entry(hose, &hose_list, list_node) {
  1055. phb_pe = eeh_phb_pe_get(hose);
  1056. if (!phb_pe ||
  1057. !(phb_pe->state & EEH_PE_ISOLATED) ||
  1058. (phb_pe->state & EEH_PE_RECOVERING))
  1059. continue;
  1060. bus = eeh_pe_bus_get(phb_pe);
  1061. if (!bus) {
  1062. pr_err("%s: Cannot find PCI bus for "
  1063. "PHB#%x-PE#%x\n",
  1064. __func__,
  1065. pe->phb->global_number,
  1066. pe->addr);
  1067. break;
  1068. }
  1069. pci_hp_remove_devices(bus);
  1070. }
  1071. }
  1072. /*
  1073. * If we have detected dead IOC, we needn't proceed
  1074. * any more since all PHBs would have been removed
  1075. */
  1076. if (rc == EEH_NEXT_ERR_DEAD_IOC)
  1077. break;
  1078. } while (rc != EEH_NEXT_ERR_NONE);
  1079. pci_unlock_rescan_remove();
  1080. }