eeh_driver.c 14 KB

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