blob: 54fff5fb00940b7a770202a28f4bdaf31dd5c3bb [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
33struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
34{
35 struct pci_dev *dev = NULL;
36 char bus_id[BUS_ID_SIZE];
37
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)) {
42 break;
43 }
44 }
45 return dev;
46}
47
John Rose9c209c92005-07-25 11:13:38 -050048struct pci_bus *find_bus_among_children(struct pci_bus *bus,
49 struct device_node *dn)
50{
51 struct pci_bus *child = NULL;
52 struct list_head *tmp;
53 struct device_node *busdn;
54
55 busdn = pci_bus_to_OF_node(bus);
56 if (busdn == dn)
57 return bus;
58
59 list_for_each(tmp, &bus->children) {
60 child = find_bus_among_children(pci_bus_b(tmp), dn);
61 if (child)
62 break;
63 }
64 return child;
65}
66
67struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
68{
69 BUG_ON(!dn->phb || !dn->phb->bus);
70
71 return find_bus_among_children(dn->phb->bus, dn);
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
75
76int rpaphp_claim_resource(struct pci_dev *dev, int resource)
77{
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";
81 int err = -EINVAL;
82
83 if (root != NULL) {
84 err = request_resource(root, res);
85 }
86
87 if (err) {
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);
92 }
93 return err;
94}
95
96EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int rpaphp_get_sensor_state(struct slot *slot, int *state)
99{
100 int rc;
101 int setlevel;
102
103 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
104
105 if (rc < 0) {
106 if (rc == -EFAULT || rc == -EEXIST) {
107 dbg("%s: slot must be power up to get sensor-state\n",
108 __FUNCTION__);
109
110 /* some slots have to be powered up
111 * before get-sensor will succeed.
112 */
113 rc = rtas_set_power_level(slot->power_domain, POWER_ON,
114 &setlevel);
115 if (rc < 0) {
116 dbg("%s: power on slot[%s] failed rc=%d.\n",
117 __FUNCTION__, slot->name, rc);
118 } else {
119 rc = rtas_get_sensor(DR_ENTITY_SENSE,
120 slot->index, state);
121 }
122 } else if (rc == -ENODEV)
123 info("%s: slot is unusable\n", __FUNCTION__);
124 else
125 err("%s failed to get sensor state\n", __FUNCTION__);
126 }
127 return rc;
128}
129
130/**
131 * get_pci_adapter_status - get the status of a slot
132 *
133 * 0-- slot is empty
134 * 1-- adapter is configured
135 * 2-- adapter is not configured
136 * 3-- not valid
137 */
138int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
139{
140 int state, rc;
141 struct device_node *child_dn;
142 struct pci_dev *child_dev = NULL;
143
144 *value = NOT_VALID;
145 rc = rpaphp_get_sensor_state(slot, &state);
146 if (rc)
147 goto exit;
148
149 if ((state == EMPTY) || (slot->type == PHB)) {
150 dbg("slot is empty\n");
151 *value = EMPTY;
152 }
153 else if (state == PRESENT) {
154 if (!is_init) {
155 /* at run-time slot->state can be changed by */
156 /* config/unconfig adapter */
157 *value = slot->state;
158 } else {
159 child_dn = slot->dn->child;
160 if (child_dn)
161 child_dev = rpaphp_find_pci_dev(child_dn);
162
163 if (child_dev)
164 *value = CONFIGURED;
165 else if (!child_dn)
166 dbg("%s: %s is not valid OFDT node\n",
167 __FUNCTION__, slot->dn->full_name);
168 else {
169 err("%s: can't find pdev of adapter in slot[%s]\n",
170 __FUNCTION__, slot->dn->full_name);
171 *value = NOT_CONFIGURED;
172 }
173 }
174 }
175exit:
176 return rc;
177}
178
179/* Must be called before pci_bus_add_devices */
180static void
181rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
182{
183 struct pci_dev *dev;
184
185 list_for_each_entry(dev, &bus->devices, bus_list) {
186 /*
187 * Skip already-present devices (which are on the
188 * global device list.)
189 */
190 if (list_empty(&dev->global_list)) {
191 int i;
192
193 /* Need to setup IOMMU tables */
194 ppc_md.iommu_dev_setup(dev);
195
196 if(fix_bus)
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];
201
202 if (r->parent || !r->start || !r->flags)
203 continue;
204 rpaphp_claim_resource(dev, i);
205 }
206 }
207 }
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int rpaphp_pci_config_bridge(struct pci_dev *dev)
211{
212 u8 sec_busno;
213 struct pci_bus *child_bus;
214 struct pci_dev *child_dev;
215
216 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
217
218 /* get busno of downstream bus */
219 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
220
221 /* add to children of PCI bridge dev->bus */
222 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
223 if (!child_bus) {
224 err("%s: could not add second bus\n", __FUNCTION__);
225 return -EIO;
226 }
227 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
228 /* do pci_scan_child_bus */
229 pci_scan_child_bus(child_bus);
230
231 list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
232 eeh_add_device_late(child_dev);
233 }
234
235 /* fixup new pci devices without touching bus struct */
236 rpaphp_fixup_new_pci_devices(child_bus, 0);
237
238 /* Make the discovered devices available */
239 pci_bus_add_devices(child_bus);
240 return 0;
241}
242
John Rosebde16842005-07-25 10:16:37 -0500243/*****************************************************************************
244 rpaphp_pci_config_slot() will configure all devices under the
245 given slot->dn and return the the first pci_dev.
246 *****************************************************************************/
247static struct pci_dev *
248rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
249{
John Rosebde16842005-07-25 10:16:37 -0500250 struct pci_dev *dev = NULL;
John Rose9c209c92005-07-25 11:13:38 -0500251 int slotno;
John Rosebde16842005-07-25 10:16:37 -0500252 int num;
253
254 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
255
John Rose9c209c92005-07-25 11:13:38 -0500256 if (dn->child) {
257 slotno = PCI_SLOT(dn->child->devfn);
258
259 /* pci_scan_slot should find all children */
260 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
John Rosebde16842005-07-25 10:16:37 -0500261 if (num) {
262 rpaphp_fixup_new_pci_devices(bus, 1);
263 pci_bus_add_devices(bus);
264 }
John Rose9c209c92005-07-25 11:13:38 -0500265 dev = rpaphp_find_pci_dev(dn->child);
John Rosebde16842005-07-25 10:16:37 -0500266 if (!dev) {
267 err("No new device found\n");
268 return NULL;
269 }
270 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
271 rpaphp_pci_config_bridge(dev);
272 }
273 return dev;
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void enable_eeh(struct device_node *dn)
277{
278 struct device_node *sib;
279
280 for (sib = dn->child; sib; sib = sib->sibling)
281 enable_eeh(sib);
282 eeh_add_device_early(dn);
283 return;
284
285}
286
287static void print_slot_pci_funcs(struct slot *slot)
288{
289 struct pci_dev *dev;
290
John Rose5eeb8c62005-07-25 10:16:42 -0500291 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return;
295}
296
297static int rpaphp_config_pci_adapter(struct slot *slot)
298{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct pci_dev *dev;
300 int rc = -ENODEV;
301
302 dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
303
John Rose9c209c92005-07-25 11:13:38 -0500304 enable_eeh(slot->dn);
305 dev = rpaphp_pci_config_slot(slot->dn, slot->bus);
306 if (!dev) {
307 err("%s: can't find any devices.\n", __FUNCTION__);
308 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
John Rose9c209c92005-07-25 11:13:38 -0500310 print_slot_pci_funcs(slot);
311 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312exit:
313 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc);
314 return rc;
315}
316
317static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
318{
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;
323 if (!bus)
324 return;
325 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
326 struct pci_dev *pdev = pci_dev_b(ln);
327 if (pdev)
328 rpaphp_eeh_remove_bus_device(pdev);
329 }
330
331 }
332 return;
333}
334
335int rpaphp_unconfig_pci_adapter(struct slot *slot)
336{
John Rose9c209c92005-07-25 11:13:38 -0500337 struct pci_dev *dev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int retval = 0;
339
John Rose9c209c92005-07-25 11:13:38 -0500340 list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 rpaphp_eeh_remove_bus_device(dev);
John Rose9c209c92005-07-25 11:13:38 -0500342 pci_remove_bus_device(dev);
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 slot->state = NOT_CONFIGURED;
346 info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
347 slot->name);
348 return retval;
349}
350
351static int setup_pci_hotplug_slot_info(struct slot *slot)
352{
353 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
354 __FUNCTION__);
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->
358 adapter_status);
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);
362 return -EINVAL;
363 }
364 return 0;
365}
366
John Rose9c209c92005-07-25 11:13:38 -0500367static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
John Rose9c209c92005-07-25 11:13:38 -0500369 struct pci_bus *bus = slot->bus;
370 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
John Rose9c209c92005-07-25 11:13:38 -0500372 bridge = bus->self;
373 if (bridge)
374 strcpy(slot->name, pci_name(bridge));
375 else
376 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
377 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static int setup_pci_slot(struct slot *slot)
381{
John Rose9c209c92005-07-25 11:13:38 -0500382 struct device_node *dn = slot->dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
John Rose9c209c92005-07-25 11:13:38 -0500385 BUG_ON(!dn);
386 bus = rpaphp_find_pci_bus(dn);
387 if (!bus) {
388 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
389 goto exit_rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
John Rose9c209c92005-07-25 11:13:38 -0500392 slot->bus = bus;
393 slot->pci_devs = &bus->devices;
394 set_slot_name(slot);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* 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 */
399 } else {
400 /* slot is occupied */
John Rose9c209c92005-07-25 11:13:38 -0500401 if (!dn->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* 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);
405 goto exit_rc;
406 }
407
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__);
413 goto exit_rc;
414 }
415
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);
419 goto exit_rc;
420 }
421 print_slot_pci_funcs(slot);
John Rose5eeb8c62005-07-25 10:16:42 -0500422 if (!list_empty(slot->pci_devs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 slot->state = CONFIGURED;
424 } else {
425 /* DLPAR add as opposed to
426 * boot time */
427 slot->state = NOT_CONFIGURED;
428 }
429 }
430 return 0;
431exit_rc:
432 dealloc_slot_struct(slot);
433 return -EINVAL;
434}
435
436int register_pci_slot(struct slot *slot)
437{
438 int rc = -EINVAL;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if ((slot->type == EMBEDDED) || (slot->type == PHB))
441 slot->removable = 0;
442 else
443 slot->removable = 1;
444 if (setup_pci_hotplug_slot_info(slot))
445 goto exit_rc;
446 if (setup_pci_slot(slot))
447 goto exit_rc;
448 rc = register_slot(slot);
449exit_rc:
450 return rc;
451}
452
453int rpaphp_enable_pci_slot(struct slot *slot)
454{
455 int retval = 0, state;
456
457 retval = rpaphp_get_sensor_state(slot, &state);
458 if (retval)
459 goto exit;
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);
465 if (!retval) {
466 slot->state = CONFIGURED;
467 dbg("%s: PCI devices in slot[%s] has been configured\n",
468 __FUNCTION__, slot->name);
469 } else {
470 slot->state = NOT_CONFIGURED;
471 dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
472 __FUNCTION__, slot->name);
473 }
474 } else if (state == EMPTY) {
475 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
476 slot->state = EMPTY;
477 } else {
478 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
479 slot->name);
480 slot->state = NOT_VALID;
481 retval = -EINVAL;
482 }
483exit:
484 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
485 return retval;
486}