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