eeh.c 29 KB

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