eeh.c 47 KB

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