eeh.c 29 KB

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