eeh.c 44 KB

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