eeh.c 28 KB

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