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