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