blob: 30a892ca4b3721d756d5a81cafdfd240b0316c4c [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>
26#include <asm/pci-bridge.h>
27#include <asm/rtas.h>
28#include <asm/machdep.h>
29#include "../pci.h" /* for pci_add_new_bus */
30
31#include "rpaphp.h"
32
John Rose0945cd52005-07-25 10:16:53 -050033static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
John Rose9c209c92005-07-25 11:13:38 -050034 struct device_node *dn)
35{
36 struct pci_bus *child = NULL;
37 struct list_head *tmp;
38 struct device_node *busdn;
39
40 busdn = pci_bus_to_OF_node(bus);
41 if (busdn == dn)
42 return bus;
43
44 list_for_each(tmp, &bus->children) {
45 child = find_bus_among_children(pci_bus_b(tmp), dn);
46 if (child)
47 break;
48 }
49 return child;
50}
51
John Rose0945cd52005-07-25 10:16:53 -050052static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
John Rose9c209c92005-07-25 11:13:38 -050053{
John Rose0945cd52005-07-25 10:16:53 -050054 if (!dn->phb || !dn->phb->bus)
55 return NULL;
John Rose9c209c92005-07-25 11:13:38 -050056
57 return find_bus_among_children(dn->phb->bus, dn);
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int rpaphp_claim_resource(struct pci_dev *dev, int resource)
61{
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";
65 int err = -EINVAL;
66
67 if (root != NULL) {
68 err = request_resource(root, res);
69 }
70
71 if (err) {
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);
76 }
77 return err;
78}
79
80EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int rpaphp_get_sensor_state(struct slot *slot, int *state)
83{
84 int rc;
85 int setlevel;
86
87 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
88
89 if (rc < 0) {
90 if (rc == -EFAULT || rc == -EEXIST) {
91 dbg("%s: slot must be power up to get sensor-state\n",
92 __FUNCTION__);
93
94 /* some slots have to be powered up
95 * before get-sensor will succeed.
96 */
97 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
98 &setlevel);
99 if (rc < 0) {
100 dbg("%s: power on slot[%s] failed rc=%d.\n",
101 __FUNCTION__, slot->name, rc);
102 } else {
103 rc = rtas_get_sensor(DR_ENTITY_SENSE,
104 slot->index, state);
105 }
106 } else if (rc == -ENODEV)
107 info("%s: slot is unusable\n", __FUNCTION__);
108 else
109 err("%s failed to get sensor state\n", __FUNCTION__);
110 }
111 return rc;
112}
113
114/**
115 * get_pci_adapter_status - get the status of a slot
116 *
117 * 0-- slot is empty
118 * 1-- adapter is configured
119 * 2-- adapter is not configured
120 * 3-- not valid
121 */
122int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
123{
John Rose0945cd52005-07-25 10:16:53 -0500124 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int state, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 *value = NOT_VALID;
128 rc = rpaphp_get_sensor_state(slot, &state);
129 if (rc)
130 goto exit;
131
132 if ((state == EMPTY) || (slot->type == PHB)) {
133 dbg("slot is empty\n");
134 *value = EMPTY;
135 }
136 else if (state == PRESENT) {
137 if (!is_init) {
138 /* at run-time slot->state can be changed by */
139 /* config/unconfig adapter */
140 *value = slot->state;
141 } else {
John Rose0945cd52005-07-25 10:16:53 -0500142 bus = rpaphp_find_pci_bus(slot->dn);
143 if (bus && !list_empty(&bus->devices))
144 *value = CONFIGURED;
145 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *value = NOT_CONFIGURED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 }
149exit:
150 return rc;
151}
152
153/* Must be called before pci_bus_add_devices */
154static void
155rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
156{
157 struct pci_dev *dev;
158
159 list_for_each_entry(dev, &bus->devices, bus_list) {
160 /*
161 * Skip already-present devices (which are on the
162 * global device list.)
163 */
164 if (list_empty(&dev->global_list)) {
165 int i;
166
167 /* Need to setup IOMMU tables */
168 ppc_md.iommu_dev_setup(dev);
169
170 if(fix_bus)
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];
175
176 if (r->parent || !r->start || !r->flags)
177 continue;
178 rpaphp_claim_resource(dev, i);
179 }
180 }
181 }
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int rpaphp_pci_config_bridge(struct pci_dev *dev)
185{
186 u8 sec_busno;
187 struct pci_bus *child_bus;
188 struct pci_dev *child_dev;
189
190 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
191
192 /* get busno of downstream bus */
193 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
194
195 /* add to children of PCI bridge dev->bus */
196 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
197 if (!child_bus) {
198 err("%s: could not add second bus\n", __FUNCTION__);
199 return -EIO;
200 }
201 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
202 /* do pci_scan_child_bus */
203 pci_scan_child_bus(child_bus);
204
205 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
206 eeh_add_device_late(child_dev);
207 }
208
209 /* fixup new pci devices without touching bus struct */
210 rpaphp_fixup_new_pci_devices(child_bus, 0);
211
212 /* Make the discovered devices available */
213 pci_bus_add_devices(child_bus);
214 return 0;
215}
216
John Rosebde16842005-07-25 10:16:37 -0500217/*****************************************************************************
218 rpaphp_pci_config_slot() will configure all devices under the
219 given slot->dn and return the the first pci_dev.
220 *****************************************************************************/
221static struct pci_dev *
222rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
223{
John Rosebde16842005-07-25 10:16:37 -0500224 struct pci_dev *dev = NULL;
John Rose9c209c92005-07-25 11:13:38 -0500225 int slotno;
John Rosebde16842005-07-25 10:16:37 -0500226 int num;
227
228 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
John Rose0945cd52005-07-25 10:16:53 -0500229 if (!dn->child)
230 return NULL;
John Rosebde16842005-07-25 10:16:37 -0500231
John Rose0945cd52005-07-25 10:16:53 -0500232 slotno = PCI_SLOT(dn->child->devfn);
John Rose9c209c92005-07-25 11:13:38 -0500233
John Rose0945cd52005-07-25 10:16:53 -0500234 /* pci_scan_slot should find all children */
235 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
236 if (num) {
237 rpaphp_fixup_new_pci_devices(bus, 1);
238 pci_bus_add_devices(bus);
239 }
240 if (list_empty(&bus->devices)) {
241 err("%s: No new device found\n", __FUNCTION__);
242 return NULL;
243 }
244 list_for_each_entry(dev, &bus->devices, bus_list) {
John Rosebde16842005-07-25 10:16:37 -0500245 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
246 rpaphp_pci_config_bridge(dev);
247 }
John Rose0945cd52005-07-25 10:16:53 -0500248
John Rosebde16842005-07-25 10:16:37 -0500249 return dev;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static void enable_eeh(struct device_node *dn)
253{
254 struct device_node *sib;
255
256 for (sib = dn->child; sib; sib = sib->sibling)
257 enable_eeh(sib);
258 eeh_add_device_early(dn);
259 return;
260
261}
262
263static void print_slot_pci_funcs(struct slot *slot)
264{
265 struct pci_dev *dev;
266
John Rose5eeb8c62005-07-25 10:16:42 -0500267 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return;
271}
272
273static int rpaphp_config_pci_adapter(struct slot *slot)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct pci_dev *dev;
276 int rc = -ENODEV;
277
278 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
279
John Rose9c209c92005-07-25 11:13:38 -0500280 enable_eeh(slot->dn);
281 dev = rpaphp_pci_config_slot(slot->dn, slot->bus);
282 if (!dev) {
283 err("%s: can't find any devices.\n", __FUNCTION__);
284 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
John Rose9c209c92005-07-25 11:13:38 -0500286 print_slot_pci_funcs(slot);
287 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288exit:
289 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
290 return rc;
291}
292
293static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
294{
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;
299 if (!bus)
300 return;
301 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
302 struct pci_dev *pdev = pci_dev_b(ln);
303 if (pdev)
304 rpaphp_eeh_remove_bus_device(pdev);
305 }
306
307 }
308 return;
309}
310
311int rpaphp_unconfig_pci_adapter(struct slot *slot)
312{
John Rose9c209c92005-07-25 11:13:38 -0500313 struct pci_dev *dev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int retval = 0;
315
John Rose9c209c92005-07-25 11:13:38 -0500316 list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 rpaphp_eeh_remove_bus_device(dev);
John Rose9c209c92005-07-25 11:13:38 -0500318 pci_remove_bus_device(dev);
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 slot->state = NOT_CONFIGURED;
322 info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
323 slot->name);
324 return retval;
325}
326
327static int setup_pci_hotplug_slot_info(struct slot *slot)
328{
329 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
330 __FUNCTION__);
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->
334 adapter_status);
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);
338 return -EINVAL;
339 }
340 return 0;
341}
342
John Rose9c209c92005-07-25 11:13:38 -0500343static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
John Rose9c209c92005-07-25 11:13:38 -0500345 struct pci_bus *bus = slot->bus;
346 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
John Rose9c209c92005-07-25 11:13:38 -0500348 bridge = bus->self;
349 if (bridge)
350 strcpy(slot->name, pci_name(bridge));
351 else
352 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
353 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static int setup_pci_slot(struct slot *slot)
357{
John Rose9c209c92005-07-25 11:13:38 -0500358 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
John Rose9c209c92005-07-25 11:13:38 -0500361 BUG_ON(!dn);
362 bus = rpaphp_find_pci_bus(dn);
363 if (!bus) {
364 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
365 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
John Rose9c209c92005-07-25 11:13:38 -0500368 slot->bus = bus;
369 slot->pci_devs = &bus->devices;
370 set_slot_name(slot);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* 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 */
375 } else {
376 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500377 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* 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);
381 goto exit_rc;
382 }
383
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__);
389 goto exit_rc;
390 }
391
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);
395 goto exit_rc;
396 }
397 print_slot_pci_funcs(slot);
John Rose5eeb8c62005-07-25 10:16:42 -0500398 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 slot->state = CONFIGURED;
400 } else {
401 /* DLPAR add as opposed to
402 * boot time */
403 slot->state = NOT_CONFIGURED;
404 }
405 }
406 return 0;
407exit_rc:
408 dealloc_slot_struct(slot);
409 return -EINVAL;
410}
411
412int register_pci_slot(struct slot *slot)
413{
414 int rc = -EINVAL;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if ((slot->type == EMBEDDED) || (slot->type == PHB))
417 slot->removable = 0;
418 else
419 slot->removable = 1;
420 if (setup_pci_hotplug_slot_info(slot))
421 goto exit_rc;
422 if (setup_pci_slot(slot))
423 goto exit_rc;
424 rc = register_slot(slot);
425exit_rc:
426 return rc;
427}
428
429int rpaphp_enable_pci_slot(struct slot *slot)
430{
431 int retval = 0, state;
432
433 retval = rpaphp_get_sensor_state(slot, &state);
434 if (retval)
435 goto exit;
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);
441 if (!retval) {
442 slot->state = CONFIGURED;
443 dbg("%s: PCI devices in slot[%s] has been configured\n",
444 __FUNCTION__, slot->name);
445 } else {
446 slot->state = NOT_CONFIGURED;
447 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
448 __FUNCTION__, slot->name);
449 }
450 } else if (state == EMPTY) {
451 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
452 slot->state = EMPTY;
453 } else {
454 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
455 slot->name);
456 slot->state = NOT_VALID;
457 retval = -EINVAL;
458 }
459exit:
460 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
461 return retval;
462}