eeh.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * Copyright IBM Corporation 2001, 2005, 2006
  3. * Copyright Dave Engebretsen & Todd Inglett 2001
  4. * Copyright Linas Vepstas 2005, 2006
  5. * Copyright 2001-2012 IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/sched.h>
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/pci.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/rbtree.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/export.h>
  33. #include <linux/of.h>
  34. #include <linux/atomic.h>
  35. #include <asm/eeh.h>
  36. #include <asm/eeh_event.h>
  37. #include <asm/io.h>
  38. #include <asm/machdep.h>
  39. #include <asm/ppc-pci.h>
  40. #include <asm/rtas.h>
  41. /** Overview:
  42. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  43. * dealing with PCI bus errors that can't be dealt with within the
  44. * usual PCI framework, except by check-stopping the CPU. Systems
  45. * that are designed for high-availability/reliability cannot afford
  46. * to crash due to a "mere" PCI error, thus the need for EEH.
  47. * An EEH-capable bridge operates by converting a detected error
  48. * into a "slot freeze", taking the PCI adapter off-line, making
  49. * the slot behave, from the OS'es point of view, as if the slot
  50. * were "empty": all reads return 0xff's and all writes are silently
  51. * ignored. EEH slot isolation events can be triggered by parity
  52. * errors on the address or data busses (e.g. during posted writes),
  53. * which in turn might be caused by low voltage on the bus, dust,
  54. * vibration, humidity, radioactivity or plain-old failed hardware.
  55. *
  56. * Note, however, that one of the leading causes of EEH slot
  57. * freeze events are buggy device drivers, buggy device microcode,
  58. * or buggy device hardware. This is because any attempt by the
  59. * device to bus-master data to a memory address that is not
  60. * assigned to the device will trigger a slot freeze. (The idea
  61. * is to prevent devices-gone-wild from corrupting system memory).
  62. * Buggy hardware/drivers will have a miserable time co-existing
  63. * with EEH.
  64. *
  65. * Ideally, a PCI device driver, when suspecting that an isolation
  66. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  67. * whether this is the case, and then take appropriate steps to
  68. * reset the PCI slot, the PCI device, and then resume operations.
  69. * However, until that day, the checking is done here, with the
  70. * eeh_check_failure() routine embedded in the MMIO macros. If
  71. * the slot is found to be isolated, an "EEH Event" is synthesized
  72. * and sent out for processing.
  73. */
  74. /* If a device driver keeps reading an MMIO register in an interrupt
  75. * handler after a slot isolation event, it might be broken.
  76. * This sets the threshold for how many read attempts we allow
  77. * before printing an error message.
  78. */
  79. #define EEH_MAX_FAILS 2100000
  80. /* Time to wait for a PCI slot to report status, in milliseconds */
  81. #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
  82. /* Platform dependent EEH operations */
  83. struct eeh_ops *eeh_ops = NULL;
  84. int eeh_subsystem_enabled;
  85. EXPORT_SYMBOL(eeh_subsystem_enabled);
  86. /* Global EEH mutex */
  87. DEFINE_MUTEX(eeh_mutex);
  88. /* Lock to avoid races due to multiple reports of an error */
  89. static DEFINE_RAW_SPINLOCK(confirm_error_lock);
  90. /* Buffer for reporting pci register dumps. Its here in BSS, and
  91. * not dynamically alloced, so that it ends up in RMO where RTAS
  92. * can access it.
  93. */
  94. #define EEH_PCI_REGS_LOG_LEN 4096
  95. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  96. /*
  97. * The struct is used to maintain the EEH global statistic
  98. * information. Besides, the EEH global statistics will be
  99. * exported to user space through procfs
  100. */
  101. struct eeh_stats {
  102. u64 no_device; /* PCI device not found */
  103. u64 no_dn; /* OF node not found */
  104. u64 no_cfg_addr; /* Config address not found */
  105. u64 ignored_check; /* EEH check skipped */
  106. u64 total_mmio_ffs; /* Total EEH checks */
  107. u64 false_positives; /* Unnecessary EEH checks */
  108. u64 slot_resets; /* PE reset */
  109. };
  110. static struct eeh_stats eeh_stats;
  111. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  112. /**
  113. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  114. * @edev: device to report data for
  115. * @buf: point to buffer in which to log
  116. * @len: amount of room in buffer
  117. *
  118. * This routine captures assorted PCI configuration space data,
  119. * and puts them into a buffer for RTAS error logging.
  120. */
  121. static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
  122. {
  123. struct device_node *dn = eeh_dev_to_of_node(edev);
  124. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  125. u32 cfg;
  126. int cap, i;
  127. int n = 0;
  128. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  129. printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
  130. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  131. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  132. printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
  133. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  134. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  135. printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
  136. if (!dev) {
  137. printk(KERN_WARNING "EEH: no PCI device for this of node\n");
  138. return n;
  139. }
  140. /* Gather bridge-specific registers */
  141. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  142. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  143. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  144. printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
  145. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  146. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  147. printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
  148. }
  149. /* Dump out the PCI-X command and status regs */
  150. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  151. if (cap) {
  152. eeh_ops->read_config(dn, cap, 4, &cfg);
  153. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  154. printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
  155. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  156. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  157. printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
  158. }
  159. /* If PCI-E capable, dump PCI-E cap 10, and the AER */
  160. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  161. if (cap) {
  162. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  163. printk(KERN_WARNING
  164. "EEH: PCI-E capabilities and status follow:\n");
  165. for (i=0; i<=8; i++) {
  166. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  167. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  168. printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
  169. }
  170. cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  171. if (cap) {
  172. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  173. printk(KERN_WARNING
  174. "EEH: PCI-E AER capability register set follows:\n");
  175. for (i=0; i<14; i++) {
  176. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  177. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  178. printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
  179. }
  180. }
  181. }
  182. /* Gather status on devices under the bridge */
  183. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  184. struct device_node *child;
  185. for_each_child_of_node(dn, child) {
  186. if (of_node_to_eeh_dev(child))
  187. n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
  188. }
  189. }
  190. return n;
  191. }
  192. /**
  193. * eeh_slot_error_detail - Generate combined log including driver log and error log
  194. * @edev: device to report error log for
  195. * @severity: temporary or permanent error log
  196. *
  197. * This routine should be called to generate the combined log, which
  198. * is comprised of driver log and error log. The driver log is figured
  199. * out from the config space of the corresponding PCI device, while
  200. * the error log is fetched through platform dependent function call.
  201. */
  202. void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
  203. {
  204. size_t loglen = 0;
  205. pci_regs_buf[0] = 0;
  206. eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
  207. eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
  208. eeh_restore_bars(edev);
  209. loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
  210. eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
  211. }
  212. /**
  213. * eeh_token_to_phys - Convert EEH address token to phys address
  214. * @token: I/O token, should be address in the form 0xA....
  215. *
  216. * This routine should be called to convert virtual I/O address
  217. * to physical one.
  218. */
  219. static inline unsigned long eeh_token_to_phys(unsigned long token)
  220. {
  221. pte_t *ptep;
  222. unsigned long pa;
  223. ptep = find_linux_pte(init_mm.pgd, token);
  224. if (!ptep)
  225. return token;
  226. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  227. return pa | (token & (PAGE_SIZE-1));
  228. }
  229. /**
  230. * eeh_find_device_pe - Retrieve the PE for the given device
  231. * @dn: device node
  232. *
  233. * Return the PE under which this device lies
  234. */
  235. struct device_node *eeh_find_device_pe(struct device_node *dn)
  236. {
  237. while (dn->parent && of_node_to_eeh_dev(dn->parent) &&
  238. (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
  239. dn = dn->parent;
  240. }
  241. return dn;
  242. }
  243. /**
  244. * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
  245. * @dn: device node
  246. * @dev: pci device, if known
  247. *
  248. * Check for an EEH failure for the given device node. Call this
  249. * routine if the result of a read was all 0xff's and you want to
  250. * find out if this is due to an EEH slot freeze. This routine
  251. * will query firmware for the EEH status.
  252. *
  253. * Returns 0 if there has not been an EEH error; otherwise returns
  254. * a non-zero value and queues up a slot isolation event notification.
  255. *
  256. * It is safe to call this routine in an interrupt context.
  257. */
  258. int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
  259. {
  260. int ret;
  261. unsigned long flags;
  262. struct eeh_dev *edev;
  263. int rc = 0;
  264. const char *location;
  265. eeh_stats.total_mmio_ffs++;
  266. if (!eeh_subsystem_enabled)
  267. return 0;
  268. if (!dn) {
  269. eeh_stats.no_dn++;
  270. return 0;
  271. }
  272. dn = eeh_find_device_pe(dn);
  273. edev = of_node_to_eeh_dev(dn);
  274. /* Access to IO BARs might get this far and still not want checking. */
  275. if (!(edev->mode & EEH_MODE_SUPPORTED) ||
  276. edev->mode & EEH_MODE_NOCHECK) {
  277. eeh_stats.ignored_check++;
  278. pr_debug("EEH: Ignored check (%x) for %s %s\n",
  279. edev->mode, eeh_pci_name(dev), dn->full_name);
  280. return 0;
  281. }
  282. if (!edev->config_addr && !edev->pe_config_addr) {
  283. eeh_stats.no_cfg_addr++;
  284. return 0;
  285. }
  286. /* If we already have a pending isolation event for this
  287. * slot, we know it's bad already, we don't need to check.
  288. * Do this checking under a lock; as multiple PCI devices
  289. * in one slot might report errors simultaneously, and we
  290. * only want one error recovery routine running.
  291. */
  292. raw_spin_lock_irqsave(&confirm_error_lock, flags);
  293. rc = 1;
  294. if (edev->mode & EEH_MODE_ISOLATED) {
  295. edev->check_count++;
  296. if (edev->check_count % EEH_MAX_FAILS == 0) {
  297. location = of_get_property(dn, "ibm,loc-code", NULL);
  298. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  299. "location=%s driver=%s pci addr=%s\n",
  300. edev->check_count, location,
  301. eeh_driver_name(dev), eeh_pci_name(dev));
  302. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  303. eeh_driver_name(dev));
  304. dump_stack();
  305. }
  306. goto dn_unlock;
  307. }
  308. /*
  309. * Now test for an EEH failure. This is VERY expensive.
  310. * Note that the eeh_config_addr may be a parent device
  311. * in the case of a device behind a bridge, or it may be
  312. * function zero of a multi-function device.
  313. * In any case they must share a common PHB.
  314. */
  315. ret = eeh_ops->get_state(dn, NULL);
  316. /* Note that config-io to empty slots may fail;
  317. * they are empty when they don't have children.
  318. * We will punt with the following conditions: Failure to get
  319. * PE's state, EEH not support and Permanently unavailable
  320. * state, PE is in good state.
  321. */
  322. if ((ret < 0) ||
  323. (ret == EEH_STATE_NOT_SUPPORT) ||
  324. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  325. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  326. eeh_stats.false_positives++;
  327. edev->false_positives ++;
  328. rc = 0;
  329. goto dn_unlock;
  330. }
  331. eeh_stats.slot_resets++;
  332. /* Avoid repeated reports of this failure, including problems
  333. * with other functions on this device, and functions under
  334. * bridges.
  335. */
  336. eeh_mark_slot(dn, EEH_MODE_ISOLATED);
  337. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  338. eeh_send_failure_event(edev);
  339. /* Most EEH events are due to device driver bugs. Having
  340. * a stack trace will help the device-driver authors figure
  341. * out what happened. So print that out.
  342. */
  343. WARN(1, "EEH: failure detected\n");
  344. return 1;
  345. dn_unlock:
  346. raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
  347. return rc;
  348. }
  349. EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
  350. /**
  351. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  352. * @token: I/O token, should be address in the form 0xA....
  353. * @val: value, should be all 1's (XXX why do we need this arg??)
  354. *
  355. * Check for an EEH failure at the given token address. Call this
  356. * routine if the result of a read was all 0xff's and you want to
  357. * find out if this is due to an EEH slot freeze event. This routine
  358. * will query firmware for the EEH status.
  359. *
  360. * Note this routine is safe to call in an interrupt context.
  361. */
  362. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  363. {
  364. unsigned long addr;
  365. struct pci_dev *dev;
  366. struct device_node *dn;
  367. /* Finding the phys addr + pci device; this is pretty quick. */
  368. addr = eeh_token_to_phys((unsigned long __force) token);
  369. dev = pci_addr_cache_get_device(addr);
  370. if (!dev) {
  371. eeh_stats.no_device++;
  372. return val;
  373. }
  374. dn = pci_device_to_OF_node(dev);
  375. eeh_dn_check_failure(dn, dev);
  376. pci_dev_put(dev);
  377. return val;
  378. }
  379. EXPORT_SYMBOL(eeh_check_failure);
  380. /**
  381. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  382. * @edev: pci device node
  383. *
  384. * This routine should be called to reenable frozen MMIO or DMA
  385. * so that it would work correctly again. It's useful while doing
  386. * recovery or log collection on the indicated device.
  387. */
  388. int eeh_pci_enable(struct eeh_dev *edev, int function)
  389. {
  390. int rc;
  391. struct device_node *dn = eeh_dev_to_of_node(edev);
  392. rc = eeh_ops->set_option(dn, function);
  393. if (rc)
  394. printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
  395. function, rc, dn->full_name);
  396. rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
  397. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  398. (function == EEH_OPT_THAW_MMIO))
  399. return 0;
  400. return rc;
  401. }
  402. /**
  403. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  404. * @dev: pci device struct
  405. * @state: reset state to enter
  406. *
  407. * Return value:
  408. * 0 if success
  409. */
  410. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  411. {
  412. struct device_node *dn = pci_device_to_OF_node(dev);
  413. switch (state) {
  414. case pcie_deassert_reset:
  415. eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
  416. break;
  417. case pcie_hot_reset:
  418. eeh_ops->reset(dn, EEH_RESET_HOT);
  419. break;
  420. case pcie_warm_reset:
  421. eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
  422. break;
  423. default:
  424. return -EINVAL;
  425. };
  426. return 0;
  427. }
  428. /**
  429. * __eeh_set_pe_freset - Check the required reset for child devices
  430. * @parent: parent device
  431. * @freset: return value
  432. *
  433. * Each device might have its preferred reset type: fundamental or
  434. * hot reset. The routine is used to collect the information from
  435. * the child devices so that they could be reset accordingly.
  436. */
  437. void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
  438. {
  439. struct device_node *dn;
  440. for_each_child_of_node(parent, dn) {
  441. if (of_node_to_eeh_dev(dn)) {
  442. struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
  443. if (dev && dev->driver)
  444. *freset |= dev->needs_freset;
  445. __eeh_set_pe_freset(dn, freset);
  446. }
  447. }
  448. }
  449. /**
  450. * eeh_set_pe_freset - Check the required reset for the indicated device and its children
  451. * @dn: parent device
  452. * @freset: return value
  453. *
  454. * Each device might have its preferred reset type: fundamental or
  455. * hot reset. The routine is used to collected the information for
  456. * the indicated device and its children so that the bunch of the
  457. * devices could be reset properly.
  458. */
  459. void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
  460. {
  461. struct pci_dev *dev;
  462. dn = eeh_find_device_pe(dn);
  463. /* Back up one, since config addrs might be shared */
  464. if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
  465. dn = dn->parent;
  466. dev = of_node_to_eeh_dev(dn)->pdev;
  467. if (dev)
  468. *freset |= dev->needs_freset;
  469. __eeh_set_pe_freset(dn, freset);
  470. }
  471. /**
  472. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  473. * @edev: pci device node to be reset.
  474. *
  475. * Assert the PCI #RST line for 1/4 second.
  476. */
  477. static void eeh_reset_pe_once(struct eeh_dev *edev)
  478. {
  479. unsigned int freset = 0;
  480. struct device_node *dn = eeh_dev_to_of_node(edev);
  481. /* Determine type of EEH reset required for
  482. * Partitionable Endpoint, a hot-reset (1)
  483. * or a fundamental reset (3).
  484. * A fundamental reset required by any device under
  485. * Partitionable Endpoint trumps hot-reset.
  486. */
  487. eeh_set_pe_freset(dn, &freset);
  488. if (freset)
  489. eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
  490. else
  491. eeh_ops->reset(dn, EEH_RESET_HOT);
  492. /* The PCI bus requires that the reset be held high for at least
  493. * a 100 milliseconds. We wait a bit longer 'just in case'.
  494. */
  495. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  496. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  497. /* We might get hit with another EEH freeze as soon as the
  498. * pci slot reset line is dropped. Make sure we don't miss
  499. * these, and clear the flag now.
  500. */
  501. eeh_clear_slot(dn, EEH_MODE_ISOLATED);
  502. eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
  503. /* After a PCI slot has been reset, the PCI Express spec requires
  504. * a 1.5 second idle time for the bus to stabilize, before starting
  505. * up traffic.
  506. */
  507. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  508. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  509. }
  510. /**
  511. * eeh_reset_pe - Reset the indicated PE
  512. * @edev: PCI device associated EEH device
  513. *
  514. * This routine should be called to reset indicated device, including
  515. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  516. * might be involved as well.
  517. */
  518. int eeh_reset_pe(struct eeh_dev *edev)
  519. {
  520. int i, rc;
  521. struct device_node *dn = eeh_dev_to_of_node(edev);
  522. /* Take three shots at resetting the bus */
  523. for (i=0; i<3; i++) {
  524. eeh_reset_pe_once(edev);
  525. rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
  526. if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
  527. return 0;
  528. if (rc < 0) {
  529. printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
  530. dn->full_name);
  531. return -1;
  532. }
  533. printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
  534. i+1, dn->full_name, rc);
  535. }
  536. return -1;
  537. }
  538. /** Save and restore of PCI BARs
  539. *
  540. * Although firmware will set up BARs during boot, it doesn't
  541. * set up device BAR's after a device reset, although it will,
  542. * if requested, set up bridge configuration. Thus, we need to
  543. * configure the PCI devices ourselves.
  544. */
  545. /**
  546. * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
  547. * @edev: PCI device associated EEH device
  548. *
  549. * Loads the PCI configuration space base address registers,
  550. * the expansion ROM base address, the latency timer, and etc.
  551. * from the saved values in the device node.
  552. */
  553. static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
  554. {
  555. int i;
  556. u32 cmd;
  557. struct device_node *dn = eeh_dev_to_of_node(edev);
  558. if (!edev->phb)
  559. return;
  560. for (i=4; i<10; i++) {
  561. eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
  562. }
  563. /* 12 == Expansion ROM Address */
  564. eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
  565. #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
  566. #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
  567. eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
  568. SAVED_BYTE(PCI_CACHE_LINE_SIZE));
  569. eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
  570. SAVED_BYTE(PCI_LATENCY_TIMER));
  571. /* max latency, min grant, interrupt pin and line */
  572. eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
  573. /* Restore PERR & SERR bits, some devices require it,
  574. * don't touch the other command bits
  575. */
  576. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
  577. if (edev->config_space[1] & PCI_COMMAND_PARITY)
  578. cmd |= PCI_COMMAND_PARITY;
  579. else
  580. cmd &= ~PCI_COMMAND_PARITY;
  581. if (edev->config_space[1] & PCI_COMMAND_SERR)
  582. cmd |= PCI_COMMAND_SERR;
  583. else
  584. cmd &= ~PCI_COMMAND_SERR;
  585. eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
  586. }
  587. /**
  588. * eeh_restore_bars - Restore the PCI config space info
  589. * @edev: EEH device
  590. *
  591. * This routine performs a recursive walk to the children
  592. * of this device as well.
  593. */
  594. void eeh_restore_bars(struct eeh_dev *edev)
  595. {
  596. struct device_node *dn;
  597. if (!edev)
  598. return;
  599. if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
  600. eeh_restore_one_device_bars(edev);
  601. for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
  602. eeh_restore_bars(of_node_to_eeh_dev(dn));
  603. }
  604. /**
  605. * eeh_save_bars - Save device bars
  606. * @edev: PCI device associated EEH device
  607. *
  608. * Save the values of the device bars. Unlike the restore
  609. * routine, this routine is *not* recursive. This is because
  610. * PCI devices are added individually; but, for the restore,
  611. * an entire slot is reset at a time.
  612. */
  613. static void eeh_save_bars(struct eeh_dev *edev)
  614. {
  615. int i;
  616. struct device_node *dn;
  617. if (!edev)
  618. return;
  619. dn = eeh_dev_to_of_node(edev);
  620. for (i = 0; i < 16; i++)
  621. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  622. }
  623. /**
  624. * eeh_early_enable - Early enable EEH on the indicated device
  625. * @dn: device node
  626. * @data: BUID
  627. *
  628. * Enable EEH functionality on the specified PCI device. The function
  629. * is expected to be called before real PCI probing is done. However,
  630. * the PHBs have been initialized at this point.
  631. */
  632. static void *eeh_early_enable(struct device_node *dn, void *data)
  633. {
  634. int ret;
  635. const u32 *class_code = of_get_property(dn, "class-code", NULL);
  636. const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
  637. const u32 *device_id = of_get_property(dn, "device-id", NULL);
  638. const u32 *regs;
  639. int enable;
  640. struct eeh_dev *edev = of_node_to_eeh_dev(dn);
  641. edev->class_code = 0;
  642. edev->mode = 0;
  643. edev->check_count = 0;
  644. edev->freeze_count = 0;
  645. edev->false_positives = 0;
  646. if (!of_device_is_available(dn))
  647. return NULL;
  648. /* Ignore bad nodes. */
  649. if (!class_code || !vendor_id || !device_id)
  650. return NULL;
  651. /* There is nothing to check on PCI to ISA bridges */
  652. if (dn->type && !strcmp(dn->type, "isa")) {
  653. edev->mode |= EEH_MODE_NOCHECK;
  654. return NULL;
  655. }
  656. edev->class_code = *class_code;
  657. /* Ok... see if this device supports EEH. Some do, some don't,
  658. * and the only way to find out is to check each and every one.
  659. */
  660. regs = of_get_property(dn, "reg", NULL);
  661. if (regs) {
  662. /* First register entry is addr (00BBSS00) */
  663. /* Try to enable eeh */
  664. ret = eeh_ops->set_option(dn, EEH_OPT_ENABLE);
  665. enable = 0;
  666. if (ret == 0) {
  667. edev->config_addr = regs[0];
  668. /* If the newer, better, ibm,get-config-addr-info is supported,
  669. * then use that instead.
  670. */
  671. edev->pe_config_addr = eeh_ops->get_pe_addr(dn);
  672. /* Some older systems (Power4) allow the
  673. * ibm,set-eeh-option call to succeed even on nodes
  674. * where EEH is not supported. Verify support
  675. * explicitly.
  676. */
  677. ret = eeh_ops->get_state(dn, NULL);
  678. if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
  679. enable = 1;
  680. }
  681. if (enable) {
  682. eeh_subsystem_enabled = 1;
  683. edev->mode |= EEH_MODE_SUPPORTED;
  684. eeh_add_to_parent_pe(edev);
  685. pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
  686. dn->full_name, edev->config_addr,
  687. edev->pe_config_addr);
  688. } else {
  689. /* This device doesn't support EEH, but it may have an
  690. * EEH parent, in which case we mark it as supported.
  691. */
  692. if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
  693. (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
  694. /* Parent supports EEH. */
  695. edev->mode |= EEH_MODE_SUPPORTED;
  696. edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
  697. edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
  698. eeh_add_to_parent_pe(edev);
  699. return NULL;
  700. }
  701. }
  702. } else {
  703. printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
  704. dn->full_name);
  705. }
  706. eeh_save_bars(edev);
  707. return NULL;
  708. }
  709. /**
  710. * eeh_ops_register - Register platform dependent EEH operations
  711. * @ops: platform dependent EEH operations
  712. *
  713. * Register the platform dependent EEH operation callback
  714. * functions. The platform should call this function before
  715. * any other EEH operations.
  716. */
  717. int __init eeh_ops_register(struct eeh_ops *ops)
  718. {
  719. if (!ops->name) {
  720. pr_warning("%s: Invalid EEH ops name for %p\n",
  721. __func__, ops);
  722. return -EINVAL;
  723. }
  724. if (eeh_ops && eeh_ops != ops) {
  725. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  726. __func__, eeh_ops->name, ops->name);
  727. return -EEXIST;
  728. }
  729. eeh_ops = ops;
  730. return 0;
  731. }
  732. /**
  733. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  734. * @name: name of EEH platform operations
  735. *
  736. * Unregister the platform dependent EEH operation callback
  737. * functions.
  738. */
  739. int __exit eeh_ops_unregister(const char *name)
  740. {
  741. if (!name || !strlen(name)) {
  742. pr_warning("%s: Invalid EEH ops name\n",
  743. __func__);
  744. return -EINVAL;
  745. }
  746. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  747. eeh_ops = NULL;
  748. return 0;
  749. }
  750. return -EEXIST;
  751. }
  752. /**
  753. * eeh_init - EEH initialization
  754. *
  755. * Initialize EEH by trying to enable it for all of the adapters in the system.
  756. * As a side effect we can determine here if eeh is supported at all.
  757. * Note that we leave EEH on so failed config cycles won't cause a machine
  758. * check. If a user turns off EEH for a particular adapter they are really
  759. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  760. * grant access to a slot if EEH isn't enabled, and so we always enable
  761. * EEH for all slots/all devices.
  762. *
  763. * The eeh-force-off option disables EEH checking globally, for all slots.
  764. * Even if force-off is set, the EEH hardware is still enabled, so that
  765. * newer systems can boot.
  766. */
  767. static int __init eeh_init(void)
  768. {
  769. struct pci_controller *hose, *tmp;
  770. struct device_node *phb;
  771. int ret;
  772. /* call platform initialization function */
  773. if (!eeh_ops) {
  774. pr_warning("%s: Platform EEH operation not found\n",
  775. __func__);
  776. return -EEXIST;
  777. } else if ((ret = eeh_ops->init())) {
  778. pr_warning("%s: Failed to call platform init function (%d)\n",
  779. __func__, ret);
  780. return ret;
  781. }
  782. raw_spin_lock_init(&confirm_error_lock);
  783. /* Enable EEH for all adapters */
  784. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  785. phb = hose->dn;
  786. traverse_pci_devices(phb, eeh_early_enable, NULL);
  787. }
  788. if (eeh_subsystem_enabled)
  789. printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
  790. else
  791. printk(KERN_WARNING "EEH: No capable adapters found\n");
  792. return ret;
  793. }
  794. core_initcall_sync(eeh_init);
  795. /**
  796. * eeh_add_device_early - Enable EEH for the indicated device_node
  797. * @dn: device node for which to set up EEH
  798. *
  799. * This routine must be used to perform EEH initialization for PCI
  800. * devices that were added after system boot (e.g. hotplug, dlpar).
  801. * This routine must be called before any i/o is performed to the
  802. * adapter (inluding any config-space i/o).
  803. * Whether this actually enables EEH or not for this device depends
  804. * on the CEC architecture, type of the device, on earlier boot
  805. * command-line arguments & etc.
  806. */
  807. static void eeh_add_device_early(struct device_node *dn)
  808. {
  809. struct pci_controller *phb;
  810. if (!dn || !of_node_to_eeh_dev(dn))
  811. return;
  812. phb = of_node_to_eeh_dev(dn)->phb;
  813. /* USB Bus children of PCI devices will not have BUID's */
  814. if (NULL == phb || 0 == phb->buid)
  815. return;
  816. eeh_early_enable(dn, NULL);
  817. }
  818. /**
  819. * eeh_add_device_tree_early - Enable EEH for the indicated device
  820. * @dn: device node
  821. *
  822. * This routine must be used to perform EEH initialization for the
  823. * indicated PCI device that was added after system boot (e.g.
  824. * hotplug, dlpar).
  825. */
  826. void eeh_add_device_tree_early(struct device_node *dn)
  827. {
  828. struct device_node *sib;
  829. for_each_child_of_node(dn, sib)
  830. eeh_add_device_tree_early(sib);
  831. eeh_add_device_early(dn);
  832. }
  833. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  834. /**
  835. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  836. * @dev: pci device for which to set up EEH
  837. *
  838. * This routine must be used to complete EEH initialization for PCI
  839. * devices that were added after system boot (e.g. hotplug, dlpar).
  840. */
  841. static void eeh_add_device_late(struct pci_dev *dev)
  842. {
  843. struct device_node *dn;
  844. struct eeh_dev *edev;
  845. if (!dev || !eeh_subsystem_enabled)
  846. return;
  847. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  848. dn = pci_device_to_OF_node(dev);
  849. edev = of_node_to_eeh_dev(dn);
  850. if (edev->pdev == dev) {
  851. pr_debug("EEH: Already referenced !\n");
  852. return;
  853. }
  854. WARN_ON(edev->pdev);
  855. pci_dev_get(dev);
  856. edev->pdev = dev;
  857. dev->dev.archdata.edev = edev;
  858. pci_addr_cache_insert_device(dev);
  859. eeh_sysfs_add_device(dev);
  860. }
  861. /**
  862. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  863. * @bus: PCI bus
  864. *
  865. * This routine must be used to perform EEH initialization for PCI
  866. * devices which are attached to the indicated PCI bus. The PCI bus
  867. * is added after system boot through hotplug or dlpar.
  868. */
  869. void eeh_add_device_tree_late(struct pci_bus *bus)
  870. {
  871. struct pci_dev *dev;
  872. list_for_each_entry(dev, &bus->devices, bus_list) {
  873. eeh_add_device_late(dev);
  874. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  875. struct pci_bus *subbus = dev->subordinate;
  876. if (subbus)
  877. eeh_add_device_tree_late(subbus);
  878. }
  879. }
  880. }
  881. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  882. /**
  883. * eeh_remove_device - Undo EEH setup for the indicated pci device
  884. * @dev: pci device to be removed
  885. *
  886. * This routine should be called when a device is removed from
  887. * a running system (e.g. by hotplug or dlpar). It unregisters
  888. * the PCI device from the EEH subsystem. I/O errors affecting
  889. * this device will no longer be detected after this call; thus,
  890. * i/o errors affecting this slot may leave this device unusable.
  891. */
  892. static void eeh_remove_device(struct pci_dev *dev)
  893. {
  894. struct eeh_dev *edev;
  895. if (!dev || !eeh_subsystem_enabled)
  896. return;
  897. edev = pci_dev_to_eeh_dev(dev);
  898. /* Unregister the device with the EEH/PCI address search system */
  899. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  900. if (!edev || !edev->pdev) {
  901. pr_debug("EEH: Not referenced !\n");
  902. return;
  903. }
  904. edev->pdev = NULL;
  905. dev->dev.archdata.edev = NULL;
  906. pci_dev_put(dev);
  907. eeh_rmv_from_parent_pe(edev);
  908. pci_addr_cache_remove_device(dev);
  909. eeh_sysfs_remove_device(dev);
  910. }
  911. /**
  912. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  913. * @dev: PCI device
  914. *
  915. * This routine must be called when a device is removed from the
  916. * running system through hotplug or dlpar. The corresponding
  917. * PCI address cache will be removed.
  918. */
  919. void eeh_remove_bus_device(struct pci_dev *dev)
  920. {
  921. struct pci_bus *bus = dev->subordinate;
  922. struct pci_dev *child, *tmp;
  923. eeh_remove_device(dev);
  924. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  925. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  926. eeh_remove_bus_device(child);
  927. }
  928. }
  929. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  930. static int proc_eeh_show(struct seq_file *m, void *v)
  931. {
  932. if (0 == eeh_subsystem_enabled) {
  933. seq_printf(m, "EEH Subsystem is globally disabled\n");
  934. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  935. } else {
  936. seq_printf(m, "EEH Subsystem is enabled\n");
  937. seq_printf(m,
  938. "no device=%llu\n"
  939. "no device node=%llu\n"
  940. "no config address=%llu\n"
  941. "check not wanted=%llu\n"
  942. "eeh_total_mmio_ffs=%llu\n"
  943. "eeh_false_positives=%llu\n"
  944. "eeh_slot_resets=%llu\n",
  945. eeh_stats.no_device,
  946. eeh_stats.no_dn,
  947. eeh_stats.no_cfg_addr,
  948. eeh_stats.ignored_check,
  949. eeh_stats.total_mmio_ffs,
  950. eeh_stats.false_positives,
  951. eeh_stats.slot_resets);
  952. }
  953. return 0;
  954. }
  955. static int proc_eeh_open(struct inode *inode, struct file *file)
  956. {
  957. return single_open(file, proc_eeh_show, NULL);
  958. }
  959. static const struct file_operations proc_eeh_operations = {
  960. .open = proc_eeh_open,
  961. .read = seq_read,
  962. .llseek = seq_lseek,
  963. .release = single_release,
  964. };
  965. static int __init eeh_init_proc(void)
  966. {
  967. if (machine_is(pseries))
  968. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  969. return 0;
  970. }
  971. __initcall(eeh_init_proc);