eeh-powernv.c 40 KB

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