eeh_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/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. /** eeh_report_error - report an EEH error to each device,
  75. * collect up and merge the device responses.
  76. */
  77. static void eeh_report_error(struct pci_dev *dev, void *userdata)
  78. {
  79. enum pci_ers_result rc, *res = userdata;
  80. struct pci_driver *driver = dev->driver;
  81. dev->error_state = pci_channel_io_frozen;
  82. if (!driver)
  83. return;
  84. if (irq_in_use (dev->irq)) {
  85. struct device_node *dn = pci_device_to_OF_node(dev);
  86. PCI_DN(dn)->eeh_mode |= EEH_MODE_IRQ_DISABLED;
  87. disable_irq_nosync(dev->irq);
  88. }
  89. if (!driver->err_handler)
  90. return;
  91. if (!driver->err_handler->error_detected)
  92. return;
  93. rc = driver->err_handler->error_detected (dev, pci_channel_io_frozen);
  94. if (*res == PCI_ERS_RESULT_NONE) *res = rc;
  95. if (*res == PCI_ERS_RESULT_NEED_RESET) return;
  96. if (*res == PCI_ERS_RESULT_DISCONNECT &&
  97. rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
  98. }
  99. /** eeh_report_reset -- tell this device that the pci slot
  100. * has been reset.
  101. */
  102. static void eeh_report_reset(struct pci_dev *dev, void *userdata)
  103. {
  104. struct pci_driver *driver = dev->driver;
  105. struct device_node *dn = pci_device_to_OF_node(dev);
  106. if (!driver)
  107. return;
  108. if ((PCI_DN(dn)->eeh_mode) & EEH_MODE_IRQ_DISABLED) {
  109. PCI_DN(dn)->eeh_mode &= ~EEH_MODE_IRQ_DISABLED;
  110. enable_irq(dev->irq);
  111. }
  112. if (!driver->err_handler)
  113. return;
  114. if (!driver->err_handler->slot_reset)
  115. return;
  116. driver->err_handler->slot_reset(dev);
  117. }
  118. static void eeh_report_resume(struct pci_dev *dev, void *userdata)
  119. {
  120. struct pci_driver *driver = dev->driver;
  121. dev->error_state = pci_channel_io_normal;
  122. if (!driver)
  123. return;
  124. if (!driver->err_handler)
  125. return;
  126. if (!driver->err_handler->resume)
  127. return;
  128. driver->err_handler->resume(dev);
  129. }
  130. static void eeh_report_failure(struct pci_dev *dev, void *userdata)
  131. {
  132. struct pci_driver *driver = dev->driver;
  133. dev->error_state = pci_channel_io_perm_failure;
  134. if (!driver)
  135. return;
  136. if (irq_in_use (dev->irq)) {
  137. struct device_node *dn = pci_device_to_OF_node(dev);
  138. PCI_DN(dn)->eeh_mode |= EEH_MODE_IRQ_DISABLED;
  139. disable_irq_nosync(dev->irq);
  140. }
  141. if (!driver->err_handler)
  142. return;
  143. if (!driver->err_handler->error_detected)
  144. return;
  145. driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
  146. }
  147. /* ------------------------------------------------------- */
  148. /**
  149. * handle_eeh_events -- reset a PCI device after hard lockup.
  150. *
  151. * pSeries systems will isolate a PCI slot if the PCI-Host
  152. * bridge detects address or data parity errors, DMA's
  153. * occurring to wild addresses (which usually happen due to
  154. * bugs in device drivers or in PCI adapter firmware).
  155. * Slot isolations also occur if #SERR, #PERR or other misc
  156. * PCI-related errors are detected.
  157. *
  158. * Recovery process consists of unplugging the device driver
  159. * (which generated hotplug events to userspace), then issuing
  160. * a PCI #RST to the device, then reconfiguring the PCI config
  161. * space for all bridges & devices under this slot, and then
  162. * finally restarting the device drivers (which cause a second
  163. * set of hotplug events to go out to userspace).
  164. */
  165. /**
  166. * eeh_reset_device() -- perform actual reset of a pci slot
  167. * Args: bus: pointer to the pci bus structure corresponding
  168. * to the isolated slot. A non-null value will
  169. * cause all devices under the bus to be removed
  170. * and then re-added.
  171. * pe_dn: pointer to a "Partionable Endpoint" device node.
  172. * This is the top-level structure on which pci
  173. * bus resets can be performed.
  174. */
  175. static int eeh_reset_device (struct pci_dn *pe_dn, struct pci_bus *bus)
  176. {
  177. int cnt, rc;
  178. /* pcibios will clear the counter; save the value */
  179. cnt = pe_dn->eeh_freeze_count;
  180. if (bus)
  181. pcibios_remove_pci_devices(bus);
  182. /* Reset the pci controller. (Asserts RST#; resets config space).
  183. * Reconfigure bridges and devices. Don't try to bring the system
  184. * up if the reset failed for some reason. */
  185. rc = rtas_set_slot_reset(pe_dn);
  186. if (rc)
  187. return rc;
  188. /* New-style config addrs might be shared across multiple devices,
  189. * Walk over all functions on this device */
  190. if (pe_dn->eeh_pe_config_addr) {
  191. struct device_node *pe = pe_dn->node;
  192. pe = pe->parent->child;
  193. while (pe) {
  194. struct pci_dn *ppe = PCI_DN(pe);
  195. if (pe_dn->eeh_pe_config_addr == ppe->eeh_pe_config_addr) {
  196. rtas_configure_bridge(ppe);
  197. eeh_restore_bars(ppe);
  198. }
  199. pe = pe->sibling;
  200. }
  201. } else {
  202. rtas_configure_bridge(pe_dn);
  203. eeh_restore_bars(pe_dn);
  204. }
  205. /* Give the system 5 seconds to finish running the user-space
  206. * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
  207. * this is a hack, but if we don't do this, and try to bring
  208. * the device up before the scripts have taken it down,
  209. * potentially weird things happen.
  210. */
  211. if (bus) {
  212. ssleep (5);
  213. pcibios_add_pci_devices(bus);
  214. }
  215. pe_dn->eeh_freeze_count = cnt;
  216. return 0;
  217. }
  218. /* The longest amount of time to wait for a pci device
  219. * to come back on line, in seconds.
  220. */
  221. #define MAX_WAIT_FOR_RECOVERY 15
  222. struct pci_dn * handle_eeh_events (struct eeh_event *event)
  223. {
  224. struct device_node *frozen_dn;
  225. struct pci_dn *frozen_pdn;
  226. struct pci_bus *frozen_bus;
  227. int rc = 0;
  228. enum pci_ers_result result = PCI_ERS_RESULT_NONE;
  229. const char *location, *pci_str, *drv_str;
  230. frozen_dn = find_device_pe(event->dn);
  231. frozen_bus = pcibios_find_pci_bus(frozen_dn);
  232. if (!frozen_dn) {
  233. location = (char *) get_property(event->dn, "ibm,loc-code", NULL);
  234. location = location ? location : "unknown";
  235. printk(KERN_ERR "EEH: Error: Cannot find partition endpoint "
  236. "for location=%s pci addr=%s\n",
  237. location, pci_name(event->dev));
  238. return NULL;
  239. }
  240. location = (char *) get_property(frozen_dn, "ibm,loc-code", NULL);
  241. location = location ? location : "unknown";
  242. /* There are two different styles for coming up with the PE.
  243. * In the old style, it was the highest EEH-capable device
  244. * which was always an EADS pci bridge. In the new style,
  245. * there might not be any EADS bridges, and even when there are,
  246. * the firmware marks them as "EEH incapable". So another
  247. * two-step is needed to find the pci bus.. */
  248. if (!frozen_bus)
  249. frozen_bus = pcibios_find_pci_bus (frozen_dn->parent);
  250. if (!frozen_bus) {
  251. printk(KERN_ERR "EEH: Cannot find PCI bus "
  252. "for location=%s dn=%s\n",
  253. location, frozen_dn->full_name);
  254. return NULL;
  255. }
  256. #if 0
  257. /* We may get "permanent failure" messages on empty slots.
  258. * These are false alarms. Empty slots have no child dn. */
  259. if ((event->state == pci_channel_io_perm_failure) && (frozen_device == NULL))
  260. return;
  261. #endif
  262. frozen_pdn = PCI_DN(frozen_dn);
  263. frozen_pdn->eeh_freeze_count++;
  264. if (frozen_pdn->pcidev) {
  265. pci_str = pci_name (frozen_pdn->pcidev);
  266. drv_str = pcid_name (frozen_pdn->pcidev);
  267. } else {
  268. pci_str = pci_name (event->dev);
  269. drv_str = pcid_name (event->dev);
  270. }
  271. if (frozen_pdn->eeh_freeze_count > EEH_MAX_ALLOWED_FREEZES)
  272. goto excess_failures;
  273. /* If the reset state is a '5' and the time to reset is 0 (infinity)
  274. * or is more then 15 seconds, then mark this as a permanent failure.
  275. */
  276. if ((event->state == pci_channel_io_perm_failure) &&
  277. ((event->time_unavail <= 0) ||
  278. (event->time_unavail > MAX_WAIT_FOR_RECOVERY*1000)))
  279. goto hard_fail;
  280. eeh_slot_error_detail(frozen_pdn, 1 /* Temporary Error */);
  281. printk(KERN_WARNING
  282. "EEH: This PCI device has failed %d times since last reboot: "
  283. "location=%s driver=%s pci addr=%s\n",
  284. frozen_pdn->eeh_freeze_count, location, drv_str, pci_str);
  285. /* Walk the various device drivers attached to this slot through
  286. * a reset sequence, giving each an opportunity to do what it needs
  287. * to accomplish the reset. Each child gets a report of the
  288. * status ... if any child can't handle the reset, then the entire
  289. * slot is dlpar removed and added.
  290. */
  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 == PCI_ERS_RESULT_NONE) {
  297. rc = eeh_reset_device(frozen_pdn, frozen_bus);
  298. if (rc)
  299. goto hard_fail;
  300. }
  301. /* If any device called out for a reset, then reset the slot */
  302. if (result == PCI_ERS_RESULT_NEED_RESET) {
  303. rc = eeh_reset_device(frozen_pdn, NULL);
  304. if (rc)
  305. goto hard_fail;
  306. pci_walk_bus(frozen_bus, eeh_report_reset, NULL);
  307. }
  308. /* If all devices reported they can proceed, the re-enable PIO */
  309. if (result == PCI_ERS_RESULT_CAN_RECOVER) {
  310. /* XXX Not supported; we brute-force reset the device */
  311. rc = eeh_reset_device(frozen_pdn, NULL);
  312. if (rc)
  313. goto hard_fail;
  314. pci_walk_bus(frozen_bus, eeh_report_reset, NULL);
  315. }
  316. /* Tell all device drivers that they can resume operations */
  317. pci_walk_bus(frozen_bus, eeh_report_resume, NULL);
  318. return frozen_pdn;
  319. excess_failures:
  320. /*
  321. * About 90% of all real-life EEH failures in the field
  322. * are due to poorly seated PCI cards. Only 10% or so are
  323. * due to actual, failed cards.
  324. */
  325. printk(KERN_ERR
  326. "EEH: PCI device at location=%s driver=%s pci addr=%s \n"
  327. "has failed %d times and has been permanently disabled. \n"
  328. "Please try reseating this device or replacing it.\n",
  329. location, drv_str, pci_str, frozen_pdn->eeh_freeze_count);
  330. goto perm_error;
  331. hard_fail:
  332. printk(KERN_ERR
  333. "EEH: Unable to recover from failure of PCI device "
  334. "at location=%s driver=%s pci addr=%s \n"
  335. "Please try reseating this device or replacing it.\n",
  336. location, drv_str, pci_str);
  337. perm_error:
  338. eeh_slot_error_detail(frozen_pdn, 2 /* Permanent Error */);
  339. /* Notify all devices that they're about to go down. */
  340. pci_walk_bus(frozen_bus, eeh_report_failure, NULL);
  341. /* Shut down the device drivers for good. */
  342. pcibios_remove_pci_devices(frozen_bus);
  343. return NULL;
  344. }
  345. /* ---------- end of file ---------- */