eeh.c 46 KB

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