xref: /OK3568_Linux_fs/kernel/drivers/pci/hotplug/rpaphp_pci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
4*4882a593Smuzhiyun  * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * All rights reserved.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Send feedback to <lxie@us.ibm.com>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #include <linux/pci.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm/pci-bridge.h>
15*4882a593Smuzhiyun #include <asm/rtas.h>
16*4882a593Smuzhiyun #include <asm/machdep.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "../pci.h"		/* for pci_add_new_bus */
19*4882a593Smuzhiyun #include "rpaphp.h"
20*4882a593Smuzhiyun 
rpaphp_get_sensor_state(struct slot * slot,int * state)21*4882a593Smuzhiyun int rpaphp_get_sensor_state(struct slot *slot, int *state)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	int rc;
24*4882a593Smuzhiyun 	int setlevel;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	if (rc < 0) {
29*4882a593Smuzhiyun 		if (rc == -EFAULT || rc == -EEXIST) {
30*4882a593Smuzhiyun 			dbg("%s: slot must be power up to get sensor-state\n",
31*4882a593Smuzhiyun 			    __func__);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 			/* some slots have to be powered up
34*4882a593Smuzhiyun 			 * before get-sensor will succeed.
35*4882a593Smuzhiyun 			 */
36*4882a593Smuzhiyun 			rc = rtas_set_power_level(slot->power_domain, POWER_ON,
37*4882a593Smuzhiyun 						  &setlevel);
38*4882a593Smuzhiyun 			if (rc < 0) {
39*4882a593Smuzhiyun 				dbg("%s: power on slot[%s] failed rc=%d.\n",
40*4882a593Smuzhiyun 				    __func__, slot->name, rc);
41*4882a593Smuzhiyun 			} else {
42*4882a593Smuzhiyun 				rc = rtas_get_sensor(DR_ENTITY_SENSE,
43*4882a593Smuzhiyun 						     slot->index, state);
44*4882a593Smuzhiyun 			}
45*4882a593Smuzhiyun 		} else if (rc == -ENODEV)
46*4882a593Smuzhiyun 			info("%s: slot is unusable\n", __func__);
47*4882a593Smuzhiyun 		else
48*4882a593Smuzhiyun 			err("%s failed to get sensor state\n", __func__);
49*4882a593Smuzhiyun 	}
50*4882a593Smuzhiyun 	return rc;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * rpaphp_enable_slot - record slot state, config pci device
55*4882a593Smuzhiyun  * @slot: target &slot
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * Initialize values in the slot structure to indicate if there is a pci card
58*4882a593Smuzhiyun  * plugged into the slot. If the slot is not empty, run the pcibios routine
59*4882a593Smuzhiyun  * to get pcibios stuff correctly set up.
60*4882a593Smuzhiyun  */
rpaphp_enable_slot(struct slot * slot)61*4882a593Smuzhiyun int rpaphp_enable_slot(struct slot *slot)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	int rc, level, state;
64*4882a593Smuzhiyun 	struct pci_bus *bus;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	slot->state = EMPTY;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	/* Find out if the power is turned on for the slot */
69*4882a593Smuzhiyun 	rc = rtas_get_power_level(slot->power_domain, &level);
70*4882a593Smuzhiyun 	if (rc)
71*4882a593Smuzhiyun 		return rc;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	/* Figure out if there is an adapter in the slot */
74*4882a593Smuzhiyun 	rc = rpaphp_get_sensor_state(slot, &state);
75*4882a593Smuzhiyun 	if (rc)
76*4882a593Smuzhiyun 		return rc;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	bus = pci_find_bus_by_node(slot->dn);
79*4882a593Smuzhiyun 	if (!bus) {
80*4882a593Smuzhiyun 		err("%s: no pci_bus for dn %pOF\n", __func__, slot->dn);
81*4882a593Smuzhiyun 		return -EINVAL;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	slot->bus = bus;
85*4882a593Smuzhiyun 	slot->pci_devs = &bus->devices;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/* if there's an adapter in the slot, go add the pci devices */
88*4882a593Smuzhiyun 	if (state == PRESENT) {
89*4882a593Smuzhiyun 		slot->state = NOT_CONFIGURED;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		/* non-empty slot has to have child */
92*4882a593Smuzhiyun 		if (!slot->dn->child) {
93*4882a593Smuzhiyun 			err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
94*4882a593Smuzhiyun 			    __func__, slot->name);
95*4882a593Smuzhiyun 			return -EINVAL;
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		if (list_empty(&bus->devices)) {
99*4882a593Smuzhiyun 			pseries_eeh_init_edev_recursive(PCI_DN(slot->dn));
100*4882a593Smuzhiyun 			pci_hp_add_devices(bus);
101*4882a593Smuzhiyun 		}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		if (!list_empty(&bus->devices)) {
104*4882a593Smuzhiyun 			slot->state = CONFIGURED;
105*4882a593Smuzhiyun 		}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 		if (rpaphp_debug) {
108*4882a593Smuzhiyun 			struct pci_dev *dev;
109*4882a593Smuzhiyun 			dbg("%s: pci_devs of slot[%pOF]\n", __func__, slot->dn);
110*4882a593Smuzhiyun 			list_for_each_entry(dev, &bus->devices, bus_list)
111*4882a593Smuzhiyun 				dbg("\t%s\n", pci_name(dev));
112*4882a593Smuzhiyun 		}
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	return 0;
116*4882a593Smuzhiyun }
117