eeh.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  647. break;
  648. case pcie_hot_reset:
  649. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  650. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  651. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  652. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  653. eeh_ops->reset(pe, EEH_RESET_HOT);
  654. break;
  655. case pcie_warm_reset:
  656. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  657. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  658. eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
  659. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  660. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  661. break;
  662. default:
  663. eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
  664. return -EINVAL;
  665. };
  666. return 0;
  667. }
  668. /**
  669. * eeh_set_pe_freset - Check the required reset for the indicated device
  670. * @data: EEH device
  671. * @flag: return value
  672. *
  673. * Each device might have its preferred reset type: fundamental or
  674. * hot reset. The routine is used to collected the information for
  675. * the indicated device and its children so that the bunch of the
  676. * devices could be reset properly.
  677. */
  678. static void *eeh_set_dev_freset(void *data, void *flag)
  679. {
  680. struct pci_dev *dev;
  681. unsigned int *freset = (unsigned int *)flag;
  682. struct eeh_dev *edev = (struct eeh_dev *)data;
  683. dev = eeh_dev_to_pci_dev(edev);
  684. if (dev)
  685. *freset |= dev->needs_freset;
  686. return NULL;
  687. }
  688. /**
  689. * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
  690. * @pe: EEH PE
  691. *
  692. * Assert the PCI #RST line for 1/4 second.
  693. */
  694. static void eeh_reset_pe_once(struct eeh_pe *pe)
  695. {
  696. unsigned int freset = 0;
  697. /* Determine type of EEH reset required for
  698. * Partitionable Endpoint, a hot-reset (1)
  699. * or a fundamental reset (3).
  700. * A fundamental reset required by any device under
  701. * Partitionable Endpoint trumps hot-reset.
  702. */
  703. eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
  704. if (freset)
  705. eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
  706. else
  707. eeh_ops->reset(pe, EEH_RESET_HOT);
  708. eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
  709. }
  710. /**
  711. * eeh_reset_pe - Reset the indicated PE
  712. * @pe: EEH PE
  713. *
  714. * This routine should be called to reset indicated device, including
  715. * PE. A PE might include multiple PCI devices and sometimes PCI bridges
  716. * might be involved as well.
  717. */
  718. int eeh_reset_pe(struct eeh_pe *pe)
  719. {
  720. int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  721. int i, state, ret;
  722. /* Mark as reset and block config space */
  723. eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  724. /* Take three shots at resetting the bus */
  725. for (i = 0; i < 3; i++) {
  726. eeh_reset_pe_once(pe);
  727. /*
  728. * EEH_PE_ISOLATED is expected to be removed after
  729. * BAR restore.
  730. */
  731. state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
  732. if ((state & flags) == flags) {
  733. ret = 0;
  734. goto out;
  735. }
  736. if (state < 0) {
  737. pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
  738. __func__, pe->phb->global_number, pe->addr);
  739. ret = -ENOTRECOVERABLE;
  740. goto out;
  741. }
  742. /* We might run out of credits */
  743. ret = -EIO;
  744. pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
  745. __func__, state, pe->phb->global_number, pe->addr, (i + 1));
  746. }
  747. out:
  748. eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
  749. return ret;
  750. }
  751. /**
  752. * eeh_save_bars - Save device bars
  753. * @edev: PCI device associated EEH device
  754. *
  755. * Save the values of the device bars. Unlike the restore
  756. * routine, this routine is *not* recursive. This is because
  757. * PCI devices are added individually; but, for the restore,
  758. * an entire slot is reset at a time.
  759. */
  760. void eeh_save_bars(struct eeh_dev *edev)
  761. {
  762. struct pci_dn *pdn;
  763. int i;
  764. pdn = eeh_dev_to_pdn(edev);
  765. if (!pdn)
  766. return;
  767. for (i = 0; i < 16; i++)
  768. eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
  769. /*
  770. * For PCI bridges including root port, we need enable bus
  771. * master explicitly. Otherwise, it can't fetch IODA table
  772. * entries correctly. So we cache the bit in advance so that
  773. * we can restore it after reset, either PHB range or PE range.
  774. */
  775. if (edev->mode & EEH_DEV_BRIDGE)
  776. edev->config_space[1] |= PCI_COMMAND_MASTER;
  777. }
  778. /**
  779. * eeh_ops_register - Register platform dependent EEH operations
  780. * @ops: platform dependent EEH operations
  781. *
  782. * Register the platform dependent EEH operation callback
  783. * functions. The platform should call this function before
  784. * any other EEH operations.
  785. */
  786. int __init eeh_ops_register(struct eeh_ops *ops)
  787. {
  788. if (!ops->name) {
  789. pr_warn("%s: Invalid EEH ops name for %p\n",
  790. __func__, ops);
  791. return -EINVAL;
  792. }
  793. if (eeh_ops && eeh_ops != ops) {
  794. pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
  795. __func__, eeh_ops->name, ops->name);
  796. return -EEXIST;
  797. }
  798. eeh_ops = ops;
  799. return 0;
  800. }
  801. /**
  802. * eeh_ops_unregister - Unreigster platform dependent EEH operations
  803. * @name: name of EEH platform operations
  804. *
  805. * Unregister the platform dependent EEH operation callback
  806. * functions.
  807. */
  808. int __exit eeh_ops_unregister(const char *name)
  809. {
  810. if (!name || !strlen(name)) {
  811. pr_warn("%s: Invalid EEH ops name\n",
  812. __func__);
  813. return -EINVAL;
  814. }
  815. if (eeh_ops && !strcmp(eeh_ops->name, name)) {
  816. eeh_ops = NULL;
  817. return 0;
  818. }
  819. return -EEXIST;
  820. }
  821. static int eeh_reboot_notifier(struct notifier_block *nb,
  822. unsigned long action, void *unused)
  823. {
  824. eeh_clear_flag(EEH_ENABLED);
  825. return NOTIFY_DONE;
  826. }
  827. static struct notifier_block eeh_reboot_nb = {
  828. .notifier_call = eeh_reboot_notifier,
  829. };
  830. /**
  831. * eeh_init - EEH initialization
  832. *
  833. * Initialize EEH by trying to enable it for all of the adapters in the system.
  834. * As a side effect we can determine here if eeh is supported at all.
  835. * Note that we leave EEH on so failed config cycles won't cause a machine
  836. * check. If a user turns off EEH for a particular adapter they are really
  837. * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
  838. * grant access to a slot if EEH isn't enabled, and so we always enable
  839. * EEH for all slots/all devices.
  840. *
  841. * The eeh-force-off option disables EEH checking globally, for all slots.
  842. * Even if force-off is set, the EEH hardware is still enabled, so that
  843. * newer systems can boot.
  844. */
  845. int eeh_init(void)
  846. {
  847. struct pci_controller *hose, *tmp;
  848. struct pci_dn *pdn;
  849. static int cnt = 0;
  850. int ret = 0;
  851. /*
  852. * We have to delay the initialization on PowerNV after
  853. * the PCI hierarchy tree has been built because the PEs
  854. * are figured out based on PCI devices instead of device
  855. * tree nodes
  856. */
  857. if (machine_is(powernv) && cnt++ <= 0)
  858. return ret;
  859. /* Register reboot notifier */
  860. ret = register_reboot_notifier(&eeh_reboot_nb);
  861. if (ret) {
  862. pr_warn("%s: Failed to register notifier (%d)\n",
  863. __func__, ret);
  864. return ret;
  865. }
  866. /* call platform initialization function */
  867. if (!eeh_ops) {
  868. pr_warn("%s: Platform EEH operation not found\n",
  869. __func__);
  870. return -EEXIST;
  871. } else if ((ret = eeh_ops->init()))
  872. return ret;
  873. /* Initialize EEH event */
  874. ret = eeh_event_init();
  875. if (ret)
  876. return ret;
  877. /* Enable EEH for all adapters */
  878. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  879. pdn = hose->pci_data;
  880. traverse_pci_dn(pdn, eeh_ops->probe, NULL);
  881. }
  882. /*
  883. * Call platform post-initialization. Actually, It's good chance
  884. * to inform platform that EEH is ready to supply service if the
  885. * I/O cache stuff has been built up.
  886. */
  887. if (eeh_ops->post_init) {
  888. ret = eeh_ops->post_init();
  889. if (ret)
  890. return ret;
  891. }
  892. if (eeh_enabled())
  893. pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
  894. else
  895. pr_warn("EEH: No capable adapters found\n");
  896. return ret;
  897. }
  898. core_initcall_sync(eeh_init);
  899. /**
  900. * eeh_add_device_early - Enable EEH for the indicated device node
  901. * @pdn: PCI device node for which to set up EEH
  902. *
  903. * This routine must be used to perform EEH initialization for PCI
  904. * devices that were added after system boot (e.g. hotplug, dlpar).
  905. * This routine must be called before any i/o is performed to the
  906. * adapter (inluding any config-space i/o).
  907. * Whether this actually enables EEH or not for this device depends
  908. * on the CEC architecture, type of the device, on earlier boot
  909. * command-line arguments & etc.
  910. */
  911. void eeh_add_device_early(struct pci_dn *pdn)
  912. {
  913. struct pci_controller *phb;
  914. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  915. if (!edev || !eeh_enabled())
  916. return;
  917. if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
  918. return;
  919. /* USB Bus children of PCI devices will not have BUID's */
  920. phb = edev->phb;
  921. if (NULL == phb ||
  922. (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
  923. return;
  924. eeh_ops->probe(pdn, NULL);
  925. }
  926. /**
  927. * eeh_add_device_tree_early - Enable EEH for the indicated device
  928. * @pdn: PCI device node
  929. *
  930. * This routine must be used to perform EEH initialization for the
  931. * indicated PCI device that was added after system boot (e.g.
  932. * hotplug, dlpar).
  933. */
  934. void eeh_add_device_tree_early(struct pci_dn *pdn)
  935. {
  936. struct pci_dn *n;
  937. if (!pdn)
  938. return;
  939. list_for_each_entry(n, &pdn->child_list, list)
  940. eeh_add_device_tree_early(n);
  941. eeh_add_device_early(pdn);
  942. }
  943. EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
  944. /**
  945. * eeh_add_device_late - Perform EEH initialization for the indicated pci device
  946. * @dev: pci device for which to set up EEH
  947. *
  948. * This routine must be used to complete EEH initialization for PCI
  949. * devices that were added after system boot (e.g. hotplug, dlpar).
  950. */
  951. void eeh_add_device_late(struct pci_dev *dev)
  952. {
  953. struct pci_dn *pdn;
  954. struct eeh_dev *edev;
  955. if (!dev || !eeh_enabled())
  956. return;
  957. pr_debug("EEH: Adding device %s\n", pci_name(dev));
  958. pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  959. edev = pdn_to_eeh_dev(pdn);
  960. if (edev->pdev == dev) {
  961. pr_debug("EEH: Already referenced !\n");
  962. return;
  963. }
  964. if (eeh_has_flag(EEH_PROBE_MODE_DEV))
  965. eeh_ops->probe(pdn, NULL);
  966. /*
  967. * The EEH cache might not be removed correctly because of
  968. * unbalanced kref to the device during unplug time, which
  969. * relies on pcibios_release_device(). So we have to remove
  970. * that here explicitly.
  971. */
  972. if (edev->pdev) {
  973. eeh_rmv_from_parent_pe(edev);
  974. eeh_addr_cache_rmv_dev(edev->pdev);
  975. eeh_sysfs_remove_device(edev->pdev);
  976. edev->mode &= ~EEH_DEV_SYSFS;
  977. /*
  978. * We definitely should have the PCI device removed
  979. * though it wasn't correctly. So we needn't call
  980. * into error handler afterwards.
  981. */
  982. edev->mode |= EEH_DEV_NO_HANDLER;
  983. edev->pdev = NULL;
  984. dev->dev.archdata.edev = NULL;
  985. }
  986. edev->pdev = dev;
  987. dev->dev.archdata.edev = edev;
  988. eeh_addr_cache_insert_dev(dev);
  989. }
  990. /**
  991. * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
  992. * @bus: PCI bus
  993. *
  994. * This routine must be used to perform EEH initialization for PCI
  995. * devices which are attached to the indicated PCI bus. The PCI bus
  996. * is added after system boot through hotplug or dlpar.
  997. */
  998. void eeh_add_device_tree_late(struct pci_bus *bus)
  999. {
  1000. struct pci_dev *dev;
  1001. list_for_each_entry(dev, &bus->devices, bus_list) {
  1002. eeh_add_device_late(dev);
  1003. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1004. struct pci_bus *subbus = dev->subordinate;
  1005. if (subbus)
  1006. eeh_add_device_tree_late(subbus);
  1007. }
  1008. }
  1009. }
  1010. EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
  1011. /**
  1012. * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
  1013. * @bus: PCI bus
  1014. *
  1015. * This routine must be used to add EEH sysfs files for PCI
  1016. * devices which are attached to the indicated PCI bus. The PCI bus
  1017. * is added after system boot through hotplug or dlpar.
  1018. */
  1019. void eeh_add_sysfs_files(struct pci_bus *bus)
  1020. {
  1021. struct pci_dev *dev;
  1022. list_for_each_entry(dev, &bus->devices, bus_list) {
  1023. eeh_sysfs_add_device(dev);
  1024. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  1025. struct pci_bus *subbus = dev->subordinate;
  1026. if (subbus)
  1027. eeh_add_sysfs_files(subbus);
  1028. }
  1029. }
  1030. }
  1031. EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
  1032. /**
  1033. * eeh_remove_device - Undo EEH setup for the indicated pci device
  1034. * @dev: pci device to be removed
  1035. *
  1036. * This routine should be called when a device is removed from
  1037. * a running system (e.g. by hotplug or dlpar). It unregisters
  1038. * the PCI device from the EEH subsystem. I/O errors affecting
  1039. * this device will no longer be detected after this call; thus,
  1040. * i/o errors affecting this slot may leave this device unusable.
  1041. */
  1042. void eeh_remove_device(struct pci_dev *dev)
  1043. {
  1044. struct eeh_dev *edev;
  1045. if (!dev || !eeh_enabled())
  1046. return;
  1047. edev = pci_dev_to_eeh_dev(dev);
  1048. /* Unregister the device with the EEH/PCI address search system */
  1049. pr_debug("EEH: Removing device %s\n", pci_name(dev));
  1050. if (!edev || !edev->pdev || !edev->pe) {
  1051. pr_debug("EEH: Not referenced !\n");
  1052. return;
  1053. }
  1054. /*
  1055. * During the hotplug for EEH error recovery, we need the EEH
  1056. * device attached to the parent PE in order for BAR restore
  1057. * a bit later. So we keep it for BAR restore and remove it
  1058. * from the parent PE during the BAR resotre.
  1059. */
  1060. edev->pdev = NULL;
  1061. dev->dev.archdata.edev = NULL;
  1062. if (!(edev->pe->state & EEH_PE_KEEP))
  1063. eeh_rmv_from_parent_pe(edev);
  1064. else
  1065. edev->mode |= EEH_DEV_DISCONNECTED;
  1066. /*
  1067. * We're removing from the PCI subsystem, that means
  1068. * the PCI device driver can't support EEH or not
  1069. * well. So we rely on hotplug completely to do recovery
  1070. * for the specific PCI device.
  1071. */
  1072. edev->mode |= EEH_DEV_NO_HANDLER;
  1073. eeh_addr_cache_rmv_dev(dev);
  1074. eeh_sysfs_remove_device(dev);
  1075. edev->mode &= ~EEH_DEV_SYSFS;
  1076. }
  1077. int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
  1078. {
  1079. int ret;
  1080. ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
  1081. if (ret) {
  1082. pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
  1083. __func__, ret, pe->phb->global_number, pe->addr);
  1084. return ret;
  1085. }
  1086. ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
  1087. if (ret) {
  1088. pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
  1089. __func__, ret, pe->phb->global_number, pe->addr);
  1090. return ret;
  1091. }
  1092. /* Clear software isolated state */
  1093. if (sw_state && (pe->state & EEH_PE_ISOLATED))
  1094. eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
  1095. return ret;
  1096. }
  1097. static struct pci_device_id eeh_reset_ids[] = {
  1098. { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
  1099. { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
  1100. { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
  1101. { 0 }
  1102. };
  1103. static int eeh_pe_change_owner(struct eeh_pe *pe)
  1104. {
  1105. struct eeh_dev *edev, *tmp;
  1106. struct pci_dev *pdev;
  1107. struct pci_device_id *id;
  1108. int flags, ret;
  1109. /* Check PE state */
  1110. flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  1111. ret = eeh_ops->get_state(pe, NULL);
  1112. if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
  1113. return 0;
  1114. /* Unfrozen PE, nothing to do */
  1115. if ((ret & flags) == flags)
  1116. return 0;
  1117. /* Frozen PE, check if it needs PE level reset */
  1118. eeh_pe_for_each_dev(pe, edev, tmp) {
  1119. pdev = eeh_dev_to_pci_dev(edev);
  1120. if (!pdev)
  1121. continue;
  1122. for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
  1123. if (id->vendor != PCI_ANY_ID &&
  1124. id->vendor != pdev->vendor)
  1125. continue;
  1126. if (id->device != PCI_ANY_ID &&
  1127. id->device != pdev->device)
  1128. continue;
  1129. if (id->subvendor != PCI_ANY_ID &&
  1130. id->subvendor != pdev->subsystem_vendor)
  1131. continue;
  1132. if (id->subdevice != PCI_ANY_ID &&
  1133. id->subdevice != pdev->subsystem_device)
  1134. continue;
  1135. goto reset;
  1136. }
  1137. }
  1138. return eeh_unfreeze_pe(pe, true);
  1139. reset:
  1140. return eeh_pe_reset_and_recover(pe);
  1141. }
  1142. /**
  1143. * eeh_dev_open - Increase count of pass through devices for PE
  1144. * @pdev: PCI device
  1145. *
  1146. * Increase count of passed through devices for the indicated
  1147. * PE. In the result, the EEH errors detected on the PE won't be
  1148. * reported. The PE owner will be responsible for detection
  1149. * and recovery.
  1150. */
  1151. int eeh_dev_open(struct pci_dev *pdev)
  1152. {
  1153. struct eeh_dev *edev;
  1154. int ret = -ENODEV;
  1155. mutex_lock(&eeh_dev_mutex);
  1156. /* No PCI device ? */
  1157. if (!pdev)
  1158. goto out;
  1159. /* No EEH device or PE ? */
  1160. edev = pci_dev_to_eeh_dev(pdev);
  1161. if (!edev || !edev->pe)
  1162. goto out;
  1163. /*
  1164. * The PE might have been put into frozen state, but we
  1165. * didn't detect that yet. The passed through PCI devices
  1166. * in frozen PE won't work properly. Clear the frozen state
  1167. * in advance.
  1168. */
  1169. ret = eeh_pe_change_owner(edev->pe);
  1170. if (ret)
  1171. goto out;
  1172. /* Increase PE's pass through count */
  1173. atomic_inc(&edev->pe->pass_dev_cnt);
  1174. mutex_unlock(&eeh_dev_mutex);
  1175. return 0;
  1176. out:
  1177. mutex_unlock(&eeh_dev_mutex);
  1178. return ret;
  1179. }
  1180. EXPORT_SYMBOL_GPL(eeh_dev_open);
  1181. /**
  1182. * eeh_dev_release - Decrease count of pass through devices for PE
  1183. * @pdev: PCI device
  1184. *
  1185. * Decrease count of pass through devices for the indicated PE. If
  1186. * there is no passed through device in PE, the EEH errors detected
  1187. * on the PE will be reported and handled as usual.
  1188. */
  1189. void eeh_dev_release(struct pci_dev *pdev)
  1190. {
  1191. struct eeh_dev *edev;
  1192. mutex_lock(&eeh_dev_mutex);
  1193. /* No PCI device ? */
  1194. if (!pdev)
  1195. goto out;
  1196. /* No EEH device ? */
  1197. edev = pci_dev_to_eeh_dev(pdev);
  1198. if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
  1199. goto out;
  1200. /* Decrease PE's pass through count */
  1201. atomic_dec(&edev->pe->pass_dev_cnt);
  1202. WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
  1203. eeh_pe_change_owner(edev->pe);
  1204. out:
  1205. mutex_unlock(&eeh_dev_mutex);
  1206. }
  1207. EXPORT_SYMBOL(eeh_dev_release);
  1208. #ifdef CONFIG_IOMMU_API
  1209. static int dev_has_iommu_table(struct device *dev, void *data)
  1210. {
  1211. struct pci_dev *pdev = to_pci_dev(dev);
  1212. struct pci_dev **ppdev = data;
  1213. struct iommu_table *tbl;
  1214. if (!dev)
  1215. return 0;
  1216. tbl = get_iommu_table_base(dev);
  1217. if (tbl && tbl->it_group) {
  1218. *ppdev = pdev;
  1219. return 1;
  1220. }
  1221. return 0;
  1222. }
  1223. /**
  1224. * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
  1225. * @group: IOMMU group
  1226. *
  1227. * The routine is called to convert IOMMU group to EEH PE.
  1228. */
  1229. struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
  1230. {
  1231. struct pci_dev *pdev = NULL;
  1232. struct eeh_dev *edev;
  1233. int ret;
  1234. /* No IOMMU group ? */
  1235. if (!group)
  1236. return NULL;
  1237. ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
  1238. if (!ret || !pdev)
  1239. return NULL;
  1240. /* No EEH device or PE ? */
  1241. edev = pci_dev_to_eeh_dev(pdev);
  1242. if (!edev || !edev->pe)
  1243. return NULL;
  1244. return edev->pe;
  1245. }
  1246. EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
  1247. #endif /* CONFIG_IOMMU_API */
  1248. /**
  1249. * eeh_pe_set_option - Set options for the indicated PE
  1250. * @pe: EEH PE
  1251. * @option: requested option
  1252. *
  1253. * The routine is called to enable or disable EEH functionality
  1254. * on the indicated PE, to enable IO or DMA for the frozen PE.
  1255. */
  1256. int eeh_pe_set_option(struct eeh_pe *pe, int option)
  1257. {
  1258. int ret = 0;
  1259. /* Invalid PE ? */
  1260. if (!pe)
  1261. return -ENODEV;
  1262. /*
  1263. * EEH functionality could possibly be disabled, just
  1264. * return error for the case. And the EEH functinality
  1265. * isn't expected to be disabled on one specific PE.
  1266. */
  1267. switch (option) {
  1268. case EEH_OPT_ENABLE:
  1269. if (eeh_enabled()) {
  1270. ret = eeh_pe_change_owner(pe);
  1271. break;
  1272. }
  1273. ret = -EIO;
  1274. break;
  1275. case EEH_OPT_DISABLE:
  1276. break;
  1277. case EEH_OPT_THAW_MMIO:
  1278. case EEH_OPT_THAW_DMA:
  1279. if (!eeh_ops || !eeh_ops->set_option) {
  1280. ret = -ENOENT;
  1281. break;
  1282. }
  1283. ret = eeh_pci_enable(pe, option);
  1284. break;
  1285. default:
  1286. pr_debug("%s: Option %d out of range (%d, %d)\n",
  1287. __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
  1288. ret = -EINVAL;
  1289. }
  1290. return ret;
  1291. }
  1292. EXPORT_SYMBOL_GPL(eeh_pe_set_option);
  1293. /**
  1294. * eeh_pe_get_state - Retrieve PE's state
  1295. * @pe: EEH PE
  1296. *
  1297. * Retrieve the PE's state, which includes 3 aspects: enabled
  1298. * DMA, enabled IO and asserted reset.
  1299. */
  1300. int eeh_pe_get_state(struct eeh_pe *pe)
  1301. {
  1302. int result, ret = 0;
  1303. bool rst_active, dma_en, mmio_en;
  1304. /* Existing PE ? */
  1305. if (!pe)
  1306. return -ENODEV;
  1307. if (!eeh_ops || !eeh_ops->get_state)
  1308. return -ENOENT;
  1309. result = eeh_ops->get_state(pe, NULL);
  1310. rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
  1311. dma_en = !!(result & EEH_STATE_DMA_ENABLED);
  1312. mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
  1313. if (rst_active)
  1314. ret = EEH_PE_STATE_RESET;
  1315. else if (dma_en && mmio_en)
  1316. ret = EEH_PE_STATE_NORMAL;
  1317. else if (!dma_en && !mmio_en)
  1318. ret = EEH_PE_STATE_STOPPED_IO_DMA;
  1319. else if (!dma_en && mmio_en)
  1320. ret = EEH_PE_STATE_STOPPED_DMA;
  1321. else
  1322. ret = EEH_PE_STATE_UNAVAIL;
  1323. return ret;
  1324. }
  1325. EXPORT_SYMBOL_GPL(eeh_pe_get_state);
  1326. static int eeh_pe_reenable_devices(struct eeh_pe *pe)
  1327. {
  1328. struct eeh_dev *edev, *tmp;
  1329. struct pci_dev *pdev;
  1330. int ret = 0;
  1331. /* Restore config space */
  1332. eeh_pe_restore_bars(pe);
  1333. /*
  1334. * Reenable PCI devices as the devices passed
  1335. * through are always enabled before the reset.
  1336. */
  1337. eeh_pe_for_each_dev(pe, edev, tmp) {
  1338. pdev = eeh_dev_to_pci_dev(edev);
  1339. if (!pdev)
  1340. continue;
  1341. ret = pci_reenable_device(pdev);
  1342. if (ret) {
  1343. pr_warn("%s: Failure %d reenabling %s\n",
  1344. __func__, ret, pci_name(pdev));
  1345. return ret;
  1346. }
  1347. }
  1348. /* The PE is still in frozen state */
  1349. return eeh_unfreeze_pe(pe, true);
  1350. }
  1351. /**
  1352. * eeh_pe_reset - Issue PE reset according to specified type
  1353. * @pe: EEH PE
  1354. * @option: reset type
  1355. *
  1356. * The routine is called to reset the specified PE with the
  1357. * indicated type, either fundamental reset or hot reset.
  1358. * PE reset is the most important part for error recovery.
  1359. */
  1360. int eeh_pe_reset(struct eeh_pe *pe, int option)
  1361. {
  1362. int ret = 0;
  1363. /* Invalid PE ? */
  1364. if (!pe)
  1365. return -ENODEV;
  1366. if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
  1367. return -ENOENT;
  1368. switch (option) {
  1369. case EEH_RESET_DEACTIVATE:
  1370. ret = eeh_ops->reset(pe, option);
  1371. eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
  1372. if (ret)
  1373. break;
  1374. ret = eeh_pe_reenable_devices(pe);
  1375. break;
  1376. case EEH_RESET_HOT:
  1377. case EEH_RESET_FUNDAMENTAL:
  1378. /*
  1379. * Proactively freeze the PE to drop all MMIO access
  1380. * during reset, which should be banned as it's always
  1381. * cause recursive EEH error.
  1382. */
  1383. eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
  1384. eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
  1385. ret = eeh_ops->reset(pe, option);
  1386. break;
  1387. default:
  1388. pr_debug("%s: Unsupported option %d\n",
  1389. __func__, option);
  1390. ret = -EINVAL;
  1391. }
  1392. return ret;
  1393. }
  1394. EXPORT_SYMBOL_GPL(eeh_pe_reset);
  1395. /**
  1396. * eeh_pe_configure - Configure PCI bridges after PE reset
  1397. * @pe: EEH PE
  1398. *
  1399. * The routine is called to restore the PCI config space for
  1400. * those PCI devices, especially PCI bridges affected by PE
  1401. * reset issued previously.
  1402. */
  1403. int eeh_pe_configure(struct eeh_pe *pe)
  1404. {
  1405. int ret = 0;
  1406. /* Invalid PE ? */
  1407. if (!pe)
  1408. return -ENODEV;
  1409. return ret;
  1410. }
  1411. EXPORT_SYMBOL_GPL(eeh_pe_configure);
  1412. static int proc_eeh_show(struct seq_file *m, void *v)
  1413. {
  1414. if (!eeh_enabled()) {
  1415. seq_printf(m, "EEH Subsystem is globally disabled\n");
  1416. seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
  1417. } else {
  1418. seq_printf(m, "EEH Subsystem is enabled\n");
  1419. seq_printf(m,
  1420. "no device=%llu\n"
  1421. "no device node=%llu\n"
  1422. "no config address=%llu\n"
  1423. "check not wanted=%llu\n"
  1424. "eeh_total_mmio_ffs=%llu\n"
  1425. "eeh_false_positives=%llu\n"
  1426. "eeh_slot_resets=%llu\n",
  1427. eeh_stats.no_device,
  1428. eeh_stats.no_dn,
  1429. eeh_stats.no_cfg_addr,
  1430. eeh_stats.ignored_check,
  1431. eeh_stats.total_mmio_ffs,
  1432. eeh_stats.false_positives,
  1433. eeh_stats.slot_resets);
  1434. }
  1435. return 0;
  1436. }
  1437. static int proc_eeh_open(struct inode *inode, struct file *file)
  1438. {
  1439. return single_open(file, proc_eeh_show, NULL);
  1440. }
  1441. static const struct file_operations proc_eeh_operations = {
  1442. .open = proc_eeh_open,
  1443. .read = seq_read,
  1444. .llseek = seq_lseek,
  1445. .release = single_release,
  1446. };
  1447. #ifdef CONFIG_DEBUG_FS
  1448. static int eeh_enable_dbgfs_set(void *data, u64 val)
  1449. {
  1450. if (val)
  1451. eeh_clear_flag(EEH_FORCE_DISABLED);
  1452. else
  1453. eeh_add_flag(EEH_FORCE_DISABLED);
  1454. /* Notify the backend */
  1455. if (eeh_ops->post_init)
  1456. eeh_ops->post_init();
  1457. return 0;
  1458. }
  1459. static int eeh_enable_dbgfs_get(void *data, u64 *val)
  1460. {
  1461. if (eeh_enabled())
  1462. *val = 0x1ul;
  1463. else
  1464. *val = 0x0ul;
  1465. return 0;
  1466. }
  1467. static int eeh_freeze_dbgfs_set(void *data, u64 val)
  1468. {
  1469. eeh_max_freezes = val;
  1470. return 0;
  1471. }
  1472. static int eeh_freeze_dbgfs_get(void *data, u64 *val)
  1473. {
  1474. *val = eeh_max_freezes;
  1475. return 0;
  1476. }
  1477. DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
  1478. eeh_enable_dbgfs_set, "0x%llx\n");
  1479. DEFINE_SIMPLE_ATTRIBUTE(eeh_freeze_dbgfs_ops, eeh_freeze_dbgfs_get,
  1480. eeh_freeze_dbgfs_set, "0x%llx\n");
  1481. #endif
  1482. static int __init eeh_init_proc(void)
  1483. {
  1484. if (machine_is(pseries) || machine_is(powernv)) {
  1485. proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
  1486. #ifdef CONFIG_DEBUG_FS
  1487. debugfs_create_file("eeh_enable", 0600,
  1488. powerpc_debugfs_root, NULL,
  1489. &eeh_enable_dbgfs_ops);
  1490. debugfs_create_file("eeh_max_freezes", 0600,
  1491. powerpc_debugfs_root, NULL,
  1492. &eeh_freeze_dbgfs_ops);
  1493. #endif
  1494. }
  1495. return 0;
  1496. }
  1497. __initcall(eeh_init_proc);