1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * The file intends to implement the platform dependent EEH operations on pseries.
4  * Actually, the pseries platform is built based on RTAS heavily. That means the
5  * pseries platform dependent EEH operations will be built on RTAS calls. The functions
6  * are derived from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
7  * been done.
8  *
9  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
10  * Copyright IBM Corporation 2001, 2005, 2006
11  * Copyright Dave Engebretsen & Todd Inglett 2001
12  * Copyright Linas Vepstas 2005, 2006
13  */
14 
15 #include <linux/atomic.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/of.h>
21 #include <linux/pci.h>
22 #include <linux/proc_fs.h>
23 #include <linux/rbtree.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/spinlock.h>
27 #include <linux/crash_dump.h>
28 
29 #include <asm/eeh.h>
30 #include <asm/eeh_event.h>
31 #include <asm/io.h>
32 #include <asm/machdep.h>
33 #include <asm/ppc-pci.h>
34 #include <asm/rtas.h>
35 
36 static int pseries_eeh_get_pe_addr(struct pci_dn *pdn);
37 
38 /* RTAS tokens */
39 static int ibm_set_eeh_option;
40 static int ibm_set_slot_reset;
41 static int ibm_read_slot_reset_state;
42 static int ibm_read_slot_reset_state2;
43 static int ibm_slot_error_detail;
44 static int ibm_get_config_addr_info;
45 static int ibm_get_config_addr_info2;
46 static int ibm_configure_pe;
47 
48 void pseries_pcibios_bus_add_device(struct pci_dev *pdev)
49 {
50 	struct pci_dn *pdn = pci_get_pdn(pdev);
51 
52 	if (eeh_has_flag(EEH_FORCE_DISABLED))
53 		return;
54 
55 	dev_dbg(&pdev->dev, "EEH: Setting up device\n");
56 #ifdef CONFIG_PCI_IOV
57 	if (pdev->is_virtfn) {
58 		pdn->device_id  =  pdev->device;
59 		pdn->vendor_id  =  pdev->vendor;
60 		pdn->class_code =  pdev->class;
61 		/*
62 		 * Last allow unfreeze return code used for retrieval
63 		 * by user space in eeh-sysfs to show the last command
64 		 * completion from platform.
65 		 */
66 		pdn->last_allow_rc =  0;
67 	}
68 #endif
69 	pseries_eeh_init_edev(pdn);
70 #ifdef CONFIG_PCI_IOV
71 	if (pdev->is_virtfn) {
72 		/*
73 		 * FIXME: This really should be handled by choosing the right
74 		 *        parent PE in in pseries_eeh_init_edev().
75 		 */
76 		struct eeh_pe *physfn_pe = pci_dev_to_eeh_dev(pdev->physfn)->pe;
77 		struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
78 
79 		edev->pe_config_addr =  (pdn->busno << 16) | (pdn->devfn << 8);
80 		eeh_pe_tree_remove(edev); /* Remove as it is adding to bus pe */
81 		eeh_pe_tree_insert(edev, physfn_pe);   /* Add as VF PE type */
82 	}
83 #endif
84 	eeh_probe_device(pdev);
85 }
86 
87 
88 /**
89  * pseries_eeh_get_config_addr - Retrieve config address
90  *
91  * Retrieve the assocated config address. Actually, there're 2 RTAS
92  * function calls dedicated for the purpose. We need implement
93  * it through the new function and then the old one. Besides,
94  * you should make sure the config address is figured out from
95  * FDT node before calling the function.
96  *
97  * It's notable that zero'ed return value means invalid PE config
98  * address.
99  */
100 static int pseries_eeh_get_config_addr(struct pci_controller *phb, int config_addr)
101 {
102 	int ret = 0;
103 	int rets[3];
104 
105 	if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
106 		/*
107 		 * First of all, we need to make sure there has one PE
108 		 * associated with the device. Otherwise, PE address is
109 		 * meaningless.
110 		 */
111 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
112 				config_addr, BUID_HI(phb->buid),
113 				BUID_LO(phb->buid), 1);
114 		if (ret || (rets[0] == 0))
115 			return 0;
116 
117 		/* Retrieve the associated PE config address */
118 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
119 				config_addr, BUID_HI(phb->buid),
120 				BUID_LO(phb->buid), 0);
121 		if (ret) {
122 			pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
123 				__func__, phb->global_number, config_addr);
124 			return 0;
125 		}
126 
127 		return rets[0];
128 	}
129 
130 	if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
131 		ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
132 				config_addr, BUID_HI(phb->buid),
133 				BUID_LO(phb->buid), 0);
134 		if (ret) {
135 			pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
136 				__func__, phb->global_number, config_addr);
137 			return 0;
138 		}
139 
140 		return rets[0];
141 	}
142 
143 	return ret;
144 }
145 
146 /**
147  * pseries_eeh_phb_reset - Reset the specified PHB
148  * @phb: PCI controller
149  * @config_adddr: the associated config address
150  * @option: reset option
151  *
152  * Reset the specified PHB/PE
153  */
154 static int pseries_eeh_phb_reset(struct pci_controller *phb, int config_addr, int option)
155 {
156 	int ret;
157 
158 	/* Reset PE through RTAS call */
159 	ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
160 			config_addr, BUID_HI(phb->buid),
161 			BUID_LO(phb->buid), option);
162 
163 	/* If fundamental-reset not supported, try hot-reset */
164 	if (option == EEH_RESET_FUNDAMENTAL &&
165 	    ret == -8) {
166 		option = EEH_RESET_HOT;
167 		ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
168 				config_addr, BUID_HI(phb->buid),
169 				BUID_LO(phb->buid), option);
170 	}
171 
172 	/* We need reset hold or settlement delay */
173 	if (option == EEH_RESET_FUNDAMENTAL ||
174 	    option == EEH_RESET_HOT)
175 		msleep(EEH_PE_RST_HOLD_TIME);
176 	else
177 		msleep(EEH_PE_RST_SETTLE_TIME);
178 
179 	return ret;
180 }
181 
182 /**
183  * pseries_eeh_phb_configure_bridge - Configure PCI bridges in the indicated PE
184  * @phb: PCI controller
185  * @config_adddr: the associated config address
186  *
187  * The function will be called to reconfigure the bridges included
188  * in the specified PE so that the mulfunctional PE would be recovered
189  * again.
190  */
191 static int pseries_eeh_phb_configure_bridge(struct pci_controller *phb, int config_addr)
192 {
193 	int ret;
194 	/* Waiting 0.2s maximum before skipping configuration */
195 	int max_wait = 200;
196 
197 	while (max_wait > 0) {
198 		ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
199 				config_addr, BUID_HI(phb->buid),
200 				BUID_LO(phb->buid));
201 
202 		if (!ret)
203 			return ret;
204 		if (ret < 0)
205 			break;
206 
207 		/*
208 		 * If RTAS returns a delay value that's above 100ms, cut it
209 		 * down to 100ms in case firmware made a mistake.  For more
210 		 * on how these delay values work see rtas_busy_delay_time
211 		 */
212 		if (ret > RTAS_EXTENDED_DELAY_MIN+2 &&
213 		    ret <= RTAS_EXTENDED_DELAY_MAX)
214 			ret = RTAS_EXTENDED_DELAY_MIN+2;
215 
216 		max_wait -= rtas_busy_delay_time(ret);
217 
218 		if (max_wait < 0)
219 			break;
220 
221 		rtas_busy_delay(ret);
222 	}
223 
224 	pr_warn("%s: Unable to configure bridge PHB#%x-PE#%x (%d)\n",
225 		__func__, phb->global_number, config_addr, ret);
226 	/* PAPR defines -3 as "Parameter Error" for this function: */
227 	if (ret == -3)
228 		return -EINVAL;
229 	else
230 		return -EIO;
231 }
232 
233 /*
234  * Buffer for reporting slot-error-detail rtas calls. Its here
235  * in BSS, and not dynamically alloced, so that it ends up in
236  * RMO where RTAS can access it.
237  */
238 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
239 static DEFINE_SPINLOCK(slot_errbuf_lock);
240 static int eeh_error_buf_size;
241 
242 /**
243  * pseries_eeh_init - EEH platform dependent initialization
244  *
245  * EEH platform dependent initialization on pseries.
246  */
247 static int pseries_eeh_init(void)
248 {
249 	struct pci_controller *phb;
250 	struct pci_dn *pdn;
251 	int addr, config_addr;
252 
253 	/* figure out EEH RTAS function call tokens */
254 	ibm_set_eeh_option		= rtas_token("ibm,set-eeh-option");
255 	ibm_set_slot_reset		= rtas_token("ibm,set-slot-reset");
256 	ibm_read_slot_reset_state2	= rtas_token("ibm,read-slot-reset-state2");
257 	ibm_read_slot_reset_state	= rtas_token("ibm,read-slot-reset-state");
258 	ibm_slot_error_detail		= rtas_token("ibm,slot-error-detail");
259 	ibm_get_config_addr_info2	= rtas_token("ibm,get-config-addr-info2");
260 	ibm_get_config_addr_info	= rtas_token("ibm,get-config-addr-info");
261 	ibm_configure_pe		= rtas_token("ibm,configure-pe");
262 
263 	/*
264 	 * ibm,configure-pe and ibm,configure-bridge have the same semantics,
265 	 * however ibm,configure-pe can be faster.  If we can't find
266 	 * ibm,configure-pe then fall back to using ibm,configure-bridge.
267 	 */
268 	if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE)
269 		ibm_configure_pe 	= rtas_token("ibm,configure-bridge");
270 
271 	/*
272 	 * Necessary sanity check. We needn't check "get-config-addr-info"
273 	 * and its variant since the old firmware probably support address
274 	 * of domain/bus/slot/function for EEH RTAS operations.
275 	 */
276 	if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE		||
277 	    ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE		||
278 	    (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
279 	     ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE)	||
280 	    ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE	||
281 	    ibm_configure_pe == RTAS_UNKNOWN_SERVICE) {
282 		pr_info("EEH functionality not supported\n");
283 		return -EINVAL;
284 	}
285 
286 	/* Initialize error log lock and size */
287 	spin_lock_init(&slot_errbuf_lock);
288 	eeh_error_buf_size = rtas_token("rtas-error-log-max");
289 	if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
290 		pr_info("%s: unknown EEH error log size\n",
291 			__func__);
292 		eeh_error_buf_size = 1024;
293 	} else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
294 		pr_info("%s: EEH error log size %d exceeds the maximal %d\n",
295 			__func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
296 		eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
297 	}
298 
299 	/* Set EEH probe mode */
300 	eeh_add_flag(EEH_PROBE_MODE_DEVTREE | EEH_ENABLE_IO_FOR_LOG);
301 
302 	/* Set EEH machine dependent code */
303 	ppc_md.pcibios_bus_add_device = pseries_pcibios_bus_add_device;
304 
305 	if (is_kdump_kernel() || reset_devices) {
306 		pr_info("Issue PHB reset ...\n");
307 		list_for_each_entry(phb, &hose_list, list_node) {
308 			pdn = list_first_entry(&PCI_DN(phb->dn)->child_list, struct pci_dn, list);
309 			addr = (pdn->busno << 16) | (pdn->devfn << 8);
310 			config_addr = pseries_eeh_get_config_addr(phb, addr);
311 			/* invalid PE config addr */
312 			if (config_addr == 0)
313 				continue;
314 
315 			pseries_eeh_phb_reset(phb, config_addr, EEH_RESET_FUNDAMENTAL);
316 			pseries_eeh_phb_reset(phb, config_addr, EEH_RESET_DEACTIVATE);
317 			pseries_eeh_phb_configure_bridge(phb, config_addr);
318 		}
319 	}
320 
321 	return 0;
322 }
323 
324 static int pseries_eeh_cap_start(struct pci_dn *pdn)
325 {
326 	u32 status;
327 
328 	if (!pdn)
329 		return 0;
330 
331 	rtas_read_config(pdn, PCI_STATUS, 2, &status);
332 	if (!(status & PCI_STATUS_CAP_LIST))
333 		return 0;
334 
335 	return PCI_CAPABILITY_LIST;
336 }
337 
338 
339 static int pseries_eeh_find_cap(struct pci_dn *pdn, int cap)
340 {
341 	int pos = pseries_eeh_cap_start(pdn);
342 	int cnt = 48;	/* Maximal number of capabilities */
343 	u32 id;
344 
345 	if (!pos)
346 		return 0;
347 
348         while (cnt--) {
349 		rtas_read_config(pdn, pos, 1, &pos);
350 		if (pos < 0x40)
351 			break;
352 		pos &= ~3;
353 		rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
354 		if (id == 0xff)
355 			break;
356 		if (id == cap)
357 			return pos;
358 		pos += PCI_CAP_LIST_NEXT;
359 	}
360 
361 	return 0;
362 }
363 
364 static int pseries_eeh_find_ecap(struct pci_dn *pdn, int cap)
365 {
366 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
367 	u32 header;
368 	int pos = 256;
369 	int ttl = (4096 - 256) / 8;
370 
371 	if (!edev || !edev->pcie_cap)
372 		return 0;
373 	if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
374 		return 0;
375 	else if (!header)
376 		return 0;
377 
378 	while (ttl-- > 0) {
379 		if (PCI_EXT_CAP_ID(header) == cap && pos)
380 			return pos;
381 
382 		pos = PCI_EXT_CAP_NEXT(header);
383 		if (pos < 256)
384 			break;
385 
386 		if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
387 			break;
388 	}
389 
390 	return 0;
391 }
392 
393 /**
394  * pseries_eeh_pe_get_parent - Retrieve the parent PE
395  * @edev: EEH device
396  *
397  * The whole PEs existing in the system are organized as hierarchy
398  * tree. The function is used to retrieve the parent PE according
399  * to the parent EEH device.
400  */
401 static struct eeh_pe *pseries_eeh_pe_get_parent(struct eeh_dev *edev)
402 {
403 	struct eeh_dev *parent;
404 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
405 
406 	/*
407 	 * It might have the case for the indirect parent
408 	 * EEH device already having associated PE, but
409 	 * the direct parent EEH device doesn't have yet.
410 	 */
411 	if (edev->physfn)
412 		pdn = pci_get_pdn(edev->physfn);
413 	else
414 		pdn = pdn ? pdn->parent : NULL;
415 	while (pdn) {
416 		/* We're poking out of PCI territory */
417 		parent = pdn_to_eeh_dev(pdn);
418 		if (!parent)
419 			return NULL;
420 
421 		if (parent->pe)
422 			return parent->pe;
423 
424 		pdn = pdn->parent;
425 	}
426 
427 	return NULL;
428 }
429 
430 /**
431  * pseries_eeh_init_edev - initialise the eeh_dev and eeh_pe for a pci_dn
432  *
433  * @pdn: PCI device node
434  *
435  * When we discover a new PCI device via the device-tree we create a
436  * corresponding pci_dn and we allocate, but don't initialise, an eeh_dev.
437  * This function takes care of the initialisation and inserts the eeh_dev
438  * into the correct eeh_pe. If no eeh_pe exists we'll allocate one.
439  */
440 void pseries_eeh_init_edev(struct pci_dn *pdn)
441 {
442 	struct eeh_dev *edev;
443 	struct eeh_pe pe;
444 	u32 pcie_flags;
445 	int enable = 0;
446 	int ret;
447 
448 	if (WARN_ON_ONCE(!eeh_has_flag(EEH_PROBE_MODE_DEVTREE)))
449 		return;
450 
451 	/*
452 	 * Find the eeh_dev for this pdn. The storage for the eeh_dev was
453 	 * allocated at the same time as the pci_dn.
454 	 *
455 	 * XXX: We should probably re-visit that.
456 	 */
457 	edev = pdn_to_eeh_dev(pdn);
458 	if (!edev)
459 		return;
460 
461 	/*
462 	 * If ->pe is set then we've already probed this device. We hit
463 	 * this path when a pci_dev is removed and rescanned while recovering
464 	 * a PE (i.e. for devices where the driver doesn't support error
465 	 * recovery).
466 	 */
467 	if (edev->pe)
468 		return;
469 
470 	/* Check class/vendor/device IDs */
471 	if (!pdn->vendor_id || !pdn->device_id || !pdn->class_code)
472 		return;
473 
474 	/* Skip for PCI-ISA bridge */
475         if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_ISA)
476 		return;
477 
478 	eeh_edev_dbg(edev, "Probing device\n");
479 
480 	/*
481 	 * Update class code and mode of eeh device. We need
482 	 * correctly reflects that current device is root port
483 	 * or PCIe switch downstream port.
484 	 */
485 	edev->pcix_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
486 	edev->pcie_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
487 	edev->aer_cap = pseries_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
488 	edev->mode &= 0xFFFFFF00;
489 	if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
490 		edev->mode |= EEH_DEV_BRIDGE;
491 		if (edev->pcie_cap) {
492 			rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
493 					 2, &pcie_flags);
494 			pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
495 			if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
496 				edev->mode |= EEH_DEV_ROOT_PORT;
497 			else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
498 				edev->mode |= EEH_DEV_DS_PORT;
499 		}
500 	}
501 
502 	/* Initialize the fake PE */
503 	memset(&pe, 0, sizeof(struct eeh_pe));
504 	pe.phb = pdn->phb;
505 	pe.config_addr = (pdn->busno << 16) | (pdn->devfn << 8);
506 
507 	/* Enable EEH on the device */
508 	eeh_edev_dbg(edev, "Enabling EEH on device\n");
509 	ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
510 	if (ret) {
511 		eeh_edev_dbg(edev, "EEH failed to enable on device (code %d)\n", ret);
512 	} else {
513 		struct eeh_pe *parent;
514 
515 		/* Retrieve PE address */
516 		edev->pe_config_addr = pseries_eeh_get_pe_addr(pdn);
517 		pe.addr = edev->pe_config_addr;
518 
519 		/* Some older systems (Power4) allow the ibm,set-eeh-option
520 		 * call to succeed even on nodes where EEH is not supported.
521 		 * Verify support explicitly.
522 		 */
523 		ret = eeh_ops->get_state(&pe, NULL);
524 		if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
525 			enable = 1;
526 
527 		/*
528 		 * This device doesn't support EEH, but it may have an
529 		 * EEH parent. In this case any error on the device will
530 		 * freeze the PE of it's upstream bridge, so added it to
531 		 * the upstream PE.
532 		 */
533 		parent = pseries_eeh_pe_get_parent(edev);
534 		if (parent && !enable)
535 			edev->pe_config_addr = parent->addr;
536 
537 		if (enable || parent) {
538 			eeh_add_flag(EEH_ENABLED);
539 			eeh_pe_tree_insert(edev, parent);
540 		}
541 		eeh_edev_dbg(edev, "EEH is %s on device (code %d)\n",
542 			     (enable ? "enabled" : "unsupported"), ret);
543 	}
544 
545 	/* Save memory bars */
546 	eeh_save_bars(edev);
547 }
548 
549 static struct eeh_dev *pseries_eeh_probe(struct pci_dev *pdev)
550 {
551 	struct eeh_dev *edev;
552 	struct pci_dn *pdn;
553 
554 	pdn = pci_get_pdn_by_devfn(pdev->bus, pdev->devfn);
555 	if (!pdn)
556 		return NULL;
557 
558 	/*
559 	 * If the system supports EEH on this device then the eeh_dev was
560 	 * configured and inserted into a PE in pseries_eeh_init_edev()
561 	 */
562 	edev = pdn_to_eeh_dev(pdn);
563 	if (!edev || !edev->pe)
564 		return NULL;
565 
566 	return edev;
567 }
568 
569 /**
570  * pseries_eeh_init_edev_recursive - Enable EEH for the indicated device
571  * @pdn: PCI device node
572  *
573  * This routine must be used to perform EEH initialization for the
574  * indicated PCI device that was added after system boot (e.g.
575  * hotplug, dlpar).
576  */
577 void pseries_eeh_init_edev_recursive(struct pci_dn *pdn)
578 {
579 	struct pci_dn *n;
580 
581 	if (!pdn)
582 		return;
583 
584 	list_for_each_entry(n, &pdn->child_list, list)
585 		pseries_eeh_init_edev_recursive(n);
586 
587 	pseries_eeh_init_edev(pdn);
588 }
589 EXPORT_SYMBOL_GPL(pseries_eeh_init_edev_recursive);
590 
591 /**
592  * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
593  * @pe: EEH PE
594  * @option: operation to be issued
595  *
596  * The function is used to control the EEH functionality globally.
597  * Currently, following options are support according to PAPR:
598  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
599  */
600 static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
601 {
602 	int ret = 0;
603 	int config_addr;
604 
605 	/*
606 	 * When we're enabling or disabling EEH functioality on
607 	 * the particular PE, the PE config address is possibly
608 	 * unavailable. Therefore, we have to figure it out from
609 	 * the FDT node.
610 	 */
611 	switch (option) {
612 	case EEH_OPT_DISABLE:
613 	case EEH_OPT_ENABLE:
614 	case EEH_OPT_THAW_MMIO:
615 	case EEH_OPT_THAW_DMA:
616 		config_addr = pe->config_addr;
617 		if (pe->addr)
618 			config_addr = pe->addr;
619 		break;
620 	case EEH_OPT_FREEZE_PE:
621 		/* Not support */
622 		return 0;
623 	default:
624 		pr_err("%s: Invalid option %d\n",
625 			__func__, option);
626 		return -EINVAL;
627 	}
628 
629 	ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
630 			config_addr, BUID_HI(pe->phb->buid),
631 			BUID_LO(pe->phb->buid), option);
632 
633 	return ret;
634 }
635 
636 /**
637  * pseries_eeh_get_pe_addr - Retrieve PE address
638  * @pe: EEH PE
639  *
640  * Retrieve the assocated PE address. Actually, there're 2 RTAS
641  * function calls dedicated for the purpose. We need implement
642  * it through the new function and then the old one. Besides,
643  * you should make sure the config address is figured out from
644  * FDT node before calling the function.
645  *
646  * It's notable that zero'ed return value means invalid PE config
647  * address.
648  */
649 static int pseries_eeh_get_pe_addr(struct pci_dn *pdn)
650 {
651 	int config_addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
652 	unsigned long buid = pdn->phb->buid;
653 	int ret = 0;
654 	int rets[3];
655 
656 	if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
657 		/*
658 		 * First of all, we need to make sure there has one PE
659 		 * associated with the device. Otherwise, PE address is
660 		 * meaningless.
661 		 */
662 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
663 				config_addr, BUID_HI(buid), BUID_LO(buid), 1);
664 		if (ret || (rets[0] == 0))
665 			return 0;
666 
667 		/* Retrieve the associated PE config address */
668 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
669 				config_addr, BUID_HI(buid), BUID_LO(buid), 0);
670 		if (ret) {
671 			pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
672 				__func__, pdn->phb->global_number, config_addr);
673 			return 0;
674 		}
675 
676 		return rets[0];
677 	}
678 
679 	if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
680 		ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
681 				config_addr, BUID_HI(buid), BUID_LO(buid), 0);
682 		if (ret) {
683 			pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
684 				__func__, pdn->phb->global_number, config_addr);
685 			return 0;
686 		}
687 
688 		return rets[0];
689 	}
690 
691 	return ret;
692 }
693 
694 /**
695  * pseries_eeh_get_state - Retrieve PE state
696  * @pe: EEH PE
697  * @delay: suggested time to wait if state is unavailable
698  *
699  * Retrieve the state of the specified PE. On RTAS compliant
700  * pseries platform, there already has one dedicated RTAS function
701  * for the purpose. It's notable that the associated PE config address
702  * might be ready when calling the function. Therefore, endeavour to
703  * use the PE config address if possible. Further more, there're 2
704  * RTAS calls for the purpose, we need to try the new one and back
705  * to the old one if the new one couldn't work properly.
706  */
707 static int pseries_eeh_get_state(struct eeh_pe *pe, int *delay)
708 {
709 	int config_addr;
710 	int ret;
711 	int rets[4];
712 	int result;
713 
714 	/* Figure out PE config address if possible */
715 	config_addr = pe->config_addr;
716 	if (pe->addr)
717 		config_addr = pe->addr;
718 
719 	if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
720 		ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
721 				config_addr, BUID_HI(pe->phb->buid),
722 				BUID_LO(pe->phb->buid));
723 	} else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
724 		/* Fake PE unavailable info */
725 		rets[2] = 0;
726 		ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
727 				config_addr, BUID_HI(pe->phb->buid),
728 				BUID_LO(pe->phb->buid));
729 	} else {
730 		return EEH_STATE_NOT_SUPPORT;
731 	}
732 
733 	if (ret)
734 		return ret;
735 
736 	/* Parse the result out */
737 	if (!rets[1])
738 		return EEH_STATE_NOT_SUPPORT;
739 
740 	switch(rets[0]) {
741 	case 0:
742 		result = EEH_STATE_MMIO_ACTIVE |
743 			 EEH_STATE_DMA_ACTIVE;
744 		break;
745 	case 1:
746 		result = EEH_STATE_RESET_ACTIVE |
747 			 EEH_STATE_MMIO_ACTIVE  |
748 			 EEH_STATE_DMA_ACTIVE;
749 		break;
750 	case 2:
751 		result = 0;
752 		break;
753 	case 4:
754 		result = EEH_STATE_MMIO_ENABLED;
755 		break;
756 	case 5:
757 		if (rets[2]) {
758 			if (delay)
759 				*delay = rets[2];
760 			result = EEH_STATE_UNAVAILABLE;
761 		} else {
762 			result = EEH_STATE_NOT_SUPPORT;
763 		}
764 		break;
765 	default:
766 		result = EEH_STATE_NOT_SUPPORT;
767 	}
768 
769 	return result;
770 }
771 
772 /**
773  * pseries_eeh_reset - Reset the specified PE
774  * @pe: EEH PE
775  * @option: reset option
776  *
777  * Reset the specified PE
778  */
779 static int pseries_eeh_reset(struct eeh_pe *pe, int option)
780 {
781 	int config_addr;
782 
783 	/* Figure out PE address */
784 	config_addr = pe->config_addr;
785 	if (pe->addr)
786 		config_addr = pe->addr;
787 
788 	return pseries_eeh_phb_reset(pe->phb, config_addr, option);
789 }
790 
791 /**
792  * pseries_eeh_get_log - Retrieve error log
793  * @pe: EEH PE
794  * @severity: temporary or permanent error log
795  * @drv_log: driver log to be combined with retrieved error log
796  * @len: length of driver log
797  *
798  * Retrieve the temporary or permanent error from the PE.
799  * Actually, the error will be retrieved through the dedicated
800  * RTAS call.
801  */
802 static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
803 {
804 	int config_addr;
805 	unsigned long flags;
806 	int ret;
807 
808 	spin_lock_irqsave(&slot_errbuf_lock, flags);
809 	memset(slot_errbuf, 0, eeh_error_buf_size);
810 
811 	/* Figure out the PE address */
812 	config_addr = pe->config_addr;
813 	if (pe->addr)
814 		config_addr = pe->addr;
815 
816 	ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
817 			BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
818 			virt_to_phys(drv_log), len,
819 			virt_to_phys(slot_errbuf), eeh_error_buf_size,
820 			severity);
821 	if (!ret)
822 		log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
823 	spin_unlock_irqrestore(&slot_errbuf_lock, flags);
824 
825 	return ret;
826 }
827 
828 /**
829  * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
830  * @pe: EEH PE
831  *
832  */
833 static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
834 {
835 	int config_addr;
836 
837 	/* Figure out the PE address */
838 	config_addr = pe->config_addr;
839 	if (pe->addr)
840 		config_addr = pe->addr;
841 
842 	return pseries_eeh_phb_configure_bridge(pe->phb, config_addr);
843 }
844 
845 /**
846  * pseries_eeh_read_config - Read PCI config space
847  * @edev: EEH device handle
848  * @where: PCI config space offset
849  * @size: size to read
850  * @val: return value
851  *
852  * Read config space from the speicifed device
853  */
854 static int pseries_eeh_read_config(struct eeh_dev *edev, int where, int size, u32 *val)
855 {
856 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
857 
858 	return rtas_read_config(pdn, where, size, val);
859 }
860 
861 /**
862  * pseries_eeh_write_config - Write PCI config space
863  * @edev: EEH device handle
864  * @where: PCI config space offset
865  * @size: size to write
866  * @val: value to be written
867  *
868  * Write config space to the specified device
869  */
870 static int pseries_eeh_write_config(struct eeh_dev *edev, int where, int size, u32 val)
871 {
872 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
873 
874 	return rtas_write_config(pdn, where, size, val);
875 }
876 
877 #ifdef CONFIG_PCI_IOV
878 int pseries_send_allow_unfreeze(struct pci_dn *pdn,
879 				u16 *vf_pe_array, int cur_vfs)
880 {
881 	int rc;
882 	int ibm_allow_unfreeze = rtas_token("ibm,open-sriov-allow-unfreeze");
883 	unsigned long buid, addr;
884 
885 	addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
886 	buid = pdn->phb->buid;
887 	spin_lock(&rtas_data_buf_lock);
888 	memcpy(rtas_data_buf, vf_pe_array, RTAS_DATA_BUF_SIZE);
889 	rc = rtas_call(ibm_allow_unfreeze, 5, 1, NULL,
890 		       addr,
891 		       BUID_HI(buid),
892 		       BUID_LO(buid),
893 		       rtas_data_buf, cur_vfs * sizeof(u16));
894 	spin_unlock(&rtas_data_buf_lock);
895 	if (rc)
896 		pr_warn("%s: Failed to allow unfreeze for PHB#%x-PE#%lx, rc=%x\n",
897 			__func__,
898 			pdn->phb->global_number, addr, rc);
899 	return rc;
900 }
901 
902 static int pseries_call_allow_unfreeze(struct eeh_dev *edev)
903 {
904 	int cur_vfs = 0, rc = 0, vf_index, bus, devfn, vf_pe_num;
905 	struct pci_dn *pdn, *tmp, *parent, *physfn_pdn;
906 	u16 *vf_pe_array;
907 
908 	vf_pe_array = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
909 	if (!vf_pe_array)
910 		return -ENOMEM;
911 	if (pci_num_vf(edev->physfn ? edev->physfn : edev->pdev)) {
912 		if (edev->pdev->is_physfn) {
913 			cur_vfs = pci_num_vf(edev->pdev);
914 			pdn = eeh_dev_to_pdn(edev);
915 			parent = pdn->parent;
916 			for (vf_index = 0; vf_index < cur_vfs; vf_index++)
917 				vf_pe_array[vf_index] =
918 					cpu_to_be16(pdn->pe_num_map[vf_index]);
919 			rc = pseries_send_allow_unfreeze(pdn, vf_pe_array,
920 							 cur_vfs);
921 			pdn->last_allow_rc = rc;
922 			for (vf_index = 0; vf_index < cur_vfs; vf_index++) {
923 				list_for_each_entry_safe(pdn, tmp,
924 							 &parent->child_list,
925 							 list) {
926 					bus = pci_iov_virtfn_bus(edev->pdev,
927 								 vf_index);
928 					devfn = pci_iov_virtfn_devfn(edev->pdev,
929 								     vf_index);
930 					if (pdn->busno != bus ||
931 					    pdn->devfn != devfn)
932 						continue;
933 					pdn->last_allow_rc = rc;
934 				}
935 			}
936 		} else {
937 			pdn = pci_get_pdn(edev->pdev);
938 			physfn_pdn = pci_get_pdn(edev->physfn);
939 
940 			vf_pe_num = physfn_pdn->pe_num_map[edev->vf_index];
941 			vf_pe_array[0] = cpu_to_be16(vf_pe_num);
942 			rc = pseries_send_allow_unfreeze(physfn_pdn,
943 							 vf_pe_array, 1);
944 			pdn->last_allow_rc = rc;
945 		}
946 	}
947 
948 	kfree(vf_pe_array);
949 	return rc;
950 }
951 
952 static int pseries_notify_resume(struct eeh_dev *edev)
953 {
954 	if (!edev)
955 		return -EEXIST;
956 
957 	if (rtas_token("ibm,open-sriov-allow-unfreeze")
958 	    == RTAS_UNKNOWN_SERVICE)
959 		return -EINVAL;
960 
961 	if (edev->pdev->is_physfn || edev->pdev->is_virtfn)
962 		return pseries_call_allow_unfreeze(edev);
963 
964 	return 0;
965 }
966 #endif
967 
968 static struct eeh_ops pseries_eeh_ops = {
969 	.name			= "pseries",
970 	.init			= pseries_eeh_init,
971 	.probe			= pseries_eeh_probe,
972 	.set_option		= pseries_eeh_set_option,
973 	.get_state		= pseries_eeh_get_state,
974 	.reset			= pseries_eeh_reset,
975 	.get_log		= pseries_eeh_get_log,
976 	.configure_bridge       = pseries_eeh_configure_bridge,
977 	.err_inject		= NULL,
978 	.read_config		= pseries_eeh_read_config,
979 	.write_config		= pseries_eeh_write_config,
980 	.next_error		= NULL,
981 	.restore_config		= NULL, /* NB: configure_bridge() does this */
982 #ifdef CONFIG_PCI_IOV
983 	.notify_resume		= pseries_notify_resume
984 #endif
985 };
986 
987 /**
988  * eeh_pseries_init - Register platform dependent EEH operations
989  *
990  * EEH initialization on pseries platform. This function should be
991  * called before any EEH related functions.
992  */
993 static int __init eeh_pseries_init(void)
994 {
995 	int ret;
996 
997 	ret = eeh_ops_register(&pseries_eeh_ops);
998 	if (!ret)
999 		pr_info("EEH: pSeries platform initialized\n");
1000 	else
1001 		pr_info("EEH: pSeries platform initialization failure (%d)\n",
1002 			ret);
1003 
1004 	return ret;
1005 }
1006 machine_early_initcall(pseries, eeh_pseries_init);
1007