eeh.c 32 KB

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