Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[linux-2.6] / drivers / pci / hotplug / acpiphp_glue.c
1 /*
2  * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3  *
4  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6  * Copyright (C) 2002,2003 NEC Corporation
7  * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8  * Copyright (C) 2003-2005 Hewlett Packard
9  * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10  * Copyright (C) 2005 Intel Corporation
11  *
12  * All rights reserved.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or (at
17  * your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22  * NON INFRINGEMENT.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  * Send feedback to <kristen.c.accardi@intel.com>
30  *
31  */
32
33 /*
34  * Lifetime rules for pci_dev:
35  *  - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36  *    when the driver is loaded or when an insertion event occurs.  It loses
37  *    a refcount when its ejected or the driver unloads.
38  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39  *    when the bridge is scanned and it loses a refcount when the bridge
40  *    is removed.
41  */
42
43 #include <linux/init.h>
44 #include <linux/module.h>
45
46 #include <linux/kernel.h>
47 #include <linux/pci.h>
48 #include <linux/pci_hotplug.h>
49 #include <linux/mutex.h>
50
51 #include "../pci.h"
52 #include "acpiphp.h"
53
54 static LIST_HEAD(bridge_list);
55 static LIST_HEAD(ioapic_list);
56 static DEFINE_SPINLOCK(ioapic_list_lock);
57
58 #define MY_NAME "acpiphp_glue"
59
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
64
65
66 /*
67  * initialization & terminatation routines
68  */
69
70 /**
71  * is_ejectable - determine if a slot is ejectable
72  * @handle: handle to acpi namespace
73  *
74  * Ejectable slot should satisfy at least these conditions:
75  *
76  *  1. has _ADR method
77  *  2. has _EJ0 method
78  *
79  * optionally
80  *
81  *  1. has _STA method
82  *  2. has _PS0 method
83  *  3. has _PS3 method
84  *  4. ..
85  */
86 static int is_ejectable(acpi_handle handle)
87 {
88         acpi_status status;
89         acpi_handle tmp;
90
91         status = acpi_get_handle(handle, "_ADR", &tmp);
92         if (ACPI_FAILURE(status)) {
93                 return 0;
94         }
95
96         status = acpi_get_handle(handle, "_EJ0", &tmp);
97         if (ACPI_FAILURE(status)) {
98                 return 0;
99         }
100
101         return 1;
102 }
103
104
105 /* callback routine to check for the existence of ejectable slots */
106 static acpi_status
107 is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108 {
109         int *count = (int *)context;
110
111         if (is_ejectable(handle)) {
112                 (*count)++;
113                 /* only one ejectable slot is enough */
114                 return AE_CTRL_TERMINATE;
115         } else {
116                 return AE_OK;
117         }
118 }
119
120 /* callback routine to check for the existence of a pci dock device */
121 static acpi_status
122 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
123 {
124         int *count = (int *)context;
125
126         if (is_dock_device(handle)) {
127                 (*count)++;
128                 return AE_CTRL_TERMINATE;
129         } else {
130                 return AE_OK;
131         }
132 }
133
134
135
136
137 /*
138  * the _DCK method can do funny things... and sometimes not
139  * hah-hah funny.
140  *
141  * TBD - figure out a way to only call fixups for
142  * systems that require them.
143  */
144 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145         void *v)
146 {
147         struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
148         struct pci_bus *bus = func->slot->bridge->pci_bus;
149         u32 buses;
150
151         if (!bus->self)
152                 return  NOTIFY_OK;
153
154         /* fixup bad _DCK function that rewrites
155          * secondary bridge on slot
156          */
157         pci_read_config_dword(bus->self,
158                         PCI_PRIMARY_BUS,
159                         &buses);
160
161         if (((buses >> 8) & 0xff) != bus->secondary) {
162                 buses = (buses & 0xff000000)
163                         | ((unsigned int)(bus->primary)     <<  0)
164                         | ((unsigned int)(bus->secondary)   <<  8)
165                         | ((unsigned int)(bus->subordinate) << 16);
166                 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
167         }
168         return NOTIFY_OK;
169 }
170
171
172 static struct acpi_dock_ops acpiphp_dock_ops = {
173         .handler = handle_hotplug_event_func,
174 };
175
176 /* callback routine to register each ACPI PCI slot object */
177 static acpi_status
178 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
179 {
180         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
181         struct acpiphp_slot *slot;
182         struct acpiphp_func *newfunc;
183         acpi_handle tmp;
184         acpi_status status = AE_OK;
185         unsigned long long adr, sun;
186         int device, function, retval;
187
188         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
189
190         if (ACPI_FAILURE(status))
191                 return AE_OK;
192
193         status = acpi_get_handle(handle, "_EJ0", &tmp);
194
195         if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
196                 return AE_OK;
197
198         device = (adr >> 16) & 0xffff;
199         function = adr & 0xffff;
200
201         newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
202         if (!newfunc)
203                 return AE_NO_MEMORY;
204
205         INIT_LIST_HEAD(&newfunc->sibling);
206         newfunc->handle = handle;
207         newfunc->function = function;
208         if (ACPI_SUCCESS(status))
209                 newfunc->flags = FUNC_HAS_EJ0;
210
211         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
212                 newfunc->flags |= FUNC_HAS_STA;
213
214         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
215                 newfunc->flags |= FUNC_HAS_PS0;
216
217         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
218                 newfunc->flags |= FUNC_HAS_PS3;
219
220         if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
221                 newfunc->flags |= FUNC_HAS_DCK;
222
223         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
224         if (ACPI_FAILURE(status)) {
225                 /*
226                  * use the count of the number of slots we've found
227                  * for the number of the slot
228                  */
229                 sun = bridge->nr_slots+1;
230         }
231
232         /* search for objects that share the same slot */
233         for (slot = bridge->slots; slot; slot = slot->next)
234                 if (slot->device == device) {
235                         if (slot->sun != sun)
236                                 warn("sibling found, but _SUN doesn't match!\n");
237                         break;
238                 }
239
240         if (!slot) {
241                 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
242                 if (!slot) {
243                         kfree(newfunc);
244                         return AE_NO_MEMORY;
245                 }
246
247                 slot->bridge = bridge;
248                 slot->device = device;
249                 slot->sun = sun;
250                 INIT_LIST_HEAD(&slot->funcs);
251                 mutex_init(&slot->crit_sect);
252
253                 slot->next = bridge->slots;
254                 bridge->slots = slot;
255
256                 bridge->nr_slots++;
257
258                 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
259                                 slot->sun, pci_domain_nr(bridge->pci_bus),
260                                 bridge->pci_bus->number, slot->device);
261                 retval = acpiphp_register_hotplug_slot(slot);
262                 if (retval) {
263                         if (retval == -EBUSY)
264                                 warn("Slot %llu already registered by another "
265                                         "hotplug driver\n", slot->sun);
266                         else
267                                 warn("acpiphp_register_hotplug_slot failed "
268                                         "(err code = 0x%x)\n", retval);
269                         goto err_exit;
270                 }
271         }
272
273         newfunc->slot = slot;
274         list_add_tail(&newfunc->sibling, &slot->funcs);
275
276         /* associate corresponding pci_dev */
277         newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
278                                          PCI_DEVFN(device, function));
279         if (newfunc->pci_dev) {
280                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
281         }
282
283         if (is_dock_device(handle)) {
284                 /* we don't want to call this device's _EJ0
285                  * because we want the dock notify handler
286                  * to call it after it calls _DCK
287                  */
288                 newfunc->flags &= ~FUNC_HAS_EJ0;
289                 if (register_hotplug_dock_device(handle,
290                         &acpiphp_dock_ops, newfunc))
291                         dbg("failed to register dock device\n");
292
293                 /* we need to be notified when dock events happen
294                  * outside of the hotplug operation, since we may
295                  * need to do fixups before we can hotplug.
296                  */
297                 newfunc->nb.notifier_call = post_dock_fixups;
298                 if (register_dock_notifier(&newfunc->nb))
299                         dbg("failed to register a dock notifier");
300         }
301
302         /* install notify handler */
303         if (!(newfunc->flags & FUNC_HAS_DCK)) {
304                 status = acpi_install_notify_handler(handle,
305                                              ACPI_SYSTEM_NOTIFY,
306                                              handle_hotplug_event_func,
307                                              newfunc);
308
309                 if (ACPI_FAILURE(status))
310                         err("failed to register interrupt notify handler\n");
311         } else
312                 status = AE_OK;
313
314         return status;
315
316  err_exit:
317         bridge->nr_slots--;
318         bridge->slots = slot->next;
319         kfree(slot);
320         kfree(newfunc);
321
322         return AE_OK;
323 }
324
325
326 /* see if it's worth looking at this bridge */
327 static int detect_ejectable_slots(acpi_handle *bridge_handle)
328 {
329         acpi_status status;
330         int count;
331
332         count = 0;
333
334         /* only check slots defined directly below bridge object */
335         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
336                                      is_ejectable_slot, (void *)&count, NULL);
337
338         /*
339          * we also need to add this bridge if there is a dock bridge or
340          * other pci device on a dock station (removable)
341          */
342         if (!count)
343                 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
344                                 (u32)1, is_pci_dock_device, (void *)&count,
345                                 NULL);
346
347         return count;
348 }
349
350
351 /* decode ACPI 2.0 _HPP hot plug parameters */
352 static void decode_hpp(struct acpiphp_bridge *bridge)
353 {
354         acpi_status status;
355
356         status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
357         if (ACPI_FAILURE(status) ||
358             !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
359                 /* use default numbers */
360                 printk(KERN_WARNING
361                        "%s: Could not get hotplug parameters. Use defaults\n",
362                        __func__);
363                 bridge->hpp.t0 = &bridge->hpp.type0_data;
364                 bridge->hpp.t0->revision = 0;
365                 bridge->hpp.t0->cache_line_size = 0x10;
366                 bridge->hpp.t0->latency_timer = 0x40;
367                 bridge->hpp.t0->enable_serr = 0;
368                 bridge->hpp.t0->enable_perr = 0;
369         }
370 }
371
372
373
374 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
375 static void init_bridge_misc(struct acpiphp_bridge *bridge)
376 {
377         acpi_status status;
378
379         /* decode ACPI 2.0 _HPP (hot plug parameters) */
380         decode_hpp(bridge);
381
382         /* must be added to the list prior to calling register_slot */
383         list_add(&bridge->list, &bridge_list);
384
385         /* register all slot objects under this bridge */
386         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
387                                      register_slot, bridge, NULL);
388         if (ACPI_FAILURE(status)) {
389                 list_del(&bridge->list);
390                 return;
391         }
392
393         /* install notify handler */
394         if (bridge->type != BRIDGE_TYPE_HOST) {
395                 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
396                         status = acpi_remove_notify_handler(bridge->func->handle,
397                                                 ACPI_SYSTEM_NOTIFY,
398                                                 handle_hotplug_event_func);
399                         if (ACPI_FAILURE(status))
400                                 err("failed to remove notify handler\n");
401                 }
402                 status = acpi_install_notify_handler(bridge->handle,
403                                              ACPI_SYSTEM_NOTIFY,
404                                              handle_hotplug_event_bridge,
405                                              bridge);
406
407                 if (ACPI_FAILURE(status)) {
408                         err("failed to register interrupt notify handler\n");
409                 }
410         }
411 }
412
413
414 /* find acpiphp_func from acpiphp_bridge */
415 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
416 {
417         struct list_head *node, *l;
418         struct acpiphp_bridge *bridge;
419         struct acpiphp_slot *slot;
420         struct acpiphp_func *func;
421
422         list_for_each(node, &bridge_list) {
423                 bridge = list_entry(node, struct acpiphp_bridge, list);
424                 for (slot = bridge->slots; slot; slot = slot->next) {
425                         list_for_each(l, &slot->funcs) {
426                                 func = list_entry(l, struct acpiphp_func,
427                                                         sibling);
428                                 if (func->handle == handle)
429                                         return func;
430                         }
431                 }
432         }
433
434         return NULL;
435 }
436
437
438 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
439 {
440         acpi_handle dummy_handle;
441
442         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
443                                         "_STA", &dummy_handle)))
444                 bridge->flags |= BRIDGE_HAS_STA;
445
446         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
447                                         "_EJ0", &dummy_handle)))
448                 bridge->flags |= BRIDGE_HAS_EJ0;
449
450         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
451                                         "_PS0", &dummy_handle)))
452                 bridge->flags |= BRIDGE_HAS_PS0;
453
454         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
455                                         "_PS3", &dummy_handle)))
456                 bridge->flags |= BRIDGE_HAS_PS3;
457
458         /* is this ejectable p2p bridge? */
459         if (bridge->flags & BRIDGE_HAS_EJ0) {
460                 struct acpiphp_func *func;
461
462                 dbg("found ejectable p2p bridge\n");
463
464                 /* make link between PCI bridge and PCI function */
465                 func = acpiphp_bridge_handle_to_function(bridge->handle);
466                 if (!func)
467                         return;
468                 bridge->func = func;
469                 func->bridge = bridge;
470         }
471 }
472
473
474 /* allocate and initialize host bridge data structure */
475 static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
476 {
477         struct acpiphp_bridge *bridge;
478
479         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
480         if (bridge == NULL)
481                 return;
482
483         bridge->type = BRIDGE_TYPE_HOST;
484         bridge->handle = handle;
485
486         bridge->pci_bus = pci_bus;
487
488         spin_lock_init(&bridge->res_lock);
489
490         init_bridge_misc(bridge);
491 }
492
493
494 /* allocate and initialize PCI-to-PCI bridge data structure */
495 static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
496 {
497         struct acpiphp_bridge *bridge;
498
499         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
500         if (bridge == NULL) {
501                 err("out of memory\n");
502                 return;
503         }
504
505         bridge->type = BRIDGE_TYPE_P2P;
506         bridge->handle = handle;
507         config_p2p_bridge_flags(bridge);
508
509         bridge->pci_dev = pci_dev_get(pci_dev);
510         bridge->pci_bus = pci_dev->subordinate;
511         if (!bridge->pci_bus) {
512                 err("This is not a PCI-to-PCI bridge!\n");
513                 goto err;
514         }
515
516         spin_lock_init(&bridge->res_lock);
517
518         init_bridge_misc(bridge);
519         return;
520  err:
521         pci_dev_put(pci_dev);
522         kfree(bridge);
523         return;
524 }
525
526
527 /* callback routine to find P2P bridges */
528 static acpi_status
529 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
530 {
531         acpi_status status;
532         acpi_handle dummy_handle;
533         unsigned long long tmp;
534         int device, function;
535         struct pci_dev *dev;
536         struct pci_bus *pci_bus = context;
537
538         status = acpi_get_handle(handle, "_ADR", &dummy_handle);
539         if (ACPI_FAILURE(status))
540                 return AE_OK;           /* continue */
541
542         status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
543         if (ACPI_FAILURE(status)) {
544                 dbg("%s: _ADR evaluation failure\n", __func__);
545                 return AE_OK;
546         }
547
548         device = (tmp >> 16) & 0xffff;
549         function = tmp & 0xffff;
550
551         dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
552
553         if (!dev || !dev->subordinate)
554                 goto out;
555
556         /* check if this bridge has ejectable slots */
557         if ((detect_ejectable_slots(handle) > 0)) {
558                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
559                 add_p2p_bridge(handle, dev);
560         }
561
562         /* search P2P bridges under this p2p bridge */
563         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
564                                      find_p2p_bridge, dev->subordinate, NULL);
565         if (ACPI_FAILURE(status))
566                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
567
568  out:
569         pci_dev_put(dev);
570         return AE_OK;
571 }
572
573
574 /* find hot-pluggable slots, and then find P2P bridge */
575 static int add_bridge(acpi_handle handle)
576 {
577         acpi_status status;
578         unsigned long long tmp;
579         int seg, bus;
580         acpi_handle dummy_handle;
581         struct pci_bus *pci_bus;
582
583         /* if the bridge doesn't have _STA, we assume it is always there */
584         status = acpi_get_handle(handle, "_STA", &dummy_handle);
585         if (ACPI_SUCCESS(status)) {
586                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
587                 if (ACPI_FAILURE(status)) {
588                         dbg("%s: _STA evaluation failure\n", __func__);
589                         return 0;
590                 }
591                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
592                         /* don't register this object */
593                         return 0;
594         }
595
596         /* get PCI segment number */
597         status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
598
599         seg = ACPI_SUCCESS(status) ? tmp : 0;
600
601         /* get PCI bus number */
602         status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
603
604         if (ACPI_SUCCESS(status)) {
605                 bus = tmp;
606         } else {
607                 warn("can't get bus number, assuming 0\n");
608                 bus = 0;
609         }
610
611         pci_bus = pci_find_bus(seg, bus);
612         if (!pci_bus) {
613                 err("Can't find bus %04x:%02x\n", seg, bus);
614                 return 0;
615         }
616
617         /* check if this bridge has ejectable slots */
618         if (detect_ejectable_slots(handle) > 0) {
619                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
620                 add_host_bridge(handle, pci_bus);
621         }
622
623         /* search P2P bridges under this host bridge */
624         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
625                                      find_p2p_bridge, pci_bus, NULL);
626
627         if (ACPI_FAILURE(status))
628                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
629
630         return 0;
631 }
632
633 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
634 {
635         struct list_head *head;
636         list_for_each(head, &bridge_list) {
637                 struct acpiphp_bridge *bridge = list_entry(head,
638                                                 struct acpiphp_bridge, list);
639                 if (bridge->handle == handle)
640                         return bridge;
641         }
642
643         return NULL;
644 }
645
646 static void cleanup_bridge(struct acpiphp_bridge *bridge)
647 {
648         struct list_head *list, *tmp;
649         struct acpiphp_slot *slot;
650         acpi_status status;
651         acpi_handle handle = bridge->handle;
652
653         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
654                                             handle_hotplug_event_bridge);
655         if (ACPI_FAILURE(status))
656                 err("failed to remove notify handler\n");
657
658         if ((bridge->type != BRIDGE_TYPE_HOST) &&
659             ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
660                 status = acpi_install_notify_handler(bridge->func->handle,
661                                                 ACPI_SYSTEM_NOTIFY,
662                                                 handle_hotplug_event_func,
663                                                 bridge->func);
664                 if (ACPI_FAILURE(status))
665                         err("failed to install interrupt notify handler\n");
666         }
667
668         slot = bridge->slots;
669         while (slot) {
670                 struct acpiphp_slot *next = slot->next;
671                 list_for_each_safe (list, tmp, &slot->funcs) {
672                         struct acpiphp_func *func;
673                         func = list_entry(list, struct acpiphp_func, sibling);
674                         if (is_dock_device(func->handle)) {
675                                 unregister_hotplug_dock_device(func->handle);
676                                 unregister_dock_notifier(&func->nb);
677                         }
678                         if (!(func->flags & FUNC_HAS_DCK)) {
679                                 status = acpi_remove_notify_handler(func->handle,
680                                                 ACPI_SYSTEM_NOTIFY,
681                                                 handle_hotplug_event_func);
682                                 if (ACPI_FAILURE(status))
683                                         err("failed to remove notify handler\n");
684                         }
685                         pci_dev_put(func->pci_dev);
686                         list_del(list);
687                         kfree(func);
688                 }
689                 acpiphp_unregister_hotplug_slot(slot);
690                 list_del(&slot->funcs);
691                 kfree(slot);
692                 slot = next;
693         }
694
695         pci_dev_put(bridge->pci_dev);
696         list_del(&bridge->list);
697         kfree(bridge);
698 }
699
700 static acpi_status
701 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
702 {
703         struct acpiphp_bridge *bridge;
704
705         /* cleanup p2p bridges under this P2P bridge
706            in a depth-first manner */
707         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
708                                 cleanup_p2p_bridge, NULL, NULL);
709
710         bridge = acpiphp_handle_to_bridge(handle);
711         if (bridge)
712                 cleanup_bridge(bridge);
713
714         return AE_OK;
715 }
716
717 static void remove_bridge(acpi_handle handle)
718 {
719         struct acpiphp_bridge *bridge;
720
721         /* cleanup p2p bridges under this host bridge
722            in a depth-first manner */
723         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
724                                 (u32)1, cleanup_p2p_bridge, NULL, NULL);
725
726         /*
727          * On root bridges with hotplug slots directly underneath (ie,
728          * no p2p bridge inbetween), we call cleanup_bridge(). 
729          *
730          * The else clause cleans up root bridges that either had no
731          * hotplug slots at all, or had a p2p bridge underneath.
732          */
733         bridge = acpiphp_handle_to_bridge(handle);
734         if (bridge)
735                 cleanup_bridge(bridge);
736         else
737                 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
738                                            handle_hotplug_event_bridge);
739 }
740
741 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
742 {
743         struct acpi_pci_id id;
744         struct pci_bus *bus;
745         struct pci_dev *dev;
746
747         if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
748                 return NULL;
749
750         bus = pci_find_bus(id.segment, id.bus);
751         if (!bus)
752                 return NULL;
753
754         dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
755         if (!dev)
756                 return NULL;
757
758         if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
759             (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
760         {
761                 pci_dev_put(dev);
762                 return NULL;
763         }
764
765         return dev;
766 }
767
768 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
769 {
770         acpi_status status;
771         int result = -1;
772         unsigned long long gsb;
773         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
774         union acpi_object *obj;
775         void *table;
776
777         status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
778         if (ACPI_SUCCESS(status)) {
779                 *gsi_base = (u32)gsb;
780                 return 0;
781         }
782
783         status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
784         if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
785                 return -1;
786
787         obj = buffer.pointer;
788         if (obj->type != ACPI_TYPE_BUFFER)
789                 goto out;
790
791         table = obj->buffer.pointer;
792         switch (((struct acpi_subtable_header *)table)->type) {
793         case ACPI_MADT_TYPE_IO_SAPIC:
794                 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
795                 result = 0;
796                 break;
797         case ACPI_MADT_TYPE_IO_APIC:
798                 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
799                 result = 0;
800                 break;
801         default:
802                 break;
803         }
804  out:
805         kfree(buffer.pointer);
806         return result;
807 }
808
809 static acpi_status
810 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
811 {
812         acpi_status status;
813         unsigned long long sta;
814         acpi_handle tmp;
815         struct pci_dev *pdev;
816         u32 gsi_base;
817         u64 phys_addr;
818         struct acpiphp_ioapic *ioapic;
819
820         /* Evaluate _STA if present */
821         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
822         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
823                 return AE_CTRL_DEPTH;
824
825         /* Scan only PCI bus scope */
826         status = acpi_get_handle(handle, "_HID", &tmp);
827         if (ACPI_SUCCESS(status))
828                 return AE_CTRL_DEPTH;
829
830         if (get_gsi_base(handle, &gsi_base))
831                 return AE_OK;
832
833         ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
834         if (!ioapic)
835                 return AE_NO_MEMORY;
836
837         pdev = get_apic_pci_info(handle);
838         if (!pdev)
839                 goto exit_kfree;
840
841         if (pci_enable_device(pdev))
842                 goto exit_pci_dev_put;
843
844         pci_set_master(pdev);
845
846         if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
847                 goto exit_pci_disable_device;
848
849         phys_addr = pci_resource_start(pdev, 0);
850         if (acpi_register_ioapic(handle, phys_addr, gsi_base))
851                 goto exit_pci_release_region;
852
853         ioapic->gsi_base = gsi_base;
854         ioapic->dev = pdev;
855         spin_lock(&ioapic_list_lock);
856         list_add_tail(&ioapic->list, &ioapic_list);
857         spin_unlock(&ioapic_list_lock);
858
859         return AE_OK;
860
861  exit_pci_release_region:
862         pci_release_region(pdev, 0);
863  exit_pci_disable_device:
864         pci_disable_device(pdev);
865  exit_pci_dev_put:
866         pci_dev_put(pdev);
867  exit_kfree:
868         kfree(ioapic);
869
870         return AE_OK;
871 }
872
873 static acpi_status
874 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
875 {
876         acpi_status status;
877         unsigned long long sta;
878         acpi_handle tmp;
879         u32 gsi_base;
880         struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
881
882         /* Evaluate _STA if present */
883         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
884         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
885                 return AE_CTRL_DEPTH;
886
887         /* Scan only PCI bus scope */
888         status = acpi_get_handle(handle, "_HID", &tmp);
889         if (ACPI_SUCCESS(status))
890                 return AE_CTRL_DEPTH;
891
892         if (get_gsi_base(handle, &gsi_base))
893                 return AE_OK;
894
895         acpi_unregister_ioapic(handle, gsi_base);
896
897         spin_lock(&ioapic_list_lock);
898         list_for_each_entry_safe(pos, n, &ioapic_list, list) {
899                 if (pos->gsi_base != gsi_base)
900                         continue;
901                 ioapic = pos;
902                 list_del(&ioapic->list);
903                 break;
904         }
905         spin_unlock(&ioapic_list_lock);
906
907         if (!ioapic)
908                 return AE_OK;
909
910         pci_release_region(ioapic->dev, 0);
911         pci_disable_device(ioapic->dev);
912         pci_dev_put(ioapic->dev);
913         kfree(ioapic);
914
915         return AE_OK;
916 }
917
918 static int acpiphp_configure_ioapics(acpi_handle handle)
919 {
920         ioapic_add(handle, 0, NULL, NULL);
921         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
922                             ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
923         return 0;
924 }
925
926 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
927 {
928         ioapic_remove(handle, 0, NULL, NULL);
929         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
930                             ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
931         return 0;
932 }
933
934 static int power_on_slot(struct acpiphp_slot *slot)
935 {
936         acpi_status status;
937         struct acpiphp_func *func;
938         struct list_head *l;
939         int retval = 0;
940
941         /* if already enabled, just skip */
942         if (slot->flags & SLOT_POWEREDON)
943                 goto err_exit;
944
945         list_for_each (l, &slot->funcs) {
946                 func = list_entry(l, struct acpiphp_func, sibling);
947
948                 if (func->flags & FUNC_HAS_PS0) {
949                         dbg("%s: executing _PS0\n", __func__);
950                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
951                         if (ACPI_FAILURE(status)) {
952                                 warn("%s: _PS0 failed\n", __func__);
953                                 retval = -1;
954                                 goto err_exit;
955                         } else
956                                 break;
957                 }
958         }
959
960         /* TBD: evaluate _STA to check if the slot is enabled */
961
962         slot->flags |= SLOT_POWEREDON;
963
964  err_exit:
965         return retval;
966 }
967
968
969 static int power_off_slot(struct acpiphp_slot *slot)
970 {
971         acpi_status status;
972         struct acpiphp_func *func;
973         struct list_head *l;
974
975         int retval = 0;
976
977         /* if already disabled, just skip */
978         if ((slot->flags & SLOT_POWEREDON) == 0)
979                 goto err_exit;
980
981         list_for_each (l, &slot->funcs) {
982                 func = list_entry(l, struct acpiphp_func, sibling);
983
984                 if (func->flags & FUNC_HAS_PS3) {
985                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
986                         if (ACPI_FAILURE(status)) {
987                                 warn("%s: _PS3 failed\n", __func__);
988                                 retval = -1;
989                                 goto err_exit;
990                         } else
991                                 break;
992                 }
993         }
994
995         /* TBD: evaluate _STA to check if the slot is disabled */
996
997         slot->flags &= (~SLOT_POWEREDON);
998
999  err_exit:
1000         return retval;
1001 }
1002
1003
1004
1005 /**
1006  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
1007  * @bus: bus to start search with
1008  */
1009 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
1010 {
1011         struct list_head *tmp;
1012         unsigned char max, n;
1013
1014         /*
1015          * pci_bus_max_busnr will return the highest
1016          * reserved busnr for all these children.
1017          * that is equivalent to the bus->subordinate
1018          * value.  We don't want to use the parent's
1019          * bus->subordinate value because it could have
1020          * padding in it.
1021          */
1022         max = bus->secondary;
1023
1024         list_for_each(tmp, &bus->children) {
1025                 n = pci_bus_max_busnr(pci_bus_b(tmp));
1026                 if (n > max)
1027                         max = n;
1028         }
1029         return max;
1030 }
1031
1032
1033 /**
1034  * acpiphp_bus_add - add a new bus to acpi subsystem
1035  * @func: acpiphp_func of the bridge
1036  */
1037 static int acpiphp_bus_add(struct acpiphp_func *func)
1038 {
1039         acpi_handle phandle;
1040         struct acpi_device *device, *pdevice;
1041         int ret_val;
1042
1043         acpi_get_parent(func->handle, &phandle);
1044         if (acpi_bus_get_device(phandle, &pdevice)) {
1045                 dbg("no parent device, assuming NULL\n");
1046                 pdevice = NULL;
1047         }
1048         if (!acpi_bus_get_device(func->handle, &device)) {
1049                 dbg("bus exists... trim\n");
1050                 /* this shouldn't be in here, so remove
1051                  * the bus then re-add it...
1052                  */
1053                 ret_val = acpi_bus_trim(device, 1);
1054                 dbg("acpi_bus_trim return %x\n", ret_val);
1055         }
1056
1057         ret_val = acpi_bus_add(&device, pdevice, func->handle,
1058                 ACPI_BUS_TYPE_DEVICE);
1059         if (ret_val) {
1060                 dbg("error adding bus, %x\n",
1061                         -ret_val);
1062                 goto acpiphp_bus_add_out;
1063         }
1064         /*
1065          * try to start anyway.  We could have failed to add
1066          * simply because this bus had previously been added
1067          * on another add.  Don't bother with the return value
1068          * we just keep going.
1069          */
1070         ret_val = acpi_bus_start(device);
1071
1072 acpiphp_bus_add_out:
1073         return ret_val;
1074 }
1075
1076
1077 /**
1078  * acpiphp_bus_trim - trim a bus from acpi subsystem
1079  * @handle: handle to acpi namespace
1080  */
1081 static int acpiphp_bus_trim(acpi_handle handle)
1082 {
1083         struct acpi_device *device;
1084         int retval;
1085
1086         retval = acpi_bus_get_device(handle, &device);
1087         if (retval) {
1088                 dbg("acpi_device not found\n");
1089                 return retval;
1090         }
1091
1092         retval = acpi_bus_trim(device, 1);
1093         if (retval)
1094                 err("cannot remove from acpi list\n");
1095
1096         return retval;
1097 }
1098
1099 /**
1100  * enable_device - enable, configure a slot
1101  * @slot: slot to be enabled
1102  *
1103  * This function should be called per *physical slot*,
1104  * not per each slot object in ACPI namespace.
1105  */
1106 static int __ref enable_device(struct acpiphp_slot *slot)
1107 {
1108         struct pci_dev *dev;
1109         struct pci_bus *bus = slot->bridge->pci_bus;
1110         struct list_head *l;
1111         struct acpiphp_func *func;
1112         int retval = 0;
1113         int num, max, pass;
1114         acpi_status status;
1115
1116         if (slot->flags & SLOT_ENABLED)
1117                 goto err_exit;
1118
1119         /* sanity check: dev should be NULL when hot-plugged in */
1120         dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1121         if (dev) {
1122                 /* This case shouldn't happen */
1123                 err("pci_dev structure already exists.\n");
1124                 pci_dev_put(dev);
1125                 retval = -1;
1126                 goto err_exit;
1127         }
1128
1129         num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1130         if (num == 0) {
1131                 err("No new device found\n");
1132                 retval = -1;
1133                 goto err_exit;
1134         }
1135
1136         max = acpiphp_max_busnr(bus);
1137         for (pass = 0; pass < 2; pass++) {
1138                 list_for_each_entry(dev, &bus->devices, bus_list) {
1139                         if (PCI_SLOT(dev->devfn) != slot->device)
1140                                 continue;
1141                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1142                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1143                                 max = pci_scan_bridge(bus, dev, max, pass);
1144                                 if (pass && dev->subordinate)
1145                                         pci_bus_size_bridges(dev->subordinate);
1146                         }
1147                 }
1148         }
1149
1150         list_for_each (l, &slot->funcs) {
1151                 func = list_entry(l, struct acpiphp_func, sibling);
1152                 acpiphp_bus_add(func);
1153         }
1154
1155         pci_bus_assign_resources(bus);
1156         acpiphp_sanitize_bus(bus);
1157         acpiphp_set_hpp_values(slot->bridge->handle, bus);
1158         list_for_each_entry(func, &slot->funcs, sibling)
1159                 acpiphp_configure_ioapics(func->handle);
1160         pci_enable_bridges(bus);
1161         pci_bus_add_devices(bus);
1162
1163         /* associate pci_dev to our representation */
1164         list_for_each (l, &slot->funcs) {
1165                 func = list_entry(l, struct acpiphp_func, sibling);
1166                 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1167                                                         func->function));
1168                 if (!func->pci_dev)
1169                         continue;
1170
1171                 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1172                     func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1173                         continue;
1174
1175                 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1176                 if (ACPI_FAILURE(status))
1177                         warn("find_p2p_bridge failed (error code = 0x%x)\n",
1178                                 status);
1179         }
1180
1181         slot->flags |= SLOT_ENABLED;
1182
1183  err_exit:
1184         return retval;
1185 }
1186
1187 static void disable_bridges(struct pci_bus *bus)
1188 {
1189         struct pci_dev *dev;
1190         list_for_each_entry(dev, &bus->devices, bus_list) {
1191                 if (dev->subordinate) {
1192                         disable_bridges(dev->subordinate);
1193                         pci_disable_device(dev);
1194                 }
1195         }
1196 }
1197
1198 /**
1199  * disable_device - disable a slot
1200  * @slot: ACPI PHP slot
1201  */
1202 static int disable_device(struct acpiphp_slot *slot)
1203 {
1204         int retval = 0;
1205         struct acpiphp_func *func;
1206         struct list_head *l;
1207
1208         /* is this slot already disabled? */
1209         if (!(slot->flags & SLOT_ENABLED))
1210                 goto err_exit;
1211
1212         list_for_each (l, &slot->funcs) {
1213                 func = list_entry(l, struct acpiphp_func, sibling);
1214
1215                 if (func->bridge) {
1216                         /* cleanup p2p bridges under this P2P bridge */
1217                         cleanup_p2p_bridge(func->bridge->handle,
1218                                                 (u32)1, NULL, NULL);
1219                         func->bridge = NULL;
1220                 }
1221
1222                 if (func->pci_dev) {
1223                         pci_stop_bus_device(func->pci_dev);
1224                         if (func->pci_dev->subordinate) {
1225                                 disable_bridges(func->pci_dev->subordinate);
1226                                 pci_disable_device(func->pci_dev);
1227                         }
1228                 }
1229         }
1230
1231         list_for_each (l, &slot->funcs) {
1232                 func = list_entry(l, struct acpiphp_func, sibling);
1233
1234                 acpiphp_unconfigure_ioapics(func->handle);
1235                 acpiphp_bus_trim(func->handle);
1236                 /* try to remove anyway.
1237                  * acpiphp_bus_add might have been failed */
1238
1239                 if (!func->pci_dev)
1240                         continue;
1241
1242                 pci_remove_bus_device(func->pci_dev);
1243                 pci_dev_put(func->pci_dev);
1244                 func->pci_dev = NULL;
1245         }
1246
1247         slot->flags &= (~SLOT_ENABLED);
1248
1249  err_exit:
1250         return retval;
1251 }
1252
1253
1254 /**
1255  * get_slot_status - get ACPI slot status
1256  * @slot: ACPI PHP slot
1257  *
1258  * If a slot has _STA for each function and if any one of them
1259  * returned non-zero status, return it.
1260  *
1261  * If a slot doesn't have _STA and if any one of its functions'
1262  * configuration space is configured, return 0x0f as a _STA.
1263  *
1264  * Otherwise return 0.
1265  */
1266 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1267 {
1268         acpi_status status;
1269         unsigned long long sta = 0;
1270         u32 dvid;
1271         struct list_head *l;
1272         struct acpiphp_func *func;
1273
1274         list_for_each (l, &slot->funcs) {
1275                 func = list_entry(l, struct acpiphp_func, sibling);
1276
1277                 if (func->flags & FUNC_HAS_STA) {
1278                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1279                         if (ACPI_SUCCESS(status) && sta)
1280                                 break;
1281                 } else {
1282                         pci_bus_read_config_dword(slot->bridge->pci_bus,
1283                                                   PCI_DEVFN(slot->device,
1284                                                             func->function),
1285                                                   PCI_VENDOR_ID, &dvid);
1286                         if (dvid != 0xffffffff) {
1287                                 sta = ACPI_STA_ALL;
1288                                 break;
1289                         }
1290                 }
1291         }
1292
1293         return (unsigned int)sta;
1294 }
1295
1296 /**
1297  * acpiphp_eject_slot - physically eject the slot
1298  * @slot: ACPI PHP slot
1299  */
1300 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1301 {
1302         acpi_status status;
1303         struct acpiphp_func *func;
1304         struct list_head *l;
1305         struct acpi_object_list arg_list;
1306         union acpi_object arg;
1307
1308         list_for_each (l, &slot->funcs) {
1309                 func = list_entry(l, struct acpiphp_func, sibling);
1310
1311                 /* We don't want to call _EJ0 on non-existing functions. */
1312                 if ((func->flags & FUNC_HAS_EJ0)) {
1313                         /* _EJ0 method take one argument */
1314                         arg_list.count = 1;
1315                         arg_list.pointer = &arg;
1316                         arg.type = ACPI_TYPE_INTEGER;
1317                         arg.integer.value = 1;
1318
1319                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1320                         if (ACPI_FAILURE(status)) {
1321                                 warn("%s: _EJ0 failed\n", __func__);
1322                                 return -1;
1323                         } else
1324                                 break;
1325                 }
1326         }
1327         return 0;
1328 }
1329
1330 /**
1331  * acpiphp_check_bridge - re-enumerate devices
1332  * @bridge: where to begin re-enumeration
1333  *
1334  * Iterate over all slots under this bridge and make sure that if a
1335  * card is present they are enabled, and if not they are disabled.
1336  */
1337 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1338 {
1339         struct acpiphp_slot *slot;
1340         int retval = 0;
1341         int enabled, disabled;
1342
1343         enabled = disabled = 0;
1344
1345         for (slot = bridge->slots; slot; slot = slot->next) {
1346                 unsigned int status = get_slot_status(slot);
1347                 if (slot->flags & SLOT_ENABLED) {
1348                         if (status == ACPI_STA_ALL)
1349                                 continue;
1350                         retval = acpiphp_disable_slot(slot);
1351                         if (retval) {
1352                                 err("Error occurred in disabling\n");
1353                                 goto err_exit;
1354                         } else {
1355                                 acpiphp_eject_slot(slot);
1356                         }
1357                         disabled++;
1358                 } else {
1359                         if (status != ACPI_STA_ALL)
1360                                 continue;
1361                         retval = acpiphp_enable_slot(slot);
1362                         if (retval) {
1363                                 err("Error occurred in enabling\n");
1364                                 goto err_exit;
1365                         }
1366                         enabled++;
1367                 }
1368         }
1369
1370         dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1371
1372  err_exit:
1373         return retval;
1374 }
1375
1376 static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1377 {
1378         u16 pci_cmd, pci_bctl;
1379         struct pci_dev *cdev;
1380
1381         /* Program hpp values for this device */
1382         if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1383                         (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1384                         (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1385                 return;
1386
1387         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1388                 return;
1389
1390         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1391                         bridge->hpp.t0->cache_line_size);
1392         pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1393                         bridge->hpp.t0->latency_timer);
1394         pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1395         if (bridge->hpp.t0->enable_serr)
1396                 pci_cmd |= PCI_COMMAND_SERR;
1397         else
1398                 pci_cmd &= ~PCI_COMMAND_SERR;
1399         if (bridge->hpp.t0->enable_perr)
1400                 pci_cmd |= PCI_COMMAND_PARITY;
1401         else
1402                 pci_cmd &= ~PCI_COMMAND_PARITY;
1403         pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1404
1405         /* Program bridge control value and child devices */
1406         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1407                 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1408                                 bridge->hpp.t0->latency_timer);
1409                 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1410                 if (bridge->hpp.t0->enable_serr)
1411                         pci_bctl |= PCI_BRIDGE_CTL_SERR;
1412                 else
1413                         pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1414                 if (bridge->hpp.t0->enable_perr)
1415                         pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1416                 else
1417                         pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1418                 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1419                 if (dev->subordinate) {
1420                         list_for_each_entry(cdev, &dev->subordinate->devices,
1421                                         bus_list)
1422                                 program_hpp(cdev, bridge);
1423                 }
1424         }
1425 }
1426
1427 static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1428 {
1429         struct acpiphp_bridge bridge;
1430         struct pci_dev *dev;
1431
1432         memset(&bridge, 0, sizeof(bridge));
1433         bridge.handle = handle;
1434         bridge.pci_bus = bus;
1435         bridge.pci_dev = bus->self;
1436         decode_hpp(&bridge);
1437         list_for_each_entry(dev, &bus->devices, bus_list)
1438                 program_hpp(dev, &bridge);
1439
1440 }
1441
1442 /*
1443  * Remove devices for which we could not assign resources, call
1444  * arch specific code to fix-up the bus
1445  */
1446 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1447 {
1448         struct pci_dev *dev;
1449         int i;
1450         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1451
1452         list_for_each_entry(dev, &bus->devices, bus_list) {
1453                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1454                         struct resource *res = &dev->resource[i];
1455                         if ((res->flags & type_mask) && !res->start &&
1456                                         res->end) {
1457                                 /* Could not assign a required resources
1458                                  * for this device, remove it */
1459                                 pci_remove_bus_device(dev);
1460                                 break;
1461                         }
1462                 }
1463         }
1464 }
1465
1466 /* Program resources in newly inserted bridge */
1467 static int acpiphp_configure_bridge (acpi_handle handle)
1468 {
1469         struct acpi_pci_id pci_id;
1470         struct pci_bus *bus;
1471
1472         if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1473                 err("cannot get PCI domain and bus number for bridge\n");
1474                 return -EINVAL;
1475         }
1476         bus = pci_find_bus(pci_id.segment, pci_id.bus);
1477         if (!bus) {
1478                 err("cannot find bus %d:%d\n",
1479                                 pci_id.segment, pci_id.bus);
1480                 return -EINVAL;
1481         }
1482
1483         pci_bus_size_bridges(bus);
1484         pci_bus_assign_resources(bus);
1485         acpiphp_sanitize_bus(bus);
1486         acpiphp_set_hpp_values(handle, bus);
1487         pci_enable_bridges(bus);
1488         acpiphp_configure_ioapics(handle);
1489         return 0;
1490 }
1491
1492 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1493 {
1494         struct acpi_device *device, *pdevice;
1495         acpi_handle phandle;
1496
1497         if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1498                         (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1499                 err("unexpected notification type %d\n", type);
1500                 return;
1501         }
1502
1503         acpi_get_parent(handle, &phandle);
1504         if (acpi_bus_get_device(phandle, &pdevice)) {
1505                 dbg("no parent device, assuming NULL\n");
1506                 pdevice = NULL;
1507         }
1508         if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1509                 err("cannot add bridge to acpi list\n");
1510                 return;
1511         }
1512         if (!acpiphp_configure_bridge(handle) &&
1513                 !acpi_bus_start(device))
1514                 add_bridge(handle);
1515         else
1516                 err("cannot configure and start bridge\n");
1517
1518 }
1519
1520 /*
1521  * ACPI event handlers
1522  */
1523
1524 static acpi_status
1525 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1526 {
1527         int *count = (int *)context;
1528         struct acpiphp_bridge *bridge;
1529
1530         bridge = acpiphp_handle_to_bridge(handle);
1531         if (bridge)
1532                 (*count)++;
1533         return AE_OK ;
1534 }
1535
1536 static acpi_status
1537 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1538 {
1539         struct acpiphp_bridge *bridge;
1540         char objname[64];
1541         struct acpi_buffer buffer = { .length = sizeof(objname),
1542                                       .pointer = objname };
1543
1544         bridge = acpiphp_handle_to_bridge(handle);
1545         if (bridge) {
1546                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1547                 dbg("%s: re-enumerating slots under %s\n",
1548                         __func__, objname);
1549                 acpiphp_check_bridge(bridge);
1550         }
1551         return AE_OK ;
1552 }
1553
1554 /**
1555  * handle_hotplug_event_bridge - handle ACPI event on bridges
1556  * @handle: Notify()'ed acpi_handle
1557  * @type: Notify code
1558  * @context: pointer to acpiphp_bridge structure
1559  *
1560  * Handles ACPI event notification on {host,p2p} bridges.
1561  */
1562 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1563 {
1564         struct acpiphp_bridge *bridge;
1565         char objname[64];
1566         struct acpi_buffer buffer = { .length = sizeof(objname),
1567                                       .pointer = objname };
1568         struct acpi_device *device;
1569         int num_sub_bridges = 0;
1570
1571         if (acpi_bus_get_device(handle, &device)) {
1572                 /* This bridge must have just been physically inserted */
1573                 handle_bridge_insertion(handle, type);
1574                 return;
1575         }
1576
1577         bridge = acpiphp_handle_to_bridge(handle);
1578         if (type == ACPI_NOTIFY_BUS_CHECK) {
1579                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1580                         count_sub_bridges, &num_sub_bridges, NULL);
1581         }
1582
1583         if (!bridge && !num_sub_bridges) {
1584                 err("cannot get bridge info\n");
1585                 return;
1586         }
1587
1588         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1589
1590         switch (type) {
1591         case ACPI_NOTIFY_BUS_CHECK:
1592                 /* bus re-enumerate */
1593                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1594                 if (bridge) {
1595                         dbg("%s: re-enumerating slots under %s\n",
1596                                 __func__, objname);
1597                         acpiphp_check_bridge(bridge);
1598                 }
1599                 if (num_sub_bridges)
1600                         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1601                                 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1602                 break;
1603
1604         case ACPI_NOTIFY_DEVICE_CHECK:
1605                 /* device check */
1606                 dbg("%s: Device check notify on %s\n", __func__, objname);
1607                 acpiphp_check_bridge(bridge);
1608                 break;
1609
1610         case ACPI_NOTIFY_DEVICE_WAKE:
1611                 /* wake event */
1612                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1613                 break;
1614
1615         case ACPI_NOTIFY_EJECT_REQUEST:
1616                 /* request device eject */
1617                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1618                 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1619                     (bridge->flags & BRIDGE_HAS_EJ0)) {
1620                         struct acpiphp_slot *slot;
1621                         slot = bridge->func->slot;
1622                         if (!acpiphp_disable_slot(slot))
1623                                 acpiphp_eject_slot(slot);
1624                 }
1625                 break;
1626
1627         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1628                 printk(KERN_ERR "Device %s cannot be configured due"
1629                                 " to a frequency mismatch\n", objname);
1630                 break;
1631
1632         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1633                 printk(KERN_ERR "Device %s cannot be configured due"
1634                                 " to a bus mode mismatch\n", objname);
1635                 break;
1636
1637         case ACPI_NOTIFY_POWER_FAULT:
1638                 printk(KERN_ERR "Device %s has suffered a power fault\n",
1639                                 objname);
1640                 break;
1641
1642         default:
1643                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1644                 break;
1645         }
1646 }
1647
1648 /**
1649  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1650  * @handle: Notify()'ed acpi_handle
1651  * @type: Notify code
1652  * @context: pointer to acpiphp_func structure
1653  *
1654  * Handles ACPI event notification on slots.
1655  */
1656 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1657 {
1658         struct acpiphp_func *func;
1659         char objname[64];
1660         struct acpi_buffer buffer = { .length = sizeof(objname),
1661                                       .pointer = objname };
1662
1663         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1664
1665         func = (struct acpiphp_func *)context;
1666
1667         switch (type) {
1668         case ACPI_NOTIFY_BUS_CHECK:
1669                 /* bus re-enumerate */
1670                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1671                 acpiphp_enable_slot(func->slot);
1672                 break;
1673
1674         case ACPI_NOTIFY_DEVICE_CHECK:
1675                 /* device check : re-enumerate from parent bus */
1676                 dbg("%s: Device check notify on %s\n", __func__, objname);
1677                 acpiphp_check_bridge(func->slot->bridge);
1678                 break;
1679
1680         case ACPI_NOTIFY_DEVICE_WAKE:
1681                 /* wake event */
1682                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1683                 break;
1684
1685         case ACPI_NOTIFY_EJECT_REQUEST:
1686                 /* request device eject */
1687                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1688                 if (!(acpiphp_disable_slot(func->slot)))
1689                         acpiphp_eject_slot(func->slot);
1690                 break;
1691
1692         default:
1693                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1694                 break;
1695         }
1696 }
1697
1698
1699 static acpi_status
1700 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1701 {
1702         int *count = (int *)context;
1703
1704         if (acpi_root_bridge(handle)) {
1705                 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1706                                 handle_hotplug_event_bridge, NULL);
1707                         (*count)++;
1708         }
1709         return AE_OK ;
1710 }
1711
1712 static struct acpi_pci_driver acpi_pci_hp_driver = {
1713         .add =          add_bridge,
1714         .remove =       remove_bridge,
1715 };
1716
1717 /**
1718  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1719  */
1720 int __init acpiphp_glue_init(void)
1721 {
1722         int num = 0;
1723
1724         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1725                         ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1726
1727         if (num <= 0)
1728                 return -1;
1729         else
1730                 acpi_pci_register_driver(&acpi_pci_hp_driver);
1731
1732         return 0;
1733 }
1734
1735
1736 /**
1737  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1738  *
1739  * This function frees all data allocated in acpiphp_glue_init().
1740  */
1741 void  acpiphp_glue_exit(void)
1742 {
1743         acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1744 }
1745
1746
1747 /**
1748  * acpiphp_get_num_slots - count number of slots in a system
1749  */
1750 int __init acpiphp_get_num_slots(void)
1751 {
1752         struct acpiphp_bridge *bridge;
1753         int num_slots = 0;
1754
1755         list_for_each_entry (bridge, &bridge_list, list) {
1756                 dbg("Bus %04x:%02x has %d slot%s\n",
1757                                 pci_domain_nr(bridge->pci_bus),
1758                                 bridge->pci_bus->number, bridge->nr_slots,
1759                                 bridge->nr_slots == 1 ? "" : "s");
1760                 num_slots += bridge->nr_slots;
1761         }
1762
1763         dbg("Total %d slots\n", num_slots);
1764         return num_slots;
1765 }
1766
1767
1768 #if 0
1769 /**
1770  * acpiphp_for_each_slot - call function for each slot
1771  * @fn: callback function
1772  * @data: context to be passed to callback function
1773  */
1774 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1775 {
1776         struct list_head *node;
1777         struct acpiphp_bridge *bridge;
1778         struct acpiphp_slot *slot;
1779         int retval = 0;
1780
1781         list_for_each (node, &bridge_list) {
1782                 bridge = (struct acpiphp_bridge *)node;
1783                 for (slot = bridge->slots; slot; slot = slot->next) {
1784                         retval = fn(slot, data);
1785                         if (!retval)
1786                                 goto err_exit;
1787                 }
1788         }
1789
1790  err_exit:
1791         return retval;
1792 }
1793 #endif
1794
1795
1796 /**
1797  * acpiphp_enable_slot - power on slot
1798  * @slot: ACPI PHP slot
1799  */
1800 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1801 {
1802         int retval;
1803
1804         mutex_lock(&slot->crit_sect);
1805
1806         /* wake up all functions */
1807         retval = power_on_slot(slot);
1808         if (retval)
1809                 goto err_exit;
1810
1811         if (get_slot_status(slot) == ACPI_STA_ALL) {
1812                 /* configure all functions */
1813                 retval = enable_device(slot);
1814                 if (retval)
1815                         power_off_slot(slot);
1816         } else {
1817                 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1818                 power_off_slot(slot);
1819         }
1820
1821  err_exit:
1822         mutex_unlock(&slot->crit_sect);
1823         return retval;
1824 }
1825
1826 /**
1827  * acpiphp_disable_slot - power off slot
1828  * @slot: ACPI PHP slot
1829  */
1830 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1831 {
1832         int retval = 0;
1833
1834         mutex_lock(&slot->crit_sect);
1835
1836         /* unconfigure all functions */
1837         retval = disable_device(slot);
1838         if (retval)
1839                 goto err_exit;
1840
1841         /* power off all functions */
1842         retval = power_off_slot(slot);
1843         if (retval)
1844                 goto err_exit;
1845
1846  err_exit:
1847         mutex_unlock(&slot->crit_sect);
1848         return retval;
1849 }
1850
1851
1852 /*
1853  * slot enabled:  1
1854  * slot disabled: 0
1855  */
1856 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1857 {
1858         return (slot->flags & SLOT_POWEREDON);
1859 }
1860
1861
1862 /*
1863  * latch   open:  1
1864  * latch closed:  0
1865  */
1866 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1867 {
1868         unsigned int sta;
1869
1870         sta = get_slot_status(slot);
1871
1872         return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1873 }
1874
1875
1876 /*
1877  * adapter presence : 1
1878  *          absence : 0
1879  */
1880 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1881 {
1882         unsigned int sta;
1883
1884         sta = get_slot_status(slot);
1885
1886         return (sta == 0) ? 0 : 1;
1887 }