powerpc/powernv: Drop PHB operation get_state()
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / eeh-powernv.c
CommitLineData
29310e5e
GS
1/*
2 * The file intends to implement the platform dependent EEH operations on
3 * powernv platform. Actually, the powernv was created in order to fully
4 * hypervisor support.
5 *
6 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/atomic.h>
4cf17445 15#include <linux/debugfs.h>
29310e5e
GS
16#include <linux/delay.h>
17#include <linux/export.h>
18#include <linux/init.h>
19#include <linux/list.h>
20#include <linux/msi.h>
21#include <linux/of.h>
22#include <linux/pci.h>
23#include <linux/proc_fs.h>
24#include <linux/rbtree.h>
25#include <linux/sched.h>
26#include <linux/seq_file.h>
27#include <linux/spinlock.h>
28
29#include <asm/eeh.h>
30#include <asm/eeh_event.h>
31#include <asm/firmware.h>
32#include <asm/io.h>
33#include <asm/iommu.h>
34#include <asm/machdep.h>
35#include <asm/msi_bitmap.h>
36#include <asm/opal.h>
37#include <asm/ppc-pci.h>
38
39#include "powernv.h"
40#include "pci.h"
41
4cf17445
GS
42static bool pnv_eeh_nb_init = false;
43
29310e5e 44/**
01f3bfb7 45 * pnv_eeh_init - EEH platform dependent initialization
29310e5e
GS
46 *
47 * EEH platform dependent initialization on powernv
48 */
01f3bfb7 49static int pnv_eeh_init(void)
29310e5e 50{
dc561fb9
GS
51 struct pci_controller *hose;
52 struct pnv_phb *phb;
53
29310e5e
GS
54 /* We require OPALv3 */
55 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
0dae2743
GS
56 pr_warn("%s: OPALv3 is required !\n",
57 __func__);
29310e5e
GS
58 return -EINVAL;
59 }
60
05b1721d
GS
61 /* Set probe mode */
62 eeh_add_flag(EEH_PROBE_MODE_DEV);
29310e5e 63
dc561fb9
GS
64 /*
65 * P7IOC blocks PCI config access to frozen PE, but PHB3
66 * doesn't do that. So we have to selectively enable I/O
67 * prior to collecting error log.
68 */
69 list_for_each_entry(hose, &hose_list, list_node) {
70 phb = hose->private_data;
71
72 if (phb->model == PNV_PHB_MODEL_P7IOC)
73 eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
2aa5cf9e
GS
74
75 /*
76 * PE#0 should be regarded as valid by EEH core
77 * if it's not the reserved one. Currently, we
78 * have the reserved PE#0 and PE#127 for PHB3
79 * and P7IOC separately. So we should regard
80 * PE#0 as valid for P7IOC.
81 */
82 if (phb->ioda.reserved_pe != 0)
83 eeh_add_flag(EEH_VALID_PE_ZERO);
84
dc561fb9
GS
85 break;
86 }
87
29310e5e
GS
88 return 0;
89}
90
4cf17445
GS
91static int pnv_eeh_event(struct notifier_block *nb,
92 unsigned long events, void *change)
93{
94 uint64_t changed_evts = (uint64_t)change;
95
96 /*
97 * We simply send special EEH event if EEH has
98 * been enabled, or clear pending events in
99 * case that we enable EEH soon
100 */
101 if (!(changed_evts & OPAL_EVENT_PCI_ERROR) ||
102 !(events & OPAL_EVENT_PCI_ERROR))
103 return 0;
104
105 if (eeh_enabled())
106 eeh_send_failure_event(NULL);
107 else
108 opal_notifier_update_evt(OPAL_EVENT_PCI_ERROR, 0x0ul);
109
110 return 0;
111}
112
113static struct notifier_block pnv_eeh_nb = {
114 .notifier_call = pnv_eeh_event,
115 .next = NULL,
116 .priority = 0
117};
118
119#ifdef CONFIG_DEBUG_FS
120static ssize_t pnv_eeh_ei_write(struct file *filp,
121 const char __user *user_buf,
122 size_t count, loff_t *ppos)
123{
124 struct pci_controller *hose = filp->private_data;
125 struct eeh_dev *edev;
126 struct eeh_pe *pe;
127 int pe_no, type, func;
128 unsigned long addr, mask;
129 char buf[50];
130 int ret;
131
132 if (!eeh_ops || !eeh_ops->err_inject)
133 return -ENXIO;
134
135 /* Copy over argument buffer */
136 ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
137 if (!ret)
138 return -EFAULT;
139
140 /* Retrieve parameters */
141 ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
142 &pe_no, &type, &func, &addr, &mask);
143 if (ret != 5)
144 return -EINVAL;
145
146 /* Retrieve PE */
147 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
148 if (!edev)
149 return -ENOMEM;
150 edev->phb = hose;
151 edev->pe_config_addr = pe_no;
152 pe = eeh_pe_get(edev);
153 kfree(edev);
154 if (!pe)
155 return -ENODEV;
156
157 /* Do error injection */
158 ret = eeh_ops->err_inject(pe, type, func, addr, mask);
159 return ret < 0 ? ret : count;
160}
161
162static const struct file_operations pnv_eeh_ei_fops = {
163 .open = simple_open,
164 .llseek = no_llseek,
165 .write = pnv_eeh_ei_write,
166};
167
168static int pnv_eeh_dbgfs_set(void *data, int offset, u64 val)
169{
170 struct pci_controller *hose = data;
171 struct pnv_phb *phb = hose->private_data;
172
173 out_be64(phb->regs + offset, val);
174 return 0;
175}
176
177static int pnv_eeh_dbgfs_get(void *data, int offset, u64 *val)
178{
179 struct pci_controller *hose = data;
180 struct pnv_phb *phb = hose->private_data;
181
182 *val = in_be64(phb->regs + offset);
183 return 0;
184}
185
186static int pnv_eeh_outb_dbgfs_set(void *data, u64 val)
187{
188 return pnv_eeh_dbgfs_set(data, 0xD10, val);
189}
190
191static int pnv_eeh_outb_dbgfs_get(void *data, u64 *val)
192{
193 return pnv_eeh_dbgfs_get(data, 0xD10, val);
194}
195
196static int pnv_eeh_inbA_dbgfs_set(void *data, u64 val)
197{
198 return pnv_eeh_dbgfs_set(data, 0xD90, val);
199}
200
201static int pnv_eeh_inbA_dbgfs_get(void *data, u64 *val)
202{
203 return pnv_eeh_dbgfs_get(data, 0xD90, val);
204}
205
206static int pnv_eeh_inbB_dbgfs_set(void *data, u64 val)
207{
208 return pnv_eeh_dbgfs_set(data, 0xE10, val);
209}
210
211static int pnv_eeh_inbB_dbgfs_get(void *data, u64 *val)
212{
213 return pnv_eeh_dbgfs_get(data, 0xE10, val);
214}
215
216DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_outb_dbgfs_ops, pnv_eeh_outb_dbgfs_get,
217 pnv_eeh_outb_dbgfs_set, "0x%llx\n");
218DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_inbA_dbgfs_ops, pnv_eeh_inbA_dbgfs_get,
219 pnv_eeh_inbA_dbgfs_set, "0x%llx\n");
220DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_inbB_dbgfs_ops, pnv_eeh_inbB_dbgfs_get,
221 pnv_eeh_inbB_dbgfs_set, "0x%llx\n");
222#endif /* CONFIG_DEBUG_FS */
223
29310e5e 224/**
01f3bfb7 225 * pnv_eeh_post_init - EEH platform dependent post initialization
29310e5e
GS
226 *
227 * EEH platform dependent post initialization on powernv. When
228 * the function is called, the EEH PEs and devices should have
229 * been built. If the I/O cache staff has been built, EEH is
230 * ready to supply service.
231 */
01f3bfb7 232static int pnv_eeh_post_init(void)
29310e5e
GS
233{
234 struct pci_controller *hose;
235 struct pnv_phb *phb;
236 int ret = 0;
237
4cf17445
GS
238 /* Register OPAL event notifier */
239 if (!pnv_eeh_nb_init) {
240 ret = opal_notifier_register(&pnv_eeh_nb);
241 if (ret) {
242 pr_warn("%s: Can't register OPAL event notifier (%d)\n",
243 __func__, ret);
244 return ret;
245 }
246
247 pnv_eeh_nb_init = true;
248 }
249
29310e5e
GS
250 list_for_each_entry(hose, &hose_list, list_node) {
251 phb = hose->private_data;
252
4cf17445
GS
253 /*
254 * If EEH is enabled, we're going to rely on that.
255 * Otherwise, we restore to conventional mechanism
256 * to clear frozen PE during PCI config access.
257 */
258 if (eeh_enabled())
259 phb->flags |= PNV_PHB_FLAG_EEH;
260 else
261 phb->flags &= ~PNV_PHB_FLAG_EEH;
262
263 /* Create debugfs entries */
264#ifdef CONFIG_DEBUG_FS
265 if (phb->has_dbgfs || !phb->dbgfs)
266 continue;
267
268 phb->has_dbgfs = 1;
269 debugfs_create_file("err_injct", 0200,
270 phb->dbgfs, hose,
271 &pnv_eeh_ei_fops);
272
273 debugfs_create_file("err_injct_outbound", 0600,
274 phb->dbgfs, hose,
275 &pnv_eeh_outb_dbgfs_ops);
276 debugfs_create_file("err_injct_inboundA", 0600,
277 phb->dbgfs, hose,
278 &pnv_eeh_inbA_dbgfs_ops);
279 debugfs_create_file("err_injct_inboundB", 0600,
280 phb->dbgfs, hose,
281 &pnv_eeh_inbB_dbgfs_ops);
282#endif /* CONFIG_DEBUG_FS */
29310e5e
GS
283 }
284
4cf17445 285
29310e5e
GS
286 return ret;
287}
288
289/**
01f3bfb7 290 * pnv_eeh_dev_probe - Do probe on PCI device
29310e5e
GS
291 * @dev: PCI device
292 * @flag: unused
293 *
294 * When EEH module is installed during system boot, all PCI devices
295 * are checked one by one to see if it supports EEH. The function
296 * is introduced for the purpose. By default, EEH has been enabled
297 * on all PCI devices. That's to say, we only need do necessary
298 * initialization on the corresponding eeh device and create PE
299 * accordingly.
300 *
301 * It's notable that's unsafe to retrieve the EEH device through
302 * the corresponding PCI device. During the PCI device hotplug, which
303 * was possiblly triggered by EEH core, the binding between EEH device
304 * and the PCI device isn't built yet.
305 */
01f3bfb7 306static int pnv_eeh_dev_probe(struct pci_dev *dev, void *flag)
29310e5e
GS
307{
308 struct pci_controller *hose = pci_bus_to_host(dev->bus);
309 struct pnv_phb *phb = hose->private_data;
310 struct device_node *dn = pci_device_to_OF_node(dev);
311 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
dadcd6d6 312 int ret;
29310e5e
GS
313
314 /*
315 * When probing the root bridge, which doesn't have any
316 * subordinate PCI devices. We don't have OF node for
317 * the root bridge. So it's not reasonable to continue
318 * the probing.
319 */
f5c57710 320 if (!dn || !edev || edev->pe)
29310e5e
GS
321 return 0;
322
323 /* Skip for PCI-ISA bridge */
324 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
325 return 0;
326
327 /* Initialize eeh device */
ab55d218
GS
328 edev->class_code = dev->class;
329 edev->mode &= 0xFFFFFF00;
4b83bd45
GS
330 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
331 edev->mode |= EEH_DEV_BRIDGE;
2a18dfc6 332 edev->pcix_cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
4b83bd45
GS
333 if (pci_is_pcie(dev)) {
334 edev->pcie_cap = pci_pcie_cap(dev);
335
336 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
337 edev->mode |= EEH_DEV_ROOT_PORT;
338 else if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)
339 edev->mode |= EEH_DEV_DS_PORT;
2a18dfc6
GS
340
341 edev->aer_cap = pci_find_ext_capability(dev,
342 PCI_EXT_CAP_ID_ERR);
4b83bd45
GS
343 }
344
29310e5e
GS
345 edev->config_addr = ((dev->bus->number << 8) | dev->devfn);
346 edev->pe_config_addr = phb->bdfn_to_pe(phb, dev->bus, dev->devfn & 0xff);
347
348 /* Create PE */
dadcd6d6
MQ
349 ret = eeh_add_to_parent_pe(edev);
350 if (ret) {
351 pr_warn("%s: Can't add PCI dev %s to parent PE (%d)\n",
352 __func__, pci_name(dev), ret);
353 return ret;
354 }
355
b6541db1
GS
356 /*
357 * If the PE contains any one of following adapters, the
358 * PCI config space can't be accessed when dumping EEH log.
359 * Otherwise, we will run into fenced PHB caused by shortage
360 * of outbound credits in the adapter. The PCI config access
361 * should be blocked until PE reset. MMIO access is dropped
362 * by hardware certainly. In order to drop PCI config requests,
363 * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
364 * will be checked in the backend for PE state retrival. If
365 * the PE becomes frozen for the first time and the flag has
366 * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
367 * that PE to block its config space.
368 *
369 * Broadcom Austin 4-ports NICs (14e4:1657)
179ea48b 370 * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
b6541db1 371 */
179ea48b
GS
372 if ((dev->vendor == PCI_VENDOR_ID_BROADCOM && dev->device == 0x1657) ||
373 (dev->vendor == PCI_VENDOR_ID_BROADCOM && dev->device == 0x168e))
b6541db1
GS
374 edev->pe->state |= EEH_PE_CFG_RESTRICTED;
375
dadcd6d6
MQ
376 /*
377 * Cache the PE primary bus, which can't be fetched when
378 * full hotplug is in progress. In that case, all child
379 * PCI devices of the PE are expected to be removed prior
380 * to PE reset.
381 */
382 if (!edev->pe->bus)
383 edev->pe->bus = dev->bus;
29310e5e
GS
384
385 /*
386 * Enable EEH explicitly so that we will do EEH check
387 * while accessing I/O stuff
29310e5e 388 */
05b1721d 389 eeh_add_flag(EEH_ENABLED);
29310e5e
GS
390
391 /* Save memory bars */
392 eeh_save_bars(edev);
393
394 return 0;
395}
396
397/**
01f3bfb7 398 * pnv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
29310e5e
GS
399 * @pe: EEH PE
400 * @option: operation to be issued
401 *
402 * The function is used to control the EEH functionality globally.
403 * Currently, following options are support according to PAPR:
404 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
405 */
01f3bfb7 406static int pnv_eeh_set_option(struct eeh_pe *pe, int option)
29310e5e
GS
407{
408 struct pci_controller *hose = pe->phb;
409 struct pnv_phb *phb = hose->private_data;
7e3e4f8d
GS
410 bool freeze_pe = false;
411 int opt, ret = 0;
412 s64 rc;
413
414 /* Sanity check on option */
415 switch (option) {
416 case EEH_OPT_DISABLE:
417 return -EPERM;
418 case EEH_OPT_ENABLE:
419 return 0;
420 case EEH_OPT_THAW_MMIO:
421 opt = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
422 break;
423 case EEH_OPT_THAW_DMA:
424 opt = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
425 break;
426 case EEH_OPT_FREEZE_PE:
427 freeze_pe = true;
428 opt = OPAL_EEH_ACTION_SET_FREEZE_ALL;
429 break;
430 default:
431 pr_warn("%s: Invalid option %d\n", __func__, option);
432 return -EINVAL;
433 }
29310e5e 434
7e3e4f8d
GS
435 /* If PHB supports compound PE, to handle it */
436 if (freeze_pe) {
437 if (phb->freeze_pe) {
438 phb->freeze_pe(phb, pe->addr);
439 } else {
440 rc = opal_pci_eeh_freeze_set(phb->opal_id,
441 pe->addr, opt);
442 if (rc != OPAL_SUCCESS) {
443 pr_warn("%s: Failure %lld freezing "
444 "PHB#%x-PE#%x\n",
445 __func__, rc,
446 phb->hose->global_number, pe->addr);
447 ret = -EIO;
448 }
449 }
450 } else {
451 if (phb->unfreeze_pe) {
452 ret = phb->unfreeze_pe(phb, pe->addr, opt);
453 } else {
454 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
455 pe->addr, opt);
456 if (rc != OPAL_SUCCESS) {
457 pr_warn("%s: Failure %lld enable %d "
458 "for PHB#%x-PE#%x\n",
459 __func__, rc, option,
460 phb->hose->global_number, pe->addr);
461 ret = -EIO;
462 }
463 }
464 }
29310e5e
GS
465
466 return ret;
467}
468
469/**
01f3bfb7 470 * pnv_eeh_get_pe_addr - Retrieve PE address
29310e5e
GS
471 * @pe: EEH PE
472 *
473 * Retrieve the PE address according to the given tranditional
474 * PCI BDF (Bus/Device/Function) address.
475 */
01f3bfb7 476static int pnv_eeh_get_pe_addr(struct eeh_pe *pe)
29310e5e
GS
477{
478 return pe->addr;
479}
480
40ae5f69
GS
481static void pnv_eeh_get_phb_diag(struct eeh_pe *pe)
482{
483 struct pnv_phb *phb = pe->phb->private_data;
484 s64 rc;
485
486 rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
487 PNV_PCI_DIAG_BUF_SIZE);
488 if (rc != OPAL_SUCCESS)
489 pr_warn("%s: Failure %lld getting PHB#%x diag-data\n",
490 __func__, rc, pe->phb->global_number);
491}
492
493static int pnv_eeh_get_phb_state(struct eeh_pe *pe)
494{
495 struct pnv_phb *phb = pe->phb->private_data;
496 u8 fstate;
497 __be16 pcierr;
498 s64 rc;
499 int result = 0;
500
501 rc = opal_pci_eeh_freeze_status(phb->opal_id,
502 pe->addr,
503 &fstate,
504 &pcierr,
505 NULL);
506 if (rc != OPAL_SUCCESS) {
507 pr_warn("%s: Failure %lld getting PHB#%x state\n",
508 __func__, rc, phb->hose->global_number);
509 return EEH_STATE_NOT_SUPPORT;
510 }
511
512 /*
513 * Check PHB state. If the PHB is frozen for the
514 * first time, to dump the PHB diag-data.
515 */
516 if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
517 result = (EEH_STATE_MMIO_ACTIVE |
518 EEH_STATE_DMA_ACTIVE |
519 EEH_STATE_MMIO_ENABLED |
520 EEH_STATE_DMA_ENABLED);
521 } else if (!(pe->state & EEH_PE_ISOLATED)) {
522 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
523 pnv_eeh_get_phb_diag(pe);
524
525 if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
526 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
527 }
528
529 return result;
530}
531
532static int pnv_eeh_get_pe_state(struct eeh_pe *pe)
533{
534 struct pnv_phb *phb = pe->phb->private_data;
535 u8 fstate;
536 __be16 pcierr;
537 s64 rc;
538 int result;
539
540 /*
541 * We don't clobber hardware frozen state until PE
542 * reset is completed. In order to keep EEH core
543 * moving forward, we have to return operational
544 * state during PE reset.
545 */
546 if (pe->state & EEH_PE_RESET) {
547 result = (EEH_STATE_MMIO_ACTIVE |
548 EEH_STATE_DMA_ACTIVE |
549 EEH_STATE_MMIO_ENABLED |
550 EEH_STATE_DMA_ENABLED);
551 return result;
552 }
553
554 /*
555 * Fetch PE state from hardware. If the PHB
556 * supports compound PE, let it handle that.
557 */
558 if (phb->get_pe_state) {
559 fstate = phb->get_pe_state(phb, pe->addr);
560 } else {
561 rc = opal_pci_eeh_freeze_status(phb->opal_id,
562 pe->addr,
563 &fstate,
564 &pcierr,
565 NULL);
566 if (rc != OPAL_SUCCESS) {
567 pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
568 __func__, rc, phb->hose->global_number,
569 pe->addr);
570 return EEH_STATE_NOT_SUPPORT;
571 }
572 }
573
574 /* Figure out state */
575 switch (fstate) {
576 case OPAL_EEH_STOPPED_NOT_FROZEN:
577 result = (EEH_STATE_MMIO_ACTIVE |
578 EEH_STATE_DMA_ACTIVE |
579 EEH_STATE_MMIO_ENABLED |
580 EEH_STATE_DMA_ENABLED);
581 break;
582 case OPAL_EEH_STOPPED_MMIO_FREEZE:
583 result = (EEH_STATE_DMA_ACTIVE |
584 EEH_STATE_DMA_ENABLED);
585 break;
586 case OPAL_EEH_STOPPED_DMA_FREEZE:
587 result = (EEH_STATE_MMIO_ACTIVE |
588 EEH_STATE_MMIO_ENABLED);
589 break;
590 case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
591 result = 0;
592 break;
593 case OPAL_EEH_STOPPED_RESET:
594 result = EEH_STATE_RESET_ACTIVE;
595 break;
596 case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
597 result = EEH_STATE_UNAVAILABLE;
598 break;
599 case OPAL_EEH_STOPPED_PERM_UNAVAIL:
600 result = EEH_STATE_NOT_SUPPORT;
601 break;
602 default:
603 result = EEH_STATE_NOT_SUPPORT;
604 pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
605 __func__, phb->hose->global_number,
606 pe->addr, fstate);
607 }
608
609 /*
610 * If PHB supports compound PE, to freeze all
611 * slave PEs for consistency.
612 *
613 * If the PE is switching to frozen state for the
614 * first time, to dump the PHB diag-data.
615 */
616 if (!(result & EEH_STATE_NOT_SUPPORT) &&
617 !(result & EEH_STATE_UNAVAILABLE) &&
618 !(result & EEH_STATE_MMIO_ACTIVE) &&
619 !(result & EEH_STATE_DMA_ACTIVE) &&
620 !(pe->state & EEH_PE_ISOLATED)) {
621 if (phb->freeze_pe)
622 phb->freeze_pe(phb, pe->addr);
623
624 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
625 pnv_eeh_get_phb_diag(pe);
626
627 if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
628 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
629 }
630
631 return result;
632}
633
29310e5e 634/**
01f3bfb7 635 * pnv_eeh_get_state - Retrieve PE state
29310e5e
GS
636 * @pe: EEH PE
637 * @delay: delay while PE state is temporarily unavailable
638 *
639 * Retrieve the state of the specified PE. For IODA-compitable
640 * platform, it should be retrieved from IODA table. Therefore,
641 * we prefer passing down to hardware implementation to handle
642 * it.
643 */
01f3bfb7 644static int pnv_eeh_get_state(struct eeh_pe *pe, int *delay)
29310e5e 645{
40ae5f69 646 int ret;
29310e5e 647
40ae5f69
GS
648 if (pe->type & EEH_PE_PHB)
649 ret = pnv_eeh_get_phb_state(pe);
650 else
651 ret = pnv_eeh_get_pe_state(pe);
29310e5e 652
40ae5f69
GS
653 if (!delay)
654 return ret;
655
656 /*
657 * If the PE state is temporarily unavailable,
658 * to inform the EEH core delay for default
659 * period (1 second)
660 */
661 *delay = 0;
662 if (ret & EEH_STATE_UNAVAILABLE)
663 *delay = 1000;
29310e5e
GS
664
665 return ret;
666}
667
668/**
01f3bfb7 669 * pnv_eeh_reset - Reset the specified PE
29310e5e
GS
670 * @pe: EEH PE
671 * @option: reset option
672 *
673 * Reset the specified PE
674 */
01f3bfb7 675static int pnv_eeh_reset(struct eeh_pe *pe, int option)
29310e5e
GS
676{
677 struct pci_controller *hose = pe->phb;
678 struct pnv_phb *phb = hose->private_data;
679 int ret = -EEXIST;
680
681 if (phb->eeh_ops && phb->eeh_ops->reset)
682 ret = phb->eeh_ops->reset(pe, option);
683
684 return ret;
685}
686
687/**
01f3bfb7 688 * pnv_eeh_wait_state - Wait for PE state
29310e5e
GS
689 * @pe: EEH PE
690 * @max_wait: maximal period in microsecond
691 *
692 * Wait for the state of associated PE. It might take some time
693 * to retrieve the PE's state.
694 */
01f3bfb7 695static int pnv_eeh_wait_state(struct eeh_pe *pe, int max_wait)
29310e5e
GS
696{
697 int ret;
698 int mwait;
699
700 while (1) {
01f3bfb7 701 ret = pnv_eeh_get_state(pe, &mwait);
29310e5e
GS
702
703 /*
704 * If the PE's state is temporarily unavailable,
705 * we have to wait for the specified time. Otherwise,
706 * the PE's state will be returned immediately.
707 */
708 if (ret != EEH_STATE_UNAVAILABLE)
709 return ret;
710
711 max_wait -= mwait;
712 if (max_wait <= 0) {
0dae2743
GS
713 pr_warn("%s: Timeout getting PE#%x's state (%d)\n",
714 __func__, pe->addr, max_wait);
29310e5e
GS
715 return EEH_STATE_NOT_SUPPORT;
716 }
717
718 msleep(mwait);
719 }
720
721 return EEH_STATE_NOT_SUPPORT;
722}
723
724/**
01f3bfb7 725 * pnv_eeh_get_log - Retrieve error log
29310e5e
GS
726 * @pe: EEH PE
727 * @severity: temporary or permanent error log
728 * @drv_log: driver log to be combined with retrieved error log
729 * @len: length of driver log
730 *
731 * Retrieve the temporary or permanent error from the PE.
732 */
01f3bfb7
GS
733static int pnv_eeh_get_log(struct eeh_pe *pe, int severity,
734 char *drv_log, unsigned long len)
29310e5e 735{
95edcdea
GS
736 if (!eeh_has_flag(EEH_EARLY_DUMP_LOG))
737 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
29310e5e 738
95edcdea 739 return 0;
29310e5e
GS
740}
741
742/**
01f3bfb7 743 * pnv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
29310e5e
GS
744 * @pe: EEH PE
745 *
746 * The function will be called to reconfigure the bridges included
747 * in the specified PE so that the mulfunctional PE would be recovered
748 * again.
749 */
01f3bfb7 750static int pnv_eeh_configure_bridge(struct eeh_pe *pe)
29310e5e 751{
bbe170ed 752 return 0;
29310e5e
GS
753}
754
131c123a 755/**
01f3bfb7 756 * pnv_pe_err_inject - Inject specified error to the indicated PE
131c123a
GS
757 * @pe: the indicated PE
758 * @type: error type
759 * @func: specific error type
760 * @addr: address
761 * @mask: address mask
762 *
763 * The routine is called to inject specified error, which is
764 * determined by @type and @func, to the indicated PE for
765 * testing purpose.
766 */
01f3bfb7
GS
767static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
768 unsigned long addr, unsigned long mask)
131c123a
GS
769{
770 struct pci_controller *hose = pe->phb;
771 struct pnv_phb *phb = hose->private_data;
fa646c3c
GS
772 s64 rc;
773
774 /* Sanity check on error type */
775 if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
776 type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
777 pr_warn("%s: Invalid error type %d\n",
778 __func__, type);
779 return -ERANGE;
780 }
131c123a 781
fa646c3c
GS
782 if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
783 func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
784 pr_warn("%s: Invalid error function %d\n",
785 __func__, func);
786 return -ERANGE;
787 }
131c123a 788
fa646c3c
GS
789 /* Firmware supports error injection ? */
790 if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
791 pr_warn("%s: Firmware doesn't support error injection\n",
792 __func__);
793 return -ENXIO;
794 }
795
796 /* Do error injection */
797 rc = opal_pci_err_inject(phb->opal_id, pe->addr,
798 type, func, addr, mask);
799 if (rc != OPAL_SUCCESS) {
800 pr_warn("%s: Failure %lld injecting error "
801 "%d-%d to PHB#%x-PE#%x\n",
802 __func__, rc, type, func,
803 hose->global_number, pe->addr);
804 return -EIO;
805 }
806
807 return 0;
131c123a
GS
808}
809
01f3bfb7 810static inline bool pnv_eeh_cfg_blocked(struct device_node *dn)
d2cfbcd7
GS
811{
812 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
813
814 if (!edev || !edev->pe)
815 return false;
816
817 if (edev->pe->state & EEH_PE_CFG_BLOCKED)
818 return true;
819
820 return false;
821}
822
01f3bfb7
GS
823static int pnv_eeh_read_config(struct device_node *dn,
824 int where, int size, u32 *val)
d2cfbcd7 825{
01f3bfb7 826 if (pnv_eeh_cfg_blocked(dn)) {
d2cfbcd7
GS
827 *val = 0xFFFFFFFF;
828 return PCIBIOS_SET_FAILED;
829 }
830
831 return pnv_pci_cfg_read(dn, where, size, val);
832}
833
01f3bfb7
GS
834static int pnv_eeh_write_config(struct device_node *dn,
835 int where, int size, u32 val)
d2cfbcd7 836{
01f3bfb7 837 if (pnv_eeh_cfg_blocked(dn))
d2cfbcd7
GS
838 return PCIBIOS_SET_FAILED;
839
840 return pnv_pci_cfg_write(dn, where, size, val);
841}
842
29310e5e 843/**
01f3bfb7 844 * pnv_eeh_next_error - Retrieve next EEH error to handle
29310e5e
GS
845 * @pe: Affected PE
846 *
847 * Using OPAL API, to retrieve next EEH error for EEH core to handle
848 */
01f3bfb7 849static int pnv_eeh_next_error(struct eeh_pe **pe)
29310e5e
GS
850{
851 struct pci_controller *hose;
852 struct pnv_phb *phb = NULL;
853
854 list_for_each_entry(hose, &hose_list, list_node) {
855 phb = hose->private_data;
856 break;
857 }
858
859 if (phb && phb->eeh_ops->next_error)
860 return phb->eeh_ops->next_error(pe);
861
862 return -EEXIST;
863}
864
01f3bfb7 865static int pnv_eeh_restore_config(struct device_node *dn)
9be3becc
GS
866{
867 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
868 struct pnv_phb *phb;
869 s64 ret;
870
871 if (!edev)
872 return -EEXIST;
873
874 phb = edev->phb->private_data;
875 ret = opal_pci_reinit(phb->opal_id,
876 OPAL_REINIT_PCI_DEV, edev->config_addr);
877 if (ret) {
878 pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
879 __func__, edev->config_addr, ret);
880 return -EIO;
881 }
882
883 return 0;
884}
885
01f3bfb7 886static struct eeh_ops pnv_eeh_ops = {
29310e5e 887 .name = "powernv",
01f3bfb7
GS
888 .init = pnv_eeh_init,
889 .post_init = pnv_eeh_post_init,
29310e5e 890 .of_probe = NULL,
01f3bfb7
GS
891 .dev_probe = pnv_eeh_dev_probe,
892 .set_option = pnv_eeh_set_option,
893 .get_pe_addr = pnv_eeh_get_pe_addr,
894 .get_state = pnv_eeh_get_state,
895 .reset = pnv_eeh_reset,
896 .wait_state = pnv_eeh_wait_state,
897 .get_log = pnv_eeh_get_log,
898 .configure_bridge = pnv_eeh_configure_bridge,
899 .err_inject = pnv_eeh_err_inject,
900 .read_config = pnv_eeh_read_config,
901 .write_config = pnv_eeh_write_config,
902 .next_error = pnv_eeh_next_error,
903 .restore_config = pnv_eeh_restore_config
29310e5e
GS
904};
905
906/**
907 * eeh_powernv_init - Register platform dependent EEH operations
908 *
909 * EEH initialization on powernv platform. This function should be
910 * called before any EEH related functions.
911 */
912static int __init eeh_powernv_init(void)
913{
914 int ret = -EINVAL;
915
bb593c00 916 eeh_set_pe_aux_size(PNV_PCI_DIAG_BUF_SIZE);
01f3bfb7 917 ret = eeh_ops_register(&pnv_eeh_ops);
29310e5e
GS
918 if (!ret)
919 pr_info("EEH: PowerNV platform initialized\n");
920 else
921 pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
922
923 return ret;
924}
b14726c5 925machine_early_initcall(powernv, eeh_powernv_init);