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 struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
35 struct pci_dev *dev = NULL;
36 char bus_id[BUS_ID_SIZE];
38 sprintf(bus_id, "%04x:%02x:%02x.%d", dn->phb->global_number,
39 dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn));
40 for_each_pci_dev(dev) {
41 if (!strcmp(pci_name(dev), bus_id)) {
48 struct pci_bus *find_bus_among_children(struct pci_bus *bus,
49 struct device_node *dn)
51 struct pci_bus *child = NULL;
52 struct list_head *tmp;
53 struct device_node *busdn;
55 busdn = pci_bus_to_OF_node(bus);
59 list_for_each(tmp, &bus->children) {
60 child = find_bus_among_children(pci_bus_b(tmp), dn);
67 struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
69 BUG_ON(!dn->phb || !dn->phb->bus);
71 return find_bus_among_children(dn->phb->bus, dn);
74 EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
76 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
78 struct resource *res = &dev->resource[resource];
79 struct resource *root = pci_find_parent_resource(dev, res);
80 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
84 err = request_resource(root, res);
88 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
89 root ? "Address space collision on" :
90 "No parent found for",
91 resource, dtype, pci_name(dev), res->start, res->end);
96 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
98 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
103 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
106 if (rc == -EFAULT || rc == -EEXIST) {
107 dbg("%s: slot must be power up to get sensor-state\n",
110 /* some slots have to be powered up
111 * before get-sensor will succeed.
113 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
116 dbg("%s: power on slot[%s] failed rc=%d.\n",
117 __FUNCTION__, slot->name, rc);
119 rc = rtas_get_sensor(DR_ENTITY_SENSE,
122 } else if (rc == -ENODEV)
123 info("%s: slot is unusable\n", __FUNCTION__);
125 err("%s failed to get sensor state\n", __FUNCTION__);
131 * get_pci_adapter_status - get the status of a slot
134 * 1-- adapter is configured
135 * 2-- adapter is not configured
138 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
141 struct device_node *child_dn;
142 struct pci_dev *child_dev = NULL;
145 rc = rpaphp_get_sensor_state(slot, &state);
149 if ((state == EMPTY) || (slot->type == PHB)) {
150 dbg("slot is empty\n");
153 else if (state == PRESENT) {
155 /* at run-time slot->state can be changed by */
156 /* config/unconfig adapter */
157 *value = slot->state;
159 child_dn = slot->dn->child;
161 child_dev = rpaphp_find_pci_dev(child_dn);
166 dbg("%s: %s is not valid OFDT node\n",
167 __FUNCTION__, slot->dn->full_name);
169 err("%s: can't find pdev of adapter in slot[%s]\n",
170 __FUNCTION__, slot->dn->full_name);
171 *value = NOT_CONFIGURED;
179 /* Must be called before pci_bus_add_devices */
181 rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
185 list_for_each_entry(dev, &bus->devices, bus_list) {
187 * Skip already-present devices (which are on the
188 * global device list.)
190 if (list_empty(&dev->global_list)) {
193 /* Need to setup IOMMU tables */
194 ppc_md.iommu_dev_setup(dev);
197 pcibios_fixup_device_resources(dev, bus);
198 pci_read_irq_line(dev);
199 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
200 struct resource *r = &dev->resource[i];
202 if (r->parent || !r->start || !r->flags)
204 rpaphp_claim_resource(dev, i);
210 static int rpaphp_pci_config_bridge(struct pci_dev *dev)
213 struct pci_bus *child_bus;
214 struct pci_dev *child_dev;
216 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
218 /* get busno of downstream bus */
219 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
221 /* add to children of PCI bridge dev->bus */
222 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
224 err("%s: could not add second bus\n", __FUNCTION__);
227 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
228 /* do pci_scan_child_bus */
229 pci_scan_child_bus(child_bus);
231 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
232 eeh_add_device_late(child_dev);
235 /* fixup new pci devices without touching bus struct */
236 rpaphp_fixup_new_pci_devices(child_bus, 0);
238 /* Make the discovered devices available */
239 pci_bus_add_devices(child_bus);
243 /*****************************************************************************
244 rpaphp_pci_config_slot() will configure all devices under the
245 given slot->dn and return the the first pci_dev.
246 *****************************************************************************/
247 static struct pci_dev *
248 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
250 struct pci_dev *dev = NULL;
254 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
257 slotno = PCI_SLOT(dn->child->devfn);
259 /* pci_scan_slot should find all children */
260 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
262 rpaphp_fixup_new_pci_devices(bus, 1);
263 pci_bus_add_devices(bus);
265 dev = rpaphp_find_pci_dev(dn->child);
267 err("No new device found\n");
270 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
271 rpaphp_pci_config_bridge(dev);
276 static void enable_eeh(struct device_node *dn)
278 struct device_node *sib;
280 for (sib = dn->child; sib; sib = sib->sibling)
282 eeh_add_device_early(dn);
287 static void print_slot_pci_funcs(struct slot *slot)
291 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, slot->name);
292 list_for_each_entry (dev, slot->pci_devs, bus_list)
293 dbg("\t%s\n", pci_name(dev));
297 static int rpaphp_config_pci_adapter(struct slot *slot)
302 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
304 enable_eeh(slot->dn);
305 dev = rpaphp_pci_config_slot(slot->dn, slot->bus);
307 err("%s: can't find any devices.\n", __FUNCTION__);
310 print_slot_pci_funcs(slot);
313 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
317 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
319 eeh_remove_device(dev);
320 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
321 struct pci_bus *bus = dev->subordinate;
322 struct list_head *ln;
325 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
326 struct pci_dev *pdev = pci_dev_b(ln);
328 rpaphp_eeh_remove_bus_device(pdev);
335 int rpaphp_unconfig_pci_adapter(struct slot *slot)
337 struct pci_dev *dev, *tmp;
340 list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
341 rpaphp_eeh_remove_bus_device(dev);
342 pci_remove_bus_device(dev);
345 slot->state = NOT_CONFIGURED;
346 info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
351 static int setup_pci_hotplug_slot_info(struct slot *slot)
353 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
355 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
356 rpaphp_get_pci_adapter_status(slot, 1,
357 &slot->hotplug_slot->info->
359 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
360 err("%s: NOT_VALID: skip dn->full_name=%s\n",
361 __FUNCTION__, slot->dn->full_name);
367 static void set_slot_name(struct slot *slot)
369 struct pci_bus *bus = slot->bus;
370 struct pci_dev *bridge;
374 strcpy(slot->name, pci_name(bridge));
376 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
380 static int setup_pci_slot(struct slot *slot)
382 struct device_node *dn = slot->dn;
386 bus = rpaphp_find_pci_bus(dn);
388 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
393 slot->pci_devs = &bus->devices;
396 /* find slot's pci_dev if it's not empty */
397 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
398 slot->state = EMPTY; /* slot is empty */
400 /* slot is occupied */
402 /* non-empty slot has to have child */
403 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
404 __FUNCTION__, slot->name);
408 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
409 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
410 __FUNCTION__, slot->name);
411 if (rpaphp_config_pci_adapter(slot)) {
412 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
416 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
417 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
418 __FUNCTION__, slot->name);
421 print_slot_pci_funcs(slot);
422 if (!list_empty(slot->pci_devs)) {
423 slot->state = CONFIGURED;
425 /* DLPAR add as opposed to
427 slot->state = NOT_CONFIGURED;
432 dealloc_slot_struct(slot);
436 int register_pci_slot(struct slot *slot)
440 if ((slot->type == EMBEDDED) || (slot->type == PHB))
444 if (setup_pci_hotplug_slot_info(slot))
446 if (setup_pci_slot(slot))
448 rc = register_slot(slot);
453 int rpaphp_enable_pci_slot(struct slot *slot)
455 int retval = 0, state;
457 retval = rpaphp_get_sensor_state(slot, &state);
460 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
461 /* if slot is not empty, enable the adapter */
462 if (state == PRESENT) {
463 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
464 retval = rpaphp_config_pci_adapter(slot);
466 slot->state = CONFIGURED;
467 dbg("%s: PCI devices in slot[%s] has been configured\n",
468 __FUNCTION__, slot->name);
470 slot->state = NOT_CONFIGURED;
471 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
472 __FUNCTION__, slot->name);
474 } else if (state == EMPTY) {
475 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
478 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
480 slot->state = NOT_VALID;
484 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);