This commit was manufactured by cvs2svn to create tag
[linux-2.6.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 "../pci.h"             /* for pci_add_new_bus */
29
30 #include "rpaphp.h"
31
32 struct pci_dev *rpaphp_find_pci_dev(struct device_node *dn)
33 {
34         struct pci_dev *retval_dev = NULL, *dev = NULL;
35         char bus_id[BUS_ID_SIZE];
36
37         sprintf(bus_id, "%04x:%02x:%02x.%d",dn->phb->global_number,
38                 dn->busno, PCI_SLOT(dn->devfn), PCI_FUNC(dn->devfn));
39         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
40                 if (!strcmp(pci_name(dev), bus_id)) {
41                         retval_dev = dev;
42                         break;
43                 }
44         }
45         return retval_dev;
46 }
47
48 EXPORT_SYMBOL_GPL(rpaphp_find_pci_dev);
49
50 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
51 {
52         struct resource *res = &dev->resource[resource];
53         struct resource *root = pci_find_parent_resource(dev, res);
54         char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
55         int err = -EINVAL;
56
57         if (root != NULL) {
58                 err = request_resource(root, res);
59         }
60
61         if (err) {
62                 err("PCI: %s region %d of %s %s [%lx:%lx]\n",
63                     root ? "Address space collision on" :
64                     "No parent found for",
65                     resource, dtype, pci_name(dev), res->start, res->end);
66         }
67         return err;
68 }
69
70 EXPORT_SYMBOL_GPL(rpaphp_claim_resource);
71
72 static struct pci_dev *rpaphp_find_bridge_pdev(struct slot *slot)
73 {
74         return rpaphp_find_pci_dev(slot->dn);
75 }
76
77 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
78 {
79         int rc;
80         int setlevel;
81
82         rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
83
84         if (rc) {
85                 if (rc == NEED_POWER || rc == PWR_ONLY) {
86                         dbg("%s: slot must be power up to get sensor-state\n",
87                             __FUNCTION__);
88
89                         /* some slots have to be powered up 
90                          * before get-sensor will succeed.
91                          */
92                         rc = rtas_set_power_level(slot->power_domain, POWER_ON,
93                                                   &setlevel);
94                         if (rc) {
95                                 dbg("%s: power on slot[%s] failed rc=%d.\n",
96                                     __FUNCTION__, slot->name, rc);
97                         } else {
98                                 rc = rtas_get_sensor(DR_ENTITY_SENSE,
99                                                      slot->index, state);
100                         }
101                 } else if (rc == ERR_SENSE_USE)
102                         info("%s: slot is unusable\n", __FUNCTION__);
103                 else
104                         err("%s failed to get sensor state\n", __FUNCTION__);
105         }
106         return rc;
107 }
108
109 /**
110  * get_pci_adapter_status - get the status of a slot
111  * 
112  * 0-- slot is empty
113  * 1-- adapter is configured
114  * 2-- adapter is not configured
115  * 3-- not valid
116  */
117 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
118 {
119         int state, rc;
120         struct device_node *child_dn;
121         struct pci_dev *child_dev;
122
123         *value = NOT_VALID;
124         rc = rpaphp_get_sensor_state(slot, &state);
125         if (rc)
126                 goto exit;
127
128         if ((state == EMPTY) || (slot->type == PHB)) {
129                 dbg("slot is empty\n");
130                 *value = EMPTY;
131         }
132         else if (state == PRESENT) {
133                 if (!is_init)
134                         /* at run-time slot->state can be changed by */
135                         /* config/unconfig adapter */
136                         *value = slot->state;
137                 else {
138                         child_dn = slot->dn->child;
139                         if (child_dn)
140                                 child_dev = rpaphp_find_pci_dev(child_dn);
141
142                         if (child_dev)
143                                 *value = CONFIGURED;
144                         else if (!child_dn)
145                                 dbg("%s: %s is not valid OFDT node\n",
146                                     __FUNCTION__, slot->dn->full_name);
147                         else {
148                                 err("%s: can't find pdev of adapter in slot[%s]\n", 
149                                         __FUNCTION__, slot->dn->full_name);
150                                 *value = NOT_CONFIGURED;
151                         }
152                 }
153         }
154 exit:
155         return rc;
156 }
157
158 /* Must be called before pci_bus_add_devices */
159 static void 
160 rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
161 {
162         struct pci_dev *dev;
163
164         list_for_each_entry(dev, &bus->devices, bus_list) {
165                 /*
166                  * Skip already-present devices (which are on the
167                  * global device list.)
168                  */
169                 if (list_empty(&dev->global_list)) {
170                         int i;
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
186 static int rpaphp_pci_config_bridge(struct pci_dev *dev);
187
188 /*****************************************************************************
189  rpaphp_pci_config_slot() will  configure all devices under the 
190  given slot->dn and return the the first pci_dev.
191  *****************************************************************************/
192 static struct pci_dev *
193 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
194 {
195         struct device_node *eads_first_child = dn->child;
196         struct pci_dev *dev = NULL;
197         int num;
198         
199         dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
200
201         if (eads_first_child) {
202                 /* pci_scan_slot should find all children of EADs */
203                 num = pci_scan_slot(bus, PCI_DEVFN(PCI_SLOT(eads_first_child->devfn), 0));
204                 if (num) {
205                         rpaphp_fixup_new_pci_devices(bus, 1); 
206                         pci_bus_add_devices(bus);
207                 }
208                 dev = rpaphp_find_pci_dev(eads_first_child);
209                 if (!dev) {
210                         err("No new device found\n");
211                         return NULL;
212                 }
213                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 
214                         rpaphp_pci_config_bridge(dev);
215         }
216         return dev;
217 }
218
219 static int rpaphp_pci_config_bridge(struct pci_dev *dev)
220 {
221         u8 sec_busno;
222         struct pci_bus *child_bus;
223         struct pci_dev *child_dev;
224
225         dbg("Enter %s:  BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
226
227         /* get busno of downstream bus */
228         pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
229                 
230         /* add to children of PCI bridge dev->bus */
231         child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
232         if (!child_bus) {
233                 err("%s: could not add second bus\n", __FUNCTION__);
234                 return -EIO;
235         }
236         sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
237         /* do pci_scan_child_bus */
238         pci_scan_child_bus(child_bus);
239
240         list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
241                 eeh_add_device_late(child_dev);
242         }
243
244          /* fixup new pci devices without touching bus struct */
245         rpaphp_fixup_new_pci_devices(child_bus, 0);
246
247         /* Make the discovered devices available */
248         pci_bus_add_devices(child_bus);
249         return 0;
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 #ifdef DEBUG
264 static void print_slot_pci_funcs(struct slot *slot)
265 {
266         struct list_head *l;
267
268         if (slot->dev_type == PCI_DEV) {
269                 printk("pci_funcs of slot[%s]\n", slot->name);
270                 if (list_empty(&slot->dev.pci_funcs))
271                         printk("        pci_funcs is EMPTY\n");
272
273                 list_for_each (l, &slot->dev.pci_funcs) {
274                         struct rpaphp_pci_func *func =
275                                 list_entry(l, struct rpaphp_pci_func, sibling);
276                         printk("        FOUND dev=%s\n", pci_name(func->pci_dev));
277                 }
278         }
279         return;
280 }
281 #else
282 static void print_slot_pci_funcs(struct slot *slot)
283 {
284         return;
285 }
286 #endif
287
288 static int init_slot_pci_funcs(struct slot *slot)
289 {
290         struct device_node *child;
291
292         for (child = slot->dn->child; child != NULL; child = child->sibling) {
293                 struct pci_dev *pdev = rpaphp_find_pci_dev(child);
294
295                 if (pdev) {
296                         struct rpaphp_pci_func *func;
297                         func = kmalloc(sizeof(struct rpaphp_pci_func), GFP_KERNEL);
298                         if (!func) 
299                                 return -ENOMEM;
300                         memset(func, 0, sizeof(struct rpaphp_pci_func));
301                         INIT_LIST_HEAD(&func->sibling);
302                         func->pci_dev = pdev;
303                         list_add_tail(&func->sibling, &slot->dev.pci_funcs);
304                         print_slot_pci_funcs(slot);
305                 } else {
306                         err("%s: dn=%s has no pci_dev\n", 
307                                 __FUNCTION__, child->full_name);
308                         return -EIO;
309                 }
310         }
311         return 0;
312 }
313
314 static int rpaphp_config_pci_adapter(struct slot *slot)
315 {
316         struct pci_bus *pci_bus;
317         struct pci_dev *dev;
318         int rc = -ENODEV;
319
320         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
321
322         if (slot->bridge) {
323
324                 pci_bus = slot->bridge->subordinate;
325                 if (!pci_bus) {
326                         err("%s: can't find bus structure\n", __FUNCTION__);
327                         goto exit;
328                 }
329                 enable_eeh(slot->dn);
330                 dev = rpaphp_pci_config_slot(slot->dn, pci_bus);
331                 if (!dev) {
332                         err("%s: can't find any devices.\n", __FUNCTION__);
333                         goto exit;
334                 }
335                 /* associate corresponding pci_dev */   
336                 rc = init_slot_pci_funcs(slot);
337                 if (rc)
338                         goto exit;
339                 print_slot_pci_funcs(slot);
340                 if (!list_empty(&slot->dev.pci_funcs)) 
341                         rc = 0;
342         } else {
343                 /* slot is not enabled */
344                 err("slot doesn't have pci_dev structure\n");
345         }
346 exit:
347         dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
348         return rc;
349 }
350
351 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
352 {
353         eeh_remove_device(dev);
354         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
355                 struct pci_bus *bus = dev->subordinate;
356                 struct list_head *ln;
357                 if (!bus)
358                         return; 
359                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
360                         struct pci_dev *pdev = pci_dev_b(ln);
361                         if (pdev)
362                                 rpaphp_eeh_remove_bus_device(pdev);
363                 }
364
365         }
366         return;
367 }
368
369 int rpaphp_unconfig_pci_adapter(struct slot *slot)
370 {
371         int retval = 0;
372         struct list_head *ln, *tmp;
373
374         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
375         if (list_empty(&slot->dev.pci_funcs)) {
376                 err("%s: slot[%s] doesn't have any devices.\n", __FUNCTION__, 
377                         slot->name);
378
379                 retval = -EINVAL;
380                 goto exit;
381         }
382         /* remove the devices from the pci core */
383         list_for_each_safe (ln, tmp, &slot->dev.pci_funcs) {
384                 struct rpaphp_pci_func *func;
385         
386                 func = list_entry(ln, struct rpaphp_pci_func, sibling);
387                 if (func->pci_dev) {
388                         pci_remove_bus_device(func->pci_dev); 
389                         rpaphp_eeh_remove_bus_device(func->pci_dev);
390                 }
391                 kfree(func);
392         }
393         INIT_LIST_HEAD(&slot->dev.pci_funcs);
394         slot->state = NOT_CONFIGURED;
395         info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
396              slot->name);
397 exit:
398         dbg("Exit %s, rc=0x%x\n", __FUNCTION__, retval);
399         return retval;
400 }
401
402 static int setup_pci_hotplug_slot_info(struct slot *slot)
403 {
404         dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
405             __FUNCTION__);
406         rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
407         rpaphp_get_pci_adapter_status(slot, 1,
408                                       &slot->hotplug_slot->info->
409                                       adapter_status);
410         if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
411                 err("%s: NOT_VALID: skip dn->full_name=%s\n",
412                     __FUNCTION__, slot->dn->full_name);
413                 return -1;
414         }
415         return 0;
416 }
417
418 static int set_phb_slot_name(struct slot *slot)
419 {
420         struct device_node *dn;
421         struct pci_controller *phb;
422         struct pci_bus *bus;
423
424         dn = slot->dn;
425         if (!dn) {
426                 return 1;
427         }
428         phb = dn->phb;
429         if (!phb) {
430                 return 1;
431         }
432         bus = phb->bus;
433         if (!bus) {
434                 return 1;
435         }
436
437         sprintf(slot->name, "%04x:%02x:%02x.%x", pci_domain_nr(bus),
438                         bus->number, 0, 0);
439         return 0;
440 }
441
442 static int setup_pci_slot(struct slot *slot)
443 {
444         int rc;
445
446         if (slot->type == PHB) {
447                 rc = set_phb_slot_name(slot);
448                 if (rc) {
449                         err("%s: failed to set phb slot name\n", __FUNCTION__);
450                         goto exit_rc;
451                 }
452         } else {
453                 slot->bridge = rpaphp_find_bridge_pdev(slot);
454                 if (!slot->bridge) {
455                         /* slot being added doesn't have pci_dev yet */
456                         err("%s: no pci_dev for bridge dn %s\n",
457                                         __FUNCTION__, slot->name);
458                         goto exit_rc;
459                 }
460                 dbg("%s set slot->name to %s\n",  __FUNCTION__,
461                                 pci_name(slot->bridge));
462                 strcpy(slot->name, pci_name(slot->bridge));
463         }
464
465         /* find slot's pci_dev if it's not empty */
466         if (slot->hotplug_slot->info->adapter_status == EMPTY) {
467                 slot->state = EMPTY;    /* slot is empty */
468         } else {
469                 /* slot is occupied */
470                 if (!(slot->dn->child)) {
471                         /* non-empty slot has to have child */
472                         err("%s: slot[%s]'s device_node doesn't have child for adapter\n", 
473                                 __FUNCTION__, slot->name);
474                         goto exit_rc;
475                 }
476
477                 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
478                         dbg("%s CONFIGURING pci adapter in slot[%s]\n",  
479                                 __FUNCTION__, slot->name);
480                         if (rpaphp_config_pci_adapter(slot)) {
481                                 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
482                                 goto exit_rc;           
483                         }
484                 } else if (slot->hotplug_slot->info->adapter_status == CONFIGURED) {
485                         if (init_slot_pci_funcs(slot)) {
486                                 err("%s: init_slot_pci_funcs failed\n", __FUNCTION__);
487                                 goto exit_rc;
488                         }
489
490                 } else {
491                         err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
492                                 __FUNCTION__, slot->name);
493                         goto exit_rc;
494                 }
495                 
496                 print_slot_pci_funcs(slot);
497                 if (!list_empty(&slot->dev.pci_funcs)) {
498                         slot->state = CONFIGURED;
499         
500                 } else {
501                         /* DLPAR add as opposed to 
502                          * boot time */
503                         slot->state = NOT_CONFIGURED;
504                 }
505         }
506         return 0;
507 exit_rc:
508         dealloc_slot_struct(slot);
509         return 1;
510 }
511
512 int register_pci_slot(struct slot *slot)
513 {
514         int rc = 1;
515
516         slot->dev_type = PCI_DEV;
517         if ((slot->type == EMBEDDED) || (slot->type == PHB))
518                 slot->removable = 0;
519         else
520                 slot->removable = 1;
521         INIT_LIST_HEAD(&slot->dev.pci_funcs);
522         if (setup_pci_hotplug_slot_info(slot))
523                 goto exit_rc;
524         if (setup_pci_slot(slot))
525                 goto exit_rc;
526         rc = register_slot(slot);
527 exit_rc:
528         return rc;
529 }
530
531 int rpaphp_enable_pci_slot(struct slot *slot)
532 {
533         int retval = 0, state;
534
535         retval = rpaphp_get_sensor_state(slot, &state);
536         if (retval)
537                 goto exit;
538         dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
539         /* if slot is not empty, enable the adapter */
540         if (state == PRESENT) {
541                 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
542                 retval = rpaphp_config_pci_adapter(slot);
543                 if (!retval) {
544                         slot->state = CONFIGURED;
545                         dbg("%s: PCI devices in slot[%s] has been configured\n", 
546                                 __FUNCTION__, slot->name);
547                 } else {
548                         slot->state = NOT_CONFIGURED;
549                         dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
550                             __FUNCTION__, slot->name);
551                 }
552         } else if (state == EMPTY) {
553                 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
554                 slot->state = EMPTY;
555         } else {
556                 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
557                     slot->name);
558                 slot->state = NOT_VALID;
559                 retval = -EINVAL;
560         }
561 exit:
562         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
563         return retval;
564 }
565
566 struct hotplug_slot *rpaphp_find_hotplug_slot(struct pci_dev *dev)
567 {
568         struct list_head        *tmp, *n;
569         struct slot             *slot;
570
571         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
572                 struct pci_bus *bus;
573                 struct list_head *ln;
574
575                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
576                 if (slot->bridge == NULL) {
577                         if (slot->dev_type == PCI_DEV) {
578                                 printk(KERN_WARNING "PCI slot missing bridge %s %s \n", 
579                                                     slot->name, slot->location);
580                         }
581                         continue;
582                 }
583
584                 bus = slot->bridge->subordinate;
585                 if (!bus) {
586                         continue;  /* should never happen? */
587                 }
588                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
589                                 struct pci_dev *pdev = pci_dev_b(ln);
590                                 if (pdev == dev)
591                                         return slot->hotplug_slot;
592                 }
593         }
594
595         return NULL;
596 }
597
598 EXPORT_SYMBOL_GPL(rpaphp_find_hotplug_slot);