1 /*
2  * The file intends to implement the platform dependent EEH operations on pseries.
3  * Actually, the pseries platform is built based on RTAS heavily. That means the
4  * pseries platform dependent EEH operations will be built on RTAS calls. The functions
5  * are devired from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
6  * been done.
7  *
8  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
9  * Copyright IBM Corporation 2001, 2005, 2006
10  * Copyright Dave Engebretsen & Todd Inglett 2001
11  * Copyright Linas Vepstas 2005, 2006
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  */
27 
28 #include <linux/atomic.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
33 #include <linux/of.h>
34 #include <linux/pci.h>
35 #include <linux/proc_fs.h>
36 #include <linux/rbtree.h>
37 #include <linux/sched.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40 
41 #include <asm/eeh.h>
42 #include <asm/eeh_event.h>
43 #include <asm/io.h>
44 #include <asm/machdep.h>
45 #include <asm/ppc-pci.h>
46 #include <asm/rtas.h>
47 
48 /* RTAS tokens */
49 static int ibm_set_eeh_option;
50 static int ibm_set_slot_reset;
51 static int ibm_read_slot_reset_state;
52 static int ibm_read_slot_reset_state2;
53 static int ibm_slot_error_detail;
54 static int ibm_get_config_addr_info;
55 static int ibm_get_config_addr_info2;
56 static int ibm_configure_bridge;
57 static int ibm_configure_pe;
58 
59 /*
60  * Buffer for reporting slot-error-detail rtas calls. Its here
61  * in BSS, and not dynamically alloced, so that it ends up in
62  * RMO where RTAS can access it.
63  */
64 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
65 static DEFINE_SPINLOCK(slot_errbuf_lock);
66 static int eeh_error_buf_size;
67 
68 /**
69  * pseries_eeh_init - EEH platform dependent initialization
70  *
71  * EEH platform dependent initialization on pseries.
72  */
73 static int pseries_eeh_init(void)
74 {
75 	/* figure out EEH RTAS function call tokens */
76 	ibm_set_eeh_option		= rtas_token("ibm,set-eeh-option");
77 	ibm_set_slot_reset		= rtas_token("ibm,set-slot-reset");
78 	ibm_read_slot_reset_state2	= rtas_token("ibm,read-slot-reset-state2");
79 	ibm_read_slot_reset_state	= rtas_token("ibm,read-slot-reset-state");
80 	ibm_slot_error_detail		= rtas_token("ibm,slot-error-detail");
81 	ibm_get_config_addr_info2	= rtas_token("ibm,get-config-addr-info2");
82 	ibm_get_config_addr_info	= rtas_token("ibm,get-config-addr-info");
83 	ibm_configure_pe		= rtas_token("ibm,configure-pe");
84 	ibm_configure_bridge		= rtas_token("ibm,configure-bridge");
85 
86 	/* necessary sanity check */
87 	if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
88 		pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
89 			__func__);
90 		return -EINVAL;
91 	} else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
92 		pr_warning("%s: RTAS service <ibm,set-slot-reset> invalid\n",
93 			__func__);
94 		return -EINVAL;
95 	} else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
96 		   ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) {
97 		pr_warning("%s: RTAS service <ibm,read-slot-reset-state2> and "
98 			"<ibm,read-slot-reset-state> invalid\n",
99 			__func__);
100 		return -EINVAL;
101 	} else if (ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE) {
102 		pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
103 			__func__);
104 		return -EINVAL;
105 	} else if (ibm_get_config_addr_info2 == RTAS_UNKNOWN_SERVICE &&
106 		   ibm_get_config_addr_info == RTAS_UNKNOWN_SERVICE) {
107 		pr_warning("%s: RTAS service <ibm,get-config-addr-info2> and "
108 			"<ibm,get-config-addr-info> invalid\n",
109 			__func__);
110 		return -EINVAL;
111 	} else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
112 		   ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
113 		pr_warning("%s: RTAS service <ibm,configure-pe> and "
114 			"<ibm,configure-bridge> invalid\n",
115 			__func__);
116 		return -EINVAL;
117 	}
118 
119 	/* Initialize error log lock and size */
120 	spin_lock_init(&slot_errbuf_lock);
121 	eeh_error_buf_size = rtas_token("rtas-error-log-max");
122 	if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
123 		pr_warning("%s: unknown EEH error log size\n",
124 			__func__);
125 		eeh_error_buf_size = 1024;
126 	} else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
127 		pr_warning("%s: EEH error log size %d exceeds the maximal %d\n",
128 			__func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
129 		eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
130 	}
131 
132 	return 0;
133 }
134 
135 /**
136  * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
137  * @dn: device node
138  * @option: operation to be issued
139  *
140  * The function is used to control the EEH functionality globally.
141  * Currently, following options are support according to PAPR:
142  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
143  */
144 static int pseries_eeh_set_option(struct device_node *dn, int option)
145 {
146 	int ret = 0;
147 	struct eeh_dev *edev;
148 	const u32 *reg;
149 	int config_addr;
150 
151 	edev = of_node_to_eeh_dev(dn);
152 
153 	/*
154 	 * When we're enabling or disabling EEH functioality on
155 	 * the particular PE, the PE config address is possibly
156 	 * unavailable. Therefore, we have to figure it out from
157 	 * the FDT node.
158 	 */
159 	switch (option) {
160 	case EEH_OPT_DISABLE:
161 	case EEH_OPT_ENABLE:
162 		reg = of_get_property(dn, "reg", NULL);
163 		config_addr = reg[0];
164 		break;
165 
166 	case EEH_OPT_THAW_MMIO:
167 	case EEH_OPT_THAW_DMA:
168 		config_addr = edev->config_addr;
169 		if (edev->pe_config_addr)
170 			config_addr = edev->pe_config_addr;
171 		break;
172 
173 	default:
174 		pr_err("%s: Invalid option %d\n",
175 			__func__, option);
176 		return -EINVAL;
177 	}
178 
179 	ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
180 			config_addr, BUID_HI(edev->phb->buid),
181 			BUID_LO(edev->phb->buid), option);
182 
183 	return ret;
184 }
185 
186 /**
187  * pseries_eeh_get_pe_addr - Retrieve PE address
188  * @dn: device node
189  *
190  * Retrieve the assocated PE address. Actually, there're 2 RTAS
191  * function calls dedicated for the purpose. We need implement
192  * it through the new function and then the old one. Besides,
193  * you should make sure the config address is figured out from
194  * FDT node before calling the function.
195  *
196  * It's notable that zero'ed return value means invalid PE config
197  * address.
198  */
199 static int pseries_eeh_get_pe_addr(struct device_node *dn)
200 {
201 	struct eeh_dev *edev;
202 	int ret = 0;
203 	int rets[3];
204 
205 	edev = of_node_to_eeh_dev(dn);
206 
207 	if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
208 		/*
209 		 * First of all, we need to make sure there has one PE
210 		 * associated with the device. Otherwise, PE address is
211 		 * meaningless.
212 		 */
213 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
214 				edev->config_addr, BUID_HI(edev->phb->buid),
215 				BUID_LO(edev->phb->buid), 1);
216 		if (ret || (rets[0] == 0))
217 			return 0;
218 
219 		/* Retrieve the associated PE config address */
220 		ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
221 				edev->config_addr, BUID_HI(edev->phb->buid),
222 				BUID_LO(edev->phb->buid), 0);
223 		if (ret) {
224 			pr_warning("%s: Failed to get PE address for %s\n",
225 				__func__, dn->full_name);
226 			return 0;
227 		}
228 
229 		return rets[0];
230 	}
231 
232 	if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
233 		ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
234 				edev->config_addr, BUID_HI(edev->phb->buid),
235 				BUID_LO(edev->phb->buid), 0);
236 		if (ret) {
237 			pr_warning("%s: Failed to get PE address for %s\n",
238 				__func__, dn->full_name);
239 			return 0;
240 		}
241 
242 		return rets[0];
243 	}
244 
245 	return ret;
246 }
247 
248 /**
249  * pseries_eeh_get_state - Retrieve PE state
250  * @dn: PE associated device node
251  * @state: return value
252  *
253  * Retrieve the state of the specified PE. On RTAS compliant
254  * pseries platform, there already has one dedicated RTAS function
255  * for the purpose. It's notable that the associated PE config address
256  * might be ready when calling the function. Therefore, endeavour to
257  * use the PE config address if possible. Further more, there're 2
258  * RTAS calls for the purpose, we need to try the new one and back
259  * to the old one if the new one couldn't work properly.
260  */
261 static int pseries_eeh_get_state(struct device_node *dn, int *state)
262 {
263 	struct eeh_dev *edev;
264 	int config_addr;
265 	int ret;
266 	int rets[4];
267 	int result;
268 
269 	/* Figure out PE config address if possible */
270 	edev = of_node_to_eeh_dev(dn);
271 	config_addr = edev->config_addr;
272 	if (edev->pe_config_addr)
273 		config_addr = edev->pe_config_addr;
274 
275 	if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
276 		ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
277 				config_addr, BUID_HI(edev->phb->buid),
278 				BUID_LO(edev->phb->buid));
279 	} else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
280 		/* Fake PE unavailable info */
281 		rets[2] = 0;
282 		ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
283 				config_addr, BUID_HI(edev->phb->buid),
284 				BUID_LO(edev->phb->buid));
285 	} else {
286 		return EEH_STATE_NOT_SUPPORT;
287 	}
288 
289 	if (ret)
290 		return ret;
291 
292 	/* Parse the result out */
293 	result = 0;
294 	if (rets[1]) {
295 		switch(rets[0]) {
296 		case 0:
297 			result &= ~EEH_STATE_RESET_ACTIVE;
298 			result |= EEH_STATE_MMIO_ACTIVE;
299 			result |= EEH_STATE_DMA_ACTIVE;
300 			break;
301 		case 1:
302 			result |= EEH_STATE_RESET_ACTIVE;
303 			result |= EEH_STATE_MMIO_ACTIVE;
304 			result |= EEH_STATE_DMA_ACTIVE;
305 			break;
306 		case 2:
307 			result &= ~EEH_STATE_RESET_ACTIVE;
308 			result &= ~EEH_STATE_MMIO_ACTIVE;
309 			result &= ~EEH_STATE_DMA_ACTIVE;
310 			break;
311 		case 4:
312 			result &= ~EEH_STATE_RESET_ACTIVE;
313 			result &= ~EEH_STATE_MMIO_ACTIVE;
314 			result &= ~EEH_STATE_DMA_ACTIVE;
315 			result |= EEH_STATE_MMIO_ENABLED;
316 			break;
317 		case 5:
318 			if (rets[2]) {
319 				if (state) *state = rets[2];
320 				result = EEH_STATE_UNAVAILABLE;
321 			} else {
322 				result = EEH_STATE_NOT_SUPPORT;
323 			}
324 		default:
325 			result = EEH_STATE_NOT_SUPPORT;
326 		}
327 	} else {
328 		result = EEH_STATE_NOT_SUPPORT;
329 	}
330 
331 	return result;
332 }
333 
334 /**
335  * pseries_eeh_reset - Reset the specified PE
336  * @dn: PE associated device node
337  * @option: reset option
338  *
339  * Reset the specified PE
340  */
341 static int pseries_eeh_reset(struct device_node *dn, int option)
342 {
343 	struct eeh_dev *edev;
344 	int config_addr;
345 	int ret;
346 
347 	/* Figure out PE address */
348 	edev = of_node_to_eeh_dev(dn);
349 	config_addr = edev->config_addr;
350 	if (edev->pe_config_addr)
351 		config_addr = edev->pe_config_addr;
352 
353 	/* Reset PE through RTAS call */
354 	ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
355 			config_addr, BUID_HI(edev->phb->buid),
356 			BUID_LO(edev->phb->buid), option);
357 
358 	/* If fundamental-reset not supported, try hot-reset */
359 	if (option == EEH_RESET_FUNDAMENTAL &&
360 	    ret == -8) {
361 		ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
362 				config_addr, BUID_HI(edev->phb->buid),
363 				BUID_LO(edev->phb->buid), EEH_RESET_HOT);
364 	}
365 
366 	return ret;
367 }
368 
369 /**
370  * pseries_eeh_wait_state - Wait for PE state
371  * @dn: PE associated device node
372  * @max_wait: maximal period in microsecond
373  *
374  * Wait for the state of associated PE. It might take some time
375  * to retrieve the PE's state.
376  */
377 static int pseries_eeh_wait_state(struct device_node *dn, int max_wait)
378 {
379 	int ret;
380 	int mwait;
381 
382 	/*
383 	 * According to PAPR, the state of PE might be temporarily
384 	 * unavailable. Under the circumstance, we have to wait
385 	 * for indicated time determined by firmware. The maximal
386 	 * wait time is 5 minutes, which is acquired from the original
387 	 * EEH implementation. Also, the original implementation
388 	 * also defined the minimal wait time as 1 second.
389 	 */
390 #define EEH_STATE_MIN_WAIT_TIME	(1000)
391 #define EEH_STATE_MAX_WAIT_TIME	(300 * 1000)
392 
393 	while (1) {
394 		ret = pseries_eeh_get_state(dn, &mwait);
395 
396 		/*
397 		 * If the PE's state is temporarily unavailable,
398 		 * we have to wait for the specified time. Otherwise,
399 		 * the PE's state will be returned immediately.
400 		 */
401 		if (ret != EEH_STATE_UNAVAILABLE)
402 			return ret;
403 
404 		if (max_wait <= 0) {
405 			pr_warning("%s: Timeout when getting PE's state (%d)\n",
406 				__func__, max_wait);
407 			return EEH_STATE_NOT_SUPPORT;
408 		}
409 
410 		if (mwait <= 0) {
411 			pr_warning("%s: Firmware returned bad wait value %d\n",
412 				__func__, mwait);
413 			mwait = EEH_STATE_MIN_WAIT_TIME;
414 		} else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
415 			pr_warning("%s: Firmware returned too long wait value %d\n",
416 				__func__, mwait);
417 			mwait = EEH_STATE_MAX_WAIT_TIME;
418 		}
419 
420 		max_wait -= mwait;
421 		msleep(mwait);
422 	}
423 
424 	return EEH_STATE_NOT_SUPPORT;
425 }
426 
427 /**
428  * pseries_eeh_get_log - Retrieve error log
429  * @dn: device node
430  * @severity: temporary or permanent error log
431  * @drv_log: driver log to be combined with retrieved error log
432  * @len: length of driver log
433  *
434  * Retrieve the temporary or permanent error from the PE.
435  * Actually, the error will be retrieved through the dedicated
436  * RTAS call.
437  */
438 static int pseries_eeh_get_log(struct device_node *dn, int severity, char *drv_log, unsigned long len)
439 {
440 	struct eeh_dev *edev;
441 	int config_addr;
442 	unsigned long flags;
443 	int ret;
444 
445 	edev = of_node_to_eeh_dev(dn);
446 	spin_lock_irqsave(&slot_errbuf_lock, flags);
447 	memset(slot_errbuf, 0, eeh_error_buf_size);
448 
449 	/* Figure out the PE address */
450 	config_addr = edev->config_addr;
451 	if (edev->pe_config_addr)
452 		config_addr = edev->pe_config_addr;
453 
454 	ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
455 			BUID_HI(edev->phb->buid), BUID_LO(edev->phb->buid),
456 			virt_to_phys(drv_log), len,
457 			virt_to_phys(slot_errbuf), eeh_error_buf_size,
458 			severity);
459 	if (!ret)
460 		log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
461 	spin_unlock_irqrestore(&slot_errbuf_lock, flags);
462 
463 	return ret;
464 }
465 
466 /**
467  * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
468  * @dn: PE associated device node
469  *
470  * The function will be called to reconfigure the bridges included
471  * in the specified PE so that the mulfunctional PE would be recovered
472  * again.
473  */
474 static int pseries_eeh_configure_bridge(struct device_node *dn)
475 {
476 	struct eeh_dev *edev;
477 	int config_addr;
478 	int ret;
479 
480 	/* Figure out the PE address */
481 	edev = of_node_to_eeh_dev(dn);
482 	config_addr = edev->config_addr;
483 	if (edev->pe_config_addr)
484 		config_addr = edev->pe_config_addr;
485 
486 	/* Use new configure-pe function, if supported */
487 	if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
488 		ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
489 				config_addr, BUID_HI(edev->phb->buid),
490 				BUID_LO(edev->phb->buid));
491 	} else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
492 		ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
493 				config_addr, BUID_HI(edev->phb->buid),
494 				BUID_LO(edev->phb->buid));
495 	} else {
496 		return -EFAULT;
497 	}
498 
499 	if (ret)
500 		pr_warning("%s: Unable to configure bridge %d for %s\n",
501 			__func__, ret, dn->full_name);
502 
503 	return ret;
504 }
505 
506 /**
507  * pseries_eeh_read_config - Read PCI config space
508  * @dn: device node
509  * @where: PCI address
510  * @size: size to read
511  * @val: return value
512  *
513  * Read config space from the speicifed device
514  */
515 static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
516 {
517 	struct pci_dn *pdn;
518 
519 	pdn = PCI_DN(dn);
520 
521 	return rtas_read_config(pdn, where, size, val);
522 }
523 
524 /**
525  * pseries_eeh_write_config - Write PCI config space
526  * @dn: device node
527  * @where: PCI address
528  * @size: size to write
529  * @val: value to be written
530  *
531  * Write config space to the specified device
532  */
533 static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
534 {
535 	struct pci_dn *pdn;
536 
537 	pdn = PCI_DN(dn);
538 
539 	return rtas_write_config(pdn, where, size, val);
540 }
541 
542 static struct eeh_ops pseries_eeh_ops = {
543 	.name			= "pseries",
544 	.init			= pseries_eeh_init,
545 	.set_option		= pseries_eeh_set_option,
546 	.get_pe_addr		= pseries_eeh_get_pe_addr,
547 	.get_state		= pseries_eeh_get_state,
548 	.reset			= pseries_eeh_reset,
549 	.wait_state		= pseries_eeh_wait_state,
550 	.get_log		= pseries_eeh_get_log,
551 	.configure_bridge       = pseries_eeh_configure_bridge,
552 	.read_config		= pseries_eeh_read_config,
553 	.write_config		= pseries_eeh_write_config
554 };
555 
556 /**
557  * eeh_pseries_init - Register platform dependent EEH operations
558  *
559  * EEH initialization on pseries platform. This function should be
560  * called before any EEH related functions.
561  */
562 int __init eeh_pseries_init(void)
563 {
564 	return eeh_ops_register(&pseries_eeh_ops);
565 }
566