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