eeh.c 32 KB

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