// SPDX-License-Identifier: GPL-2.0-or-later1/*2* The file intends to implement PE based on the information from3* platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.4* All the PEs should be organized as hierarchy tree. The first level5* of the tree will be associated to existing PHBs since the particular6* PE is only meaningful in one PHB domain.7*8* Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.9*/1011#include <linux/delay.h>12#include <linux/export.h>13#include <linux/gfp.h>14#include <linux/kernel.h>15#include <linux/of.h>16#include <linux/pci.h>17#include <linux/string.h>1819#include <asm/pci-bridge.h>20#include <asm/ppc-pci.h>2122static int eeh_pe_aux_size = 0;23static LIST_HEAD(eeh_phb_pe);2425/**26* eeh_set_pe_aux_size - Set PE auxiliary data size27* @size: PE auxiliary data size in bytes28*29* Set PE auxiliary data size.30*/31void eeh_set_pe_aux_size(int size)32{33if (size < 0)34return;3536eeh_pe_aux_size = size;37}3839/**40* eeh_pe_alloc - Allocate PE41* @phb: PCI controller42* @type: PE type43*44* Allocate PE instance dynamically.45*/46static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)47{48struct eeh_pe *pe;49size_t alloc_size;5051alloc_size = sizeof(struct eeh_pe);52if (eeh_pe_aux_size) {53alloc_size = ALIGN(alloc_size, cache_line_size());54alloc_size += eeh_pe_aux_size;55}5657/* Allocate PHB PE */58pe = kzalloc(alloc_size, GFP_KERNEL);59if (!pe) return NULL;6061/* Initialize PHB PE */62pe->type = type;63pe->phb = phb;64INIT_LIST_HEAD(&pe->child_list);65INIT_LIST_HEAD(&pe->edevs);6667pe->data = (void *)pe + ALIGN(sizeof(struct eeh_pe),68cache_line_size());69return pe;70}7172/**73* eeh_phb_pe_create - Create PHB PE74* @phb: PCI controller75*76* The function should be called while the PHB is detected during77* system boot or PCI hotplug in order to create PHB PE.78*/79int eeh_phb_pe_create(struct pci_controller *phb)80{81struct eeh_pe *pe;8283/* Allocate PHB PE */84pe = eeh_pe_alloc(phb, EEH_PE_PHB);85if (!pe) {86pr_err("%s: out of memory!\n", __func__);87return -ENOMEM;88}8990/* Put it into the list */91list_add_tail(&pe->child, &eeh_phb_pe);9293pr_debug("EEH: Add PE for PHB#%x\n", phb->global_number);9495return 0;96}9798/**99* eeh_wait_state - Wait for PE state100* @pe: EEH PE101* @max_wait: maximal period in millisecond102*103* Wait for the state of associated PE. It might take some time104* to retrieve the PE's state.105*/106int eeh_wait_state(struct eeh_pe *pe, int max_wait)107{108int ret;109int mwait;110111/*112* According to PAPR, the state of PE might be temporarily113* unavailable. Under the circumstance, we have to wait114* for indicated time determined by firmware. The maximal115* wait time is 5 minutes, which is acquired from the original116* EEH implementation. Also, the original implementation117* also defined the minimal wait time as 1 second.118*/119#define EEH_STATE_MIN_WAIT_TIME (1000)120#define EEH_STATE_MAX_WAIT_TIME (300 * 1000)121122while (1) {123ret = eeh_ops->get_state(pe, &mwait);124125if (ret != EEH_STATE_UNAVAILABLE)126return ret;127128if (max_wait <= 0) {129pr_warn("%s: Timeout when getting PE's state (%d)\n",130__func__, max_wait);131return EEH_STATE_NOT_SUPPORT;132}133134if (mwait < EEH_STATE_MIN_WAIT_TIME) {135pr_warn("%s: Firmware returned bad wait value %d\n",136__func__, mwait);137mwait = EEH_STATE_MIN_WAIT_TIME;138} else if (mwait > EEH_STATE_MAX_WAIT_TIME) {139pr_warn("%s: Firmware returned too long wait value %d\n",140__func__, mwait);141mwait = EEH_STATE_MAX_WAIT_TIME;142}143144msleep(min(mwait, max_wait));145max_wait -= mwait;146}147}148149/**150* eeh_phb_pe_get - Retrieve PHB PE based on the given PHB151* @phb: PCI controller152*153* The overall PEs form hierarchy tree. The first layer of the154* hierarchy tree is composed of PHB PEs. The function is used155* to retrieve the corresponding PHB PE according to the given PHB.156*/157struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)158{159struct eeh_pe *pe;160161list_for_each_entry(pe, &eeh_phb_pe, child) {162/*163* Actually, we needn't check the type since164* the PE for PHB has been determined when that165* was created.166*/167if ((pe->type & EEH_PE_PHB) && pe->phb == phb)168return pe;169}170171return NULL;172}173174/**175* eeh_pe_next - Retrieve the next PE in the tree176* @pe: current PE177* @root: root PE178*179* The function is used to retrieve the next PE in the180* hierarchy PE tree.181*/182struct eeh_pe *eeh_pe_next(struct eeh_pe *pe, struct eeh_pe *root)183{184struct list_head *next = pe->child_list.next;185186if (next == &pe->child_list) {187while (1) {188if (pe == root)189return NULL;190next = pe->child.next;191if (next != &pe->parent->child_list)192break;193pe = pe->parent;194}195}196197return list_entry(next, struct eeh_pe, child);198}199200/**201* eeh_pe_traverse - Traverse PEs in the specified PHB202* @root: root PE203* @fn: callback204* @flag: extra parameter to callback205*206* The function is used to traverse the specified PE and its207* child PEs. The traversing is to be terminated once the208* callback returns something other than NULL, or no more PEs209* to be traversed.210*/211void *eeh_pe_traverse(struct eeh_pe *root,212eeh_pe_traverse_func fn, void *flag)213{214struct eeh_pe *pe;215void *ret;216217eeh_for_each_pe(root, pe) {218ret = fn(pe, flag);219if (ret) return ret;220}221222return NULL;223}224225/**226* eeh_pe_dev_traverse - Traverse the devices from the PE227* @root: EEH PE228* @fn: function callback229* @flag: extra parameter to callback230*231* The function is used to traverse the devices of the specified232* PE and its child PEs.233*/234void eeh_pe_dev_traverse(struct eeh_pe *root,235eeh_edev_traverse_func fn, void *flag)236{237struct eeh_pe *pe;238struct eeh_dev *edev, *tmp;239240if (!root) {241pr_warn("%s: Invalid PE %p\n",242__func__, root);243return;244}245246/* Traverse root PE */247eeh_for_each_pe(root, pe)248eeh_pe_for_each_dev(pe, edev, tmp)249fn(edev, flag);250}251252/**253* __eeh_pe_get - Check the PE address254*255* For one particular PE, it can be identified by PE address256* or tranditional BDF address. BDF address is composed of257* Bus/Device/Function number. The extra data referred by flag258* indicates which type of address should be used.259*/260static void *__eeh_pe_get(struct eeh_pe *pe, void *flag)261{262int *target_pe = flag;263264/* PHB PEs are special and should be ignored */265if (pe->type & EEH_PE_PHB)266return NULL;267268if (*target_pe == pe->addr)269return pe;270271return NULL;272}273274/**275* eeh_pe_get - Search PE based on the given address276* @phb: PCI controller277* @pe_no: PE number278*279* Search the corresponding PE based on the specified address which280* is included in the eeh device. The function is used to check if281* the associated PE has been created against the PE address. It's282* notable that the PE address has 2 format: traditional PE address283* which is composed of PCI bus/device/function number, or unified284* PE address.285*/286struct eeh_pe *eeh_pe_get(struct pci_controller *phb, int pe_no)287{288struct eeh_pe *root = eeh_phb_pe_get(phb);289290return eeh_pe_traverse(root, __eeh_pe_get, &pe_no);291}292293/**294* eeh_pe_tree_insert - Add EEH device to parent PE295* @edev: EEH device296* @new_pe_parent: PE to create additional PEs under297*298* Add EEH device to the PE in edev->pe_config_addr. If a PE already299* exists with that address then @edev is added to that PE. Otherwise300* a new PE is created and inserted into the PE tree as a child of301* @new_pe_parent.302*303* If @new_pe_parent is NULL then the new PE will be inserted under304* directly under the PHB.305*/306int eeh_pe_tree_insert(struct eeh_dev *edev, struct eeh_pe *new_pe_parent)307{308struct pci_controller *hose = edev->controller;309struct eeh_pe *pe, *parent;310311/*312* Search the PE has been existing or not according313* to the PE address. If that has been existing, the314* PE should be composed of PCI bus and its subordinate315* components.316*/317pe = eeh_pe_get(hose, edev->pe_config_addr);318if (pe) {319if (pe->type & EEH_PE_INVALID) {320list_add_tail(&edev->entry, &pe->edevs);321edev->pe = pe;322/*323* We're running to here because of PCI hotplug caused by324* EEH recovery. We need clear EEH_PE_INVALID until the top.325*/326parent = pe;327while (parent) {328if (!(parent->type & EEH_PE_INVALID))329break;330parent->type &= ~EEH_PE_INVALID;331parent = parent->parent;332}333334eeh_edev_dbg(edev, "Added to existing PE (parent: PE#%x)\n",335pe->parent->addr);336} else {337/* Mark the PE as type of PCI bus */338pe->type = EEH_PE_BUS;339edev->pe = pe;340341/* Put the edev to PE */342list_add_tail(&edev->entry, &pe->edevs);343eeh_edev_dbg(edev, "Added to bus PE\n");344}345return 0;346}347348/* Create a new EEH PE */349if (edev->physfn)350pe = eeh_pe_alloc(hose, EEH_PE_VF);351else352pe = eeh_pe_alloc(hose, EEH_PE_DEVICE);353if (!pe) {354pr_err("%s: out of memory!\n", __func__);355return -ENOMEM;356}357358pe->addr = edev->pe_config_addr;359360/*361* Put the new EEH PE into hierarchy tree. If the parent362* can't be found, the newly created PE will be attached363* to PHB directly. Otherwise, we have to associate the364* PE with its parent.365*/366if (!new_pe_parent) {367new_pe_parent = eeh_phb_pe_get(hose);368if (!new_pe_parent) {369pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",370__func__, hose->global_number);371edev->pe = NULL;372kfree(pe);373return -EEXIST;374}375}376377/* link new PE into the tree */378pe->parent = new_pe_parent;379list_add_tail(&pe->child, &new_pe_parent->child_list);380381/*382* Put the newly created PE into the child list and383* link the EEH device accordingly.384*/385list_add_tail(&edev->entry, &pe->edevs);386edev->pe = pe;387eeh_edev_dbg(edev, "Added to new (parent: PE#%x)\n",388new_pe_parent->addr);389390return 0;391}392393/**394* eeh_pe_tree_remove - Remove one EEH device from the associated PE395* @edev: EEH device396*397* The PE hierarchy tree might be changed when doing PCI hotplug.398* Also, the PCI devices or buses could be removed from the system399* during EEH recovery. So we have to call the function remove the400* corresponding PE accordingly if necessary.401*/402int eeh_pe_tree_remove(struct eeh_dev *edev)403{404struct eeh_pe *pe, *parent, *child;405bool keep, recover;406int cnt;407408pe = eeh_dev_to_pe(edev);409if (!pe) {410eeh_edev_dbg(edev, "No PE found for device.\n");411return -EEXIST;412}413414/* Remove the EEH device */415edev->pe = NULL;416list_del(&edev->entry);417418/*419* Check if the parent PE includes any EEH devices.420* If not, we should delete that. Also, we should421* delete the parent PE if it doesn't have associated422* child PEs and EEH devices.423*/424while (1) {425parent = pe->parent;426427/* PHB PEs should never be removed */428if (pe->type & EEH_PE_PHB)429break;430431/*432* XXX: KEEP is set while resetting a PE. I don't think it's433* ever set without RECOVERING also being set. I could434* be wrong though so catch that with a WARN.435*/436keep = !!(pe->state & EEH_PE_KEEP);437recover = !!(pe->state & EEH_PE_RECOVERING);438WARN_ON(keep && !recover);439440if (!keep && !recover) {441if (list_empty(&pe->edevs) &&442list_empty(&pe->child_list)) {443list_del(&pe->child);444kfree(pe);445} else {446break;447}448} else {449/*450* Mark the PE as invalid. At the end of the recovery451* process any invalid PEs will be garbage collected.452*453* We need to delay the free()ing of them since we can454* remove edev's while traversing the PE tree which455* might trigger the removal of a PE and we can't456* deal with that (yet).457*/458if (list_empty(&pe->edevs)) {459cnt = 0;460list_for_each_entry(child, &pe->child_list, child) {461if (!(child->type & EEH_PE_INVALID)) {462cnt++;463break;464}465}466467if (!cnt)468pe->type |= EEH_PE_INVALID;469else470break;471}472}473474pe = parent;475}476477return 0;478}479480/**481* eeh_pe_update_time_stamp - Update PE's frozen time stamp482* @pe: EEH PE483*484* We have time stamp for each PE to trace its time of getting485* frozen in last hour. The function should be called to update486* the time stamp on first error of the specific PE. On the other487* handle, we needn't account for errors happened in last hour.488*/489void eeh_pe_update_time_stamp(struct eeh_pe *pe)490{491time64_t tstamp;492493if (!pe) return;494495if (pe->freeze_count <= 0) {496pe->freeze_count = 0;497pe->tstamp = ktime_get_seconds();498} else {499tstamp = ktime_get_seconds();500if (tstamp - pe->tstamp > 3600) {501pe->tstamp = tstamp;502pe->freeze_count = 0;503}504}505}506507/**508* eeh_pe_state_mark - Mark specified state for PE and its associated device509* @pe: EEH PE510*511* EEH error affects the current PE and its child PEs. The function512* is used to mark appropriate state for the affected PEs and the513* associated devices.514*/515void eeh_pe_state_mark(struct eeh_pe *root, int state)516{517struct eeh_pe *pe;518519eeh_for_each_pe(root, pe)520if (!(pe->state & EEH_PE_REMOVED))521pe->state |= state;522}523EXPORT_SYMBOL_GPL(eeh_pe_state_mark);524525/**526* eeh_pe_mark_isolated527* @pe: EEH PE528*529* Record that a PE has been isolated by marking the PE and its children as530* EEH_PE_ISOLATED (and EEH_PE_CFG_BLOCKED, if required) and their PCI devices531* as pci_channel_io_frozen.532*/533void eeh_pe_mark_isolated(struct eeh_pe *root)534{535struct eeh_pe *pe;536struct eeh_dev *edev;537struct pci_dev *pdev;538539eeh_pe_state_mark(root, EEH_PE_ISOLATED);540eeh_for_each_pe(root, pe) {541list_for_each_entry(edev, &pe->edevs, entry) {542pdev = eeh_dev_to_pci_dev(edev);543if (pdev)544pdev->error_state = pci_channel_io_frozen;545}546/* Block PCI config access if required */547if (pe->state & EEH_PE_CFG_RESTRICTED)548pe->state |= EEH_PE_CFG_BLOCKED;549}550}551EXPORT_SYMBOL_GPL(eeh_pe_mark_isolated);552553static void __eeh_pe_dev_mode_mark(struct eeh_dev *edev, void *flag)554{555int mode = *((int *)flag);556557edev->mode |= mode;558}559560/**561* eeh_pe_dev_state_mark - Mark state for all device under the PE562* @pe: EEH PE563*564* Mark specific state for all child devices of the PE.565*/566void eeh_pe_dev_mode_mark(struct eeh_pe *pe, int mode)567{568eeh_pe_dev_traverse(pe, __eeh_pe_dev_mode_mark, &mode);569}570571/**572* eeh_pe_state_clear - Clear state for the PE573* @data: EEH PE574* @state: state575* @include_passed: include passed-through devices?576*577* The function is used to clear the indicated state from the578* given PE. Besides, we also clear the check count of the PE579* as well.580*/581void eeh_pe_state_clear(struct eeh_pe *root, int state, bool include_passed)582{583struct eeh_pe *pe;584struct eeh_dev *edev, *tmp;585struct pci_dev *pdev;586587eeh_for_each_pe(root, pe) {588/* Keep the state of permanently removed PE intact */589if (pe->state & EEH_PE_REMOVED)590continue;591592if (!include_passed && eeh_pe_passed(pe))593continue;594595pe->state &= ~state;596597/*598* Special treatment on clearing isolated state. Clear599* check count since last isolation and put all affected600* devices to normal state.601*/602if (!(state & EEH_PE_ISOLATED))603continue;604605pe->check_count = 0;606eeh_pe_for_each_dev(pe, edev, tmp) {607pdev = eeh_dev_to_pci_dev(edev);608if (!pdev)609continue;610611pdev->error_state = pci_channel_io_normal;612}613614/* Unblock PCI config access if required */615if (pe->state & EEH_PE_CFG_RESTRICTED)616pe->state &= ~EEH_PE_CFG_BLOCKED;617}618}619620/*621* Some PCI bridges (e.g. PLX bridges) have primary/secondary622* buses assigned explicitly by firmware, and we probably have623* lost that after reset. So we have to delay the check until624* the PCI-CFG registers have been restored for the parent625* bridge.626*627* Don't use normal PCI-CFG accessors, which probably has been628* blocked on normal path during the stage. So we need utilize629* eeh operations, which is always permitted.630*/631static void eeh_bridge_check_link(struct eeh_dev *edev)632{633int cap;634uint32_t val;635int timeout = 0;636637/*638* We only check root port and downstream ports of639* PCIe switches640*/641if (!(edev->mode & (EEH_DEV_ROOT_PORT | EEH_DEV_DS_PORT)))642return;643644eeh_edev_dbg(edev, "Checking PCIe link...\n");645646/* Check slot status */647cap = edev->pcie_cap;648eeh_ops->read_config(edev, cap + PCI_EXP_SLTSTA, 2, &val);649if (!(val & PCI_EXP_SLTSTA_PDS)) {650eeh_edev_dbg(edev, "No card in the slot (0x%04x) !\n", val);651return;652}653654/* Check power status if we have the capability */655eeh_ops->read_config(edev, cap + PCI_EXP_SLTCAP, 2, &val);656if (val & PCI_EXP_SLTCAP_PCP) {657eeh_ops->read_config(edev, cap + PCI_EXP_SLTCTL, 2, &val);658if (val & PCI_EXP_SLTCTL_PCC) {659eeh_edev_dbg(edev, "In power-off state, power it on ...\n");660val &= ~(PCI_EXP_SLTCTL_PCC | PCI_EXP_SLTCTL_PIC);661val |= (0x0100 & PCI_EXP_SLTCTL_PIC);662eeh_ops->write_config(edev, cap + PCI_EXP_SLTCTL, 2, val);663msleep(2 * 1000);664}665}666667/* Enable link */668eeh_ops->read_config(edev, cap + PCI_EXP_LNKCTL, 2, &val);669val &= ~PCI_EXP_LNKCTL_LD;670eeh_ops->write_config(edev, cap + PCI_EXP_LNKCTL, 2, val);671672/* Check link */673if (edev->pdev) {674if (!edev->pdev->link_active_reporting) {675eeh_edev_dbg(edev, "No link reporting capability\n");676msleep(1000);677return;678}679}680681/* Wait the link is up until timeout (5s) */682timeout = 0;683while (timeout < 5000) {684msleep(20);685timeout += 20;686687eeh_ops->read_config(edev, cap + PCI_EXP_LNKSTA, 2, &val);688if (val & PCI_EXP_LNKSTA_DLLLA)689break;690}691692if (val & PCI_EXP_LNKSTA_DLLLA)693eeh_edev_dbg(edev, "Link up (%s)\n",694(val & PCI_EXP_LNKSTA_CLS_2_5GB) ? "2.5GB" : "5GB");695else696eeh_edev_dbg(edev, "Link not ready (0x%04x)\n", val);697}698699#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))700#define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])701702static void eeh_restore_bridge_bars(struct eeh_dev *edev)703{704int i;705706/*707* Device BARs: 0x10 - 0x18708* Bus numbers and windows: 0x18 - 0x30709*/710for (i = 4; i < 13; i++)711eeh_ops->write_config(edev, i*4, 4, edev->config_space[i]);712/* Rom: 0x38 */713eeh_ops->write_config(edev, 14*4, 4, edev->config_space[14]);714715/* Cache line & Latency timer: 0xC 0xD */716eeh_ops->write_config(edev, PCI_CACHE_LINE_SIZE, 1,717SAVED_BYTE(PCI_CACHE_LINE_SIZE));718eeh_ops->write_config(edev, PCI_LATENCY_TIMER, 1,719SAVED_BYTE(PCI_LATENCY_TIMER));720/* Max latency, min grant, interrupt ping and line: 0x3C */721eeh_ops->write_config(edev, 15*4, 4, edev->config_space[15]);722723/* PCI Command: 0x4 */724eeh_ops->write_config(edev, PCI_COMMAND, 4, edev->config_space[1] |725PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);726727/* Check the PCIe link is ready */728eeh_bridge_check_link(edev);729}730731static void eeh_restore_device_bars(struct eeh_dev *edev)732{733int i;734u32 cmd;735736for (i = 4; i < 10; i++)737eeh_ops->write_config(edev, i*4, 4, edev->config_space[i]);738/* 12 == Expansion ROM Address */739eeh_ops->write_config(edev, 12*4, 4, edev->config_space[12]);740741eeh_ops->write_config(edev, PCI_CACHE_LINE_SIZE, 1,742SAVED_BYTE(PCI_CACHE_LINE_SIZE));743eeh_ops->write_config(edev, PCI_LATENCY_TIMER, 1,744SAVED_BYTE(PCI_LATENCY_TIMER));745746/* max latency, min grant, interrupt pin and line */747eeh_ops->write_config(edev, 15*4, 4, edev->config_space[15]);748749/*750* Restore PERR & SERR bits, some devices require it,751* don't touch the other command bits752*/753eeh_ops->read_config(edev, PCI_COMMAND, 4, &cmd);754if (edev->config_space[1] & PCI_COMMAND_PARITY)755cmd |= PCI_COMMAND_PARITY;756else757cmd &= ~PCI_COMMAND_PARITY;758if (edev->config_space[1] & PCI_COMMAND_SERR)759cmd |= PCI_COMMAND_SERR;760else761cmd &= ~PCI_COMMAND_SERR;762eeh_ops->write_config(edev, PCI_COMMAND, 4, cmd);763}764765/**766* eeh_restore_one_device_bars - Restore the Base Address Registers for one device767* @data: EEH device768* @flag: Unused769*770* Loads the PCI configuration space base address registers,771* the expansion ROM base address, the latency timer, and etc.772* from the saved values in the device node.773*/774static void eeh_restore_one_device_bars(struct eeh_dev *edev, void *flag)775{776/* Do special restore for bridges */777if (edev->mode & EEH_DEV_BRIDGE)778eeh_restore_bridge_bars(edev);779else780eeh_restore_device_bars(edev);781782if (eeh_ops->restore_config)783eeh_ops->restore_config(edev);784}785786/**787* eeh_pe_restore_bars - Restore the PCI config space info788* @pe: EEH PE789*790* This routine performs a recursive walk to the children791* of this device as well.792*/793void eeh_pe_restore_bars(struct eeh_pe *pe)794{795/*796* We needn't take the EEH lock since eeh_pe_dev_traverse()797* will take that.798*/799eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);800}801802/**803* eeh_pe_loc_get - Retrieve location code binding to the given PE804* @pe: EEH PE805*806* Retrieve the location code of the given PE. If the primary PE bus807* is root bus, we will grab location code from PHB device tree node808* or root port. Otherwise, the upstream bridge's device tree node809* of the primary PE bus will be checked for the location code.810*/811const char *eeh_pe_loc_get(struct eeh_pe *pe)812{813struct pci_bus *bus = eeh_pe_bus_get(pe);814struct device_node *dn;815const char *loc = NULL;816817while (bus) {818dn = pci_bus_to_OF_node(bus);819if (!dn) {820bus = bus->parent;821continue;822}823824if (pci_is_root_bus(bus))825loc = of_get_property(dn, "ibm,io-base-loc-code", NULL);826else827loc = of_get_property(dn, "ibm,slot-location-code",828NULL);829830if (loc)831return loc;832833bus = bus->parent;834}835836return "N/A";837}838839/**840* eeh_pe_bus_get - Retrieve PCI bus according to the given PE841* @pe: EEH PE842*843* Retrieve the PCI bus according to the given PE. Basically,844* there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the845* primary PCI bus will be retrieved. The parent bus will be846* returned for BUS PE. However, we don't have associated PCI847* bus for DEVICE PE.848*/849struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)850{851struct eeh_dev *edev;852struct pci_dev *pdev;853struct pci_bus *bus = NULL;854855if (pe->type & EEH_PE_PHB)856return pe->phb->bus;857858/* The primary bus might be cached during probe time */859if (pe->state & EEH_PE_PRI_BUS)860return pe->bus;861862/* Retrieve the parent PCI bus of first (top) PCI device */863edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry);864pci_lock_rescan_remove();865pdev = eeh_dev_to_pci_dev(edev);866if (pdev)867bus = pdev->bus;868pci_unlock_rescan_remove();869870return bus;871}872873874