eeh.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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/debugfs.h>
  25. #include <linux/sched.h>
  26. #include <linux/init.h>
  27. #include <linux/list.h>
  28. #include <linux/pci.h>
  29. #include <linux/iommu.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/rbtree.h>
  32. #include <linux/reboot.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/export.h>
  36. #include <linux/of.h>
  37. #include <linux/atomic.h>
  38. #include <asm/debug.h>
  39. #include <asm/eeh.h>
  40. #include <asm/eeh_event.h>
  41. #include <asm/io.h>
  42. #include <asm/iommu.h>
  43. #include <asm/machdep.h>
  44. #include <asm/ppc-pci.h>
  45. #include <asm/rtas.h>
  46. /** Overview:
  47. * EEH, or "Extended Error Handling" is a PCI bridge technology for
  48. * dealing with PCI bus errors that can't be dealt with within the
  49. * usual PCI framework, except by check-stopping the CPU. Systems
  50. * that are designed for high-availability/reliability cannot afford
  51. * to crash due to a "mere" PCI error, thus the need for EEH.
  52. * An EEH-capable bridge operates by converting a detected error
  53. * into a "slot freeze", taking the PCI adapter off-line, making
  54. * the slot behave, from the OS'es point of view, as if the slot
  55. * were "empty": all reads return 0xff's and all writes are silently
  56. * ignored. EEH slot isolation events can be triggered by parity
  57. * errors on the address or data busses (e.g. during posted writes),
  58. * which in turn might be caused by low voltage on the bus, dust,
  59. * vibration, humidity, radioactivity or plain-old failed hardware.
  60. *
  61. * Note, however, that one of the leading causes of EEH slot
  62. * freeze events are buggy device drivers, buggy device microcode,
  63. * or buggy device hardware. This is because any attempt by the
  64. * device to bus-master data to a memory address that is not
  65. * assigned to the device will trigger a slot freeze. (The idea
  66. * is to prevent devices-gone-wild from corrupting system memory).
  67. * Buggy hardware/drivers will have a miserable time co-existing
  68. * with EEH.
  69. *
  70. * Ideally, a PCI device driver, when suspecting that an isolation
  71. * event has occurred (e.g. by reading 0xff's), will then ask EEH
  72. * whether this is the case, and then take appropriate steps to
  73. * reset the PCI slot, the PCI device, and then resume operations.
  74. * However, until that day, the checking is done here, with the
  75. * eeh_check_failure() routine embedded in the MMIO macros. If
  76. * the slot is found to be isolated, an "EEH Event" is synthesized
  77. * and sent out for processing.
  78. */
  79. /* If a device driver keeps reading an MMIO register in an interrupt
  80. * handler after a slot isolation event, it might be broken.
  81. * This sets the threshold for how many read attempts we allow
  82. * before printing an error message.
  83. */
  84. #define EEH_MAX_FAILS 2100000
  85. /* Time to wait for a PCI slot to report status, in milliseconds */
  86. #define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
  87. /*
  88. * EEH probe mode support, which is part of the flags,
  89. * is to support multiple platforms for EEH. Some platforms
  90. * like pSeries do PCI emunation based on device tree.
  91. * However, other platforms like powernv probe PCI devices
  92. * from hardware. The flag is used to distinguish that.
  93. * In addition, struct eeh_ops::probe would be invoked for
  94. * particular OF node or PCI device so that the corresponding
  95. * PE would be created there.
  96. */
  97. int eeh_subsystem_flags;
  98. EXPORT_SYMBOL(eeh_subsystem_flags);
  99. /* Platform dependent EEH operations */
  100. struct eeh_ops *eeh_ops = NULL;
  101. /* Lock to avoid races due to multiple reports of an error */
  102. DEFINE_RAW_SPINLOCK(confirm_error_lock);
  103. /* Lock to protect passed flags */
  104. static DEFINE_MUTEX(eeh_dev_mutex);
  105. /* Buffer for reporting pci register dumps. Its here in BSS, and
  106. * not dynamically alloced, so that it ends up in RMO where RTAS
  107. * can access it.
  108. */
  109. #define EEH_PCI_REGS_LOG_LEN 8192
  110. static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
  111. /*
  112. * The struct is used to maintain the EEH global statistic
  113. * information. Besides, the EEH global statistics will be
  114. * exported to user space through procfs
  115. */
  116. struct eeh_stats {
  117. u64 no_device; /* PCI device not found */
  118. u64 no_dn; /* OF node not found */
  119. u64 no_cfg_addr; /* Config address not found */
  120. u64 ignored_check; /* EEH check skipped */
  121. u64 total_mmio_ffs; /* Total EEH checks */
  122. u64 false_positives; /* Unnecessary EEH checks */
  123. u64 slot_resets; /* PE reset */
  124. };
  125. static struct eeh_stats eeh_stats;
  126. #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
  127. static int __init eeh_setup(char *str)
  128. {
  129. if (!strcmp(str, "off"))
  130. eeh_add_flag(EEH_FORCE_DISABLED);
  131. return 1;
  132. }
  133. __setup("eeh=", eeh_setup);
  134. /*
  135. * This routine captures assorted PCI configuration space data
  136. * for the indicated PCI device, and puts them into a buffer
  137. * for RTAS error logging.
  138. */
  139. static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
  140. {
  141. struct device_node *dn = eeh_dev_to_of_node(edev);
  142. u32 cfg;
  143. int cap, i;
  144. int n = 0, l = 0;
  145. char buffer[128];
  146. n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
  147. pr_warn("EEH: of node=%s\n", dn->full_name);
  148. eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
  149. n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
  150. pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
  151. eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
  152. n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
  153. pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
  154. /* Gather bridge-specific registers */
  155. if (edev->mode & EEH_DEV_BRIDGE) {
  156. eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
  157. n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
  158. pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
  159. eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
  160. n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
  161. pr_warn("EEH: Bridge control: %04x\n", cfg);
  162. }
  163. /* Dump out the PCI-X command and status regs */
  164. cap = edev->pcix_cap;
  165. if (cap) {
  166. eeh_ops->read_config(dn, cap, 4, &cfg);
  167. n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
  168. pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
  169. eeh_ops->read_config(dn, cap+4, 4, &cfg);
  170. n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
  171. pr_warn("EEH: PCI-X status: %08x\n", cfg);
  172. }
  173. /* If PCI-E capable, dump PCI-E cap 10 */
  174. cap = edev->pcie_cap;
  175. if (cap) {
  176. n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
  177. pr_warn("EEH: PCI-E capabilities and status follow:\n");
  178. for (i=0; i<=8; i++) {
  179. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  180. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  181. if ((i % 4) == 0) {
  182. if (i != 0)
  183. pr_warn("%s\n", buffer);
  184. l = scnprintf(buffer, sizeof(buffer),
  185. "EEH: PCI-E %02x: %08x ",
  186. 4*i, cfg);
  187. } else {
  188. l += scnprintf(buffer+l, sizeof(buffer)-l,
  189. "%08x ", cfg);
  190. }
  191. }
  192. pr_warn("%s\n", buffer);
  193. }
  194. /* If AER capable, dump it */
  195. cap = edev->aer_cap;
  196. if (cap) {
  197. n += scnprintf(buf+n, len-n, "pci-e AER:\n");
  198. pr_warn("EEH: PCI-E AER capability register set follows:\n");
  199. for (i=0; i<=13; i++) {
  200. eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
  201. n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
  202. if ((i % 4) == 0) {
  203. if (i != 0)
  204. pr_warn("%s\n", buffer);
  205. l = scnprintf(buffer, sizeof(buffer),
  206. "EEH: PCI-E AER %02x: %08x ",
  207. 4*i, cfg);
  208. } else {
  209. l += scnprintf(buffer+l, sizeof(buffer)-l,
  210. "%08x ", cfg);
  211. }
  212. }
  213. pr_warn("%s\n", buffer);
  214. }
  215. return n;
  216. }
  217. static void *eeh_dump_pe_log(void *data, void *flag)
  218. {
  219. struct eeh_pe *pe = data;
  220. struct eeh_dev *edev, *tmp;
  221. size_t *plen = flag;
  222. eeh_pe_for_each_dev(pe, edev, tmp)
  223. *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
  224. EEH_PCI_REGS_LOG_LEN - *plen);
  225. return NULL;
  226. }
  227. /**
  228. * eeh_slot_error_detail - Generate combined log including driver log and error log
  229. * @pe: EEH PE
  230. * @severity: temporary or permanent error log
  231. *
  232. * This routine should be called to generate the combined log, which
  233. * is comprised of driver log and error log. The driver log is figured
  234. * out from the config space of the corresponding PCI device, while
  235. * the error log is fetched through platform dependent function call.
  236. */
  237. void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
  238. {
  239. size_t loglen = 0;
  240. /*
  241. * When the PHB is fenced or dead, it's pointless to collect
  242. * the data from PCI config space because it should return
  243. * 0xFF's. For ER, we still retrieve the data from the PCI
  244. * config space.
  245. *
  246. * For pHyp, we have to enable IO for log retrieval. Otherwise,
  247. * 0xFF's is always returned from PCI config space.
  248. */
  249. if (!(pe->type & EEH_PE_PHB)) {
  250. if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
  251. eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  252. eeh_ops->configure_bridge(pe);
  253. eeh_pe_restore_bars(pe);
  254. pci_regs_buf[0] = 0;
  255. eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
  256. }
  257. eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
  258. }
  259. /**
  260. * eeh_token_to_phys - Convert EEH address token to phys address
  261. * @token: I/O token, should be address in the form 0xA....
  262. *
  263. * This routine should be called to convert virtual I/O address
  264. * to physical one.
  265. */
  266. static inline unsigned long eeh_token_to_phys(unsigned long token)
  267. {
  268. pte_t *ptep;
  269. unsigned long pa;
  270. int hugepage_shift;
  271. /*
  272. * We won't find hugepages here, iomem
  273. */
  274. ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
  275. if (!ptep)
  276. return token;
  277. WARN_ON(hugepage_shift);
  278. pa = pte_pfn(*ptep) << PAGE_SHIFT;
  279. return pa | (token & (PAGE_SIZE-1));
  280. }
  281. /*
  282. * On PowerNV platform, we might already have fenced PHB there.
  283. * For that case, it's meaningless to recover frozen PE. Intead,
  284. * We have to handle fenced PHB firstly.
  285. */
  286. static int eeh_phb_check_failure(struct eeh_pe *pe)
  287. {
  288. struct eeh_pe *phb_pe;
  289. unsigned long flags;
  290. int ret;
  291. if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
  292. return -EPERM;
  293. /* Find the PHB PE */
  294. phb_pe = eeh_phb_pe_get(pe->phb);
  295. if (!phb_pe) {
  296. pr_warn("%s Can't find PE for PHB#%d\n",
  297. __func__, pe->phb->global_number);
  298. return -EEXIST;
  299. }
  300. /* If the PHB has been in problematic state */
  301. eeh_serialize_lock(&flags);
  302. if (phb_pe->state & EEH_PE_ISOLATED) {
  303. ret = 0;
  304. goto out;
  305. }
  306. /* Check PHB state */
  307. ret = eeh_ops->get_state(phb_pe, NULL);
  308. if ((ret < 0) ||
  309. (ret == EEH_STATE_NOT_SUPPORT) ||
  310. (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
  311. (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
  312. ret = 0;
  313. goto out;
  314. }
  315. /* Isolate the PHB and send event */
  316. eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
  317. eeh_serialize_unlock(flags);
  318. pr_err("EEH: PHB#%x failure detected, location: %s\n",
  319. phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
  320. dump_stack();
  321. eeh_send_failure_event(phb_pe);
  322. return 1;
  323. out:
  324. eeh_serialize_unlock(flags);
  325. return ret;
  326. }
  327. /**
  328. * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
  329. * @edev: eeh device
  330. *
  331. * Check for an EEH failure for the given device node. Call this
  332. * routine if the result of a read was all 0xff's and you want to
  333. * find out if this is due to an EEH slot freeze. This routine
  334. * will query firmware for the EEH status.
  335. *
  336. * Returns 0 if there has not been an EEH error; otherwise returns
  337. * a non-zero value and queues up a slot isolation event notification.
  338. *
  339. * It is safe to call this routine in an interrupt context.
  340. */
  341. int eeh_dev_check_failure(struct eeh_dev *edev)
  342. {
  343. int ret;
  344. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  345. unsigned long flags;
  346. struct device_node *dn;
  347. struct pci_dev *dev;
  348. struct eeh_pe *pe, *parent_pe, *phb_pe;
  349. int rc = 0;
  350. const char *location;
  351. eeh_stats.total_mmio_ffs++;
  352. if (!eeh_enabled())
  353. return 0;
  354. if (!edev) {
  355. eeh_stats.no_dn++;
  356. return 0;
  357. }
  358. dn = eeh_dev_to_of_node(edev);
  359. dev = eeh_dev_to_pci_dev(edev);
  360. pe = eeh_dev_to_pe(edev);
  361. /* Access to IO BARs might get this far and still not want checking. */
  362. if (!pe) {
  363. eeh_stats.ignored_check++;
  364. pr_debug("EEH: Ignored check for %s %s\n",
  365. eeh_pci_name(dev), dn->full_name);
  366. return 0;
  367. }
  368. if (!pe->addr && !pe->config_addr) {
  369. eeh_stats.no_cfg_addr++;
  370. return 0;
  371. }
  372. /*
  373. * On PowerNV platform, we might already have fenced PHB
  374. * there and we need take care of that firstly.
  375. */
  376. ret = eeh_phb_check_failure(pe);
  377. if (ret > 0)
  378. return ret;
  379. /*
  380. * If the PE isn't owned by us, we shouldn't check the
  381. * state. Instead, let the owner handle it if the PE has
  382. * been frozen.
  383. */
  384. if (eeh_pe_passed(pe))
  385. return 0;
  386. /* If we already have a pending isolation event for this
  387. * slot, we know it's bad already, we don't need to check.
  388. * Do this checking under a lock; as multiple PCI devices
  389. * in one slot might report errors simultaneously, and we
  390. * only want one error recovery routine running.
  391. */
  392. eeh_serialize_lock(&flags);
  393. rc = 1;
  394. if (pe->state & EEH_PE_ISOLATED) {
  395. pe->check_count++;
  396. if (pe->check_count % EEH_MAX_FAILS == 0) {
  397. location = of_get_property(dn, "ibm,loc-code", NULL);
  398. printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
  399. "location=%s driver=%s pci addr=%s\n",
  400. pe->check_count, location,
  401. eeh_driver_name(dev), eeh_pci_name(dev));
  402. printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
  403. eeh_driver_name(dev));
  404. dump_stack();
  405. }
  406. goto dn_unlock;
  407. }
  408. /*
  409. * Now test for an EEH failure. This is VERY expensive.
  410. * Note that the eeh_config_addr may be a parent device
  411. * in the case of a device behind a bridge, or it may be
  412. * function zero of a multi-function device.
  413. * In any case they must share a common PHB.
  414. */
  415. ret = eeh_ops->get_state(pe, NULL);
  416. /* Note that config-io to empty slots may fail;
  417. * they are empty when they don't have children.
  418. * We will punt with the following conditions: Failure to get
  419. * PE's state, EEH not support and Permanently unavailable
  420. * state, PE is in good state.
  421. */
  422. if ((ret < 0) ||
  423. (ret == EEH_STATE_NOT_SUPPORT) ||
  424. ((ret & active_flags) == active_flags)) {
  425. eeh_stats.false_positives++;
  426. pe->false_positives++;
  427. rc = 0;
  428. goto dn_unlock;
  429. }
  430. /*
  431. * It should be corner case that the parent PE has been
  432. * put into frozen state as well. We should take care
  433. * that at first.
  434. */
  435. parent_pe = pe->parent;
  436. while (parent_pe) {
  437. /* Hit the ceiling ? */
  438. if (parent_pe->type & EEH_PE_PHB)
  439. break;
  440. /* Frozen parent PE ? */
  441. ret = eeh_ops->get_state(parent_pe, NULL);
  442. if (ret > 0 &&
  443. (ret & active_flags) != active_flags)
  444. pe = parent_pe;
  445. /* Next parent level */
  446. parent_pe = parent_pe->parent;
  447. }
  448. eeh_stats.slot_resets++;
  449. /* Avoid repeated reports of this failure, including problems
  450. * with other functions on this device, and functions under
  451. * bridges.
  452. */
  453. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  454. eeh_serialize_unlock(flags);
  455. /* Most EEH events are due to device driver bugs. Having
  456. * a stack trace will help the device-driver authors figure
  457. * out what happened. So print that out.
  458. */
  459. phb_pe = eeh_phb_pe_get(pe->phb);
  460. pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
  461. pe->phb->global_number, pe->addr);
  462. pr_err("EEH: PE location: %s, PHB location: %s\n",
  463. eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
  464. dump_stack();
  465. eeh_send_failure_event(pe);
  466. return 1;
  467. dn_unlock:
  468. eeh_serialize_unlock(flags);
  469. return rc;
  470. }
  471. EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
  472. /**
  473. * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
  474. * @token: I/O address
  475. *
  476. * Check for an EEH failure at the given I/O address. Call this
  477. * routine if the result of a read was all 0xff's and you want to
  478. * find out if this is due to an EEH slot freeze event. This routine
  479. * will query firmware for the EEH status.
  480. *
  481. * Note this routine is safe to call in an interrupt context.
  482. */
  483. int eeh_check_failure(const volatile void __iomem *token)
  484. {
  485. unsigned long addr;
  486. struct eeh_dev *edev;
  487. /* Finding the phys addr + pci device; this is pretty quick. */
  488. addr = eeh_token_to_phys((unsigned long __force) token);
  489. edev = eeh_addr_cache_get_dev(addr);
  490. if (!edev) {
  491. eeh_stats.no_device++;
  492. return 0;
  493. }
  494. return eeh_dev_check_failure(edev);
  495. }
  496. EXPORT_SYMBOL(eeh_check_failure);
  497. /**
  498. * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
  499. * @pe: EEH PE
  500. *
  501. * This routine should be called to reenable frozen MMIO or DMA
  502. * so that it would work correctly again. It's useful while doing
  503. * recovery or log collection on the indicated device.
  504. */
  505. int eeh_pci_enable(struct eeh_pe *pe, int function)
  506. {
  507. int active_flag, rc;
  508. /*
  509. * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
  510. * Also, it's pointless to enable them on unfrozen PE. So
  511. * we have to check before enabling IO or DMA.
  512. */
  513. switch (function) {
  514. case EEH_OPT_THAW_MMIO:
  515. active_flag = EEH_STATE_MMIO_ACTIVE;
  516. break;
  517. case EEH_OPT_THAW_DMA:
  518. active_flag = EEH_STATE_DMA_ACTIVE;
  519. break;
  520. case EEH_OPT_DISABLE:
  521. case EEH_OPT_ENABLE:
  522. case EEH_OPT_FREEZE_PE:
  523. active_flag = 0;
  524. break;
  525. default:
  526. pr_warn("%s: Invalid function %d\n",
  527. __func__, function);
  528. return -EINVAL;
  529. }
  530. /*
  531. * Check if IO or DMA has been enabled before
  532. * enabling them.
  533. */
  534. if (active_flag) {
  535. rc = eeh_ops->get_state(pe, NULL);
  536. if (rc < 0)
  537. return rc;
  538. /* Needn't enable it at all */
  539. if (rc == EEH_STATE_NOT_SUPPORT)
  540. return 0;
  541. /* It's already enabled */
  542. if (rc & active_flag)
  543. return 0;
  544. }
  545. /* Issue the request */
  546. rc = eeh_ops->set_option(pe, function);
  547. if (rc)
  548. pr_warn("%s: Unexpected state change %d on "
  549. "PHB#%d-PE#%x, err=%d\n",
  550. __func__, function, pe->phb->global_number,
  551. pe->addr, rc);
  552. /* Check if the request is finished successfully */
  553. if (active_flag) {
  554. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  555. if (rc <= 0)
  556. return rc;
  557. if (rc & active_flag)
  558. return 0;
  559. return -EIO;
  560. }
  561. return rc;
  562. }
  563. /**
  564. * pcibios_set_pcie_slot_reset - Set PCI-E reset state
  565. * @dev: pci device struct
  566. * @state: reset state to enter
  567. *
  568. * Return value:
  569. * 0 if success
  570. */
  571. int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
  572. {
  573. struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
  574. struct eeh_pe *pe = eeh_dev_to_pe(edev);
  575. if (!pe) {
  576. pr_err("%s: No PE found on PCI device %s\n",
  577. __func__, pci_name(dev));
  578. return -EINVAL;
  579. }
  580. switch (state) {
  581. case pcie_deassert_reset:
  582. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  583. eeh_pe_state_clear(pe, EEH_PE_RESET);
  584. break;
  585. case pcie_hot_reset:
  586. eeh_pe_state_mark(pe, EEH_PE_RESET);
  587. eeh_ops->reset(pe, EEH_RESET_HOT);
  588. break;
  589. case pcie_warm_reset:
  590. eeh_pe_state_mark(pe, EEH_PE_RESET);
  591. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  592. break;
  593. default:
  594. eeh_pe_state_clear(pe, EEH_PE_RESET);
  595. return -EINVAL;
  596. };
  597. return 0;
  598. }
  599. /**
  600. * eeh_set_pe_freset - Check the required reset for the indicated device
  601. * @data: EEH device
  602. * @flag: return value
  603. *
  604. * Each device might have its preferred reset type: fundamental or
  605. * hot reset. The routine is used to collected the information for
  606. * the indicated device and its children so that the bunch of the
  607. * devices could be reset properly.
  608. */
  609. static void *eeh_set_dev_freset(void *data, void *flag)
  610. {
  611. struct pci_dev *dev;
  612. unsigned int *freset = (unsigned int *)flag;
  613. struct eeh_dev *edev = (struct eeh_dev *)data;
  614. dev = eeh_dev_to_pci_dev(edev);
  615. if (dev)
  616. *freset |= dev->needs_freset;
  617. return NULL;
  618. }
  619. /**
  620. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  621. * @pe: EEH PE
  622. *
  623. * Assert the PCI #RST line for 1/4 second.
  624. */
  625. static void eeh_reset_pe_once(struct eeh_pe *pe)
  626. {
  627. unsigned int freset = 0;
  628. /* Determine type of EEH reset required for
  629. * Partitionable Endpoint, a hot-reset (1)
  630. * or a fundamental reset (3).
  631. * A fundamental reset required by any device under
  632. * Partitionable Endpoint trumps hot-reset.
  633. */
  634. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  635. if (freset)
  636. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  637. else
  638. eeh_ops->reset(pe, EEH_RESET_HOT);
  639. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  640. }
  641. /**
  642. * eeh_reset_pe - Reset the indicated PE
  643. * @pe: EEH PE
  644. *
  645. * This routine should be called to reset indicated device, including
  646. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  647. * might be involved as well.
  648. */
  649. int eeh_reset_pe(struct eeh_pe *pe)
  650. {
  651. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  652. int i, rc;
  653. /* Take three shots at resetting the bus */
  654. for (i=0; i<3; i++) {
  655. eeh_reset_pe_once(pe);
  656. /*
  657. * EEH_PE_ISOLATED is expected to be removed after
  658. * BAR restore.
  659. */
  660. rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  661. if ((rc & flags) == flags)
  662. return 0;
  663. if (rc < 0) {
  664. pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  665. __func__, pe->phb->global_number, pe->addr);
  666. return -1;
  667. }
  668. pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
  669. i+1, pe->phb->global_number, pe->addr, rc);
  670. }
  671. return -1;
  672. }
  673. /**
  674. * eeh_save_bars - Save device bars
  675. * @edev: PCI device associated EEH device
  676. *
  677. * Save the values of the device bars. Unlike the restore
  678. * routine, this routine is *not* recursive. This is because
  679. * PCI devices are added individually; but, for the restore,
  680. * an entire slot is reset at a time.
  681. */
  682. void eeh_save_bars(struct eeh_dev *edev)
  683. {
  684. int i;
  685. struct device_node *dn;
  686. if (!edev)
  687. return;
  688. dn = eeh_dev_to_of_node(edev);
  689. for (i = 0; i < 16; i++)
  690. eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
  691. /*
  692. * For PCI bridges including root port, we need enable bus
  693. * master explicitly. Otherwise, it can't fetch IODA table
  694. * entries correctly. So we cache the bit in advance so that
  695. * we can restore it after reset, either PHB range or PE range.
  696. */
  697. if (edev->mode & EEH_DEV_BRIDGE)
  698. edev->config_space[1] |= PCI_COMMAND_MASTER;
  699. }
  700. /**
  701. * eeh_ops_register - Register platform dependent EEH operations
  702. * @ops: platform dependent EEH operations
  703. *
  704. * Register the platform dependent EEH operation callback
  705. * functions. The platform should call this function before
  706. * any other EEH operations.
  707. */
  708. int __init eeh_ops_register(struct eeh_ops *ops)
  709. {
  710. if (!ops->name) {
  711. pr_warn("%s: Invalid EEH ops name for %p\n",
  712. __func__, ops);
  713. return -EINVAL;
  714. }
  715. if (eeh_ops && eeh_ops != ops) {
  716. pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
  717. __func__, eeh_ops->name, ops->name);
  718. return -EEXIST;
  719. }
  720. eeh_ops = ops;
  721. return 0;
  722. }
  723. /**
  724. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  725. * @name: name of EEH platform operations
  726. *
  727. * Unregister the platform dependent EEH operation callback
  728. * functions.
  729. */
  730. int __exit eeh_ops_unregister(const char *name)
  731. {
  732. if (!name || !strlen(name)) {
  733. pr_warn("%s: Invalid EEH ops name\n",
  734. __func__);
  735. return -EINVAL;
  736. }
  737. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  738. eeh_ops = NULL;
  739. return 0;
  740. }
  741. return -EEXIST;
  742. }
  743. static int eeh_reboot_notifier(struct notifier_block *nb,
  744. unsigned long action, void *unused)
  745. {
  746. eeh_clear_flag(EEH_ENABLED);
  747. return NOTIFY_DONE;
  748. }
  749. static struct notifier_block eeh_reboot_nb = {
  750. .notifier_call = eeh_reboot_notifier,
  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. int eeh_init(void)
  768. {
  769. struct pci_controller *hose, *tmp;
  770. struct device_node *phb;
  771. static int cnt = 0;
  772. int ret = 0;
  773. /*
  774. * We have to delay the initialization on PowerNV after
  775. * the PCI hierarchy tree has been built because the PEs
  776. * are figured out based on PCI devices instead of device
  777. * tree nodes
  778. */
  779. if (machine_is(powernv) && cnt++ <= 0)
  780. return ret;
  781. /* Register reboot notifier */
  782. ret = register_reboot_notifier(&eeh_reboot_nb);
  783. if (ret) {
  784. pr_warn("%s: Failed to register notifier (%d)\n",
  785. __func__, ret);
  786. return ret;
  787. }
  788. /* call platform initialization function */
  789. if (!eeh_ops) {
  790. pr_warn("%s: Platform EEH operation not found\n",
  791. __func__);
  792. return -EEXIST;
  793. } else if ((ret = eeh_ops->init())) {
  794. pr_warn("%s: Failed to call platform init function (%d)\n",
  795. __func__, ret);
  796. return ret;
  797. }
  798. /* Initialize EEH event */
  799. ret = eeh_event_init();
  800. if (ret)
  801. return ret;
  802. /* Enable EEH for all adapters */
  803. if (eeh_has_flag(EEH_PROBE_MODE_DEVTREE)) {
  804. list_for_each_entry_safe(hose, tmp,
  805. &hose_list, list_node) {
  806. phb = hose->dn;
  807. traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
  808. }
  809. } else if (eeh_has_flag(EEH_PROBE_MODE_DEV)) {
  810. list_for_each_entry_safe(hose, tmp,
  811. &hose_list, list_node)
  812. pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
  813. } else {
  814. pr_warn("%s: Invalid probe mode %x",
  815. __func__, eeh_subsystem_flags);
  816. return -EINVAL;
  817. }
  818. /*
  819. * Call platform post-initialization. Actually, It's good chance
  820. * to inform platform that EEH is ready to supply service if the
  821. * I/O cache stuff has been built up.
  822. */
  823. if (eeh_ops->post_init) {
  824. ret = eeh_ops->post_init();
  825. if (ret)
  826. return ret;
  827. }
  828. if (eeh_enabled())
  829. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  830. else
  831. pr_warn("EEH: No capable adapters found\n");
  832. return ret;
  833. }
  834. core_initcall_sync(eeh_init);
  835. /**
  836. * eeh_add_device_early - Enable EEH for the indicated device_node
  837. * @dn: device node for which to set up EEH
  838. *
  839. * This routine must be used to perform EEH initialization for PCI
  840. * devices that were added after system boot (e.g. hotplug, dlpar).
  841. * This routine must be called before any i/o is performed to the
  842. * adapter (inluding any config-space i/o).
  843. * Whether this actually enables EEH or not for this device depends
  844. * on the CEC architecture, type of the device, on earlier boot
  845. * command-line arguments & etc.
  846. */
  847. void eeh_add_device_early(struct device_node *dn)
  848. {
  849. struct pci_controller *phb;
  850. /*
  851. * If we're doing EEH probe based on PCI device, we
  852. * would delay the probe until late stage because
  853. * the PCI device isn't available this moment.
  854. */
  855. if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
  856. return;
  857. if (!of_node_to_eeh_dev(dn))
  858. return;
  859. phb = of_node_to_eeh_dev(dn)->phb;
  860. /* USB Bus children of PCI devices will not have BUID's */
  861. if (NULL == phb || 0 == phb->buid)
  862. return;
  863. eeh_ops->of_probe(dn, NULL);
  864. }
  865. /**
  866. * eeh_add_device_tree_early - Enable EEH for the indicated device
  867. * @dn: device node
  868. *
  869. * This routine must be used to perform EEH initialization for the
  870. * indicated PCI device that was added after system boot (e.g.
  871. * hotplug, dlpar).
  872. */
  873. void eeh_add_device_tree_early(struct device_node *dn)
  874. {
  875. struct device_node *sib;
  876. for_each_child_of_node(dn, sib)
  877. eeh_add_device_tree_early(sib);
  878. eeh_add_device_early(dn);
  879. }
  880. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  881. /**
  882. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  883. * @dev: pci device for which to set up EEH
  884. *
  885. * This routine must be used to complete EEH initialization for PCI
  886. * devices that were added after system boot (e.g. hotplug, dlpar).
  887. */
  888. void eeh_add_device_late(struct pci_dev *dev)
  889. {
  890. struct device_node *dn;
  891. struct eeh_dev *edev;
  892. if (!dev || !eeh_enabled())
  893. return;
  894. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  895. dn = pci_device_to_OF_node(dev);
  896. edev = of_node_to_eeh_dev(dn);
  897. if (edev->pdev == dev) {
  898. pr_debug("EEH: Already referenced !\n");
  899. return;
  900. }
  901. /*
  902. * The EEH cache might not be removed correctly because of
  903. * unbalanced kref to the device during unplug time, which
  904. * relies on pcibios_release_device(). So we have to remove
  905. * that here explicitly.
  906. */
  907. if (edev->pdev) {
  908. eeh_rmv_from_parent_pe(edev);
  909. eeh_addr_cache_rmv_dev(edev->pdev);
  910. eeh_sysfs_remove_device(edev->pdev);
  911. edev->mode &= ~EEH_DEV_SYSFS;
  912. /*
  913. * We definitely should have the PCI device removed
  914. * though it wasn't correctly. So we needn't call
  915. * into error handler afterwards.
  916. */
  917. edev->mode |= EEH_DEV_NO_HANDLER;
  918. edev->pdev = NULL;
  919. dev->dev.archdata.edev = NULL;
  920. }
  921. edev->pdev = dev;
  922. dev->dev.archdata.edev = edev;
  923. /*
  924. * We have to do the EEH probe here because the PCI device
  925. * hasn't been created yet in the early stage.
  926. */
  927. if (eeh_has_flag(EEH_PROBE_MODE_DEV))
  928. eeh_ops->dev_probe(dev, NULL);
  929. eeh_addr_cache_insert_dev(dev);
  930. }
  931. /**
  932. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  933. * @bus: PCI bus
  934. *
  935. * This routine must be used to perform EEH initialization for PCI
  936. * devices which are attached to the indicated PCI bus. The PCI bus
  937. * is added after system boot through hotplug or dlpar.
  938. */
  939. void eeh_add_device_tree_late(struct pci_bus *bus)
  940. {
  941. struct pci_dev *dev;
  942. list_for_each_entry(dev, &bus->devices, bus_list) {
  943. eeh_add_device_late(dev);
  944. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  945. struct pci_bus *subbus = dev->subordinate;
  946. if (subbus)
  947. eeh_add_device_tree_late(subbus);
  948. }
  949. }
  950. }
  951. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  952. /**
  953. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  954. * @bus: PCI bus
  955. *
  956. * This routine must be used to add EEH sysfs files for PCI
  957. * devices which are attached to the indicated PCI bus. The PCI bus
  958. * is added after system boot through hotplug or dlpar.
  959. */
  960. void eeh_add_sysfs_files(struct pci_bus *bus)
  961. {
  962. struct pci_dev *dev;
  963. list_for_each_entry(dev, &bus->devices, bus_list) {
  964. eeh_sysfs_add_device(dev);
  965. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  966. struct pci_bus *subbus = dev->subordinate;
  967. if (subbus)
  968. eeh_add_sysfs_files(subbus);
  969. }
  970. }
  971. }
  972. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  973. /**
  974. * eeh_remove_device - Undo EEH setup for the indicated pci device
  975. * @dev: pci device to be removed
  976. *
  977. * This routine should be called when a device is removed from
  978. * a running system (e.g. by hotplug or dlpar). It unregisters
  979. * the PCI device from the EEH subsystem. I/O errors affecting
  980. * this device will no longer be detected after this call; thus,
  981. * i/o errors affecting this slot may leave this device unusable.
  982. */
  983. void eeh_remove_device(struct pci_dev *dev)
  984. {
  985. struct eeh_dev *edev;
  986. if (!dev || !eeh_enabled())
  987. return;
  988. edev = pci_dev_to_eeh_dev(dev);
  989. /* Unregister the device with the EEH/PCI address search system */
  990. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  991. if (!edev || !edev->pdev || !edev->pe) {
  992. pr_debug("EEH: Not referenced !\n");
  993. return;
  994. }
  995. /*
  996. * During the hotplug for EEH error recovery, we need the EEH
  997. * device attached to the parent PE in order for BAR restore
  998. * a bit later. So we keep it for BAR restore and remove it
  999. * from the parent PE during the BAR resotre.
  1000. */
  1001. edev->pdev = NULL;
  1002. dev->dev.archdata.edev = NULL;
  1003. if (!(edev->pe->state & EEH_PE_KEEP))
  1004. eeh_rmv_from_parent_pe(edev);
  1005. else
  1006. edev->mode |= EEH_DEV_DISCONNECTED;
  1007. /*
  1008. * We're removing from the PCI subsystem, that means
  1009. * the PCI device driver can't support EEH or not
  1010. * well. So we rely on hotplug completely to do recovery
  1011. * for the specific PCI device.
  1012. */
  1013. edev->mode |= EEH_DEV_NO_HANDLER;
  1014. eeh_addr_cache_rmv_dev(dev);
  1015. eeh_sysfs_remove_device(dev);
  1016. edev->mode &= ~EEH_DEV_SYSFS;
  1017. }
  1018. int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
  1019. {
  1020. int ret;
  1021. ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  1022. if (ret) {
  1023. pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
  1024. __func__, ret, pe->phb->global_number, pe->addr);
  1025. return ret;
  1026. }
  1027. ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  1028. if (ret) {
  1029. pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
  1030. __func__, ret, pe->phb->global_number, pe->addr);
  1031. return ret;
  1032. }
  1033. /* Clear software isolated state */
  1034. if (sw_state && (pe->state & EEH_PE_ISOLATED))
  1035. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  1036. return ret;
  1037. }
  1038. static struct pci_device_id eeh_reset_ids[] = {
  1039. { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
  1040. { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
  1041. { 0 }
  1042. };
  1043. static int eeh_pe_change_owner(struct eeh_pe *pe)
  1044. {
  1045. struct eeh_dev *edev, *tmp;
  1046. struct pci_dev *pdev;
  1047. struct pci_device_id *id;
  1048. int flags, ret;
  1049. /* Check PE state */
  1050. flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  1051. ret = eeh_ops->get_state(pe, NULL);
  1052. if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
  1053. return 0;
  1054. /* Unfrozen PE, nothing to do */
  1055. if ((ret & flags) == flags)
  1056. return 0;
  1057. /* Frozen PE, check if it needs PE level reset */
  1058. eeh_pe_for_each_dev(pe, edev, tmp) {
  1059. pdev = eeh_dev_to_pci_dev(edev);
  1060. if (!pdev)
  1061. continue;
  1062. for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
  1063. if (id->vendor != PCI_ANY_ID &&
  1064. id->vendor != pdev->vendor)
  1065. continue;
  1066. if (id->device != PCI_ANY_ID &&
  1067. id->device != pdev->device)
  1068. continue;
  1069. if (id->subvendor != PCI_ANY_ID &&
  1070. id->subvendor != pdev->subsystem_vendor)
  1071. continue;
  1072. if (id->subdevice != PCI_ANY_ID &&
  1073. id->subdevice != pdev->subsystem_device)
  1074. continue;
  1075. goto reset;
  1076. }
  1077. }
  1078. return eeh_unfreeze_pe(pe, true);
  1079. reset:
  1080. return eeh_pe_reset_and_recover(pe);
  1081. }
  1082. /**
  1083. * eeh_dev_open - Increase count of pass through devices for PE
  1084. * @pdev: PCI device
  1085. *
  1086. * Increase count of passed through devices for the indicated
  1087. * PE. In the result, the EEH errors detected on the PE won't be
  1088. * reported. The PE owner will be responsible for detection
  1089. * and recovery.
  1090. */
  1091. int eeh_dev_open(struct pci_dev *pdev)
  1092. {
  1093. struct eeh_dev *edev;
  1094. int ret = -ENODEV;
  1095. mutex_lock(&eeh_dev_mutex);
  1096. /* No PCI device ? */
  1097. if (!pdev)
  1098. goto out;
  1099. /* No EEH device or PE ? */
  1100. edev = pci_dev_to_eeh_dev(pdev);
  1101. if (!edev || !edev->pe)
  1102. goto out;
  1103. /*
  1104. * The PE might have been put into frozen state, but we
  1105. * didn't detect that yet. The passed through PCI devices
  1106. * in frozen PE won't work properly. Clear the frozen state
  1107. * in advance.
  1108. */
  1109. ret = eeh_pe_change_owner(edev->pe);
  1110. if (ret)
  1111. goto out;
  1112. /* Increase PE's pass through count */
  1113. atomic_inc(&edev->pe->pass_dev_cnt);
  1114. mutex_unlock(&eeh_dev_mutex);
  1115. return 0;
  1116. out:
  1117. mutex_unlock(&eeh_dev_mutex);
  1118. return ret;
  1119. }
  1120. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1121. /**
  1122. * eeh_dev_release - Decrease count of pass through devices for PE
  1123. * @pdev: PCI device
  1124. *
  1125. * Decrease count of pass through devices for the indicated PE. If
  1126. * there is no passed through device in PE, the EEH errors detected
  1127. * on the PE will be reported and handled as usual.
  1128. */
  1129. void eeh_dev_release(struct pci_dev *pdev)
  1130. {
  1131. struct eeh_dev *edev;
  1132. mutex_lock(&eeh_dev_mutex);
  1133. /* No PCI device ? */
  1134. if (!pdev)
  1135. goto out;
  1136. /* No EEH device ? */
  1137. edev = pci_dev_to_eeh_dev(pdev);
  1138. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1139. goto out;
  1140. /* Decrease PE's pass through count */
  1141. atomic_dec(&edev->pe->pass_dev_cnt);
  1142. WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
  1143. eeh_pe_change_owner(edev->pe);
  1144. out:
  1145. mutex_unlock(&eeh_dev_mutex);
  1146. }
  1147. EXPORT_SYMBOL(eeh_dev_release);
  1148. #ifdef CONFIG_IOMMU_API
  1149. static int dev_has_iommu_table(struct device *dev, void *data)
  1150. {
  1151. struct pci_dev *pdev = to_pci_dev(dev);
  1152. struct pci_dev **ppdev = data;
  1153. struct iommu_table *tbl;
  1154. if (!dev)
  1155. return 0;
  1156. tbl = get_iommu_table_base(dev);
  1157. if (tbl && tbl->it_group) {
  1158. *ppdev = pdev;
  1159. return 1;
  1160. }
  1161. return 0;
  1162. }
  1163. /**
  1164. * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
  1165. * @group: IOMMU group
  1166. *
  1167. * The routine is called to convert IOMMU group to EEH PE.
  1168. */
  1169. struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
  1170. {
  1171. struct pci_dev *pdev = NULL;
  1172. struct eeh_dev *edev;
  1173. int ret;
  1174. /* No IOMMU group ? */
  1175. if (!group)
  1176. return NULL;
  1177. ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
  1178. if (!ret || !pdev)
  1179. return NULL;
  1180. /* No EEH device or PE ? */
  1181. edev = pci_dev_to_eeh_dev(pdev);
  1182. if (!edev || !edev->pe)
  1183. return NULL;
  1184. return edev->pe;
  1185. }
  1186. EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
  1187. #endif /* CONFIG_IOMMU_API */
  1188. /**
  1189. * eeh_pe_set_option - Set options for the indicated PE
  1190. * @pe: EEH PE
  1191. * @option: requested option
  1192. *
  1193. * The routine is called to enable or disable EEH functionality
  1194. * on the indicated PE, to enable IO or DMA for the frozen PE.
  1195. */
  1196. int eeh_pe_set_option(struct eeh_pe *pe, int option)
  1197. {
  1198. int ret = 0;
  1199. /* Invalid PE ? */
  1200. if (!pe)
  1201. return -ENODEV;
  1202. /*
  1203. * EEH functionality could possibly be disabled, just
  1204. * return error for the case. And the EEH functinality
  1205. * isn't expected to be disabled on one specific PE.
  1206. */
  1207. switch (option) {
  1208. case EEH_OPT_ENABLE:
  1209. if (eeh_enabled()) {
  1210. ret = eeh_pe_change_owner(pe);
  1211. break;
  1212. }
  1213. ret = -EIO;
  1214. break;
  1215. case EEH_OPT_DISABLE:
  1216. break;
  1217. case EEH_OPT_THAW_MMIO:
  1218. case EEH_OPT_THAW_DMA:
  1219. if (!eeh_ops || !eeh_ops->set_option) {
  1220. ret = -ENOENT;
  1221. break;
  1222. }
  1223. ret = eeh_pci_enable(pe, option);
  1224. break;
  1225. default:
  1226. pr_debug("%s: Option %d out of range (%d, %d)\n",
  1227. __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
  1228. ret = -EINVAL;
  1229. }
  1230. return ret;
  1231. }
  1232. EXPORT_SYMBOL_GPL(eeh_pe_set_option);
  1233. /**
  1234. * eeh_pe_get_state - Retrieve PE's state
  1235. * @pe: EEH PE
  1236. *
  1237. * Retrieve the PE's state, which includes 3 aspects: enabled
  1238. * DMA, enabled IO and asserted reset.
  1239. */
  1240. int eeh_pe_get_state(struct eeh_pe *pe)
  1241. {
  1242. int result, ret = 0;
  1243. bool rst_active, dma_en, mmio_en;
  1244. /* Existing PE ? */
  1245. if (!pe)
  1246. return -ENODEV;
  1247. if (!eeh_ops || !eeh_ops->get_state)
  1248. return -ENOENT;
  1249. result = eeh_ops->get_state(pe, NULL);
  1250. rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
  1251. dma_en = !!(result & EEH_STATE_DMA_ENABLED);
  1252. mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
  1253. if (rst_active)
  1254. ret = EEH_PE_STATE_RESET;
  1255. else if (dma_en && mmio_en)
  1256. ret = EEH_PE_STATE_NORMAL;
  1257. else if (!dma_en && !mmio_en)
  1258. ret = EEH_PE_STATE_STOPPED_IO_DMA;
  1259. else if (!dma_en && mmio_en)
  1260. ret = EEH_PE_STATE_STOPPED_DMA;
  1261. else
  1262. ret = EEH_PE_STATE_UNAVAIL;
  1263. return ret;
  1264. }
  1265. EXPORT_SYMBOL_GPL(eeh_pe_get_state);
  1266. static int eeh_pe_reenable_devices(struct eeh_pe *pe)
  1267. {
  1268. struct eeh_dev *edev, *tmp;
  1269. struct pci_dev *pdev;
  1270. int ret = 0;
  1271. /* Restore config space */
  1272. eeh_pe_restore_bars(pe);
  1273. /*
  1274. * Reenable PCI devices as the devices passed
  1275. * through are always enabled before the reset.
  1276. */
  1277. eeh_pe_for_each_dev(pe, edev, tmp) {
  1278. pdev = eeh_dev_to_pci_dev(edev);
  1279. if (!pdev)
  1280. continue;
  1281. ret = pci_reenable_device(pdev);
  1282. if (ret) {
  1283. pr_warn("%s: Failure %d reenabling %s\n",
  1284. __func__, ret, pci_name(pdev));
  1285. return ret;
  1286. }
  1287. }
  1288. /* The PE is still in frozen state */
  1289. return eeh_unfreeze_pe(pe, true);
  1290. }
  1291. /**
  1292. * eeh_pe_reset - Issue PE reset according to specified type
  1293. * @pe: EEH PE
  1294. * @option: reset type
  1295. *
  1296. * The routine is called to reset the specified PE with the
  1297. * indicated type, either fundamental reset or hot reset.
  1298. * PE reset is the most important part for error recovery.
  1299. */
  1300. int eeh_pe_reset(struct eeh_pe *pe, int option)
  1301. {
  1302. int ret = 0;
  1303. /* Invalid PE ? */
  1304. if (!pe)
  1305. return -ENODEV;
  1306. if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
  1307. return -ENOENT;
  1308. switch (option) {
  1309. case EEH_RESET_DEACTIVATE:
  1310. ret = eeh_ops->reset(pe, option);
  1311. eeh_pe_state_clear(pe, EEH_PE_RESET);
  1312. if (ret)
  1313. break;
  1314. ret = eeh_pe_reenable_devices(pe);
  1315. break;
  1316. case EEH_RESET_HOT:
  1317. case EEH_RESET_FUNDAMENTAL:
  1318. /*
  1319. * Proactively freeze the PE to drop all MMIO access
  1320. * during reset, which should be banned as it's always
  1321. * cause recursive EEH error.
  1322. */
  1323. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  1324. eeh_pe_state_mark(pe, EEH_PE_RESET);
  1325. ret = eeh_ops->reset(pe, option);
  1326. break;
  1327. default:
  1328. pr_debug("%s: Unsupported option %d\n",
  1329. __func__, option);
  1330. ret = -EINVAL;
  1331. }
  1332. return ret;
  1333. }
  1334. EXPORT_SYMBOL_GPL(eeh_pe_reset);
  1335. /**
  1336. * eeh_pe_configure - Configure PCI bridges after PE reset
  1337. * @pe: EEH PE
  1338. *
  1339. * The routine is called to restore the PCI config space for
  1340. * those PCI devices, especially PCI bridges affected by PE
  1341. * reset issued previously.
  1342. */
  1343. int eeh_pe_configure(struct eeh_pe *pe)
  1344. {
  1345. int ret = 0;
  1346. /* Invalid PE ? */
  1347. if (!pe)
  1348. return -ENODEV;
  1349. return ret;
  1350. }
  1351. EXPORT_SYMBOL_GPL(eeh_pe_configure);
  1352. static int proc_eeh_show(struct seq_file *m, void *v)
  1353. {
  1354. if (!eeh_enabled()) {
  1355. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1356. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  1357. } else {
  1358. seq_printf(m, "EEH Subsystem is enabled\n");
  1359. seq_printf(m,
  1360. "no device=%llu\n"
  1361. "no device node=%llu\n"
  1362. "no config address=%llu\n"
  1363. "check not wanted=%llu\n"
  1364. "eeh_total_mmio_ffs=%llu\n"
  1365. "eeh_false_positives=%llu\n"
  1366. "eeh_slot_resets=%llu\n",
  1367. eeh_stats.no_device,
  1368. eeh_stats.no_dn,
  1369. eeh_stats.no_cfg_addr,
  1370. eeh_stats.ignored_check,
  1371. eeh_stats.total_mmio_ffs,
  1372. eeh_stats.false_positives,
  1373. eeh_stats.slot_resets);
  1374. }
  1375. return 0;
  1376. }
  1377. static int proc_eeh_open(struct inode *inode, struct file *file)
  1378. {
  1379. return single_open(file, proc_eeh_show, NULL);
  1380. }
  1381. static const struct file_operations proc_eeh_operations = {
  1382. .open = proc_eeh_open,
  1383. .read = seq_read,
  1384. .llseek = seq_lseek,
  1385. .release = single_release,
  1386. };
  1387. #ifdef CONFIG_DEBUG_FS
  1388. static int eeh_enable_dbgfs_set(void *data, u64 val)
  1389. {
  1390. if (val)
  1391. eeh_clear_flag(EEH_FORCE_DISABLED);
  1392. else
  1393. eeh_add_flag(EEH_FORCE_DISABLED);
  1394. /* Notify the backend */
  1395. if (eeh_ops->post_init)
  1396. eeh_ops->post_init();
  1397. return 0;
  1398. }
  1399. static int eeh_enable_dbgfs_get(void *data, u64 *val)
  1400. {
  1401. if (eeh_enabled())
  1402. *val = 0x1ul;
  1403. else
  1404. *val = 0x0ul;
  1405. return 0;
  1406. }
  1407. DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
  1408. eeh_enable_dbgfs_set, "0x%llx\n");
  1409. #endif
  1410. static int __init eeh_init_proc(void)
  1411. {
  1412. if (machine_is(pseries) || machine_is(powernv)) {
  1413. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  1414. #ifdef CONFIG_DEBUG_FS
  1415. debugfs_create_file("eeh_enable", 0600,
  1416. powerpc_debugfs_root, NULL,
  1417. &eeh_enable_dbgfs_ops);
  1418. #endif
  1419. }
  1420. return 0;
  1421. }
  1422. __initcall(eeh_init_proc);