eeh_driver.c 28 KB

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