1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
49static int ibm_set_eeh_option;
50static int ibm_set_slot_reset;
51static int ibm_read_slot_reset_state;
52static int ibm_read_slot_reset_state2;
53static int ibm_slot_error_detail;
54static int ibm_get_config_addr_info;
55static int ibm_get_config_addr_info2;
56static int ibm_configure_bridge;
57static int ibm_configure_pe;
58
59
60
61
62
63
64static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
65static DEFINE_SPINLOCK(slot_errbuf_lock);
66static int eeh_error_buf_size;
67
68
69
70
71
72
73static int pseries_eeh_init(void)
74{
75
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
87
88
89
90
91 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
92 pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
93 __func__);
94 return -EINVAL;
95 } else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
96 pr_warning("%s: RTAS service <ibm,set-slot-reset> invalid\n",
97 __func__);
98 return -EINVAL;
99 } else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
100 ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) {
101 pr_warning("%s: RTAS service <ibm,read-slot-reset-state2> and "
102 "<ibm,read-slot-reset-state> invalid\n",
103 __func__);
104 return -EINVAL;
105 } else if (ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE) {
106 pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
107 __func__);
108 return -EINVAL;
109 } else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
110 ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
111 pr_warning("%s: RTAS service <ibm,configure-pe> and "
112 "<ibm,configure-bridge> invalid\n",
113 __func__);
114 return -EINVAL;
115 }
116
117
118 spin_lock_init(&slot_errbuf_lock);
119 eeh_error_buf_size = rtas_token("rtas-error-log-max");
120 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
121 pr_warning("%s: unknown EEH error log size\n",
122 __func__);
123 eeh_error_buf_size = 1024;
124 } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
125 pr_warning("%s: EEH error log size %d exceeds the maximal %d\n",
126 __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
127 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
128 }
129
130
131 eeh_probe_mode_set(EEH_PROBE_MODE_DEVTREE);
132
133 return 0;
134}
135
136
137
138
139
140
141
142
143
144
145static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
146{
147 struct eeh_dev *edev;
148 struct eeh_pe pe;
149 const u32 *class_code, *vendor_id, *device_id;
150 const u32 *regs;
151 int enable = 0;
152 int ret;
153
154
155 edev = of_node_to_eeh_dev(dn);
156 if (!of_device_is_available(dn))
157 return NULL;
158
159
160 class_code = of_get_property(dn, "class-code", NULL);
161 vendor_id = of_get_property(dn, "vendor-id", NULL);
162 device_id = of_get_property(dn, "device-id", NULL);
163
164
165 if (!class_code || !vendor_id || !device_id)
166 return NULL;
167 if (dn->type && !strcmp(dn->type, "isa"))
168 return NULL;
169
170
171 edev->class_code = *class_code;
172 edev->mode = 0;
173
174
175 regs = of_get_property(dn, "reg", NULL);
176 if (!regs) {
177 pr_warning("%s: OF node property %s::reg not found\n",
178 __func__, dn->full_name);
179 return NULL;
180 }
181
182
183 memset(&pe, 0, sizeof(struct eeh_pe));
184 pe.phb = edev->phb;
185 pe.config_addr = regs[0];
186
187
188 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
189 if (!ret) {
190 edev->config_addr = regs[0];
191
192 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
193 pe.addr = edev->pe_config_addr;
194
195
196
197
198
199 ret = eeh_ops->get_state(&pe, NULL);
200 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
201 enable = 1;
202
203 if (enable) {
204 eeh_subsystem_enabled = 1;
205 eeh_add_to_parent_pe(edev);
206
207 pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
208 __func__, dn->full_name, pe.phb->global_number,
209 pe.addr, pe.config_addr);
210 } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
211 (of_node_to_eeh_dev(dn->parent))->pe) {
212
213
214
215 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
216 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
217 eeh_add_to_parent_pe(edev);
218 }
219 }
220
221
222 eeh_save_bars(edev);
223
224 return NULL;
225}
226
227
228
229
230
231
232
233
234
235
236static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
237{
238 int ret = 0;
239 int config_addr;
240
241
242
243
244
245
246
247 switch (option) {
248 case EEH_OPT_DISABLE:
249 case EEH_OPT_ENABLE:
250 case EEH_OPT_THAW_MMIO:
251 case EEH_OPT_THAW_DMA:
252 config_addr = pe->config_addr;
253 if (pe->addr)
254 config_addr = pe->addr;
255 break;
256
257 default:
258 pr_err("%s: Invalid option %d\n",
259 __func__, option);
260 return -EINVAL;
261 }
262
263 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
264 config_addr, BUID_HI(pe->phb->buid),
265 BUID_LO(pe->phb->buid), option);
266
267 return ret;
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
284{
285 int ret = 0;
286 int rets[3];
287
288 if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
289
290
291
292
293
294 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
295 pe->config_addr, BUID_HI(pe->phb->buid),
296 BUID_LO(pe->phb->buid), 1);
297 if (ret || (rets[0] == 0))
298 return 0;
299
300
301 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
302 pe->config_addr, BUID_HI(pe->phb->buid),
303 BUID_LO(pe->phb->buid), 0);
304 if (ret) {
305 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
306 __func__, pe->phb->global_number, pe->config_addr);
307 return 0;
308 }
309
310 return rets[0];
311 }
312
313 if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
314 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
315 pe->config_addr, BUID_HI(pe->phb->buid),
316 BUID_LO(pe->phb->buid), 0);
317 if (ret) {
318 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
319 __func__, pe->phb->global_number, pe->config_addr);
320 return 0;
321 }
322
323 return rets[0];
324 }
325
326 return ret;
327}
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
343{
344 int config_addr;
345 int ret;
346 int rets[4];
347 int result;
348
349
350 config_addr = pe->config_addr;
351 if (pe->addr)
352 config_addr = pe->addr;
353
354 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
355 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
356 config_addr, BUID_HI(pe->phb->buid),
357 BUID_LO(pe->phb->buid));
358 } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
359
360 rets[2] = 0;
361 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
362 config_addr, BUID_HI(pe->phb->buid),
363 BUID_LO(pe->phb->buid));
364 } else {
365 return EEH_STATE_NOT_SUPPORT;
366 }
367
368 if (ret)
369 return ret;
370
371
372 result = 0;
373 if (rets[1]) {
374 switch(rets[0]) {
375 case 0:
376 result &= ~EEH_STATE_RESET_ACTIVE;
377 result |= EEH_STATE_MMIO_ACTIVE;
378 result |= EEH_STATE_DMA_ACTIVE;
379 break;
380 case 1:
381 result |= EEH_STATE_RESET_ACTIVE;
382 result |= EEH_STATE_MMIO_ACTIVE;
383 result |= EEH_STATE_DMA_ACTIVE;
384 break;
385 case 2:
386 result &= ~EEH_STATE_RESET_ACTIVE;
387 result &= ~EEH_STATE_MMIO_ACTIVE;
388 result &= ~EEH_STATE_DMA_ACTIVE;
389 break;
390 case 4:
391 result &= ~EEH_STATE_RESET_ACTIVE;
392 result &= ~EEH_STATE_MMIO_ACTIVE;
393 result &= ~EEH_STATE_DMA_ACTIVE;
394 result |= EEH_STATE_MMIO_ENABLED;
395 break;
396 case 5:
397 if (rets[2]) {
398 if (state) *state = rets[2];
399 result = EEH_STATE_UNAVAILABLE;
400 } else {
401 result = EEH_STATE_NOT_SUPPORT;
402 }
403 default:
404 result = EEH_STATE_NOT_SUPPORT;
405 }
406 } else {
407 result = EEH_STATE_NOT_SUPPORT;
408 }
409
410 return result;
411}
412
413
414
415
416
417
418
419
420static int pseries_eeh_reset(struct eeh_pe *pe, int option)
421{
422 int config_addr;
423 int ret;
424
425
426 config_addr = pe->config_addr;
427 if (pe->addr)
428 config_addr = pe->addr;
429
430
431 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
432 config_addr, BUID_HI(pe->phb->buid),
433 BUID_LO(pe->phb->buid), option);
434
435
436 if (option == EEH_RESET_FUNDAMENTAL &&
437 ret == -8) {
438 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
439 config_addr, BUID_HI(pe->phb->buid),
440 BUID_LO(pe->phb->buid), EEH_RESET_HOT);
441 }
442
443 return ret;
444}
445
446
447
448
449
450
451
452
453
454static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
455{
456 int ret;
457 int mwait;
458
459
460
461
462
463
464
465
466
467#define EEH_STATE_MIN_WAIT_TIME (1000)
468#define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
469
470 while (1) {
471 ret = pseries_eeh_get_state(pe, &mwait);
472
473
474
475
476
477
478 if (ret != EEH_STATE_UNAVAILABLE)
479 return ret;
480
481 if (max_wait <= 0) {
482 pr_warning("%s: Timeout when getting PE's state (%d)\n",
483 __func__, max_wait);
484 return EEH_STATE_NOT_SUPPORT;
485 }
486
487 if (mwait <= 0) {
488 pr_warning("%s: Firmware returned bad wait value %d\n",
489 __func__, mwait);
490 mwait = EEH_STATE_MIN_WAIT_TIME;
491 } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
492 pr_warning("%s: Firmware returned too long wait value %d\n",
493 __func__, mwait);
494 mwait = EEH_STATE_MAX_WAIT_TIME;
495 }
496
497 max_wait -= mwait;
498 msleep(mwait);
499 }
500
501 return EEH_STATE_NOT_SUPPORT;
502}
503
504
505
506
507
508
509
510
511
512
513
514
515static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
516{
517 int config_addr;
518 unsigned long flags;
519 int ret;
520
521 spin_lock_irqsave(&slot_errbuf_lock, flags);
522 memset(slot_errbuf, 0, eeh_error_buf_size);
523
524
525 config_addr = pe->config_addr;
526 if (pe->addr)
527 config_addr = pe->addr;
528
529 ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
530 BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
531 virt_to_phys(drv_log), len,
532 virt_to_phys(slot_errbuf), eeh_error_buf_size,
533 severity);
534 if (!ret)
535 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
536 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
537
538 return ret;
539}
540
541
542
543
544
545
546
547
548
549static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
550{
551 int config_addr;
552 int ret;
553
554
555 config_addr = pe->config_addr;
556 if (pe->addr)
557 config_addr = pe->addr;
558
559
560 if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
561 ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
562 config_addr, BUID_HI(pe->phb->buid),
563 BUID_LO(pe->phb->buid));
564 } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
565 ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
566 config_addr, BUID_HI(pe->phb->buid),
567 BUID_LO(pe->phb->buid));
568 } else {
569 return -EFAULT;
570 }
571
572 if (ret)
573 pr_warning("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
574 __func__, pe->phb->global_number, pe->addr, ret);
575
576 return ret;
577}
578
579
580
581
582
583
584
585
586
587
588static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
589{
590 struct pci_dn *pdn;
591
592 pdn = PCI_DN(dn);
593
594 return rtas_read_config(pdn, where, size, val);
595}
596
597
598
599
600
601
602
603
604
605
606static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
607{
608 struct pci_dn *pdn;
609
610 pdn = PCI_DN(dn);
611
612 return rtas_write_config(pdn, where, size, val);
613}
614
615static struct eeh_ops pseries_eeh_ops = {
616 .name = "pseries",
617 .init = pseries_eeh_init,
618 .of_probe = pseries_eeh_of_probe,
619 .dev_probe = NULL,
620 .set_option = pseries_eeh_set_option,
621 .get_pe_addr = pseries_eeh_get_pe_addr,
622 .get_state = pseries_eeh_get_state,
623 .reset = pseries_eeh_reset,
624 .wait_state = pseries_eeh_wait_state,
625 .get_log = pseries_eeh_get_log,
626 .configure_bridge = pseries_eeh_configure_bridge,
627 .read_config = pseries_eeh_read_config,
628 .write_config = pseries_eeh_write_config
629};
630
631
632
633
634
635
636
637static int __init eeh_pseries_init(void)
638{
639 int ret = -EINVAL;
640
641 if (!machine_is(pseries))
642 return ret;
643
644 ret = eeh_ops_register(&pseries_eeh_ops);
645 if (!ret)
646 pr_info("EEH: pSeries platform initialized\n");
647 else
648 pr_info("EEH: pSeries platform initialization failure (%d)\n",
649 ret);
650
651 return ret;
652}
653
654early_initcall(eeh_pseries_init);
655