eeh.c 33 KB

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