vserver 1.9.5.x5
[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 <asm/machdep.h>
29 #include "../pci.h"             /* for pci_add_new_bus */
30
31 #include "rpaphp.h"
32
33 struct 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
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 = NULL;
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                         /* Need to setup IOMMU tables */
173                         ppc_md.iommu_dev_setup(dev);
174
175                         if(fix_bus)
176                                 pcibios_fixup_device_resources(dev, bus);
177                         pci_read_irq_line(dev);
178                         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
179                                 struct resource *r = &dev->resource[i];
180
181                                 if (r->parent || !r->start || !r->flags)
182                                         continue;
183                                 rpaphp_claim_resource(dev, i);
184                         }
185                 }
186         }
187 }
188
189 static int rpaphp_pci_config_bridge(struct pci_dev *dev);
190
191 /*****************************************************************************
192  rpaphp_pci_config_slot() will  configure all devices under the 
193  given slot->dn and return the the first pci_dev.
194  *****************************************************************************/
195 static struct pci_dev *
196 rpaphp_pci_config_slot(struct device_node *dn, struct pci_bus *bus)
197 {
198         struct device_node *eads_first_child = dn->child;
199         struct pci_dev *dev = NULL;
200         int num;
201         
202         dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
203
204         if (eads_first_child) {
205                 /* pci_scan_slot should find all children of EADs */
206                 num = pci_scan_slot(bus, PCI_DEVFN(PCI_SLOT(eads_first_child->devfn), 0));
207                 if (num) {
208                         rpaphp_fixup_new_pci_devices(bus, 1); 
209                         pci_bus_add_devices(bus);
210                 }
211                 dev = rpaphp_find_pci_dev(eads_first_child);
212                 if (!dev) {
213                         err("No new device found\n");
214                         return NULL;
215                 }
216                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 
217                         rpaphp_pci_config_bridge(dev);
218         }
219         return dev;
220 }
221
222 static int rpaphp_pci_config_bridge(struct pci_dev *dev)
223 {
224         u8 sec_busno;
225         struct pci_bus *child_bus;
226         struct pci_dev *child_dev;
227
228         dbg("Enter %s:  BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));
229
230         /* get busno of downstream bus */
231         pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
232                 
233         /* add to children of PCI bridge dev->bus */
234         child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
235         if (!child_bus) {
236                 err("%s: could not add second bus\n", __FUNCTION__);
237                 return -EIO;
238         }
239         sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
240         /* do pci_scan_child_bus */
241         pci_scan_child_bus(child_bus);
242
243         list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
244                 eeh_add_device_late(child_dev);
245         }
246
247          /* fixup new pci devices without touching bus struct */
248         rpaphp_fixup_new_pci_devices(child_bus, 0);
249
250         /* Make the discovered devices available */
251         pci_bus_add_devices(child_bus);
252         return 0;
253 }
254
255 static void enable_eeh(struct device_node *dn)
256 {
257         struct device_node *sib;
258
259         for (sib = dn->child; sib; sib = sib->sibling) 
260                 enable_eeh(sib);
261         eeh_add_device_early(dn);
262         return;
263         
264 }
265
266 #ifdef DEBUG
267 static void print_slot_pci_funcs(struct slot *slot)
268 {
269         struct list_head *l;
270
271         if (slot->dev_type == PCI_DEV) {
272                 printk("pci_funcs of slot[%s]\n", slot->name);
273                 if (list_empty(&slot->dev.pci_funcs))
274                         printk("        pci_funcs is EMPTY\n");
275
276                 list_for_each (l, &slot->dev.pci_funcs) {
277                         struct rpaphp_pci_func *func =
278                                 list_entry(l, struct rpaphp_pci_func, sibling);
279                         printk("        FOUND dev=%s\n", pci_name(func->pci_dev));
280                 }
281         }
282         return;
283 }
284 #else
285 static void print_slot_pci_funcs(struct slot *slot)
286 {
287         return;
288 }
289 #endif
290
291 static int init_slot_pci_funcs(struct slot *slot)
292 {
293         struct device_node *child;
294
295         for (child = slot->dn->child; child != NULL; child = child->sibling) {
296                 struct pci_dev *pdev = rpaphp_find_pci_dev(child);
297
298                 if (pdev) {
299                         struct rpaphp_pci_func *func;
300                         func = kmalloc(sizeof(struct rpaphp_pci_func), GFP_KERNEL);
301                         if (!func) 
302                                 return -ENOMEM;
303                         memset(func, 0, sizeof(struct rpaphp_pci_func));
304                         INIT_LIST_HEAD(&func->sibling);
305                         func->pci_dev = pdev;
306                         list_add_tail(&func->sibling, &slot->dev.pci_funcs);
307                         print_slot_pci_funcs(slot);
308                 } else {
309                         err("%s: dn=%s has no pci_dev\n", 
310                                 __FUNCTION__, child->full_name);
311                         return -EIO;
312                 }
313         }
314         return 0;
315 }
316
317 static int rpaphp_config_pci_adapter(struct slot *slot)
318 {
319         struct pci_bus *pci_bus;
320         struct pci_dev *dev;
321         int rc = -ENODEV;
322
323         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
324
325         if (slot->bridge) {
326
327                 pci_bus = slot->bridge->subordinate;
328                 if (!pci_bus) {
329                         err("%s: can't find bus structure\n", __FUNCTION__);
330                         goto exit;
331                 }
332                 enable_eeh(slot->dn);
333                 dev = rpaphp_pci_config_slot(slot->dn, pci_bus);
334                 if (!dev) {
335                         err("%s: can't find any devices.\n", __FUNCTION__);
336                         goto exit;
337                 }
338                 /* associate corresponding pci_dev */   
339                 rc = init_slot_pci_funcs(slot);
340                 if (rc)
341                         goto exit;
342                 print_slot_pci_funcs(slot);
343                 if (!list_empty(&slot->dev.pci_funcs)) 
344                         rc = 0;
345         } else {
346                 /* slot is not enabled */
347                 err("slot doesn't have pci_dev structure\n");
348         }
349 exit:
350         dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
351         return rc;
352 }
353
354 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
355 {
356         eeh_remove_device(dev);
357         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
358                 struct pci_bus *bus = dev->subordinate;
359                 struct list_head *ln;
360                 if (!bus)
361                         return; 
362                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
363                         struct pci_dev *pdev = pci_dev_b(ln);
364                         if (pdev)
365                                 rpaphp_eeh_remove_bus_device(pdev);
366                 }
367
368         }
369         return;
370 }
371
372 int rpaphp_unconfig_pci_adapter(struct slot *slot)
373 {
374         int retval = 0;
375         struct list_head *ln, *tmp;
376
377         dbg("Entry %s: slot[%s]\n", __FUNCTION__, slot->name);
378         if (list_empty(&slot->dev.pci_funcs)) {
379                 err("%s: slot[%s] doesn't have any devices.\n", __FUNCTION__, 
380                         slot->name);
381
382                 retval = -EINVAL;
383                 goto exit;
384         }
385         /* remove the devices from the pci core */
386         list_for_each_safe (ln, tmp, &slot->dev.pci_funcs) {
387                 struct rpaphp_pci_func *func;
388         
389                 func = list_entry(ln, struct rpaphp_pci_func, sibling);
390                 if (func->pci_dev) {
391                         pci_remove_bus_device(func->pci_dev); 
392                         rpaphp_eeh_remove_bus_device(func->pci_dev);
393                 }
394                 kfree(func);
395         }
396         INIT_LIST_HEAD(&slot->dev.pci_funcs);
397         slot->state = NOT_CONFIGURED;
398         info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
399              slot->name);
400 exit:
401         dbg("Exit %s, rc=0x%x\n", __FUNCTION__, retval);
402         return retval;
403 }
404
405 static int setup_pci_hotplug_slot_info(struct slot *slot)
406 {
407         dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
408             __FUNCTION__);
409         rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
410         rpaphp_get_pci_adapter_status(slot, 1,
411                                       &slot->hotplug_slot->info->
412                                       adapter_status);
413         if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
414                 err("%s: NOT_VALID: skip dn->full_name=%s\n",
415                     __FUNCTION__, slot->dn->full_name);
416                 return -1;
417         }
418         return 0;
419 }
420
421 static int set_phb_slot_name(struct slot *slot)
422 {
423         struct device_node *dn;
424         struct pci_controller *phb;
425         struct pci_bus *bus;
426
427         dn = slot->dn;
428         if (!dn) {
429                 return 1;
430         }
431         phb = dn->phb;
432         if (!phb) {
433                 return 1;
434         }
435         bus = phb->bus;
436         if (!bus) {
437                 return 1;
438         }
439
440         sprintf(slot->name, "%04x:%02x:%02x.%x", pci_domain_nr(bus),
441                         bus->number, 0, 0);
442         return 0;
443 }
444
445 static int setup_pci_slot(struct slot *slot)
446 {
447         int rc;
448
449         if (slot->type == PHB) {
450                 rc = set_phb_slot_name(slot);
451                 if (rc) {
452                         err("%s: failed to set phb slot name\n", __FUNCTION__);
453                         goto exit_rc;
454                 }
455         } else {
456                 slot->bridge = rpaphp_find_bridge_pdev(slot);
457                 if (!slot->bridge) {
458                         /* slot being added doesn't have pci_dev yet */
459                         err("%s: no pci_dev for bridge dn %s\n",
460                                         __FUNCTION__, slot->name);
461                         goto exit_rc;
462                 }
463                 dbg("%s set slot->name to %s\n",  __FUNCTION__,
464                                 pci_name(slot->bridge));
465                 strcpy(slot->name, pci_name(slot->bridge));
466         }
467
468         /* find slot's pci_dev if it's not empty */
469         if (slot->hotplug_slot->info->adapter_status == EMPTY) {
470                 slot->state = EMPTY;    /* slot is empty */
471         } else {
472                 /* slot is occupied */
473                 if (!(slot->dn->child)) {
474                         /* non-empty slot has to have child */
475                         err("%s: slot[%s]'s device_node doesn't have child for adapter\n", 
476                                 __FUNCTION__, slot->name);
477                         goto exit_rc;
478                 }
479
480                 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
481                         dbg("%s CONFIGURING pci adapter in slot[%s]\n",  
482                                 __FUNCTION__, slot->name);
483                         if (rpaphp_config_pci_adapter(slot)) {
484                                 err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
485                                 goto exit_rc;           
486                         }
487                 } else if (slot->hotplug_slot->info->adapter_status == CONFIGURED) {
488                         if (init_slot_pci_funcs(slot)) {
489                                 err("%s: init_slot_pci_funcs failed\n", __FUNCTION__);
490                                 goto exit_rc;
491                         }
492
493                 } else {
494                         err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
495                                 __FUNCTION__, slot->name);
496                         goto exit_rc;
497                 }
498                 
499                 print_slot_pci_funcs(slot);
500                 if (!list_empty(&slot->dev.pci_funcs)) {
501                         slot->state = CONFIGURED;
502         
503                 } else {
504                         /* DLPAR add as opposed to 
505                          * boot time */
506                         slot->state = NOT_CONFIGURED;
507                 }
508         }
509         return 0;
510 exit_rc:
511         dealloc_slot_struct(slot);
512         return 1;
513 }
514
515 int register_pci_slot(struct slot *slot)
516 {
517         int rc = 1;
518
519         slot->dev_type = PCI_DEV;
520         if ((slot->type == EMBEDDED) || (slot->type == PHB))
521                 slot->removable = 0;
522         else
523                 slot->removable = 1;
524         INIT_LIST_HEAD(&slot->dev.pci_funcs);
525         if (setup_pci_hotplug_slot_info(slot))
526                 goto exit_rc;
527         if (setup_pci_slot(slot))
528                 goto exit_rc;
529         rc = register_slot(slot);
530 exit_rc:
531         return rc;
532 }
533
534 int rpaphp_enable_pci_slot(struct slot *slot)
535 {
536         int retval = 0, state;
537
538         retval = rpaphp_get_sensor_state(slot, &state);
539         if (retval)
540                 goto exit;
541         dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
542         /* if slot is not empty, enable the adapter */
543         if (state == PRESENT) {
544                 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
545                 retval = rpaphp_config_pci_adapter(slot);
546                 if (!retval) {
547                         slot->state = CONFIGURED;
548                         dbg("%s: PCI devices in slot[%s] has been configured\n", 
549                                 __FUNCTION__, slot->name);
550                 } else {
551                         slot->state = NOT_CONFIGURED;
552                         dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
553                             __FUNCTION__, slot->name);
554                 }
555         } else if (state == EMPTY) {
556                 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
557                 slot->state = EMPTY;
558         } else {
559                 err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
560                     slot->name);
561                 slot->state = NOT_VALID;
562                 retval = -EINVAL;
563         }
564 exit:
565         dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
566         return retval;
567 }
568
569 struct hotplug_slot *rpaphp_find_hotplug_slot(struct pci_dev *dev)
570 {
571         struct list_head        *tmp, *n;
572         struct slot             *slot;
573
574         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
575                 struct pci_bus *bus;
576                 struct list_head *ln;
577
578                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
579                 if (slot->bridge == NULL) {
580                         if (slot->dev_type == PCI_DEV) {
581                                 printk(KERN_WARNING "PCI slot missing bridge %s %s \n", 
582                                                     slot->name, slot->location);
583                         }
584                         continue;
585                 }
586
587                 bus = slot->bridge->subordinate;
588                 if (!bus) {
589                         continue;  /* should never happen? */
590                 }
591                 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
592                                 struct pci_dev *pdev = pci_dev_b(ln);
593                                 if (pdev == dev)
594                                         return slot->hotplug_slot;
595                 }
596         }
597
598         return NULL;
599 }
600
601 EXPORT_SYMBOL_GPL(rpaphp_find_hotplug_slot);