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