eeh.c 30 KB

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