eeh_driver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
  3. * Copyright (C) 2004, 2005 Linas Vepstas <linas@linas.org>
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Send feedback to <linas@us.ibm.com>
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/notifier.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. static inline const char * pcid_name (struct pci_dev *pdev)
  37. {
  38. if (pdev && pdev->dev.driver)
  39. return pdev->dev.driver->name;
  40. return "";
  41. }
  42. #ifdef DEBUG
  43. static void print_device_node_tree (struct pci_dn *pdn, int dent)
  44. {
  45. int i;
  46. if (!pdn) return;
  47. for (i=0;i<dent; i++)
  48. printk(" ");
  49. printk("dn=%s mode=%x \tcfg_addr=%x pe_addr=%x \tfull=%s\n",
  50. pdn->node->name, pdn->eeh_mode, pdn->eeh_config_addr,
  51. pdn->eeh_pe_config_addr, pdn->node->full_name);
  52. dent += 3;
  53. struct device_node *pc = pdn->node->child;
  54. while (pc) {
  55. print_device_node_tree(PCI_DN(pc), dent);
  56. pc = pc->sibling;
  57. }
  58. }
  59. #endif
  60. /**
  61. * irq_in_use - return true if this irq is being used
  62. */
  63. static int irq_in_use(unsigned int irq)
  64. {
  65. int rc = 0;
  66. unsigned long flags;
  67. struct irq_desc *desc = irq_desc + irq;
  68. spin_lock_irqsave(&desc->lock, flags);
  69. if (desc->action)
  70. rc = 1;
  71. spin_unlock_irqrestore(&desc->lock, flags);
  72. return rc;
  73. }
  74. /* ------------------------------------------------------- */
  75. /** eeh_report_error - report an EEH error to each device,
  76. * collect up and merge the device responses.
  77. */
  78. static void eeh_report_error(struct pci_dev *dev, void *userdata)
  79. {
  80. enum pci_ers_result rc, *res = userdata;
  81. struct pci_driver *driver = dev->driver;
  82. dev->error_state = pci_channel_io_frozen;
  83. if (!driver)
  84. return;
  85. if (irq_in_use (dev->irq)) {
  86. struct device_node *dn = pci_device_to_OF_node(dev);
  87. PCI_DN(dn)->eeh_mode |= EEH_MODE_IRQ_DISABLED;
  88. disable_irq_nosync(dev->irq);
  89. }
  90. if (!driver->err_handler)
  91. return;
  92. if (!driver->err_handler->error_detected)
  93. return;
  94. rc = driver->err_handler->error_detected (dev, pci_channel_io_frozen);
  95. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  96. if (*res == PCI_ERS_RESULT_NEED_RESET) return;
  97. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  98. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  99. }
  100. /** eeh_report_reset -- tell this device that the pci slot
  101. * has been reset.
  102. */
  103. static void eeh_report_reset(struct pci_dev *dev, void *userdata)
  104. {
  105. struct pci_driver *driver = dev->driver;
  106. struct device_node *dn = pci_device_to_OF_node(dev);
  107. if (!driver)
  108. return;
  109. if ((PCI_DN(dn)->eeh_mode) & EEH_MODE_IRQ_DISABLED) {
  110. PCI_DN(dn)->eeh_mode &= ~EEH_MODE_IRQ_DISABLED;
  111. enable_irq(dev->irq);
  112. }
  113. if (!driver->err_handler)
  114. return;
  115. if (!driver->err_handler->slot_reset)
  116. return;
  117. driver->err_handler->slot_reset(dev);
  118. }
  119. static void eeh_report_resume(struct pci_dev *dev, void *userdata)
  120. {
  121. struct pci_driver *driver = dev->driver;
  122. dev->error_state = pci_channel_io_normal;
  123. if (!driver)
  124. return;
  125. if (!driver->err_handler)
  126. return;
  127. if (!driver->err_handler->resume)
  128. return;
  129. driver->err_handler->resume(dev);
  130. }
  131. static void eeh_report_failure(struct pci_dev *dev, void *userdata)
  132. {
  133. struct pci_driver *driver = dev->driver;
  134. dev->error_state = pci_channel_io_perm_failure;
  135. if (!driver)
  136. return;
  137. if (irq_in_use (dev->irq)) {
  138. struct device_node *dn = pci_device_to_OF_node(dev);
  139. PCI_DN(dn)->eeh_mode |= EEH_MODE_IRQ_DISABLED;
  140. disable_irq_nosync(dev->irq);
  141. }
  142. if (!driver->err_handler)
  143. return;
  144. if (!driver->err_handler->error_detected)
  145. return;
  146. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  147. }
  148. /* ------------------------------------------------------- */
  149. /**
  150. * handle_eeh_events -- reset a PCI device after hard lockup.
  151. *
  152. * pSeries systems will isolate a PCI slot if the PCI-Host
  153. * bridge detects address or data parity errors, DMA's
  154. * occuring to wild addresses (which usually happen due to
  155. * bugs in device drivers or in PCI adapter firmware).
  156. * Slot isolations also occur if #SERR, #PERR or other misc
  157. * PCI-related errors are detected.
  158. *
  159. * Recovery process consists of unplugging the device driver
  160. * (which generated hotplug events to userspace), then issuing
  161. * a PCI #RST to the device, then reconfiguring the PCI config
  162. * space for all bridges & devices under this slot, and then
  163. * finally restarting the device drivers (which cause a second
  164. * set of hotplug events to go out to userspace).
  165. */
  166. /**
  167. * eeh_reset_device() -- perform actual reset of a pci slot
  168. * Args: bus: pointer to the pci bus structure corresponding
  169. * to the isolated slot. A non-null value will
  170. * cause all devices under the bus to be removed
  171. * and then re-added.
  172. * pe_dn: pointer to a "Partionable Endpoint" device node.
  173. * This is the top-level structure on which pci
  174. * bus resets can be performed.
  175. */
  176. static int eeh_reset_device (struct pci_dn *pe_dn, struct pci_bus *bus)
  177. {
  178. int rc;
  179. if (bus)
  180. pcibios_remove_pci_devices(bus);
  181. /* Reset the pci controller. (Asserts RST#; resets config space).
  182. * Reconfigure bridges and devices. Don't try to bring the system
  183. * up if the reset failed for some reason. */
  184. rc = rtas_set_slot_reset(pe_dn);
  185. if (rc)
  186. return rc;
  187. /* New-style config addrs might be shared across multiple devices,
  188. * Walk over all functions on this device */
  189. if (pe_dn->eeh_pe_config_addr) {
  190. struct device_node *pe = pe_dn->node;
  191. pe = pe->parent->child;
  192. while (pe) {
  193. struct pci_dn *ppe = PCI_DN(pe);
  194. if (pe_dn->eeh_pe_config_addr == ppe->eeh_pe_config_addr) {
  195. rtas_configure_bridge(ppe);
  196. eeh_restore_bars(ppe);
  197. }
  198. pe = pe->sibling;
  199. }
  200. } else {
  201. rtas_configure_bridge(pe_dn);
  202. eeh_restore_bars(pe_dn);
  203. }
  204. /* Give the system 5 seconds to finish running the user-space
  205. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  206. * this is a hack, but if we don't do this, and try to bring
  207. * the device up before the scripts have taken it down,
  208. * potentially weird things happen.
  209. */
  210. if (bus) {
  211. ssleep (5);
  212. pcibios_add_pci_devices(bus);
  213. }
  214. return 0;
  215. }
  216. /* The longest amount of time to wait for a pci device
  217. * to come back on line, in seconds.
  218. */
  219. #define MAX_WAIT_FOR_RECOVERY 15
  220. void handle_eeh_events (struct eeh_event *event)
  221. {
  222. struct device_node *frozen_dn;
  223. struct pci_dn *frozen_pdn;
  224. struct pci_bus *frozen_bus;
  225. int rc = 0;
  226. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  227. const char *pci_str, *drv_str;
  228. frozen_dn = find_device_pe(event->dn);
  229. frozen_bus = pcibios_find_pci_bus(frozen_dn);
  230. if (!frozen_dn) {
  231. printk(KERN_ERR "EEH: Error: Cannot find partition endpoint for %s\n",
  232. pci_name(event->dev));
  233. return;
  234. }
  235. /* There are two different styles for coming up with the PE.
  236. * In the old style, it was the highest EEH-capable device
  237. * which was always an EADS pci bridge. In the new style,
  238. * there might not be any EADS bridges, and even when there are,
  239. * the firmware marks them as "EEH incapable". So another
  240. * two-step is needed to find the pci bus.. */
  241. if (!frozen_bus)
  242. frozen_bus = pcibios_find_pci_bus (frozen_dn->parent);
  243. if (!frozen_bus) {
  244. printk(KERN_ERR "EEH: Cannot find PCI bus for %s\n",
  245. frozen_dn->full_name);
  246. return;
  247. }
  248. #if 0
  249. /* We may get "permanent failure" messages on empty slots.
  250. * These are false alarms. Empty slots have no child dn. */
  251. if ((event->state == pci_channel_io_perm_failure) && (frozen_device == NULL))
  252. return;
  253. #endif
  254. frozen_pdn = PCI_DN(frozen_dn);
  255. frozen_pdn->eeh_freeze_count++;
  256. if (frozen_pdn->pcidev) {
  257. pci_str = pci_name (frozen_pdn->pcidev);
  258. drv_str = pcid_name (frozen_pdn->pcidev);
  259. } else {
  260. pci_str = pci_name (event->dev);
  261. drv_str = pcid_name (event->dev);
  262. }
  263. if (frozen_pdn->eeh_freeze_count > EEH_MAX_ALLOWED_FREEZES)
  264. goto excess_failures;
  265. /* If the reset state is a '5' and the time to reset is 0 (infinity)
  266. * or is more then 15 seconds, then mark this as a permanent failure.
  267. */
  268. if ((event->state == pci_channel_io_perm_failure) &&
  269. ((event->time_unavail <= 0) ||
  270. (event->time_unavail > MAX_WAIT_FOR_RECOVERY*1000)))
  271. goto hard_fail;
  272. eeh_slot_error_detail(frozen_pdn, 1 /* Temporary Error */);
  273. printk(KERN_WARNING
  274. "EEH: This PCI device has failed %d times since last reboot: %s - %s\n",
  275. frozen_pdn->eeh_freeze_count, drv_str, pci_str);
  276. /* Walk the various device drivers attached to this slot through
  277. * a reset sequence, giving each an opportunity to do what it needs
  278. * to accomplish the reset. Each child gets a report of the
  279. * status ... if any child can't handle the reset, then the entire
  280. * slot is dlpar removed and added.
  281. */
  282. pci_walk_bus(frozen_bus, eeh_report_error, &result);
  283. /* If all device drivers were EEH-unaware, then shut
  284. * down all of the device drivers, and hope they
  285. * go down willingly, without panicing the system.
  286. */
  287. if (result == PCI_ERS_RESULT_NONE) {
  288. rc = eeh_reset_device(frozen_pdn, frozen_bus);
  289. if (rc)
  290. goto hard_fail;
  291. }
  292. /* If any device called out for a reset, then reset the slot */
  293. if (result == PCI_ERS_RESULT_NEED_RESET) {
  294. rc = eeh_reset_device(frozen_pdn, NULL);
  295. if (rc)
  296. goto hard_fail;
  297. pci_walk_bus(frozen_bus, eeh_report_reset, NULL);
  298. }
  299. /* If all devices reported they can proceed, the re-enable PIO */
  300. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  301. /* XXX Not supported; we brute-force reset the device */
  302. rc = eeh_reset_device(frozen_pdn, NULL);
  303. if (rc)
  304. goto hard_fail;
  305. pci_walk_bus(frozen_bus, eeh_report_reset, NULL);
  306. }
  307. /* Tell all device drivers that they can resume operations */
  308. pci_walk_bus(frozen_bus, eeh_report_resume, NULL);
  309. return;
  310. excess_failures:
  311. /*
  312. * About 90% of all real-life EEH failures in the field
  313. * are due to poorly seated PCI cards. Only 10% or so are
  314. * due to actual, failed cards.
  315. */
  316. printk(KERN_ERR
  317. "EEH: PCI device %s - %s has failed %d times \n"
  318. "and has been permanently disabled. Please try reseating\n"
  319. "this device or replacing it.\n",
  320. drv_str, pci_str, frozen_pdn->eeh_freeze_count);
  321. goto perm_error;
  322. hard_fail:
  323. printk(KERN_ERR
  324. "EEH: Unable to recover from failure of PCI device %s - %s\n"
  325. "Please try reseating this device or replacing it.\n",
  326. drv_str, pci_str);
  327. perm_error:
  328. eeh_slot_error_detail(frozen_pdn, 2 /* Permanent Error */);
  329. /* Notify all devices that they're about to go down. */
  330. pci_walk_bus(frozen_bus, eeh_report_failure, NULL);
  331. /* Shut down the device drivers for good. */
  332. pcibios_remove_pci_devices(frozen_bus);
  333. }
  334. /* ---------- end of file ---------- */