eeh_driver.c 11 KB

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