eeh_driver.c 30 KB

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