blob: f16d0f9240ee70d11488c125f302c909edeb555e [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int rpaphp_get_sensor_state(struct slot *slot, int *state)
36{
37 int rc;
38 int setlevel;
39
40 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
41
42 if (rc < 0) {
43 if (rc == -EFAULT || rc == -EEXIST) {
44 dbg("%s: slot must be power up to get sensor-state\n",
45 __FUNCTION__);
46
47 /* some slots have to be powered up
48 * before get-sensor will succeed.
49 */
50 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
51 &setlevel);
52 if (rc < 0) {
53 dbg("%s: power on slot[%s] failed rc=%d.\n",
54 __FUNCTION__, slot->name, rc);
55 } else {
56 rc = rtas_get_sensor(DR_ENTITY_SENSE,
57 slot->index, state);
58 }
59 } else if (rc == -ENODEV)
60 info("%s: slot is unusable\n", __FUNCTION__);
61 else
62 err("%s failed to get sensor state\n", __FUNCTION__);
63 }
64 return rc;
65}
66
67/**
68 * get_pci_adapter_status - get the status of a slot
69 *
70 * 0-- slot is empty
71 * 1-- adapter is configured
72 * 2-- adapter is not configured
73 * 3-- not valid
74 */
75int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
76{
John Rose0945cd52005-07-25 10:16:53 -050077 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int state, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 *value = NOT_VALID;
81 rc = rpaphp_get_sensor_state(slot, &state);
82 if (rc)
83 goto exit;
84
John Rose56d84562005-07-25 10:17:03 -050085 if (state == EMPTY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *value = EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 else if (state == PRESENT) {
88 if (!is_init) {
89 /* at run-time slot->state can be changed by */
90 /* config/unconfig adapter */
91 *value = slot->state;
92 } else {
linas@austin.ibm.com01657862006-01-12 18:18:26 -060093 bus = pcibios_find_pci_bus(slot->dn);
John Rose0945cd52005-07-25 10:16:53 -050094 if (bus && !list_empty(&bus->devices))
95 *value = CONFIGURED;
96 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *value = NOT_CONFIGURED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 }
100exit:
101 return rc;
102}
103
104/* Must be called before pci_bus_add_devices */
John Rose5fa80fc2005-11-04 15:38:50 -0600105void rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct pci_dev *dev;
108
109 list_for_each_entry(dev, &bus->devices, bus_list) {
110 /*
111 * Skip already-present devices (which are on the
112 * global device list.)
113 */
114 if (list_empty(&dev->global_list)) {
115 int i;
116
117 /* Need to setup IOMMU tables */
118 ppc_md.iommu_dev_setup(dev);
119
120 if(fix_bus)
121 pcibios_fixup_device_resources(dev, bus);
122 pci_read_irq_line(dev);
123 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
124 struct resource *r = &dev->resource[i];
125
126 if (r->parent || !r->start || !r->flags)
127 continue;
linas@austin.ibm.comcd5b50b2005-12-01 18:59:58 -0600128 pci_claim_resource(dev, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 }
131 }
132}
133
John Rose5fa80fc2005-11-04 15:38:50 -0600134static void rpaphp_eeh_add_bus_device(struct pci_bus *bus)
135{
136 struct pci_dev *dev;
137
138 list_for_each_entry(dev, &bus->devices, bus_list) {
139 eeh_add_device_late(dev);
140 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
141 struct pci_bus *subbus = dev->subordinate;
142 if (subbus)
143 rpaphp_eeh_add_bus_device (subbus);
144 }
145 }
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int rpaphp_pci_config_bridge(struct pci_dev *dev)
149{
150 u8 sec_busno;
151 struct pci_bus *child_bus;
152 struct pci_dev *child_dev;
153
154 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
155
156 /* get busno of downstream bus */
157 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
158
159 /* add to children of PCI bridge dev->bus */
160 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
161 if (!child_bus) {
162 err("%s: could not add second bus\n", __FUNCTION__);
163 return -EIO;
164 }
165 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
166 /* do pci_scan_child_bus */
167 pci_scan_child_bus(child_bus);
168
169 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
170 eeh_add_device_late(child_dev);
171 }
172
173 /* fixup new pci devices without touching bus struct */
174 rpaphp_fixup_new_pci_devices(child_bus, 0);
175
176 /* Make the discovered devices available */
177 pci_bus_add_devices(child_bus);
178 return 0;
179}
180
John Rose5fa80fc2005-11-04 15:38:50 -0600181void rpaphp_init_new_devs(struct pci_bus *bus)
182{
183 rpaphp_fixup_new_pci_devices(bus, 0);
184 rpaphp_eeh_add_bus_device(bus);
185}
186EXPORT_SYMBOL_GPL(rpaphp_init_new_devs);
187
John Rosebde16842005-07-25 10:16:37 -0500188/*****************************************************************************
189 rpaphp_pci_config_slot() will configure all devices under the
190 given slot->dn and return the the first pci_dev.
191 *****************************************************************************/
192static struct pci_dev *
John Rose940903c2005-07-25 10:16:58 -0500193rpaphp_pci_config_slot(struct pci_bus *bus)
John Rosebde16842005-07-25 10:16:37 -0500194{
John Rose940903c2005-07-25 10:16:58 -0500195 struct device_node *dn = pci_bus_to_OF_node(bus);
John Rosebde16842005-07-25 10:16:37 -0500196 struct pci_dev *dev = NULL;
John Rose9c209c92005-07-25 11:13:38 -0500197 int slotno;
John Rosebde16842005-07-25 10:16:37 -0500198 int num;
199
200 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
John Rose940903c2005-07-25 10:16:58 -0500201 if (!dn || !dn->child)
John Rose0945cd52005-07-25 10:16:53 -0500202 return NULL;
John Rosebde16842005-07-25 10:16:37 -0500203
akpm@osdl.org89a071b2005-11-13 16:06:33 -0800204 if (_machine == PLATFORM_PSERIES_LPAR) {
John Rose5fa80fc2005-11-04 15:38:50 -0600205 of_scan_bus(dn, bus);
206 if (list_empty(&bus->devices)) {
207 err("%s: No new device found\n", __FUNCTION__);
208 return NULL;
209 }
John Rose9c209c92005-07-25 11:13:38 -0500210
John Rose5fa80fc2005-11-04 15:38:50 -0600211 rpaphp_init_new_devs(bus);
John Rose0945cd52005-07-25 10:16:53 -0500212 pci_bus_add_devices(bus);
John Rose5fa80fc2005-11-04 15:38:50 -0600213 dev = list_entry(&bus->devices, struct pci_dev, bus_list);
214 } else {
215 slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
216
217 /* pci_scan_slot should find all children */
218 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
219 if (num) {
220 rpaphp_fixup_new_pci_devices(bus, 1);
221 pci_bus_add_devices(bus);
222 }
223 if (list_empty(&bus->devices)) {
224 err("%s: No new device found\n", __FUNCTION__);
225 return NULL;
226 }
227 list_for_each_entry(dev, &bus->devices, bus_list) {
228 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
229 rpaphp_pci_config_bridge(dev);
230
231 rpaphp_eeh_add_bus_device(bus);
232 }
John Rosebde16842005-07-25 10:16:37 -0500233 }
John Rose0945cd52005-07-25 10:16:53 -0500234
John Rosebde16842005-07-25 10:16:37 -0500235 return dev;
236}
237
John Rose940903c2005-07-25 10:16:58 -0500238static void print_slot_pci_funcs(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
John Rose940903c2005-07-25 10:16:58 -0500240 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct pci_dev *dev;
242
John Rose940903c2005-07-25 10:16:58 -0500243 dn = pci_bus_to_OF_node(bus);
244 if (!dn)
245 return;
246
247 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
248 list_for_each_entry (dev, &bus->devices, bus_list)
John Rose5eeb8c62005-07-25 10:16:42 -0500249 dbg("\t%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return;
251}
252
John Rose940903c2005-07-25 10:16:58 -0500253int rpaphp_config_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
John Rose940903c2005-07-25 10:16:58 -0500255 struct device_node *dn = pci_bus_to_OF_node(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct pci_dev *dev;
257 int rc = -ENODEV;
258
John Rose940903c2005-07-25 10:16:58 -0500259 dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
260 if (!dn)
261 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
linas@austin.ibm.comd681db42005-12-01 18:56:14 -0600263 eeh_add_device_tree_early(dn);
John Rose940903c2005-07-25 10:16:58 -0500264 dev = rpaphp_pci_config_slot(bus);
John Rose9c209c92005-07-25 11:13:38 -0500265 if (!dev) {
266 err("%s: can't find any devices.\n", __FUNCTION__);
267 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
John Rose940903c2005-07-25 10:16:58 -0500269 print_slot_pci_funcs(bus);
John Rose9c209c92005-07-25 11:13:38 -0500270 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271exit:
272 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
273 return rc;
274}
John Rose940903c2005-07-25 10:16:58 -0500275EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
278{
279 eeh_remove_device(dev);
280 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
281 struct pci_bus *bus = dev->subordinate;
282 struct list_head *ln;
283 if (!bus)
284 return;
285 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
286 struct pci_dev *pdev = pci_dev_b(ln);
287 if (pdev)
288 rpaphp_eeh_remove_bus_device(pdev);
289 }
290
291 }
292 return;
293}
294
linas934199e2005-09-28 19:33:38 -0500295int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
John Rose9c209c92005-07-25 11:13:38 -0500297 struct pci_dev *dev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
linas934199e2005-09-28 19:33:38 -0500299 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 rpaphp_eeh_remove_bus_device(dev);
John Rose9c209c92005-07-25 11:13:38 -0500301 pci_remove_bus_device(dev);
302 }
linas934199e2005-09-28 19:33:38 -0500303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
John Rose5fa80fc2005-11-04 15:38:50 -0600305EXPORT_SYMBOL_GPL(rpaphp_unconfig_pci_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307static int setup_pci_hotplug_slot_info(struct slot *slot)
308{
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100309 struct hotplug_slot_info *hotplug_slot_info = slot->hotplug_slot->info;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
312 __FUNCTION__);
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100313 rpaphp_get_power_status(slot, &hotplug_slot_info->power_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 rpaphp_get_pci_adapter_status(slot, 1,
Jesper Juhlf5c99df2005-12-11 06:42:38 +0100315 &hotplug_slot_info->adapter_status);
316 if (hotplug_slot_info->adapter_status == NOT_VALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 err("%s: NOT_VALID: skip dn->full_name=%s\n",
318 __FUNCTION__, slot->dn->full_name);
319 return -EINVAL;
320 }
321 return 0;
322}
323
John Rose9c209c92005-07-25 11:13:38 -0500324static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
John Rose9c209c92005-07-25 11:13:38 -0500326 struct pci_bus *bus = slot->bus;
327 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
John Rose9c209c92005-07-25 11:13:38 -0500329 bridge = bus->self;
330 if (bridge)
331 strcpy(slot->name, pci_name(bridge));
332 else
333 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
334 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static int setup_pci_slot(struct slot *slot)
338{
John Rose9c209c92005-07-25 11:13:38 -0500339 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
John Rose9c209c92005-07-25 11:13:38 -0500342 BUG_ON(!dn);
linas@austin.ibm.com01657862006-01-12 18:18:26 -0600343 bus = pcibios_find_pci_bus(dn);
John Rose9c209c92005-07-25 11:13:38 -0500344 if (!bus) {
345 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
346 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
John Rose9c209c92005-07-25 11:13:38 -0500349 slot->bus = bus;
350 slot->pci_devs = &bus->devices;
351 set_slot_name(slot);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* find slot's pci_dev if it's not empty */
354 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
355 slot->state = EMPTY; /* slot is empty */
356 } else {
357 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500358 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* non-empty slot has to have child */
360 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
361 __FUNCTION__, slot->name);
362 goto exit_rc;
363 }
364
365 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
366 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
367 __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500368 if (rpaphp_config_pci_adapter(slot->bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
370 goto exit_rc;
371 }
372
373 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
374 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
375 __FUNCTION__, slot->name);
376 goto exit_rc;
377 }
John Rose940903c2005-07-25 10:16:58 -0500378 print_slot_pci_funcs(slot->bus);
John Rose5eeb8c62005-07-25 10:16:42 -0500379 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 slot->state = CONFIGURED;
381 } else {
382 /* DLPAR add as opposed to
383 * boot time */
384 slot->state = NOT_CONFIGURED;
385 }
386 }
387 return 0;
388exit_rc:
389 dealloc_slot_struct(slot);
390 return -EINVAL;
391}
392
393int register_pci_slot(struct slot *slot)
394{
395 int rc = -EINVAL;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (setup_pci_hotplug_slot_info(slot))
398 goto exit_rc;
399 if (setup_pci_slot(slot))
400 goto exit_rc;
401 rc = register_slot(slot);
402exit_rc:
403 return rc;
404}
405
406int rpaphp_enable_pci_slot(struct slot *slot)
407{
408 int retval = 0, state;
409
410 retval = rpaphp_get_sensor_state(slot, &state);
411 if (retval)
412 goto exit;
413 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
414 /* if slot is not empty, enable the adapter */
415 if (state == PRESENT) {
416 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500417 retval = rpaphp_config_pci_adapter(slot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!retval) {
419 slot->state = CONFIGURED;
John Rose5fa80fc2005-11-04 15:38:50 -0600420 info("%s: devices in slot[%s] configured\n",
421 __FUNCTION__, slot->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
423 slot->state = NOT_CONFIGURED;
424 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
425 __FUNCTION__, slot->name);
426 }
427 } else if (state == EMPTY) {
428 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
429 slot->state = EMPTY;
430 } else {
431 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
432 slot->name);
433 slot->state = NOT_VALID;
434 retval = -EINVAL;
435 }
436exit:
437 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
438 return retval;
439}