eeh.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * eeh.c
  3. * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/notifier.h>
  22. #include <linux/pci.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/rbtree.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/atomic.h>
  28. #include <asm/eeh.h>
  29. #include <asm/io.h>
  30. #include <asm/machdep.h>
  31. #include <asm/rtas.h>
  32. #include <asm/atomic.h>
  33. #include <asm/systemcfg.h>
  34. #include <asm/ppc-pci.h>
  35. #undef DEBUG
  36. /** Overview:
  37. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  38. * dealing with PCI bus errors that can't be dealt with within the
  39. * usual PCI framework, except by check-stopping the CPU. Systems
  40. * that are designed for high-availability/reliability cannot afford
  41. * to crash due to a "mere" PCI error, thus the need for EEH.
  42. * An EEH-capable bridge operates by converting a detected error
  43. * into a "slot freeze", taking the PCI adapter off-line, making
  44. * the slot behave, from the OS'es point of view, as if the slot
  45. * were "empty": all reads return 0xff's and all writes are silently
  46. * ignored. EEH slot isolation events can be triggered by parity
  47. * errors on the address or data busses (e.g. during posted writes),
  48. * which in turn might be caused by low voltage on the bus, dust,
  49. * vibration, humidity, radioactivity or plain-old failed hardware.
  50. *
  51. * Note, however, that one of the leading causes of EEH slot
  52. * freeze events are buggy device drivers, buggy device microcode,
  53. * or buggy device hardware. This is because any attempt by the
  54. * device to bus-master data to a memory address that is not
  55. * assigned to the device will trigger a slot freeze. (The idea
  56. * is to prevent devices-gone-wild from corrupting system memory).
  57. * Buggy hardware/drivers will have a miserable time co-existing
  58. * with EEH.
  59. *
  60. * Ideally, a PCI device driver, when suspecting that an isolation
  61. * event has occured (e.g. by reading 0xff's), will then ask EEH
  62. * whether this is the case, and then take appropriate steps to
  63. * reset the PCI slot, the PCI device, and then resume operations.
  64. * However, until that day, the checking is done here, with the
  65. * eeh_check_failure() routine embedded in the MMIO macros. If
  66. * the slot is found to be isolated, an "EEH Event" is synthesized
  67. * and sent out for processing.
  68. */
  69. /* EEH event workqueue setup. */
  70. static DEFINE_SPINLOCK(eeh_eventlist_lock);
  71. LIST_HEAD(eeh_eventlist);
  72. static void eeh_event_handler(void *);
  73. DECLARE_WORK(eeh_event_wq, eeh_event_handler, NULL);
  74. static struct notifier_block *eeh_notifier_chain;
  75. /* If a device driver keeps reading an MMIO register in an interrupt
  76. * handler after a slot isolation event has occurred, we assume it
  77. * is broken and panic. This sets the threshold for how many read
  78. * attempts we allow before panicking.
  79. */
  80. #define EEH_MAX_FAILS 100000
  81. /* RTAS tokens */
  82. static int ibm_set_eeh_option;
  83. static int ibm_set_slot_reset;
  84. static int ibm_read_slot_reset_state;
  85. static int ibm_read_slot_reset_state2;
  86. static int ibm_slot_error_detail;
  87. static int eeh_subsystem_enabled;
  88. /* Lock to avoid races due to multiple reports of an error */
  89. static DEFINE_SPINLOCK(confirm_error_lock);
  90. /* Buffer for reporting slot-error-detail rtas calls */
  91. static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
  92. static DEFINE_SPINLOCK(slot_errbuf_lock);
  93. static int eeh_error_buf_size;
  94. /* System monitoring statistics */
  95. static DEFINE_PER_CPU(unsigned long, no_device);
  96. static DEFINE_PER_CPU(unsigned long, no_dn);
  97. static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
  98. static DEFINE_PER_CPU(unsigned long, ignored_check);
  99. static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
  100. static DEFINE_PER_CPU(unsigned long, false_positives);
  101. static DEFINE_PER_CPU(unsigned long, ignored_failures);
  102. static DEFINE_PER_CPU(unsigned long, slot_resets);
  103. /**
  104. * The pci address cache subsystem. This subsystem places
  105. * PCI device address resources into a red-black tree, sorted
  106. * according to the address range, so that given only an i/o
  107. * address, the corresponding PCI device can be **quickly**
  108. * found. It is safe to perform an address lookup in an interrupt
  109. * context; this ability is an important feature.
  110. *
  111. * Currently, the only customer of this code is the EEH subsystem;
  112. * thus, this code has been somewhat tailored to suit EEH better.
  113. * In particular, the cache does *not* hold the addresses of devices
  114. * for which EEH is not enabled.
  115. *
  116. * (Implementation Note: The RB tree seems to be better/faster
  117. * than any hash algo I could think of for this problem, even
  118. * with the penalty of slow pointer chases for d-cache misses).
  119. */
  120. struct pci_io_addr_range
  121. {
  122. struct rb_node rb_node;
  123. unsigned long addr_lo;
  124. unsigned long addr_hi;
  125. struct pci_dev *pcidev;
  126. unsigned int flags;
  127. };
  128. static struct pci_io_addr_cache
  129. {
  130. struct rb_root rb_root;
  131. spinlock_t piar_lock;
  132. } pci_io_addr_cache_root;
  133. static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
  134. {
  135. struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
  136. while (n) {
  137. struct pci_io_addr_range *piar;
  138. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  139. if (addr < piar->addr_lo) {
  140. n = n->rb_left;
  141. } else {
  142. if (addr > piar->addr_hi) {
  143. n = n->rb_right;
  144. } else {
  145. pci_dev_get(piar->pcidev);
  146. return piar->pcidev;
  147. }
  148. }
  149. }
  150. return NULL;
  151. }
  152. /**
  153. * pci_get_device_by_addr - Get device, given only address
  154. * @addr: mmio (PIO) phys address or i/o port number
  155. *
  156. * Given an mmio phys address, or a port number, find a pci device
  157. * that implements this address. Be sure to pci_dev_put the device
  158. * when finished. I/O port numbers are assumed to be offset
  159. * from zero (that is, they do *not* have pci_io_addr added in).
  160. * It is safe to call this function within an interrupt.
  161. */
  162. static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
  163. {
  164. struct pci_dev *dev;
  165. unsigned long flags;
  166. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  167. dev = __pci_get_device_by_addr(addr);
  168. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  169. return dev;
  170. }
  171. #ifdef DEBUG
  172. /*
  173. * Handy-dandy debug print routine, does nothing more
  174. * than print out the contents of our addr cache.
  175. */
  176. static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
  177. {
  178. struct rb_node *n;
  179. int cnt = 0;
  180. n = rb_first(&cache->rb_root);
  181. while (n) {
  182. struct pci_io_addr_range *piar;
  183. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  184. printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
  185. (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
  186. piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
  187. cnt++;
  188. n = rb_next(n);
  189. }
  190. }
  191. #endif
  192. /* Insert address range into the rb tree. */
  193. static struct pci_io_addr_range *
  194. pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
  195. unsigned long ahi, unsigned int flags)
  196. {
  197. struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
  198. struct rb_node *parent = NULL;
  199. struct pci_io_addr_range *piar;
  200. /* Walk tree, find a place to insert into tree */
  201. while (*p) {
  202. parent = *p;
  203. piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
  204. if (ahi < piar->addr_lo) {
  205. p = &parent->rb_left;
  206. } else if (alo > piar->addr_hi) {
  207. p = &parent->rb_right;
  208. } else {
  209. if (dev != piar->pcidev ||
  210. alo != piar->addr_lo || ahi != piar->addr_hi) {
  211. printk(KERN_WARNING "PIAR: overlapping address range\n");
  212. }
  213. return piar;
  214. }
  215. }
  216. piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
  217. if (!piar)
  218. return NULL;
  219. piar->addr_lo = alo;
  220. piar->addr_hi = ahi;
  221. piar->pcidev = dev;
  222. piar->flags = flags;
  223. #ifdef DEBUG
  224. printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
  225. alo, ahi, pci_name (dev));
  226. #endif
  227. rb_link_node(&piar->rb_node, parent, p);
  228. rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
  229. return piar;
  230. }
  231. static void __pci_addr_cache_insert_device(struct pci_dev *dev)
  232. {
  233. struct device_node *dn;
  234. struct pci_dn *pdn;
  235. int i;
  236. int inserted = 0;
  237. dn = pci_device_to_OF_node(dev);
  238. if (!dn) {
  239. printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
  240. return;
  241. }
  242. /* Skip any devices for which EEH is not enabled. */
  243. pdn = PCI_DN(dn);
  244. if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  245. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  246. #ifdef DEBUG
  247. printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
  248. pci_name(dev), pdn->node->full_name);
  249. #endif
  250. return;
  251. }
  252. /* The cache holds a reference to the device... */
  253. pci_dev_get(dev);
  254. /* Walk resources on this device, poke them into the tree */
  255. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  256. unsigned long start = pci_resource_start(dev,i);
  257. unsigned long end = pci_resource_end(dev,i);
  258. unsigned int flags = pci_resource_flags(dev,i);
  259. /* We are interested only bus addresses, not dma or other stuff */
  260. if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  261. continue;
  262. if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
  263. continue;
  264. pci_addr_cache_insert(dev, start, end, flags);
  265. inserted = 1;
  266. }
  267. /* If there was nothing to add, the cache has no reference... */
  268. if (!inserted)
  269. pci_dev_put(dev);
  270. }
  271. /**
  272. * pci_addr_cache_insert_device - Add a device to the address cache
  273. * @dev: PCI device whose I/O addresses we are interested in.
  274. *
  275. * In order to support the fast lookup of devices based on addresses,
  276. * we maintain a cache of devices that can be quickly searched.
  277. * This routine adds a device to that cache.
  278. */
  279. static void pci_addr_cache_insert_device(struct pci_dev *dev)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  283. __pci_addr_cache_insert_device(dev);
  284. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  285. }
  286. static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
  287. {
  288. struct rb_node *n;
  289. int removed = 0;
  290. restart:
  291. n = rb_first(&pci_io_addr_cache_root.rb_root);
  292. while (n) {
  293. struct pci_io_addr_range *piar;
  294. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  295. if (piar->pcidev == dev) {
  296. rb_erase(n, &pci_io_addr_cache_root.rb_root);
  297. removed = 1;
  298. kfree(piar);
  299. goto restart;
  300. }
  301. n = rb_next(n);
  302. }
  303. /* The cache no longer holds its reference to this device... */
  304. if (removed)
  305. pci_dev_put(dev);
  306. }
  307. /**
  308. * pci_addr_cache_remove_device - remove pci device from addr cache
  309. * @dev: device to remove
  310. *
  311. * Remove a device from the addr-cache tree.
  312. * This is potentially expensive, since it will walk
  313. * the tree multiple times (once per resource).
  314. * But so what; device removal doesn't need to be that fast.
  315. */
  316. static void pci_addr_cache_remove_device(struct pci_dev *dev)
  317. {
  318. unsigned long flags;
  319. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  320. __pci_addr_cache_remove_device(dev);
  321. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  322. }
  323. /**
  324. * pci_addr_cache_build - Build a cache of I/O addresses
  325. *
  326. * Build a cache of pci i/o addresses. This cache will be used to
  327. * find the pci device that corresponds to a given address.
  328. * This routine scans all pci busses to build the cache.
  329. * Must be run late in boot process, after the pci controllers
  330. * have been scaned for devices (after all device resources are known).
  331. */
  332. void __init pci_addr_cache_build(void)
  333. {
  334. struct pci_dev *dev = NULL;
  335. if (!eeh_subsystem_enabled)
  336. return;
  337. spin_lock_init(&pci_io_addr_cache_root.piar_lock);
  338. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  339. /* Ignore PCI bridges ( XXX why ??) */
  340. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  341. continue;
  342. }
  343. pci_addr_cache_insert_device(dev);
  344. }
  345. #ifdef DEBUG
  346. /* Verify tree built up above, echo back the list of addrs. */
  347. pci_addr_cache_print(&pci_io_addr_cache_root);
  348. #endif
  349. }
  350. /* --------------------------------------------------------------- */
  351. /* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
  352. void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
  353. {
  354. unsigned long flags;
  355. int rc;
  356. /* Log the error with the rtas logger */
  357. spin_lock_irqsave(&slot_errbuf_lock, flags);
  358. memset(slot_errbuf, 0, eeh_error_buf_size);
  359. rc = rtas_call(ibm_slot_error_detail,
  360. 8, 1, NULL, pdn->eeh_config_addr,
  361. BUID_HI(pdn->phb->buid),
  362. BUID_LO(pdn->phb->buid), NULL, 0,
  363. virt_to_phys(slot_errbuf),
  364. eeh_error_buf_size,
  365. severity);
  366. if (rc == 0)
  367. log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
  368. spin_unlock_irqrestore(&slot_errbuf_lock, flags);
  369. }
  370. /**
  371. * eeh_register_notifier - Register to find out about EEH events.
  372. * @nb: notifier block to callback on events
  373. */
  374. int eeh_register_notifier(struct notifier_block *nb)
  375. {
  376. return notifier_chain_register(&eeh_notifier_chain, nb);
  377. }
  378. /**
  379. * eeh_unregister_notifier - Unregister to an EEH event notifier.
  380. * @nb: notifier block to callback on events
  381. */
  382. int eeh_unregister_notifier(struct notifier_block *nb)
  383. {
  384. return notifier_chain_unregister(&eeh_notifier_chain, nb);
  385. }
  386. /**
  387. * read_slot_reset_state - Read the reset state of a device node's slot
  388. * @dn: device node to read
  389. * @rets: array to return results in
  390. */
  391. static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
  392. {
  393. int token, outputs;
  394. if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
  395. token = ibm_read_slot_reset_state2;
  396. outputs = 4;
  397. } else {
  398. token = ibm_read_slot_reset_state;
  399. rets[2] = 0; /* fake PE Unavailable info */
  400. outputs = 3;
  401. }
  402. return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
  403. BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
  404. }
  405. /**
  406. * eeh_panic - call panic() for an eeh event that cannot be handled.
  407. * The philosophy of this routine is that it is better to panic and
  408. * halt the OS than it is to risk possible data corruption by
  409. * oblivious device drivers that don't know better.
  410. *
  411. * @dev pci device that had an eeh event
  412. * @reset_state current reset state of the device slot
  413. */
  414. static void eeh_panic(struct pci_dev *dev, int reset_state)
  415. {
  416. /*
  417. * XXX We should create a separate sysctl for this.
  418. *
  419. * Since the panic_on_oops sysctl is used to halt the system
  420. * in light of potential corruption, we can use it here.
  421. */
  422. if (panic_on_oops) {
  423. struct device_node *dn = pci_device_to_OF_node(dev);
  424. eeh_slot_error_detail (PCI_DN(dn), 2 /* Permanent Error */);
  425. panic("EEH: MMIO failure (%d) on device:%s\n", reset_state,
  426. pci_name(dev));
  427. }
  428. else {
  429. __get_cpu_var(ignored_failures)++;
  430. printk(KERN_INFO "EEH: Ignored MMIO failure (%d) on device:%s\n",
  431. reset_state, pci_name(dev));
  432. }
  433. }
  434. /**
  435. * eeh_event_handler - dispatch EEH events. The detection of a frozen
  436. * slot can occur inside an interrupt, where it can be hard to do
  437. * anything about it. The goal of this routine is to pull these
  438. * detection events out of the context of the interrupt handler, and
  439. * re-dispatch them for processing at a later time in a normal context.
  440. *
  441. * @dummy - unused
  442. */
  443. static void eeh_event_handler(void *dummy)
  444. {
  445. unsigned long flags;
  446. struct eeh_event *event;
  447. while (1) {
  448. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  449. event = NULL;
  450. if (!list_empty(&eeh_eventlist)) {
  451. event = list_entry(eeh_eventlist.next, struct eeh_event, list);
  452. list_del(&event->list);
  453. }
  454. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  455. if (event == NULL)
  456. break;
  457. printk(KERN_INFO "EEH: MMIO failure (%d), notifiying device "
  458. "%s\n", event->reset_state,
  459. pci_name(event->dev));
  460. notifier_call_chain (&eeh_notifier_chain,
  461. EEH_NOTIFY_FREEZE, event);
  462. pci_dev_put(event->dev);
  463. kfree(event);
  464. }
  465. }
  466. /**
  467. * eeh_token_to_phys - convert EEH address token to phys address
  468. * @token i/o token, should be address in the form 0xA....
  469. */
  470. static inline unsigned long eeh_token_to_phys(unsigned long token)
  471. {
  472. pte_t *ptep;
  473. unsigned long pa;
  474. ptep = find_linux_pte(init_mm.pgd, token);
  475. if (!ptep)
  476. return token;
  477. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  478. return pa | (token & (PAGE_SIZE-1));
  479. }
  480. /**
  481. * Return the "partitionable endpoint" (pe) under which this device lies
  482. */
  483. static struct device_node * find_device_pe(struct device_node *dn)
  484. {
  485. while ((dn->parent) && PCI_DN(dn->parent) &&
  486. (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  487. dn = dn->parent;
  488. }
  489. return dn;
  490. }
  491. /** Mark all devices that are peers of this device as failed.
  492. * Mark the device driver too, so that it can see the failure
  493. * immediately; this is critical, since some drivers poll
  494. * status registers in interrupts ... If a driver is polling,
  495. * and the slot is frozen, then the driver can deadlock in
  496. * an interrupt context, which is bad.
  497. */
  498. static inline void __eeh_mark_slot (struct device_node *dn)
  499. {
  500. while (dn) {
  501. PCI_DN(dn)->eeh_mode |= EEH_MODE_ISOLATED;
  502. if (dn->child)
  503. __eeh_mark_slot (dn->child);
  504. dn = dn->sibling;
  505. }
  506. }
  507. static inline void __eeh_clear_slot (struct device_node *dn)
  508. {
  509. while (dn) {
  510. PCI_DN(dn)->eeh_mode &= ~EEH_MODE_ISOLATED;
  511. if (dn->child)
  512. __eeh_clear_slot (dn->child);
  513. dn = dn->sibling;
  514. }
  515. }
  516. static inline void eeh_clear_slot (struct device_node *dn)
  517. {
  518. unsigned long flags;
  519. spin_lock_irqsave(&confirm_error_lock, flags);
  520. __eeh_clear_slot (dn);
  521. spin_unlock_irqrestore(&confirm_error_lock, flags);
  522. }
  523. /**
  524. * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
  525. * @dn device node
  526. * @dev pci device, if known
  527. *
  528. * Check for an EEH failure for the given device node. Call this
  529. * routine if the result of a read was all 0xff's and you want to
  530. * find out if this is due to an EEH slot freeze. This routine
  531. * will query firmware for the EEH status.
  532. *
  533. * Returns 0 if there has not been an EEH error; otherwise returns
  534. * a non-zero value and queues up a slot isolation event notification.
  535. *
  536. * It is safe to call this routine in an interrupt context.
  537. */
  538. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  539. {
  540. int ret;
  541. int rets[3];
  542. unsigned long flags;
  543. int reset_state;
  544. struct eeh_event *event;
  545. struct pci_dn *pdn;
  546. struct device_node *pe_dn;
  547. int rc = 0;
  548. __get_cpu_var(total_mmio_ffs)++;
  549. if (!eeh_subsystem_enabled)
  550. return 0;
  551. if (!dn) {
  552. __get_cpu_var(no_dn)++;
  553. return 0;
  554. }
  555. pdn = PCI_DN(dn);
  556. /* Access to IO BARs might get this far and still not want checking. */
  557. if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  558. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  559. __get_cpu_var(ignored_check)++;
  560. #ifdef DEBUG
  561. printk ("EEH:ignored check (%x) for %s %s\n",
  562. pdn->eeh_mode, pci_name (dev), dn->full_name);
  563. #endif
  564. return 0;
  565. }
  566. if (!pdn->eeh_config_addr) {
  567. __get_cpu_var(no_cfg_addr)++;
  568. return 0;
  569. }
  570. /* If we already have a pending isolation event for this
  571. * slot, we know it's bad already, we don't need to check.
  572. * Do this checking under a lock; as multiple PCI devices
  573. * in one slot might report errors simultaneously, and we
  574. * only want one error recovery routine running.
  575. */
  576. spin_lock_irqsave(&confirm_error_lock, flags);
  577. rc = 1;
  578. if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
  579. pdn->eeh_check_count ++;
  580. if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
  581. printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
  582. pdn->eeh_check_count);
  583. dump_stack();
  584. /* re-read the slot reset state */
  585. if (read_slot_reset_state(pdn, rets) != 0)
  586. rets[0] = -1; /* reset state unknown */
  587. /* If we are here, then we hit an infinite loop. Stop. */
  588. panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
  589. }
  590. goto dn_unlock;
  591. }
  592. /*
  593. * Now test for an EEH failure. This is VERY expensive.
  594. * Note that the eeh_config_addr may be a parent device
  595. * in the case of a device behind a bridge, or it may be
  596. * function zero of a multi-function device.
  597. * In any case they must share a common PHB.
  598. */
  599. ret = read_slot_reset_state(pdn, rets);
  600. /* If the call to firmware failed, punt */
  601. if (ret != 0) {
  602. printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
  603. ret, dn->full_name);
  604. __get_cpu_var(false_positives)++;
  605. rc = 0;
  606. goto dn_unlock;
  607. }
  608. /* If EEH is not supported on this device, punt. */
  609. if (rets[1] != 1) {
  610. printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
  611. ret, dn->full_name);
  612. __get_cpu_var(false_positives)++;
  613. rc = 0;
  614. goto dn_unlock;
  615. }
  616. /* If not the kind of error we know about, punt. */
  617. if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
  618. __get_cpu_var(false_positives)++;
  619. rc = 0;
  620. goto dn_unlock;
  621. }
  622. /* Note that config-io to empty slots may fail;
  623. * we recognize empty because they don't have children. */
  624. if ((rets[0] == 5) && (dn->child == NULL)) {
  625. __get_cpu_var(false_positives)++;
  626. rc = 0;
  627. goto dn_unlock;
  628. }
  629. __get_cpu_var(slot_resets)++;
  630. /* Avoid repeated reports of this failure, including problems
  631. * with other functions on this device, and functions under
  632. * bridges. */
  633. pe_dn = find_device_pe (dn);
  634. __eeh_mark_slot (pe_dn);
  635. spin_unlock_irqrestore(&confirm_error_lock, flags);
  636. reset_state = rets[0];
  637. eeh_slot_error_detail (pdn, 1 /* Temporary Error */);
  638. printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
  639. rets[0], dn->name, dn->full_name);
  640. event = kmalloc(sizeof(*event), GFP_ATOMIC);
  641. if (event == NULL) {
  642. eeh_panic(dev, reset_state);
  643. return 1;
  644. }
  645. event->dev = dev;
  646. event->dn = dn;
  647. event->reset_state = reset_state;
  648. /* We may or may not be called in an interrupt context */
  649. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  650. list_add(&event->list, &eeh_eventlist);
  651. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  652. /* Most EEH events are due to device driver bugs. Having
  653. * a stack trace will help the device-driver authors figure
  654. * out what happened. So print that out. */
  655. if (rets[0] != 5) dump_stack();
  656. schedule_work(&eeh_event_wq);
  657. return 1;
  658. dn_unlock:
  659. spin_unlock_irqrestore(&confirm_error_lock, flags);
  660. return rc;
  661. }
  662. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  663. /**
  664. * eeh_check_failure - check if all 1's data is due to EEH slot freeze
  665. * @token i/o token, should be address in the form 0xA....
  666. * @val value, should be all 1's (XXX why do we need this arg??)
  667. *
  668. * Check for an EEH failure at the given token address. Call this
  669. * routine if the result of a read was all 0xff's and you want to
  670. * find out if this is due to an EEH slot freeze event. This routine
  671. * will query firmware for the EEH status.
  672. *
  673. * Note this routine is safe to call in an interrupt context.
  674. */
  675. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  676. {
  677. unsigned long addr;
  678. struct pci_dev *dev;
  679. struct device_node *dn;
  680. /* Finding the phys addr + pci device; this is pretty quick. */
  681. addr = eeh_token_to_phys((unsigned long __force) token);
  682. dev = pci_get_device_by_addr(addr);
  683. if (!dev) {
  684. __get_cpu_var(no_device)++;
  685. return val;
  686. }
  687. dn = pci_device_to_OF_node(dev);
  688. eeh_dn_check_failure (dn, dev);
  689. pci_dev_put(dev);
  690. return val;
  691. }
  692. EXPORT_SYMBOL(eeh_check_failure);
  693. struct eeh_early_enable_info {
  694. unsigned int buid_hi;
  695. unsigned int buid_lo;
  696. };
  697. /* Enable eeh for the given device node. */
  698. static void *early_enable_eeh(struct device_node *dn, void *data)
  699. {
  700. struct eeh_early_enable_info *info = data;
  701. int ret;
  702. char *status = get_property(dn, "status", NULL);
  703. u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
  704. u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
  705. u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
  706. u32 *regs;
  707. int enable;
  708. struct pci_dn *pdn = PCI_DN(dn);
  709. pdn->eeh_mode = 0;
  710. pdn->eeh_check_count = 0;
  711. pdn->eeh_freeze_count = 0;
  712. if (status && strcmp(status, "ok") != 0)
  713. return NULL; /* ignore devices with bad status */
  714. /* Ignore bad nodes. */
  715. if (!class_code || !vendor_id || !device_id)
  716. return NULL;
  717. /* There is nothing to check on PCI to ISA bridges */
  718. if (dn->type && !strcmp(dn->type, "isa")) {
  719. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  720. return NULL;
  721. }
  722. /*
  723. * Now decide if we are going to "Disable" EEH checking
  724. * for this device. We still run with the EEH hardware active,
  725. * but we won't be checking for ff's. This means a driver
  726. * could return bad data (very bad!), an interrupt handler could
  727. * hang waiting on status bits that won't change, etc.
  728. * But there are a few cases like display devices that make sense.
  729. */
  730. enable = 1; /* i.e. we will do checking */
  731. if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
  732. enable = 0;
  733. if (!enable)
  734. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  735. /* Ok... see if this device supports EEH. Some do, some don't,
  736. * and the only way to find out is to check each and every one. */
  737. regs = (u32 *)get_property(dn, "reg", NULL);
  738. if (regs) {
  739. /* First register entry is addr (00BBSS00) */
  740. /* Try to enable eeh */
  741. ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
  742. regs[0], info->buid_hi, info->buid_lo,
  743. EEH_ENABLE);
  744. if (ret == 0) {
  745. eeh_subsystem_enabled = 1;
  746. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  747. pdn->eeh_config_addr = regs[0];
  748. #ifdef DEBUG
  749. printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
  750. #endif
  751. } else {
  752. /* This device doesn't support EEH, but it may have an
  753. * EEH parent, in which case we mark it as supported. */
  754. if (dn->parent && PCI_DN(dn->parent)
  755. && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  756. /* Parent supports EEH. */
  757. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  758. pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
  759. return NULL;
  760. }
  761. }
  762. } else {
  763. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  764. dn->full_name);
  765. }
  766. return NULL;
  767. }
  768. /*
  769. * Initialize EEH by trying to enable it for all of the adapters in the system.
  770. * As a side effect we can determine here if eeh is supported at all.
  771. * Note that we leave EEH on so failed config cycles won't cause a machine
  772. * check. If a user turns off EEH for a particular adapter they are really
  773. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  774. * grant access to a slot if EEH isn't enabled, and so we always enable
  775. * EEH for all slots/all devices.
  776. *
  777. * The eeh-force-off option disables EEH checking globally, for all slots.
  778. * Even if force-off is set, the EEH hardware is still enabled, so that
  779. * newer systems can boot.
  780. */
  781. void __init eeh_init(void)
  782. {
  783. struct device_node *phb, *np;
  784. struct eeh_early_enable_info info;
  785. spin_lock_init(&confirm_error_lock);
  786. spin_lock_init(&slot_errbuf_lock);
  787. np = of_find_node_by_path("/rtas");
  788. if (np == NULL)
  789. return;
  790. ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
  791. ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
  792. ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
  793. ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
  794. ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
  795. if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
  796. return;
  797. eeh_error_buf_size = rtas_token("rtas-error-log-max");
  798. if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
  799. eeh_error_buf_size = 1024;
  800. }
  801. if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
  802. printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
  803. "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
  804. eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
  805. }
  806. /* Enable EEH for all adapters. Note that eeh requires buid's */
  807. for (phb = of_find_node_by_name(NULL, "pci"); phb;
  808. phb = of_find_node_by_name(phb, "pci")) {
  809. unsigned long buid;
  810. buid = get_phb_buid(phb);
  811. if (buid == 0 || PCI_DN(phb) == NULL)
  812. continue;
  813. info.buid_lo = BUID_LO(buid);
  814. info.buid_hi = BUID_HI(buid);
  815. traverse_pci_devices(phb, early_enable_eeh, &info);
  816. }
  817. if (eeh_subsystem_enabled)
  818. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  819. else
  820. printk(KERN_WARNING "EEH: No capable adapters found\n");
  821. }
  822. /**
  823. * eeh_add_device_early - enable EEH for the indicated device_node
  824. * @dn: device node for which to set up EEH
  825. *
  826. * This routine must be used to perform EEH initialization for PCI
  827. * devices that were added after system boot (e.g. hotplug, dlpar).
  828. * This routine must be called before any i/o is performed to the
  829. * adapter (inluding any config-space i/o).
  830. * Whether this actually enables EEH or not for this device depends
  831. * on the CEC architecture, type of the device, on earlier boot
  832. * command-line arguments & etc.
  833. */
  834. void eeh_add_device_early(struct device_node *dn)
  835. {
  836. struct pci_controller *phb;
  837. struct eeh_early_enable_info info;
  838. if (!dn || !PCI_DN(dn))
  839. return;
  840. phb = PCI_DN(dn)->phb;
  841. if (NULL == phb || 0 == phb->buid) {
  842. printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
  843. dn->full_name);
  844. dump_stack();
  845. return;
  846. }
  847. info.buid_hi = BUID_HI(phb->buid);
  848. info.buid_lo = BUID_LO(phb->buid);
  849. early_enable_eeh(dn, &info);
  850. }
  851. EXPORT_SYMBOL_GPL(eeh_add_device_early);
  852. /**
  853. * eeh_add_device_late - perform EEH initialization for the indicated pci device
  854. * @dev: pci device for which to set up EEH
  855. *
  856. * This routine must be used to complete EEH initialization for PCI
  857. * devices that were added after system boot (e.g. hotplug, dlpar).
  858. */
  859. void eeh_add_device_late(struct pci_dev *dev)
  860. {
  861. struct device_node *dn;
  862. if (!dev || !eeh_subsystem_enabled)
  863. return;
  864. #ifdef DEBUG
  865. printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
  866. #endif
  867. pci_dev_get (dev);
  868. dn = pci_device_to_OF_node(dev);
  869. PCI_DN(dn)->pcidev = dev;
  870. pci_addr_cache_insert_device (dev);
  871. }
  872. EXPORT_SYMBOL_GPL(eeh_add_device_late);
  873. /**
  874. * eeh_remove_device - undo EEH setup for the indicated pci device
  875. * @dev: pci device to be removed
  876. *
  877. * This routine should be when a device is removed from a running
  878. * system (e.g. by hotplug or dlpar).
  879. */
  880. void eeh_remove_device(struct pci_dev *dev)
  881. {
  882. struct device_node *dn;
  883. if (!dev || !eeh_subsystem_enabled)
  884. return;
  885. /* Unregister the device with the EEH/PCI address search system */
  886. #ifdef DEBUG
  887. printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
  888. #endif
  889. pci_addr_cache_remove_device(dev);
  890. dn = pci_device_to_OF_node(dev);
  891. PCI_DN(dn)->pcidev = NULL;
  892. pci_dev_put (dev);
  893. }
  894. EXPORT_SYMBOL_GPL(eeh_remove_device);
  895. static int proc_eeh_show(struct seq_file *m, void *v)
  896. {
  897. unsigned int cpu;
  898. unsigned long ffs = 0, positives = 0, failures = 0;
  899. unsigned long resets = 0;
  900. unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
  901. for_each_cpu(cpu) {
  902. ffs += per_cpu(total_mmio_ffs, cpu);
  903. positives += per_cpu(false_positives, cpu);
  904. failures += per_cpu(ignored_failures, cpu);
  905. resets += per_cpu(slot_resets, cpu);
  906. no_dev += per_cpu(no_device, cpu);
  907. no_dn += per_cpu(no_dn, cpu);
  908. no_cfg += per_cpu(no_cfg_addr, cpu);
  909. no_check += per_cpu(ignored_check, cpu);
  910. }
  911. if (0 == eeh_subsystem_enabled) {
  912. seq_printf(m, "EEH Subsystem is globally disabled\n");
  913. seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
  914. } else {
  915. seq_printf(m, "EEH Subsystem is enabled\n");
  916. seq_printf(m,
  917. "no device=%ld\n"
  918. "no device node=%ld\n"
  919. "no config address=%ld\n"
  920. "check not wanted=%ld\n"
  921. "eeh_total_mmio_ffs=%ld\n"
  922. "eeh_false_positives=%ld\n"
  923. "eeh_ignored_failures=%ld\n"
  924. "eeh_slot_resets=%ld\n",
  925. no_dev, no_dn, no_cfg, no_check,
  926. ffs, positives, failures, resets);
  927. }
  928. return 0;
  929. }
  930. static int proc_eeh_open(struct inode *inode, struct file *file)
  931. {
  932. return single_open(file, proc_eeh_show, NULL);
  933. }
  934. static struct file_operations proc_eeh_operations = {
  935. .open = proc_eeh_open,
  936. .read = seq_read,
  937. .llseek = seq_lseek,
  938. .release = single_release,
  939. };
  940. static int __init eeh_init_proc(void)
  941. {
  942. struct proc_dir_entry *e;
  943. if (systemcfg->platform & PLATFORM_PSERIES) {
  944. e = create_proc_entry("ppc64/eeh", 0, NULL);
  945. if (e)
  946. e->proc_fops = &proc_eeh_operations;
  947. }
  948. return 0;
  949. }
  950. __initcall(eeh_init_proc);