powerpc/eeh: Freeze PE before PE reset
[cascardo/linux.git] / arch / powerpc / platforms / pseries / eeh_pseries.c
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         /*
87          * Necessary sanity check. We needn't check "get-config-addr-info"
88          * and its variant since the old firmware probably support address
89          * of domain/bus/slot/function for EEH RTAS operations.
90          */
91         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
92                 pr_warn("%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_warn("%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_warn("%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_warn("%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_warn("%s: RTAS service <ibm,configure-pe> and "
112                         "<ibm,configure-bridge> invalid\n",
113                         __func__);
114                 return -EINVAL;
115         }
116
117         /* Initialize error log lock and size */
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_warn("%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_warn("%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         /* Set EEH probe mode */
131         eeh_add_flag(EEH_PROBE_MODE_DEVTREE | EEH_ENABLE_IO_FOR_LOG);
132
133         return 0;
134 }
135
136 static int pseries_eeh_cap_start(struct device_node *dn)
137 {
138         struct pci_dn *pdn = PCI_DN(dn);
139         u32 status;
140
141         if (!pdn)
142                 return 0;
143
144         rtas_read_config(pdn, PCI_STATUS, 2, &status);
145         if (!(status & PCI_STATUS_CAP_LIST))
146                 return 0;
147
148         return PCI_CAPABILITY_LIST;
149 }
150
151
152 static int pseries_eeh_find_cap(struct device_node *dn, int cap)
153 {
154         struct pci_dn *pdn = PCI_DN(dn);
155         int pos = pseries_eeh_cap_start(dn);
156         int cnt = 48;   /* Maximal number of capabilities */
157         u32 id;
158
159         if (!pos)
160                 return 0;
161
162         while (cnt--) {
163                 rtas_read_config(pdn, pos, 1, &pos);
164                 if (pos < 0x40)
165                         break;
166                 pos &= ~3;
167                 rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
168                 if (id == 0xff)
169                         break;
170                 if (id == cap)
171                         return pos;
172                 pos += PCI_CAP_LIST_NEXT;
173         }
174
175         return 0;
176 }
177
178 static int pseries_eeh_find_ecap(struct device_node *dn, int cap)
179 {
180         struct pci_dn *pdn = PCI_DN(dn);
181         struct eeh_dev *edev = of_node_to_eeh_dev(dn);
182         u32 header;
183         int pos = 256;
184         int ttl = (4096 - 256) / 8;
185
186         if (!edev || !edev->pcie_cap)
187                 return 0;
188         if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
189                 return 0;
190         else if (!header)
191                 return 0;
192
193         while (ttl-- > 0) {
194                 if (PCI_EXT_CAP_ID(header) == cap && pos)
195                         return pos;
196
197                 pos = PCI_EXT_CAP_NEXT(header);
198                 if (pos < 256)
199                         break;
200
201                 if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
202                         break;
203         }
204
205         return 0;
206 }
207
208 /**
209  * pseries_eeh_of_probe - EEH probe on the given device
210  * @dn: OF node
211  * @flag: Unused
212  *
213  * When EEH module is installed during system boot, all PCI devices
214  * are checked one by one to see if it supports EEH. The function
215  * is introduced for the purpose.
216  */
217 static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
218 {
219         struct eeh_dev *edev;
220         struct eeh_pe pe;
221         struct pci_dn *pdn = PCI_DN(dn);
222         const __be32 *classp, *vendorp, *devicep;
223         u32 class_code;
224         const __be32 *regs;
225         u32 pcie_flags;
226         int enable = 0;
227         int ret;
228
229         /* Retrieve OF node and eeh device */
230         edev = of_node_to_eeh_dev(dn);
231         if (edev->pe || !of_device_is_available(dn))
232                 return NULL;
233
234         /* Retrieve class/vendor/device IDs */
235         classp = of_get_property(dn, "class-code", NULL);
236         vendorp = of_get_property(dn, "vendor-id", NULL);
237         devicep = of_get_property(dn, "device-id", NULL);
238
239         /* Skip for bad OF node or PCI-ISA bridge */
240         if (!classp || !vendorp || !devicep)
241                 return NULL;
242         if (dn->type && !strcmp(dn->type, "isa"))
243                 return NULL;
244
245         class_code = of_read_number(classp, 1);
246
247         /*
248          * Update class code and mode of eeh device. We need
249          * correctly reflects that current device is root port
250          * or PCIe switch downstream port.
251          */
252         edev->class_code = class_code;
253         edev->pcix_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_PCIX);
254         edev->pcie_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_EXP);
255         edev->aer_cap = pseries_eeh_find_ecap(dn, PCI_EXT_CAP_ID_ERR);
256         edev->mode &= 0xFFFFFF00;
257         if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
258                 edev->mode |= EEH_DEV_BRIDGE;
259                 if (edev->pcie_cap) {
260                         rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
261                                          2, &pcie_flags);
262                         pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
263                         if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
264                                 edev->mode |= EEH_DEV_ROOT_PORT;
265                         else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
266                                 edev->mode |= EEH_DEV_DS_PORT;
267                 }
268         }
269
270         /* Retrieve the device address */
271         regs = of_get_property(dn, "reg", NULL);
272         if (!regs) {
273                 pr_warn("%s: OF node property %s::reg not found\n",
274                         __func__, dn->full_name);
275                 return NULL;
276         }
277
278         /* Initialize the fake PE */
279         memset(&pe, 0, sizeof(struct eeh_pe));
280         pe.phb = edev->phb;
281         pe.config_addr = of_read_number(regs, 1);
282
283         /* Enable EEH on the device */
284         ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
285         if (!ret) {
286                 edev->config_addr = of_read_number(regs, 1);
287                 /* Retrieve PE address */
288                 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
289                 pe.addr = edev->pe_config_addr;
290
291                 /* Some older systems (Power4) allow the ibm,set-eeh-option
292                  * call to succeed even on nodes where EEH is not supported.
293                  * Verify support explicitly.
294                  */
295                 ret = eeh_ops->get_state(&pe, NULL);
296                 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
297                         enable = 1;
298
299                 if (enable) {
300                         eeh_add_flag(EEH_ENABLED);
301                         eeh_add_to_parent_pe(edev);
302
303                         pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
304                                 __func__, dn->full_name, pe.phb->global_number,
305                                 pe.addr, pe.config_addr);
306                 } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
307                            (of_node_to_eeh_dev(dn->parent))->pe) {
308                         /* This device doesn't support EEH, but it may have an
309                          * EEH parent, in which case we mark it as supported.
310                          */
311                         edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
312                         edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
313                         eeh_add_to_parent_pe(edev);
314                 }
315         }
316
317         /* Save memory bars */
318         eeh_save_bars(edev);
319
320         return NULL;
321 }
322
323 /**
324  * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
325  * @pe: EEH PE
326  * @option: operation to be issued
327  *
328  * The function is used to control the EEH functionality globally.
329  * Currently, following options are support according to PAPR:
330  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
331  */
332 static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
333 {
334         int ret = 0;
335         int config_addr;
336
337         /*
338          * When we're enabling or disabling EEH functioality on
339          * the particular PE, the PE config address is possibly
340          * unavailable. Therefore, we have to figure it out from
341          * the FDT node.
342          */
343         switch (option) {
344         case EEH_OPT_DISABLE:
345         case EEH_OPT_ENABLE:
346         case EEH_OPT_THAW_MMIO:
347         case EEH_OPT_THAW_DMA:
348                 config_addr = pe->config_addr;
349                 if (pe->addr)
350                         config_addr = pe->addr;
351                 break;
352         case EEH_OPT_FREEZE_PE:
353                 /* Not support */
354                 return 0;
355         default:
356                 pr_err("%s: Invalid option %d\n",
357                         __func__, option);
358                 return -EINVAL;
359         }
360
361         ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
362                         config_addr, BUID_HI(pe->phb->buid),
363                         BUID_LO(pe->phb->buid), option);
364
365         return ret;
366 }
367
368 /**
369  * pseries_eeh_get_pe_addr - Retrieve PE address
370  * @pe: EEH PE
371  *
372  * Retrieve the assocated PE address. Actually, there're 2 RTAS
373  * function calls dedicated for the purpose. We need implement
374  * it through the new function and then the old one. Besides,
375  * you should make sure the config address is figured out from
376  * FDT node before calling the function.
377  *
378  * It's notable that zero'ed return value means invalid PE config
379  * address.
380  */
381 static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
382 {
383         int ret = 0;
384         int rets[3];
385
386         if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
387                 /*
388                  * First of all, we need to make sure there has one PE
389                  * associated with the device. Otherwise, PE address is
390                  * meaningless.
391                  */
392                 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
393                                 pe->config_addr, BUID_HI(pe->phb->buid),
394                                 BUID_LO(pe->phb->buid), 1);
395                 if (ret || (rets[0] == 0))
396                         return 0;
397
398                 /* Retrieve the associated PE config address */
399                 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
400                                 pe->config_addr, BUID_HI(pe->phb->buid),
401                                 BUID_LO(pe->phb->buid), 0);
402                 if (ret) {
403                         pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
404                                 __func__, pe->phb->global_number, pe->config_addr);
405                         return 0;
406                 }
407
408                 return rets[0];
409         }
410
411         if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
412                 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
413                                 pe->config_addr, BUID_HI(pe->phb->buid),
414                                 BUID_LO(pe->phb->buid), 0);
415                 if (ret) {
416                         pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
417                                 __func__, pe->phb->global_number, pe->config_addr);
418                         return 0;
419                 }
420
421                 return rets[0];
422         }
423
424         return ret;
425 }
426
427 /**
428  * pseries_eeh_get_state - Retrieve PE state
429  * @pe: EEH PE
430  * @state: return value
431  *
432  * Retrieve the state of the specified PE. On RTAS compliant
433  * pseries platform, there already has one dedicated RTAS function
434  * for the purpose. It's notable that the associated PE config address
435  * might be ready when calling the function. Therefore, endeavour to
436  * use the PE config address if possible. Further more, there're 2
437  * RTAS calls for the purpose, we need to try the new one and back
438  * to the old one if the new one couldn't work properly.
439  */
440 static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
441 {
442         int config_addr;
443         int ret;
444         int rets[4];
445         int result;
446
447         /* Figure out PE config address if possible */
448         config_addr = pe->config_addr;
449         if (pe->addr)
450                 config_addr = pe->addr;
451
452         if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
453                 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
454                                 config_addr, BUID_HI(pe->phb->buid),
455                                 BUID_LO(pe->phb->buid));
456         } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
457                 /* Fake PE unavailable info */
458                 rets[2] = 0;
459                 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
460                                 config_addr, BUID_HI(pe->phb->buid),
461                                 BUID_LO(pe->phb->buid));
462         } else {
463                 return EEH_STATE_NOT_SUPPORT;
464         }
465
466         if (ret)
467                 return ret;
468
469         /* Parse the result out */
470         result = 0;
471         if (rets[1]) {
472                 switch(rets[0]) {
473                 case 0:
474                         result &= ~EEH_STATE_RESET_ACTIVE;
475                         result |= EEH_STATE_MMIO_ACTIVE;
476                         result |= EEH_STATE_DMA_ACTIVE;
477                         break;
478                 case 1:
479                         result |= EEH_STATE_RESET_ACTIVE;
480                         result |= EEH_STATE_MMIO_ACTIVE;
481                         result |= EEH_STATE_DMA_ACTIVE;
482                         break;
483                 case 2:
484                         result &= ~EEH_STATE_RESET_ACTIVE;
485                         result &= ~EEH_STATE_MMIO_ACTIVE;
486                         result &= ~EEH_STATE_DMA_ACTIVE;
487                         break;
488                 case 4:
489                         result &= ~EEH_STATE_RESET_ACTIVE;
490                         result &= ~EEH_STATE_MMIO_ACTIVE;
491                         result &= ~EEH_STATE_DMA_ACTIVE;
492                         result |= EEH_STATE_MMIO_ENABLED;
493                         break;
494                 case 5:
495                         if (rets[2]) {
496                                 if (state) *state = rets[2];
497                                 result = EEH_STATE_UNAVAILABLE;
498                         } else {
499                                 result = EEH_STATE_NOT_SUPPORT;
500                         }
501                         break;
502                 default:
503                         result = EEH_STATE_NOT_SUPPORT;
504                 }
505         } else {
506                 result = EEH_STATE_NOT_SUPPORT;
507         }
508
509         return result;
510 }
511
512 /**
513  * pseries_eeh_reset - Reset the specified PE
514  * @pe: EEH PE
515  * @option: reset option
516  *
517  * Reset the specified PE
518  */
519 static int pseries_eeh_reset(struct eeh_pe *pe, int option)
520 {
521         int config_addr;
522         int ret;
523
524         /* Figure out PE address */
525         config_addr = pe->config_addr;
526         if (pe->addr)
527                 config_addr = pe->addr;
528
529         /* Reset PE through RTAS call */
530         ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
531                         config_addr, BUID_HI(pe->phb->buid),
532                         BUID_LO(pe->phb->buid), option);
533
534         /* If fundamental-reset not supported, try hot-reset */
535         if (option == EEH_RESET_FUNDAMENTAL &&
536             ret == -8) {
537                 option = EEH_RESET_HOT;
538                 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
539                                 config_addr, BUID_HI(pe->phb->buid),
540                                 BUID_LO(pe->phb->buid), option);
541         }
542
543         /* We need reset hold or settlement delay */
544         if (option == EEH_RESET_FUNDAMENTAL ||
545             option == EEH_RESET_HOT)
546                 msleep(EEH_PE_RST_HOLD_TIME);
547         else
548                 msleep(EEH_PE_RST_SETTLE_TIME);
549
550         return ret;
551 }
552
553 /**
554  * pseries_eeh_wait_state - Wait for PE state
555  * @pe: EEH PE
556  * @max_wait: maximal period in microsecond
557  *
558  * Wait for the state of associated PE. It might take some time
559  * to retrieve the PE's state.
560  */
561 static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
562 {
563         int ret;
564         int mwait;
565
566         /*
567          * According to PAPR, the state of PE might be temporarily
568          * unavailable. Under the circumstance, we have to wait
569          * for indicated time determined by firmware. The maximal
570          * wait time is 5 minutes, which is acquired from the original
571          * EEH implementation. Also, the original implementation
572          * also defined the minimal wait time as 1 second.
573          */
574 #define EEH_STATE_MIN_WAIT_TIME (1000)
575 #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
576
577         while (1) {
578                 ret = pseries_eeh_get_state(pe, &mwait);
579
580                 /*
581                  * If the PE's state is temporarily unavailable,
582                  * we have to wait for the specified time. Otherwise,
583                  * the PE's state will be returned immediately.
584                  */
585                 if (ret != EEH_STATE_UNAVAILABLE)
586                         return ret;
587
588                 if (max_wait <= 0) {
589                         pr_warn("%s: Timeout when getting PE's state (%d)\n",
590                                 __func__, max_wait);
591                         return EEH_STATE_NOT_SUPPORT;
592                 }
593
594                 if (mwait <= 0) {
595                         pr_warn("%s: Firmware returned bad wait value %d\n",
596                                 __func__, mwait);
597                         mwait = EEH_STATE_MIN_WAIT_TIME;
598                 } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
599                         pr_warn("%s: Firmware returned too long wait value %d\n",
600                                 __func__, mwait);
601                         mwait = EEH_STATE_MAX_WAIT_TIME;
602                 }
603
604                 max_wait -= mwait;
605                 msleep(mwait);
606         }
607
608         return EEH_STATE_NOT_SUPPORT;
609 }
610
611 /**
612  * pseries_eeh_get_log - Retrieve error log
613  * @pe: EEH PE
614  * @severity: temporary or permanent error log
615  * @drv_log: driver log to be combined with retrieved error log
616  * @len: length of driver log
617  *
618  * Retrieve the temporary or permanent error from the PE.
619  * Actually, the error will be retrieved through the dedicated
620  * RTAS call.
621  */
622 static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
623 {
624         int config_addr;
625         unsigned long flags;
626         int ret;
627
628         spin_lock_irqsave(&slot_errbuf_lock, flags);
629         memset(slot_errbuf, 0, eeh_error_buf_size);
630
631         /* Figure out the PE address */
632         config_addr = pe->config_addr;
633         if (pe->addr)
634                 config_addr = pe->addr;
635
636         ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
637                         BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
638                         virt_to_phys(drv_log), len,
639                         virt_to_phys(slot_errbuf), eeh_error_buf_size,
640                         severity);
641         if (!ret)
642                 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
643         spin_unlock_irqrestore(&slot_errbuf_lock, flags);
644
645         return ret;
646 }
647
648 /**
649  * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
650  * @pe: EEH PE
651  *
652  * The function will be called to reconfigure the bridges included
653  * in the specified PE so that the mulfunctional PE would be recovered
654  * again.
655  */
656 static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
657 {
658         int config_addr;
659         int ret;
660
661         /* Figure out the PE address */
662         config_addr = pe->config_addr;
663         if (pe->addr)
664                 config_addr = pe->addr;
665
666         /* Use new configure-pe function, if supported */
667         if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
668                 ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
669                                 config_addr, BUID_HI(pe->phb->buid),
670                                 BUID_LO(pe->phb->buid));
671         } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
672                 ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
673                                 config_addr, BUID_HI(pe->phb->buid),
674                                 BUID_LO(pe->phb->buid));
675         } else {
676                 return -EFAULT;
677         }
678
679         if (ret)
680                 pr_warn("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
681                         __func__, pe->phb->global_number, pe->addr, ret);
682
683         return ret;
684 }
685
686 /**
687  * pseries_eeh_read_config - Read PCI config space
688  * @dn: device node
689  * @where: PCI address
690  * @size: size to read
691  * @val: return value
692  *
693  * Read config space from the speicifed device
694  */
695 static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
696 {
697         struct pci_dn *pdn;
698
699         pdn = PCI_DN(dn);
700
701         return rtas_read_config(pdn, where, size, val);
702 }
703
704 /**
705  * pseries_eeh_write_config - Write PCI config space
706  * @dn: device node
707  * @where: PCI address
708  * @size: size to write
709  * @val: value to be written
710  *
711  * Write config space to the specified device
712  */
713 static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
714 {
715         struct pci_dn *pdn;
716
717         pdn = PCI_DN(dn);
718
719         return rtas_write_config(pdn, where, size, val);
720 }
721
722 static struct eeh_ops pseries_eeh_ops = {
723         .name                   = "pseries",
724         .init                   = pseries_eeh_init,
725         .of_probe               = pseries_eeh_of_probe,
726         .dev_probe              = NULL,
727         .set_option             = pseries_eeh_set_option,
728         .get_pe_addr            = pseries_eeh_get_pe_addr,
729         .get_state              = pseries_eeh_get_state,
730         .reset                  = pseries_eeh_reset,
731         .wait_state             = pseries_eeh_wait_state,
732         .get_log                = pseries_eeh_get_log,
733         .configure_bridge       = pseries_eeh_configure_bridge,
734         .read_config            = pseries_eeh_read_config,
735         .write_config           = pseries_eeh_write_config,
736         .next_error             = NULL,
737         .restore_config         = NULL
738 };
739
740 /**
741  * eeh_pseries_init - Register platform dependent EEH operations
742  *
743  * EEH initialization on pseries platform. This function should be
744  * called before any EEH related functions.
745  */
746 static int __init eeh_pseries_init(void)
747 {
748         int ret;
749
750         ret = eeh_ops_register(&pseries_eeh_ops);
751         if (!ret)
752                 pr_info("EEH: pSeries platform initialized\n");
753         else
754                 pr_info("EEH: pSeries platform initialization failure (%d)\n",
755                         ret);
756
757         return ret;
758 }
759 machine_early_initcall(pseries, eeh_pseries_init);