blob: f7c12d7dfcfc35bc164163672b60ef817bc23ff5 [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 */
157static void
158rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
159{
160 struct pci_dev *dev;
161
162 list_for_each_entry(dev, &bus->devices, bus_list) {
163 /*
164 * Skip already-present devices (which are on the
165 * global device list.)
166 */
167 if (list_empty(&dev->global_list)) {
168 int i;
169
170 /* Need to setup IOMMU tables */
171 ppc_md.iommu_dev_setup(dev);
172
173 if(fix_bus)
174 pcibios_fixup_device_resources(dev, bus);
175 pci_read_irq_line(dev);
176 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
177 struct resource *r = &dev->resource[i];
178
179 if (r->parent || !r->start || !r->flags)
180 continue;
181 rpaphp_claim_resource(dev, i);
182 }
183 }
184 }
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int rpaphp_pci_config_bridge(struct pci_dev *dev)
188{
189 u8 sec_busno;
190 struct pci_bus *child_bus;
191 struct pci_dev *child_dev;
192
193 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
194
195 /* get busno of downstream bus */
196 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
197
198 /* add to children of PCI bridge dev->bus */
199 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
200 if (!child_bus) {
201 err("%s: could not add second bus\n", __FUNCTION__);
202 return -EIO;
203 }
204 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
205 /* do pci_scan_child_bus */
206 pci_scan_child_bus(child_bus);
207
208 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
209 eeh_add_device_late(child_dev);
210 }
211
212 /* fixup new pci devices without touching bus struct */
213 rpaphp_fixup_new_pci_devices(child_bus, 0);
214
215 /* Make the discovered devices available */
216 pci_bus_add_devices(child_bus);
217 return 0;
218}
219
John Rosebde16842005-07-25 10:16:37 -0500220/*****************************************************************************
221 rpaphp_pci_config_slot() will configure all devices under the
222 given slot->dn and return the the first pci_dev.
223 *****************************************************************************/
224static struct pci_dev *
John Rose940903c2005-07-25 10:16:58 -0500225rpaphp_pci_config_slot(struct pci_bus *bus)
John Rosebde16842005-07-25 10:16:37 -0500226{
John Rose940903c2005-07-25 10:16:58 -0500227 struct device_node *dn = pci_bus_to_OF_node(bus);
John Rosebde16842005-07-25 10:16:37 -0500228 struct pci_dev *dev = NULL;
John Rose9c209c92005-07-25 11:13:38 -0500229 int slotno;
John Rosebde16842005-07-25 10:16:37 -0500230 int num;
231
232 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
John Rose940903c2005-07-25 10:16:58 -0500233 if (!dn || !dn->child)
John Rose0945cd52005-07-25 10:16:53 -0500234 return NULL;
John Rosebde16842005-07-25 10:16:37 -0500235
Paul Mackerras16353172005-09-06 13:17:54 +1000236 slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
John Rose9c209c92005-07-25 11:13:38 -0500237
John Rose0945cd52005-07-25 10:16:53 -0500238 /* pci_scan_slot should find all children */
239 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
240 if (num) {
241 rpaphp_fixup_new_pci_devices(bus, 1);
242 pci_bus_add_devices(bus);
243 }
244 if (list_empty(&bus->devices)) {
245 err("%s: No new device found\n", __FUNCTION__);
246 return NULL;
247 }
248 list_for_each_entry(dev, &bus->devices, bus_list) {
John Rosebde16842005-07-25 10:16:37 -0500249 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
250 rpaphp_pci_config_bridge(dev);
251 }
John Rose0945cd52005-07-25 10:16:53 -0500252
John Rosebde16842005-07-25 10:16:37 -0500253 return dev;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static void enable_eeh(struct device_node *dn)
257{
258 struct device_node *sib;
259
260 for (sib = dn->child; sib; sib = sib->sibling)
261 enable_eeh(sib);
262 eeh_add_device_early(dn);
263 return;
264
265}
266
John Rose940903c2005-07-25 10:16:58 -0500267static void print_slot_pci_funcs(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
John Rose940903c2005-07-25 10:16:58 -0500269 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct pci_dev *dev;
271
John Rose940903c2005-07-25 10:16:58 -0500272 dn = pci_bus_to_OF_node(bus);
273 if (!dn)
274 return;
275
276 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
277 list_for_each_entry (dev, &bus->devices, bus_list)
John Rose5eeb8c62005-07-25 10:16:42 -0500278 dbg("\t%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return;
280}
281
John Rose940903c2005-07-25 10:16:58 -0500282int rpaphp_config_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
John Rose940903c2005-07-25 10:16:58 -0500284 struct device_node *dn = pci_bus_to_OF_node(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct pci_dev *dev;
286 int rc = -ENODEV;
287
John Rose940903c2005-07-25 10:16:58 -0500288 dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
289 if (!dn)
290 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
John Rose940903c2005-07-25 10:16:58 -0500292 enable_eeh(dn);
293 dev = rpaphp_pci_config_slot(bus);
John Rose9c209c92005-07-25 11:13:38 -0500294 if (!dev) {
295 err("%s: can't find any devices.\n", __FUNCTION__);
296 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
John Rose940903c2005-07-25 10:16:58 -0500298 print_slot_pci_funcs(bus);
John Rose9c209c92005-07-25 11:13:38 -0500299 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300exit:
301 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
302 return rc;
303}
John Rose940903c2005-07-25 10:16:58 -0500304EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
307{
308 eeh_remove_device(dev);
309 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
310 struct pci_bus *bus = dev->subordinate;
311 struct list_head *ln;
312 if (!bus)
313 return;
314 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
315 struct pci_dev *pdev = pci_dev_b(ln);
316 if (pdev)
317 rpaphp_eeh_remove_bus_device(pdev);
318 }
319
320 }
321 return;
322}
323
linas934199e2005-09-28 19:33:38 -0500324int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
John Rose9c209c92005-07-25 11:13:38 -0500326 struct pci_dev *dev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
linas934199e2005-09-28 19:33:38 -0500328 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 rpaphp_eeh_remove_bus_device(dev);
John Rose9c209c92005-07-25 11:13:38 -0500330 pci_remove_bus_device(dev);
331 }
linas934199e2005-09-28 19:33:38 -0500332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static int setup_pci_hotplug_slot_info(struct slot *slot)
336{
337 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
338 __FUNCTION__);
339 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
340 rpaphp_get_pci_adapter_status(slot, 1,
341 &slot->hotplug_slot->info->
342 adapter_status);
343 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
344 err("%s: NOT_VALID: skip dn->full_name=%s\n",
345 __FUNCTION__, slot->dn->full_name);
346 return -EINVAL;
347 }
348 return 0;
349}
350
John Rose9c209c92005-07-25 11:13:38 -0500351static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
John Rose9c209c92005-07-25 11:13:38 -0500353 struct pci_bus *bus = slot->bus;
354 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
John Rose9c209c92005-07-25 11:13:38 -0500356 bridge = bus->self;
357 if (bridge)
358 strcpy(slot->name, pci_name(bridge));
359 else
360 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
361 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static int setup_pci_slot(struct slot *slot)
365{
John Rose9c209c92005-07-25 11:13:38 -0500366 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
John Rose9c209c92005-07-25 11:13:38 -0500369 BUG_ON(!dn);
370 bus = rpaphp_find_pci_bus(dn);
371 if (!bus) {
372 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
373 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
John Rose9c209c92005-07-25 11:13:38 -0500376 slot->bus = bus;
377 slot->pci_devs = &bus->devices;
378 set_slot_name(slot);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* find slot's pci_dev if it's not empty */
381 if (slot->hotplug_slot->info->adapter_status == EMPTY) {
382 slot->state = EMPTY; /* slot is empty */
383 } else {
384 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500385 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* non-empty slot has to have child */
387 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
388 __FUNCTION__, slot->name);
389 goto exit_rc;
390 }
391
392 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
393 dbg("%s CONFIGURING pci adapter in slot[%s]\n",
394 __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500395 if (rpaphp_config_pci_adapter(slot->bus)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
397 goto exit_rc;
398 }
399
400 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
401 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
402 __FUNCTION__, slot->name);
403 goto exit_rc;
404 }
John Rose940903c2005-07-25 10:16:58 -0500405 print_slot_pci_funcs(slot->bus);
John Rose5eeb8c62005-07-25 10:16:42 -0500406 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 slot->state = CONFIGURED;
408 } else {
409 /* DLPAR add as opposed to
410 * boot time */
411 slot->state = NOT_CONFIGURED;
412 }
413 }
414 return 0;
415exit_rc:
416 dealloc_slot_struct(slot);
417 return -EINVAL;
418}
419
420int register_pci_slot(struct slot *slot)
421{
422 int rc = -EINVAL;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (setup_pci_hotplug_slot_info(slot))
425 goto exit_rc;
426 if (setup_pci_slot(slot))
427 goto exit_rc;
428 rc = register_slot(slot);
429exit_rc:
430 return rc;
431}
432
433int rpaphp_enable_pci_slot(struct slot *slot)
434{
435 int retval = 0, state;
436
437 retval = rpaphp_get_sensor_state(slot, &state);
438 if (retval)
439 goto exit;
440 dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
441 /* if slot is not empty, enable the adapter */
442 if (state == PRESENT) {
443 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
John Rose940903c2005-07-25 10:16:58 -0500444 retval = rpaphp_config_pci_adapter(slot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (!retval) {
446 slot->state = CONFIGURED;
447 dbg("%s: PCI devices in slot[%s] has been configured\n",
448 __FUNCTION__, slot->name);
449 } else {
450 slot->state = NOT_CONFIGURED;
451 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
452 __FUNCTION__, slot->name);
453 }
454 } else if (state == EMPTY) {
455 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
456 slot->state = EMPTY;
457 } else {
458 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
459 slot->name);
460 slot->state = NOT_VALID;
461 retval = -EINVAL;
462 }
463exit:
464 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
465 return retval;
466}