2 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
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.
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
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.
22 * Send feedback to <lxie@us.ibm.com>
25 #include <linux/pci.h>
26 #include <asm/pci-bridge.h>
27 #include "../pci.h" /* for pci_add_new_bus */
31 struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
33 struct pci_dev *retval_dev = NULL, *dev = NULL;
34 char bus_id[BUS_ID_SIZE];
36 sprintf(bus_id, "%04x:%02x:%02x.%d",dn->phb->global_number,
37 dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn));
39 dbg("Enter rpaphp_find_pci_dev() full_name=%s bus_id=%s\n",
40 dn->full_name, bus_id);
42 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
43 if (!strcmp(pci_name(dev), bus_id)) {
45 dbg("rpaphp_find_pci_dev(): found dev=%p\n\n", dev);
53 EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
55 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
57 struct resource *res = &dev->resource[resource];
58 struct resource *root = pci_find_parent_resource(dev, res);
59 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
64 err = request_resource(root, res);
68 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
69 root ? "Address space collision on" :
70 "No parent found for",
71 resource, dtype, pci_name(dev), res->start, res->end);
76 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
78 static struct pci_dev *rpaphp_find_bridge_pdev(struct slot *slot)
80 return rpaphp_find_pci_dev(slot->dn);
83 static struct pci_dev *rpaphp_find_adapter_pdev(struct slot *slot)
85 return rpaphp_find_pci_dev(slot->dn->child);
88 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
93 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
96 if (rc == NEED_POWER || rc == PWR_ONLY) {
97 dbg("%s: slot must be power up to get sensor-state\n",
100 /* some slots have to be powered up
101 * before get-sensor will succeed.
103 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
106 dbg("%s: power on slot[%s] failed rc=%d.\n",
107 __FUNCTION__, slot->name, rc);
109 rc = rtas_get_sensor(DR_ENTITY_SENSE,
112 } else if (rc == ERR_SENSE_USE)
113 info("%s: slot is unusable\n", __FUNCTION__);
115 err("%s failed to get sensor state\n", __FUNCTION__);
121 * get_pci_adapter_status - get the status of a slot
124 * 1-- adapter is configured
125 * 2-- adapter is not configured
128 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
133 rc = rpaphp_get_sensor_state(slot, &state);
136 if (state == PRESENT) {
138 /* at run-time slot->state can be changed by */
139 /* config/unconfig adapter */
140 *value = slot->state;
142 if (!slot->dn->child)
143 dbg("%s: %s is not valid OFDT node\n",
144 __FUNCTION__, slot->dn->full_name);
145 else if (rpaphp_find_pci_dev(slot->dn->child))
148 dbg("%s: can't find pdev of adapter in slot[%s]\n", __FUNCTION__, slot->name);
149 *value = NOT_CONFIGURED;
152 } else if (state == EMPTY) {
153 dbg("slot is empty\n");
161 /* Must be called before pci_bus_add_devices */
162 static void rpaphp_fixup_new_pci_devices(struct pci_bus *bus)
166 list_for_each_entry(dev, &bus->devices, bus_list) {
168 * Skip already-present devices (which are on the
169 * global device list.)
171 if (list_empty(&dev->global_list)) {
174 pcibios_fixup_device_resources(dev, bus);
175 pci_read_irq_line(dev);
176 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
177 struct resource *r = &dev->resource[i];
178 if (r->parent || !r->start || !r->flags)
180 rpaphp_claim_resource(dev, i);
186 static void rpaphp_pci_config_device(struct pci_bus *pci_bus, struct device_node *dn)
190 num = pci_scan_slot(pci_bus, PCI_DEVFN(PCI_SLOT(dn->devfn), 0));
192 rpaphp_fixup_new_pci_devices(pci_bus);
193 pci_bus_add_devices(pci_bus);
198 static int rpaphp_pci_config_bridge(struct pci_dev *dev, struct device_node *dn);
200 /*****************************************************************************
201 rpaphp_pci_config_dn() will recursively configure all devices under the
202 given slot->dn and return the dn's pci_dev.
203 *****************************************************************************/
204 static struct pci_dev *rpaphp_pci_config_dn(struct device_node *dn, struct pci_bus *bus)
206 struct device_node *local;
209 for (local = dn->child; local; local = local->sibling) {
210 rpaphp_pci_config_device(bus, local);
211 dev = rpaphp_find_pci_dev(local);
212 if (!rpaphp_pci_config_bridge(dev, local))
219 static int rpaphp_pci_config_bridge(struct pci_dev *dev, struct device_node *dn)
221 if (dev && dn->child) { /* dn is a PCI bridge node */
222 struct pci_bus *child;
225 /* get busno of downstream bus */
226 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
228 /* add to children of PCI bridge dev->bus */
229 child = pci_add_new_bus(dev->bus, dev, sec_busno);
231 err("%s: could not add second bus\n", __FUNCTION__);
234 sprintf(child->name, "PCI Bus #%02x", child->number);
235 /* Fixup subordinate bridge bases and resureces */
236 pcibios_fixup_bus(child);
238 /* may need do more stuff here */
239 rpaphp_pci_config_dn(dn, dev->subordinate);
244 static struct pci_dev *rpaphp_config_pci_adapter(struct slot *slot)
246 struct pci_bus *pci_bus;
247 struct pci_dev *dev = NULL;
249 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
253 pci_bus = slot->bridge->subordinate;
255 err("%s: can't find bus structure\n", __FUNCTION__);
259 dev = rpaphp_pci_config_dn(slot->dn, pci_bus);
262 /* slot is not enabled */
263 err("slot doesn't have pci_dev structure\n");
269 dbg("Exit %s: pci_dev %s\n", __FUNCTION__, dev ? "found" : "not found");
273 int rpaphp_unconfig_pci_adapter(struct slot *slot)
277 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
278 if (!slot->dev.pci_dev) {
279 info("%s: no card in slot[%s]\n", __FUNCTION__, slot->name);
284 /* remove the device from the pci core */
285 eeh_remove_device(slot->dev.pci_dev);
286 pci_remove_bus_device(slot->dev.pci_dev);
288 slot->state = NOT_CONFIGURED;
289 info("%s: adapter in slot[%s] unconfigured.\n", __FUNCTION__,
292 dbg("Exit %s, rc=0x%x\n", __FUNCTION__, retval);
296 static int setup_pci_hotplug_slot_info(struct slot *slot)
298 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
300 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
301 rpaphp_get_pci_adapter_status(slot, 1,
302 &slot->hotplug_slot->info->
304 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
305 dbg("%s: NOT_VALID: skip dn->full_name=%s\n",
306 __FUNCTION__, slot->dn->full_name);
312 static int setup_pci_slot(struct slot *slot)
314 slot->bridge = rpaphp_find_bridge_pdev(slot);
315 if (!slot->bridge) { /* slot being added doesn't have pci_dev yet */
316 dbg("%s: no pci_dev for bridge dn %s\n", __FUNCTION__, slot->name);
317 dealloc_slot_struct(slot);
321 /* find slot's pci_dev if it's not empty */
322 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
323 slot->state = EMPTY; /* slot is empty */
324 slot->dev.pci_dev = NULL;
326 /* slot is occupied */
327 if (!(slot->dn->child)) {
328 /* non-empty slot has to have child */
329 err("%s: slot[%s]'s device_node doesn't have child for adapter\n", __FUNCTION__, slot->name);
330 dealloc_slot_struct(slot);
333 slot->dev.pci_dev = rpaphp_find_adapter_pdev(slot);
334 if (slot->dev.pci_dev) {
335 slot->state = CONFIGURED;
338 /* DLPAR add as opposed to
340 slot->state = NOT_CONFIGURED;
346 int register_pci_slot(struct slot *slot)
350 slot->dev_type = PCI_DEV;
351 if (setup_pci_hotplug_slot_info(slot))
353 if (setup_pci_slot(slot))
355 rc = register_slot(slot);
358 dealloc_slot_struct(slot);
362 int rpaphp_enable_pci_slot(struct slot *slot)
364 int retval = 0, state;
366 retval = rpaphp_get_sensor_state(slot, &state);
369 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
370 /* if slot is not empty, enable the adapter */
371 if (state == PRESENT) {
372 dbg("%s : slot[%s] is occupid.\n", __FUNCTION__, slot->name);
373 if ((slot->dev.pci_dev =
374 rpaphp_config_pci_adapter(slot)) != NULL) {
375 slot->state = CONFIGURED;
376 dbg("%s: PCI adapter %s in slot[%s] has been configured\n",
377 __FUNCTION__, pci_name(slot->dev.pci_dev), slot->name);
379 slot->state = NOT_CONFIGURED;
380 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
381 __FUNCTION__, slot->name);
383 } else if (state == EMPTY) {
384 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
387 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
389 slot->state = NOT_VALID;
393 if (slot->state != NOT_VALID)
394 rpaphp_set_attention_status(slot, LED_ON);
396 rpaphp_set_attention_status(slot, LED_ID);
397 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);