eeh-powernv.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * The file intends to implement the platform dependent EEH operations on
  3. * powernv platform. Actually, the powernv was created in order to fully
  4. * hypervisor support.
  5. *
  6. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/delay.h>
  16. #include <linux/export.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/list.h>
  20. #include <linux/msi.h>
  21. #include <linux/of.h>
  22. #include <linux/pci.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/rbtree.h>
  25. #include <linux/sched.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/spinlock.h>
  28. #include <asm/eeh.h>
  29. #include <asm/eeh_event.h>
  30. #include <asm/firmware.h>
  31. #include <asm/io.h>
  32. #include <asm/iommu.h>
  33. #include <asm/machdep.h>
  34. #include <asm/msi_bitmap.h>
  35. #include <asm/opal.h>
  36. #include <asm/ppc-pci.h>
  37. #include "powernv.h"
  38. #include "pci.h"
  39. static bool pnv_eeh_nb_init = false;
  40. static int eeh_event_irq = -EINVAL;
  41. static int pnv_eeh_init(void)
  42. {
  43. struct pci_controller *hose;
  44. struct pnv_phb *phb;
  45. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  46. pr_warn("%s: OPAL is required !\n",
  47. __func__);
  48. return -EINVAL;
  49. }
  50. /* Set probe mode */
  51. eeh_add_flag(EEH_PROBE_MODE_DEV);
  52. /*
  53. * P7IOC blocks PCI config access to frozen PE, but PHB3
  54. * doesn't do that. So we have to selectively enable I/O
  55. * prior to collecting error log.
  56. */
  57. list_for_each_entry(hose, &hose_list, list_node) {
  58. phb = hose->private_data;
  59. if (phb->model == PNV_PHB_MODEL_P7IOC)
  60. eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
  61. /*
  62. * PE#0 should be regarded as valid by EEH core
  63. * if it's not the reserved one. Currently, we
  64. * have the reserved PE#255 and PE#127 for PHB3
  65. * and P7IOC separately. So we should regard
  66. * PE#0 as valid for PHB3 and P7IOC.
  67. */
  68. if (phb->ioda.reserved_pe != 0)
  69. eeh_add_flag(EEH_VALID_PE_ZERO);
  70. break;
  71. }
  72. return 0;
  73. }
  74. static irqreturn_t pnv_eeh_event(int irq, void *data)
  75. {
  76. /*
  77. * We simply send a special EEH event if EEH has been
  78. * enabled. We don't care about EEH events until we've
  79. * finished processing the outstanding ones. Event processing
  80. * gets unmasked in next_error() if EEH is enabled.
  81. */
  82. disable_irq_nosync(irq);
  83. if (eeh_enabled())
  84. eeh_send_failure_event(NULL);
  85. return IRQ_HANDLED;
  86. }
  87. #ifdef CONFIG_DEBUG_FS
  88. static ssize_t pnv_eeh_ei_write(struct file *filp,
  89. const char __user *user_buf,
  90. size_t count, loff_t *ppos)
  91. {
  92. struct pci_controller *hose = filp->private_data;
  93. struct eeh_dev *edev;
  94. struct eeh_pe *pe;
  95. int pe_no, type, func;
  96. unsigned long addr, mask;
  97. char buf[50];
  98. int ret;
  99. if (!eeh_ops || !eeh_ops->err_inject)
  100. return -ENXIO;
  101. /* Copy over argument buffer */
  102. ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
  103. if (!ret)
  104. return -EFAULT;
  105. /* Retrieve parameters */
  106. ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
  107. &pe_no, &type, &func, &addr, &mask);
  108. if (ret != 5)
  109. return -EINVAL;
  110. /* Retrieve PE */
  111. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  112. if (!edev)
  113. return -ENOMEM;
  114. edev->phb = hose;
  115. edev->pe_config_addr = pe_no;
  116. pe = eeh_pe_get(edev);
  117. kfree(edev);
  118. if (!pe)
  119. return -ENODEV;
  120. /* Do error injection */
  121. ret = eeh_ops->err_inject(pe, type, func, addr, mask);
  122. return ret < 0 ? ret : count;
  123. }
  124. static const struct file_operations pnv_eeh_ei_fops = {
  125. .open = simple_open,
  126. .llseek = no_llseek,
  127. .write = pnv_eeh_ei_write,
  128. };
  129. static int pnv_eeh_dbgfs_set(void *data, int offset, u64 val)
  130. {
  131. struct pci_controller *hose = data;
  132. struct pnv_phb *phb = hose->private_data;
  133. out_be64(phb->regs + offset, val);
  134. return 0;
  135. }
  136. static int pnv_eeh_dbgfs_get(void *data, int offset, u64 *val)
  137. {
  138. struct pci_controller *hose = data;
  139. struct pnv_phb *phb = hose->private_data;
  140. *val = in_be64(phb->regs + offset);
  141. return 0;
  142. }
  143. static int pnv_eeh_outb_dbgfs_set(void *data, u64 val)
  144. {
  145. return pnv_eeh_dbgfs_set(data, 0xD10, val);
  146. }
  147. static int pnv_eeh_outb_dbgfs_get(void *data, u64 *val)
  148. {
  149. return pnv_eeh_dbgfs_get(data, 0xD10, val);
  150. }
  151. static int pnv_eeh_inbA_dbgfs_set(void *data, u64 val)
  152. {
  153. return pnv_eeh_dbgfs_set(data, 0xD90, val);
  154. }
  155. static int pnv_eeh_inbA_dbgfs_get(void *data, u64 *val)
  156. {
  157. return pnv_eeh_dbgfs_get(data, 0xD90, val);
  158. }
  159. static int pnv_eeh_inbB_dbgfs_set(void *data, u64 val)
  160. {
  161. return pnv_eeh_dbgfs_set(data, 0xE10, val);
  162. }
  163. static int pnv_eeh_inbB_dbgfs_get(void *data, u64 *val)
  164. {
  165. return pnv_eeh_dbgfs_get(data, 0xE10, val);
  166. }
  167. DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_outb_dbgfs_ops, pnv_eeh_outb_dbgfs_get,
  168. pnv_eeh_outb_dbgfs_set, "0x%llx\n");
  169. DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_inbA_dbgfs_ops, pnv_eeh_inbA_dbgfs_get,
  170. pnv_eeh_inbA_dbgfs_set, "0x%llx\n");
  171. DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_inbB_dbgfs_ops, pnv_eeh_inbB_dbgfs_get,
  172. pnv_eeh_inbB_dbgfs_set, "0x%llx\n");
  173. #endif /* CONFIG_DEBUG_FS */
  174. /**
  175. * pnv_eeh_post_init - EEH platform dependent post initialization
  176. *
  177. * EEH platform dependent post initialization on powernv. When
  178. * the function is called, the EEH PEs and devices should have
  179. * been built. If the I/O cache staff has been built, EEH is
  180. * ready to supply service.
  181. */
  182. static int pnv_eeh_post_init(void)
  183. {
  184. struct pci_controller *hose;
  185. struct pnv_phb *phb;
  186. int ret = 0;
  187. /* Register OPAL event notifier */
  188. if (!pnv_eeh_nb_init) {
  189. eeh_event_irq = opal_event_request(ilog2(OPAL_EVENT_PCI_ERROR));
  190. if (eeh_event_irq < 0) {
  191. pr_err("%s: Can't register OPAL event interrupt (%d)\n",
  192. __func__, eeh_event_irq);
  193. return eeh_event_irq;
  194. }
  195. ret = request_irq(eeh_event_irq, pnv_eeh_event,
  196. IRQ_TYPE_LEVEL_HIGH, "opal-eeh", NULL);
  197. if (ret < 0) {
  198. irq_dispose_mapping(eeh_event_irq);
  199. pr_err("%s: Can't request OPAL event interrupt (%d)\n",
  200. __func__, eeh_event_irq);
  201. return ret;
  202. }
  203. pnv_eeh_nb_init = true;
  204. }
  205. if (!eeh_enabled())
  206. disable_irq(eeh_event_irq);
  207. list_for_each_entry(hose, &hose_list, list_node) {
  208. phb = hose->private_data;
  209. /*
  210. * If EEH is enabled, we're going to rely on that.
  211. * Otherwise, we restore to conventional mechanism
  212. * to clear frozen PE during PCI config access.
  213. */
  214. if (eeh_enabled())
  215. phb->flags |= PNV_PHB_FLAG_EEH;
  216. else
  217. phb->flags &= ~PNV_PHB_FLAG_EEH;
  218. /* Create debugfs entries */
  219. #ifdef CONFIG_DEBUG_FS
  220. if (phb->has_dbgfs || !phb->dbgfs)
  221. continue;
  222. phb->has_dbgfs = 1;
  223. debugfs_create_file("err_injct", 0200,
  224. phb->dbgfs, hose,
  225. &pnv_eeh_ei_fops);
  226. debugfs_create_file("err_injct_outbound", 0600,
  227. phb->dbgfs, hose,
  228. &pnv_eeh_outb_dbgfs_ops);
  229. debugfs_create_file("err_injct_inboundA", 0600,
  230. phb->dbgfs, hose,
  231. &pnv_eeh_inbA_dbgfs_ops);
  232. debugfs_create_file("err_injct_inboundB", 0600,
  233. phb->dbgfs, hose,
  234. &pnv_eeh_inbB_dbgfs_ops);
  235. #endif /* CONFIG_DEBUG_FS */
  236. }
  237. return ret;
  238. }
  239. static int pnv_eeh_find_cap(struct pci_dn *pdn, int cap)
  240. {
  241. int pos = PCI_CAPABILITY_LIST;
  242. int cnt = 48; /* Maximal number of capabilities */
  243. u32 status, id;
  244. if (!pdn)
  245. return 0;
  246. /* Check if the device supports capabilities */
  247. pnv_pci_cfg_read(pdn, PCI_STATUS, 2, &status);
  248. if (!(status & PCI_STATUS_CAP_LIST))
  249. return 0;
  250. while (cnt--) {
  251. pnv_pci_cfg_read(pdn, pos, 1, &pos);
  252. if (pos < 0x40)
  253. break;
  254. pos &= ~3;
  255. pnv_pci_cfg_read(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
  256. if (id == 0xff)
  257. break;
  258. /* Found */
  259. if (id == cap)
  260. return pos;
  261. /* Next one */
  262. pos += PCI_CAP_LIST_NEXT;
  263. }
  264. return 0;
  265. }
  266. static int pnv_eeh_find_ecap(struct pci_dn *pdn, int cap)
  267. {
  268. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  269. u32 header;
  270. int pos = 256, ttl = (4096 - 256) / 8;
  271. if (!edev || !edev->pcie_cap)
  272. return 0;
  273. if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
  274. return 0;
  275. else if (!header)
  276. return 0;
  277. while (ttl-- > 0) {
  278. if (PCI_EXT_CAP_ID(header) == cap && pos)
  279. return pos;
  280. pos = PCI_EXT_CAP_NEXT(header);
  281. if (pos < 256)
  282. break;
  283. if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
  284. break;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * pnv_eeh_probe - Do probe on PCI device
  290. * @pdn: PCI device node
  291. * @data: unused
  292. *
  293. * When EEH module is installed during system boot, all PCI devices
  294. * are checked one by one to see if it supports EEH. The function
  295. * is introduced for the purpose. By default, EEH has been enabled
  296. * on all PCI devices. That's to say, we only need do necessary
  297. * initialization on the corresponding eeh device and create PE
  298. * accordingly.
  299. *
  300. * It's notable that's unsafe to retrieve the EEH device through
  301. * the corresponding PCI device. During the PCI device hotplug, which
  302. * was possiblly triggered by EEH core, the binding between EEH device
  303. * and the PCI device isn't built yet.
  304. */
  305. static void *pnv_eeh_probe(struct pci_dn *pdn, void *data)
  306. {
  307. struct pci_controller *hose = pdn->phb;
  308. struct pnv_phb *phb = hose->private_data;
  309. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  310. uint32_t pcie_flags;
  311. int ret;
  312. /*
  313. * When probing the root bridge, which doesn't have any
  314. * subordinate PCI devices. We don't have OF node for
  315. * the root bridge. So it's not reasonable to continue
  316. * the probing.
  317. */
  318. if (!edev || edev->pe)
  319. return NULL;
  320. /* Skip for PCI-ISA bridge */
  321. if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_ISA)
  322. return NULL;
  323. /* Initialize eeh device */
  324. edev->class_code = pdn->class_code;
  325. edev->mode &= 0xFFFFFF00;
  326. edev->pcix_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
  327. edev->pcie_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
  328. edev->aer_cap = pnv_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
  329. if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
  330. edev->mode |= EEH_DEV_BRIDGE;
  331. if (edev->pcie_cap) {
  332. pnv_pci_cfg_read(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
  333. 2, &pcie_flags);
  334. pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
  335. if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
  336. edev->mode |= EEH_DEV_ROOT_PORT;
  337. else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
  338. edev->mode |= EEH_DEV_DS_PORT;
  339. }
  340. }
  341. edev->config_addr = (pdn->busno << 8) | (pdn->devfn);
  342. edev->pe_config_addr = phb->ioda.pe_rmap[edev->config_addr];
  343. /* Create PE */
  344. ret = eeh_add_to_parent_pe(edev);
  345. if (ret) {
  346. pr_warn("%s: Can't add PCI dev %04x:%02x:%02x.%01x to parent PE (%d)\n",
  347. __func__, hose->global_number, pdn->busno,
  348. PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn), ret);
  349. return NULL;
  350. }
  351. /*
  352. * If the PE contains any one of following adapters, the
  353. * PCI config space can't be accessed when dumping EEH log.
  354. * Otherwise, we will run into fenced PHB caused by shortage
  355. * of outbound credits in the adapter. The PCI config access
  356. * should be blocked until PE reset. MMIO access is dropped
  357. * by hardware certainly. In order to drop PCI config requests,
  358. * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
  359. * will be checked in the backend for PE state retrival. If
  360. * the PE becomes frozen for the first time and the flag has
  361. * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
  362. * that PE to block its config space.
  363. *
  364. * Broadcom Austin 4-ports NICs (14e4:1657)
  365. * Broadcom Shiner 4-ports 1G NICs (14e4:168a)
  366. * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
  367. */
  368. if ((pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
  369. pdn->device_id == 0x1657) ||
  370. (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
  371. pdn->device_id == 0x168a) ||
  372. (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
  373. pdn->device_id == 0x168e))
  374. edev->pe->state |= EEH_PE_CFG_RESTRICTED;
  375. /*
  376. * Cache the PE primary bus, which can't be fetched when
  377. * full hotplug is in progress. In that case, all child
  378. * PCI devices of the PE are expected to be removed prior
  379. * to PE reset.
  380. */
  381. if (!(edev->pe->state & EEH_PE_PRI_BUS)) {
  382. edev->pe->bus = pci_find_bus(hose->global_number,
  383. pdn->busno);
  384. if (edev->pe->bus)
  385. edev->pe->state |= EEH_PE_PRI_BUS;
  386. }
  387. /*
  388. * Enable EEH explicitly so that we will do EEH check
  389. * while accessing I/O stuff
  390. */
  391. eeh_add_flag(EEH_ENABLED);
  392. /* Save memory bars */
  393. eeh_save_bars(edev);
  394. return NULL;
  395. }
  396. /**
  397. * pnv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
  398. * @pe: EEH PE
  399. * @option: operation to be issued
  400. *
  401. * The function is used to control the EEH functionality globally.
  402. * Currently, following options are support according to PAPR:
  403. * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
  404. */
  405. static int pnv_eeh_set_option(struct eeh_pe *pe, int option)
  406. {
  407. struct pci_controller *hose = pe->phb;
  408. struct pnv_phb *phb = hose->private_data;
  409. bool freeze_pe = false;
  410. int opt;
  411. s64 rc;
  412. switch (option) {
  413. case EEH_OPT_DISABLE:
  414. return -EPERM;
  415. case EEH_OPT_ENABLE:
  416. return 0;
  417. case EEH_OPT_THAW_MMIO:
  418. opt = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
  419. break;
  420. case EEH_OPT_THAW_DMA:
  421. opt = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
  422. break;
  423. case EEH_OPT_FREEZE_PE:
  424. freeze_pe = true;
  425. opt = OPAL_EEH_ACTION_SET_FREEZE_ALL;
  426. break;
  427. default:
  428. pr_warn("%s: Invalid option %d\n", __func__, option);
  429. return -EINVAL;
  430. }
  431. /* Freeze master and slave PEs if PHB supports compound PEs */
  432. if (freeze_pe) {
  433. if (phb->freeze_pe) {
  434. phb->freeze_pe(phb, pe->addr);
  435. return 0;
  436. }
  437. rc = opal_pci_eeh_freeze_set(phb->opal_id, pe->addr, opt);
  438. if (rc != OPAL_SUCCESS) {
  439. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  440. __func__, rc, phb->hose->global_number,
  441. pe->addr);
  442. return -EIO;
  443. }
  444. return 0;
  445. }
  446. /* Unfreeze master and slave PEs if PHB supports */
  447. if (phb->unfreeze_pe)
  448. return phb->unfreeze_pe(phb, pe->addr, opt);
  449. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe->addr, opt);
  450. if (rc != OPAL_SUCCESS) {
  451. pr_warn("%s: Failure %lld enable %d for PHB#%x-PE#%x\n",
  452. __func__, rc, option, phb->hose->global_number,
  453. pe->addr);
  454. return -EIO;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * pnv_eeh_get_pe_addr - Retrieve PE address
  460. * @pe: EEH PE
  461. *
  462. * Retrieve the PE address according to the given tranditional
  463. * PCI BDF (Bus/Device/Function) address.
  464. */
  465. static int pnv_eeh_get_pe_addr(struct eeh_pe *pe)
  466. {
  467. return pe->addr;
  468. }
  469. static void pnv_eeh_get_phb_diag(struct eeh_pe *pe)
  470. {
  471. struct pnv_phb *phb = pe->phb->private_data;
  472. s64 rc;
  473. rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
  474. PNV_PCI_DIAG_BUF_SIZE);
  475. if (rc != OPAL_SUCCESS)
  476. pr_warn("%s: Failure %lld getting PHB#%x diag-data\n",
  477. __func__, rc, pe->phb->global_number);
  478. }
  479. static int pnv_eeh_get_phb_state(struct eeh_pe *pe)
  480. {
  481. struct pnv_phb *phb = pe->phb->private_data;
  482. u8 fstate;
  483. __be16 pcierr;
  484. s64 rc;
  485. int result = 0;
  486. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  487. pe->addr,
  488. &fstate,
  489. &pcierr,
  490. NULL);
  491. if (rc != OPAL_SUCCESS) {
  492. pr_warn("%s: Failure %lld getting PHB#%x state\n",
  493. __func__, rc, phb->hose->global_number);
  494. return EEH_STATE_NOT_SUPPORT;
  495. }
  496. /*
  497. * Check PHB state. If the PHB is frozen for the
  498. * first time, to dump the PHB diag-data.
  499. */
  500. if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
  501. result = (EEH_STATE_MMIO_ACTIVE |
  502. EEH_STATE_DMA_ACTIVE |
  503. EEH_STATE_MMIO_ENABLED |
  504. EEH_STATE_DMA_ENABLED);
  505. } else if (!(pe->state & EEH_PE_ISOLATED)) {
  506. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  507. pnv_eeh_get_phb_diag(pe);
  508. if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
  509. pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
  510. }
  511. return result;
  512. }
  513. static int pnv_eeh_get_pe_state(struct eeh_pe *pe)
  514. {
  515. struct pnv_phb *phb = pe->phb->private_data;
  516. u8 fstate;
  517. __be16 pcierr;
  518. s64 rc;
  519. int result;
  520. /*
  521. * We don't clobber hardware frozen state until PE
  522. * reset is completed. In order to keep EEH core
  523. * moving forward, we have to return operational
  524. * state during PE reset.
  525. */
  526. if (pe->state & EEH_PE_RESET) {
  527. result = (EEH_STATE_MMIO_ACTIVE |
  528. EEH_STATE_DMA_ACTIVE |
  529. EEH_STATE_MMIO_ENABLED |
  530. EEH_STATE_DMA_ENABLED);
  531. return result;
  532. }
  533. /*
  534. * Fetch PE state from hardware. If the PHB
  535. * supports compound PE, let it handle that.
  536. */
  537. if (phb->get_pe_state) {
  538. fstate = phb->get_pe_state(phb, pe->addr);
  539. } else {
  540. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  541. pe->addr,
  542. &fstate,
  543. &pcierr,
  544. NULL);
  545. if (rc != OPAL_SUCCESS) {
  546. pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
  547. __func__, rc, phb->hose->global_number,
  548. pe->addr);
  549. return EEH_STATE_NOT_SUPPORT;
  550. }
  551. }
  552. /* Figure out state */
  553. switch (fstate) {
  554. case OPAL_EEH_STOPPED_NOT_FROZEN:
  555. result = (EEH_STATE_MMIO_ACTIVE |
  556. EEH_STATE_DMA_ACTIVE |
  557. EEH_STATE_MMIO_ENABLED |
  558. EEH_STATE_DMA_ENABLED);
  559. break;
  560. case OPAL_EEH_STOPPED_MMIO_FREEZE:
  561. result = (EEH_STATE_DMA_ACTIVE |
  562. EEH_STATE_DMA_ENABLED);
  563. break;
  564. case OPAL_EEH_STOPPED_DMA_FREEZE:
  565. result = (EEH_STATE_MMIO_ACTIVE |
  566. EEH_STATE_MMIO_ENABLED);
  567. break;
  568. case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
  569. result = 0;
  570. break;
  571. case OPAL_EEH_STOPPED_RESET:
  572. result = EEH_STATE_RESET_ACTIVE;
  573. break;
  574. case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
  575. result = EEH_STATE_UNAVAILABLE;
  576. break;
  577. case OPAL_EEH_STOPPED_PERM_UNAVAIL:
  578. result = EEH_STATE_NOT_SUPPORT;
  579. break;
  580. default:
  581. result = EEH_STATE_NOT_SUPPORT;
  582. pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
  583. __func__, phb->hose->global_number,
  584. pe->addr, fstate);
  585. }
  586. /*
  587. * If PHB supports compound PE, to freeze all
  588. * slave PEs for consistency.
  589. *
  590. * If the PE is switching to frozen state for the
  591. * first time, to dump the PHB diag-data.
  592. */
  593. if (!(result & EEH_STATE_NOT_SUPPORT) &&
  594. !(result & EEH_STATE_UNAVAILABLE) &&
  595. !(result & EEH_STATE_MMIO_ACTIVE) &&
  596. !(result & EEH_STATE_DMA_ACTIVE) &&
  597. !(pe->state & EEH_PE_ISOLATED)) {
  598. if (phb->freeze_pe)
  599. phb->freeze_pe(phb, pe->addr);
  600. eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
  601. pnv_eeh_get_phb_diag(pe);
  602. if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
  603. pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
  604. }
  605. return result;
  606. }
  607. /**
  608. * pnv_eeh_get_state - Retrieve PE state
  609. * @pe: EEH PE
  610. * @delay: delay while PE state is temporarily unavailable
  611. *
  612. * Retrieve the state of the specified PE. For IODA-compitable
  613. * platform, it should be retrieved from IODA table. Therefore,
  614. * we prefer passing down to hardware implementation to handle
  615. * it.
  616. */
  617. static int pnv_eeh_get_state(struct eeh_pe *pe, int *delay)
  618. {
  619. int ret;
  620. if (pe->type & EEH_PE_PHB)
  621. ret = pnv_eeh_get_phb_state(pe);
  622. else
  623. ret = pnv_eeh_get_pe_state(pe);
  624. if (!delay)
  625. return ret;
  626. /*
  627. * If the PE state is temporarily unavailable,
  628. * to inform the EEH core delay for default
  629. * period (1 second)
  630. */
  631. *delay = 0;
  632. if (ret & EEH_STATE_UNAVAILABLE)
  633. *delay = 1000;
  634. return ret;
  635. }
  636. static s64 pnv_eeh_phb_poll(struct pnv_phb *phb)
  637. {
  638. s64 rc = OPAL_HARDWARE;
  639. while (1) {
  640. rc = opal_pci_poll(phb->opal_id);
  641. if (rc <= 0)
  642. break;
  643. if (system_state < SYSTEM_RUNNING)
  644. udelay(1000 * rc);
  645. else
  646. msleep(rc);
  647. }
  648. return rc;
  649. }
  650. int pnv_eeh_phb_reset(struct pci_controller *hose, int option)
  651. {
  652. struct pnv_phb *phb = hose->private_data;
  653. s64 rc = OPAL_HARDWARE;
  654. pr_debug("%s: Reset PHB#%x, option=%d\n",
  655. __func__, hose->global_number, option);
  656. /* Issue PHB complete reset request */
  657. if (option == EEH_RESET_FUNDAMENTAL ||
  658. option == EEH_RESET_HOT)
  659. rc = opal_pci_reset(phb->opal_id,
  660. OPAL_RESET_PHB_COMPLETE,
  661. OPAL_ASSERT_RESET);
  662. else if (option == EEH_RESET_DEACTIVATE)
  663. rc = opal_pci_reset(phb->opal_id,
  664. OPAL_RESET_PHB_COMPLETE,
  665. OPAL_DEASSERT_RESET);
  666. if (rc < 0)
  667. goto out;
  668. /*
  669. * Poll state of the PHB until the request is done
  670. * successfully. The PHB reset is usually PHB complete
  671. * reset followed by hot reset on root bus. So we also
  672. * need the PCI bus settlement delay.
  673. */
  674. rc = pnv_eeh_phb_poll(phb);
  675. if (option == EEH_RESET_DEACTIVATE) {
  676. if (system_state < SYSTEM_RUNNING)
  677. udelay(1000 * EEH_PE_RST_SETTLE_TIME);
  678. else
  679. msleep(EEH_PE_RST_SETTLE_TIME);
  680. }
  681. out:
  682. if (rc != OPAL_SUCCESS)
  683. return -EIO;
  684. return 0;
  685. }
  686. static int pnv_eeh_root_reset(struct pci_controller *hose, int option)
  687. {
  688. struct pnv_phb *phb = hose->private_data;
  689. s64 rc = OPAL_HARDWARE;
  690. pr_debug("%s: Reset PHB#%x, option=%d\n",
  691. __func__, hose->global_number, option);
  692. /*
  693. * During the reset deassert time, we needn't care
  694. * the reset scope because the firmware does nothing
  695. * for fundamental or hot reset during deassert phase.
  696. */
  697. if (option == EEH_RESET_FUNDAMENTAL)
  698. rc = opal_pci_reset(phb->opal_id,
  699. OPAL_RESET_PCI_FUNDAMENTAL,
  700. OPAL_ASSERT_RESET);
  701. else if (option == EEH_RESET_HOT)
  702. rc = opal_pci_reset(phb->opal_id,
  703. OPAL_RESET_PCI_HOT,
  704. OPAL_ASSERT_RESET);
  705. else if (option == EEH_RESET_DEACTIVATE)
  706. rc = opal_pci_reset(phb->opal_id,
  707. OPAL_RESET_PCI_HOT,
  708. OPAL_DEASSERT_RESET);
  709. if (rc < 0)
  710. goto out;
  711. /* Poll state of the PHB until the request is done */
  712. rc = pnv_eeh_phb_poll(phb);
  713. if (option == EEH_RESET_DEACTIVATE)
  714. msleep(EEH_PE_RST_SETTLE_TIME);
  715. out:
  716. if (rc != OPAL_SUCCESS)
  717. return -EIO;
  718. return 0;
  719. }
  720. static int pnv_eeh_bridge_reset(struct pci_dev *dev, int option)
  721. {
  722. struct pci_dn *pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  723. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  724. int aer = edev ? edev->aer_cap : 0;
  725. u32 ctrl;
  726. pr_debug("%s: Reset PCI bus %04x:%02x with option %d\n",
  727. __func__, pci_domain_nr(dev->bus),
  728. dev->bus->number, option);
  729. switch (option) {
  730. case EEH_RESET_FUNDAMENTAL:
  731. case EEH_RESET_HOT:
  732. /* Don't report linkDown event */
  733. if (aer) {
  734. eeh_ops->read_config(pdn, aer + PCI_ERR_UNCOR_MASK,
  735. 4, &ctrl);
  736. ctrl |= PCI_ERR_UNC_SURPDN;
  737. eeh_ops->write_config(pdn, aer + PCI_ERR_UNCOR_MASK,
  738. 4, ctrl);
  739. }
  740. eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &ctrl);
  741. ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
  742. eeh_ops->write_config(pdn, PCI_BRIDGE_CONTROL, 2, ctrl);
  743. msleep(EEH_PE_RST_HOLD_TIME);
  744. break;
  745. case EEH_RESET_DEACTIVATE:
  746. eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &ctrl);
  747. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  748. eeh_ops->write_config(pdn, PCI_BRIDGE_CONTROL, 2, ctrl);
  749. msleep(EEH_PE_RST_SETTLE_TIME);
  750. /* Continue reporting linkDown event */
  751. if (aer) {
  752. eeh_ops->read_config(pdn, aer + PCI_ERR_UNCOR_MASK,
  753. 4, &ctrl);
  754. ctrl &= ~PCI_ERR_UNC_SURPDN;
  755. eeh_ops->write_config(pdn, aer + PCI_ERR_UNCOR_MASK,
  756. 4, ctrl);
  757. }
  758. break;
  759. }
  760. return 0;
  761. }
  762. void pnv_pci_reset_secondary_bus(struct pci_dev *dev)
  763. {
  764. struct pci_controller *hose;
  765. if (pci_is_root_bus(dev->bus)) {
  766. hose = pci_bus_to_host(dev->bus);
  767. pnv_eeh_root_reset(hose, EEH_RESET_HOT);
  768. pnv_eeh_root_reset(hose, EEH_RESET_DEACTIVATE);
  769. } else {
  770. pnv_eeh_bridge_reset(dev, EEH_RESET_HOT);
  771. pnv_eeh_bridge_reset(dev, EEH_RESET_DEACTIVATE);
  772. }
  773. }
  774. /**
  775. * pnv_eeh_reset - Reset the specified PE
  776. * @pe: EEH PE
  777. * @option: reset option
  778. *
  779. * Do reset on the indicated PE. For PCI bus sensitive PE,
  780. * we need to reset the parent p2p bridge. The PHB has to
  781. * be reinitialized if the p2p bridge is root bridge. For
  782. * PCI device sensitive PE, we will try to reset the device
  783. * through FLR. For now, we don't have OPAL APIs to do HARD
  784. * reset yet, so all reset would be SOFT (HOT) reset.
  785. */
  786. static int pnv_eeh_reset(struct eeh_pe *pe, int option)
  787. {
  788. struct pci_controller *hose = pe->phb;
  789. struct pci_bus *bus;
  790. int ret;
  791. /*
  792. * For PHB reset, we always have complete reset. For those PEs whose
  793. * primary bus derived from root complex (root bus) or root port
  794. * (usually bus#1), we apply hot or fundamental reset on the root port.
  795. * For other PEs, we always have hot reset on the PE primary bus.
  796. *
  797. * Here, we have different design to pHyp, which always clear the
  798. * frozen state during PE reset. However, the good idea here from
  799. * benh is to keep frozen state before we get PE reset done completely
  800. * (until BAR restore). With the frozen state, HW drops illegal IO
  801. * or MMIO access, which can incur recrusive frozen PE during PE
  802. * reset. The side effect is that EEH core has to clear the frozen
  803. * state explicitly after BAR restore.
  804. */
  805. if (pe->type & EEH_PE_PHB) {
  806. ret = pnv_eeh_phb_reset(hose, option);
  807. } else {
  808. struct pnv_phb *phb;
  809. s64 rc;
  810. /*
  811. * The frozen PE might be caused by PAPR error injection
  812. * registers, which are expected to be cleared after hitting
  813. * frozen PE as stated in the hardware spec. Unfortunately,
  814. * that's not true on P7IOC. So we have to clear it manually
  815. * to avoid recursive EEH errors during recovery.
  816. */
  817. phb = hose->private_data;
  818. if (phb->model == PNV_PHB_MODEL_P7IOC &&
  819. (option == EEH_RESET_HOT ||
  820. option == EEH_RESET_FUNDAMENTAL)) {
  821. rc = opal_pci_reset(phb->opal_id,
  822. OPAL_RESET_PHB_ERROR,
  823. OPAL_ASSERT_RESET);
  824. if (rc != OPAL_SUCCESS) {
  825. pr_warn("%s: Failure %lld clearing "
  826. "error injection registers\n",
  827. __func__, rc);
  828. return -EIO;
  829. }
  830. }
  831. bus = eeh_pe_bus_get(pe);
  832. if (!bus) {
  833. pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
  834. __func__, pe->phb->global_number, pe->addr);
  835. return -EIO;
  836. }
  837. if (pci_is_root_bus(bus) ||
  838. pci_is_root_bus(bus->parent))
  839. ret = pnv_eeh_root_reset(hose, option);
  840. else
  841. ret = pnv_eeh_bridge_reset(bus->self, option);
  842. }
  843. return ret;
  844. }
  845. /**
  846. * pnv_eeh_wait_state - Wait for PE state
  847. * @pe: EEH PE
  848. * @max_wait: maximal period in millisecond
  849. *
  850. * Wait for the state of associated PE. It might take some time
  851. * to retrieve the PE's state.
  852. */
  853. static int pnv_eeh_wait_state(struct eeh_pe *pe, int max_wait)
  854. {
  855. int ret;
  856. int mwait;
  857. while (1) {
  858. ret = pnv_eeh_get_state(pe, &mwait);
  859. /*
  860. * If the PE's state is temporarily unavailable,
  861. * we have to wait for the specified time. Otherwise,
  862. * the PE's state will be returned immediately.
  863. */
  864. if (ret != EEH_STATE_UNAVAILABLE)
  865. return ret;
  866. if (max_wait <= 0) {
  867. pr_warn("%s: Timeout getting PE#%x's state (%d)\n",
  868. __func__, pe->addr, max_wait);
  869. return EEH_STATE_NOT_SUPPORT;
  870. }
  871. max_wait -= mwait;
  872. msleep(mwait);
  873. }
  874. return EEH_STATE_NOT_SUPPORT;
  875. }
  876. /**
  877. * pnv_eeh_get_log - Retrieve error log
  878. * @pe: EEH PE
  879. * @severity: temporary or permanent error log
  880. * @drv_log: driver log to be combined with retrieved error log
  881. * @len: length of driver log
  882. *
  883. * Retrieve the temporary or permanent error from the PE.
  884. */
  885. static int pnv_eeh_get_log(struct eeh_pe *pe, int severity,
  886. char *drv_log, unsigned long len)
  887. {
  888. if (!eeh_has_flag(EEH_EARLY_DUMP_LOG))
  889. pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
  890. return 0;
  891. }
  892. /**
  893. * pnv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
  894. * @pe: EEH PE
  895. *
  896. * The function will be called to reconfigure the bridges included
  897. * in the specified PE so that the mulfunctional PE would be recovered
  898. * again.
  899. */
  900. static int pnv_eeh_configure_bridge(struct eeh_pe *pe)
  901. {
  902. return 0;
  903. }
  904. /**
  905. * pnv_pe_err_inject - Inject specified error to the indicated PE
  906. * @pe: the indicated PE
  907. * @type: error type
  908. * @func: specific error type
  909. * @addr: address
  910. * @mask: address mask
  911. *
  912. * The routine is called to inject specified error, which is
  913. * determined by @type and @func, to the indicated PE for
  914. * testing purpose.
  915. */
  916. static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
  917. unsigned long addr, unsigned long mask)
  918. {
  919. struct pci_controller *hose = pe->phb;
  920. struct pnv_phb *phb = hose->private_data;
  921. s64 rc;
  922. if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
  923. type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
  924. pr_warn("%s: Invalid error type %d\n",
  925. __func__, type);
  926. return -ERANGE;
  927. }
  928. if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
  929. func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
  930. pr_warn("%s: Invalid error function %d\n",
  931. __func__, func);
  932. return -ERANGE;
  933. }
  934. /* Firmware supports error injection ? */
  935. if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
  936. pr_warn("%s: Firmware doesn't support error injection\n",
  937. __func__);
  938. return -ENXIO;
  939. }
  940. /* Do error injection */
  941. rc = opal_pci_err_inject(phb->opal_id, pe->addr,
  942. type, func, addr, mask);
  943. if (rc != OPAL_SUCCESS) {
  944. pr_warn("%s: Failure %lld injecting error "
  945. "%d-%d to PHB#%x-PE#%x\n",
  946. __func__, rc, type, func,
  947. hose->global_number, pe->addr);
  948. return -EIO;
  949. }
  950. return 0;
  951. }
  952. static inline bool pnv_eeh_cfg_blocked(struct pci_dn *pdn)
  953. {
  954. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  955. if (!edev || !edev->pe)
  956. return false;
  957. if (edev->pe->state & EEH_PE_CFG_BLOCKED)
  958. return true;
  959. return false;
  960. }
  961. static int pnv_eeh_read_config(struct pci_dn *pdn,
  962. int where, int size, u32 *val)
  963. {
  964. if (!pdn)
  965. return PCIBIOS_DEVICE_NOT_FOUND;
  966. if (pnv_eeh_cfg_blocked(pdn)) {
  967. *val = 0xFFFFFFFF;
  968. return PCIBIOS_SET_FAILED;
  969. }
  970. return pnv_pci_cfg_read(pdn, where, size, val);
  971. }
  972. static int pnv_eeh_write_config(struct pci_dn *pdn,
  973. int where, int size, u32 val)
  974. {
  975. if (!pdn)
  976. return PCIBIOS_DEVICE_NOT_FOUND;
  977. if (pnv_eeh_cfg_blocked(pdn))
  978. return PCIBIOS_SET_FAILED;
  979. return pnv_pci_cfg_write(pdn, where, size, val);
  980. }
  981. static void pnv_eeh_dump_hub_diag_common(struct OpalIoP7IOCErrorData *data)
  982. {
  983. /* GEM */
  984. if (data->gemXfir || data->gemRfir ||
  985. data->gemRirqfir || data->gemMask || data->gemRwof)
  986. pr_info(" GEM: %016llx %016llx %016llx %016llx %016llx\n",
  987. be64_to_cpu(data->gemXfir),
  988. be64_to_cpu(data->gemRfir),
  989. be64_to_cpu(data->gemRirqfir),
  990. be64_to_cpu(data->gemMask),
  991. be64_to_cpu(data->gemRwof));
  992. /* LEM */
  993. if (data->lemFir || data->lemErrMask ||
  994. data->lemAction0 || data->lemAction1 || data->lemWof)
  995. pr_info(" LEM: %016llx %016llx %016llx %016llx %016llx\n",
  996. be64_to_cpu(data->lemFir),
  997. be64_to_cpu(data->lemErrMask),
  998. be64_to_cpu(data->lemAction0),
  999. be64_to_cpu(data->lemAction1),
  1000. be64_to_cpu(data->lemWof));
  1001. }
  1002. static void pnv_eeh_get_and_dump_hub_diag(struct pci_controller *hose)
  1003. {
  1004. struct pnv_phb *phb = hose->private_data;
  1005. struct OpalIoP7IOCErrorData *data = &phb->diag.hub_diag;
  1006. long rc;
  1007. rc = opal_pci_get_hub_diag_data(phb->hub_id, data, sizeof(*data));
  1008. if (rc != OPAL_SUCCESS) {
  1009. pr_warn("%s: Failed to get HUB#%llx diag-data (%ld)\n",
  1010. __func__, phb->hub_id, rc);
  1011. return;
  1012. }
  1013. switch (be16_to_cpu(data->type)) {
  1014. case OPAL_P7IOC_DIAG_TYPE_RGC:
  1015. pr_info("P7IOC diag-data for RGC\n\n");
  1016. pnv_eeh_dump_hub_diag_common(data);
  1017. if (data->rgc.rgcStatus || data->rgc.rgcLdcp)
  1018. pr_info(" RGC: %016llx %016llx\n",
  1019. be64_to_cpu(data->rgc.rgcStatus),
  1020. be64_to_cpu(data->rgc.rgcLdcp));
  1021. break;
  1022. case OPAL_P7IOC_DIAG_TYPE_BI:
  1023. pr_info("P7IOC diag-data for BI %s\n\n",
  1024. data->bi.biDownbound ? "Downbound" : "Upbound");
  1025. pnv_eeh_dump_hub_diag_common(data);
  1026. if (data->bi.biLdcp0 || data->bi.biLdcp1 ||
  1027. data->bi.biLdcp2 || data->bi.biFenceStatus)
  1028. pr_info(" BI: %016llx %016llx %016llx %016llx\n",
  1029. be64_to_cpu(data->bi.biLdcp0),
  1030. be64_to_cpu(data->bi.biLdcp1),
  1031. be64_to_cpu(data->bi.biLdcp2),
  1032. be64_to_cpu(data->bi.biFenceStatus));
  1033. break;
  1034. case OPAL_P7IOC_DIAG_TYPE_CI:
  1035. pr_info("P7IOC diag-data for CI Port %d\n\n",
  1036. data->ci.ciPort);
  1037. pnv_eeh_dump_hub_diag_common(data);
  1038. if (data->ci.ciPortStatus || data->ci.ciPortLdcp)
  1039. pr_info(" CI: %016llx %016llx\n",
  1040. be64_to_cpu(data->ci.ciPortStatus),
  1041. be64_to_cpu(data->ci.ciPortLdcp));
  1042. break;
  1043. case OPAL_P7IOC_DIAG_TYPE_MISC:
  1044. pr_info("P7IOC diag-data for MISC\n\n");
  1045. pnv_eeh_dump_hub_diag_common(data);
  1046. break;
  1047. case OPAL_P7IOC_DIAG_TYPE_I2C:
  1048. pr_info("P7IOC diag-data for I2C\n\n");
  1049. pnv_eeh_dump_hub_diag_common(data);
  1050. break;
  1051. default:
  1052. pr_warn("%s: Invalid type of HUB#%llx diag-data (%d)\n",
  1053. __func__, phb->hub_id, data->type);
  1054. }
  1055. }
  1056. static int pnv_eeh_get_pe(struct pci_controller *hose,
  1057. u16 pe_no, struct eeh_pe **pe)
  1058. {
  1059. struct pnv_phb *phb = hose->private_data;
  1060. struct pnv_ioda_pe *pnv_pe;
  1061. struct eeh_pe *dev_pe;
  1062. struct eeh_dev edev;
  1063. /*
  1064. * If PHB supports compound PE, to fetch
  1065. * the master PE because slave PE is invisible
  1066. * to EEH core.
  1067. */
  1068. pnv_pe = &phb->ioda.pe_array[pe_no];
  1069. if (pnv_pe->flags & PNV_IODA_PE_SLAVE) {
  1070. pnv_pe = pnv_pe->master;
  1071. WARN_ON(!pnv_pe ||
  1072. !(pnv_pe->flags & PNV_IODA_PE_MASTER));
  1073. pe_no = pnv_pe->pe_number;
  1074. }
  1075. /* Find the PE according to PE# */
  1076. memset(&edev, 0, sizeof(struct eeh_dev));
  1077. edev.phb = hose;
  1078. edev.pe_config_addr = pe_no;
  1079. dev_pe = eeh_pe_get(&edev);
  1080. if (!dev_pe)
  1081. return -EEXIST;
  1082. /* Freeze the (compound) PE */
  1083. *pe = dev_pe;
  1084. if (!(dev_pe->state & EEH_PE_ISOLATED))
  1085. phb->freeze_pe(phb, pe_no);
  1086. /*
  1087. * At this point, we're sure the (compound) PE should
  1088. * have been frozen. However, we still need poke until
  1089. * hitting the frozen PE on top level.
  1090. */
  1091. dev_pe = dev_pe->parent;
  1092. while (dev_pe && !(dev_pe->type & EEH_PE_PHB)) {
  1093. int ret;
  1094. int active_flags = (EEH_STATE_MMIO_ACTIVE |
  1095. EEH_STATE_DMA_ACTIVE);
  1096. ret = eeh_ops->get_state(dev_pe, NULL);
  1097. if (ret <= 0 || (ret & active_flags) == active_flags) {
  1098. dev_pe = dev_pe->parent;
  1099. continue;
  1100. }
  1101. /* Frozen parent PE */
  1102. *pe = dev_pe;
  1103. if (!(dev_pe->state & EEH_PE_ISOLATED))
  1104. phb->freeze_pe(phb, dev_pe->addr);
  1105. /* Next one */
  1106. dev_pe = dev_pe->parent;
  1107. }
  1108. return 0;
  1109. }
  1110. /**
  1111. * pnv_eeh_next_error - Retrieve next EEH error to handle
  1112. * @pe: Affected PE
  1113. *
  1114. * The function is expected to be called by EEH core while it gets
  1115. * special EEH event (without binding PE). The function calls to
  1116. * OPAL APIs for next error to handle. The informational error is
  1117. * handled internally by platform. However, the dead IOC, dead PHB,
  1118. * fenced PHB and frozen PE should be handled by EEH core eventually.
  1119. */
  1120. static int pnv_eeh_next_error(struct eeh_pe **pe)
  1121. {
  1122. struct pci_controller *hose;
  1123. struct pnv_phb *phb;
  1124. struct eeh_pe *phb_pe, *parent_pe;
  1125. __be64 frozen_pe_no;
  1126. __be16 err_type, severity;
  1127. int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
  1128. long rc;
  1129. int state, ret = EEH_NEXT_ERR_NONE;
  1130. /*
  1131. * While running here, it's safe to purge the event queue. The
  1132. * event should still be masked.
  1133. */
  1134. eeh_remove_event(NULL, false);
  1135. list_for_each_entry(hose, &hose_list, list_node) {
  1136. /*
  1137. * If the subordinate PCI buses of the PHB has been
  1138. * removed or is exactly under error recovery, we
  1139. * needn't take care of it any more.
  1140. */
  1141. phb = hose->private_data;
  1142. phb_pe = eeh_phb_pe_get(hose);
  1143. if (!phb_pe || (phb_pe->state & EEH_PE_ISOLATED))
  1144. continue;
  1145. rc = opal_pci_next_error(phb->opal_id,
  1146. &frozen_pe_no, &err_type, &severity);
  1147. if (rc != OPAL_SUCCESS) {
  1148. pr_devel("%s: Invalid return value on "
  1149. "PHB#%x (0x%lx) from opal_pci_next_error",
  1150. __func__, hose->global_number, rc);
  1151. continue;
  1152. }
  1153. /* If the PHB doesn't have error, stop processing */
  1154. if (be16_to_cpu(err_type) == OPAL_EEH_NO_ERROR ||
  1155. be16_to_cpu(severity) == OPAL_EEH_SEV_NO_ERROR) {
  1156. pr_devel("%s: No error found on PHB#%x\n",
  1157. __func__, hose->global_number);
  1158. continue;
  1159. }
  1160. /*
  1161. * Processing the error. We're expecting the error with
  1162. * highest priority reported upon multiple errors on the
  1163. * specific PHB.
  1164. */
  1165. pr_devel("%s: Error (%d, %d, %llu) on PHB#%x\n",
  1166. __func__, be16_to_cpu(err_type),
  1167. be16_to_cpu(severity), be64_to_cpu(frozen_pe_no),
  1168. hose->global_number);
  1169. switch (be16_to_cpu(err_type)) {
  1170. case OPAL_EEH_IOC_ERROR:
  1171. if (be16_to_cpu(severity) == OPAL_EEH_SEV_IOC_DEAD) {
  1172. pr_err("EEH: dead IOC detected\n");
  1173. ret = EEH_NEXT_ERR_DEAD_IOC;
  1174. } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
  1175. pr_info("EEH: IOC informative error "
  1176. "detected\n");
  1177. pnv_eeh_get_and_dump_hub_diag(hose);
  1178. ret = EEH_NEXT_ERR_NONE;
  1179. }
  1180. break;
  1181. case OPAL_EEH_PHB_ERROR:
  1182. if (be16_to_cpu(severity) == OPAL_EEH_SEV_PHB_DEAD) {
  1183. *pe = phb_pe;
  1184. pr_err("EEH: dead PHB#%x detected, "
  1185. "location: %s\n",
  1186. hose->global_number,
  1187. eeh_pe_loc_get(phb_pe));
  1188. ret = EEH_NEXT_ERR_DEAD_PHB;
  1189. } else if (be16_to_cpu(severity) ==
  1190. OPAL_EEH_SEV_PHB_FENCED) {
  1191. *pe = phb_pe;
  1192. pr_err("EEH: Fenced PHB#%x detected, "
  1193. "location: %s\n",
  1194. hose->global_number,
  1195. eeh_pe_loc_get(phb_pe));
  1196. ret = EEH_NEXT_ERR_FENCED_PHB;
  1197. } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
  1198. pr_info("EEH: PHB#%x informative error "
  1199. "detected, location: %s\n",
  1200. hose->global_number,
  1201. eeh_pe_loc_get(phb_pe));
  1202. pnv_eeh_get_phb_diag(phb_pe);
  1203. pnv_pci_dump_phb_diag_data(hose, phb_pe->data);
  1204. ret = EEH_NEXT_ERR_NONE;
  1205. }
  1206. break;
  1207. case OPAL_EEH_PE_ERROR:
  1208. /*
  1209. * If we can't find the corresponding PE, we
  1210. * just try to unfreeze.
  1211. */
  1212. if (pnv_eeh_get_pe(hose,
  1213. be64_to_cpu(frozen_pe_no), pe)) {
  1214. pr_info("EEH: Clear non-existing PHB#%x-PE#%llx\n",
  1215. hose->global_number, be64_to_cpu(frozen_pe_no));
  1216. pr_info("EEH: PHB location: %s\n",
  1217. eeh_pe_loc_get(phb_pe));
  1218. /* Dump PHB diag-data */
  1219. rc = opal_pci_get_phb_diag_data2(phb->opal_id,
  1220. phb->diag.blob, PNV_PCI_DIAG_BUF_SIZE);
  1221. if (rc == OPAL_SUCCESS)
  1222. pnv_pci_dump_phb_diag_data(hose,
  1223. phb->diag.blob);
  1224. /* Try best to clear it */
  1225. opal_pci_eeh_freeze_clear(phb->opal_id,
  1226. be64_to_cpu(frozen_pe_no),
  1227. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  1228. ret = EEH_NEXT_ERR_NONE;
  1229. } else if ((*pe)->state & EEH_PE_ISOLATED ||
  1230. eeh_pe_passed(*pe)) {
  1231. ret = EEH_NEXT_ERR_NONE;
  1232. } else {
  1233. pr_err("EEH: Frozen PE#%x "
  1234. "on PHB#%x detected\n",
  1235. (*pe)->addr,
  1236. (*pe)->phb->global_number);
  1237. pr_err("EEH: PE location: %s, "
  1238. "PHB location: %s\n",
  1239. eeh_pe_loc_get(*pe),
  1240. eeh_pe_loc_get(phb_pe));
  1241. ret = EEH_NEXT_ERR_FROZEN_PE;
  1242. }
  1243. break;
  1244. default:
  1245. pr_warn("%s: Unexpected error type %d\n",
  1246. __func__, be16_to_cpu(err_type));
  1247. }
  1248. /*
  1249. * EEH core will try recover from fenced PHB or
  1250. * frozen PE. In the time for frozen PE, EEH core
  1251. * enable IO path for that before collecting logs,
  1252. * but it ruins the site. So we have to dump the
  1253. * log in advance here.
  1254. */
  1255. if ((ret == EEH_NEXT_ERR_FROZEN_PE ||
  1256. ret == EEH_NEXT_ERR_FENCED_PHB) &&
  1257. !((*pe)->state & EEH_PE_ISOLATED)) {
  1258. eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
  1259. pnv_eeh_get_phb_diag(*pe);
  1260. if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
  1261. pnv_pci_dump_phb_diag_data((*pe)->phb,
  1262. (*pe)->data);
  1263. }
  1264. /*
  1265. * We probably have the frozen parent PE out there and
  1266. * we need have to handle frozen parent PE firstly.
  1267. */
  1268. if (ret == EEH_NEXT_ERR_FROZEN_PE) {
  1269. parent_pe = (*pe)->parent;
  1270. while (parent_pe) {
  1271. /* Hit the ceiling ? */
  1272. if (parent_pe->type & EEH_PE_PHB)
  1273. break;
  1274. /* Frozen parent PE ? */
  1275. state = eeh_ops->get_state(parent_pe, NULL);
  1276. if (state > 0 &&
  1277. (state & active_flags) != active_flags)
  1278. *pe = parent_pe;
  1279. /* Next parent level */
  1280. parent_pe = parent_pe->parent;
  1281. }
  1282. /* We possibly migrate to another PE */
  1283. eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
  1284. }
  1285. /*
  1286. * If we have no errors on the specific PHB or only
  1287. * informative error there, we continue poking it.
  1288. * Otherwise, we need actions to be taken by upper
  1289. * layer.
  1290. */
  1291. if (ret > EEH_NEXT_ERR_INF)
  1292. break;
  1293. }
  1294. /* Unmask the event */
  1295. if (ret == EEH_NEXT_ERR_NONE && eeh_enabled())
  1296. enable_irq(eeh_event_irq);
  1297. return ret;
  1298. }
  1299. static int pnv_eeh_restore_config(struct pci_dn *pdn)
  1300. {
  1301. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  1302. struct pnv_phb *phb;
  1303. s64 ret;
  1304. if (!edev)
  1305. return -EEXIST;
  1306. phb = edev->phb->private_data;
  1307. ret = opal_pci_reinit(phb->opal_id,
  1308. OPAL_REINIT_PCI_DEV, edev->config_addr);
  1309. if (ret) {
  1310. pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
  1311. __func__, edev->config_addr, ret);
  1312. return -EIO;
  1313. }
  1314. return 0;
  1315. }
  1316. static struct eeh_ops pnv_eeh_ops = {
  1317. .name = "powernv",
  1318. .init = pnv_eeh_init,
  1319. .post_init = pnv_eeh_post_init,
  1320. .probe = pnv_eeh_probe,
  1321. .set_option = pnv_eeh_set_option,
  1322. .get_pe_addr = pnv_eeh_get_pe_addr,
  1323. .get_state = pnv_eeh_get_state,
  1324. .reset = pnv_eeh_reset,
  1325. .wait_state = pnv_eeh_wait_state,
  1326. .get_log = pnv_eeh_get_log,
  1327. .configure_bridge = pnv_eeh_configure_bridge,
  1328. .err_inject = pnv_eeh_err_inject,
  1329. .read_config = pnv_eeh_read_config,
  1330. .write_config = pnv_eeh_write_config,
  1331. .next_error = pnv_eeh_next_error,
  1332. .restore_config = pnv_eeh_restore_config
  1333. };
  1334. /**
  1335. * eeh_powernv_init - Register platform dependent EEH operations
  1336. *
  1337. * EEH initialization on powernv platform. This function should be
  1338. * called before any EEH related functions.
  1339. */
  1340. static int __init eeh_powernv_init(void)
  1341. {
  1342. int ret = -EINVAL;
  1343. eeh_set_pe_aux_size(PNV_PCI_DIAG_BUF_SIZE);
  1344. ret = eeh_ops_register(&pnv_eeh_ops);
  1345. if (!ret)
  1346. pr_info("EEH: PowerNV platform initialized\n");
  1347. else
  1348. pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
  1349. return ret;
  1350. }
  1351. machine_early_initcall(powernv, eeh_powernv_init);