eeh.c 42 KB

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