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