blob: e43dcefbe73f47bf84814e9954fa9cdae4d938a1 [file] [log] [blame]
Gavin Shan55037d12012-09-07 22:44:07 +00001/*
2 * The file intends to implement PE based on the information from
3 * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
4 * All the PEs should be organized as hierarchy tree. The first level
5 * of the tree will be associated to existing PHBs since the particular
6 * PE is only meaningful in one PHB domain.
7 *
8 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
Gavin Shan652defe2013-06-27 13:46:43 +080025#include <linux/delay.h>
Gavin Shan55037d12012-09-07 22:44:07 +000026#include <linux/export.h>
27#include <linux/gfp.h>
Gavin Shan55037d12012-09-07 22:44:07 +000028#include <linux/kernel.h>
29#include <linux/pci.h>
30#include <linux/string.h>
31
32#include <asm/pci-bridge.h>
33#include <asm/ppc-pci.h>
34
Gavin Shanbb593c02014-07-17 14:41:43 +100035static int eeh_pe_aux_size = 0;
Gavin Shan55037d12012-09-07 22:44:07 +000036static LIST_HEAD(eeh_phb_pe);
37
38/**
Gavin Shanbb593c02014-07-17 14:41:43 +100039 * eeh_set_pe_aux_size - Set PE auxillary data size
40 * @size: PE auxillary data size
41 *
42 * Set PE auxillary data size
43 */
44void eeh_set_pe_aux_size(int size)
45{
46 if (size < 0)
47 return;
48
49 eeh_pe_aux_size = size;
50}
51
52/**
Gavin Shan55037d12012-09-07 22:44:07 +000053 * eeh_pe_alloc - Allocate PE
54 * @phb: PCI controller
55 * @type: PE type
56 *
57 * Allocate PE instance dynamically.
58 */
59static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)
60{
61 struct eeh_pe *pe;
Gavin Shanbb593c02014-07-17 14:41:43 +100062 size_t alloc_size;
63
64 alloc_size = sizeof(struct eeh_pe);
65 if (eeh_pe_aux_size) {
66 alloc_size = ALIGN(alloc_size, cache_line_size());
67 alloc_size += eeh_pe_aux_size;
68 }
Gavin Shan55037d12012-09-07 22:44:07 +000069
70 /* Allocate PHB PE */
Gavin Shanbb593c02014-07-17 14:41:43 +100071 pe = kzalloc(alloc_size, GFP_KERNEL);
Gavin Shan55037d12012-09-07 22:44:07 +000072 if (!pe) return NULL;
73
74 /* Initialize PHB PE */
75 pe->type = type;
76 pe->phb = phb;
77 INIT_LIST_HEAD(&pe->child_list);
Gavin Shan55037d12012-09-07 22:44:07 +000078 INIT_LIST_HEAD(&pe->edevs);
79
Gavin Shanbb593c02014-07-17 14:41:43 +100080 pe->data = (void *)pe + ALIGN(sizeof(struct eeh_pe),
81 cache_line_size());
Gavin Shan55037d12012-09-07 22:44:07 +000082 return pe;
83}
84
85/**
86 * eeh_phb_pe_create - Create PHB PE
87 * @phb: PCI controller
88 *
89 * The function should be called while the PHB is detected during
90 * system boot or PCI hotplug in order to create PHB PE.
91 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080092int eeh_phb_pe_create(struct pci_controller *phb)
Gavin Shan55037d12012-09-07 22:44:07 +000093{
94 struct eeh_pe *pe;
95
96 /* Allocate PHB PE */
97 pe = eeh_pe_alloc(phb, EEH_PE_PHB);
98 if (!pe) {
99 pr_err("%s: out of memory!\n", __func__);
100 return -ENOMEM;
101 }
102
103 /* Put it into the list */
Gavin Shan55037d12012-09-07 22:44:07 +0000104 list_add_tail(&pe->child, &eeh_phb_pe);
Gavin Shan55037d12012-09-07 22:44:07 +0000105
Russell Currey1f52f172016-11-16 14:02:15 +1100106 pr_debug("EEH: Add PE for PHB#%x\n", phb->global_number);
Gavin Shan55037d12012-09-07 22:44:07 +0000107
108 return 0;
109}
110
111/**
112 * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
113 * @phb: PCI controller
114 *
115 * The overall PEs form hierarchy tree. The first layer of the
116 * hierarchy tree is composed of PHB PEs. The function is used
117 * to retrieve the corresponding PHB PE according to the given PHB.
118 */
Gavin Shan9ff67432013-06-20 13:20:53 +0800119struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
Gavin Shan55037d12012-09-07 22:44:07 +0000120{
121 struct eeh_pe *pe;
122
Gavin Shan55037d12012-09-07 22:44:07 +0000123 list_for_each_entry(pe, &eeh_phb_pe, child) {
124 /*
125 * Actually, we needn't check the type since
126 * the PE for PHB has been determined when that
127 * was created.
128 */
Aneesh Kumar K.V78446632012-09-20 23:29:46 +0000129 if ((pe->type & EEH_PE_PHB) && pe->phb == phb)
Gavin Shan55037d12012-09-07 22:44:07 +0000130 return pe;
Gavin Shan55037d12012-09-07 22:44:07 +0000131 }
132
Gavin Shan55037d12012-09-07 22:44:07 +0000133 return NULL;
134}
Gavin Shan22f4ab12012-09-07 22:44:08 +0000135
136/**
137 * eeh_pe_next - Retrieve the next PE in the tree
138 * @pe: current PE
139 * @root: root PE
140 *
141 * The function is used to retrieve the next PE in the
142 * hierarchy PE tree.
143 */
Sam Bobroff309ed3a2018-05-25 13:11:35 +1000144struct eeh_pe *eeh_pe_next(struct eeh_pe *pe, struct eeh_pe *root)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000145{
146 struct list_head *next = pe->child_list.next;
147
148 if (next == &pe->child_list) {
149 while (1) {
150 if (pe == root)
151 return NULL;
152 next = pe->child.next;
153 if (next != &pe->parent->child_list)
154 break;
155 pe = pe->parent;
156 }
157 }
158
159 return list_entry(next, struct eeh_pe, child);
160}
161
162/**
163 * eeh_pe_traverse - Traverse PEs in the specified PHB
164 * @root: root PE
165 * @fn: callback
166 * @flag: extra parameter to callback
167 *
168 * The function is used to traverse the specified PE and its
169 * child PEs. The traversing is to be terminated once the
170 * callback returns something other than NULL, or no more PEs
171 * to be traversed.
172 */
Gavin Shanf5c57712013-07-24 10:24:58 +0800173void *eeh_pe_traverse(struct eeh_pe *root,
Sam Bobroffd6c49322018-05-25 13:11:32 +1000174 eeh_pe_traverse_func fn, void *flag)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000175{
176 struct eeh_pe *pe;
177 void *ret;
178
Sam Bobroff309ed3a2018-05-25 13:11:35 +1000179 eeh_for_each_pe(root, pe) {
Gavin Shan22f4ab12012-09-07 22:44:08 +0000180 ret = fn(pe, flag);
181 if (ret) return ret;
182 }
183
184 return NULL;
185}
186
187/**
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000188 * eeh_pe_dev_traverse - Traverse the devices from the PE
189 * @root: EEH PE
190 * @fn: function callback
191 * @flag: extra parameter to callback
192 *
193 * The function is used to traverse the devices of the specified
194 * PE and its child PEs.
195 */
196void *eeh_pe_dev_traverse(struct eeh_pe *root,
Sam Bobroffd6c49322018-05-25 13:11:32 +1000197 eeh_edev_traverse_func fn, void *flag)
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000198{
199 struct eeh_pe *pe;
Gavin Shan9feed422013-07-24 10:24:56 +0800200 struct eeh_dev *edev, *tmp;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000201 void *ret;
202
203 if (!root) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000204 pr_warn("%s: Invalid PE %p\n",
205 __func__, root);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000206 return NULL;
207 }
208
209 /* Traverse root PE */
Sam Bobroff309ed3a2018-05-25 13:11:35 +1000210 eeh_for_each_pe(root, pe) {
Gavin Shan9feed422013-07-24 10:24:56 +0800211 eeh_pe_for_each_dev(pe, edev, tmp) {
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000212 ret = fn(edev, flag);
Gavin Shanef6a2852013-06-25 14:35:27 +0800213 if (ret)
Gavin Shanea812452012-09-11 19:16:18 +0000214 return ret;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000215 }
216 }
217
218 return NULL;
219}
220
221/**
Gavin Shan22f4ab12012-09-07 22:44:08 +0000222 * __eeh_pe_get - Check the PE address
223 * @data: EEH PE
224 * @flag: EEH device
225 *
226 * For one particular PE, it can be identified by PE address
227 * or tranditional BDF address. BDF address is composed of
228 * Bus/Device/Function number. The extra data referred by flag
229 * indicates which type of address should be used.
230 */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000231struct eeh_pe_get_flag {
232 int pe_no;
233 int config_addr;
234};
235
Sam Bobroffd6c49322018-05-25 13:11:32 +1000236static void *__eeh_pe_get(struct eeh_pe *pe, void *flag)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000237{
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000238 struct eeh_pe_get_flag *tmp = (struct eeh_pe_get_flag *) flag;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000239
240 /* Unexpected PHB PE */
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000241 if (pe->type & EEH_PE_PHB)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000242 return NULL;
243
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100244 /*
245 * We prefer PE address. For most cases, we should
246 * have non-zero PE address
247 */
248 if (eeh_has_flag(EEH_VALID_PE_ZERO)) {
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000249 if (tmp->pe_no == pe->addr)
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100250 return pe;
251 } else {
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000252 if (tmp->pe_no &&
253 (tmp->pe_no == pe->addr))
Andrew Donnellan2d521782016-04-26 15:02:50 +1000254 return pe;
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100255 }
Gavin Shan22f4ab12012-09-07 22:44:08 +0000256
257 /* Try BDF address */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000258 if (tmp->config_addr &&
259 (tmp->config_addr == pe->config_addr))
Gavin Shan22f4ab12012-09-07 22:44:08 +0000260 return pe;
261
262 return NULL;
263}
264
265/**
266 * eeh_pe_get - Search PE based on the given address
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000267 * @phb: PCI controller
268 * @pe_no: PE number
269 * @config_addr: Config address
Gavin Shan22f4ab12012-09-07 22:44:08 +0000270 *
271 * Search the corresponding PE based on the specified address which
272 * is included in the eeh device. The function is used to check if
273 * the associated PE has been created against the PE address. It's
274 * notable that the PE address has 2 format: traditional PE address
275 * which is composed of PCI bus/device/function number, or unified
276 * PE address.
277 */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000278struct eeh_pe *eeh_pe_get(struct pci_controller *phb,
279 int pe_no, int config_addr)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000280{
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000281 struct eeh_pe *root = eeh_phb_pe_get(phb);
282 struct eeh_pe_get_flag tmp = { pe_no, config_addr };
Gavin Shan22f4ab12012-09-07 22:44:08 +0000283 struct eeh_pe *pe;
284
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000285 pe = eeh_pe_traverse(root, __eeh_pe_get, &tmp);
Gavin Shan22f4ab12012-09-07 22:44:08 +0000286
287 return pe;
288}
289
290/**
291 * eeh_pe_get_parent - Retrieve the parent PE
292 * @edev: EEH device
293 *
294 * The whole PEs existing in the system are organized as hierarchy
295 * tree. The function is used to retrieve the parent PE according
296 * to the parent EEH device.
297 */
298static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
299{
Gavin Shan22f4ab12012-09-07 22:44:08 +0000300 struct eeh_dev *parent;
Gavin Shan0bd78582015-03-17 16:15:07 +1100301 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan22f4ab12012-09-07 22:44:08 +0000302
303 /*
304 * It might have the case for the indirect parent
305 * EEH device already having associated PE, but
306 * the direct parent EEH device doesn't have yet.
307 */
Wei Yangc29fa272016-03-04 10:53:08 +1100308 if (edev->physfn)
309 pdn = pci_get_pdn(edev->physfn);
310 else
311 pdn = pdn ? pdn->parent : NULL;
Gavin Shan0bd78582015-03-17 16:15:07 +1100312 while (pdn) {
Gavin Shan22f4ab12012-09-07 22:44:08 +0000313 /* We're poking out of PCI territory */
Gavin Shan0bd78582015-03-17 16:15:07 +1100314 parent = pdn_to_eeh_dev(pdn);
315 if (!parent)
316 return NULL;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000317
318 if (parent->pe)
319 return parent->pe;
320
Gavin Shan0bd78582015-03-17 16:15:07 +1100321 pdn = pdn->parent;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000322 }
323
324 return NULL;
325}
Gavin Shan9b843482012-09-07 22:44:09 +0000326
327/**
328 * eeh_add_to_parent_pe - Add EEH device to parent PE
329 * @edev: EEH device
330 *
331 * Add EEH device to the parent PE. If the parent PE already
332 * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
333 * we have to create new PE to hold the EEH device and the new
334 * PE will be linked to its parent PE as well.
335 */
336int eeh_add_to_parent_pe(struct eeh_dev *edev)
337{
338 struct eeh_pe *pe, *parent;
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000339 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000340 int config_addr = (pdn->busno << 8) | (pdn->devfn);
Gavin Shan9b843482012-09-07 22:44:09 +0000341
Gavin Shan433185d2015-03-27 11:22:17 +1100342 /* Check if the PE number is valid */
343 if (!eeh_has_flag(EEH_VALID_PE_ZERO) && !edev->pe_config_addr) {
Russell Currey1f52f172016-11-16 14:02:15 +1100344 pr_err("%s: Invalid PE#0 for edev 0x%x on PHB#%x\n",
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000345 __func__, config_addr, pdn->phb->global_number);
Gavin Shan433185d2015-03-27 11:22:17 +1100346 return -EINVAL;
347 }
348
Gavin Shan9b843482012-09-07 22:44:09 +0000349 /*
350 * Search the PE has been existing or not according
351 * to the PE address. If that has been existing, the
352 * PE should be composed of PCI bus and its subordinate
353 * components.
354 */
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000355 pe = eeh_pe_get(pdn->phb, edev->pe_config_addr, config_addr);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000356 if (pe && !(pe->type & EEH_PE_INVALID)) {
Gavin Shan9b843482012-09-07 22:44:09 +0000357 /* Mark the PE as type of PCI bus */
358 pe->type = EEH_PE_BUS;
359 edev->pe = pe;
360
361 /* Put the edev to PE */
Sam Bobroff80e65b02018-09-12 11:23:26 +1000362 list_add_tail(&edev->entry, &pe->edevs);
Gavin Shanc6406d82015-03-17 16:15:08 +1100363 pr_debug("EEH: Add %04x:%02x:%02x.%01x to Bus PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000364 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000365 pdn->busno,
366 PCI_SLOT(pdn->devfn),
367 PCI_FUNC(pdn->devfn),
368 pe->addr);
Gavin Shan9b843482012-09-07 22:44:09 +0000369 return 0;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000370 } else if (pe && (pe->type & EEH_PE_INVALID)) {
Sam Bobroff80e65b02018-09-12 11:23:26 +1000371 list_add_tail(&edev->entry, &pe->edevs);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000372 edev->pe = pe;
373 /*
374 * We're running to here because of PCI hotplug caused by
375 * EEH recovery. We need clear EEH_PE_INVALID until the top.
376 */
377 parent = pe;
378 while (parent) {
379 if (!(parent->type & EEH_PE_INVALID))
380 break;
Sam Bobroff473af092018-09-12 11:23:22 +1000381 parent->type &= ~EEH_PE_INVALID;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000382 parent = parent->parent;
383 }
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000384
Gavin Shanc6406d82015-03-17 16:15:08 +1100385 pr_debug("EEH: Add %04x:%02x:%02x.%01x to Device "
386 "PE#%x, Parent PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000387 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000388 pdn->busno,
389 PCI_SLOT(pdn->devfn),
390 PCI_FUNC(pdn->devfn),
391 pe->addr, pe->parent->addr);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000392 return 0;
Gavin Shan9b843482012-09-07 22:44:09 +0000393 }
394
395 /* Create a new EEH PE */
Wei Yangc29fa272016-03-04 10:53:08 +1100396 if (edev->physfn)
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000397 pe = eeh_pe_alloc(pdn->phb, EEH_PE_VF);
Wei Yangc29fa272016-03-04 10:53:08 +1100398 else
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000399 pe = eeh_pe_alloc(pdn->phb, EEH_PE_DEVICE);
Gavin Shan9b843482012-09-07 22:44:09 +0000400 if (!pe) {
401 pr_err("%s: out of memory!\n", __func__);
402 return -ENOMEM;
403 }
404 pe->addr = edev->pe_config_addr;
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000405 pe->config_addr = config_addr;
Gavin Shan9b843482012-09-07 22:44:09 +0000406
407 /*
408 * Put the new EEH PE into hierarchy tree. If the parent
409 * can't be found, the newly created PE will be attached
410 * to PHB directly. Otherwise, we have to associate the
411 * PE with its parent.
412 */
413 parent = eeh_pe_get_parent(edev);
414 if (!parent) {
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000415 parent = eeh_phb_pe_get(pdn->phb);
Gavin Shan9b843482012-09-07 22:44:09 +0000416 if (!parent) {
417 pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000418 __func__, pdn->phb->global_number);
Gavin Shan9b843482012-09-07 22:44:09 +0000419 edev->pe = NULL;
420 kfree(pe);
421 return -EEXIST;
422 }
423 }
424 pe->parent = parent;
425
426 /*
427 * Put the newly created PE into the child list and
428 * link the EEH device accordingly.
429 */
430 list_add_tail(&pe->child, &parent->child_list);
Sam Bobroff80e65b02018-09-12 11:23:26 +1000431 list_add_tail(&edev->entry, &pe->edevs);
Gavin Shan9b843482012-09-07 22:44:09 +0000432 edev->pe = pe;
Gavin Shanc6406d82015-03-17 16:15:08 +1100433 pr_debug("EEH: Add %04x:%02x:%02x.%01x to "
434 "Device PE#%x, Parent PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000435 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000436 pdn->busno,
437 PCI_SLOT(pdn->devfn),
438 PCI_FUNC(pdn->devfn),
Gavin Shanc6406d82015-03-17 16:15:08 +1100439 pe->addr, pe->parent->addr);
Gavin Shan9b843482012-09-07 22:44:09 +0000440
441 return 0;
442}
Gavin Shan82e88822012-09-07 22:44:10 +0000443
444/**
445 * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
446 * @edev: EEH device
447 *
448 * The PE hierarchy tree might be changed when doing PCI hotplug.
449 * Also, the PCI devices or buses could be removed from the system
450 * during EEH recovery. So we have to call the function remove the
451 * corresponding PE accordingly if necessary.
452 */
Gavin Shan807a8272013-07-24 10:24:55 +0800453int eeh_rmv_from_parent_pe(struct eeh_dev *edev)
Gavin Shan82e88822012-09-07 22:44:10 +0000454{
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000455 struct eeh_pe *pe, *parent, *child;
456 int cnt;
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000457 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan82e88822012-09-07 22:44:10 +0000458
Sam Bobroff9a3eda22018-09-12 11:23:28 +1000459 pe = eeh_dev_to_pe(edev);
460 if (!pe) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100461 pr_debug("%s: No PE found for device %04x:%02x:%02x.%01x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000462 __func__, pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000463 pdn->busno,
464 PCI_SLOT(pdn->devfn),
465 PCI_FUNC(pdn->devfn));
Gavin Shan82e88822012-09-07 22:44:10 +0000466 return -EEXIST;
467 }
468
469 /* Remove the EEH device */
Gavin Shan82e88822012-09-07 22:44:10 +0000470 edev->pe = NULL;
Sam Bobroff80e65b02018-09-12 11:23:26 +1000471 list_del(&edev->entry);
Gavin Shan82e88822012-09-07 22:44:10 +0000472
473 /*
474 * Check if the parent PE includes any EEH devices.
475 * If not, we should delete that. Also, we should
476 * delete the parent PE if it doesn't have associated
477 * child PEs and EEH devices.
478 */
479 while (1) {
480 parent = pe->parent;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000481 if (pe->type & EEH_PE_PHB)
Gavin Shan82e88822012-09-07 22:44:10 +0000482 break;
483
Gavin Shan807a8272013-07-24 10:24:55 +0800484 if (!(pe->state & EEH_PE_KEEP)) {
Gavin Shan20ee6a92012-09-11 19:16:17 +0000485 if (list_empty(&pe->edevs) &&
486 list_empty(&pe->child_list)) {
487 list_del(&pe->child);
488 kfree(pe);
489 } else {
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000490 break;
Gavin Shan20ee6a92012-09-11 19:16:17 +0000491 }
492 } else {
493 if (list_empty(&pe->edevs)) {
494 cnt = 0;
495 list_for_each_entry(child, &pe->child_list, child) {
Gavin Shane716e012012-11-22 21:58:26 +0000496 if (!(child->type & EEH_PE_INVALID)) {
Gavin Shan20ee6a92012-09-11 19:16:17 +0000497 cnt++;
498 break;
499 }
500 }
501
502 if (!cnt)
503 pe->type |= EEH_PE_INVALID;
504 else
505 break;
506 }
Gavin Shan82e88822012-09-07 22:44:10 +0000507 }
508
509 pe = parent;
510 }
511
512 return 0;
513}
Gavin Shan5b663522012-09-07 22:44:12 +0000514
515/**
Gavin Shan5a719782013-06-20 13:21:01 +0800516 * eeh_pe_update_time_stamp - Update PE's frozen time stamp
517 * @pe: EEH PE
518 *
519 * We have time stamp for each PE to trace its time of getting
520 * frozen in last hour. The function should be called to update
521 * the time stamp on first error of the specific PE. On the other
522 * handle, we needn't account for errors happened in last hour.
523 */
524void eeh_pe_update_time_stamp(struct eeh_pe *pe)
525{
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100526 time64_t tstamp;
Gavin Shan5a719782013-06-20 13:21:01 +0800527
528 if (!pe) return;
529
530 if (pe->freeze_count <= 0) {
531 pe->freeze_count = 0;
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100532 pe->tstamp = ktime_get_seconds();
Gavin Shan5a719782013-06-20 13:21:01 +0800533 } else {
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100534 tstamp = ktime_get_seconds();
535 if (tstamp - pe->tstamp > 3600) {
Gavin Shan5a719782013-06-20 13:21:01 +0800536 pe->tstamp = tstamp;
537 pe->freeze_count = 0;
538 }
539 }
540}
541
542/**
Gavin Shan5b663522012-09-07 22:44:12 +0000543 * eeh_pe_state_mark - Mark specified state for PE and its associated device
544 * @pe: EEH PE
545 *
546 * EEH error affects the current PE and its child PEs. The function
547 * is used to mark appropriate state for the affected PEs and the
548 * associated devices.
549 */
Sam Bobroffe762bb82018-09-12 11:23:31 +1000550void eeh_pe_state_mark(struct eeh_pe *root, int state)
Gavin Shan5b663522012-09-07 22:44:12 +0000551{
Sam Bobroffe762bb82018-09-12 11:23:31 +1000552 struct eeh_pe *pe;
553
554 eeh_for_each_pe(root, pe)
555 if (!(pe->state & EEH_PE_REMOVED))
556 pe->state |= state;
Gavin Shan5b663522012-09-07 22:44:12 +0000557}
Gavin Shane0056b02016-09-28 14:34:55 +1000558EXPORT_SYMBOL_GPL(eeh_pe_state_mark);
Gavin Shan5b663522012-09-07 22:44:12 +0000559
Sam Bobroffe762bb82018-09-12 11:23:31 +1000560/**
561 * eeh_pe_mark_isolated
562 * @pe: EEH PE
563 *
564 * Record that a PE has been isolated by marking the PE and it's children as
565 * EEH_PE_ISOLATED (and EEH_PE_CFG_BLOCKED, if required) and their PCI devices
566 * as pci_channel_io_frozen.
567 */
568void eeh_pe_mark_isolated(struct eeh_pe *root)
569{
570 struct eeh_pe *pe;
571 struct eeh_dev *edev;
572 struct pci_dev *pdev;
573
574 eeh_pe_state_mark(root, EEH_PE_ISOLATED);
575 eeh_for_each_pe(root, pe) {
576 list_for_each_entry(edev, &pe->edevs, entry) {
577 pdev = eeh_dev_to_pci_dev(edev);
578 if (pdev)
579 pdev->error_state = pci_channel_io_frozen;
580 }
581 /* Block PCI config access if required */
582 if (pe->state & EEH_PE_CFG_RESTRICTED)
583 pe->state |= EEH_PE_CFG_BLOCKED;
584 }
585}
586EXPORT_SYMBOL_GPL(eeh_pe_mark_isolated);
587
Sam Bobroffd6c49322018-05-25 13:11:32 +1000588static void *__eeh_pe_dev_mode_mark(struct eeh_dev *edev, void *flag)
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000589{
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000590 int mode = *((int *)flag);
591
592 edev->mode |= mode;
593
594 return NULL;
595}
596
597/**
598 * eeh_pe_dev_state_mark - Mark state for all device under the PE
599 * @pe: EEH PE
600 *
601 * Mark specific state for all child devices of the PE.
602 */
603void eeh_pe_dev_mode_mark(struct eeh_pe *pe, int mode)
604{
605 eeh_pe_dev_traverse(pe, __eeh_pe_dev_mode_mark, &mode);
606}
607
Gavin Shan5b663522012-09-07 22:44:12 +0000608/**
609 * __eeh_pe_state_clear - Clear state for the PE
610 * @data: EEH PE
611 * @flag: state
612 *
613 * The function is used to clear the indicated state from the
614 * given PE. Besides, we also clear the check count of the PE
615 * as well.
616 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000617static void *__eeh_pe_state_clear(struct eeh_pe *pe, void *flag)
Gavin Shan5b663522012-09-07 22:44:12 +0000618{
Gavin Shan5b663522012-09-07 22:44:12 +0000619 int state = *((int *)flag);
Gavin Shan22fca172014-09-30 12:38:59 +1000620 struct eeh_dev *edev, *tmp;
621 struct pci_dev *pdev;
Gavin Shan5b663522012-09-07 22:44:12 +0000622
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000623 /* Keep the state of permanently removed PE intact */
Gavin Shan432227e2014-12-11 14:28:55 +1100624 if (pe->state & EEH_PE_REMOVED)
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000625 return NULL;
626
Gavin Shan5b663522012-09-07 22:44:12 +0000627 pe->state &= ~state;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000628
Gavin Shan22fca172014-09-30 12:38:59 +1000629 /*
630 * Special treatment on clearing isolated state. Clear
631 * check count since last isolation and put all affected
632 * devices to normal state.
633 */
634 if (!(state & EEH_PE_ISOLATED))
635 return NULL;
636
637 pe->check_count = 0;
638 eeh_pe_for_each_dev(pe, edev, tmp) {
639 pdev = eeh_dev_to_pci_dev(edev);
640 if (!pdev)
641 continue;
642
643 pdev->error_state = pci_channel_io_normal;
644 }
Gavin Shan5b663522012-09-07 22:44:12 +0000645
Gavin Shanb6541db2014-10-01 17:07:53 +1000646 /* Unblock PCI config access if required */
647 if (pe->state & EEH_PE_CFG_RESTRICTED)
648 pe->state &= ~EEH_PE_CFG_BLOCKED;
649
Gavin Shan5b663522012-09-07 22:44:12 +0000650 return NULL;
651}
652
653/**
654 * eeh_pe_state_clear - Clear state for the PE and its children
655 * @pe: PE
656 * @state: state to be cleared
657 *
658 * When the PE and its children has been recovered from error,
659 * we need clear the error state for that. The function is used
660 * for the purpose.
661 */
662void eeh_pe_state_clear(struct eeh_pe *pe, int state)
663{
664 eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
665}
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000666
Gavin Shan652defe2013-06-27 13:46:43 +0800667/*
668 * Some PCI bridges (e.g. PLX bridges) have primary/secondary
669 * buses assigned explicitly by firmware, and we probably have
670 * lost that after reset. So we have to delay the check until
671 * the PCI-CFG registers have been restored for the parent
672 * bridge.
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000673 *
Gavin Shan652defe2013-06-27 13:46:43 +0800674 * Don't use normal PCI-CFG accessors, which probably has been
675 * blocked on normal path during the stage. So we need utilize
676 * eeh operations, which is always permitted.
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000677 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100678static void eeh_bridge_check_link(struct eeh_dev *edev)
Gavin Shan652defe2013-06-27 13:46:43 +0800679{
Gavin Shan0bd78582015-03-17 16:15:07 +1100680 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800681 int cap;
682 uint32_t val;
683 int timeout = 0;
684
685 /*
686 * We only check root port and downstream ports of
687 * PCIe switches
688 */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800689 if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT)))
Gavin Shan652defe2013-06-27 13:46:43 +0800690 return;
691
Gavin Shan4b83bd42013-07-24 10:24:59 +0800692 pr_debug("%s: Check PCIe link for %04x:%02x:%02x.%01x ...\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000693 __func__, pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000694 pdn->busno,
695 PCI_SLOT(pdn->devfn),
696 PCI_FUNC(pdn->devfn));
Gavin Shan652defe2013-06-27 13:46:43 +0800697
698 /* Check slot status */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800699 cap = edev->pcie_cap;
Gavin Shan0bd78582015-03-17 16:15:07 +1100700 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTSTA, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800701 if (!(val & PCI_EXP_SLTSTA_PDS)) {
702 pr_debug(" No card in the slot (0x%04x) !\n", val);
703 return;
704 }
705
706 /* Check power status if we have the capability */
Gavin Shan0bd78582015-03-17 16:15:07 +1100707 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCAP, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800708 if (val & PCI_EXP_SLTCAP_PCP) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100709 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCTL, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800710 if (val & PCI_EXP_SLTCTL_PCC) {
711 pr_debug(" In power-off state, power it on ...\n");
712 val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC);
713 val |= (0x0100 & PCI_EXP_SLTCTL_PIC);
Gavin Shan0bd78582015-03-17 16:15:07 +1100714 eeh_ops->write_config(pdn, cap + PCI_EXP_SLTCTL, 2, val);
Gavin Shan652defe2013-06-27 13:46:43 +0800715 msleep(2 * 1000);
716 }
717 }
718
719 /* Enable link */
Gavin Shan0bd78582015-03-17 16:15:07 +1100720 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCTL, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800721 val &= ~PCI_EXP_LNKCTL_LD;
Gavin Shan0bd78582015-03-17 16:15:07 +1100722 eeh_ops->write_config(pdn, cap + PCI_EXP_LNKCTL, 2, val);
Gavin Shan652defe2013-06-27 13:46:43 +0800723
724 /* Check link */
Gavin Shan0bd78582015-03-17 16:15:07 +1100725 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCAP, 4, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800726 if (!(val & PCI_EXP_LNKCAP_DLLLARC)) {
727 pr_debug(" No link reporting capability (0x%08x) \n", val);
728 msleep(1000);
729 return;
730 }
731
732 /* Wait the link is up until timeout (5s) */
733 timeout = 0;
734 while (timeout < 5000) {
735 msleep(20);
736 timeout += 20;
737
Gavin Shan0bd78582015-03-17 16:15:07 +1100738 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKSTA, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800739 if (val & PCI_EXP_LNKSTA_DLLLA)
740 break;
741 }
742
743 if (val & PCI_EXP_LNKSTA_DLLLA)
744 pr_debug(" Link up (%s)\n",
745 (val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB");
746 else
747 pr_debug(" Link not ready (0x%04x)\n", val);
748}
749
750#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
751#define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
752
Gavin Shan0bd78582015-03-17 16:15:07 +1100753static void eeh_restore_bridge_bars(struct eeh_dev *edev)
Gavin Shan652defe2013-06-27 13:46:43 +0800754{
Gavin Shan0bd78582015-03-17 16:15:07 +1100755 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800756 int i;
757
758 /*
759 * Device BARs: 0x10 - 0x18
760 * Bus numbers and windows: 0x18 - 0x30
761 */
762 for (i = 4; i < 13; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100763 eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]);
Gavin Shan652defe2013-06-27 13:46:43 +0800764 /* Rom: 0x38 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100765 eeh_ops->write_config(pdn, 14*4, 4, edev->config_space[14]);
Gavin Shan652defe2013-06-27 13:46:43 +0800766
767 /* Cache line & Latency timer: 0xC 0xD */
Gavin Shan0bd78582015-03-17 16:15:07 +1100768 eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1,
Gavin Shan652defe2013-06-27 13:46:43 +0800769 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
Gavin Shan0bd78582015-03-17 16:15:07 +1100770 eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1,
Gavin Shan652defe2013-06-27 13:46:43 +0800771 SAVED_BYTE(PCI_LATENCY_TIMER));
772 /* Max latency, min grant, interrupt ping and line: 0x3C */
Gavin Shan0bd78582015-03-17 16:15:07 +1100773 eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]);
Gavin Shan652defe2013-06-27 13:46:43 +0800774
775 /* PCI Command: 0x4 */
Michael Neuling13a83ea2018-04-11 13:37:58 +1000776 eeh_ops->write_config(pdn, PCI_COMMAND, 4, edev->config_space[1] |
777 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Gavin Shan652defe2013-06-27 13:46:43 +0800778
779 /* Check the PCIe link is ready */
Gavin Shan0bd78582015-03-17 16:15:07 +1100780 eeh_bridge_check_link(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800781}
782
Gavin Shan0bd78582015-03-17 16:15:07 +1100783static void eeh_restore_device_bars(struct eeh_dev *edev)
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000784{
Gavin Shan0bd78582015-03-17 16:15:07 +1100785 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000786 int i;
787 u32 cmd;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000788
789 for (i = 4; i < 10; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100790 eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000791 /* 12 == Expansion ROM Address */
Gavin Shan0bd78582015-03-17 16:15:07 +1100792 eeh_ops->write_config(pdn, 12*4, 4, edev->config_space[12]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000793
Gavin Shan0bd78582015-03-17 16:15:07 +1100794 eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1,
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000795 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
Gavin Shan0bd78582015-03-17 16:15:07 +1100796 eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1,
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000797 SAVED_BYTE(PCI_LATENCY_TIMER));
798
799 /* max latency, min grant, interrupt pin and line */
Gavin Shan0bd78582015-03-17 16:15:07 +1100800 eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000801
802 /*
803 * Restore PERR & SERR bits, some devices require it,
804 * don't touch the other command bits
805 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100806 eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cmd);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000807 if (edev->config_space[1] & PCI_COMMAND_PARITY)
808 cmd |= PCI_COMMAND_PARITY;
809 else
810 cmd &= ~PCI_COMMAND_PARITY;
811 if (edev->config_space[1] & PCI_COMMAND_SERR)
812 cmd |= PCI_COMMAND_SERR;
813 else
814 cmd &= ~PCI_COMMAND_SERR;
Gavin Shan0bd78582015-03-17 16:15:07 +1100815 eeh_ops->write_config(pdn, PCI_COMMAND, 4, cmd);
Gavin Shan652defe2013-06-27 13:46:43 +0800816}
817
818/**
819 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
820 * @data: EEH device
821 * @flag: Unused
822 *
823 * Loads the PCI configuration space base address registers,
824 * the expansion ROM base address, the latency timer, and etc.
825 * from the saved values in the device node.
826 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000827static void *eeh_restore_one_device_bars(struct eeh_dev *edev, void *flag)
Gavin Shan652defe2013-06-27 13:46:43 +0800828{
Gavin Shan0bd78582015-03-17 16:15:07 +1100829 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800830
Gavin Shanf5c57712013-07-24 10:24:58 +0800831 /* Do special restore for bridges */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800832 if (edev->mode & EEH_DEV_BRIDGE)
Gavin Shan0bd78582015-03-17 16:15:07 +1100833 eeh_restore_bridge_bars(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800834 else
Gavin Shan0bd78582015-03-17 16:15:07 +1100835 eeh_restore_device_bars(edev);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000836
Gavin Shan0bd78582015-03-17 16:15:07 +1100837 if (eeh_ops->restore_config && pdn)
838 eeh_ops->restore_config(pdn);
Gavin Shan1d350542014-01-03 17:47:12 +0800839
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000840 return NULL;
841}
842
843/**
844 * eeh_pe_restore_bars - Restore the PCI config space info
845 * @pe: EEH PE
846 *
847 * This routine performs a recursive walk to the children
848 * of this device as well.
849 */
850void eeh_pe_restore_bars(struct eeh_pe *pe)
851{
Gavin Shanea812452012-09-11 19:16:18 +0000852 /*
853 * We needn't take the EEH lock since eeh_pe_dev_traverse()
854 * will take that.
855 */
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000856 eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
857}
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000858
859/**
Gavin Shan357b2f32014-06-11 18:26:44 +1000860 * eeh_pe_loc_get - Retrieve location code binding to the given PE
861 * @pe: EEH PE
862 *
863 * Retrieve the location code of the given PE. If the primary PE bus
864 * is root bus, we will grab location code from PHB device tree node
865 * or root port. Otherwise, the upstream bridge's device tree node
866 * of the primary PE bus will be checked for the location code.
867 */
868const char *eeh_pe_loc_get(struct eeh_pe *pe)
869{
Gavin Shan357b2f32014-06-11 18:26:44 +1000870 struct pci_bus *bus = eeh_pe_bus_get(pe);
Gavin Shan7e56f622015-12-02 16:25:32 +1100871 struct device_node *dn;
Mike Qiu9e5c6e52014-07-15 01:42:22 -0400872 const char *loc = NULL;
Gavin Shan357b2f32014-06-11 18:26:44 +1000873
Gavin Shan7e56f622015-12-02 16:25:32 +1100874 while (bus) {
875 dn = pci_bus_to_OF_node(bus);
876 if (!dn) {
877 bus = bus->parent;
878 continue;
879 }
Gavin Shan357b2f32014-06-11 18:26:44 +1000880
Gavin Shan7e56f622015-12-02 16:25:32 +1100881 if (pci_is_root_bus(bus))
Mike Qiu9e5c6e52014-07-15 01:42:22 -0400882 loc = of_get_property(dn, "ibm,io-base-loc-code", NULL);
Gavin Shan7e56f622015-12-02 16:25:32 +1100883 else
884 loc = of_get_property(dn, "ibm,slot-location-code",
885 NULL);
Gavin Shan357b2f32014-06-11 18:26:44 +1000886
Gavin Shan7e56f622015-12-02 16:25:32 +1100887 if (loc)
888 return loc;
889
890 bus = bus->parent;
Gavin Shan357b2f32014-06-11 18:26:44 +1000891 }
892
Gavin Shan7e56f622015-12-02 16:25:32 +1100893 return "N/A";
Gavin Shan357b2f32014-06-11 18:26:44 +1000894}
895
896/**
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000897 * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
898 * @pe: EEH PE
899 *
900 * Retrieve the PCI bus according to the given PE. Basically,
901 * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
902 * primary PCI bus will be retrieved. The parent bus will be
903 * returned for BUS PE. However, we don't have associated PCI
904 * bus for DEVICE PE.
905 */
906struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
907{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000908 struct eeh_dev *edev;
909 struct pci_dev *pdev;
910
Gavin Shan4eb07992016-02-09 15:50:23 +1100911 if (pe->type & EEH_PE_PHB)
912 return pe->phb->bus;
Gavin Shan8cdb2832013-06-20 13:20:55 +0800913
Gavin Shan4eb07992016-02-09 15:50:23 +1100914 /* The primary bus might be cached during probe time */
915 if (pe->state & EEH_PE_PRI_BUS)
916 return pe->bus;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000917
Gavin Shan4eb07992016-02-09 15:50:23 +1100918 /* Retrieve the parent PCI bus of first (top) PCI device */
Sam Bobroff80e65b02018-09-12 11:23:26 +1000919 edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry);
Gavin Shan4eb07992016-02-09 15:50:23 +1100920 pdev = eeh_dev_to_pci_dev(edev);
921 if (pdev)
922 return pdev->bus;
923
924 return NULL;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000925}