blob: 38a4bcd8ed13e19f4a83e5fdaaa28cc03b2d6475 [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);
78 INIT_LIST_HEAD(&pe->child);
79 INIT_LIST_HEAD(&pe->edevs);
80
Gavin Shanbb593c02014-07-17 14:41:43 +100081 pe->data = (void *)pe + ALIGN(sizeof(struct eeh_pe),
82 cache_line_size());
Gavin Shan55037d12012-09-07 22:44:07 +000083 return pe;
84}
85
86/**
87 * eeh_phb_pe_create - Create PHB PE
88 * @phb: PCI controller
89 *
90 * The function should be called while the PHB is detected during
91 * system boot or PCI hotplug in order to create PHB PE.
92 */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080093int eeh_phb_pe_create(struct pci_controller *phb)
Gavin Shan55037d12012-09-07 22:44:07 +000094{
95 struct eeh_pe *pe;
96
97 /* Allocate PHB PE */
98 pe = eeh_pe_alloc(phb, EEH_PE_PHB);
99 if (!pe) {
100 pr_err("%s: out of memory!\n", __func__);
101 return -ENOMEM;
102 }
103
104 /* Put it into the list */
Gavin Shan55037d12012-09-07 22:44:07 +0000105 list_add_tail(&pe->child, &eeh_phb_pe);
Gavin Shan55037d12012-09-07 22:44:07 +0000106
Russell Currey1f52f172016-11-16 14:02:15 +1100107 pr_debug("EEH: Add PE for PHB#%x\n", phb->global_number);
Gavin Shan55037d12012-09-07 22:44:07 +0000108
109 return 0;
110}
111
112/**
113 * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
114 * @phb: PCI controller
115 *
116 * The overall PEs form hierarchy tree. The first layer of the
117 * hierarchy tree is composed of PHB PEs. The function is used
118 * to retrieve the corresponding PHB PE according to the given PHB.
119 */
Gavin Shan9ff67432013-06-20 13:20:53 +0800120struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
Gavin Shan55037d12012-09-07 22:44:07 +0000121{
122 struct eeh_pe *pe;
123
Gavin Shan55037d12012-09-07 22:44:07 +0000124 list_for_each_entry(pe, &eeh_phb_pe, child) {
125 /*
126 * Actually, we needn't check the type since
127 * the PE for PHB has been determined when that
128 * was created.
129 */
Aneesh Kumar K.V78446632012-09-20 23:29:46 +0000130 if ((pe->type & EEH_PE_PHB) && pe->phb == phb)
Gavin Shan55037d12012-09-07 22:44:07 +0000131 return pe;
Gavin Shan55037d12012-09-07 22:44:07 +0000132 }
133
Gavin Shan55037d12012-09-07 22:44:07 +0000134 return NULL;
135}
Gavin Shan22f4ab12012-09-07 22:44:08 +0000136
137/**
138 * eeh_pe_next - Retrieve the next PE in the tree
139 * @pe: current PE
140 * @root: root PE
141 *
142 * The function is used to retrieve the next PE in the
143 * hierarchy PE tree.
144 */
145static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
146 struct eeh_pe *root)
147{
148 struct list_head *next = pe->child_list.next;
149
150 if (next == &pe->child_list) {
151 while (1) {
152 if (pe == root)
153 return NULL;
154 next = pe->child.next;
155 if (next != &pe->parent->child_list)
156 break;
157 pe = pe->parent;
158 }
159 }
160
161 return list_entry(next, struct eeh_pe, child);
162}
163
164/**
165 * eeh_pe_traverse - Traverse PEs in the specified PHB
166 * @root: root PE
167 * @fn: callback
168 * @flag: extra parameter to callback
169 *
170 * The function is used to traverse the specified PE and its
171 * child PEs. The traversing is to be terminated once the
172 * callback returns something other than NULL, or no more PEs
173 * to be traversed.
174 */
Gavin Shanf5c57712013-07-24 10:24:58 +0800175void *eeh_pe_traverse(struct eeh_pe *root,
Sam Bobroffd6c49322018-05-25 13:11:32 +1000176 eeh_pe_traverse_func fn, void *flag)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000177{
178 struct eeh_pe *pe;
179 void *ret;
180
181 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
182 ret = fn(pe, flag);
183 if (ret) return ret;
184 }
185
186 return NULL;
187}
188
189/**
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000190 * eeh_pe_dev_traverse - Traverse the devices from the PE
191 * @root: EEH PE
192 * @fn: function callback
193 * @flag: extra parameter to callback
194 *
195 * The function is used to traverse the devices of the specified
196 * PE and its child PEs.
197 */
198void *eeh_pe_dev_traverse(struct eeh_pe *root,
Sam Bobroffd6c49322018-05-25 13:11:32 +1000199 eeh_edev_traverse_func fn, void *flag)
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000200{
201 struct eeh_pe *pe;
Gavin Shan9feed422013-07-24 10:24:56 +0800202 struct eeh_dev *edev, *tmp;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000203 void *ret;
204
205 if (!root) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000206 pr_warn("%s: Invalid PE %p\n",
207 __func__, root);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000208 return NULL;
209 }
210
211 /* Traverse root PE */
212 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
Gavin Shan9feed422013-07-24 10:24:56 +0800213 eeh_pe_for_each_dev(pe, edev, tmp) {
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000214 ret = fn(edev, flag);
Gavin Shanef6a2852013-06-25 14:35:27 +0800215 if (ret)
Gavin Shanea812452012-09-11 19:16:18 +0000216 return ret;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000217 }
218 }
219
220 return NULL;
221}
222
223/**
Gavin Shan22f4ab12012-09-07 22:44:08 +0000224 * __eeh_pe_get - Check the PE address
225 * @data: EEH PE
226 * @flag: EEH device
227 *
228 * For one particular PE, it can be identified by PE address
229 * or tranditional BDF address. BDF address is composed of
230 * Bus/Device/Function number. The extra data referred by flag
231 * indicates which type of address should be used.
232 */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000233struct eeh_pe_get_flag {
234 int pe_no;
235 int config_addr;
236};
237
Sam Bobroffd6c49322018-05-25 13:11:32 +1000238static void *__eeh_pe_get(struct eeh_pe *pe, void *flag)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000239{
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000240 struct eeh_pe_get_flag *tmp = (struct eeh_pe_get_flag *) flag;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000241
242 /* Unexpected PHB PE */
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000243 if (pe->type & EEH_PE_PHB)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000244 return NULL;
245
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100246 /*
247 * We prefer PE address. For most cases, we should
248 * have non-zero PE address
249 */
250 if (eeh_has_flag(EEH_VALID_PE_ZERO)) {
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000251 if (tmp->pe_no == pe->addr)
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100252 return pe;
253 } else {
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000254 if (tmp->pe_no &&
255 (tmp->pe_no == pe->addr))
Andrew Donnellan2d521782016-04-26 15:02:50 +1000256 return pe;
Gavin Shan2aa5cf92014-11-25 09:27:00 +1100257 }
Gavin Shan22f4ab12012-09-07 22:44:08 +0000258
259 /* Try BDF address */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000260 if (tmp->config_addr &&
261 (tmp->config_addr == pe->config_addr))
Gavin Shan22f4ab12012-09-07 22:44:08 +0000262 return pe;
263
264 return NULL;
265}
266
267/**
268 * eeh_pe_get - Search PE based on the given address
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000269 * @phb: PCI controller
270 * @pe_no: PE number
271 * @config_addr: Config address
Gavin Shan22f4ab12012-09-07 22:44:08 +0000272 *
273 * Search the corresponding PE based on the specified address which
274 * is included in the eeh device. The function is used to check if
275 * the associated PE has been created against the PE address. It's
276 * notable that the PE address has 2 format: traditional PE address
277 * which is composed of PCI bus/device/function number, or unified
278 * PE address.
279 */
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000280struct eeh_pe *eeh_pe_get(struct pci_controller *phb,
281 int pe_no, int config_addr)
Gavin Shan22f4ab12012-09-07 22:44:08 +0000282{
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000283 struct eeh_pe *root = eeh_phb_pe_get(phb);
284 struct eeh_pe_get_flag tmp = { pe_no, config_addr };
Gavin Shan22f4ab12012-09-07 22:44:08 +0000285 struct eeh_pe *pe;
286
Alexey Kardashevskiy8bae6a22017-08-29 17:34:00 +1000287 pe = eeh_pe_traverse(root, __eeh_pe_get, &tmp);
Gavin Shan22f4ab12012-09-07 22:44:08 +0000288
289 return pe;
290}
291
292/**
293 * eeh_pe_get_parent - Retrieve the parent PE
294 * @edev: EEH device
295 *
296 * The whole PEs existing in the system are organized as hierarchy
297 * tree. The function is used to retrieve the parent PE according
298 * to the parent EEH device.
299 */
300static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
301{
Gavin Shan22f4ab12012-09-07 22:44:08 +0000302 struct eeh_dev *parent;
Gavin Shan0bd78582015-03-17 16:15:07 +1100303 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan22f4ab12012-09-07 22:44:08 +0000304
305 /*
306 * It might have the case for the indirect parent
307 * EEH device already having associated PE, but
308 * the direct parent EEH device doesn't have yet.
309 */
Wei Yangc29fa272016-03-04 10:53:08 +1100310 if (edev->physfn)
311 pdn = pci_get_pdn(edev->physfn);
312 else
313 pdn = pdn ? pdn->parent : NULL;
Gavin Shan0bd78582015-03-17 16:15:07 +1100314 while (pdn) {
Gavin Shan22f4ab12012-09-07 22:44:08 +0000315 /* We're poking out of PCI territory */
Gavin Shan0bd78582015-03-17 16:15:07 +1100316 parent = pdn_to_eeh_dev(pdn);
317 if (!parent)
318 return NULL;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000319
320 if (parent->pe)
321 return parent->pe;
322
Gavin Shan0bd78582015-03-17 16:15:07 +1100323 pdn = pdn->parent;
Gavin Shan22f4ab12012-09-07 22:44:08 +0000324 }
325
326 return NULL;
327}
Gavin Shan9b843482012-09-07 22:44:09 +0000328
329/**
330 * eeh_add_to_parent_pe - Add EEH device to parent PE
331 * @edev: EEH device
332 *
333 * Add EEH device to the parent PE. If the parent PE already
334 * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
335 * we have to create new PE to hold the EEH device and the new
336 * PE will be linked to its parent PE as well.
337 */
338int eeh_add_to_parent_pe(struct eeh_dev *edev)
339{
340 struct eeh_pe *pe, *parent;
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000341 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000342 int config_addr = (pdn->busno << 8) | (pdn->devfn);
Gavin Shan9b843482012-09-07 22:44:09 +0000343
Gavin Shan433185d2015-03-27 11:22:17 +1100344 /* Check if the PE number is valid */
345 if (!eeh_has_flag(EEH_VALID_PE_ZERO) && !edev->pe_config_addr) {
Russell Currey1f52f172016-11-16 14:02:15 +1100346 pr_err("%s: Invalid PE#0 for edev 0x%x on PHB#%x\n",
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000347 __func__, config_addr, pdn->phb->global_number);
Gavin Shan433185d2015-03-27 11:22:17 +1100348 return -EINVAL;
349 }
350
Gavin Shan9b843482012-09-07 22:44:09 +0000351 /*
352 * Search the PE has been existing or not according
353 * to the PE address. If that has been existing, the
354 * PE should be composed of PCI bus and its subordinate
355 * components.
356 */
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000357 pe = eeh_pe_get(pdn->phb, edev->pe_config_addr, config_addr);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000358 if (pe && !(pe->type & EEH_PE_INVALID)) {
Gavin Shan9b843482012-09-07 22:44:09 +0000359 /* Mark the PE as type of PCI bus */
360 pe->type = EEH_PE_BUS;
361 edev->pe = pe;
362
363 /* Put the edev to PE */
364 list_add_tail(&edev->list, &pe->edevs);
Gavin Shanc6406d82015-03-17 16:15:08 +1100365 pr_debug("EEH: Add %04x:%02x:%02x.%01x to Bus PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000366 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000367 pdn->busno,
368 PCI_SLOT(pdn->devfn),
369 PCI_FUNC(pdn->devfn),
370 pe->addr);
Gavin Shan9b843482012-09-07 22:44:09 +0000371 return 0;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000372 } else if (pe && (pe->type & EEH_PE_INVALID)) {
373 list_add_tail(&edev->list, &pe->edevs);
374 edev->pe = pe;
375 /*
376 * We're running to here because of PCI hotplug caused by
377 * EEH recovery. We need clear EEH_PE_INVALID until the top.
378 */
379 parent = pe;
380 while (parent) {
381 if (!(parent->type & EEH_PE_INVALID))
382 break;
Gavin Shan807a8272013-07-24 10:24:55 +0800383 parent->type &= ~(EEH_PE_INVALID | EEH_PE_KEEP);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000384 parent = parent->parent;
385 }
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000386
Gavin Shanc6406d82015-03-17 16:15:08 +1100387 pr_debug("EEH: Add %04x:%02x:%02x.%01x to Device "
388 "PE#%x, Parent PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000389 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000390 pdn->busno,
391 PCI_SLOT(pdn->devfn),
392 PCI_FUNC(pdn->devfn),
393 pe->addr, pe->parent->addr);
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000394 return 0;
Gavin Shan9b843482012-09-07 22:44:09 +0000395 }
396
397 /* Create a new EEH PE */
Wei Yangc29fa272016-03-04 10:53:08 +1100398 if (edev->physfn)
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000399 pe = eeh_pe_alloc(pdn->phb, EEH_PE_VF);
Wei Yangc29fa272016-03-04 10:53:08 +1100400 else
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000401 pe = eeh_pe_alloc(pdn->phb, EEH_PE_DEVICE);
Gavin Shan9b843482012-09-07 22:44:09 +0000402 if (!pe) {
403 pr_err("%s: out of memory!\n", __func__);
404 return -ENOMEM;
405 }
406 pe->addr = edev->pe_config_addr;
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000407 pe->config_addr = config_addr;
Gavin Shan9b843482012-09-07 22:44:09 +0000408
409 /*
410 * Put the new EEH PE into hierarchy tree. If the parent
411 * can't be found, the newly created PE will be attached
412 * to PHB directly. Otherwise, we have to associate the
413 * PE with its parent.
414 */
415 parent = eeh_pe_get_parent(edev);
416 if (!parent) {
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000417 parent = eeh_phb_pe_get(pdn->phb);
Gavin Shan9b843482012-09-07 22:44:09 +0000418 if (!parent) {
419 pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000420 __func__, pdn->phb->global_number);
Gavin Shan9b843482012-09-07 22:44:09 +0000421 edev->pe = NULL;
422 kfree(pe);
423 return -EEXIST;
424 }
425 }
426 pe->parent = parent;
427
428 /*
429 * Put the newly created PE into the child list and
430 * link the EEH device accordingly.
431 */
432 list_add_tail(&pe->child, &parent->child_list);
433 list_add_tail(&edev->list, &pe->edevs);
434 edev->pe = pe;
Gavin Shanc6406d82015-03-17 16:15:08 +1100435 pr_debug("EEH: Add %04x:%02x:%02x.%01x to "
436 "Device PE#%x, Parent PE#%x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000437 pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000438 pdn->busno,
439 PCI_SLOT(pdn->devfn),
440 PCI_FUNC(pdn->devfn),
Gavin Shanc6406d82015-03-17 16:15:08 +1100441 pe->addr, pe->parent->addr);
Gavin Shan9b843482012-09-07 22:44:09 +0000442
443 return 0;
444}
Gavin Shan82e88822012-09-07 22:44:10 +0000445
446/**
447 * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
448 * @edev: EEH device
449 *
450 * The PE hierarchy tree might be changed when doing PCI hotplug.
451 * Also, the PCI devices or buses could be removed from the system
452 * during EEH recovery. So we have to call the function remove the
453 * corresponding PE accordingly if necessary.
454 */
Gavin Shan807a8272013-07-24 10:24:55 +0800455int eeh_rmv_from_parent_pe(struct eeh_dev *edev)
Gavin Shan82e88822012-09-07 22:44:10 +0000456{
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000457 struct eeh_pe *pe, *parent, *child;
458 int cnt;
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000459 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan82e88822012-09-07 22:44:10 +0000460
461 if (!edev->pe) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100462 pr_debug("%s: No PE found for device %04x:%02x:%02x.%01x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000463 __func__, pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000464 pdn->busno,
465 PCI_SLOT(pdn->devfn),
466 PCI_FUNC(pdn->devfn));
Gavin Shan82e88822012-09-07 22:44:10 +0000467 return -EEXIST;
468 }
469
470 /* Remove the EEH device */
Wei Yang2a582222014-09-17 10:48:26 +0800471 pe = eeh_dev_to_pe(edev);
Gavin Shan82e88822012-09-07 22:44:10 +0000472 edev->pe = NULL;
473 list_del(&edev->list);
474
475 /*
476 * Check if the parent PE includes any EEH devices.
477 * If not, we should delete that. Also, we should
478 * delete the parent PE if it doesn't have associated
479 * child PEs and EEH devices.
480 */
481 while (1) {
482 parent = pe->parent;
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000483 if (pe->type & EEH_PE_PHB)
Gavin Shan82e88822012-09-07 22:44:10 +0000484 break;
485
Gavin Shan807a8272013-07-24 10:24:55 +0800486 if (!(pe->state & EEH_PE_KEEP)) {
Gavin Shan20ee6a92012-09-11 19:16:17 +0000487 if (list_empty(&pe->edevs) &&
488 list_empty(&pe->child_list)) {
489 list_del(&pe->child);
490 kfree(pe);
491 } else {
Gavin Shan5efc3ad2012-09-11 19:16:16 +0000492 break;
Gavin Shan20ee6a92012-09-11 19:16:17 +0000493 }
494 } else {
495 if (list_empty(&pe->edevs)) {
496 cnt = 0;
497 list_for_each_entry(child, &pe->child_list, child) {
Gavin Shane716e012012-11-22 21:58:26 +0000498 if (!(child->type & EEH_PE_INVALID)) {
Gavin Shan20ee6a92012-09-11 19:16:17 +0000499 cnt++;
500 break;
501 }
502 }
503
504 if (!cnt)
505 pe->type |= EEH_PE_INVALID;
506 else
507 break;
508 }
Gavin Shan82e88822012-09-07 22:44:10 +0000509 }
510
511 pe = parent;
512 }
513
514 return 0;
515}
Gavin Shan5b663522012-09-07 22:44:12 +0000516
517/**
Gavin Shan5a719782013-06-20 13:21:01 +0800518 * eeh_pe_update_time_stamp - Update PE's frozen time stamp
519 * @pe: EEH PE
520 *
521 * We have time stamp for each PE to trace its time of getting
522 * frozen in last hour. The function should be called to update
523 * the time stamp on first error of the specific PE. On the other
524 * handle, we needn't account for errors happened in last hour.
525 */
526void eeh_pe_update_time_stamp(struct eeh_pe *pe)
527{
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100528 time64_t tstamp;
Gavin Shan5a719782013-06-20 13:21:01 +0800529
530 if (!pe) return;
531
532 if (pe->freeze_count <= 0) {
533 pe->freeze_count = 0;
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100534 pe->tstamp = ktime_get_seconds();
Gavin Shan5a719782013-06-20 13:21:01 +0800535 } else {
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100536 tstamp = ktime_get_seconds();
537 if (tstamp - pe->tstamp > 3600) {
Gavin Shan5a719782013-06-20 13:21:01 +0800538 pe->tstamp = tstamp;
539 pe->freeze_count = 0;
540 }
541 }
542}
543
544/**
Gavin Shan5b663522012-09-07 22:44:12 +0000545 * __eeh_pe_state_mark - Mark the state for the PE
546 * @data: EEH PE
547 * @flag: state
548 *
549 * The function is used to mark the indicated state for the given
550 * PE. Also, the associated PCI devices will be put into IO frozen
551 * state as well.
552 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000553static void *__eeh_pe_state_mark(struct eeh_pe *pe, void *flag)
Gavin Shan5b663522012-09-07 22:44:12 +0000554{
Gavin Shan5b663522012-09-07 22:44:12 +0000555 int state = *((int *)flag);
Gavin Shan9feed422013-07-24 10:24:56 +0800556 struct eeh_dev *edev, *tmp;
Gavin Shan5b663522012-09-07 22:44:12 +0000557 struct pci_dev *pdev;
558
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000559 /* Keep the state of permanently removed PE intact */
Gavin Shan432227e2014-12-11 14:28:55 +1100560 if (pe->state & EEH_PE_REMOVED)
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000561 return NULL;
562
Gavin Shan5b663522012-09-07 22:44:12 +0000563 pe->state |= state;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000564
565 /* Offline PCI devices if applicable */
Gavin Shan83150702014-10-01 17:07:49 +1000566 if (!(state & EEH_PE_ISOLATED))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000567 return NULL;
568
Gavin Shan9feed422013-07-24 10:24:56 +0800569 eeh_pe_for_each_dev(pe, edev, tmp) {
570 pdev = eeh_dev_to_pci_dev(edev);
Gavin Shan5b663522012-09-07 22:44:12 +0000571 if (pdev)
572 pdev->error_state = pci_channel_io_frozen;
573 }
574
Gavin Shanb6541db2014-10-01 17:07:53 +1000575 /* Block PCI config access if required */
576 if (pe->state & EEH_PE_CFG_RESTRICTED)
577 pe->state |= EEH_PE_CFG_BLOCKED;
578
Gavin Shan5b663522012-09-07 22:44:12 +0000579 return NULL;
580}
581
582/**
583 * eeh_pe_state_mark - Mark specified state for PE and its associated device
584 * @pe: EEH PE
585 *
586 * EEH error affects the current PE and its child PEs. The function
587 * is used to mark appropriate state for the affected PEs and the
588 * associated devices.
589 */
590void eeh_pe_state_mark(struct eeh_pe *pe, int state)
591{
592 eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
593}
Gavin Shane0056b02016-09-28 14:34:55 +1000594EXPORT_SYMBOL_GPL(eeh_pe_state_mark);
Gavin Shan5b663522012-09-07 22:44:12 +0000595
Sam Bobroffd6c49322018-05-25 13:11:32 +1000596static void *__eeh_pe_dev_mode_mark(struct eeh_dev *edev, void *flag)
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000597{
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000598 int mode = *((int *)flag);
599
600 edev->mode |= mode;
601
602 return NULL;
603}
604
605/**
606 * eeh_pe_dev_state_mark - Mark state for all device under the PE
607 * @pe: EEH PE
608 *
609 * Mark specific state for all child devices of the PE.
610 */
611void eeh_pe_dev_mode_mark(struct eeh_pe *pe, int mode)
612{
613 eeh_pe_dev_traverse(pe, __eeh_pe_dev_mode_mark, &mode);
614}
615
Gavin Shan5b663522012-09-07 22:44:12 +0000616/**
617 * __eeh_pe_state_clear - Clear state for the PE
618 * @data: EEH PE
619 * @flag: state
620 *
621 * The function is used to clear the indicated state from the
622 * given PE. Besides, we also clear the check count of the PE
623 * as well.
624 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000625static void *__eeh_pe_state_clear(struct eeh_pe *pe, void *flag)
Gavin Shan5b663522012-09-07 22:44:12 +0000626{
Gavin Shan5b663522012-09-07 22:44:12 +0000627 int state = *((int *)flag);
Gavin Shan22fca172014-09-30 12:38:59 +1000628 struct eeh_dev *edev, *tmp;
629 struct pci_dev *pdev;
Gavin Shan5b663522012-09-07 22:44:12 +0000630
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000631 /* Keep the state of permanently removed PE intact */
Gavin Shan432227e2014-12-11 14:28:55 +1100632 if (pe->state & EEH_PE_REMOVED)
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000633 return NULL;
634
Gavin Shan5b663522012-09-07 22:44:12 +0000635 pe->state &= ~state;
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000636
Gavin Shan22fca172014-09-30 12:38:59 +1000637 /*
638 * Special treatment on clearing isolated state. Clear
639 * check count since last isolation and put all affected
640 * devices to normal state.
641 */
642 if (!(state & EEH_PE_ISOLATED))
643 return NULL;
644
645 pe->check_count = 0;
646 eeh_pe_for_each_dev(pe, edev, tmp) {
647 pdev = eeh_dev_to_pci_dev(edev);
648 if (!pdev)
649 continue;
650
651 pdev->error_state = pci_channel_io_normal;
652 }
Gavin Shan5b663522012-09-07 22:44:12 +0000653
Gavin Shanb6541db2014-10-01 17:07:53 +1000654 /* Unblock PCI config access if required */
655 if (pe->state & EEH_PE_CFG_RESTRICTED)
656 pe->state &= ~EEH_PE_CFG_BLOCKED;
657
Gavin Shan5b663522012-09-07 22:44:12 +0000658 return NULL;
659}
660
661/**
662 * eeh_pe_state_clear - Clear state for the PE and its children
663 * @pe: PE
664 * @state: state to be cleared
665 *
666 * When the PE and its children has been recovered from error,
667 * we need clear the error state for that. The function is used
668 * for the purpose.
669 */
670void eeh_pe_state_clear(struct eeh_pe *pe, int state)
671{
672 eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
673}
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000674
Gavin Shan39bfd712015-07-30 09:26:51 +1000675/**
676 * eeh_pe_state_mark_with_cfg - Mark PE state with unblocked config space
677 * @pe: PE
678 * @state: PE state to be set
679 *
680 * Set specified flag to PE and its child PEs. The PCI config space
681 * of some PEs is blocked automatically when EEH_PE_ISOLATED is set,
682 * which isn't needed in some situations. The function allows to set
683 * the specified flag to indicated PEs without blocking their PCI
684 * config space.
685 */
686void eeh_pe_state_mark_with_cfg(struct eeh_pe *pe, int state)
687{
688 eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
689 if (!(state & EEH_PE_ISOLATED))
690 return;
691
692 /* Clear EEH_PE_CFG_BLOCKED, which might be set just now */
693 state = EEH_PE_CFG_BLOCKED;
694 eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
695}
696
Gavin Shan652defe2013-06-27 13:46:43 +0800697/*
698 * Some PCI bridges (e.g. PLX bridges) have primary/secondary
699 * buses assigned explicitly by firmware, and we probably have
700 * lost that after reset. So we have to delay the check until
701 * the PCI-CFG registers have been restored for the parent
702 * bridge.
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000703 *
Gavin Shan652defe2013-06-27 13:46:43 +0800704 * Don't use normal PCI-CFG accessors, which probably has been
705 * blocked on normal path during the stage. So we need utilize
706 * eeh operations, which is always permitted.
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000707 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100708static void eeh_bridge_check_link(struct eeh_dev *edev)
Gavin Shan652defe2013-06-27 13:46:43 +0800709{
Gavin Shan0bd78582015-03-17 16:15:07 +1100710 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800711 int cap;
712 uint32_t val;
713 int timeout = 0;
714
715 /*
716 * We only check root port and downstream ports of
717 * PCIe switches
718 */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800719 if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT)))
Gavin Shan652defe2013-06-27 13:46:43 +0800720 return;
721
Gavin Shan4b83bd42013-07-24 10:24:59 +0800722 pr_debug("%s: Check PCIe link for %04x:%02x:%02x.%01x ...\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000723 __func__, pdn->phb->global_number,
Alexey Kardashevskiy405b33a2017-08-29 17:34:02 +1000724 pdn->busno,
725 PCI_SLOT(pdn->devfn),
726 PCI_FUNC(pdn->devfn));
Gavin Shan652defe2013-06-27 13:46:43 +0800727
728 /* Check slot status */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800729 cap = edev->pcie_cap;
Gavin Shan0bd78582015-03-17 16:15:07 +1100730 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTSTA, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800731 if (!(val & PCI_EXP_SLTSTA_PDS)) {
732 pr_debug(" No card in the slot (0x%04x) !\n", val);
733 return;
734 }
735
736 /* Check power status if we have the capability */
Gavin Shan0bd78582015-03-17 16:15:07 +1100737 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCAP, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800738 if (val & PCI_EXP_SLTCAP_PCP) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100739 eeh_ops->read_config(pdn, cap + PCI_EXP_SLTCTL, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800740 if (val & PCI_EXP_SLTCTL_PCC) {
741 pr_debug(" In power-off state, power it on ...\n");
742 val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC);
743 val |= (0x0100 & PCI_EXP_SLTCTL_PIC);
Gavin Shan0bd78582015-03-17 16:15:07 +1100744 eeh_ops->write_config(pdn, cap + PCI_EXP_SLTCTL, 2, val);
Gavin Shan652defe2013-06-27 13:46:43 +0800745 msleep(2 * 1000);
746 }
747 }
748
749 /* Enable link */
Gavin Shan0bd78582015-03-17 16:15:07 +1100750 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCTL, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800751 val &= ~PCI_EXP_LNKCTL_LD;
Gavin Shan0bd78582015-03-17 16:15:07 +1100752 eeh_ops->write_config(pdn, cap + PCI_EXP_LNKCTL, 2, val);
Gavin Shan652defe2013-06-27 13:46:43 +0800753
754 /* Check link */
Gavin Shan0bd78582015-03-17 16:15:07 +1100755 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKCAP, 4, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800756 if (!(val & PCI_EXP_LNKCAP_DLLLARC)) {
757 pr_debug(" No link reporting capability (0x%08x) \n", val);
758 msleep(1000);
759 return;
760 }
761
762 /* Wait the link is up until timeout (5s) */
763 timeout = 0;
764 while (timeout < 5000) {
765 msleep(20);
766 timeout += 20;
767
Gavin Shan0bd78582015-03-17 16:15:07 +1100768 eeh_ops->read_config(pdn, cap + PCI_EXP_LNKSTA, 2, &val);
Gavin Shan652defe2013-06-27 13:46:43 +0800769 if (val & PCI_EXP_LNKSTA_DLLLA)
770 break;
771 }
772
773 if (val & PCI_EXP_LNKSTA_DLLLA)
774 pr_debug(" Link up (%s)\n",
775 (val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB");
776 else
777 pr_debug(" Link not ready (0x%04x)\n", val);
778}
779
780#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
781#define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
782
Gavin Shan0bd78582015-03-17 16:15:07 +1100783static void eeh_restore_bridge_bars(struct eeh_dev *edev)
Gavin Shan652defe2013-06-27 13:46:43 +0800784{
Gavin Shan0bd78582015-03-17 16:15:07 +1100785 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800786 int i;
787
788 /*
789 * Device BARs: 0x10 - 0x18
790 * Bus numbers and windows: 0x18 - 0x30
791 */
792 for (i = 4; i < 13; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100793 eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]);
Gavin Shan652defe2013-06-27 13:46:43 +0800794 /* Rom: 0x38 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100795 eeh_ops->write_config(pdn, 14*4, 4, edev->config_space[14]);
Gavin Shan652defe2013-06-27 13:46:43 +0800796
797 /* Cache line & Latency timer: 0xC 0xD */
Gavin Shan0bd78582015-03-17 16:15:07 +1100798 eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1,
Gavin Shan652defe2013-06-27 13:46:43 +0800799 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
Gavin Shan0bd78582015-03-17 16:15:07 +1100800 eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1,
Gavin Shan652defe2013-06-27 13:46:43 +0800801 SAVED_BYTE(PCI_LATENCY_TIMER));
802 /* Max latency, min grant, interrupt ping and line: 0x3C */
Gavin Shan0bd78582015-03-17 16:15:07 +1100803 eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]);
Gavin Shan652defe2013-06-27 13:46:43 +0800804
805 /* PCI Command: 0x4 */
Michael Neuling13a83ea2018-04-11 13:37:58 +1000806 eeh_ops->write_config(pdn, PCI_COMMAND, 4, edev->config_space[1] |
807 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
Gavin Shan652defe2013-06-27 13:46:43 +0800808
809 /* Check the PCIe link is ready */
Gavin Shan0bd78582015-03-17 16:15:07 +1100810 eeh_bridge_check_link(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800811}
812
Gavin Shan0bd78582015-03-17 16:15:07 +1100813static void eeh_restore_device_bars(struct eeh_dev *edev)
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000814{
Gavin Shan0bd78582015-03-17 16:15:07 +1100815 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000816 int i;
817 u32 cmd;
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000818
819 for (i = 4; i < 10; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100820 eeh_ops->write_config(pdn, i*4, 4, edev->config_space[i]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000821 /* 12 == Expansion ROM Address */
Gavin Shan0bd78582015-03-17 16:15:07 +1100822 eeh_ops->write_config(pdn, 12*4, 4, edev->config_space[12]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000823
Gavin Shan0bd78582015-03-17 16:15:07 +1100824 eeh_ops->write_config(pdn, PCI_CACHE_LINE_SIZE, 1,
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000825 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
Gavin Shan0bd78582015-03-17 16:15:07 +1100826 eeh_ops->write_config(pdn, PCI_LATENCY_TIMER, 1,
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000827 SAVED_BYTE(PCI_LATENCY_TIMER));
828
829 /* max latency, min grant, interrupt pin and line */
Gavin Shan0bd78582015-03-17 16:15:07 +1100830 eeh_ops->write_config(pdn, 15*4, 4, edev->config_space[15]);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000831
832 /*
833 * Restore PERR & SERR bits, some devices require it,
834 * don't touch the other command bits
835 */
Gavin Shan0bd78582015-03-17 16:15:07 +1100836 eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cmd);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000837 if (edev->config_space[1] & PCI_COMMAND_PARITY)
838 cmd |= PCI_COMMAND_PARITY;
839 else
840 cmd &= ~PCI_COMMAND_PARITY;
841 if (edev->config_space[1] & PCI_COMMAND_SERR)
842 cmd |= PCI_COMMAND_SERR;
843 else
844 cmd &= ~PCI_COMMAND_SERR;
Gavin Shan0bd78582015-03-17 16:15:07 +1100845 eeh_ops->write_config(pdn, PCI_COMMAND, 4, cmd);
Gavin Shan652defe2013-06-27 13:46:43 +0800846}
847
848/**
849 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
850 * @data: EEH device
851 * @flag: Unused
852 *
853 * Loads the PCI configuration space base address registers,
854 * the expansion ROM base address, the latency timer, and etc.
855 * from the saved values in the device node.
856 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000857static void *eeh_restore_one_device_bars(struct eeh_dev *edev, void *flag)
Gavin Shan652defe2013-06-27 13:46:43 +0800858{
Gavin Shan0bd78582015-03-17 16:15:07 +1100859 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800860
Gavin Shanf5c57712013-07-24 10:24:58 +0800861 /* Do special restore for bridges */
Gavin Shan4b83bd42013-07-24 10:24:59 +0800862 if (edev->mode & EEH_DEV_BRIDGE)
Gavin Shan0bd78582015-03-17 16:15:07 +1100863 eeh_restore_bridge_bars(edev);
Gavin Shan652defe2013-06-27 13:46:43 +0800864 else
Gavin Shan0bd78582015-03-17 16:15:07 +1100865 eeh_restore_device_bars(edev);
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000866
Gavin Shan0bd78582015-03-17 16:15:07 +1100867 if (eeh_ops->restore_config && pdn)
868 eeh_ops->restore_config(pdn);
Gavin Shan1d350542014-01-03 17:47:12 +0800869
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000870 return NULL;
871}
872
873/**
874 * eeh_pe_restore_bars - Restore the PCI config space info
875 * @pe: EEH PE
876 *
877 * This routine performs a recursive walk to the children
878 * of this device as well.
879 */
880void eeh_pe_restore_bars(struct eeh_pe *pe)
881{
Gavin Shanea812452012-09-11 19:16:18 +0000882 /*
883 * We needn't take the EEH lock since eeh_pe_dev_traverse()
884 * will take that.
885 */
Gavin Shan9e6d2cf2012-09-07 22:44:15 +0000886 eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
887}
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000888
889/**
Gavin Shan357b2f32014-06-11 18:26:44 +1000890 * eeh_pe_loc_get - Retrieve location code binding to the given PE
891 * @pe: EEH PE
892 *
893 * Retrieve the location code of the given PE. If the primary PE bus
894 * is root bus, we will grab location code from PHB device tree node
895 * or root port. Otherwise, the upstream bridge's device tree node
896 * of the primary PE bus will be checked for the location code.
897 */
898const char *eeh_pe_loc_get(struct eeh_pe *pe)
899{
Gavin Shan357b2f32014-06-11 18:26:44 +1000900 struct pci_bus *bus = eeh_pe_bus_get(pe);
Gavin Shan7e56f622015-12-02 16:25:32 +1100901 struct device_node *dn;
Mike Qiu9e5c6e52014-07-15 01:42:22 -0400902 const char *loc = NULL;
Gavin Shan357b2f32014-06-11 18:26:44 +1000903
Gavin Shan7e56f622015-12-02 16:25:32 +1100904 while (bus) {
905 dn = pci_bus_to_OF_node(bus);
906 if (!dn) {
907 bus = bus->parent;
908 continue;
909 }
Gavin Shan357b2f32014-06-11 18:26:44 +1000910
Gavin Shan7e56f622015-12-02 16:25:32 +1100911 if (pci_is_root_bus(bus))
Mike Qiu9e5c6e52014-07-15 01:42:22 -0400912 loc = of_get_property(dn, "ibm,io-base-loc-code", NULL);
Gavin Shan7e56f622015-12-02 16:25:32 +1100913 else
914 loc = of_get_property(dn, "ibm,slot-location-code",
915 NULL);
Gavin Shan357b2f32014-06-11 18:26:44 +1000916
Gavin Shan7e56f622015-12-02 16:25:32 +1100917 if (loc)
918 return loc;
919
920 bus = bus->parent;
Gavin Shan357b2f32014-06-11 18:26:44 +1000921 }
922
Gavin Shan7e56f622015-12-02 16:25:32 +1100923 return "N/A";
Gavin Shan357b2f32014-06-11 18:26:44 +1000924}
925
926/**
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000927 * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
928 * @pe: EEH PE
929 *
930 * Retrieve the PCI bus according to the given PE. Basically,
931 * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
932 * primary PCI bus will be retrieved. The parent bus will be
933 * returned for BUS PE. However, we don't have associated PCI
934 * bus for DEVICE PE.
935 */
936struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
937{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000938 struct eeh_dev *edev;
939 struct pci_dev *pdev;
940
Gavin Shan4eb07992016-02-09 15:50:23 +1100941 if (pe->type & EEH_PE_PHB)
942 return pe->phb->bus;
Gavin Shan8cdb2832013-06-20 13:20:55 +0800943
Gavin Shan4eb07992016-02-09 15:50:23 +1100944 /* The primary bus might be cached during probe time */
945 if (pe->state & EEH_PE_PRI_BUS)
946 return pe->bus;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000947
Gavin Shan4eb07992016-02-09 15:50:23 +1100948 /* Retrieve the parent PCI bus of first (top) PCI device */
949 edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, list);
950 pdev = eeh_dev_to_pci_dev(edev);
951 if (pdev)
952 return pdev->bus;
953
954 return NULL;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000955}