eeh_driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/pci.h>
  29. #include <asm/eeh.h>
  30. #include <asm/eeh_event.h>
  31. #include <asm/ppc-pci.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. static inline const char * pcid_name (struct pci_dev *pdev)
  36. {
  37. if (pdev && pdev->dev.driver)
  38. return pdev->dev.driver->name;
  39. return "";
  40. }
  41. #if 0
  42. static void print_device_node_tree(struct pci_dn *pdn, int dent)
  43. {
  44. int i;
  45. struct device_node *pc;
  46. if (!pdn)
  47. return;
  48. for (i = 0; i < dent; i++)
  49. printk(" ");
  50. printk("dn=%s mode=%x \tcfg_addr=%x pe_addr=%x \tfull=%s\n",
  51. pdn->node->name, pdn->eeh_mode, pdn->eeh_config_addr,
  52. pdn->eeh_pe_config_addr, pdn->node->full_name);
  53. dent += 3;
  54. pc = pdn->node->child;
  55. while (pc) {
  56. print_device_node_tree(PCI_DN(pc), dent);
  57. pc = pc->sibling;
  58. }
  59. }
  60. #endif
  61. /**
  62. * irq_in_use - return true if this irq is being used
  63. */
  64. static int irq_in_use(unsigned int irq)
  65. {
  66. int rc = 0;
  67. unsigned long flags;
  68. struct irq_desc *desc = irq_desc + irq;
  69. spin_lock_irqsave(&desc->lock, flags);
  70. if (desc->action)
  71. rc = 1;
  72. spin_unlock_irqrestore(&desc->lock, flags);
  73. return rc;
  74. }
  75. /**
  76. * eeh_disable_irq - disable interrupt for the recovering device
  77. */
  78. static void eeh_disable_irq(struct pci_dev *dev)
  79. {
  80. struct device_node *dn = pci_device_to_OF_node(dev);
  81. /* Don't disable MSI and MSI-X interrupts. They are
  82. * effectively disabled by the DMA Stopped state
  83. * when an EEH error occurs.
  84. */
  85. if (dev->msi_enabled || dev->msix_enabled)
  86. return;
  87. if (!irq_in_use(dev->irq))
  88. return;
  89. PCI_DN(dn)->eeh_mode |= EEH_MODE_IRQ_DISABLED;
  90. disable_irq_nosync(dev->irq);
  91. }
  92. /**
  93. * eeh_enable_irq - enable interrupt for the recovering device
  94. */
  95. static void eeh_enable_irq(struct pci_dev *dev)
  96. {
  97. struct device_node *dn = pci_device_to_OF_node(dev);
  98. if ((PCI_DN(dn)->eeh_mode) & EEH_MODE_IRQ_DISABLED) {
  99. PCI_DN(dn)->eeh_mode &= ~EEH_MODE_IRQ_DISABLED;
  100. enable_irq(dev->irq);
  101. }
  102. }
  103. /* ------------------------------------------------------- */
  104. /**
  105. * eeh_report_error - report pci error to each device driver
  106. *
  107. * Report an EEH error to each device driver, collect up and
  108. * merge the device driver responses. Cumulative response
  109. * passed back in "userdata".
  110. */
  111. static void eeh_report_error(struct pci_dev *dev, void *userdata)
  112. {
  113. enum pci_ers_result rc, *res = userdata;
  114. struct pci_driver *driver = dev->driver;
  115. dev->error_state = pci_channel_io_frozen;
  116. if (!driver)
  117. return;
  118. eeh_disable_irq(dev);
  119. if (!driver->err_handler ||
  120. !driver->err_handler->error_detected)
  121. return;
  122. rc = driver->err_handler->error_detected (dev, pci_channel_io_frozen);
  123. /* A driver that needs a reset trumps all others */
  124. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  125. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  126. }
  127. /**
  128. * eeh_report_mmio_enabled - tell drivers that MMIO has been enabled
  129. *
  130. * Tells each device driver that IO ports, MMIO and config space I/O
  131. * are now enabled. Collects up and merges the device driver responses.
  132. * Cumulative response passed back in "userdata".
  133. */
  134. static void eeh_report_mmio_enabled(struct pci_dev *dev, void *userdata)
  135. {
  136. enum pci_ers_result rc, *res = userdata;
  137. struct pci_driver *driver = dev->driver;
  138. if (!driver ||
  139. !driver->err_handler ||
  140. !driver->err_handler->mmio_enabled)
  141. return;
  142. rc = driver->err_handler->mmio_enabled (dev);
  143. /* A driver that needs a reset trumps all others */
  144. if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  145. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  146. }
  147. /**
  148. * eeh_report_reset - tell device that slot has been reset
  149. */
  150. static void eeh_report_reset(struct pci_dev *dev, void *userdata)
  151. {
  152. enum pci_ers_result rc, *res = userdata;
  153. struct pci_driver *driver = dev->driver;
  154. if (!driver)
  155. return;
  156. eeh_enable_irq(dev);
  157. if (!driver->err_handler ||
  158. !driver->err_handler->slot_reset)
  159. return;
  160. rc = driver->err_handler->slot_reset(dev);
  161. if ((*res == PCI_ERS_RESULT_NONE) ||
  162. (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
  163. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  164. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  165. }
  166. /**
  167. * eeh_report_resume - tell device to resume normal operations
  168. */
  169. static void eeh_report_resume(struct pci_dev *dev, void *userdata)
  170. {
  171. struct pci_driver *driver = dev->driver;
  172. dev->error_state = pci_channel_io_normal;
  173. if (!driver)
  174. return;
  175. eeh_enable_irq(dev);
  176. if (!driver->err_handler ||
  177. !driver->err_handler->resume)
  178. return;
  179. driver->err_handler->resume(dev);
  180. }
  181. /**
  182. * eeh_report_failure - tell device driver that device is dead.
  183. *
  184. * This informs the device driver that the device is permanently
  185. * dead, and that no further recovery attempts will be made on it.
  186. */
  187. static void eeh_report_failure(struct pci_dev *dev, void *userdata)
  188. {
  189. struct pci_driver *driver = dev->driver;
  190. dev->error_state = pci_channel_io_perm_failure;
  191. if (!driver)
  192. return;
  193. eeh_disable_irq(dev);
  194. if (!driver->err_handler ||
  195. !driver->err_handler->error_detected)
  196. return;
  197. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  198. }
  199. /* ------------------------------------------------------- */
  200. /**
  201. * handle_eeh_events -- reset a PCI device after hard lockup.
  202. *
  203. * pSeries systems will isolate a PCI slot if the PCI-Host
  204. * bridge detects address or data parity errors, DMA's
  205. * occurring to wild addresses (which usually happen due to
  206. * bugs in device drivers or in PCI adapter firmware).
  207. * Slot isolations also occur if #SERR, #PERR or other misc
  208. * PCI-related errors are detected.
  209. *
  210. * Recovery process consists of unplugging the device driver
  211. * (which generated hotplug events to userspace), then issuing
  212. * a PCI #RST to the device, then reconfiguring the PCI config
  213. * space for all bridges & devices under this slot, and then
  214. * finally restarting the device drivers (which cause a second
  215. * set of hotplug events to go out to userspace).
  216. */
  217. /**
  218. * eeh_reset_device() -- perform actual reset of a pci slot
  219. * @bus: pointer to the pci bus structure corresponding
  220. * to the isolated slot. A non-null value will
  221. * cause all devices under the bus to be removed
  222. * and then re-added.
  223. * @pe_dn: pointer to a "Partionable Endpoint" device node.
  224. * This is the top-level structure on which pci
  225. * bus resets can be performed.
  226. */
  227. static int eeh_reset_device (struct pci_dn *pe_dn, struct pci_bus *bus)
  228. {
  229. struct device_node *dn;
  230. int cnt, rc;
  231. /* pcibios will clear the counter; save the value */
  232. cnt = pe_dn->eeh_freeze_count;
  233. if (bus)
  234. pcibios_remove_pci_devices(bus);
  235. /* Reset the pci controller. (Asserts RST#; resets config space).
  236. * Reconfigure bridges and devices. Don't try to bring the system
  237. * up if the reset failed for some reason. */
  238. rc = rtas_set_slot_reset(pe_dn);
  239. if (rc)
  240. return rc;
  241. /* Walk over all functions on this device. */
  242. dn = pe_dn->node;
  243. if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
  244. dn = dn->parent->child;
  245. while (dn) {
  246. struct pci_dn *ppe = PCI_DN(dn);
  247. /* On Power4, always true because eeh_pe_config_addr=0 */
  248. if (pe_dn->eeh_pe_config_addr == ppe->eeh_pe_config_addr) {
  249. rtas_configure_bridge(ppe);
  250. eeh_restore_bars(ppe);
  251. }
  252. dn = dn->sibling;
  253. }
  254. /* Give the system 5 seconds to finish running the user-space
  255. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  256. * this is a hack, but if we don't do this, and try to bring
  257. * the device up before the scripts have taken it down,
  258. * potentially weird things happen.
  259. */
  260. if (bus) {
  261. ssleep (5);
  262. pcibios_add_pci_devices(bus);
  263. }
  264. pe_dn->eeh_freeze_count = cnt;
  265. return 0;
  266. }
  267. /* The longest amount of time to wait for a pci device
  268. * to come back on line, in seconds.
  269. */
  270. #define MAX_WAIT_FOR_RECOVERY 150
  271. struct pci_dn * handle_eeh_events (struct eeh_event *event)
  272. {
  273. struct device_node *frozen_dn;
  274. struct pci_dn *frozen_pdn;
  275. struct pci_bus *frozen_bus;
  276. int rc = 0;
  277. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  278. const char *location, *pci_str, *drv_str;
  279. frozen_dn = find_device_pe(event->dn);
  280. if (!frozen_dn) {
  281. location = of_get_property(event->dn, "ibm,loc-code", NULL);
  282. location = location ? location : "unknown";
  283. printk(KERN_ERR "EEH: Error: Cannot find partition endpoint "
  284. "for location=%s pci addr=%s\n",
  285. location, pci_name(event->dev));
  286. return NULL;
  287. }
  288. frozen_bus = pcibios_find_pci_bus(frozen_dn);
  289. location = of_get_property(frozen_dn, "ibm,loc-code", NULL);
  290. location = location ? location : "unknown";
  291. /* There are two different styles for coming up with the PE.
  292. * In the old style, it was the highest EEH-capable device
  293. * which was always an EADS pci bridge. In the new style,
  294. * there might not be any EADS bridges, and even when there are,
  295. * the firmware marks them as "EEH incapable". So another
  296. * two-step is needed to find the pci bus.. */
  297. if (!frozen_bus)
  298. frozen_bus = pcibios_find_pci_bus (frozen_dn->parent);
  299. if (!frozen_bus) {
  300. printk(KERN_ERR "EEH: Cannot find PCI bus "
  301. "for location=%s dn=%s\n",
  302. location, frozen_dn->full_name);
  303. return NULL;
  304. }
  305. frozen_pdn = PCI_DN(frozen_dn);
  306. frozen_pdn->eeh_freeze_count++;
  307. if (frozen_pdn->pcidev) {
  308. pci_str = pci_name (frozen_pdn->pcidev);
  309. drv_str = pcid_name (frozen_pdn->pcidev);
  310. } else {
  311. pci_str = pci_name (event->dev);
  312. drv_str = pcid_name (event->dev);
  313. }
  314. if (frozen_pdn->eeh_freeze_count > EEH_MAX_ALLOWED_FREEZES)
  315. goto excess_failures;
  316. printk(KERN_WARNING
  317. "EEH: This PCI device has failed %d times in the last hour:\n",
  318. frozen_pdn->eeh_freeze_count);
  319. printk(KERN_WARNING
  320. "EEH: location=%s driver=%s pci addr=%s\n",
  321. location, drv_str, pci_str);
  322. /* Walk the various device drivers attached to this slot through
  323. * a reset sequence, giving each an opportunity to do what it needs
  324. * to accomplish the reset. Each child gets a report of the
  325. * status ... if any child can't handle the reset, then the entire
  326. * slot is dlpar removed and added.
  327. */
  328. pci_walk_bus(frozen_bus, eeh_report_error, &result);
  329. /* Get the current PCI slot state. This can take a long time,
  330. * sometimes over 3 seconds for certain systems. */
  331. rc = eeh_wait_for_slot_status (frozen_pdn, MAX_WAIT_FOR_RECOVERY*1000);
  332. if (rc < 0) {
  333. printk(KERN_WARNING "EEH: Permanent failure\n");
  334. goto hard_fail;
  335. }
  336. /* Since rtas may enable MMIO when posting the error log,
  337. * don't post the error log until after all dev drivers
  338. * have been informed.
  339. */
  340. eeh_slot_error_detail(frozen_pdn, EEH_LOG_TEMP_FAILURE);
  341. /* If all device drivers were EEH-unaware, then shut
  342. * down all of the device drivers, and hope they
  343. * go down willingly, without panicing the system.
  344. */
  345. if (result == PCI_ERS_RESULT_NONE) {
  346. rc = eeh_reset_device(frozen_pdn, frozen_bus);
  347. if (rc) {
  348. printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
  349. goto hard_fail;
  350. }
  351. }
  352. /* If all devices reported they can proceed, then re-enable MMIO */
  353. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  354. rc = rtas_pci_enable(frozen_pdn, EEH_THAW_MMIO);
  355. if (rc < 0)
  356. goto hard_fail;
  357. if (rc) {
  358. result = PCI_ERS_RESULT_NEED_RESET;
  359. } else {
  360. result = PCI_ERS_RESULT_NONE;
  361. pci_walk_bus(frozen_bus, eeh_report_mmio_enabled, &result);
  362. }
  363. }
  364. /* If all devices reported they can proceed, then re-enable DMA */
  365. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  366. rc = rtas_pci_enable(frozen_pdn, EEH_THAW_DMA);
  367. if (rc < 0)
  368. goto hard_fail;
  369. if (rc)
  370. result = PCI_ERS_RESULT_NEED_RESET;
  371. else
  372. result = PCI_ERS_RESULT_RECOVERED;
  373. }
  374. /* If any device has a hard failure, then shut off everything. */
  375. if (result == PCI_ERS_RESULT_DISCONNECT) {
  376. printk(KERN_WARNING "EEH: Device driver gave up\n");
  377. goto hard_fail;
  378. }
  379. /* If any device called out for a reset, then reset the slot */
  380. if (result == PCI_ERS_RESULT_NEED_RESET) {
  381. rc = eeh_reset_device(frozen_pdn, NULL);
  382. if (rc) {
  383. printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
  384. goto hard_fail;
  385. }
  386. result = PCI_ERS_RESULT_NONE;
  387. pci_walk_bus(frozen_bus, eeh_report_reset, &result);
  388. }
  389. /* All devices should claim they have recovered by now. */
  390. if ((result != PCI_ERS_RESULT_RECOVERED) &&
  391. (result != PCI_ERS_RESULT_NONE)) {
  392. printk(KERN_WARNING "EEH: Not recovered\n");
  393. goto hard_fail;
  394. }
  395. /* Tell all device drivers that they can resume operations */
  396. pci_walk_bus(frozen_bus, eeh_report_resume, NULL);
  397. return frozen_pdn;
  398. excess_failures:
  399. /*
  400. * About 90% of all real-life EEH failures in the field
  401. * are due to poorly seated PCI cards. Only 10% or so are
  402. * due to actual, failed cards.
  403. */
  404. printk(KERN_ERR
  405. "EEH: PCI device at location=%s driver=%s pci addr=%s \n"
  406. "has failed %d times in the last hour "
  407. "and has been permanently disabled. \n"
  408. "Please try reseating this device or replacing it.\n",
  409. location, drv_str, pci_str, frozen_pdn->eeh_freeze_count);
  410. goto perm_error;
  411. hard_fail:
  412. printk(KERN_ERR
  413. "EEH: Unable to recover from failure of PCI device "
  414. "at location=%s driver=%s pci addr=%s \n"
  415. "Please try reseating this device or replacing it.\n",
  416. location, drv_str, pci_str);
  417. perm_error:
  418. eeh_slot_error_detail(frozen_pdn, EEH_LOG_PERM_FAILURE);
  419. /* Notify all devices that they're about to go down. */
  420. pci_walk_bus(frozen_bus, eeh_report_failure, NULL);
  421. /* Shut down the device drivers for good. */
  422. pcibios_remove_pci_devices(frozen_bus);
  423. return NULL;
  424. }
  425. /* ---------- end of file ---------- */