blob: a669ba3f29c9fc9367257463ab11637e59df899b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Send feedback to <lxie@us.ibm.com>
23 *
24 */
25#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080026#include <linux/string.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/pci-bridge.h>
29#include <asm/rtas.h>
30#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Tim Schmielau4e57b682005-10-30 15:03:48 -080032#include "../pci.h" /* for pci_add_new_bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "rpaphp.h"
34
linas@austin.ibm.come06b80b2006-01-12 18:28:22 -060035int rpaphp_get_sensor_state(struct slot *slot, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int rc;
38 int setlevel;
39
40 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
41
42 if (rc < 0) {
43 if (rc == -EFAULT || rc == -EEXIST) {
44 dbg("%s: slot must be power up to get sensor-state\n",
45 __FUNCTION__);
46
47 /* some slots have to be powered up
48 * before get-sensor will succeed.
49 */
50 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
51 &setlevel);
52 if (rc < 0) {
53 dbg("%s: power on slot[%s] failed rc=%d.\n",
54 __FUNCTION__, slot->name, rc);
55 } else {
56 rc = rtas_get_sensor(DR_ENTITY_SENSE,
57 slot->index, state);
58 }
59 } else if (rc == -ENODEV)
60 info("%s: slot is unusable\n", __FUNCTION__);
61 else
62 err("%s failed to get sensor state\n", __FUNCTION__);
63 }
64 return rc;
65}
66
67/**
68 * get_pci_adapter_status - get the status of a slot
69 *
70 * 0-- slot is empty
71 * 1-- adapter is configured
72 * 2-- adapter is not configured
73 * 3-- not valid
74 */
75int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
76{
John Rose0945cd52005-07-25 10:16:53 -050077 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int state, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 *value = NOT_VALID;
81 rc = rpaphp_get_sensor_state(slot, &state);
82 if (rc)
83 goto exit;
84
John Rose56d84562005-07-25 10:17:03 -050085 if (state == EMPTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *value = EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 else if (state == PRESENT) {
88 if (!is_init) {
89 /* at run-time slot->state can be changed by */
90 /* config/unconfig adapter */
91 *value = slot->state;
92 } else {
linas@austin.ibm.com01657862006-01-12 18:18:26 -060093 bus = pcibios_find_pci_bus(slot->dn);
John Rose0945cd52005-07-25 10:16:53 -050094 if (bus && !list_empty(&bus->devices))
95 *value = CONFIGURED;
96 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *value = NOT_CONFIGURED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 }
100exit:
101 return rc;
102}
103
John Rose940903c2005-07-25 10:16:58 -0500104static void print_slot_pci_funcs(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
John Rose940903c2005-07-25 10:16:58 -0500106 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct pci_dev *dev;
108
John Rose940903c2005-07-25 10:16:58 -0500109 dn = pci_bus_to_OF_node(bus);
110 if (!dn)
111 return;
112
113 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
114 list_for_each_entry (dev, &bus->devices, bus_list)
John Rose5eeb8c62005-07-25 10:16:42 -0500115 dbg("\t%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int setup_pci_hotplug_slot_info(struct slot *slot)
120{
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100121 struct hotplug_slot_info *hotplug_slot_info = slot->hotplug_slot->info;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
124 __FUNCTION__);
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100125 rpaphp_get_power_status(slot, &hotplug_slot_info->power_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 rpaphp_get_pci_adapter_status(slot, 1,
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100127 &hotplug_slot_info->adapter_status);
128 if (hotplug_slot_info->adapter_status == NOT_VALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 err("%s: NOT_VALID: skip dn->full_name=%s\n",
130 __FUNCTION__, slot->dn->full_name);
131 return -EINVAL;
132 }
133 return 0;
134}
135
John Rose9c209c92005-07-25 11:13:38 -0500136static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
John Rose9c209c92005-07-25 11:13:38 -0500138 struct pci_bus *bus = slot->bus;
139 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
John Rose9c209c92005-07-25 11:13:38 -0500141 bridge = bus->self;
142 if (bridge)
143 strcpy(slot->name, pci_name(bridge));
144 else
145 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
146 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static int setup_pci_slot(struct slot *slot)
150{
John Rose9c209c92005-07-25 11:13:38 -0500151 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
John Rose9c209c92005-07-25 11:13:38 -0500154 BUG_ON(!dn);
linas@austin.ibm.com01657862006-01-12 18:18:26 -0600155 bus = pcibios_find_pci_bus(dn);
John Rose9c209c92005-07-25 11:13:38 -0500156 if (!bus) {
157 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
158 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
John Rose9c209c92005-07-25 11:13:38 -0500161 slot->bus = bus;
162 slot->pci_devs = &bus->devices;
163 set_slot_name(slot);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* find slot's pci_dev if it's not empty */
166 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
167 slot->state = EMPTY; /* slot is empty */
168 } else {
169 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500170 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* non-empty slot has to have child */
172 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
173 __FUNCTION__, slot->name);
174 goto exit_rc;
175 }
176
177 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
178 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
179 __FUNCTION__, slot->name);
linas@austin.ibm.com7fec77e2006-01-12 18:22:07 -0600180 pcibios_add_pci_devices(slot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
183 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
184 __FUNCTION__, slot->name);
185 goto exit_rc;
186 }
John Rose940903c2005-07-25 10:16:58 -0500187 print_slot_pci_funcs(slot->bus);
John Rose5eeb8c62005-07-25 10:16:42 -0500188 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 slot->state = CONFIGURED;
190 } else {
191 /* DLPAR add as opposed to
192 * boot time */
193 slot->state = NOT_CONFIGURED;
194 }
195 }
196 return 0;
197exit_rc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
199}
200
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -0600201int rpaphp_register_pci_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int rc = -EINVAL;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (setup_pci_hotplug_slot_info(slot))
206 goto exit_rc;
207 if (setup_pci_slot(slot))
208 goto exit_rc;
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -0600209 rc = rpaphp_register_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210exit_rc:
211 return rc;
212}
213