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