eeh.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. /* 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, total_mmio_ffs);
  96. static DEFINE_PER_CPU(unsigned long, false_positives);
  97. static DEFINE_PER_CPU(unsigned long, ignored_failures);
  98. static DEFINE_PER_CPU(unsigned long, slot_resets);
  99. /**
  100. * The pci address cache subsystem. This subsystem places
  101. * PCI device address resources into a red-black tree, sorted
  102. * according to the address range, so that given only an i/o
  103. * address, the corresponding PCI device can be **quickly**
  104. * found. It is safe to perform an address lookup in an interrupt
  105. * context; this ability is an important feature.
  106. *
  107. * Currently, the only customer of this code is the EEH subsystem;
  108. * thus, this code has been somewhat tailored to suit EEH better.
  109. * In particular, the cache does *not* hold the addresses of devices
  110. * for which EEH is not enabled.
  111. *
  112. * (Implementation Note: The RB tree seems to be better/faster
  113. * than any hash algo I could think of for this problem, even
  114. * with the penalty of slow pointer chases for d-cache misses).
  115. */
  116. struct pci_io_addr_range
  117. {
  118. struct rb_node rb_node;
  119. unsigned long addr_lo;
  120. unsigned long addr_hi;
  121. struct pci_dev *pcidev;
  122. unsigned int flags;
  123. };
  124. static struct pci_io_addr_cache
  125. {
  126. struct rb_root rb_root;
  127. spinlock_t piar_lock;
  128. } pci_io_addr_cache_root;
  129. static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
  130. {
  131. struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
  132. while (n) {
  133. struct pci_io_addr_range *piar;
  134. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  135. if (addr < piar->addr_lo) {
  136. n = n->rb_left;
  137. } else {
  138. if (addr > piar->addr_hi) {
  139. n = n->rb_right;
  140. } else {
  141. pci_dev_get(piar->pcidev);
  142. return piar->pcidev;
  143. }
  144. }
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * pci_get_device_by_addr - Get device, given only address
  150. * @addr: mmio (PIO) phys address or i/o port number
  151. *
  152. * Given an mmio phys address, or a port number, find a pci device
  153. * that implements this address. Be sure to pci_dev_put the device
  154. * when finished. I/O port numbers are assumed to be offset
  155. * from zero (that is, they do *not* have pci_io_addr added in).
  156. * It is safe to call this function within an interrupt.
  157. */
  158. static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
  159. {
  160. struct pci_dev *dev;
  161. unsigned long flags;
  162. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  163. dev = __pci_get_device_by_addr(addr);
  164. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  165. return dev;
  166. }
  167. #ifdef DEBUG
  168. /*
  169. * Handy-dandy debug print routine, does nothing more
  170. * than print out the contents of our addr cache.
  171. */
  172. static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
  173. {
  174. struct rb_node *n;
  175. int cnt = 0;
  176. n = rb_first(&cache->rb_root);
  177. while (n) {
  178. struct pci_io_addr_range *piar;
  179. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  180. printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
  181. (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
  182. piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
  183. cnt++;
  184. n = rb_next(n);
  185. }
  186. }
  187. #endif
  188. /* Insert address range into the rb tree. */
  189. static struct pci_io_addr_range *
  190. pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
  191. unsigned long ahi, unsigned int flags)
  192. {
  193. struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
  194. struct rb_node *parent = NULL;
  195. struct pci_io_addr_range *piar;
  196. /* Walk tree, find a place to insert into tree */
  197. while (*p) {
  198. parent = *p;
  199. piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
  200. if (alo < piar->addr_lo) {
  201. p = &parent->rb_left;
  202. } else if (ahi > piar->addr_hi) {
  203. p = &parent->rb_right;
  204. } else {
  205. if (dev != piar->pcidev ||
  206. alo != piar->addr_lo || ahi != piar->addr_hi) {
  207. printk(KERN_WARNING "PIAR: overlapping address range\n");
  208. }
  209. return piar;
  210. }
  211. }
  212. piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
  213. if (!piar)
  214. return NULL;
  215. piar->addr_lo = alo;
  216. piar->addr_hi = ahi;
  217. piar->pcidev = dev;
  218. piar->flags = flags;
  219. rb_link_node(&piar->rb_node, parent, p);
  220. rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
  221. return piar;
  222. }
  223. static void __pci_addr_cache_insert_device(struct pci_dev *dev)
  224. {
  225. struct device_node *dn;
  226. struct pci_dn *pdn;
  227. int i;
  228. int inserted = 0;
  229. dn = pci_device_to_OF_node(dev);
  230. if (!dn) {
  231. printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
  232. return;
  233. }
  234. /* Skip any devices for which EEH is not enabled. */
  235. pdn = PCI_DN(dn);
  236. if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  237. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  238. #ifdef DEBUG
  239. printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
  240. pci_name(dev), pdn->node->full_name);
  241. #endif
  242. return;
  243. }
  244. /* The cache holds a reference to the device... */
  245. pci_dev_get(dev);
  246. /* Walk resources on this device, poke them into the tree */
  247. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  248. unsigned long start = pci_resource_start(dev,i);
  249. unsigned long end = pci_resource_end(dev,i);
  250. unsigned int flags = pci_resource_flags(dev,i);
  251. /* We are interested only bus addresses, not dma or other stuff */
  252. if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  253. continue;
  254. if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
  255. continue;
  256. pci_addr_cache_insert(dev, start, end, flags);
  257. inserted = 1;
  258. }
  259. /* If there was nothing to add, the cache has no reference... */
  260. if (!inserted)
  261. pci_dev_put(dev);
  262. }
  263. /**
  264. * pci_addr_cache_insert_device - Add a device to the address cache
  265. * @dev: PCI device whose I/O addresses we are interested in.
  266. *
  267. * In order to support the fast lookup of devices based on addresses,
  268. * we maintain a cache of devices that can be quickly searched.
  269. * This routine adds a device to that cache.
  270. */
  271. void pci_addr_cache_insert_device(struct pci_dev *dev)
  272. {
  273. unsigned long flags;
  274. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  275. __pci_addr_cache_insert_device(dev);
  276. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  277. }
  278. static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
  279. {
  280. struct rb_node *n;
  281. int removed = 0;
  282. restart:
  283. n = rb_first(&pci_io_addr_cache_root.rb_root);
  284. while (n) {
  285. struct pci_io_addr_range *piar;
  286. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  287. if (piar->pcidev == dev) {
  288. rb_erase(n, &pci_io_addr_cache_root.rb_root);
  289. removed = 1;
  290. kfree(piar);
  291. goto restart;
  292. }
  293. n = rb_next(n);
  294. }
  295. /* The cache no longer holds its reference to this device... */
  296. if (removed)
  297. pci_dev_put(dev);
  298. }
  299. /**
  300. * pci_addr_cache_remove_device - remove pci device from addr cache
  301. * @dev: device to remove
  302. *
  303. * Remove a device from the addr-cache tree.
  304. * This is potentially expensive, since it will walk
  305. * the tree multiple times (once per resource).
  306. * But so what; device removal doesn't need to be that fast.
  307. */
  308. void pci_addr_cache_remove_device(struct pci_dev *dev)
  309. {
  310. unsigned long flags;
  311. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  312. __pci_addr_cache_remove_device(dev);
  313. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  314. }
  315. /**
  316. * pci_addr_cache_build - Build a cache of I/O addresses
  317. *
  318. * Build a cache of pci i/o addresses. This cache will be used to
  319. * find the pci device that corresponds to a given address.
  320. * This routine scans all pci busses to build the cache.
  321. * Must be run late in boot process, after the pci controllers
  322. * have been scaned for devices (after all device resources are known).
  323. */
  324. void __init pci_addr_cache_build(void)
  325. {
  326. struct pci_dev *dev = NULL;
  327. spin_lock_init(&pci_io_addr_cache_root.piar_lock);
  328. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  329. /* Ignore PCI bridges ( XXX why ??) */
  330. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  331. continue;
  332. }
  333. pci_addr_cache_insert_device(dev);
  334. }
  335. #ifdef DEBUG
  336. /* Verify tree built up above, echo back the list of addrs. */
  337. pci_addr_cache_print(&pci_io_addr_cache_root);
  338. #endif
  339. }
  340. /* --------------------------------------------------------------- */
  341. /* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
  342. /**
  343. * eeh_register_notifier - Register to find out about EEH events.
  344. * @nb: notifier block to callback on events
  345. */
  346. int eeh_register_notifier(struct notifier_block *nb)
  347. {
  348. return notifier_chain_register(&eeh_notifier_chain, nb);
  349. }
  350. /**
  351. * eeh_unregister_notifier - Unregister to an EEH event notifier.
  352. * @nb: notifier block to callback on events
  353. */
  354. int eeh_unregister_notifier(struct notifier_block *nb)
  355. {
  356. return notifier_chain_unregister(&eeh_notifier_chain, nb);
  357. }
  358. /**
  359. * read_slot_reset_state - Read the reset state of a device node's slot
  360. * @dn: device node to read
  361. * @rets: array to return results in
  362. */
  363. static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
  364. {
  365. int token, outputs;
  366. if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
  367. token = ibm_read_slot_reset_state2;
  368. outputs = 4;
  369. } else {
  370. token = ibm_read_slot_reset_state;
  371. rets[2] = 0; /* fake PE Unavailable info */
  372. outputs = 3;
  373. }
  374. return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
  375. BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
  376. }
  377. /**
  378. * eeh_panic - call panic() for an eeh event that cannot be handled.
  379. * The philosophy of this routine is that it is better to panic and
  380. * halt the OS than it is to risk possible data corruption by
  381. * oblivious device drivers that don't know better.
  382. *
  383. * @dev pci device that had an eeh event
  384. * @reset_state current reset state of the device slot
  385. */
  386. static void eeh_panic(struct pci_dev *dev, int reset_state)
  387. {
  388. /*
  389. * XXX We should create a separate sysctl for this.
  390. *
  391. * Since the panic_on_oops sysctl is used to halt the system
  392. * in light of potential corruption, we can use it here.
  393. */
  394. if (panic_on_oops)
  395. panic("EEH: MMIO failure (%d) on device:%s\n", reset_state,
  396. pci_name(dev));
  397. else {
  398. __get_cpu_var(ignored_failures)++;
  399. printk(KERN_INFO "EEH: Ignored MMIO failure (%d) on device:%s\n",
  400. reset_state, pci_name(dev));
  401. }
  402. }
  403. /**
  404. * eeh_event_handler - dispatch EEH events. The detection of a frozen
  405. * slot can occur inside an interrupt, where it can be hard to do
  406. * anything about it. The goal of this routine is to pull these
  407. * detection events out of the context of the interrupt handler, and
  408. * re-dispatch them for processing at a later time in a normal context.
  409. *
  410. * @dummy - unused
  411. */
  412. static void eeh_event_handler(void *dummy)
  413. {
  414. unsigned long flags;
  415. struct eeh_event *event;
  416. while (1) {
  417. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  418. event = NULL;
  419. if (!list_empty(&eeh_eventlist)) {
  420. event = list_entry(eeh_eventlist.next, struct eeh_event, list);
  421. list_del(&event->list);
  422. }
  423. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  424. if (event == NULL)
  425. break;
  426. printk(KERN_INFO "EEH: MMIO failure (%d), notifiying device "
  427. "%s\n", event->reset_state,
  428. pci_name(event->dev));
  429. atomic_set(&eeh_fail_count, 0);
  430. notifier_call_chain (&eeh_notifier_chain,
  431. EEH_NOTIFY_FREEZE, event);
  432. __get_cpu_var(slot_resets)++;
  433. pci_dev_put(event->dev);
  434. kfree(event);
  435. }
  436. }
  437. /**
  438. * eeh_token_to_phys - convert EEH address token to phys address
  439. * @token i/o token, should be address in the form 0xA....
  440. */
  441. static inline unsigned long eeh_token_to_phys(unsigned long token)
  442. {
  443. pte_t *ptep;
  444. unsigned long pa;
  445. ptep = find_linux_pte(init_mm.pgd, token);
  446. if (!ptep)
  447. return token;
  448. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  449. return pa | (token & (PAGE_SIZE-1));
  450. }
  451. /**
  452. * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
  453. * @dn device node
  454. * @dev pci device, if known
  455. *
  456. * Check for an EEH failure for the given device node. Call this
  457. * routine if the result of a read was all 0xff's and you want to
  458. * find out if this is due to an EEH slot freeze. This routine
  459. * will query firmware for the EEH status.
  460. *
  461. * Returns 0 if there has not been an EEH error; otherwise returns
  462. * a non-zero value and queues up a slot isolation event notification.
  463. *
  464. * It is safe to call this routine in an interrupt context.
  465. */
  466. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  467. {
  468. int ret;
  469. int rets[3];
  470. unsigned long flags;
  471. int rc, reset_state;
  472. struct eeh_event *event;
  473. struct pci_dn *pdn;
  474. __get_cpu_var(total_mmio_ffs)++;
  475. if (!eeh_subsystem_enabled)
  476. return 0;
  477. if (!dn)
  478. return 0;
  479. pdn = PCI_DN(dn);
  480. /* Access to IO BARs might get this far and still not want checking. */
  481. if (!pdn->eeh_capable || !(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  482. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  483. return 0;
  484. }
  485. if (!pdn->eeh_config_addr) {
  486. return 0;
  487. }
  488. /*
  489. * If we already have a pending isolation event for this
  490. * slot, we know it's bad already, we don't need to check...
  491. */
  492. if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
  493. atomic_inc(&eeh_fail_count);
  494. if (atomic_read(&eeh_fail_count) >= EEH_MAX_FAILS) {
  495. /* re-read the slot reset state */
  496. if (read_slot_reset_state(pdn, rets) != 0)
  497. rets[0] = -1; /* reset state unknown */
  498. eeh_panic(dev, rets[0]);
  499. }
  500. return 0;
  501. }
  502. /*
  503. * Now test for an EEH failure. This is VERY expensive.
  504. * Note that the eeh_config_addr may be a parent device
  505. * in the case of a device behind a bridge, or it may be
  506. * function zero of a multi-function device.
  507. * In any case they must share a common PHB.
  508. */
  509. ret = read_slot_reset_state(pdn, rets);
  510. if (!(ret == 0 && rets[1] == 1 && (rets[0] == 2 || rets[0] == 4))) {
  511. __get_cpu_var(false_positives)++;
  512. return 0;
  513. }
  514. /* prevent repeated reports of this failure */
  515. pdn->eeh_mode |= EEH_MODE_ISOLATED;
  516. reset_state = rets[0];
  517. spin_lock_irqsave(&slot_errbuf_lock, flags);
  518. memset(slot_errbuf, 0, eeh_error_buf_size);
  519. rc = rtas_call(ibm_slot_error_detail,
  520. 8, 1, NULL, pdn->eeh_config_addr,
  521. BUID_HI(pdn->phb->buid),
  522. BUID_LO(pdn->phb->buid), NULL, 0,
  523. virt_to_phys(slot_errbuf),
  524. eeh_error_buf_size,
  525. 1 /* Temporary Error */);
  526. if (rc == 0)
  527. log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
  528. spin_unlock_irqrestore(&slot_errbuf_lock, flags);
  529. printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
  530. rets[0], dn->name, dn->full_name);
  531. event = kmalloc(sizeof(*event), GFP_ATOMIC);
  532. if (event == NULL) {
  533. eeh_panic(dev, reset_state);
  534. return 1;
  535. }
  536. event->dev = dev;
  537. event->dn = dn;
  538. event->reset_state = reset_state;
  539. /* We may or may not be called in an interrupt context */
  540. spin_lock_irqsave(&eeh_eventlist_lock, flags);
  541. list_add(&event->list, &eeh_eventlist);
  542. spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
  543. /* Most EEH events are due to device driver bugs. Having
  544. * a stack trace will help the device-driver authors figure
  545. * out what happened. So print that out. */
  546. dump_stack();
  547. schedule_work(&eeh_event_wq);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL(eeh_dn_check_failure);
  551. /**
  552. * eeh_check_failure - check if all 1's data is due to EEH slot freeze
  553. * @token i/o token, should be address in the form 0xA....
  554. * @val value, should be all 1's (XXX why do we need this arg??)
  555. *
  556. * Check for an EEH failure at the given token address. Call this
  557. * routine if the result of a read was all 0xff's and you want to
  558. * find out if this is due to an EEH slot freeze event. This routine
  559. * will query firmware for the EEH status.
  560. *
  561. * Note this routine is safe to call in an interrupt context.
  562. */
  563. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  564. {
  565. unsigned long addr;
  566. struct pci_dev *dev;
  567. struct device_node *dn;
  568. /* Finding the phys addr + pci device; this is pretty quick. */
  569. addr = eeh_token_to_phys((unsigned long __force) token);
  570. dev = pci_get_device_by_addr(addr);
  571. if (!dev)
  572. return val;
  573. dn = pci_device_to_OF_node(dev);
  574. eeh_dn_check_failure (dn, dev);
  575. pci_dev_put(dev);
  576. return val;
  577. }
  578. EXPORT_SYMBOL(eeh_check_failure);
  579. struct eeh_early_enable_info {
  580. unsigned int buid_hi;
  581. unsigned int buid_lo;
  582. };
  583. /* Enable eeh for the given device node. */
  584. static void *early_enable_eeh(struct device_node *dn, void *data)
  585. {
  586. struct eeh_early_enable_info *info = data;
  587. int ret;
  588. char *status = get_property(dn, "status", NULL);
  589. u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
  590. u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
  591. u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
  592. u32 *regs;
  593. int enable;
  594. struct pci_dn *pdn = PCI_DN(dn);
  595. pdn->eeh_mode = 0;
  596. if (status && strcmp(status, "ok") != 0)
  597. return NULL; /* ignore devices with bad status */
  598. /* Ignore bad nodes. */
  599. if (!class_code || !vendor_id || !device_id)
  600. return NULL;
  601. /* There is nothing to check on PCI to ISA bridges */
  602. if (dn->type && !strcmp(dn->type, "isa")) {
  603. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  604. return NULL;
  605. }
  606. /*
  607. * Now decide if we are going to "Disable" EEH checking
  608. * for this device. We still run with the EEH hardware active,
  609. * but we won't be checking for ff's. This means a driver
  610. * could return bad data (very bad!), an interrupt handler could
  611. * hang waiting on status bits that won't change, etc.
  612. * But there are a few cases like display devices that make sense.
  613. */
  614. enable = 1; /* i.e. we will do checking */
  615. if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
  616. enable = 0;
  617. if (!enable)
  618. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  619. /* Ok... see if this device supports EEH. Some do, some don't,
  620. * and the only way to find out is to check each and every one. */
  621. regs = (u32 *)get_property(dn, "reg", NULL);
  622. if (regs) {
  623. /* First register entry is addr (00BBSS00) */
  624. /* Try to enable eeh */
  625. ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
  626. regs[0], info->buid_hi, info->buid_lo,
  627. EEH_ENABLE);
  628. if (ret == 0) {
  629. eeh_subsystem_enabled = 1;
  630. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  631. pdn->eeh_config_addr = regs[0];
  632. #ifdef DEBUG
  633. printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
  634. #endif
  635. } else {
  636. /* This device doesn't support EEH, but it may have an
  637. * EEH parent, in which case we mark it as supported. */
  638. if (dn->parent && PCI_DN(dn->parent)
  639. && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  640. /* Parent supports EEH. */
  641. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  642. pdn->eeh_config_addr = PCI_DN(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 || PCI_DN(phb) == NULL)
  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 || !PCI_DN(dn))
  721. return;
  722. phb = PCI_DN(dn)->phb;
  723. if (NULL == phb || 0 == phb->buid) {
  724. printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
  725. dn->full_name);
  726. dump_stack();
  727. return;
  728. }
  729. info.buid_hi = BUID_HI(phb->buid);
  730. info.buid_lo = BUID_LO(phb->buid);
  731. early_enable_eeh(dn, &info);
  732. }
  733. EXPORT_SYMBOL(eeh_add_device_early);
  734. /**
  735. * eeh_add_device_late - perform EEH initialization for the indicated pci device
  736. * @dev: pci device for which to set up EEH
  737. *
  738. * This routine must be used to complete EEH initialization for PCI
  739. * devices that were added after system boot (e.g. hotplug, dlpar).
  740. */
  741. void eeh_add_device_late(struct pci_dev *dev)
  742. {
  743. if (!dev || !eeh_subsystem_enabled)
  744. return;
  745. #ifdef DEBUG
  746. printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
  747. #endif
  748. pci_addr_cache_insert_device (dev);
  749. }
  750. EXPORT_SYMBOL(eeh_add_device_late);
  751. /**
  752. * eeh_remove_device - undo EEH setup for the indicated pci device
  753. * @dev: pci device to be removed
  754. *
  755. * This routine should be when a device is removed from a running
  756. * system (e.g. by hotplug or dlpar).
  757. */
  758. void eeh_remove_device(struct pci_dev *dev)
  759. {
  760. if (!dev || !eeh_subsystem_enabled)
  761. return;
  762. /* Unregister the device with the EEH/PCI address search system */
  763. #ifdef DEBUG
  764. printk(KERN_DEBUG "EEH: remove device %s\n", pci_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);