blob: 3f48f6df1cf3937e1f854f9d2bfcb84e27f946c6 [file] [log] [blame]
Gavin Shan29310e52013-06-20 13:21:13 +08001/*
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>
Gavin Shan4cf17442015-02-16 14:45:41 +110015#include <linux/debugfs.h>
Gavin Shan29310e52013-06-20 13:21:13 +080016#include <linux/delay.h>
17#include <linux/export.h>
18#include <linux/init.h>
Alistair Popple79231442015-05-15 14:06:40 +100019#include <linux/interrupt.h>
Gavin Shan29310e52013-06-20 13:21:13 +080020#include <linux/list.h>
21#include <linux/msi.h>
22#include <linux/of.h>
23#include <linux/pci.h>
24#include <linux/proc_fs.h>
25#include <linux/rbtree.h>
26#include <linux/sched.h>
27#include <linux/seq_file.h>
28#include <linux/spinlock.h>
29
30#include <asm/eeh.h>
31#include <asm/eeh_event.h>
32#include <asm/firmware.h>
33#include <asm/io.h>
34#include <asm/iommu.h>
35#include <asm/machdep.h>
36#include <asm/msi_bitmap.h>
37#include <asm/opal.h>
38#include <asm/ppc-pci.h>
Gavin Shan9c0e1ec2016-05-20 16:41:39 +100039#include <asm/pnv-pci.h>
Gavin Shan29310e52013-06-20 13:21:13 +080040
41#include "powernv.h"
42#include "pci.h"
43
Gavin Shan4cf17442015-02-16 14:45:41 +110044static bool pnv_eeh_nb_init = false;
Alistair Popple79231442015-05-15 14:06:40 +100045static int eeh_event_irq = -EINVAL;
Gavin Shan4cf17442015-02-16 14:45:41 +110046
Gavin Shan01f3bfb2015-02-16 14:45:39 +110047static int pnv_eeh_init(void)
Gavin Shan29310e52013-06-20 13:21:13 +080048{
Gavin Shandc561fb2014-07-17 14:41:39 +100049 struct pci_controller *hose;
50 struct pnv_phb *phb;
Russell Currey5cb1f8f2017-06-14 14:19:59 +100051 int max_diag_size = PNV_PCI_DIAG_BUF_SIZE;
Gavin Shandc561fb2014-07-17 14:41:39 +100052
Stewart Smithe4d54f72015-12-09 17:18:20 +110053 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
54 pr_warn("%s: OPAL is required !\n",
Gavin Shan0dae2742014-07-17 14:41:41 +100055 __func__);
Gavin Shan29310e52013-06-20 13:21:13 +080056 return -EINVAL;
57 }
58
Gavin Shan05b17212014-07-17 14:41:38 +100059 /* Set probe mode */
60 eeh_add_flag(EEH_PROBE_MODE_DEV);
Gavin Shan29310e52013-06-20 13:21:13 +080061
Gavin Shandc561fb2014-07-17 14:41:39 +100062 /*
63 * P7IOC blocks PCI config access to frozen PE, but PHB3
64 * doesn't do that. So we have to selectively enable I/O
65 * prior to collecting error log.
66 */
67 list_for_each_entry(hose, &hose_list, list_node) {
68 phb = hose->private_data;
69
70 if (phb->model == PNV_PHB_MODEL_P7IOC)
71 eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
Gavin Shan2aa5cf92014-11-25 09:27:00 +110072
Russell Currey5cb1f8f2017-06-14 14:19:59 +100073 if (phb->diag_data_size > max_diag_size)
74 max_diag_size = phb->diag_data_size;
75
Gavin Shan2aa5cf92014-11-25 09:27:00 +110076 /*
77 * PE#0 should be regarded as valid by EEH core
78 * if it's not the reserved one. Currently, we
Gavin Shan608fb9c2015-10-08 14:58:57 +110079 * have the reserved PE#255 and PE#127 for PHB3
Gavin Shan2aa5cf92014-11-25 09:27:00 +110080 * and P7IOC separately. So we should regard
Gavin Shan608fb9c2015-10-08 14:58:57 +110081 * PE#0 as valid for PHB3 and P7IOC.
Gavin Shan2aa5cf92014-11-25 09:27:00 +110082 */
Gavin Shan92b8f132016-05-03 15:41:24 +100083 if (phb->ioda.reserved_pe_idx != 0)
Gavin Shan2aa5cf92014-11-25 09:27:00 +110084 eeh_add_flag(EEH_VALID_PE_ZERO);
85
Gavin Shandc561fb2014-07-17 14:41:39 +100086 break;
87 }
88
Russell Currey5cb1f8f2017-06-14 14:19:59 +100089 eeh_set_pe_aux_size(max_diag_size);
90
Gavin Shan29310e52013-06-20 13:21:13 +080091 return 0;
92}
93
Alistair Popple79231442015-05-15 14:06:40 +100094static irqreturn_t pnv_eeh_event(int irq, void *data)
Gavin Shan4cf17442015-02-16 14:45:41 +110095{
Gavin Shan4cf17442015-02-16 14:45:41 +110096 /*
Alistair Popple79231442015-05-15 14:06:40 +100097 * We simply send a special EEH event if EEH has been
98 * enabled. We don't care about EEH events until we've
99 * finished processing the outstanding ones. Event processing
100 * gets unmasked in next_error() if EEH is enabled.
Gavin Shan4cf17442015-02-16 14:45:41 +1100101 */
Alistair Popple79231442015-05-15 14:06:40 +1000102 disable_irq_nosync(irq);
Gavin Shan4cf17442015-02-16 14:45:41 +1100103
104 if (eeh_enabled())
105 eeh_send_failure_event(NULL);
Gavin Shan4cf17442015-02-16 14:45:41 +1100106
Alistair Popple79231442015-05-15 14:06:40 +1000107 return IRQ_HANDLED;
Gavin Shan4cf17442015-02-16 14:45:41 +1100108}
109
Gavin Shan4cf17442015-02-16 14:45:41 +1100110#ifdef CONFIG_DEBUG_FS
111static ssize_t pnv_eeh_ei_write(struct file *filp,
112 const char __user *user_buf,
113 size_t count, loff_t *ppos)
114{
115 struct pci_controller *hose = filp->private_data;
116 struct eeh_dev *edev;
117 struct eeh_pe *pe;
118 int pe_no, type, func;
119 unsigned long addr, mask;
120 char buf[50];
121 int ret;
122
123 if (!eeh_ops || !eeh_ops->err_inject)
124 return -ENXIO;
125
126 /* Copy over argument buffer */
127 ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
128 if (!ret)
129 return -EFAULT;
130
131 /* Retrieve parameters */
132 ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
133 &pe_no, &type, &func, &addr, &mask);
134 if (ret != 5)
135 return -EINVAL;
136
137 /* Retrieve PE */
138 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
139 if (!edev)
140 return -ENOMEM;
141 edev->phb = hose;
142 edev->pe_config_addr = pe_no;
143 pe = eeh_pe_get(edev);
144 kfree(edev);
145 if (!pe)
146 return -ENODEV;
147
148 /* Do error injection */
149 ret = eeh_ops->err_inject(pe, type, func, addr, mask);
150 return ret < 0 ? ret : count;
151}
152
153static const struct file_operations pnv_eeh_ei_fops = {
154 .open = simple_open,
155 .llseek = no_llseek,
156 .write = pnv_eeh_ei_write,
157};
158
159static int pnv_eeh_dbgfs_set(void *data, int offset, u64 val)
160{
161 struct pci_controller *hose = data;
162 struct pnv_phb *phb = hose->private_data;
163
164 out_be64(phb->regs + offset, val);
165 return 0;
166}
167
168static int pnv_eeh_dbgfs_get(void *data, int offset, u64 *val)
169{
170 struct pci_controller *hose = data;
171 struct pnv_phb *phb = hose->private_data;
172
173 *val = in_be64(phb->regs + offset);
174 return 0;
175}
176
Gavin Shanccc96622016-02-09 15:50:24 +1100177#define PNV_EEH_DBGFS_ENTRY(name, reg) \
178static int pnv_eeh_dbgfs_set_##name(void *data, u64 val) \
179{ \
180 return pnv_eeh_dbgfs_set(data, reg, val); \
181} \
182 \
183static int pnv_eeh_dbgfs_get_##name(void *data, u64 *val) \
184{ \
185 return pnv_eeh_dbgfs_get(data, reg, val); \
186} \
187 \
188DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_dbgfs_ops_##name, \
189 pnv_eeh_dbgfs_get_##name, \
190 pnv_eeh_dbgfs_set_##name, \
191 "0x%llx\n")
Gavin Shan4cf17442015-02-16 14:45:41 +1100192
Gavin Shanccc96622016-02-09 15:50:24 +1100193PNV_EEH_DBGFS_ENTRY(outb, 0xD10);
194PNV_EEH_DBGFS_ENTRY(inbA, 0xD90);
195PNV_EEH_DBGFS_ENTRY(inbB, 0xE10);
Gavin Shan4cf17442015-02-16 14:45:41 +1100196
Gavin Shan4cf17442015-02-16 14:45:41 +1100197#endif /* CONFIG_DEBUG_FS */
198
Gavin Shan29310e52013-06-20 13:21:13 +0800199/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100200 * pnv_eeh_post_init - EEH platform dependent post initialization
Gavin Shan29310e52013-06-20 13:21:13 +0800201 *
202 * EEH platform dependent post initialization on powernv. When
203 * the function is called, the EEH PEs and devices should have
204 * been built. If the I/O cache staff has been built, EEH is
205 * ready to supply service.
206 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100207static int pnv_eeh_post_init(void)
Gavin Shan29310e52013-06-20 13:21:13 +0800208{
209 struct pci_controller *hose;
210 struct pnv_phb *phb;
211 int ret = 0;
212
Gavin Shan4cf17442015-02-16 14:45:41 +1100213 /* Register OPAL event notifier */
214 if (!pnv_eeh_nb_init) {
Alistair Popple79231442015-05-15 14:06:40 +1000215 eeh_event_irq = opal_event_request(ilog2(OPAL_EVENT_PCI_ERROR));
216 if (eeh_event_irq < 0) {
217 pr_err("%s: Can't register OPAL event interrupt (%d)\n",
218 __func__, eeh_event_irq);
219 return eeh_event_irq;
220 }
221
222 ret = request_irq(eeh_event_irq, pnv_eeh_event,
223 IRQ_TYPE_LEVEL_HIGH, "opal-eeh", NULL);
224 if (ret < 0) {
225 irq_dispose_mapping(eeh_event_irq);
226 pr_err("%s: Can't request OPAL event interrupt (%d)\n",
227 __func__, eeh_event_irq);
Gavin Shan4cf17442015-02-16 14:45:41 +1100228 return ret;
229 }
230
231 pnv_eeh_nb_init = true;
232 }
233
Alistair Popple79231442015-05-15 14:06:40 +1000234 if (!eeh_enabled())
235 disable_irq(eeh_event_irq);
236
Gavin Shan29310e52013-06-20 13:21:13 +0800237 list_for_each_entry(hose, &hose_list, list_node) {
238 phb = hose->private_data;
239
Gavin Shan4cf17442015-02-16 14:45:41 +1100240 /*
241 * If EEH is enabled, we're going to rely on that.
242 * Otherwise, we restore to conventional mechanism
243 * to clear frozen PE during PCI config access.
244 */
245 if (eeh_enabled())
246 phb->flags |= PNV_PHB_FLAG_EEH;
247 else
248 phb->flags &= ~PNV_PHB_FLAG_EEH;
249
250 /* Create debugfs entries */
251#ifdef CONFIG_DEBUG_FS
252 if (phb->has_dbgfs || !phb->dbgfs)
253 continue;
254
255 phb->has_dbgfs = 1;
256 debugfs_create_file("err_injct", 0200,
257 phb->dbgfs, hose,
258 &pnv_eeh_ei_fops);
259
260 debugfs_create_file("err_injct_outbound", 0600,
261 phb->dbgfs, hose,
Gavin Shanccc96622016-02-09 15:50:24 +1100262 &pnv_eeh_dbgfs_ops_outb);
Gavin Shan4cf17442015-02-16 14:45:41 +1100263 debugfs_create_file("err_injct_inboundA", 0600,
264 phb->dbgfs, hose,
Gavin Shanccc96622016-02-09 15:50:24 +1100265 &pnv_eeh_dbgfs_ops_inbA);
Gavin Shan4cf17442015-02-16 14:45:41 +1100266 debugfs_create_file("err_injct_inboundB", 0600,
267 phb->dbgfs, hose,
Gavin Shanccc96622016-02-09 15:50:24 +1100268 &pnv_eeh_dbgfs_ops_inbB);
Gavin Shan4cf17442015-02-16 14:45:41 +1100269#endif /* CONFIG_DEBUG_FS */
Gavin Shan29310e52013-06-20 13:21:13 +0800270 }
271
272 return ret;
273}
274
Gavin Shan4d6186c2015-10-08 14:58:58 +1100275static int pnv_eeh_find_cap(struct pci_dn *pdn, int cap)
Gavin Shanff57b452015-03-17 16:15:06 +1100276{
Gavin Shan4d6186c2015-10-08 14:58:58 +1100277 int pos = PCI_CAPABILITY_LIST;
278 int cnt = 48; /* Maximal number of capabilities */
279 u32 status, id;
Gavin Shanff57b452015-03-17 16:15:06 +1100280
281 if (!pdn)
282 return 0;
283
Gavin Shan4d6186c2015-10-08 14:58:58 +1100284 /* Check if the device supports capabilities */
Gavin Shanff57b452015-03-17 16:15:06 +1100285 pnv_pci_cfg_read(pdn, PCI_STATUS, 2, &status);
286 if (!(status & PCI_STATUS_CAP_LIST))
287 return 0;
288
Gavin Shanff57b452015-03-17 16:15:06 +1100289 while (cnt--) {
290 pnv_pci_cfg_read(pdn, pos, 1, &pos);
291 if (pos < 0x40)
292 break;
293
294 pos &= ~3;
295 pnv_pci_cfg_read(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
296 if (id == 0xff)
297 break;
298
299 /* Found */
300 if (id == cap)
301 return pos;
302
303 /* Next one */
304 pos += PCI_CAP_LIST_NEXT;
305 }
306
307 return 0;
308}
309
310static int pnv_eeh_find_ecap(struct pci_dn *pdn, int cap)
311{
312 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
313 u32 header;
314 int pos = 256, ttl = (4096 - 256) / 8;
315
316 if (!edev || !edev->pcie_cap)
317 return 0;
318 if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
319 return 0;
320 else if (!header)
321 return 0;
322
323 while (ttl-- > 0) {
324 if (PCI_EXT_CAP_ID(header) == cap && pos)
325 return pos;
326
327 pos = PCI_EXT_CAP_NEXT(header);
328 if (pos < 256)
329 break;
330
331 if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
332 break;
333 }
334
335 return 0;
336}
337
Gavin Shan29310e52013-06-20 13:21:13 +0800338/**
Gavin Shanff57b452015-03-17 16:15:06 +1100339 * pnv_eeh_probe - Do probe on PCI device
340 * @pdn: PCI device node
341 * @data: unused
Gavin Shan29310e52013-06-20 13:21:13 +0800342 *
343 * When EEH module is installed during system boot, all PCI devices
344 * are checked one by one to see if it supports EEH. The function
345 * is introduced for the purpose. By default, EEH has been enabled
346 * on all PCI devices. That's to say, we only need do necessary
347 * initialization on the corresponding eeh device and create PE
348 * accordingly.
349 *
350 * It's notable that's unsafe to retrieve the EEH device through
351 * the corresponding PCI device. During the PCI device hotplug, which
352 * was possiblly triggered by EEH core, the binding between EEH device
353 * and the PCI device isn't built yet.
354 */
Gavin Shanff57b452015-03-17 16:15:06 +1100355static void *pnv_eeh_probe(struct pci_dn *pdn, void *data)
Gavin Shan29310e52013-06-20 13:21:13 +0800356{
Gavin Shanff57b452015-03-17 16:15:06 +1100357 struct pci_controller *hose = pdn->phb;
Gavin Shan29310e52013-06-20 13:21:13 +0800358 struct pnv_phb *phb = hose->private_data;
Gavin Shanff57b452015-03-17 16:15:06 +1100359 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
360 uint32_t pcie_flags;
Mike Qiudadcd6d2014-06-26 02:58:47 -0400361 int ret;
Gavin Shan29310e52013-06-20 13:21:13 +0800362
363 /*
364 * When probing the root bridge, which doesn't have any
365 * subordinate PCI devices. We don't have OF node for
366 * the root bridge. So it's not reasonable to continue
367 * the probing.
368 */
Gavin Shanff57b452015-03-17 16:15:06 +1100369 if (!edev || edev->pe)
370 return NULL;
Gavin Shan29310e52013-06-20 13:21:13 +0800371
372 /* Skip for PCI-ISA bridge */
Gavin Shanff57b452015-03-17 16:15:06 +1100373 if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_ISA)
374 return NULL;
Gavin Shan29310e52013-06-20 13:21:13 +0800375
376 /* Initialize eeh device */
Gavin Shanff57b452015-03-17 16:15:06 +1100377 edev->class_code = pdn->class_code;
Gavin Shanab55d212013-07-24 10:25:01 +0800378 edev->mode &= 0xFFFFFF00;
Gavin Shanff57b452015-03-17 16:15:06 +1100379 edev->pcix_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
380 edev->pcie_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
Wei Yang9312bc52016-03-04 10:53:09 +1100381 edev->af_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_AF);
Gavin Shanff57b452015-03-17 16:15:06 +1100382 edev->aer_cap = pnv_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
383 if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
Gavin Shan4b83bd42013-07-24 10:24:59 +0800384 edev->mode |= EEH_DEV_BRIDGE;
Gavin Shanff57b452015-03-17 16:15:06 +1100385 if (edev->pcie_cap) {
386 pnv_pci_cfg_read(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
387 2, &pcie_flags);
388 pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
389 if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
390 edev->mode |= EEH_DEV_ROOT_PORT;
391 else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
392 edev->mode |= EEH_DEV_DS_PORT;
393 }
Gavin Shan4b83bd42013-07-24 10:24:59 +0800394 }
395
Gavin Shanff57b452015-03-17 16:15:06 +1100396 edev->config_addr = (pdn->busno << 8) | (pdn->devfn);
397 edev->pe_config_addr = phb->ioda.pe_rmap[edev->config_addr];
Gavin Shan29310e52013-06-20 13:21:13 +0800398
399 /* Create PE */
Mike Qiudadcd6d2014-06-26 02:58:47 -0400400 ret = eeh_add_to_parent_pe(edev);
401 if (ret) {
Russell Currey1f52f172016-11-16 14:02:15 +1100402 pr_warn("%s: Can't add PCI dev %04x:%02x:%02x.%01x to parent PE (%x)\n",
Gavin Shanff57b452015-03-17 16:15:06 +1100403 __func__, hose->global_number, pdn->busno,
404 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn), ret);
405 return NULL;
Mike Qiudadcd6d2014-06-26 02:58:47 -0400406 }
407
408 /*
Gavin Shanb6541db2014-10-01 17:07:53 +1000409 * If the PE contains any one of following adapters, the
410 * PCI config space can't be accessed when dumping EEH log.
411 * Otherwise, we will run into fenced PHB caused by shortage
412 * of outbound credits in the adapter. The PCI config access
413 * should be blocked until PE reset. MMIO access is dropped
414 * by hardware certainly. In order to drop PCI config requests,
415 * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
416 * will be checked in the backend for PE state retrival. If
417 * the PE becomes frozen for the first time and the flag has
418 * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
419 * that PE to block its config space.
420 *
Gavin Shanc374ed22017-04-19 14:46:24 +1000421 * Broadcom BCM5718 2-ports NICs (14e4:1656)
Gavin Shanb6541db2014-10-01 17:07:53 +1000422 * Broadcom Austin 4-ports NICs (14e4:1657)
Gavin Shan353169a2015-10-15 15:22:35 +1100423 * Broadcom Shiner 4-ports 1G NICs (14e4:168a)
Gavin Shan179ea482014-10-03 14:58:32 +1000424 * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
Gavin Shanb6541db2014-10-01 17:07:53 +1000425 */
Gavin Shanff57b452015-03-17 16:15:06 +1100426 if ((pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
Gavin Shanc374ed22017-04-19 14:46:24 +1000427 pdn->device_id == 0x1656) ||
428 (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
Gavin Shanff57b452015-03-17 16:15:06 +1100429 pdn->device_id == 0x1657) ||
430 (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
Gavin Shan353169a2015-10-15 15:22:35 +1100431 pdn->device_id == 0x168a) ||
432 (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
Gavin Shanff57b452015-03-17 16:15:06 +1100433 pdn->device_id == 0x168e))
Gavin Shanb6541db2014-10-01 17:07:53 +1000434 edev->pe->state |= EEH_PE_CFG_RESTRICTED;
435
436 /*
Mike Qiudadcd6d2014-06-26 02:58:47 -0400437 * Cache the PE primary bus, which can't be fetched when
438 * full hotplug is in progress. In that case, all child
439 * PCI devices of the PE are expected to be removed prior
440 * to PE reset.
441 */
Gavin Shan05ba75f2016-02-09 15:50:21 +1100442 if (!(edev->pe->state & EEH_PE_PRI_BUS)) {
Gavin Shanff57b452015-03-17 16:15:06 +1100443 edev->pe->bus = pci_find_bus(hose->global_number,
444 pdn->busno);
Gavin Shan05ba75f2016-02-09 15:50:21 +1100445 if (edev->pe->bus)
446 edev->pe->state |= EEH_PE_PRI_BUS;
447 }
Gavin Shan29310e52013-06-20 13:21:13 +0800448
449 /*
450 * Enable EEH explicitly so that we will do EEH check
451 * while accessing I/O stuff
Gavin Shan29310e52013-06-20 13:21:13 +0800452 */
Gavin Shan05b17212014-07-17 14:41:38 +1000453 eeh_add_flag(EEH_ENABLED);
Gavin Shan29310e52013-06-20 13:21:13 +0800454
455 /* Save memory bars */
456 eeh_save_bars(edev);
457
Gavin Shanff57b452015-03-17 16:15:06 +1100458 return NULL;
Gavin Shan29310e52013-06-20 13:21:13 +0800459}
460
461/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100462 * pnv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
Gavin Shan29310e52013-06-20 13:21:13 +0800463 * @pe: EEH PE
464 * @option: operation to be issued
465 *
466 * The function is used to control the EEH functionality globally.
467 * Currently, following options are support according to PAPR:
468 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
469 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100470static int pnv_eeh_set_option(struct eeh_pe *pe, int option)
Gavin Shan29310e52013-06-20 13:21:13 +0800471{
472 struct pci_controller *hose = pe->phb;
473 struct pnv_phb *phb = hose->private_data;
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100474 bool freeze_pe = false;
Gavin Shanf94337182015-10-08 14:58:59 +1100475 int opt;
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100476 s64 rc;
Gavin Shan29310e52013-06-20 13:21:13 +0800477
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100478 switch (option) {
479 case EEH_OPT_DISABLE:
480 return -EPERM;
481 case EEH_OPT_ENABLE:
482 return 0;
483 case EEH_OPT_THAW_MMIO:
484 opt = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
485 break;
486 case EEH_OPT_THAW_DMA:
487 opt = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
488 break;
489 case EEH_OPT_FREEZE_PE:
490 freeze_pe = true;
491 opt = OPAL_EEH_ACTION_SET_FREEZE_ALL;
492 break;
493 default:
494 pr_warn("%s: Invalid option %d\n", __func__, option);
495 return -EINVAL;
496 }
497
Gavin Shanf94337182015-10-08 14:58:59 +1100498 /* Freeze master and slave PEs if PHB supports compound PEs */
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100499 if (freeze_pe) {
500 if (phb->freeze_pe) {
501 phb->freeze_pe(phb, pe->addr);
Gavin Shanf94337182015-10-08 14:58:59 +1100502 return 0;
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100503 }
Gavin Shanf94337182015-10-08 14:58:59 +1100504
505 rc = opal_pci_eeh_freeze_set(phb->opal_id, pe->addr, opt);
506 if (rc != OPAL_SUCCESS) {
507 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
508 __func__, rc, phb->hose->global_number,
509 pe->addr);
510 return -EIO;
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100511 }
Gavin Shanf94337182015-10-08 14:58:59 +1100512
513 return 0;
Gavin Shan7e3e4f82015-02-16 14:45:44 +1100514 }
Gavin Shan29310e52013-06-20 13:21:13 +0800515
Gavin Shanf94337182015-10-08 14:58:59 +1100516 /* Unfreeze master and slave PEs if PHB supports */
517 if (phb->unfreeze_pe)
518 return phb->unfreeze_pe(phb, pe->addr, opt);
519
520 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe->addr, opt);
521 if (rc != OPAL_SUCCESS) {
522 pr_warn("%s: Failure %lld enable %d for PHB#%x-PE#%x\n",
523 __func__, rc, option, phb->hose->global_number,
524 pe->addr);
525 return -EIO;
526 }
527
528 return 0;
Gavin Shan29310e52013-06-20 13:21:13 +0800529}
530
531/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100532 * pnv_eeh_get_pe_addr - Retrieve PE address
Gavin Shan29310e52013-06-20 13:21:13 +0800533 * @pe: EEH PE
534 *
535 * Retrieve the PE address according to the given tranditional
536 * PCI BDF (Bus/Device/Function) address.
537 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100538static int pnv_eeh_get_pe_addr(struct eeh_pe *pe)
Gavin Shan29310e52013-06-20 13:21:13 +0800539{
540 return pe->addr;
541}
542
Gavin Shan40ae5f62015-02-16 14:45:45 +1100543static void pnv_eeh_get_phb_diag(struct eeh_pe *pe)
544{
545 struct pnv_phb *phb = pe->phb->private_data;
546 s64 rc;
547
548 rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
Russell Currey5cb1f8f2017-06-14 14:19:59 +1000549 phb->diag_data_size);
Gavin Shan40ae5f62015-02-16 14:45:45 +1100550 if (rc != OPAL_SUCCESS)
551 pr_warn("%s: Failure %lld getting PHB#%x diag-data\n",
552 __func__, rc, pe->phb->global_number);
553}
554
555static int pnv_eeh_get_phb_state(struct eeh_pe *pe)
556{
557 struct pnv_phb *phb = pe->phb->private_data;
558 u8 fstate;
559 __be16 pcierr;
560 s64 rc;
561 int result = 0;
562
563 rc = opal_pci_eeh_freeze_status(phb->opal_id,
564 pe->addr,
565 &fstate,
566 &pcierr,
567 NULL);
568 if (rc != OPAL_SUCCESS) {
569 pr_warn("%s: Failure %lld getting PHB#%x state\n",
570 __func__, rc, phb->hose->global_number);
571 return EEH_STATE_NOT_SUPPORT;
572 }
573
574 /*
575 * Check PHB state. If the PHB is frozen for the
576 * first time, to dump the PHB diag-data.
577 */
578 if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
579 result = (EEH_STATE_MMIO_ACTIVE |
580 EEH_STATE_DMA_ACTIVE |
581 EEH_STATE_MMIO_ENABLED |
582 EEH_STATE_DMA_ENABLED);
583 } else if (!(pe->state & EEH_PE_ISOLATED)) {
584 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
585 pnv_eeh_get_phb_diag(pe);
586
587 if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
588 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
589 }
590
591 return result;
592}
593
594static int pnv_eeh_get_pe_state(struct eeh_pe *pe)
595{
596 struct pnv_phb *phb = pe->phb->private_data;
597 u8 fstate;
598 __be16 pcierr;
599 s64 rc;
600 int result;
601
602 /*
603 * We don't clobber hardware frozen state until PE
604 * reset is completed. In order to keep EEH core
605 * moving forward, we have to return operational
606 * state during PE reset.
607 */
608 if (pe->state & EEH_PE_RESET) {
609 result = (EEH_STATE_MMIO_ACTIVE |
610 EEH_STATE_DMA_ACTIVE |
611 EEH_STATE_MMIO_ENABLED |
612 EEH_STATE_DMA_ENABLED);
613 return result;
614 }
615
616 /*
617 * Fetch PE state from hardware. If the PHB
618 * supports compound PE, let it handle that.
619 */
620 if (phb->get_pe_state) {
621 fstate = phb->get_pe_state(phb, pe->addr);
622 } else {
623 rc = opal_pci_eeh_freeze_status(phb->opal_id,
624 pe->addr,
625 &fstate,
626 &pcierr,
627 NULL);
628 if (rc != OPAL_SUCCESS) {
629 pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
630 __func__, rc, phb->hose->global_number,
631 pe->addr);
632 return EEH_STATE_NOT_SUPPORT;
633 }
634 }
635
636 /* Figure out state */
637 switch (fstate) {
638 case OPAL_EEH_STOPPED_NOT_FROZEN:
639 result = (EEH_STATE_MMIO_ACTIVE |
640 EEH_STATE_DMA_ACTIVE |
641 EEH_STATE_MMIO_ENABLED |
642 EEH_STATE_DMA_ENABLED);
643 break;
644 case OPAL_EEH_STOPPED_MMIO_FREEZE:
645 result = (EEH_STATE_DMA_ACTIVE |
646 EEH_STATE_DMA_ENABLED);
647 break;
648 case OPAL_EEH_STOPPED_DMA_FREEZE:
649 result = (EEH_STATE_MMIO_ACTIVE |
650 EEH_STATE_MMIO_ENABLED);
651 break;
652 case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
653 result = 0;
654 break;
655 case OPAL_EEH_STOPPED_RESET:
656 result = EEH_STATE_RESET_ACTIVE;
657 break;
658 case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
659 result = EEH_STATE_UNAVAILABLE;
660 break;
661 case OPAL_EEH_STOPPED_PERM_UNAVAIL:
662 result = EEH_STATE_NOT_SUPPORT;
663 break;
664 default:
665 result = EEH_STATE_NOT_SUPPORT;
666 pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
667 __func__, phb->hose->global_number,
668 pe->addr, fstate);
669 }
670
671 /*
672 * If PHB supports compound PE, to freeze all
673 * slave PEs for consistency.
674 *
675 * If the PE is switching to frozen state for the
676 * first time, to dump the PHB diag-data.
677 */
678 if (!(result & EEH_STATE_NOT_SUPPORT) &&
679 !(result & EEH_STATE_UNAVAILABLE) &&
680 !(result & EEH_STATE_MMIO_ACTIVE) &&
681 !(result & EEH_STATE_DMA_ACTIVE) &&
682 !(pe->state & EEH_PE_ISOLATED)) {
683 if (phb->freeze_pe)
684 phb->freeze_pe(phb, pe->addr);
685
686 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
687 pnv_eeh_get_phb_diag(pe);
688
689 if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
690 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
691 }
692
693 return result;
694}
695
Gavin Shan29310e52013-06-20 13:21:13 +0800696/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100697 * pnv_eeh_get_state - Retrieve PE state
Gavin Shan29310e52013-06-20 13:21:13 +0800698 * @pe: EEH PE
699 * @delay: delay while PE state is temporarily unavailable
700 *
701 * Retrieve the state of the specified PE. For IODA-compitable
702 * platform, it should be retrieved from IODA table. Therefore,
703 * we prefer passing down to hardware implementation to handle
704 * it.
705 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +1100706static int pnv_eeh_get_state(struct eeh_pe *pe, int *delay)
Gavin Shan29310e52013-06-20 13:21:13 +0800707{
Gavin Shan40ae5f62015-02-16 14:45:45 +1100708 int ret;
Gavin Shan29310e52013-06-20 13:21:13 +0800709
Gavin Shan40ae5f62015-02-16 14:45:45 +1100710 if (pe->type & EEH_PE_PHB)
711 ret = pnv_eeh_get_phb_state(pe);
712 else
713 ret = pnv_eeh_get_pe_state(pe);
Gavin Shan29310e52013-06-20 13:21:13 +0800714
Gavin Shan40ae5f62015-02-16 14:45:45 +1100715 if (!delay)
716 return ret;
717
718 /*
719 * If the PE state is temporarily unavailable,
720 * to inform the EEH core delay for default
721 * period (1 second)
722 */
723 *delay = 0;
724 if (ret & EEH_STATE_UNAVAILABLE)
725 *delay = 1000;
Gavin Shan29310e52013-06-20 13:21:13 +0800726
727 return ret;
728}
729
Gavin Shanebe22532016-05-20 16:41:38 +1000730static s64 pnv_eeh_poll(unsigned long id)
Gavin Shancadf3642015-02-16 14:45:47 +1100731{
732 s64 rc = OPAL_HARDWARE;
733
734 while (1) {
Gavin Shanebe22532016-05-20 16:41:38 +1000735 rc = opal_pci_poll(id);
Gavin Shancadf3642015-02-16 14:45:47 +1100736 if (rc <= 0)
737 break;
738
739 if (system_state < SYSTEM_RUNNING)
740 udelay(1000 * rc);
741 else
742 msleep(rc);
743 }
744
745 return rc;
746}
747
748int pnv_eeh_phb_reset(struct pci_controller *hose, int option)
749{
750 struct pnv_phb *phb = hose->private_data;
751 s64 rc = OPAL_HARDWARE;
752
753 pr_debug("%s: Reset PHB#%x, option=%d\n",
754 __func__, hose->global_number, option);
755
756 /* Issue PHB complete reset request */
757 if (option == EEH_RESET_FUNDAMENTAL ||
758 option == EEH_RESET_HOT)
759 rc = opal_pci_reset(phb->opal_id,
760 OPAL_RESET_PHB_COMPLETE,
761 OPAL_ASSERT_RESET);
762 else if (option == EEH_RESET_DEACTIVATE)
763 rc = opal_pci_reset(phb->opal_id,
764 OPAL_RESET_PHB_COMPLETE,
765 OPAL_DEASSERT_RESET);
766 if (rc < 0)
767 goto out;
768
769 /*
770 * Poll state of the PHB until the request is done
771 * successfully. The PHB reset is usually PHB complete
772 * reset followed by hot reset on root bus. So we also
773 * need the PCI bus settlement delay.
774 */
Gavin Shanfbce44d2016-06-24 16:44:19 +1000775 if (rc > 0)
776 rc = pnv_eeh_poll(phb->opal_id);
Gavin Shancadf3642015-02-16 14:45:47 +1100777 if (option == EEH_RESET_DEACTIVATE) {
778 if (system_state < SYSTEM_RUNNING)
779 udelay(1000 * EEH_PE_RST_SETTLE_TIME);
780 else
781 msleep(EEH_PE_RST_SETTLE_TIME);
782 }
783out:
784 if (rc != OPAL_SUCCESS)
785 return -EIO;
786
787 return 0;
788}
789
790static int pnv_eeh_root_reset(struct pci_controller *hose, int option)
791{
792 struct pnv_phb *phb = hose->private_data;
793 s64 rc = OPAL_HARDWARE;
794
795 pr_debug("%s: Reset PHB#%x, option=%d\n",
796 __func__, hose->global_number, option);
797
798 /*
799 * During the reset deassert time, we needn't care
800 * the reset scope because the firmware does nothing
801 * for fundamental or hot reset during deassert phase.
802 */
803 if (option == EEH_RESET_FUNDAMENTAL)
804 rc = opal_pci_reset(phb->opal_id,
805 OPAL_RESET_PCI_FUNDAMENTAL,
806 OPAL_ASSERT_RESET);
807 else if (option == EEH_RESET_HOT)
808 rc = opal_pci_reset(phb->opal_id,
809 OPAL_RESET_PCI_HOT,
810 OPAL_ASSERT_RESET);
811 else if (option == EEH_RESET_DEACTIVATE)
812 rc = opal_pci_reset(phb->opal_id,
813 OPAL_RESET_PCI_HOT,
814 OPAL_DEASSERT_RESET);
815 if (rc < 0)
816 goto out;
817
818 /* Poll state of the PHB until the request is done */
Gavin Shanfbce44d2016-06-24 16:44:19 +1000819 if (rc > 0)
820 rc = pnv_eeh_poll(phb->opal_id);
Gavin Shancadf3642015-02-16 14:45:47 +1100821 if (option == EEH_RESET_DEACTIVATE)
822 msleep(EEH_PE_RST_SETTLE_TIME);
823out:
824 if (rc != OPAL_SUCCESS)
825 return -EIO;
826
827 return 0;
828}
829
Gavin Shan9c0e1ec2016-05-20 16:41:39 +1000830static int __pnv_eeh_bridge_reset(struct pci_dev *dev, int option)
Gavin Shancadf3642015-02-16 14:45:47 +1100831{
Gavin Shan0bd78582015-03-17 16:15:07 +1100832 struct pci_dn *pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
833 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Gavin Shancadf3642015-02-16 14:45:47 +1100834 int aer = edev ? edev->aer_cap : 0;
835 u32 ctrl;
836
837 pr_debug("%s: Reset PCI bus %04x:%02x with option %d\n",
838 __func__, pci_domain_nr(dev->bus),
839 dev->bus->number, option);
840
841 switch (option) {
842 case EEH_RESET_FUNDAMENTAL:
843 case EEH_RESET_HOT:
844 /* Don't report linkDown event */
845 if (aer) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100846 eeh_ops->read_config(pdn, aer + PCI_ERR_UNCOR_MASK,
Gavin Shancadf3642015-02-16 14:45:47 +1100847 4, &ctrl);
848 ctrl |= PCI_ERR_UNC_SURPDN;
Gavin Shan0bd78582015-03-17 16:15:07 +1100849 eeh_ops->write_config(pdn, aer + PCI_ERR_UNCOR_MASK,
Gavin Shancadf3642015-02-16 14:45:47 +1100850 4, ctrl);
851 }
852
Gavin Shan0bd78582015-03-17 16:15:07 +1100853 eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &ctrl);
Gavin Shancadf3642015-02-16 14:45:47 +1100854 ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
Gavin Shan0bd78582015-03-17 16:15:07 +1100855 eeh_ops->write_config(pdn, PCI_BRIDGE_CONTROL, 2, ctrl);
Gavin Shancadf3642015-02-16 14:45:47 +1100856
857 msleep(EEH_PE_RST_HOLD_TIME);
858 break;
859 case EEH_RESET_DEACTIVATE:
Gavin Shan0bd78582015-03-17 16:15:07 +1100860 eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &ctrl);
Gavin Shancadf3642015-02-16 14:45:47 +1100861 ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
Gavin Shan0bd78582015-03-17 16:15:07 +1100862 eeh_ops->write_config(pdn, PCI_BRIDGE_CONTROL, 2, ctrl);
Gavin Shancadf3642015-02-16 14:45:47 +1100863
864 msleep(EEH_PE_RST_SETTLE_TIME);
865
866 /* Continue reporting linkDown event */
867 if (aer) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100868 eeh_ops->read_config(pdn, aer + PCI_ERR_UNCOR_MASK,
Gavin Shancadf3642015-02-16 14:45:47 +1100869 4, &ctrl);
870 ctrl &= ~PCI_ERR_UNC_SURPDN;
Gavin Shan0bd78582015-03-17 16:15:07 +1100871 eeh_ops->write_config(pdn, aer + PCI_ERR_UNCOR_MASK,
Gavin Shancadf3642015-02-16 14:45:47 +1100872 4, ctrl);
873 }
874
875 break;
876 }
877
878 return 0;
879}
880
Gavin Shan9c0e1ec2016-05-20 16:41:39 +1000881static int pnv_eeh_bridge_reset(struct pci_dev *pdev, int option)
882{
883 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
884 struct pnv_phb *phb = hose->private_data;
885 struct device_node *dn = pci_device_to_OF_node(pdev);
886 uint64_t id = PCI_SLOT_ID(phb->opal_id,
887 (pdev->bus->number << 8) | pdev->devfn);
888 uint8_t scope;
889 int64_t rc;
890
891 /* Hot reset to the bus if firmware cannot handle */
892 if (!dn || !of_get_property(dn, "ibm,reset-by-firmware", NULL))
893 return __pnv_eeh_bridge_reset(pdev, option);
894
895 switch (option) {
896 case EEH_RESET_FUNDAMENTAL:
897 scope = OPAL_RESET_PCI_FUNDAMENTAL;
898 break;
899 case EEH_RESET_HOT:
900 scope = OPAL_RESET_PCI_HOT;
901 break;
902 case EEH_RESET_DEACTIVATE:
903 return 0;
904 default:
905 dev_dbg(&pdev->dev, "%s: Unsupported reset %d\n",
906 __func__, option);
907 return -EINVAL;
908 }
909
910 rc = opal_pci_reset(id, scope, OPAL_ASSERT_RESET);
911 if (rc <= OPAL_SUCCESS)
912 goto out;
913
914 rc = pnv_eeh_poll(id);
915out:
916 return (rc == OPAL_SUCCESS) ? 0 : -EIO;
917}
918
Gavin Shancadf3642015-02-16 14:45:47 +1100919void pnv_pci_reset_secondary_bus(struct pci_dev *dev)
920{
Michael Ellerman848912e2016-05-12 19:43:37 +1000921 struct pci_controller *hose;
922
923 if (pci_is_root_bus(dev->bus)) {
924 hose = pci_bus_to_host(dev->bus);
925 pnv_eeh_root_reset(hose, EEH_RESET_HOT);
926 pnv_eeh_root_reset(hose, EEH_RESET_DEACTIVATE);
927 } else {
928 pnv_eeh_bridge_reset(dev, EEH_RESET_HOT);
929 pnv_eeh_bridge_reset(dev, EEH_RESET_DEACTIVATE);
930 }
Gavin Shancadf3642015-02-16 14:45:47 +1100931}
932
Wei Yang9312bc52016-03-04 10:53:09 +1100933static void pnv_eeh_wait_for_pending(struct pci_dn *pdn, const char *type,
934 int pos, u16 mask)
935{
936 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
937 int i, status = 0;
938
939 /* Wait for Transaction Pending bit to be cleared */
940 for (i = 0; i < 4; i++) {
941 eeh_ops->read_config(pdn, pos, 2, &status);
942 if (!(status & mask))
943 return;
944
945 msleep((1 << i) * 100);
946 }
947
948 pr_warn("%s: Pending transaction while issuing %sFLR to %04x:%02x:%02x.%01x\n",
949 __func__, type,
950 edev->phb->global_number, pdn->busno,
951 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
952}
953
954static int pnv_eeh_do_flr(struct pci_dn *pdn, int option)
955{
956 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
957 u32 reg = 0;
958
959 if (WARN_ON(!edev->pcie_cap))
960 return -ENOTTY;
961
962 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCAP, 4, &reg);
963 if (!(reg & PCI_EXP_DEVCAP_FLR))
964 return -ENOTTY;
965
966 switch (option) {
967 case EEH_RESET_HOT:
968 case EEH_RESET_FUNDAMENTAL:
969 pnv_eeh_wait_for_pending(pdn, "",
970 edev->pcie_cap + PCI_EXP_DEVSTA,
971 PCI_EXP_DEVSTA_TRPND);
972 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
973 4, &reg);
974 reg |= PCI_EXP_DEVCTL_BCR_FLR;
975 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
976 4, reg);
977 msleep(EEH_PE_RST_HOLD_TIME);
978 break;
979 case EEH_RESET_DEACTIVATE:
980 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
981 4, &reg);
982 reg &= ~PCI_EXP_DEVCTL_BCR_FLR;
983 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
984 4, reg);
985 msleep(EEH_PE_RST_SETTLE_TIME);
986 break;
987 }
988
989 return 0;
990}
991
992static int pnv_eeh_do_af_flr(struct pci_dn *pdn, int option)
993{
994 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
995 u32 cap = 0;
996
997 if (WARN_ON(!edev->af_cap))
998 return -ENOTTY;
999
1000 eeh_ops->read_config(pdn, edev->af_cap + PCI_AF_CAP, 1, &cap);
1001 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
1002 return -ENOTTY;
1003
1004 switch (option) {
1005 case EEH_RESET_HOT:
1006 case EEH_RESET_FUNDAMENTAL:
1007 /*
1008 * Wait for Transaction Pending bit to clear. A word-aligned
1009 * test is used, so we use the conrol offset rather than status
1010 * and shift the test bit to match.
1011 */
1012 pnv_eeh_wait_for_pending(pdn, "AF",
1013 edev->af_cap + PCI_AF_CTRL,
1014 PCI_AF_STATUS_TP << 8);
1015 eeh_ops->write_config(pdn, edev->af_cap + PCI_AF_CTRL,
1016 1, PCI_AF_CTRL_FLR);
1017 msleep(EEH_PE_RST_HOLD_TIME);
1018 break;
1019 case EEH_RESET_DEACTIVATE:
1020 eeh_ops->write_config(pdn, edev->af_cap + PCI_AF_CTRL, 1, 0);
1021 msleep(EEH_PE_RST_SETTLE_TIME);
1022 break;
1023 }
1024
1025 return 0;
1026}
1027
1028static int pnv_eeh_reset_vf_pe(struct eeh_pe *pe, int option)
1029{
1030 struct eeh_dev *edev;
1031 struct pci_dn *pdn;
1032 int ret;
1033
1034 /* The VF PE should have only one child device */
1035 edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, list);
1036 pdn = eeh_dev_to_pdn(edev);
1037 if (!pdn)
1038 return -ENXIO;
1039
1040 ret = pnv_eeh_do_flr(pdn, option);
1041 if (!ret)
1042 return ret;
1043
1044 return pnv_eeh_do_af_flr(pdn, option);
1045}
1046
Gavin Shan29310e52013-06-20 13:21:13 +08001047/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001048 * pnv_eeh_reset - Reset the specified PE
Gavin Shan29310e52013-06-20 13:21:13 +08001049 * @pe: EEH PE
1050 * @option: reset option
1051 *
Gavin Shancadf3642015-02-16 14:45:47 +11001052 * Do reset on the indicated PE. For PCI bus sensitive PE,
1053 * we need to reset the parent p2p bridge. The PHB has to
1054 * be reinitialized if the p2p bridge is root bridge. For
1055 * PCI device sensitive PE, we will try to reset the device
1056 * through FLR. For now, we don't have OPAL APIs to do HARD
1057 * reset yet, so all reset would be SOFT (HOT) reset.
Gavin Shan29310e52013-06-20 13:21:13 +08001058 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001059static int pnv_eeh_reset(struct eeh_pe *pe, int option)
Gavin Shan29310e52013-06-20 13:21:13 +08001060{
1061 struct pci_controller *hose = pe->phb;
Gavin Shan4fad4942016-05-03 15:41:44 +10001062 struct pnv_phb *phb;
Gavin Shancadf3642015-02-16 14:45:47 +11001063 struct pci_bus *bus;
Gavin Shan4fad4942016-05-03 15:41:44 +10001064 int64_t rc;
Gavin Shan29310e52013-06-20 13:21:13 +08001065
Gavin Shancadf3642015-02-16 14:45:47 +11001066 /*
1067 * For PHB reset, we always have complete reset. For those PEs whose
1068 * primary bus derived from root complex (root bus) or root port
1069 * (usually bus#1), we apply hot or fundamental reset on the root port.
1070 * For other PEs, we always have hot reset on the PE primary bus.
1071 *
1072 * Here, we have different design to pHyp, which always clear the
1073 * frozen state during PE reset. However, the good idea here from
1074 * benh is to keep frozen state before we get PE reset done completely
1075 * (until BAR restore). With the frozen state, HW drops illegal IO
1076 * or MMIO access, which can incur recrusive frozen PE during PE
1077 * reset. The side effect is that EEH core has to clear the frozen
1078 * state explicitly after BAR restore.
1079 */
Gavin Shan4fad4942016-05-03 15:41:44 +10001080 if (pe->type & EEH_PE_PHB)
1081 return pnv_eeh_phb_reset(hose, option);
Gavin Shancadf3642015-02-16 14:45:47 +11001082
Gavin Shan4fad4942016-05-03 15:41:44 +10001083 /*
1084 * The frozen PE might be caused by PAPR error injection
1085 * registers, which are expected to be cleared after hitting
1086 * frozen PE as stated in the hardware spec. Unfortunately,
1087 * that's not true on P7IOC. So we have to clear it manually
1088 * to avoid recursive EEH errors during recovery.
1089 */
1090 phb = hose->private_data;
1091 if (phb->model == PNV_PHB_MODEL_P7IOC &&
1092 (option == EEH_RESET_HOT ||
1093 option == EEH_RESET_FUNDAMENTAL)) {
1094 rc = opal_pci_reset(phb->opal_id,
1095 OPAL_RESET_PHB_ERROR,
1096 OPAL_ASSERT_RESET);
1097 if (rc != OPAL_SUCCESS) {
1098 pr_warn("%s: Failure %lld clearing error injection registers\n",
1099 __func__, rc);
1100 return -EIO;
Gavin Shancadf3642015-02-16 14:45:47 +11001101 }
Gavin Shancadf3642015-02-16 14:45:47 +11001102 }
Gavin Shan29310e52013-06-20 13:21:13 +08001103
Russell Curreye98ddb72016-09-12 14:17:23 +10001104 if (pe->type & EEH_PE_VF)
1105 return pnv_eeh_reset_vf_pe(pe, option);
1106
Gavin Shan4fad4942016-05-03 15:41:44 +10001107 bus = eeh_pe_bus_get(pe);
Russell Currey04fec21c2016-09-12 14:17:22 +10001108 if (!bus) {
Russell Currey1f52f172016-11-16 14:02:15 +11001109 pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
Russell Currey04fec21c2016-09-12 14:17:22 +10001110 __func__, pe->phb->global_number, pe->addr);
1111 return -EIO;
1112 }
Gavin Shan4fad4942016-05-03 15:41:44 +10001113
Andrew Donnellanb7da1232016-07-22 17:16:35 +10001114 /*
1115 * If dealing with the root bus (or the bus underneath the
1116 * root port), we reset the bus underneath the root port.
1117 *
1118 * The cxl driver depends on this behaviour for bi-modal card
1119 * switching.
1120 */
Gavin Shan4fad4942016-05-03 15:41:44 +10001121 if (pci_is_root_bus(bus) ||
1122 pci_is_root_bus(bus->parent))
1123 return pnv_eeh_root_reset(hose, option);
1124
1125 return pnv_eeh_bridge_reset(bus->self, option);
Gavin Shan29310e52013-06-20 13:21:13 +08001126}
1127
1128/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001129 * pnv_eeh_wait_state - Wait for PE state
Gavin Shan29310e52013-06-20 13:21:13 +08001130 * @pe: EEH PE
Wei Yang2ac39902015-04-27 09:25:10 +08001131 * @max_wait: maximal period in millisecond
Gavin Shan29310e52013-06-20 13:21:13 +08001132 *
1133 * Wait for the state of associated PE. It might take some time
1134 * to retrieve the PE's state.
1135 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001136static int pnv_eeh_wait_state(struct eeh_pe *pe, int max_wait)
Gavin Shan29310e52013-06-20 13:21:13 +08001137{
1138 int ret;
1139 int mwait;
1140
1141 while (1) {
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001142 ret = pnv_eeh_get_state(pe, &mwait);
Gavin Shan29310e52013-06-20 13:21:13 +08001143
1144 /*
1145 * If the PE's state is temporarily unavailable,
1146 * we have to wait for the specified time. Otherwise,
1147 * the PE's state will be returned immediately.
1148 */
1149 if (ret != EEH_STATE_UNAVAILABLE)
1150 return ret;
1151
Gavin Shan29310e52013-06-20 13:21:13 +08001152 if (max_wait <= 0) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001153 pr_warn("%s: Timeout getting PE#%x's state (%d)\n",
1154 __func__, pe->addr, max_wait);
Gavin Shan29310e52013-06-20 13:21:13 +08001155 return EEH_STATE_NOT_SUPPORT;
1156 }
1157
Wei Yange17866d2015-04-27 09:25:11 +08001158 max_wait -= mwait;
Gavin Shan29310e52013-06-20 13:21:13 +08001159 msleep(mwait);
1160 }
1161
1162 return EEH_STATE_NOT_SUPPORT;
1163}
1164
1165/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001166 * pnv_eeh_get_log - Retrieve error log
Gavin Shan29310e52013-06-20 13:21:13 +08001167 * @pe: EEH PE
1168 * @severity: temporary or permanent error log
1169 * @drv_log: driver log to be combined with retrieved error log
1170 * @len: length of driver log
1171 *
1172 * Retrieve the temporary or permanent error from the PE.
1173 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001174static int pnv_eeh_get_log(struct eeh_pe *pe, int severity,
1175 char *drv_log, unsigned long len)
Gavin Shan29310e52013-06-20 13:21:13 +08001176{
Gavin Shan95edcde2015-02-16 14:45:42 +11001177 if (!eeh_has_flag(EEH_EARLY_DUMP_LOG))
1178 pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
Gavin Shan29310e52013-06-20 13:21:13 +08001179
Gavin Shan95edcde2015-02-16 14:45:42 +11001180 return 0;
Gavin Shan29310e52013-06-20 13:21:13 +08001181}
1182
1183/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001184 * pnv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
Gavin Shan29310e52013-06-20 13:21:13 +08001185 * @pe: EEH PE
1186 *
1187 * The function will be called to reconfigure the bridges included
1188 * in the specified PE so that the mulfunctional PE would be recovered
1189 * again.
1190 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001191static int pnv_eeh_configure_bridge(struct eeh_pe *pe)
Gavin Shan29310e52013-06-20 13:21:13 +08001192{
Gavin Shanbbe170e2015-02-16 14:45:43 +11001193 return 0;
Gavin Shan29310e52013-06-20 13:21:13 +08001194}
1195
1196/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001197 * pnv_pe_err_inject - Inject specified error to the indicated PE
Gavin Shan131c1232014-09-30 12:38:56 +10001198 * @pe: the indicated PE
1199 * @type: error type
1200 * @func: specific error type
1201 * @addr: address
1202 * @mask: address mask
1203 *
1204 * The routine is called to inject specified error, which is
1205 * determined by @type and @func, to the indicated PE for
1206 * testing purpose.
1207 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001208static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
1209 unsigned long addr, unsigned long mask)
Gavin Shan131c1232014-09-30 12:38:56 +10001210{
1211 struct pci_controller *hose = pe->phb;
1212 struct pnv_phb *phb = hose->private_data;
Gavin Shanfa646c32015-02-16 14:45:40 +11001213 s64 rc;
Gavin Shan131c1232014-09-30 12:38:56 +10001214
Gavin Shanfa646c32015-02-16 14:45:40 +11001215 if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
1216 type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
1217 pr_warn("%s: Invalid error type %d\n",
1218 __func__, type);
1219 return -ERANGE;
1220 }
Gavin Shan131c1232014-09-30 12:38:56 +10001221
Gavin Shanfa646c32015-02-16 14:45:40 +11001222 if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
1223 func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
1224 pr_warn("%s: Invalid error function %d\n",
1225 __func__, func);
1226 return -ERANGE;
1227 }
1228
1229 /* Firmware supports error injection ? */
1230 if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
1231 pr_warn("%s: Firmware doesn't support error injection\n",
1232 __func__);
1233 return -ENXIO;
1234 }
1235
1236 /* Do error injection */
1237 rc = opal_pci_err_inject(phb->opal_id, pe->addr,
1238 type, func, addr, mask);
1239 if (rc != OPAL_SUCCESS) {
1240 pr_warn("%s: Failure %lld injecting error "
1241 "%d-%d to PHB#%x-PE#%x\n",
1242 __func__, rc, type, func,
1243 hose->global_number, pe->addr);
1244 return -EIO;
1245 }
1246
1247 return 0;
Gavin Shan131c1232014-09-30 12:38:56 +10001248}
1249
Gavin Shan0bd78582015-03-17 16:15:07 +11001250static inline bool pnv_eeh_cfg_blocked(struct pci_dn *pdn)
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001251{
Gavin Shan0bd78582015-03-17 16:15:07 +11001252 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001253
1254 if (!edev || !edev->pe)
1255 return false;
1256
Wei Yang9312bc52016-03-04 10:53:09 +11001257 /*
1258 * We will issue FLR or AF FLR to all VFs, which are contained
1259 * in VF PE. It relies on the EEH PCI config accessors. So we
1260 * can't block them during the window.
1261 */
1262 if (edev->physfn && (edev->pe->state & EEH_PE_RESET))
1263 return false;
1264
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001265 if (edev->pe->state & EEH_PE_CFG_BLOCKED)
1266 return true;
1267
1268 return false;
1269}
1270
Gavin Shan0bd78582015-03-17 16:15:07 +11001271static int pnv_eeh_read_config(struct pci_dn *pdn,
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001272 int where, int size, u32 *val)
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001273{
Gavin Shan3532a7412015-03-17 16:15:03 +11001274 if (!pdn)
1275 return PCIBIOS_DEVICE_NOT_FOUND;
1276
Gavin Shan0bd78582015-03-17 16:15:07 +11001277 if (pnv_eeh_cfg_blocked(pdn)) {
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001278 *val = 0xFFFFFFFF;
1279 return PCIBIOS_SET_FAILED;
1280 }
1281
Gavin Shan3532a7412015-03-17 16:15:03 +11001282 return pnv_pci_cfg_read(pdn, where, size, val);
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001283}
1284
Gavin Shan0bd78582015-03-17 16:15:07 +11001285static int pnv_eeh_write_config(struct pci_dn *pdn,
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001286 int where, int size, u32 val)
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001287{
Gavin Shan3532a7412015-03-17 16:15:03 +11001288 if (!pdn)
1289 return PCIBIOS_DEVICE_NOT_FOUND;
1290
Gavin Shan0bd78582015-03-17 16:15:07 +11001291 if (pnv_eeh_cfg_blocked(pdn))
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001292 return PCIBIOS_SET_FAILED;
1293
Gavin Shan3532a7412015-03-17 16:15:03 +11001294 return pnv_pci_cfg_write(pdn, where, size, val);
Gavin Shand2cfbcd2014-10-01 17:07:51 +10001295}
1296
Gavin Shan2a485ad2015-02-16 14:45:46 +11001297static void pnv_eeh_dump_hub_diag_common(struct OpalIoP7IOCErrorData *data)
1298{
1299 /* GEM */
1300 if (data->gemXfir || data->gemRfir ||
1301 data->gemRirqfir || data->gemMask || data->gemRwof)
1302 pr_info(" GEM: %016llx %016llx %016llx %016llx %016llx\n",
1303 be64_to_cpu(data->gemXfir),
1304 be64_to_cpu(data->gemRfir),
1305 be64_to_cpu(data->gemRirqfir),
1306 be64_to_cpu(data->gemMask),
1307 be64_to_cpu(data->gemRwof));
1308
1309 /* LEM */
1310 if (data->lemFir || data->lemErrMask ||
1311 data->lemAction0 || data->lemAction1 || data->lemWof)
1312 pr_info(" LEM: %016llx %016llx %016llx %016llx %016llx\n",
1313 be64_to_cpu(data->lemFir),
1314 be64_to_cpu(data->lemErrMask),
1315 be64_to_cpu(data->lemAction0),
1316 be64_to_cpu(data->lemAction1),
1317 be64_to_cpu(data->lemWof));
1318}
1319
1320static void pnv_eeh_get_and_dump_hub_diag(struct pci_controller *hose)
1321{
1322 struct pnv_phb *phb = hose->private_data;
Russell Currey5cb1f8f2017-06-14 14:19:59 +10001323 struct OpalIoP7IOCErrorData *data =
1324 (struct OpalIoP7IOCErrorData*)phb->diag_data;
Gavin Shan2a485ad2015-02-16 14:45:46 +11001325 long rc;
1326
1327 rc = opal_pci_get_hub_diag_data(phb->hub_id, data, sizeof(*data));
1328 if (rc != OPAL_SUCCESS) {
1329 pr_warn("%s: Failed to get HUB#%llx diag-data (%ld)\n",
1330 __func__, phb->hub_id, rc);
1331 return;
1332 }
1333
Gavin Shana7032132016-08-02 14:10:30 +10001334 switch (be16_to_cpu(data->type)) {
Gavin Shan2a485ad2015-02-16 14:45:46 +11001335 case OPAL_P7IOC_DIAG_TYPE_RGC:
1336 pr_info("P7IOC diag-data for RGC\n\n");
1337 pnv_eeh_dump_hub_diag_common(data);
1338 if (data->rgc.rgcStatus || data->rgc.rgcLdcp)
1339 pr_info(" RGC: %016llx %016llx\n",
1340 be64_to_cpu(data->rgc.rgcStatus),
1341 be64_to_cpu(data->rgc.rgcLdcp));
1342 break;
1343 case OPAL_P7IOC_DIAG_TYPE_BI:
1344 pr_info("P7IOC diag-data for BI %s\n\n",
1345 data->bi.biDownbound ? "Downbound" : "Upbound");
1346 pnv_eeh_dump_hub_diag_common(data);
1347 if (data->bi.biLdcp0 || data->bi.biLdcp1 ||
1348 data->bi.biLdcp2 || data->bi.biFenceStatus)
1349 pr_info(" BI: %016llx %016llx %016llx %016llx\n",
1350 be64_to_cpu(data->bi.biLdcp0),
1351 be64_to_cpu(data->bi.biLdcp1),
1352 be64_to_cpu(data->bi.biLdcp2),
1353 be64_to_cpu(data->bi.biFenceStatus));
1354 break;
1355 case OPAL_P7IOC_DIAG_TYPE_CI:
1356 pr_info("P7IOC diag-data for CI Port %d\n\n",
1357 data->ci.ciPort);
1358 pnv_eeh_dump_hub_diag_common(data);
1359 if (data->ci.ciPortStatus || data->ci.ciPortLdcp)
1360 pr_info(" CI: %016llx %016llx\n",
1361 be64_to_cpu(data->ci.ciPortStatus),
1362 be64_to_cpu(data->ci.ciPortLdcp));
1363 break;
1364 case OPAL_P7IOC_DIAG_TYPE_MISC:
1365 pr_info("P7IOC diag-data for MISC\n\n");
1366 pnv_eeh_dump_hub_diag_common(data);
1367 break;
1368 case OPAL_P7IOC_DIAG_TYPE_I2C:
1369 pr_info("P7IOC diag-data for I2C\n\n");
1370 pnv_eeh_dump_hub_diag_common(data);
1371 break;
1372 default:
1373 pr_warn("%s: Invalid type of HUB#%llx diag-data (%d)\n",
1374 __func__, phb->hub_id, data->type);
1375 }
1376}
1377
1378static int pnv_eeh_get_pe(struct pci_controller *hose,
1379 u16 pe_no, struct eeh_pe **pe)
1380{
1381 struct pnv_phb *phb = hose->private_data;
1382 struct pnv_ioda_pe *pnv_pe;
1383 struct eeh_pe *dev_pe;
1384 struct eeh_dev edev;
1385
1386 /*
1387 * If PHB supports compound PE, to fetch
1388 * the master PE because slave PE is invisible
1389 * to EEH core.
1390 */
1391 pnv_pe = &phb->ioda.pe_array[pe_no];
1392 if (pnv_pe->flags & PNV_IODA_PE_SLAVE) {
1393 pnv_pe = pnv_pe->master;
1394 WARN_ON(!pnv_pe ||
1395 !(pnv_pe->flags & PNV_IODA_PE_MASTER));
1396 pe_no = pnv_pe->pe_number;
1397 }
1398
1399 /* Find the PE according to PE# */
1400 memset(&edev, 0, sizeof(struct eeh_dev));
1401 edev.phb = hose;
1402 edev.pe_config_addr = pe_no;
1403 dev_pe = eeh_pe_get(&edev);
1404 if (!dev_pe)
1405 return -EEXIST;
1406
1407 /* Freeze the (compound) PE */
1408 *pe = dev_pe;
1409 if (!(dev_pe->state & EEH_PE_ISOLATED))
1410 phb->freeze_pe(phb, pe_no);
1411
1412 /*
1413 * At this point, we're sure the (compound) PE should
1414 * have been frozen. However, we still need poke until
1415 * hitting the frozen PE on top level.
1416 */
1417 dev_pe = dev_pe->parent;
1418 while (dev_pe && !(dev_pe->type & EEH_PE_PHB)) {
1419 int ret;
1420 int active_flags = (EEH_STATE_MMIO_ACTIVE |
1421 EEH_STATE_DMA_ACTIVE);
1422
1423 ret = eeh_ops->get_state(dev_pe, NULL);
1424 if (ret <= 0 || (ret & active_flags) == active_flags) {
1425 dev_pe = dev_pe->parent;
1426 continue;
1427 }
1428
1429 /* Frozen parent PE */
1430 *pe = dev_pe;
1431 if (!(dev_pe->state & EEH_PE_ISOLATED))
1432 phb->freeze_pe(phb, dev_pe->addr);
1433
1434 /* Next one */
1435 dev_pe = dev_pe->parent;
1436 }
1437
1438 return 0;
1439}
1440
Gavin Shan131c1232014-09-30 12:38:56 +10001441/**
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001442 * pnv_eeh_next_error - Retrieve next EEH error to handle
Gavin Shan29310e52013-06-20 13:21:13 +08001443 * @pe: Affected PE
1444 *
Gavin Shan2a485ad2015-02-16 14:45:46 +11001445 * The function is expected to be called by EEH core while it gets
1446 * special EEH event (without binding PE). The function calls to
1447 * OPAL APIs for next error to handle. The informational error is
1448 * handled internally by platform. However, the dead IOC, dead PHB,
1449 * fenced PHB and frozen PE should be handled by EEH core eventually.
Gavin Shan29310e52013-06-20 13:21:13 +08001450 */
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001451static int pnv_eeh_next_error(struct eeh_pe **pe)
Gavin Shan29310e52013-06-20 13:21:13 +08001452{
1453 struct pci_controller *hose;
Gavin Shan2a485ad2015-02-16 14:45:46 +11001454 struct pnv_phb *phb;
1455 struct eeh_pe *phb_pe, *parent_pe;
1456 __be64 frozen_pe_no;
1457 __be16 err_type, severity;
1458 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1459 long rc;
1460 int state, ret = EEH_NEXT_ERR_NONE;
1461
1462 /*
Alistair Popple79231442015-05-15 14:06:40 +10001463 * While running here, it's safe to purge the event queue. The
1464 * event should still be masked.
Gavin Shan2a485ad2015-02-16 14:45:46 +11001465 */
1466 eeh_remove_event(NULL, false);
Gavin Shan29310e52013-06-20 13:21:13 +08001467
1468 list_for_each_entry(hose, &hose_list, list_node) {
Gavin Shan2a485ad2015-02-16 14:45:46 +11001469 /*
1470 * If the subordinate PCI buses of the PHB has been
1471 * removed or is exactly under error recovery, we
1472 * needn't take care of it any more.
1473 */
Gavin Shan29310e52013-06-20 13:21:13 +08001474 phb = hose->private_data;
Gavin Shan2a485ad2015-02-16 14:45:46 +11001475 phb_pe = eeh_phb_pe_get(hose);
1476 if (!phb_pe || (phb_pe->state & EEH_PE_ISOLATED))
1477 continue;
1478
1479 rc = opal_pci_next_error(phb->opal_id,
1480 &frozen_pe_no, &err_type, &severity);
1481 if (rc != OPAL_SUCCESS) {
1482 pr_devel("%s: Invalid return value on "
1483 "PHB#%x (0x%lx) from opal_pci_next_error",
1484 __func__, hose->global_number, rc);
1485 continue;
1486 }
1487
1488 /* If the PHB doesn't have error, stop processing */
1489 if (be16_to_cpu(err_type) == OPAL_EEH_NO_ERROR ||
1490 be16_to_cpu(severity) == OPAL_EEH_SEV_NO_ERROR) {
1491 pr_devel("%s: No error found on PHB#%x\n",
1492 __func__, hose->global_number);
1493 continue;
1494 }
1495
1496 /*
1497 * Processing the error. We're expecting the error with
1498 * highest priority reported upon multiple errors on the
1499 * specific PHB.
1500 */
1501 pr_devel("%s: Error (%d, %d, %llu) on PHB#%x\n",
1502 __func__, be16_to_cpu(err_type),
1503 be16_to_cpu(severity), be64_to_cpu(frozen_pe_no),
1504 hose->global_number);
1505 switch (be16_to_cpu(err_type)) {
1506 case OPAL_EEH_IOC_ERROR:
1507 if (be16_to_cpu(severity) == OPAL_EEH_SEV_IOC_DEAD) {
1508 pr_err("EEH: dead IOC detected\n");
1509 ret = EEH_NEXT_ERR_DEAD_IOC;
1510 } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
1511 pr_info("EEH: IOC informative error "
1512 "detected\n");
1513 pnv_eeh_get_and_dump_hub_diag(hose);
1514 ret = EEH_NEXT_ERR_NONE;
1515 }
1516
1517 break;
1518 case OPAL_EEH_PHB_ERROR:
1519 if (be16_to_cpu(severity) == OPAL_EEH_SEV_PHB_DEAD) {
1520 *pe = phb_pe;
1521 pr_err("EEH: dead PHB#%x detected, "
1522 "location: %s\n",
1523 hose->global_number,
1524 eeh_pe_loc_get(phb_pe));
1525 ret = EEH_NEXT_ERR_DEAD_PHB;
1526 } else if (be16_to_cpu(severity) ==
1527 OPAL_EEH_SEV_PHB_FENCED) {
1528 *pe = phb_pe;
1529 pr_err("EEH: Fenced PHB#%x detected, "
1530 "location: %s\n",
1531 hose->global_number,
1532 eeh_pe_loc_get(phb_pe));
1533 ret = EEH_NEXT_ERR_FENCED_PHB;
1534 } else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
1535 pr_info("EEH: PHB#%x informative error "
1536 "detected, location: %s\n",
1537 hose->global_number,
1538 eeh_pe_loc_get(phb_pe));
1539 pnv_eeh_get_phb_diag(phb_pe);
1540 pnv_pci_dump_phb_diag_data(hose, phb_pe->data);
1541 ret = EEH_NEXT_ERR_NONE;
1542 }
1543
1544 break;
1545 case OPAL_EEH_PE_ERROR:
1546 /*
1547 * If we can't find the corresponding PE, we
1548 * just try to unfreeze.
1549 */
1550 if (pnv_eeh_get_pe(hose,
1551 be64_to_cpu(frozen_pe_no), pe)) {
Gavin Shan2a485ad2015-02-16 14:45:46 +11001552 pr_info("EEH: Clear non-existing PHB#%x-PE#%llx\n",
Gavin Shan0f36db72015-05-12 17:05:22 +10001553 hose->global_number, be64_to_cpu(frozen_pe_no));
Gavin Shan2a485ad2015-02-16 14:45:46 +11001554 pr_info("EEH: PHB location: %s\n",
1555 eeh_pe_loc_get(phb_pe));
Gavin Shan79cd9522015-05-12 17:05:32 +10001556
1557 /* Dump PHB diag-data */
1558 rc = opal_pci_get_phb_diag_data2(phb->opal_id,
Russell Currey5cb1f8f2017-06-14 14:19:59 +10001559 phb->diag_data, phb->diag_data_size);
Gavin Shan79cd9522015-05-12 17:05:32 +10001560 if (rc == OPAL_SUCCESS)
1561 pnv_pci_dump_phb_diag_data(hose,
Russell Currey5cb1f8f2017-06-14 14:19:59 +10001562 phb->diag_data);
Gavin Shan79cd9522015-05-12 17:05:32 +10001563
1564 /* Try best to clear it */
Gavin Shan2a485ad2015-02-16 14:45:46 +11001565 opal_pci_eeh_freeze_clear(phb->opal_id,
Gavin Shand63e51b2016-08-02 14:10:29 +10001566 be64_to_cpu(frozen_pe_no),
Gavin Shan2a485ad2015-02-16 14:45:46 +11001567 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
1568 ret = EEH_NEXT_ERR_NONE;
1569 } else if ((*pe)->state & EEH_PE_ISOLATED ||
1570 eeh_pe_passed(*pe)) {
1571 ret = EEH_NEXT_ERR_NONE;
1572 } else {
1573 pr_err("EEH: Frozen PE#%x "
1574 "on PHB#%x detected\n",
1575 (*pe)->addr,
1576 (*pe)->phb->global_number);
1577 pr_err("EEH: PE location: %s, "
1578 "PHB location: %s\n",
1579 eeh_pe_loc_get(*pe),
1580 eeh_pe_loc_get(phb_pe));
1581 ret = EEH_NEXT_ERR_FROZEN_PE;
1582 }
1583
1584 break;
1585 default:
1586 pr_warn("%s: Unexpected error type %d\n",
1587 __func__, be16_to_cpu(err_type));
1588 }
1589
1590 /*
1591 * EEH core will try recover from fenced PHB or
1592 * frozen PE. In the time for frozen PE, EEH core
1593 * enable IO path for that before collecting logs,
1594 * but it ruins the site. So we have to dump the
1595 * log in advance here.
1596 */
1597 if ((ret == EEH_NEXT_ERR_FROZEN_PE ||
1598 ret == EEH_NEXT_ERR_FENCED_PHB) &&
1599 !((*pe)->state & EEH_PE_ISOLATED)) {
1600 eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
1601 pnv_eeh_get_phb_diag(*pe);
1602
1603 if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
1604 pnv_pci_dump_phb_diag_data((*pe)->phb,
1605 (*pe)->data);
1606 }
1607
1608 /*
1609 * We probably have the frozen parent PE out there and
1610 * we need have to handle frozen parent PE firstly.
1611 */
1612 if (ret == EEH_NEXT_ERR_FROZEN_PE) {
1613 parent_pe = (*pe)->parent;
1614 while (parent_pe) {
1615 /* Hit the ceiling ? */
1616 if (parent_pe->type & EEH_PE_PHB)
1617 break;
1618
1619 /* Frozen parent PE ? */
1620 state = eeh_ops->get_state(parent_pe, NULL);
1621 if (state > 0 &&
1622 (state & active_flags) != active_flags)
1623 *pe = parent_pe;
1624
1625 /* Next parent level */
1626 parent_pe = parent_pe->parent;
1627 }
1628
1629 /* We possibly migrate to another PE */
1630 eeh_pe_state_mark(*pe, EEH_PE_ISOLATED);
1631 }
1632
1633 /*
1634 * If we have no errors on the specific PHB or only
1635 * informative error there, we continue poking it.
1636 * Otherwise, we need actions to be taken by upper
1637 * layer.
1638 */
1639 if (ret > EEH_NEXT_ERR_INF)
1640 break;
Gavin Shan29310e52013-06-20 13:21:13 +08001641 }
1642
Alistair Popple79231442015-05-15 14:06:40 +10001643 /* Unmask the event */
Alistair Poppleb8d65e92015-07-30 16:53:54 +10001644 if (ret == EEH_NEXT_ERR_NONE && eeh_enabled())
Alistair Popple79231442015-05-15 14:06:40 +10001645 enable_irq(eeh_event_irq);
1646
Gavin Shan2a485ad2015-02-16 14:45:46 +11001647 return ret;
Gavin Shan29310e52013-06-20 13:21:13 +08001648}
1649
Wei Yang0dc28302016-03-04 10:53:10 +11001650static int pnv_eeh_restore_vf_config(struct pci_dn *pdn)
1651{
1652 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
1653 u32 devctl, cmd, cap2, aer_capctl;
1654 int old_mps;
1655
1656 if (edev->pcie_cap) {
1657 /* Restore MPS */
1658 old_mps = (ffs(pdn->mps) - 8) << 5;
1659 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
1660 2, &devctl);
1661 devctl &= ~PCI_EXP_DEVCTL_PAYLOAD;
1662 devctl |= old_mps;
1663 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
1664 2, devctl);
1665
1666 /* Disable Completion Timeout */
1667 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCAP2,
1668 4, &cap2);
1669 if (cap2 & 0x10) {
1670 eeh_ops->read_config(pdn,
1671 edev->pcie_cap + PCI_EXP_DEVCTL2,
1672 4, &cap2);
1673 cap2 |= 0x10;
1674 eeh_ops->write_config(pdn,
1675 edev->pcie_cap + PCI_EXP_DEVCTL2,
1676 4, cap2);
1677 }
1678 }
1679
1680 /* Enable SERR and parity checking */
1681 eeh_ops->read_config(pdn, PCI_COMMAND, 2, &cmd);
1682 cmd |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1683 eeh_ops->write_config(pdn, PCI_COMMAND, 2, cmd);
1684
1685 /* Enable report various errors */
1686 if (edev->pcie_cap) {
1687 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
1688 2, &devctl);
1689 devctl &= ~PCI_EXP_DEVCTL_CERE;
1690 devctl |= (PCI_EXP_DEVCTL_NFERE |
1691 PCI_EXP_DEVCTL_FERE |
1692 PCI_EXP_DEVCTL_URRE);
1693 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
1694 2, devctl);
1695 }
1696
1697 /* Enable ECRC generation and check */
1698 if (edev->pcie_cap && edev->aer_cap) {
1699 eeh_ops->read_config(pdn, edev->aer_cap + PCI_ERR_CAP,
1700 4, &aer_capctl);
1701 aer_capctl |= (PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
1702 eeh_ops->write_config(pdn, edev->aer_cap + PCI_ERR_CAP,
1703 4, aer_capctl);
1704 }
1705
1706 return 0;
1707}
1708
Gavin Shan0bd78582015-03-17 16:15:07 +11001709static int pnv_eeh_restore_config(struct pci_dn *pdn)
Gavin Shan9be3becc2014-01-03 17:47:13 +08001710{
Gavin Shan0bd78582015-03-17 16:15:07 +11001711 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Gavin Shan9be3becc2014-01-03 17:47:13 +08001712 struct pnv_phb *phb;
1713 s64 ret;
1714
1715 if (!edev)
1716 return -EEXIST;
1717
Wei Yang0dc28302016-03-04 10:53:10 +11001718 /*
1719 * We have to restore the PCI config space after reset since the
1720 * firmware can't see SRIOV VFs.
1721 *
1722 * FIXME: The MPS, error routing rules, timeout setting are worthy
1723 * to be exported by firmware in extendible way.
1724 */
1725 if (edev->physfn) {
1726 ret = pnv_eeh_restore_vf_config(pdn);
1727 } else {
1728 phb = edev->phb->private_data;
1729 ret = opal_pci_reinit(phb->opal_id,
1730 OPAL_REINIT_PCI_DEV, edev->config_addr);
1731 }
1732
Gavin Shan9be3becc2014-01-03 17:47:13 +08001733 if (ret) {
1734 pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
1735 __func__, edev->config_addr, ret);
1736 return -EIO;
1737 }
1738
1739 return 0;
1740}
1741
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001742static struct eeh_ops pnv_eeh_ops = {
Gavin Shan29310e52013-06-20 13:21:13 +08001743 .name = "powernv",
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001744 .init = pnv_eeh_init,
1745 .post_init = pnv_eeh_post_init,
Gavin Shanff57b452015-03-17 16:15:06 +11001746 .probe = pnv_eeh_probe,
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001747 .set_option = pnv_eeh_set_option,
1748 .get_pe_addr = pnv_eeh_get_pe_addr,
1749 .get_state = pnv_eeh_get_state,
1750 .reset = pnv_eeh_reset,
1751 .wait_state = pnv_eeh_wait_state,
1752 .get_log = pnv_eeh_get_log,
1753 .configure_bridge = pnv_eeh_configure_bridge,
1754 .err_inject = pnv_eeh_err_inject,
1755 .read_config = pnv_eeh_read_config,
1756 .write_config = pnv_eeh_write_config,
1757 .next_error = pnv_eeh_next_error,
1758 .restore_config = pnv_eeh_restore_config
Gavin Shan29310e52013-06-20 13:21:13 +08001759};
1760
Wei Yangc29fa272016-03-04 10:53:08 +11001761void pcibios_bus_add_device(struct pci_dev *pdev)
1762{
1763 struct pci_dn *pdn = pci_get_pdn(pdev);
1764
1765 if (!pdev->is_virtfn)
1766 return;
1767
1768 /*
1769 * The following operations will fail if VF's sysfs files
1770 * aren't created or its resources aren't finalized.
1771 */
1772 eeh_add_device_early(pdn);
1773 eeh_add_device_late(pdev);
1774 eeh_sysfs_add_device(pdev);
1775}
1776
Wei Yang0dc28302016-03-04 10:53:10 +11001777#ifdef CONFIG_PCI_IOV
1778static void pnv_pci_fixup_vf_mps(struct pci_dev *pdev)
1779{
1780 struct pci_dn *pdn = pci_get_pdn(pdev);
1781 int parent_mps;
1782
1783 if (!pdev->is_virtfn)
1784 return;
1785
1786 /* Synchronize MPS for VF and PF */
1787 parent_mps = pcie_get_mps(pdev->physfn);
1788 if ((128 << pdev->pcie_mpss) >= parent_mps)
1789 pcie_set_mps(pdev, parent_mps);
1790 pdn->mps = pcie_get_mps(pdev);
1791}
1792DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pnv_pci_fixup_vf_mps);
1793#endif /* CONFIG_PCI_IOV */
1794
Gavin Shan29310e52013-06-20 13:21:13 +08001795/**
1796 * eeh_powernv_init - Register platform dependent EEH operations
1797 *
1798 * EEH initialization on powernv platform. This function should be
1799 * called before any EEH related functions.
1800 */
1801static int __init eeh_powernv_init(void)
1802{
1803 int ret = -EINVAL;
1804
Gavin Shan01f3bfb2015-02-16 14:45:39 +11001805 ret = eeh_ops_register(&pnv_eeh_ops);
Gavin Shan29310e52013-06-20 13:21:13 +08001806 if (!ret)
1807 pr_info("EEH: PowerNV platform initialized\n");
1808 else
1809 pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
1810
1811 return ret;
1812}
Michael Ellermanb14726c2014-07-15 22:22:24 +10001813machine_early_initcall(powernv, eeh_powernv_init);