eeh.c 29 KB

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