eeh.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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/bootmem.h>
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/mm.h>
  23. #include <linux/notifier.h>
  24. #include <linux/pci.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/rbtree.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/spinlock.h>
  29. #include <asm/eeh.h>
  30. #include <asm/io.h>
  31. #include <asm/machdep.h>
  32. #include <asm/rtas.h>
  33. #include <asm/atomic.h>
  34. #include <asm/systemcfg.h>
  35. #include "pci.h"
  36. #undef DEBUG
  37. /** Overview:
  38. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  39. * dealing with PCI bus errors that can't be dealt with within the
  40. * usual PCI framework, except by check-stopping the CPU. Systems
  41. * that are designed for high-availability/reliability cannot afford
  42. * to crash due to a "mere" PCI error, thus the need for EEH.
  43. * An EEH-capable bridge operates by converting a detected error
  44. * into a "slot freeze", taking the PCI adapter off-line, making
  45. * the slot behave, from the OS'es point of view, as if the slot
  46. * were "empty": all reads return 0xff's and all writes are silently
  47. * ignored. EEH slot isolation events can be triggered by parity
  48. * errors on the address or data busses (e.g. during posted writes),
  49. * which in turn might be caused by dust, vibration, humidity,
  50. * radioactivity or plain-old failed hardware.
  51. *
  52. * Note, however, that one of the leading causes of EEH slot
  53. * freeze events are buggy device drivers, buggy device microcode,
  54. * or buggy device hardware. This is because any attempt by the
  55. * device to bus-master data to a memory address that is not
  56. * assigned to the device will trigger a slot freeze. (The idea
  57. * is to prevent devices-gone-wild from corrupting system memory).
  58. * Buggy hardware/drivers will have a miserable time co-existing
  59. * with EEH.
  60. *
  61. * Ideally, a PCI device driver, when suspecting that an isolation
  62. * event has occured (e.g. by reading 0xff's), will then ask EEH
  63. * whether this is the case, and then take appropriate steps to
  64. * reset the PCI slot, the PCI device, and then resume operations.
  65. * However, until that day, the checking is done here, with the
  66. * eeh_check_failure() routine embedded in the MMIO macros. If
  67. * the slot is found to be isolated, an "EEH Event" is synthesized
  68. * and sent out for processing.
  69. */
  70. /** Bus Unit ID macros; get low and hi 32-bits of the 64-bit BUID */
  71. #define BUID_HI(buid) ((buid) >> 32)
  72. #define BUID_LO(buid) ((buid) & 0xffffffff)
  73. /* EEH event workqueue setup. */
  74. static DEFINE_SPINLOCK(eeh_eventlist_lock);
  75. LIST_HEAD(eeh_eventlist);
  76. static void eeh_event_handler(void *);
  77. DECLARE_WORK(eeh_event_wq, eeh_event_handler, NULL);
  78. static struct notifier_block *eeh_notifier_chain;
  79. /*
  80. * If a device driver keeps reading an MMIO register in an interrupt
  81. * handler after a slot isolation event has occurred, we assume it
  82. * is broken and panic. This sets the threshold for how many read
  83. * attempts we allow before panicking.
  84. */
  85. #define EEH_MAX_FAILS 1000
  86. static atomic_t eeh_fail_count;
  87. /* RTAS tokens */
  88. static int ibm_set_eeh_option;
  89. static int ibm_set_slot_reset;
  90. static int ibm_read_slot_reset_state;
  91. static int ibm_read_slot_reset_state2;
  92. static int ibm_slot_error_detail;
  93. static int eeh_subsystem_enabled;
  94. /* Buffer for reporting slot-error-detail rtas calls */
  95. static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
  96. static DEFINE_SPINLOCK(slot_errbuf_lock);
  97. static int eeh_error_buf_size;
  98. /* System monitoring statistics */
  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 %s\n",
  185. (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
  186. piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev),
  187. pci_pretty_name(piar->pcidev));
  188. cnt++;
  189. n = rb_next(n);
  190. }
  191. }
  192. #endif
  193. /* Insert address range into the rb tree. */
  194. static struct pci_io_addr_range *
  195. pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
  196. unsigned long ahi, unsigned int flags)
  197. {
  198. struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
  199. struct rb_node *parent = NULL;
  200. struct pci_io_addr_range *piar;
  201. /* Walk tree, find a place to insert into tree */
  202. while (*p) {
  203. parent = *p;
  204. piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
  205. if (alo < piar->addr_lo) {
  206. p = &parent->rb_left;
  207. } else if (ahi > piar->addr_hi) {
  208. p = &parent->rb_right;
  209. } else {
  210. if (dev != piar->pcidev ||
  211. alo != piar->addr_lo || ahi != piar->addr_hi) {
  212. printk(KERN_WARNING "PIAR: overlapping address range\n");
  213. }
  214. return piar;
  215. }
  216. }
  217. piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
  218. if (!piar)
  219. return NULL;
  220. piar->addr_lo = alo;
  221. piar->addr_hi = ahi;
  222. piar->pcidev = dev;
  223. piar->flags = flags;
  224. rb_link_node(&piar->rb_node, parent, p);
  225. rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
  226. return piar;
  227. }
  228. static void __pci_addr_cache_insert_device(struct pci_dev *dev)
  229. {
  230. struct device_node *dn;
  231. int i;
  232. int inserted = 0;
  233. dn = pci_device_to_OF_node(dev);
  234. if (!dn) {
  235. printk(KERN_WARNING "PCI: no pci dn found for dev=%s %s\n",
  236. pci_name(dev), pci_pretty_name(dev));
  237. return;
  238. }
  239. /* Skip any devices for which EEH is not enabled. */
  240. if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
  241. dn->eeh_mode & EEH_MODE_NOCHECK) {
  242. #ifdef DEBUG
  243. printk(KERN_INFO "PCI: skip building address cache for=%s %s\n",
  244. pci_name(dev), pci_pretty_name(dev));
  245. #endif
  246. return;
  247. }
  248. /* The cache holds a reference to the device... */
  249. pci_dev_get(dev);
  250. /* Walk resources on this device, poke them into the tree */
  251. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  252. unsigned long start = pci_resource_start(dev,i);
  253. unsigned long end = pci_resource_end(dev,i);
  254. unsigned int flags = pci_resource_flags(dev,i);
  255. /* We are interested only bus addresses, not dma or other stuff */
  256. if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  257. continue;
  258. if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
  259. continue;
  260. pci_addr_cache_insert(dev, start, end, flags);
  261. inserted = 1;
  262. }
  263. /* If there was nothing to add, the cache has no reference... */
  264. if (!inserted)
  265. pci_dev_put(dev);
  266. }
  267. /**
  268. * pci_addr_cache_insert_device - Add a device to the address cache
  269. * @dev: PCI device whose I/O addresses we are interested in.
  270. *
  271. * In order to support the fast lookup of devices based on addresses,
  272. * we maintain a cache of devices that can be quickly searched.
  273. * This routine adds a device to that cache.
  274. */
  275. void pci_addr_cache_insert_device(struct pci_dev *dev)
  276. {
  277. unsigned long flags;
  278. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  279. __pci_addr_cache_insert_device(dev);
  280. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  281. }
  282. static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
  283. {
  284. struct rb_node *n;
  285. int removed = 0;
  286. restart:
  287. n = rb_first(&pci_io_addr_cache_root.rb_root);
  288. while (n) {
  289. struct pci_io_addr_range *piar;
  290. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  291. if (piar->pcidev == dev) {
  292. rb_erase(n, &pci_io_addr_cache_root.rb_root);
  293. removed = 1;
  294. kfree(piar);
  295. goto restart;
  296. }
  297. n = rb_next(n);
  298. }
  299. /* The cache no longer holds its reference to this device... */
  300. if (removed)
  301. pci_dev_put(dev);
  302. }
  303. /**
  304. * pci_addr_cache_remove_device - remove pci device from addr cache
  305. * @dev: device to remove
  306. *
  307. * Remove a device from the addr-cache tree.
  308. * This is potentially expensive, since it will walk
  309. * the tree multiple times (once per resource).
  310. * But so what; device removal doesn't need to be that fast.
  311. */
  312. void pci_addr_cache_remove_device(struct pci_dev *dev)
  313. {
  314. unsigned long flags;
  315. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  316. __pci_addr_cache_remove_device(dev);
  317. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  318. }
  319. /**
  320. * pci_addr_cache_build - Build a cache of I/O addresses
  321. *
  322. * Build a cache of pci i/o addresses. This cache will be used to
  323. * find the pci device that corresponds to a given address.
  324. * This routine scans all pci busses to build the cache.
  325. * Must be run late in boot process, after the pci controllers
  326. * have been scaned for devices (after all device resources are known).
  327. */
  328. void __init pci_addr_cache_build(void)
  329. {
  330. struct pci_dev *dev = NULL;
  331. spin_lock_init(&pci_io_addr_cache_root.piar_lock);
  332. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  333. /* Ignore PCI bridges ( XXX why ??) */
  334. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  335. continue;
  336. }
  337. pci_addr_cache_insert_device(dev);
  338. }
  339. #ifdef DEBUG
  340. /* Verify tree built up above, echo back the list of addrs. */
  341. pci_addr_cache_print(&pci_io_addr_cache_root);
  342. #endif
  343. }
  344. /* --------------------------------------------------------------- */
  345. /* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
  346. /**
  347. * eeh_register_notifier - Register to find out about EEH events.
  348. * @nb: notifier block to callback on events
  349. */
  350. int eeh_register_notifier(struct notifier_block *nb)
  351. {
  352. return notifier_chain_register(&eeh_notifier_chain, nb);
  353. }
  354. /**
  355. * eeh_unregister_notifier - Unregister to an EEH event notifier.
  356. * @nb: notifier block to callback on events
  357. */
  358. int eeh_unregister_notifier(struct notifier_block *nb)
  359. {
  360. return notifier_chain_unregister(&eeh_notifier_chain, nb);
  361. }
  362. /**
  363. * read_slot_reset_state - Read the reset state of a device node's slot
  364. * @dn: device node to read
  365. * @rets: array to return results in
  366. */
  367. static int read_slot_reset_state(struct device_node *dn, int rets[])
  368. {
  369. int token, outputs;
  370. if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
  371. token = ibm_read_slot_reset_state2;
  372. outputs = 4;
  373. } else {
  374. token = ibm_read_slot_reset_state;
  375. outputs = 3;
  376. }
  377. return rtas_call(token, 3, outputs, rets, dn->eeh_config_addr,
  378. BUID_HI(dn->phb->buid), BUID_LO(dn->phb->buid));
  379. }
  380. /**
  381. * eeh_panic - call panic() for an eeh event that cannot be handled.
  382. * The philosophy of this routine is that it is better to panic and
  383. * halt the OS than it is to risk possible data corruption by
  384. * oblivious device drivers that don't know better.
  385. *
  386. * @dev pci device that had an eeh event
  387. * @reset_state current reset state of the device slot
  388. */
  389. static void eeh_panic(struct pci_dev *dev, int reset_state)
  390. {
  391. /*
  392. * XXX We should create a separate sysctl for this.
  393. *
  394. * Since the panic_on_oops sysctl is used to halt the system
  395. * in light of potential corruption, we can use it here.
  396. */
  397. if (panic_on_oops)
  398. panic("EEH: MMIO failure (%d) on device:%s %s\n", reset_state,
  399. pci_name(dev), pci_pretty_name(dev));
  400. else {
  401. __get_cpu_var(ignored_failures)++;
  402. printk(KERN_INFO "EEH: Ignored MMIO failure (%d) on device:%s %s\n",
  403. reset_state, pci_name(dev), pci_pretty_name(dev));
  404. }
  405. }
  406. /**
  407. * eeh_event_handler - dispatch EEH events. The detection of a frozen
  408. * slot can occur inside an interrupt, where it can be hard to do
  409. * anything about it. The goal of this routine is to pull these
  410. * detection events out of the context of the interrupt handler, and
  411. * re-dispatch them for processing at a later time in a normal context.
  412. *
  413. * @dummy - unused
  414. */
  415. static void eeh_event_handler(void *dummy)
  416. {
  417. unsigned long flags;
  418. struct eeh_event *event;
  419. while (1) {
  420. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  421. event = NULL;
  422. if (!list_empty(&eeh_eventlist)) {
  423. event = list_entry(eeh_eventlist.next, struct eeh_event, list);
  424. list_del(&event->list);
  425. }
  426. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  427. if (event == NULL)
  428. break;
  429. printk(KERN_INFO "EEH: MMIO failure (%d), notifiying device "
  430. "%s %s\n", event->reset_state,
  431. pci_name(event->dev), pci_pretty_name(event->dev));
  432. atomic_set(&eeh_fail_count, 0);
  433. notifier_call_chain (&eeh_notifier_chain,
  434. EEH_NOTIFY_FREEZE, event);
  435. __get_cpu_var(slot_resets)++;
  436. pci_dev_put(event->dev);
  437. kfree(event);
  438. }
  439. }
  440. /**
  441. * eeh_token_to_phys - convert EEH address token to phys address
  442. * @token i/o token, should be address in the form 0xE....
  443. */
  444. static inline unsigned long eeh_token_to_phys(unsigned long token)
  445. {
  446. pte_t *ptep;
  447. unsigned long pa;
  448. ptep = find_linux_pte(init_mm.pgd, token);
  449. if (!ptep)
  450. return token;
  451. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  452. return pa | (token & (PAGE_SIZE-1));
  453. }
  454. /**
  455. * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
  456. * @dn device node
  457. * @dev pci device, if known
  458. *
  459. * Check for an EEH failure for the given device node. Call this
  460. * routine if the result of a read was all 0xff's and you want to
  461. * find out if this is due to an EEH slot freeze. This routine
  462. * will query firmware for the EEH status.
  463. *
  464. * Returns 0 if there has not been an EEH error; otherwise returns
  465. * a non-zero value and queues up a solt isolation event notification.
  466. *
  467. * It is safe to call this routine in an interrupt context.
  468. */
  469. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  470. {
  471. int ret;
  472. int rets[3];
  473. unsigned long flags;
  474. int rc, reset_state;
  475. struct eeh_event *event;
  476. __get_cpu_var(total_mmio_ffs)++;
  477. if (!eeh_subsystem_enabled)
  478. return 0;
  479. if (!dn)
  480. return 0;
  481. /* Access to IO BARs might get this far and still not want checking. */
  482. if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
  483. dn->eeh_mode & EEH_MODE_NOCHECK) {
  484. return 0;
  485. }
  486. if (!dn->eeh_config_addr) {
  487. return 0;
  488. }
  489. /*
  490. * If we already have a pending isolation event for this
  491. * slot, we know it's bad already, we don't need to check...
  492. */
  493. if (dn->eeh_mode & EEH_MODE_ISOLATED) {
  494. atomic_inc(&eeh_fail_count);
  495. if (atomic_read(&eeh_fail_count) >= EEH_MAX_FAILS) {
  496. /* re-read the slot reset state */
  497. if (read_slot_reset_state(dn, rets) != 0)
  498. rets[0] = -1; /* reset state unknown */
  499. eeh_panic(dev, rets[0]);
  500. }
  501. return 0;
  502. }
  503. /*
  504. * Now test for an EEH failure. This is VERY expensive.
  505. * Note that the eeh_config_addr may be a parent device
  506. * in the case of a device behind a bridge, or it may be
  507. * function zero of a multi-function device.
  508. * In any case they must share a common PHB.
  509. */
  510. ret = read_slot_reset_state(dn, rets);
  511. if (!(ret == 0 && rets[1] == 1 && (rets[0] == 2 || rets[0] == 4))) {
  512. __get_cpu_var(false_positives)++;
  513. return 0;
  514. }
  515. /* prevent repeated reports of this failure */
  516. dn->eeh_mode |= EEH_MODE_ISOLATED;
  517. reset_state = rets[0];
  518. spin_lock_irqsave(&slot_errbuf_lock, flags);
  519. memset(slot_errbuf, 0, eeh_error_buf_size);
  520. rc = rtas_call(ibm_slot_error_detail,
  521. 8, 1, NULL, dn->eeh_config_addr,
  522. BUID_HI(dn->phb->buid),
  523. BUID_LO(dn->phb->buid), NULL, 0,
  524. virt_to_phys(slot_errbuf),
  525. eeh_error_buf_size,
  526. 1 /* Temporary Error */);
  527. if (rc == 0)
  528. log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
  529. spin_unlock_irqrestore(&slot_errbuf_lock, flags);
  530. printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
  531. rets[0], dn->name, dn->full_name);
  532. event = kmalloc(sizeof(*event), GFP_ATOMIC);
  533. if (event == NULL) {
  534. eeh_panic(dev, reset_state);
  535. return 1;
  536. }
  537. event->dev = dev;
  538. event->dn = dn;
  539. event->reset_state = reset_state;
  540. /* We may or may not be called in an interrupt context */
  541. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  542. list_add(&event->list, &eeh_eventlist);
  543. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  544. /* Most EEH events are due to device driver bugs. Having
  545. * a stack trace will help the device-driver authors figure
  546. * out what happened. So print that out. */
  547. dump_stack();
  548. schedule_work(&eeh_event_wq);
  549. return 0;
  550. }
  551. EXPORT_SYMBOL(eeh_dn_check_failure);
  552. /**
  553. * eeh_check_failure - check if all 1's data is due to EEH slot freeze
  554. * @token i/o token, should be address in the form 0xA....
  555. * @val value, should be all 1's (XXX why do we need this arg??)
  556. *
  557. * Check for an eeh failure at the given token address.
  558. * Check for an EEH failure at the given token address. Call this
  559. * routine if the result of a read was all 0xff's and you want to
  560. * find out if this is due to an EEH slot freeze event. This routine
  561. * will query firmware for the EEH status.
  562. *
  563. * Note this routine is safe to call in an interrupt context.
  564. */
  565. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  566. {
  567. unsigned long addr;
  568. struct pci_dev *dev;
  569. struct device_node *dn;
  570. /* Finding the phys addr + pci device; this is pretty quick. */
  571. addr = eeh_token_to_phys((unsigned long __force) token);
  572. dev = pci_get_device_by_addr(addr);
  573. if (!dev)
  574. return val;
  575. dn = pci_device_to_OF_node(dev);
  576. eeh_dn_check_failure (dn, dev);
  577. pci_dev_put(dev);
  578. return val;
  579. }
  580. EXPORT_SYMBOL(eeh_check_failure);
  581. struct eeh_early_enable_info {
  582. unsigned int buid_hi;
  583. unsigned int buid_lo;
  584. };
  585. /* Enable eeh for the given device node. */
  586. static void *early_enable_eeh(struct device_node *dn, void *data)
  587. {
  588. struct eeh_early_enable_info *info = data;
  589. int ret;
  590. char *status = get_property(dn, "status", NULL);
  591. u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
  592. u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
  593. u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
  594. u32 *regs;
  595. int enable;
  596. dn->eeh_mode = 0;
  597. if (status && strcmp(status, "ok") != 0)
  598. return NULL; /* ignore devices with bad status */
  599. /* Ignore bad nodes. */
  600. if (!class_code || !vendor_id || !device_id)
  601. return NULL;
  602. /* There is nothing to check on PCI to ISA bridges */
  603. if (dn->type && !strcmp(dn->type, "isa")) {
  604. dn->eeh_mode |= EEH_MODE_NOCHECK;
  605. return NULL;
  606. }
  607. /*
  608. * Now decide if we are going to "Disable" EEH checking
  609. * for this device. We still run with the EEH hardware active,
  610. * but we won't be checking for ff's. This means a driver
  611. * could return bad data (very bad!), an interrupt handler could
  612. * hang waiting on status bits that won't change, etc.
  613. * But there are a few cases like display devices that make sense.
  614. */
  615. enable = 1; /* i.e. we will do checking */
  616. if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
  617. enable = 0;
  618. if (!enable)
  619. dn->eeh_mode |= EEH_MODE_NOCHECK;
  620. /* Ok... see if this device supports EEH. Some do, some don't,
  621. * and the only way to find out is to check each and every one. */
  622. regs = (u32 *)get_property(dn, "reg", NULL);
  623. if (regs) {
  624. /* First register entry is addr (00BBSS00) */
  625. /* Try to enable eeh */
  626. ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
  627. regs[0], info->buid_hi, info->buid_lo,
  628. EEH_ENABLE);
  629. if (ret == 0) {
  630. eeh_subsystem_enabled = 1;
  631. dn->eeh_mode |= EEH_MODE_SUPPORTED;
  632. dn->eeh_config_addr = regs[0];
  633. #ifdef DEBUG
  634. printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
  635. #endif
  636. } else {
  637. /* This device doesn't support EEH, but it may have an
  638. * EEH parent, in which case we mark it as supported. */
  639. if (dn->parent && (dn->parent->eeh_mode & EEH_MODE_SUPPORTED)) {
  640. /* Parent supports EEH. */
  641. dn->eeh_mode |= EEH_MODE_SUPPORTED;
  642. dn->eeh_config_addr = dn->parent->eeh_config_addr;
  643. return NULL;
  644. }
  645. }
  646. } else {
  647. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  648. dn->full_name);
  649. }
  650. return NULL;
  651. }
  652. /*
  653. * Initialize EEH by trying to enable it for all of the adapters in the system.
  654. * As a side effect we can determine here if eeh is supported at all.
  655. * Note that we leave EEH on so failed config cycles won't cause a machine
  656. * check. If a user turns off EEH for a particular adapter they are really
  657. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  658. * grant access to a slot if EEH isn't enabled, and so we always enable
  659. * EEH for all slots/all devices.
  660. *
  661. * The eeh-force-off option disables EEH checking globally, for all slots.
  662. * Even if force-off is set, the EEH hardware is still enabled, so that
  663. * newer systems can boot.
  664. */
  665. void __init eeh_init(void)
  666. {
  667. struct device_node *phb, *np;
  668. struct eeh_early_enable_info info;
  669. np = of_find_node_by_path("/rtas");
  670. if (np == NULL)
  671. return;
  672. ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
  673. ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
  674. ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
  675. ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
  676. ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
  677. if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
  678. return;
  679. eeh_error_buf_size = rtas_token("rtas-error-log-max");
  680. if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
  681. eeh_error_buf_size = 1024;
  682. }
  683. if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
  684. printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
  685. "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
  686. eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
  687. }
  688. /* Enable EEH for all adapters. Note that eeh requires buid's */
  689. for (phb = of_find_node_by_name(NULL, "pci"); phb;
  690. phb = of_find_node_by_name(phb, "pci")) {
  691. unsigned long buid;
  692. buid = get_phb_buid(phb);
  693. if (buid == 0)
  694. continue;
  695. info.buid_lo = BUID_LO(buid);
  696. info.buid_hi = BUID_HI(buid);
  697. traverse_pci_devices(phb, early_enable_eeh, &info);
  698. }
  699. if (eeh_subsystem_enabled)
  700. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  701. else
  702. printk(KERN_WARNING "EEH: No capable adapters found\n");
  703. }
  704. /**
  705. * eeh_add_device_early - enable EEH for the indicated device_node
  706. * @dn: device node for which to set up EEH
  707. *
  708. * This routine must be used to perform EEH initialization for PCI
  709. * devices that were added after system boot (e.g. hotplug, dlpar).
  710. * This routine must be called before any i/o is performed to the
  711. * adapter (inluding any config-space i/o).
  712. * Whether this actually enables EEH or not for this device depends
  713. * on the CEC architecture, type of the device, on earlier boot
  714. * command-line arguments & etc.
  715. */
  716. void eeh_add_device_early(struct device_node *dn)
  717. {
  718. struct pci_controller *phb;
  719. struct eeh_early_enable_info info;
  720. if (!dn)
  721. return;
  722. phb = dn->phb;
  723. if (NULL == phb || 0 == phb->buid) {
  724. printk(KERN_WARNING "EEH: Expected buid but found none\n");
  725. return;
  726. }
  727. info.buid_hi = BUID_HI(phb->buid);
  728. info.buid_lo = BUID_LO(phb->buid);
  729. early_enable_eeh(dn, &info);
  730. }
  731. EXPORT_SYMBOL(eeh_add_device_early);
  732. /**
  733. * eeh_add_device_late - perform EEH initialization for the indicated pci device
  734. * @dev: pci device for which to set up EEH
  735. *
  736. * This routine must be used to complete EEH initialization for PCI
  737. * devices that were added after system boot (e.g. hotplug, dlpar).
  738. */
  739. void eeh_add_device_late(struct pci_dev *dev)
  740. {
  741. if (!dev || !eeh_subsystem_enabled)
  742. return;
  743. #ifdef DEBUG
  744. printk(KERN_DEBUG "EEH: adding device %s %s\n", pci_name(dev),
  745. pci_pretty_name(dev));
  746. #endif
  747. pci_addr_cache_insert_device (dev);
  748. }
  749. EXPORT_SYMBOL(eeh_add_device_late);
  750. /**
  751. * eeh_remove_device - undo EEH setup for the indicated pci device
  752. * @dev: pci device to be removed
  753. *
  754. * This routine should be when a device is removed from a running
  755. * system (e.g. by hotplug or dlpar).
  756. */
  757. void eeh_remove_device(struct pci_dev *dev)
  758. {
  759. if (!dev || !eeh_subsystem_enabled)
  760. return;
  761. /* Unregister the device with the EEH/PCI address search system */
  762. #ifdef DEBUG
  763. printk(KERN_DEBUG "EEH: remove device %s %s\n", pci_name(dev),
  764. pci_pretty_name(dev));
  765. #endif
  766. pci_addr_cache_remove_device(dev);
  767. }
  768. EXPORT_SYMBOL(eeh_remove_device);
  769. static int proc_eeh_show(struct seq_file *m, void *v)
  770. {
  771. unsigned int cpu;
  772. unsigned long ffs = 0, positives = 0, failures = 0;
  773. unsigned long resets = 0;
  774. for_each_cpu(cpu) {
  775. ffs += per_cpu(total_mmio_ffs, cpu);
  776. positives += per_cpu(false_positives, cpu);
  777. failures += per_cpu(ignored_failures, cpu);
  778. resets += per_cpu(slot_resets, cpu);
  779. }
  780. if (0 == eeh_subsystem_enabled) {
  781. seq_printf(m, "EEH Subsystem is globally disabled\n");
  782. seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
  783. } else {
  784. seq_printf(m, "EEH Subsystem is enabled\n");
  785. seq_printf(m, "eeh_total_mmio_ffs=%ld\n"
  786. "eeh_false_positives=%ld\n"
  787. "eeh_ignored_failures=%ld\n"
  788. "eeh_slot_resets=%ld\n"
  789. "eeh_fail_count=%d\n",
  790. ffs, positives, failures, resets,
  791. eeh_fail_count.counter);
  792. }
  793. return 0;
  794. }
  795. static int proc_eeh_open(struct inode *inode, struct file *file)
  796. {
  797. return single_open(file, proc_eeh_show, NULL);
  798. }
  799. static struct file_operations proc_eeh_operations = {
  800. .open = proc_eeh_open,
  801. .read = seq_read,
  802. .llseek = seq_lseek,
  803. .release = single_release,
  804. };
  805. static int __init eeh_init_proc(void)
  806. {
  807. struct proc_dir_entry *e;
  808. if (systemcfg->platform & PLATFORM_PSERIES) {
  809. e = create_proc_entry("ppc64/eeh", 0, NULL);
  810. if (e)
  811. e->proc_fops = &proc_eeh_operations;
  812. }
  813. return 0;
  814. }
  815. __initcall(eeh_init_proc);