eeh.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. /*
  87. * EEH probe mode support. The intention is to support multiple
  88. * platforms for EEH. Some platforms like pSeries do PCI emunation
  89. * based on device tree. However, other platforms like powernv probe
  90. * PCI devices from hardware. The flag is used to distinguish that.
  91. * In addition, struct eeh_ops::probe would be invoked for particular
  92. * OF node or PCI device so that the corresponding PE would be created
  93. * there.
  94. */
  95. int eeh_probe_mode;
  96. /* Global EEH mutex */
  97. DEFINE_MUTEX(eeh_mutex);
  98. /* Lock to avoid races due to multiple reports of an error */
  99. DEFINE_RAW_SPINLOCK(confirm_error_lock);
  100. /* Buffer for reporting pci register dumps. Its here in BSS, and
  101. * not dynamically alloced, so that it ends up in RMO where RTAS
  102. * can access it.
  103. */
  104. #define EEH_PCI_REGS_LOG_LEN 4096
  105. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  106. /*
  107. * The struct is used to maintain the EEH global statistic
  108. * information. Besides, the EEH global statistics will be
  109. * exported to user space through procfs
  110. */
  111. struct eeh_stats {
  112. u64 no_device; /* PCI device not found */
  113. u64 no_dn; /* OF node not found */
  114. u64 no_cfg_addr; /* Config address not found */
  115. u64 ignored_check; /* EEH check skipped */
  116. u64 total_mmio_ffs; /* Total EEH checks */
  117. u64 false_positives; /* Unnecessary EEH checks */
  118. u64 slot_resets; /* PE reset */
  119. };
  120. static struct eeh_stats eeh_stats;
  121. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  122. /**
  123. * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
  124. * @edev: device to report data for
  125. * @buf: point to buffer in which to log
  126. * @len: amount of room in buffer
  127. *
  128. * This routine captures assorted PCI configuration space data,
  129. * and puts them into a buffer for RTAS error logging.
  130. */
  131. static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
  132. {
  133. struct device_node *dn = eeh_dev_to_of_node(edev);
  134. struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
  135. u32 cfg;
  136. int cap, i;
  137. int n = 0;
  138. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  139. printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
  140. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  141. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  142. printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
  143. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  144. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  145. printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
  146. if (!dev) {
  147. printk(KERN_WARNING "EEH: no PCI device for this of node\n");
  148. return n;
  149. }
  150. /* Gather bridge-specific registers */
  151. if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
  152. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  153. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  154. printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
  155. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  156. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  157. printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
  158. }
  159. /* Dump out the PCI-X command and status regs */
  160. cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  161. if (cap) {
  162. eeh_ops->read_config(dn, cap, 4, &cfg);
  163. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  164. printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
  165. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  166. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  167. printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
  168. }
  169. /* If PCI-E capable, dump PCI-E cap 10, and the AER */
  170. cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  171. if (cap) {
  172. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  173. printk(KERN_WARNING
  174. "EEH: PCI-E capabilities and status follow:\n");
  175. for (i=0; i<=8; 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 %02x: %08x\n", i, cfg);
  179. }
  180. cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  181. if (cap) {
  182. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  183. printk(KERN_WARNING
  184. "EEH: PCI-E AER capability register set follows:\n");
  185. for (i=0; i<14; i++) {
  186. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  187. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  188. printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
  189. }
  190. }
  191. }
  192. return n;
  193. }
  194. /**
  195. * eeh_slot_error_detail - Generate combined log including driver log and error log
  196. * @pe: EEH PE
  197. * @severity: temporary or permanent error log
  198. *
  199. * This routine should be called to generate the combined log, which
  200. * is comprised of driver log and error log. The driver log is figured
  201. * out from the config space of the corresponding PCI device, while
  202. * the error log is fetched through platform dependent function call.
  203. */
  204. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  205. {
  206. size_t loglen = 0;
  207. struct eeh_dev *edev;
  208. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  209. eeh_ops->configure_bridge(pe);
  210. eeh_pe_restore_bars(pe);
  211. pci_regs_buf[0] = 0;
  212. eeh_pe_for_each_dev(pe, edev) {
  213. loglen += eeh_gather_pci_data(edev, pci_regs_buf,
  214. EEH_PCI_REGS_LOG_LEN);
  215. }
  216. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  217. }
  218. /**
  219. * eeh_token_to_phys - Convert EEH address token to phys address
  220. * @token: I/O token, should be address in the form 0xA....
  221. *
  222. * This routine should be called to convert virtual I/O address
  223. * to physical one.
  224. */
  225. static inline unsigned long eeh_token_to_phys(unsigned long token)
  226. {
  227. pte_t *ptep;
  228. unsigned long pa;
  229. int hugepage_shift;
  230. /*
  231. * We won't find hugepages here, iomem
  232. */
  233. ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
  234. if (!ptep)
  235. return token;
  236. WARN_ON(hugepage_shift);
  237. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  238. return pa | (token & (PAGE_SIZE-1));
  239. }
  240. /*
  241. * On PowerNV platform, we might already have fenced PHB there.
  242. * For that case, it's meaningless to recover frozen PE. Intead,
  243. * We have to handle fenced PHB firstly.
  244. */
  245. static int eeh_phb_check_failure(struct eeh_pe *pe)
  246. {
  247. struct eeh_pe *phb_pe;
  248. unsigned long flags;
  249. int ret;
  250. if (!eeh_probe_mode_dev())
  251. return -EPERM;
  252. /* Find the PHB PE */
  253. phb_pe = eeh_phb_pe_get(pe->phb);
  254. if (!phb_pe) {
  255. pr_warning("%s Can't find PE for PHB#%d\n",
  256. __func__, pe->phb->global_number);
  257. return -EEXIST;
  258. }
  259. /* If the PHB has been in problematic state */
  260. eeh_serialize_lock(&flags);
  261. if (phb_pe->state & (EEH_PE_ISOLATED | EEH_PE_PHB_DEAD)) {
  262. ret = 0;
  263. goto out;
  264. }
  265. /* Check PHB state */
  266. ret = eeh_ops->get_state(phb_pe, NULL);
  267. if ((ret < 0) ||
  268. (ret == EEH_STATE_NOT_SUPPORT) ||
  269. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  270. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  271. ret = 0;
  272. goto out;
  273. }
  274. /* Isolate the PHB and send event */
  275. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  276. eeh_serialize_unlock(flags);
  277. eeh_send_failure_event(phb_pe);
  278. WARN(1, "EEH: PHB failure detected\n");
  279. return 1;
  280. out:
  281. eeh_serialize_unlock(flags);
  282. return ret;
  283. }
  284. /**
  285. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  286. * @edev: eeh device
  287. *
  288. * Check for an EEH failure for the given device node. Call this
  289. * routine if the result of a read was all 0xff's and you want to
  290. * find out if this is due to an EEH slot freeze. This routine
  291. * will query firmware for the EEH status.
  292. *
  293. * Returns 0 if there has not been an EEH error; otherwise returns
  294. * a non-zero value and queues up a slot isolation event notification.
  295. *
  296. * It is safe to call this routine in an interrupt context.
  297. */
  298. int eeh_dev_check_failure(struct eeh_dev *edev)
  299. {
  300. int ret;
  301. unsigned long flags;
  302. struct device_node *dn;
  303. struct pci_dev *dev;
  304. struct eeh_pe *pe;
  305. int rc = 0;
  306. const char *location;
  307. eeh_stats.total_mmio_ffs++;
  308. if (!eeh_subsystem_enabled)
  309. return 0;
  310. if (!edev) {
  311. eeh_stats.no_dn++;
  312. return 0;
  313. }
  314. dn = eeh_dev_to_of_node(edev);
  315. dev = eeh_dev_to_pci_dev(edev);
  316. pe = edev->pe;
  317. /* Access to IO BARs might get this far and still not want checking. */
  318. if (!pe) {
  319. eeh_stats.ignored_check++;
  320. pr_debug("EEH: Ignored check for %s %s\n",
  321. eeh_pci_name(dev), dn->full_name);
  322. return 0;
  323. }
  324. if (!pe->addr && !pe->config_addr) {
  325. eeh_stats.no_cfg_addr++;
  326. return 0;
  327. }
  328. /*
  329. * On PowerNV platform, we might already have fenced PHB
  330. * there and we need take care of that firstly.
  331. */
  332. ret = eeh_phb_check_failure(pe);
  333. if (ret > 0)
  334. return ret;
  335. /* If we already have a pending isolation event for this
  336. * slot, we know it's bad already, we don't need to check.
  337. * Do this checking under a lock; as multiple PCI devices
  338. * in one slot might report errors simultaneously, and we
  339. * only want one error recovery routine running.
  340. */
  341. eeh_serialize_lock(&flags);
  342. rc = 1;
  343. if (pe->state & EEH_PE_ISOLATED) {
  344. pe->check_count++;
  345. if (pe->check_count % EEH_MAX_FAILS == 0) {
  346. location = of_get_property(dn, "ibm,loc-code", NULL);
  347. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  348. "location=%s driver=%s pci addr=%s\n",
  349. pe->check_count, location,
  350. eeh_driver_name(dev), eeh_pci_name(dev));
  351. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  352. eeh_driver_name(dev));
  353. dump_stack();
  354. }
  355. goto dn_unlock;
  356. }
  357. /*
  358. * Now test for an EEH failure. This is VERY expensive.
  359. * Note that the eeh_config_addr may be a parent device
  360. * in the case of a device behind a bridge, or it may be
  361. * function zero of a multi-function device.
  362. * In any case they must share a common PHB.
  363. */
  364. ret = eeh_ops->get_state(pe, NULL);
  365. /* Note that config-io to empty slots may fail;
  366. * they are empty when they don't have children.
  367. * We will punt with the following conditions: Failure to get
  368. * PE's state, EEH not support and Permanently unavailable
  369. * state, PE is in good state.
  370. */
  371. if ((ret < 0) ||
  372. (ret == EEH_STATE_NOT_SUPPORT) ||
  373. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  374. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  375. eeh_stats.false_positives++;
  376. pe->false_positives++;
  377. rc = 0;
  378. goto dn_unlock;
  379. }
  380. eeh_stats.slot_resets++;
  381. /* Avoid repeated reports of this failure, including problems
  382. * with other functions on this device, and functions under
  383. * bridges.
  384. */
  385. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  386. eeh_serialize_unlock(flags);
  387. eeh_send_failure_event(pe);
  388. /* Most EEH events are due to device driver bugs. Having
  389. * a stack trace will help the device-driver authors figure
  390. * out what happened. So print that out.
  391. */
  392. WARN(1, "EEH: failure detected\n");
  393. return 1;
  394. dn_unlock:
  395. eeh_serialize_unlock(flags);
  396. return rc;
  397. }
  398. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  399. /**
  400. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  401. * @token: I/O token, should be address in the form 0xA....
  402. * @val: value, should be all 1's (XXX why do we need this arg??)
  403. *
  404. * Check for an EEH failure at the given token address. Call this
  405. * routine if the result of a read was all 0xff's and you want to
  406. * find out if this is due to an EEH slot freeze event. This routine
  407. * will query firmware for the EEH status.
  408. *
  409. * Note this routine is safe to call in an interrupt context.
  410. */
  411. unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
  412. {
  413. unsigned long addr;
  414. struct eeh_dev *edev;
  415. /* Finding the phys addr + pci device; this is pretty quick. */
  416. addr = eeh_token_to_phys((unsigned long __force) token);
  417. edev = eeh_addr_cache_get_dev(addr);
  418. if (!edev) {
  419. eeh_stats.no_device++;
  420. return val;
  421. }
  422. eeh_dev_check_failure(edev);
  423. pci_dev_put(eeh_dev_to_pci_dev(edev));
  424. return val;
  425. }
  426. EXPORT_SYMBOL(eeh_check_failure);
  427. /**
  428. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  429. * @pe: EEH PE
  430. *
  431. * This routine should be called to reenable frozen MMIO or DMA
  432. * so that it would work correctly again. It's useful while doing
  433. * recovery or log collection on the indicated device.
  434. */
  435. int eeh_pci_enable(struct eeh_pe *pe, int function)
  436. {
  437. int rc;
  438. rc = eeh_ops->set_option(pe, function);
  439. if (rc)
  440. pr_warning("%s: Unexpected state change %d on PHB#%d-PE#%x, err=%d\n",
  441. __func__, function, pe->phb->global_number, pe->addr, rc);
  442. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  443. if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
  444. (function == EEH_OPT_THAW_MMIO))
  445. return 0;
  446. return rc;
  447. }
  448. /**
  449. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  450. * @dev: pci device struct
  451. * @state: reset state to enter
  452. *
  453. * Return value:
  454. * 0 if success
  455. */
  456. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  457. {
  458. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  459. struct eeh_pe *pe = edev->pe;
  460. if (!pe) {
  461. pr_err("%s: No PE found on PCI device %s\n",
  462. __func__, pci_name(dev));
  463. return -EINVAL;
  464. }
  465. switch (state) {
  466. case pcie_deassert_reset:
  467. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  468. break;
  469. case pcie_hot_reset:
  470. eeh_ops->reset(pe, EEH_RESET_HOT);
  471. break;
  472. case pcie_warm_reset:
  473. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  474. break;
  475. default:
  476. return -EINVAL;
  477. };
  478. return 0;
  479. }
  480. /**
  481. * eeh_set_pe_freset - Check the required reset for the indicated device
  482. * @data: EEH device
  483. * @flag: return value
  484. *
  485. * Each device might have its preferred reset type: fundamental or
  486. * hot reset. The routine is used to collected the information for
  487. * the indicated device and its children so that the bunch of the
  488. * devices could be reset properly.
  489. */
  490. static void *eeh_set_dev_freset(void *data, void *flag)
  491. {
  492. struct pci_dev *dev;
  493. unsigned int *freset = (unsigned int *)flag;
  494. struct eeh_dev *edev = (struct eeh_dev *)data;
  495. dev = eeh_dev_to_pci_dev(edev);
  496. if (dev)
  497. *freset |= dev->needs_freset;
  498. return NULL;
  499. }
  500. /**
  501. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  502. * @pe: EEH PE
  503. *
  504. * Assert the PCI #RST line for 1/4 second.
  505. */
  506. static void eeh_reset_pe_once(struct eeh_pe *pe)
  507. {
  508. unsigned int freset = 0;
  509. /* Determine type of EEH reset required for
  510. * Partitionable Endpoint, a hot-reset (1)
  511. * or a fundamental reset (3).
  512. * A fundamental reset required by any device under
  513. * Partitionable Endpoint trumps hot-reset.
  514. */
  515. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  516. if (freset)
  517. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  518. else
  519. eeh_ops->reset(pe, EEH_RESET_HOT);
  520. /* The PCI bus requires that the reset be held high for at least
  521. * a 100 milliseconds. We wait a bit longer 'just in case'.
  522. */
  523. #define PCI_BUS_RST_HOLD_TIME_MSEC 250
  524. msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
  525. /* We might get hit with another EEH freeze as soon as the
  526. * pci slot reset line is dropped. Make sure we don't miss
  527. * these, and clear the flag now.
  528. */
  529. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  530. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  531. /* After a PCI slot has been reset, the PCI Express spec requires
  532. * a 1.5 second idle time for the bus to stabilize, before starting
  533. * up traffic.
  534. */
  535. #define PCI_BUS_SETTLE_TIME_MSEC 1800
  536. msleep(PCI_BUS_SETTLE_TIME_MSEC);
  537. }
  538. /**
  539. * eeh_reset_pe - Reset the indicated PE
  540. * @pe: EEH PE
  541. *
  542. * This routine should be called to reset indicated device, including
  543. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  544. * might be involved as well.
  545. */
  546. int eeh_reset_pe(struct eeh_pe *pe)
  547. {
  548. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  549. int i, rc;
  550. /* Take three shots at resetting the bus */
  551. for (i=0; i<3; i++) {
  552. eeh_reset_pe_once(pe);
  553. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  554. if ((rc & flags) == flags)
  555. return 0;
  556. if (rc < 0) {
  557. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  558. __func__, pe->phb->global_number, pe->addr);
  559. return -1;
  560. }
  561. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  562. i+1, pe->phb->global_number, pe->addr, rc);
  563. }
  564. return -1;
  565. }
  566. /**
  567. * eeh_save_bars - Save device bars
  568. * @edev: PCI device associated EEH device
  569. *
  570. * Save the values of the device bars. Unlike the restore
  571. * routine, this routine is *not* recursive. This is because
  572. * PCI devices are added individually; but, for the restore,
  573. * an entire slot is reset at a time.
  574. */
  575. void eeh_save_bars(struct eeh_dev *edev)
  576. {
  577. int i;
  578. struct device_node *dn;
  579. if (!edev)
  580. return;
  581. dn = eeh_dev_to_of_node(edev);
  582. for (i = 0; i < 16; i++)
  583. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  584. }
  585. /**
  586. * eeh_ops_register - Register platform dependent EEH operations
  587. * @ops: platform dependent EEH operations
  588. *
  589. * Register the platform dependent EEH operation callback
  590. * functions. The platform should call this function before
  591. * any other EEH operations.
  592. */
  593. int __init eeh_ops_register(struct eeh_ops *ops)
  594. {
  595. if (!ops->name) {
  596. pr_warning("%s: Invalid EEH ops name for %p\n",
  597. __func__, ops);
  598. return -EINVAL;
  599. }
  600. if (eeh_ops && eeh_ops != ops) {
  601. pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
  602. __func__, eeh_ops->name, ops->name);
  603. return -EEXIST;
  604. }
  605. eeh_ops = ops;
  606. return 0;
  607. }
  608. /**
  609. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  610. * @name: name of EEH platform operations
  611. *
  612. * Unregister the platform dependent EEH operation callback
  613. * functions.
  614. */
  615. int __exit eeh_ops_unregister(const char *name)
  616. {
  617. if (!name || !strlen(name)) {
  618. pr_warning("%s: Invalid EEH ops name\n",
  619. __func__);
  620. return -EINVAL;
  621. }
  622. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  623. eeh_ops = NULL;
  624. return 0;
  625. }
  626. return -EEXIST;
  627. }
  628. /**
  629. * eeh_init - EEH initialization
  630. *
  631. * Initialize EEH by trying to enable it for all of the adapters in the system.
  632. * As a side effect we can determine here if eeh is supported at all.
  633. * Note that we leave EEH on so failed config cycles won't cause a machine
  634. * check. If a user turns off EEH for a particular adapter they are really
  635. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  636. * grant access to a slot if EEH isn't enabled, and so we always enable
  637. * EEH for all slots/all devices.
  638. *
  639. * The eeh-force-off option disables EEH checking globally, for all slots.
  640. * Even if force-off is set, the EEH hardware is still enabled, so that
  641. * newer systems can boot.
  642. */
  643. int __init eeh_init(void)
  644. {
  645. struct pci_controller *hose, *tmp;
  646. struct device_node *phb;
  647. static int cnt = 0;
  648. int ret = 0;
  649. /*
  650. * We have to delay the initialization on PowerNV after
  651. * the PCI hierarchy tree has been built because the PEs
  652. * are figured out based on PCI devices instead of device
  653. * tree nodes
  654. */
  655. if (machine_is(powernv) && cnt++ <= 0)
  656. return ret;
  657. /* call platform initialization function */
  658. if (!eeh_ops) {
  659. pr_warning("%s: Platform EEH operation not found\n",
  660. __func__);
  661. return -EEXIST;
  662. } else if ((ret = eeh_ops->init())) {
  663. pr_warning("%s: Failed to call platform init function (%d)\n",
  664. __func__, ret);
  665. return ret;
  666. }
  667. /* Initialize EEH event */
  668. ret = eeh_event_init();
  669. if (ret)
  670. return ret;
  671. /* Enable EEH for all adapters */
  672. if (eeh_probe_mode_devtree()) {
  673. list_for_each_entry_safe(hose, tmp,
  674. &hose_list, list_node) {
  675. phb = hose->dn;
  676. traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
  677. }
  678. } else if (eeh_probe_mode_dev()) {
  679. list_for_each_entry_safe(hose, tmp,
  680. &hose_list, list_node)
  681. pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
  682. } else {
  683. pr_warning("%s: Invalid probe mode %d\n",
  684. __func__, eeh_probe_mode);
  685. return -EINVAL;
  686. }
  687. /*
  688. * Call platform post-initialization. Actually, It's good chance
  689. * to inform platform that EEH is ready to supply service if the
  690. * I/O cache stuff has been built up.
  691. */
  692. if (eeh_ops->post_init) {
  693. ret = eeh_ops->post_init();
  694. if (ret)
  695. return ret;
  696. }
  697. if (eeh_subsystem_enabled)
  698. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  699. else
  700. pr_warning("EEH: No capable adapters found\n");
  701. return ret;
  702. }
  703. core_initcall_sync(eeh_init);
  704. /**
  705. * eeh_add_device_early - Enable EEH for the indicated device_node
  706. * @dn: device node for which to set up EEH
  707. *
  708. * This routine must be used to perform EEH initialization for PCI
  709. * devices that were added after system boot (e.g. hotplug, dlpar).
  710. * This routine must be called before any i/o is performed to the
  711. * adapter (inluding any config-space i/o).
  712. * Whether this actually enables EEH or not for this device depends
  713. * on the CEC architecture, type of the device, on earlier boot
  714. * command-line arguments & etc.
  715. */
  716. static void eeh_add_device_early(struct device_node *dn)
  717. {
  718. struct pci_controller *phb;
  719. /*
  720. * If we're doing EEH probe based on PCI device, we
  721. * would delay the probe until late stage because
  722. * the PCI device isn't available this moment.
  723. */
  724. if (!eeh_probe_mode_devtree())
  725. return;
  726. if (!of_node_to_eeh_dev(dn))
  727. return;
  728. phb = of_node_to_eeh_dev(dn)->phb;
  729. /* USB Bus children of PCI devices will not have BUID's */
  730. if (NULL == phb || 0 == phb->buid)
  731. return;
  732. eeh_ops->of_probe(dn, NULL);
  733. }
  734. /**
  735. * eeh_add_device_tree_early - Enable EEH for the indicated device
  736. * @dn: device node
  737. *
  738. * This routine must be used to perform EEH initialization for the
  739. * indicated PCI device that was added after system boot (e.g.
  740. * hotplug, dlpar).
  741. */
  742. void eeh_add_device_tree_early(struct device_node *dn)
  743. {
  744. struct device_node *sib;
  745. for_each_child_of_node(dn, sib)
  746. eeh_add_device_tree_early(sib);
  747. eeh_add_device_early(dn);
  748. }
  749. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  750. /**
  751. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  752. * @dev: pci device for which to set up EEH
  753. *
  754. * This routine must be used to complete EEH initialization for PCI
  755. * devices that were added after system boot (e.g. hotplug, dlpar).
  756. */
  757. static void eeh_add_device_late(struct pci_dev *dev)
  758. {
  759. struct device_node *dn;
  760. struct eeh_dev *edev;
  761. if (!dev || !eeh_subsystem_enabled)
  762. return;
  763. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  764. dn = pci_device_to_OF_node(dev);
  765. edev = of_node_to_eeh_dev(dn);
  766. if (edev->pdev == dev) {
  767. pr_debug("EEH: Already referenced !\n");
  768. return;
  769. }
  770. WARN_ON(edev->pdev);
  771. pci_dev_get(dev);
  772. edev->pdev = dev;
  773. dev->dev.archdata.edev = edev;
  774. /*
  775. * We have to do the EEH probe here because the PCI device
  776. * hasn't been created yet in the early stage.
  777. */
  778. if (eeh_probe_mode_dev())
  779. eeh_ops->dev_probe(dev, NULL);
  780. eeh_addr_cache_insert_dev(dev);
  781. }
  782. /**
  783. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  784. * @bus: PCI bus
  785. *
  786. * This routine must be used to perform EEH initialization for PCI
  787. * devices which are attached to the indicated PCI bus. The PCI bus
  788. * is added after system boot through hotplug or dlpar.
  789. */
  790. void eeh_add_device_tree_late(struct pci_bus *bus)
  791. {
  792. struct pci_dev *dev;
  793. list_for_each_entry(dev, &bus->devices, bus_list) {
  794. eeh_add_device_late(dev);
  795. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  796. struct pci_bus *subbus = dev->subordinate;
  797. if (subbus)
  798. eeh_add_device_tree_late(subbus);
  799. }
  800. }
  801. }
  802. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  803. /**
  804. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  805. * @bus: PCI bus
  806. *
  807. * This routine must be used to add EEH sysfs files for PCI
  808. * devices which are attached to the indicated PCI bus. The PCI bus
  809. * is added after system boot through hotplug or dlpar.
  810. */
  811. void eeh_add_sysfs_files(struct pci_bus *bus)
  812. {
  813. struct pci_dev *dev;
  814. list_for_each_entry(dev, &bus->devices, bus_list) {
  815. eeh_sysfs_add_device(dev);
  816. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  817. struct pci_bus *subbus = dev->subordinate;
  818. if (subbus)
  819. eeh_add_sysfs_files(subbus);
  820. }
  821. }
  822. }
  823. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  824. /**
  825. * eeh_remove_device - Undo EEH setup for the indicated pci device
  826. * @dev: pci device to be removed
  827. * @purge_pe: remove the PE or not
  828. *
  829. * This routine should be called when a device is removed from
  830. * a running system (e.g. by hotplug or dlpar). It unregisters
  831. * the PCI device from the EEH subsystem. I/O errors affecting
  832. * this device will no longer be detected after this call; thus,
  833. * i/o errors affecting this slot may leave this device unusable.
  834. */
  835. static void eeh_remove_device(struct pci_dev *dev, int purge_pe)
  836. {
  837. struct eeh_dev *edev;
  838. if (!dev || !eeh_subsystem_enabled)
  839. return;
  840. edev = pci_dev_to_eeh_dev(dev);
  841. /* Unregister the device with the EEH/PCI address search system */
  842. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  843. if (!edev || !edev->pdev) {
  844. pr_debug("EEH: Not referenced !\n");
  845. return;
  846. }
  847. edev->pdev = NULL;
  848. dev->dev.archdata.edev = NULL;
  849. pci_dev_put(dev);
  850. eeh_rmv_from_parent_pe(edev, purge_pe);
  851. eeh_addr_cache_rmv_dev(dev);
  852. eeh_sysfs_remove_device(dev);
  853. }
  854. /**
  855. * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
  856. * @dev: PCI device
  857. * @purge_pe: remove the corresponding PE or not
  858. *
  859. * This routine must be called when a device is removed from the
  860. * running system through hotplug or dlpar. The corresponding
  861. * PCI address cache will be removed.
  862. */
  863. void eeh_remove_bus_device(struct pci_dev *dev, int purge_pe)
  864. {
  865. struct pci_bus *bus = dev->subordinate;
  866. struct pci_dev *child, *tmp;
  867. eeh_remove_device(dev, purge_pe);
  868. if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  869. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  870. eeh_remove_bus_device(child, purge_pe);
  871. }
  872. }
  873. EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
  874. static int proc_eeh_show(struct seq_file *m, void *v)
  875. {
  876. if (0 == eeh_subsystem_enabled) {
  877. seq_printf(m, "EEH Subsystem is globally disabled\n");
  878. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  879. } else {
  880. seq_printf(m, "EEH Subsystem is enabled\n");
  881. seq_printf(m,
  882. "no device=%llu\n"
  883. "no device node=%llu\n"
  884. "no config address=%llu\n"
  885. "check not wanted=%llu\n"
  886. "eeh_total_mmio_ffs=%llu\n"
  887. "eeh_false_positives=%llu\n"
  888. "eeh_slot_resets=%llu\n",
  889. eeh_stats.no_device,
  890. eeh_stats.no_dn,
  891. eeh_stats.no_cfg_addr,
  892. eeh_stats.ignored_check,
  893. eeh_stats.total_mmio_ffs,
  894. eeh_stats.false_positives,
  895. eeh_stats.slot_resets);
  896. }
  897. return 0;
  898. }
  899. static int proc_eeh_open(struct inode *inode, struct file *file)
  900. {
  901. return single_open(file, proc_eeh_show, NULL);
  902. }
  903. static const struct file_operations proc_eeh_operations = {
  904. .open = proc_eeh_open,
  905. .read = seq_read,
  906. .llseek = seq_lseek,
  907. .release = single_release,
  908. };
  909. static int __init eeh_init_proc(void)
  910. {
  911. if (machine_is(pseries))
  912. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  913. return 0;
  914. }
  915. __initcall(eeh_init_proc);