[PATCH] PCI Hotplug: rpaphp: Remove rpaphp_find_pci
[pandora-kernel.git] / drivers / pci / hotplug / rpaphp_pci.c
1 /*
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
33 static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
34                                         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
52 static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
53 {
54         if (!dn->phb || !dn->phb->bus)
55                 return NULL;
56
57         return find_bus_among_children(dn->phb->bus, dn);
58 }
59
60 int 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
80 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
81
82 static 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  */
122 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
123 {
124         struct pci_bus *bus;
125         int state, rc;
126
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 {
142                         bus = rpaphp_find_pci_bus(slot->dn);
143                         if (bus && !list_empty(&bus->devices))
144                                 *value = CONFIGURED;
145                         else
146                                 *value = NOT_CONFIGURED;
147                 }
148         }
149 exit:
150         return rc;
151 }
152
153 /* Must be called before pci_bus_add_devices */
154 static void 
155 rpaphp_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
184 static 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
217 /*****************************************************************************
218  rpaphp_pci_config_slot() will  configure all devices under the
219  given slot->dn and return the the first pci_dev.
220  *****************************************************************************/
221 static struct pci_dev *
222 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
223 {
224         struct pci_dev *dev = NULL;
225         int slotno;
226         int num;
227
228         dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
229         if (!dn->child)
230                 return NULL;
231
232         slotno = PCI_SLOT(dn->child->devfn);
233
234         /* 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) {
245                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
246                         rpaphp_pci_config_bridge(dev);
247         }
248
249         return dev;
250 }
251
252 static 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
263 static void print_slot_pci_funcs(struct slot *slot)
264 {
265         struct pci_dev *dev;
266
267         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));
270         return;
271 }
272
273 static int rpaphp_config_pci_adapter(struct slot *slot)
274 {
275         struct pci_dev *dev;
276         int rc = -ENODEV;
277
278         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
279
280         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;
285         }
286         print_slot_pci_funcs(slot);
287         rc = 0;
288 exit:
289         dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
290         return rc;
291 }
292
293 static 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
311 int rpaphp_unconfig_pci_adapter(struct slot *slot)
312 {
313         struct pci_dev *dev, *tmp;
314         int retval = 0;
315
316         list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
317                 rpaphp_eeh_remove_bus_device(dev);
318                 pci_remove_bus_device(dev);
319         }
320
321         slot->state = NOT_CONFIGURED;
322         info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
323              slot->name);
324         return retval;
325 }
326
327 static 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
343 static void set_slot_name(struct slot *slot)
344 {
345         struct pci_bus *bus = slot->bus;
346         struct pci_dev *bridge;
347
348         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);
354 }
355
356 static int setup_pci_slot(struct slot *slot)
357 {
358         struct device_node *dn = slot->dn;
359         struct pci_bus *bus;
360
361         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;
366         }
367
368         slot->bus = bus;
369         slot->pci_devs = &bus->devices;
370         set_slot_name(slot);
371
372         /* 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 */
377                 if (!dn->child) {
378                         /* 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);
398                 if (!list_empty(slot->pci_devs)) {
399                         slot->state = CONFIGURED;
400                 } else {
401                         /* DLPAR add as opposed to 
402                          * boot time */
403                         slot->state = NOT_CONFIGURED;
404                 }
405         }
406         return 0;
407 exit_rc:
408         dealloc_slot_struct(slot);
409         return -EINVAL;
410 }
411
412 int register_pci_slot(struct slot *slot)
413 {
414         int rc = -EINVAL;
415
416         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);
425 exit_rc:
426         return rc;
427 }
428
429 int 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         }
459 exit:
460         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
461         return retval;
462 }