blob: fc2368710cf711a98bd45c49b6a1d7df25c016e5 [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
John Rose0945cd52005-07-25 10:16:53 -050035static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
John Rose9c209c92005-07-25 11:13:38 -050036 struct device_node *dn)
37{
38 struct pci_bus *child = NULL;
39 struct list_head *tmp;
40 struct device_node *busdn;
41
42 busdn = pci_bus_to_OF_node(bus);
43 if (busdn == dn)
44 return bus;
45
46 list_for_each(tmp, &bus->children) {
47 child = find_bus_among_children(pci_bus_b(tmp), dn);
48 if (child)
49 break;
50 }
51 return child;
52}
53
John Rose56d84562005-07-25 10:17:03 -050054struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
John Rose9c209c92005-07-25 11:13:38 -050055{
Paul Mackerras16353172005-09-06 13:17:54 +100056 struct pci_dn *pdn = dn->data;
57
58 if (!pdn || !pdn->phb || !pdn->phb->bus)
John Rose0945cd52005-07-25 10:16:53 -050059 return NULL;
John Rose9c209c92005-07-25 11:13:38 -050060
Paul Mackerras16353172005-09-06 13:17:54 +100061 return find_bus_among_children(pdn->phb->bus, dn);
John Rose9c209c92005-07-25 11:13:38 -050062}
John Rose56d84562005-07-25 10:17:03 -050063EXPORT_SYMBOL_GPL(rpaphp_find_pci_bus);
John Rose9c209c92005-07-25 11:13:38 -050064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int rpaphp_claim_resource(struct pci_dev *dev, int resource)
66{
67 struct resource *res = &dev->resource[resource];
68 struct resource *root = pci_find_parent_resource(dev, res);
69 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
70 int err = -EINVAL;
71
72 if (root != NULL) {
73 err = request_resource(root, res);
74 }
75
76 if (err) {
77 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
78 root ? "Address space collision on" :
79 "No parent found for",
80 resource, dtype, pci_name(dev), res->start, res->end);
81 }
82 return err;
83}
84
85EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static int rpaphp_get_sensor_state(struct slot *slot, int *state)
88{
89 int rc;
90 int setlevel;
91
92 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
93
94 if (rc < 0) {
95 if (rc == -EFAULT || rc == -EEXIST) {
96 dbg("%s: slot must be power up to get sensor-state\n",
97 __FUNCTION__);
98
99 /* some slots have to be powered up
100 * before get-sensor will succeed.
101 */
102 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
103 &setlevel);
104 if (rc < 0) {
105 dbg("%s: power on slot[%s] failed rc=%d.\n",
106 __FUNCTION__, slot->name, rc);
107 } else {
108 rc = rtas_get_sensor(DR_ENTITY_SENSE,
109 slot->index, state);
110 }
111 } else if (rc == -ENODEV)
112 info("%s: slot is unusable\n", __FUNCTION__);
113 else
114 err("%s failed to get sensor state\n", __FUNCTION__);
115 }
116 return rc;
117}
118
119/**
120 * get_pci_adapter_status - get the status of a slot
121 *
122 * 0-- slot is empty
123 * 1-- adapter is configured
124 * 2-- adapter is not configured
125 * 3-- not valid
126 */
127int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
128{
John Rose0945cd52005-07-25 10:16:53 -0500129 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int state, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 *value = NOT_VALID;
133 rc = rpaphp_get_sensor_state(slot, &state);
134 if (rc)
135 goto exit;
136
John Rose56d84562005-07-25 10:17:03 -0500137 if (state == EMPTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *value = EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 else if (state == PRESENT) {
140 if (!is_init) {
141 /* at run-time slot->state can be changed by */
142 /* config/unconfig adapter */
143 *value = slot->state;
144 } else {
John Rose0945cd52005-07-25 10:16:53 -0500145 bus = rpaphp_find_pci_bus(slot->dn);
146 if (bus && !list_empty(&bus->devices))
147 *value = CONFIGURED;
148 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *value = NOT_CONFIGURED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 }
152exit:
153 return rc;
154}
155
156/* Must be called before pci_bus_add_devices */
John Rose5fa80fc2005-11-04 15:38:50 -0600157void rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct pci_dev *dev;
160
161 list_for_each_entry(dev, &bus->devices, bus_list) {
162 /*
163 * Skip already-present devices (which are on the
164 * global device list.)
165 */
166 if (list_empty(&dev->global_list)) {
167 int i;
168
169 /* Need to setup IOMMU tables */
170 ppc_md.iommu_dev_setup(dev);
171
172 if(fix_bus)
173 pcibios_fixup_device_resources(dev, bus);
174 pci_read_irq_line(dev);
175 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
176 struct resource *r = &dev->resource[i];
177
178 if (r->parent || !r->start || !r->flags)
179 continue;
180 rpaphp_claim_resource(dev, i);
181 }
182 }
183 }
184}
185
John Rose5fa80fc2005-11-04 15:38:50 -0600186static void rpaphp_eeh_add_bus_device(struct pci_bus *bus)
187{
188 struct pci_dev *dev;
189
190 list_for_each_entry(dev, &bus->devices, bus_list) {
191 eeh_add_device_late(dev);
192 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
193 struct pci_bus *subbus = dev->subordinate;
194 if (subbus)
195 rpaphp_eeh_add_bus_device (subbus);
196 }
197 }
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int rpaphp_pci_config_bridge(struct pci_dev *dev)
201{
202 u8 sec_busno;
203 struct pci_bus *child_bus;
204 struct pci_dev *child_dev;
205
206 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
207
208 /* get busno of downstream bus */
209 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
210
211 /* add to children of PCI bridge dev->bus */
212 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
213 if (!child_bus) {
214 err("%s: could not add second bus\n", __FUNCTION__);
215 return -EIO;
216 }
217 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
218 /* do pci_scan_child_bus */
219 pci_scan_child_bus(child_bus);
220
221 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
222 eeh_add_device_late(child_dev);
223 }
224
225 /* fixup new pci devices without touching bus struct */
226 rpaphp_fixup_new_pci_devices(child_bus, 0);
227
228 /* Make the discovered devices available */
229 pci_bus_add_devices(child_bus);
230 return 0;
231}
232
John Rose5fa80fc2005-11-04 15:38:50 -0600233void rpaphp_init_new_devs(struct pci_bus *bus)
234{
235 rpaphp_fixup_new_pci_devices(bus, 0);
236 rpaphp_eeh_add_bus_device(bus);
237}
238EXPORT_SYMBOL_GPL(rpaphp_init_new_devs);
239
John Rosebde16842005-07-25 10:16:37 -0500240/*****************************************************************************
241 rpaphp_pci_config_slot() will configure all devices under the
242 given slot->dn and return the the first pci_dev.
243 *****************************************************************************/
244static struct pci_dev *
John Rose940903c2005-07-25 10:16:58 -0500245rpaphp_pci_config_slot(struct pci_bus *bus)
John Rosebde16842005-07-25 10:16:37 -0500246{
John Rose940903c2005-07-25 10:16:58 -0500247 struct device_node *dn = pci_bus_to_OF_node(bus);
John Rosebde16842005-07-25 10:16:37 -0500248 struct pci_dev *dev = NULL;
John Rose9c209c92005-07-25 11:13:38 -0500249 int slotno;
John Rosebde16842005-07-25 10:16:37 -0500250 int num;
251
252 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
John Rose940903c2005-07-25 10:16:58 -0500253 if (!dn || !dn->child)
John Rose0945cd52005-07-25 10:16:53 -0500254 return NULL;
John Rosebde16842005-07-25 10:16:37 -0500255
akpm@osdl.org89a071b2005-11-13 16:06:33 -0800256 if (_machine == PLATFORM_PSERIES_LPAR) {
John Rose5fa80fc2005-11-04 15:38:50 -0600257 of_scan_bus(dn, bus);
258 if (list_empty(&bus->devices)) {
259 err("%s: No new device found\n", __FUNCTION__);
260 return NULL;
261 }
John Rose9c209c92005-07-25 11:13:38 -0500262
John Rose5fa80fc2005-11-04 15:38:50 -0600263 rpaphp_init_new_devs(bus);
John Rose0945cd52005-07-25 10:16:53 -0500264 pci_bus_add_devices(bus);
John Rose5fa80fc2005-11-04 15:38:50 -0600265 dev = list_entry(&bus->devices, struct pci_dev, bus_list);
266 } else {
267 slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
268
269 /* pci_scan_slot should find all children */
270 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
271 if (num) {
272 rpaphp_fixup_new_pci_devices(bus, 1);
273 pci_bus_add_devices(bus);
274 }
275 if (list_empty(&bus->devices)) {
276 err("%s: No new device found\n", __FUNCTION__);
277 return NULL;
278 }
279 list_for_each_entry(dev, &bus->devices, bus_list) {
280 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
281 rpaphp_pci_config_bridge(dev);
282
283 rpaphp_eeh_add_bus_device(bus);
284 }
John Rosebde16842005-07-25 10:16:37 -0500285 }
John Rose0945cd52005-07-25 10:16:53 -0500286
John Rosebde16842005-07-25 10:16:37 -0500287 return dev;
288}
289
John Rose940903c2005-07-25 10:16:58 -0500290static void print_slot_pci_funcs(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
John Rose940903c2005-07-25 10:16:58 -0500292 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct pci_dev *dev;
294
John Rose940903c2005-07-25 10:16:58 -0500295 dn = pci_bus_to_OF_node(bus);
296 if (!dn)
297 return;
298
299 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
300 list_for_each_entry (dev, &bus->devices, bus_list)
John Rose5eeb8c62005-07-25 10:16:42 -0500301 dbg("\t%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
303}
304
John Rose940903c2005-07-25 10:16:58 -0500305int rpaphp_config_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
John Rose940903c2005-07-25 10:16:58 -0500307 struct device_node *dn = pci_bus_to_OF_node(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct pci_dev *dev;
309 int rc = -ENODEV;
310
John Rose940903c2005-07-25 10:16:58 -0500311 dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
312 if (!dn)
313 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
linas@austin.ibm.comd681db42005-12-01 18:56:14 -0600315 eeh_add_device_tree_early(dn);
John Rose940903c2005-07-25 10:16:58 -0500316 dev = rpaphp_pci_config_slot(bus);
John Rose9c209c92005-07-25 11:13:38 -0500317 if (!dev) {
318 err("%s: can't find any devices.\n", __FUNCTION__);
319 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
John Rose940903c2005-07-25 10:16:58 -0500321 print_slot_pci_funcs(bus);
John Rose9c209c92005-07-25 11:13:38 -0500322 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323exit:
324 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
325 return rc;
326}
John Rose940903c2005-07-25 10:16:58 -0500327EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
330{
331 eeh_remove_device(dev);
332 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
333 struct pci_bus *bus = dev->subordinate;
334 struct list_head *ln;
335 if (!bus)
336 return;
337 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
338 struct pci_dev *pdev = pci_dev_b(ln);
339 if (pdev)
340 rpaphp_eeh_remove_bus_device(pdev);
341 }
342
343 }
344 return;
345}
346
linas934199e2005-09-28 19:33:38 -0500347int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
John Rose9c209c92005-07-25 11:13:38 -0500349 struct pci_dev *dev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
linas934199e2005-09-28 19:33:38 -0500351 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 rpaphp_eeh_remove_bus_device(dev);
John Rose9c209c92005-07-25 11:13:38 -0500353 pci_remove_bus_device(dev);
354 }
linas934199e2005-09-28 19:33:38 -0500355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
John Rose5fa80fc2005-11-04 15:38:50 -0600357EXPORT_SYMBOL_GPL(rpaphp_unconfig_pci_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359static int setup_pci_hotplug_slot_info(struct slot *slot)
360{
361 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
362 __FUNCTION__);
363 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
364 rpaphp_get_pci_adapter_status(slot, 1,
365 &slot->hotplug_slot->info->
366 adapter_status);
367 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
368 err("%s: NOT_VALID: skip dn->full_name=%s\n",
369 __FUNCTION__, slot->dn->full_name);
370 return -EINVAL;
371 }
372 return 0;
373}
374
John Rose9c209c92005-07-25 11:13:38 -0500375static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
John Rose9c209c92005-07-25 11:13:38 -0500377 struct pci_bus *bus = slot->bus;
378 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
John Rose9c209c92005-07-25 11:13:38 -0500380 bridge = bus->self;
381 if (bridge)
382 strcpy(slot->name, pci_name(bridge));
383 else
384 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
385 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static int setup_pci_slot(struct slot *slot)
389{
John Rose9c209c92005-07-25 11:13:38 -0500390 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
John Rose9c209c92005-07-25 11:13:38 -0500393 BUG_ON(!dn);
394 bus = rpaphp_find_pci_bus(dn);
395 if (!bus) {
396 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
397 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
John Rose9c209c92005-07-25 11:13:38 -0500400 slot->bus = bus;
401 slot->pci_devs = &bus->devices;
402 set_slot_name(slot);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* find slot's pci_dev if it's not empty */
405 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
406 slot->state = EMPTY; /* slot is empty */
407 } else {
408 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500409 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* non-empty slot has to have child */
411 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
412 __FUNCTION__, slot->name);
413 goto exit_rc;
414 }
415
416 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
417 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
418 __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500419 if (rpaphp_config_pci_adapter(slot->bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
421 goto exit_rc;
422 }
423
424 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
425 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
426 __FUNCTION__, slot->name);
427 goto exit_rc;
428 }
John Rose940903c2005-07-25 10:16:58 -0500429 print_slot_pci_funcs(slot->bus);
John Rose5eeb8c62005-07-25 10:16:42 -0500430 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 slot->state = CONFIGURED;
432 } else {
433 /* DLPAR add as opposed to
434 * boot time */
435 slot->state = NOT_CONFIGURED;
436 }
437 }
438 return 0;
439exit_rc:
440 dealloc_slot_struct(slot);
441 return -EINVAL;
442}
443
444int register_pci_slot(struct slot *slot)
445{
446 int rc = -EINVAL;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (setup_pci_hotplug_slot_info(slot))
449 goto exit_rc;
450 if (setup_pci_slot(slot))
451 goto exit_rc;
452 rc = register_slot(slot);
453exit_rc:
454 return rc;
455}
456
457int rpaphp_enable_pci_slot(struct slot *slot)
458{
459 int retval = 0, state;
460
461 retval = rpaphp_get_sensor_state(slot, &state);
462 if (retval)
463 goto exit;
464 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
465 /* if slot is not empty, enable the adapter */
466 if (state == PRESENT) {
467 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500468 retval = rpaphp_config_pci_adapter(slot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!retval) {
470 slot->state = CONFIGURED;
John Rose5fa80fc2005-11-04 15:38:50 -0600471 info("%s: devices in slot[%s] configured\n",
472 __FUNCTION__, slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
474 slot->state = NOT_CONFIGURED;
475 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
476 __FUNCTION__, slot->name);
477 }
478 } else if (state == EMPTY) {
479 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
480 slot->state = EMPTY;
481 } else {
482 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
483 slot->name);
484 slot->state = NOT_VALID;
485 retval = -EINVAL;
486 }
487exit:
488 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
489 return retval;
490}