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