eeh.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * Copyright IBM Corporation 2001, 2005, 2006
  3. * Copyright Dave Engebretsen & Todd Inglett 2001
  4. * Copyright Linas Vepstas 2005, 2006
  5. * Copyright 2001-2012 IBM Corporation.
  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
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/sched.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/pci.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/rbtree.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/export.h>
  33. #include <linux/of.h>
  34. #include <linux/atomic.h>
  35. #include <asm/eeh.h>
  36. #include <asm/eeh_event.h>
  37. #include <asm/io.h>
  38. #include <asm/machdep.h>
  39. #include <asm/ppc-pci.h>
  40. #include <asm/rtas.h>
  41. /** Overview:
  42. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  43. * dealing with PCI bus errors that can't be dealt with within the
  44. * usual PCI framework, except by check-stopping the CPU. Systems
  45. * that are designed for high-availability/reliability cannot afford
  46. * to crash due to a "mere" PCI error, thus the need for EEH.
  47. * An EEH-capable bridge operates by converting a detected error
  48. * into a "slot freeze", taking the PCI adapter off-line, making
  49. * the slot behave, from the OS'es point of view, as if the slot
  50. * were "empty": all reads return 0xff's and all writes are silently
  51. * ignored. EEH slot isolation events can be triggered by parity
  52. * errors on the address or data busses (e.g. during posted writes),
  53. * which in turn might be caused by low voltage on the bus, dust,
  54. * vibration, humidity, radioactivity or plain-old failed hardware.
  55. *
  56. * Note, however, that one of the leading causes of EEH slot
  57. * freeze events are buggy device drivers, buggy device microcode,
  58. * or buggy device hardware. This is because any attempt by the
  59. * device to bus-master data to a memory address that is not
  60. * assigned to the device will trigger a slot freeze. (The idea
  61. * is to prevent devices-gone-wild from corrupting system memory).
  62. * Buggy hardware/drivers will have a miserable time co-existing
  63. * with EEH.
  64. *
  65. * Ideally, a PCI device driver, when suspecting that an isolation
  66. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  67. * whether this is the case, and then take appropriate steps to
  68. * reset the PCI slot, the PCI device, and then resume operations.
  69. * However, until that day, the checking is done here, with the
  70. * eeh_check_failure() routine embedded in the MMIO macros. If
  71. * the slot is found to be isolated, an "EEH Event" is synthesized
  72. * and sent out for processing.
  73. */
  74. /* If a device driver keeps reading an MMIO register in an interrupt
  75. * handler after a slot isolation event, it might be broken.
  76. * This sets the threshold for how many read attempts we allow
  77. * before printing an error message.
  78. */
  79. #define EEH_MAX_FAILS 2100000
  80. /* Time to wait for a PCI slot to report status, in milliseconds */
  81. #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
  82. /* Platform dependent EEH operations */
  83. struct eeh_ops *eeh_ops = NULL;
  84. int eeh_subsystem_enabled;
  85. EXPORT_SYMBOL(eeh_subsystem_enabled);
  86. /* Lock to avoid races due to multiple reports of an error */
  87. static DEFINE_RAW_SPINLOCK(confirm_error_lock);
  88. /* Buffer for reporting pci register dumps. Its here in BSS, and
  89. * not dynamically alloced, so that it ends up in RMO where RTAS
  90. * can access it.
  91. */
  92. #define EEH_PCI_REGS_LOG_LEN 4096
  93. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  94. /* System monitoring statistics */
  95. static unsigned long no_device;
  96. static unsigned long no_dn;
  97. static unsigned long no_cfg_addr;
  98. static unsigned long ignored_check;
  99. static unsigned long total_mmio_ffs;
  100. static unsigned long false_positives;
  101. static unsigned long slot_resets;
  102. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  103. /**
  104. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  105. * @pdn: device to report data for
  106. * @buf: point to buffer in which to log
  107. * @len: amount of room in buffer
  108. *
  109. * This routine captures assorted PCI configuration space data,
  110. * and puts them into a buffer for RTAS error logging.
  111. */
  112. static size_t eeh_gather_pci_data(struct pci_dn *pdn, char * buf, size_t len)
  113. {
  114. struct pci_dev *dev = pdn->pcidev;
  115. u32 cfg;
  116. int cap, i;
  117. int n = 0;
  118. n += scnprintf(buf+n, len-n, "%s\n", pdn->node->full_name);
  119. printk(KERN_WARNING "EEH: of node=%s\n", pdn->node->full_name);
  120. rtas_read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
  121. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  122. printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
  123. rtas_read_config(pdn, PCI_COMMAND, 4, &cfg);
  124. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  125. printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
  126. if (!dev) {
  127. printk(KERN_WARNING "EEH: no PCI device for this of node\n");
  128. return n;
  129. }
  130. /* Gather bridge-specific registers */
  131. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  132. rtas_read_config(pdn, PCI_SEC_STATUS, 2, &cfg);
  133. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  134. printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
  135. rtas_read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);
  136. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  137. printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
  138. }
  139. /* Dump out the PCI-X command and status regs */
  140. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  141. if (cap) {
  142. rtas_read_config(pdn, cap, 4, &cfg);
  143. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  144. printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
  145. rtas_read_config(pdn, cap+4, 4, &cfg);
  146. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  147. printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
  148. }
  149. /* If PCI-E capable, dump PCI-E cap 10, and the AER */
  150. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  151. if (cap) {
  152. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  153. printk(KERN_WARNING
  154. "EEH: PCI-E capabilities and status follow:\n");
  155. for (i=0; i<=8; i++) {
  156. rtas_read_config(pdn, cap+4*i, 4, &cfg);
  157. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  158. printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
  159. }
  160. cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  161. if (cap) {
  162. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  163. printk(KERN_WARNING
  164. "EEH: PCI-E AER capability register set follows:\n");
  165. for (i=0; i<14; i++) {
  166. rtas_read_config(pdn, cap+4*i, 4, &cfg);
  167. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  168. printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
  169. }
  170. }
  171. }
  172. /* Gather status on devices under the bridge */
  173. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  174. struct device_node *dn;
  175. for_each_child_of_node(pdn->node, dn) {
  176. pdn = PCI_DN(dn);
  177. if (pdn)
  178. n += eeh_gather_pci_data(pdn, buf+n, len-n);
  179. }
  180. }
  181. return n;
  182. }
  183. /**
  184. * eeh_slot_error_detail - Generate combined log including driver log and error log
  185. * @pdn: device node
  186. * @severity: temporary or permanent error log
  187. *
  188. * This routine should be called to generate the combined log, which
  189. * is comprised of driver log and error log. The driver log is figured
  190. * out from the config space of the corresponding PCI device, while
  191. * the error log is fetched through platform dependent function call.
  192. */
  193. void eeh_slot_error_detail(struct pci_dn *pdn, int severity)
  194. {
  195. size_t loglen = 0;
  196. pci_regs_buf[0] = 0;
  197. eeh_pci_enable(pdn, EEH_OPT_THAW_MMIO);
  198. eeh_ops->configure_bridge(pdn->node);
  199. eeh_restore_bars(pdn);
  200. loglen = eeh_gather_pci_data(pdn, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
  201. eeh_ops->get_log(pdn->node, severity, pci_regs_buf, loglen);
  202. }
  203. /**
  204. * eeh_token_to_phys - Convert EEH address token to phys address
  205. * @token: I/O token, should be address in the form 0xA....
  206. *
  207. * This routine should be called to convert virtual I/O address
  208. * to physical one.
  209. */
  210. static inline unsigned long eeh_token_to_phys(unsigned long token)
  211. {
  212. pte_t *ptep;
  213. unsigned long pa;
  214. ptep = find_linux_pte(init_mm.pgd, token);
  215. if (!ptep)
  216. return token;
  217. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  218. return pa | (token & (PAGE_SIZE-1));
  219. }
  220. /**
  221. * eeh_find_device_pe - Retrieve the PE for the given device
  222. * @dn: device node
  223. *
  224. * Return the PE under which this device lies
  225. */
  226. struct device_node *eeh_find_device_pe(struct device_node *dn)
  227. {
  228. while ((dn->parent) && PCI_DN(dn->parent) &&
  229. (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  230. dn = dn->parent;
  231. }
  232. return dn;
  233. }
  234. /**
  235. * __eeh_mark_slot - Mark all child devices as failed
  236. * @parent: parent device
  237. * @mode_flag: failure flag
  238. *
  239. * Mark all devices that are children of this device as failed.
  240. * Mark the device driver too, so that it can see the failure
  241. * immediately; this is critical, since some drivers poll
  242. * status registers in interrupts ... If a driver is polling,
  243. * and the slot is frozen, then the driver can deadlock in
  244. * an interrupt context, which is bad.
  245. */
  246. static void __eeh_mark_slot(struct device_node *parent, int mode_flag)
  247. {
  248. struct device_node *dn;
  249. for_each_child_of_node(parent, dn) {
  250. if (PCI_DN(dn)) {
  251. /* Mark the pci device driver too */
  252. struct pci_dev *dev = PCI_DN(dn)->pcidev;
  253. PCI_DN(dn)->eeh_mode |= mode_flag;
  254. if (dev && dev->driver)
  255. dev->error_state = pci_channel_io_frozen;
  256. __eeh_mark_slot(dn, mode_flag);
  257. }
  258. }
  259. }
  260. /**
  261. * eeh_mark_slot - Mark the indicated device and its children as failed
  262. * @dn: parent device
  263. * @mode_flag: failure flag
  264. *
  265. * Mark the indicated device and its child devices as failed.
  266. * The device drivers are marked as failed as well.
  267. */
  268. void eeh_mark_slot(struct device_node *dn, int mode_flag)
  269. {
  270. struct pci_dev *dev;
  271. dn = eeh_find_device_pe(dn);
  272. /* Back up one, since config addrs might be shared */
  273. if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
  274. dn = dn->parent;
  275. PCI_DN(dn)->eeh_mode |= mode_flag;
  276. /* Mark the pci device too */
  277. dev = PCI_DN(dn)->pcidev;
  278. if (dev)
  279. dev->error_state = pci_channel_io_frozen;
  280. __eeh_mark_slot(dn, mode_flag);
  281. }
  282. /**
  283. * __eeh_clear_slot - Clear failure flag for the child devices
  284. * @parent: parent device
  285. * @mode_flag: flag to be cleared
  286. *
  287. * Clear failure flag for the child devices.
  288. */
  289. static void __eeh_clear_slot(struct device_node *parent, int mode_flag)
  290. {
  291. struct device_node *dn;
  292. for_each_child_of_node(parent, dn) {
  293. if (PCI_DN(dn)) {
  294. PCI_DN(dn)->eeh_mode &= ~mode_flag;
  295. PCI_DN(dn)->eeh_check_count = 0;
  296. __eeh_clear_slot(dn, mode_flag);
  297. }
  298. }
  299. }
  300. /**
  301. * eeh_clear_slot - Clear failure flag for the indicated device and its children
  302. * @dn: parent device
  303. * @mode_flag: flag to be cleared
  304. *
  305. * Clear failure flag for the indicated device and its children.
  306. */
  307. void eeh_clear_slot(struct device_node *dn, int mode_flag)
  308. {
  309. unsigned long flags;
  310. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  311. dn = eeh_find_device_pe(dn);
  312. /* Back up one, since config addrs might be shared */
  313. if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
  314. dn = dn->parent;
  315. PCI_DN(dn)->eeh_mode &= ~mode_flag;
  316. PCI_DN(dn)->eeh_check_count = 0;
  317. __eeh_clear_slot(dn, mode_flag);
  318. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  319. }
  320. /**
  321. * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
  322. * @dn: device node
  323. * @dev: pci device, if known
  324. *
  325. * Check for an EEH failure for the given device node. Call this
  326. * routine if the result of a read was all 0xff's and you want to
  327. * find out if this is due to an EEH slot freeze. This routine
  328. * will query firmware for the EEH status.
  329. *
  330. * Returns 0 if there has not been an EEH error; otherwise returns
  331. * a non-zero value and queues up a slot isolation event notification.
  332. *
  333. * It is safe to call this routine in an interrupt context.
  334. */
  335. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  336. {
  337. int ret;
  338. unsigned long flags;
  339. struct pci_dn *pdn;
  340. int rc = 0;
  341. const char *location;
  342. total_mmio_ffs++;
  343. if (!eeh_subsystem_enabled)
  344. return 0;
  345. if (!dn) {
  346. no_dn++;
  347. return 0;
  348. }
  349. dn = eeh_find_device_pe(dn);
  350. pdn = PCI_DN(dn);
  351. /* Access to IO BARs might get this far and still not want checking. */
  352. if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  353. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  354. ignored_check++;
  355. pr_debug("EEH: Ignored check (%x) for %s %s\n",
  356. pdn->eeh_mode, eeh_pci_name(dev), dn->full_name);
  357. return 0;
  358. }
  359. if (!pdn->eeh_config_addr && !pdn->eeh_pe_config_addr) {
  360. no_cfg_addr++;
  361. return 0;
  362. }
  363. /* If we already have a pending isolation event for this
  364. * slot, we know it's bad already, we don't need to check.
  365. * Do this checking under a lock; as multiple PCI devices
  366. * in one slot might report errors simultaneously, and we
  367. * only want one error recovery routine running.
  368. */
  369. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  370. rc = 1;
  371. if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
  372. pdn->eeh_check_count ++;
  373. if (pdn->eeh_check_count % EEH_MAX_FAILS == 0) {
  374. location = of_get_property(dn, "ibm,loc-code", NULL);
  375. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  376. "location=%s driver=%s pci addr=%s\n",
  377. pdn->eeh_check_count, location,
  378. eeh_driver_name(dev), eeh_pci_name(dev));
  379. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  380. eeh_driver_name(dev));
  381. dump_stack();
  382. }
  383. goto dn_unlock;
  384. }
  385. /*
  386. * Now test for an EEH failure. This is VERY expensive.
  387. * Note that the eeh_config_addr may be a parent device
  388. * in the case of a device behind a bridge, or it may be
  389. * function zero of a multi-function device.
  390. * In any case they must share a common PHB.
  391. */
  392. ret = eeh_ops->get_state(pdn->node, NULL);
  393. /* Note that config-io to empty slots may fail;
  394. * they are empty when they don't have children.
  395. * We will punt with the following conditions: Failure to get
  396. * PE's state, EEH not support and Permanently unavailable
  397. * state, PE is in good state.
  398. */
  399. if ((ret < 0) ||
  400. (ret == EEH_STATE_NOT_SUPPORT) ||
  401. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  402. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  403. false_positives++;
  404. pdn->eeh_false_positives ++;
  405. rc = 0;
  406. goto dn_unlock;
  407. }
  408. slot_resets++;
  409. /* Avoid repeated reports of this failure, including problems
  410. * with other functions on this device, and functions under
  411. * bridges.
  412. */
  413. eeh_mark_slot(dn, EEH_MODE_ISOLATED);
  414. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  415. eeh_send_failure_event(dn, dev);
  416. /* Most EEH events are due to device driver bugs. Having
  417. * a stack trace will help the device-driver authors figure
  418. * out what happened. So print that out.
  419. */
  420. dump_stack();
  421. return 1;
  422. dn_unlock:
  423. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  424. return rc;
  425. }
  426. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  427. /**
  428. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  429. * @token: I/O token, should be address in the form 0xA....
  430. * @val: value, should be all 1's (XXX why do we need this arg??)
  431. *
  432. * Check for an EEH failure at the given token address. Call this
  433. * routine if the result of a read was all 0xff's and you want to
  434. * find out if this is due to an EEH slot freeze event. This routine
  435. * will query firmware for the EEH status.
  436. *
  437. * Note this routine is safe to call in an interrupt context.
  438. */
  439. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  440. {
  441. unsigned long addr;
  442. struct pci_dev *dev;
  443. struct device_node *dn;
  444. /* Finding the phys addr + pci device; this is pretty quick. */
  445. addr = eeh_token_to_phys((unsigned long __force) token);
  446. dev = pci_addr_cache_get_device(addr);
  447. if (!dev) {
  448. no_device++;
  449. return val;
  450. }
  451. dn = pci_device_to_OF_node(dev);
  452. eeh_dn_check_failure(dn, dev);
  453. pci_dev_put(dev);
  454. return val;
  455. }
  456. EXPORT_SYMBOL(eeh_check_failure);
  457. /**
  458. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  459. * @pdn pci device node
  460. *
  461. * This routine should be called to reenable frozen MMIO or DMA
  462. * so that it would work correctly again. It's useful while doing
  463. * recovery or log collection on the indicated device.
  464. */
  465. int eeh_pci_enable(struct pci_dn *pdn, int function)
  466. {
  467. int rc;
  468. rc = eeh_ops->set_option(pdn->node, function);
  469. if (rc)
  470. printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
  471. function, rc, pdn->node->full_name);
  472. rc = eeh_ops->wait_state(pdn->node, PCI_BUS_RESET_WAIT_MSEC);
  473. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  474. (function == EEH_OPT_THAW_MMIO))
  475. return 0;
  476. return rc;
  477. }
  478. /**
  479. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  480. * @dev: pci device struct
  481. * @state: reset state to enter
  482. *
  483. * Return value:
  484. * 0 if success
  485. */
  486. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  487. {
  488. struct device_node *dn = pci_device_to_OF_node(dev);
  489. switch (state) {
  490. case pcie_deassert_reset:
  491. eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
  492. break;
  493. case pcie_hot_reset:
  494. eeh_ops->reset(dn, EEH_RESET_HOT);
  495. break;
  496. case pcie_warm_reset:
  497. eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
  498. break;
  499. default:
  500. return -EINVAL;
  501. };
  502. return 0;
  503. }
  504. /**
  505. * __eeh_set_pe_freset - Check the required reset for child devices
  506. * @parent: parent device
  507. * @freset: return value
  508. *
  509. * Each device might have its preferred reset type: fundamental or
  510. * hot reset. The routine is used to collect the information from
  511. * the child devices so that they could be reset accordingly.
  512. */
  513. void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
  514. {
  515. struct device_node *dn;
  516. for_each_child_of_node(parent, dn) {
  517. if (PCI_DN(dn)) {
  518. struct pci_dev *dev = PCI_DN(dn)->pcidev;
  519. if (dev && dev->driver)
  520. *freset |= dev->needs_freset;
  521. __eeh_set_pe_freset(dn, freset);
  522. }
  523. }
  524. }
  525. /**
  526. * eeh_set_pe_freset - Check the required reset for the indicated device and its children
  527. * @dn: parent device
  528. * @freset: return value
  529. *
  530. * Each device might have its preferred reset type: fundamental or
  531. * hot reset. The routine is used to collected the information for
  532. * the indicated device and its children so that the bunch of the
  533. * devices could be reset properly.
  534. */
  535. void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
  536. {
  537. struct pci_dev *dev;
  538. dn = eeh_find_device_pe(dn);
  539. /* Back up one, since config addrs might be shared */
  540. if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
  541. dn = dn->parent;
  542. dev = PCI_DN(dn)->pcidev;
  543. if (dev)
  544. *freset |= dev->needs_freset;
  545. __eeh_set_pe_freset(dn, freset);
  546. }
  547. /**
  548. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  549. * @pdn: pci device node to be reset.
  550. *
  551. * Assert the PCI #RST line for 1/4 second.
  552. */
  553. static void eeh_reset_pe_once(struct pci_dn *pdn)
  554. {
  555. unsigned int freset = 0;
  556. /* Determine type of EEH reset required for
  557. * Partitionable Endpoint, a hot-reset (1)
  558. * or a fundamental reset (3).
  559. * A fundamental reset required by any device under
  560. * Partitionable Endpoint trumps hot-reset.
  561. */
  562. eeh_set_pe_freset(pdn->node, &freset);
  563. if (freset)
  564. eeh_ops->reset(pdn->node, EEH_RESET_FUNDAMENTAL);
  565. else
  566. eeh_ops->reset(pdn->node, EEH_RESET_HOT);
  567. /* The PCI bus requires that the reset be held high for at least
  568. * a 100 milliseconds. We wait a bit longer 'just in case'.
  569. */
  570. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  571. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  572. /* We might get hit with another EEH freeze as soon as the
  573. * pci slot reset line is dropped. Make sure we don't miss
  574. * these, and clear the flag now.
  575. */
  576. eeh_clear_slot(pdn->node, EEH_MODE_ISOLATED);
  577. eeh_ops->reset(pdn->node, EEH_RESET_DEACTIVATE);
  578. /* After a PCI slot has been reset, the PCI Express spec requires
  579. * a 1.5 second idle time for the bus to stabilize, before starting
  580. * up traffic.
  581. */
  582. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  583. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  584. }
  585. /**
  586. * eeh_reset_pe - Reset the indicated PE
  587. * @pdn: PCI device node
  588. *
  589. * This routine should be called to reset indicated device, including
  590. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  591. * might be involved as well.
  592. */
  593. int eeh_reset_pe(struct pci_dn *pdn)
  594. {
  595. int i, rc;
  596. /* Take three shots at resetting the bus */
  597. for (i=0; i<3; i++) {
  598. eeh_reset_pe_once(pdn);
  599. rc = eeh_ops->wait_state(pdn->node, PCI_BUS_RESET_WAIT_MSEC);
  600. if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
  601. return 0;
  602. if (rc < 0) {
  603. printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
  604. pdn->node->full_name);
  605. return -1;
  606. }
  607. printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
  608. i+1, pdn->node->full_name, rc);
  609. }
  610. return -1;
  611. }
  612. /** Save and restore of PCI BARs
  613. *
  614. * Although firmware will set up BARs during boot, it doesn't
  615. * set up device BAR's after a device reset, although it will,
  616. * if requested, set up bridge configuration. Thus, we need to
  617. * configure the PCI devices ourselves.
  618. */
  619. /**
  620. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  621. * @pdn: pci device node
  622. *
  623. * Loads the PCI configuration space base address registers,
  624. * the expansion ROM base address, the latency timer, and etc.
  625. * from the saved values in the device node.
  626. */
  627. static inline void eeh_restore_one_device_bars(struct pci_dn *pdn)
  628. {
  629. int i;
  630. u32 cmd;
  631. if (NULL==pdn->phb) return;
  632. for (i=4; i<10; i++) {
  633. rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
  634. }
  635. /* 12 == Expansion ROM Address */
  636. rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
  637. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  638. #define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
  639. rtas_write_config(pdn, PCI_CACHE_LINE_SIZE, 1,
  640. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  641. rtas_write_config(pdn, PCI_LATENCY_TIMER, 1,
  642. SAVED_BYTE(PCI_LATENCY_TIMER));
  643. /* max latency, min grant, interrupt pin and line */
  644. rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
  645. /* Restore PERR & SERR bits, some devices require it,
  646. * don't touch the other command bits
  647. */
  648. rtas_read_config(pdn, PCI_COMMAND, 4, &cmd);
  649. if (pdn->config_space[1] & PCI_COMMAND_PARITY)
  650. cmd |= PCI_COMMAND_PARITY;
  651. else
  652. cmd &= ~PCI_COMMAND_PARITY;
  653. if (pdn->config_space[1] & PCI_COMMAND_SERR)
  654. cmd |= PCI_COMMAND_SERR;
  655. else
  656. cmd &= ~PCI_COMMAND_SERR;
  657. rtas_write_config(pdn, PCI_COMMAND, 4, cmd);
  658. }
  659. /**
  660. * eeh_restore_bars - Restore the PCI config space info
  661. * @pdn: PCI device node
  662. *
  663. * This routine performs a recursive walk to the children
  664. * of this device as well.
  665. */
  666. void eeh_restore_bars(struct pci_dn *pdn)
  667. {
  668. struct device_node *dn;
  669. if (!pdn)
  670. return;
  671. if ((pdn->eeh_mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(pdn->class_code))
  672. eeh_restore_one_device_bars(pdn);
  673. for_each_child_of_node(pdn->node, dn)
  674. eeh_restore_bars(PCI_DN(dn));
  675. }
  676. /**
  677. * eeh_save_bars - Save device bars
  678. * @pdn: PCI device node
  679. *
  680. * Save the values of the device bars. Unlike the restore
  681. * routine, this routine is *not* recursive. This is because
  682. * PCI devices are added individually; but, for the restore,
  683. * an entire slot is reset at a time.
  684. */
  685. static void eeh_save_bars(struct pci_dn *pdn)
  686. {
  687. int i;
  688. if (!pdn )
  689. return;
  690. for (i = 0; i < 16; i++)
  691. rtas_read_config(pdn, i * 4, 4, &pdn->config_space[i]);
  692. }
  693. /**
  694. * eeh_early_enable - Early enable EEH on the indicated device
  695. * @dn: device node
  696. * @data: BUID
  697. *
  698. * Enable EEH functionality on the specified PCI device. The function
  699. * is expected to be called before real PCI probing is done. However,
  700. * the PHBs have been initialized at this point.
  701. */
  702. static void *eeh_early_enable(struct device_node *dn, void *data)
  703. {
  704. int ret;
  705. const u32 *class_code = of_get_property(dn, "class-code", NULL);
  706. const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
  707. const u32 *device_id = of_get_property(dn, "device-id", NULL);
  708. const u32 *regs;
  709. int enable;
  710. struct pci_dn *pdn = PCI_DN(dn);
  711. pdn->class_code = 0;
  712. pdn->eeh_mode = 0;
  713. pdn->eeh_check_count = 0;
  714. pdn->eeh_freeze_count = 0;
  715. pdn->eeh_false_positives = 0;
  716. if (!of_device_is_available(dn))
  717. return NULL;
  718. /* Ignore bad nodes. */
  719. if (!class_code || !vendor_id || !device_id)
  720. return NULL;
  721. /* There is nothing to check on PCI to ISA bridges */
  722. if (dn->type && !strcmp(dn->type, "isa")) {
  723. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  724. return NULL;
  725. }
  726. pdn->class_code = *class_code;
  727. /* Ok... see if this device supports EEH. Some do, some don't,
  728. * and the only way to find out is to check each and every one.
  729. */
  730. regs = of_get_property(dn, "reg", NULL);
  731. if (regs) {
  732. /* First register entry is addr (00BBSS00) */
  733. /* Try to enable eeh */
  734. ret = eeh_ops->set_option(dn, EEH_OPT_ENABLE);
  735. enable = 0;
  736. if (ret == 0) {
  737. pdn->eeh_config_addr = regs[0];
  738. /* If the newer, better, ibm,get-config-addr-info is supported,
  739. * then use that instead.
  740. */
  741. pdn->eeh_pe_config_addr = eeh_ops->get_pe_addr(dn);
  742. /* Some older systems (Power4) allow the
  743. * ibm,set-eeh-option call to succeed even on nodes
  744. * where EEH is not supported. Verify support
  745. * explicitly.
  746. */
  747. ret = eeh_ops->get_state(pdn->node, NULL);
  748. if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
  749. enable = 1;
  750. }
  751. if (enable) {
  752. eeh_subsystem_enabled = 1;
  753. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  754. pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
  755. dn->full_name, pdn->eeh_config_addr,
  756. pdn->eeh_pe_config_addr);
  757. } else {
  758. /* This device doesn't support EEH, but it may have an
  759. * EEH parent, in which case we mark it as supported.
  760. */
  761. if (dn->parent && PCI_DN(dn->parent)
  762. && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  763. /* Parent supports EEH. */
  764. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  765. pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
  766. return NULL;
  767. }
  768. }
  769. } else {
  770. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  771. dn->full_name);
  772. }
  773. eeh_save_bars(pdn);
  774. return NULL;
  775. }
  776. /**
  777. * eeh_ops_register - Register platform dependent EEH operations
  778. * @ops: platform dependent EEH operations
  779. *
  780. * Register the platform dependent EEH operation callback
  781. * functions. The platform should call this function before
  782. * any other EEH operations.
  783. */
  784. int __init eeh_ops_register(struct eeh_ops *ops)
  785. {
  786. if (!ops->name) {
  787. pr_warning("%s: Invalid EEH ops name for %p\n",
  788. __func__, ops);
  789. return -EINVAL;
  790. }
  791. if (eeh_ops && eeh_ops != ops) {
  792. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  793. __func__, eeh_ops->name, ops->name);
  794. return -EEXIST;
  795. }
  796. eeh_ops = ops;
  797. return 0;
  798. }
  799. /**
  800. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  801. * @name: name of EEH platform operations
  802. *
  803. * Unregister the platform dependent EEH operation callback
  804. * functions.
  805. */
  806. int __exit eeh_ops_unregister(const char *name)
  807. {
  808. if (!name || !strlen(name)) {
  809. pr_warning("%s: Invalid EEH ops name\n",
  810. __func__);
  811. return -EINVAL;
  812. }
  813. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  814. eeh_ops = NULL;
  815. return 0;
  816. }
  817. return -EEXIST;
  818. }
  819. /**
  820. * eeh_init - EEH initialization
  821. *
  822. * Initialize EEH by trying to enable it for all of the adapters in the system.
  823. * As a side effect we can determine here if eeh is supported at all.
  824. * Note that we leave EEH on so failed config cycles won't cause a machine
  825. * check. If a user turns off EEH for a particular adapter they are really
  826. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  827. * grant access to a slot if EEH isn't enabled, and so we always enable
  828. * EEH for all slots/all devices.
  829. *
  830. * The eeh-force-off option disables EEH checking globally, for all slots.
  831. * Even if force-off is set, the EEH hardware is still enabled, so that
  832. * newer systems can boot.
  833. */
  834. void __init eeh_init(void)
  835. {
  836. struct device_node *phb, *np;
  837. int ret;
  838. /* call platform initialization function */
  839. if (!eeh_ops) {
  840. pr_warning("%s: Platform EEH operation not found\n",
  841. __func__);
  842. return;
  843. } else if ((ret = eeh_ops->init())) {
  844. pr_warning("%s: Failed to call platform init function (%d)\n",
  845. __func__, ret);
  846. return;
  847. }
  848. raw_spin_lock_init(&confirm_error_lock);
  849. np = of_find_node_by_path("/rtas");
  850. if (np == NULL)
  851. return;
  852. /* Enable EEH for all adapters. Note that eeh requires buid's */
  853. for (phb = of_find_node_by_name(NULL, "pci"); phb;
  854. phb = of_find_node_by_name(phb, "pci")) {
  855. unsigned long buid;
  856. buid = get_phb_buid(phb);
  857. if (buid == 0 || PCI_DN(phb) == NULL)
  858. continue;
  859. traverse_pci_devices(phb, eeh_early_enable, NULL);
  860. }
  861. if (eeh_subsystem_enabled)
  862. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  863. else
  864. printk(KERN_WARNING "EEH: No capable adapters found\n");
  865. }
  866. /**
  867. * eeh_add_device_early - Enable EEH for the indicated device_node
  868. * @dn: device node for which to set up EEH
  869. *
  870. * This routine must be used to perform EEH initialization for PCI
  871. * devices that were added after system boot (e.g. hotplug, dlpar).
  872. * This routine must be called before any i/o is performed to the
  873. * adapter (inluding any config-space i/o).
  874. * Whether this actually enables EEH or not for this device depends
  875. * on the CEC architecture, type of the device, on earlier boot
  876. * command-line arguments & etc.
  877. */
  878. static void eeh_add_device_early(struct device_node *dn)
  879. {
  880. struct pci_controller *phb;
  881. if (!dn || !PCI_DN(dn))
  882. return;
  883. phb = PCI_DN(dn)->phb;
  884. /* USB Bus children of PCI devices will not have BUID's */
  885. if (NULL == phb || 0 == phb->buid)
  886. return;
  887. eeh_early_enable(dn, NULL);
  888. }
  889. /**
  890. * eeh_add_device_tree_early - Enable EEH for the indicated device
  891. * @dn: device node
  892. *
  893. * This routine must be used to perform EEH initialization for the
  894. * indicated PCI device that was added after system boot (e.g.
  895. * hotplug, dlpar).
  896. */
  897. void eeh_add_device_tree_early(struct device_node *dn)
  898. {
  899. struct device_node *sib;
  900. for_each_child_of_node(dn, sib)
  901. eeh_add_device_tree_early(sib);
  902. eeh_add_device_early(dn);
  903. }
  904. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  905. /**
  906. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  907. * @dev: pci device for which to set up EEH
  908. *
  909. * This routine must be used to complete EEH initialization for PCI
  910. * devices that were added after system boot (e.g. hotplug, dlpar).
  911. */
  912. static void eeh_add_device_late(struct pci_dev *dev)
  913. {
  914. struct device_node *dn;
  915. struct pci_dn *pdn;
  916. if (!dev || !eeh_subsystem_enabled)
  917. return;
  918. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  919. dn = pci_device_to_OF_node(dev);
  920. pdn = PCI_DN(dn);
  921. if (pdn->pcidev == dev) {
  922. pr_debug("EEH: Already referenced !\n");
  923. return;
  924. }
  925. WARN_ON(pdn->pcidev);
  926. pci_dev_get(dev);
  927. pdn->pcidev = dev;
  928. pci_addr_cache_insert_device(dev);
  929. eeh_sysfs_add_device(dev);
  930. }
  931. /**
  932. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  933. * @bus: PCI bus
  934. *
  935. * This routine must be used to perform EEH initialization for PCI
  936. * devices which are attached to the indicated PCI bus. The PCI bus
  937. * is added after system boot through hotplug or dlpar.
  938. */
  939. void eeh_add_device_tree_late(struct pci_bus *bus)
  940. {
  941. struct pci_dev *dev;
  942. list_for_each_entry(dev, &bus->devices, bus_list) {
  943. eeh_add_device_late(dev);
  944. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  945. struct pci_bus *subbus = dev->subordinate;
  946. if (subbus)
  947. eeh_add_device_tree_late(subbus);
  948. }
  949. }
  950. }
  951. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  952. /**
  953. * eeh_remove_device - Undo EEH setup for the indicated pci device
  954. * @dev: pci device to be removed
  955. *
  956. * This routine should be called when a device is removed from
  957. * a running system (e.g. by hotplug or dlpar). It unregisters
  958. * the PCI device from the EEH subsystem. I/O errors affecting
  959. * this device will no longer be detected after this call; thus,
  960. * i/o errors affecting this slot may leave this device unusable.
  961. */
  962. static void eeh_remove_device(struct pci_dev *dev)
  963. {
  964. struct device_node *dn;
  965. if (!dev || !eeh_subsystem_enabled)
  966. return;
  967. /* Unregister the device with the EEH/PCI address search system */
  968. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  969. dn = pci_device_to_OF_node(dev);
  970. if (PCI_DN(dn)->pcidev == NULL) {
  971. pr_debug("EEH: Not referenced !\n");
  972. return;
  973. }
  974. PCI_DN(dn)->pcidev = NULL;
  975. pci_dev_put(dev);
  976. pci_addr_cache_remove_device(dev);
  977. eeh_sysfs_remove_device(dev);
  978. }
  979. /**
  980. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  981. * @dev: PCI device
  982. *
  983. * This routine must be called when a device is removed from the
  984. * running system through hotplug or dlpar. The corresponding
  985. * PCI address cache will be removed.
  986. */
  987. void eeh_remove_bus_device(struct pci_dev *dev)
  988. {
  989. struct pci_bus *bus = dev->subordinate;
  990. struct pci_dev *child, *tmp;
  991. eeh_remove_device(dev);
  992. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  993. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  994. eeh_remove_bus_device(child);
  995. }
  996. }
  997. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  998. static int proc_eeh_show(struct seq_file *m, void *v)
  999. {
  1000. if (0 == eeh_subsystem_enabled) {
  1001. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1002. seq_printf(m, "eeh_total_mmio_ffs=%ld\n", total_mmio_ffs);
  1003. } else {
  1004. seq_printf(m, "EEH Subsystem is enabled\n");
  1005. seq_printf(m,
  1006. "no device=%ld\n"
  1007. "no device node=%ld\n"
  1008. "no config address=%ld\n"
  1009. "check not wanted=%ld\n"
  1010. "eeh_total_mmio_ffs=%ld\n"
  1011. "eeh_false_positives=%ld\n"
  1012. "eeh_slot_resets=%ld\n",
  1013. no_device, no_dn, no_cfg_addr,
  1014. ignored_check, total_mmio_ffs,
  1015. false_positives,
  1016. slot_resets);
  1017. }
  1018. return 0;
  1019. }
  1020. static int proc_eeh_open(struct inode *inode, struct file *file)
  1021. {
  1022. return single_open(file, proc_eeh_show, NULL);
  1023. }
  1024. static const struct file_operations proc_eeh_operations = {
  1025. .open = proc_eeh_open,
  1026. .read = seq_read,
  1027. .llseek = seq_lseek,
  1028. .release = single_release,
  1029. };
  1030. static int __init eeh_init_proc(void)
  1031. {
  1032. if (machine_is(pseries))
  1033. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  1034. return 0;
  1035. }
  1036. __initcall(eeh_init_proc);