eeh.c 32 KB

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