eeh-powernv.c 44 KB

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