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