eeh_driver.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #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 pcierr_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 == PCIERR_RESULT_NONE) *res = rc;
  96. if (*res == PCIERR_RESULT_NEED_RESET) return;
  97. if (*res == PCIERR_RESULT_DISCONNECT &&
  98. rc == PCIERR_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. /* Walk over all functions on this device */
  188. rtas_configure_bridge(pe_dn);
  189. eeh_restore_bars(pe_dn);
  190. /* Give the system 5 seconds to finish running the user-space
  191. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  192. * this is a hack, but if we don't do this, and try to bring
  193. * the device up before the scripts have taken it down,
  194. * potentially weird things happen.
  195. */
  196. if (bus) {
  197. ssleep (5);
  198. pcibios_add_pci_devices(bus);
  199. }
  200. return 0;
  201. }
  202. /* The longest amount of time to wait for a pci device
  203. * to come back on line, in seconds.
  204. */
  205. #define MAX_WAIT_FOR_RECOVERY 15
  206. void handle_eeh_events (struct eeh_event *event)
  207. {
  208. struct device_node *frozen_dn;
  209. struct pci_dn *frozen_pdn;
  210. struct pci_bus *frozen_bus;
  211. int rc = 0;
  212. frozen_dn = find_device_pe(event->dn);
  213. frozen_bus = pcibios_find_pci_bus(frozen_dn);
  214. if (!frozen_dn) {
  215. printk(KERN_ERR "EEH: Error: Cannot find partition endpoint for %s\n",
  216. pci_name(event->dev));
  217. return;
  218. }
  219. /* There are two different styles for coming up with the PE.
  220. * In the old style, it was the highest EEH-capable device
  221. * which was always an EADS pci bridge. In the new style,
  222. * there might not be any EADS bridges, and even when there are,
  223. * the firmware marks them as "EEH incapable". So another
  224. * two-step is needed to find the pci bus.. */
  225. if (!frozen_bus)
  226. frozen_bus = pcibios_find_pci_bus (frozen_dn->parent);
  227. if (!frozen_bus) {
  228. printk(KERN_ERR "EEH: Cannot find PCI bus for %s\n",
  229. frozen_dn->full_name);
  230. return;
  231. }
  232. #if 0
  233. /* We may get "permanent failure" messages on empty slots.
  234. * These are false alarms. Empty slots have no child dn. */
  235. if ((event->state == pci_channel_io_perm_failure) && (frozen_device == NULL))
  236. return;
  237. #endif
  238. frozen_pdn = PCI_DN(frozen_dn);
  239. frozen_pdn->eeh_freeze_count++;
  240. if (frozen_pdn->eeh_freeze_count > EEH_MAX_ALLOWED_FREEZES)
  241. goto hard_fail;
  242. /* If the reset state is a '5' and the time to reset is 0 (infinity)
  243. * or is more then 15 seconds, then mark this as a permanent failure.
  244. */
  245. if ((event->state == pci_channel_io_perm_failure) &&
  246. ((event->time_unavail <= 0) ||
  247. (event->time_unavail > MAX_WAIT_FOR_RECOVERY*1000)))
  248. goto hard_fail;
  249. eeh_slot_error_detail(frozen_pdn, 1 /* Temporary Error */);
  250. printk(KERN_WARNING
  251. "EEH: This PCI device has failed %d times since last reboot: %s - %s\n",
  252. frozen_pdn->eeh_freeze_count,
  253. pci_name (frozen_pdn->pcidev),
  254. pcid_name(frozen_pdn->pcidev));
  255. /* Walk the various device drivers attached to this slot through
  256. * a reset sequence, giving each an opportunity to do what it needs
  257. * to accomplish the reset. Each child gets a report of the
  258. * status ... if any child can't handle the reset, then the entire
  259. * slot is dlpar removed and added.
  260. */
  261. enum pcierr_result result = PCIERR_RESULT_NONE;
  262. pci_walk_bus(frozen_bus, eeh_report_error, &result);
  263. /* If all device drivers were EEH-unaware, then shut
  264. * down all of the device drivers, and hope they
  265. * go down willingly, without panicing the system.
  266. */
  267. if (result == PCIERR_RESULT_NONE) {
  268. rc = eeh_reset_device(frozen_pdn, frozen_bus);
  269. if (rc)
  270. goto hard_fail;
  271. }
  272. /* If any device called out for a reset, then reset the slot */
  273. if (result == PCIERR_RESULT_NEED_RESET) {
  274. rc = eeh_reset_device(frozen_pdn, NULL);
  275. if (rc)
  276. goto hard_fail;
  277. pci_walk_bus(frozen_bus, eeh_report_reset, 0);
  278. }
  279. /* If all devices reported they can proceed, the re-enable PIO */
  280. if (result == PCIERR_RESULT_CAN_RECOVER) {
  281. /* XXX Not supported; we brute-force reset the device */
  282. rc = eeh_reset_device(frozen_pdn, NULL);
  283. if (rc)
  284. goto hard_fail;
  285. pci_walk_bus(frozen_bus, eeh_report_reset, 0);
  286. }
  287. /* Tell all device drivers that they can resume operations */
  288. pci_walk_bus(frozen_bus, eeh_report_resume, 0);
  289. return;
  290. hard_fail:
  291. /*
  292. * About 90% of all real-life EEH failures in the field
  293. * are due to poorly seated PCI cards. Only 10% or so are
  294. * due to actual, failed cards.
  295. */
  296. printk(KERN_ERR
  297. "EEH: PCI device %s - %s has failed %d times \n"
  298. "and has been permanently disabled. Please try reseating\n"
  299. "this device or replacing it.\n",
  300. pci_name (frozen_pdn->pcidev),
  301. pcid_name(frozen_pdn->pcidev),
  302. frozen_pdn->eeh_freeze_count);
  303. eeh_slot_error_detail(frozen_pdn, 2 /* Permanent Error */);
  304. /* Notify all devices that they're about to go down. */
  305. pci_walk_bus(frozen_bus, eeh_report_failure, 0);
  306. /* Shut down the device drivers for good. */
  307. pcibios_remove_pci_devices(frozen_bus);
  308. }
  309. /* ---------- end of file ---------- */