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>
28 #include <asm/machdep.h>
29 #include "../pci.h" /* for pci_add_new_bus */
33 static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
34 struct device_node *dn)
36 struct pci_bus *child = NULL;
37 struct list_head *tmp;
38 struct device_node *busdn;
40 busdn = pci_bus_to_OF_node(bus);
44 list_for_each(tmp, &bus->children) {
45 child = find_bus_among_children(pci_bus_b(tmp), dn);
52 static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
54 if (!dn->phb || !dn->phb->bus)
57 return find_bus_among_children(dn->phb->bus, dn);
60 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
62 struct resource *res = &dev->resource[resource];
63 struct resource *root = pci_find_parent_resource(dev, res);
64 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
68 err = request_resource(root, res);
72 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
73 root ? "Address space collision on" :
74 "No parent found for",
75 resource, dtype, pci_name(dev), res->start, res->end);
80 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
82 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
87 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
90 if (rc == -EFAULT || rc == -EEXIST) {
91 dbg("%s: slot must be power up to get sensor-state\n",
94 /* some slots have to be powered up
95 * before get-sensor will succeed.
97 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
100 dbg("%s: power on slot[%s] failed rc=%d.\n",
101 __FUNCTION__, slot->name, rc);
103 rc = rtas_get_sensor(DR_ENTITY_SENSE,
106 } else if (rc == -ENODEV)
107 info("%s: slot is unusable\n", __FUNCTION__);
109 err("%s failed to get sensor state\n", __FUNCTION__);
115 * get_pci_adapter_status - get the status of a slot
118 * 1-- adapter is configured
119 * 2-- adapter is not configured
122 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
128 rc = rpaphp_get_sensor_state(slot, &state);
132 if ((state == EMPTY) || (slot->type == PHB)) {
133 dbg("slot is empty\n");
136 else if (state == PRESENT) {
138 /* at run-time slot->state can be changed by */
139 /* config/unconfig adapter */
140 *value = slot->state;
142 bus = rpaphp_find_pci_bus(slot->dn);
143 if (bus && !list_empty(&bus->devices))
146 *value = NOT_CONFIGURED;
153 /* Must be called before pci_bus_add_devices */
155 rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
159 list_for_each_entry(dev, &bus->devices, bus_list) {
161 * Skip already-present devices (which are on the
162 * global device list.)
164 if (list_empty(&dev->global_list)) {
167 /* Need to setup IOMMU tables */
168 ppc_md.iommu_dev_setup(dev);
171 pcibios_fixup_device_resources(dev, bus);
172 pci_read_irq_line(dev);
173 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
174 struct resource *r = &dev->resource[i];
176 if (r->parent || !r->start || !r->flags)
178 rpaphp_claim_resource(dev, i);
184 static int rpaphp_pci_config_bridge(struct pci_dev *dev)
187 struct pci_bus *child_bus;
188 struct pci_dev *child_dev;
190 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
192 /* get busno of downstream bus */
193 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
195 /* add to children of PCI bridge dev->bus */
196 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
198 err("%s: could not add second bus\n", __FUNCTION__);
201 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
202 /* do pci_scan_child_bus */
203 pci_scan_child_bus(child_bus);
205 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
206 eeh_add_device_late(child_dev);
209 /* fixup new pci devices without touching bus struct */
210 rpaphp_fixup_new_pci_devices(child_bus, 0);
212 /* Make the discovered devices available */
213 pci_bus_add_devices(child_bus);
217 /*****************************************************************************
218 rpaphp_pci_config_slot() will configure all devices under the
219 given slot->dn and return the the first pci_dev.
220 *****************************************************************************/
221 static struct pci_dev *
222 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
224 struct pci_dev *dev = NULL;
228 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
232 slotno = PCI_SLOT(dn->child->devfn);
234 /* pci_scan_slot should find all children */
235 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
237 rpaphp_fixup_new_pci_devices(bus, 1);
238 pci_bus_add_devices(bus);
240 if (list_empty(&bus->devices)) {
241 err("%s: No new device found\n", __FUNCTION__);
244 list_for_each_entry(dev, &bus->devices, bus_list) {
245 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
246 rpaphp_pci_config_bridge(dev);
252 static void enable_eeh(struct device_node *dn)
254 struct device_node *sib;
256 for (sib = dn->child; sib; sib = sib->sibling)
258 eeh_add_device_early(dn);
263 static void print_slot_pci_funcs(struct slot *slot)
267 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, slot->name);
268 list_for_each_entry (dev, slot->pci_devs, bus_list)
269 dbg("\t%s\n", pci_name(dev));
273 static int rpaphp_config_pci_adapter(struct slot *slot)
278 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
280 enable_eeh(slot->dn);
281 dev = rpaphp_pci_config_slot(slot->dn, slot->bus);
283 err("%s: can't find any devices.\n", __FUNCTION__);
286 print_slot_pci_funcs(slot);
289 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
293 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
295 eeh_remove_device(dev);
296 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
297 struct pci_bus *bus = dev->subordinate;
298 struct list_head *ln;
301 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
302 struct pci_dev *pdev = pci_dev_b(ln);
304 rpaphp_eeh_remove_bus_device(pdev);
311 int rpaphp_unconfig_pci_adapter(struct slot *slot)
313 struct pci_dev *dev, *tmp;
316 list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
317 rpaphp_eeh_remove_bus_device(dev);
318 pci_remove_bus_device(dev);
321 slot->state = NOT_CONFIGURED;
322 info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
327 static int setup_pci_hotplug_slot_info(struct slot *slot)
329 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
331 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
332 rpaphp_get_pci_adapter_status(slot, 1,
333 &slot->hotplug_slot->info->
335 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
336 err("%s: NOT_VALID: skip dn->full_name=%s\n",
337 __FUNCTION__, slot->dn->full_name);
343 static void set_slot_name(struct slot *slot)
345 struct pci_bus *bus = slot->bus;
346 struct pci_dev *bridge;
350 strcpy(slot->name, pci_name(bridge));
352 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
356 static int setup_pci_slot(struct slot *slot)
358 struct device_node *dn = slot->dn;
362 bus = rpaphp_find_pci_bus(dn);
364 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
369 slot->pci_devs = &bus->devices;
372 /* find slot's pci_dev if it's not empty */
373 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
374 slot->state = EMPTY; /* slot is empty */
376 /* slot is occupied */
378 /* non-empty slot has to have child */
379 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
380 __FUNCTION__, slot->name);
384 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
385 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
386 __FUNCTION__, slot->name);
387 if (rpaphp_config_pci_adapter(slot)) {
388 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
392 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
393 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
394 __FUNCTION__, slot->name);
397 print_slot_pci_funcs(slot);
398 if (!list_empty(slot->pci_devs)) {
399 slot->state = CONFIGURED;
401 /* DLPAR add as opposed to
403 slot->state = NOT_CONFIGURED;
408 dealloc_slot_struct(slot);
412 int register_pci_slot(struct slot *slot)
416 if ((slot->type == EMBEDDED) || (slot->type == PHB))
420 if (setup_pci_hotplug_slot_info(slot))
422 if (setup_pci_slot(slot))
424 rc = register_slot(slot);
429 int rpaphp_enable_pci_slot(struct slot *slot)
431 int retval = 0, state;
433 retval = rpaphp_get_sensor_state(slot, &state);
436 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
437 /* if slot is not empty, enable the adapter */
438 if (state == PRESENT) {
439 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
440 retval = rpaphp_config_pci_adapter(slot);
442 slot->state = CONFIGURED;
443 dbg("%s: PCI devices in slot[%s] has been configured\n",
444 __FUNCTION__, slot->name);
446 slot->state = NOT_CONFIGURED;
447 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
448 __FUNCTION__, slot->name);
450 } else if (state == EMPTY) {
451 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
454 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
456 slot->state = NOT_VALID;
460 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);