eeh.c 29 KB

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