1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
4 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
8 * Send feedback to <lxie@us.ibm.com>
12 #include <linux/pci.h>
13 #include <linux/string.h>
15 #include <asm/pci-bridge.h>
17 #include <asm/machdep.h>
19 #include "../pci.h" /* for pci_add_new_bus */
23 * RTAS call get-sensor-state(DR_ENTITY_SENSE) return values as per PAPR:
24 * -- generic return codes ---
27 * -3: Invalid sensor. RTAS Parameter Error.
28 * -- rtas_get_sensor function specific return codes ---
29 * -9000: Need DR entity to be powered up and unisolated before RTAS call
30 * -9001: Need DR entity to be powered up, but not unisolated, before RTAS call
31 * -9002: DR entity unusable
32 * 990x: Extended delay - where x is a number in the range of 0-5
34 #define RTAS_SLOT_UNISOLATED -9000
35 #define RTAS_SLOT_NOT_UNISOLATED -9001
36 #define RTAS_SLOT_NOT_USABLE -9002
38 static int rtas_get_sensor_errno(int rtas_rc
)
44 case RTAS_SLOT_UNISOLATED
:
45 case RTAS_SLOT_NOT_UNISOLATED
:
47 case RTAS_SLOT_NOT_USABLE
:
50 case RTAS_EXTENDED_DELAY_MIN
...RTAS_EXTENDED_DELAY_MAX
:
53 return rtas_error_rc(rtas_rc
);
58 * get_adapter_status() can be called by the EEH handler during EEH recovery.
59 * On certain PHB failures, the RTAS call rtas_call(get-sensor-state) returns
60 * extended busy error (9902) until PHB is recovered by pHyp. The RTAS call
61 * interface rtas_get_sensor() loops over the RTAS call on extended delay
62 * return code (9902) until the return value is either success (0) or error
63 * (-1). This causes the EEH handler to get stuck for ~6 seconds before it
64 * could notify that the PCI error has been detected and stop any active
65 * operations. This sometimes causes EEH recovery to fail. To avoid this issue,
66 * invoke rtas_call(get-sensor-state) directly if the respective PE is in EEH
67 * recovery state and return -EBUSY error based on RTAS return status. This
68 * will help the EEH handler to notify the driver about the PCI error
69 * immediately and successfully proceed with EEH recovery steps.
72 static int __rpaphp_get_sensor_state(struct slot
*slot
, int *state
)
75 int token
= rtas_token("get-sensor-state");
78 struct pci_controller
*phb
= PCI_DN(slot
->dn
)->phb
;
80 if (token
== RTAS_UNKNOWN_SERVICE
)
84 * Fallback to existing method for empty slot or PE isn't in EEH
87 pdn
= list_first_entry_or_null(&PCI_DN(phb
->dn
)->child_list
,
92 pe
= eeh_dev_to_pe(pdn
->edev
);
93 if (pe
&& (pe
->state
& EEH_PE_RECOVERING
)) {
94 rc
= rtas_call(token
, 2, 2, state
, DR_ENTITY_SENSE
,
96 return rtas_get_sensor_errno(rc
);
99 return rtas_get_sensor(DR_ENTITY_SENSE
, slot
->index
, state
);
102 int rpaphp_get_sensor_state(struct slot
*slot
, int *state
)
107 rc
= __rpaphp_get_sensor_state(slot
, state
);
110 if (rc
== -EFAULT
|| rc
== -EEXIST
) {
111 dbg("%s: slot must be power up to get sensor-state\n",
114 /* some slots have to be powered up
115 * before get-sensor will succeed.
117 rc
= rtas_set_power_level(slot
->power_domain
, POWER_ON
,
120 dbg("%s: power on slot[%s] failed rc=%d.\n",
121 __func__
, slot
->name
, rc
);
123 rc
= __rpaphp_get_sensor_state(slot
, state
);
125 } else if (rc
== -ENODEV
)
126 info("%s: slot is unusable\n", __func__
);
128 err("%s failed to get sensor state\n", __func__
);
134 * rpaphp_enable_slot - record slot state, config pci device
135 * @slot: target &slot
137 * Initialize values in the slot structure to indicate if there is a pci card
138 * plugged into the slot. If the slot is not empty, run the pcibios routine
139 * to get pcibios stuff correctly set up.
141 int rpaphp_enable_slot(struct slot
*slot
)
143 int rc
, level
, state
;
148 /* Find out if the power is turned on for the slot */
149 rc
= rtas_get_power_level(slot
->power_domain
, &level
);
153 /* Figure out if there is an adapter in the slot */
154 rc
= rpaphp_get_sensor_state(slot
, &state
);
158 bus
= pci_find_bus_by_node(slot
->dn
);
160 err("%s: no pci_bus for dn %pOF\n", __func__
, slot
->dn
);
165 slot
->pci_devs
= &bus
->devices
;
167 /* if there's an adapter in the slot, go add the pci devices */
168 if (state
== PRESENT
) {
169 slot
->state
= NOT_CONFIGURED
;
171 /* non-empty slot has to have child */
172 if (!slot
->dn
->child
) {
173 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
174 __func__
, slot
->name
);
178 if (list_empty(&bus
->devices
)) {
179 pseries_eeh_init_edev_recursive(PCI_DN(slot
->dn
));
180 pci_hp_add_devices(bus
);
183 if (!list_empty(&bus
->devices
)) {
184 slot
->state
= CONFIGURED
;
189 dbg("%s: pci_devs of slot[%pOF]\n", __func__
, slot
->dn
);
190 list_for_each_entry(dev
, &bus
->devices
, bus_list
)
191 dbg("\t%s\n", pci_name(dev
));