eeh-powernv.c 44 KB

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