eeh.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/list.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/eeh_event.h>
  30. #include <asm/io.h>
  31. #include <asm/machdep.h>
  32. #include <asm/ppc-pci.h>
  33. #include <asm/rtas.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 ibm_get_config_addr_info;
  81. int eeh_subsystem_enabled;
  82. EXPORT_SYMBOL(eeh_subsystem_enabled);
  83. /* Lock to avoid races due to multiple reports of an error */
  84. static DEFINE_SPINLOCK(confirm_error_lock);
  85. /* Buffer for reporting slot-error-detail rtas calls */
  86. static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
  87. static DEFINE_SPINLOCK(slot_errbuf_lock);
  88. static int eeh_error_buf_size;
  89. /* System monitoring statistics */
  90. static DEFINE_PER_CPU(unsigned long, no_device);
  91. static DEFINE_PER_CPU(unsigned long, no_dn);
  92. static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
  93. static DEFINE_PER_CPU(unsigned long, ignored_check);
  94. static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
  95. static DEFINE_PER_CPU(unsigned long, false_positives);
  96. static DEFINE_PER_CPU(unsigned long, ignored_failures);
  97. static DEFINE_PER_CPU(unsigned long, slot_resets);
  98. /* --------------------------------------------------------------- */
  99. /* Below lies the EEH event infrastructure */
  100. void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
  101. {
  102. int config_addr;
  103. unsigned long flags;
  104. int rc;
  105. /* Log the error with the rtas logger */
  106. spin_lock_irqsave(&slot_errbuf_lock, flags);
  107. memset(slot_errbuf, 0, eeh_error_buf_size);
  108. /* Use PE configuration address, if present */
  109. config_addr = pdn->eeh_config_addr;
  110. if (pdn->eeh_pe_config_addr)
  111. config_addr = pdn->eeh_pe_config_addr;
  112. rc = rtas_call(ibm_slot_error_detail,
  113. 8, 1, NULL, config_addr,
  114. BUID_HI(pdn->phb->buid),
  115. BUID_LO(pdn->phb->buid), NULL, 0,
  116. virt_to_phys(slot_errbuf),
  117. eeh_error_buf_size,
  118. severity);
  119. if (rc == 0)
  120. log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
  121. spin_unlock_irqrestore(&slot_errbuf_lock, flags);
  122. }
  123. /**
  124. * read_slot_reset_state - Read the reset state of a device node's slot
  125. * @dn: device node to read
  126. * @rets: array to return results in
  127. */
  128. static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
  129. {
  130. int token, outputs;
  131. int config_addr;
  132. if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
  133. token = ibm_read_slot_reset_state2;
  134. outputs = 4;
  135. } else {
  136. token = ibm_read_slot_reset_state;
  137. rets[2] = 0; /* fake PE Unavailable info */
  138. outputs = 3;
  139. }
  140. /* Use PE configuration address, if present */
  141. config_addr = pdn->eeh_config_addr;
  142. if (pdn->eeh_pe_config_addr)
  143. config_addr = pdn->eeh_pe_config_addr;
  144. return rtas_call(token, 3, outputs, rets, config_addr,
  145. BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
  146. }
  147. /**
  148. * eeh_token_to_phys - convert EEH address token to phys address
  149. * @token i/o token, should be address in the form 0xA....
  150. */
  151. static inline unsigned long eeh_token_to_phys(unsigned long token)
  152. {
  153. pte_t *ptep;
  154. unsigned long pa;
  155. ptep = find_linux_pte(init_mm.pgd, token);
  156. if (!ptep)
  157. return token;
  158. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  159. return pa | (token & (PAGE_SIZE-1));
  160. }
  161. /**
  162. * Return the "partitionable endpoint" (pe) under which this device lies
  163. */
  164. struct device_node * find_device_pe(struct device_node *dn)
  165. {
  166. while ((dn->parent) && PCI_DN(dn->parent) &&
  167. (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  168. dn = dn->parent;
  169. }
  170. return dn;
  171. }
  172. /** Mark all devices that are peers of this device as failed.
  173. * Mark the device driver too, so that it can see the failure
  174. * immediately; this is critical, since some drivers poll
  175. * status registers in interrupts ... If a driver is polling,
  176. * and the slot is frozen, then the driver can deadlock in
  177. * an interrupt context, which is bad.
  178. */
  179. static void __eeh_mark_slot (struct device_node *dn, int mode_flag)
  180. {
  181. while (dn) {
  182. if (PCI_DN(dn)) {
  183. PCI_DN(dn)->eeh_mode |= mode_flag;
  184. /* Mark the pci device driver too */
  185. struct pci_dev *dev = PCI_DN(dn)->pcidev;
  186. if (dev && dev->driver)
  187. dev->error_state = pci_channel_io_frozen;
  188. if (dn->child)
  189. __eeh_mark_slot (dn->child, mode_flag);
  190. }
  191. dn = dn->sibling;
  192. }
  193. }
  194. void eeh_mark_slot (struct device_node *dn, int mode_flag)
  195. {
  196. dn = find_device_pe (dn);
  197. PCI_DN(dn)->eeh_mode |= mode_flag;
  198. __eeh_mark_slot (dn->child, mode_flag);
  199. }
  200. static void __eeh_clear_slot (struct device_node *dn, int mode_flag)
  201. {
  202. while (dn) {
  203. if (PCI_DN(dn)) {
  204. PCI_DN(dn)->eeh_mode &= ~mode_flag;
  205. PCI_DN(dn)->eeh_check_count = 0;
  206. if (dn->child)
  207. __eeh_clear_slot (dn->child, mode_flag);
  208. }
  209. dn = dn->sibling;
  210. }
  211. }
  212. void eeh_clear_slot (struct device_node *dn, int mode_flag)
  213. {
  214. unsigned long flags;
  215. spin_lock_irqsave(&confirm_error_lock, flags);
  216. dn = find_device_pe (dn);
  217. PCI_DN(dn)->eeh_mode &= ~mode_flag;
  218. PCI_DN(dn)->eeh_check_count = 0;
  219. __eeh_clear_slot (dn->child, mode_flag);
  220. spin_unlock_irqrestore(&confirm_error_lock, flags);
  221. }
  222. /**
  223. * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
  224. * @dn device node
  225. * @dev pci device, if known
  226. *
  227. * Check for an EEH failure for the given device node. Call this
  228. * routine if the result of a read was all 0xff's and you want to
  229. * find out if this is due to an EEH slot freeze. This routine
  230. * will query firmware for the EEH status.
  231. *
  232. * Returns 0 if there has not been an EEH error; otherwise returns
  233. * a non-zero value and queues up a slot isolation event notification.
  234. *
  235. * It is safe to call this routine in an interrupt context.
  236. */
  237. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  238. {
  239. int ret;
  240. int rets[3];
  241. unsigned long flags;
  242. struct pci_dn *pdn;
  243. enum pci_channel_state state;
  244. int rc = 0;
  245. __get_cpu_var(total_mmio_ffs)++;
  246. if (!eeh_subsystem_enabled)
  247. return 0;
  248. if (!dn) {
  249. __get_cpu_var(no_dn)++;
  250. return 0;
  251. }
  252. pdn = PCI_DN(dn);
  253. /* Access to IO BARs might get this far and still not want checking. */
  254. if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
  255. pdn->eeh_mode & EEH_MODE_NOCHECK) {
  256. __get_cpu_var(ignored_check)++;
  257. #ifdef DEBUG
  258. printk ("EEH:ignored check (%x) for %s %s\n",
  259. pdn->eeh_mode, pci_name (dev), dn->full_name);
  260. #endif
  261. return 0;
  262. }
  263. if (!pdn->eeh_config_addr && !pdn->eeh_pe_config_addr) {
  264. __get_cpu_var(no_cfg_addr)++;
  265. return 0;
  266. }
  267. /* If we already have a pending isolation event for this
  268. * slot, we know it's bad already, we don't need to check.
  269. * Do this checking under a lock; as multiple PCI devices
  270. * in one slot might report errors simultaneously, and we
  271. * only want one error recovery routine running.
  272. */
  273. spin_lock_irqsave(&confirm_error_lock, flags);
  274. rc = 1;
  275. if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
  276. pdn->eeh_check_count ++;
  277. if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
  278. printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
  279. pdn->eeh_check_count);
  280. dump_stack();
  281. /* re-read the slot reset state */
  282. if (read_slot_reset_state(pdn, rets) != 0)
  283. rets[0] = -1; /* reset state unknown */
  284. /* If we are here, then we hit an infinite loop. Stop. */
  285. panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
  286. }
  287. goto dn_unlock;
  288. }
  289. /*
  290. * Now test for an EEH failure. This is VERY expensive.
  291. * Note that the eeh_config_addr may be a parent device
  292. * in the case of a device behind a bridge, or it may be
  293. * function zero of a multi-function device.
  294. * In any case they must share a common PHB.
  295. */
  296. ret = read_slot_reset_state(pdn, rets);
  297. /* If the call to firmware failed, punt */
  298. if (ret != 0) {
  299. printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
  300. ret, dn->full_name);
  301. __get_cpu_var(false_positives)++;
  302. rc = 0;
  303. goto dn_unlock;
  304. }
  305. /* If EEH is not supported on this device, punt. */
  306. if (rets[1] != 1) {
  307. printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
  308. ret, dn->full_name);
  309. __get_cpu_var(false_positives)++;
  310. rc = 0;
  311. goto dn_unlock;
  312. }
  313. /* If not the kind of error we know about, punt. */
  314. if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
  315. __get_cpu_var(false_positives)++;
  316. rc = 0;
  317. goto dn_unlock;
  318. }
  319. /* Note that config-io to empty slots may fail;
  320. * we recognize empty because they don't have children. */
  321. if ((rets[0] == 5) && (dn->child == NULL)) {
  322. __get_cpu_var(false_positives)++;
  323. rc = 0;
  324. goto dn_unlock;
  325. }
  326. __get_cpu_var(slot_resets)++;
  327. /* Avoid repeated reports of this failure, including problems
  328. * with other functions on this device, and functions under
  329. * bridges. */
  330. eeh_mark_slot (dn, EEH_MODE_ISOLATED);
  331. spin_unlock_irqrestore(&confirm_error_lock, flags);
  332. state = pci_channel_io_normal;
  333. if ((rets[0] == 2) || (rets[0] == 4))
  334. state = pci_channel_io_frozen;
  335. if (rets[0] == 5)
  336. state = pci_channel_io_perm_failure;
  337. eeh_send_failure_event (dn, dev, state, rets[2]);
  338. /* Most EEH events are due to device driver bugs. Having
  339. * a stack trace will help the device-driver authors figure
  340. * out what happened. So print that out. */
  341. if (rets[0] != 5) dump_stack();
  342. return 1;
  343. dn_unlock:
  344. spin_unlock_irqrestore(&confirm_error_lock, flags);
  345. return rc;
  346. }
  347. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  348. /**
  349. * eeh_check_failure - check if all 1's data is due to EEH slot freeze
  350. * @token i/o token, should be address in the form 0xA....
  351. * @val value, should be all 1's (XXX why do we need this arg??)
  352. *
  353. * Check for an EEH failure at the given token address. Call this
  354. * routine if the result of a read was all 0xff's and you want to
  355. * find out if this is due to an EEH slot freeze event. This routine
  356. * will query firmware for the EEH status.
  357. *
  358. * Note this routine is safe to call in an interrupt context.
  359. */
  360. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  361. {
  362. unsigned long addr;
  363. struct pci_dev *dev;
  364. struct device_node *dn;
  365. /* Finding the phys addr + pci device; this is pretty quick. */
  366. addr = eeh_token_to_phys((unsigned long __force) token);
  367. dev = pci_get_device_by_addr(addr);
  368. if (!dev) {
  369. __get_cpu_var(no_device)++;
  370. return val;
  371. }
  372. dn = pci_device_to_OF_node(dev);
  373. eeh_dn_check_failure (dn, dev);
  374. pci_dev_put(dev);
  375. return val;
  376. }
  377. EXPORT_SYMBOL(eeh_check_failure);
  378. /* ------------------------------------------------------------- */
  379. /* The code below deals with error recovery */
  380. /** Return negative value if a permanent error, else return
  381. * a number of milliseconds to wait until the PCI slot is
  382. * ready to be used.
  383. */
  384. static int
  385. eeh_slot_availability(struct pci_dn *pdn)
  386. {
  387. int rc;
  388. int rets[3];
  389. rc = read_slot_reset_state(pdn, rets);
  390. if (rc) return rc;
  391. if (rets[1] == 0) return -1; /* EEH is not supported */
  392. if (rets[0] == 0) return 0; /* Oll Korrect */
  393. if (rets[0] == 5) {
  394. if (rets[2] == 0) return -1; /* permanently unavailable */
  395. return rets[2]; /* number of millisecs to wait */
  396. }
  397. return -1;
  398. }
  399. /** rtas_pci_slot_reset raises/lowers the pci #RST line
  400. * state: 1/0 to raise/lower the #RST
  401. *
  402. * Clear the EEH-frozen condition on a slot. This routine
  403. * asserts the PCI #RST line if the 'state' argument is '1',
  404. * and drops the #RST line if 'state is '0'. This routine is
  405. * safe to call in an interrupt context.
  406. *
  407. */
  408. static void
  409. rtas_pci_slot_reset(struct pci_dn *pdn, int state)
  410. {
  411. int config_addr;
  412. int rc;
  413. BUG_ON (pdn==NULL);
  414. if (!pdn->phb) {
  415. printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",
  416. pdn->node->full_name);
  417. return;
  418. }
  419. /* Use PE configuration address, if present */
  420. config_addr = pdn->eeh_config_addr;
  421. if (pdn->eeh_pe_config_addr)
  422. config_addr = pdn->eeh_pe_config_addr;
  423. rc = rtas_call(ibm_set_slot_reset,4,1, NULL,
  424. config_addr,
  425. BUID_HI(pdn->phb->buid),
  426. BUID_LO(pdn->phb->buid),
  427. state);
  428. if (rc) {
  429. printk (KERN_WARNING "EEH: Unable to reset the failed slot, (%d) #RST=%d dn=%s\n",
  430. rc, state, pdn->node->full_name);
  431. return;
  432. }
  433. }
  434. /** rtas_set_slot_reset -- assert the pci #RST line for 1/4 second
  435. * dn -- device node to be reset.
  436. */
  437. void
  438. rtas_set_slot_reset(struct pci_dn *pdn)
  439. {
  440. int i, rc;
  441. rtas_pci_slot_reset (pdn, 1);
  442. /* The PCI bus requires that the reset be held high for at least
  443. * a 100 milliseconds. We wait a bit longer 'just in case'. */
  444. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  445. msleep (PCI_BUS_RST_HOLD_TIME_MSEC);
  446. /* We might get hit with another EEH freeze as soon as the
  447. * pci slot reset line is dropped. Make sure we don't miss
  448. * these, and clear the flag now. */
  449. eeh_clear_slot (pdn->node, EEH_MODE_ISOLATED);
  450. rtas_pci_slot_reset (pdn, 0);
  451. /* After a PCI slot has been reset, the PCI Express spec requires
  452. * a 1.5 second idle time for the bus to stabilize, before starting
  453. * up traffic. */
  454. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  455. msleep (PCI_BUS_SETTLE_TIME_MSEC);
  456. /* Now double check with the firmware to make sure the device is
  457. * ready to be used; if not, wait for recovery. */
  458. for (i=0; i<10; i++) {
  459. rc = eeh_slot_availability (pdn);
  460. if (rc <= 0) break;
  461. msleep (rc+100);
  462. }
  463. }
  464. /* ------------------------------------------------------- */
  465. /** Save and restore of PCI BARs
  466. *
  467. * Although firmware will set up BARs during boot, it doesn't
  468. * set up device BAR's after a device reset, although it will,
  469. * if requested, set up bridge configuration. Thus, we need to
  470. * configure the PCI devices ourselves.
  471. */
  472. /**
  473. * __restore_bars - Restore the Base Address Registers
  474. * Loads the PCI configuration space base address registers,
  475. * the expansion ROM base address, the latency timer, and etc.
  476. * from the saved values in the device node.
  477. */
  478. static inline void __restore_bars (struct pci_dn *pdn)
  479. {
  480. int i;
  481. if (NULL==pdn->phb) return;
  482. for (i=4; i<10; i++) {
  483. rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
  484. }
  485. /* 12 == Expansion ROM Address */
  486. rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
  487. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  488. #define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
  489. rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,
  490. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  491. rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,
  492. SAVED_BYTE(PCI_LATENCY_TIMER));
  493. /* max latency, min grant, interrupt pin and line */
  494. rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
  495. }
  496. /**
  497. * eeh_restore_bars - restore the PCI config space info
  498. *
  499. * This routine performs a recursive walk to the children
  500. * of this device as well.
  501. */
  502. void eeh_restore_bars(struct pci_dn *pdn)
  503. {
  504. struct device_node *dn;
  505. if (!pdn)
  506. return;
  507. if (! pdn->eeh_is_bridge)
  508. __restore_bars (pdn);
  509. dn = pdn->node->child;
  510. while (dn) {
  511. eeh_restore_bars (PCI_DN(dn));
  512. dn = dn->sibling;
  513. }
  514. }
  515. /**
  516. * eeh_save_bars - save device bars
  517. *
  518. * Save the values of the device bars. Unlike the restore
  519. * routine, this routine is *not* recursive. This is because
  520. * PCI devices are added individuallly; but, for the restore,
  521. * an entire slot is reset at a time.
  522. */
  523. void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn)
  524. {
  525. int i;
  526. if (!pdev || !pdn )
  527. return;
  528. for (i = 0; i < 16; i++)
  529. pci_read_config_dword(pdev, i * 4, &pdn->config_space[i]);
  530. if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  531. pdn->eeh_is_bridge = 1;
  532. }
  533. void
  534. rtas_configure_bridge(struct pci_dn *pdn)
  535. {
  536. int config_addr;
  537. int token = rtas_token ("ibm,configure-bridge");
  538. int rc;
  539. if (token == RTAS_UNKNOWN_SERVICE)
  540. return;
  541. /* Use PE configuration address, if present */
  542. config_addr = pdn->eeh_config_addr;
  543. if (pdn->eeh_pe_config_addr)
  544. config_addr = pdn->eeh_pe_config_addr;
  545. rc = rtas_call(token,3,1, NULL,
  546. config_addr,
  547. BUID_HI(pdn->phb->buid),
  548. BUID_LO(pdn->phb->buid));
  549. if (rc) {
  550. printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",
  551. rc, pdn->node->full_name);
  552. }
  553. }
  554. /* ------------------------------------------------------------- */
  555. /* The code below deals with enabling EEH for devices during the
  556. * early boot sequence. EEH must be enabled before any PCI probing
  557. * can be done.
  558. */
  559. #define EEH_ENABLE 1
  560. struct eeh_early_enable_info {
  561. unsigned int buid_hi;
  562. unsigned int buid_lo;
  563. };
  564. /* Enable eeh for the given device node. */
  565. static void *early_enable_eeh(struct device_node *dn, void *data)
  566. {
  567. struct eeh_early_enable_info *info = data;
  568. int ret;
  569. char *status = get_property(dn, "status", NULL);
  570. u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
  571. u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
  572. u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
  573. u32 *regs;
  574. int enable;
  575. struct pci_dn *pdn = PCI_DN(dn);
  576. pdn->eeh_mode = 0;
  577. pdn->eeh_check_count = 0;
  578. pdn->eeh_freeze_count = 0;
  579. if (status && strcmp(status, "ok") != 0)
  580. return NULL; /* ignore devices with bad status */
  581. /* Ignore bad nodes. */
  582. if (!class_code || !vendor_id || !device_id)
  583. return NULL;
  584. /* There is nothing to check on PCI to ISA bridges */
  585. if (dn->type && !strcmp(dn->type, "isa")) {
  586. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  587. return NULL;
  588. }
  589. /*
  590. * Now decide if we are going to "Disable" EEH checking
  591. * for this device. We still run with the EEH hardware active,
  592. * but we won't be checking for ff's. This means a driver
  593. * could return bad data (very bad!), an interrupt handler could
  594. * hang waiting on status bits that won't change, etc.
  595. * But there are a few cases like display devices that make sense.
  596. */
  597. enable = 1; /* i.e. we will do checking */
  598. #if 0
  599. if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
  600. enable = 0;
  601. #endif
  602. if (!enable)
  603. pdn->eeh_mode |= EEH_MODE_NOCHECK;
  604. /* Ok... see if this device supports EEH. Some do, some don't,
  605. * and the only way to find out is to check each and every one. */
  606. regs = (u32 *)get_property(dn, "reg", NULL);
  607. if (regs) {
  608. /* First register entry is addr (00BBSS00) */
  609. /* Try to enable eeh */
  610. ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
  611. regs[0], info->buid_hi, info->buid_lo,
  612. EEH_ENABLE);
  613. if (ret == 0) {
  614. eeh_subsystem_enabled = 1;
  615. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  616. pdn->eeh_config_addr = regs[0];
  617. /* If the newer, better, ibm,get-config-addr-info is supported,
  618. * then use that instead. */
  619. pdn->eeh_pe_config_addr = 0;
  620. if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
  621. unsigned int rets[2];
  622. ret = rtas_call (ibm_get_config_addr_info, 4, 2, rets,
  623. pdn->eeh_config_addr,
  624. info->buid_hi, info->buid_lo,
  625. 0);
  626. if (ret == 0)
  627. pdn->eeh_pe_config_addr = rets[0];
  628. }
  629. #ifdef DEBUG
  630. printk(KERN_DEBUG "EEH: %s: eeh enabled, config=%x pe_config=%x\n",
  631. dn->full_name, pdn->eeh_config_addr, pdn->eeh_pe_config_addr);
  632. #endif
  633. } else {
  634. /* This device doesn't support EEH, but it may have an
  635. * EEH parent, in which case we mark it as supported. */
  636. if (dn->parent && PCI_DN(dn->parent)
  637. && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
  638. /* Parent supports EEH. */
  639. pdn->eeh_mode |= EEH_MODE_SUPPORTED;
  640. pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
  641. return NULL;
  642. }
  643. }
  644. } else {
  645. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  646. dn->full_name);
  647. }
  648. return NULL;
  649. }
  650. /*
  651. * Initialize EEH by trying to enable it for all of the adapters in the system.
  652. * As a side effect we can determine here if eeh is supported at all.
  653. * Note that we leave EEH on so failed config cycles won't cause a machine
  654. * check. If a user turns off EEH for a particular adapter they are really
  655. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  656. * grant access to a slot if EEH isn't enabled, and so we always enable
  657. * EEH for all slots/all devices.
  658. *
  659. * The eeh-force-off option disables EEH checking globally, for all slots.
  660. * Even if force-off is set, the EEH hardware is still enabled, so that
  661. * newer systems can boot.
  662. */
  663. void __init eeh_init(void)
  664. {
  665. struct device_node *phb, *np;
  666. struct eeh_early_enable_info info;
  667. spin_lock_init(&confirm_error_lock);
  668. spin_lock_init(&slot_errbuf_lock);
  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. ibm_get_config_addr_info = rtas_token("ibm,get-config-addr-info");
  678. if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
  679. return;
  680. eeh_error_buf_size = rtas_token("rtas-error-log-max");
  681. if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
  682. eeh_error_buf_size = 1024;
  683. }
  684. if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
  685. printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
  686. "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
  687. eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
  688. }
  689. /* Enable EEH for all adapters. Note that eeh requires buid's */
  690. for (phb = of_find_node_by_name(NULL, "pci"); phb;
  691. phb = of_find_node_by_name(phb, "pci")) {
  692. unsigned long buid;
  693. buid = get_phb_buid(phb);
  694. if (buid == 0 || PCI_DN(phb) == NULL)
  695. continue;
  696. info.buid_lo = BUID_LO(buid);
  697. info.buid_hi = BUID_HI(buid);
  698. traverse_pci_devices(phb, early_enable_eeh, &info);
  699. }
  700. if (eeh_subsystem_enabled)
  701. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  702. else
  703. printk(KERN_WARNING "EEH: No capable adapters found\n");
  704. }
  705. /**
  706. * eeh_add_device_early - enable EEH for the indicated device_node
  707. * @dn: device node for which to set up EEH
  708. *
  709. * This routine must be used to perform EEH initialization for PCI
  710. * devices that were added after system boot (e.g. hotplug, dlpar).
  711. * This routine must be called before any i/o is performed to the
  712. * adapter (inluding any config-space i/o).
  713. * Whether this actually enables EEH or not for this device depends
  714. * on the CEC architecture, type of the device, on earlier boot
  715. * command-line arguments & etc.
  716. */
  717. void eeh_add_device_early(struct device_node *dn)
  718. {
  719. struct pci_controller *phb;
  720. struct eeh_early_enable_info info;
  721. if (!dn || !PCI_DN(dn))
  722. return;
  723. phb = PCI_DN(dn)->phb;
  724. /* USB Bus children of PCI devices will not have BUID's */
  725. if (NULL == phb || 0 == phb->buid)
  726. return;
  727. info.buid_hi = BUID_HI(phb->buid);
  728. info.buid_lo = BUID_LO(phb->buid);
  729. early_enable_eeh(dn, &info);
  730. }
  731. EXPORT_SYMBOL_GPL(eeh_add_device_early);
  732. void eeh_add_device_tree_early(struct device_node *dn)
  733. {
  734. struct device_node *sib;
  735. for (sib = dn->child; sib; sib = sib->sibling)
  736. eeh_add_device_tree_early(sib);
  737. eeh_add_device_early(dn);
  738. }
  739. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  740. /**
  741. * eeh_add_device_late - perform EEH initialization for the indicated pci device
  742. * @dev: pci device for which to set up EEH
  743. *
  744. * This routine must be used to complete EEH initialization for PCI
  745. * devices that were added after system boot (e.g. hotplug, dlpar).
  746. */
  747. void eeh_add_device_late(struct pci_dev *dev)
  748. {
  749. struct device_node *dn;
  750. struct pci_dn *pdn;
  751. if (!dev || !eeh_subsystem_enabled)
  752. return;
  753. #ifdef DEBUG
  754. printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
  755. #endif
  756. pci_dev_get (dev);
  757. dn = pci_device_to_OF_node(dev);
  758. pdn = PCI_DN(dn);
  759. pdn->pcidev = dev;
  760. pci_addr_cache_insert_device (dev);
  761. eeh_save_bars(dev, pdn);
  762. }
  763. EXPORT_SYMBOL_GPL(eeh_add_device_late);
  764. /**
  765. * eeh_remove_device - undo EEH setup for the indicated pci device
  766. * @dev: pci device to be removed
  767. *
  768. * This routine should be when a device is removed from a running
  769. * system (e.g. by hotplug or dlpar).
  770. */
  771. void eeh_remove_device(struct pci_dev *dev)
  772. {
  773. struct device_node *dn;
  774. if (!dev || !eeh_subsystem_enabled)
  775. return;
  776. /* Unregister the device with the EEH/PCI address search system */
  777. #ifdef DEBUG
  778. printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
  779. #endif
  780. pci_addr_cache_remove_device(dev);
  781. dn = pci_device_to_OF_node(dev);
  782. PCI_DN(dn)->pcidev = NULL;
  783. pci_dev_put (dev);
  784. }
  785. EXPORT_SYMBOL_GPL(eeh_remove_device);
  786. void eeh_remove_bus_device(struct pci_dev *dev)
  787. {
  788. eeh_remove_device(dev);
  789. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  790. struct pci_bus *bus = dev->subordinate;
  791. struct list_head *ln;
  792. if (!bus)
  793. return;
  794. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  795. struct pci_dev *pdev = pci_dev_b(ln);
  796. if (pdev)
  797. eeh_remove_bus_device(pdev);
  798. }
  799. }
  800. }
  801. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  802. static int proc_eeh_show(struct seq_file *m, void *v)
  803. {
  804. unsigned int cpu;
  805. unsigned long ffs = 0, positives = 0, failures = 0;
  806. unsigned long resets = 0;
  807. unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
  808. for_each_cpu(cpu) {
  809. ffs += per_cpu(total_mmio_ffs, cpu);
  810. positives += per_cpu(false_positives, cpu);
  811. failures += per_cpu(ignored_failures, cpu);
  812. resets += per_cpu(slot_resets, cpu);
  813. no_dev += per_cpu(no_device, cpu);
  814. no_dn += per_cpu(no_dn, cpu);
  815. no_cfg += per_cpu(no_cfg_addr, cpu);
  816. no_check += per_cpu(ignored_check, cpu);
  817. }
  818. if (0 == eeh_subsystem_enabled) {
  819. seq_printf(m, "EEH Subsystem is globally disabled\n");
  820. seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
  821. } else {
  822. seq_printf(m, "EEH Subsystem is enabled\n");
  823. seq_printf(m,
  824. "no device=%ld\n"
  825. "no device node=%ld\n"
  826. "no config address=%ld\n"
  827. "check not wanted=%ld\n"
  828. "eeh_total_mmio_ffs=%ld\n"
  829. "eeh_false_positives=%ld\n"
  830. "eeh_ignored_failures=%ld\n"
  831. "eeh_slot_resets=%ld\n",
  832. no_dev, no_dn, no_cfg, no_check,
  833. ffs, positives, failures, resets);
  834. }
  835. return 0;
  836. }
  837. static int proc_eeh_open(struct inode *inode, struct file *file)
  838. {
  839. return single_open(file, proc_eeh_show, NULL);
  840. }
  841. static struct file_operations proc_eeh_operations = {
  842. .open = proc_eeh_open,
  843. .read = seq_read,
  844. .llseek = seq_lseek,
  845. .release = single_release,
  846. };
  847. static int __init eeh_init_proc(void)
  848. {
  849. struct proc_dir_entry *e;
  850. if (platform_is_pseries()) {
  851. e = create_proc_entry("ppc64/eeh", 0, NULL);
  852. if (e)
  853. e->proc_fops = &proc_eeh_operations;
  854. }
  855. return 0;
  856. }
  857. __initcall(eeh_init_proc);